blob: 67224b44497715edc07df9d7df469339caa48e51 [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
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +010018our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d);
Martin Langhoff5e0306a2005-11-07 17:57:08 +130019
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +010020getopts('hPpvcfam: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
127my $fuzz = $opt_p ? 0 : 2;
128
129print "Checking if patch will apply\n";
130
131my @stat;
132open APPLY, "GIT_DIR= git-apply -C$fuzz --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
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]+ (.*)$/;
143}
144map { s/^"(.*)"$/$1/g } @bfiles,@files;
145map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300146
147# check that the files are clean and up to date according to cvs
148my $dirty;
Robin Rosenberge86ad712006-12-11 00:30:06 +0100149my @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
Yann Dirson3f0f7562006-05-27 18:39:35 +0200158foreach my $d (@dirs) {
159 if (-e $d) {
160 $dirty = 1;
161 warn "$d exists and is not a directory!\n";
162 }
163}
Yann Dirson576cfc82006-01-06 21:54:41 +0100164foreach my $f (@afiles) {
Martin Langhoffd41df152006-01-30 19:12:12 +1300165 # This should return only one value
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100166 if ($f =~ m,(.*)/[^/]*$,) {
167 my $p = $1;
168 next if (grep { $_ eq $p } @dirs);
169 }
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100170 my @status = grep(m/^File/, safe_pipe_capture(@cvs, '-q', 'status' ,$f));
Martin Langhoffd41df152006-01-30 19:12:12 +1300171 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
Yann Dirson3f0f7562006-05-27 18:39:35 +0200172 if (-d dirname $f and $status[0] !~ m/Status: Unknown$/
173 and $status[0] !~ m/^File: no file /) {
Martin Langhoffd41df152006-01-30 19:12:12 +1300174 $dirty = 1;
Martin Langhoff992793c2006-04-26 12:26:16 +1200175 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";
Sven Verdoolaegee9687512006-06-17 19:46:40 +0200176 warn "Status was: $status[0]\n";
Yann Dirson576cfc82006-01-06 21:54:41 +0100177 }
178}
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100179
Robin Rosenberge86ad712006-12-11 00:30:06 +0100180foreach my $f (@files) {
181 next if grep { $_ eq $f } @afiles;
Yann Dirson576cfc82006-01-06 21:54:41 +0100182 # TODO:we need to handle removed in cvs
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100183 my @status = grep(m/^File/, safe_pipe_capture(@cvs, '-q', 'status' ,$f));
Martin Langhoffd41df152006-01-30 19:12:12 +1300184 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
185 unless ($status[0] =~ m/Status: Up-to-date$/) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300186 $dirty = 1;
187 warn "File $f not up to date in your CVS checkout!\n";
188 }
189}
190if ($dirty) {
Martin Langhoff992793c2006-04-26 12:26:16 +1200191 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
192 $dirty = 0;
193 } else {
194 die "Exiting: your CVS tree is not clean for this merge.";
195 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300196}
197
Robin Rosenberge86ad712006-12-11 00:30:06 +0100198print "Applying\n";
199`GIT_DIR= git-apply -C$fuzz --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300200
Robin Rosenberge86ad712006-12-11 00:30:06 +0100201print "Patch applied successfully. Adding new files and directories to CVS\n";
202my $dirtypatch = 0;
Yann Dirson3f0f7562006-05-27 18:39:35 +0200203foreach my $d (@dirs) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100204 if (system(@cvs,'add',$d)) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100205 $dirtypatch = 1;
Yann Dirson3f0f7562006-05-27 18:39:35 +0200206 warn "Failed to cvs add directory $d -- you may need to do it manually";
207 }
208}
209
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300210foreach my $f (@afiles) {
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100211 if (grep { $_ eq $f } @bfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100212 system(@cvs, 'add','-kb',$f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100213 } else {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100214 system(@cvs, 'add', $f);
Robin Rosenbergfe142b32006-11-12 16:29:42 +0100215 }
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300216 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100217 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300218 warn "Failed to cvs add $f -- you may need to do it manually";
219 }
220}
221
222foreach my $f (@dfiles) {
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100223 system(@cvs, 'rm', '-f', $f);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300224 if ($?) {
Robin Rosenberge86ad712006-12-11 00:30:06 +0100225 $dirtypatch = 1;
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300226 warn "Failed to cvs rm -f $f -- you may need to do it manually";
227 }
228}
229
230print "Commit to CVS\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100231print "Patch title (first comment line): $title\n";
232my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100233my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300234
235if ($dirtypatch) {
236 print "NOTE: One or more hunks failed to apply cleanly.\n";
Robin Rosenberge86ad712006-12-11 00:30:06 +0100237 print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
238 print "using a patch program. After applying the patch and resolving the\n";
239 print "problems you may commit using:";
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300240 print "\n $cmd\n\n";
Junio C Hamano27dedf02005-11-16 21:32:44 -0800241 exit(1);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300242}
243
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300244if ($opt_c) {
245 print "Autocommit\n $cmd\n";
Simon 'corecode' Schubert4a6b9bb2007-02-18 18:17:08 +0100246 print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300247 if ($?) {
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300248 die "Exiting: The commit did not succeed";
249 }
250 print "Committed successfully to CVS\n";
Gerrit Papecf70c162007-02-28 12:35:39 +0000251 # clean up
252 unlink(".msg");
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300253} else {
254 print "Ready for you to commit, just run:\n\n $cmd\n";
255}
Robin Rosenberge86ad712006-12-11 00:30:06 +0100256
257# clean up
258unlink(".cvsexportcommit.diff");
Robin Rosenberge86ad712006-12-11 00:30:06 +0100259
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300260sub usage {
261 print STDERR <<END;
Martin Langhoff992793c2006-04-26 12:26:16 +1200262Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
Martin Langhoff5e0306a2005-11-07 17:57:08 +1300263END
264 exit(1);
265}
266
Pavel Roskin82e5a822006-07-10 01:50:18 -0400267# An alternative to `command` that allows input to be passed as an array
Martin Langhoffd41df152006-01-30 19:12:12 +1300268# to work around shell problems with weird characters in arguments
269# if the exec returns non-zero we die
270sub safe_pipe_capture {
271 my @output;
272 if (my $pid = open my $child, '-|') {
273 @output = (<$child>);
274 close $child or die join(' ',@_).": $! $?";
275 } else {
276 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
277 }
278 return wantarray ? @output : join('',@output);
279}
Jim Meyering7c0f7022006-12-04 08:44:08 +0100280
Robin Rosenberge86ad712006-12-11 00:30:06 +0100281sub safe_pipe_capture_blob {
282 my $output;
283 if (my $pid = open my $child, '-|') {
284 local $/;
285 undef $/;
286 $output = (<$child>);
287 close $child or die join(' ',@_).": $! $?";
288 } else {
289 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
290 }
291 return $output;
Jim Meyering7c0f7022006-12-04 08:44:08 +0100292}