5 use File::Temp qw(tempdir);
8 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
9 die "GIT_DIR is not defined or is unreadable";
12 our ($opt_h, $opt_p, $opt_v, $opt_c );
18 die "Need at least one commit identifier!" unless @ARGV;
21 our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
26 # resolve target commit
29 $commit = `git-rev-parse --verify "$commit"^0`;
32 die "The commit reference $commit did not resolve!";
35 # resolve what parent we want
39 $parent = `git-rev-parse --verify "$parent"^0"`;
42 die "The parent reference did not resolve!";
46 # find parents from the commit itself
47 my @commit = `git-cat-file commit $commit`;
49 foreach my $p (@commit) {
50 if ($p =~ m/^$/) { # end of commit headers, we're done
53 if ($p =~ m/^parent (\w{40})$/) { # found a parent
59 # double check that it's a valid parent
60 foreach my $p (@parents) {
66 die "Did not find $parent in the parents for this commit!";
68 } else { # we don't have a parent from the cmdline...
69 if (@parents == 1) { # it's safe to get it from the commit
70 $parent = $parents[0];
71 } else { # or perhaps not!
72 die "This commit has more than one parent -- please name the parent you want to use explicitly";
76 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
78 # grab the commit message
79 `git-cat-file commit $commit | sed -e '1,/^\$/d' > .msg`;
80 $? && die "Error extraction the commit message";
82 my (@afiles, @dfiles, @mfiles);
83 my @files = `git-diff-tree -r $parent $commit`;
85 $? && die "Error in git-diff-tree";
86 foreach my $f (@files) {
88 my @fields = split(m/\s+/, $f);
89 if ($fields[4] eq 'A') {
90 push @afiles, $fields[5];
92 if ($fields[4] eq 'M') {
93 push @mfiles, $fields[5];
95 if ($fields[4] eq 'R') {
96 push @dfiles, $fields[5];
99 $opt_v && print "The commit affects:\n ";
100 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
101 undef @files; # don't need it anymore
103 # check that the files are clean and up to date according to cvs
105 foreach my $f (@afiles, @mfiles, @dfiles) {
106 # TODO:we need to handle removed in cvs and/or new (from git)
107 my $status = `cvs -q status "$f" | grep '^File: '`;
109 unless ($status =~ m/Status: Up-to-date$/) {
111 warn "File $f not up to date in your CVS checkout!\n";
115 die "Exiting: your CVS tree is not clean for this merge.";
119 ### NOTE: if you are planning to die() past this point
120 ### you MUST call cleanupcvs(@files) before die()
124 print "'Patching' binary files\n";
126 my @bfiles = `git-diff-tree -p $parent $commit | grep '^Binary'`;
127 @bfiles = map { chomp } @bfiles;
128 foreach my $f (@bfiles) {
129 # check that the file in cvs matches the "old" file
130 # extract the file to $tmpdir and comparre with cmp
131 my $tree = `git-rev-parse $parent^{tree} `;
133 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
135 `git-cat-file blob $blob > $tmpdir/blob`;
136 `cmp -q $f $tmpdir/blob`;
138 warn "Binary file $f in CVS does not match parent.\n";
143 # replace with the new file
144 `git-cat-file blob $blob > $f`;
146 # TODO: something smart with file modes
151 die "Exiting: Binary files in CVS do not match parent";
154 ## apply non-binary changes
155 my $fuzz = $opt_p ? 0 : 2;
157 print "Patching non-binary files\n";
158 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
161 if (($? >> 8) == 2) {
163 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
164 } elsif (($? >> 8) == 1) { # some hunks failed to apply
168 foreach my $f (@afiles) {
172 warn "Failed to cvs add $f -- you may need to do it manually";
176 foreach my $f (@dfiles) {
180 warn "Failed to cvs rm -f $f -- you may need to do it manually";
184 print "Commit to CVS\n";
185 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
186 my $cmd = "cvs commit -F .msg $commitfiles";
189 print "NOTE: One or more hunks failed to apply cleanly.\n";
190 print "Resolve the conflicts and then commit using:n";
197 print "Autocommit\n $cmd\n";
198 print `cvs commit -F .msg $commitfiles 2>&1`;
201 die "Exiting: The commit did not succeed";
203 print "Committed successfully to CVS\n";
205 print "Ready for you to commit, just run:\n\n $cmd\n";
209 Usage: GIT_DIR=/path/to/.gi ${\basename $0} # fetch/update GIT from CVS
210 [-h] [-p] [ parent ] commit
215 # ensure cvs is clean before we die
218 foreach my $f (@files) {
219 `cvs -q update -C "$f"`;
221 warn "Warning! Failed to cleanup state of $f\n";