]> git.ipfire.org Git - thirdparty/git.git/blame - git-cvsexportcommit.perl
git-cvsexportcommit.perl: git-apply no longer needs --binary
[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# resolve target commit
34my $commit;
35$commit = pop @ARGV;
d41df15e 36$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
5e0306ad
ML
37chomp $commit;
38if ($?) {
39 die "The commit reference $commit did not resolve!";
40}
41
42# resolve what parent we want
43my $parent;
44if (@ARGV) {
45 $parent = pop @ARGV;
d41df15e 46 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
5e0306ad
ML
47 chomp $parent;
48 if ($?) {
49 die "The parent reference did not resolve!";
50 }
51}
52
53# find parents from the commit itself
d41df15e 54my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
5e0306ad 55my @parents;
1b91abe3
ML
56my $committer;
57my $author;
58my $stage = 'headers'; # headers, msg
59my $title;
60my $msg = '';
61
62foreach my $line (@commit) {
63 chomp $line;
64 if ($stage eq 'headers' && $line eq '') {
65 $stage = 'msg';
66 next;
5e0306ad 67 }
1b91abe3
ML
68
69 if ($stage eq 'headers') {
70 if ($line =~ m/^parent (\w{40})$/) { # found a parent
71 push @parents, $1;
fe142b3a 72 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
1b91abe3 73 $author = $1;
fe142b3a 74 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
1b91abe3
ML
75 $committer = $1;
76 }
77 } else {
78 $msg .= $line . "\n";
79 unless ($title) {
80 $title = $line;
81 }
5e0306ad
ML
82 }
83}
84
85if ($parent) {
135a522e 86 my $found;
5e0306ad
ML
87 # double check that it's a valid parent
88 foreach my $p (@parents) {
5e0306ad
ML
89 if ($p eq $parent) {
90 $found = 1;
91 last;
92 }; # found it
e09f5d7b 93 }
ca28370a 94 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
5e0306ad
ML
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";
100 }
101}
102
103$opt_v && print "Applying to CVS commit $commit from parent $parent\n";
104
105# grab the commit message
992793c8 106open(MSG, ">.msg") or die "Cannot open .msg for writing";
1b91abe3
ML
107if ($opt_m) {
108 print MSG $opt_m;
109}
110print MSG $msg;
111if ($opt_a) {
112 print MSG "\n\nAuthor: $author\n";
113 if ($author ne $committer) {
114 print MSG "Committer: $committer\n";
115 }
116}
992793c8
ML
117close MSG;
118
e86ad71f
RR
119`git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
120
121## apply non-binary changes
fc1f458c
TB
122
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.
127my $context = $opt_p ? '' : '-C1';
e86ad71f
RR
128
129print "Checking if patch will apply\n";
130
131my @stat;
54f0db79 132open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
e86ad71f
RR
133@stat=<APPLY>;
134close APPLY || die "Cannot patch";
135my (@bfiles,@files,@afiles,@dfiles);
136chomp @stat;
137foreach (@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]+ (.*)$/;
5e0306ad 143}
e86ad71f
RR
144map { s/^"(.*)"$/$1/g } @bfiles,@files;
145map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
5e0306ad
ML
146
147# check that the files are clean and up to date according to cvs
148my $dirty;
e86ad71f
RR
149my @dirs;
150foreach 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;
155 }
156}
157
c56f0d9c 158# ... check dirs,
3f0f756b
YD
159foreach my $d (@dirs) {
160 if (-e $d) {
161 $dirty = 1;
162 warn "$d exists and is not a directory!\n";
163 }
164}
c56f0d9c
SP
165
166# ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
167my @canstatusfiles;
168foreach my $f (@files) {
169 my $path = dirname $f;
170 next if (grep { $_ eq $path } @dirs);
171 push @canstatusfiles, $f;
172}
173
174my %cvsstat;
175if (@canstatusfiles) {
e5d80641
RR
176 if ($opt_u) {
177 my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles);
178 print @updated;
179 }
c56f0d9c
SP
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++;
188 }
fe142b3a 189 }
c56f0d9c
SP
190}
191
192# ... validate new files,
193foreach my $f (@afiles) {
194 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
a6080a0a 195 $dirty = 1;
992793c8 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";
c56f0d9c 197 warn "Status was: $cvsstat{$f}\n";
576cfc86
YD
198 }
199}
c56f0d9c 200# ... validate known files.
e86ad71f
RR
201foreach my $f (@files) {
202 next if grep { $_ eq $f } @afiles;
576cfc86 203 # TODO:we need to handle removed in cvs
c56f0d9c 204 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
5e0306ad 205 $dirty = 1;
c56f0d9c 206 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
5e0306ad
ML
207 }
208}
209if ($dirty) {
992793c8
ML
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.";
214 }
5e0306ad
ML
215}
216
e86ad71f 217print "Applying\n";
54f0db79 218`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
5e0306ad 219
e86ad71f
RR
220print "Patch applied successfully. Adding new files and directories to CVS\n";
221my $dirtypatch = 0;
3f0f756b 222foreach my $d (@dirs) {
4a6b9bb6 223 if (system(@cvs,'add',$d)) {
e86ad71f 224 $dirtypatch = 1;
3f0f756b
YD
225 warn "Failed to cvs add directory $d -- you may need to do it manually";
226 }
227}
228
5e0306ad 229foreach my $f (@afiles) {
fe142b3a 230 if (grep { $_ eq $f } @bfiles) {
4a6b9bb6 231 system(@cvs, 'add','-kb',$f);
fe142b3a 232 } else {
4a6b9bb6 233 system(@cvs, 'add', $f);
fe142b3a 234 }
5e0306ad 235 if ($?) {
e86ad71f 236 $dirtypatch = 1;
5e0306ad
ML
237 warn "Failed to cvs add $f -- you may need to do it manually";
238 }
239}
240
241foreach my $f (@dfiles) {
4a6b9bb6 242 system(@cvs, 'rm', '-f', $f);
5e0306ad 243 if ($?) {
e86ad71f 244 $dirtypatch = 1;
5e0306ad
ML
245 warn "Failed to cvs rm -f $f -- you may need to do it manually";
246 }
247}
248
249print "Commit to CVS\n";
e86ad71f
RR
250print "Patch title (first comment line): $title\n";
251my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
4a6b9bb6 252my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
5e0306ad
ML
253
254if ($dirtypatch) {
255 print "NOTE: One or more hunks failed to apply cleanly.\n";
e86ad71f
RR
256 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
257 print "using a patch program. After applying the patch and resolving the\n";
258 print "problems you may commit using:";
5e0306ad 259 print "\n $cmd\n\n";
27dedf0c 260 exit(1);
5e0306ad
ML
261}
262
5e0306ad
ML
263if ($opt_c) {
264 print "Autocommit\n $cmd\n";
4a6b9bb6 265 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
5e0306ad 266 if ($?) {
5e0306ad
ML
267 die "Exiting: The commit did not succeed";
268 }
269 print "Committed successfully to CVS\n";
cf70c16f
GP
270 # clean up
271 unlink(".msg");
5e0306ad
ML
272} else {
273 print "Ready for you to commit, just run:\n\n $cmd\n";
274}
e86ad71f
RR
275
276# clean up
277unlink(".cvsexportcommit.diff");
e86ad71f 278
f836f1ae
RR
279# CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
280# used by CVS and the one set by subsequence file modifications are different.
281# If they are not different CVS will not detect changes.
282sleep(1);
283
5e0306ad
ML
284sub usage {
285 print STDERR <<END;
992793c8 286Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
5e0306ad
ML
287END
288 exit(1);
289}
290
82e5a82f 291# An alternative to `command` that allows input to be passed as an array
d41df15e
ML
292# to work around shell problems with weird characters in arguments
293# if the exec returns non-zero we die
294sub safe_pipe_capture {
295 my @output;
296 if (my $pid = open my $child, '-|') {
297 @output = (<$child>);
298 close $child or die join(' ',@_).": $! $?";
299 } else {
300 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
301 }
302 return wantarray ? @output : join('',@output);
303}
7c0f7028 304
e86ad71f
RR
305sub safe_pipe_capture_blob {
306 my $output;
307 if (my $pid = open my $child, '-|') {
308 local $/;
309 undef $/;
310 $output = (<$child>);
311 close $child or die join(' ',@_).": $! $?";
312 } else {
313 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
314 }
315 return $output;
7c0f7028 316}