blob: a33fa8d4c86da54a4d741cf921d1cf7b2138d186 [file] [log] [blame]
Martin Langhoff5e0306a2005-11-07 17:57:08 +13001#!/usr/bin/perl -w
2
Yann Dirson0d71b312006-05-27 18:39:33 +02003# Known limitations:
Yann Dirson0d71b312006-05-27 18:39:33 +02004# - does not propagate permissions
Robin Rosenberge86ad712006-12-11 00:30:06 +01005# - error handling has not been extensively tested
6#
Yann Dirson0d71b312006-05-27 18:39:33 +02007
Martin Langhoff5e0306a2005-11-07 17:57:08 +13008use strict;
9use Getopt::Std;
10use File::Temp qw(tempdir);
11use Data::Dumper;
Yann Dirson3f0f7562006-05-27 18:39:35 +020012use File::Basename qw(basename dirname);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130013
14unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
15 die "GIT_DIR is not defined or is unreadable";
16}
17
Robin Rosenberge5d80642007-05-24 17:06:55 +020018our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130019
Robin Rosenberge5d80642007-05-24 17:06:55 +020020getopts('uhPpvcfam:d:');
Martin Langhoff5e0306a2005-11-07 17:57:08 +130021
22$opt_h && usage();
23
24die "Need at least one commit identifier!" unless @ARGV;
25
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +010026my @cvs;
27if ($opt_d) {
28 @cvs = ('cvs', '-d', $opt_d);
29} else {
30 @cvs = ('cvs');
31}
32
Martin Langhoff5e0306a2005-11-07 17:57:08 +130033# setup a tempdir
34our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
35 TMPDIR => 1,
36 CLEANUP => 1);
37
Martin Langhoff5e0306a2005-11-07 17:57:08 +130038# resolve target commit
39my $commit;
40$commit = pop @ARGV;
Martin Langhoffd41df152006-01-30 19:12:12 +130041$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
Martin Langhoff5e0306a2005-11-07 17:57:08 +130042chomp $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;
Martin Langhoffd41df152006-01-30 19:12:12 +130051 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
Martin Langhoff5e0306a2005-11-07 17:57:08 +130052 chomp $parent;
53 if ($?) {
54 die "The parent reference did not resolve!";
55 }
56}
57
58# find parents from the commit itself
Martin Langhoffd41df152006-01-30 19:12:12 +130059my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130060my @parents;
Martin Langhoff1b91abe2006-07-18 14:22:49 +120061my $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;
Martin Langhoff5e0306a2005-11-07 17:57:08 +130072 }
Martin Langhoff1b91abe2006-07-18 14:22:49 +120073
74 if ($stage eq 'headers') {
75 if ($line =~ m/^parent (\w{40})$/) { # found a parent
76 push @parents, $1;
Robin Rosenbergfe142b32006-11-12 16:29:42 +010077 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
Martin Langhoff1b91abe2006-07-18 14:22:49 +120078 $author = $1;
Robin Rosenbergfe142b32006-11-12 16:29:42 +010079 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
Martin Langhoff1b91abe2006-07-18 14:22:49 +120080 $committer = $1;
81 }
82 } else {
83 $msg .= $line . "\n";
84 unless ($title) {
85 $title = $line;
86 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +130087 }
88}
89
90if ($parent) {
Peter Baumann135a5222006-07-07 12:55:41 +020091 my $found;
Martin Langhoff5e0306a2005-11-07 17:57:08 +130092 # double check that it's a valid parent
93 foreach my $p (@parents) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +130094 if ($p eq $parent) {
95 $found = 1;
96 last;
97 }; # found it
Alexander Litvinove09f5d72005-11-09 13:02:58 +060098 }
Simon 'corecode' Schubertca283702007-02-01 11:43:39 +010099 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300100} 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
Martin Langhoff992793c2006-04-26 12:26:16 +1200111open(MSG, ">.msg") or die "Cannot open .msg for writing";
Martin Langhoff1b91abe2006-07-18 14:22:49 +1200112if ($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}
Martin Langhoff992793c2006-04-26 12:26:16 +1200122close MSG;
123
Robin Rosenberge86ad712006-12-11 00:30:06 +0100124`git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100125
Robin Rosenberge86ad712006-12-11 00:30:06 +0100126## apply non-binary changes
Tomash Brechkofc1f4582007-04-09 15:24:02 +0400127
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';
Robin Rosenberge86ad712006-12-11 00:30:06 +0100133
134print "Checking if patch will apply\n";
135
136my @stat;
Tomash Brechkofc1f4582007-04-09 15:24:02 +0400137open APPLY, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100138@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]+ (.*)$/;
148}
149map { s/^"(.*)"$/$1/g } @bfiles,@files;
150map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300151
152# check that the files are clean and up to date according to cvs
153my $dirty;
Robin Rosenberge86ad712006-12-11 00:30:06 +0100154my @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
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200163# ... check dirs,
Yann Dirson3f0f7562006-05-27 18:39:35 +0200164foreach my $d (@dirs) {
165 if (-e $d) {
166 $dirty = 1;
167 warn "$d exists and is not a directory!\n";
168 }
169}
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200170
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) {
Robin Rosenberge5d80642007-05-24 17:06:55 +0200181 if ($opt_u) {
182 my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles);
183 print @updated;
184 }
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200185 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 }
Yann Dirson576cfc82006-01-06 21:54:41 +0100194 }
195}
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100196
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200197# ... validate new files,
198foreach my $f (@afiles) {
199 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700200 $dirty = 1;
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200201 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";
202 warn "Status was: $cvsstat{$f}\n";
203 }
204}
205# ... validate known files.
Robin Rosenberge86ad712006-12-11 00:30:06 +0100206foreach my $f (@files) {
207 next if grep { $_ eq $f } @afiles;
Yann Dirson576cfc82006-01-06 21:54:41 +0100208 # TODO:we need to handle removed in cvs
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200209 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300210 $dirty = 1;
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200211 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300212 }
213}
214if ($dirty) {
Martin Langhoff992793c2006-04-26 12:26:16 +1200215 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 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300220}
221
Robin Rosenberge86ad712006-12-11 00:30:06 +0100222print "Applying\n";
Tomash Brechkofc1f4582007-04-09 15:24:02 +0400223`GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300224
Robin Rosenberge86ad712006-12-11 00:30:06 +0100225print "Patch applied successfully. Adding new files and directories to CVS\n";
226my $dirtypatch = 0;
Yann Dirson3f0f7562006-05-27 18:39:35 +0200227foreach my $d (@dirs) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100228 if (system(@cvs,'add',$d)) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100229 $dirtypatch = 1;
Yann Dirson3f0f7562006-05-27 18:39:35 +0200230 warn "Failed to cvs add directory $d -- you may need to do it manually";
231 }
232}
233
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300234foreach my $f (@afiles) {
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100235 if (grep { $_ eq $f } @bfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100236 system(@cvs, 'add','-kb',$f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100237 } else {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100238 system(@cvs, 'add', $f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100239 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300240 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100241 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300242 warn "Failed to cvs add $f -- you may need to do it manually";
243 }
244}
245
246foreach my $f (@dfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100247 system(@cvs, 'rm', '-f', $f);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300248 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100249 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300250 warn "Failed to cvs rm -f $f -- you may need to do it manually";
251 }
252}
253
254print "Commit to CVS\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100255print "Patch title (first comment line): $title\n";
256my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100257my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300258
259if ($dirtypatch) {
260 print "NOTE: One or more hunks failed to apply cleanly.\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100261 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:";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300264 print "\n $cmd\n\n";
Junio C Hamano27dedf02005-11-16 21:32:44 -0800265 exit(1);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300266}
267
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300268if ($opt_c) {
269 print "Autocommit\n $cmd\n";
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100270 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300271 if ($?) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300272 die "Exiting: The commit did not succeed";
273 }
274 print "Committed successfully to CVS\n";
Gerrit Papecf70c162007-02-28 12:35:39 +0000275 # clean up
276 unlink(".msg");
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300277} else {
278 print "Ready for you to commit, just run:\n\n $cmd\n";
279}
Robin Rosenberge86ad712006-12-11 00:30:06 +0100280
281# clean up
282unlink(".cvsexportcommit.diff");
Robin Rosenberge86ad712006-12-11 00:30:06 +0100283
Robin Rosenbergf836f1a2007-07-25 00:56:20 +0200284# 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
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300289sub usage {
290 print STDERR <<END;
Martin Langhoff992793c2006-04-26 12:26:16 +1200291Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300292END
293 exit(1);
294}
295
Pavel Roskin82e5a822006-07-10 01:50:18 -0400296# An alternative to `command` that allows input to be passed as an array
Martin Langhoffd41df152006-01-30 19:12:12 +1300297# 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}
Jim Meyering7c0f7022006-12-04 08:44:08 +0100309
Robin Rosenberge86ad712006-12-11 00:30:06 +0100310sub 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;
Jim Meyering7c0f7022006-12-04 08:44:08 +0100321}