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