git-cvsexportcommit can't commit files which have been removed from CVS
[git] / git-cvsexportcommit.perl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use File::Temp qw(tempdir);
6 use Data::Dumper;
7 use File::Basename qw(basename dirname);
8 use File::Spec;
9 use Git;
10
11 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w, $opt_W);
12
13 getopts('uhPpvcfam:d:w:W');
14
15 $opt_h && usage();
16
17 die "Need at least one commit identifier!" unless @ARGV;
18
19 # Get git-config settings
20 my $repo = Git->repository();
21 $opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w;
22
23 if ($opt_w || $opt_W) {
24         # Remember where GIT_DIR is before changing to CVS checkout
25         unless ($ENV{GIT_DIR}) {
26                 # No GIT_DIR set. Figure it out for ourselves
27                 my $gd =`git-rev-parse --git-dir`;
28                 chomp($gd);
29                 $ENV{GIT_DIR} = $gd;
30         }
31         # Make sure GIT_DIR is absolute
32         $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR});
33 }
34
35 if ($opt_w) {
36         if (! -d $opt_w."/CVS" ) {
37                 die "$opt_w is not a CVS checkout";
38         }
39         chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
40 }
41 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
42     die "GIT_DIR is not defined or is unreadable";
43 }
44
45
46 my @cvs;
47 if ($opt_d) {
48         @cvs = ('cvs', '-d', $opt_d);
49 } else {
50         @cvs = ('cvs');
51 }
52
53 # resolve target commit
54 my $commit;
55 $commit = pop @ARGV;
56 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
57 chomp $commit;
58 if ($?) {
59     die "The commit reference $commit did not resolve!";
60 }
61
62 # resolve what parent we want
63 my $parent;
64 if (@ARGV) {
65     $parent = pop @ARGV;
66     $parent =  safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
67     chomp $parent;
68     if ($?) {
69         die "The parent reference did not resolve!";
70     }
71 }
72
73 # find parents from the commit itself
74 my @commit  = safe_pipe_capture('git-cat-file', 'commit', $commit);
75 my @parents;
76 my $committer;
77 my $author;
78 my $stage = 'headers'; # headers, msg
79 my $title;
80 my $msg = '';
81
82 foreach my $line (@commit) {
83     chomp $line;
84     if ($stage eq 'headers' && $line eq '') {
85         $stage = 'msg';
86         next;
87     }
88
89     if ($stage eq 'headers') {
90         if ($line =~ m/^parent (\w{40})$/) { # found a parent
91             push @parents, $1;
92         } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
93             $author = $1;
94         } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
95             $committer = $1;
96         }
97     } else {
98         $msg .= $line . "\n";
99         unless ($title) {
100             $title = $line;
101         }
102     }
103 }
104
105 my $noparent = "0000000000000000000000000000000000000000";
106 if ($parent) {
107     my $found;
108     # double check that it's a valid parent
109     foreach my $p (@parents) {
110         if ($p eq $parent) {
111             $found = 1;
112             last;
113         }; # found it
114     }
115     die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
116 } else { # we don't have a parent from the cmdline...
117     if (@parents == 1) { # it's safe to get it from the commit
118         $parent = $parents[0];
119     } elsif (@parents == 0) { # there is no parent
120         $parent = $noparent;
121     } else { # cannot choose automatically from multiple parents
122         die "This commit has more than one parent -- please name the parent you want to use explicitly";
123     }
124 }
125
126 my $go_back_to = 0;
127
128 if ($opt_W) {
129     $opt_v && print "Resetting to $parent\n";
130     $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||
131         git rev-parse HEAD` || die "Could not determine current branch";
132     system("git checkout -q $parent^0") && die "Could not check out $parent^0";
133 }
134
135 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
136
137 # grab the commit message
138 open(MSG, ">.msg") or die "Cannot open .msg for writing";
139 if ($opt_m) {
140     print MSG $opt_m;
141 }
142 print MSG $msg;
143 if ($opt_a) {
144     print MSG "\n\nAuthor: $author\n";
145     if ($author ne $committer) {
146         print MSG "Committer: $committer\n";
147     }
148 }
149 close MSG;
150
151 if ($parent eq $noparent) {
152     `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
153 } else {
154     `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
155 }
156
157 ## apply non-binary changes
158
159 # In pedantic mode require all lines of context to match.  In normal
160 # mode, be compatible with diff/patch: assume 3 lines of context and
161 # require at least one line match, i.e. ignore at most 2 lines of
162 # context, like diff/patch do by default.
163 my $context = $opt_p ? '' : '-C1';
164
165 print "Checking if patch will apply\n";
166
167 my @stat;
168 open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
169 @stat=<APPLY>;
170 close APPLY || die "Cannot patch";
171 my (@bfiles,@files,@afiles,@dfiles);
172 chomp @stat;
173 foreach (@stat) {
174         push (@bfiles,$1) if m/^-\t-\t(.*)$/;
175         push (@files, $1) if m/^-\t-\t(.*)$/;
176         push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
177         push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
178         push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
179 }
180 map { s/^"(.*)"$/$1/g } @bfiles,@files;
181 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
182
183 # check that the files are clean and up to date according to cvs
184 my $dirty;
185 my @dirs;
186 foreach my $p (@afiles) {
187     my $path = dirname $p;
188     while (!-d $path and ! grep { $_ eq $path } @dirs) {
189         unshift @dirs, $path;
190         $path = dirname $path;
191     }
192 }
193
194 # ... check dirs,
195 foreach my $d (@dirs) {
196     if (-e $d) {
197         $dirty = 1;
198         warn "$d exists and is not a directory!\n";
199     }
200 }
201
202 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
203 my @canstatusfiles;
204 foreach my $f (@files) {
205     my $path = dirname $f;
206     next if (grep { $_ eq $path } @dirs);
207     push @canstatusfiles, $f;
208 }
209
210 my %cvsstat;
211 if (@canstatusfiles) {
212     if ($opt_u) {
213       my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles);
214       print @updated;
215     }
216     # "cvs status" reorders the parameters, notably when there are multiple
217     # arguments with the same basename.  So be precise here.
218
219     my %added = map { $_ => 1 } @afiles;
220     my %todo = map { $_ => 1 } @canstatusfiles;
221
222     while (%todo) {
223       my @canstatusfiles2 = ();
224       my %fullname = ();
225       foreach my $name (keys %todo) {
226         my $basename = basename($name);
227
228         # CVS reports files that don't exist in the current revision as
229         # "no file $basename" in its "status" output, so we should
230         # anticipate that.  Totally unknown files will have a status
231         # "Unknown". However, if they exist in the Attic, their status
232         # will be "Up-to-date" (this means they were added once but have
233         # been removed).
234         $basename = "no file $basename" if $added{$basename};
235
236         $basename =~ s/^\s+//;
237         $basename =~ s/\s+$//;
238
239         if (!exists($fullname{$basename})) {
240           $fullname{$basename} = $name;
241           push (@canstatusfiles2, $name);
242           delete($todo{$name});
243         }
244       }
245       my @cvsoutput;
246       @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2);
247       foreach my $l (@cvsoutput) {
248         chomp $l;
249         next unless
250             my ($file, $status) = $l =~ /^File:\s+(.*\S)\s+Status: (.*)$/;
251
252         my $fullname = $fullname{$file};
253         print STDERR "Huh? Status '$status' reported for unexpected file '$file'\n"
254             unless defined $fullname;
255
256         # This response means the file does not exist except in
257         # CVS's attic, so set the status accordingly
258         $status = "In-attic"
259             if $file =~ /^no file /
260                 && $status eq 'Up-to-date';
261
262         $cvsstat{$fullname{$file}} = $status;
263       }
264     }
265 }
266
267 # ... Validate that new files have the correct status
268 foreach my $f (@afiles) {
269     next unless defined(my $stat = $cvsstat{$f});
270
271     # This means the file has never been seen before
272     next if $stat eq 'Unknown';
273
274     # This means the file has been seen before but was removed
275     next if $stat eq 'In-attic';
276
277     $dirty = 1;
278         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";
279         warn "Status was: $cvsstat{$f}\n";
280 }
281
282 # ... validate known files.
283 foreach my $f (@files) {
284     next if grep { $_ eq $f } @afiles;
285     # TODO:we need to handle removed in cvs
286     unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
287         $dirty = 1;
288         warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
289     }
290 }
291 if ($dirty) {
292     if ($opt_f) {       warn "The tree is not clean -- forced merge\n";
293         $dirty = 0;
294     } else {
295         die "Exiting: your CVS tree is not clean for this merge.";
296     }
297 }
298
299 print "Applying\n";
300 if ($opt_W) {
301     system("git checkout -q $commit^0") && die "cannot patch";
302 } else {
303     `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
304 }
305
306 print "Patch applied successfully. Adding new files and directories to CVS\n";
307 my $dirtypatch = 0;
308
309 #
310 # We have to add the directories in order otherwise we will have
311 # problems when we try and add the sub-directory of a directory we
312 # have not added yet.
313 #
314 # Luckily this is easy to deal with by sorting the directories and
315 # dealing with the shortest ones first.
316 #
317 @dirs = sort { length $a <=> length $b} @dirs;
318
319 foreach my $d (@dirs) {
320     if (system(@cvs,'add',$d)) {
321         $dirtypatch = 1;
322         warn "Failed to cvs add directory $d -- you may need to do it manually";
323     }
324 }
325
326 foreach my $f (@afiles) {
327     if (grep { $_ eq $f } @bfiles) {
328       system(@cvs, 'add','-kb',$f);
329     } else {
330       system(@cvs, 'add', $f);
331     }
332     if ($?) {
333         $dirtypatch = 1;
334         warn "Failed to cvs add $f -- you may need to do it manually";
335     }
336 }
337
338 foreach my $f (@dfiles) {
339     system(@cvs, 'rm', '-f', $f);
340     if ($?) {
341         $dirtypatch = 1;
342         warn "Failed to cvs rm -f $f -- you may need to do it manually";
343     }
344 }
345
346 print "Commit to CVS\n";
347 print "Patch title (first comment line): $title\n";
348 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
349 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
350
351 if ($dirtypatch) {
352     print "NOTE: One or more hunks failed to apply cleanly.\n";
353     print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
354     print "using a patch program. After applying the patch and resolving the\n";
355     print "problems you may commit using:";
356     print "\n    cd \"$opt_w\"" if $opt_w;
357     print "\n    $cmd\n";
358     print "\n    git checkout $go_back_to\n" if $go_back_to;
359     print "\n";
360     exit(1);
361 }
362
363 if ($opt_c) {
364     print "Autocommit\n  $cmd\n";
365     print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files);
366     if ($?) {
367         die "Exiting: The commit did not succeed";
368     }
369     print "Committed successfully to CVS\n";
370     # clean up
371     unlink(".msg");
372 } else {
373     print "Ready for you to commit, just run:\n\n   $cmd\n";
374 }
375
376 # clean up
377 unlink(".cvsexportcommit.diff");
378
379 if ($opt_W) {
380     system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
381     if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) {
382         system("git symbolic-ref HEAD $go_back_to") &&
383             die "cannot move back to $go_back_to";
384     }
385 }
386
387 # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
388 # used by CVS and the one set by subsequence file modifications are different.
389 # If they are not different CVS will not detect changes.
390 sleep(1);
391
392 sub usage {
393         print STDERR <<END;
394 Usage: GIT_DIR=/path/to/.git git cvsexportcommit [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
395 END
396         exit(1);
397 }
398
399 # An alternative to `command` that allows input to be passed as an array
400 # to work around shell problems with weird characters in arguments
401 # if the exec returns non-zero we die
402 sub safe_pipe_capture {
403     my @output;
404     if (my $pid = open my $child, '-|') {
405         @output = (<$child>);
406         close $child or die join(' ',@_).": $! $?";
407     } else {
408         exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
409     }
410     return wantarray ? @output : join('',@output);
411 }
412
413 sub xargs_safe_pipe_capture {
414         my $MAX_ARG_LENGTH = 65536;
415         my $cmd = shift;
416         my @output;
417         my $output;
418         while(@_) {
419                 my @args;
420                 my $length = 0;
421                 while(@_ && $length < $MAX_ARG_LENGTH) {
422                         push @args, shift;
423                         $length += length($args[$#args]);
424                 }
425                 if (wantarray) {
426                         push @output, safe_pipe_capture(@$cmd, @args);
427                 }
428                 else {
429                         $output .= safe_pipe_capture(@$cmd, @args);
430                 }
431         }
432         return wantarray ? @output : $output;
433 }