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]{4,40}/;
36 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
37 $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
38 my (@_branch_from, %tree_map, %users);
40 GetOptions( 'revision|r=s' => \$_revision,
41 'no-ignore-externals' => \$_no_ignore_ext,
45 'upgrade' => \$_upgrade,
46 'help|H|h' => \$_help,
47 'branch|b=s' => \@_branch_from,
48 'find-copies-harder' => \$_find_copies_harder,
49 'authors-file|authors|A=s' => \$_authors,
51 'version|V' => \$_version,
52 'no-stop-on-copy' => \$_no_stop_copy );
54 fetch => [ \&fetch, "Download new revisions from SVN" ],
55 init => [ \&init, "Initialize and fetch (import)"],
56 commit => [ \&commit, "Commit git revisions to SVN" ],
57 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings" ],
58 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
59 help => [ \&usage, "Show help" ],
62 for (my $i = 0; $i < @ARGV; $i++) {
63 if (defined $cmd{$ARGV[$i]}) {
70 # we may be called as git-svn-(command), or git-svn(command).
72 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
78 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
80 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
83 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
84 my ($user, $name, $email) = ($1, $2, $3);
85 $users{$user} = [$name, $email];
87 close $authors or croak $!;
91 version() if $_version;
92 usage(1) unless (defined $cmd);
93 svn_check_ignore_externals();
94 $cmd{$cmd}->[0]->(@ARGV);
97 ####################### primary functions ######################
99 my $exit = shift || 0;
100 my $fd = $exit ? \*STDERR : \*STDOUT;
102 git-svn - bidirectional operations between a single Subversion tree and git
103 Usage: $0 <command> [options] [arguments]\n
106 foreach (sort keys %cmd) {
107 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
110 \nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
111 you're tracking multiple SVN branches/repositories in one git repository
112 and want to keep them separate. See git-svn(1) for more information.
118 print "git-svn version $VERSION\n";
123 $SVN_URL = shift or undef;
127 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
129 check_upgrade_needed();
132 my $pid = open(my $rev_list,'-|');
133 defined $pid or croak $!;
135 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
138 while (<$rev_list>) {
141 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
142 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
143 next if (!@commit); # skip merges
144 my $id = $commit[$#commit];
145 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
147 if (!$rev || !$uuid || !$url) {
148 # some of the original repositories I made had
149 # indentifiers like this:
150 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
152 if (!$rev || !$uuid) {
153 croak "Unable to extract revision or UUID from ",
158 # if we merged or otherwise started elsewhere, this is
159 # how we break out of it
160 next if (defined $repo_uuid && ($uuid ne $repo_uuid));
161 next if (defined $SVN_URL && ($url ne $SVN_URL));
163 print "r$rev = $c\n";
164 unless (defined $latest) {
165 if (!$SVN_URL && !$url) {
166 croak "SVN repository location required: $url\n";
169 $repo_uuid ||= setup_git_svn();
172 assert_revision_eq_or_unknown($rev, $c);
173 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
174 $newest_rev = $rev if ($rev > $newest_rev);
176 close $rev_list or croak $?;
177 if (!chdir $SVN_WC) {
178 my @svn_co = ('svn','co',"-r$latest");
179 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
180 sys(@svn_co, $SVN_URL, $SVN_WC);
181 chdir $SVN_WC or croak $!;
185 defined $pid or croak $!;
187 my @svn_up = qw(svn up);
188 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
189 sys(@svn_up,"-r$newest_rev");
190 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
192 exec('git-write-tree');
198 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
199 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
205 $SVN_URL = shift or croak "SVN repository location required\n";
206 unless (-d $GIT_DIR) {
214 check_upgrade_needed();
215 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
216 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
217 unless ($_revision) {
218 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
220 push @log_args, "-r$_revision";
221 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
223 my $svn_log = svn_log_raw(@log_args);
224 @$svn_log = sort { $a->{revision} <=> $b->{revision} } @$svn_log;
226 my $base = shift @$svn_log or croak "No base revision!\n";
227 my $last_commit = undef;
228 unless (-d $SVN_WC) {
229 my @svn_co = ('svn','co',"-r$base->{revision}");
230 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
231 sys(@svn_co, $SVN_URL, $SVN_WC);
232 chdir $SVN_WC or croak $!;
233 $last_commit = git_commit($base, @parents);
234 unless (-f "$GIT_DIR/refs/heads/master") {
235 sys(qw(git-update-ref refs/heads/master),$last_commit);
237 assert_svn_wc_clean($base->{revision}, $last_commit);
239 chdir $SVN_WC or croak $!;
240 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
242 my @svn_up = qw(svn up);
243 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
244 my $last_rev = $base->{revision};
245 foreach my $log_msg (@$svn_log) {
246 assert_svn_wc_clean($last_rev, $last_commit);
247 $last_rev = $log_msg->{revision};
248 sys(@svn_up,"-r$last_rev");
249 $last_commit = git_commit($log_msg, $last_commit, @parents);
251 assert_svn_wc_clean($last_rev, $last_commit);
252 return pop @$svn_log;
257 check_upgrade_needed();
258 if ($_stdin || !@commits) {
259 print "Reading from stdin...\n";
262 if (/\b($sha1_short)\b/) {
263 unshift @commits, $1;
268 foreach my $c (@commits) {
269 chomp(my @tmp = safe_qx('git-rev-parse',$c));
270 if (scalar @tmp == 1) {
272 } elsif (scalar @tmp > 1) {
273 push @revs, reverse (safe_qx('git-rev-list',@tmp));
275 die "Failed to rev-parse $c\n";
281 chdir $SVN_WC or croak $!;
282 my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
283 foreach my $c (@revs) {
284 my $mods = svn_checkout_tree($svn_current_rev, $c);
285 if (scalar @$mods == 0) {
286 print "Skipping, no changes detected\n";
289 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
291 print "Done committing ",scalar @revs," revisions to SVN\n";
296 require File::Find or die $!;
297 my $exclude_file = "$GIT_DIR/info/exclude";
298 open my $fh, '<', $exclude_file or croak $!;
299 chomp(my @excludes = (<$fh>));
300 close $fh or croak $!;
302 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
303 chdir $SVN_WC or croak $!;
305 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
307 @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
308 }}, no_chdir=>1},'.');
311 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
313 foreach my $i (sort keys %ign) {
314 print "\n# ",$i,"\n";
315 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
319 ########################### utility functions #########################
322 defined $SVN_URL or croak "SVN repository location required\n";
323 unless (-d $GIT_DIR) {
324 croak "GIT_DIR=$GIT_DIR does not exist!\n";
326 mkpath(["$GIT_DIR/$GIT_SVN"]);
327 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
329 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
330 my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
331 croak "Repository UUID unreadable\n";
332 s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
334 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
335 print $fd '.svn',"\n";
336 close $fd or croak $!;
340 sub assert_svn_wc_clean {
341 my ($svn_rev, $treeish) = @_;
342 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
343 croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
344 my $svn_info = svn_info('.');
345 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
346 croak "Expected r$svn_rev, got r",
347 $svn_info->{'Last Changed Rev'},"\n";
349 my @status = grep(!/^Performing status on external/,(`svn status`));
350 @status = grep(!/^\s*$/,@status);
351 if (scalar @status) {
352 print STDERR "Tree ($SVN_WC) is not clean:\n";
353 print STDERR $_ foreach @status;
356 assert_tree($treeish);
361 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
362 chomp(my $type = `git-cat-file -t $treeish`);
364 while ($type eq 'tag') {
365 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
367 if ($type eq 'commit') {
368 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
369 ($expected) = ($expected =~ /^tree ($sha1)$/);
370 die "Unable to get tree from $treeish\n" unless $expected;
371 } elsif ($type eq 'tree') {
372 $expected = $treeish;
374 die "$treeish is a $type, expected tree, tag or commit\n";
377 my $old_index = $ENV{GIT_INDEX_FILE};
378 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
380 unlink $tmpindex or croak $!;
382 $ENV{GIT_INDEX_FILE} = $tmpindex;
384 chomp(my $tree = `git-write-tree`);
386 $ENV{GIT_INDEX_FILE} = $old_index;
388 delete $ENV{GIT_INDEX_FILE};
390 if ($tree ne $expected) {
391 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
395 sub parse_diff_tree {
401 chomp $_; # this gets rid of the trailing "\0"
402 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
403 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
404 push @mods, { mode_a => $1, mode_b => $2,
405 sha1_b => $3, chg => $4 };
406 if ($4 =~ /^(?:C|R)$/) {
411 } elsif ($state eq 'file_a') {
412 my $x = $mods[$#mods] or croak "Empty array\n";
413 if ($x->{chg} !~ /^(?:C|R)$/) {
414 croak "Error parsing $_, $x->{chg}\n";
418 } elsif ($state eq 'file_b') {
419 my $x = $mods[$#mods] or croak "Empty array\n";
420 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
421 croak "Error parsing $_, $x->{chg}\n";
423 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
424 croak "Error parsing $_, $x->{chg}\n";
429 croak "Error parsing $_\n";
432 close $diff_fh or croak $!;
437 sub svn_check_prop_executable {
439 return if -l $m->{file_b};
440 if ($m->{mode_b} =~ /755$/) {
441 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
442 if ($m->{mode_a} !~ /755$/) {
443 sys(qw(svn propset svn:executable 1), $m->{file_b});
445 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
446 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
447 sys(qw(svn propdel svn:executable), $m->{file_b});
448 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
449 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
453 sub svn_ensure_parent_path {
454 my $dir_b = dirname(shift);
455 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
456 mkpath([$dir_b]) unless (-d $dir_b);
457 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
460 sub precommit_check {
462 my (%rm_file, %rmdir_check, %added_check);
464 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
465 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
466 if ($m->{chg} eq 'R') {
467 if (-d $m->{file_b}) {
468 err_dir_to_file("$m->{file_a} => $m->{file_b}");
470 # dir/$file => dir/file/$file
471 my $dirname = dirname($m->{file_b});
472 while ($dirname ne File::Spec->curdir) {
473 if ($dirname ne $m->{file_a}) {
474 $dirname = dirname($dirname);
477 err_file_to_dir("$m->{file_a} => $m->{file_b}");
479 # baz/zzz => baz (baz is a file)
480 $dirname = dirname($m->{file_a});
481 while ($dirname ne File::Spec->curdir) {
482 if ($dirname ne $m->{file_b}) {
483 $dirname = dirname($dirname);
486 err_dir_to_file("$m->{file_a} => $m->{file_b}");
489 if ($m->{chg} =~ /^(D|R)$/) {
490 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
491 $rm_file{ $m->{$t} } = 1;
492 my $dirname = dirname( $m->{$t} );
493 my $basename = basename( $m->{$t} );
494 $rmdir_check{$dirname}->{$basename} = 1;
495 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
496 if (-d $m->{file_b}) {
497 err_dir_to_file($m->{file_b});
499 my $dirname = dirname( $m->{file_b} );
500 my $basename = basename( $m->{file_b} );
501 $added_check{$dirname}->{$basename} = 1;
502 while ($dirname ne File::Spec->curdir) {
503 if ($rm_file{$dirname}) {
504 err_file_to_dir($m->{file_b});
506 $dirname = dirname $dirname;
510 return (\%rmdir_check, \%added_check);
512 sub err_dir_to_file {
514 print STDERR "Node change from directory to file ",
515 "is not supported by Subversion: ",$file,"\n";
518 sub err_file_to_dir {
520 print STDERR "Node change from file to directory ",
521 "is not supported by Subversion: ",$file,"\n";
526 sub svn_checkout_tree {
527 my ($svn_rev, $treeish) = @_;
528 my $from = file_to_s("$REV_DIR/$svn_rev");
529 assert_svn_wc_clean($svn_rev,$from);
530 print "diff-tree $from $treeish\n";
531 my $pid = open my $diff_fh, '-|';
532 defined $pid or croak $!;
534 my @diff_tree = qw(git-diff-tree -z -r -C);
535 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
536 push @diff_tree, "-l$_l" if defined $_l;
537 exec(@diff_tree, $from, $treeish) or croak $!;
539 my $mods = parse_diff_tree($diff_fh);
541 # git can do empty commits, but SVN doesn't allow it...
544 my ($rm, $add) = precommit_check($mods);
546 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
547 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
548 if ($m->{chg} eq 'C') {
549 svn_ensure_parent_path( $m->{file_b} );
550 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
551 apply_mod_line_blob($m);
552 svn_check_prop_executable($m);
553 } elsif ($m->{chg} eq 'D') {
554 sys(qw(svn rm --force), $m->{file_b});
555 } elsif ($m->{chg} eq 'R') {
556 svn_ensure_parent_path( $m->{file_b} );
557 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
558 apply_mod_line_blob($m);
559 svn_check_prop_executable($m);
560 } elsif ($m->{chg} eq 'M') {
561 apply_mod_line_blob($m);
562 svn_check_prop_executable($m);
563 } elsif ($m->{chg} eq 'T') {
564 sys(qw(svn rm --force),$m->{file_b});
565 apply_mod_line_blob($m);
566 sys(qw(svn add --force), $m->{file_b});
567 svn_check_prop_executable($m);
568 } elsif ($m->{chg} eq 'A') {
569 svn_ensure_parent_path( $m->{file_b} );
570 apply_mod_line_blob($m);
571 sys(qw(svn add --force), $m->{file_b});
572 svn_check_prop_executable($m);
574 croak "Invalid chg: $m->{chg}\n";
578 assert_tree($treeish);
579 if ($_rmdir) { # remove empty directories
580 handle_rmdir($rm, $add);
582 assert_tree($treeish);
586 # svn ls doesn't work with respect to the current working tree, but what's
587 # in the repository. There's not even an option for it... *sigh*
588 # (added files don't show up and removed files remain in the ls listing)
590 my ($dir, $rm, $add) = @_;
591 chomp(my @ls = safe_qx('svn','ls',$dir));
594 s#/$##; # trailing slashes are evil
595 push @ret, $_ unless $rm->{$dir}->{$_};
597 if (exists $add->{$dir}) {
598 push @ret, keys %{$add->{$dir}};
606 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
607 my $ls = svn_ls_current($dir, $rm, $add);
608 next if (scalar @$ls);
609 sys(qw(svn rm --force),$dir);
611 my $dn = dirname $dir;
612 $rm->{ $dn }->{ basename $dir } = 1;
613 $ls = svn_ls_current($dn, $rm, $add);
614 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
615 sys(qw(svn rm --force),$dn);
618 $rm->{ $dn }->{ $dir } = 1;
619 $ls = svn_ls_current($dn, $rm, $add);
624 sub svn_commit_tree {
625 my ($svn_rev, $commit) = @_;
626 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
627 my %log_msg = ( msg => '' );
628 open my $msg, '>', $commit_msg or croak $!;
630 chomp(my $type = `git-cat-file -t $commit`);
631 if ($type eq 'commit') {
632 my $pid = open my $msg_fh, '-|';
633 defined $pid or croak $!;
636 exec(qw(git-cat-file commit), $commit) or croak $!;
641 $in_msg = 1 if (/^\s*$/);
642 } elsif (/^git-svn-id: /) {
643 # skip this, we regenerate the correct one
644 # on re-fetch anyways
646 print $msg $_ or croak $!;
649 close $msg_fh or croak $!;
651 close $msg or croak $!;
653 if ($_edit || ($type eq 'tree')) {
654 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
655 system($editor, $commit_msg);
658 # file_to_s removes all trailing newlines, so just use chomp() here:
659 open $msg, '<', $commit_msg or croak $!;
660 { local $/; chomp($log_msg{msg} = <$msg>); }
661 close $msg or croak $!;
663 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
664 print "Committing $commit: $oneline\n";
666 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
667 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
669 defined $committed or croak
670 "Commit output failed to parse committed revision!\n",
671 join("\n",@ci_output),"\n";
672 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
674 my @svn_up = qw(svn up);
675 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
676 if ($rev_committed == ($svn_rev + 1)) {
677 push @svn_up, "-r$rev_committed";
679 my $info = svn_info('.');
680 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
681 if ($info->{'Last Changed Rev'} != $rev_committed) {
682 croak "$info->{'Last Changed Rev'} != $rev_committed\n"
684 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
685 /(\d{4})\-(\d\d)\-(\d\d)\s
686 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
687 or croak "Failed to parse date: $date\n";
688 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
689 $log_msg{author} = $info->{'Last Changed Author'};
690 $log_msg{revision} = $rev_committed;
691 $log_msg{msg} .= "\n";
692 my $parent = file_to_s("$REV_DIR/$svn_rev");
693 git_commit(\%log_msg, $parent, $commit);
694 return $rev_committed;
697 push @svn_up, "-r$svn_rev";
699 return fetch("$rev_committed=$commit")->{revision};
704 my $pid = open my $log_fh,'-|';
705 defined $pid or croak $!;
708 exec (qw(svn log), @log_args) or croak $!
716 if ($state eq 'msg') {
717 if ($svn_log[$#svn_log]->{lines}) {
718 $svn_log[$#svn_log]->{msg} .= $_."\n";
719 unless(--$svn_log[$#svn_log]->{lines}) {
723 croak "Log parse error at: $_\n",
724 $svn_log[$#svn_log]->{revision},
729 if ($state ne 'sep') {
730 croak "Log parse error at: $_\n",
732 $svn_log[$#svn_log]->{revision},
737 # if we have an empty log message, put something there:
739 $svn_log[$#svn_log]->{msg} ||= "\n";
740 delete $svn_log[$#svn_log]->{lines};
744 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
746 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
747 ($lines) = ($lines =~ /(\d+)/);
748 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
749 /(\d{4})\-(\d\d)\-(\d\d)\s
750 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
751 or croak "Failed to parse date: $date\n";
752 my %log_msg = ( revision => $rev,
753 date => "$tz $Y-$m-$d $H:$M:$S",
757 if (defined $_authors && ! defined $users{$author}) {
758 die "Author: $author not defined in ",
761 push @svn_log, \%log_msg;
762 $state = 'msg_start';
765 # skip the first blank line of the message:
766 if ($state eq 'msg_start' && /^$/) {
768 } elsif ($state eq 'msg') {
769 if ($svn_log[$#svn_log]->{lines}) {
770 $svn_log[$#svn_log]->{msg} .= $_."\n";
771 unless (--$svn_log[$#svn_log]->{lines}) {
775 croak "Log parse error at: $_\n",
776 $svn_log[$#svn_log]->{revision},"\n";
780 close $log_fh or croak $?;
785 my $url = shift || $SVN_URL;
787 my $pid = open my $info_fh, '-|';
788 defined $pid or croak $!;
791 exec(qw(svn info),$url) or croak $!;
795 # only single-lines seem to exist in svn info output
798 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
800 push @{$ret->{-order}}, $1;
803 close $info_fh or croak $!;
807 sub sys { system(@_) == 0 or croak $? }
810 system( "git-diff-files --name-only -z ".
811 " | git-update-index --remove -z --stdin && ".
812 "git-ls-files -z --others ".
813 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
814 " | git-update-index --add -z --stdin"
819 my ($str, $file, $mode) = @_;
820 open my $fd,'>',$file or croak $!;
821 print $fd $str,"\n" or croak $!;
822 close $fd or croak $!;
823 chmod ($mode &~ umask, $file) if (defined $mode);
828 open my $fd,'<',$file or croak "$!: file: $file\n";
831 close $fd or croak $!;
836 sub assert_revision_unknown {
838 if (-f "$REV_DIR/$revno") {
839 croak "$REV_DIR/$revno already exists! ",
840 "Why are we refetching it?";
844 sub assert_revision_eq_or_unknown {
845 my ($revno, $commit) = @_;
846 if (-f "$REV_DIR/$revno") {
847 my $current = file_to_s("$REV_DIR/$revno");
848 if ($commit ne $current) {
849 croak "$REV_DIR/$revno already exists!\n",
850 "current: $current\nexpected: $commit\n";
857 my ($log_msg, @parents) = @_;
858 assert_revision_unknown($log_msg->{revision});
859 my $out_fh = IO::File->new_tmpfile or croak $!;
860 my $info = svn_info('.');
861 my $uuid = $info->{'Repository UUID'};
862 defined $uuid or croak "Unable to get Repository UUID\n";
864 map_tree_joins() if (@_branch_from && !%tree_map);
866 # commit parents can be conditionally bound to a particular
867 # svn revision via: "svn_revno=commit_sha1", filter them out here:
869 foreach my $p (@parents) {
870 next unless defined $p;
871 if ($p =~ /^(\d+)=($sha1_short)$/o) {
872 if ($1 == $log_msg->{revision}) {
873 push @exec_parents, $2;
876 push @exec_parents, $p if $p =~ /$sha1_short/o;
881 defined $pid or croak $!;
883 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
885 chomp(my $tree = `git-write-tree`);
887 if (exists $tree_map{$tree}) {
888 my %seen_parent = map { $_ => 1 } @exec_parents;
889 foreach (@{$tree_map{$tree}}) {
890 # MAXPARENT is defined to 16 in commit-tree.c:
891 if ($seen_parent{$_} || @exec_parents > 16) {
894 push @exec_parents, $_;
895 $seen_parent{$_} = 1;
898 my $msg_fh = IO::File->new_tmpfile or croak $!;
899 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
900 "$SVN_URL\@$log_msg->{revision}",
901 " $uuid\n" or croak $!;
902 $msg_fh->flush == 0 or croak $!;
903 seek $msg_fh, 0, 0 or croak $!;
905 set_commit_env($log_msg, $uuid);
907 my @exec = ('git-commit-tree',$tree);
908 push @exec, '-p', $_ foreach @exec_parents;
909 open STDIN, '<&', $msg_fh or croak $!;
910 open STDOUT, '>&', $out_fh or croak $!;
911 exec @exec or croak $!;
916 $out_fh->flush == 0 or croak $!;
917 seek $out_fh, 0, 0 or croak $!;
918 chomp(my $commit = do { local $/; <$out_fh> });
919 if ($commit !~ /^$sha1$/o) {
920 croak "Failed to commit, invalid sha1: $commit\n";
922 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
923 if (my $primary_parent = shift @exec_parents) {
924 push @update_ref, $primary_parent;
927 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
928 print "r$log_msg->{revision} = $commit\n";
933 my ($log_msg, $uuid) = @_;
934 my $author = $log_msg->{author};
935 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
936 : ($author,"$author\@$uuid");
937 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
938 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
939 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
942 sub apply_mod_line_blob {
944 if ($m->{mode_b} =~ /^120/) {
945 blob_to_symlink($m->{sha1_b}, $m->{file_b});
947 blob_to_file($m->{sha1_b}, $m->{file_b});
951 sub blob_to_symlink {
952 my ($blob, $link) = @_;
953 defined $link or croak "\$link not defined!\n";
954 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
955 if (-l $link || -f _) {
956 unlink $link or croak $!;
959 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
960 symlink $dest, $link or croak $!;
964 my ($blob, $file) = @_;
965 defined $file or croak "\$file not defined!\n";
966 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
967 if (-l $file || -f _) {
968 unlink $file or croak $!;
971 open my $blob_fh, '>', $file or croak "$!: $file\n";
973 defined $pid or croak $!;
976 open STDOUT, '>&', $blob_fh or croak $!;
977 exec('git-cat-file','blob',$blob);
982 close $blob_fh or croak $!;
986 my $pid = open my $child, '-|';
987 defined $pid or croak $!;
989 exec(@_) or croak $?;
991 my @ret = (<$child>);
992 close $child or croak $?;
993 die $? if $?; # just in case close didn't error out
994 return wantarray ? @ret : join('',@ret);
997 sub svn_check_ignore_externals {
998 return if $_no_ignore_ext;
999 unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
1000 print STDERR "W: Installed svn version does not support ",
1001 "--ignore-externals\n";
1002 $_no_ignore_ext = 1;
1006 sub check_upgrade_needed {
1008 my $pid = open my $child, '-|';
1009 defined $pid or croak $!;
1012 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
1014 my @ret = (<$child>);
1015 close $child or croak $?;
1016 die $? if $?; # just in case close didn't error out
1017 return wantarray ? @ret : join('',@ret);
1020 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
1022 print STDERR "Please run: $0 rebuild --upgrade\n";
1027 # fills %tree_map with a reverse mapping of trees to commits. Useful
1028 # for finding parents to commit on.
1029 sub map_tree_joins {
1030 foreach my $br (@_branch_from) {
1031 my $pid = open my $pipe, '-|';
1032 defined $pid or croak $!;
1034 exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
1037 if (/^commit ($sha1)$/o) {
1039 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1040 unless (defined $tree) {
1041 die "Failed to parse commit $commit\n";
1043 push @{$tree_map{$tree}}, $commit;
1046 close $pipe or croak $?;
1054 @svn_log = array of log_msg hashes
1058 msg => 'whitespace-formatted log entry
1059 ', # trailing newline is preserved
1060 revision => '8', # integer
1061 date => '2004-02-24T17:01:44.108345Z', # commit date
1062 author => 'committer name'
1066 @mods = array of diff-index line hashes, each element represents one line
1067 of diff-index output
1069 diff-index line ($m hash)
1071 mode_a => first column of diff-index output, no leading ':',
1072 mode_b => second column of diff-index output,
1073 sha1_b => sha1sum of the final blob,
1074 chg => change type [MCRADT],
1075 file_a => original file name of a file (iff chg is 'C' or 'R')
1076 file_b => new/current file name of a file (any chg)