core.compression documentation formatting fix.
[git/gitweb.git] / git-cvsexportcommit.perl
blobd1051d074bcf685dcc57cae90cca9053dc71a76f
1 #!/usr/bin/perl -w
3 # Known limitations:
4 # - cannot add or remove binary files
5 # - does not propagate permissions
6 # - tells "ready for commit" even when things could not be completed
7 # (eg addition of a binary file)
9 use strict;
10 use Getopt::Std;
11 use File::Temp qw(tempdir);
12 use Data::Dumper;
13 use File::Basename qw(basename dirname);
15 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
16 die "GIT_DIR is not defined or is unreadable";
19 our ($opt_h, $opt_p, $opt_v, $opt_c, $opt_f, $opt_m );
21 getopts('hpvcfm:');
23 $opt_h && usage();
25 die "Need at least one commit identifier!" unless @ARGV;
27 # setup a tempdir
28 our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
29 TMPDIR => 1,
30 CLEANUP => 1);
32 print Dumper(@ARGV);
33 # resolve target commit
34 my $commit;
35 $commit = pop @ARGV;
36 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
37 chomp $commit;
38 if ($?) {
39 die "The commit reference $commit did not resolve!";
42 # resolve what parent we want
43 my $parent;
44 if (@ARGV) {
45 $parent = pop @ARGV;
46 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
47 chomp $parent;
48 if ($?) {
49 die "The parent reference did not resolve!";
53 # find parents from the commit itself
54 my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
55 my @parents;
56 foreach my $p (@commit) {
57 if ($p =~ m/^$/) { # end of commit headers, we're done
58 last;
60 if ($p =~ m/^parent (\w{40})$/) { # found a parent
61 push @parents, $1;
65 if ($parent) {
66 # double check that it's a valid parent
67 foreach my $p (@parents) {
68 my $found;
69 if ($p eq $parent) {
70 $found = 1;
71 last;
72 }; # found it
73 die "Did not find $parent in the parents for this commit!";
75 } else { # we don't have a parent from the cmdline...
76 if (@parents == 1) { # it's safe to get it from the commit
77 $parent = $parents[0];
78 } else { # or perhaps not!
79 die "This commit has more than one parent -- please name the parent you want to use explicitly";
83 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
85 # grab the commit message
86 open(MSG, ">.msg") or die "Cannot open .msg for writing";
87 print MSG $opt_m;
88 close MSG;
90 `git-cat-file commit $commit | sed -e '1,/^\$/d' >> .msg`;
91 $? && die "Error extracting the commit message";
93 my (@afiles, @dfiles, @mfiles, @dirs);
94 my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit);
95 #print @files;
96 $? && die "Error in git-diff-tree";
97 foreach my $f (@files) {
98 chomp $f;
99 my @fields = split(m!\s+!, $f);
100 if ($fields[4] eq 'A') {
101 my $path = $fields[5];
102 push @afiles, $path;
103 # add any needed parent directories
104 $path = dirname $path;
105 while (!-d $path and ! grep { $_ eq $path } @dirs) {
106 unshift @dirs, $path;
107 $path = dirname $path;
110 if ($fields[4] eq 'M') {
111 push @mfiles, $fields[5];
113 if ($fields[4] eq 'R') {
114 push @dfiles, $fields[5];
117 $opt_v && print "The commit affects:\n ";
118 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
119 undef @files; # don't need it anymore
121 # check that the files are clean and up to date according to cvs
122 my $dirty;
123 foreach my $d (@dirs) {
124 if (-e $d) {
125 $dirty = 1;
126 warn "$d exists and is not a directory!\n";
129 foreach my $f (@afiles) {
130 # This should return only one value
131 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
132 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
133 if (-d dirname $f and $status[0] !~ m/Status: Unknown$/
134 and $status[0] !~ m/^File: no file /) {
135 $dirty = 1;
136 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";
137 warn "Status was: $status[0]\n";
140 foreach my $f (@mfiles, @dfiles) {
141 # TODO:we need to handle removed in cvs
142 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
143 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
144 unless ($status[0] =~ m/Status: Up-to-date$/) {
145 $dirty = 1;
146 warn "File $f not up to date in your CVS checkout!\n";
149 if ($dirty) {
150 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
151 $dirty = 0;
152 } else {
153 die "Exiting: your CVS tree is not clean for this merge.";
158 ### NOTE: if you are planning to die() past this point
159 ### you MUST call cleanupcvs(@files) before die()
163 print "Creating new directories\n";
164 foreach my $d (@dirs) {
165 unless (mkdir $d) {
166 warn "Could not mkdir $d: $!";
167 $dirty = 1;
169 `cvs add $d`;
170 if ($?) {
171 $dirty = 1;
172 warn "Failed to cvs add directory $d -- you may need to do it manually";
176 print "'Patching' binary files\n";
178 my @bfiles = grep(m/^Binary/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit));
179 @bfiles = map { chomp } @bfiles;
180 foreach my $f (@bfiles) {
181 # check that the file in cvs matches the "old" file
182 # extract the file to $tmpdir and comparre with cmp
183 my $tree = safe_pipe_capture('git-rev-parse', "$parent^{tree}");
184 chomp $tree;
185 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
186 chomp $blob;
187 `git-cat-file blob $blob > $tmpdir/blob`;
188 if (system('cmp', '-s', $f, "$tmpdir/blob")) {
189 warn "Binary file $f in CVS does not match parent.\n";
190 $dirty = 1;
191 next;
194 # replace with the new file
195 `git-cat-file blob $blob > $f`;
197 # TODO: something smart with file modes
200 if ($dirty) {
201 cleanupcvs(@files);
202 die "Exiting: Binary files in CVS do not match parent";
205 ## apply non-binary changes
206 my $fuzz = $opt_p ? 0 : 2;
208 print "Patching non-binary files\n";
209 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
211 my $dirtypatch = 0;
212 if (($? >> 8) == 2) {
213 cleanupcvs(@files);
214 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
215 } elsif (($? >> 8) == 1) { # some hunks failed to apply
216 $dirtypatch = 1;
219 foreach my $f (@afiles) {
220 system('cvs', 'add', $f);
221 if ($?) {
222 $dirty = 1;
223 warn "Failed to cvs add $f -- you may need to do it manually";
227 foreach my $f (@dfiles) {
228 system('cvs', 'rm', '-f', $f);
229 if ($?) {
230 $dirty = 1;
231 warn "Failed to cvs rm -f $f -- you may need to do it manually";
235 print "Commit to CVS\n";
236 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
237 my $cmd = "cvs commit -F .msg $commitfiles";
239 if ($dirtypatch) {
240 print "NOTE: One or more hunks failed to apply cleanly.\n";
241 print "Resolve the conflicts and then commit using:\n";
242 print "\n $cmd\n\n";
243 exit(1);
247 if ($opt_c) {
248 print "Autocommit\n $cmd\n";
249 print safe_pipe_capture('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
250 if ($?) {
251 cleanupcvs(@files);
252 die "Exiting: The commit did not succeed";
254 print "Committed successfully to CVS\n";
255 } else {
256 print "Ready for you to commit, just run:\n\n $cmd\n";
258 sub usage {
259 print STDERR <<END;
260 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
262 exit(1);
265 # ensure cvs is clean before we die
266 sub cleanupcvs {
267 my @files = @_;
268 foreach my $f (@files) {
269 system('cvs', '-q', 'update', '-C', $f);
270 if ($?) {
271 warn "Warning! Failed to cleanup state of $f\n";
276 # An alterative to `command` that allows input to be passed as an array
277 # to work around shell problems with weird characters in arguments
278 # if the exec returns non-zero we die
279 sub safe_pipe_capture {
280 my @output;
281 if (my $pid = open my $child, '-|') {
282 @output = (<$child>);
283 close $child or die join(' ',@_).": $! $?";
284 } else {
285 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
287 return wantarray ? @output : join('',@output);