Fix error handling for nonexistent names
[git/spearce.git] / git-cvsexportcommit.perl
blob7b3a3d323baeb2babd51f3a653e63a7a71aa48eb
1 #!/usr/bin/perl -w
3 use strict;
4 use Getopt::Std;
5 use File::Temp qw(tempdir);
6 use Data::Dumper;
7 use File::Basename qw(basename);
9 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
10 die "GIT_DIR is not defined or is unreadable";
13 our ($opt_h, $opt_p, $opt_v, $opt_c );
15 getopts('hpvc');
17 $opt_h && usage();
19 die "Need at least one commit identifier!" unless @ARGV;
21 # setup a tempdir
22 our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
23 TMPDIR => 1,
24 CLEANUP => 1);
26 print Dumper(@ARGV);
27 # resolve target commit
28 my $commit;
29 $commit = pop @ARGV;
30 $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
31 chomp $commit;
32 if ($?) {
33 die "The commit reference $commit did not resolve!";
36 # resolve what parent we want
37 my $parent;
38 if (@ARGV) {
39 $parent = pop @ARGV;
40 $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
41 chomp $parent;
42 if ($?) {
43 die "The parent reference did not resolve!";
47 # find parents from the commit itself
48 my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
49 my @parents;
50 foreach my $p (@commit) {
51 if ($p =~ m/^$/) { # end of commit headers, we're done
52 last;
54 if ($p =~ m/^parent (\w{40})$/) { # found a parent
55 push @parents, $1;
59 if ($parent) {
60 # double check that it's a valid parent
61 foreach my $p (@parents) {
62 my $found;
63 if ($p eq $parent) {
64 $found = 1;
65 last;
66 }; # found it
67 die "Did not find $parent in the parents for this commit!";
69 } else { # we don't have a parent from the cmdline...
70 if (@parents == 1) { # it's safe to get it from the commit
71 $parent = $parents[0];
72 } else { # or perhaps not!
73 die "This commit has more than one parent -- please name the parent you want to use explicitly";
77 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
79 # grab the commit message
80 `git-cat-file commit $commit | sed -e '1,/^\$/d' > .msg`;
81 $? && die "Error extracting the commit message";
83 my (@afiles, @dfiles, @mfiles);
84 my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit);
85 print @files;
86 $? && die "Error in git-diff-tree";
87 foreach my $f (@files) {
88 chomp $f;
89 my @fields = split(m!\s+!, $f);
90 if ($fields[4] eq 'A') {
91 push @afiles, $fields[5];
93 if ($fields[4] eq 'M') {
94 push @mfiles, $fields[5];
96 if ($fields[4] eq 'R') {
97 push @dfiles, $fields[5];
100 $opt_v && print "The commit affects:\n ";
101 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
102 undef @files; # don't need it anymore
104 # check that the files are clean and up to date according to cvs
105 my $dirty;
106 foreach my $f (@afiles) {
107 # This should return only one value
108 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
109 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
110 unless ($status[0] =~ m/Status: Unknown$/) {
111 $dirty = 1;
112 warn "File $f is already known in your CVS checkout!\n";
115 foreach my $f (@mfiles, @dfiles) {
116 # TODO:we need to handle removed in cvs
117 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
118 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
119 unless ($status[0] =~ m/Status: Up-to-date$/) {
120 $dirty = 1;
121 warn "File $f not up to date in your CVS checkout!\n";
124 if ($dirty) {
125 die "Exiting: your CVS tree is not clean for this merge.";
129 ### NOTE: if you are planning to die() past this point
130 ### you MUST call cleanupcvs(@files) before die()
134 print "'Patching' binary files\n";
136 my @bfiles = grep(m/^Binary/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit));
137 @bfiles = map { chomp } @bfiles;
138 foreach my $f (@bfiles) {
139 # check that the file in cvs matches the "old" file
140 # extract the file to $tmpdir and comparre with cmp
141 my $tree = safe_pipe_capture('git-rev-parse', "$parent^{tree}");
142 chomp $tree;
143 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
144 chomp $blob;
145 `git-cat-file blob $blob > $tmpdir/blob`;
146 if (system('cmp', '-q', $f, "$tmpdir/blob")) {
147 warn "Binary file $f in CVS does not match parent.\n";
148 $dirty = 1;
149 next;
152 # replace with the new file
153 `git-cat-file blob $blob > $f`;
155 # TODO: something smart with file modes
158 if ($dirty) {
159 cleanupcvs(@files);
160 die "Exiting: Binary files in CVS do not match parent";
163 ## apply non-binary changes
164 my $fuzz = $opt_p ? 0 : 2;
166 print "Patching non-binary files\n";
167 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
169 my $dirtypatch = 0;
170 if (($? >> 8) == 2) {
171 cleanupcvs(@files);
172 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
173 } elsif (($? >> 8) == 1) { # some hunks failed to apply
174 $dirtypatch = 1;
177 foreach my $f (@afiles) {
178 system('cvs', 'add', $f);
179 if ($?) {
180 $dirty = 1;
181 warn "Failed to cvs add $f -- you may need to do it manually";
185 foreach my $f (@dfiles) {
186 system('cvs', 'rm', '-f', $f);
187 if ($?) {
188 $dirty = 1;
189 warn "Failed to cvs rm -f $f -- you may need to do it manually";
193 print "Commit to CVS\n";
194 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
195 my $cmd = "cvs commit -F .msg $commitfiles";
197 if ($dirtypatch) {
198 print "NOTE: One or more hunks failed to apply cleanly.\n";
199 print "Resolve the conflicts and then commit using:\n";
200 print "\n $cmd\n\n";
201 exit(1);
205 if ($opt_c) {
206 print "Autocommit\n $cmd\n";
207 print safe_pipe_capture('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
208 if ($?) {
209 cleanupcvs(@files);
210 die "Exiting: The commit did not succeed";
212 print "Committed successfully to CVS\n";
213 } else {
214 print "Ready for you to commit, just run:\n\n $cmd\n";
216 sub usage {
217 print STDERR <<END;
218 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [ parent ] commit
220 exit(1);
223 # ensure cvs is clean before we die
224 sub cleanupcvs {
225 my @files = @_;
226 foreach my $f (@files) {
227 system('cvs', '-q', 'update', '-C', $f);
228 if ($?) {
229 warn "Warning! Failed to cleanup state of $f\n";
234 # An alterative to `command` that allows input to be passed as an array
235 # to work around shell problems with weird characters in arguments
236 # if the exec returns non-zero we die
237 sub safe_pipe_capture {
238 my @output;
239 if (my $pid = open my $child, '-|') {
240 @output = (<$child>);
241 close $child or die join(' ',@_).": $! $?";
242 } else {
243 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
245 return wantarray ? @output : join('',@output);