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