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