]>
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 | ||
4a6b9bb6 | 18 | our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d); |
5e0306ad | 19 | |
4a6b9bb6 | 20 | getopts('hPpvcfam: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 | # setup a tempdir |
34 | our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX', | |
35 | TMPDIR => 1, | |
36 | CLEANUP => 1); | |
37 | ||
5e0306ad ML |
38 | # resolve target commit |
39 | my $commit; | |
40 | $commit = pop @ARGV; | |
d41df15e | 41 | $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0"); |
5e0306ad ML |
42 | chomp $commit; |
43 | if ($?) { | |
44 | die "The commit reference $commit did not resolve!"; | |
45 | } | |
46 | ||
47 | # resolve what parent we want | |
48 | my $parent; | |
49 | if (@ARGV) { | |
50 | $parent = pop @ARGV; | |
d41df15e | 51 | $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0"); |
5e0306ad ML |
52 | chomp $parent; |
53 | if ($?) { | |
54 | die "The parent reference did not resolve!"; | |
55 | } | |
56 | } | |
57 | ||
58 | # find parents from the commit itself | |
d41df15e | 59 | my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit); |
5e0306ad | 60 | my @parents; |
1b91abe3 ML |
61 | my $committer; |
62 | my $author; | |
63 | my $stage = 'headers'; # headers, msg | |
64 | my $title; | |
65 | my $msg = ''; | |
66 | ||
67 | foreach my $line (@commit) { | |
68 | chomp $line; | |
69 | if ($stage eq 'headers' && $line eq '') { | |
70 | $stage = 'msg'; | |
71 | next; | |
5e0306ad | 72 | } |
1b91abe3 ML |
73 | |
74 | if ($stage eq 'headers') { | |
75 | if ($line =~ m/^parent (\w{40})$/) { # found a parent | |
76 | push @parents, $1; | |
fe142b3a | 77 | } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) { |
1b91abe3 | 78 | $author = $1; |
fe142b3a | 79 | } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) { |
1b91abe3 ML |
80 | $committer = $1; |
81 | } | |
82 | } else { | |
83 | $msg .= $line . "\n"; | |
84 | unless ($title) { | |
85 | $title = $line; | |
86 | } | |
5e0306ad ML |
87 | } |
88 | } | |
89 | ||
90 | if ($parent) { | |
135a522e | 91 | my $found; |
5e0306ad ML |
92 | # double check that it's a valid parent |
93 | foreach my $p (@parents) { | |
5e0306ad ML |
94 | if ($p eq $parent) { |
95 | $found = 1; | |
96 | last; | |
97 | }; # found it | |
e09f5d7b | 98 | } |
ca28370a | 99 | die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P; |
5e0306ad ML |
100 | } else { # we don't have a parent from the cmdline... |
101 | if (@parents == 1) { # it's safe to get it from the commit | |
102 | $parent = $parents[0]; | |
103 | } else { # or perhaps not! | |
104 | die "This commit has more than one parent -- please name the parent you want to use explicitly"; | |
105 | } | |
106 | } | |
107 | ||
108 | $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; | |
109 | ||
110 | # grab the commit message | |
992793c8 | 111 | open(MSG, ">.msg") or die "Cannot open .msg for writing"; |
1b91abe3 ML |
112 | if ($opt_m) { |
113 | print MSG $opt_m; | |
114 | } | |
115 | print MSG $msg; | |
116 | if ($opt_a) { | |
117 | print MSG "\n\nAuthor: $author\n"; | |
118 | if ($author ne $committer) { | |
119 | print MSG "Committer: $committer\n"; | |
120 | } | |
121 | } | |
992793c8 ML |
122 | close MSG; |
123 | ||
e86ad71f RR |
124 | `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; |
125 | ||
126 | ## apply non-binary changes | |
127 | my $fuzz = $opt_p ? 0 : 2; | |
128 | ||
129 | print "Checking if patch will apply\n"; | |
130 | ||
131 | my @stat; | |
132 | open APPLY, "GIT_DIR= git-apply -C$fuzz --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch"; | |
133 | @stat=<APPLY>; | |
134 | close APPLY || die "Cannot patch"; | |
135 | my (@bfiles,@files,@afiles,@dfiles); | |
136 | chomp @stat; | |
137 | foreach (@stat) { | |
138 | push (@bfiles,$1) if m/^-\t-\t(.*)$/; | |
139 | push (@files, $1) if m/^-\t-\t(.*)$/; | |
140 | push (@files, $1) if m/^\d+\t\d+\t(.*)$/; | |
141 | push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/; | |
142 | push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/; | |
5e0306ad | 143 | } |
e86ad71f RR |
144 | map { s/^"(.*)"$/$1/g } @bfiles,@files; |
145 | map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files; | |
5e0306ad ML |
146 | |
147 | # check that the files are clean and up to date according to cvs | |
148 | my $dirty; | |
e86ad71f RR |
149 | my @dirs; |
150 | foreach my $p (@afiles) { | |
151 | my $path = dirname $p; | |
152 | while (!-d $path and ! grep { $_ eq $path } @dirs) { | |
153 | unshift @dirs, $path; | |
154 | $path = dirname $path; | |
155 | } | |
156 | } | |
157 | ||
3f0f756b YD |
158 | foreach my $d (@dirs) { |
159 | if (-e $d) { | |
160 | $dirty = 1; | |
161 | warn "$d exists and is not a directory!\n"; | |
162 | } | |
163 | } | |
576cfc86 | 164 | foreach my $f (@afiles) { |
d41df15e | 165 | # This should return only one value |
fe142b3a RR |
166 | if ($f =~ m,(.*)/[^/]*$,) { |
167 | my $p = $1; | |
168 | next if (grep { $_ eq $p } @dirs); | |
169 | } | |
4a6b9bb6 | 170 | my @status = grep(m/^File/, safe_pipe_capture(@cvs, '-q', 'status' ,$f)); |
d41df15e | 171 | if (@status > 1) { warn 'Strange! cvs status returned more than one line?'}; |
3f0f756b YD |
172 | if (-d dirname $f and $status[0] !~ m/Status: Unknown$/ |
173 | and $status[0] !~ m/^File: no file /) { | |
d41df15e | 174 | $dirty = 1; |
992793c8 | 175 | 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 | 176 | warn "Status was: $status[0]\n"; |
576cfc86 YD |
177 | } |
178 | } | |
fe142b3a | 179 | |
e86ad71f RR |
180 | foreach my $f (@files) { |
181 | next if grep { $_ eq $f } @afiles; | |
576cfc86 | 182 | # TODO:we need to handle removed in cvs |
4a6b9bb6 | 183 | my @status = grep(m/^File/, safe_pipe_capture(@cvs, '-q', 'status' ,$f)); |
d41df15e ML |
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 | ||
e86ad71f RR |
198 | print "Applying\n"; |
199 | `GIT_DIR= git-apply -C$fuzz --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; | |
5e0306ad | 200 | |
e86ad71f RR |
201 | print "Patch applied successfully. Adding new files and directories to CVS\n"; |
202 | my $dirtypatch = 0; | |
3f0f756b | 203 | foreach my $d (@dirs) { |
4a6b9bb6 | 204 | if (system(@cvs,'add',$d)) { |
e86ad71f | 205 | $dirtypatch = 1; |
3f0f756b YD |
206 | warn "Failed to cvs add directory $d -- you may need to do it manually"; |
207 | } | |
208 | } | |
209 | ||
5e0306ad | 210 | foreach my $f (@afiles) { |
fe142b3a | 211 | if (grep { $_ eq $f } @bfiles) { |
4a6b9bb6 | 212 | system(@cvs, 'add','-kb',$f); |
fe142b3a | 213 | } else { |
4a6b9bb6 | 214 | system(@cvs, 'add', $f); |
fe142b3a | 215 | } |
5e0306ad | 216 | if ($?) { |
e86ad71f | 217 | $dirtypatch = 1; |
5e0306ad ML |
218 | warn "Failed to cvs add $f -- you may need to do it manually"; |
219 | } | |
220 | } | |
221 | ||
222 | foreach my $f (@dfiles) { | |
4a6b9bb6 | 223 | system(@cvs, 'rm', '-f', $f); |
5e0306ad | 224 | if ($?) { |
e86ad71f | 225 | $dirtypatch = 1; |
5e0306ad ML |
226 | warn "Failed to cvs rm -f $f -- you may need to do it manually"; |
227 | } | |
228 | } | |
229 | ||
230 | print "Commit to CVS\n"; | |
e86ad71f RR |
231 | print "Patch title (first comment line): $title\n"; |
232 | my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files); | |
4a6b9bb6 | 233 | my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles"; |
5e0306ad ML |
234 | |
235 | if ($dirtypatch) { | |
236 | print "NOTE: One or more hunks failed to apply cleanly.\n"; | |
e86ad71f RR |
237 | print "You'll need to apply the patch in .cvsexportcommit.diff manually\n"; |
238 | print "using a patch program. After applying the patch and resolving the\n"; | |
239 | print "problems you may commit using:"; | |
5e0306ad | 240 | print "\n $cmd\n\n"; |
27dedf0c | 241 | exit(1); |
5e0306ad ML |
242 | } |
243 | ||
5e0306ad ML |
244 | if ($opt_c) { |
245 | print "Autocommit\n $cmd\n"; | |
4a6b9bb6 | 246 | print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files); |
5e0306ad | 247 | if ($?) { |
5e0306ad ML |
248 | die "Exiting: The commit did not succeed"; |
249 | } | |
250 | print "Committed successfully to CVS\n"; | |
251 | } else { | |
252 | print "Ready for you to commit, just run:\n\n $cmd\n"; | |
253 | } | |
e86ad71f RR |
254 | |
255 | # clean up | |
256 | unlink(".cvsexportcommit.diff"); | |
257 | unlink(".msg"); | |
258 | ||
5e0306ad ML |
259 | sub usage { |
260 | print STDERR <<END; | |
992793c8 | 261 | Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit |
5e0306ad ML |
262 | END |
263 | exit(1); | |
264 | } | |
265 | ||
82e5a82f | 266 | # An alternative to `command` that allows input to be passed as an array |
d41df15e ML |
267 | # to work around shell problems with weird characters in arguments |
268 | # if the exec returns non-zero we die | |
269 | sub safe_pipe_capture { | |
270 | my @output; | |
271 | if (my $pid = open my $child, '-|') { | |
272 | @output = (<$child>); | |
273 | close $child or die join(' ',@_).": $! $?"; | |
274 | } else { | |
275 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found | |
276 | } | |
277 | return wantarray ? @output : join('',@output); | |
278 | } | |
7c0f7028 | 279 | |
e86ad71f RR |
280 | sub safe_pipe_capture_blob { |
281 | my $output; | |
282 | if (my $pid = open my $child, '-|') { | |
283 | local $/; | |
284 | undef $/; | |
285 | $output = (<$child>); | |
286 | close $child or die join(' ',@_).": $! $?"; | |
287 | } else { | |
288 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found | |
289 | } | |
290 | return $output; | |
7c0f7028 | 291 | } |