]>
Commit | Line | Data |
---|---|---|
5e0306ad ML |
1 | #!/usr/bin/perl -w |
2 | ||
0d71b31a | 3 | # Known limitations: |
0d71b31a YD |
4 | # - does not propagate permissions |
5 | # - tells "ready for commit" even when things could not be completed | |
fe142b3a RR |
6 | # (not sure this is true anymore, more testing is needed) |
7 | # - does not handle whitespace in pathnames at all. | |
0d71b31a | 8 | |
5e0306ad ML |
9 | use strict; |
10 | use Getopt::Std; | |
11 | use File::Temp qw(tempdir); | |
12 | use Data::Dumper; | |
3f0f756b | 13 | use File::Basename qw(basename dirname); |
5e0306ad ML |
14 | |
15 | unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ | |
16 | die "GIT_DIR is not defined or is unreadable"; | |
17 | } | |
18 | ||
1b91abe3 | 19 | our ($opt_h, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m ); |
5e0306ad | 20 | |
1b91abe3 | 21 | getopts('hpvcfam:'); |
5e0306ad ML |
22 | |
23 | $opt_h && usage(); | |
24 | ||
25 | die "Need at least one commit identifier!" unless @ARGV; | |
26 | ||
27 | # setup a tempdir | |
28 | our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX', | |
29 | TMPDIR => 1, | |
30 | CLEANUP => 1); | |
31 | ||
5e0306ad ML |
32 | # resolve target commit |
33 | my $commit; | |
34 | $commit = pop @ARGV; | |
d41df15e | 35 | $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0"); |
5e0306ad ML |
36 | chomp $commit; |
37 | if ($?) { | |
38 | die "The commit reference $commit did not resolve!"; | |
39 | } | |
40 | ||
41 | # resolve what parent we want | |
42 | my $parent; | |
43 | if (@ARGV) { | |
44 | $parent = pop @ARGV; | |
d41df15e | 45 | $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0"); |
5e0306ad ML |
46 | chomp $parent; |
47 | if ($?) { | |
48 | die "The parent reference did not resolve!"; | |
49 | } | |
50 | } | |
51 | ||
52 | # find parents from the commit itself | |
d41df15e | 53 | my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit); |
5e0306ad | 54 | my @parents; |
1b91abe3 ML |
55 | my $committer; |
56 | my $author; | |
57 | my $stage = 'headers'; # headers, msg | |
58 | my $title; | |
59 | my $msg = ''; | |
60 | ||
61 | foreach my $line (@commit) { | |
62 | chomp $line; | |
63 | if ($stage eq 'headers' && $line eq '') { | |
64 | $stage = 'msg'; | |
65 | next; | |
5e0306ad | 66 | } |
1b91abe3 ML |
67 | |
68 | if ($stage eq 'headers') { | |
69 | if ($line =~ m/^parent (\w{40})$/) { # found a parent | |
70 | push @parents, $1; | |
fe142b3a | 71 | } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) { |
1b91abe3 | 72 | $author = $1; |
fe142b3a | 73 | } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) { |
1b91abe3 ML |
74 | $committer = $1; |
75 | } | |
76 | } else { | |
77 | $msg .= $line . "\n"; | |
78 | unless ($title) { | |
79 | $title = $line; | |
80 | } | |
5e0306ad ML |
81 | } |
82 | } | |
83 | ||
84 | if ($parent) { | |
135a522e | 85 | my $found; |
5e0306ad ML |
86 | # double check that it's a valid parent |
87 | foreach my $p (@parents) { | |
5e0306ad ML |
88 | if ($p eq $parent) { |
89 | $found = 1; | |
90 | last; | |
91 | }; # found it | |
e09f5d7b | 92 | } |
135a522e | 93 | die "Did not find $parent in the parents for this commit!" if !$found; |
5e0306ad ML |
94 | } else { # we don't have a parent from the cmdline... |
95 | if (@parents == 1) { # it's safe to get it from the commit | |
96 | $parent = $parents[0]; | |
97 | } else { # or perhaps not! | |
98 | die "This commit has more than one parent -- please name the parent you want to use explicitly"; | |
99 | } | |
100 | } | |
101 | ||
102 | $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; | |
103 | ||
104 | # grab the commit message | |
992793c8 | 105 | open(MSG, ">.msg") or die "Cannot open .msg for writing"; |
1b91abe3 ML |
106 | if ($opt_m) { |
107 | print MSG $opt_m; | |
108 | } | |
109 | print MSG $msg; | |
110 | if ($opt_a) { | |
111 | print MSG "\n\nAuthor: $author\n"; | |
112 | if ($author ne $committer) { | |
113 | print MSG "Committer: $committer\n"; | |
114 | } | |
115 | } | |
992793c8 ML |
116 | close MSG; |
117 | ||
3f0f756b | 118 | my (@afiles, @dfiles, @mfiles, @dirs); |
d41df15e | 119 | my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit); |
992793c8 | 120 | #print @files; |
5e0306ad ML |
121 | $? && die "Error in git-diff-tree"; |
122 | foreach my $f (@files) { | |
123 | chomp $f; | |
d41df15e | 124 | my @fields = split(m!\s+!, $f); |
5e0306ad | 125 | if ($fields[4] eq 'A') { |
3f0f756b YD |
126 | my $path = $fields[5]; |
127 | push @afiles, $path; | |
128 | # add any needed parent directories | |
129 | $path = dirname $path; | |
130 | while (!-d $path and ! grep { $_ eq $path } @dirs) { | |
131 | unshift @dirs, $path; | |
132 | $path = dirname $path; | |
133 | } | |
5e0306ad ML |
134 | } |
135 | if ($fields[4] eq 'M') { | |
136 | push @mfiles, $fields[5]; | |
137 | } | |
21ff2bdb | 138 | if ($fields[4] eq 'D') { |
5e0306ad ML |
139 | push @dfiles, $fields[5]; |
140 | } | |
141 | } | |
fe142b3a RR |
142 | my (@binfiles, @abfiles, @dbfiles, @bfiles, @mbfiles); |
143 | @binfiles = grep m/^Binary files/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit); | |
144 | map { chomp } @binfiles; | |
145 | @abfiles = grep s/^Binary files \/dev\/null and b\/(.*) differ$/$1/, @binfiles; | |
146 | @dbfiles = grep s/^Binary files a\/(.*) and \/dev\/null differ$/$1/, @binfiles; | |
147 | @mbfiles = grep s/^Binary files a\/(.*) and b\/(.*) differ$/$1/, @binfiles; | |
148 | push @bfiles, @abfiles; | |
149 | push @bfiles, @dbfiles; | |
150 | push @bfiles, @mbfiles; | |
151 | push @mfiles, @mbfiles; | |
152 | ||
5e0306ad ML |
153 | $opt_v && print "The commit affects:\n "; |
154 | $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n"; | |
155 | undef @files; # don't need it anymore | |
156 | ||
157 | # check that the files are clean and up to date according to cvs | |
158 | my $dirty; | |
3f0f756b YD |
159 | foreach my $d (@dirs) { |
160 | if (-e $d) { | |
161 | $dirty = 1; | |
162 | warn "$d exists and is not a directory!\n"; | |
163 | } | |
164 | } | |
576cfc86 | 165 | foreach my $f (@afiles) { |
d41df15e | 166 | # This should return only one value |
fe142b3a RR |
167 | if ($f =~ m,(.*)/[^/]*$,) { |
168 | my $p = $1; | |
169 | next if (grep { $_ eq $p } @dirs); | |
170 | } | |
d41df15e ML |
171 | my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f)); |
172 | if (@status > 1) { warn 'Strange! cvs status returned more than one line?'}; | |
3f0f756b YD |
173 | if (-d dirname $f and $status[0] !~ m/Status: Unknown$/ |
174 | and $status[0] !~ m/^File: no file /) { | |
d41df15e | 175 | $dirty = 1; |
992793c8 | 176 | 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"; |
e9687515 | 177 | warn "Status was: $status[0]\n"; |
576cfc86 YD |
178 | } |
179 | } | |
fe142b3a | 180 | |
576cfc86 YD |
181 | foreach my $f (@mfiles, @dfiles) { |
182 | # TODO:we need to handle removed in cvs | |
d41df15e ML |
183 | my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f)); |
184 | if (@status > 1) { warn 'Strange! cvs status returned more than one line?'}; | |
185 | unless ($status[0] =~ m/Status: Up-to-date$/) { | |
5e0306ad ML |
186 | $dirty = 1; |
187 | warn "File $f not up to date in your CVS checkout!\n"; | |
188 | } | |
189 | } | |
190 | if ($dirty) { | |
992793c8 ML |
191 | if ($opt_f) { warn "The tree is not clean -- forced merge\n"; |
192 | $dirty = 0; | |
193 | } else { | |
194 | die "Exiting: your CVS tree is not clean for this merge."; | |
195 | } | |
5e0306ad ML |
196 | } |
197 | ||
198 | ### | |
199 | ### NOTE: if you are planning to die() past this point | |
200 | ### you MUST call cleanupcvs(@files) before die() | |
201 | ### | |
202 | ||
203 | ||
3f0f756b YD |
204 | print "Creating new directories\n"; |
205 | foreach my $d (@dirs) { | |
206 | unless (mkdir $d) { | |
207 | warn "Could not mkdir $d: $!"; | |
208 | $dirty = 1; | |
209 | } | |
210 | `cvs add $d`; | |
211 | if ($?) { | |
212 | $dirty = 1; | |
213 | warn "Failed to cvs add directory $d -- you may need to do it manually"; | |
214 | } | |
215 | } | |
216 | ||
5e0306ad ML |
217 | print "'Patching' binary files\n"; |
218 | ||
5e0306ad ML |
219 | foreach my $f (@bfiles) { |
220 | # check that the file in cvs matches the "old" file | |
82e5a82f | 221 | # extract the file to $tmpdir and compare with cmp |
fe142b3a RR |
222 | if (not(grep { $_ eq $f } @afiles)) { |
223 | my $tree = safe_pipe_capture('git-rev-parse', "$parent^{tree}"); | |
224 | chomp $tree; | |
225 | my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`; | |
226 | chomp $blob; | |
227 | `git-cat-file blob $blob > $tmpdir/blob`; | |
228 | if (system('cmp', '-s', $f, "$tmpdir/blob")) { | |
229 | warn "Binary file $f in CVS does not match parent.\n"; | |
230 | if (not $opt_f) { | |
231 | $dirty = 1; | |
232 | next; | |
233 | } | |
234 | } | |
235 | } | |
236 | if (not(grep { $_ eq $f } @dfiles)) { | |
237 | my $tree = safe_pipe_capture('git-rev-parse', "$commit^{tree}"); | |
238 | chomp $tree; | |
239 | my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`; | |
240 | chomp $blob; | |
241 | # replace with the new file | |
242 | `git-cat-file blob $blob > $f`; | |
5e0306ad | 243 | } |
5e0306ad ML |
244 | |
245 | # TODO: something smart with file modes | |
246 | ||
247 | } | |
248 | if ($dirty) { | |
249 | cleanupcvs(@files); | |
250 | die "Exiting: Binary files in CVS do not match parent"; | |
251 | } | |
252 | ||
253 | ## apply non-binary changes | |
254 | my $fuzz = $opt_p ? 0 : 2; | |
255 | ||
256 | print "Patching non-binary files\n"; | |
fe142b3a RR |
257 | |
258 | if (scalar(@afiles)+scalar(@dfiles)+scalar(@mfiles) != scalar(@bfiles)) { | |
259 | print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`; | |
260 | } | |
5e0306ad ML |
261 | |
262 | my $dirtypatch = 0; | |
263 | if (($? >> 8) == 2) { | |
264 | cleanupcvs(@files); | |
265 | die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually"; | |
266 | } elsif (($? >> 8) == 1) { # some hunks failed to apply | |
267 | $dirtypatch = 1; | |
268 | } | |
269 | ||
270 | foreach my $f (@afiles) { | |
fe142b3a RR |
271 | if (grep { $_ eq $f } @bfiles) { |
272 | system('cvs', 'add','-kb',$f); | |
273 | } else { | |
274 | system('cvs', 'add', $f); | |
275 | } | |
5e0306ad ML |
276 | if ($?) { |
277 | $dirty = 1; | |
278 | warn "Failed to cvs add $f -- you may need to do it manually"; | |
279 | } | |
280 | } | |
281 | ||
282 | foreach my $f (@dfiles) { | |
d41df15e | 283 | system('cvs', 'rm', '-f', $f); |
5e0306ad ML |
284 | if ($?) { |
285 | $dirty = 1; | |
286 | warn "Failed to cvs rm -f $f -- you may need to do it manually"; | |
287 | } | |
288 | } | |
289 | ||
290 | print "Commit to CVS\n"; | |
1b91abe3 | 291 | print "Patch: $title\n"; |
5e0306ad ML |
292 | my $commitfiles = join(' ', @afiles, @mfiles, @dfiles); |
293 | my $cmd = "cvs commit -F .msg $commitfiles"; | |
294 | ||
295 | if ($dirtypatch) { | |
296 | print "NOTE: One or more hunks failed to apply cleanly.\n"; | |
27dedf0c | 297 | print "Resolve the conflicts and then commit using:\n"; |
5e0306ad | 298 | print "\n $cmd\n\n"; |
27dedf0c | 299 | exit(1); |
5e0306ad ML |
300 | } |
301 | ||
302 | ||
303 | if ($opt_c) { | |
304 | print "Autocommit\n $cmd\n"; | |
d41df15e | 305 | print safe_pipe_capture('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles); |
5e0306ad ML |
306 | if ($?) { |
307 | cleanupcvs(@files); | |
308 | die "Exiting: The commit did not succeed"; | |
309 | } | |
310 | print "Committed successfully to CVS\n"; | |
311 | } else { | |
312 | print "Ready for you to commit, just run:\n\n $cmd\n"; | |
313 | } | |
314 | sub usage { | |
315 | print STDERR <<END; | |
992793c8 | 316 | Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit |
5e0306ad ML |
317 | END |
318 | exit(1); | |
319 | } | |
320 | ||
321 | # ensure cvs is clean before we die | |
322 | sub cleanupcvs { | |
323 | my @files = @_; | |
324 | foreach my $f (@files) { | |
d41df15e | 325 | system('cvs', '-q', 'update', '-C', $f); |
5e0306ad ML |
326 | if ($?) { |
327 | warn "Warning! Failed to cleanup state of $f\n"; | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
82e5a82f | 332 | # An alternative to `command` that allows input to be passed as an array |
d41df15e ML |
333 | # to work around shell problems with weird characters in arguments |
334 | # if the exec returns non-zero we die | |
335 | sub safe_pipe_capture { | |
336 | my @output; | |
337 | if (my $pid = open my $child, '-|') { | |
338 | @output = (<$child>); | |
339 | close $child or die join(' ',@_).": $! $?"; | |
340 | } else { | |
341 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found | |
342 | } | |
343 | return wantarray ? @output : join('',@output); | |
344 | } |