]> git.ipfire.org Git - thirdparty/git.git/blame - git-cvsexportcommit.perl
Avoid scary errors about tagged trees/blobs during git-fetch
[thirdparty/git.git] / git-cvsexportcommit.perl
CommitLineData
5e0306ad
ML
1#!/usr/bin/perl -w
2
0d71b31a 3# Known limitations:
0d71b31a 4# - does not propagate permissions
e86ad71f
RR
5# - error handling has not been extensively tested
6#
0d71b31a 7
5e0306ad
ML
8use strict;
9use Getopt::Std;
10use File::Temp qw(tempdir);
11use Data::Dumper;
3f0f756b 12use File::Basename qw(basename dirname);
5e0306ad
ML
13
14unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
15 die "GIT_DIR is not defined or is unreadable";
16}
17
e5d80641 18our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u);
5e0306ad 19
e5d80641 20getopts('uhPpvcfam:d:');
5e0306ad
ML
21
22$opt_h && usage();
23
24die "Need at least one commit identifier!" unless @ARGV;
25
4a6b9bb6
SS
26my @cvs;
27if ($opt_d) {
28 @cvs = ('cvs', '-d', $opt_d);
29} else {
30 @cvs = ('cvs');
31}
32
5e0306ad
ML
33# setup a tempdir
34our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
35 TMPDIR => 1,
36 CLEANUP => 1);
37
5e0306ad
ML
38# resolve target commit
39my $commit;
40$commit = pop @ARGV;
d41df15e 41$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
5e0306ad
ML
42chomp $commit;
43if ($?) {
44 die "The commit reference $commit did not resolve!";
45}
46
47# resolve what parent we want
48my $parent;
49if (@ARGV) {
50 $parent = pop @ARGV;
d41df15e 51 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
5e0306ad
ML
52 chomp $parent;
53 if ($?) {
54 die "The parent reference did not resolve!";
55 }
56}
57
58# find parents from the commit itself
d41df15e 59my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
5e0306ad 60my @parents;
1b91abe3
ML
61my $committer;
62my $author;
63my $stage = 'headers'; # headers, msg
64my $title;
65my $msg = '';
66
67foreach my $line (@commit) {
68 chomp $line;
69 if ($stage eq 'headers' && $line eq '') {
70 $stage = 'msg';
71 next;
5e0306ad 72 }
1b91abe3
ML
73
74 if ($stage eq 'headers') {
75 if ($line =~ m/^parent (\w{40})$/) { # found a parent
76 push @parents, $1;
fe142b3a 77 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
1b91abe3 78 $author = $1;
fe142b3a 79 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
1b91abe3
ML
80 $committer = $1;
81 }
82 } else {
83 $msg .= $line . "\n";
84 unless ($title) {
85 $title = $line;
86 }
5e0306ad
ML
87 }
88}
89
90if ($parent) {
135a522e 91 my $found;
5e0306ad
ML
92 # double check that it's a valid parent
93 foreach my $p (@parents) {
5e0306ad
ML
94 if ($p eq $parent) {
95 $found = 1;
96 last;
97 }; # found it
e09f5d7b 98 }
ca28370a 99 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
5e0306ad
ML
100} else { # we don't have a parent from the cmdline...
101 if (@parents == 1) { # it's safe to get it from the commit
102 $parent = $parents[0];
103 } else { # or perhaps not!
104 die "This commit has more than one parent -- please name the parent you want to use explicitly";
105 }
106}
107
108$opt_v && print "Applying to CVS commit $commit from parent $parent\n";
109
110# grab the commit message
992793c8 111open(MSG, ">.msg") or die "Cannot open .msg for writing";
1b91abe3
ML
112if ($opt_m) {
113 print MSG $opt_m;
114}
115print MSG $msg;
116if ($opt_a) {
117 print MSG "\n\nAuthor: $author\n";
118 if ($author ne $committer) {
119 print MSG "Committer: $committer\n";
120 }
121}
992793c8
ML
122close MSG;
123
e86ad71f
RR
124`git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
125
126## apply non-binary changes
fc1f458c
TB
127
128# In pedantic mode require all lines of context to match. In normal
129# mode, be compatible with diff/patch: assume 3 lines of context and
130# require at least one line match, i.e. ignore at most 2 lines of
131# context, like diff/patch do by default.
132my $context = $opt_p ? '' : '-C1';
e86ad71f
RR
133
134print "Checking if patch will apply\n";
135
136my @stat;
fc1f458c 137open APPLY, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
e86ad71f
RR
138@stat=<APPLY>;
139close APPLY || die "Cannot patch";
140my (@bfiles,@files,@afiles,@dfiles);
141chomp @stat;
142foreach (@stat) {
143 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
144 push (@files, $1) if m/^-\t-\t(.*)$/;
145 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
146 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
147 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
5e0306ad 148}
e86ad71f
RR
149map { s/^"(.*)"$/$1/g } @bfiles,@files;
150map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
5e0306ad
ML
151
152# check that the files are clean and up to date according to cvs
153my $dirty;
e86ad71f
RR
154my @dirs;
155foreach my $p (@afiles) {
156 my $path = dirname $p;
157 while (!-d $path and ! grep { $_ eq $path } @dirs) {
158 unshift @dirs, $path;
159 $path = dirname $path;
160 }
161}
162
c56f0d9c 163# ... check dirs,
3f0f756b
YD
164foreach my $d (@dirs) {
165 if (-e $d) {
166 $dirty = 1;
167 warn "$d exists and is not a directory!\n";
168 }
169}
c56f0d9c
SP
170
171# ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
172my @canstatusfiles;
173foreach my $f (@files) {
174 my $path = dirname $f;
175 next if (grep { $_ eq $path } @dirs);
176 push @canstatusfiles, $f;
177}
178
179my %cvsstat;
180if (@canstatusfiles) {
e5d80641
RR
181 if ($opt_u) {
182 my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles);
183 print @updated;
184 }
c56f0d9c
SP
185 my @cvsoutput;
186 @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles);
187 my $matchcount = 0;
188 foreach my $l (@cvsoutput) {
189 chomp $l;
190 if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) {
191 $cvsstat{$canstatusfiles[$matchcount]} = $1;
192 $matchcount++;
193 }
fe142b3a 194 }
c56f0d9c
SP
195}
196
197# ... validate new files,
198foreach my $f (@afiles) {
199 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
a6080a0a 200 $dirty = 1;
992793c8 201 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";
c56f0d9c 202 warn "Status was: $cvsstat{$f}\n";
576cfc86
YD
203 }
204}
c56f0d9c 205# ... validate known files.
e86ad71f
RR
206foreach my $f (@files) {
207 next if grep { $_ eq $f } @afiles;
576cfc86 208 # TODO:we need to handle removed in cvs
c56f0d9c 209 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
5e0306ad 210 $dirty = 1;
c56f0d9c 211 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
5e0306ad
ML
212 }
213}
214if ($dirty) {
992793c8
ML
215 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
216 $dirty = 0;
217 } else {
218 die "Exiting: your CVS tree is not clean for this merge.";
219 }
5e0306ad
ML
220}
221
e86ad71f 222print "Applying\n";
fc1f458c 223`GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
5e0306ad 224
e86ad71f
RR
225print "Patch applied successfully. Adding new files and directories to CVS\n";
226my $dirtypatch = 0;
3f0f756b 227foreach my $d (@dirs) {
4a6b9bb6 228 if (system(@cvs,'add',$d)) {
e86ad71f 229 $dirtypatch = 1;
3f0f756b
YD
230 warn "Failed to cvs add directory $d -- you may need to do it manually";
231 }
232}
233
5e0306ad 234foreach my $f (@afiles) {
fe142b3a 235 if (grep { $_ eq $f } @bfiles) {
4a6b9bb6 236 system(@cvs, 'add','-kb',$f);
fe142b3a 237 } else {
4a6b9bb6 238 system(@cvs, 'add', $f);
fe142b3a 239 }
5e0306ad 240 if ($?) {
e86ad71f 241 $dirtypatch = 1;
5e0306ad
ML
242 warn "Failed to cvs add $f -- you may need to do it manually";
243 }
244}
245
246foreach my $f (@dfiles) {
4a6b9bb6 247 system(@cvs, 'rm', '-f', $f);
5e0306ad 248 if ($?) {
e86ad71f 249 $dirtypatch = 1;
5e0306ad
ML
250 warn "Failed to cvs rm -f $f -- you may need to do it manually";
251 }
252}
253
254print "Commit to CVS\n";
e86ad71f
RR
255print "Patch title (first comment line): $title\n";
256my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
4a6b9bb6 257my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
5e0306ad
ML
258
259if ($dirtypatch) {
260 print "NOTE: One or more hunks failed to apply cleanly.\n";
e86ad71f
RR
261 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
262 print "using a patch program. After applying the patch and resolving the\n";
263 print "problems you may commit using:";
5e0306ad 264 print "\n $cmd\n\n";
27dedf0c 265 exit(1);
5e0306ad
ML
266}
267
5e0306ad
ML
268if ($opt_c) {
269 print "Autocommit\n $cmd\n";
4a6b9bb6 270 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
5e0306ad 271 if ($?) {
5e0306ad
ML
272 die "Exiting: The commit did not succeed";
273 }
274 print "Committed successfully to CVS\n";
cf70c16f
GP
275 # clean up
276 unlink(".msg");
5e0306ad
ML
277} else {
278 print "Ready for you to commit, just run:\n\n $cmd\n";
279}
e86ad71f
RR
280
281# clean up
282unlink(".cvsexportcommit.diff");
e86ad71f 283
f836f1ae
RR
284# CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
285# used by CVS and the one set by subsequence file modifications are different.
286# If they are not different CVS will not detect changes.
287sleep(1);
288
5e0306ad
ML
289sub usage {
290 print STDERR <<END;
992793c8 291Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
5e0306ad
ML
292END
293 exit(1);
294}
295
82e5a82f 296# An alternative to `command` that allows input to be passed as an array
d41df15e
ML
297# to work around shell problems with weird characters in arguments
298# if the exec returns non-zero we die
299sub safe_pipe_capture {
300 my @output;
301 if (my $pid = open my $child, '-|') {
302 @output = (<$child>);
303 close $child or die join(' ',@_).": $! $?";
304 } else {
305 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
306 }
307 return wantarray ? @output : join('',@output);
308}
7c0f7028 309
e86ad71f
RR
310sub safe_pipe_capture_blob {
311 my $output;
312 if (my $pid = open my $child, '-|') {
313 local $/;
314 undef $/;
315 $output = (<$child>);
316 close $child or die join(' ',@_).": $! $?";
317 } else {
318 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
319 }
320 return $output;
7c0f7028 321}