]>
git.ipfire.org Git - thirdparty/git.git/blob - git-cvsexportcommit.perl
7 use File
:: Temp
qw(tempdir) ;
9 use File
:: Basename
qw(basename dirname) ;
13 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 , $opt_k );
15 getopts
( 'uhPpvcfkam:d:w:W' );
19 die "Need at least one commit identifier!" unless @ARGV ;
21 # Get git-config settings
22 my $repo = Git
-> repository ();
23 $opt_w = $repo -> config ( 'cvsexportcommit.cvsdir' ) unless defined $opt_w ;
25 if ( $opt_w || $opt_W ) {
26 # Remember where GIT_DIR is before changing to CVS checkout
27 unless ( $ENV { GIT_DIR
}) {
28 # No GIT_DIR set. Figure it out for ourselves
29 my $gd = `git-rev-parse --git-dir` ;
34 # On MSYS, convert a Windows-style path to an MSYS-style path
35 # so that rel2abs() below works correctly.
37 $ENV { GIT_DIR
} =~ s
#^([[:alpha:]]):/#/$1/#;
40 # Make sure GIT_DIR is absolute
41 $ENV { GIT_DIR
} = File
:: Spec
-> rel2abs ( $ENV { GIT_DIR
});
45 if (! - d
$opt_w . "/CVS" ) {
46 die " $opt_w is not a CVS checkout" ;
48 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w " ;
50 unless ( $ENV { GIT_DIR
} && - r
$ENV { GIT_DIR
}){
51 die "GIT_DIR is not defined or is unreadable" ;
57 @cvs = ( 'cvs' , '-d' , $opt_d );
62 # resolve target commit
65 $commit = safe_pipe_capture
( 'git-rev-parse' , '--verify' , " $commit ^0" );
68 die "The commit reference $commit did not resolve!" ;
71 # resolve what parent we want
75 $parent = safe_pipe_capture
( 'git-rev-parse' , '--verify' , " $parent ^0" );
78 die "The parent reference did not resolve!" ;
82 # find parents from the commit itself
83 my @commit = safe_pipe_capture
( 'git-cat-file' , 'commit' , $commit );
87 my $stage = 'headers' ; # headers, msg
91 foreach my $line ( @commit ) {
93 if ( $stage eq 'headers' && $line eq '' ) {
98 if ( $stage eq 'headers' ) {
99 if ( $line =~ m/^parent (\w{40})$/ ) { # found a parent
101 } elsif ( $line =~ m/^author (.+) \d+ [-+]\d+$/ ) {
103 } elsif ( $line =~ m/^committer (.+) \d+ [-+]\d+$/ ) {
107 $msg .= $line . " \n " ;
114 my $noparent = "0000000000000000000000000000000000000000" ;
117 # double check that it's a valid parent
118 foreach my $p ( @parents ) {
124 die "Did not find $parent in the parents for this commit!" if ! $found and ! $opt_P ;
125 } else { # we don't have a parent from the cmdline...
126 if ( @parents == 1 ) { # it's safe to get it from the commit
127 $parent = $parents [ 0 ];
128 } elsif ( @parents == 0 ) { # there is no parent
130 } else { # cannot choose automatically from multiple parents
131 die "This commit has more than one parent -- please name the parent you want to use explicitly" ;
138 $opt_v && print "Resetting to $parent \n " ;
139 $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||
140 git rev-parse HEAD` || die "Could not determine current branch" ;
141 system ( "git checkout -q $parent ^0" ) && die "Could not check out $parent ^0" ;
144 $opt_v && print "Applying to CVS commit $commit from parent $parent \n " ;
146 # grab the commit message
147 open ( MSG
, ">.msg" ) or die "Cannot open .msg for writing" ;
153 print MSG
" \n\n Author: $author \n " ;
154 if ( $author ne $committer ) {
155 print MSG
"Committer: $committer \n " ;
160 if ( $parent eq $noparent ) {
161 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff` ; # || die "Cannot diff";
163 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff` ; # || die "Cannot diff";
166 ## apply non-binary changes
168 # In pedantic mode require all lines of context to match. In normal
169 # mode, be compatible with diff/patch: assume 3 lines of context and
170 # require at least one line match, i.e. ignore at most 2 lines of
171 # context, like diff/patch do by default.
172 my $context = $opt_p ?
'' : '-C1' ;
174 print "Checking if patch will apply \n " ;
177 open APPLY
, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch" ;
179 close APPLY
|| die "Cannot patch" ;
180 my ( @bfiles , @files , @afiles , @dfiles );
183 push ( @bfiles , $1 ) if m/^-\t-\t(.*)$/ ;
184 push ( @files , $1 ) if m/^-\t-\t(.*)$/ ;
185 push ( @files , $1 ) if m/^\d+\t\d+\t(.*)$/ ;
186 push ( @afiles , $1 ) if m/^ create mode [0-7]+ (.*)$/ ;
187 push ( @dfiles , $1 ) if m/^ delete mode [0-7]+ (.*)$/ ;
189 map { s/^"(.*)"$/$1/g } @bfiles , @files ;
190 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles , @files ;
192 # check that the files are clean and up to date according to cvs
195 foreach my $p ( @afiles ) {
196 my $path = dirname
$p ;
197 while (!- d
$path and ! grep { $_ eq $path } @dirs ) {
198 unshift @dirs , $path ;
199 $path = dirname
$path ;
204 foreach my $d ( @dirs ) {
207 warn " $d exists and is not a directory! \n " ;
211 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
213 foreach my $f ( @files ) {
214 my $path = dirname
$f ;
215 next if ( grep { $_ eq $path } @dirs );
216 push @canstatusfiles , $f ;
220 if ( @canstatusfiles ) {
222 my @updated = xargs_safe_pipe_capture
([ @cvs , 'update' ], @canstatusfiles );
225 # "cvs status" reorders the parameters, notably when there are multiple
226 # arguments with the same basename. So be precise here.
228 my %added = map { $_ => 1 } @afiles ;
229 my %todo = map { $_ => 1 } @canstatusfiles ;
232 my @canstatusfiles2 = ();
234 foreach my $name ( keys %todo ) {
235 my $basename = basename
( $name );
237 # CVS reports files that don't exist in the current revision as
238 # "no file $basename" in its "status" output, so we should
239 # anticipate that. Totally unknown files will have a status
240 # "Unknown". However, if they exist in the Attic, their status
241 # will be "Up-to-date" (this means they were added once but have
243 $basename = "no file $basename " if $added { $basename };
245 $basename =~ s/^\s+// ;
246 $basename =~ s/\s+$// ;
248 if (! exists ( $fullname { $basename })) {
249 $fullname { $basename } = $name ;
250 push ( @canstatusfiles2 , $name );
251 delete ( $todo { $name });
255 @cvsoutput = xargs_safe_pipe_capture
([ @cvs , 'status' ], @canstatusfiles2 );
256 foreach my $l ( @cvsoutput ) {
259 my ( $file , $status ) = $l =~ /^File:\s+(.*\S)\s+Status: (.*)$/ ;
261 my $fullname = $fullname { $file };
262 print STDERR
"Huh? Status ' $status ' reported for unexpected file ' $file ' \n "
263 unless defined $fullname ;
265 # This response means the file does not exist except in
266 # CVS's attic, so set the status accordingly
268 if $file =~ /^no file /
269 && $status eq 'Up-to-date' ;
271 $cvsstat { $fullname { $file }} = $status
272 if defined $fullname { $file };
277 # ... Validate that new files have the correct status
278 foreach my $f ( @afiles ) {
279 next unless defined ( my $stat = $cvsstat { $f });
281 # This means the file has never been seen before
282 next if $stat eq 'Unknown' ;
284 # This means the file has been seen before but was removed
285 next if $stat eq 'In-attic' ;
288 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 " ;
289 warn "Status was: $cvsstat { $f } \n " ;
292 # ... validate known files.
293 foreach my $f ( @files ) {
294 next if grep { $_ eq $f } @afiles ;
295 # TODO:we need to handle removed in cvs
296 unless ( defined ( $cvsstat { $f }) and $cvsstat { $f } eq "Up-to-date" ) {
298 warn "File $f not up to date but has status ' $cvsstat { $f }' in your CVS checkout! \n " ;
301 # Depending on how your GIT tree got imported from CVS you may
302 # have a conflict between expanded keywords in your CVS tree and
303 # unexpanded keywords in the patch about to be applied.
305 my $orig_file = " $f .orig" ;
306 rename $f , $orig_file ;
307 open ( FILTER_IN
, "< $orig_file " ) or die "Cannot open $orig_file \n " ;
308 open ( FILTER_OUT
, "> $f " ) or die "Cannot open $f \n " ;
312 $line =~ s/\$([A-Z][a-z]+):[^\$]+\$/\$$1\$/g ;
313 print FILTER_OUT
$line ;
321 if ( $opt_f ) { warn "The tree is not clean -- forced merge \n " ;
324 die "Exiting: your CVS tree is not clean for this merge." ;
330 system ( "git checkout -q $commit ^0" ) && die "cannot patch" ;
332 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch" ;
335 print "Patch applied successfully. Adding new files and directories to CVS \n " ;
339 # We have to add the directories in order otherwise we will have
340 # problems when we try and add the sub-directory of a directory we
341 # have not added yet.
343 # Luckily this is easy to deal with by sorting the directories and
344 # dealing with the shortest ones first.
346 @dirs = sort { length $a <=> length $b } @dirs ;
348 foreach my $d ( @dirs ) {
349 if ( system ( @cvs , 'add' , $d )) {
351 warn "Failed to cvs add directory $d -- you may need to do it manually" ;
355 foreach my $f ( @afiles ) {
356 if ( grep { $_ eq $f } @bfiles ) {
357 system ( @cvs , 'add' , '-kb' , $f );
359 system ( @cvs , 'add' , $f );
363 warn "Failed to cvs add $f -- you may need to do it manually" ;
367 foreach my $f ( @dfiles ) {
368 system ( @cvs , 'rm' , '-f' , $f );
371 warn "Failed to cvs rm -f $f -- you may need to do it manually" ;
375 print "Commit to CVS \n " ;
376 print "Patch title (first comment line): $title \n " ;
377 my @commitfiles = map { unless ( m/\s/ ) { ' \' ' . $_ . ' \' ' ; } else { $_ ; }; } ( @files );
378 my $cmd = join ( ' ' , @cvs ). " commit -F .msg @commitfiles " ;
381 print "NOTE: One or more hunks failed to apply cleanly. \n " ;
382 print "You'll need to apply the patch in .cvsexportcommit.diff manually \n " ;
383 print "using a patch program. After applying the patch and resolving the \n " ;
384 print "problems you may commit using:" ;
385 print " \n cd \" $opt_w \" " if $opt_w ;
387 print " \n git checkout $go_back_to \n " if $go_back_to ;
393 print "Autocommit \n $cmd \n " ;
394 print xargs_safe_pipe_capture
([ @cvs , 'commit' , '-F' , '.msg' ], @files );
396 die "Exiting: The commit did not succeed" ;
398 print "Committed successfully to CVS \n " ;
402 print "Ready for you to commit, just run: \n\n $cmd \n " ;
406 unlink ( ".cvsexportcommit.diff" );
409 system ( "git checkout $go_back_to " ) && die "cannot move back to $go_back_to " ;
410 if (!( $go_back_to =~ /^[0-9a-fA-F]{40}$/ )) {
411 system ( "git symbolic-ref HEAD $go_back_to " ) &&
412 die "cannot move back to $go_back_to " ;
416 # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
417 # used by CVS and the one set by subsequence file modifications are different.
418 # If they are not different CVS will not detect changes.
423 usage: GIT_DIR=/path/to/.git git cvsexportcommit [-h] [-p] [-v] [-c] [-f] [-u] [-k] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
428 # An alternative to `command` that allows input to be passed as an array
429 # to work around shell problems with weird characters in arguments
430 # if the exec returns non-zero we die
431 sub safe_pipe_capture
{
433 if ( my $pid = open my $child , '-|' ) {
434 @output = (< $child >);
435 close $child or die join ( ' ' , @_ ). ": $! $?" ;
437 exec ( @_ ) or die "$! $?" ; # exec() can fail the executable can't be found
439 return wantarray ?
@output : join ( '' , @output );
442 sub xargs_safe_pipe_capture
{
443 my $MAX_ARG_LENGTH = 65536 ;
450 while ( @_ && $length < $MAX_ARG_LENGTH ) {
452 $length += length ( $args [ $#args ]);
455 push @output , safe_pipe_capture
( @
$cmd , @args );
458 $output .= safe_pipe_capture
( @
$cmd , @args );
461 return wantarray ?
@output : $output ;