4 # - does not propagate permissions
5 # - tells "ready for commit" even when things could not be completed
6 # (not sure this is true anymore, more testing is needed)
7 # - does not handle whitespace in pathnames at all.
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_a, $opt_m );
25 die "Need at least one commit identifier!" unless @ARGV;
28 our ($tmpdir, $tmpdirname) = tempdir
('git-cvsapplycommit-XXXXXX',
32 # resolve target commit
35 $commit = safe_pipe_capture
('git-rev-parse', '--verify', "$commit^0");
38 die "The commit reference $commit did not resolve!";
41 # resolve what parent we want
45 $parent = safe_pipe_capture
('git-rev-parse', '--verify', "$parent^0");
48 die "The parent reference did not resolve!";
52 # find parents from the commit itself
53 my @commit = safe_pipe_capture
('git-cat-file', 'commit', $commit);
57 my $stage = 'headers'; # headers, msg
61 foreach my $line (@commit) {
63 if ($stage eq 'headers' && $line eq '') {
68 if ($stage eq 'headers') {
69 if ($line =~ m/^parent (\w{40})$/) { # found a parent
71 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
73 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
86 # double check that it's a valid parent
87 foreach my $p (@parents) {
93 die "Did not find $parent in the parents for this commit!" if !$found;
94 } else { # we don't have a parent from the cmdline...
95 if (@parents == 1) { # it's safe to get it from the commit
96 $parent = $parents[0];
97 } else { # or perhaps not!
98 die "This commit has more than one parent -- please name the parent you want to use explicitly";
102 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
104 # grab the commit message
105 open(MSG
, ">.msg") or die "Cannot open .msg for writing";
111 print MSG
"\n\nAuthor: $author\n";
112 if ($author ne $committer) {
113 print MSG
"Committer: $committer\n";
118 my (@afiles, @dfiles, @mfiles, @dirs);
120 my @files = safe_pipe_capture
('git-diff-tree', '-r', $parent, $commit);
122 $?
&& die "Error in git-diff-tree";
123 foreach my $f (@files) {
125 my @fields = split(m!\s+!, $f);
126 if ($fields[4] eq 'A') {
127 my $path = $fields[5];
128 $amodes{$path} = $fields[1];
130 # add any needed parent directories
131 $path = dirname
$path;
132 while (!-d
$path and ! grep { $_ eq $path } @dirs) {
133 unshift @dirs, $path;
134 $path = dirname
$path;
137 if ($fields[4] eq 'M') {
138 push @mfiles, $fields[5];
140 if ($fields[4] eq 'D') {
141 push @dfiles, $fields[5];
144 my (@binfiles, @abfiles, @dbfiles, @bfiles, @mbfiles);
145 @binfiles = grep m/^Binary files/, safe_pipe_capture
('git-diff-tree', '-p', $parent, $commit);
146 map { chomp } @binfiles;
147 @abfiles = grep s/^Binary files \/dev\/null
and b\
/(.*) differ$/$1/, @binfiles;
148 @dbfiles = grep s/^Binary files a\/(.*) and \/dev\
/null differ$/$1/, @binfiles;
149 @mbfiles = grep s/^Binary files a\/(.*) and b\/(.*) differ
$/$1/, @binfiles;
150 push @bfiles, @abfiles;
151 push @bfiles, @dbfiles;
152 push @bfiles, @mbfiles;
153 push @mfiles, @mbfiles;
155 $opt_v && print "The commit affects:\n ";
156 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
157 undef @files; # don't need it anymore
159 # check that the files are clean and up to date according to cvs
161 foreach my $d (@dirs) {
164 warn "$d exists and is not a directory!\n";
167 foreach my $f (@afiles) {
168 # This should return only one value
169 if ($f =~ m
,(.*)/[^/]*$,) {
171 next if (grep { $_ eq $p } @dirs);
173 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
174 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
175 if (-d dirname
$f and $status[0] !~ m/Status: Unknown$/
176 and $status[0] !~ m/^File: no file /) {
178 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";
179 warn "Status was: $status[0]\n";
183 foreach my $f (@mfiles, @dfiles) {
184 # TODO:we need to handle removed in cvs
185 my @status = grep(m/^File/, safe_pipe_capture
('cvs', '-q', 'status' ,$f));
186 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
187 unless ($status[0] =~ m/Status: Up-to-date$/) {
189 warn "File $f not up to date in your CVS checkout!\n";
193 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
196 die "Exiting: your CVS tree is not clean for this merge.";
201 ### NOTE: if you are planning to die() past this point
202 ### you MUST call cleanupcvs(@files) before die()
206 print "Creating new directories\n";
207 foreach my $d (@dirs) {
209 warn "Could not mkdir $d: $!";
215 warn "Failed to cvs add directory $d -- you may need to do it manually";
219 print "'Patching' binary files\n";
221 foreach my $f (@bfiles) {
222 # check that the file in cvs matches the "old" file
223 # extract the file to $tmpdir and compare with cmp
224 if (not(grep { $_ eq $f } @afiles)) {
225 my $tree = safe_pipe_capture
('git-rev-parse', "$parent^{tree}");
227 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
229 `git-cat-file blob $blob > $tmpdir/blob`;
230 if (system('cmp', '-s', $f, "$tmpdir/blob")) {
231 warn "Binary file $f in CVS does not match parent.\n";
238 if (not(grep { $_ eq $f } @dfiles)) {
239 my $tree = safe_pipe_capture
('git-rev-parse', "$commit^{tree}");
241 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
243 # replace with the new file
244 `git-cat-file blob $blob > $f`;
247 # TODO: something smart with file modes
252 die "Exiting: Binary files in CVS do not match parent";
255 ## apply non-binary changes
256 my $fuzz = $opt_p ?
0 : 2;
258 print "Patching non-binary files\n";
260 if (scalar(@afiles)+scalar(@dfiles)+scalar(@mfiles) != scalar(@bfiles)) {
261 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
265 if (($?
>> 8) == 2) {
267 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
268 } elsif (($?
>> 8) == 1) { # some hunks failed to apply
272 foreach my $f (@afiles) {
273 set_new_file_permissions
($f, $amodes{$f});
274 if (grep { $_ eq $f } @bfiles) {
275 system('cvs', 'add','-kb',$f);
277 system('cvs', 'add', $f);
281 warn "Failed to cvs add $f -- you may need to do it manually";
285 foreach my $f (@dfiles) {
286 system('cvs', 'rm', '-f', $f);
289 warn "Failed to cvs rm -f $f -- you may need to do it manually";
293 print "Commit to CVS\n";
294 print "Patch: $title\n";
295 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
296 my $cmd = "cvs commit -F .msg $commitfiles";
299 print "NOTE: One or more hunks failed to apply cleanly.\n";
300 print "Resolve the conflicts and then commit using:\n";
307 print "Autocommit\n $cmd\n";
308 print safe_pipe_capture
('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
311 die "Exiting: The commit did not succeed";
313 print "Committed successfully to CVS\n";
315 print "Ready for you to commit, just run:\n\n $cmd\n";
319 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
324 # ensure cvs is clean before we die
327 foreach my $f (@files) {
328 system('cvs', '-q', 'update', '-C', $f);
330 warn "Warning! Failed to cleanup state of $f\n";
335 # An alternative to `command` that allows input to be passed as an array
336 # to work around shell problems with weird characters in arguments
337 # if the exec returns non-zero we die
338 sub safe_pipe_capture
{
340 if (my $pid = open my $child, '-|') {
341 @output = (<$child>);
342 close $child or die join(' ',@_).": $! $?";
344 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
346 return wantarray ?
@output : join('',@output);
349 # For any file we want to add to cvs, we must first set its permissions
350 # properly, *before* the "cvs add ..." command. Otherwise, it is impossible
351 # to change the permission of the file in the CVS repository using only cvs
352 # commands. This should be fixed in cvs-1.12.14.
353 sub set_new_file_permissions
{
354 my ($file, $perm) = @_;
355 chmod oct($perm), $file
356 or die "failed to set permissions of \"$file\": $!\n";