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);
13 getopts
('uhPpvcfam:d: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;
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
});
34 if (! -d
$opt_w."/CVS" ) {
35 die "$opt_w is not a CVS checkout";
37 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
39 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
40 die "GIT_DIR is not defined or is unreadable";
46 @cvs = ('cvs', '-d', $opt_d);
51 # resolve target commit
54 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
57 die "The commit reference $commit did not resolve!";
60 # resolve what parent we want
64 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
67 die "The parent reference did not resolve!";
71 # find parents from the commit itself
72 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
76 my $stage = 'headers'; # headers, msg
80 foreach my $line (@commit) {
82 if ($stage eq 'headers' && $line eq '') {
87 if ($stage eq 'headers') {
88 if ($line =~ m/^parent (\w{40})$/) { # found a parent
90 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
92 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
103 my $noparent = "0000000000000000000000000000000000000000";
106 # double check that it's a valid parent
107 foreach my $p (@parents) {
113 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
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];
117 } elsif (@parents == 0) { # there is no parent
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";
124 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
126 # grab the commit message
127 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
133 print MSG
"\n\nAuthor: $author\n";
134 if ($author ne $committer) {
135 print MSG
"Committer: $committer\n";
140 if ($parent eq $noparent) {
141 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
143 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
146 ## apply non-binary changes
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';
154 print "Checking if patch will apply\n";
157 open APPLY
, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
159 close APPLY
|| die "Cannot patch";
160 my (@bfiles,@files,@afiles,@dfiles);
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]+ (.*)$/;
169 map { s/^"(.*)"$/$1/g } @bfiles,@files;
170 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
172 # check that the files are clean and up to date according to cvs
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;
184 foreach my $d (@dirs) {
187 warn "$d exists and is not a directory!\n";
191 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
193 foreach my $f (@files) {
194 my $path = dirname
$f;
195 next if (grep { $_ eq $path } @dirs);
196 push @canstatusfiles, $f;
200 if (@canstatusfiles) {
202 my @updated = xargs_safe_pipe_capture
([@cvs, 'update'], @canstatusfiles);
205 # "cvs status" reorders the parameters, notably when there are multiple
206 # arguments with the same basename. So be precise here.
208 my %added = map { $_ => 1 } @afiles;
209 my %todo = map { $_ => 1 } @canstatusfiles;
212 my @canstatusfiles2 = ();
214 foreach my $name (keys %todo) {
215 my $basename = basename
($name);
217 $basename = "no file " . $basename if (exists($added{$basename}));
220 if (!exists($fullname{$basename})) {
221 $fullname{$basename} = $name;
222 push (@canstatusfiles2, $name);
223 delete($todo{$name});
227 @cvsoutput = xargs_safe_pipe_capture
([@cvs, 'status'], @canstatusfiles2);
228 foreach my $l (@cvsoutput) {
230 if ($l =~ /^File:\s+(.*\S)\s+Status: (.*)$/) {
231 if (!exists($fullname{$1})) {
232 print STDERR
"Huh? Status reported for unexpected file '$1'\n";
234 $cvsstat{$fullname{$1}} = $2;
241 # ... validate new files,
242 foreach my $f (@afiles) {
243 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
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";
246 warn "Status was: $cvsstat{$f}\n";
249 # ... validate known files.
250 foreach my $f (@files) {
251 next if grep { $_ eq $f } @afiles;
252 # TODO:we need to handle removed in cvs
253 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
255 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
259 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
262 die "Exiting: your CVS tree is not clean for this merge.";
267 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
269 print "Patch applied successfully. Adding new files and directories to CVS\n";
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.
277 # Luckily this is easy to deal with by sorting the directories and
278 # dealing with the shortest ones first.
280 @dirs = sort { length $a <=> length $b} @dirs;
282 foreach my $d (@dirs) {
283 if (system(@cvs,'add',$d)) {
285 warn "Failed to cvs add directory $d -- you may need to do it manually";
289 foreach my $f (@afiles) {
290 if (grep { $_ eq $f } @bfiles) {
291 system(@cvs, 'add','-kb',$f);
293 system(@cvs, 'add', $f);
297 warn "Failed to cvs add $f -- you may need to do it manually";
301 foreach my $f (@dfiles) {
302 system(@cvs, 'rm', '-f', $f);
305 warn "Failed to cvs rm -f $f -- you may need to do it manually";
309 print "Commit to CVS\n";
310 print "Patch title (first comment line): $title\n";
311 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
312 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
315 print "NOTE: One or more hunks failed to apply cleanly.\n";
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:";
319 print "\n cd \"$opt_w\"" if $opt_w;
325 print "Autocommit\n $cmd\n";
326 print xargs_safe_pipe_capture
([@cvs, 'commit', '-F', '.msg'], @files);
328 die "Exiting: The commit did not succeed";
330 print "Committed successfully to CVS\n";
334 print "Ready for you to commit, just run:\n\n $cmd\n";
338 unlink(".cvsexportcommit.diff");
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.
347 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
352 # An alternative to `command` that allows input to be passed as an array
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
{
357 if (my $pid = open my $child, '-|') {
358 @output = (<$child>);
359 close $child or die join(' ',@_).": $! $?";
361 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
363 return wantarray ?
@output : join('',@output);
366 sub xargs_safe_pipe_capture
{
367 my $MAX_ARG_LENGTH = 65536;
374 while(@_ && $length < $MAX_ARG_LENGTH) {
376 $length += length($args[$#args]);
379 push @output, safe_pipe_capture
(@
$cmd, @args);
382 $output .= safe_pipe_capture
(@
$cmd, @args);
385 return wantarray ?
@output : $output;