2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC
8 $GIT_SVN_INDEX $GIT_SVN
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
12 $GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
13 $GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
14 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
15 $ENV{GIT_DIR} ||= $GIT_DIR;
17 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
18 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
20 # make sure the svn binary gives consistent output between locales and TZs:
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
29 use File::Basename qw/dirname basename/;
30 use File::Path qw/mkpath/;
31 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
33 use POSIX qw/strftime/;
34 my $sha1 = qr/[a-f\d]{40}/;
35 my $sha1_short = qr/[a-f\d]{6,40}/;
36 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
37 $_find_copies_harder, $_l, $_version, $_upgrade);
39 GetOptions( 'revision|r=s' => \$_revision,
40 'no-ignore-externals' => \$_no_ignore_ext,
44 'upgrade' => \$_upgrade,
45 'help|H|h' => \$_help,
46 'find-copies-harder' => \$_find_copies_harder,
48 'version|V' => \$_version,
49 'no-stop-on-copy' => \$_no_stop_copy );
51 fetch => [ \&fetch, "Download new revisions from SVN" ],
52 init => [ \&init, "Initialize and fetch (import)"],
53 commit => [ \&commit, "Commit git revisions to SVN" ],
54 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings" ],
55 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
56 help => [ \&usage, "Show help" ],
59 for (my $i = 0; $i < @ARGV; $i++) {
60 if (defined $cmd{$ARGV[$i]}) {
67 # we may be called as git-svn-(command), or git-svn(command).
69 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
75 version() if $_version;
76 usage(1) unless (defined $cmd);
77 svn_check_ignore_externals();
78 $cmd{$cmd}->[0]->(@ARGV);
81 ####################### primary functions ######################
83 my $exit = shift || 0;
84 my $fd = $exit ? \*STDERR : \*STDOUT;
86 git-svn - bidirectional operations between a single Subversion tree and git
87 Usage: $0 <command> [options] [arguments]\n
90 foreach (sort keys %cmd) {
91 print $fd ' ',pack('A10',$_),$cmd{$_}->[1],"\n";
94 \nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
95 you're tracking multiple SVN branches/repositories in one git repository
96 and want to keep them separate.
102 print "git-svn version $VERSION\n";
107 $SVN_URL = shift or undef;
111 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
113 check_upgrade_needed();
116 my $pid = open(my $rev_list,'-|');
117 defined $pid or croak $!;
119 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
122 while (<$rev_list>) {
125 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
126 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
127 next if (!@commit); # skip merges
128 my $id = $commit[$#commit];
129 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
131 if (!$rev || !$uuid || !$url) {
132 # some of the original repositories I made had
133 # indentifiers like this:
134 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
136 if (!$rev || !$uuid) {
137 croak "Unable to extract revision or UUID from ",
142 # if we merged or otherwise started elsewhere, this is
143 # how we break out of it
144 next if (defined $repo_uuid && ($uuid ne $repo_uuid));
145 next if (defined $SVN_URL && ($url ne $SVN_URL));
147 print "r$rev = $c\n";
148 unless (defined $latest) {
149 if (!$SVN_URL && !$url) {
150 croak "SVN repository location required: $url\n";
153 $repo_uuid ||= setup_git_svn();
156 assert_revision_eq_or_unknown($rev, $c);
157 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
158 $newest_rev = $rev if ($rev > $newest_rev);
160 close $rev_list or croak $?;
161 if (!chdir $SVN_WC) {
162 my @svn_co = ('svn','co',"-r$latest");
163 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
164 sys(@svn_co, $SVN_URL, $SVN_WC);
165 chdir $SVN_WC or croak $!;
169 defined $pid or croak $!;
171 my @svn_up = qw(svn up);
172 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
173 sys(@svn_up,"-r$newest_rev");
174 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
176 exec('git-write-tree');
182 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
183 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
189 $SVN_URL = shift or croak "SVN repository location required\n";
190 unless (-d $GIT_DIR) {
198 check_upgrade_needed();
199 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
200 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
201 unless ($_revision) {
202 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
204 push @log_args, "-r$_revision";
205 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
207 my $svn_log = svn_log_raw(@log_args);
208 @$svn_log = sort { $a->{revision} <=> $b->{revision} } @$svn_log;
210 my $base = shift @$svn_log or croak "No base revision!\n";
211 my $last_commit = undef;
212 unless (-d $SVN_WC) {
213 my @svn_co = ('svn','co',"-r$base->{revision}");
214 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
215 sys(@svn_co, $SVN_URL, $SVN_WC);
216 chdir $SVN_WC or croak $!;
217 $last_commit = git_commit($base, @parents);
218 unless (-f "$GIT_DIR/refs/heads/master") {
219 sys(qw(git-update-ref refs/heads/master),$last_commit);
221 assert_svn_wc_clean($base->{revision}, $last_commit);
223 chdir $SVN_WC or croak $!;
224 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
226 my @svn_up = qw(svn up);
227 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
228 my $last_rev = $base->{revision};
229 foreach my $log_msg (@$svn_log) {
230 assert_svn_wc_clean($last_rev, $last_commit);
231 $last_rev = $log_msg->{revision};
232 sys(@svn_up,"-r$last_rev");
233 $last_commit = git_commit($log_msg, $last_commit, @parents);
235 assert_svn_wc_clean($last_rev, $last_commit);
236 return pop @$svn_log;
241 check_upgrade_needed();
242 if ($_stdin || !@commits) {
243 print "Reading from stdin...\n";
246 if (/\b([a-f\d]{6,40})\b/) {
247 unshift @commits, $1;
252 foreach my $c (@commits) {
253 chomp(my @tmp = safe_qx('git-rev-parse',$c));
254 if (scalar @tmp == 1) {
256 } elsif (scalar @tmp > 1) {
257 push @revs, reverse (safe_qx('git-rev-list',@tmp));
259 die "Failed to rev-parse $c\n";
265 chdir $SVN_WC or croak $!;
266 my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
267 foreach my $c (@revs) {
268 print "Committing $c\n";
269 my $mods = svn_checkout_tree($svn_current_rev, $c);
270 if (scalar @$mods == 0) {
271 print "Skipping, no changes detected\n";
274 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
276 print "Done committing ",scalar @revs," revisions to SVN\n";
281 require File::Find or die $!;
282 my $exclude_file = "$GIT_DIR/info/exclude";
283 open my $fh, '<', $exclude_file or croak $!;
284 chomp(my @excludes = (<$fh>));
285 close $fh or croak $!;
287 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
288 chdir $SVN_WC or croak $!;
290 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
292 @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
293 }}, no_chdir=>1},'.');
296 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
298 foreach my $i (sort keys %ign) {
299 print "\n# ",$i,"\n";
300 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
304 ########################### utility functions #########################
307 defined $SVN_URL or croak "SVN repository location required\n";
308 unless (-d $GIT_DIR) {
309 croak "GIT_DIR=$GIT_DIR does not exist!\n";
311 mkpath(["$GIT_DIR/$GIT_SVN"]);
312 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
314 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
315 my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
316 croak "Repository UUID unreadable\n";
317 s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
319 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
320 print $fd '.svn',"\n";
321 close $fd or croak $!;
325 sub assert_svn_wc_clean {
326 my ($svn_rev, $treeish) = @_;
327 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
328 croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
329 my $svn_info = svn_info('.');
330 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
331 croak "Expected r$svn_rev, got r",
332 $svn_info->{'Last Changed Rev'},"\n";
334 my @status = grep(!/^Performing status on external/,(`svn status`));
335 @status = grep(!/^\s*$/,@status);
336 if (scalar @status) {
337 print STDERR "Tree ($SVN_WC) is not clean:\n";
338 print STDERR $_ foreach @status;
341 assert_tree($treeish);
346 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
347 chomp(my $type = `git-cat-file -t $treeish`);
349 while ($type eq 'tag') {
350 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
352 if ($type eq 'commit') {
353 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
354 ($expected) = ($expected =~ /^tree ($sha1)$/);
355 die "Unable to get tree from $treeish\n" unless $expected;
356 } elsif ($type eq 'tree') {
357 $expected = $treeish;
359 die "$treeish is a $type, expected tree, tag or commit\n";
362 my $old_index = $ENV{GIT_INDEX_FILE};
363 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
365 unlink $tmpindex or croak $!;
367 $ENV{GIT_INDEX_FILE} = $tmpindex;
369 chomp(my $tree = `git-write-tree`);
371 $ENV{GIT_INDEX_FILE} = $old_index;
373 delete $ENV{GIT_INDEX_FILE};
375 if ($tree ne $expected) {
376 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
380 sub parse_diff_tree {
386 chomp $_; # this gets rid of the trailing "\0"
387 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
388 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
389 push @mods, { mode_a => $1, mode_b => $2,
390 sha1_b => $3, chg => $4 };
391 if ($4 =~ /^(?:C|R)$/) {
396 } elsif ($state eq 'file_a') {
397 my $x = $mods[$#mods] or croak "Empty array\n";
398 if ($x->{chg} !~ /^(?:C|R)$/) {
399 croak "Error parsing $_, $x->{chg}\n";
403 } elsif ($state eq 'file_b') {
404 my $x = $mods[$#mods] or croak "Empty array\n";
405 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
406 croak "Error parsing $_, $x->{chg}\n";
408 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
409 croak "Error parsing $_, $x->{chg}\n";
414 croak "Error parsing $_\n";
417 close $diff_fh or croak $!;
422 sub svn_check_prop_executable {
424 return if -l $m->{file_b};
425 if ($m->{mode_b} =~ /755$/) {
426 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
427 if ($m->{mode_a} !~ /755$/) {
428 sys(qw(svn propset svn:executable 1), $m->{file_b});
430 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
431 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
432 sys(qw(svn propdel svn:executable), $m->{file_b});
433 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
434 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
438 sub svn_ensure_parent_path {
439 my $dir_b = dirname(shift);
440 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
441 mkpath([$dir_b]) unless (-d $dir_b);
442 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
445 sub precommit_check {
447 my (%rm_file, %rmdir_check, %added_check);
449 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
450 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
451 if ($m->{chg} eq 'R') {
452 if (-d $m->{file_b}) {
453 err_dir_to_file("$m->{file_a} => $m->{file_b}");
455 # dir/$file => dir/file/$file
456 my $dirname = dirname($m->{file_b});
457 while ($dirname ne File::Spec->curdir) {
458 if ($dirname ne $m->{file_a}) {
459 $dirname = dirname($dirname);
462 err_file_to_dir("$m->{file_a} => $m->{file_b}");
464 # baz/zzz => baz (baz is a file)
465 $dirname = dirname($m->{file_a});
466 while ($dirname ne File::Spec->curdir) {
467 if ($dirname ne $m->{file_b}) {
468 $dirname = dirname($dirname);
471 err_dir_to_file("$m->{file_a} => $m->{file_b}");
474 if ($m->{chg} =~ /^(D|R)$/) {
475 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
476 $rm_file{ $m->{$t} } = 1;
477 my $dirname = dirname( $m->{$t} );
478 my $basename = basename( $m->{$t} );
479 $rmdir_check{$dirname}->{$basename} = 1;
480 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
481 if (-d $m->{file_b}) {
482 err_dir_to_file($m->{file_b});
484 my $dirname = dirname( $m->{file_b} );
485 my $basename = basename( $m->{file_b} );
486 $added_check{$dirname}->{$basename} = 1;
487 while ($dirname ne File::Spec->curdir) {
488 if ($rm_file{$dirname}) {
489 err_file_to_dir($m->{file_b});
491 $dirname = dirname $dirname;
495 return (\%rmdir_check, \%added_check);
497 sub err_dir_to_file {
499 print STDERR "Node change from directory to file ",
500 "is not supported by Subversion: ",$file,"\n";
503 sub err_file_to_dir {
505 print STDERR "Node change from file to directory ",
506 "is not supported by Subversion: ",$file,"\n";
511 sub svn_checkout_tree {
512 my ($svn_rev, $treeish) = @_;
513 my $from = file_to_s("$REV_DIR/$svn_rev");
514 assert_svn_wc_clean($svn_rev,$from);
515 print "diff-tree '$from' '$treeish'\n";
516 my $pid = open my $diff_fh, '-|';
517 defined $pid or croak $!;
519 my @diff_tree = qw(git-diff-tree -z -r -C);
520 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
521 push @diff_tree, "-l$_l" if defined $_l;
522 exec(@diff_tree, $from, $treeish) or croak $!;
524 my $mods = parse_diff_tree($diff_fh);
526 # git can do empty commits, SVN doesn't allow it...
529 my ($rm, $add) = precommit_check($mods);
531 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
532 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
533 if ($m->{chg} eq 'C') {
534 svn_ensure_parent_path( $m->{file_b} );
535 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
536 apply_mod_line_blob($m);
537 svn_check_prop_executable($m);
538 } elsif ($m->{chg} eq 'D') {
539 sys(qw(svn rm --force), $m->{file_b});
540 } elsif ($m->{chg} eq 'R') {
541 svn_ensure_parent_path( $m->{file_b} );
542 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
543 apply_mod_line_blob($m);
544 svn_check_prop_executable($m);
545 } elsif ($m->{chg} eq 'M') {
546 apply_mod_line_blob($m);
547 svn_check_prop_executable($m);
548 } elsif ($m->{chg} eq 'T') {
549 sys(qw(svn rm --force),$m->{file_b});
550 apply_mod_line_blob($m);
551 sys(qw(svn add --force), $m->{file_b});
552 svn_check_prop_executable($m);
553 } elsif ($m->{chg} eq 'A') {
554 svn_ensure_parent_path( $m->{file_b} );
555 apply_mod_line_blob($m);
556 sys(qw(svn add --force), $m->{file_b});
557 svn_check_prop_executable($m);
559 croak "Invalid chg: $m->{chg}\n";
563 assert_tree($treeish);
564 if ($_rmdir) { # remove empty directories
565 handle_rmdir($rm, $add);
567 assert_tree($treeish);
571 # svn ls doesn't work with respect to the current working tree, but what's
572 # in the repository. There's not even an option for it... *sigh*
573 # (added files don't show up and removed files remain in the ls listing)
575 my ($dir, $rm, $add) = @_;
576 chomp(my @ls = safe_qx('svn','ls',$dir));
579 s#/$##; # trailing slashes are evil
580 push @ret, $_ unless $rm->{$dir}->{$_};
582 if (exists $add->{$dir}) {
583 push @ret, keys %{$add->{$dir}};
591 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
592 my $ls = svn_ls_current($dir, $rm, $add);
593 next if (scalar @$ls);
594 sys(qw(svn rm --force),$dir);
596 my $dn = dirname $dir;
597 $rm->{ $dn }->{ basename $dir } = 1;
598 $ls = svn_ls_current($dn, $rm, $add);
599 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
600 sys(qw(svn rm --force),$dn);
603 $rm->{ $dn }->{ $dir } = 1;
604 $ls = svn_ls_current($dn, $rm, $add);
609 sub svn_commit_tree {
610 my ($svn_rev, $commit) = @_;
611 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
612 my %log_msg = ( msg => '' );
613 open my $msg, '>', $commit_msg or croak $!;
615 chomp(my $type = `git-cat-file -t $commit`);
616 if ($type eq 'commit') {
617 my $pid = open my $msg_fh, '-|';
618 defined $pid or croak $!;
621 exec(qw(git-cat-file commit), $commit) or croak $!;
626 $in_msg = 1 if (/^\s*$/);
629 print $msg $_ or croak $!;
632 close $msg_fh or croak $!;
634 close $msg or croak $!;
636 if ($_edit || ($type eq 'tree')) {
637 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
638 system($editor, $commit_msg);
640 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
641 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
643 defined $committed or croak
644 "Commit output failed to parse committed revision!\n",
645 join("\n",@ci_output),"\n";
646 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
648 my @svn_up = qw(svn up);
649 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
650 if ($rev_committed == ($svn_rev + 1)) {
651 push @svn_up, "-r$rev_committed";
653 my $info = svn_info('.');
654 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
655 if ($info->{'Last Changed Rev'} != $rev_committed) {
656 croak "$info->{'Last Changed Rev'} != $rev_committed\n"
658 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
659 /(\d{4})\-(\d\d)\-(\d\d)\s
660 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
661 or croak "Failed to parse date: $date\n";
662 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
663 $log_msg{author} = $info->{'Last Changed Author'};
664 $log_msg{revision} = $rev_committed;
665 $log_msg{msg} .= "\n";
666 my $parent = file_to_s("$REV_DIR/$svn_rev");
667 git_commit(\%log_msg, $parent, $commit);
668 return $rev_committed;
671 push @svn_up, "-r$svn_rev";
673 return fetch("$rev_committed=$commit")->{revision};
678 my $pid = open my $log_fh,'-|';
679 defined $pid or croak $!;
682 exec (qw(svn log), @log_args) or croak $!
690 if ($state eq 'msg') {
691 if ($svn_log[$#svn_log]->{lines}) {
692 $svn_log[$#svn_log]->{msg} .= $_."\n";
693 unless(--$svn_log[$#svn_log]->{lines}) {
697 croak "Log parse error at: $_\n",
698 $svn_log[$#svn_log]->{revision},
703 if ($state ne 'sep') {
704 croak "Log parse error at: $_\n",
706 $svn_log[$#svn_log]->{revision},
711 # if we have an empty log message, put something there:
713 $svn_log[$#svn_log]->{msg} ||= "\n";
714 delete $svn_log[$#svn_log]->{lines};
718 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
720 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
721 ($lines) = ($lines =~ /(\d+)/);
722 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
723 /(\d{4})\-(\d\d)\-(\d\d)\s
724 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
725 or croak "Failed to parse date: $date\n";
726 my %log_msg = ( revision => $rev,
727 date => "$tz $Y-$m-$d $H:$M:$S",
731 push @svn_log, \%log_msg;
732 $state = 'msg_start';
735 # skip the first blank line of the message:
736 if ($state eq 'msg_start' && /^$/) {
738 } elsif ($state eq 'msg') {
739 if ($svn_log[$#svn_log]->{lines}) {
740 $svn_log[$#svn_log]->{msg} .= $_."\n";
741 unless (--$svn_log[$#svn_log]->{lines}) {
745 croak "Log parse error at: $_\n",
746 $svn_log[$#svn_log]->{revision},"\n";
750 close $log_fh or croak $?;
755 my $url = shift || $SVN_URL;
757 my $pid = open my $info_fh, '-|';
758 defined $pid or croak $!;
761 exec(qw(svn info),$url) or croak $!;
765 # only single-lines seem to exist in svn info output
768 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
770 push @{$ret->{-order}}, $1;
773 close $info_fh or croak $!;
777 sub sys { system(@_) == 0 or croak $? }
780 system( "git-diff-files --name-only -z ".
781 " | git-update-index --remove -z --stdin && ".
782 "git-ls-files -z --others ".
783 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
784 " | git-update-index --add -z --stdin"
789 my ($str, $file, $mode) = @_;
790 open my $fd,'>',$file or croak $!;
791 print $fd $str,"\n" or croak $!;
792 close $fd or croak $!;
793 chmod ($mode &~ umask, $file) if (defined $mode);
798 open my $fd,'<',$file or croak "$!: file: $file\n";
801 close $fd or croak $!;
806 sub assert_revision_unknown {
808 if (-f "$REV_DIR/$revno") {
809 croak "$REV_DIR/$revno already exists! ",
810 "Why are we refetching it?";
814 sub assert_revision_eq_or_unknown {
815 my ($revno, $commit) = @_;
816 if (-f "$REV_DIR/$revno") {
817 my $current = file_to_s("$REV_DIR/$revno");
818 if ($commit ne $current) {
819 croak "$REV_DIR/$revno already exists!\n",
820 "current: $current\nexpected: $commit\n";
827 my ($log_msg, @parents) = @_;
828 assert_revision_unknown($log_msg->{revision});
829 my $out_fh = IO::File->new_tmpfile or croak $!;
830 my $info = svn_info('.');
831 my $uuid = $info->{'Repository UUID'};
832 defined $uuid or croak "Unable to get Repository UUID\n";
834 # commit parents can be conditionally bound to a particular
835 # svn revision via: "svn_revno=commit_sha1", filter them out here:
837 foreach my $p (@parents) {
838 next unless defined $p;
839 if ($p =~ /^(\d+)=($sha1_short)$/o) {
840 if ($1 == $log_msg->{revision}) {
841 push @exec_parents, $2;
844 push @exec_parents, $p if $p =~ /$sha1_short/o;
849 defined $pid or croak $!;
851 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
853 chomp(my $tree = `git-write-tree`);
855 my $msg_fh = IO::File->new_tmpfile or croak $!;
856 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
857 "$SVN_URL\@$log_msg->{revision}",
858 " $uuid\n" or croak $!;
859 $msg_fh->flush == 0 or croak $!;
860 seek $msg_fh, 0, 0 or croak $!;
862 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
864 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
865 $log_msg->{author}."\@$uuid";
866 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
868 my @exec = ('git-commit-tree',$tree);
869 push @exec, '-p', $_ foreach @exec_parents;
870 open STDIN, '<&', $msg_fh or croak $!;
871 open STDOUT, '>&', $out_fh or croak $!;
872 exec @exec or croak $!;
877 $out_fh->flush == 0 or croak $!;
878 seek $out_fh, 0, 0 or croak $!;
879 chomp(my $commit = do { local $/; <$out_fh> });
880 if ($commit !~ /^$sha1$/o) {
881 croak "Failed to commit, invalid sha1: $commit\n";
883 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
884 if (my $primary_parent = shift @exec_parents) {
885 push @update_ref, $primary_parent;
888 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
889 print "r$log_msg->{revision} = $commit\n";
893 sub apply_mod_line_blob {
895 if ($m->{mode_b} =~ /^120/) {
896 blob_to_symlink($m->{sha1_b}, $m->{file_b});
898 blob_to_file($m->{sha1_b}, $m->{file_b});
902 sub blob_to_symlink {
903 my ($blob, $link) = @_;
904 defined $link or croak "\$link not defined!\n";
905 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
906 if (-l $link || -f _) {
907 unlink $link or croak $!;
910 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
911 symlink $dest, $link or croak $!;
915 my ($blob, $file) = @_;
916 defined $file or croak "\$file not defined!\n";
917 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
918 if (-l $file || -f _) {
919 unlink $file or croak $!;
922 open my $blob_fh, '>', $file or croak "$!: $file\n";
924 defined $pid or croak $!;
927 open STDOUT, '>&', $blob_fh or croak $!;
928 exec('git-cat-file','blob',$blob);
933 close $blob_fh or croak $!;
937 my $pid = open my $child, '-|';
938 defined $pid or croak $!;
940 exec(@_) or croak $?;
942 my @ret = (<$child>);
943 close $child or croak $?;
944 die $? if $?; # just in case close didn't error out
945 return wantarray ? @ret : join('',@ret);
948 sub svn_check_ignore_externals {
949 return if $_no_ignore_ext;
950 unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
951 print STDERR "W: Installed svn version does not support ",
952 "--ignore-externals\n";
957 sub check_upgrade_needed {
959 my $pid = open my $child, '-|';
960 defined $pid or croak $!;
963 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
965 my @ret = (<$child>);
966 close $child or croak $?;
967 die $? if $?; # just in case close didn't error out
968 return wantarray ? @ret : join('',@ret);
971 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
973 print STDERR "Please run: $0 rebuild --upgrade\n";
982 @svn_log = array of log_msg hashes
986 msg => 'whitespace-formatted log entry
987 ', # trailing newline is preserved
988 revision => '8', # integer
989 date => '2004-02-24T17:01:44.108345Z', # commit date
990 author => 'committer name'
994 @mods = array of diff-index line hashes, each element represents one line
997 diff-index line ($m hash)
999 mode_a => first column of diff-index output, no leading ':',
1000 mode_b => second column of diff-index output,
1001 sha1_b => sha1sum of the final blob,
1002 chg => change type [MCRAD],
1003 file_a => original file name of a file (iff chg is 'C' or 'R')
1004 file_b => new/current file name of a file (any chg)