blob: 92e41620fd2a998e765a47c2c2ca7085834b14cc [file] [log] [blame]
Martin Langhoff5e0306a2005-11-07 17:57:08 +13001#!/usr/bin/perl -w
2
3use strict;
4use Getopt::Std;
5use File::Temp qw(tempdir);
6use Data::Dumper;
Yann Dirson3f0f7562006-05-27 18:39:35 +02007use File::Basename qw(basename dirname);
Martin Langhoff5e0306a2005-11-07 17:57:08 +13008
Robin Rosenberg648ee552007-10-31 23:12:20 +01009our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130010
Robin Rosenberg648ee552007-10-31 23:12:20 +010011getopts('uhPpvcfam:d:w:');
Martin Langhoff5e0306a2005-11-07 17:57:08 +130012
13$opt_h && usage();
14
15die "Need at least one commit identifier!" unless @ARGV;
16
Robin Rosenberg648ee552007-10-31 23:12:20 +010017if ($opt_w) {
18 unless ($ENV{GIT_DIR}) {
19 # Remember where our GIT_DIR is before changing to CVS checkout
20 my $gd =`git-rev-parse --git-dir`;
21 chomp($gd);
22 if ($gd eq '.git') {
23 my $wd = `pwd`;
24 chomp($wd);
25 $gd = $wd."/.git" ;
26 }
27 $ENV{GIT_DIR} = $gd;
28 }
29
30 if (! -d $opt_w."/CVS" ) {
31 die "$opt_w is not a CVS checkout";
32 }
33 chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
34}
35unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
36 die "GIT_DIR is not defined or is unreadable";
37}
38
39
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +010040my @cvs;
41if ($opt_d) {
42 @cvs = ('cvs', '-d', $opt_d);
43} else {
44 @cvs = ('cvs');
45}
46
Martin Langhoff5e0306a2005-11-07 17:57:08 +130047# resolve target commit
48my $commit;
49$commit = pop @ARGV;
Martin Langhoffd41df152006-01-30 19:12:12 +130050$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
Martin Langhoff5e0306a2005-11-07 17:57:08 +130051chomp $commit;
52if ($?) {
53 die "The commit reference $commit did not resolve!";
54}
55
56# resolve what parent we want
57my $parent;
58if (@ARGV) {
59 $parent = pop @ARGV;
Martin Langhoffd41df152006-01-30 19:12:12 +130060 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
Martin Langhoff5e0306a2005-11-07 17:57:08 +130061 chomp $parent;
62 if ($?) {
63 die "The parent reference did not resolve!";
64 }
65}
66
67# find parents from the commit itself
Martin Langhoffd41df152006-01-30 19:12:12 +130068my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130069my @parents;
Martin Langhoff1b91abe2006-07-18 14:22:49 +120070my $committer;
71my $author;
72my $stage = 'headers'; # headers, msg
73my $title;
74my $msg = '';
75
76foreach my $line (@commit) {
77 chomp $line;
78 if ($stage eq 'headers' && $line eq '') {
79 $stage = 'msg';
80 next;
Martin Langhoff5e0306a2005-11-07 17:57:08 +130081 }
Martin Langhoff1b91abe2006-07-18 14:22:49 +120082
83 if ($stage eq 'headers') {
84 if ($line =~ m/^parent (\w{40})$/) { # found a parent
85 push @parents, $1;
Robin Rosenbergfe142b32006-11-12 16:29:42 +010086 } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
Martin Langhoff1b91abe2006-07-18 14:22:49 +120087 $author = $1;
Robin Rosenbergfe142b32006-11-12 16:29:42 +010088 } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
Martin Langhoff1b91abe2006-07-18 14:22:49 +120089 $committer = $1;
90 }
91 } else {
92 $msg .= $line . "\n";
93 unless ($title) {
94 $title = $line;
95 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +130096 }
97}
98
Brad King6b6012e2007-10-31 16:55:13 -040099my $noparent = "0000000000000000000000000000000000000000";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300100if ($parent) {
Peter Baumann135a5222006-07-07 12:55:41 +0200101 my $found;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300102 # double check that it's a valid parent
103 foreach my $p (@parents) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300104 if ($p eq $parent) {
105 $found = 1;
106 last;
107 }; # found it
Alexander Litvinove09f5d72005-11-09 13:02:58 +0600108 }
Simon 'corecode' Schubertca283702007-02-01 11:43:39 +0100109 die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300110} else { # we don't have a parent from the cmdline...
111 if (@parents == 1) { # it's safe to get it from the commit
112 $parent = $parents[0];
Brad King6b6012e2007-10-31 16:55:13 -0400113 } elsif (@parents == 0) { # there is no parent
114 $parent = $noparent;
115 } else { # cannot choose automatically from multiple parents
116 die "This commit has more than one parent -- please name the parent you want to use explicitly";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300117 }
118}
119
120$opt_v && print "Applying to CVS commit $commit from parent $parent\n";
121
122# grab the commit message
Martin Langhoff992793c2006-04-26 12:26:16 +1200123open(MSG, ">.msg") or die "Cannot open .msg for writing";
Martin Langhoff1b91abe2006-07-18 14:22:49 +1200124if ($opt_m) {
125 print MSG $opt_m;
126}
127print MSG $msg;
128if ($opt_a) {
129 print MSG "\n\nAuthor: $author\n";
130 if ($author ne $committer) {
131 print MSG "Committer: $committer\n";
132 }
133}
Martin Langhoff992793c2006-04-26 12:26:16 +1200134close MSG;
135
Brad King6b6012e2007-10-31 16:55:13 -0400136if ($parent eq $noparent) {
137 `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
138} else {
139 `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
140}
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100141
Robin Rosenberge86ad712006-12-11 00:30:06 +0100142## apply non-binary changes
Tomash Brechkofc1f4582007-04-09 15:24:02 +0400143
144# In pedantic mode require all lines of context to match. In normal
145# mode, be compatible with diff/patch: assume 3 lines of context and
146# require at least one line match, i.e. ignore at most 2 lines of
147# context, like diff/patch do by default.
148my $context = $opt_p ? '' : '-C1';
Robin Rosenberge86ad712006-12-11 00:30:06 +0100149
150print "Checking if patch will apply\n";
151
152my @stat;
Michael Witten54f0db72007-10-16 04:08:14 -0400153open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100154@stat=<APPLY>;
155close APPLY || die "Cannot patch";
156my (@bfiles,@files,@afiles,@dfiles);
157chomp @stat;
158foreach (@stat) {
159 push (@bfiles,$1) if m/^-\t-\t(.*)$/;
160 push (@files, $1) if m/^-\t-\t(.*)$/;
161 push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
162 push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
163 push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
164}
165map { s/^"(.*)"$/$1/g } @bfiles,@files;
166map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300167
168# check that the files are clean and up to date according to cvs
169my $dirty;
Robin Rosenberge86ad712006-12-11 00:30:06 +0100170my @dirs;
171foreach my $p (@afiles) {
172 my $path = dirname $p;
173 while (!-d $path and ! grep { $_ eq $path } @dirs) {
174 unshift @dirs, $path;
175 $path = dirname $path;
176 }
177}
178
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200179# ... check dirs,
Yann Dirson3f0f7562006-05-27 18:39:35 +0200180foreach my $d (@dirs) {
181 if (-e $d) {
182 $dirty = 1;
183 warn "$d exists and is not a directory!\n";
184 }
185}
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200186
187# ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
188my @canstatusfiles;
189foreach my $f (@files) {
190 my $path = dirname $f;
191 next if (grep { $_ eq $path } @dirs);
192 push @canstatusfiles, $f;
193}
194
195my %cvsstat;
196if (@canstatusfiles) {
Robin Rosenberge5d80642007-05-24 17:06:55 +0200197 if ($opt_u) {
198 my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles);
199 print @updated;
200 }
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200201 my @cvsoutput;
202 @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles);
203 my $matchcount = 0;
204 foreach my $l (@cvsoutput) {
205 chomp $l;
206 if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) {
207 $cvsstat{$canstatusfiles[$matchcount]} = $1;
208 $matchcount++;
209 }
Yann Dirson576cfc82006-01-06 21:54:41 +0100210 }
211}
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100212
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200213# ... validate new files,
214foreach my $f (@afiles) {
215 if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700216 $dirty = 1;
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200217 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";
218 warn "Status was: $cvsstat{$f}\n";
219 }
220}
221# ... validate known files.
Robin Rosenberge86ad712006-12-11 00:30:06 +0100222foreach my $f (@files) {
223 next if grep { $_ eq $f } @afiles;
Yann Dirson576cfc82006-01-06 21:54:41 +0100224 # TODO:we need to handle removed in cvs
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200225 unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300226 $dirty = 1;
Steffen Prohaskac56f0d92007-05-10 01:06:36 +0200227 warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300228 }
229}
230if ($dirty) {
Martin Langhoff992793c2006-04-26 12:26:16 +1200231 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
232 $dirty = 0;
233 } else {
234 die "Exiting: your CVS tree is not clean for this merge.";
235 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300236}
237
Robin Rosenberge86ad712006-12-11 00:30:06 +0100238print "Applying\n";
Michael Witten54f0db72007-10-16 04:08:14 -0400239`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300240
Robin Rosenberge86ad712006-12-11 00:30:06 +0100241print "Patch applied successfully. Adding new files and directories to CVS\n";
242my $dirtypatch = 0;
Alex Benneefd0b9592007-10-18 17:15:44 +0100243
244#
245# We have to add the directories in order otherwise we will have
246# problems when we try and add the sub-directory of a directory we
247# have not added yet.
248#
249# Luckily this is easy to deal with by sorting the directories and
250# dealing with the shortest ones first.
251#
252@dirs = sort { length $a <=> length $b} @dirs;
253
Yann Dirson3f0f7562006-05-27 18:39:35 +0200254foreach my $d (@dirs) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100255 if (system(@cvs,'add',$d)) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100256 $dirtypatch = 1;
Yann Dirson3f0f7562006-05-27 18:39:35 +0200257 warn "Failed to cvs add directory $d -- you may need to do it manually";
258 }
259}
260
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300261foreach my $f (@afiles) {
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100262 if (grep { $_ eq $f } @bfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100263 system(@cvs, 'add','-kb',$f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100264 } else {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100265 system(@cvs, 'add', $f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100266 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300267 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100268 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300269 warn "Failed to cvs add $f -- you may need to do it manually";
270 }
271}
272
273foreach my $f (@dfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100274 system(@cvs, 'rm', '-f', $f);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300275 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100276 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300277 warn "Failed to cvs rm -f $f -- you may need to do it manually";
278 }
279}
280
281print "Commit to CVS\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100282print "Patch title (first comment line): $title\n";
283my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100284my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300285
286if ($dirtypatch) {
287 print "NOTE: One or more hunks failed to apply cleanly.\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100288 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
289 print "using a patch program. After applying the patch and resolving the\n";
290 print "problems you may commit using:";
Robin Rosenberg648ee552007-10-31 23:12:20 +0100291 print "\n cd \"$opt_w\"" if $opt_w;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300292 print "\n $cmd\n\n";
Junio C Hamano27dedf02005-11-16 21:32:44 -0800293 exit(1);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300294}
295
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300296if ($opt_c) {
297 print "Autocommit\n $cmd\n";
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100298 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300299 if ($?) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300300 die "Exiting: The commit did not succeed";
301 }
302 print "Committed successfully to CVS\n";
Gerrit Papecf70c162007-02-28 12:35:39 +0000303 # clean up
304 unlink(".msg");
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300305} else {
306 print "Ready for you to commit, just run:\n\n $cmd\n";
307}
Robin Rosenberge86ad712006-12-11 00:30:06 +0100308
309# clean up
310unlink(".cvsexportcommit.diff");
Robin Rosenberge86ad712006-12-11 00:30:06 +0100311
Robin Rosenbergf836f1a2007-07-25 00:56:20 +0200312# CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
313# used by CVS and the one set by subsequence file modifications are different.
314# If they are not different CVS will not detect changes.
315sleep(1);
316
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300317sub usage {
318 print STDERR <<END;
Robin Rosenberg648ee552007-10-31 23:12:20 +0100319Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300320END
321 exit(1);
322}
323
Pavel Roskin82e5a822006-07-10 01:50:18 -0400324# An alternative to `command` that allows input to be passed as an array
Martin Langhoffd41df152006-01-30 19:12:12 +1300325# to work around shell problems with weird characters in arguments
326# if the exec returns non-zero we die
327sub safe_pipe_capture {
328 my @output;
329 if (my $pid = open my $child, '-|') {
330 @output = (<$child>);
331 close $child or die join(' ',@_).": $! $?";
332 } else {
333 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
334 }
335 return wantarray ? @output : join('',@output);
336}
Jim Meyering7c0f7022006-12-04 08:44:08 +0100337
Robin Rosenberge86ad712006-12-11 00:30:06 +0100338sub safe_pipe_capture_blob {
339 my $output;
340 if (my $pid = open my $child, '-|') {
341 local $/;
342 undef $/;
343 $output = (<$child>);
344 close $child or die join(' ',@_).": $! $?";
345 } else {
346 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
347 }
348 return $output;
Jim Meyering7c0f7022006-12-04 08:44:08 +0100349}