4 # - does not propagate permissions
5 # - error handling has not been extensively tested
10 use File
::Temp
qw(tempdir);
12 use File
::Basename
qw(basename dirname);
14 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
15 die "GIT_DIR is not defined or is unreadable";
18 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d);
20 getopts
('hPpvcfam:d:');
24 die "Need at least one commit identifier!" unless @ARGV;
28 @cvs = ('cvs', '-d', $opt_d);
34 our ($tmpdir, $tmpdirname) = tempdir
('git-cvsapplycommit-XXXXXX',
38 # resolve target commit
41 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
44 die "The commit reference $commit did not resolve!";
47 # resolve what parent we want
51 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
54 die "The parent reference did not resolve!";
58 # find parents from the commit itself
59 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
63 my $stage = 'headers'; # headers, msg
67 foreach my $line (@commit) {
69 if ($stage eq 'headers' && $line eq '') {
74 if ($stage eq 'headers') {
75 if ($line =~ m/^parent (\w{40})$/) { # found a parent
77 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
79 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
92 # double check that it's a valid parent
93 foreach my $p (@parents) {
99 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
100 } else { # we don't have a parent from the cmdline...
101 if (@parents == 1) { # it's safe to get it from the commit
102 $parent = $parents[0];
103 } else { # or perhaps not!
104 die "This commit has more than one parent -- please name the parent you want to use explicitly";
108 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
110 # grab the commit message
111 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
117 print MSG
"\n\nAuthor: $author\n";
118 if ($author ne $committer) {
119 print MSG
"Committer: $committer\n";
124 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
126 ## apply non-binary changes
128 # In pedantic mode require all lines of context to match. In normal
129 # mode, be compatible with diff/patch: assume 3 lines of context and
130 # require at least one line match, i.e. ignore at most 2 lines of
131 # context, like diff/patch do by default.
132 my $context = $opt_p ?
'' : '-C1';
134 print "Checking if patch will apply\n";
137 open APPLY
, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
139 close APPLY
|| die "Cannot patch";
140 my (@bfiles,@files,@afiles,@dfiles);
143 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
144 push (@files, $1) if m/^-\t-\t(.*)$/;
145 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
146 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
147 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
149 map { s/^"(.*)"$/$1/g } @bfiles,@files;
150 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
152 # check that the files are clean and up to date according to cvs
155 foreach my $p (@afiles) {
156 my $path = dirname
$p;
157 while (!-d
$path and ! grep { $_ eq $path } @dirs) {
158 unshift @dirs, $path;
159 $path = dirname
$path;
163 foreach my $d (@dirs) {
166 warn "$d exists and is not a directory!\n";
169 foreach my $f (@afiles) {
170 # This should return only one value
171 if ($f =~ m
,(.*)/[^/]*$,) {
173 next if (grep { $_ eq $p } @dirs);
175 my @status = grep(m/^File/, safe_pipe_capture
(@cvs, '-q', 'status' ,$f));
176 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
177 if (-d dirname
$f and $status[0] !~ m/Status: Unknown$/
178 and $status[0] !~ m/^File: no file /) {
180 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";
181 warn "Status was: $status[0]\n";
185 foreach my $f (@files) {
186 next if grep { $_ eq $f } @afiles;
187 # TODO:we need to handle removed in cvs
188 my @status = grep(m/^File/, safe_pipe_capture
(@cvs, '-q', 'status' ,$f));
189 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
190 unless ($status[0] =~ m/Status: Up-to-date$/) {
192 warn "File $f not up to date in your CVS checkout!\n";
196 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
199 die "Exiting: your CVS tree is not clean for this merge.";
204 `GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
206 print "Patch applied successfully. Adding new files and directories to CVS\n";
208 foreach my $d (@dirs) {
209 if (system(@cvs,'add',$d)) {
211 warn "Failed to cvs add directory $d -- you may need to do it manually";
215 foreach my $f (@afiles) {
216 if (grep { $_ eq $f } @bfiles) {
217 system(@cvs, 'add','-kb',$f);
219 system(@cvs, 'add', $f);
223 warn "Failed to cvs add $f -- you may need to do it manually";
227 foreach my $f (@dfiles) {
228 system(@cvs, 'rm', '-f', $f);
231 warn "Failed to cvs rm -f $f -- you may need to do it manually";
235 print "Commit to CVS\n";
236 print "Patch title (first comment line): $title\n";
237 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
238 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
241 print "NOTE: One or more hunks failed to apply cleanly.\n";
242 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
243 print "using a patch program. After applying the patch and resolving the\n";
244 print "problems you may commit using:";
250 print "Autocommit\n $cmd\n";
251 print safe_pipe_capture
(@cvs, 'commit', '-F', '.msg', @files);
253 die "Exiting: The commit did not succeed";
255 print "Committed successfully to CVS\n";
259 print "Ready for you to commit, just run:\n\n $cmd\n";
263 unlink(".cvsexportcommit.diff");
267 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
272 # An alternative to `command` that allows input to be passed as an array
273 # to work around shell problems with weird characters in arguments
274 # if the exec returns non-zero we die
275 sub safe_pipe_capture
{
277 if (my $pid = open my $child, '-|') {
278 @output = (<$child>);
279 close $child or die join(' ',@_).": $! $?";
281 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
283 return wantarray ?
@output : join('',@output);
286 sub safe_pipe_capture_blob
{
288 if (my $pid = open my $child, '-|') {
291 $output = (<$child>);
292 close $child or die join(' ',@_).": $! $?";
294 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found