#!/usr/bin/perl -w use strict; use Getopt::Std; use File::Temp qw(tempdir); unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ die "GIT_DIR is not defined or is unreadable"; } our ($opt_h, $opt_p); getopt('hp'); $opt_h && usage(); die "Need at least one commit identifier!" unless @ARGV; # setup a tempdir our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX', TMPDIR => 1, CLEANUP => 1); # resolve target commit my $commit; $commit = pop @ARGV; $commit = `git-rev-parse --verify "$commit"^0"`; chomp $commit; if ($?) { die "The commit reference did not resolve!"; } # resolve what parent we want my $parent; if (@ARGV) { $parent = pop @ARGV; $parent = `git-rev-parse --verify "$parent"^0"`; chomp $parent; if ($?) { die "The parent reference did not resolve!"; } } # find parents from the commit itself my @parents = `git-cat-file commmit $commit | grep -E '^parent \\w{40}\$' | sed -e 's/^parent //'`; @parents = map { chomp } @parents; if ($parent) { # double check that it's a valid parent foreach my $p (@parents) { my $found; if ($p eq $parent) { $found = 1; last; }; # found it die "Did not find $parent in the parents for this commit!"; } } else { # we don't have a parent from the cmdline... if (@parents == 1) { # it's safe to get it from the commit $parent = $parents[0]; } else { # or perhaps not! die "This commit has more than one parent -- please name the parent you want to use explicitly"; } } # grab the commit message `git-cat-file commit $commit | sed -e '1,/^$/d' > .msg`; $? && die "Error extraction the commit message"; my @files = `git-diff-tree -r $parent $commit | cut -f 2`; $? && die "Error in git-diff-tree"; @files = map { chomp } @files; # check that the files are clean and up to date according to cvs my $dirty; foreach my $f (@files) { # TODO:we need to handle removed in cvs and/or new (from git) my $status = `cvs -q status "$f" | grep '^File: '`; unless ($status =~ m/Status: Up-to-date$/) { $dirty = 1; warn "File $f not up to date in your CVS checkout!\n"; } } if ($dirty) { die "Exiting: your CVS tree is not clean for this merge."; } ### ### NOTE: if you are planning to die() past this point ### you MUST call cleanupcvs(@files) before die() ### print "'Patching' binary files\n"; my @bfiles = `git-diff-tree -p $parent $commit | grep '^Binary'`; @bfiles = map { chomp } @bfiles; foreach my $f (@bfiles) { # check that the file in cvs matches the "old" file # extract the file to $tmpdir and comparre with cmp my $tree = `git-rev-parse $parent^{tree} `; chomp $tree; my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`; chomp $blob; `git-cat-file blob $blob > $tmp/blob`; `cmp -q $f $tmp/blob`; if ($?) { warn "Binary file $f in CVS does not match parent.\n"; $dirty = 1; next; } # replace with the new file `git-cat-file blob $blob > $f`; # TODO: something smart with file modes } if ($dirty) { cleanupcvs(@files); die "Exiting: Binary files in CVS do not match parent"; } ## apply non-binary changes my $fuzz = $opt_p ? 0 : 2; print "Patching non-binary files\n"; print `(git-diff-tree -p $1 | patch -p1 -F $fuzz ) 2>&1`; if ($?) { cleanupcvs(@files); die "Exiting: Patch did not succeed -- you will have to apply this patch manually"; } print "Commit to CVS\n"; my $commitfiles = join(' ', @files); print `cvs commit -F .msg $commitfiles 2>&1`; if ($?) { cleanupcvs(@files); die "Exiting: The commit did not succeed"; } print "Committed successfully to CVS\n"; sub usage { print STDERR <