4 # - cannot add or remove binary files
5 # - does not propagate permissions
6 # - tells "ready for commit" even when things could not be completed
7 # (eg addition of a binary file)
11 use File
::Temp
qw(tempdir);
13 use File
::Basename
qw(basename dirname);
15 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
16 die "GIT_DIR is not defined or is unreadable";
19 our ($opt_h, $opt_p, $opt_v, $opt_c, $opt_f, $opt_m );
25 die "Need at least one commit identifier!" unless @ARGV;
28 our ($tmpdir, $tmpdirname) = tempdir
('git-cvsapplycommit-XXXXXX',
33 # resolve target commit
36 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
39 die "The commit reference $commit did not resolve!";
42 # resolve what parent we want
46 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
49 die "The parent reference did not resolve!";
53 # find parents from the commit itself
54 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
56 foreach my $p (@commit) {
57 if ($p =~ m/^$/) { # end of commit headers, we're done
60 if ($p =~ m/^parent (\w{40})$/) { # found a parent
67 # double check that it's a valid parent
68 foreach my $p (@parents) {
74 die "Did not find $parent in the parents for this commit!" if !$found;
75 } else { # we don't have a parent from the cmdline...
76 if (@parents == 1) { # it's safe to get it from the commit
77 $parent = $parents[0];
78 } else { # or perhaps not!
79 die "This commit has more than one parent -- please name the parent you want to use explicitly";
83 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
85 # grab the commit message
86 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
90 `git-cat-file commit $commit | sed -e '1,/^\$/d' >> .msg`;
91 $?
&& die "Error extracting the commit message";
93 my (@afiles, @dfiles, @mfiles, @dirs);
94 my @files = safe_pipe_capture
('git-diff-tree', '-r', $parent, $commit);
96 $?
&& die "Error in git-diff-tree";
97 foreach my $f (@files) {
99 my @fields = split(m!\s+!, $f);
100 if ($fields[4] eq 'A') {
101 my $path = $fields[5];
103 # add any needed parent directories
104 $path = dirname
$path;
105 while (!-d
$path and ! grep { $_ eq $path } @dirs) {
106 unshift @dirs, $path;
107 $path = dirname
$path;
110 if ($fields[4] eq 'M') {
111 push @mfiles, $fields[5];
113 if ($fields[4] eq 'R') {
114 push @dfiles, $fields[5];
117 $opt_v && print "The commit affects:\n ";
118 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
119 undef @files; # don't need it anymore
121 # check that the files are clean and up to date according to cvs
123 foreach my $d (@dirs) {
126 warn "$d exists and is not a directory!\n";
129 foreach my $f (@afiles) {
130 # This should return only one value
131 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
132 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
133 if (-d dirname
$f and $status[0] !~ m/Status: Unknown$/
134 and $status[0] !~ m/^File: no file /) {
136 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";
137 warn "Status was: $status[0]\n";
140 foreach my $f (@mfiles, @dfiles) {
141 # TODO:we need to handle removed in cvs
142 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
143 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
144 unless ($status[0] =~ m/Status: Up-to-date$/) {
146 warn "File $f not up to date in your CVS checkout!\n";
150 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
153 die "Exiting: your CVS tree is not clean for this merge.";
158 ### NOTE: if you are planning to die() past this point
159 ### you MUST call cleanupcvs(@files) before die()
163 print "Creating new directories\n";
164 foreach my $d (@dirs) {
166 warn "Could not mkdir $d: $!";
172 warn "Failed to cvs add directory $d -- you may need to do it manually";
176 print "'Patching' binary files\n";
178 my @bfiles = grep(m/^Binary/, safe_pipe_capture
('git-diff-tree', '-p', $parent, $commit));
179 @bfiles = map { chomp } @bfiles;
180 foreach my $f (@bfiles) {
181 # check that the file in cvs matches the "old" file
182 # extract the file to $tmpdir and comparre with cmp
183 my $tree = safe_pipe_capture
('git-rev-parse', "$parent^{tree}");
185 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
187 `git-cat-file blob $blob > $tmpdir/blob`;
188 if (system('cmp', '-s', $f, "$tmpdir/blob")) {
189 warn "Binary file $f in CVS does not match parent.\n";
194 # replace with the new file
195 `git-cat-file blob $blob > $f`;
197 # TODO: something smart with file modes
202 die "Exiting: Binary files in CVS do not match parent";
205 ## apply non-binary changes
206 my $fuzz = $opt_p ?
0 : 2;
208 print "Patching non-binary files\n";
209 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
212 if (($?
>> 8) == 2) {
214 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
215 } elsif (($?
>> 8) == 1) { # some hunks failed to apply
219 foreach my $f (@afiles) {
220 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 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
237 my $cmd = "cvs commit -F .msg $commitfiles";
240 print "NOTE: One or more hunks failed to apply cleanly.\n";
241 print "Resolve the conflicts and then commit using:\n";
248 print "Autocommit\n $cmd\n";
249 print safe_pipe_capture
('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
252 die "Exiting: The commit did not succeed";
254 print "Committed successfully to CVS\n";
256 print "Ready for you to commit, just run:\n\n $cmd\n";
260 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
265 # ensure cvs is clean before we die
268 foreach my $f (@files) {
269 system('cvs', '-q', 'update', '-C', $f);
271 warn "Warning! Failed to cleanup state of $f\n";
276 # An alterative to `command` that allows input to be passed as an array
277 # to work around shell problems with weird characters in arguments
278 # if the exec returns non-zero we die
279 sub safe_pipe_capture
{
281 if (my $pid = open my $child, '-|') {
282 @output = (<$child>);
283 close $child or die join(' ',@_).": $! $?";
285 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
287 return wantarray ?
@output : join('',@output);