Merge branches 'jc/sb' and 'jc/mb'
[git/gitweb.git] / git-cvsexportcommit.perl
blob50b041c3247238f7b399c0648cc7c3bc2a58dba7
1 #!/usr/bin/perl -w
3 use strict;
4 use Getopt::Std;
5 use File::Temp qw(tempdir);
6 use Data::Dumper;
8 unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
9 die "GIT_DIR is not defined or is unreadable";
12 our ($opt_h, $opt_p, $opt_v, $opt_c );
14 getopt('hpvc');
16 $opt_h && usage();
18 die "Need at least one commit identifier!" unless @ARGV;
20 # setup a tempdir
21 our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
22 TMPDIR => 1,
23 CLEANUP => 1);
25 print Dumper(@ARGV);
26 # resolve target commit
27 my $commit;
28 $commit = pop @ARGV;
29 $commit = `git-rev-parse --verify "$commit"^0`;
30 chomp $commit;
31 if ($?) {
32 die "The commit reference $commit did not resolve!";
35 # resolve what parent we want
36 my $parent;
37 if (@ARGV) {
38 $parent = pop @ARGV;
39 $parent = `git-rev-parse --verify "$parent"^0"`;
40 chomp $parent;
41 if ($?) {
42 die "The parent reference did not resolve!";
46 # find parents from the commit itself
47 my @commit = `git-cat-file commit $commit`;
48 my @parents;
49 foreach my $p (@commit) {
50 if ($p =~ m/^$/) { # end of commit headers, we're done
51 last;
53 if ($p =~ m/^parent (\w{40})$/) { # found a parent
54 push @parents, $1;
58 if ($parent) {
59 # double check that it's a valid parent
60 foreach my $p (@parents) {
61 my $found;
62 if ($p eq $parent) {
63 $found = 1;
64 last;
65 }; # found it
66 die "Did not find $parent in the parents for this commit!";
68 } else { # we don't have a parent from the cmdline...
69 if (@parents == 1) { # it's safe to get it from the commit
70 $parent = $parents[0];
71 } else { # or perhaps not!
72 die "This commit has more than one parent -- please name the parent you want to use explicitly";
76 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
78 # grab the commit message
79 `git-cat-file commit $commit | sed -e '1,/^\$/d' > .msg`;
80 $? && die "Error extraction the commit message";
82 my (@afiles, @dfiles, @mfiles);
83 my @files = `git-diff-tree -r $parent $commit`;
84 print @files;
85 $? && die "Error in git-diff-tree";
86 foreach my $f (@files) {
87 chomp $f;
88 my @fields = split(m/\s+/, $f);
89 if ($fields[4] eq 'A') {
90 push @afiles, $fields[5];
92 if ($fields[4] eq 'M') {
93 push @mfiles, $fields[5];
95 if ($fields[4] eq 'R') {
96 push @dfiles, $fields[5];
99 $opt_v && print "The commit affects:\n ";
100 $opt_v && print join ("\n ", @afiles,@mfiles,@dfiles) . "\n\n";
101 undef @files; # don't need it anymore
103 # check that the files are clean and up to date according to cvs
104 my $dirty;
105 foreach my $f (@afiles, @mfiles, @dfiles) {
106 # TODO:we need to handle removed in cvs and/or new (from git)
107 my $status = `cvs -q status "$f" | grep '^File: '`;
109 unless ($status =~ m/Status: Up-to-date$/) {
110 $dirty = 1;
111 warn "File $f not up to date in your CVS checkout!\n";
114 if ($dirty) {
115 die "Exiting: your CVS tree is not clean for this merge.";
119 ### NOTE: if you are planning to die() past this point
120 ### you MUST call cleanupcvs(@files) before die()
124 print "'Patching' binary files\n";
126 my @bfiles = `git-diff-tree -p $parent $commit | grep '^Binary'`;
127 @bfiles = map { chomp } @bfiles;
128 foreach my $f (@bfiles) {
129 # check that the file in cvs matches the "old" file
130 # extract the file to $tmpdir and comparre with cmp
131 my $tree = `git-rev-parse $parent^{tree} `;
132 chomp $tree;
133 my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
134 chomp $blob;
135 `git-cat-file blob $blob > $tmpdir/blob`;
136 `cmp -q $f $tmpdir/blob`;
137 if ($?) {
138 warn "Binary file $f in CVS does not match parent.\n";
139 $dirty = 1;
140 next;
143 # replace with the new file
144 `git-cat-file blob $blob > $f`;
146 # TODO: something smart with file modes
149 if ($dirty) {
150 cleanupcvs(@files);
151 die "Exiting: Binary files in CVS do not match parent";
154 ## apply non-binary changes
155 my $fuzz = $opt_p ? 0 : 2;
157 print "Patching non-binary files\n";
158 print `(git-diff-tree -p $parent -p $commit | patch -p1 -F $fuzz ) 2>&1`;
160 my $dirtypatch = 0;
161 if (($? >> 8) == 2) {
162 cleanupcvs(@files);
163 die "Exiting: Patch reported serious trouble -- you will have to apply this patch manually";
164 } elsif (($? >> 8) == 1) { # some hunks failed to apply
165 $dirtypatch = 1;
168 foreach my $f (@afiles) {
169 `cvs add $f`;
170 if ($?) {
171 $dirty = 1;
172 warn "Failed to cvs add $f -- you may need to do it manually";
176 foreach my $f (@dfiles) {
177 `cvs rm -f $f`;
178 if ($?) {
179 $dirty = 1;
180 warn "Failed to cvs rm -f $f -- you may need to do it manually";
184 print "Commit to CVS\n";
185 my $commitfiles = join(' ', @afiles, @mfiles, @dfiles);
186 my $cmd = "cvs commit -F .msg $commitfiles";
188 if ($dirtypatch) {
189 print "NOTE: One or more hunks failed to apply cleanly.\n";
190 print "Resolve the conflicts and then commit using:n";
191 print "\n $cmd\n\n";
192 exit;
196 if ($opt_c) {
197 print "Autocommit\n $cmd\n";
198 print `cvs commit -F .msg $commitfiles 2>&1`;
199 if ($?) {
200 cleanupcvs(@files);
201 die "Exiting: The commit did not succeed";
203 print "Committed successfully to CVS\n";
204 } else {
205 print "Ready for you to commit, just run:\n\n $cmd\n";
207 sub usage {
208 print STDERR <<END;
209 Usage: GIT_DIR=/path/to/.gi ${\basename $0} # fetch/update GIT from CVS
210 [-h] [-p] [ parent ] commit
212 exit(1);
215 # ensure cvs is clean before we die
216 sub cleanupcvs {
217 my @files = @_;
218 foreach my $f (@files) {
219 `cvs -q update -C "$f"`;
220 if ($?) {
221 warn "Warning! Failed to cleanup state of $f\n";