1#!/usr/bin/perl -w 2 3# Known limitations: 4# - does not propagate permissions 5# - error handling has not been extensively tested 6# 7 8use strict; 9use Getopt::Std; 10use File::Temp qw(tempdir); 11use Data::Dumper; 12use File::Basename qw(basename dirname); 13 14unless($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ 15die"GIT_DIR is not defined or is unreadable"; 16} 17 18our($opt_h,$opt_P,$opt_p,$opt_v,$opt_c,$opt_f,$opt_a,$opt_m,$opt_d); 19 20getopts('hPpvcfam:d:'); 21 22$opt_h&& usage(); 23 24die"Need at least one commit identifier!"unless@ARGV; 25 26my@cvs; 27if($opt_d) { 28@cvs= ('cvs','-d',$opt_d); 29}else{ 30@cvs= ('cvs'); 31} 32 33# setup a tempdir 34our($tmpdir,$tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX', 35 TMPDIR =>1, 36 CLEANUP =>1); 37 38# resolve target commit 39my$commit; 40$commit=pop@ARGV; 41$commit= safe_pipe_capture('git-rev-parse','--verify',"$commit^0"); 42chomp$commit; 43if($?) { 44die"The commit reference$commitdid not resolve!"; 45} 46 47# resolve what parent we want 48my$parent; 49if(@ARGV) { 50$parent=pop@ARGV; 51$parent= safe_pipe_capture('git-rev-parse','--verify',"$parent^0"); 52chomp$parent; 53if($?) { 54die"The parent reference did not resolve!"; 55} 56} 57 58# find parents from the commit itself 59my@commit= safe_pipe_capture('git-cat-file','commit',$commit); 60my@parents; 61my$committer; 62my$author; 63my$stage='headers';# headers, msg 64my$title; 65my$msg=''; 66 67foreachmy$line(@commit) { 68chomp$line; 69if($stageeq'headers'&&$lineeq'') { 70$stage='msg'; 71next; 72} 73 74if($stageeq'headers') { 75if($line=~m/^parent (\w{40})$/) {# found a parent 76push@parents,$1; 77}elsif($line=~m/^author (.+) \d+ [-+]\d+$/) { 78$author=$1; 79}elsif($line=~m/^committer (.+) \d+ [-+]\d+$/) { 80$committer=$1; 81} 82}else{ 83$msg.=$line."\n"; 84unless($title) { 85$title=$line; 86} 87} 88} 89 90if($parent) { 91my$found; 92# double check that it's a valid parent 93foreachmy$p(@parents) { 94if($peq$parent) { 95$found=1; 96last; 97};# found it 98} 99die"Did not find$parentin the parents for this commit!"if!$foundand!$opt_P; 100}else{# we don't have a parent from the cmdline... 101if(@parents==1) {# it's safe to get it from the commit 102$parent=$parents[0]; 103}else{# or perhaps not! 104die"This commit has more than one parent -- please name the parent you want to use explicitly"; 105} 106} 107 108$opt_v&&print"Applying to CVS commit$commitfrom parent$parent\n"; 109 110# grab the commit message 111open(MSG,">.msg")or die"Cannot open .msg for writing"; 112if($opt_m) { 113print MSG $opt_m; 114} 115print MSG $msg; 116if($opt_a) { 117print MSG "\n\nAuthor:$author\n"; 118if($authorne$committer) { 119print MSG "Committer:$committer\n"; 120} 121} 122close MSG; 123 124`git-diff-tree --binary -p$parent$commit>.cvsexportcommit.diff`;# || die "Cannot diff"; 125 126## apply non-binary changes 127my$fuzz=$opt_p?0:2; 128 129print"Checking if patch will apply\n"; 130 131my@stat; 132open APPLY,"GIT_DIR= git-apply -C$fuzz--binary --summary --numstat<.cvsexportcommit.diff|"||die"cannot patch"; 133@stat=<APPLY>; 134close APPLY ||die"Cannot patch"; 135my(@bfiles,@files,@afiles,@dfiles); 136chomp@stat; 137foreach(@stat) { 138push(@bfiles,$1)ifm/^-\t-\t(.*)$/; 139push(@files,$1)ifm/^-\t-\t(.*)$/; 140push(@files,$1)ifm/^\d+\t\d+\t(.*)$/; 141push(@afiles,$1)ifm/^ create mode [0-7]+ (.*)$/; 142push(@dfiles,$1)ifm/^ delete mode [0-7]+ (.*)$/; 143} 144map{s/^"(.*)"$/$1/g}@bfiles,@files; 145map{s/\\([0-7]{3})/sprintf('%c',oct $1)/eg}@bfiles,@files; 146 147# check that the files are clean and up to date according to cvs 148my$dirty; 149my@dirs; 150foreachmy$p(@afiles) { 151my$path= dirname $p; 152while(!-d $pathand!grep{$_eq$path}@dirs) { 153unshift@dirs,$path; 154$path= dirname $path; 155} 156} 157 158foreachmy$d(@dirs) { 159if(-e $d) { 160$dirty=1; 161warn"$dexists and is not a directory!\n"; 162} 163} 164foreachmy$f(@afiles) { 165# This should return only one value 166if($f=~ m,(.*)/[^/]*$,) { 167my$p=$1; 168next if(grep{$_eq$p}@dirs); 169} 170my@status=grep(m/^File/, safe_pipe_capture(@cvs,'-q','status',$f)); 171if(@status>1) {warn'Strange! cvs status returned more than one line?'}; 172if(-d dirname $fand$status[0] !~m/Status: Unknown$/ 173and$status[0] !~m/^File: no file /) { 174$dirty=1; 175warn"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"; 176warn"Status was:$status[0]\n"; 177} 178} 179 180foreachmy$f(@files) { 181next ifgrep{$_eq$f}@afiles; 182# TODO:we need to handle removed in cvs 183my@status=grep(m/^File/, safe_pipe_capture(@cvs,'-q','status',$f)); 184if(@status>1) {warn'Strange! cvs status returned more than one line?'}; 185unless($status[0] =~m/Status: Up-to-date$/) { 186$dirty=1; 187warn"File$fnot up to date in your CVS checkout!\n"; 188} 189} 190if($dirty) { 191if($opt_f) {warn"The tree is not clean -- forced merge\n"; 192$dirty=0; 193}else{ 194die"Exiting: your CVS tree is not clean for this merge."; 195} 196} 197 198print"Applying\n"; 199`GIT_DIR= git-apply -C$fuzz--binary --summary --numstat --apply <.cvsexportcommit.diff`||die"cannot patch"; 200 201print"Patch applied successfully. Adding new files and directories to CVS\n"; 202my$dirtypatch=0; 203foreachmy$d(@dirs) { 204if(system(@cvs,'add',$d)) { 205$dirtypatch=1; 206warn"Failed to cvs add directory$d-- you may need to do it manually"; 207} 208} 209 210foreachmy$f(@afiles) { 211if(grep{$_eq$f}@bfiles) { 212system(@cvs,'add','-kb',$f); 213}else{ 214system(@cvs,'add',$f); 215} 216if($?) { 217$dirtypatch=1; 218warn"Failed to cvs add$f-- you may need to do it manually"; 219} 220} 221 222foreachmy$f(@dfiles) { 223system(@cvs,'rm','-f',$f); 224if($?) { 225$dirtypatch=1; 226warn"Failed to cvs rm -f$f-- you may need to do it manually"; 227} 228} 229 230print"Commit to CVS\n"; 231print"Patch title (first comment line):$title\n"; 232my@commitfiles=map{unless(m/\s/) {'\''.$_.'\''; }else{$_; }; } (@files); 233my$cmd=join(' ',@cvs)." commit -F .msg@commitfiles"; 234 235if($dirtypatch) { 236print"NOTE: One or more hunks failed to apply cleanly.\n"; 237print"You'll need to apply the patch in .cvsexportcommit.diff manually\n"; 238print"using a patch program. After applying the patch and resolving the\n"; 239print"problems you may commit using:"; 240print"\n$cmd\n\n"; 241exit(1); 242} 243 244if($opt_c) { 245print"Autocommit\n$cmd\n"; 246print safe_pipe_capture(@cvs,'commit','-F','.msg',@files); 247if($?) { 248die"Exiting: The commit did not succeed"; 249} 250print"Committed successfully to CVS\n"; 251# clean up 252unlink(".msg"); 253}else{ 254print"Ready for you to commit, just run:\n\n$cmd\n"; 255} 256 257# clean up 258unlink(".cvsexportcommit.diff"); 259 260sub usage { 261print STDERR <<END; 262Usage: GIT_DIR=/path/to/.git ${\basename$0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit 263END 264exit(1); 265} 266 267# An alternative to `command` that allows input to be passed as an array 268# to work around shell problems with weird characters in arguments 269# if the exec returns non-zero we die 270sub safe_pipe_capture { 271my@output; 272if(my$pid=open my$child,'-|') { 273@output= (<$child>); 274close$childor die join(' ',@_).":$!$?"; 275}else{ 276exec(@_)or die"$!$?";# exec() can fail the executable can't be found 277} 278returnwantarray?@output:join('',@output); 279} 280 281sub safe_pipe_capture_blob { 282my$output; 283if(my$pid=open my$child,'-|') { 284local$/; 285undef$/; 286$output= (<$child>); 287close$childor die join(' ',@_).":$!$?"; 288}else{ 289exec(@_)or die"$!$?";# exec() can fail the executable can't be found 290} 291return$output; 292}