]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - git-cvsexportcommit.perl
cvsexportcommit: chomp only removes trailing whitespace
[thirdparty/git.git] / git-cvsexportcommit.perl
... / ...
CommitLineData
1#!/usr/bin/perl -w
2
3use strict;
4use Getopt::Std;
5use File::Temp qw(tempdir);
6use Data::Dumper;
7use File::Basename qw(basename dirname);
8use File::Spec;
9
10our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w);
11
12getopts('uhPpvcfam:d:w:');
13
14$opt_h && usage();
15
16die "Need at least one commit identifier!" unless @ARGV;
17
18if ($opt_w) {
19 # Remember where GIT_DIR is before changing to CVS checkout
20 unless ($ENV{GIT_DIR}) {
21 # No GIT_DIR set. Figure it out for ourselves
22 my $gd =`git-rev-parse --git-dir`;
23 chomp($gd);
24 $ENV{GIT_DIR} = $gd;
25 }
26 # Make sure GIT_DIR is absolute
27 $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR});
28
29 if (! -d $opt_w."/CVS" ) {
30 die "$opt_w is not a CVS checkout";
31 }
32 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
33}
34unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
35 die "GIT_DIR is not defined or is unreadable";
36}
37
38
39my @cvs;
40if ($opt_d) {
41 @cvs = ('cvs', '-d', $opt_d);
42} else {
43 @cvs = ('cvs');
44}
45
46# resolve target commit
47my $commit;
48$commit = pop @ARGV;
49$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
50chomp $commit;
51if ($?) {
52 die "The commit reference $commit did not resolve!";
53}
54
55# resolve what parent we want
56my $parent;
57if (@ARGV) {
58 $parent = pop @ARGV;
59 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
60 chomp $parent;
61 if ($?) {
62 die "The parent reference did not resolve!";
63 }
64}
65
66# find parents from the commit itself
67my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
68my @parents;
69my $committer;
70my $author;
71my $stage = 'headers'; # headers, msg
72my $title;
73my $msg = '';
74
75foreach my $line (@commit) {
76 chomp $line;
77 if ($stage eq 'headers' && $line eq '') {
78 $stage = 'msg';
79 next;
80 }
81
82 if ($stage eq 'headers') {
83 if ($line =~ m/^parent (\w{40})$/) { # found a parent
84 push @parents, $1;
85 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
86 $author = $1;
87 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
88 $committer = $1;
89 }
90 } else {
91 $msg .= $line . "\n";
92 unless ($title) {
93 $title = $line;
94 }
95 }
96}
97
98my $noparent = "0000000000000000000000000000000000000000";
99if ($parent) {
100 my $found;
101 # double check that it's a valid parent
102 foreach my $p (@parents) {
103 if ($p eq $parent) {
104 $found = 1;
105 last;
106 }; # found it
107 }
108 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
109} else { # we don't have a parent from the cmdline...
110 if (@parents == 1) { # it's safe to get it from the commit
111 $parent = $parents[0];
112 } elsif (@parents == 0) { # there is no parent
113 $parent = $noparent;
114 } else { # cannot choose automatically from multiple parents
115 die "This commit has more than one parent -- please name the parent you want to use explicitly";
116 }
117}
118
119$opt_v && print "Applying to CVS commit $commit from parent $parent\n";
120
121# grab the commit message
122open(MSG, ">.msg") or die "Cannot open .msg for writing";
123if ($opt_m) {
124 print MSG $opt_m;
125}
126print MSG $msg;
127if ($opt_a) {
128 print MSG "\n\nAuthor: $author\n";
129 if ($author ne $committer) {
130 print MSG "Committer: $committer\n";
131 }
132}
133close MSG;
134
135if ($parent eq $noparent) {
136 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
137} else {
138 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
139}
140
141## apply non-binary changes
142
143# In pedantic mode require all lines of context to match. In normal
144# mode, be compatible with diff/patch: assume 3 lines of context and
145# require at least one line match, i.e. ignore at most 2 lines of
146# context, like diff/patch do by default.
147my $context = $opt_p ? '' : '-C1';
148
149print "Checking if patch will apply\n";
150
151my @stat;
152open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
153@stat=<APPLY>;
154close APPLY || die "Cannot patch";
155my (@bfiles,@files,@afiles,@dfiles);
156chomp @stat;
157foreach (@stat) {
158 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
159 push (@files, $1) if m/^-\t-\t(.*)$/;
160 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
161 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
162 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
163}
164map { s/^"(.*)"$/$1/g } @bfiles,@files;
165map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
166
167# check that the files are clean and up to date according to cvs
168my $dirty;
169my @dirs;
170foreach my $p (@afiles) {
171 my $path = dirname $p;
172 while (!-d $path and ! grep { $_ eq $path } @dirs) {
173 unshift @dirs, $path;
174 $path = dirname $path;
175 }
176}
177
178# ... check dirs,
179foreach my $d (@dirs) {
180 if (-e $d) {
181 $dirty = 1;
182 warn "$d exists and is not a directory!\n";
183 }
184}
185
186# ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
187my @canstatusfiles;
188foreach my $f (@files) {
189 my $path = dirname $f;
190 next if (grep { $_ eq $path } @dirs);
191 push @canstatusfiles, $f;
192}
193
194my %cvsstat;
195if (@canstatusfiles) {
196 if ($opt_u) {
197 my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles);
198 print @updated;
199 }
200 # "cvs status" reorders the parameters, notably when there are multiple
201 # arguments with the same basename. So be precise here.
202
203 my %added = map { $_ => 1 } @afiles;
204 my %todo = map { $_ => 1 } @canstatusfiles;
205
206 while (%todo) {
207 my @canstatusfiles2 = ();
208 my %fullname = ();
209 foreach my $name (keys %todo) {
210 my $basename = basename($name);
211
212 $basename = "no file " . $basename if (exists($added{$basename}));
213 $basename =~ s/^\s+//;
214 $basename =~ s/\s+$//;
215
216 if (!exists($fullname{$basename})) {
217 $fullname{$basename} = $name;
218 push (@canstatusfiles2, $name);
219 delete($todo{$name});
220 }
221 }
222 my @cvsoutput;
223 @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2);
224 foreach my $l (@cvsoutput) {
225 chomp $l;
226 if ($l =~ /^File:\s+(.*\S)\s+Status: (.*)$/) {
227 if (!exists($fullname{$1})) {
228 print STDERR "Huh? Status reported for unexpected file '$1'\n";
229 } else {
230 $cvsstat{$fullname{$1}} = $2;
231 }
232 }
233 }
234 }
235}
236
237# ... validate new files,
238foreach my $f (@afiles) {
239 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
240 $dirty = 1;
241 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";
242 warn "Status was: $cvsstat{$f}\n";
243 }
244}
245# ... validate known files.
246foreach my $f (@files) {
247 next if grep { $_ eq $f } @afiles;
248 # TODO:we need to handle removed in cvs
249 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
250 $dirty = 1;
251 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
252 }
253}
254if ($dirty) {
255 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
256 $dirty = 0;
257 } else {
258 die "Exiting: your CVS tree is not clean for this merge.";
259 }
260}
261
262print "Applying\n";
263`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
264
265print "Patch applied successfully. Adding new files and directories to CVS\n";
266my $dirtypatch = 0;
267
268#
269# We have to add the directories in order otherwise we will have
270# problems when we try and add the sub-directory of a directory we
271# have not added yet.
272#
273# Luckily this is easy to deal with by sorting the directories and
274# dealing with the shortest ones first.
275#
276@dirs = sort { length $a <=> length $b} @dirs;
277
278foreach my $d (@dirs) {
279 if (system(@cvs,'add',$d)) {
280 $dirtypatch = 1;
281 warn "Failed to cvs add directory $d -- you may need to do it manually";
282 }
283}
284
285foreach my $f (@afiles) {
286 if (grep { $_ eq $f } @bfiles) {
287 system(@cvs, 'add','-kb',$f);
288 } else {
289 system(@cvs, 'add', $f);
290 }
291 if ($?) {
292 $dirtypatch = 1;
293 warn "Failed to cvs add $f -- you may need to do it manually";
294 }
295}
296
297foreach my $f (@dfiles) {
298 system(@cvs, 'rm', '-f', $f);
299 if ($?) {
300 $dirtypatch = 1;
301 warn "Failed to cvs rm -f $f -- you may need to do it manually";
302 }
303}
304
305print "Commit to CVS\n";
306print "Patch title (first comment line): $title\n";
307my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
308my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
309
310if ($dirtypatch) {
311 print "NOTE: One or more hunks failed to apply cleanly.\n";
312 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
313 print "using a patch program. After applying the patch and resolving the\n";
314 print "problems you may commit using:";
315 print "\n cd \"$opt_w\"" if $opt_w;
316 print "\n $cmd\n\n";
317 exit(1);
318}
319
320if ($opt_c) {
321 print "Autocommit\n $cmd\n";
322 print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files);
323 if ($?) {
324 die "Exiting: The commit did not succeed";
325 }
326 print "Committed successfully to CVS\n";
327 # clean up
328 unlink(".msg");
329} else {
330 print "Ready for you to commit, just run:\n\n $cmd\n";
331}
332
333# clean up
334unlink(".cvsexportcommit.diff");
335
336# CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
337# used by CVS and the one set by subsequence file modifications are different.
338# If they are not different CVS will not detect changes.
339sleep(1);
340
341sub usage {
342 print STDERR <<END;
343Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
344END
345 exit(1);
346}
347
348# An alternative to `command` that allows input to be passed as an array
349# to work around shell problems with weird characters in arguments
350# if the exec returns non-zero we die
351sub safe_pipe_capture {
352 my @output;
353 if (my $pid = open my $child, '-|') {
354 @output = (<$child>);
355 close $child or die join(' ',@_).": $! $?";
356 } else {
357 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
358 }
359 return wantarray ? @output : join('',@output);
360}
361
362sub xargs_safe_pipe_capture {
363 my $MAX_ARG_LENGTH = 65536;
364 my $cmd = shift;
365 my @output;
366 my $output;
367 while(@_) {
368 my @args;
369 my $length = 0;
370 while(@_ && $length < $MAX_ARG_LENGTH) {
371 push @args, shift;
372 $length += length($args[$#args]);
373 }
374 if (wantarray) {
375 push @output, safe_pipe_capture(@$cmd, @args);
376 }
377 else {
378 $output .= safe_pipe_capture(@$cmd, @args);
379 }
380 }
381 return wantarray ? @output : $output;
382}