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