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,
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
=> [ \
&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,
138 'multi-fetch' => [ \
&multi_fetch
,
139 'Fetch multiple trees (like git-svnimport)',
141 'log' => [ \
&show_log
, 'Show commit logs',
142 { 'limit=i' => \
$_limit,
143 'revision|r=s' => \
$_revision,
144 'verbose|v' => \
$_verbose,
145 'incremental' => \
$_incremental,
146 'oneline' => \
$_oneline,
147 'show-commit' => \
$_show_commit,
148 'non-recursive' => \
$_non_recursive,
149 'authors-file|A=s' => \
$_authors,
151 'pager=s' => \
$_pager,
153 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
154 { 'message|m=s' => \
$_message,
155 'file|F=s' => \
$_file,
156 'revision|r=s' => \
$_revision,
161 for (my $i = 0; $i < @ARGV; $i++) {
162 if (defined $cmd{$ARGV[$i]}) {
169 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
171 read_repo_config
(\
%opts);
172 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
173 'version|V' => \
$_version,
174 'id|i=s' => \
$GIT_SVN);
175 exit 1 if (!$rv && $cmd ne 'log');
179 version
() if $_version;
180 usage
(1) unless defined $cmd;
182 load_authors
() if $_authors;
183 load_all_refs
() if $_branch_all_refs;
184 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
185 $cmd{$cmd}->[0]->(@ARGV);
188 ####################### primary functions ######################
190 my $exit = shift || 0;
191 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
193 git
-svn
- bidirectional operations between a single Subversion tree
and git
194 Usage
: $0 <command
> [options
] [arguments
]\n
196 print $fd "Available commands:\n" unless $cmd;
198 foreach (sort keys %cmd) {
199 next if $cmd && $cmd ne $_;
200 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
201 foreach (keys %{$cmd{$_}->[2]}) {
202 # prints out arguments as they should be passed:
203 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
204 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
206 split /\|/,$_)," $x\n";
210 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
211 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
212 one git repository and want to keep them separate. See git-svn(1) for more
219 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
224 if (!verify_ref("refs/remotes/$GIT_SVN^0")) {
227 $SVN_URL = shift or undef;
230 command_noisy('update
-ref',"refs/remotes/$GIT_SVN","
233 check_upgrade_needed();
236 my ($rev_list, $ctx) = command_output_pipe("rev-list",
237 "refs/remotes/$GIT_SVN");
239 while (<$rev_list>) {
242 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
243 my @commit = grep(/^git-svn-id: /,
244 command(qw/cat-file commit/, $c));
245 next if (!@commit); # skip merges
246 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
247 if (!defined $rev || !$uuid) {
248 croak "Unable to extract revision or UUID from ",
249 "$c, $commit[$#commit]\n";
252 # if we merged or otherwise started elsewhere, this is
253 # how we break out of it
254 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
255 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
257 unless (defined $latest) {
258 if (!$SVN_URL && !$url) {
259 croak "SVN repository location required: $url\n";
266 revdb_set($REVDB, $rev, $c);
267 print "r$rev = $c\n";
268 $newest_rev = $rev if ($rev > $newest_rev);
270 command_close_pipe($rev_list, $ctx);
274 my $url = shift or die "SVN repository location required " .
275 "as a command-line argument\n";
276 $url =~ s!/+$!!; # strip trailing slash
278 if (my $repo_path = shift) {
279 unless (-d $repo_path) {
280 mkpath([$repo_path]);
282 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
287 unless (-d $GIT_DIR) {
288 my @init_db = ('init
-db
');
289 push @init_db, "--template=$_template" if defined $_template;
290 push @init_db, "--shared" if defined $_shared;
291 command_noisy(@init_db);
297 check_upgrade_needed();
298 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
299 my $ret = fetch_lib(@_);
300 if ($ret->{commit} && !verify_ref('refs
/heads/master
^0')) {
301 command_noisy(qw(update-ref refs/heads/master),$ret->{commit
});
308 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
309 $SVN ||= libsvn_connect
($SVN_URL);
310 my ($last_rev, $last_commit) = svn_grab_base_rev
();
311 my ($base, $head) = libsvn_parse_revision
($last_rev);
313 return { revision
=> $last_rev, commit
=> $last_commit }
315 my $index = set_index
($GIT_SVN_INDEX);
317 # limit ourselves and also fork() since get_log won't release memory
318 # after processing a revision and SVN stuff seems to leak
320 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
322 if (defined $last_commit) {
323 unless (-e
$GIT_SVN_INDEX) {
324 command_noisy
('read-tree', $last_commit);
326 my $x = command_oneline
('write-tree');
327 my ($y) = (command
(qw
/cat-file commit/, $last_commit)
328 =~ /^tree ($sha1)/m);
330 unlink $GIT_SVN_INDEX or croak
$!;
331 command_noisy
('read-tree', $last_commit);
333 $x = command_oneline
('write-tree');
335 print STDERR
"trees ($last_commit) $y != $x\n",
336 "Something is seriously wrong...\n";
340 # fork, because using SVN::Pool with get_log() still doesn't
341 # seem to help enough to keep memory usage down.
342 defined(my $pid = fork) or croak
$!;
344 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
346 # Yes I'm perfectly aware that the fourth argument
347 # below is the limit revisions number. Unfortunately
348 # performance sucks with it enabled, so it's much
349 # faster to fetch revision ranges instead of relying
351 libsvn_get_log
(libsvn_dup_ra
($SVN), [''],
356 $log_msg = libsvn_fetch
(
358 $last_commit = git_commit
(
363 $log_msg = libsvn_new_tree
(@_);
364 $last_commit = git_commit
(
372 ($last_rev, $last_commit) = svn_grab_base_rev
();
373 last if ($max >= $head);
376 $max = $head if ($max > $head);
377 $SVN = libsvn_connect
($SVN_URL);
379 restore_index
($index);
380 return { revision
=> $last_rev, commit
=> $last_commit };
385 check_upgrade_needed
();
386 if ($_stdin || !@commits) {
387 print "Reading from stdin...\n";
390 if (/\b($sha1_short)\b/o) {
391 unshift @commits, $1;
396 foreach my $c (@commits) {
397 my @tmp = command
('rev-parse',$c);
398 if (scalar @tmp == 1) {
400 } elsif (scalar @tmp > 1) {
401 push @revs, reverse(command
('rev-list',@tmp));
403 die "Failed to rev-parse $c\n";
407 print "Done committing ",scalar @revs," revisions to SVN\n";
412 my ($r_last, $cmt_last) = svn_grab_base_rev
();
413 defined $r_last or die "Must have an existing revision to commit\n";
414 my $fetched = fetch
();
415 if ($r_last != $fetched->{revision
}) {
416 print STDERR
"There are new revisions that were fetched ",
417 "and need to be merged (or acknowledged) ",
418 "before committing.\n",
419 "last rev: $r_last\n",
420 " current: $fetched->{revision}\n";
424 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
425 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
428 set_svn_commit_env
();
429 foreach my $c (@revs) {
430 my $log_msg = get_commit_message
($c, $commit_msg);
432 # fork for each commit because there's a memory leak I
433 # can't track down... (it's probably in the SVN code)
434 defined(my $pid = open my $fh, '-|') or croak
$!;
436 my $ed = SVN
::Git
::Editor
->new(
438 ra
=> libsvn_dup_ra
($SVN),
440 svn_path
=> $SVN->{svn_path
},
442 $SVN->get_commit_editor(
453 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
455 print "No changes\nr$r_last = $cmt_last\n";
462 my ($r_new, $cmt_new, $no);
466 if (/^r(\d+) = ($sha1)$/o) {
467 ($r_new, $cmt_new) = ($1, $2);
468 } elsif ($_ eq 'No changes') {
473 if (! defined $r_new && ! defined $cmt_new) {
475 die "Failed to parse revision information\n";
478 ($r_last, $cmt_last) = ($r_new, $cmt_new);
486 my $head = shift || 'HEAD';
487 my $gs = "refs/remotes/$GIT_SVN";
488 my @refs = command
(qw
/rev-list --no-merges/, "$gs..$head");
490 foreach my $d (reverse @refs) {
491 if (!verify_ref
("$d~1")) {
493 "has no parent commit, and therefore ",
494 "nothing to diff against.\n",
495 "You should be working from a repository ",
496 "originally created by git-svn\n";
498 unless (defined $last_rev) {
499 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
500 unless (defined $last_rev) {
501 die "Unable to extract revision information ",
502 "from commit $d~1\n";
506 print "diff-tree $d~1 $d\n";
508 if (my $r = commit_diff
("$d~1", $d, undef, $last_rev)) {
510 } # else: no changes, same $last_rev
515 my @diff = command
('diff-tree', 'HEAD', $gs, '--');
518 @finish = qw
/rebase/;
519 push @finish, qw
/--merge/ if $_merge;
520 push @finish, "--strategy=$_strategy" if $_strategy;
521 print STDERR
"W: HEAD and $gs differ, using @finish:\n", @diff;
523 print "No changes between current HEAD and $gs\n",
524 "Resetting to the latest $gs\n";
525 @finish = qw
/reset --mixed/;
527 command_noisy
(@finish, $gs);
531 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
533 $SVN ||= libsvn_connect
($SVN_URL);
534 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
535 libsvn_traverse_ignore
(\
*STDOUT
, $SVN->{svn_path
}, $r);
539 my $gr_file = "$GIT_DIR/info/grafts";
540 my ($grafts, $comments) = read_grafts
($gr_file);
544 # temporarily disable our grafts file to make this idempotent
545 chomp($gr_sha1 = command
(qw
/hash-object -w/,$gr_file));
546 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
549 my $l_map = read_url_paths
();
550 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
551 unless ($_no_default_regex) {
552 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
553 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
554 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
556 foreach my $u (keys %$l_map) {
558 foreach my $p (keys %{$l_map->{$u}}) {
559 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
562 unless ($_no_graft_copy) {
563 graft_file_copy_lib
($grafts,$l_map,$u);
566 graft_tree_joins
($grafts);
568 write_grafts
($grafts, $comments, $gr_file);
569 unlink "$gr_file~$gr_sha1" if $gr_sha1;
576 $url =~ s
#/+$## if $url;
577 if ($_trunk !~ m
#^[a-z\+]+://#) {
578 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
580 print STDERR
"E: '$_trunk' is not a complete URL ",
581 "and a separate URL is not specified\n";
584 $_trunk = $url . $_trunk;
587 if ($GIT_SVN eq 'git-svn') {
589 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
592 unless (-d
$GIT_SVN_DIR) {
593 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
595 command_noisy
('repo-config', 'svn.trunk', $_trunk);
597 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
598 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
602 # try to do trunk first, since branches/tags
603 # may be descended from it.
604 if (-e
"$GIT_DIR/svn/trunk/info/url") {
605 fetch_child_id
('trunk', @_);
607 rec_fetch
('', "$GIT_DIR/svn", @_);
613 my $r_last = -1; # prevent dupes
614 rload_authors
() if $_authors;
620 if (defined $_revision) {
621 if ($_revision =~ /^(\d+):(\d+)$/) {
622 ($r_min, $r_max) = ($1, $2);
623 } elsif ($_revision =~ /^\d+$/) {
624 $r_min = $r_max = $_revision;
626 print STDERR
"-r$_revision is not supported, use ",
627 "standard \'git log\' arguments instead\n";
633 @args = (git_svn_log_cmd
($r_min, $r_max), @args);
634 my $log = command_output_pipe
(@args);
639 if (/^${_esc_color}commit ($sha1_short)/o) {
641 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
643 process_commit
($c, $r_min, $r_max, \
@k) or
648 } elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
649 get_author_info
($c, $1, $2, $3);
650 } elsif (/^${_esc_color}(?:tree|parent|committer) /) {
652 } elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
653 push @
{$c->{raw
}}, $_;
654 } elsif (/^${_esc_color}[ACRMDT]\t/) {
655 # we could add $SVN->{svn_path} here, but that requires
656 # remote access at the moment (repo_path_split)...
657 s
#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
658 push @
{$c->{changed
}}, $_;
659 } elsif (/^${_esc_color}diff /) {
661 push @
{$c->{diff
}}, $_;
663 push @
{$c->{diff
}}, $_;
664 } elsif (/^${_esc_color} (git-svn-id:.+)$/) {
665 ($c->{url
}, $c->{r
}, undef) = extract_metadata
($1);
666 } elsif (s/^${_esc_color} //) {
670 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
672 process_commit
($c, $r_min, $r_max, \
@k);
678 process_commit
($_, $r_min, $r_max) foreach reverse @k;
681 eval { command_close_pipe
($log) };
682 print '-' x72
,"\n" unless $_incremental || $_oneline;
685 sub commit_diff_usage
{
686 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
691 my $ta = shift or commit_diff_usage
();
692 my $tb = shift or commit_diff_usage
();
693 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
694 print STDERR
"Needed URL or usable git-svn id command-line\n";
698 unless (defined $r) {
699 if (defined $_revision) {
702 die "-r|--revision is a required argument\n";
705 if (defined $_message && defined $_file) {
706 print STDERR
"Both --message/-m and --file/-F specified ",
707 "for the commit message.\n",
708 "I have no idea what you mean\n";
711 if (defined $_file) {
712 $_message = file_to_s
($_file);
714 $_message ||= get_commit_message
($tb,
715 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
717 $SVN ||= libsvn_connect
($SVN_URL);
719 $r = $SVN->get_latest_revnum;
720 } elsif ($r !~ /^\d+$/) {
721 die "revision argument: $r not understood by git-svn\n";
723 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
725 my $ed = SVN
::Git
::Editor
->new({ r
=> $r,
726 ra
=> libsvn_dup_ra
($SVN),
728 svn_path
=> $SVN->{svn_path
}
730 $SVN->get_commit_editor($_message,
732 $rev_committed = $_[0];
733 print "Committed $_[0]\n";
737 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
739 print "No changes\n$ta == $tb\n";
746 $_message = $_file = undef;
747 return $rev_committed;
750 ########################### utility functions #########################
754 return 1 if defined $c->{r
};
755 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
756 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
757 my @msg = command
(qw
/cat-file commit/, $c->{c
});
758 shift @msg while ($msg[0] ne "\n");
760 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
762 (undef, $c->{r
}, undef) = extract_metadata
(
763 (grep(/^git-svn-id: /, @msg))[-1]);
765 return defined $c->{r
};
771 $dcvar = 'color.diff';
772 $dc = `git-repo-config --get $dcvar`;
774 # nothing at all; fallback to "diff.color"
775 $dcvar = 'diff.color';
776 $dc = `git-repo-config --get $dcvar`;
781 $pc = `git-repo-config --get color.pager`;
783 # does not have it -- fallback to pager.color
784 $pc = `git-repo-config --bool --get pager.color`;
787 $pc = `git-repo-config --bool --get color.pager`;
793 if (-t
*STDOUT
|| (defined $_pager && $pc eq 'true')) {
794 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
798 return 0 if $dc eq 'never';
799 return 1 if $dc eq 'always';
800 chomp($dc = `git-repo-config --bool --get $dcvar`);
801 return ($dc eq 'true');
804 sub git_svn_log_cmd
{
805 my ($r_min, $r_max) = @_;
806 my @cmd = (qw
/log --abbrev
-commit
--pretty
=raw
807 --default/, "refs/remotes
/$GIT_SVN");
808 push @cmd, '-r' unless $_non_recursive;
809 push @cmd, qw/--raw --name-status/ if $_verbose;
810 push @cmd, '--color' if log_use_color();
811 return @cmd unless defined $r_max;
812 if ($r_max == $r_min) {
813 push @cmd, '--max-count=1';
814 if (my $c = revdb_get($REVDB, $r_max)) {
819 $c_max = revdb_get($REVDB, $r_max);
820 $c_min = revdb_get($REVDB, $r_min);
821 if (defined $c_min && defined $c_max) {
822 if ($r_max > $r_max) {
823 push @cmd, "$c_min..$c_max";
825 push @cmd, "$c_max..$c_min";
827 } elsif ($r_max > $r_min) {
838 print "Fetching
$id\n";
839 my $ref = "$GIT_DIR/refs/remotes
/$id";
840 defined(my $pid = open my $fh, '-|') or croak $!;
843 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
850 check_repack() if (/^r\d+ = $sha1/);
852 close $fh or croak $?;
856 my ($pfx, $p, @args) = @_;
858 foreach (sort <$p/*>) {
859 if (-r "$_/info/url
") {
860 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
861 my $id = $pfx . basename $_;
862 next if $id eq 'trunk';
863 fetch_child_id($id, @args);
870 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
875 sub complete_url_ls_init {
876 my ($url, $var, $switch, $pfx) = @_;
878 print STDERR "W
: $switch not specified
\n";
882 if ($var !~ m#^[a-z\+]+://#) {
883 $var = '/' . $var if ($var !~ m#^/#);
885 print STDERR "E
: '$var' is
not a complete URL
",
886 "and a separate URL is
not specified
\n";
891 my @ls = libsvn_ls_fullurl($var);
893 defined(my $pid = fork) or croak $!;
895 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
897 if ($u !~ m!\Q$var\E/(.+)$!) {
898 print STDERR
"W: Unrecognized URL: $u\n";
899 die "This should never happen\n";
901 # don't try to init already existing refs
903 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
905 unless (-d
$GIT_SVN_DIR) {
906 print "init $u => $id\n";
914 my ($n) = ($switch =~ /^--(\w+)/);
915 command_noisy
('repo-config', "svn.$n", $var);
922 my @tmp = split m
#/#, $_;
924 while (my $x = shift @tmp) {
930 foreach (sort {length $b <=> length $a} keys %common) {
931 if ($common{$_} == @
$paths) {
938 # grafts set here are 'stronger' in that they're based on actual tree
939 # matches, and won't be deleted from merge-base checking in write_grafts()
940 sub graft_tree_joins
{
942 map_tree_joins
() if (@_branch_from && !%tree_map);
943 return unless %tree_map;
947 my @args = (qw
/rev-list --pretty=raw/, "refs/remotes/$i");
948 my ($fh, $ctx) = command_output_pipe
(@args);
950 next unless /^commit ($sha1)$/o;
952 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
953 next unless $tree_map{$t};
958 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
960 my ($s, $tz) = ($1, $2);
961 if ($tz =~ s/^\+//) {
962 $s += tz_to_s_offset
($tz);
963 } elsif ($tz =~ s/^\-//) {
964 $s -= tz_to_s_offset
($tz);
967 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
969 foreach my $p (@
{$tree_map{$t}}) {
971 my $mb = eval { command
('merge-base', $c, $p) };
972 next unless ($@
|| $?
);
974 # see if SVN says it's a relative
975 my ($url_b, $r_b, $uuid_b) =
977 next if (defined $url_b &&
979 ($url_a eq $url_b) &&
980 ($uuid_a eq $uuid_b));
981 if ($uuid_a eq $uuid_b) {
983 $grafts->{$c}->{$p} = 2;
985 } elsif ($r_b > $r_a) {
986 $grafts->{$p}->{$c} = 2;
991 my $ct = get_commit_time
($p);
993 $grafts->{$c}->{$p} = 2;
995 $grafts->{$p}->{$c} = 2;
997 # what should we do when $ct == $s ?
1000 command_close_pipe
($fh, $ctx);
1004 sub graft_file_copy_lib
{
1005 my ($grafts, $l_map, $u) = @_;
1006 my $tree_paths = $l_map->{$u};
1007 my $pfx = common_prefix
([keys %$tree_paths]);
1008 my ($repo, $path) = repo_path_split
($u.$pfx);
1009 $SVN = libsvn_connect
($repo);
1011 my ($base, $head) = libsvn_parse_revision
();
1013 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1014 my $eh = $SVN::Error
::handler
;
1015 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1017 my $pool = SVN
::Pool
->new;
1018 libsvn_get_log
(libsvn_dup_ra
($SVN), [$path],
1019 $min, $max, 0, 2, 1,
1021 libsvn_graft_file_copies
($grafts, $tree_paths,
1025 last if ($max >= $head);
1028 $max = $head if ($max > $head);
1030 $SVN::Error
::handler
= $eh;
1033 sub process_merge_msg_matches
{
1034 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1035 my (@strong, @weak);
1036 foreach (@matches) {
1037 # merging with ourselves is not interesting
1039 if ($l_map->{$u}->{$_}) {
1045 foreach my $w (@weak) {
1047 # no exact match, use branch name as regexp.
1048 my $re = qr/\Q$w\E/i;
1049 foreach (keys %{$l_map->{$u}}) {
1051 push @strong, $l_map->{$u}->{$_};
1058 foreach (keys %{$l_map->{$u}}) {
1060 push @strong, $l_map->{$u}->{$_};
1065 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1066 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1067 unless (defined $rev) {
1068 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1069 \@
(?
:[a
-f\d\
-]+)/xsm
);
1070 return unless defined $rev;
1072 foreach my $m (@strong) {
1073 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1074 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1078 sub graft_merge_msg
{
1079 my ($grafts, $l_map, $u, $p, @re) = @_;
1081 my $x = $l_map->{$u}->{$p};
1082 my $rl = rev_list_raw
($x);
1083 while (my $c = next_rev_list_entry
($rl)) {
1084 foreach my $re (@re) {
1085 my (@br) = ($c->{m
} =~ /$re/g);
1087 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1093 return if $SVN_UUID;
1094 my $pool = SVN
::Pool
->new;
1095 $SVN_UUID = $SVN->get_uuid($pool);
1101 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1102 { STDERR
=> 0 }); };
1105 sub repo_path_split
{
1106 my $full_url = shift;
1107 $full_url =~ s
#/+$##;
1109 foreach (@repo_path_split_cache) {
1110 if ($full_url =~ s
#$_##) {
1112 $full_url =~ s
#^/+##;
1113 return ($u, $full_url);
1116 my $tmp = libsvn_connect
($full_url);
1117 return ($tmp->{repos_root
}, $tmp->{svn_path
});
1121 defined $SVN_URL or croak
"SVN repository location required\n";
1122 unless (-d
$GIT_DIR) {
1123 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1125 mkpath
([$GIT_SVN_DIR]);
1126 mkpath
(["$GIT_SVN_DIR/info"]);
1127 open my $fh, '>>',$REVDB or croak
$!;
1129 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1133 sub get_tree_from_treeish
{
1135 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1136 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1138 while ($type eq 'tag') {
1139 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1141 if ($type eq 'commit') {
1142 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1144 ($expected) = ($expected =~ /^tree ($sha1)$/);
1145 die "Unable to get tree from $treeish\n" unless $expected;
1146 } elsif ($type eq 'tree') {
1147 $expected = $treeish;
1149 die "$treeish is a $type, expected tree, tag or commit\n";
1155 my ($from, $treeish) = @_;
1156 print "diff-tree $from $treeish\n";
1157 my @diff_tree = qw(diff-tree -z -r);
1158 if ($_cp_similarity) {
1159 push @diff_tree, "-C$_cp_similarity";
1161 push @diff_tree, '-C';
1163 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1164 push @diff_tree, "-l$_l" if defined $_l;
1165 push @diff_tree, $from, $treeish;
1166 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
1170 while (<$diff_fh>) {
1171 chomp $_; # this gets rid of the trailing "\0"
1172 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1173 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1174 push @mods, { mode_a
=> $1, mode_b
=> $2,
1175 sha1_b
=> $3, chg
=> $4 };
1176 if ($4 =~ /^(?:C|R)$/) {
1181 } elsif ($state eq 'file_a') {
1182 my $x = $mods[$#mods] or croak
"Empty array\n";
1183 if ($x->{chg
} !~ /^(?:C|R)$/) {
1184 croak
"Error parsing $_, $x->{chg}\n";
1188 } elsif ($state eq 'file_b') {
1189 my $x = $mods[$#mods] or croak
"Empty array\n";
1190 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1191 croak
"Error parsing $_, $x->{chg}\n";
1193 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1194 croak
"Error parsing $_, $x->{chg}\n";
1199 croak
"Error parsing $_\n";
1202 command_close_pipe
($diff_fh, $ctx);
1206 sub libsvn_checkout_tree
{
1207 my ($from, $treeish, $ed) = @_;
1208 my $mods = get_diff
($from, $treeish);
1209 return $mods unless (scalar @
$mods);
1210 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1211 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1213 if (defined $o{$f}) {
1216 croak
"Invalid change type: $f\n";
1219 $ed->rmdirs($_q) if $_rmdir;
1223 sub get_commit_message
{
1224 my ($commit, $commit_msg) = (@_);
1225 my %log_msg = ( msg
=> '' );
1226 open my $msg, '>', $commit_msg or croak
$!;
1228 my $type = command_oneline
(qw
/cat-file -t/, $commit);
1229 if ($type eq 'commit' || $type eq 'tag') {
1230 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1235 $in_msg = 1 if (/^\s*$/);
1236 } elsif (/^git-svn-id: /) {
1237 # skip this, we regenerate the correct one
1238 # on re-fetch anyways
1240 print $msg $_ or croak
$!;
1243 command_close_pipe
($msg_fh, $ctx);
1245 close $msg or croak
$!;
1247 if ($_edit || ($type eq 'tree')) {
1248 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1249 system($editor, $commit_msg);
1252 # file_to_s removes all trailing newlines, so just use chomp() here:
1253 open $msg, '<', $commit_msg or croak
$!;
1254 { local $/; chomp($log_msg{msg
} = <$msg>); }
1255 close $msg or croak
$!;
1260 sub set_svn_commit_env
{
1261 if (defined $LC_ALL) {
1262 $ENV{LC_ALL
} = $LC_ALL;
1264 delete $ENV{LC_ALL
};
1269 my ($fh, $c) = command_output_pipe
(qw
/rev-list --pretty=raw/, @_);
1270 return { fh
=> $fh, ctx
=> $c, t
=> { } };
1273 sub next_rev_list_entry
{
1278 if (/^commit ($sha1)$/o) {
1280 $rl->{t
} = { c
=> $1 };
1285 } elsif (/^parent ($sha1)$/o) {
1292 command_close_pipe
($fh, $rl->{ctx
});
1293 return ($x != $rl->{t
}) ?
$x : undef;
1297 my ($str, $file, $mode) = @_;
1298 open my $fd,'>',$file or croak
$!;
1299 print $fd $str,"\n" or croak
$!;
1300 close $fd or croak
$!;
1301 chmod ($mode &~ umask, $file) if (defined $mode);
1306 open my $fd,'<',$file or croak
"$!: file: $file\n";
1309 close $fd or croak
$!;
1314 sub assert_revision_unknown
{
1316 if (my $c = revdb_get
($REVDB, $r)) {
1317 croak
"$r = $c already exists! Why are we refetching it?";
1322 my ($log_msg, @parents) = @_;
1323 assert_revision_unknown
($log_msg->{revision
});
1324 map_tree_joins
() if (@_branch_from && !%tree_map);
1326 my (@tmp_parents, @exec_parents, %seen_parent);
1327 if (my $lparents = $log_msg->{parents
}) {
1328 @tmp_parents = @
$lparents
1330 # commit parents can be conditionally bound to a particular
1331 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1332 foreach my $p (@parents) {
1333 next unless defined $p;
1334 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1335 if ($1 == $log_msg->{revision
}) {
1336 push @tmp_parents, $2;
1339 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1342 my $tree = $log_msg->{tree
};
1343 if (!defined $tree) {
1344 my $index = set_index
($GIT_SVN_INDEX);
1345 $tree = command_oneline
('write-tree');
1347 restore_index
($index);
1349 # just in case we clobber the existing ref, we still want that ref
1351 if (my $cur = verify_ref
("refs/remotes/$GIT_SVN^0")) {
1353 push @tmp_parents, $cur;
1356 if (exists $tree_map{$tree}) {
1357 foreach my $p (@
{$tree_map{$tree}}) {
1359 foreach (@tmp_parents) {
1360 # see if a common parent is found
1361 my $mb = eval { command
('merge-base', $_, $p) };
1367 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
1368 next if (($SVN_UUID eq $uuid_p) &&
1369 ($log_msg->{revision
} > $r_p));
1370 next if (defined $url_p && defined $SVN_URL &&
1371 ($SVN_UUID eq $uuid_p) &&
1372 ($url_p eq $SVN_URL));
1373 push @tmp_parents, $p;
1376 foreach (@tmp_parents) {
1377 next if $seen_parent{$_};
1378 $seen_parent{$_} = 1;
1379 push @exec_parents, $_;
1380 # MAXPARENT is defined to 16 in commit-tree.c:
1381 last if @exec_parents > 16;
1384 set_commit_env
($log_msg);
1385 my @exec = ('git-commit-tree', $tree);
1386 push @exec, '-p', $_ foreach @exec_parents;
1387 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1389 print $msg_fh $log_msg->{msg
} or croak
$!;
1390 unless ($_no_metadata) {
1391 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1392 " $SVN_UUID\n" or croak
$!;
1394 $msg_fh->flush == 0 or croak
$!;
1395 close $msg_fh or croak
$!;
1396 chomp(my $commit = do { local $/; <$out_fh> });
1397 close $out_fh or croak
$!;
1400 if ($commit !~ /^$sha1$/o) {
1401 die "Failed to commit, invalid sha1: $commit\n";
1403 command_noisy
('update-ref',"refs/remotes/$GIT_SVN",$commit);
1404 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
1406 # this output is read via pipe, do not change:
1407 print "r$log_msg->{revision} = $commit\n";
1413 if ($_repack && (--$_repack_nr == 0)) {
1414 $_repack_nr = $_repack;
1415 # repack doesn't use any arguments with spaces in them, does it?
1416 command_noisy
('repack', split(/\s+/, $_repack_flags));
1420 sub set_commit_env
{
1422 my $author = $log_msg->{author
};
1423 if (!defined $author || length $author == 0) {
1424 $author = '(no author)';
1426 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
1427 : ($author,"$author\@$SVN_UUID");
1428 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1429 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1430 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
1433 sub check_upgrade_needed
{
1435 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
1436 open my $fh, '>>',$REVDB or croak
$!;
1439 return unless eval {
1440 command
([qw
/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
1443 my $head = eval { command
('rev-parse',"refs/remotes/$GIT_SVN") };
1445 print STDERR
"Please run: $0 rebuild --upgrade\n";
1450 # fills %tree_map with a reverse mapping of trees to commits. Useful
1451 # for finding parents to commit on.
1452 sub map_tree_joins
{
1454 foreach my $br (@_branch_from) {
1455 my $pipe = command_output_pipe
(qw
/rev
-list
1456 --topo
-order
--pretty
=raw
/, $br);
1458 if (/^commit ($sha1)$/o) {
1461 # if we've seen a commit,
1462 # we've seen its parents
1463 last if $seen{$commit};
1464 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1465 unless (defined $tree) {
1466 die "Failed to parse commit $commit\n";
1468 push @
{$tree_map{$tree}}, $commit;
1472 eval { command_close_pipe
($pipe) };
1477 if (@_branch_from) {
1478 print STDERR
'--branch|-b parameters are ignored when ',
1479 "--branch-all-refs|-B is passed\n";
1482 # don't worry about rev-list on non-commit objects/tags,
1483 # it shouldn't blow up if a ref is a blob or tree...
1484 @_branch_from = command
(qw
/rev-parse --symbolic --all/);
1487 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1489 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1490 while (<$authors>) {
1492 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1493 my ($user, $name, $email) = ($1, $2, $3);
1494 $users{$user} = [$name, $email];
1496 close $authors or croak
$!;
1500 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1501 while (<$authors>) {
1503 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1504 my ($user, $name, $email) = ($1, $2, $3);
1505 $rusers{"$name <$email>"} = $user;
1507 close $authors or croak
$!;
1512 foreach (command
(qw
/rev-parse --symbolic --all/)) {
1513 next unless s
#^refs/remotes/##;
1515 next unless -f
"$GIT_DIR/svn/$_/info/url";
1523 defined(my $pid = fork) or croak
$!;
1525 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
1527 exit 0 if -r
$REVDB;
1528 print "Upgrading svn => git mapping...\n";
1529 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
1530 open my $fh, '>>',$REVDB or croak
$!;
1533 print "Done upgrading. You may now delete the ",
1534 "deprecated $GIT_SVN_DIR/revs directory\n";
1542 sub migration_check
{
1543 migrate_revdb
() unless (-e
$REVDB);
1544 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
1545 print "Upgrading repository...\n";
1546 unless (-d
"$GIT_DIR/svn") {
1547 mkdir "$GIT_DIR/svn" or croak
$!;
1549 print "Data from a previous version of git-svn exists, but\n\t",
1550 "$GIT_SVN_DIR\n\t(required for this version ",
1551 "($VERSION) of git-svn) does not.\n";
1553 foreach my $x (command
(qw
/rev-parse --symbolic --all/)) {
1554 next unless $x =~ s
#^refs/remotes/##;
1556 next unless -f
"$GIT_DIR/$x/info/url";
1557 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
1559 my $dn = dirname
("$GIT_DIR/svn/$x");
1560 mkpath
([$dn]) unless -d
$dn;
1561 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
1563 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
1564 print "Done upgrading.\n";
1567 sub find_rev_before
{
1568 my ($r, $id, $eq_ok) = @_;
1569 my $f = "$GIT_DIR/svn/$id/.rev_db";
1570 return (undef,undef) unless -r
$f;
1573 if (my $c = revdb_get
($f, $r)) {
1578 return (undef, undef);
1582 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
1583 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1584 $REVDB = "$GIT_SVN_DIR/.rev_db";
1585 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1587 $SVN_WC = "$GIT_SVN_DIR/tree";
1591 # convert GetOpt::Long specs for use by git-repo-config
1592 sub read_repo_config
{
1593 return unless -d
$GIT_DIR;
1595 foreach my $o (keys %$opts) {
1596 my $v = $opts->{$o};
1597 my ($key) = ($o =~ /^([a-z\-]+)/);
1599 my $arg = 'git-repo-config';
1600 $arg .= ' --int' if ($o =~ /[:=]i$/);
1601 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1602 if (ref $v eq 'ARRAY') {
1603 chomp(my @tmp = `$arg --get-all svn.$key`);
1606 chomp(my $tmp = `$arg --get svn.$key`);
1607 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
1614 sub set_default_vals
{
1615 if (defined $_repack) {
1616 $_repack = 1000 if ($_repack <= 0);
1617 $_repack_nr = $_repack;
1618 $_repack_flags ||= '-d';
1623 my $gr_file = shift;
1624 my ($grafts, $comments) = ({}, {});
1625 if (open my $fh, '<', $gr_file) {
1628 if (/^($sha1)\s+/) {
1631 @
{$comments->{$c}} = @tmp;
1634 foreach my $p (split /\s+/, $_) {
1635 $grafts->{$c}->{$p} = 1;
1641 close $fh or croak
$!;
1642 @
{$comments->{'END'}} = @tmp if @tmp;
1644 return ($grafts, $comments);
1648 my ($grafts, $comments, $gr_file) = @_;
1650 open my $fh, '>', $gr_file or croak
$!;
1651 foreach my $c (sort keys %$grafts) {
1652 if ($comments->{$c}) {
1653 print $fh $_ foreach @
{$comments->{$c}};
1655 my $p = $grafts->{$c};
1656 my %x; # real parents
1657 delete $p->{$c}; # commits are not self-reproducing...
1658 my $ch = command_output_pipe
(qw
/cat-file commit/, $c);
1660 if (/^parent ($sha1)/) {
1661 $x{$1} = $p->{$1} = 1;
1666 eval { command_close_pipe
($ch) }; # breaking the pipe
1668 # if real parents are the only ones in the grafts, drop it
1669 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1673 @ip = @jp = keys %$p;
1674 foreach my $i (@ip) {
1675 next if $del{$i} || $p->{$i} == 2;
1676 foreach my $j (@jp) {
1677 next if $i eq $j || $del{$j} || $p->{$j} == 2;
1678 $mb = eval { command
('merge-base', $i, $j) };
1685 } elsif ($mb eq $i) {
1692 # if real parents are the only ones in the grafts, drop it
1693 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1695 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1697 if ($comments->{'END'}) {
1698 print $fh $_ foreach @
{$comments->{'END'}};
1700 close $fh or croak
$!;
1703 sub read_url_paths_all
{
1704 my ($l_map, $pfx, $p) = @_;
1707 if (-r
"$_/info/url") {
1708 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
1709 my $id = $pfx . basename
$_;
1710 my $url = file_to_s
("$_/info/url");
1711 my ($u, $p) = repo_path_split
($url);
1712 $l_map->{$u}->{$p} = $id;
1719 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
1720 read_url_paths_all
($l_map, $x, $_);
1724 # this one only gets ids that have been imported, not new ones
1725 sub read_url_paths
{
1727 git_svn_each
(sub { my $x = shift;
1728 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
1729 my ($u, $p) = repo_path_split
($url);
1730 $l_map->{$u}->{$p} = $x;
1735 sub extract_metadata
{
1736 my $id = shift or return (undef, undef, undef);
1737 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
1739 if (!defined $rev || !$uuid || !$url) {
1740 # some of the original repositories I made had
1741 # identifiers like this:
1742 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1744 return ($url, $rev, $uuid);
1748 return extract_metadata
((grep(/^git-svn-id: /,
1749 command
(qw
/cat-file commit/, shift)))[-1]);
1752 sub get_commit_time
{
1754 my $fh = command_output_pipe
(qw
/rev-list --pretty=raw -n1/, $cmt);
1756 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
1757 my ($s, $tz) = ($1, $2);
1758 if ($tz =~ s/^\+//) {
1759 $s += tz_to_s_offset
($tz);
1760 } elsif ($tz =~ s/^\-//) {
1761 $s -= tz_to_s_offset
($tz);
1763 eval { command_close_pipe
($fh) };
1766 die "Can't get commit time for commit: $cmt\n";
1769 sub tz_to_s_offset
{
1772 return ($1 * 60) + ($tz * 3600);
1775 # adapted from pager.c
1777 $_pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
1778 if (!defined $_pager) {
1780 } elsif (length $_pager == 0 || $_pager eq 'cat') {
1786 return unless -t
*STDOUT
;
1787 pipe my $rfd, my $wfd or return;
1788 defined(my $pid = fork) or croak
$!;
1790 open STDOUT
, '>&', $wfd or croak
$!;
1793 open STDIN
, '<&', $rfd or croak
$!;
1794 $ENV{LESS
} ||= 'FRSX';
1795 exec $_pager or croak
"Can't run pager: $! ($_pager)\n";
1798 sub get_author_info
{
1799 my ($dest, $author, $t, $tz) = @_;
1800 $author =~ s/(?:^\s*|\s*$)//g;
1801 $dest->{a_raw
} = $author;
1804 $_a = $rusers{$author} || undef;
1807 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1812 # Date::Parse isn't in the standard Perl distro :(
1813 if ($tz =~ s/^\+//) {
1814 $t += tz_to_s_offset
($tz);
1815 } elsif ($tz =~ s/^\-//) {
1816 $t -= tz_to_s_offset
($tz);
1818 $dest->{t_utc
} = $t;
1821 sub process_commit
{
1822 my ($c, $r_min, $r_max, $defer) = @_;
1823 if (defined $r_min && defined $r_max) {
1824 if ($r_min == $c->{r
} && $r_min == $r_max) {
1828 return 1 if $r_min == $r_max;
1829 if ($r_min < $r_max) {
1830 # we need to reverse the print order
1831 return 0 if (defined $_limit && --$_limit < 0);
1835 if ($r_min != $r_max) {
1836 return 1 if ($r_min < $c->{r
});
1837 return 1 if ($r_max > $c->{r
});
1840 return 0 if (defined $_limit && --$_limit < 0);
1849 if (my $l = $c->{l
}) {
1850 while ($l->[0] =~ /^\s*$/) { shift @
$l }
1853 $_l_fmt ||= 'A' . length($c->{r
});
1854 print 'r',pack($_l_fmt, $c->{r
}),' | ';
1855 print "$c->{c} | " if $_show_commit;
1858 show_commit_normal
($c);
1862 sub show_commit_changed_paths
{
1864 return unless $c->{changed
};
1865 print "Changed paths:\n", @
{$c->{changed
}};
1868 sub show_commit_normal
{
1870 print '-' x72
, "\nr$c->{r} | ";
1871 print "$c->{c} | " if $_show_commit;
1872 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
1873 localtime($c->{t_utc
})), ' | ';
1876 if (my $l = $c->{l
}) {
1877 while ($l->[$#$l] eq "\n" && $#$l > 0
1878 && $l->[($#$l - 1)] eq "\n") {
1881 $nr_line = scalar @
$l;
1883 print "1 line\n\n\n";
1885 if ($nr_line == 1) {
1886 $nr_line = '1 line';
1888 $nr_line .= ' lines';
1890 print $nr_line, "\n";
1891 show_commit_changed_paths
($c);
1893 print $_ foreach @
$l;
1897 show_commit_changed_paths
($c);
1901 foreach my $x (qw
/raw diff/) {
1904 print $_ foreach @
{$c->{$x}}
1909 sub _simple_prompt
{
1910 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1911 $may_save = undef if $_no_auth_cache;
1912 $default_username = $_username if defined $_username;
1913 if (defined $default_username && length $default_username) {
1914 if (defined $realm && length $realm) {
1915 print "Authentication realm: $realm\n";
1917 $cred->username($default_username);
1919 _username_prompt
($cred, $realm, $may_save, $pool);
1921 $cred->password(_read_password
("Password for '" .
1922 $cred->username . "': ", $realm));
1923 $cred->may_save($may_save);
1924 $SVN::_Core
::SVN_NO_ERROR
;
1927 sub _ssl_server_trust_prompt
{
1928 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1929 $may_save = undef if $_no_auth_cache;
1930 print "Error validating server certificate for '$realm':\n";
1931 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
1932 print " - The certificate is not issued by a trusted ",
1933 "authority. Use the\n",
1934 " fingerprint to validate the certificate manually!\n";
1936 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
1937 print " - The certificate hostname does not match.\n";
1939 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
1940 print " - The certificate is not yet valid.\n";
1942 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
1943 print " - The certificate has expired.\n";
1945 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
1946 print " - The certificate has an unknown error.\n";
1948 printf( "Certificate information:\n".
1949 " - Hostname: %s\n".
1950 " - Valid: from %s until %s\n".
1952 " - Fingerprint: %s\n",
1953 map $cert_info->$_, qw(hostname valid_from valid_until
1954 issuer_dname fingerprint) );
1958 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1959 "(R)eject or accept (t)emporarily? ";
1960 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
1961 if ($choice =~ /^t$/i) {
1962 $cred->may_save(undef);
1963 } elsif ($choice =~ /^r$/i) {
1965 } elsif ($may_save && $choice =~ /^p$/i) {
1966 $cred->may_save($may_save);
1970 $cred->accepted_failures($failures);
1971 $SVN::_Core
::SVN_NO_ERROR
;
1974 sub _ssl_client_cert_prompt
{
1975 my ($cred, $realm, $may_save, $pool) = @_;
1976 $may_save = undef if $_no_auth_cache;
1977 print "Client certificate filename: ";
1978 chomp(my $filename = <STDIN
>);
1979 $cred->cert_file($filename);
1980 $cred->may_save($may_save);
1981 $SVN::_Core
::SVN_NO_ERROR
;
1984 sub _ssl_client_cert_pw_prompt
{
1985 my ($cred, $realm, $may_save, $pool) = @_;
1986 $may_save = undef if $_no_auth_cache;
1987 $cred->password(_read_password
("Password: ", $realm));
1988 $cred->may_save($may_save);
1989 $SVN::_Core
::SVN_NO_ERROR
;
1992 sub _username_prompt
{
1993 my ($cred, $realm, $may_save, $pool) = @_;
1994 $may_save = undef if $_no_auth_cache;
1995 if (defined $realm && length $realm) {
1996 print "Authentication realm: $realm\n";
1999 if (defined $_username) {
2000 $username = $_username;
2003 chomp($username = <STDIN
>);
2005 $cred->username($username);
2006 $cred->may_save($may_save);
2007 $SVN::_Core
::SVN_NO_ERROR
;
2010 sub _read_password
{
2011 my ($prompt, $realm) = @_;
2013 require Term
::ReadKey
;
2014 Term
::ReadKey
::ReadMode
('noecho');
2016 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2017 last if $key =~ /[\012\015]/; # \n\r
2020 Term
::ReadKey
::ReadMode
('restore');
2025 sub libsvn_connect
{
2027 SVN
::_Core
::svn_config_ensure
($_config_dir, undef);
2028 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2029 SVN
::Client
::get_simple_provider
(),
2030 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2031 SVN
::Client
::get_simple_prompt_provider
(
2032 \
&_simple_prompt
, 2),
2033 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2034 \
&_ssl_client_cert_prompt
, 2),
2035 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2036 \
&_ssl_client_cert_pw_prompt
, 2),
2037 SVN
::Client
::get_username_provider
(),
2038 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2039 \
&_ssl_server_trust_prompt
),
2040 SVN
::Client
::get_username_prompt_provider
(
2041 \
&_username_prompt
, 2),
2043 my $config = SVN
::Core
::config_get_config
($_config_dir);
2044 my $ra = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2046 pool
=> SVN
::Pool
->new,
2047 auth_provider_callbacks
=> $callbacks);
2048 $ra->{svn_path
} = $url;
2049 $ra->{repos_root
} = $ra->get_repos_root;
2050 $ra->{svn_path
} =~ s
#^\Q$ra->{repos_root}\E/*##;
2051 push @repo_path_split_cache, qr/^(\Q$ra->{repos_root}\E)/;
2055 sub libsvn_can_do_switch
{
2056 unless (defined $_svn_can_do_switch) {
2057 my $pool = SVN
::Pool
->new;
2059 $SVN->do_switch(1, '', 0, $SVN->{url
},
2060 SVN
::Delta
::Editor
->new, $pool);
2063 $_svn_can_do_switch = 0;
2065 $rep->abort_report($pool);
2066 $_svn_can_do_switch = 1;
2070 $_svn_can_do_switch;
2075 SVN
::Ra
->new(map { $_ => $ra->{$_} } qw
/config url
2076 auth auth_provider_callbacks repos_root svn_path
/);
2081 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2088 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
2092 sub libsvn_log_entry
{
2093 my ($rev, $author, $date, $msg, $parents, $untracked) = @_;
2094 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2095 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2096 or die "Unable to parse date: $date\n";
2097 if (defined $author && length $author > 0 &&
2098 defined $_authors && ! defined $users{$author}) {
2099 die "Author: $author not defined in $_authors file\n";
2101 $msg = '' if ($rev == 0 && !defined $msg);
2103 open my $un, '>>', "$GIT_SVN_DIR/unhandled.log" or croak
$!;
2105 print $un "r$rev\n" or croak
$!;
2106 $h = $untracked->{empty
};
2107 foreach (sort keys %$h) {
2108 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2109 print $un " $act: ", uri_encode
($_), "\n" or croak
$!;
2110 warn "W: $act: $_\n";
2112 foreach my $t (qw
/dir_prop file_prop/) {
2113 $h = $untracked->{$t} or next;
2114 foreach my $path (sort keys %$h) {
2115 my $ppath = $path eq '' ?
'.' : $path;
2116 foreach my $prop (sort keys %{$h->{$path}}) {
2117 next if $SKIP{$prop};
2118 my $v = $h->{$path}->{$prop};
2121 uri_encode
($ppath), ' ',
2122 uri_encode
($prop), ' ',
2123 uri_encode
($v), "\n"
2127 uri_encode
($ppath), ' ',
2128 uri_encode
($prop), "\n"
2134 foreach my $t (qw
/absent_file absent_directory/) {
2135 $h = $untracked->{$t} or next;
2136 foreach my $parent (sort keys %$h) {
2137 foreach my $path (sort @
{$h->{$parent}}) {
2139 uri_encode
("$parent/$path"), "\n"
2141 warn "W: $t: $parent/$path ",
2142 "Insufficient permissions?\n";
2147 # revprops (make this optional? it's an extra network trip...)
2148 my $pool = SVN
::Pool
->new;
2149 my $rp = $SVN->rev_proplist($rev, $pool);
2150 foreach (sort keys %$rp) {
2151 next if /^svn:(?:author|date|log)$/;
2152 print $un " rev_prop: ", uri_encode
($_), ' ',
2153 uri_encode
($rp->{$_}), "\n";
2156 close $un or croak
$!;
2158 { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2159 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [],
2164 my ($gui, $last_commit, $f, $q) = @_;
2165 # remove entire directories.
2166 if (command
('ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2167 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2169 $last_commit,'--',$f);
2172 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2173 print "\tD\t$_\n" unless $q;
2175 print "\tD\t$f/\n" unless $q;
2176 command_close_pipe
($ls, $ctx);
2177 return $SVN::Node
::dir
;
2179 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2180 print "\tD\t$f\n" unless $q;
2181 return $SVN::Node
::file
;
2186 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2187 my $pool = SVN
::Pool
->new;
2188 my $ed = SVN
::Git
::Fetcher
->new({ c
=> $last_commit, q
=> $_q });
2189 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2190 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2191 my (undef, $last_rev, undef) = cmt_metadata
($last_commit);
2192 $reporter->set_path('', $last_rev, 0, @lock, $pool);
2193 $reporter->finish_report($pool);
2195 unless ($ed->{git_commit_ok
}) {
2196 die "SVN connection failed somewhere...\n";
2198 libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit], $ed);
2201 sub svn_grab_base_rev
{
2202 my $c = eval { command_oneline
([qw
/rev-parse --verify/,
2203 "refs/remotes/$GIT_SVN^0"],
2205 if (defined $c && length $c) {
2206 my ($url, $rev, $uuid) = cmt_metadata
($c);
2207 return ($rev, $c) if defined $rev;
2209 if ($_no_metadata) {
2210 my $offset = -41; # from tail
2212 open my $fh, '<', $REVDB or
2213 die "--no-metadata specified and $REVDB not readable\n";
2214 seek $fh, $offset, 2;
2216 defined $rl or return (undef, undef);
2218 while ($c ne $rl && tell $fh != 0) {
2220 seek $fh, $offset, 2;
2222 defined $rl or return (undef, undef);
2226 croak
$! if ($rev < -1);
2227 $rev = ($rev - 41) / 41;
2228 close $fh or croak
$!;
2231 return (undef, undef);
2234 sub libsvn_parse_revision
{
2236 my $head = $SVN->get_latest_revnum();
2237 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2238 return ($base + 1, $head) if (defined $base);
2241 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2242 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2243 if ($_revision =~ /^BASE:(\d+)$/) {
2244 return ($base + 1, $1) if (defined $base);
2247 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2248 die "revision argument: $_revision not understood by git-svn\n",
2249 "Try using the command-line svn client instead\n";
2252 sub libsvn_traverse_ignore
{
2253 my ($fh, $path, $r) = @_;
2255 my $pool = SVN
::Pool
->new;
2256 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2258 $p =~ s
#^\Q$SVN->{svn_path}\E/##;
2259 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2260 if (my $s = $props->{'svn:ignore'}) {
2261 $s =~ s/[\r\n]+/\n/g;
2263 if (length $p == 0) {
2267 $s =~ s
#\n#\n/$p/#g;
2268 print $fh "/$p/$s\n";
2271 foreach (sort keys %$dirent) {
2272 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2273 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2279 my ($path, $r0, $r1) = @_;
2280 return 1 if $r0 == $r1;
2282 # should be OK to use Pool here (r1 - r0) should be small
2283 my $pool = SVN
::Pool
->new;
2284 libsvn_get_log
($SVN, [$path], $r0, $r1,
2285 0, 0, 1, sub {$nr++}, $pool);
2287 return 0 if ($nr > 1);
2291 sub libsvn_find_parent_branch
{
2292 my ($paths, $rev, $author, $date, $msg) = @_;
2293 my $svn_path = '/'.$SVN->{svn_path
};
2295 # look for a parent from another branch:
2296 my $i = $paths->{$svn_path} or return;
2297 my $branch_from = $i->copyfrom_path or return;
2298 my $r = $i->copyfrom_rev;
2299 print STDERR
"Found possible branch point: ",
2300 "$branch_from => $svn_path, $r\n";
2301 $branch_from =~ s
#^/##;
2303 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
2304 my $url = $SVN->{repos_root
};
2305 defined $l_map->{$url} or return;
2306 my $id = $l_map->{$url}->{$branch_from};
2307 if (!defined $id && $_follow_parent) {
2308 print STDERR
"Following parent: $branch_from\@$r\n";
2309 # auto create a new branch and follow it
2310 $id = basename
($branch_from);
2311 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
2312 while (-r
"$GIT_DIR/svn/$id") {
2313 # just grow a tail if we're not unique enough :x
2317 return unless defined $id;
2319 my ($r0, $parent) = find_rev_before
($r,$id,1);
2320 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2321 defined(my $pid = fork) or croak
$!;
2323 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2325 $SVN_URL = "$url/$branch_from";
2328 # we can't assume SVN_URL exists at r+1:
2329 $_revision = "0:$r";
2335 ($r0, $parent) = find_rev_before
($r,$id,1);
2337 return unless (defined $r0 && defined $parent);
2338 if (revisions_eq
($branch_from, $r0, $r)) {
2339 unlink $GIT_SVN_INDEX;
2340 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
2341 command_noisy
('read-tree', $parent);
2342 unless (libsvn_can_do_switch
()) {
2343 return _libsvn_new_tree
($paths, $rev, $author, $date,
2346 # do_switch works with svn/trunk >= r22312, but that is not
2347 # included with SVN 1.4.2 (the latest version at the moment),
2348 # so we can't rely on it.
2349 my $ra = libsvn_connect
("$url/$branch_from");
2350 my $ed = SVN
::Git
::Fetcher
->new({c
=> $parent, q
=> $_q });
2351 my $pool = SVN
::Pool
->new;
2352 my $reporter = $ra->do_switch($rev, '', 1, $SVN->{url
},
2354 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2355 $reporter->set_path('', $r0, 0, @lock, $pool);
2356 $reporter->finish_report($pool);
2358 unless ($ed->{git_commit_ok
}) {
2359 die "SVN connection failed somewhere...\n";
2361 return libsvn_log_entry
($rev, $author, $date, $msg, [$parent]);
2363 print STDERR
"Nope, branch point not imported or unknown\n";
2367 sub libsvn_get_log
{
2368 my ($ra, @args) = @_;
2369 $args[4]-- if $args[4] && ! $_follow_parent;
2370 if ($SVN::Core
::VERSION
le '1.2.0') {
2371 splice(@args, 3, 1);
2373 $ra->get_log(@args);
2376 sub libsvn_new_tree
{
2377 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
2380 my ($paths, $rev, $author, $date, $msg) = @_; # $pool is last
2381 _libsvn_new_tree
($paths, $rev, $author, $date, $msg, []);
2384 sub _libsvn_new_tree
{
2385 my ($paths, $rev, $author, $date, $msg, $parents) = @_;
2386 my $pool = SVN
::Pool
->new;
2387 my $ed = SVN
::Git
::Fetcher
->new({q
=> $_q});
2388 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2389 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2390 $reporter->set_path('', $rev, 1, @lock, $pool);
2391 $reporter->finish_report($pool);
2393 unless ($ed->{git_commit_ok
}) {
2394 die "SVN connection failed somewhere...\n";
2396 libsvn_log_entry
($rev, $author, $date, $msg, $parents, $ed);
2399 sub find_graft_path_commit
{
2400 my ($tree_paths, $p1, $r1) = @_;
2401 foreach my $x (keys %$tree_paths) {
2402 next unless ($p1 =~ /^\Q$x\E/);
2403 my $i = $tree_paths->{$x};
2404 my ($r0, $parent) = find_rev_before
($r1,$i,1);
2405 return $parent if (defined $r0 && $r0 == $r1);
2406 print STDERR
"r$r1 of $i not imported\n";
2412 sub find_graft_path_parents
{
2413 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2414 foreach my $x (keys %$tree_paths) {
2415 next unless ($p0 =~ /^\Q$x\E/);
2416 my $i = $tree_paths->{$x};
2417 my ($r, $parent) = find_rev_before
($r0, $i, 1);
2418 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
2419 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
2420 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
2421 next if ($url_a && $url_b && $url_a eq $url_b &&
2422 $uuid_b eq $uuid_a);
2423 $grafts->{$c}->{$parent} = 1;
2428 sub libsvn_graft_file_copies
{
2429 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2430 foreach (keys %$paths) {
2431 my $i = $paths->{$_};
2432 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2434 next unless (defined $p0 && defined $r0);
2439 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
2441 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
2446 my $old = $ENV{GIT_INDEX_FILE
};
2447 $ENV{GIT_INDEX_FILE
} = shift;
2454 $ENV{GIT_INDEX_FILE
} = $old;
2456 delete $ENV{GIT_INDEX_FILE
};
2460 sub libsvn_commit_cb
{
2461 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2462 if ($_optimize_commits && $rev == ($r_last + 1)) {
2463 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
2464 $log->{tree
} = get_tree_from_treeish
($c);
2465 my $cmt = git_commit
($log, $cmt_last, $c);
2466 my @diff = command
('diff-tree', $cmt, $c);
2468 print STDERR
"Trees differ: $cmt $c\n",
2469 join('',@diff),"\n";
2477 sub libsvn_ls_fullurl
{
2478 my $fullurl = shift;
2479 my $ra = libsvn_connect
($fullurl);
2481 my $pool = SVN
::Pool
->new;
2482 my $r = defined $_revision ?
$_revision : $ra->get_latest_revnum;
2483 my ($dirent, undef, undef) = $ra->get_dir('', $r, $pool);
2484 foreach my $d (sort keys %$dirent) {
2485 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
2486 push @ret, "$d/"; # add '/' for compat with cli svn
2494 sub libsvn_skip_unknown_revs
{
2496 my $errno = $err->apr_err();
2497 # Maybe the branch we're tracking didn't
2498 # exist when the repo started, so it's
2499 # not an error if it doesn't, just continue
2501 # Wonderfully consistent library, eh?
2502 # 160013 - svn:// and file://
2503 # 175002 - http(s)://
2504 # 175007 - http(s):// (this repo required authorization, too...)
2505 # More codes may be discovered later...
2506 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2509 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
2512 # Tie::File seems to be prone to offset errors if revisions get sparse,
2513 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2514 # one of my favorite modules is out :< Next up would be one of the DBM
2515 # modules, but I'm not sure which is most portable... So I'll just
2516 # go with something that's plain-text, but still capable of
2517 # being randomly accessed. So here's my ultra-simple fixed-width
2518 # database. All records are 40 characters + "\n", so it's easy to seek
2519 # to a revision: (41 * rev) is the byte offset.
2520 # A record of 40 0s denotes an empty revision.
2521 # And yes, it's still pretty fast (faster than Tie::File).
2523 my ($file, $rev, $commit) = @_;
2524 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
2525 open my $fh, '+<', $file or croak
$!;
2526 my $offset = $rev * 41;
2527 # assume that append is the common case:
2528 seek $fh, 0, 2 or croak
$!;
2530 if ($pos < $offset) {
2531 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
2533 seek $fh, $offset, 0 or croak
$!;
2534 print $fh $commit,"\n";
2535 close $fh or croak
$!;
2539 my ($file, $rev) = @_;
2541 my $offset = $rev * 41;
2542 open my $fh, '<', $file or croak
$!;
2543 seek $fh, $offset, 0;
2544 if (tell $fh == $offset) {
2545 $ret = readline $fh;
2548 $ret = undef if ($ret =~ /^0{40}$/);
2551 close $fh or croak
$!;
2555 sub copy_remote_ref
{
2556 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
2557 my $ref = "refs/remotes/$GIT_SVN";
2558 if (command
('ls-remote', $origin, $ref)) {
2559 command_noisy
('fetch', $origin, "$ref:$ref");
2560 } elsif ($_cp_remote && !$_upgrade) {
2561 die "Unable to find remote reference: ",
2562 "refs/remotes/$GIT_SVN on $origin\n";
2567 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2568 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2569 $SVN::Node
::none
.$SVN::Node
::file
.
2570 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2571 $SVN::Auth
::SSL
::CNMISMATCH
.
2572 $SVN::Auth
::SSL
::NOTYETVALID
.
2573 $SVN::Auth
::SSL
::EXPIRED
.
2574 $SVN::Auth
::SSL
::UNKNOWNCA
.
2575 $SVN::Auth
::SSL
::OTHER
;
2578 package SVN
::Git
::Fetcher
;
2584 use Git qw
/command command_oneline command_noisy
2585 command_output_pipe command_input_pipe command_close_pipe
/;
2587 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2589 my ($class, $git_svn) = @_;
2590 my $self = SVN
::Delta
::Editor
->new;
2591 bless $self, $class;
2592 $self->{c
} = $git_svn->{c
} if exists $git_svn->{c
};
2593 $self->{q
} = $git_svn->{q
};
2594 $self->{empty
} = {};
2595 $self->{dir_prop
} = {};
2596 $self->{file_prop
} = {};
2597 $self->{absent_dir
} = {};
2598 $self->{absent_file
} = {};
2599 ($self->{gui
}, $self->{ctx
}) = command_input_pipe
(
2600 qw
/update-index -z --index-info/);
2601 require Digest
::MD5
;
2609 sub open_directory
{
2610 my ($self, $path, $pb, $rev) = @_;
2615 my ($self, $path, $rev, $pb) = @_;
2616 my $t = process_rm
($self->{gui
}, $self->{c
}, $path, $self->{q
});
2617 $self->{empty
}->{$path} = 0 if $t == $SVN::Node
::dir
;
2622 my ($self, $path, $pb, $rev) = @_;
2623 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--',$path)
2624 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2625 unless (defined $mode && defined $blob) {
2626 die "$path was not found in commit $self->{c} (r$rev)\n";
2628 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2629 pool
=> SVN
::Pool
->new, action
=> 'M' };
2633 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2634 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2635 delete $self->{empty
}->{$dir};
2636 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2637 pool
=> SVN
::Pool
->new, action
=> 'A' };
2641 my ($self, $path, $cp_path, $cp_rev) = @_;
2642 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2643 delete $self->{empty
}->{$dir};
2644 $self->{empty
}->{$path} = 1;
2648 sub change_dir_prop
{
2649 my ($self, $db, $prop, $value) = @_;
2650 $self->{dir_prop
}->{$db->{path
}} ||= {};
2651 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2655 sub absent_directory
{
2656 my ($self, $path, $pb) = @_;
2657 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2658 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2663 my ($self, $path, $pb) = @_;
2664 $self->{absent_file
}->{$pb->{path
}} ||= [];
2665 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2669 sub change_file_prop
{
2670 my ($self, $fb, $prop, $value) = @_;
2671 if ($prop eq 'svn:executable') {
2672 if ($fb->{mode_b
} != 120000) {
2673 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2675 } elsif ($prop eq 'svn:special') {
2676 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2678 $self->{file_prop
}->{$fb->{path
}} ||= {};
2679 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2684 sub apply_textdelta
{
2685 my ($self, $fb, $exp) = @_;
2686 my $fh = IO
::File
->new_tmpfile;
2688 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2689 # (but $base does not,) so dup() it for reading in close_file
2690 open my $dup, '<&', $fh or croak
$!;
2691 my $base = IO
::File
->new_tmpfile;
2692 $base->autoflush(1);
2694 defined (my $pid = fork) or croak
$!;
2696 open STDOUT
, '>&', $base or croak
$!;
2697 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2698 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2704 seek $base, 0, 0 or croak
$!;
2705 my $md5 = Digest
::MD5
->new;
2706 $md5->addfile($base);
2707 my $got = $md5->hexdigest;
2708 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2710 " got: $got\n" if ($got ne $exp);
2713 seek $base, 0, 0 or croak
$!;
2715 $fb->{base
} = $base;
2716 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2720 my ($self, $fb, $exp) = @_;
2722 my $path = $fb->{path
};
2723 if (my $fh = $fb->{fh
}) {
2724 seek($fh, 0, 0) or croak
$!;
2725 my $md5 = Digest
::MD5
->new;
2727 my $got = $md5->hexdigest;
2728 die "Checksum mismatch: $path\n",
2729 "expected: $exp\n got: $got\n" if ($got ne $exp);
2730 seek($fh, 0, 0) or croak
$!;
2731 if ($fb->{mode_b
} == 120000) {
2732 read($fh, my $buf, 5) == 5 or croak
$!;
2733 $buf eq 'link ' or die "$path has mode 120000",
2734 "but is not a link\n";
2736 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2738 open STDIN
, '<&', $fh or croak
$!;
2739 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2741 chomp($hash = do { local $/; <$out> });
2742 close $out or croak
$!;
2743 close $fh or croak
$!;
2744 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2745 close $fb->{base
} or croak
$!;
2747 $hash = $fb->{blob
} or die "no blob information\n";
2750 my $gui = $self->{gui
};
2751 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
2752 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $self->{q
};
2758 eval { command_close_pipe
($self->{gui
}, $self->{ctx
}) };
2759 $self->SUPER::abort_edit
(@_);
2764 command_close_pipe
($self->{gui
}, $self->{ctx
});
2765 $self->{git_commit_ok
} = 1;
2766 $self->SUPER::close_edit
(@_);
2769 package SVN
::Git
::Editor
;
2775 use Git qw
/command command_oneline command_noisy
2776 command_output_pipe command_input_pipe command_close_pipe
/;
2780 my $git_svn = shift;
2781 my $self = SVN
::Delta
::Editor
->new(@_);
2782 bless $self, $class;
2783 foreach (qw
/svn_path c r ra /) {
2784 die "$_ required!\n" unless (defined $git_svn->{$_});
2785 $self->{$_} = $git_svn->{$_};
2787 $self->{pool
} = SVN
::Pool
->new;
2788 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2790 require Digest
::MD5
;
2795 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2799 (defined $_[1] && length $_[1]) ?
$_[1] : ''
2803 my ($self, $path) = @_;
2804 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
2808 my ($self, $q) = @_;
2809 my $rm = $self->{rm
};
2810 delete $rm->{''}; # we never delete the url we're tracking
2813 foreach (keys %$rm) {
2814 my @d = split m
#/#, $_;
2818 $c .= '/' . shift @d;
2822 delete $rm->{$self->{svn_path
}};
2823 delete $rm->{''}; # we never delete the url we're tracking
2826 my ($fh, $ctx) = command_output_pipe
(
2827 qw
/ls-tree --name-only -r -z/, $self->{c
});
2831 my @dn = split m
#/#, $_;
2833 delete $rm->{join '/', @dn};
2836 eval { command_close_pipe
($fh) };
2840 command_close_pipe
($fh, $ctx);
2842 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2843 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2844 $self->close_directory($bat->{$d}, $p);
2845 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2846 print "\tD+\t/$d/\n" unless $q;
2847 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2852 sub open_or_add_dir
{
2853 my ($self, $full_path, $baton) = @_;
2854 my $p = SVN
::Pool
->new;
2855 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
2857 if ($t == $SVN::Node
::none
) {
2858 return $self->add_directory($full_path, $baton,
2859 undef, -1, $self->{pool
});
2860 } elsif ($t == $SVN::Node
::dir
) {
2861 return $self->open_directory($full_path, $baton,
2862 $self->{r
}, $self->{pool
});
2864 print STDERR
"$full_path already exists in repository at ",
2865 "r$self->{r} and it is not a directory (",
2866 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2871 my ($self, $path) = @_;
2872 my $bat = $self->{bat
};
2873 $path = $self->repo_path($path);
2874 return $bat->{''} unless (length $path);
2875 my @p = split m
#/+#, $path;
2877 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2880 $c .= '/' . shift @p;
2881 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2887 my ($self, $m, $q) = @_;
2888 my ($dir, $file) = split_path
($m->{file_b
});
2889 my $pbat = $self->ensure_path($dir);
2890 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2892 print "\tA\t$m->{file_b}\n" unless $q;
2893 $self->chg_file($fbat, $m);
2894 $self->close_file($fbat,undef,$self->{pool
});
2898 my ($self, $m, $q) = @_;
2899 my ($dir, $file) = split_path
($m->{file_b
});
2900 my $pbat = $self->ensure_path($dir);
2901 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2902 $self->url_path($m->{file_a
}), $self->{r
});
2903 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
2904 $self->chg_file($fbat, $m);
2905 $self->close_file($fbat,undef,$self->{pool
});
2909 my ($self, $path, $pbat) = @_;
2910 my $rpath = $self->repo_path($path);
2911 my ($dir, $file) = split_path
($rpath);
2912 $self->{rm
}->{$dir} = 1;
2913 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2917 my ($self, $m, $q) = @_;
2918 my ($dir, $file) = split_path
($m->{file_b
});
2919 my $pbat = $self->ensure_path($dir);
2920 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2921 $self->url_path($m->{file_a
}), $self->{r
});
2922 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
2923 $self->chg_file($fbat, $m);
2924 $self->close_file($fbat,undef,$self->{pool
});
2926 ($dir, $file) = split_path
($m->{file_a
});
2927 $pbat = $self->ensure_path($dir);
2928 $self->delete_entry($m->{file_a
}, $pbat);
2932 my ($self, $m, $q) = @_;
2933 my ($dir, $file) = split_path
($m->{file_b
});
2934 my $pbat = $self->ensure_path($dir);
2935 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2936 $pbat,$self->{r
},$self->{pool
});
2937 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
2938 $self->chg_file($fbat, $m);
2939 $self->close_file($fbat,undef,$self->{pool
});
2942 sub T
{ shift->M(@_) }
2944 sub change_file_prop
{
2945 my ($self, $fbat, $pname, $pval) = @_;
2946 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2950 my ($self, $fbat, $m) = @_;
2951 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2952 $self->change_file_prop($fbat,'svn:executable','*');
2953 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2954 $self->change_file_prop($fbat,'svn:executable',undef);
2956 my $fh = IO
::File
->new_tmpfile or croak
$!;
2957 if ($m->{mode_b
} =~ /^120/) {
2958 print $fh 'link ' or croak
$!;
2959 $self->change_file_prop($fbat,'svn:special','*');
2960 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2961 $self->change_file_prop($fbat,'svn:special',undef);
2963 defined(my $pid = fork) or croak
$!;
2965 open STDOUT
, '>&', $fh or croak
$!;
2966 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2970 $fh->flush == 0 or croak
$!;
2971 seek $fh, 0, 0 or croak
$!;
2973 my $md5 = Digest
::MD5
->new;
2974 $md5->addfile($fh) or croak
$!;
2975 seek $fh, 0, 0 or croak
$!;
2977 my $exp = $md5->hexdigest;
2978 my $pool = SVN
::Pool
->new;
2979 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2980 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2981 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2984 close $fh or croak
$!;
2988 my ($self, $m, $q) = @_;
2989 my ($dir, $file) = split_path
($m->{file_b
});
2990 my $pbat = $self->ensure_path($dir);
2991 print "\tD\t$m->{file_b}\n" unless $q;
2992 $self->delete_entry($m->{file_b
}, $pbat);
2997 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2998 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2999 $self->close_directory($bat->{$_}, $p);
3001 $self->SUPER::close_edit
($p);
3007 $self->SUPER::abort_edit
($self->{pool
});
3008 $self->{pool
}->clear;
3015 $log_msg hashref as returned by libsvn_log_entry
()
3017 msg
=> 'whitespace-formatted log entry
3018 ', # trailing newline is preserved
3019 revision
=> '8', # integer
3020 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3021 author
=> 'committer name'
3024 @mods = array of diff
-index line hashes
, each element represents one line
3025 of diff
-index output
3027 diff
-index line
($m hash
)
3029 mode_a
=> first column of diff
-index output
, no leading
':',
3030 mode_b
=> second column of diff
-index output
,
3031 sha1_b
=> sha1sum of the final blob
,
3032 chg
=> change type
[MCRADT
],
3033 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3034 file_b
=> new
/current file name of a file
(any chg
)
3038 # retval of read_url_paths{,_all}();
3040 # repository root url
3041 'https://svn.musicpd.org' => {
3042 # repository path # GIT_SVN_ID
3043 'mpd/trunk' => 'trunk',
3044 'mpd/tags/0.11.5' => 'tags/0.11.5',
3049 I don
't trust the each() function on unless I created %hash myself
3050 because the internal iterator may not have started at base.