5 use File::Temp qw(tempdir);
7 use File::Basename qw(basename dirname);
11 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);
13 getopts('uhPpvcfam:d:w:W');
17 die "Need at least one commit identifier!" unless @ARGV;
19 # Get git-config settings
20 my $repo = Git->repository();
21 $opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w;
23 if ($opt_w || $opt_W) {
24 # Remember where GIT_DIR is before changing to CVS checkout
25 unless ($ENV{GIT_DIR}) {
26 # No GIT_DIR set. Figure it out for ourselves
27 my $gd =`git-rev-parse --git-dir`;
31 # Make sure GIT_DIR is absolute
32 $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR});
36 if (! -d $opt_w."/CVS" ) {
37 die "$opt_w is not a CVS checkout";
39 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
41 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
42 die "GIT_DIR is not defined or is unreadable";
48 @cvs = ('cvs', '-d', $opt_d);
53 # resolve target commit
56 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
59 die "The commit reference $commit did not resolve!";
62 # resolve what parent we want
66 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
69 die "The parent reference did not resolve!";
73 # find parents from the commit itself
74 my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
78 my $stage = 'headers'; # headers, msg
82 foreach my $line (@commit) {
84 if ($stage eq 'headers' && $line eq '') {
89 if ($stage eq 'headers') {
90 if ($line =~ m/^parent (\w{40})$/) { # found a parent
92 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
94 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
105 my $noparent = "0000000000000000000000000000000000000000";
108 # double check that it's a valid parent
109 foreach my $p (@parents) {
115 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
116 } else { # we don't have a parent from the cmdline...
117 if (@parents == 1) { # it's safe to get it from the commit
118 $parent = $parents[0];
119 } elsif (@parents == 0) { # there is no parent
121 } else { # cannot choose automatically from multiple parents
122 die "This commit has more than one parent -- please name the parent you want to use explicitly";
129 $opt_v && print "Resetting to $parent\n";
130 $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||
131 git rev-parse HEAD` || die "Could not determine current branch";
132 system("git checkout -q $parent^0") && die "Could not check out $parent^0";
135 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
137 # grab the commit message
138 open(MSG, ">.msg") or die "Cannot open .msg for writing";
144 print MSG "\n\nAuthor: $author\n";
145 if ($author ne $committer) {
146 print MSG "Committer: $committer\n";
151 if ($parent eq $noparent) {
152 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
154 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
157 ## apply non-binary changes
159 # In pedantic mode require all lines of context to match. In normal
160 # mode, be compatible with diff/patch: assume 3 lines of context and
161 # require at least one line match, i.e. ignore at most 2 lines of
162 # context, like diff/patch do by default.
163 my $context = $opt_p ? '' : '-C1';
165 print "Checking if patch will apply\n";
168 open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
170 close APPLY || die "Cannot patch";
171 my (@bfiles,@files,@afiles,@dfiles);
174 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
175 push (@files, $1) if m/^-\t-\t(.*)$/;
176 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
177 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
178 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
180 map { s/^"(.*)"$/$1/g } @bfiles,@files;
181 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
183 # check that the files are clean and up to date according to cvs
186 foreach my $p (@afiles) {
187 my $path = dirname $p;
188 while (!-d $path and ! grep { $_ eq $path } @dirs) {
189 unshift @dirs, $path;
190 $path = dirname $path;
195 foreach my $d (@dirs) {
198 warn "$d exists and is not a directory!\n";
202 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
204 foreach my $f (@files) {
205 my $path = dirname $f;
206 next if (grep { $_ eq $path } @dirs);
207 push @canstatusfiles, $f;
211 if (@canstatusfiles) {
213 my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles);
216 # "cvs status" reorders the parameters, notably when there are multiple
217 # arguments with the same basename. So be precise here.
219 my %added = map { $_ => 1 } @afiles;
220 my %todo = map { $_ => 1 } @canstatusfiles;
223 my @canstatusfiles2 = ();
225 foreach my $name (keys %todo) {
226 my $basename = basename($name);
228 $basename = "no file " . $basename if (exists($added{$basename}));
229 $basename =~ s/^\s+//;
230 $basename =~ s/\s+$//;
232 if (!exists($fullname{$basename})) {
233 $fullname{$basename} = $name;
234 push (@canstatusfiles2, $name);
235 delete($todo{$name});
239 @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2);
240 foreach my $l (@cvsoutput) {
242 if ($l =~ /^File:\s+(.*\S)\s+Status: (.*)$/) {
243 if (!exists($fullname{$1})) {
244 print STDERR "Huh? Status reported for unexpected file '$1'\n";
246 $cvsstat{$fullname{$1}} = $2;
253 # ... validate new files,
254 foreach my $f (@afiles) {
255 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
257 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";
258 warn "Status was: $cvsstat{$f}\n";
261 # ... validate known files.
262 foreach my $f (@files) {
263 next if grep { $_ eq $f } @afiles;
264 # TODO:we need to handle removed in cvs
265 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
267 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
271 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
274 die "Exiting: your CVS tree is not clean for this merge.";
280 system("git checkout -q $commit^0") && die "cannot patch";
282 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
285 print "Patch applied successfully. Adding new files and directories to CVS\n";
289 # We have to add the directories in order otherwise we will have
290 # problems when we try and add the sub-directory of a directory we
291 # have not added yet.
293 # Luckily this is easy to deal with by sorting the directories and
294 # dealing with the shortest ones first.
296 @dirs = sort { length $a <=> length $b} @dirs;
298 foreach my $d (@dirs) {
299 if (system(@cvs,'add',$d)) {
301 warn "Failed to cvs add directory $d -- you may need to do it manually";
305 foreach my $f (@afiles) {
306 if (grep { $_ eq $f } @bfiles) {
307 system(@cvs, 'add','-kb',$f);
309 system(@cvs, 'add', $f);
313 warn "Failed to cvs add $f -- you may need to do it manually";
317 foreach my $f (@dfiles) {
318 system(@cvs, 'rm', '-f', $f);
321 warn "Failed to cvs rm -f $f -- you may need to do it manually";
325 print "Commit to CVS\n";
326 print "Patch title (first comment line): $title\n";
327 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
328 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
331 print "NOTE: One or more hunks failed to apply cleanly.\n";
332 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
333 print "using a patch program. After applying the patch and resolving the\n";
334 print "problems you may commit using:";
335 print "\n cd \"$opt_w\"" if $opt_w;
337 print "\n git checkout $go_back_to\n" if $go_back_to;
343 print "Autocommit\n $cmd\n";
344 print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files);
346 die "Exiting: The commit did not succeed";
348 print "Committed successfully to CVS\n";
352 print "Ready for you to commit, just run:\n\n $cmd\n";
356 unlink(".cvsexportcommit.diff");
359 system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
360 if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) {
361 system("git symbolic-ref HEAD $go_back_to") &&
362 die "cannot move back to $go_back_to";
366 # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
367 # used by CVS and the one set by subsequence file modifications are different.
368 # If they are not different CVS will not detect changes.
373 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
378 # An alternative to `command` that allows input to be passed as an array
379 # to work around shell problems with weird characters in arguments
380 # if the exec returns non-zero we die
381 sub safe_pipe_capture {
383 if (my $pid = open my $child, '-|') {
384 @output = (<$child>);
385 close $child or die join(' ',@_).": $! $?";
387 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
389 return wantarray ? @output : join('',@output);
392 sub xargs_safe_pipe_capture {
393 my $MAX_ARG_LENGTH = 65536;
400 while(@_ && $length < $MAX_ARG_LENGTH) {
402 $length += length($args[$#args]);
405 push @output, safe_pipe_capture(@$cmd, @args);
408 $output .= safe_pipe_capture(@$cmd, @args);
411 return wantarray ? @output : $output;