5 use File
::Temp
qw(tempdir);
7 use File
::Basename
qw(basename dirname);
10 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w);
12 getopts
('uhPpvcfam:d:w:');
16 die "Need at least one commit identifier!" unless @ARGV;
19 # Remember where GIT_DIR is before changing to CVS checkout
20 unless ($ENV{GIT_DIR
}) {
21 # No GIT_DIR set. Figure it out for ourselves
22 my $gd =`git-rev-parse --git-dir`;
26 # Make sure GIT_DIR is absolute
27 $ENV{GIT_DIR
} = File
::Spec
->rel2abs($ENV{GIT_DIR
});
29 if (! -d
$opt_w."/CVS" ) {
30 die "$opt_w is not a CVS checkout";
32 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
34 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
35 die "GIT_DIR is not defined or is unreadable";
41 @cvs = ('cvs', '-d', $opt_d);
46 # resolve target commit
49 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
52 die "The commit reference $commit did not resolve!";
55 # resolve what parent we want
59 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
62 die "The parent reference did not resolve!";
66 # find parents from the commit itself
67 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
71 my $stage = 'headers'; # headers, msg
75 foreach my $line (@commit) {
77 if ($stage eq 'headers' && $line eq '') {
82 if ($stage eq 'headers') {
83 if ($line =~ m/^parent (\w{40})$/) { # found a parent
85 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
87 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
98 my $noparent = "0000000000000000000000000000000000000000";
101 # double check that it's a valid parent
102 foreach my $p (@parents) {
108 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
109 } else { # we don't have a parent from the cmdline...
110 if (@parents == 1) { # it's safe to get it from the commit
111 $parent = $parents[0];
112 } elsif (@parents == 0) { # there is no parent
114 } else { # cannot choose automatically from multiple parents
115 die "This commit has more than one parent -- please name the parent you want to use explicitly";
119 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
121 # grab the commit message
122 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
128 print MSG
"\n\nAuthor: $author\n";
129 if ($author ne $committer) {
130 print MSG
"Committer: $committer\n";
135 if ($parent eq $noparent) {
136 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
138 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
141 ## apply non-binary changes
143 # In pedantic mode require all lines of context to match. In normal
144 # mode, be compatible with diff/patch: assume 3 lines of context and
145 # require at least one line match, i.e. ignore at most 2 lines of
146 # context, like diff/patch do by default.
147 my $context = $opt_p ?
'' : '-C1';
149 print "Checking if patch will apply\n";
152 open APPLY
, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
154 close APPLY
|| die "Cannot patch";
155 my (@bfiles,@files,@afiles,@dfiles);
158 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
159 push (@files, $1) if m/^-\t-\t(.*)$/;
160 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
161 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
162 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
164 map { s/^"(.*)"$/$1/g } @bfiles,@files;
165 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
167 # check that the files are clean and up to date according to cvs
170 foreach my $p (@afiles) {
171 my $path = dirname
$p;
172 while (!-d
$path and ! grep { $_ eq $path } @dirs) {
173 unshift @dirs, $path;
174 $path = dirname
$path;
179 foreach my $d (@dirs) {
182 warn "$d exists and is not a directory!\n";
186 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
188 foreach my $f (@files) {
189 my $path = dirname
$f;
190 next if (grep { $_ eq $path } @dirs);
191 push @canstatusfiles, $f;
195 if (@canstatusfiles) {
197 my @updated = xargs_safe_pipe_capture
([@cvs, 'update'], @canstatusfiles);
201 @cvsoutput = xargs_safe_pipe_capture
([@cvs, 'status'], @canstatusfiles);
203 foreach my $l (@cvsoutput) {
205 if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) {
206 $cvsstat{$canstatusfiles[$matchcount]} = $1;
212 # ... validate new files,
213 foreach my $f (@afiles) {
214 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
216 warn "File $f is already known in your CVS checkout -- perhaps it has been added by another user. Or this may indicate that it exists on a different branch. If this is the case, use -f to force the merge.\n";
217 warn "Status was: $cvsstat{$f}\n";
220 # ... validate known files.
221 foreach my $f (@files) {
222 next if grep { $_ eq $f } @afiles;
223 # TODO:we need to handle removed in cvs
224 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
226 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
230 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
233 die "Exiting: your CVS tree is not clean for this merge.";
238 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
240 print "Patch applied successfully. Adding new files and directories to CVS\n";
244 # We have to add the directories in order otherwise we will have
245 # problems when we try and add the sub-directory of a directory we
246 # have not added yet.
248 # Luckily this is easy to deal with by sorting the directories and
249 # dealing with the shortest ones first.
251 @dirs = sort { length $a <=> length $b} @dirs;
253 foreach my $d (@dirs) {
254 if (system(@cvs,'add',$d)) {
256 warn "Failed to cvs add directory $d -- you may need to do it manually";
260 foreach my $f (@afiles) {
261 if (grep { $_ eq $f } @bfiles) {
262 system(@cvs, 'add','-kb',$f);
264 system(@cvs, 'add', $f);
268 warn "Failed to cvs add $f -- you may need to do it manually";
272 foreach my $f (@dfiles) {
273 system(@cvs, 'rm', '-f', $f);
276 warn "Failed to cvs rm -f $f -- you may need to do it manually";
280 print "Commit to CVS\n";
281 print "Patch title (first comment line): $title\n";
282 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
283 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
286 print "NOTE: One or more hunks failed to apply cleanly.\n";
287 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
288 print "using a patch program. After applying the patch and resolving the\n";
289 print "problems you may commit using:";
290 print "\n cd \"$opt_w\"" if $opt_w;
296 print "Autocommit\n $cmd\n";
297 print xargs_safe_pipe_capture
([@cvs, 'commit', '-F', '.msg'], @files);
299 die "Exiting: The commit did not succeed";
301 print "Committed successfully to CVS\n";
305 print "Ready for you to commit, just run:\n\n $cmd\n";
309 unlink(".cvsexportcommit.diff");
311 # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
312 # used by CVS and the one set by subsequence file modifications are different.
313 # If they are not different CVS will not detect changes.
318 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
323 # An alternative to `command` that allows input to be passed as an array
324 # to work around shell problems with weird characters in arguments
325 # if the exec returns non-zero we die
326 sub safe_pipe_capture
{
328 if (my $pid = open my $child, '-|') {
329 @output = (<$child>);
330 close $child or die join(' ',@_).": $! $?";
332 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
334 return wantarray ?
@output : join('',@output);
337 sub xargs_safe_pipe_capture
{
338 my $MAX_ARG_LENGTH = 65536;
345 while(@_ && $length < $MAX_ARG_LENGTH) {
347 $length += length($args[$#args]);
350 push @output, safe_pipe_capture
(@
$cmd, @args);
353 $output .= safe_pipe_capture
(@
$cmd, @args);
356 return wantarray ?
@output : $output;