2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw/ $AUTHOR $VERSION
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
14 $GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15 $ENV{GIT_DIR} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL};
19 # make sure the svn binary gives consistent output between locales and TZs:
22 $| = 1; # unbuffer STDOUT
24 # properties that we do not log:
25 my %SKIP = ( 'svn:wc:ra_dav:version-url' => 1,
27 'svn:executable' => 1,
28 'svn:entry:committed-rev' => 1,
29 'svn:entry:last-author' => 1,
30 'svn:entry:uuid' => 1,
31 'svn:entry:committed-date' => 1,
34 sub fatal (@) { print STDERR @_; exit 1 }
35 require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
38 if ($SVN::Core::VERSION lt '1.1.0') {
39 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
41 push @Git::SVN::Ra::ISA, 'SVN::Ra';
42 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
43 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
46 use File::Basename qw/dirname basename/;
47 use File::Path qw/mkpath/;
48 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
49 use POSIX qw/strftime/;
53 memoize('revisions_eq');
54 memoize('cmt_metadata');
55 memoize('get_commit_time');
59 foreach (qw/command command_oneline command_noisy command_output_pipe
60 command_input_pipe command_close_pipe/) {
61 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
69 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
70 my $sha1 = qr/[a-f\d]{40}/;
71 my $sha1_short = qr/[a-f\d]{4,40}/;
72 my $_esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
73 my ($_revision,$_stdin,$_help,$_rmdir,$_edit,
74 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
75 $_repack, $_repack_nr, $_repack_flags, $_q,
76 $_message, $_file, $_no_metadata,
77 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
78 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
79 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
80 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive,
81 $_pager, $_color, $_prefix);
82 my (@_branch_from, %tree_map, %users, %rusers);
83 my @repo_path_split_cache;
84 use vars qw/$_follow_parent/;
86 my %fc_opts = ( 'branch|b=s' => \@_branch_from,
87 'follow-parent|follow' => \$_follow_parent,
88 'branch-all-refs|B' => \$_branch_all_refs,
89 'authors-file|A=s' => \$_authors,
90 'repack:i' => \$_repack,
91 'no-metadata' => \$_no_metadata,
93 'username=s' => \$Git::SVN::Prompt::_username,
94 'config-dir=s' => \$Git::SVN::Ra::config_dir,
95 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
96 'ignore-nodate' => \$_ignore_nodate,
97 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
99 my ($_trunk, $_tags, $_branches);
100 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
101 'tags|t=s' => \$_tags,
102 'branches|b=s' => \$_branches );
103 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
104 my %cmt_opts = ( 'edit|e' => \$_edit,
106 'find-copies-harder' => \$_find_copies_harder,
108 'copy-similarity|C=i'=> \$_cp_similarity
112 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
113 { 'revision|r=s' => \$_revision, %fc_opts } ],
114 init => [ \&init, "Initialize a repo for tracking" .
115 " (requires URL argument)",
117 dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
118 { 'merge|m|M' => \$_merge,
119 'strategy|s=s' => \$_strategy,
120 'dry-run|n' => \$_dry_run,
121 %cmt_opts, %fc_opts } ],
122 'set-tree' => [ \&commit, "Set an SVN repository to a git tree-ish",
123 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
124 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
125 { 'revision|r=i' => \$_revision } ],
126 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
127 { 'copy-remote|remote=s' => \$_cp_remote,
128 'upgrade' => \$_upgrade } ],
129 'graft-branches' => [ \&graft_branches,
130 'Detect merges/branches from already imported history',
131 { 'merge-rx|m' => \@_opt_m,
132 'branch|b=s' => \@_branch_from,
133 'branch-all-refs|B' => \$_branch_all_refs,
134 'no-default-regex' => \$_no_default_regex,
135 'no-graft-copy' => \$_no_graft_copy } ],
136 'multi-init' => [ \&multi_init,
137 'Initialize multiple trees (like git-svnimport)',
138 { %multi_opts, %init_opts,
139 'revision|r=i' => \$_revision,
140 'username=s' => \$Git::SVN::Prompt::_username,
141 'config-dir=s' => \$Git::SVN::Ra::config_dir,
142 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
143 'prefix=s' => \$_prefix,
145 'multi-fetch' => [ \&multi_fetch,
146 'Fetch multiple trees (like git-svnimport)',
148 'log' => [ \&show_log, 'Show commit logs',
149 { 'limit=i' => \$_limit,
150 'revision|r=s' => \$_revision,
151 'verbose|v' => \$_verbose,
152 'incremental' => \$_incremental,
153 'oneline' => \$_oneline,
154 'show-commit' => \$_show_commit,
155 'non-recursive' => \$_non_recursive,
156 'authors-file|A=s' => \$_authors,
158 'pager=s' => \$_pager,
160 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
161 { 'message|m=s' => \$_message,
162 'file|F=s' => \$_file,
163 'revision|r=s' => \$_revision,
168 for (my $i = 0; $i < @ARGV; $i++) {
169 if (defined $cmd{$ARGV[$i]}) {
176 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
178 read_repo_config(\%opts);
179 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
180 'version|V' => \$_version,
181 'id|i=s' => \$GIT_SVN);
182 exit 1 if (!$rv && $cmd ne 'log');
186 version() if $_version;
187 usage(1) unless defined $cmd;
189 load_authors() if $_authors;
190 load_all_refs() if $_branch_all_refs;
191 migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
192 $cmd{$cmd}->[0]->(@ARGV);
195 ####################### primary functions ######################
197 my $exit = shift || 0;
198 my $fd = $exit ? \*STDERR : \*STDOUT;
200 git-svn - bidirectional operations between a single Subversion tree and git
201 Usage: $0 <command> [options] [arguments]\n
203 print $fd "Available commands:\n" unless $cmd;
205 foreach (sort keys %cmd) {
206 next if $cmd && $cmd ne $_;
207 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
208 foreach (keys %{$cmd{$_}->[2]}) {
209 # prints out arguments as they should be passed:
210 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
211 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
213 split /\|/,$_)," $x\n";
217 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
218 arbitrary identifier if you're tracking multiple SVN branches/repositories in
219 one git repository and want to keep them separate. See git-svn(1) for more
226 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
231 if (!verify_ref("refs/remotes/$GIT_SVN^0")) {
234 $SVN_URL = shift or undef;
237 command_noisy('update-ref',"refs/remotes/$GIT_SVN","
240 check_upgrade_needed();
243 my ($rev_list, $ctx) = command_output_pipe("rev-list",
244 "refs/remotes/$GIT_SVN");
247 while (<$rev_list>) {
250 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
251 my @commit = grep(/^git-svn-id: /,
252 command(qw/cat-file commit/, $c));
253 next if (!@commit); # skip merges
254 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
255 if (!defined $rev || !$uuid) {
256 croak "Unable to extract revision or UUID from ",
257 "$c, $commit[$#commit]\n";
260 # if we merged or otherwise started elsewhere, this is
261 # how we break out of it
262 next if (defined $svn_uuid && ($uuid ne $svn_uuid));
263 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
265 unless (defined $latest) {
266 if (!$SVN_URL && !$url) {
267 croak "SVN repository location required: $url\n";
274 revdb_set($REVDB, $rev, $c);
275 print "r$rev = $c\n";
276 $newest_rev = $rev if ($rev > $newest_rev);
278 command_close_pipe($rev_list, $ctx);
282 my $url = shift or die "SVN repository location required " .
283 "as a command-line argument\n";
284 $url =~ s!/+$!!; # strip trailing slash
286 if (my $repo_path = shift) {
287 unless (-d $repo_path) {
288 mkpath([$repo_path]);
290 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
295 unless (-d $GIT_DIR) {
296 my @init_db = ('init');
297 push @init_db, "--template=$_template" if defined $_template;
298 push @init_db, "--shared" if defined $_shared;
299 command_noisy(@init_db);
305 fetch_child_id($GIT_SVN, @_);
309 check_upgrade_needed();
310 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
311 my $ret = fetch_lib(@_);
312 if ($ret->{commit} && !verify_ref('refs/heads/master^0')) {
313 command_noisy(qw(update-ref refs/heads/master),$ret->{commit});
320 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
321 $SVN ||= Git::SVN::Ra->new($SVN_URL);
322 my ($last_rev, $last_commit) = svn_grab_base_rev();
323 my ($base, $head) = libsvn_parse_revision($last_rev);
325 return { revision => $last_rev, commit => $last_commit }
327 my $index = set_index($GIT_SVN_INDEX);
329 # limit ourselves and also fork() since get_log won't release memory
330 # after processing a revision and SVN stuff seems to leak
332 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
333 if (defined $last_commit) {
334 unless (-e $GIT_SVN_INDEX) {
335 command_noisy('read-tree', $last_commit);
337 my $x = command_oneline('write-tree');
338 my ($y) = (command(qw/cat-file commit/, $last_commit)
339 =~ /^tree ($sha1)/m);
341 unlink $GIT_SVN_INDEX or croak $!;
342 command_noisy('read-tree', $last_commit);
344 $x = command_oneline('write-tree');
346 print STDERR "trees ($last_commit) $y != $x\n",
347 "Something is seriously wrong...\n";
351 # fork, because using SVN::Pool with get_log() still doesn't
352 # seem to help enough to keep memory usage down.
353 defined(my $pid = fork) or croak $!;
355 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
357 # Yes I'm perfectly aware that the fourth argument
358 # below is the limit revisions number. Unfortunately
359 # performance sucks with it enabled, so it's much
360 # faster to fetch revision ranges instead of relying
362 $SVN->dup->get_log([''], $min, $max, 0, 1, 1,
366 $log_msg = libsvn_fetch(
368 $last_commit = git_commit(
373 $log_msg = libsvn_new_tree(@_);
374 $last_commit = git_commit(
382 ($last_rev, $last_commit) = svn_grab_base_rev();
383 last if ($max >= $head);
386 $max = $head if ($max > $head);
387 $SVN = Git::SVN::Ra->new($SVN_URL);
389 restore_index($index);
390 return { revision => $last_rev, commit => $last_commit };
395 check_upgrade_needed();
396 if ($_stdin || !@commits) {
397 print "Reading from stdin...\n";
400 if (/\b($sha1_short)\b/o) {
401 unshift @commits, $1;
406 foreach my $c (@commits) {
407 my @tmp = command('rev-parse',$c);
408 if (scalar @tmp == 1) {
410 } elsif (scalar @tmp > 1) {
411 push @revs, reverse(command('rev-list',@tmp));
413 die "Failed to rev-parse $c\n";
417 print "Done committing ",scalar @revs," revisions to SVN\n";
422 my ($r_last, $cmt_last) = svn_grab_base_rev();
423 defined $r_last or die "Must have an existing revision to commit\n";
424 my $fetched = fetch();
425 if ($r_last != $fetched->{revision}) {
426 print STDERR "There are new revisions that were fetched ",
427 "and need to be merged (or acknowledged) ",
428 "before committing.\n",
429 "last rev: $r_last\n",
430 " current: $fetched->{revision}\n";
433 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
436 set_svn_commit_env();
437 foreach my $c (@revs) {
438 my $log_msg = get_commit_message($c, $commit_msg);
440 # fork for each commit because there's a memory leak I
441 # can't track down... (it's probably in the SVN code)
442 defined(my $pid = open my $fh, '-|') or croak $!;
444 my $pool = SVN::Pool->new;
445 my $ed = SVN::Git::Editor->new(
449 svn_path => $SVN->{svn_path},
451 $SVN->get_commit_editor(
461 my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
463 print "No changes\nr$r_last = $cmt_last\n";
471 my ($r_new, $cmt_new, $no);
475 if (/^r(\d+) = ($sha1)$/o) {
476 ($r_new, $cmt_new) = ($1, $2);
477 } elsif ($_ eq 'No changes') {
482 if (! defined $r_new && ! defined $cmt_new) {
484 die "Failed to parse revision information\n";
487 ($r_last, $cmt_last) = ($r_new, $cmt_new);
495 my $head = shift || 'HEAD';
496 my $gs = "refs/remotes/$GIT_SVN";
497 my @refs = command(qw/rev-list --no-merges/, "$gs..$head");
499 foreach my $d (reverse @refs) {
500 if (!verify_ref("$d~1")) {
502 "has no parent commit, and therefore ",
503 "nothing to diff against.\n",
504 "You should be working from a repository ",
505 "originally created by git-svn\n";
507 unless (defined $last_rev) {
508 (undef, $last_rev, undef) = cmt_metadata("$d~1");
509 unless (defined $last_rev) {
510 die "Unable to extract revision information ",
511 "from commit $d~1\n";
515 print "diff-tree $d~1 $d\n";
517 if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
519 } # else: no changes, same $last_rev
524 my @diff = command('diff-tree', 'HEAD', $gs, '--');
527 @finish = qw/rebase/;
528 push @finish, qw/--merge/ if $_merge;
529 push @finish, "--strategy=$_strategy" if $_strategy;
530 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
532 print "No changes between current HEAD and $gs\n",
533 "Resetting to the latest $gs\n";
534 @finish = qw/reset --mixed/;
536 command_noisy(@finish, $gs);
540 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
542 $SVN ||= Git::SVN::Ra->new($SVN_URL);
543 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
544 libsvn_traverse_ignore(\*STDOUT, '', $r);
548 my $gr_file = "$GIT_DIR/info/grafts";
549 my ($grafts, $comments) = read_grafts($gr_file);
553 # temporarily disable our grafts file to make this idempotent
554 chomp($gr_sha1 = command(qw/hash-object -w/,$gr_file));
555 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
558 my $l_map = read_url_paths();
559 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
560 unless ($_no_default_regex) {
561 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
562 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
563 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
565 foreach my $u (keys %$l_map) {
567 foreach my $p (keys %{$l_map->{$u}}) {
568 graft_merge_msg($grafts,$l_map,$u,$p,@re);
571 unless ($_no_graft_copy) {
572 graft_file_copy_lib($grafts,$l_map,$u);
575 graft_tree_joins($grafts);
577 write_grafts($grafts, $comments, $gr_file);
578 unlink "$gr_file~$gr_sha1" if $gr_sha1;
583 unless (defined $_trunk || defined $_branches || defined $_tags) {
586 if (defined $_trunk) {
587 my $trunk_url = complete_svn_url($url, $_trunk);
589 if ($GIT_SVN eq 'git-svn') {
591 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
594 unless (-d $GIT_SVN_DIR) {
596 print "GIT_SVN_ID set to 'trunk' for ",
597 "$trunk_url ($_trunk)\n";
600 command_noisy('config', 'svn.trunk', $trunk_url);
603 $_prefix = '' unless defined $_prefix;
604 complete_url_ls_init($url, $_branches, '--branches/-b', $_prefix);
605 complete_url_ls_init($url, $_tags, '--tags/-t', $_prefix . 'tags/');
609 # try to do trunk first, since branches/tags
610 # may be descended from it.
611 if (-e "$GIT_DIR/svn/trunk/info/url") {
612 fetch_child_id('trunk', @_);
614 rec_fetch('', "$GIT_DIR/svn", @_);
620 my $r_last = -1; # prevent dupes
621 rload_authors() if $_authors;
627 if (defined $_revision) {
628 if ($_revision =~ /^(\d+):(\d+)$/) {
629 ($r_min, $r_max) = ($1, $2);
630 } elsif ($_revision =~ /^\d+$/) {
631 $r_min = $r_max = $_revision;
633 print STDERR "-r$_revision is not supported, use ",
634 "standard \'git log\' arguments instead\n";
640 @args = (git_svn_log_cmd($r_min, $r_max), @args);
641 my $log = command_output_pipe(@args);
646 if (/^${_esc_color}commit ($sha1_short)/o) {
648 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
650 process_commit($c, $r_min, $r_max, \@k) or
655 } elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
656 get_author_info($c, $1, $2, $3);
657 } elsif (/^${_esc_color}(?:tree|parent|committer) /) {
659 } elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
660 push @{$c->{raw}}, $_;
661 } elsif (/^${_esc_color}[ACRMDT]\t/) {
662 # we could add $SVN->{svn_path} here, but that requires
663 # remote access at the moment (repo_path_split)...
664 s#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
665 push @{$c->{changed}}, $_;
666 } elsif (/^${_esc_color}diff /) {
668 push @{$c->{diff}}, $_;
670 push @{$c->{diff}}, $_;
671 } elsif (/^${_esc_color} (git-svn-id:.+)$/) {
672 ($c->{url}, $c->{r}, undef) = extract_metadata($1);
673 } elsif (s/^${_esc_color} //) {
677 if ($c && defined $c->{r} && $c->{r} != $r_last) {
679 process_commit($c, $r_min, $r_max, \@k);
685 process_commit($_, $r_min, $r_max) foreach reverse @k;
689 print '-' x72,"\n" unless $_incremental || $_oneline;
692 sub commit_diff_usage {
693 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
698 my $ta = shift or commit_diff_usage();
699 my $tb = shift or commit_diff_usage();
700 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
701 print STDERR "Needed URL or usable git-svn id command-line\n";
705 unless (defined $r) {
706 if (defined $_revision) {
709 die "-r|--revision is a required argument\n";
712 if (defined $_message && defined $_file) {
713 print STDERR "Both --message/-m and --file/-F specified ",
714 "for the commit message.\n",
715 "I have no idea what you mean\n";
718 if (defined $_file) {
719 $_message = file_to_s($_file);
721 $_message ||= get_commit_message($tb,
722 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
724 $SVN ||= Git::SVN::Ra->new($SVN_URL);
726 $r = $SVN->get_latest_revnum;
727 } elsif ($r !~ /^\d+$/) {
728 die "revision argument: $r not understood by git-svn\n";
731 my $pool = SVN::Pool->new;
732 my $ed = SVN::Git::Editor->new({ r => $r,
735 svn_path => $SVN->{svn_path}
737 $SVN->get_commit_editor($_message,
739 $rev_committed = $_[0];
740 print "Committed $_[0]\n";
745 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
747 print "No changes\n$ta == $tb\n";
755 $_message = $_file = undef;
756 return $rev_committed;
759 ########################### utility functions #########################
763 return 1 if defined $c->{r};
764 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
765 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
766 my @msg = command(qw/cat-file commit/, $c->{c});
767 shift @msg while ($msg[0] ne "\n");
769 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
771 (undef, $c->{r}, undef) = extract_metadata(
772 (grep(/^git-svn-id: /, @msg))[-1]);
774 return defined $c->{r};
780 $dcvar = 'color.diff';
781 $dc = `git-config --get $dcvar`;
783 # nothing at all; fallback to "diff.color"
784 $dcvar = 'diff.color';
785 $dc = `git-config --get $dcvar`;
790 $pc = `git-config --get color.pager`;
792 # does not have it -- fallback to pager.color
793 $pc = `git-config --bool --get pager.color`;
796 $pc = `git-config --bool --get color.pager`;
802 if (-t *STDOUT || (defined $_pager && $pc eq 'true')) {
803 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
807 return 0 if $dc eq 'never';
808 return 1 if $dc eq 'always';
809 chomp($dc = `git-config --bool --get $dcvar`);
810 return ($dc eq 'true');
813 sub git_svn_log_cmd {
814 my ($r_min, $r_max) = @_;
815 my @cmd = (qw/log --abbrev-commit --pretty=raw
816 --default/, "refs/remotes/$GIT_SVN");
817 push @cmd, '-r' unless $_non_recursive;
818 push @cmd, qw/--raw --name-status/ if $_verbose;
819 push @cmd, '--color' if log_use_color();
820 return @cmd unless defined $r_max;
821 if ($r_max == $r_min) {
822 push @cmd, '--max-count=1';
823 if (my $c = revdb_get($REVDB, $r_max)) {
828 $c_max = revdb_get($REVDB, $r_max);
829 $c_min = revdb_get($REVDB, $r_min);
830 if (defined $c_min && defined $c_max) {
831 if ($r_max > $r_max) {
832 push @cmd, "$c_min..$c_max";
834 push @cmd, "$c_max..$c_min";
836 } elsif ($r_max > $r_min) {
847 print "Fetching $id\n";
848 my $ref = "$GIT_DIR/refs/remotes/$id";
849 defined(my $pid = open my $fh, '-|') or croak $!;
851 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
858 check_repack() if (/^r\d+ = $sha1/o);
860 close $fh or croak $?;
864 my ($pfx, $p, @args) = @_;
866 foreach (sort <$p/*>) {
867 if (-r "$_/info/url") {
868 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
869 my $id = $pfx . basename $_;
870 next if $id eq 'trunk';
871 fetch_child_id($id, @args);
878 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
883 sub complete_svn_url {
884 my ($url, $path) = @_;
886 $url =~ s#/+$## if $url;
887 if ($path !~ m#^[a-z\+]+://#) {
888 $path = '/' . $path if ($path !~ m#^/#);
889 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
890 fatal("E: '$path' is not a complete URL ",
891 "and a separate URL is not specified\n");
893 $path = $url . $path;
898 sub complete_url_ls_init {
899 my ($url, $path, $switch, $pfx) = @_;
901 print STDERR "W: $switch not specified\n";
904 my $full_url = complete_svn_url($url, $path);
905 my @ls = libsvn_ls_fullurl($full_url);
906 defined(my $pid = fork) or croak $!;
908 foreach my $u (map { "$full_url/$_" } (grep m!/$!, @ls)) {
910 if ($u !~ m!\Q$full_url\E/(.+)$!) {
911 print STDERR "W: Unrecognized URL: $u\n";
912 die "This should never happen\n";
914 # don't try to init already existing refs
916 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
918 unless (-d $GIT_SVN_DIR) {
919 print "init $u => $id\n";
927 my ($n) = ($switch =~ /^--(\w+)/);
928 command_noisy('config', "svn.$n", $full_url);
935 my @tmp = split m#/#, $_;
937 while (my $x = shift @tmp) {
943 foreach (sort {length $b <=> length $a} keys %common) {
944 if ($common{$_} == @$paths) {
951 # grafts set here are 'stronger' in that they're based on actual tree
952 # matches, and won't be deleted from merge-base checking in write_grafts()
953 sub graft_tree_joins {
955 map_tree_joins() if (@_branch_from && !%tree_map);
956 return unless %tree_map;
960 my @args = (qw/rev-list --pretty=raw/, "refs/remotes/$i");
961 my ($fh, $ctx) = command_output_pipe(@args);
963 next unless /^commit ($sha1)$/o;
965 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
966 next unless $tree_map{$t};
971 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
973 my ($s, $tz) = ($1, $2);
974 if ($tz =~ s/^\+//) {
975 $s += tz_to_s_offset($tz);
976 } elsif ($tz =~ s/^\-//) {
977 $s -= tz_to_s_offset($tz);
980 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
982 foreach my $p (@{$tree_map{$t}}) {
984 my $mb = eval { command('merge-base', $c, $p) };
985 next unless ($@ || $?);
987 # see if SVN says it's a relative
988 my ($url_b, $r_b, $uuid_b) =
990 next if (defined $url_b &&
992 ($url_a eq $url_b) &&
993 ($uuid_a eq $uuid_b));
994 if ($uuid_a eq $uuid_b) {
996 $grafts->{$c}->{$p} = 2;
998 } elsif ($r_b > $r_a) {
999 $grafts->{$p}->{$c} = 2;
1004 my $ct = get_commit_time($p);
1006 $grafts->{$c}->{$p} = 2;
1007 } elsif ($ct > $s) {
1008 $grafts->{$p}->{$c} = 2;
1010 # what should we do when $ct == $s ?
1013 command_close_pipe($fh, $ctx);
1017 sub graft_file_copy_lib {
1018 my ($grafts, $l_map, $u) = @_;
1019 my $tree_paths = $l_map->{$u};
1020 my $pfx = common_prefix([keys %$tree_paths]);
1021 my ($repo, $path) = repo_path_split($u.$pfx);
1022 $SVN = Git::SVN::Ra->new($repo);
1024 my ($base, $head) = libsvn_parse_revision();
1026 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
1027 my $eh = $SVN::Error::handler;
1028 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
1030 $SVN->dup->get_log([$path], $min, $max, 0, 2, 1,
1032 libsvn_graft_file_copies($grafts, $tree_paths,
1035 last if ($max >= $head);
1038 $max = $head if ($max > $head);
1040 $SVN::Error::handler = $eh;
1043 sub process_merge_msg_matches {
1044 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1045 my (@strong, @weak);
1046 foreach (@matches) {
1047 # merging with ourselves is not interesting
1049 if ($l_map->{$u}->{$_}) {
1055 foreach my $w (@weak) {
1057 # no exact match, use branch name as regexp.
1058 my $re = qr/\Q$w\E/i;
1059 foreach (keys %{$l_map->{$u}}) {
1061 push @strong, $l_map->{$u}->{$_};
1068 foreach (keys %{$l_map->{$u}}) {
1070 push @strong, $l_map->{$u}->{$_};
1075 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1076 \s(?:[a-f\d\-]+)$/xsm);
1077 unless (defined $rev) {
1078 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1079 \@(?:[a-f\d\-]+)/xsm);
1080 return unless defined $rev;
1082 foreach my $m (@strong) {
1083 my ($r0, $s0) = find_rev_before($rev, $m, 1);
1084 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1088 sub graft_merge_msg {
1089 my ($grafts, $l_map, $u, $p, @re) = @_;
1091 my $x = $l_map->{$u}->{$p};
1092 my $rl = rev_list_raw("refs/remotes/$x");
1093 while (my $c = next_rev_list_entry($rl)) {
1094 foreach my $re (@re) {
1095 my (@br) = ($c->{m} =~ /$re/g);
1097 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1104 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1105 { STDERR => 0 }); };
1108 sub repo_path_split {
1109 my $full_url = shift;
1110 $full_url =~ s#/+$##;
1112 foreach (@repo_path_split_cache) {
1113 if ($full_url =~ s#$_##) {
1115 $full_url =~ s#^/+##;
1116 return ($u, $full_url);
1119 my $tmp = Git::SVN::Ra->new($full_url);
1120 return ($tmp->{repos_root}, $tmp->{svn_path});
1124 defined $SVN_URL or croak "SVN repository location required\n";
1125 unless (-d $GIT_DIR) {
1126 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1128 mkpath([$GIT_SVN_DIR]);
1129 mkpath(["$GIT_SVN_DIR/info"]);
1130 open my $fh, '>>',$REVDB or croak $!;
1132 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
1136 sub get_tree_from_treeish {
1138 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1139 my $type = command_oneline(qw/cat-file -t/, $treeish);
1141 while ($type eq 'tag') {
1142 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
1144 if ($type eq 'commit') {
1145 $expected = (grep /^tree /, command(qw/cat-file commit/,
1147 ($expected) = ($expected =~ /^tree ($sha1)$/);
1148 die "Unable to get tree from $treeish\n" unless $expected;
1149 } elsif ($type eq 'tree') {
1150 $expected = $treeish;
1152 die "$treeish is a $type, expected tree, tag or commit\n";
1158 my ($from, $treeish) = @_;
1159 print "diff-tree $from $treeish\n";
1160 my @diff_tree = qw(diff-tree -z -r);
1161 if ($_cp_similarity) {
1162 push @diff_tree, "-C$_cp_similarity";
1164 push @diff_tree, '-C';
1166 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1167 push @diff_tree, "-l$_l" if defined $_l;
1168 push @diff_tree, $from, $treeish;
1169 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
1173 while (<$diff_fh>) {
1174 chomp $_; # this gets rid of the trailing "\0"
1175 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1176 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1177 push @mods, { mode_a => $1, mode_b => $2,
1178 sha1_b => $3, chg => $4 };
1179 if ($4 =~ /^(?:C|R)$/) {
1184 } elsif ($state eq 'file_a') {
1185 my $x = $mods[$#mods] or croak "Empty array\n";
1186 if ($x->{chg} !~ /^(?:C|R)$/) {
1187 croak "Error parsing $_, $x->{chg}\n";
1191 } elsif ($state eq 'file_b') {
1192 my $x = $mods[$#mods] or croak "Empty array\n";
1193 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1194 croak "Error parsing $_, $x->{chg}\n";
1196 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1197 croak "Error parsing $_, $x->{chg}\n";
1202 croak "Error parsing $_\n";
1205 command_close_pipe($diff_fh, $ctx);
1209 sub libsvn_checkout_tree {
1210 my ($from, $treeish, $ed) = @_;
1211 my $mods = get_diff($from, $treeish);
1212 return $mods unless (scalar @$mods);
1213 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1214 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1216 if (defined $o{$f}) {
1219 croak "Invalid change type: $f\n";
1222 $ed->rmdirs($_q) if $_rmdir;
1226 sub get_commit_message {
1227 my ($commit, $commit_msg) = (@_);
1228 my %log_msg = ( msg => '' );
1229 open my $msg, '>', $commit_msg or croak $!;
1231 my $type = command_oneline(qw/cat-file -t/, $commit);
1232 if ($type eq 'commit' || $type eq 'tag') {
1233 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
1238 $in_msg = 1 if (/^\s*$/);
1239 } elsif (/^git-svn-id: /) {
1240 # skip this, we regenerate the correct one
1241 # on re-fetch anyways
1243 print $msg $_ or croak $!;
1246 command_close_pipe($msg_fh, $ctx);
1248 close $msg or croak $!;
1250 if ($_edit || ($type eq 'tree')) {
1251 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1252 system($editor, $commit_msg);
1255 # file_to_s removes all trailing newlines, so just use chomp() here:
1256 open $msg, '<', $commit_msg or croak $!;
1257 { local $/; chomp($log_msg{msg} = <$msg>); }
1258 close $msg or croak $!;
1263 sub set_svn_commit_env {
1264 if (defined $LC_ALL) {
1265 $ENV{LC_ALL} = $LC_ALL;
1267 delete $ENV{LC_ALL};
1272 my ($fh, $c) = command_output_pipe(qw/rev-list --pretty=raw/, @_);
1273 return { fh => $fh, ctx => $c, t => { } };
1276 sub next_rev_list_entry {
1281 if (/^commit ($sha1)$/o) {
1283 $rl->{t} = { c => $1 };
1288 } elsif (/^parent ($sha1)$/o) {
1295 command_close_pipe($fh, $rl->{ctx});
1296 return ($x != $rl->{t}) ? $x : undef;
1300 my ($str, $file, $mode) = @_;
1301 open my $fd,'>',$file or croak $!;
1302 print $fd $str,"\n" or croak $!;
1303 close $fd or croak $!;
1304 chmod ($mode &~ umask, $file) if (defined $mode);
1309 open my $fd,'<',$file or croak "$!: file: $file\n";
1312 close $fd or croak $!;
1317 sub assert_revision_unknown {
1319 if (my $c = revdb_get($REVDB, $r)) {
1320 croak "$r = $c already exists! Why are we refetching it?";
1325 my ($log_msg, @parents) = @_;
1326 assert_revision_unknown($log_msg->{revision});
1327 map_tree_joins() if (@_branch_from && !%tree_map);
1329 my (@tmp_parents, @exec_parents, %seen_parent);
1330 if (my $lparents = $log_msg->{parents}) {
1331 @tmp_parents = @$lparents
1333 # commit parents can be conditionally bound to a particular
1334 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1335 foreach my $p (@parents) {
1336 next unless defined $p;
1337 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1338 if ($1 == $log_msg->{revision}) {
1339 push @tmp_parents, $2;
1342 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1345 my $tree = $log_msg->{tree};
1346 if (!defined $tree) {
1347 my $index = set_index($GIT_SVN_INDEX);
1348 $tree = command_oneline('write-tree');
1350 restore_index($index);
1352 # just in case we clobber the existing ref, we still want that ref
1354 if (my $cur = verify_ref("refs/remotes/$GIT_SVN^0")) {
1356 push @tmp_parents, $cur;
1359 if (exists $tree_map{$tree}) {
1360 foreach my $p (@{$tree_map{$tree}}) {
1362 foreach (@tmp_parents) {
1363 # see if a common parent is found
1364 my $mb = eval { command('merge-base', $_, $p) };
1370 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
1371 next if (($SVN->uuid eq $uuid_p) &&
1372 ($log_msg->{revision} > $r_p));
1373 next if (defined $url_p && defined $SVN_URL &&
1374 ($SVN->uuid eq $uuid_p) &&
1375 ($url_p eq $SVN_URL));
1376 push @tmp_parents, $p;
1379 foreach (@tmp_parents) {
1380 next if $seen_parent{$_};
1381 $seen_parent{$_} = 1;
1382 push @exec_parents, $_;
1383 # MAXPARENT is defined to 16 in commit-tree.c:
1384 last if @exec_parents > 16;
1387 set_commit_env($log_msg);
1388 my @exec = ('git-commit-tree', $tree);
1389 push @exec, '-p', $_ foreach @exec_parents;
1390 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1392 print $msg_fh $log_msg->{msg} or croak $!;
1393 unless ($_no_metadata) {
1394 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision} ",
1395 $SVN->uuid,"\n" or croak $!;
1397 $msg_fh->flush == 0 or croak $!;
1398 close $msg_fh or croak $!;
1399 chomp(my $commit = do { local $/; <$out_fh> });
1400 close $out_fh or croak $!;
1403 if ($commit !~ /^$sha1$/o) {
1404 die "Failed to commit, invalid sha1: $commit\n";
1406 command_noisy('update-ref',"refs/remotes/$GIT_SVN",$commit);
1407 revdb_set($REVDB, $log_msg->{revision}, $commit);
1409 # this output is read via pipe, do not change:
1410 print "r$log_msg->{revision} = $commit\n";
1415 if ($_repack && (--$_repack_nr == 0)) {
1416 $_repack_nr = $_repack;
1417 # repack doesn't use any arguments with spaces in them, does it?
1418 command_noisy('repack', split(/\s+/, $_repack_flags));
1422 sub set_commit_env {
1424 my $author = $log_msg->{author};
1425 if (!defined $author || length $author == 0) {
1426 $author = '(no author)';
1428 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1429 : ($author,$author . '@' . $SVN->uuid);
1430 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1431 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1432 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1435 sub check_upgrade_needed {
1437 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
1438 open my $fh, '>>',$REVDB or croak $!;
1441 return unless eval {
1442 command([qw/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
1445 my $head = eval { command('rev-parse',"refs/remotes/$GIT_SVN") };
1447 print STDERR "Please run: $0 rebuild --upgrade\n";
1452 # fills %tree_map with a reverse mapping of trees to commits. Useful
1453 # for finding parents to commit on.
1454 sub map_tree_joins {
1456 foreach my $br (@_branch_from) {
1457 my $pipe = command_output_pipe(qw/rev-list
1458 --topo-order --pretty=raw/, $br);
1460 if (/^commit ($sha1)$/o) {
1463 # if we've seen a commit,
1464 # we've seen its parents
1465 last if $seen{$commit};
1466 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1467 unless (defined $tree) {
1468 die "Failed to parse commit $commit\n";
1470 push @{$tree_map{$tree}}, $commit;
1479 if (@_branch_from) {
1480 print STDERR '--branch|-b parameters are ignored when ',
1481 "--branch-all-refs|-B is passed\n";
1484 # don't worry about rev-list on non-commit objects/tags,
1485 # it shouldn't blow up if a ref is a blob or tree...
1486 @_branch_from = command(qw/rev-parse --symbolic --all/);
1489 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1491 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1492 while (<$authors>) {
1494 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1495 my ($user, $name, $email) = ($1, $2, $3);
1496 $users{$user} = [$name, $email];
1498 close $authors or croak $!;
1502 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1503 while (<$authors>) {
1505 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1506 my ($user, $name, $email) = ($1, $2, $3);
1507 $rusers{"$name <$email>"} = $user;
1509 close $authors or croak $!;
1514 foreach (command(qw/rev-parse --symbolic --all/)) {
1515 next unless s#^refs/remotes/##;
1517 next unless -f "$GIT_DIR/svn/$_/info/url";
1525 defined(my $pid = fork) or croak $!;
1527 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
1529 exit 0 if -r $REVDB;
1530 print "Upgrading svn => git mapping...\n";
1531 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
1532 open my $fh, '>>',$REVDB or croak $!;
1535 print "Done upgrading. You may now delete the ",
1536 "deprecated $GIT_SVN_DIR/revs directory\n";
1544 sub migration_check {
1545 migrate_revdb() unless (-e $REVDB);
1546 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
1547 print "Upgrading repository...\n";
1548 unless (-d "$GIT_DIR/svn") {
1549 mkdir "$GIT_DIR/svn" or croak $!;
1551 print "Data from a previous version of git-svn exists, but\n\t",
1552 "$GIT_SVN_DIR\n\t(required for this version ",
1553 "($VERSION) of git-svn) does not.\n";
1555 foreach my $x (command(qw/rev-parse --symbolic --all/)) {
1556 next unless $x =~ s#^refs/remotes/##;
1558 next unless -f "$GIT_DIR/$x/info/url";
1559 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
1561 my $dn = dirname("$GIT_DIR/svn/$x");
1562 mkpath([$dn]) unless -d $dn;
1563 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
1565 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
1566 print "Done upgrading.\n";
1569 sub find_rev_before {
1570 my ($r, $id, $eq_ok) = @_;
1571 my $f = "$GIT_DIR/svn/$id/.rev_db";
1572 return (undef,undef) unless -r $f;
1575 if (my $c = revdb_get($f, $r)) {
1580 return (undef, undef);
1584 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1585 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1586 $REVDB = "$GIT_SVN_DIR/.rev_db";
1587 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1592 # convert GetOpt::Long specs for use by git-config
1593 sub read_repo_config {
1594 return unless -d $GIT_DIR;
1596 foreach my $o (keys %$opts) {
1597 my $v = $opts->{$o};
1598 my ($key) = ($o =~ /^([a-z\-]+)/);
1600 my $arg = 'git-config';
1601 $arg .= ' --int' if ($o =~ /[:=]i$/);
1602 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1603 if (ref $v eq 'ARRAY') {
1604 chomp(my @tmp = `$arg --get-all svn.$key`);
1607 chomp(my $tmp = `$arg --get svn.$key`);
1608 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1615 sub set_default_vals {
1616 if (defined $_repack) {
1617 $_repack = 1000 if ($_repack <= 0);
1618 $_repack_nr = $_repack;
1619 $_repack_flags ||= '-d';
1624 my $gr_file = shift;
1625 my ($grafts, $comments) = ({}, {});
1626 if (open my $fh, '<', $gr_file) {
1629 if (/^($sha1)\s+/) {
1632 @{$comments->{$c}} = @tmp;
1635 foreach my $p (split /\s+/, $_) {
1636 $grafts->{$c}->{$p} = 1;
1642 close $fh or croak $!;
1643 @{$comments->{'END'}} = @tmp if @tmp;
1645 return ($grafts, $comments);
1649 my ($grafts, $comments, $gr_file) = @_;
1651 open my $fh, '>', $gr_file or croak $!;
1652 foreach my $c (sort keys %$grafts) {
1653 if ($comments->{$c}) {
1654 print $fh $_ foreach @{$comments->{$c}};
1656 my $p = $grafts->{$c};
1657 my %x; # real parents
1658 delete $p->{$c}; # commits are not self-reproducing...
1659 my $ch = command_output_pipe(qw/cat-file commit/, $c);
1661 if (/^parent ($sha1)/) {
1662 $x{$1} = $p->{$1} = 1;
1667 close $ch; # breaking the pipe
1669 # if real parents are the only ones in the grafts, drop it
1670 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1674 @ip = @jp = keys %$p;
1675 foreach my $i (@ip) {
1676 next if $del{$i} || $p->{$i} == 2;
1677 foreach my $j (@jp) {
1678 next if $i eq $j || $del{$j} || $p->{$j} == 2;
1679 $mb = eval { command('merge-base', $i, $j) };
1686 } elsif ($mb eq $i) {
1693 # if real parents are the only ones in the grafts, drop it
1694 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1696 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1698 if ($comments->{'END'}) {
1699 print $fh $_ foreach @{$comments->{'END'}};
1701 close $fh or croak $!;
1704 sub read_url_paths_all {
1705 my ($l_map, $pfx, $p) = @_;
1708 if (-r "$_/info/url") {
1709 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
1710 my $id = $pfx . basename $_;
1711 my $url = file_to_s("$_/info/url");
1712 my ($u, $p) = repo_path_split($url);
1713 $l_map->{$u}->{$p} = $id;
1720 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
1721 read_url_paths_all($l_map, $x, $_);
1725 # this one only gets ids that have been imported, not new ones
1726 sub read_url_paths {
1728 git_svn_each(sub { my $x = shift;
1729 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
1730 my ($u, $p) = repo_path_split($url);
1731 $l_map->{$u}->{$p} = $x;
1736 sub extract_metadata {
1737 my $id = shift or return (undef, undef, undef);
1738 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
1740 if (!defined $rev || !$uuid || !$url) {
1741 # some of the original repositories I made had
1742 # identifiers like this:
1743 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1745 return ($url, $rev, $uuid);
1749 return extract_metadata((grep(/^git-svn-id: /,
1750 command(qw/cat-file commit/, shift)))[-1]);
1753 sub get_commit_time {
1755 my $fh = command_output_pipe(qw/rev-list --pretty=raw -n1/, $cmt);
1757 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
1758 my ($s, $tz) = ($1, $2);
1759 if ($tz =~ s/^\+//) {
1760 $s += tz_to_s_offset($tz);
1761 } elsif ($tz =~ s/^\-//) {
1762 $s -= tz_to_s_offset($tz);
1767 die "Can't get commit time for commit: $cmt\n";
1770 sub tz_to_s_offset {
1773 return ($1 * 60) + ($tz * 3600);
1776 # adapted from pager.c
1778 $_pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
1779 if (!defined $_pager) {
1781 } elsif (length $_pager == 0 || $_pager eq 'cat') {
1787 return unless -t *STDOUT;
1788 pipe my $rfd, my $wfd or return;
1789 defined(my $pid = fork) or croak $!;
1791 open STDOUT, '>&', $wfd or croak $!;
1794 open STDIN, '<&', $rfd or croak $!;
1795 $ENV{LESS} ||= 'FRSX';
1796 exec $_pager or croak "Can't run pager: $! ($_pager)\n";
1799 sub get_author_info {
1800 my ($dest, $author, $t, $tz) = @_;
1801 $author =~ s/(?:^\s*|\s*$)//g;
1802 $dest->{a_raw} = $author;
1805 $_a = $rusers{$author} || undef;
1808 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1813 # Date::Parse isn't in the standard Perl distro :(
1814 if ($tz =~ s/^\+//) {
1815 $t += tz_to_s_offset($tz);
1816 } elsif ($tz =~ s/^\-//) {
1817 $t -= tz_to_s_offset($tz);
1819 $dest->{t_utc} = $t;
1822 sub process_commit {
1823 my ($c, $r_min, $r_max, $defer) = @_;
1824 if (defined $r_min && defined $r_max) {
1825 if ($r_min == $c->{r} && $r_min == $r_max) {
1829 return 1 if $r_min == $r_max;
1830 if ($r_min < $r_max) {
1831 # we need to reverse the print order
1832 return 0 if (defined $_limit && --$_limit < 0);
1836 if ($r_min != $r_max) {
1837 return 1 if ($r_min < $c->{r});
1838 return 1 if ($r_max > $c->{r});
1841 return 0 if (defined $_limit && --$_limit < 0);
1850 if (my $l = $c->{l}) {
1851 while ($l->[0] =~ /^\s*$/) { shift @$l }
1854 $_l_fmt ||= 'A' . length($c->{r});
1855 print 'r',pack($_l_fmt, $c->{r}),' | ';
1856 print "$c->{c} | " if $_show_commit;
1859 show_commit_normal($c);
1863 sub show_commit_changed_paths {
1865 return unless $c->{changed};
1866 print "Changed paths:\n", @{$c->{changed}};
1869 sub show_commit_normal {
1871 print '-' x72, "\nr$c->{r} | ";
1872 print "$c->{c} | " if $_show_commit;
1873 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
1874 localtime($c->{t_utc})), ' | ';
1877 if (my $l = $c->{l}) {
1878 while ($l->[$#$l] eq "\n" && $#$l > 0
1879 && $l->[($#$l - 1)] eq "\n") {
1882 $nr_line = scalar @$l;
1884 print "1 line\n\n\n";
1886 if ($nr_line == 1) {
1887 $nr_line = '1 line';
1889 $nr_line .= ' lines';
1891 print $nr_line, "\n";
1892 show_commit_changed_paths($c);
1894 print $_ foreach @$l;
1898 show_commit_changed_paths($c);
1902 foreach my $x (qw/raw diff/) {
1905 print $_ foreach @{$c->{$x}}
1913 use vars qw/$default/;
1915 use File::Path qw/mkpath/;
1918 # properties that we do not log:
1921 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1922 svn:special svn:executable
1923 svn:entry:committed-rev
1924 svn:entry:last-author
1926 svn:entry:committed-date/;
1930 my ($class, $id, $url) = @_;
1931 my $self = _new($class, $id);
1932 mkpath(["$self->{dir}/info"]);
1934 $url =~ s!/+$!!; # strip trailing slash
1935 s_to_file($url, "$self->{dir}/info/url");
1937 $self->{url} = $url;
1938 open my $fh, '>>', $self->{db_path} or croak $!;
1939 close $fh or croak $!;
1944 my ($class, $id) = @_;
1945 my $self = _new($class, $id);
1946 $self->{url} = file_to_s("$self->{dir}/info/url");
1950 sub refname { "refs/remotes/$_[0]->{id}" }
1954 $self->{ra} ||= Git::SVN::Ra->new($self->{url});
1957 sub copy_remote_ref {
1959 my $origin = $::_cp_remote ? $::_cp_remote : 'origin';
1960 my $ref = $self->refname;
1961 if (command('ls-remote', $origin, $ref)) {
1962 command_noisy('fetch', $origin, "$ref:$ref");
1963 } elsif ($::_cp_remote && !$::_upgrade) {
1964 die "Unable to find remote reference: $ref on $origin\n";
1968 sub traverse_ignore {
1969 my ($self, $fh, $path, $r) = @_;
1971 my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
1973 $p =~ s#^\Q$self->{ra}->{svn_path}\E/##;
1974 print $fh length $p ? "\n# $p\n" : "\n# /\n";
1975 if (my $s = $props->{'svn:ignore'}) {
1976 $s =~ s/[\r\n]+/\n/g;
1978 if (length $p == 0) {
1982 $s =~ s#\n#\n/$p/#g;
1983 print $fh "/$p/$s\n";
1986 foreach (sort keys %$dirent) {
1987 next if $dirent->{$_}->kind != $SVN::Node::dir;
1988 $self->traverse_ignore($fh, "$path/$_", $r);
1992 # returns the newest SVN revision number and newest commit SHA1
1993 sub last_rev_commit {
1995 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1996 return ($self->{last_rev}, $self->{last_commit});
1998 my $c = verify_ref($self->refname.'^0');
1999 if (defined $c && length $c) {
2000 my $rev = (cmt_metadata($c))[1];
2002 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2006 my $offset = -41; # from tail
2008 open my $fh, '<', $self->{db_path} or
2009 croak "$self->{db_path} not readable: $!\n";
2010 seek $fh, $offset, 2;
2012 defined $rl or return (undef, undef);
2014 while ($c ne $rl && tell $fh != 0) {
2016 seek $fh, $offset, 2;
2018 defined $rl or return (undef, undef);
2022 croak $! if ($rev < 0);
2023 $rev = ($rev - 41) / 41;
2024 close $fh or croak $!;
2025 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2029 sub parse_revision {
2030 my ($self, $base) = @_;
2031 my $head = $self->ra->get_latest_revnum;
2032 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
2033 return ($base + 1, $head) if (defined $base);
2036 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
2037 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
2038 if ($::_revision =~ /^BASE:(\d+)$/) {
2039 return ($base + 1, $1) if (defined $base);
2042 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
2043 die "revision argument: $::_revision not understood by git-svn\n",
2044 "Try using the command-line svn client instead\n";
2048 my ($self, $sub) = @_;
2049 my $old_index = $ENV{GIT_INDEX_FILE};
2050 $ENV{GIT_INDEX_FILE} = $self->{index};
2053 $ENV{GIT_INDEX_FILE} = $old_index;
2055 delete $ENV{GIT_INDEX_FILE};
2057 wantarray ? @ret : $ret[0];
2060 sub assert_index_clean {
2061 my ($self, $treeish) = @_;
2063 $self->tmp_index_do(sub {
2064 command_noisy('read-tree', $treeish) unless -e $self->{index};
2065 my $x = command_oneline('write-tree');
2066 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2067 /^tree ($::sha1)/mo);
2069 unlink $self->{index} or croak $!;
2070 command_noisy('read-tree', $treeish);
2072 $x = command_oneline('write-tree');
2074 ::fatal "trees ($treeish) $y != $x\n",
2075 "Something is seriously wrong...\n";
2080 sub get_commit_parents {
2081 my ($self, $log_msg, @parents) = @_;
2082 my (%seen, @ret, @tmp);
2083 # commit parents can be conditionally bound to a particular
2084 # svn revision via: "svn_revno=commit_sha1", filter them out here:
2085 foreach my $p (@parents) {
2086 next unless defined $p;
2087 if ($p =~ /^(\d+)=($::sha1_short)$/o) {
2088 push @tmp, $2 if $1 == $log_msg->{revision};
2090 push @tmp, $p if $p =~ /^$::sha1_short$/o;
2093 if (my $cur = verify_ref($self->refname.'^0')) {
2096 push @tmp, $_ foreach (@{$log_msg->{parents}}, @tmp);
2097 while (my $p = shift @tmp) {
2101 # MAXPARENT is defined to 16 in commit-tree.c:
2105 die "r$log_msg->{revision}: No room for parents:\n\t",
2106 join("\n\t", @tmp), "\n";
2111 sub check_upgrade_needed {
2113 if (!-r $self->{db_path}) {
2114 -d $self->{dir} or mkpath([$self->{dir}]);
2115 open my $fh, '>>', $self->{db_path} or croak $!;
2118 return unless verify_ref($self->{id}.'-HEAD^0');
2119 my $head = verify_ref($self->refname.'^0');
2121 fatal("Please run: $0 rebuild --upgrade\n");
2126 my ($self, $log_msg, @parents) = @_;
2127 if (my $c = $self->rev_db_get($log_msg->{revision})) {
2128 croak "$log_msg->{revision} = $c already exists! ",
2129 "Why are we refetching it?\n";
2131 my ($name, $email) = author_name_email($log_msg->{author}, $self->ra);
2132 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2133 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2134 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2136 my $tree = $log_msg->{tree};
2137 if (!defined $tree) {
2138 $tree = $self->tmp_index_do(sub {
2139 command_oneline('write-tree') });
2141 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2143 my @exec = ('git-commit-tree', $tree);
2144 foreach ($self->get_commit_parents($log_msg, @parents)) {
2145 push @exec, '-p', $_;
2147 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2149 print $msg_fh $log_msg->{log} or croak $!;
2150 print $msg_fh "\ngit-svn-id: $self->{ra}->{url}\@$log_msg->{revision}",
2151 " ", $self->ra->uuid,"\n" or croak $!;
2152 $msg_fh->flush == 0 or croak $!;
2153 close $msg_fh or croak $!;
2154 chomp(my $commit = do { local $/; <$out_fh> });
2155 close $out_fh or croak $!;
2158 if ($commit !~ /^$::sha1$/o) {
2159 die "Failed to commit, invalid sha1: $commit\n";
2162 command_noisy('update-ref',$self->refname, $commit);
2163 $self->rev_db_set($log_msg->{revision}, $commit);
2165 $self->{last_rev} = $log_msg->{revision};
2166 $self->{last_commit} = $commit;
2167 print "r$log_msg->{revision} = $commit\n";
2172 my ($self, $paths, $rev) = @_; #, $author, $date, $msg) = @_;
2173 my $ed = SVN::Git::Fetcher->new($self);
2174 my ($last_rev, @parents);
2175 if ($self->{last_commit}) {
2176 $last_rev = $self->{last_rev};
2177 $ed->{c} = $self->{last_commit};
2178 @parents = ($self->{last_commit});
2182 unless ($self->ra->do_update($last_rev, $rev, '', 1, $ed)) {
2183 die "SVN connection failed somewhere...\n";
2185 $self->make_log_entry($rev, \@parents, $ed);
2188 sub write_untracked {
2189 my ($self, $rev, $fh, $untracked) = @_;
2191 print $fh "r$rev\n" or croak $!;
2192 $h = $untracked->{empty};
2193 foreach (sort keys %$h) {
2194 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
2195 print $fh " $act: ", uri_encode($_), "\n" or croak $!;
2196 warn "W: $act: $_\n";
2198 foreach my $t (qw/dir_prop file_prop/) {
2199 $h = $untracked->{$t} or next;
2200 foreach my $path (sort keys %$h) {
2201 my $ppath = $path eq '' ? '.' : $path;
2202 foreach my $prop (sort keys %{$h->{$path}}) {
2203 next if $SKIP{$prop};
2204 my $v = $h->{$path}->{$prop};
2207 uri_encode($ppath), ' ',
2208 uri_encode($prop), ' ',
2209 uri_encode($v), "\n"
2213 uri_encode($ppath), ' ',
2214 uri_encode($prop), "\n"
2220 foreach my $t (qw/absent_file absent_directory/) {
2221 $h = $untracked->{$t} or next;
2222 foreach my $parent (sort keys %$h) {
2223 foreach my $path (sort @{$h->{$parent}}) {
2225 uri_encode("$parent/$path"), "\n"
2227 warn "W: $t: $parent/$path ",
2228 "Insufficient permissions?\n";
2234 sub make_log_entry {
2235 my ($self, $rev, $parents, $untracked) = @_;
2236 my $rp = $self->ra->rev_proplist($rev);
2237 my %log_entry = ( parents => $parents || [], revision => $rev,
2238 revprops => $rp, log => '');
2239 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
2240 $self->write_untracked($rev, $un, $untracked);
2241 foreach (sort keys %$rp) {
2243 if (/^svn:(author|date|log)$/) {
2244 $log_entry{$1} = $v;
2246 print $un " rev_prop: ", uri_encode($_), ' ',
2247 uri_encode($v), "\n";
2250 close $un or croak $!;
2251 $log_entry{date} = parse_svn_date($log_entry{date});
2252 $log_entry{author} = check_author($log_entry{author});
2253 $log_entry{log} .= "\n";
2258 my ($self, @parents) = @_;
2259 my ($last_rev, $last_commit) = $self->last_rev_commit;
2260 my ($base, $head) = $self->parse_revision($last_rev);
2261 return if ($base > $head);
2262 if (defined $last_commit) {
2263 $self->assert_index_clean($last_commit);
2266 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
2267 my $err_handler = $SVN::Error::handler;
2268 $SVN::Error::handler = \&skip_unknown_revs;
2271 $self->ra->get_log([''], $min, $max, 0, 1, 1, sub {
2272 my ($paths, $rev, $author, $date, $msg) = @_;
2273 push @revs, $rev });
2275 my $log_entry = $self->do_fetch(undef, $_);
2276 $self->do_git_commit($log_entry, @parents);
2278 last if $max >= $head;
2281 $max = $head if ($max > $head);
2283 $SVN::Error::handler = $err_handler;
2287 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2288 # TODO: enable and test optimized commits:
2289 if (0 && $rev == ($self->{last_rev} + 1)) {
2290 $log_entry->{revision} = $rev;
2291 $log_entry->{author} = $author;
2292 $self->do_git_commit($log_entry, "$rev=$tree");
2294 $self->fetch("$rev=$tree");
2299 my ($self, $tree) = (shift, shift);
2300 my $log_entry = get_commit_entry($tree);
2301 unless ($self->{last_rev}) {
2302 fatal("Must have an existing revision to commit\n");
2304 my $pool = SVN::Pool->new;
2305 my $ed = SVN::Git::Editor->new({ r => $self->{last_rev},
2306 ra => $self->ra->dup,
2308 svn_path => $self->ra->{svn_path}
2310 $self->ra->get_commit_editor(
2311 $log_entry->{log}, sub {
2312 $self->set_tree_cb($log_entry,
2316 my $mods = $ed->apply_diff($self->{last_commit}, $tree);
2318 print "No changes\nr$self->{last_rev} = $tree\n";
2323 sub skip_unknown_revs {
2325 my $errno = $err->apr_err();
2326 # Maybe the branch we're tracking didn't
2327 # exist when the repo started, so it's
2328 # not an error if it doesn't, just continue
2330 # Wonderfully consistent library, eh?
2331 # 160013 - svn:// and file://
2332 # 175002 - http(s)://
2333 # 175007 - http(s):// (this repo required authorization, too...)
2334 # More codes may be discovered later...
2335 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2338 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2342 # Tie::File seems to be prone to offset errors if revisions get sparse,
2343 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2344 # one of my favorite modules is out :< Next up would be one of the DBM
2345 # modules, but I'm not sure which is most portable... So I'll just
2346 # go with something that's plain-text, but still capable of
2347 # being randomly accessed. So here's my ultra-simple fixed-width
2348 # database. All records are 40 characters + "\n", so it's easy to seek
2349 # to a revision: (41 * rev) is the byte offset.
2350 # A record of 40 0s denotes an empty revision.
2351 # And yes, it's still pretty fast (faster than Tie::File).
2354 my ($self, $rev, $commit) = @_;
2355 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
2356 open my $fh, '+<', $self->{db_path} or croak $!;
2357 my $offset = $rev * 41;
2358 # assume that append is the common case:
2359 seek $fh, 0, 2 or croak $!;
2361 if ($pos < $offset) {
2362 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41)
2365 seek $fh, $offset, 0 or croak $!;
2366 print $fh $commit,"\n" or croak $!;
2367 close $fh or croak $!;
2371 my ($self, $rev) = @_;
2373 my $offset = $rev * 41;
2374 open my $fh, '<', $self->{db_path} or croak $!;
2375 if (seek $fh, $offset, 0) {
2376 $ret = readline $fh;
2379 $ret = undef if ($ret =~ /^0{40}$/);
2382 close $fh or croak $!;
2387 my ($class, $id) = @_;
2388 $id ||= $Git::SVN::default;
2389 my $dir = "$ENV{GIT_DIR}/svn/$id";
2390 bless { id => $id, dir => $dir, index => "$dir/index",
2391 db_path => "$dir/.rev_db" }, $class;
2395 package Git::SVN::Prompt;
2399 use vars qw/$_no_auth_cache $_username/;
2402 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2403 $may_save = undef if $_no_auth_cache;
2404 $default_username = $_username if defined $_username;
2405 if (defined $default_username && length $default_username) {
2406 if (defined $realm && length $realm) {
2407 print STDERR "Authentication realm: $realm\n";
2410 $cred->username($default_username);
2412 username($cred, $realm, $may_save, $pool);
2414 $cred->password(_read_password("Password for '" .
2415 $cred->username . "': ", $realm));
2416 $cred->may_save($may_save);
2417 $SVN::_Core::SVN_NO_ERROR;
2420 sub ssl_server_trust {
2421 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2422 $may_save = undef if $_no_auth_cache;
2423 print STDERR "Error validating server certificate for '$realm':\n";
2424 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
2425 print STDERR " - The certificate is not issued by a trusted ",
2426 "authority. Use the\n",
2427 " fingerprint to validate the certificate manually!\n";
2429 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
2430 print STDERR " - The certificate hostname does not match.\n";
2432 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
2433 print STDERR " - The certificate is not yet valid.\n";
2435 if ($failures & $SVN::Auth::SSL::EXPIRED) {
2436 print STDERR " - The certificate has expired.\n";
2438 if ($failures & $SVN::Auth::SSL::OTHER) {
2439 print STDERR " - The certificate has an unknown error.\n";
2442 "Certificate information:\n".
2443 " - Hostname: %s\n".
2444 " - Valid: from %s until %s\n".
2446 " - Fingerprint: %s\n",
2447 map $cert_info->$_, qw(hostname valid_from valid_until
2448 issuer_dname fingerprint);
2451 print STDERR $may_save ?
2452 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2453 "(R)eject or accept (t)emporarily? ";
2455 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2456 if ($choice =~ /^t$/i) {
2457 $cred->may_save(undef);
2458 } elsif ($choice =~ /^r$/i) {
2460 } elsif ($may_save && $choice =~ /^p$/i) {
2461 $cred->may_save($may_save);
2465 $cred->accepted_failures($failures);
2466 $SVN::_Core::SVN_NO_ERROR;
2469 sub ssl_client_cert {
2470 my ($cred, $realm, $may_save, $pool) = @_;
2471 $may_save = undef if $_no_auth_cache;
2472 print STDERR "Client certificate filename: ";
2474 chomp(my $filename = <STDIN>);
2475 $cred->cert_file($filename);
2476 $cred->may_save($may_save);
2477 $SVN::_Core::SVN_NO_ERROR;
2480 sub ssl_client_cert_pw {
2481 my ($cred, $realm, $may_save, $pool) = @_;
2482 $may_save = undef if $_no_auth_cache;
2483 $cred->password(_read_password("Password: ", $realm));
2484 $cred->may_save($may_save);
2485 $SVN::_Core::SVN_NO_ERROR;
2489 my ($cred, $realm, $may_save, $pool) = @_;
2490 $may_save = undef if $_no_auth_cache;
2491 if (defined $realm && length $realm) {
2492 print STDERR "Authentication realm: $realm\n";
2495 if (defined $_username) {
2496 $username = $_username;
2498 print STDERR "Username: ";
2500 chomp($username = <STDIN>);
2502 $cred->username($username);
2503 $cred->may_save($may_save);
2504 $SVN::_Core::SVN_NO_ERROR;
2507 sub _read_password {
2508 my ($prompt, $realm) = @_;
2509 print STDERR $prompt;
2511 require Term::ReadKey;
2512 Term::ReadKey::ReadMode('noecho');
2514 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2515 last if $key =~ /[\012\015]/; # \n\r
2518 Term::ReadKey::ReadMode('restore');
2528 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2535 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
2539 sub libsvn_log_entry {
2540 my ($rev, $author, $date, $msg, $parents, $untracked) = @_;
2541 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2542 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2543 or die "Unable to parse date: $date\n";
2544 if (defined $author && length $author > 0 &&
2545 defined $_authors && ! defined $users{$author}) {
2546 die "Author: $author not defined in $_authors file\n";
2548 $msg = '' if ($rev == 0 && !defined $msg);
2550 open my $un, '>>', "$GIT_SVN_DIR/unhandled.log" or croak $!;
2552 print $un "r$rev\n" or croak $!;
2553 $h = $untracked->{empty};
2554 foreach (sort keys %$h) {
2555 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
2556 print $un " $act: ", uri_encode($_), "\n" or croak $!;
2557 warn "W: $act: $_\n";
2559 foreach my $t (qw/dir_prop file_prop/) {
2560 $h = $untracked->{$t} or next;
2561 foreach my $path (sort keys %$h) {
2562 my $ppath = $path eq '' ? '.' : $path;
2563 foreach my $prop (sort keys %{$h->{$path}}) {
2564 next if $SKIP{$prop};
2565 my $v = $h->{$path}->{$prop};
2568 uri_encode($ppath), ' ',
2569 uri_encode($prop), ' ',
2570 uri_encode($v), "\n"
2574 uri_encode($ppath), ' ',
2575 uri_encode($prop), "\n"
2581 foreach my $t (qw/absent_file absent_directory/) {
2582 $h = $untracked->{$t} or next;
2583 foreach my $parent (sort keys %$h) {
2584 foreach my $path (sort @{$h->{$parent}}) {
2586 uri_encode("$parent/$path"), "\n"
2588 warn "W: $t: $parent/$path ",
2589 "Insufficient permissions?\n";
2594 # revprops (make this optional? it's an extra network trip...)
2595 my $rp = $SVN->rev_proplist($rev);
2596 foreach (sort keys %$rp) {
2597 next if /^svn:(?:author|date|log)$/;
2598 print $un " rev_prop: ", uri_encode($_), ' ',
2599 uri_encode($rp->{$_}), "\n";
2601 close $un or croak $!;
2603 { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2604 author => $author, msg => $msg."\n", parents => $parents || [],
2609 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2610 my $ed = SVN::Git::Fetcher->new({ c => $last_commit, q => $_q });
2611 my (undef, $last_rev, undef) = cmt_metadata($last_commit);
2612 unless ($SVN->gs_do_update($last_rev, $rev, '', 1, $ed)) {
2613 die "SVN connection failed somewhere...\n";
2615 libsvn_log_entry($rev, $author, $date, $msg, [$last_commit], $ed);
2618 sub svn_grab_base_rev {
2619 my $c = eval { command_oneline([qw/rev-parse --verify/,
2620 "refs/remotes/$GIT_SVN^0"],
2622 if (defined $c && length $c) {
2623 my ($url, $rev, $uuid) = cmt_metadata($c);
2624 return ($rev, $c) if defined $rev;
2626 if ($_no_metadata) {
2627 my $offset = -41; # from tail
2629 open my $fh, '<', $REVDB or
2630 die "--no-metadata specified and $REVDB not readable\n";
2631 seek $fh, $offset, 2;
2633 defined $rl or return (undef, undef);
2635 while ($c ne $rl && tell $fh != 0) {
2637 seek $fh, $offset, 2;
2639 defined $rl or return (undef, undef);
2643 croak $! if ($rev < -1);
2644 $rev = ($rev - 41) / 41;
2645 close $fh or croak $!;
2648 return (undef, undef);
2651 sub libsvn_parse_revision {
2653 my $head = $SVN->get_latest_revnum();
2654 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2655 return ($base + 1, $head) if (defined $base);
2658 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2659 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2660 if ($_revision =~ /^BASE:(\d+)$/) {
2661 return ($base + 1, $1) if (defined $base);
2664 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2665 die "revision argument: $_revision not understood by git-svn\n",
2666 "Try using the command-line svn client instead\n";
2669 sub libsvn_traverse_ignore {
2670 my ($fh, $path, $r) = @_;
2672 my ($dirent, undef, $props) = $SVN->get_dir($path, $r);
2674 $p =~ s#^\Q$SVN->{svn_path}\E/##;
2675 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2676 if (my $s = $props->{'svn:ignore'}) {
2677 $s =~ s/[\r\n]+/\n/g;
2679 if (length $p == 0) {
2683 $s =~ s#\n#\n/$p/#g;
2684 print $fh "/$p/$s\n";
2687 foreach (sort keys %$dirent) {
2688 next if $dirent->{$_}->kind != $SVN::Node::dir;
2689 libsvn_traverse_ignore($fh, "$path/$_", $r);
2694 my ($path, $r0, $r1) = @_;
2695 return 1 if $r0 == $r1;
2697 # should be OK to use Pool here (r1 - r0) should be small
2698 $SVN->get_log([$path], $r0, $r1, 0, 0, 1, sub {$nr++});
2699 return 0 if ($nr > 1);
2703 sub libsvn_find_parent_branch {
2704 my ($paths, $rev, $author, $date, $msg) = @_;
2705 my $svn_path = '/'.$SVN->{svn_path};
2707 # look for a parent from another branch:
2708 my $i = $paths->{$svn_path} or return;
2709 my $branch_from = $i->copyfrom_path or return;
2710 my $r = $i->copyfrom_rev;
2711 print STDERR "Found possible branch point: ",
2712 "$branch_from => $svn_path, $r\n";
2713 $branch_from =~ s#^/##;
2715 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2716 my $url = $SVN->{repos_root};
2717 defined $l_map->{$url} or return;
2718 my $id = $l_map->{$url}->{$branch_from};
2719 if (!defined $id && $_follow_parent) {
2720 print STDERR "Following parent: $branch_from\@$r\n";
2721 # auto create a new branch and follow it
2722 $id = basename($branch_from);
2723 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2724 while (-r "$GIT_DIR/svn/$id") {
2725 # just grow a tail if we're not unique enough :x
2729 return unless defined $id;
2731 my ($r0, $parent) = find_rev_before($r,$id,1);
2732 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2733 defined(my $pid = fork) or croak $!;
2735 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2737 $SVN_URL = "$url/$branch_from";
2740 # we can't assume SVN_URL exists at r+1:
2741 $_revision = "0:$r";
2747 ($r0, $parent) = find_rev_before($r,$id,1);
2749 return unless (defined $r0 && defined $parent);
2750 if (revisions_eq($branch_from, $r0, $r)) {
2751 unlink $GIT_SVN_INDEX;
2752 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2753 command_noisy('read-tree', $parent);
2754 unless ($SVN->can_do_switch) {
2755 return _libsvn_new_tree($paths, $rev, $author, $date,
2758 # do_switch works with svn/trunk >= r22312, but that is not
2759 # included with SVN 1.4.2 (the latest version at the moment),
2760 # so we can't rely on it.
2761 my $ra = Git::SVN::Ra->new("$url/$branch_from");
2762 my $ed = SVN::Git::Fetcher->new({c => $parent, q => $_q });
2763 $ra->gs_do_switch($r0, $rev, '', 1, $SVN->{url}, $ed) or
2764 die "SVN connection failed somewhere...\n";
2765 return libsvn_log_entry($rev, $author, $date, $msg, [$parent]);
2767 print STDERR "Nope, branch point not imported or unknown\n";
2771 sub libsvn_new_tree {
2772 if (my $log_entry = libsvn_find_parent_branch(@_)) {
2775 my ($paths, $rev, $author, $date, $msg) = @_; # $pool is last
2776 _libsvn_new_tree($paths, $rev, $author, $date, $msg, []);
2779 sub _libsvn_new_tree {
2780 my ($paths, $rev, $author, $date, $msg, $parents) = @_;
2781 my $ed = SVN::Git::Fetcher->new({q => $_q});
2782 unless ($SVN->gs_do_update($rev, $rev, '', 1, $ed)) {
2783 die "SVN connection failed somewhere...\n";
2785 libsvn_log_entry($rev, $author, $date, $msg, $parents, $ed);
2788 sub find_graft_path_commit {
2789 my ($tree_paths, $p1, $r1) = @_;
2790 foreach my $x (keys %$tree_paths) {
2791 next unless ($p1 =~ /^\Q$x\E/);
2792 my $i = $tree_paths->{$x};
2793 my ($r0, $parent) = find_rev_before($r1,$i,1);
2794 return $parent if (defined $r0 && $r0 == $r1);
2795 print STDERR "r$r1 of $i not imported\n";
2801 sub find_graft_path_parents {
2802 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2803 foreach my $x (keys %$tree_paths) {
2804 next unless ($p0 =~ /^\Q$x\E/);
2805 my $i = $tree_paths->{$x};
2806 my ($r, $parent) = find_rev_before($r0, $i, 1);
2807 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
2808 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
2809 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
2810 next if ($url_a && $url_b && $url_a eq $url_b &&
2811 $uuid_b eq $uuid_a);
2812 $grafts->{$c}->{$parent} = 1;
2817 sub libsvn_graft_file_copies {
2818 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2819 foreach (keys %$paths) {
2820 my $i = $paths->{$_};
2821 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2823 next unless (defined $p0 && defined $r0);
2828 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
2830 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
2835 my $old = $ENV{GIT_INDEX_FILE};
2836 $ENV{GIT_INDEX_FILE} = shift;
2843 $ENV{GIT_INDEX_FILE} = $old;
2845 delete $ENV{GIT_INDEX_FILE};
2849 sub libsvn_commit_cb {
2850 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2851 if ($_optimize_commits && $rev == ($r_last + 1)) {
2852 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
2853 $log->{tree} = get_tree_from_treeish($c);
2854 my $cmt = git_commit($log, $cmt_last, $c);
2855 my @diff = command('diff-tree', $cmt, $c);
2857 print STDERR "Trees differ: $cmt $c\n",
2858 join('',@diff),"\n";
2866 sub libsvn_ls_fullurl {
2867 my $fullurl = shift;
2868 my $ra = Git::SVN::Ra->new($fullurl);
2870 my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
2871 my ($dirent, undef, undef) = $ra->get_dir('', $r);
2872 foreach my $d (sort keys %$dirent) {
2873 if ($dirent->{$d}->kind == $SVN::Node::dir) {
2874 push @ret, "$d/"; # add '/' for compat with cli svn
2880 sub libsvn_skip_unknown_revs {
2882 my $errno = $err->apr_err();
2883 # Maybe the branch we're tracking didn't
2884 # exist when the repo started, so it's
2885 # not an error if it doesn't, just continue
2887 # Wonderfully consistent library, eh?
2888 # 160013 - svn:// and file://
2889 # 175002 - http(s)://
2890 # 175007 - http(s):// (this repo required authorization, too...)
2891 # More codes may be discovered later...
2892 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2895 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2898 # Tie::File seems to be prone to offset errors if revisions get sparse,
2899 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2900 # one of my favorite modules is out :< Next up would be one of the DBM
2901 # modules, but I'm not sure which is most portable... So I'll just
2902 # go with something that's plain-text, but still capable of
2903 # being randomly accessed. So here's my ultra-simple fixed-width
2904 # database. All records are 40 characters + "\n", so it's easy to seek
2905 # to a revision: (41 * rev) is the byte offset.
2906 # A record of 40 0s denotes an empty revision.
2907 # And yes, it's still pretty fast (faster than Tie::File).
2909 my ($file, $rev, $commit) = @_;
2910 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
2911 open my $fh, '+<', $file or croak $!;
2912 my $offset = $rev * 41;
2913 # assume that append is the common case:
2914 seek $fh, 0, 2 or croak $!;
2916 if ($pos < $offset) {
2917 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
2919 seek $fh, $offset, 0 or croak $!;
2920 print $fh $commit,"\n";
2921 close $fh or croak $!;
2925 my ($file, $rev) = @_;
2927 my $offset = $rev * 41;
2928 open my $fh, '<', $file or croak $!;
2929 seek $fh, $offset, 0;
2930 if (tell $fh == $offset) {
2931 $ret = readline $fh;
2934 $ret = undef if ($ret =~ /^0{40}$/);
2937 close $fh or croak $!;
2941 sub copy_remote_ref {
2942 my $origin = $_cp_remote ? $_cp_remote : 'origin';
2943 my $ref = "refs/remotes/$GIT_SVN";
2944 if (command('ls-remote', $origin, $ref)) {
2945 command_noisy('fetch', $origin, "$ref:$ref");
2946 } elsif ($_cp_remote && !$_upgrade) {
2947 die "Unable to find remote reference: ",
2948 "refs/remotes/$GIT_SVN on $origin\n";
2953 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2954 $SVN::Node::dir.$SVN::Node::unknown.
2955 $SVN::Node::none.$SVN::Node::file.
2956 $SVN::Node::dir.$SVN::Node::unknown.
2957 $SVN::Auth::SSL::CNMISMATCH.
2958 $SVN::Auth::SSL::NOTYETVALID.
2959 $SVN::Auth::SSL::EXPIRED.
2960 $SVN::Auth::SSL::UNKNOWNCA.
2961 $SVN::Auth::SSL::OTHER;
2964 package SVN::Git::Fetcher;
2971 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2973 my ($class, $git_svn) = @_;
2974 my $self = SVN::Delta::Editor->new;
2975 bless $self, $class;
2976 $self->{c} = $git_svn->{c} if exists $git_svn->{c};
2977 $self->{q} = $git_svn->{q};
2978 $self->{empty} = {};
2979 $self->{dir_prop} = {};
2980 $self->{file_prop} = {};
2981 $self->{absent_dir} = {};
2982 $self->{absent_file} = {};
2983 ($self->{gui}, $self->{ctx}) = command_input_pipe(
2984 qw/update-index -z --index-info/);
2985 require Digest::MD5;
2993 sub open_directory {
2994 my ($self, $path, $pb, $rev) = @_;
2999 my ($self, $path, $rev, $pb) = @_;
3000 my $gui = $self->{gui};
3002 # remove entire directories.
3003 if (command('ls-tree', $self->{c}, '--', $path) =~ /^040000 tree/) {
3004 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3006 $self->{c}, '--', $path);
3009 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
3010 print "\tD\t$_\n" unless $self->{q};
3012 print "\tD\t$path/\n" unless $self->{q};
3013 command_close_pipe($ls, $ctx);
3014 $self->{empty}->{$path} = 0
3016 print $gui '0 ',0 x 40,"\t",$path,"\0" or croak $!;
3017 print "\tD\t$path\n" unless $self->{q};
3023 my ($self, $path, $pb, $rev) = @_;
3024 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--',$path)
3025 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
3026 unless (defined $mode && defined $blob) {
3027 die "$path was not found in commit $self->{c} (r$rev)\n";
3029 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
3030 pool => SVN::Pool->new, action => 'M' };
3034 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3035 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3036 delete $self->{empty}->{$dir};
3037 { path => $path, mode_a => 100644, mode_b => 100644,
3038 pool => SVN::Pool->new, action => 'A' };
3042 my ($self, $path, $cp_path, $cp_rev) = @_;
3043 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3044 delete $self->{empty}->{$dir};
3045 $self->{empty}->{$path} = 1;
3049 sub change_dir_prop {
3050 my ($self, $db, $prop, $value) = @_;
3051 $self->{dir_prop}->{$db->{path}} ||= {};
3052 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3056 sub absent_directory {
3057 my ($self, $path, $pb) = @_;
3058 $self->{absent_dir}->{$pb->{path}} ||= [];
3059 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3064 my ($self, $path, $pb) = @_;
3065 $self->{absent_file}->{$pb->{path}} ||= [];
3066 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3070 sub change_file_prop {
3071 my ($self, $fb, $prop, $value) = @_;
3072 if ($prop eq 'svn:executable') {
3073 if ($fb->{mode_b} != 120000) {
3074 $fb->{mode_b} = defined $value ? 100755 : 100644;
3076 } elsif ($prop eq 'svn:special') {
3077 $fb->{mode_b} = defined $value ? 120000 : 100644;
3079 $self->{file_prop}->{$fb->{path}} ||= {};
3080 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
3085 sub apply_textdelta {
3086 my ($self, $fb, $exp) = @_;
3087 my $fh = IO::File->new_tmpfile;
3089 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3090 # (but $base does not,) so dup() it for reading in close_file
3091 open my $dup, '<&', $fh or croak $!;
3092 my $base = IO::File->new_tmpfile;
3093 $base->autoflush(1);
3095 defined (my $pid = fork) or croak $!;
3097 open STDOUT, '>&', $base or croak $!;
3098 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
3099 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
3105 seek $base, 0, 0 or croak $!;
3106 my $md5 = Digest::MD5->new;
3107 $md5->addfile($base);
3108 my $got = $md5->hexdigest;
3109 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
3111 " got: $got\n" if ($got ne $exp);
3114 seek $base, 0, 0 or croak $!;
3116 $fb->{base} = $base;
3117 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
3121 my ($self, $fb, $exp) = @_;
3123 my $path = $fb->{path};
3124 if (my $fh = $fb->{fh}) {
3125 seek($fh, 0, 0) or croak $!;
3126 my $md5 = Digest::MD5->new;
3128 my $got = $md5->hexdigest;
3129 die "Checksum mismatch: $path\n",
3130 "expected: $exp\n got: $got\n" if ($got ne $exp);
3131 seek($fh, 0, 0) or croak $!;
3132 if ($fb->{mode_b} == 120000) {
3133 read($fh, my $buf, 5) == 5 or croak $!;
3134 $buf eq 'link ' or die "$path has mode 120000",
3135 "but is not a link\n";
3137 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
3139 open STDIN, '<&', $fh or croak $!;
3140 exec qw/git-hash-object -w --stdin/ or croak $!;
3142 chomp($hash = do { local $/; <$out> });
3143 close $out or croak $!;
3144 close $fh or croak $!;
3145 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3146 close $fb->{base} or croak $!;
3148 $hash = $fb->{blob} or die "no blob information\n";
3151 my $gui = $self->{gui};
3152 print $gui "$fb->{mode_b} $hash\t$path\0" or croak $!;
3153 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $self->{q};
3159 eval { command_close_pipe($self->{gui}, $self->{ctx}) };
3160 $self->SUPER::abort_edit(@_);
3165 command_close_pipe($self->{gui}, $self->{ctx});
3166 $self->{git_commit_ok} = 1;
3167 $self->SUPER::close_edit(@_);
3170 package SVN::Git::Editor;
3179 my $git_svn = shift;
3180 my $self = SVN::Delta::Editor->new(@_);
3181 bless $self, $class;
3182 foreach (qw/svn_path c r ra /) {
3183 die "$_ required!\n" unless (defined $git_svn->{$_});
3184 $self->{$_} = $git_svn->{$_};
3186 $self->{pool} = SVN::Pool->new;
3187 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3189 require Digest::MD5;
3194 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3198 (defined $_[1] && length $_[1]) ? $_[1] : ''
3202 my ($self, $path) = @_;
3203 $self->{ra}->{url} . '/' . $self->repo_path($path);
3207 my ($self, $q) = @_;
3208 my $rm = $self->{rm};
3209 delete $rm->{''}; # we never delete the url we're tracking
3212 foreach (keys %$rm) {
3213 my @d = split m#/#, $_;
3217 $c .= '/' . shift @d;
3221 delete $rm->{$self->{svn_path}};
3222 delete $rm->{''}; # we never delete the url we're tracking
3225 my ($fh, $ctx) = command_output_pipe(
3226 qw/ls-tree --name-only -r -z/, $self->{c});
3230 my @dn = split m#/#, $_;
3232 delete $rm->{join '/', @dn};
3239 command_close_pipe($fh, $ctx);
3241 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3242 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3243 $self->close_directory($bat->{$d}, $p);
3244 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
3245 print "\tD+\t$d/\n" unless $q;
3246 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3251 sub open_or_add_dir {
3252 my ($self, $full_path, $baton) = @_;
3253 my $t = $self->{ra}->check_path($full_path, $self->{r});
3254 if ($t == $SVN::Node::none) {
3255 return $self->add_directory($full_path, $baton,
3256 undef, -1, $self->{pool});
3257 } elsif ($t == $SVN::Node::dir) {
3258 return $self->open_directory($full_path, $baton,
3259 $self->{r}, $self->{pool});
3261 print STDERR "$full_path already exists in repository at ",
3262 "r$self->{r} and it is not a directory (",
3263 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3268 my ($self, $path) = @_;
3269 my $bat = $self->{bat};
3270 $path = $self->repo_path($path);
3271 return $bat->{''} unless (length $path);
3272 my @p = split m#/+#, $path;
3274 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3277 $c .= '/' . shift @p;
3278 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3284 my ($self, $m, $q) = @_;
3285 my ($dir, $file) = split_path($m->{file_b});
3286 my $pbat = $self->ensure_path($dir);
3287 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3289 print "\tA\t$m->{file_b}\n" unless $q;
3290 $self->chg_file($fbat, $m);
3291 $self->close_file($fbat,undef,$self->{pool});
3295 my ($self, $m, $q) = @_;
3296 my ($dir, $file) = split_path($m->{file_b});
3297 my $pbat = $self->ensure_path($dir);
3298 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3299 $self->url_path($m->{file_a}), $self->{r});
3300 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3301 $self->chg_file($fbat, $m);
3302 $self->close_file($fbat,undef,$self->{pool});
3306 my ($self, $path, $pbat) = @_;
3307 my $rpath = $self->repo_path($path);
3308 my ($dir, $file) = split_path($rpath);
3309 $self->{rm}->{$dir} = 1;
3310 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3314 my ($self, $m, $q) = @_;
3315 my ($dir, $file) = split_path($m->{file_b});
3316 my $pbat = $self->ensure_path($dir);
3317 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3318 $self->url_path($m->{file_a}), $self->{r});
3319 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3320 $self->chg_file($fbat, $m);
3321 $self->close_file($fbat,undef,$self->{pool});
3323 ($dir, $file) = split_path($m->{file_a});
3324 $pbat = $self->ensure_path($dir);
3325 $self->delete_entry($m->{file_a}, $pbat);
3329 my ($self, $m, $q) = @_;
3330 my ($dir, $file) = split_path($m->{file_b});
3331 my $pbat = $self->ensure_path($dir);
3332 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3333 $pbat,$self->{r},$self->{pool});
3334 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3335 $self->chg_file($fbat, $m);
3336 $self->close_file($fbat,undef,$self->{pool});
3339 sub T { shift->M(@_) }
3341 sub change_file_prop {
3342 my ($self, $fbat, $pname, $pval) = @_;
3343 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3347 my ($self, $fbat, $m) = @_;
3348 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3349 $self->change_file_prop($fbat,'svn:executable','*');
3350 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3351 $self->change_file_prop($fbat,'svn:executable',undef);
3353 my $fh = IO::File->new_tmpfile or croak $!;
3354 if ($m->{mode_b} =~ /^120/) {
3355 print $fh 'link ' or croak $!;
3356 $self->change_file_prop($fbat,'svn:special','*');
3357 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3358 $self->change_file_prop($fbat,'svn:special',undef);
3360 defined(my $pid = fork) or croak $!;
3362 open STDOUT, '>&', $fh or croak $!;
3363 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3367 $fh->flush == 0 or croak $!;
3368 seek $fh, 0, 0 or croak $!;
3370 my $md5 = Digest::MD5->new;
3371 $md5->addfile($fh) or croak $!;
3372 seek $fh, 0, 0 or croak $!;
3374 my $exp = $md5->hexdigest;
3375 my $pool = SVN::Pool->new;
3376 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3377 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
3378 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3381 close $fh or croak $!;
3385 my ($self, $m, $q) = @_;
3386 my ($dir, $file) = split_path($m->{file_b});
3387 my $pbat = $self->ensure_path($dir);
3388 print "\tD\t$m->{file_b}\n" unless $q;
3389 $self->delete_entry($m->{file_b}, $pbat);
3394 my ($p,$bat) = ($self->{pool}, $self->{bat});
3395 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3396 $self->close_directory($bat->{$_}, $p);
3398 $self->SUPER::close_edit($p);
3404 $self->SUPER::abort_edit($self->{pool});
3405 $self->{pool}->clear;
3408 package Git::SVN::Ra;
3409 use vars qw/@ISA $config_dir/;
3412 my ($can_do_switch);
3415 # enforce temporary pool usage for some simple functions
3417 foreach (qw/get_latest_revnum rev_proplist get_file
3418 check_path get_dir get_uuid get_repos_root/) {
3421 my \$pool = SVN::Pool->new;
3422 my \@ret = \$self->SUPER::$_(\@_,\$pool);
3424 wantarray ? \@ret : \$ret[0]; }\n";
3430 my ($class, $url) = @_;
3431 SVN::_Core::svn_config_ensure($config_dir, undef);
3432 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
3433 SVN::Client::get_simple_provider(),
3434 SVN::Client::get_ssl_server_trust_file_provider(),
3435 SVN::Client::get_simple_prompt_provider(
3436 \&Git::SVN::Prompt::simple, 2),
3437 SVN::Client::get_ssl_client_cert_prompt_provider(
3438 \&Git::SVN::Prompt::ssl_client_cert, 2),
3439 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
3440 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
3441 SVN::Client::get_username_provider(),
3442 SVN::Client::get_ssl_server_trust_prompt_provider(
3443 \&Git::SVN::Prompt::ssl_server_trust),
3444 SVN::Client::get_username_prompt_provider(
3445 \&Git::SVN::Prompt::username, 2),
3447 my $config = SVN::Core::config_get_config($config_dir);
3448 my $self = SVN::Ra->new(url => $url, auth => $baton,
3450 pool => SVN::Pool->new,
3451 auth_provider_callbacks => $callbacks);
3452 $self->{svn_path} = $url;
3453 $self->{repos_root} = $self->get_repos_root;
3454 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E/*##;
3455 bless $self, $class;
3460 $self->{pool}->clear if $self->{pool};
3461 $self->SUPER::DESTROY(@_);
3466 my $dup = SVN::Ra->new(pool => SVN::Pool->new,
3467 map { $_ => $self->{$_} } qw/config url
3468 auth auth_provider_callbacks repos_root svn_path/);
3469 bless $dup, ref $self;
3473 my ($self, @args) = @_;
3474 my $pool = SVN::Pool->new;
3475 $args[4]-- if $args[4] && ! $::_follow_parent;
3476 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
3477 my $ret = $self->SUPER::get_log(@args, $pool);
3482 sub get_commit_editor {
3483 my ($self, $msg, $cb, $pool) = @_;
3484 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
3485 $self->SUPER::get_commit_editor($msg, $cb, @lock, $pool);
3490 $self->{uuid} ||= $self->get_uuid;
3494 my ($self, $rev_a, $rev_b, $path, $recurse, $editor) = @_;
3495 my $pool = SVN::Pool->new;
3496 my $reporter = $self->do_update($rev_b, $path, $recurse,
3498 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
3499 my $new = ($rev_a == $rev_b);
3500 $reporter->set_path($path, $rev_a, $new, @lock, $pool);
3501 $reporter->finish_report($pool);
3503 $editor->{git_commit_ok};
3507 my ($self, $rev_a, $rev_b, $path, $recurse, $url_b, $editor) = @_;
3508 my $pool = SVN::Pool->new;
3509 my $reporter = $self->do_switch($rev_b, $path, $recurse,
3510 $url_b, $editor, $pool);
3511 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
3512 $reporter->set_path($path, $rev_a, 0, @lock, $pool);
3513 $reporter->finish_report($pool);
3515 $editor->{git_commit_ok};
3520 unless (defined $can_do_switch) {
3521 my $pool = SVN::Pool->new;
3523 $self->do_switch(1, '', 0, $self->{url},
3524 SVN::Delta::Editor->new, $pool);
3529 $rep->abort_report($pool);
3541 $log_msg hashref as returned by libsvn_log_entry()
3543 msg => 'whitespace-formatted log entry
3544 ', # trailing newline is preserved
3545 revision => '8', # integer
3546 date => '2004-02-24T17:01:44.108345Z', # commit date
3547 author => 'committer name'
3550 @mods = array of diff-index line hashes, each element represents one line
3551 of diff-index output
3553 diff-index line ($m hash)
3555 mode_a => first column of diff-index output, no leading ':',
3556 mode_b => second column of diff-index output,
3557 sha1_b => sha1sum of the final blob,
3558 chg => change type [MCRADT],
3559 file_a => original file name of a file (iff chg is 'C' or 'R')
3560 file_b => new/current file name of a file (any chg)
3564 # retval of read_url_paths{,_all}();
3566 # repository root url
3567 'https://svn.musicpd.org' => {
3568 # repository path # GIT_SVN_ID
3569 'mpd/trunk' => 'trunk',
3570 'mpd/tags/0.11.5' => 'tags/0.11.5',
3575 I don't trust the each() function on unless I created %hash myself
3576 because the internal iterator may not have started at base.