]>
Commit | Line | Data |
---|---|---|
5e0306ad ML |
1 | #!/usr/bin/perl -w |
2 | ||
0d71b31a | 3 | # Known limitations: |
0d71b31a | 4 | # - does not propagate permissions |
e86ad71f RR |
5 | # - error handling has not been extensively tested |
6 | # | |
0d71b31a | 7 | |
5e0306ad ML |
8 | use strict; |
9 | use Getopt::Std; | |
10 | use File::Temp qw(tempdir); | |
11 | use Data::Dumper; | |
3f0f756b | 12 | use File::Basename qw(basename dirname); |
5e0306ad ML |
13 | |
14 | unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ | |
15 | die "GIT_DIR is not defined or is unreadable"; | |
16 | } | |
17 | ||
e5d80641 | 18 | our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u); |
5e0306ad | 19 | |
e5d80641 | 20 | getopts('uhPpvcfam:d:'); |
5e0306ad ML |
21 | |
22 | $opt_h && usage(); | |
23 | ||
24 | die "Need at least one commit identifier!" unless @ARGV; | |
25 | ||
4a6b9bb6 SS |
26 | my @cvs; |
27 | if ($opt_d) { | |
28 | @cvs = ('cvs', '-d', $opt_d); | |
29 | } else { | |
30 | @cvs = ('cvs'); | |
31 | } | |
32 | ||
5e0306ad ML |
33 | # resolve target commit |
34 | my $commit; | |
35 | $commit = pop @ARGV; | |
d41df15e | 36 | $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0"); |
5e0306ad ML |
37 | chomp $commit; |
38 | if ($?) { | |
39 | die "The commit reference $commit did not resolve!"; | |
40 | } | |
41 | ||
42 | # resolve what parent we want | |
43 | my $parent; | |
44 | if (@ARGV) { | |
45 | $parent = pop @ARGV; | |
d41df15e | 46 | $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0"); |
5e0306ad ML |
47 | chomp $parent; |
48 | if ($?) { | |
49 | die "The parent reference did not resolve!"; | |
50 | } | |
51 | } | |
52 | ||
53 | # find parents from the commit itself | |
d41df15e | 54 | my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit); |
5e0306ad | 55 | my @parents; |
1b91abe3 ML |
56 | my $committer; |
57 | my $author; | |
58 | my $stage = 'headers'; # headers, msg | |
59 | my $title; | |
60 | my $msg = ''; | |
61 | ||
62 | foreach my $line (@commit) { | |
63 | chomp $line; | |
64 | if ($stage eq 'headers' && $line eq '') { | |
65 | $stage = 'msg'; | |
66 | next; | |
5e0306ad | 67 | } |
1b91abe3 ML |
68 | |
69 | if ($stage eq 'headers') { | |
70 | if ($line =~ m/^parent (\w{40})$/) { # found a parent | |
71 | push @parents, $1; | |
fe142b3a | 72 | } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) { |
1b91abe3 | 73 | $author = $1; |
fe142b3a | 74 | } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) { |
1b91abe3 ML |
75 | $committer = $1; |
76 | } | |
77 | } else { | |
78 | $msg .= $line . "\n"; | |
79 | unless ($title) { | |
80 | $title = $line; | |
81 | } | |
5e0306ad ML |
82 | } |
83 | } | |
84 | ||
6b6012e6 | 85 | my $noparent = "0000000000000000000000000000000000000000"; |
5e0306ad | 86 | if ($parent) { |
135a522e | 87 | my $found; |
5e0306ad ML |
88 | # double check that it's a valid parent |
89 | foreach my $p (@parents) { | |
5e0306ad ML |
90 | if ($p eq $parent) { |
91 | $found = 1; | |
92 | last; | |
93 | }; # found it | |
e09f5d7b | 94 | } |
ca28370a | 95 | die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P; |
5e0306ad ML |
96 | } else { # we don't have a parent from the cmdline... |
97 | if (@parents == 1) { # it's safe to get it from the commit | |
98 | $parent = $parents[0]; | |
6b6012e6 BK |
99 | } elsif (@parents == 0) { # there is no parent |
100 | $parent = $noparent; | |
101 | } else { # cannot choose automatically from multiple parents | |
102 | die "This commit has more than one parent -- please name the parent you want to use explicitly"; | |
5e0306ad ML |
103 | } |
104 | } | |
105 | ||
106 | $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; | |
107 | ||
108 | # grab the commit message | |
992793c8 | 109 | open(MSG, ">.msg") or die "Cannot open .msg for writing"; |
1b91abe3 ML |
110 | if ($opt_m) { |
111 | print MSG $opt_m; | |
112 | } | |
113 | print MSG $msg; | |
114 | if ($opt_a) { | |
115 | print MSG "\n\nAuthor: $author\n"; | |
116 | if ($author ne $committer) { | |
117 | print MSG "Committer: $committer\n"; | |
118 | } | |
119 | } | |
992793c8 ML |
120 | close MSG; |
121 | ||
6b6012e6 BK |
122 | if ($parent eq $noparent) { |
123 | `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; | |
124 | } else { | |
125 | `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; | |
126 | } | |
e86ad71f RR |
127 | |
128 | ## apply non-binary changes | |
fc1f458c TB |
129 | |
130 | # In pedantic mode require all lines of context to match. In normal | |
131 | # mode, be compatible with diff/patch: assume 3 lines of context and | |
132 | # require at least one line match, i.e. ignore at most 2 lines of | |
133 | # context, like diff/patch do by default. | |
134 | my $context = $opt_p ? '' : '-C1'; | |
e86ad71f RR |
135 | |
136 | print "Checking if patch will apply\n"; | |
137 | ||
138 | my @stat; | |
54f0db79 | 139 | open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch"; |
e86ad71f RR |
140 | @stat=<APPLY>; |
141 | close APPLY || die "Cannot patch"; | |
142 | my (@bfiles,@files,@afiles,@dfiles); | |
143 | chomp @stat; | |
144 | foreach (@stat) { | |
145 | push (@bfiles,$1) if m/^-\t-\t(.*)$/; | |
146 | push (@files, $1) if m/^-\t-\t(.*)$/; | |
147 | push (@files, $1) if m/^\d+\t\d+\t(.*)$/; | |
148 | push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/; | |
149 | push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/; | |
5e0306ad | 150 | } |
e86ad71f RR |
151 | map { s/^"(.*)"$/$1/g } @bfiles,@files; |
152 | map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files; | |
5e0306ad ML |
153 | |
154 | # check that the files are clean and up to date according to cvs | |
155 | my $dirty; | |
e86ad71f RR |
156 | my @dirs; |
157 | foreach my $p (@afiles) { | |
158 | my $path = dirname $p; | |
159 | while (!-d $path and ! grep { $_ eq $path } @dirs) { | |
160 | unshift @dirs, $path; | |
161 | $path = dirname $path; | |
162 | } | |
163 | } | |
164 | ||
c56f0d9c | 165 | # ... check dirs, |
3f0f756b YD |
166 | foreach my $d (@dirs) { |
167 | if (-e $d) { | |
168 | $dirty = 1; | |
169 | warn "$d exists and is not a directory!\n"; | |
170 | } | |
171 | } | |
c56f0d9c SP |
172 | |
173 | # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat. | |
174 | my @canstatusfiles; | |
175 | foreach my $f (@files) { | |
176 | my $path = dirname $f; | |
177 | next if (grep { $_ eq $path } @dirs); | |
178 | push @canstatusfiles, $f; | |
179 | } | |
180 | ||
181 | my %cvsstat; | |
182 | if (@canstatusfiles) { | |
e5d80641 RR |
183 | if ($opt_u) { |
184 | my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles); | |
185 | print @updated; | |
186 | } | |
c56f0d9c SP |
187 | my @cvsoutput; |
188 | @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles); | |
189 | my $matchcount = 0; | |
190 | foreach my $l (@cvsoutput) { | |
191 | chomp $l; | |
192 | if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) { | |
193 | $cvsstat{$canstatusfiles[$matchcount]} = $1; | |
194 | $matchcount++; | |
195 | } | |
fe142b3a | 196 | } |
c56f0d9c SP |
197 | } |
198 | ||
199 | # ... validate new files, | |
200 | foreach my $f (@afiles) { | |
201 | if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") { | |
a6080a0a | 202 | $dirty = 1; |
992793c8 | 203 | 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"; |
c56f0d9c | 204 | warn "Status was: $cvsstat{$f}\n"; |
576cfc86 YD |
205 | } |
206 | } | |
c56f0d9c | 207 | # ... validate known files. |
e86ad71f RR |
208 | foreach my $f (@files) { |
209 | next if grep { $_ eq $f } @afiles; | |
576cfc86 | 210 | # TODO:we need to handle removed in cvs |
c56f0d9c | 211 | unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") { |
5e0306ad | 212 | $dirty = 1; |
c56f0d9c | 213 | warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n"; |
5e0306ad ML |
214 | } |
215 | } | |
216 | if ($dirty) { | |
992793c8 ML |
217 | if ($opt_f) { warn "The tree is not clean -- forced merge\n"; |
218 | $dirty = 0; | |
219 | } else { | |
220 | die "Exiting: your CVS tree is not clean for this merge."; | |
221 | } | |
5e0306ad ML |
222 | } |
223 | ||
e86ad71f | 224 | print "Applying\n"; |
54f0db79 | 225 | `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; |
5e0306ad | 226 | |
e86ad71f RR |
227 | print "Patch applied successfully. Adding new files and directories to CVS\n"; |
228 | my $dirtypatch = 0; | |
fd0b9594 AB |
229 | |
230 | # | |
231 | # We have to add the directories in order otherwise we will have | |
232 | # problems when we try and add the sub-directory of a directory we | |
233 | # have not added yet. | |
234 | # | |
235 | # Luckily this is easy to deal with by sorting the directories and | |
236 | # dealing with the shortest ones first. | |
237 | # | |
238 | @dirs = sort { length $a <=> length $b} @dirs; | |
239 | ||
3f0f756b | 240 | foreach my $d (@dirs) { |
4a6b9bb6 | 241 | if (system(@cvs,'add',$d)) { |
e86ad71f | 242 | $dirtypatch = 1; |
3f0f756b YD |
243 | warn "Failed to cvs add directory $d -- you may need to do it manually"; |
244 | } | |
245 | } | |
246 | ||
5e0306ad | 247 | foreach my $f (@afiles) { |
fe142b3a | 248 | if (grep { $_ eq $f } @bfiles) { |
4a6b9bb6 | 249 | system(@cvs, 'add','-kb',$f); |
fe142b3a | 250 | } else { |
4a6b9bb6 | 251 | system(@cvs, 'add', $f); |
fe142b3a | 252 | } |
5e0306ad | 253 | if ($?) { |
e86ad71f | 254 | $dirtypatch = 1; |
5e0306ad ML |
255 | warn "Failed to cvs add $f -- you may need to do it manually"; |
256 | } | |
257 | } | |
258 | ||
259 | foreach my $f (@dfiles) { | |
4a6b9bb6 | 260 | system(@cvs, 'rm', '-f', $f); |
5e0306ad | 261 | if ($?) { |
e86ad71f | 262 | $dirtypatch = 1; |
5e0306ad ML |
263 | warn "Failed to cvs rm -f $f -- you may need to do it manually"; |
264 | } | |
265 | } | |
266 | ||
267 | print "Commit to CVS\n"; | |
e86ad71f RR |
268 | print "Patch title (first comment line): $title\n"; |
269 | my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files); | |
4a6b9bb6 | 270 | my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles"; |
5e0306ad ML |
271 | |
272 | if ($dirtypatch) { | |
273 | print "NOTE: One or more hunks failed to apply cleanly.\n"; | |
e86ad71f RR |
274 | print "You'll need to apply the patch in .cvsexportcommit.diff manually\n"; |
275 | print "using a patch program. After applying the patch and resolving the\n"; | |
276 | print "problems you may commit using:"; | |
5e0306ad | 277 | print "\n $cmd\n\n"; |
27dedf0c | 278 | exit(1); |
5e0306ad ML |
279 | } |
280 | ||
5e0306ad ML |
281 | if ($opt_c) { |
282 | print "Autocommit\n $cmd\n"; | |
4a6b9bb6 | 283 | print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files); |
5e0306ad | 284 | if ($?) { |
5e0306ad ML |
285 | die "Exiting: The commit did not succeed"; |
286 | } | |
287 | print "Committed successfully to CVS\n"; | |
cf70c16f GP |
288 | # clean up |
289 | unlink(".msg"); | |
5e0306ad ML |
290 | } else { |
291 | print "Ready for you to commit, just run:\n\n $cmd\n"; | |
292 | } | |
e86ad71f RR |
293 | |
294 | # clean up | |
295 | unlink(".cvsexportcommit.diff"); | |
e86ad71f | 296 | |
f836f1ae RR |
297 | # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp |
298 | # used by CVS and the one set by subsequence file modifications are different. | |
299 | # If they are not different CVS will not detect changes. | |
300 | sleep(1); | |
301 | ||
5e0306ad ML |
302 | sub usage { |
303 | print STDERR <<END; | |
992793c8 | 304 | Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit |
5e0306ad ML |
305 | END |
306 | exit(1); | |
307 | } | |
308 | ||
82e5a82f | 309 | # An alternative to `command` that allows input to be passed as an array |
d41df15e ML |
310 | # to work around shell problems with weird characters in arguments |
311 | # if the exec returns non-zero we die | |
312 | sub safe_pipe_capture { | |
313 | my @output; | |
314 | if (my $pid = open my $child, '-|') { | |
315 | @output = (<$child>); | |
316 | close $child or die join(' ',@_).": $! $?"; | |
317 | } else { | |
318 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found | |
319 | } | |
320 | return wantarray ? @output : join('',@output); | |
321 | } | |
7c0f7028 | 322 | |
e86ad71f RR |
323 | sub safe_pipe_capture_blob { |
324 | my $output; | |
325 | if (my $pid = open my $child, '-|') { | |
326 | local $/; | |
327 | undef $/; | |
328 | $output = (<$child>); | |
329 | close $child or die join(' ',@_).": $! $?"; | |
330 | } else { | |
331 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found | |
332 | } | |
333 | return $output; | |
7c0f7028 | 334 | } |