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 $SVN_UUID
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 @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
42 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
43 *SVN
::Git
::Fetcher
::process_rm
= *process_rm
;
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/;
52 use Git qw
/command command_oneline command_noisy
53 command_output_pipe command_input_pipe command_close_pipe
/;
54 memoize
('revisions_eq');
55 memoize
('cmt_metadata');
56 memoize
('get_commit_time');
60 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
61 my $sha1 = qr/[a-f\d]{40}/;
62 my $sha1_short = qr/[a-f\d]{4,40}/;
63 my $_esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
64 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
65 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
66 $_repack, $_repack_nr, $_repack_flags, $_q,
67 $_message, $_file, $_follow_parent, $_no_metadata,
68 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
69 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
70 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
71 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive,
72 $_username, $_config_dir, $_no_auth_cache,
73 $_pager, $_color, $_prefix);
74 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
75 my ($_svn_can_do_switch);
76 my @repo_path_split_cache;
78 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
79 'branch|b=s' => \
@_branch_from,
80 'follow-parent|follow' => \
$_follow_parent,
81 'branch-all-refs|B' => \
$_branch_all_refs,
82 'authors-file|A=s' => \
$_authors,
83 'repack:i' => \
$_repack,
84 'no-metadata' => \
$_no_metadata,
86 'username=s' => \
$_username,
87 'config-dir=s' => \
$_config_dir,
88 'no-auth-cache' => \
$_no_auth_cache,
89 'ignore-nodate' => \
$_ignore_nodate,
90 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
92 my ($_trunk, $_tags, $_branches);
93 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
94 'tags|t=s' => \
$_tags,
95 'branches|b=s' => \
$_branches );
96 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
97 my %cmt_opts = ( 'edit|e' => \
$_edit,
99 'find-copies-harder' => \
$_find_copies_harder,
101 'copy-similarity|C=i'=> \
$_cp_similarity
105 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
106 { 'revision|r=s' => \
$_revision, %fc_opts } ],
107 init
=> [ \
&init
, "Initialize a repo for tracking" .
108 " (requires URL argument)",
110 dcommit
=> [ \
&dcommit
, 'Commit several diffs to merge with upstream',
111 { 'merge|m|M' => \
$_merge,
112 'strategy|s=s' => \
$_strategy,
113 'dry-run|n' => \
$_dry_run,
114 %cmt_opts, %fc_opts } ],
115 'set-tree' => [ \
&commit
, "Set an SVN repository to a git tree-ish",
116 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
117 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings",
118 { 'revision|r=i' => \
$_revision } ],
119 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
120 { 'no-ignore-externals' => \
$_no_ignore_ext,
121 'copy-remote|remote=s' => \
$_cp_remote,
122 'upgrade' => \
$_upgrade } ],
123 'graft-branches' => [ \
&graft_branches
,
124 'Detect merges/branches from already imported history',
125 { 'merge-rx|m' => \
@_opt_m,
126 'branch|b=s' => \
@_branch_from,
127 'branch-all-refs|B' => \
$_branch_all_refs,
128 'no-default-regex' => \
$_no_default_regex,
129 'no-graft-copy' => \
$_no_graft_copy } ],
130 'multi-init' => [ \
&multi_init
,
131 'Initialize multiple trees (like git-svnimport)',
132 { %multi_opts, %init_opts,
133 'revision|r=i' => \
$_revision,
134 'username=s' => \
$_username,
135 'config-dir=s' => \
$_config_dir,
136 'no-auth-cache' => \
$_no_auth_cache,
137 'prefix=s' => \
$_prefix,
139 'multi-fetch' => [ \
&multi_fetch
,
140 'Fetch multiple trees (like git-svnimport)',
142 'log' => [ \
&show_log
, 'Show commit logs',
143 { 'limit=i' => \
$_limit,
144 'revision|r=s' => \
$_revision,
145 'verbose|v' => \
$_verbose,
146 'incremental' => \
$_incremental,
147 'oneline' => \
$_oneline,
148 'show-commit' => \
$_show_commit,
149 'non-recursive' => \
$_non_recursive,
150 'authors-file|A=s' => \
$_authors,
152 'pager=s' => \
$_pager,
154 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
155 { 'message|m=s' => \
$_message,
156 'file|F=s' => \
$_file,
157 'revision|r=s' => \
$_revision,
162 for (my $i = 0; $i < @ARGV; $i++) {
163 if (defined $cmd{$ARGV[$i]}) {
170 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
172 read_repo_config
(\
%opts);
173 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
174 'version|V' => \
$_version,
175 'id|i=s' => \
$GIT_SVN);
176 exit 1 if (!$rv && $cmd ne 'log');
180 version
() if $_version;
181 usage
(1) unless defined $cmd;
183 load_authors
() if $_authors;
184 load_all_refs
() if $_branch_all_refs;
185 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
186 $cmd{$cmd}->[0]->(@ARGV);
189 ####################### primary functions ######################
191 my $exit = shift || 0;
192 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
194 git
-svn
- bidirectional operations between a single Subversion tree
and git
195 Usage
: $0 <command
> [options
] [arguments
]\n
197 print $fd "Available commands:\n" unless $cmd;
199 foreach (sort keys %cmd) {
200 next if $cmd && $cmd ne $_;
201 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
202 foreach (keys %{$cmd{$_}->[2]}) {
203 # prints out arguments as they should be passed:
204 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
205 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
207 split /\|/,$_)," $x\n";
211 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
212 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
213 one git repository and want to keep them separate. See git-svn(1) for more
220 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
225 if (!verify_ref("refs/remotes/$GIT_SVN^0")) {
228 $SVN_URL = shift or undef;
231 command_noisy('update
-ref',"refs/remotes/$GIT_SVN","
234 check_upgrade_needed();
237 my ($rev_list, $ctx) = command_output_pipe("rev-list",
238 "refs/remotes/$GIT_SVN");
240 while (<$rev_list>) {
243 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
244 my @commit = grep(/^git-svn-id: /,
245 command(qw/cat-file commit/, $c));
246 next if (!@commit); # skip merges
247 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
248 if (!defined $rev || !$uuid) {
249 croak "Unable to extract revision or UUID from ",
250 "$c, $commit[$#commit]\n";
253 # if we merged or otherwise started elsewhere, this is
254 # how we break out of it
255 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
256 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
258 unless (defined $latest) {
259 if (!$SVN_URL && !$url) {
260 croak "SVN repository location required: $url\n";
267 revdb_set($REVDB, $rev, $c);
268 print "r$rev = $c\n";
269 $newest_rev = $rev if ($rev > $newest_rev);
271 command_close_pipe($rev_list, $ctx);
275 my $url = shift or die "SVN repository location required " .
276 "as a command-line argument\n";
277 $url =~ s!/+$!!; # strip trailing slash
279 if (my $repo_path = shift) {
280 unless (-d $repo_path) {
281 mkpath([$repo_path]);
283 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
288 unless (-d $GIT_DIR) {
289 my @init_db = ('init
');
290 push @init_db, "--template=$_template" if defined $_template;
291 push @init_db, "--shared" if defined $_shared;
292 command_noisy(@init_db);
298 fetch_child_id($GIT_SVN, @_);
302 check_upgrade_needed();
303 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
304 my $ret = fetch_lib(@_);
305 if ($ret->{commit} && !verify_ref('refs
/heads/master
^0')) {
306 command_noisy(qw(update-ref refs/heads/master),$ret->{commit
});
313 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
314 $SVN ||= libsvn_connect
($SVN_URL);
315 my ($last_rev, $last_commit) = svn_grab_base_rev
();
316 my ($base, $head) = libsvn_parse_revision
($last_rev);
318 return { revision
=> $last_rev, commit
=> $last_commit }
320 my $index = set_index
($GIT_SVN_INDEX);
322 # limit ourselves and also fork() since get_log won't release memory
323 # after processing a revision and SVN stuff seems to leak
325 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
327 if (defined $last_commit) {
328 unless (-e
$GIT_SVN_INDEX) {
329 command_noisy
('read-tree', $last_commit);
331 my $x = command_oneline
('write-tree');
332 my ($y) = (command
(qw
/cat-file commit/, $last_commit)
333 =~ /^tree ($sha1)/m);
335 unlink $GIT_SVN_INDEX or croak
$!;
336 command_noisy
('read-tree', $last_commit);
338 $x = command_oneline
('write-tree');
340 print STDERR
"trees ($last_commit) $y != $x\n",
341 "Something is seriously wrong...\n";
345 # fork, because using SVN::Pool with get_log() still doesn't
346 # seem to help enough to keep memory usage down.
347 defined(my $pid = fork) or croak
$!;
349 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
351 # Yes I'm perfectly aware that the fourth argument
352 # below is the limit revisions number. Unfortunately
353 # performance sucks with it enabled, so it's much
354 # faster to fetch revision ranges instead of relying
356 libsvn_get_log
(libsvn_dup_ra
($SVN), [''],
361 $log_msg = libsvn_fetch
(
363 $last_commit = git_commit
(
368 $log_msg = libsvn_new_tree
(@_);
369 $last_commit = git_commit
(
377 ($last_rev, $last_commit) = svn_grab_base_rev
();
378 last if ($max >= $head);
381 $max = $head if ($max > $head);
382 $SVN = libsvn_connect
($SVN_URL);
384 restore_index
($index);
385 return { revision
=> $last_rev, commit
=> $last_commit };
390 check_upgrade_needed
();
391 if ($_stdin || !@commits) {
392 print "Reading from stdin...\n";
395 if (/\b($sha1_short)\b/o) {
396 unshift @commits, $1;
401 foreach my $c (@commits) {
402 my @tmp = command
('rev-parse',$c);
403 if (scalar @tmp == 1) {
405 } elsif (scalar @tmp > 1) {
406 push @revs, reverse(command
('rev-list',@tmp));
408 die "Failed to rev-parse $c\n";
412 print "Done committing ",scalar @revs," revisions to SVN\n";
417 my ($r_last, $cmt_last) = svn_grab_base_rev
();
418 defined $r_last or die "Must have an existing revision to commit\n";
419 my $fetched = fetch
();
420 if ($r_last != $fetched->{revision
}) {
421 print STDERR
"There are new revisions that were fetched ",
422 "and need to be merged (or acknowledged) ",
423 "before committing.\n",
424 "last rev: $r_last\n",
425 " current: $fetched->{revision}\n";
429 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
430 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
433 set_svn_commit_env
();
434 foreach my $c (@revs) {
435 my $log_msg = get_commit_message
($c, $commit_msg);
437 # fork for each commit because there's a memory leak I
438 # can't track down... (it's probably in the SVN code)
439 defined(my $pid = open my $fh, '-|') or croak
$!;
441 my $ed = SVN
::Git
::Editor
->new(
443 ra
=> libsvn_dup_ra
($SVN),
445 svn_path
=> $SVN->{svn_path
},
447 $SVN->get_commit_editor(
458 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
460 print "No changes\nr$r_last = $cmt_last\n";
467 my ($r_new, $cmt_new, $no);
471 if (/^r(\d+) = ($sha1)$/o) {
472 ($r_new, $cmt_new) = ($1, $2);
473 } elsif ($_ eq 'No changes') {
478 if (! defined $r_new && ! defined $cmt_new) {
480 die "Failed to parse revision information\n";
483 ($r_last, $cmt_last) = ($r_new, $cmt_new);
491 my $head = shift || 'HEAD';
492 my $gs = "refs/remotes/$GIT_SVN";
493 my @refs = command
(qw
/rev-list --no-merges/, "$gs..$head");
495 foreach my $d (reverse @refs) {
496 if (!verify_ref
("$d~1")) {
498 "has no parent commit, and therefore ",
499 "nothing to diff against.\n",
500 "You should be working from a repository ",
501 "originally created by git-svn\n";
503 unless (defined $last_rev) {
504 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
505 unless (defined $last_rev) {
506 die "Unable to extract revision information ",
507 "from commit $d~1\n";
511 print "diff-tree $d~1 $d\n";
513 if (my $r = commit_diff
("$d~1", $d, undef, $last_rev)) {
515 } # else: no changes, same $last_rev
520 my @diff = command
('diff-tree', 'HEAD', $gs, '--');
523 @finish = qw
/rebase/;
524 push @finish, qw
/--merge/ if $_merge;
525 push @finish, "--strategy=$_strategy" if $_strategy;
526 print STDERR
"W: HEAD and $gs differ, using @finish:\n", @diff;
528 print "No changes between current HEAD and $gs\n",
529 "Resetting to the latest $gs\n";
530 @finish = qw
/reset --mixed/;
532 command_noisy
(@finish, $gs);
536 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
538 $SVN ||= libsvn_connect
($SVN_URL);
539 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
540 libsvn_traverse_ignore
(\
*STDOUT
, '', $r);
544 my $gr_file = "$GIT_DIR/info/grafts";
545 my ($grafts, $comments) = read_grafts
($gr_file);
549 # temporarily disable our grafts file to make this idempotent
550 chomp($gr_sha1 = command
(qw
/hash-object -w/,$gr_file));
551 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
554 my $l_map = read_url_paths
();
555 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
556 unless ($_no_default_regex) {
557 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
558 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
559 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
561 foreach my $u (keys %$l_map) {
563 foreach my $p (keys %{$l_map->{$u}}) {
564 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
567 unless ($_no_graft_copy) {
568 graft_file_copy_lib
($grafts,$l_map,$u);
571 graft_tree_joins
($grafts);
573 write_grafts
($grafts, $comments, $gr_file);
574 unlink "$gr_file~$gr_sha1" if $gr_sha1;
579 unless (defined $_trunk || defined $_branches || defined $_tags) {
582 if (defined $_trunk) {
583 my $trunk_url = complete_svn_url
($url, $_trunk);
585 if ($GIT_SVN eq 'git-svn') {
587 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
590 unless (-d
$GIT_SVN_DIR) {
592 print "GIT_SVN_ID set to 'trunk' for ",
593 "$trunk_url ($_trunk)\n";
596 command_noisy
('repo-config', 'svn.trunk', $trunk_url);
599 $_prefix = '' unless defined $_prefix;
600 complete_url_ls_init
($url, $_branches, '--branches/-b', $_prefix);
601 complete_url_ls_init
($url, $_tags, '--tags/-t', $_prefix . 'tags/');
605 # try to do trunk first, since branches/tags
606 # may be descended from it.
607 if (-e
"$GIT_DIR/svn/trunk/info/url") {
608 fetch_child_id
('trunk', @_);
610 rec_fetch
('', "$GIT_DIR/svn", @_);
616 my $r_last = -1; # prevent dupes
617 rload_authors
() if $_authors;
623 if (defined $_revision) {
624 if ($_revision =~ /^(\d+):(\d+)$/) {
625 ($r_min, $r_max) = ($1, $2);
626 } elsif ($_revision =~ /^\d+$/) {
627 $r_min = $r_max = $_revision;
629 print STDERR
"-r$_revision is not supported, use ",
630 "standard \'git log\' arguments instead\n";
636 @args = (git_svn_log_cmd
($r_min, $r_max), @args);
637 my $log = command_output_pipe
(@args);
642 if (/^${_esc_color}commit ($sha1_short)/o) {
644 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
646 process_commit
($c, $r_min, $r_max, \
@k) or
651 } elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
652 get_author_info
($c, $1, $2, $3);
653 } elsif (/^${_esc_color}(?:tree|parent|committer) /) {
655 } elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
656 push @
{$c->{raw
}}, $_;
657 } elsif (/^${_esc_color}[ACRMDT]\t/) {
658 # we could add $SVN->{svn_path} here, but that requires
659 # remote access at the moment (repo_path_split)...
660 s
#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
661 push @
{$c->{changed
}}, $_;
662 } elsif (/^${_esc_color}diff /) {
664 push @
{$c->{diff
}}, $_;
666 push @
{$c->{diff
}}, $_;
667 } elsif (/^${_esc_color} (git-svn-id:.+)$/) {
668 ($c->{url
}, $c->{r
}, undef) = extract_metadata
($1);
669 } elsif (s/^${_esc_color} //) {
673 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
675 process_commit
($c, $r_min, $r_max, \
@k);
681 process_commit
($_, $r_min, $r_max) foreach reverse @k;
684 eval { command_close_pipe
($log) };
685 print '-' x72
,"\n" unless $_incremental || $_oneline;
688 sub commit_diff_usage
{
689 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
694 my $ta = shift or commit_diff_usage
();
695 my $tb = shift or commit_diff_usage
();
696 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
697 print STDERR
"Needed URL or usable git-svn id command-line\n";
701 unless (defined $r) {
702 if (defined $_revision) {
705 die "-r|--revision is a required argument\n";
708 if (defined $_message && defined $_file) {
709 print STDERR
"Both --message/-m and --file/-F specified ",
710 "for the commit message.\n",
711 "I have no idea what you mean\n";
714 if (defined $_file) {
715 $_message = file_to_s
($_file);
717 $_message ||= get_commit_message
($tb,
718 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
720 $SVN ||= libsvn_connect
($SVN_URL);
722 $r = $SVN->get_latest_revnum;
723 } elsif ($r !~ /^\d+$/) {
724 die "revision argument: $r not understood by git-svn\n";
726 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
728 my $ed = SVN
::Git
::Editor
->new({ r
=> $r,
729 ra
=> libsvn_dup_ra
($SVN),
731 svn_path
=> $SVN->{svn_path
}
733 $SVN->get_commit_editor($_message,
735 $rev_committed = $_[0];
736 print "Committed $_[0]\n";
740 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
742 print "No changes\n$ta == $tb\n";
749 $_message = $_file = undef;
750 return $rev_committed;
753 ########################### utility functions #########################
757 return 1 if defined $c->{r
};
758 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
759 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
760 my @msg = command
(qw
/cat-file commit/, $c->{c
});
761 shift @msg while ($msg[0] ne "\n");
763 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
765 (undef, $c->{r
}, undef) = extract_metadata
(
766 (grep(/^git-svn-id: /, @msg))[-1]);
768 return defined $c->{r
};
774 $dcvar = 'color.diff';
775 $dc = `git-repo-config --get $dcvar`;
777 # nothing at all; fallback to "diff.color"
778 $dcvar = 'diff.color';
779 $dc = `git-repo-config --get $dcvar`;
784 $pc = `git-repo-config --get color.pager`;
786 # does not have it -- fallback to pager.color
787 $pc = `git-repo-config --bool --get pager.color`;
790 $pc = `git-repo-config --bool --get color.pager`;
796 if (-t
*STDOUT
|| (defined $_pager && $pc eq 'true')) {
797 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
801 return 0 if $dc eq 'never';
802 return 1 if $dc eq 'always';
803 chomp($dc = `git-repo-config --bool --get $dcvar`);
804 return ($dc eq 'true');
807 sub git_svn_log_cmd
{
808 my ($r_min, $r_max) = @_;
809 my @cmd = (qw
/log --abbrev
-commit
--pretty
=raw
810 --default/, "refs/remotes
/$GIT_SVN");
811 push @cmd, '-r' unless $_non_recursive;
812 push @cmd, qw/--raw --name-status/ if $_verbose;
813 push @cmd, '--color' if log_use_color();
814 return @cmd unless defined $r_max;
815 if ($r_max == $r_min) {
816 push @cmd, '--max-count=1';
817 if (my $c = revdb_get($REVDB, $r_max)) {
822 $c_max = revdb_get($REVDB, $r_max);
823 $c_min = revdb_get($REVDB, $r_min);
824 if (defined $c_min && defined $c_max) {
825 if ($r_max > $r_max) {
826 push @cmd, "$c_min..$c_max";
828 push @cmd, "$c_max..$c_min";
830 } elsif ($r_max > $r_min) {
841 print "Fetching
$id\n";
842 my $ref = "$GIT_DIR/refs/remotes
/$id";
843 defined(my $pid = open my $fh, '-|') or croak $!;
845 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
852 check_repack() if (/^r\d+ = $sha1/o);
854 close $fh or croak $?;
858 my ($pfx, $p, @args) = @_;
860 foreach (sort <$p/*>) {
861 if (-r "$_/info/url
") {
862 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
863 my $id = $pfx . basename $_;
864 next if $id eq 'trunk';
865 fetch_child_id($id, @args);
872 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
877 sub complete_svn_url {
878 my ($url, $path) = @_;
880 $url =~ s#/+$## if $url;
881 if ($path !~ m#^[a-z\+]+://#) {
882 $path = '/' . $path if ($path !~ m#^/#);
883 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
884 fatal("E
: '$path' is
not a complete URL
",
885 "and a separate URL is
not specified
\n");
887 $path = $url . $path;
892 sub complete_url_ls_init {
893 my ($url, $path, $switch, $pfx) = @_;
895 print STDERR "W
: $switch not specified
\n";
898 my $full_url = complete_svn_url($url, $path);
899 my @ls = libsvn_ls_fullurl($full_url);
900 defined(my $pid = fork) or croak $!;
902 foreach my $u (map { "$full_url/$_" } (grep m!/$!, @ls)) {
904 if ($u !~ m!\Q$full_url\E/(.+)$!) {
905 print STDERR
"W: Unrecognized URL: $u\n";
906 die "This should never happen\n";
908 # don't try to init already existing refs
910 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
912 unless (-d
$GIT_SVN_DIR) {
913 print "init $u => $id\n";
921 my ($n) = ($switch =~ /^--(\w+)/);
922 command_noisy
('repo-config', "svn.$n", $full_url);
929 my @tmp = split m
#/#, $_;
931 while (my $x = shift @tmp) {
937 foreach (sort {length $b <=> length $a} keys %common) {
938 if ($common{$_} == @
$paths) {
945 # grafts set here are 'stronger' in that they're based on actual tree
946 # matches, and won't be deleted from merge-base checking in write_grafts()
947 sub graft_tree_joins
{
949 map_tree_joins
() if (@_branch_from && !%tree_map);
950 return unless %tree_map;
954 my @args = (qw
/rev-list --pretty=raw/, "refs/remotes/$i");
955 my ($fh, $ctx) = command_output_pipe
(@args);
957 next unless /^commit ($sha1)$/o;
959 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
960 next unless $tree_map{$t};
965 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
967 my ($s, $tz) = ($1, $2);
968 if ($tz =~ s/^\+//) {
969 $s += tz_to_s_offset
($tz);
970 } elsif ($tz =~ s/^\-//) {
971 $s -= tz_to_s_offset
($tz);
974 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
976 foreach my $p (@
{$tree_map{$t}}) {
978 my $mb = eval { command
('merge-base', $c, $p) };
979 next unless ($@
|| $?
);
981 # see if SVN says it's a relative
982 my ($url_b, $r_b, $uuid_b) =
984 next if (defined $url_b &&
986 ($url_a eq $url_b) &&
987 ($uuid_a eq $uuid_b));
988 if ($uuid_a eq $uuid_b) {
990 $grafts->{$c}->{$p} = 2;
992 } elsif ($r_b > $r_a) {
993 $grafts->{$p}->{$c} = 2;
998 my $ct = get_commit_time
($p);
1000 $grafts->{$c}->{$p} = 2;
1001 } elsif ($ct > $s) {
1002 $grafts->{$p}->{$c} = 2;
1004 # what should we do when $ct == $s ?
1007 command_close_pipe
($fh, $ctx);
1011 sub graft_file_copy_lib
{
1012 my ($grafts, $l_map, $u) = @_;
1013 my $tree_paths = $l_map->{$u};
1014 my $pfx = common_prefix
([keys %$tree_paths]);
1015 my ($repo, $path) = repo_path_split
($u.$pfx);
1016 $SVN = libsvn_connect
($repo);
1018 my ($base, $head) = libsvn_parse_revision
();
1020 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1021 my $eh = $SVN::Error
::handler
;
1022 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1024 my $pool = SVN
::Pool
->new;
1025 libsvn_get_log
(libsvn_dup_ra
($SVN), [$path],
1026 $min, $max, 0, 2, 1,
1028 libsvn_graft_file_copies
($grafts, $tree_paths,
1032 last if ($max >= $head);
1035 $max = $head if ($max > $head);
1037 $SVN::Error
::handler
= $eh;
1040 sub process_merge_msg_matches
{
1041 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1042 my (@strong, @weak);
1043 foreach (@matches) {
1044 # merging with ourselves is not interesting
1046 if ($l_map->{$u}->{$_}) {
1052 foreach my $w (@weak) {
1054 # no exact match, use branch name as regexp.
1055 my $re = qr/\Q$w\E/i;
1056 foreach (keys %{$l_map->{$u}}) {
1058 push @strong, $l_map->{$u}->{$_};
1065 foreach (keys %{$l_map->{$u}}) {
1067 push @strong, $l_map->{$u}->{$_};
1072 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1073 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1074 unless (defined $rev) {
1075 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1076 \@
(?
:[a
-f\d\
-]+)/xsm
);
1077 return unless defined $rev;
1079 foreach my $m (@strong) {
1080 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1081 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1085 sub graft_merge_msg
{
1086 my ($grafts, $l_map, $u, $p, @re) = @_;
1088 my $x = $l_map->{$u}->{$p};
1089 my $rl = rev_list_raw
("refs/remotes/$x");
1090 while (my $c = next_rev_list_entry
($rl)) {
1091 foreach my $re (@re) {
1092 my (@br) = ($c->{m
} =~ /$re/g);
1094 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1100 return if $SVN_UUID;
1101 my $pool = SVN
::Pool
->new;
1102 $SVN_UUID = $SVN->get_uuid($pool);
1108 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1109 { STDERR
=> 0 }); };
1112 sub repo_path_split
{
1113 my $full_url = shift;
1114 $full_url =~ s
#/+$##;
1116 foreach (@repo_path_split_cache) {
1117 if ($full_url =~ s
#$_##) {
1119 $full_url =~ s
#^/+##;
1120 return ($u, $full_url);
1123 my $tmp = libsvn_connect
($full_url);
1124 return ($tmp->{repos_root
}, $tmp->{svn_path
});
1128 defined $SVN_URL or croak
"SVN repository location required\n";
1129 unless (-d
$GIT_DIR) {
1130 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1132 mkpath
([$GIT_SVN_DIR]);
1133 mkpath
(["$GIT_SVN_DIR/info"]);
1134 open my $fh, '>>',$REVDB or croak
$!;
1136 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1140 sub get_tree_from_treeish
{
1142 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1143 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1145 while ($type eq 'tag') {
1146 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1148 if ($type eq 'commit') {
1149 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1151 ($expected) = ($expected =~ /^tree ($sha1)$/);
1152 die "Unable to get tree from $treeish\n" unless $expected;
1153 } elsif ($type eq 'tree') {
1154 $expected = $treeish;
1156 die "$treeish is a $type, expected tree, tag or commit\n";
1162 my ($from, $treeish) = @_;
1163 print "diff-tree $from $treeish\n";
1164 my @diff_tree = qw(diff-tree -z -r);
1165 if ($_cp_similarity) {
1166 push @diff_tree, "-C$_cp_similarity";
1168 push @diff_tree, '-C';
1170 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1171 push @diff_tree, "-l$_l" if defined $_l;
1172 push @diff_tree, $from, $treeish;
1173 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
1177 while (<$diff_fh>) {
1178 chomp $_; # this gets rid of the trailing "\0"
1179 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1180 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1181 push @mods, { mode_a
=> $1, mode_b
=> $2,
1182 sha1_b
=> $3, chg
=> $4 };
1183 if ($4 =~ /^(?:C|R)$/) {
1188 } elsif ($state eq 'file_a') {
1189 my $x = $mods[$#mods] or croak
"Empty array\n";
1190 if ($x->{chg
} !~ /^(?:C|R)$/) {
1191 croak
"Error parsing $_, $x->{chg}\n";
1195 } elsif ($state eq 'file_b') {
1196 my $x = $mods[$#mods] or croak
"Empty array\n";
1197 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1198 croak
"Error parsing $_, $x->{chg}\n";
1200 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1201 croak
"Error parsing $_, $x->{chg}\n";
1206 croak
"Error parsing $_\n";
1209 command_close_pipe
($diff_fh, $ctx);
1213 sub libsvn_checkout_tree
{
1214 my ($from, $treeish, $ed) = @_;
1215 my $mods = get_diff
($from, $treeish);
1216 return $mods unless (scalar @
$mods);
1217 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1218 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1220 if (defined $o{$f}) {
1223 croak
"Invalid change type: $f\n";
1226 $ed->rmdirs($_q) if $_rmdir;
1230 sub get_commit_message
{
1231 my ($commit, $commit_msg) = (@_);
1232 my %log_msg = ( msg
=> '' );
1233 open my $msg, '>', $commit_msg or croak
$!;
1235 my $type = command_oneline
(qw
/cat-file -t/, $commit);
1236 if ($type eq 'commit' || $type eq 'tag') {
1237 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1242 $in_msg = 1 if (/^\s*$/);
1243 } elsif (/^git-svn-id: /) {
1244 # skip this, we regenerate the correct one
1245 # on re-fetch anyways
1247 print $msg $_ or croak
$!;
1250 command_close_pipe
($msg_fh, $ctx);
1252 close $msg or croak
$!;
1254 if ($_edit || ($type eq 'tree')) {
1255 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1256 system($editor, $commit_msg);
1259 # file_to_s removes all trailing newlines, so just use chomp() here:
1260 open $msg, '<', $commit_msg or croak
$!;
1261 { local $/; chomp($log_msg{msg
} = <$msg>); }
1262 close $msg or croak
$!;
1267 sub set_svn_commit_env
{
1268 if (defined $LC_ALL) {
1269 $ENV{LC_ALL
} = $LC_ALL;
1271 delete $ENV{LC_ALL
};
1276 my ($fh, $c) = command_output_pipe
(qw
/rev-list --pretty=raw/, @_);
1277 return { fh
=> $fh, ctx
=> $c, t
=> { } };
1280 sub next_rev_list_entry
{
1285 if (/^commit ($sha1)$/o) {
1287 $rl->{t
} = { c
=> $1 };
1292 } elsif (/^parent ($sha1)$/o) {
1299 command_close_pipe
($fh, $rl->{ctx
});
1300 return ($x != $rl->{t
}) ?
$x : undef;
1304 my ($str, $file, $mode) = @_;
1305 open my $fd,'>',$file or croak
$!;
1306 print $fd $str,"\n" or croak
$!;
1307 close $fd or croak
$!;
1308 chmod ($mode &~ umask, $file) if (defined $mode);
1313 open my $fd,'<',$file or croak
"$!: file: $file\n";
1316 close $fd or croak
$!;
1321 sub assert_revision_unknown
{
1323 if (my $c = revdb_get
($REVDB, $r)) {
1324 croak
"$r = $c already exists! Why are we refetching it?";
1329 my ($log_msg, @parents) = @_;
1330 assert_revision_unknown
($log_msg->{revision
});
1331 map_tree_joins
() if (@_branch_from && !%tree_map);
1333 my (@tmp_parents, @exec_parents, %seen_parent);
1334 if (my $lparents = $log_msg->{parents
}) {
1335 @tmp_parents = @
$lparents
1337 # commit parents can be conditionally bound to a particular
1338 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1339 foreach my $p (@parents) {
1340 next unless defined $p;
1341 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1342 if ($1 == $log_msg->{revision
}) {
1343 push @tmp_parents, $2;
1346 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1349 my $tree = $log_msg->{tree
};
1350 if (!defined $tree) {
1351 my $index = set_index
($GIT_SVN_INDEX);
1352 $tree = command_oneline
('write-tree');
1354 restore_index
($index);
1356 # just in case we clobber the existing ref, we still want that ref
1358 if (my $cur = verify_ref
("refs/remotes/$GIT_SVN^0")) {
1360 push @tmp_parents, $cur;
1363 if (exists $tree_map{$tree}) {
1364 foreach my $p (@
{$tree_map{$tree}}) {
1366 foreach (@tmp_parents) {
1367 # see if a common parent is found
1368 my $mb = eval { command
('merge-base', $_, $p) };
1374 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
1375 next if (($SVN_UUID eq $uuid_p) &&
1376 ($log_msg->{revision
} > $r_p));
1377 next if (defined $url_p && defined $SVN_URL &&
1378 ($SVN_UUID eq $uuid_p) &&
1379 ($url_p eq $SVN_URL));
1380 push @tmp_parents, $p;
1383 foreach (@tmp_parents) {
1384 next if $seen_parent{$_};
1385 $seen_parent{$_} = 1;
1386 push @exec_parents, $_;
1387 # MAXPARENT is defined to 16 in commit-tree.c:
1388 last if @exec_parents > 16;
1391 set_commit_env
($log_msg);
1392 my @exec = ('git-commit-tree', $tree);
1393 push @exec, '-p', $_ foreach @exec_parents;
1394 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1396 print $msg_fh $log_msg->{msg
} or croak
$!;
1397 unless ($_no_metadata) {
1398 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1399 " $SVN_UUID\n" or croak
$!;
1401 $msg_fh->flush == 0 or croak
$!;
1402 close $msg_fh or croak
$!;
1403 chomp(my $commit = do { local $/; <$out_fh> });
1404 close $out_fh or croak
$!;
1407 if ($commit !~ /^$sha1$/o) {
1408 die "Failed to commit, invalid sha1: $commit\n";
1410 command_noisy
('update-ref',"refs/remotes/$GIT_SVN",$commit);
1411 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
1413 # this output is read via pipe, do not change:
1414 print "r$log_msg->{revision} = $commit\n";
1419 if ($_repack && (--$_repack_nr == 0)) {
1420 $_repack_nr = $_repack;
1421 # repack doesn't use any arguments with spaces in them, does it?
1422 command_noisy
('repack', split(/\s+/, $_repack_flags));
1426 sub set_commit_env
{
1428 my $author = $log_msg->{author
};
1429 if (!defined $author || length $author == 0) {
1430 $author = '(no author)';
1432 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
1433 : ($author,"$author\@$SVN_UUID");
1434 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1435 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1436 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
1439 sub check_upgrade_needed
{
1441 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
1442 open my $fh, '>>',$REVDB or croak
$!;
1445 return unless eval {
1446 command
([qw
/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
1449 my $head = eval { command
('rev-parse',"refs/remotes/$GIT_SVN") };
1451 print STDERR
"Please run: $0 rebuild --upgrade\n";
1456 # fills %tree_map with a reverse mapping of trees to commits. Useful
1457 # for finding parents to commit on.
1458 sub map_tree_joins
{
1460 foreach my $br (@_branch_from) {
1461 my $pipe = command_output_pipe
(qw
/rev
-list
1462 --topo
-order
--pretty
=raw
/, $br);
1464 if (/^commit ($sha1)$/o) {
1467 # if we've seen a commit,
1468 # we've seen its parents
1469 last if $seen{$commit};
1470 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1471 unless (defined $tree) {
1472 die "Failed to parse commit $commit\n";
1474 push @
{$tree_map{$tree}}, $commit;
1478 eval { command_close_pipe
($pipe) };
1483 if (@_branch_from) {
1484 print STDERR
'--branch|-b parameters are ignored when ',
1485 "--branch-all-refs|-B is passed\n";
1488 # don't worry about rev-list on non-commit objects/tags,
1489 # it shouldn't blow up if a ref is a blob or tree...
1490 @_branch_from = command
(qw
/rev-parse --symbolic --all/);
1493 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1495 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1496 while (<$authors>) {
1498 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1499 my ($user, $name, $email) = ($1, $2, $3);
1500 $users{$user} = [$name, $email];
1502 close $authors or croak
$!;
1506 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1507 while (<$authors>) {
1509 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1510 my ($user, $name, $email) = ($1, $2, $3);
1511 $rusers{"$name <$email>"} = $user;
1513 close $authors or croak
$!;
1518 foreach (command
(qw
/rev-parse --symbolic --all/)) {
1519 next unless s
#^refs/remotes/##;
1521 next unless -f
"$GIT_DIR/svn/$_/info/url";
1529 defined(my $pid = fork) or croak
$!;
1531 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
1533 exit 0 if -r
$REVDB;
1534 print "Upgrading svn => git mapping...\n";
1535 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
1536 open my $fh, '>>',$REVDB or croak
$!;
1539 print "Done upgrading. You may now delete the ",
1540 "deprecated $GIT_SVN_DIR/revs directory\n";
1548 sub migration_check
{
1549 migrate_revdb
() unless (-e
$REVDB);
1550 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
1551 print "Upgrading repository...\n";
1552 unless (-d
"$GIT_DIR/svn") {
1553 mkdir "$GIT_DIR/svn" or croak
$!;
1555 print "Data from a previous version of git-svn exists, but\n\t",
1556 "$GIT_SVN_DIR\n\t(required for this version ",
1557 "($VERSION) of git-svn) does not.\n";
1559 foreach my $x (command
(qw
/rev-parse --symbolic --all/)) {
1560 next unless $x =~ s
#^refs/remotes/##;
1562 next unless -f
"$GIT_DIR/$x/info/url";
1563 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
1565 my $dn = dirname
("$GIT_DIR/svn/$x");
1566 mkpath
([$dn]) unless -d
$dn;
1567 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
1569 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
1570 print "Done upgrading.\n";
1573 sub find_rev_before
{
1574 my ($r, $id, $eq_ok) = @_;
1575 my $f = "$GIT_DIR/svn/$id/.rev_db";
1576 return (undef,undef) unless -r
$f;
1579 if (my $c = revdb_get
($f, $r)) {
1584 return (undef, undef);
1588 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
1589 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1590 $REVDB = "$GIT_SVN_DIR/.rev_db";
1591 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1593 $SVN_WC = "$GIT_SVN_DIR/tree";
1597 # convert GetOpt::Long specs for use by git-repo-config
1598 sub read_repo_config
{
1599 return unless -d
$GIT_DIR;
1601 foreach my $o (keys %$opts) {
1602 my $v = $opts->{$o};
1603 my ($key) = ($o =~ /^([a-z\-]+)/);
1605 my $arg = 'git-repo-config';
1606 $arg .= ' --int' if ($o =~ /[:=]i$/);
1607 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1608 if (ref $v eq 'ARRAY') {
1609 chomp(my @tmp = `$arg --get-all svn.$key`);
1612 chomp(my $tmp = `$arg --get svn.$key`);
1613 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
1620 sub set_default_vals
{
1621 if (defined $_repack) {
1622 $_repack = 1000 if ($_repack <= 0);
1623 $_repack_nr = $_repack;
1624 $_repack_flags ||= '-d';
1629 my $gr_file = shift;
1630 my ($grafts, $comments) = ({}, {});
1631 if (open my $fh, '<', $gr_file) {
1634 if (/^($sha1)\s+/) {
1637 @
{$comments->{$c}} = @tmp;
1640 foreach my $p (split /\s+/, $_) {
1641 $grafts->{$c}->{$p} = 1;
1647 close $fh or croak
$!;
1648 @
{$comments->{'END'}} = @tmp if @tmp;
1650 return ($grafts, $comments);
1654 my ($grafts, $comments, $gr_file) = @_;
1656 open my $fh, '>', $gr_file or croak
$!;
1657 foreach my $c (sort keys %$grafts) {
1658 if ($comments->{$c}) {
1659 print $fh $_ foreach @
{$comments->{$c}};
1661 my $p = $grafts->{$c};
1662 my %x; # real parents
1663 delete $p->{$c}; # commits are not self-reproducing...
1664 my $ch = command_output_pipe
(qw
/cat-file commit/, $c);
1666 if (/^parent ($sha1)/) {
1667 $x{$1} = $p->{$1} = 1;
1672 eval { command_close_pipe
($ch) }; # breaking the pipe
1674 # if real parents are the only ones in the grafts, drop it
1675 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1679 @ip = @jp = keys %$p;
1680 foreach my $i (@ip) {
1681 next if $del{$i} || $p->{$i} == 2;
1682 foreach my $j (@jp) {
1683 next if $i eq $j || $del{$j} || $p->{$j} == 2;
1684 $mb = eval { command
('merge-base', $i, $j) };
1691 } elsif ($mb eq $i) {
1698 # if real parents are the only ones in the grafts, drop it
1699 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1701 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1703 if ($comments->{'END'}) {
1704 print $fh $_ foreach @
{$comments->{'END'}};
1706 close $fh or croak
$!;
1709 sub read_url_paths_all
{
1710 my ($l_map, $pfx, $p) = @_;
1713 if (-r
"$_/info/url") {
1714 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
1715 my $id = $pfx . basename
$_;
1716 my $url = file_to_s
("$_/info/url");
1717 my ($u, $p) = repo_path_split
($url);
1718 $l_map->{$u}->{$p} = $id;
1725 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
1726 read_url_paths_all
($l_map, $x, $_);
1730 # this one only gets ids that have been imported, not new ones
1731 sub read_url_paths
{
1733 git_svn_each
(sub { my $x = shift;
1734 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
1735 my ($u, $p) = repo_path_split
($url);
1736 $l_map->{$u}->{$p} = $x;
1741 sub extract_metadata
{
1742 my $id = shift or return (undef, undef, undef);
1743 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
1745 if (!defined $rev || !$uuid || !$url) {
1746 # some of the original repositories I made had
1747 # identifiers like this:
1748 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1750 return ($url, $rev, $uuid);
1754 return extract_metadata
((grep(/^git-svn-id: /,
1755 command
(qw
/cat-file commit/, shift)))[-1]);
1758 sub get_commit_time
{
1760 my $fh = command_output_pipe
(qw
/rev-list --pretty=raw -n1/, $cmt);
1762 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
1763 my ($s, $tz) = ($1, $2);
1764 if ($tz =~ s/^\+//) {
1765 $s += tz_to_s_offset
($tz);
1766 } elsif ($tz =~ s/^\-//) {
1767 $s -= tz_to_s_offset
($tz);
1769 eval { command_close_pipe
($fh) };
1772 die "Can't get commit time for commit: $cmt\n";
1775 sub tz_to_s_offset
{
1778 return ($1 * 60) + ($tz * 3600);
1781 # adapted from pager.c
1783 $_pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
1784 if (!defined $_pager) {
1786 } elsif (length $_pager == 0 || $_pager eq 'cat') {
1792 return unless -t
*STDOUT
;
1793 pipe my $rfd, my $wfd or return;
1794 defined(my $pid = fork) or croak
$!;
1796 open STDOUT
, '>&', $wfd or croak
$!;
1799 open STDIN
, '<&', $rfd or croak
$!;
1800 $ENV{LESS
} ||= 'FRSX';
1801 exec $_pager or croak
"Can't run pager: $! ($_pager)\n";
1804 sub get_author_info
{
1805 my ($dest, $author, $t, $tz) = @_;
1806 $author =~ s/(?:^\s*|\s*$)//g;
1807 $dest->{a_raw
} = $author;
1810 $_a = $rusers{$author} || undef;
1813 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1818 # Date::Parse isn't in the standard Perl distro :(
1819 if ($tz =~ s/^\+//) {
1820 $t += tz_to_s_offset
($tz);
1821 } elsif ($tz =~ s/^\-//) {
1822 $t -= tz_to_s_offset
($tz);
1824 $dest->{t_utc
} = $t;
1827 sub process_commit
{
1828 my ($c, $r_min, $r_max, $defer) = @_;
1829 if (defined $r_min && defined $r_max) {
1830 if ($r_min == $c->{r
} && $r_min == $r_max) {
1834 return 1 if $r_min == $r_max;
1835 if ($r_min < $r_max) {
1836 # we need to reverse the print order
1837 return 0 if (defined $_limit && --$_limit < 0);
1841 if ($r_min != $r_max) {
1842 return 1 if ($r_min < $c->{r
});
1843 return 1 if ($r_max > $c->{r
});
1846 return 0 if (defined $_limit && --$_limit < 0);
1855 if (my $l = $c->{l
}) {
1856 while ($l->[0] =~ /^\s*$/) { shift @
$l }
1859 $_l_fmt ||= 'A' . length($c->{r
});
1860 print 'r',pack($_l_fmt, $c->{r
}),' | ';
1861 print "$c->{c} | " if $_show_commit;
1864 show_commit_normal
($c);
1868 sub show_commit_changed_paths
{
1870 return unless $c->{changed
};
1871 print "Changed paths:\n", @
{$c->{changed
}};
1874 sub show_commit_normal
{
1876 print '-' x72
, "\nr$c->{r} | ";
1877 print "$c->{c} | " if $_show_commit;
1878 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
1879 localtime($c->{t_utc
})), ' | ';
1882 if (my $l = $c->{l
}) {
1883 while ($l->[$#$l] eq "\n" && $#$l > 0
1884 && $l->[($#$l - 1)] eq "\n") {
1887 $nr_line = scalar @
$l;
1889 print "1 line\n\n\n";
1891 if ($nr_line == 1) {
1892 $nr_line = '1 line';
1894 $nr_line .= ' lines';
1896 print $nr_line, "\n";
1897 show_commit_changed_paths
($c);
1899 print $_ foreach @
$l;
1903 show_commit_changed_paths
($c);
1907 foreach my $x (qw
/raw diff/) {
1910 print $_ foreach @
{$c->{$x}}
1915 sub _simple_prompt
{
1916 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1917 $may_save = undef if $_no_auth_cache;
1918 $default_username = $_username if defined $_username;
1919 if (defined $default_username && length $default_username) {
1920 if (defined $realm && length $realm) {
1921 print "Authentication realm: $realm\n";
1923 $cred->username($default_username);
1925 _username_prompt
($cred, $realm, $may_save, $pool);
1927 $cred->password(_read_password
("Password for '" .
1928 $cred->username . "': ", $realm));
1929 $cred->may_save($may_save);
1930 $SVN::_Core
::SVN_NO_ERROR
;
1933 sub _ssl_server_trust_prompt
{
1934 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1935 $may_save = undef if $_no_auth_cache;
1936 print "Error validating server certificate for '$realm':\n";
1937 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
1938 print " - The certificate is not issued by a trusted ",
1939 "authority. Use the\n",
1940 " fingerprint to validate the certificate manually!\n";
1942 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
1943 print " - The certificate hostname does not match.\n";
1945 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
1946 print " - The certificate is not yet valid.\n";
1948 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
1949 print " - The certificate has expired.\n";
1951 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
1952 print " - The certificate has an unknown error.\n";
1954 printf( "Certificate information:\n".
1955 " - Hostname: %s\n".
1956 " - Valid: from %s until %s\n".
1958 " - Fingerprint: %s\n",
1959 map $cert_info->$_, qw(hostname valid_from valid_until
1960 issuer_dname fingerprint) );
1964 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1965 "(R)eject or accept (t)emporarily? ";
1966 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
1967 if ($choice =~ /^t$/i) {
1968 $cred->may_save(undef);
1969 } elsif ($choice =~ /^r$/i) {
1971 } elsif ($may_save && $choice =~ /^p$/i) {
1972 $cred->may_save($may_save);
1976 $cred->accepted_failures($failures);
1977 $SVN::_Core
::SVN_NO_ERROR
;
1980 sub _ssl_client_cert_prompt
{
1981 my ($cred, $realm, $may_save, $pool) = @_;
1982 $may_save = undef if $_no_auth_cache;
1983 print "Client certificate filename: ";
1984 chomp(my $filename = <STDIN
>);
1985 $cred->cert_file($filename);
1986 $cred->may_save($may_save);
1987 $SVN::_Core
::SVN_NO_ERROR
;
1990 sub _ssl_client_cert_pw_prompt
{
1991 my ($cred, $realm, $may_save, $pool) = @_;
1992 $may_save = undef if $_no_auth_cache;
1993 $cred->password(_read_password
("Password: ", $realm));
1994 $cred->may_save($may_save);
1995 $SVN::_Core
::SVN_NO_ERROR
;
1998 sub _username_prompt
{
1999 my ($cred, $realm, $may_save, $pool) = @_;
2000 $may_save = undef if $_no_auth_cache;
2001 if (defined $realm && length $realm) {
2002 print "Authentication realm: $realm\n";
2005 if (defined $_username) {
2006 $username = $_username;
2009 chomp($username = <STDIN
>);
2011 $cred->username($username);
2012 $cred->may_save($may_save);
2013 $SVN::_Core
::SVN_NO_ERROR
;
2016 sub _read_password
{
2017 my ($prompt, $realm) = @_;
2019 require Term
::ReadKey
;
2020 Term
::ReadKey
::ReadMode
('noecho');
2022 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2023 last if $key =~ /[\012\015]/; # \n\r
2026 Term
::ReadKey
::ReadMode
('restore');
2031 sub libsvn_connect
{
2033 SVN
::_Core
::svn_config_ensure
($_config_dir, undef);
2034 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2035 SVN
::Client
::get_simple_provider
(),
2036 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2037 SVN
::Client
::get_simple_prompt_provider
(
2038 \
&_simple_prompt
, 2),
2039 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2040 \
&_ssl_client_cert_prompt
, 2),
2041 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2042 \
&_ssl_client_cert_pw_prompt
, 2),
2043 SVN
::Client
::get_username_provider
(),
2044 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2045 \
&_ssl_server_trust_prompt
),
2046 SVN
::Client
::get_username_prompt_provider
(
2047 \
&_username_prompt
, 2),
2049 my $config = SVN
::Core
::config_get_config
($_config_dir);
2050 my $ra = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2052 pool
=> SVN
::Pool
->new,
2053 auth_provider_callbacks
=> $callbacks);
2054 $ra->{svn_path
} = $url;
2055 $ra->{repos_root
} = $ra->get_repos_root;
2056 $ra->{svn_path
} =~ s
#^\Q$ra->{repos_root}\E/*##;
2057 push @repo_path_split_cache, qr/^(\Q$ra->{repos_root}\E)/;
2061 sub libsvn_can_do_switch
{
2062 unless (defined $_svn_can_do_switch) {
2063 my $pool = SVN
::Pool
->new;
2065 $SVN->do_switch(1, '', 0, $SVN->{url
},
2066 SVN
::Delta
::Editor
->new, $pool);
2069 $_svn_can_do_switch = 0;
2071 $rep->abort_report($pool);
2072 $_svn_can_do_switch = 1;
2076 $_svn_can_do_switch;
2081 SVN
::Ra
->new(map { $_ => $ra->{$_} } qw
/config url
2082 auth auth_provider_callbacks repos_root svn_path
/);
2087 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2094 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
2098 sub libsvn_log_entry
{
2099 my ($rev, $author, $date, $msg, $parents, $untracked) = @_;
2100 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2101 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2102 or die "Unable to parse date: $date\n";
2103 if (defined $author && length $author > 0 &&
2104 defined $_authors && ! defined $users{$author}) {
2105 die "Author: $author not defined in $_authors file\n";
2107 $msg = '' if ($rev == 0 && !defined $msg);
2109 open my $un, '>>', "$GIT_SVN_DIR/unhandled.log" or croak
$!;
2111 print $un "r$rev\n" or croak
$!;
2112 $h = $untracked->{empty
};
2113 foreach (sort keys %$h) {
2114 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2115 print $un " $act: ", uri_encode
($_), "\n" or croak
$!;
2116 warn "W: $act: $_\n";
2118 foreach my $t (qw
/dir_prop file_prop/) {
2119 $h = $untracked->{$t} or next;
2120 foreach my $path (sort keys %$h) {
2121 my $ppath = $path eq '' ?
'.' : $path;
2122 foreach my $prop (sort keys %{$h->{$path}}) {
2123 next if $SKIP{$prop};
2124 my $v = $h->{$path}->{$prop};
2127 uri_encode
($ppath), ' ',
2128 uri_encode
($prop), ' ',
2129 uri_encode
($v), "\n"
2133 uri_encode
($ppath), ' ',
2134 uri_encode
($prop), "\n"
2140 foreach my $t (qw
/absent_file absent_directory/) {
2141 $h = $untracked->{$t} or next;
2142 foreach my $parent (sort keys %$h) {
2143 foreach my $path (sort @
{$h->{$parent}}) {
2145 uri_encode
("$parent/$path"), "\n"
2147 warn "W: $t: $parent/$path ",
2148 "Insufficient permissions?\n";
2153 # revprops (make this optional? it's an extra network trip...)
2154 my $pool = SVN
::Pool
->new;
2155 my $rp = $SVN->rev_proplist($rev, $pool);
2156 foreach (sort keys %$rp) {
2157 next if /^svn:(?:author|date|log)$/;
2158 print $un " rev_prop: ", uri_encode
($_), ' ',
2159 uri_encode
($rp->{$_}), "\n";
2162 close $un or croak
$!;
2164 { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2165 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [],
2170 my ($gui, $last_commit, $f, $q) = @_;
2171 # remove entire directories.
2172 if (command
('ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2173 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2175 $last_commit,'--',$f);
2178 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2179 print "\tD\t$_\n" unless $q;
2181 print "\tD\t$f/\n" unless $q;
2182 command_close_pipe
($ls, $ctx);
2183 return $SVN::Node
::dir
;
2185 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2186 print "\tD\t$f\n" unless $q;
2187 return $SVN::Node
::file
;
2192 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2193 my $pool = SVN
::Pool
->new;
2194 my $ed = SVN
::Git
::Fetcher
->new({ c
=> $last_commit, q
=> $_q });
2195 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2196 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2197 my (undef, $last_rev, undef) = cmt_metadata
($last_commit);
2198 $reporter->set_path('', $last_rev, 0, @lock, $pool);
2199 $reporter->finish_report($pool);
2201 unless ($ed->{git_commit_ok
}) {
2202 die "SVN connection failed somewhere...\n";
2204 libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit], $ed);
2207 sub svn_grab_base_rev
{
2208 my $c = eval { command_oneline
([qw
/rev-parse --verify/,
2209 "refs/remotes/$GIT_SVN^0"],
2211 if (defined $c && length $c) {
2212 my ($url, $rev, $uuid) = cmt_metadata
($c);
2213 return ($rev, $c) if defined $rev;
2215 if ($_no_metadata) {
2216 my $offset = -41; # from tail
2218 open my $fh, '<', $REVDB or
2219 die "--no-metadata specified and $REVDB not readable\n";
2220 seek $fh, $offset, 2;
2222 defined $rl or return (undef, undef);
2224 while ($c ne $rl && tell $fh != 0) {
2226 seek $fh, $offset, 2;
2228 defined $rl or return (undef, undef);
2232 croak
$! if ($rev < -1);
2233 $rev = ($rev - 41) / 41;
2234 close $fh or croak
$!;
2237 return (undef, undef);
2240 sub libsvn_parse_revision
{
2242 my $head = $SVN->get_latest_revnum();
2243 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2244 return ($base + 1, $head) if (defined $base);
2247 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2248 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2249 if ($_revision =~ /^BASE:(\d+)$/) {
2250 return ($base + 1, $1) if (defined $base);
2253 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2254 die "revision argument: $_revision not understood by git-svn\n",
2255 "Try using the command-line svn client instead\n";
2258 sub libsvn_traverse_ignore
{
2259 my ($fh, $path, $r) = @_;
2261 my $pool = SVN
::Pool
->new;
2262 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2264 $p =~ s
#^\Q$SVN->{svn_path}\E/##;
2265 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2266 if (my $s = $props->{'svn:ignore'}) {
2267 $s =~ s/[\r\n]+/\n/g;
2269 if (length $p == 0) {
2273 $s =~ s
#\n#\n/$p/#g;
2274 print $fh "/$p/$s\n";
2277 foreach (sort keys %$dirent) {
2278 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2279 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2285 my ($path, $r0, $r1) = @_;
2286 return 1 if $r0 == $r1;
2288 # should be OK to use Pool here (r1 - r0) should be small
2289 my $pool = SVN
::Pool
->new;
2290 libsvn_get_log
($SVN, [$path], $r0, $r1,
2291 0, 0, 1, sub {$nr++}, $pool);
2293 return 0 if ($nr > 1);
2297 sub libsvn_find_parent_branch
{
2298 my ($paths, $rev, $author, $date, $msg) = @_;
2299 my $svn_path = '/'.$SVN->{svn_path
};
2301 # look for a parent from another branch:
2302 my $i = $paths->{$svn_path} or return;
2303 my $branch_from = $i->copyfrom_path or return;
2304 my $r = $i->copyfrom_rev;
2305 print STDERR
"Found possible branch point: ",
2306 "$branch_from => $svn_path, $r\n";
2307 $branch_from =~ s
#^/##;
2309 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
2310 my $url = $SVN->{repos_root
};
2311 defined $l_map->{$url} or return;
2312 my $id = $l_map->{$url}->{$branch_from};
2313 if (!defined $id && $_follow_parent) {
2314 print STDERR
"Following parent: $branch_from\@$r\n";
2315 # auto create a new branch and follow it
2316 $id = basename
($branch_from);
2317 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
2318 while (-r
"$GIT_DIR/svn/$id") {
2319 # just grow a tail if we're not unique enough :x
2323 return unless defined $id;
2325 my ($r0, $parent) = find_rev_before
($r,$id,1);
2326 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2327 defined(my $pid = fork) or croak
$!;
2329 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2331 $SVN_URL = "$url/$branch_from";
2334 # we can't assume SVN_URL exists at r+1:
2335 $_revision = "0:$r";
2341 ($r0, $parent) = find_rev_before
($r,$id,1);
2343 return unless (defined $r0 && defined $parent);
2344 if (revisions_eq
($branch_from, $r0, $r)) {
2345 unlink $GIT_SVN_INDEX;
2346 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
2347 command_noisy
('read-tree', $parent);
2348 unless (libsvn_can_do_switch
()) {
2349 return _libsvn_new_tree
($paths, $rev, $author, $date,
2352 # do_switch works with svn/trunk >= r22312, but that is not
2353 # included with SVN 1.4.2 (the latest version at the moment),
2354 # so we can't rely on it.
2355 my $ra = libsvn_connect
("$url/$branch_from");
2356 my $ed = SVN
::Git
::Fetcher
->new({c
=> $parent, q
=> $_q });
2357 my $pool = SVN
::Pool
->new;
2358 my $reporter = $ra->do_switch($rev, '', 1, $SVN->{url
},
2360 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2361 $reporter->set_path('', $r0, 0, @lock, $pool);
2362 $reporter->finish_report($pool);
2364 unless ($ed->{git_commit_ok
}) {
2365 die "SVN connection failed somewhere...\n";
2367 return libsvn_log_entry
($rev, $author, $date, $msg, [$parent]);
2369 print STDERR
"Nope, branch point not imported or unknown\n";
2373 sub libsvn_get_log
{
2374 my ($ra, @args) = @_;
2375 $args[4]-- if $args[4] && ! $_follow_parent;
2376 if ($SVN::Core
::VERSION
le '1.2.0') {
2377 splice(@args, 3, 1);
2379 $ra->get_log(@args);
2382 sub libsvn_new_tree
{
2383 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
2386 my ($paths, $rev, $author, $date, $msg) = @_; # $pool is last
2387 _libsvn_new_tree
($paths, $rev, $author, $date, $msg, []);
2390 sub _libsvn_new_tree
{
2391 my ($paths, $rev, $author, $date, $msg, $parents) = @_;
2392 my $pool = SVN
::Pool
->new;
2393 my $ed = SVN
::Git
::Fetcher
->new({q
=> $_q});
2394 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2395 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2396 $reporter->set_path('', $rev, 1, @lock, $pool);
2397 $reporter->finish_report($pool);
2399 unless ($ed->{git_commit_ok
}) {
2400 die "SVN connection failed somewhere...\n";
2402 libsvn_log_entry
($rev, $author, $date, $msg, $parents, $ed);
2405 sub find_graft_path_commit
{
2406 my ($tree_paths, $p1, $r1) = @_;
2407 foreach my $x (keys %$tree_paths) {
2408 next unless ($p1 =~ /^\Q$x\E/);
2409 my $i = $tree_paths->{$x};
2410 my ($r0, $parent) = find_rev_before
($r1,$i,1);
2411 return $parent if (defined $r0 && $r0 == $r1);
2412 print STDERR
"r$r1 of $i not imported\n";
2418 sub find_graft_path_parents
{
2419 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2420 foreach my $x (keys %$tree_paths) {
2421 next unless ($p0 =~ /^\Q$x\E/);
2422 my $i = $tree_paths->{$x};
2423 my ($r, $parent) = find_rev_before
($r0, $i, 1);
2424 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
2425 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
2426 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
2427 next if ($url_a && $url_b && $url_a eq $url_b &&
2428 $uuid_b eq $uuid_a);
2429 $grafts->{$c}->{$parent} = 1;
2434 sub libsvn_graft_file_copies
{
2435 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2436 foreach (keys %$paths) {
2437 my $i = $paths->{$_};
2438 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2440 next unless (defined $p0 && defined $r0);
2445 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
2447 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
2452 my $old = $ENV{GIT_INDEX_FILE
};
2453 $ENV{GIT_INDEX_FILE
} = shift;
2460 $ENV{GIT_INDEX_FILE
} = $old;
2462 delete $ENV{GIT_INDEX_FILE
};
2466 sub libsvn_commit_cb
{
2467 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2468 if ($_optimize_commits && $rev == ($r_last + 1)) {
2469 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
2470 $log->{tree
} = get_tree_from_treeish
($c);
2471 my $cmt = git_commit
($log, $cmt_last, $c);
2472 my @diff = command
('diff-tree', $cmt, $c);
2474 print STDERR
"Trees differ: $cmt $c\n",
2475 join('',@diff),"\n";
2483 sub libsvn_ls_fullurl
{
2484 my $fullurl = shift;
2485 my $ra = libsvn_connect
($fullurl);
2487 my $pool = SVN
::Pool
->new;
2488 my $r = defined $_revision ?
$_revision : $ra->get_latest_revnum;
2489 my ($dirent, undef, undef) = $ra->get_dir('', $r, $pool);
2490 foreach my $d (sort keys %$dirent) {
2491 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
2492 push @ret, "$d/"; # add '/' for compat with cli svn
2500 sub libsvn_skip_unknown_revs
{
2502 my $errno = $err->apr_err();
2503 # Maybe the branch we're tracking didn't
2504 # exist when the repo started, so it's
2505 # not an error if it doesn't, just continue
2507 # Wonderfully consistent library, eh?
2508 # 160013 - svn:// and file://
2509 # 175002 - http(s)://
2510 # 175007 - http(s):// (this repo required authorization, too...)
2511 # More codes may be discovered later...
2512 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2515 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
2518 # Tie::File seems to be prone to offset errors if revisions get sparse,
2519 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2520 # one of my favorite modules is out :< Next up would be one of the DBM
2521 # modules, but I'm not sure which is most portable... So I'll just
2522 # go with something that's plain-text, but still capable of
2523 # being randomly accessed. So here's my ultra-simple fixed-width
2524 # database. All records are 40 characters + "\n", so it's easy to seek
2525 # to a revision: (41 * rev) is the byte offset.
2526 # A record of 40 0s denotes an empty revision.
2527 # And yes, it's still pretty fast (faster than Tie::File).
2529 my ($file, $rev, $commit) = @_;
2530 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
2531 open my $fh, '+<', $file or croak
$!;
2532 my $offset = $rev * 41;
2533 # assume that append is the common case:
2534 seek $fh, 0, 2 or croak
$!;
2536 if ($pos < $offset) {
2537 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
2539 seek $fh, $offset, 0 or croak
$!;
2540 print $fh $commit,"\n";
2541 close $fh or croak
$!;
2545 my ($file, $rev) = @_;
2547 my $offset = $rev * 41;
2548 open my $fh, '<', $file or croak
$!;
2549 seek $fh, $offset, 0;
2550 if (tell $fh == $offset) {
2551 $ret = readline $fh;
2554 $ret = undef if ($ret =~ /^0{40}$/);
2557 close $fh or croak
$!;
2561 sub copy_remote_ref
{
2562 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
2563 my $ref = "refs/remotes/$GIT_SVN";
2564 if (command
('ls-remote', $origin, $ref)) {
2565 command_noisy
('fetch', $origin, "$ref:$ref");
2566 } elsif ($_cp_remote && !$_upgrade) {
2567 die "Unable to find remote reference: ",
2568 "refs/remotes/$GIT_SVN on $origin\n";
2573 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2574 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2575 $SVN::Node
::none
.$SVN::Node
::file
.
2576 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2577 $SVN::Auth
::SSL
::CNMISMATCH
.
2578 $SVN::Auth
::SSL
::NOTYETVALID
.
2579 $SVN::Auth
::SSL
::EXPIRED
.
2580 $SVN::Auth
::SSL
::UNKNOWNCA
.
2581 $SVN::Auth
::SSL
::OTHER
;
2584 package SVN
::Git
::Fetcher
;
2590 use Git qw
/command command_oneline command_noisy
2591 command_output_pipe command_input_pipe command_close_pipe
/;
2593 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2595 my ($class, $git_svn) = @_;
2596 my $self = SVN
::Delta
::Editor
->new;
2597 bless $self, $class;
2598 $self->{c
} = $git_svn->{c
} if exists $git_svn->{c
};
2599 $self->{q
} = $git_svn->{q
};
2600 $self->{empty
} = {};
2601 $self->{dir_prop
} = {};
2602 $self->{file_prop
} = {};
2603 $self->{absent_dir
} = {};
2604 $self->{absent_file
} = {};
2605 ($self->{gui
}, $self->{ctx
}) = command_input_pipe
(
2606 qw
/update-index -z --index-info/);
2607 require Digest
::MD5
;
2615 sub open_directory
{
2616 my ($self, $path, $pb, $rev) = @_;
2621 my ($self, $path, $rev, $pb) = @_;
2622 my $t = process_rm
($self->{gui
}, $self->{c
}, $path, $self->{q
});
2623 $self->{empty
}->{$path} = 0 if $t == $SVN::Node
::dir
;
2628 my ($self, $path, $pb, $rev) = @_;
2629 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--',$path)
2630 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2631 unless (defined $mode && defined $blob) {
2632 die "$path was not found in commit $self->{c} (r$rev)\n";
2634 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2635 pool
=> SVN
::Pool
->new, action
=> 'M' };
2639 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2640 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2641 delete $self->{empty
}->{$dir};
2642 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2643 pool
=> SVN
::Pool
->new, action
=> 'A' };
2647 my ($self, $path, $cp_path, $cp_rev) = @_;
2648 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2649 delete $self->{empty
}->{$dir};
2650 $self->{empty
}->{$path} = 1;
2654 sub change_dir_prop
{
2655 my ($self, $db, $prop, $value) = @_;
2656 $self->{dir_prop
}->{$db->{path
}} ||= {};
2657 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2661 sub absent_directory
{
2662 my ($self, $path, $pb) = @_;
2663 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2664 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2669 my ($self, $path, $pb) = @_;
2670 $self->{absent_file
}->{$pb->{path
}} ||= [];
2671 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2675 sub change_file_prop
{
2676 my ($self, $fb, $prop, $value) = @_;
2677 if ($prop eq 'svn:executable') {
2678 if ($fb->{mode_b
} != 120000) {
2679 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2681 } elsif ($prop eq 'svn:special') {
2682 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2684 $self->{file_prop
}->{$fb->{path
}} ||= {};
2685 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2690 sub apply_textdelta
{
2691 my ($self, $fb, $exp) = @_;
2692 my $fh = IO
::File
->new_tmpfile;
2694 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2695 # (but $base does not,) so dup() it for reading in close_file
2696 open my $dup, '<&', $fh or croak
$!;
2697 my $base = IO
::File
->new_tmpfile;
2698 $base->autoflush(1);
2700 defined (my $pid = fork) or croak
$!;
2702 open STDOUT
, '>&', $base or croak
$!;
2703 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2704 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2710 seek $base, 0, 0 or croak
$!;
2711 my $md5 = Digest
::MD5
->new;
2712 $md5->addfile($base);
2713 my $got = $md5->hexdigest;
2714 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2716 " got: $got\n" if ($got ne $exp);
2719 seek $base, 0, 0 or croak
$!;
2721 $fb->{base
} = $base;
2722 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2726 my ($self, $fb, $exp) = @_;
2728 my $path = $fb->{path
};
2729 if (my $fh = $fb->{fh
}) {
2730 seek($fh, 0, 0) or croak
$!;
2731 my $md5 = Digest
::MD5
->new;
2733 my $got = $md5->hexdigest;
2734 die "Checksum mismatch: $path\n",
2735 "expected: $exp\n got: $got\n" if ($got ne $exp);
2736 seek($fh, 0, 0) or croak
$!;
2737 if ($fb->{mode_b
} == 120000) {
2738 read($fh, my $buf, 5) == 5 or croak
$!;
2739 $buf eq 'link ' or die "$path has mode 120000",
2740 "but is not a link\n";
2742 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2744 open STDIN
, '<&', $fh or croak
$!;
2745 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2747 chomp($hash = do { local $/; <$out> });
2748 close $out or croak
$!;
2749 close $fh or croak
$!;
2750 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2751 close $fb->{base
} or croak
$!;
2753 $hash = $fb->{blob
} or die "no blob information\n";
2756 my $gui = $self->{gui
};
2757 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
2758 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $self->{q
};
2764 eval { command_close_pipe
($self->{gui
}, $self->{ctx
}) };
2765 $self->SUPER::abort_edit
(@_);
2770 command_close_pipe
($self->{gui
}, $self->{ctx
});
2771 $self->{git_commit_ok
} = 1;
2772 $self->SUPER::close_edit
(@_);
2775 package SVN
::Git
::Editor
;
2781 use Git qw
/command command_oneline command_noisy
2782 command_output_pipe command_input_pipe command_close_pipe
/;
2786 my $git_svn = shift;
2787 my $self = SVN
::Delta
::Editor
->new(@_);
2788 bless $self, $class;
2789 foreach (qw
/svn_path c r ra /) {
2790 die "$_ required!\n" unless (defined $git_svn->{$_});
2791 $self->{$_} = $git_svn->{$_};
2793 $self->{pool
} = SVN
::Pool
->new;
2794 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2796 require Digest
::MD5
;
2801 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2805 (defined $_[1] && length $_[1]) ?
$_[1] : ''
2809 my ($self, $path) = @_;
2810 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
2814 my ($self, $q) = @_;
2815 my $rm = $self->{rm
};
2816 delete $rm->{''}; # we never delete the url we're tracking
2819 foreach (keys %$rm) {
2820 my @d = split m
#/#, $_;
2824 $c .= '/' . shift @d;
2828 delete $rm->{$self->{svn_path
}};
2829 delete $rm->{''}; # we never delete the url we're tracking
2832 my ($fh, $ctx) = command_output_pipe
(
2833 qw
/ls-tree --name-only -r -z/, $self->{c
});
2837 my @dn = split m
#/#, $_;
2839 delete $rm->{join '/', @dn};
2842 eval { command_close_pipe
($fh) };
2846 command_close_pipe
($fh, $ctx);
2848 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2849 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2850 $self->close_directory($bat->{$d}, $p);
2851 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2852 print "\tD+\t/$d/\n" unless $q;
2853 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2858 sub open_or_add_dir
{
2859 my ($self, $full_path, $baton) = @_;
2860 my $p = SVN
::Pool
->new;
2861 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
2863 if ($t == $SVN::Node
::none
) {
2864 return $self->add_directory($full_path, $baton,
2865 undef, -1, $self->{pool
});
2866 } elsif ($t == $SVN::Node
::dir
) {
2867 return $self->open_directory($full_path, $baton,
2868 $self->{r
}, $self->{pool
});
2870 print STDERR
"$full_path already exists in repository at ",
2871 "r$self->{r} and it is not a directory (",
2872 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2877 my ($self, $path) = @_;
2878 my $bat = $self->{bat
};
2879 $path = $self->repo_path($path);
2880 return $bat->{''} unless (length $path);
2881 my @p = split m
#/+#, $path;
2883 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2886 $c .= '/' . shift @p;
2887 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2893 my ($self, $m, $q) = @_;
2894 my ($dir, $file) = split_path
($m->{file_b
});
2895 my $pbat = $self->ensure_path($dir);
2896 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2898 print "\tA\t$m->{file_b}\n" unless $q;
2899 $self->chg_file($fbat, $m);
2900 $self->close_file($fbat,undef,$self->{pool
});
2904 my ($self, $m, $q) = @_;
2905 my ($dir, $file) = split_path
($m->{file_b
});
2906 my $pbat = $self->ensure_path($dir);
2907 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2908 $self->url_path($m->{file_a
}), $self->{r
});
2909 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
2910 $self->chg_file($fbat, $m);
2911 $self->close_file($fbat,undef,$self->{pool
});
2915 my ($self, $path, $pbat) = @_;
2916 my $rpath = $self->repo_path($path);
2917 my ($dir, $file) = split_path
($rpath);
2918 $self->{rm
}->{$dir} = 1;
2919 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2923 my ($self, $m, $q) = @_;
2924 my ($dir, $file) = split_path
($m->{file_b
});
2925 my $pbat = $self->ensure_path($dir);
2926 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2927 $self->url_path($m->{file_a
}), $self->{r
});
2928 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
2929 $self->chg_file($fbat, $m);
2930 $self->close_file($fbat,undef,$self->{pool
});
2932 ($dir, $file) = split_path
($m->{file_a
});
2933 $pbat = $self->ensure_path($dir);
2934 $self->delete_entry($m->{file_a
}, $pbat);
2938 my ($self, $m, $q) = @_;
2939 my ($dir, $file) = split_path
($m->{file_b
});
2940 my $pbat = $self->ensure_path($dir);
2941 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2942 $pbat,$self->{r
},$self->{pool
});
2943 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
2944 $self->chg_file($fbat, $m);
2945 $self->close_file($fbat,undef,$self->{pool
});
2948 sub T
{ shift->M(@_) }
2950 sub change_file_prop
{
2951 my ($self, $fbat, $pname, $pval) = @_;
2952 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2956 my ($self, $fbat, $m) = @_;
2957 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2958 $self->change_file_prop($fbat,'svn:executable','*');
2959 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2960 $self->change_file_prop($fbat,'svn:executable',undef);
2962 my $fh = IO
::File
->new_tmpfile or croak
$!;
2963 if ($m->{mode_b
} =~ /^120/) {
2964 print $fh 'link ' or croak
$!;
2965 $self->change_file_prop($fbat,'svn:special','*');
2966 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2967 $self->change_file_prop($fbat,'svn:special',undef);
2969 defined(my $pid = fork) or croak
$!;
2971 open STDOUT
, '>&', $fh or croak
$!;
2972 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2976 $fh->flush == 0 or croak
$!;
2977 seek $fh, 0, 0 or croak
$!;
2979 my $md5 = Digest
::MD5
->new;
2980 $md5->addfile($fh) or croak
$!;
2981 seek $fh, 0, 0 or croak
$!;
2983 my $exp = $md5->hexdigest;
2984 my $pool = SVN
::Pool
->new;
2985 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2986 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2987 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2990 close $fh or croak
$!;
2994 my ($self, $m, $q) = @_;
2995 my ($dir, $file) = split_path
($m->{file_b
});
2996 my $pbat = $self->ensure_path($dir);
2997 print "\tD\t$m->{file_b}\n" unless $q;
2998 $self->delete_entry($m->{file_b
}, $pbat);
3003 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3004 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3005 $self->close_directory($bat->{$_}, $p);
3007 $self->SUPER::close_edit
($p);
3013 $self->SUPER::abort_edit
($self->{pool
});
3014 $self->{pool
}->clear;
3021 $log_msg hashref as returned by libsvn_log_entry
()
3023 msg
=> 'whitespace-formatted log entry
3024 ', # trailing newline is preserved
3025 revision
=> '8', # integer
3026 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3027 author
=> 'committer name'
3030 @mods = array of diff
-index line hashes
, each element represents one line
3031 of diff
-index output
3033 diff
-index line
($m hash
)
3035 mode_a
=> first column of diff
-index output
, no leading
':',
3036 mode_b
=> second column of diff
-index output
,
3037 sha1_b
=> sha1sum of the final blob
,
3038 chg
=> change type
[MCRADT
],
3039 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3040 file_b
=> new
/current file name of a file
(any chg
)
3044 # retval of read_url_paths{,_all}();
3046 # repository root url
3047 'https://svn.musicpd.org' => {
3048 # repository path # GIT_SVN_ID
3049 'mpd/trunk' => 'trunk',
3050 'mpd/tags/0.11.5' => 'tags/0.11.5',
3055 I don
't trust the each() function on unless I created %hash myself
3056 because the internal iterator may not have started at base.