4 # - cannot add or remove binary files
5 # - cannot add parent directories when needed
6 # - does not propagate permissions
7 # - tells "ready for commit" even when things could not be completed
8 # (eg addition of a binary file)
12 use File
::Temp
qw(tempdir);
14 use File
::Basename
qw(basename);
16 unless ($ENV{GIT_DIR
} && -r
$ENV{GIT_DIR
}){
17 die "GIT_DIR is not defined or is unreadable";
20 our ($opt_h, $opt_p, $opt_v, $opt_c, $opt_f, $opt_m );
26 die "Need at least one commit identifier!" unless @ARGV;
29 our ($tmpdir, $tmpdirname) = tempdir
('git-cvsapplycommit-XXXXXX',
34 # resolve target commit
37 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
40 die "The commit reference $commit did not resolve!";
43 # resolve what parent we want
47 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
50 die "The parent reference did not resolve!";
54 # find parents from the commit itself
55 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
57 foreach my $p (@commit) {
58 if ($p =~ m/^$/) { # end of commit headers, we're done
61 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!";
76 } else { # we don't have a parent from the cmdline...
77 if (@parents == 1) { # it's safe to get it from the commit
78 $parent = $parents[0];
79 } else { # or perhaps not!
80 die "This commit has more than one parent -- please name the parent you want to use explicitly";
84 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
86 # grab the commit message
87 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
91 `git-cat-file commit $commit | sed -e '1,/^\$/d' >> .msg`;
92 $?
&& die "Error extracting the commit message";
94 my (@afiles, @dfiles, @mfiles);
95 my @files = safe_pipe_capture
('git-diff-tree', '-r', $parent, $commit);
97 $?
&& die "Error in git-diff-tree";
98 foreach my $f (@files) {
100 my @fields = split(m!\s+!, $f);
101 if ($fields[4] eq 'A') {
102 push @afiles, $fields[5];
104 if ($fields[4] eq 'M') {
105 push @mfiles, $fields[5];
107 if ($fields[4] eq 'R') {
108 push @dfiles, $fields[5];
111 $opt_v && print "The commit affects:\n ";
112 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
113 undef @files; # don't need it anymore
115 # check that the files are clean and up to date according to cvs
117 foreach my $f (@afiles) {
118 # This should return only one value
119 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
120 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
121 unless ($status[0] =~ m/Status: Unknown$/) {
123 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";
126 foreach my $f (@mfiles, @dfiles) {
127 # TODO:we need to handle removed in cvs
128 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
129 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
130 unless ($status[0] =~ m/Status: Up-to-date$/) {
132 warn "File $f not up to date in your CVS checkout!\n";
136 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
139 die "Exiting: your CVS tree is not clean for this merge.";
144 ### NOTE: if you are planning to die() past this point
145 ### you MUST call cleanupcvs(@files) before die()
149 print "'Patching' binary files\n";
151 my @bfiles = grep(m/^Binary/, safe_pipe_capture
('git-diff-tree', '-p', $parent, $commit));
152 @bfiles = map { chomp } @bfiles;
153 foreach my $f (@bfiles) {
154 # check that the file in cvs matches the "old" file
155 # extract the file to $tmpdir and comparre with cmp
156 my $tree = safe_pipe_capture
('git-rev-parse', "$parent^{tree}");
158 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
160 `git-cat-file blob $blob > $tmpdir/blob`;
161 if (system('cmp', '-s', $f, "$tmpdir/blob")) {
162 warn "Binary file $f in CVS does not match parent.\n";
167 # replace with the new file
168 `git-cat-file blob $blob > $f`;
170 # TODO: something smart with file modes
175 die "Exiting: Binary files in CVS do not match parent";
178 ## apply non-binary changes
179 my $fuzz = $opt_p ?
0 : 2;
181 print "Patching non-binary files\n";
182 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
185 if (($?
>> 8) == 2) {
187 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
188 } elsif (($?
>> 8) == 1) { # some hunks failed to apply
192 foreach my $f (@afiles) {
193 system('cvs', 'add', $f);
196 warn "Failed to cvs add $f -- you may need to do it manually";
200 foreach my $f (@dfiles) {
201 system('cvs', 'rm', '-f', $f);
204 warn "Failed to cvs rm -f $f -- you may need to do it manually";
208 print "Commit to CVS\n";
209 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
210 my $cmd = "cvs commit -F .msg $commitfiles";
213 print "NOTE: One or more hunks failed to apply cleanly.\n";
214 print "Resolve the conflicts and then commit using:\n";
221 print "Autocommit\n $cmd\n";
222 print safe_pipe_capture
('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
225 die "Exiting: The commit did not succeed";
227 print "Committed successfully to CVS\n";
229 print "Ready for you to commit, just run:\n\n $cmd\n";
233 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
238 # ensure cvs is clean before we die
241 foreach my $f (@files) {
242 system('cvs', '-q', 'update', '-C', $f);
244 warn "Warning! Failed to cleanup state of $f\n";
249 # An alterative to `command` that allows input to be passed as an array
250 # to work around shell problems with weird characters in arguments
251 # if the exec returns non-zero we die
252 sub safe_pipe_capture
{
254 if (my $pid = open my $child, '-|') {
255 @output = (<$child>);
256 close $child or die join(' ',@_).": $! $?";
258 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
260 return wantarray ?
@output : join('',@output);