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