git-cvsexportcommit: Add -f(orce) and -m(essage prefix) flags, small cleanups.
[git/raj.git] / git-cvsexportcommit.perl
blobf994443c6fa16257b9b6a44abfc999d2ed25e601
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, $opt_f, $opt_m );
15 getopts('hpvcfm:');
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 open(MSG, ">.msg") or die "Cannot open .msg for writing";
81 print MSG $opt_m;
82 close MSG;
84 `git-cat-file commit $commit | sed -e '1,/^\$/d' >> .msg`;
85 $? && die "Error extracting the commit message";
87 my (@afiles, @dfiles, @mfiles);
88 my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit);
89 #print @files;
90 $? && die "Error in git-diff-tree";
91 foreach my $f (@files) {
92 chomp $f;
93 my @fields = split(m!\s+!, $f);
94 if ($fields[4] eq 'A') {
95 push @afiles, $fields[5];
97 if ($fields[4] eq 'M') {
98 push @mfiles, $fields[5];
100 if ($fields[4] eq 'R') {
101 push @dfiles, $fields[5];
104 $opt_v && print "The commit affects:\n ";
105 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
106 undef @files; # don't need it anymore
108 # check that the files are clean and up to date according to cvs
109 my $dirty;
110 foreach my $f (@afiles) {
111 # This should return only one value
112 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
113 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
114 unless ($status[0] =~ m/Status: Unknown$/) {
115 $dirty = 1;
116 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";
119 foreach my $f (@mfiles, @dfiles) {
120 # TODO:we need to handle removed in cvs
121 my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
122 if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
123 unless ($status[0] =~ m/Status: Up-to-date$/) {
124 $dirty = 1;
125 warn "File $f not up to date in your CVS checkout!\n";
128 if ($dirty) {
129 if ($opt_f) { warn "The tree is not clean -- forced merge\n";
130 $dirty = 0;
131 } else {
132 die "Exiting: your CVS tree is not clean for this merge.";
137 ### NOTE: if you are planning to die() past this point
138 ### you MUST call cleanupcvs(@files) before die()
142 print "'Patching' binary files\n";
144 my @bfiles = grep(m/^Binary/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit));
145 @bfiles = map { chomp } @bfiles;
146 foreach my $f (@bfiles) {
147 # check that the file in cvs matches the "old" file
148 # extract the file to $tmpdir and comparre with cmp
149 my $tree = safe_pipe_capture('git-rev-parse', "$parent^{tree}");
150 chomp $tree;
151 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
152 chomp $blob;
153 `git-cat-file blob $blob > $tmpdir/blob`;
154 if (system('cmp', '-q', $f, "$tmpdir/blob")) {
155 warn "Binary file $f in CVS does not match parent.\n";
156 $dirty = 1;
157 next;
160 # replace with the new file
161 `git-cat-file blob $blob > $f`;
163 # TODO: something smart with file modes
166 if ($dirty) {
167 cleanupcvs(@files);
168 die "Exiting: Binary files in CVS do not match parent";
171 ## apply non-binary changes
172 my $fuzz = $opt_p ? 0 : 2;
174 print "Patching non-binary files\n";
175 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
177 my $dirtypatch = 0;
178 if (($? >> 8) == 2) {
179 cleanupcvs(@files);
180 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
181 } elsif (($? >> 8) == 1) { # some hunks failed to apply
182 $dirtypatch = 1;
185 foreach my $f (@afiles) {
186 system('cvs', 'add', $f);
187 if ($?) {
188 $dirty = 1;
189 warn "Failed to cvs add $f -- you may need to do it manually";
193 foreach my $f (@dfiles) {
194 system('cvs', 'rm', '-f', $f);
195 if ($?) {
196 $dirty = 1;
197 warn "Failed to cvs rm -f $f -- you may need to do it manually";
201 print "Commit to CVS\n";
202 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
203 my $cmd = "cvs commit -F .msg $commitfiles";
205 if ($dirtypatch) {
206 print "NOTE: One or more hunks failed to apply cleanly.\n";
207 print "Resolve the conflicts and then commit using:\n";
208 print "\n $cmd\n\n";
209 exit(1);
213 if ($opt_c) {
214 print "Autocommit\n $cmd\n";
215 print safe_pipe_capture('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
216 if ($?) {
217 cleanupcvs(@files);
218 die "Exiting: The commit did not succeed";
220 print "Committed successfully to CVS\n";
221 } else {
222 print "Ready for you to commit, just run:\n\n $cmd\n";
224 sub usage {
225 print STDERR <<END;
226 Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
228 exit(1);
231 # ensure cvs is clean before we die
232 sub cleanupcvs {
233 my @files = @_;
234 foreach my $f (@files) {
235 system('cvs', '-q', 'update', '-C', $f);
236 if ($?) {
237 warn "Warning! Failed to cleanup state of $f\n";
242 # An alterative to `command` that allows input to be passed as an array
243 # to work around shell problems with weird characters in arguments
244 # if the exec returns non-zero we die
245 sub safe_pipe_capture {
246 my @output;
247 if (my $pid = open my $child, '-|') {
248 @output = (<$child>);
249 close $child or die join(' ',@_).": $! $?";
250 } else {
251 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
253 return wantarray ? @output : join('',@output);