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