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