On error, do not list all commands, but point to --help option
[git/mingw.git] / git-cvsexportcommit.perl
blobf284c88a46b5cc7d6e75b78346829ce03e60b060
1 #!/usr/bin/perl -w
3 # Known limitations:
4 # - does not propagate permissions
5 # - error handling has not been extensively tested
8 use strict;
9 use Getopt::Std;
10 use File::Temp qw(tempdir);
11 use Data::Dumper;
12 use File::Basename qw(basename dirname);
14 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
15 die "GIT_DIR is not defined or is unreadable";
18 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u);
20 getopts('uhPpvcfam:d:');
22 $opt_h && usage();
24 die "Need at least one commit identifier!" unless @ARGV;
26 my @cvs;
27 if ($opt_d) {
28 @cvs = ('cvs', '-d', $opt_d);
29 } else {
30 @cvs = ('cvs');
33 # resolve target commit
34 my $commit;
35 $commit = pop @ARGV;
36 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
37 chomp $commit;
38 if ($?) {
39 die "The commit reference $commit did not resolve!";
42 # resolve what parent we want
43 my $parent;
44 if (@ARGV) {
45 $parent = pop @ARGV;
46 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
47 chomp $parent;
48 if ($?) {
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);
55 my @parents;
56 my $committer;
57 my $author;
58 my $stage = 'headers'; # headers, msg
59 my $title;
60 my $msg = '';
62 foreach my $line (@commit) {
63 chomp $line;
64 if ($stage eq 'headers' && $line eq '') {
65 $stage = 'msg';
66 next;
69 if ($stage eq 'headers') {
70 if ($line =~ m/^parent (\w{40})$/) { # found a parent
71 push @parents, $1;
72 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
73 $author = $1;
74 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
75 $committer = $1;
77 } else {
78 $msg .= $line . "\n";
79 unless ($title) {
80 $title = $line;
85 if ($parent) {
86 my $found;
87 # double check that it's a valid parent
88 foreach my $p (@parents) {
89 if ($p eq $parent) {
90 $found = 1;
91 last;
92 }; # found it
94 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
95 } else { # we don't have a parent from the cmdline...
96 if (@parents == 1) { # it's safe to get it from the commit
97 $parent = $parents[0];
98 } else { # or perhaps not!
99 die "This commit has more than one parent -- please name the parent you want to use explicitly";
103 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
105 # grab the commit message
106 open(MSG, ">.msg") or die "Cannot open .msg for writing";
107 if ($opt_m) {
108 print MSG $opt_m;
110 print MSG $msg;
111 if ($opt_a) {
112 print MSG "\n\nAuthor: $author\n";
113 if ($author ne $committer) {
114 print MSG "Committer: $committer\n";
117 close MSG;
119 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
121 ## apply non-binary changes
123 # In pedantic mode require all lines of context to match. In normal
124 # mode, be compatible with diff/patch: assume 3 lines of context and
125 # require at least one line match, i.e. ignore at most 2 lines of
126 # context, like diff/patch do by default.
127 my $context = $opt_p ? '' : '-C1';
129 print "Checking if patch will apply\n";
131 my @stat;
132 open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
133 @stat=<APPLY>;
134 close APPLY || die "Cannot patch";
135 my (@bfiles,@files,@afiles,@dfiles);
136 chomp @stat;
137 foreach (@stat) {
138 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
139 push (@files, $1) if m/^-\t-\t(.*)$/;
140 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
141 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
142 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
144 map { s/^"(.*)"$/$1/g } @bfiles,@files;
145 map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
147 # check that the files are clean and up to date according to cvs
148 my $dirty;
149 my @dirs;
150 foreach my $p (@afiles) {
151 my $path = dirname $p;
152 while (!-d $path and ! grep { $_ eq $path } @dirs) {
153 unshift @dirs, $path;
154 $path = dirname $path;
158 # ... check dirs,
159 foreach my $d (@dirs) {
160 if (-e $d) {
161 $dirty = 1;
162 warn "$d exists and is not a directory!\n";
166 # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
167 my @canstatusfiles;
168 foreach my $f (@files) {
169 my $path = dirname $f;
170 next if (grep { $_ eq $path } @dirs);
171 push @canstatusfiles, $f;
174 my %cvsstat;
175 if (@canstatusfiles) {
176 if ($opt_u) {
177 my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles);
178 print @updated;
180 my @cvsoutput;
181 @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles);
182 my $matchcount = 0;
183 foreach my $l (@cvsoutput) {
184 chomp $l;
185 if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) {
186 $cvsstat{$canstatusfiles[$matchcount]} = $1;
187 $matchcount++;
192 # ... validate new files,
193 foreach my $f (@afiles) {
194 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
195 $dirty = 1;
196 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";
197 warn "Status was: $cvsstat{$f}\n";
200 # ... validate known files.
201 foreach my $f (@files) {
202 next if grep { $_ eq $f } @afiles;
203 # TODO:we need to handle removed in cvs
204 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
205 $dirty = 1;
206 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
209 if ($dirty) {
210 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
211 $dirty = 0;
212 } else {
213 die "Exiting: your CVS tree is not clean for this merge.";
217 print "Applying\n";
218 `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
220 print "Patch applied successfully. Adding new files and directories to CVS\n";
221 my $dirtypatch = 0;
224 # We have to add the directories in order otherwise we will have
225 # problems when we try and add the sub-directory of a directory we
226 # have not added yet.
228 # Luckily this is easy to deal with by sorting the directories and
229 # dealing with the shortest ones first.
231 @dirs = sort { length $a <=> length $b} @dirs;
233 foreach my $d (@dirs) {
234 if (system(@cvs,'add',$d)) {
235 $dirtypatch = 1;
236 warn "Failed to cvs add directory $d -- you may need to do it manually";
240 foreach my $f (@afiles) {
241 if (grep { $_ eq $f } @bfiles) {
242 system(@cvs, 'add','-kb',$f);
243 } else {
244 system(@cvs, 'add', $f);
246 if ($?) {
247 $dirtypatch = 1;
248 warn "Failed to cvs add $f -- you may need to do it manually";
252 foreach my $f (@dfiles) {
253 system(@cvs, 'rm', '-f', $f);
254 if ($?) {
255 $dirtypatch = 1;
256 warn "Failed to cvs rm -f $f -- you may need to do it manually";
260 print "Commit to CVS\n";
261 print "Patch title (first comment line): $title\n";
262 my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
263 my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
265 if ($dirtypatch) {
266 print "NOTE: One or more hunks failed to apply cleanly.\n";
267 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
268 print "using a patch program. After applying the patch and resolving the\n";
269 print "problems you may commit using:";
270 print "\n $cmd\n\n";
271 exit(1);
274 if ($opt_c) {
275 print "Autocommit\n $cmd\n";
276 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
277 if ($?) {
278 die "Exiting: The commit did not succeed";
280 print "Committed successfully to CVS\n";
281 # clean up
282 unlink(".msg");
283 } else {
284 print "Ready for you to commit, just run:\n\n $cmd\n";
287 # clean up
288 unlink(".cvsexportcommit.diff");
290 # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
291 # used by CVS and the one set by subsequence file modifications are different.
292 # If they are not different CVS will not detect changes.
293 sleep(1);
295 sub usage {
296 print STDERR <<END;
297 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
299 exit(1);
302 # An alternative to `command` that allows input to be passed as an array
303 # to work around shell problems with weird characters in arguments
304 # if the exec returns non-zero we die
305 sub safe_pipe_capture {
306 my @output;
307 if (my $pid = open my $child, '-|') {
308 @output = (<$child>);
309 close $child or die join(' ',@_).": $! $?";
310 } else {
311 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
313 return wantarray ? @output : join('',@output);
316 sub safe_pipe_capture_blob {
317 my $output;
318 if (my $pid = open my $child, '-|') {
319 local $/;
320 undef $/;
321 $output = (<$child>);
322 close $child or die join(' ',@_).": $! $?";
323 } else {
324 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
326 return $output;