Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | use strict; |
| 4 | use Getopt::Std; |
| 5 | use File::Temp qw(tempdir); |
| 6 | use Data::Dumper; |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 7 | use File::Basename qw(basename dirname); |
Johan Herland | a723759 | 2008-02-12 00:43:41 +0100 | [diff] [blame] | 8 | use File::Spec; |
Trent Piepho | 325abb7 | 2008-05-08 14:26:55 -0700 | [diff] [blame] | 9 | use Git; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 10 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 11 | our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w, $opt_W); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 12 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 13 | getopts('uhPpvcfam:d:w:W'); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 14 | |
| 15 | $opt_h && usage(); |
| 16 | |
| 17 | die "Need at least one commit identifier!" unless @ARGV; |
| 18 | |
Trent Piepho | 325abb7 | 2008-05-08 14:26:55 -0700 | [diff] [blame] | 19 | # Get git-config settings |
| 20 | my $repo = Git->repository(); |
| 21 | $opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w; |
| 22 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 23 | if ($opt_w || $opt_W) { |
Johan Herland | a723759 | 2008-02-12 00:43:41 +0100 | [diff] [blame] | 24 | # Remember where GIT_DIR is before changing to CVS checkout |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 25 | unless ($ENV{GIT_DIR}) { |
Johan Herland | a723759 | 2008-02-12 00:43:41 +0100 | [diff] [blame] | 26 | # No GIT_DIR set. Figure it out for ourselves |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 27 | my $gd =`git-rev-parse --git-dir`; |
| 28 | chomp($gd); |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 29 | $ENV{GIT_DIR} = $gd; |
| 30 | } |
Johan Herland | a723759 | 2008-02-12 00:43:41 +0100 | [diff] [blame] | 31 | # Make sure GIT_DIR is absolute |
| 32 | $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR}); |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 33 | } |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 34 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 35 | if ($opt_w) { |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 36 | if (! -d $opt_w."/CVS" ) { |
| 37 | die "$opt_w is not a CVS checkout"; |
| 38 | } |
| 39 | chdir $opt_w or die "Cannot change to CVS checkout at $opt_w"; |
| 40 | } |
| 41 | unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ |
| 42 | die "GIT_DIR is not defined or is unreadable"; |
| 43 | } |
| 44 | |
| 45 | |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 46 | my @cvs; |
| 47 | if ($opt_d) { |
| 48 | @cvs = ('cvs', '-d', $opt_d); |
| 49 | } else { |
| 50 | @cvs = ('cvs'); |
| 51 | } |
| 52 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 53 | # resolve target commit |
| 54 | my $commit; |
| 55 | $commit = pop @ARGV; |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 56 | $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 57 | chomp $commit; |
| 58 | if ($?) { |
| 59 | die "The commit reference $commit did not resolve!"; |
| 60 | } |
| 61 | |
| 62 | # resolve what parent we want |
| 63 | my $parent; |
| 64 | if (@ARGV) { |
| 65 | $parent = pop @ARGV; |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 66 | $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 67 | chomp $parent; |
| 68 | if ($?) { |
| 69 | die "The parent reference did not resolve!"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | # find parents from the commit itself |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 74 | my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 75 | my @parents; |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 76 | my $committer; |
| 77 | my $author; |
| 78 | my $stage = 'headers'; # headers, msg |
| 79 | my $title; |
| 80 | my $msg = ''; |
| 81 | |
| 82 | foreach my $line (@commit) { |
| 83 | chomp $line; |
| 84 | if ($stage eq 'headers' && $line eq '') { |
| 85 | $stage = 'msg'; |
| 86 | next; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 87 | } |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 88 | |
| 89 | if ($stage eq 'headers') { |
| 90 | if ($line =~ m/^parent (\w{40})$/) { # found a parent |
| 91 | push @parents, $1; |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 92 | } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) { |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 93 | $author = $1; |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 94 | } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) { |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 95 | $committer = $1; |
| 96 | } |
| 97 | } else { |
| 98 | $msg .= $line . "\n"; |
| 99 | unless ($title) { |
| 100 | $title = $line; |
| 101 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Brad King | 6b6012e | 2007-10-31 16:55:13 -0400 | [diff] [blame] | 105 | my $noparent = "0000000000000000000000000000000000000000"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 106 | if ($parent) { |
Peter Baumann | 135a522 | 2006-07-07 12:55:41 +0200 | [diff] [blame] | 107 | my $found; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 108 | # double check that it's a valid parent |
| 109 | foreach my $p (@parents) { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 110 | if ($p eq $parent) { |
| 111 | $found = 1; |
| 112 | last; |
| 113 | }; # found it |
Alexander Litvinov | e09f5d7 | 2005-11-09 13:02:58 +0600 | [diff] [blame] | 114 | } |
Simon 'corecode' Schubert | ca28370 | 2007-02-01 11:43:39 +0100 | [diff] [blame] | 115 | die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 116 | } else { # we don't have a parent from the cmdline... |
| 117 | if (@parents == 1) { # it's safe to get it from the commit |
| 118 | $parent = $parents[0]; |
Brad King | 6b6012e | 2007-10-31 16:55:13 -0400 | [diff] [blame] | 119 | } elsif (@parents == 0) { # there is no parent |
| 120 | $parent = $noparent; |
| 121 | } else { # cannot choose automatically from multiple parents |
| 122 | die "This commit has more than one parent -- please name the parent you want to use explicitly"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 126 | my $go_back_to = 0; |
| 127 | |
| 128 | if ($opt_W) { |
| 129 | $opt_v && print "Resetting to $parent\n"; |
| 130 | $go_back_to = `git symbolic-ref HEAD 2> /dev/null || |
| 131 | git rev-parse HEAD` || die "Could not determine current branch"; |
| 132 | system("git checkout -q $parent^0") && die "Could not check out $parent^0"; |
| 133 | } |
| 134 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 135 | $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; |
| 136 | |
| 137 | # grab the commit message |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 138 | open(MSG, ">.msg") or die "Cannot open .msg for writing"; |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 139 | if ($opt_m) { |
| 140 | print MSG $opt_m; |
| 141 | } |
| 142 | print MSG $msg; |
| 143 | if ($opt_a) { |
| 144 | print MSG "\n\nAuthor: $author\n"; |
| 145 | if ($author ne $committer) { |
| 146 | print MSG "Committer: $committer\n"; |
| 147 | } |
| 148 | } |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 149 | close MSG; |
| 150 | |
Brad King | 6b6012e | 2007-10-31 16:55:13 -0400 | [diff] [blame] | 151 | if ($parent eq $noparent) { |
| 152 | `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; |
| 153 | } else { |
| 154 | `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; |
| 155 | } |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 156 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 157 | ## apply non-binary changes |
Tomash Brechko | fc1f458 | 2007-04-09 15:24:02 +0400 | [diff] [blame] | 158 | |
| 159 | # In pedantic mode require all lines of context to match. In normal |
| 160 | # mode, be compatible with diff/patch: assume 3 lines of context and |
| 161 | # require at least one line match, i.e. ignore at most 2 lines of |
| 162 | # context, like diff/patch do by default. |
| 163 | my $context = $opt_p ? '' : '-C1'; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 164 | |
| 165 | print "Checking if patch will apply\n"; |
| 166 | |
| 167 | my @stat; |
Michael Witten | 54f0db7 | 2007-10-16 04:08:14 -0400 | [diff] [blame] | 168 | open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 169 | @stat=<APPLY>; |
| 170 | close APPLY || die "Cannot patch"; |
| 171 | my (@bfiles,@files,@afiles,@dfiles); |
| 172 | chomp @stat; |
| 173 | foreach (@stat) { |
| 174 | push (@bfiles,$1) if m/^-\t-\t(.*)$/; |
| 175 | push (@files, $1) if m/^-\t-\t(.*)$/; |
| 176 | push (@files, $1) if m/^\d+\t\d+\t(.*)$/; |
| 177 | push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/; |
| 178 | push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/; |
| 179 | } |
| 180 | map { s/^"(.*)"$/$1/g } @bfiles,@files; |
| 181 | map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 182 | |
| 183 | # check that the files are clean and up to date according to cvs |
| 184 | my $dirty; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 185 | my @dirs; |
| 186 | foreach my $p (@afiles) { |
| 187 | my $path = dirname $p; |
| 188 | while (!-d $path and ! grep { $_ eq $path } @dirs) { |
| 189 | unshift @dirs, $path; |
| 190 | $path = dirname $path; |
| 191 | } |
| 192 | } |
| 193 | |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 194 | # ... check dirs, |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 195 | foreach my $d (@dirs) { |
| 196 | if (-e $d) { |
| 197 | $dirty = 1; |
| 198 | warn "$d exists and is not a directory!\n"; |
| 199 | } |
| 200 | } |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 201 | |
| 202 | # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat. |
| 203 | my @canstatusfiles; |
| 204 | foreach my $f (@files) { |
| 205 | my $path = dirname $f; |
| 206 | next if (grep { $_ eq $path } @dirs); |
| 207 | push @canstatusfiles, $f; |
| 208 | } |
| 209 | |
| 210 | my %cvsstat; |
| 211 | if (@canstatusfiles) { |
Robin Rosenberg | e5d8064 | 2007-05-24 17:06:55 +0200 | [diff] [blame] | 212 | if ($opt_u) { |
Jeff King | 38a5b1d | 2007-12-14 04:15:47 -0500 | [diff] [blame] | 213 | my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles); |
Robin Rosenberg | e5d8064 | 2007-05-24 17:06:55 +0200 | [diff] [blame] | 214 | print @updated; |
| 215 | } |
Johannes Schindelin | fef3a7c | 2008-02-18 17:55:22 +0000 | [diff] [blame] | 216 | # "cvs status" reorders the parameters, notably when there are multiple |
| 217 | # arguments with the same basename. So be precise here. |
| 218 | |
| 219 | my %added = map { $_ => 1 } @afiles; |
| 220 | my %todo = map { $_ => 1 } @canstatusfiles; |
| 221 | |
| 222 | while (%todo) { |
| 223 | my @canstatusfiles2 = (); |
| 224 | my %fullname = (); |
| 225 | foreach my $name (keys %todo) { |
| 226 | my $basename = basename($name); |
| 227 | |
| 228 | $basename = "no file " . $basename if (exists($added{$basename})); |
Johannes Schindelin | 57e0e3e | 2008-05-14 23:30:43 +0100 | [diff] [blame] | 229 | $basename =~ s/^\s+//; |
| 230 | $basename =~ s/\s+$//; |
Johannes Schindelin | fef3a7c | 2008-02-18 17:55:22 +0000 | [diff] [blame] | 231 | |
| 232 | if (!exists($fullname{$basename})) { |
| 233 | $fullname{$basename} = $name; |
| 234 | push (@canstatusfiles2, $name); |
| 235 | delete($todo{$name}); |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 236 | } |
Johannes Schindelin | fef3a7c | 2008-02-18 17:55:22 +0000 | [diff] [blame] | 237 | } |
| 238 | my @cvsoutput; |
| 239 | @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2); |
| 240 | foreach my $l (@cvsoutput) { |
| 241 | chomp $l; |
| 242 | if ($l =~ /^File:\s+(.*\S)\s+Status: (.*)$/) { |
| 243 | if (!exists($fullname{$1})) { |
| 244 | print STDERR "Huh? Status reported for unexpected file '$1'\n"; |
| 245 | } else { |
| 246 | $cvsstat{$fullname{$1}} = $2; |
| 247 | } |
| 248 | } |
| 249 | } |
Yann Dirson | 576cfc8 | 2006-01-06 21:54:41 +0100 | [diff] [blame] | 250 | } |
| 251 | } |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 252 | |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 253 | # ... validate new files, |
| 254 | foreach my $f (@afiles) { |
| 255 | if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") { |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 256 | $dirty = 1; |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 257 | 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"; |
| 258 | warn "Status was: $cvsstat{$f}\n"; |
| 259 | } |
| 260 | } |
| 261 | # ... validate known files. |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 262 | foreach my $f (@files) { |
| 263 | next if grep { $_ eq $f } @afiles; |
Yann Dirson | 576cfc8 | 2006-01-06 21:54:41 +0100 | [diff] [blame] | 264 | # TODO:we need to handle removed in cvs |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 265 | unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 266 | $dirty = 1; |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 267 | warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | if ($dirty) { |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 271 | if ($opt_f) { warn "The tree is not clean -- forced merge\n"; |
| 272 | $dirty = 0; |
| 273 | } else { |
| 274 | die "Exiting: your CVS tree is not clean for this merge."; |
| 275 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 276 | } |
| 277 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 278 | print "Applying\n"; |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 279 | if ($opt_W) { |
| 280 | system("git checkout -q $commit^0") && die "cannot patch"; |
| 281 | } else { |
| 282 | `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; |
| 283 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 284 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 285 | print "Patch applied successfully. Adding new files and directories to CVS\n"; |
| 286 | my $dirtypatch = 0; |
Alex Bennee | fd0b959 | 2007-10-18 17:15:44 +0100 | [diff] [blame] | 287 | |
| 288 | # |
| 289 | # We have to add the directories in order otherwise we will have |
| 290 | # problems when we try and add the sub-directory of a directory we |
| 291 | # have not added yet. |
| 292 | # |
| 293 | # Luckily this is easy to deal with by sorting the directories and |
| 294 | # dealing with the shortest ones first. |
| 295 | # |
| 296 | @dirs = sort { length $a <=> length $b} @dirs; |
| 297 | |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 298 | foreach my $d (@dirs) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 299 | if (system(@cvs,'add',$d)) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 300 | $dirtypatch = 1; |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 301 | warn "Failed to cvs add directory $d -- you may need to do it manually"; |
| 302 | } |
| 303 | } |
| 304 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 305 | foreach my $f (@afiles) { |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 306 | if (grep { $_ eq $f } @bfiles) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 307 | system(@cvs, 'add','-kb',$f); |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 308 | } else { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 309 | system(@cvs, 'add', $f); |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 310 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 311 | if ($?) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 312 | $dirtypatch = 1; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 313 | warn "Failed to cvs add $f -- you may need to do it manually"; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | foreach my $f (@dfiles) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 318 | system(@cvs, 'rm', '-f', $f); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 319 | if ($?) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 320 | $dirtypatch = 1; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 321 | warn "Failed to cvs rm -f $f -- you may need to do it manually"; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | print "Commit to CVS\n"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 326 | print "Patch title (first comment line): $title\n"; |
| 327 | my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files); |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 328 | my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 329 | |
| 330 | if ($dirtypatch) { |
| 331 | print "NOTE: One or more hunks failed to apply cleanly.\n"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 332 | print "You'll need to apply the patch in .cvsexportcommit.diff manually\n"; |
| 333 | print "using a patch program. After applying the patch and resolving the\n"; |
| 334 | print "problems you may commit using:"; |
Robin Rosenberg | 648ee55 | 2007-10-31 23:12:20 +0100 | [diff] [blame] | 335 | print "\n cd \"$opt_w\"" if $opt_w; |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 336 | print "\n $cmd\n"; |
| 337 | print "\n git checkout $go_back_to\n" if $go_back_to; |
| 338 | print "\n"; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 339 | exit(1); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 340 | } |
| 341 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 342 | if ($opt_c) { |
| 343 | print "Autocommit\n $cmd\n"; |
Jeff King | 38a5b1d | 2007-12-14 04:15:47 -0500 | [diff] [blame] | 344 | print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 345 | if ($?) { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 346 | die "Exiting: The commit did not succeed"; |
| 347 | } |
| 348 | print "Committed successfully to CVS\n"; |
Gerrit Pape | cf70c16 | 2007-02-28 12:35:39 +0000 | [diff] [blame] | 349 | # clean up |
| 350 | unlink(".msg"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 351 | } else { |
| 352 | print "Ready for you to commit, just run:\n\n $cmd\n"; |
| 353 | } |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 354 | |
| 355 | # clean up |
| 356 | unlink(".cvsexportcommit.diff"); |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 357 | |
Johannes Schindelin | d775734 | 2008-05-14 15:29:49 +0100 | [diff] [blame] | 358 | if ($opt_W) { |
| 359 | system("git checkout $go_back_to") && die "cannot move back to $go_back_to"; |
| 360 | if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) { |
| 361 | system("git symbolic-ref HEAD $go_back_to") && |
| 362 | die "cannot move back to $go_back_to"; |
| 363 | } |
| 364 | } |
| 365 | |
Robin Rosenberg | f836f1a | 2007-07-25 00:56:20 +0200 | [diff] [blame] | 366 | # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp |
| 367 | # used by CVS and the one set by subsequence file modifications are different. |
| 368 | # If they are not different CVS will not detect changes. |
| 369 | sleep(1); |
| 370 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 371 | sub usage { |
| 372 | print STDERR <<END; |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 373 | Usage: GIT_DIR=/path/to/.git git cvsexportcommit [-h] [-p] [-v] [-c] [-f] [-u] [-w cvsworkdir] [-m msgprefix] [ parent ] commit |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 374 | END |
| 375 | exit(1); |
| 376 | } |
| 377 | |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 378 | # An alternative to `command` that allows input to be passed as an array |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 379 | # to work around shell problems with weird characters in arguments |
| 380 | # if the exec returns non-zero we die |
| 381 | sub safe_pipe_capture { |
| 382 | my @output; |
| 383 | if (my $pid = open my $child, '-|') { |
| 384 | @output = (<$child>); |
| 385 | close $child or die join(' ',@_).": $! $?"; |
| 386 | } else { |
| 387 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found |
| 388 | } |
| 389 | return wantarray ? @output : join('',@output); |
| 390 | } |
Jim Meyering | 7c0f702 | 2006-12-04 08:44:08 +0100 | [diff] [blame] | 391 | |
Jeff King | 38a5b1d | 2007-12-14 04:15:47 -0500 | [diff] [blame] | 392 | sub xargs_safe_pipe_capture { |
| 393 | my $MAX_ARG_LENGTH = 65536; |
| 394 | my $cmd = shift; |
| 395 | my @output; |
| 396 | my $output; |
| 397 | while(@_) { |
| 398 | my @args; |
| 399 | my $length = 0; |
| 400 | while(@_ && $length < $MAX_ARG_LENGTH) { |
| 401 | push @args, shift; |
| 402 | $length += length($args[$#args]); |
| 403 | } |
| 404 | if (wantarray) { |
| 405 | push @output, safe_pipe_capture(@$cmd, @args); |
| 406 | } |
| 407 | else { |
| 408 | $output .= safe_pipe_capture(@$cmd, @args); |
| 409 | } |
| 410 | } |
| 411 | return wantarray ? @output : $output; |
Jim Meyering | 7c0f702 | 2006-12-04 08:44:08 +0100 | [diff] [blame] | 412 | } |