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