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