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