5 use File::Temp qw(tempdir);
7 use File::Basename qw(basename dirname);
9 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w);
11 getopts('uhPpvcfam:d:w:');
15 die "Need at least one commit identifier!" unless @ARGV;
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`;
30 if (! -d $opt_w."/CVS" ) {
31 die "$opt_w is not a CVS checkout";
33 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
35 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
36 die "GIT_DIR is not defined or is unreadable";
42 @cvs = ('cvs', '-d', $opt_d);
47 # resolve target commit
50 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
53 die "The commit reference $commit did not resolve!";
56 # resolve what parent we want
60 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
63 die "The parent reference did not resolve!";
67 # find parents from the commit itself
68 my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
72 my $stage = 'headers'; # headers, msg
76 foreach my $line (@commit) {
78 if ($stage eq 'headers' && $line eq '') {
83 if ($stage eq 'headers') {
84 if ($line =~ m/^parent (\w{40})$/) { # found a parent
86 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
88 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
99 my $noparent = "0000000000000000000000000000000000000000";
102 # double check that it's a valid parent
103 foreach my $p (@parents) {
109 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
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];
113 } elsif (@parents == 0) { # there is no parent
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";
120 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
122 # grab the commit message
123 open(MSG, ">.msg") or die "Cannot open .msg for writing";
129 print MSG "\n\nAuthor: $author\n";
130 if ($author ne $committer) {
131 print MSG "Committer: $committer\n";
136 if ($parent eq $noparent) {
137 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
139 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
142 ## apply non-binary changes
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';
150 print "Checking if patch will apply\n";
153 open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
155 close APPLY || die "Cannot patch";
156 my (@bfiles,@files,@afiles,@dfiles);
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]+ (.*)$/;
165 map { s/^"(.*)"$/$1/g } @bfiles,@files;
166 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
168 # check that the files are clean and up to date according to cvs
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;
180 foreach my $d (@dirs) {
183 warn "$d exists and is not a directory!\n";
187 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
189 foreach my $f (@files) {
190 my $path = dirname $f;
191 next if (grep { $_ eq $path } @dirs);
192 push @canstatusfiles, $f;
196 if (@canstatusfiles) {
198 my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles);
201 # "cvs status" reorders the parameters, notably when there are multiple
202 # arguments with the same basename. So be precise here.
204 my %added = map { $_ => 1 } @afiles;
205 my %todo = map { $_ => 1 } @canstatusfiles;
208 my @canstatusfiles2 = ();
210 foreach my $name (keys %todo) {
211 my $basename = basename($name);
213 $basename = "no file " . $basename if (exists($added{$basename}));
216 if (!exists($fullname{$basename})) {
217 $fullname{$basename} = $name;
218 push (@canstatusfiles2, $name);
219 delete($todo{$name});
223 @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2);
224 foreach my $l (@cvsoutput) {
226 if ($l =~ /^File:\s+(.*\S)\s+Status: (.*)$/) {
227 if (!exists($fullname{$1})) {
228 print STDERR "Huh? Status reported for unexpected file '$1'\n";
230 $cvsstat{$fullname{$1}} = $2;
237 # ... validate new files,
238 foreach my $f (@afiles) {
239 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
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";
242 warn "Status was: $cvsstat{$f}\n";
245 # ... validate known files.
246 foreach my $f (@files) {
247 next if grep { $_ eq $f } @afiles;
248 # TODO:we need to handle removed in cvs
249 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
251 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
255 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
258 die "Exiting: your CVS tree is not clean for this merge.";
263 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
265 print "Patch applied successfully. Adding new files and directories to CVS\n";
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.
273 # Luckily this is easy to deal with by sorting the directories and
274 # dealing with the shortest ones first.
276 @dirs = sort { length $a <=> length $b} @dirs;
278 foreach my $d (@dirs) {
279 if (system(@cvs,'add',$d)) {
281 warn "Failed to cvs add directory $d -- you may need to do it manually";
285 foreach my $f (@afiles) {
286 if (grep { $_ eq $f } @bfiles) {
287 system(@cvs, 'add','-kb',$f);
289 system(@cvs, 'add', $f);
293 warn "Failed to cvs add $f -- you may need to do it manually";
297 foreach my $f (@dfiles) {
298 system(@cvs, 'rm', '-f', $f);
301 warn "Failed to cvs rm -f $f -- you may need to do it manually";
305 print "Commit to CVS\n";
306 print "Patch title (first comment line): $title\n";
307 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
308 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
311 print "NOTE: One or more hunks failed to apply cleanly.\n";
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:";
315 print "\n cd \"$opt_w\"" if $opt_w;
321 print "Autocommit\n $cmd\n";
322 print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files);
324 die "Exiting: The commit did not succeed";
326 print "Committed successfully to CVS\n";
330 print "Ready for you to commit, just run:\n\n $cmd\n";
334 unlink(".cvsexportcommit.diff");
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.
343 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
348 # An alternative to `command` that allows input to be passed as an array
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 {
353 if (my $pid = open my $child, '-|') {
354 @output = (<$child>);
355 close $child or die join(' ',@_).": $! $?";
357 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
359 return wantarray ? @output : join('',@output);
362 sub xargs_safe_pipe_capture {
363 my $MAX_ARG_LENGTH = 65536;
370 while(@_ && $length < $MAX_ARG_LENGTH) {
372 $length += length($args[$#args]);
375 push @output, safe_pipe_capture(@$cmd, @args);
378 $output .= safe_pipe_capture(@$cmd, @args);
381 return wantarray ? @output : $output;