5 use File
::Temp
qw(tempdir);
7 use File
::Basename
qw(basename);
9 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
10 die "GIT_DIR is not defined or is unreadable";
13 our ($opt_h, $opt_p, $opt_v, $opt_c );
19 die "Need at least one commit identifier!" unless @ARGV;
22 our ($tmpdir, $tmpdirname) = tempdir
('git-cvsapplycommit-XXXXXX',
27 # resolve target commit
30 $commit = `git-rev-parse --verify "$commit"^0`;
33 die "The commit reference $commit did not resolve!";
36 # resolve what parent we want
40 $parent = `git-rev-parse --verify "$parent"^0"`;
43 die "The parent reference did not resolve!";
47 # find parents from the commit itself
48 my @commit = `git-cat-file commit $commit`;
50 foreach my $p (@commit) {
51 if ($p =~ m/^$/) { # end of commit headers, we're done
54 if ($p =~ m/^parent (\w{40})$/) { # found a parent
60 # double check that it's a valid parent
61 foreach my $p (@parents) {
67 die "Did not find $parent in the parents for this commit!";
69 } else { # we don't have a parent from the cmdline...
70 if (@parents == 1) { # it's safe to get it from the commit
71 $parent = $parents[0];
72 } else { # or perhaps not!
73 die "This commit has more than one parent -- please name the parent you want to use explicitly";
77 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
79 # grab the commit message
80 `git-cat-file commit $commit | sed -e '1,/^\$/d' > .msg`;
81 $?
&& die "Error extracting the commit message";
83 my (@afiles, @dfiles, @mfiles);
84 my @files = `git-diff-tree -r $parent $commit`;
86 $?
&& die "Error in git-diff-tree";
87 foreach my $f (@files) {
89 my @fields = split(m/\s+/, $f);
90 if ($fields[4] eq 'A') {
91 push @afiles, $fields[5];
93 if ($fields[4] eq 'M') {
94 push @mfiles, $fields[5];
96 if ($fields[4] eq 'R') {
97 push @dfiles, $fields[5];
100 $opt_v && print "The commit affects:\n ";
101 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
102 undef @files; # don't need it anymore
104 # check that the files are clean and up to date according to cvs
106 foreach my $f (@afiles, @mfiles, @dfiles) {
107 # TODO:we need to handle removed in cvs and/or new (from git)
108 my $status = `cvs -q status "$f" | grep '^File: '`;
110 unless ($status =~ m/Status: Up-to-date$/) {
112 warn "File $f not up to date in your CVS checkout!\n";
116 die "Exiting: your CVS tree is not clean for this merge.";
120 ### NOTE: if you are planning to die() past this point
121 ### you MUST call cleanupcvs(@files) before die()
125 print "'Patching' binary files\n";
127 my @bfiles = `git-diff-tree -p $parent $commit | grep '^Binary'`;
128 @bfiles = map { chomp } @bfiles;
129 foreach my $f (@bfiles) {
130 # check that the file in cvs matches the "old" file
131 # extract the file to $tmpdir and comparre with cmp
132 my $tree = `git-rev-parse $parent^{tree} `;
134 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
136 `git-cat-file blob $blob > $tmpdir/blob`;
137 `cmp -q $f $tmpdir/blob`;
139 warn "Binary file $f in CVS does not match parent.\n";
144 # replace with the new file
145 `git-cat-file blob $blob > $f`;
147 # TODO: something smart with file modes
152 die "Exiting: Binary files in CVS do not match parent";
155 ## apply non-binary changes
156 my $fuzz = $opt_p ?
0 : 2;
158 print "Patching non-binary files\n";
159 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
162 if (($?
>> 8) == 2) {
164 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
165 } elsif (($?
>> 8) == 1) { # some hunks failed to apply
169 foreach my $f (@afiles) {
173 warn "Failed to cvs add $f -- you may need to do it manually";
177 foreach my $f (@dfiles) {
181 warn "Failed to cvs rm -f $f -- you may need to do it manually";
185 print "Commit to CVS\n";
186 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
187 my $cmd = "cvs commit -F .msg $commitfiles";
190 print "NOTE: One or more hunks failed to apply cleanly.\n";
191 print "Resolve the conflicts and then commit using:\n";
198 print "Autocommit\n $cmd\n";
199 print `cvs commit -F .msg $commitfiles 2>&1`;
202 die "Exiting: The commit did not succeed";
204 print "Committed successfully to CVS\n";
206 print "Ready for you to commit, just run:\n\n $cmd\n";
210 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [ 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";