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