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