git-cvsexportcommit.perl 8.1 KB
Newer Older
M
Martin Langhoff 已提交
1 2
#!/usr/bin/perl -w

3 4
# Known limitations:
# - does not propagate permissions
5 6
# - error handling has not been extensively tested
#
7

M
Martin Langhoff 已提交
8 9 10 11
use strict;
use Getopt::Std;
use File::Temp qw(tempdir);
use Data::Dumper;
12
use File::Basename qw(basename dirname);
M
Martin Langhoff 已提交
13 14 15 16 17

unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
    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);
M
Martin Langhoff 已提交
19

20
getopts('hPpvcfam:d:');
M
Martin Langhoff 已提交
21 22 23 24 25

$opt_h && usage();

die "Need at least one commit identifier!" unless @ARGV;

26 27 28 29 30 31 32
my @cvs;
if ($opt_d) {
	@cvs = ('cvs', '-d', $opt_d);
} else {
	@cvs = ('cvs');
}

M
Martin Langhoff 已提交
33 34 35 36 37 38 39 40
# setup a tempdir
our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
				     TMPDIR => 1,
				     CLEANUP => 1);

# resolve target commit
my $commit;
$commit = pop @ARGV;
41
$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
M
Martin Langhoff 已提交
42 43 44 45 46 47 48 49 50
chomp $commit;
if ($?) {
    die "The commit reference $commit did not resolve!";
}

# resolve what parent we want
my $parent;
if (@ARGV) {
    $parent = pop @ARGV;
51
    $parent =  safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
M
Martin Langhoff 已提交
52 53 54 55 56 57 58
    chomp $parent;
    if ($?) {
	die "The parent reference did not resolve!";
    }
}

# find parents from the commit itself
59
my @commit  = safe_pipe_capture('git-cat-file', 'commit', $commit);
M
Martin Langhoff 已提交
60
my @parents;
61 62 63 64 65 66 67 68 69 70 71
my $committer;
my $author;
my $stage = 'headers'; # headers, msg
my $title;
my $msg = '';

foreach my $line (@commit) {
    chomp $line;
    if ($stage eq 'headers' && $line eq '') {
	$stage = 'msg';
	next;
M
Martin Langhoff 已提交
72
    }
73 74 75 76

    if ($stage eq 'headers') {
	if ($line =~ m/^parent (\w{40})$/) { # found a parent
	    push @parents, $1;
77
	} elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
78
	    $author = $1;
79
	} elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
80 81 82 83 84 85 86
	    $committer = $1;
	}
    } else {
	$msg .= $line . "\n";
	unless ($title) {
	    $title = $line;
	}
M
Martin Langhoff 已提交
87 88 89 90
    }
}

if ($parent) {
91
    my $found;
M
Martin Langhoff 已提交
92 93 94 95 96 97
    # double check that it's a valid parent
    foreach my $p (@parents) {
	if ($p eq $parent) {
	    $found = 1;
	    last;
	}; # found it
98
    }
99
    die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
M
Martin Langhoff 已提交
100 101 102 103 104 105 106 107 108 109 110
} else { # we don't have a parent from the cmdline...
    if (@parents == 1) { # it's safe to get it from the commit
	$parent = $parents[0];
    } else { # or perhaps not!
	die "This commit has more than one parent -- please name the parent you want to use explicitly";
    }
}

$opt_v && print "Applying to CVS commit $commit from parent $parent\n";

# grab the commit message
111
open(MSG, ">.msg") or die "Cannot open .msg for writing";
112 113 114 115 116 117 118 119 120 121
if ($opt_m) {
    print MSG $opt_m;
}
print MSG $msg;
if ($opt_a) {
    print MSG "\n\nAuthor: $author\n";
    if ($author ne $committer) {
	print MSG "Committer: $committer\n";
    }
}
122 123
close MSG;

