4 use vars qw/ $AUTHOR $VERSION
5 $SVN_URL $SVN_INFO $SVN_WC
6 $GIT_SVN_INDEX $GIT_SVN
8 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
10 $GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
11 $GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
12 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
13 $ENV{GIT_DIR} ||= $GIT_DIR;
15 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
16 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
18 # make sure the svn binary gives consistent output between locales and TZs:
22 # If SVN:: library support is added, please make the dependencies
23 # optional and preserve the capability to use the command-line client.
24 # use eval { require SVN::... } to make it lazy load
27 use File::Basename qw/dirname basename/;
28 use File::Path qw/mkpath/;
29 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
31 my $sha1 = qr/[a-f\d]{40}/;
32 my $sha1_short = qr/[a-f\d]{6,40}/;
33 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
34 $_find_copies_harder, $_l);
36 GetOptions( 'revision|r=s' => \$_revision,
37 'no-ignore-externals' => \$_no_ignore_ext,
41 'help|H|h' => \$_help,
42 'find-copies-harder' => \$_find_copies_harder,
44 'no-stop-on-copy' => \$_no_stop_copy );
46 fetch => [ \&fetch, "Download new revisions from SVN" ],
47 init => [ \&init, "Initialize and fetch (import)"],
48 commit => [ \&commit, "Commit git revisions to SVN" ],
49 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
50 help => [ \&usage, "Show help" ],
53 for (my $i = 0; $i < @ARGV; $i++) {
54 if (defined $cmd{$ARGV[$i]}) {
61 # we may be called as git-svn-(command), or git-svn(command).
63 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
69 usage(1) unless (defined $cmd);
70 svn_check_ignore_externals();
71 $cmd{$cmd}->[0]->(@ARGV);
74 ####################### primary functions ######################
76 my $exit = shift || 0;
77 my $fd = $exit ? \*STDERR : \*STDOUT;
79 git-svn - bidirectional operations between a single Subversion tree and git
80 Usage: $0 <command> [options] [arguments]\n
83 foreach (sort keys %cmd) {
84 print $fd ' ',pack('A10',$_),$cmd{$_}->[1],"\n";
87 \nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
88 you're tracking multiple SVN branches/repositories in one git repository
89 and want to keep them separate.
95 $SVN_URL = shift or undef;
99 my $pid = open(my $rev_list,'-|');
100 defined $pid or croak $!;
102 exec("git-rev-list","$GIT_SVN-HEAD") or croak $!;
105 while (<$rev_list>) {
108 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
109 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
110 next if (!@commit); # skip merges
111 my $id = $commit[$#commit];
112 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
114 if (!$rev || !$uuid || !$url) {
115 # some of the original repositories I made had
116 # indentifiers like this:
117 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
119 if (!$rev || !$uuid) {
120 croak "Unable to extract revision or UUID from ",
124 print "r$rev = $c\n";
125 unless (defined $first) {
126 if (!$SVN_URL && !$url) {
127 croak "SVN repository location required: $url\n";
130 $repo_uuid = setup_git_svn();
133 if ($uuid ne $repo_uuid) {
134 croak "Repository UUIDs do not match!\ngot: $uuid\n",
135 "expected: $repo_uuid\n";
137 assert_revision_eq_or_unknown($rev, $c);
138 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
139 $newest_rev = $rev if ($rev > $newest_rev);
141 close $rev_list or croak $?;
142 if (!chdir $SVN_WC) {
143 my @svn_co = ('svn','co',"-r$first");
144 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
145 sys(@svn_co, $SVN_URL, $SVN_WC);
146 chdir $SVN_WC or croak $!;
150 defined $pid or croak $!;
152 my @svn_up = qw(svn up);
153 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
154 sys(@svn_up,"-r$newest_rev");
155 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
157 exec('git-write-tree');
163 $SVN_URL = shift or croak "SVN repository location required\n";
164 unless (-d $GIT_DIR) {
172 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
173 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
174 unless ($_revision) {
175 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
177 push @log_args, "-r$_revision";
178 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
180 my $svn_log = svn_log_raw(@log_args);
181 @$svn_log = sort { $a->{revision} <=> $b->{revision} } @$svn_log;
183 my $base = shift @$svn_log or croak "No base revision!\n";
184 my $last_commit = undef;
185 unless (-d $SVN_WC) {
186 my @svn_co = ('svn','co',"-r$base->{revision}");
187 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
188 sys(@svn_co, $SVN_URL, $SVN_WC);
189 chdir $SVN_WC or croak $!;
190 $last_commit = git_commit($base, @parents);
191 unless (-f "$GIT_DIR/refs/heads/master") {
192 sys(qw(git-update-ref refs/heads/master),$last_commit);
194 assert_svn_wc_clean($base->{revision}, $last_commit);
196 chdir $SVN_WC or croak $!;
197 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
199 my @svn_up = qw(svn up);
200 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
201 my $last_rev = $base->{revision};
202 foreach my $log_msg (@$svn_log) {
203 assert_svn_wc_clean($last_rev, $last_commit);
204 $last_rev = $log_msg->{revision};
205 sys(@svn_up,"-r$last_rev");
206 $last_commit = git_commit($log_msg, $last_commit, @parents);
208 assert_svn_wc_clean($last_rev, $last_commit);
209 return pop @$svn_log;
214 if ($_stdin || !@commits) {
215 print "Reading from stdin...\n";
218 if (/\b([a-f\d]{6,40})\b/) {
219 unshift @commits, $1;
224 foreach my $c (@commits) {
225 chomp(my @tmp = safe_qx('git-rev-parse',$c));
226 if (scalar @tmp == 1) {
228 } elsif (scalar @tmp > 1) {
229 push @revs, reverse (safe_qx('git-rev-list',@tmp));
231 die "Failed to rev-parse $c\n";
237 chdir $SVN_WC or croak $!;
238 my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
239 foreach my $c (@revs) {
240 print "Committing $c\n";
241 my $mods = svn_checkout_tree($svn_current_rev, $c);
242 if (scalar @$mods == 0) {
243 print "Skipping, no changes detected\n";
246 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
248 print "Done committing ",scalar @revs," revisions to SVN\n";
252 ########################### utility functions #########################
255 defined $SVN_URL or croak "SVN repository location required\n";
256 unless (-d $GIT_DIR) {
257 croak "GIT_DIR=$GIT_DIR does not exist!\n";
259 mkpath(["$GIT_DIR/$GIT_SVN"]);
260 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
262 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
263 my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
264 croak "Repository UUID unreadable\n";
265 s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
267 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
268 print $fd '.svn',"\n";
269 close $fd or croak $!;
273 sub assert_svn_wc_clean {
274 my ($svn_rev, $treeish) = @_;
275 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
276 croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
277 my $svn_info = svn_info('.');
278 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
279 croak "Expected r$svn_rev, got r",
280 $svn_info->{'Last Changed Rev'},"\n";
282 my @status = grep(!/^Performing status on external/,(`svn status`));
283 @status = grep(!/^\s*$/,@status);
284 if (scalar @status) {
285 print STDERR "Tree ($SVN_WC) is not clean:\n";
286 print STDERR $_ foreach @status;
289 assert_tree($treeish);
294 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
295 chomp(my $type = `git-cat-file -t $treeish`);
297 while ($type eq 'tag') {
298 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
300 if ($type eq 'commit') {
301 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
302 ($expected) = ($expected =~ /^tree ($sha1)$/);
303 die "Unable to get tree from $treeish\n" unless $expected;
304 } elsif ($type eq 'tree') {
305 $expected = $treeish;
307 die "$treeish is a $type, expected tree, tag or commit\n";
310 my $old_index = $ENV{GIT_INDEX_FILE};
311 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
313 unlink $tmpindex or croak $!;
315 $ENV{GIT_INDEX_FILE} = $tmpindex;
317 chomp(my $tree = `git-write-tree`);
319 $ENV{GIT_INDEX_FILE} = $old_index;
321 delete $ENV{GIT_INDEX_FILE};
323 if ($tree ne $expected) {
324 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
328 sub parse_diff_tree {
334 chomp $_; # this gets rid of the trailing "\0"
335 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
336 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
337 push @mods, { mode_a => $1, mode_b => $2,
338 sha1_b => $3, chg => $4 };
339 if ($4 =~ /^(?:C|R)$/) {
344 } elsif ($state eq 'file_a') {
345 my $x = $mods[$#mods] or croak "Empty array\n";
346 if ($x->{chg} !~ /^(?:C|R)$/) {
347 croak "Error parsing $_, $x->{chg}\n";
351 } elsif ($state eq 'file_b') {
352 my $x = $mods[$#mods] or croak "Empty array\n";
353 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
354 croak "Error parsing $_, $x->{chg}\n";
356 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
357 croak "Error parsing $_, $x->{chg}\n";
362 croak "Error parsing $_\n";
365 close $diff_fh or croak $!;
370 sub svn_check_prop_executable {
372 return if -l $m->{file_b};
373 if ($m->{mode_b} =~ /755$/) {
374 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
375 if ($m->{mode_a} !~ /755$/) {
376 sys(qw(svn propset svn:executable 1), $m->{file_b});
378 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
379 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
380 sys(qw(svn propdel svn:executable), $m->{file_b});
381 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
382 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
386 sub svn_ensure_parent_path {
387 my $dir_b = dirname(shift);
388 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
389 mkpath([$dir_b]) unless (-d $dir_b);
390 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
393 sub precommit_check {
395 my (%rm_file, %rmdir_check, %added_check);
397 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
398 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
399 if ($m->{chg} eq 'R') {
400 if (-d $m->{file_b}) {
401 err_dir_to_file("$m->{file_a} => $m->{file_b}");
403 # dir/$file => dir/file/$file
404 my $dirname = dirname($m->{file_b});
405 while ($dirname ne File::Spec->curdir) {
406 if ($dirname ne $m->{file_a}) {
407 $dirname = dirname($dirname);
410 err_file_to_dir("$m->{file_a} => $m->{file_b}");
412 # baz/zzz => baz (baz is a file)
413 $dirname = dirname($m->{file_a});
414 while ($dirname ne File::Spec->curdir) {
415 if ($dirname ne $m->{file_b}) {
416 $dirname = dirname($dirname);
419 err_dir_to_file("$m->{file_a} => $m->{file_b}");
422 if ($m->{chg} =~ /^(D|R)$/) {
423 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
424 $rm_file{ $m->{$t} } = 1;
425 my $dirname = dirname( $m->{$t} );
426 my $basename = basename( $m->{$t} );
427 $rmdir_check{$dirname}->{$basename} = 1;
428 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
429 if (-d $m->{file_b}) {
430 err_dir_to_file($m->{file_b});
432 my $dirname = dirname( $m->{file_b} );
433 my $basename = basename( $m->{file_b} );
434 $added_check{$dirname}->{$basename} = 1;
435 while ($dirname ne File::Spec->curdir) {
436 if ($rm_file{$dirname}) {
437 err_file_to_dir($m->{file_b});
439 $dirname = dirname $dirname;
443 return (\%rmdir_check, \%added_check);
445 sub err_dir_to_file {
447 print STDERR "Node change from directory to file ",
448 "is not supported by Subversion: ",$file,"\n";
451 sub err_file_to_dir {
453 print STDERR "Node change from file to directory ",
454 "is not supported by Subversion: ",$file,"\n";
459 sub svn_checkout_tree {
460 my ($svn_rev, $treeish) = @_;
461 my $from = file_to_s("$REV_DIR/$svn_rev");
462 assert_svn_wc_clean($svn_rev,$from);
463 print "diff-tree '$from' '$treeish'\n";
464 my $pid = open my $diff_fh, '-|';
465 defined $pid or croak $!;
467 my @diff_tree = qw(git-diff-tree -z -r -C);
468 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
469 push @diff_tree, "-l$_l" if defined $_l;
470 exec(@diff_tree, $from, $treeish) or croak $!;
472 my $mods = parse_diff_tree($diff_fh);
474 # git can do empty commits, SVN doesn't allow it...
477 my ($rm, $add) = precommit_check($mods);
479 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
480 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
481 if ($m->{chg} eq 'C') {
482 svn_ensure_parent_path( $m->{file_b} );
483 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
484 apply_mod_line_blob($m);
485 svn_check_prop_executable($m);
486 } elsif ($m->{chg} eq 'D') {
487 sys(qw(svn rm --force), $m->{file_b});
488 } elsif ($m->{chg} eq 'R') {
489 svn_ensure_parent_path( $m->{file_b} );
490 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
491 apply_mod_line_blob($m);
492 svn_check_prop_executable($m);
493 } elsif ($m->{chg} eq 'M') {
494 apply_mod_line_blob($m);
495 svn_check_prop_executable($m);
496 } elsif ($m->{chg} eq 'T') {
497 sys(qw(svn rm --force),$m->{file_b});
498 apply_mod_line_blob($m);
499 sys(qw(svn add --force), $m->{file_b});
500 svn_check_prop_executable($m);
501 } elsif ($m->{chg} eq 'A') {
502 svn_ensure_parent_path( $m->{file_b} );
503 apply_mod_line_blob($m);
504 sys(qw(svn add --force), $m->{file_b});
505 svn_check_prop_executable($m);
507 croak "Invalid chg: $m->{chg}\n";
511 assert_tree($treeish);
512 if ($_rmdir) { # remove empty directories
513 handle_rmdir($rm, $add);
515 assert_tree($treeish);
519 # svn ls doesn't work with respect to the current working tree, but what's
520 # in the repository. There's not even an option for it... *sigh*
521 # (added files don't show up and removed files remain in the ls listing)
523 my ($dir, $rm, $add) = @_;
524 chomp(my @ls = safe_qx('svn','ls',$dir));
527 s#/$##; # trailing slashes are evil
528 push @ret, $_ unless $rm->{$dir}->{$_};
530 if (exists $add->{$dir}) {
531 push @ret, keys %{$add->{$dir}};
539 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
540 my $ls = svn_ls_current($dir, $rm, $add);
541 next if (scalar @$ls);
542 sys(qw(svn rm --force),$dir);
544 my $dn = dirname $dir;
545 $rm->{ $dn }->{ basename $dir } = 1;
546 $ls = svn_ls_current($dn, $rm, $add);
547 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
548 sys(qw(svn rm --force),$dn);
551 $rm->{ $dn }->{ $dir } = 1;
552 $ls = svn_ls_current($dn, $rm, $add);
557 sub svn_commit_tree {
558 my ($svn_rev, $commit) = @_;
559 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
560 open my $msg, '>', $commit_msg or croak $!;
562 chomp(my $type = `git-cat-file -t $commit`);
563 if ($type eq 'commit') {
564 my $pid = open my $msg_fh, '-|';
565 defined $pid or croak $!;
568 exec(qw(git-cat-file commit), $commit) or croak $!;
573 $in_msg = 1 if (/^\s*$/);
575 print $msg $_ or croak $!;
578 close $msg_fh or croak $!;
580 close $msg or croak $!;
582 if ($_edit || ($type eq 'tree')) {
583 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
584 system($editor, $commit_msg);
586 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
587 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
589 defined $committed or croak
590 "Commit output failed to parse committed revision!\n",
591 join("\n",@ci_output),"\n";
592 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
595 my @svn_up = (qw(svn up), "-r$svn_rev");
596 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
598 return fetch("$rev_committed=$commit")->{revision};
603 my $pid = open my $log_fh,'-|';
604 defined $pid or croak $!;
607 exec (qw(svn log), @log_args) or croak $!
615 if ($state eq 'msg') {
616 if ($svn_log[$#svn_log]->{lines}) {
617 $svn_log[$#svn_log]->{msg} .= $_."\n";
618 unless(--$svn_log[$#svn_log]->{lines}) {
622 croak "Log parse error at: $_\n",
623 $svn_log[$#svn_log]->{revision},
628 if ($state ne 'sep') {
629 croak "Log parse error at: $_\n",
631 $svn_log[$#svn_log]->{revision},
636 # if we have an empty log message, put something there:
638 $svn_log[$#svn_log]->{msg} ||= "\n";
639 delete $svn_log[$#svn_log]->{lines};
643 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
645 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
646 ($lines) = ($lines =~ /(\d+)/);
647 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
648 /(\d{4})\-(\d\d)\-(\d\d)\s
649 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
650 or croak "Failed to parse date: $date\n";
651 my %log_msg = ( revision => $rev,
652 date => "$tz $Y-$m-$d $H:$M:$S",
656 push @svn_log, \%log_msg;
657 $state = 'msg_start';
660 # skip the first blank line of the message:
661 if ($state eq 'msg_start' && /^$/) {
663 } elsif ($state eq 'msg') {
664 if ($svn_log[$#svn_log]->{lines}) {
665 $svn_log[$#svn_log]->{msg} .= $_."\n";
666 unless (--$svn_log[$#svn_log]->{lines}) {
670 croak "Log parse error at: $_\n",
671 $svn_log[$#svn_log]->{revision},"\n";
675 close $log_fh or croak $?;
680 my $url = shift || $SVN_URL;
682 my $pid = open my $info_fh, '-|';
683 defined $pid or croak $!;
686 exec(qw(svn info),$url) or croak $!;
690 # only single-lines seem to exist in svn info output
693 if (m#^([^:]+)\s*:\s*(\S*)$#) {
695 push @{$ret->{-order}}, $1;
698 close $info_fh or croak $!;
702 sub sys { system(@_) == 0 or croak $? }
705 system( "git-diff-files --name-only -z ".
706 " | git-update-index --remove -z --stdin && ".
707 "git-ls-files -z --others ".
708 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
709 " | git-update-index --add -z --stdin"
714 my ($str, $file, $mode) = @_;
715 open my $fd,'>',$file or croak $!;
716 print $fd $str,"\n" or croak $!;
717 close $fd or croak $!;
718 chmod ($mode &~ umask, $file) if (defined $mode);
723 open my $fd,'<',$file or croak "$!: file: $file\n";
726 close $fd or croak $!;
731 sub assert_revision_unknown {
733 if (-f "$REV_DIR/$revno") {
734 croak "$REV_DIR/$revno already exists! ",
735 "Why are we refetching it?";
739 sub assert_revision_eq_or_unknown {
740 my ($revno, $commit) = @_;
741 if (-f "$REV_DIR/$revno") {
742 my $current = file_to_s("$REV_DIR/$revno");
743 if ($commit ne $current) {
744 croak "$REV_DIR/$revno already exists!\n",
745 "current: $current\nexpected: $commit\n";
752 my ($log_msg, @parents) = @_;
753 assert_revision_unknown($log_msg->{revision});
754 my $out_fh = IO::File->new_tmpfile or croak $!;
755 my $info = svn_info('.');
756 my $uuid = $info->{'Repository UUID'};
757 defined $uuid or croak "Unable to get Repository UUID\n";
759 # commit parents can be conditionally bound to a particular
760 # svn revision via: "svn_revno=commit_sha1", filter them out here:
762 foreach my $p (@parents) {
763 next unless defined $p;
764 if ($p =~ /^(\d+)=($sha1_short)$/o) {
765 if ($1 == $log_msg->{revision}) {
766 push @exec_parents, $2;
769 push @exec_parents, $p if $p =~ /$sha1_short/o;
774 defined $pid or croak $!;
776 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
778 chomp(my $tree = `git-write-tree`);
780 my $msg_fh = IO::File->new_tmpfile or croak $!;
781 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
782 "$SVN_URL\@$log_msg->{revision}",
783 " $uuid\n" or croak $!;
784 $msg_fh->flush == 0 or croak $!;
785 seek $msg_fh, 0, 0 or croak $!;
787 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
789 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
790 $log_msg->{author}."\@$uuid";
791 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
793 my @exec = ('git-commit-tree',$tree);
794 push @exec, '-p', $_ foreach @exec_parents;
795 open STDIN, '<&', $msg_fh or croak $!;
796 open STDOUT, '>&', $out_fh or croak $!;
797 exec @exec or croak $!;
802 $out_fh->flush == 0 or croak $!;
803 seek $out_fh, 0, 0 or croak $!;
804 chomp(my $commit = do { local $/; <$out_fh> });
805 if ($commit !~ /^$sha1$/o) {
806 croak "Failed to commit, invalid sha1: $commit\n";
808 my @update_ref = ('git-update-ref',"refs/heads/$GIT_SVN-HEAD",$commit);
809 if (my $primary_parent = shift @exec_parents) {
810 push @update_ref, $primary_parent;
813 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
814 print "r$log_msg->{revision} = $commit\n";
818 sub apply_mod_line_blob {
820 if ($m->{mode_b} =~ /^120/) {
821 blob_to_symlink($m->{sha1_b}, $m->{file_b});
823 blob_to_file($m->{sha1_b}, $m->{file_b});
827 sub blob_to_symlink {
828 my ($blob, $link) = @_;
829 defined $link or croak "\$link not defined!\n";
830 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
831 if (-l $link || -f _) {
832 unlink $link or croak $!;
835 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
836 symlink $dest, $link or croak $!;
840 my ($blob, $file) = @_;
841 defined $file or croak "\$file not defined!\n";
842 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
843 if (-l $file || -f _) {
844 unlink $file or croak $!;
847 open my $blob_fh, '>', $file or croak "$!: $file\n";
849 defined $pid or croak $!;
852 open STDOUT, '>&', $blob_fh or croak $!;
853 exec('git-cat-file','blob',$blob);
858 close $blob_fh or croak $!;
862 my $pid = open my $child, '-|';
863 defined $pid or croak $!;
865 exec(@_) or croak $?;
867 my @ret = (<$child>);
868 close $child or croak $?;
869 die $? if $?; # just in case close didn't error out
870 return wantarray ? @ret : join('',@ret);
873 sub svn_check_ignore_externals {
874 return if $_no_ignore_ext;
875 unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
876 print STDERR "W: Installed svn version does not support ",
877 "--ignore-externals\n";
885 @svn_log = array of log_msg hashes
889 msg => 'whitespace-formatted log entry
890 ', # trailing newline is preserved
891 revision => '8', # integer
892 date => '2004-02-24T17:01:44.108345Z', # commit date
893 author => 'committer name'
897 @mods = array of diff-index line hashes, each element represents one line
900 diff-index line ($m hash)
902 mode_a => first column of diff-index output, no leading ':',
903 mode_b => second column of diff-index output,
904 sha1_b => sha1sum of the final blob,
905 chg => change type [MCRAD],
906 file_a => original file name of a file (iff chg is 'C' or 'R')
907 file_b => new/current file name of a file (any chg)