124 125 126
`git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";

## apply non-binary changes
127 128 129 130 131 132

# In pedantic mode require all lines of context to match.  In normal
# mode, be compatible with diff/patch: assume 3 lines of context and
# require at least one line match, i.e. ignore at most 2 lines of
# context, like diff/patch do by default.
my $context = $opt_p ? '' : '-C1';
133 134 135 136

print "Checking if patch will apply\n";

my @stat;
137
open APPLY, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
138 139 140 141 142 143 144 145 146 147
@stat=<APPLY>;
close APPLY || die "Cannot patch";
my (@bfiles,@files,@afiles,@dfiles);
chomp @stat;
foreach (@stat) {
	push (@bfiles,$1) if m/^-\t-\t(.*)$/;
	push (@files, $1) if m/^-\t-\t(.*)$/;
	push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
	push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
	push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
M
Martin Langhoff 已提交
148
}
149 150
map { s/^"(.*)"$/$1/g } @bfiles,@files;
map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
M
Martin Langhoff 已提交
151 152 153

# check that the files are clean and up to date according to cvs
my $dirty;
154 155 156 157 158 159 160 161 162
my @dirs;
foreach my $p (@afiles) {
    my $path = dirname $p;
    while (!-d $path and ! grep { $_ eq $path } @dirs) {
	unshift @dirs, $path;
	$path = dirname $path;
    }
}

163
# ... check dirs,
164 165 166 167 168 169
foreach my $d (@dirs) {
    if (-e $d) {
	$dirty = 1;
	warn "$d exists and is not a directory!\n";
    }
}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

# ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
my @canstatusfiles;
foreach my $f (@files) {
    my $path = dirname $f;
    next if (grep { $_ eq $path } @dirs);
    push @canstatusfiles, $f;
}

my %cvsstat;
if (@canstatusfiles) {
    my @cvsoutput;
    @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles);
    my $matchcount = 0;
    foreach my $l (@cvsoutput) {
        chomp $l;
        if ( $l =~ /^File:/ and  $l =~ /Status: (.*)$/ ) {
            $cvsstat{$canstatusfiles[$matchcount]} = $1;
            $matchcount++;
        }
190
    }
191 192 193 194 195
}

# ... validate new files,
foreach my $f (@afiles) {
    if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") {
196
 	$dirty = 1;
197
	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";
198
	warn "Status was: $cvsstat{$f}\n";
199 200
    }
}
201
# ... validate known files.
202 203
foreach my $f (@files) {
    next if grep { $_ eq $f } @afiles;
204
    # TODO:we need to handle removed in cvs
205
    unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
M
Martin Langhoff 已提交
206
	$dirty = 1;
207
	warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
M
Martin Langhoff 已提交
208 209 210
    }
}
if ($dirty) {
211 212 213 214 215
    if ($opt_f) {	warn "The tree is not clean -- forced merge\n";
	$dirty = 0;
    } else {
	die "Exiting: your CVS tree is not clean for this merge.";
    }
M
Martin Langhoff 已提交
216 217
}

218
print "Applying\n";
219
`GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
M
Martin Langhoff 已提交
220

221 222
print "Patch applied successfully. Adding new files and directories to CVS\n";
my $dirtypatch = 0;
223
foreach my $d (@dirs) {
224
    if (system(@cvs,'add',$d)) {
225
	$dirtypatch = 1;
226 227 228 229
	warn "Failed to cvs add directory $d -- you may need to do it manually";
    }
}

M
Martin Langhoff 已提交
230
foreach my $f (@afiles) {
231
    if (grep { $_ eq $f } @bfiles) {
232
      system(@cvs, 'add','-kb',$f);
233
    } else {
234
      system(@cvs, 'add', $f);
235
    }
M
Martin Langhoff 已提交
236
    if ($?) {
237
	$dirtypatch = 1;
M
Martin Langhoff 已提交
238 239 240 241 242
	warn "Failed to cvs add $f -- you may need to do it manually";
    }
}

foreach my $f (@dfiles) {
243
    system(@cvs, 'rm', '-f', $f);
M
Martin Langhoff 已提交
244
    if ($?) {
245
	$dirtypatch = 1;
M
Martin Langhoff 已提交
246 247 248 249 250
	warn "Failed to cvs rm -f $f -- you may need to do it manually";
    }
}

print "Commit to CVS\n";
251 252
print "Patch title (first comment line): $title\n";
my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
253
my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
M
Martin Langhoff 已提交
254 255 256

if ($dirtypatch) {
    print "NOTE: One or more hunks failed to apply cleanly.\n";
257 258 259
    print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
    print "using a patch program. After applying the patch and resolving the\n";
    print "problems you may commit using:";
M
Martin Langhoff 已提交
260
    print "\n    $cmd\n\n";
J
Junio C Hamano 已提交
261
    exit(1);
M
Martin Langhoff 已提交
262 263 264 265
}

if ($opt_c) {
    print "Autocommit\n  $cmd\n";
266
    print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
M
Martin Langhoff 已提交
267 268 269 270
    if ($?) {
	die "Exiting: The commit did not succeed";
    }
    print "Committed successfully to CVS\n";
271 272
    # clean up
    unlink(".msg");
M
Martin Langhoff 已提交
273 274 275
} else {
    print "Ready for you to commit, just run:\n\n   $cmd\n";
}
276 277 278 279

# clean up
unlink(".cvsexportcommit.diff");

M
Martin Langhoff 已提交
280 281
sub usage {
	print STDERR <<END;
282
Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
M
Martin Langhoff 已提交
283 284 285 286
END
	exit(1);
}

287
# An alternative to `command` that allows input to be passed as an array
288 289 290 291 292 293 294 295 296 297 298 299
# to work around shell problems with weird characters in arguments
# if the exec returns non-zero we die
sub safe_pipe_capture {
    my @output;
    if (my $pid = open my $child, '-|') {
	@output = (<$child>);
	close $child or die join(' ',@_).": $! $?";
    } else {
	exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
    }
    return wantarray ? @output : join('',@output);
}
300

301 302 303 304 305 306 307 308 309 310 311
sub safe_pipe_capture_blob {
    my $output;
    if (my $pid = open my $child, '-|') {
        local $/;
	undef $/;
	$output = (<$child>);
	close $child or die join(' ',@_).": $! $?";
    } else {
	exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
    }
    return $output;
312
}