2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB
10 $_follow_parent $sha1 $sha1_short $_revision
11 $_cp_remote $_upgrade $_rmdir $_q $_cp_similarity
12 $_find_copies_harder $_l $_authors %users/;
13 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
14 $VERSION = '@@GIT_VERSION@@';
16 $ENV{GIT_DIR
} ||= '.git';
17 $Git::SVN
::default_repo_id
= 'git-svn';
18 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
20 my $LC_ALL = $ENV{LC_ALL
};
21 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
22 # make sure the svn binary gives consistent output between locales and TZs:
25 $| = 1; # unbuffer STDOUT
27 sub fatal
(@
) { print STDERR
@_; exit 1 }
28 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
31 if ($SVN::Core
::VERSION
lt '1.1.0') {
32 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
34 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
35 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
36 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
39 use File
::Basename qw
/dirname basename/;
40 use File
::Path qw
/mkpath/;
41 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
47 foreach (qw
/command command_oneline command_noisy command_output_pipe
48 command_input_pipe command_close_pipe
/) {
49 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
50 "*Git::SVN::Migration::$_ = ".
51 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
58 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
59 $sha1 = qr/[a-f\d]{40}/;
60 $sha1_short = qr/[a-f\d]{4,40}/;
61 my ($_stdin, $_help, $_edit,
62 $_repack, $_repack_nr, $_repack_flags,
63 $_message, $_file, $_no_metadata,
66 $_merge, $_strategy, $_dry_run,
69 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
70 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
71 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
);
72 my %fc_opts = ( 'follow-parent|follow' => \
$_follow_parent,
73 'authors-file|A=s' => \
$_authors,
74 'repack:i' => \
$_repack,
75 'no-metadata' => \
$_no_metadata,
77 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags,
80 my ($_trunk, $_tags, $_branches);
81 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
82 'tags|t=s' => \
$_tags,
83 'branches|b=s' => \
$_branches );
84 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
85 my %cmt_opts = ( 'edit|e' => \
$_edit,
87 'find-copies-harder' => \
$_find_copies_harder,
89 'copy-similarity|C=i'=> \
$_cp_similarity
93 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
94 { 'revision|r=s' => \
$_revision, %fc_opts } ],
95 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
96 " (requires URL argument)",
98 dcommit
=> [ \
&cmd_dcommit
,
99 'Commit several diffs to merge with upstream',
100 { 'merge|m|M' => \
$_merge,
101 'strategy|s=s' => \
$_strategy,
102 'dry-run|n' => \
$_dry_run,
103 %cmt_opts, %fc_opts } ],
104 'set-tree' => [ \
&cmd_set_tree
,
105 "Set an SVN repository to a git tree-ish",
106 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
107 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
108 { 'revision|r=i' => \
$_revision } ],
109 rebuild
=> [ \
&cmd_rebuild
, "Rebuild git-svn metadata (after git clone)",
110 { 'copy-remote|remote=s' => \
$_cp_remote,
111 'upgrade' => \
$_upgrade } ],
112 'multi-init' => [ \
&cmd_multi_init
,
113 'Initialize multiple trees (like git-svnimport)',
114 { %multi_opts, %init_opts, %remote_opts,
115 'revision|r=i' => \
$_revision,
116 'prefix=s' => \
$_prefix,
118 'multi-fetch' => [ \
&cmd_multi_fetch
,
119 'Fetch multiple trees (like git-svnimport)',
121 'migrate' => [ sub { },
122 # no-op, we automatically run this anyways,
123 'Migrate configuration/metadata/layout from
124 previous versions of git-svn',
126 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
127 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
128 'revision|r=s' => \
$_revision,
129 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
130 'incremental' => \
$Git::SVN
::Log
::incremental
,
131 'oneline' => \
$Git::SVN
::Log
::oneline
,
132 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
133 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
134 'authors-file|A=s' => \
$_authors,
135 'color' => \
$Git::SVN
::Log
::color
,
136 'pager=s' => \
$Git::SVN
::Log
::pager
,
138 'commit-diff' => [ \
&cmd_commit_diff
,
139 'Commit a diff between two trees',
140 { 'message|m=s' => \
$_message,
141 'file|F=s' => \
$_file,
142 'revision|r=s' => \
$_revision,
147 for (my $i = 0; $i < @ARGV; $i++) {
148 if (defined $cmd{$ARGV[$i]}) {
155 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
157 read_repo_config
(\
%opts);
158 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
159 'version|V' => \
$_version,
160 'minimize-connections' =>
161 \
$Git::SVN
::Migration
::_minimize
,
162 'id|i=s' => \
$Git::SVN
::default_ref_id
);
163 exit 1 if (!$rv && $cmd ne 'log');
166 version
() if $_version;
167 usage
(1) unless defined $cmd;
168 load_authors
() if $_authors;
169 unless ($cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/) {
170 Git
::SVN
::Migration
::migration_check
();
173 Git
::SVN
::verify_remotes_sanity
();
174 $cmd{$cmd}->[0]->(@ARGV);
179 ####################### primary functions ######################
181 my $exit = shift || 0;
182 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
184 git
-svn
- bidirectional operations between a single Subversion tree
and git
185 Usage
: $0 <command
> [options
] [arguments
]\n
187 print $fd "Available commands:\n" unless $cmd;
189 foreach (sort keys %cmd) {
190 next if $cmd && $cmd ne $_;
191 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
192 foreach (keys %{$cmd{$_}->[2]}) {
193 # prints out arguments as they should be passed:
194 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
195 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
197 split /\|/,$_)," $x\n";
201 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
202 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
203 one git repository and want to keep them separate. See git-svn(1) for more
210 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
216 my $gs = $url ? Git::SVN->init($url)
217 : eval { Git::SVN->new };
218 $gs ||= Git::SVN->_new;
219 if (!verify_ref($gs->refname.'^0')) {
220 $gs->copy_remote_ref;
223 my ($rev_list, $ctx) = command_output_pipe("rev-list", $gs->refname);
226 while (<$rev_list>) {
229 fatal "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
230 my ($url, $rev, $uuid) = cmt_metadata($c);
232 # ignore merges (from set-tree)
233 next if (!defined $rev || !$uuid);
235 # if we merged or otherwise started elsewhere, this is
236 # how we break out of it
237 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
238 ($gs->{url} && $url && ($url ne $gs->{url}))) {
242 unless (defined $latest) {
243 if (!$gs->{url} && !$url) {
244 fatal "SVN repository location required\n";
246 $gs = Git::SVN->init($url);
249 $gs->rev_db_set($rev, $c);
250 print "r$rev = $c\n";
252 command_close_pipe($rev_list, $ctx);
256 unless (-d $ENV{GIT_DIR}) {
257 my @init_db = ('init
');
258 push @init_db, "--template=$_template" if defined $_template;
259 push @init_db, "--shared" if defined $_shared;
260 command_noisy(@init_db);
265 my $url = shift or die "SVN repository location required " .
266 "as a command-line argument\n";
267 if (my $repo_path = shift) {
268 unless (-d $repo_path) {
269 mkpath([$repo_path]);
271 chdir $repo_path or croak $!;
272 $ENV{GIT_DIR} = $repo_path . "/.git";
276 Git::SVN->init($url);
281 die "Additional fetch arguments are no longer supported.\n",
282 "Use --follow-parent if you have moved/copied directories
285 my $gs = Git::SVN->new;
287 if ($gs->{last_commit} && !verify_ref('refs
/heads/master
^0')) {
288 command_noisy(qw(update-ref refs/heads/master),
295 if ($_stdin || !@commits) {
296 print "Reading from stdin...\n";
299 if (/\b($sha1_short)\b/o) {
300 unshift @commits, $1;
305 foreach my $c (@commits) {
306 my @tmp = command
('rev-parse',$c);
307 if (scalar @tmp == 1) {
309 } elsif (scalar @tmp > 1) {
310 push @revs, reverse(command
('rev-list',@tmp));
312 fatal
"Failed to rev-parse $c\n";
315 my $gs = Git
::SVN
->new;
316 my ($r_last, $cmt_last) = $gs->last_rev_commit;
318 if ($r_last != $gs->{last_rev
}) {
319 fatal
"There are new revisions that were fetched ",
320 "and need to be merged (or acknowledged) ",
321 "before committing.\nlast rev: $r_last\n",
322 " current: $gs->{last_rev}\n";
324 $gs->set_tree($_) foreach @revs;
325 print "Done committing ",scalar @revs," revisions to SVN\n";
330 my $gs = Git
::SVN
->new;
332 my @refs = command
(qw
/rev-list --no-merges/, $gs->refname."..$head");
334 foreach my $d (reverse @refs) {
335 if (!verify_ref
("$d~1")) {
337 "has no parent commit, and therefore ",
338 "nothing to diff against.\n",
339 "You should be working from a repository ",
340 "originally created by git-svn\n";
342 unless (defined $last_rev) {
343 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
344 unless (defined $last_rev) {
345 fatal
"Unable to extract revision information ",
346 "from commit $d~1\n";
350 print "diff-tree $d~1 $d\n";
352 my $log = get_commit_entry
($d)->{log};
354 my $pool = SVN
::Pool
->new;
355 my %ed_opts = ( r
=> $last_rev,
357 svn_path
=> $ra->{svn_path
} );
358 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
359 $ra->get_commit_editor($log,
360 sub { print "Committed r$_[0]\n";
361 $last_rev = $_[0]; }),
363 my $mods = $ed->apply_diff("$d~1", $d);
365 print "No changes\n$d~1 == $d\n";
371 # we always want to rebase against the current HEAD, not any
372 # head that was passed to us
373 my @diff = command
('diff-tree', 'HEAD', $gs->refname, '--');
376 @finish = qw
/rebase/;
377 push @finish, qw
/--merge/ if $_merge;
378 push @finish, "--strategy=$_strategy" if $_strategy;
379 print STDERR
"W: HEAD and ", $gs->refname, " differ, ",
380 "using @finish:\n", "@diff";
382 print "No changes between current HEAD and ",
383 $gs->refname, "\nResetting to the latest ",
385 @finish = qw
/reset --mixed/;
387 command_noisy
(@finish, $gs->refname);
390 sub cmd_show_ignore
{
391 my $gs = Git
::SVN
->new;
392 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
393 $gs->traverse_ignore(\
*STDOUT
, '', $r);
398 unless (defined $_trunk || defined $_branches || defined $_tags) {
402 $_prefix = '' unless defined $_prefix;
403 $url =~ s
#/+$## if defined $url;
404 if (defined $_trunk) {
405 my $trunk_ref = $_prefix . 'trunk';
406 # try both old-style and new-style lookups:
407 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
409 my ($trunk_url, $trunk_path) =
410 complete_svn_url
($url, $_trunk);
411 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
415 return unless defined $_branches || defined $_tags;
416 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
417 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
418 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
421 sub cmd_multi_fetch
{
423 foreach (command
(qw
/config -l/)) {
424 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
425 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
426 my ($repo_id, $path, $ref_id) = ($1, $2, $3);
427 push @gs, Git
::SVN
->new($ref_id, $repo_id, $path);
434 # this command is special because it requires no metadata
435 sub cmd_commit_diff
{
436 my ($ta, $tb, $url) = @_;
437 my $usage = "Usage: $0 commit-diff -r<revision> ".
438 "<tree-ish> <tree-ish> [<URL>]\n";
439 fatal
($usage) if (!defined $ta || !defined $tb);
441 my $gs = eval { Git
::SVN
->new };
443 fatal
("Needed URL or usable git-svn --id in ",
444 "the command-line\n", $usage);
448 unless (defined $_revision) {
449 fatal
("-r|--revision is a required argument\n", $usage);
451 if (defined $_message && defined $_file) {
452 fatal
("Both --message/-m and --file/-F specified ",
453 "for the commit message.\n",
454 "I have no idea what you mean\n");
456 if (defined $_file) {
457 $_message = file_to_s
($_file);
459 $_message ||= get_commit_entry
($tb)->{log};
461 my $ra ||= Git
::SVN
::Ra
->new($url);
464 $r = $ra->get_latest_revnum;
465 } elsif ($r !~ /^\d+$/) {
466 die "revision argument: $r not understood by git-svn\n";
468 my $pool = SVN
::Pool
->new;
469 my %ed_opts = ( r
=> $r,
471 svn_path
=> $ra->{svn_path
} );
472 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
473 $ra->get_commit_editor($_message,
474 sub { print "Committed r$_[0]\n" }),
476 my $mods = $ed->apply_diff($ta, $tb);
478 print "No changes\n$ta == $tb\n";
483 ########################### utility functions #########################
485 sub complete_svn_url
{
486 my ($url, $path) = @_;
488 if ($path !~ m
#^[a-z\+]+://#) {
489 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
490 fatal
("E: '$path' is not a complete URL ",
491 "and a separate URL is not specified\n");
493 return ($url, $path);
498 sub complete_url_ls_init
{
499 my ($ra, $repo_path, $switch, $pfx) = @_;
500 unless ($repo_path) {
501 print STDERR
"W: $switch not specified\n";
504 $repo_path =~ s
#/+$##;
505 if ($repo_path =~ m
#^[a-z\+]+://#) {
506 $ra = Git
::SVN
::Ra
->new($repo_path);
509 $repo_path =~ s
#^/+##;
511 fatal
("E: '$repo_path' is not a complete URL ",
512 "and a separate URL is not specified\n");
515 my $r = defined $_revision ?
$_revision : $ra->get_latest_revnum;
516 my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r);
517 my $url = $ra->{url
};
518 foreach my $d (sort keys %$dirent) {
519 next if ($dirent->{$d}->kind != $SVN::Node
::dir
);
520 my $path = "$repo_path/$d";
522 my $gs = eval { Git
::SVN
->new($ref) };
523 # don't try to init already existing refs
525 print "init $url/$path => $ref\n";
526 Git
::SVN
->init($url, $path, undef, $ref);
533 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
537 sub get_tree_from_treeish
{
539 # $treeish can be a symbolic ref, too:
540 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
542 while ($type eq 'tag') {
543 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
545 if ($type eq 'commit') {
546 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
548 ($expected) = ($expected =~ /^tree ($sha1)$/o);
549 die "Unable to get tree from $treeish\n" unless $expected;
550 } elsif ($type eq 'tree') {
551 $expected = $treeish;
553 die "$treeish is a $type, expected tree, tag or commit\n";
558 sub get_commit_entry
{
559 my ($treeish) = shift;
560 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
561 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
562 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
563 open my $log_fh, '>', $commit_editmsg or croak
$!;
565 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
566 if ($type eq 'commit' || $type eq 'tag') {
567 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
572 $in_msg = 1 if (/^\s*$/);
573 } elsif (/^git-svn-id: /) {
574 # skip this for now, we regenerate the
575 # correct one on re-fetch anyways
576 # TODO: set *:merge properties or like...
578 print $log_fh $_ or croak
$!;
581 command_close_pipe
($msg_fh, $ctx);
583 close $log_fh or croak
$!;
585 if ($_edit || ($type eq 'tree')) {
586 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
587 # TODO: strip out spaces, comments, like git-commit.sh
588 system($editor, $commit_editmsg);
590 rename $commit_editmsg, $commit_msg or croak
$!;
591 open $log_fh, '<', $commit_msg or croak
$!;
592 { local $/; chomp($log_entry{log} = <$log_fh>); }
593 close $log_fh or croak
$!;
599 my ($str, $file, $mode) = @_;
600 open my $fd,'>',$file or croak
$!;
601 print $fd $str,"\n" or croak
$!;
602 close $fd or croak
$!;
603 chmod ($mode &~ umask, $file) if (defined $mode);
608 open my $fd,'<',$file or croak
"$!: file: $file\n";
611 close $fd or croak
$!;
616 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
618 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
619 my $log = $cmd eq 'log';
622 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
623 my ($user, $name, $email) = ($1, $2, $3);
625 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
627 $users{$user} = [$name, $email];
630 close $authors or croak
$!;
633 # convert GetOpt::Long specs for use by git-config
634 sub read_repo_config
{
635 return unless -d
$ENV{GIT_DIR
};
637 foreach my $o (keys %$opts) {
639 my ($key) = ($o =~ /^([a-z\-]+)/);
641 my $arg = 'git-config';
642 $arg .= ' --int' if ($o =~ /[:=]i$/);
643 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
644 if (ref $v eq 'ARRAY') {
645 chomp(my @tmp = `$arg --get-all svn.$key`);
648 chomp(my $tmp = `$arg --get svn.$key`);
649 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
656 sub extract_metadata
{
657 my $id = shift or return (undef, undef, undef);
658 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
660 if (!defined $rev || !$uuid || !$url) {
661 # some of the original repositories I made had
662 # identifiers like this:
663 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
665 return ($url, $rev, $uuid);
669 return extract_metadata
((grep(/^git-svn-id: /,
670 command
(qw
/cat-file commit/, shift)))[-1]);
673 sub get_commit_time
{
675 my $fh = command_output_pipe
(qw
/rev-list --pretty=raw -n1/, $cmt);
677 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
678 my ($s, $tz) = ($1, $2);
679 if ($tz =~ s/^\+//) {
680 $s += tz_to_s_offset
($tz);
681 } elsif ($tz =~ s/^\-//) {
682 $s -= tz_to_s_offset
($tz);
687 die "Can't get commit time for commit: $cmt\n";
693 return ($1 * 60) + ($tz * 3600);
699 use vars qw
/$default_repo_id $default_ref_id/;
701 use File
::Path qw
/mkpath/;
704 # properties that we do not log:
707 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
708 svn
:special svn
:executable
709 svn
:entry
:committed
-rev
710 svn
:entry
:last-author
712 svn
:entry
:committed
-date
/;
715 sub read_all_remotes
{
717 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
718 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
719 $r->{$1}->{fetch
}->{$2} = $3;
720 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
721 $r->{$1}->{url
} = $2;
727 sub verify_remotes_sanity
{
728 return unless -d
$ENV{GIT_DIR
};
730 foreach (command
(qw
/config -l/)) {
731 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
733 die "Remote ref refs/remote/$1 is tracked by",
734 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
735 "Please resolve this ambiguity in ",
736 "your git configuration file before ",
744 # we allow more chars than remotes2config.sh...
745 sub sanitize_remote_name
{
747 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
751 sub find_existing_remote
{
752 my ($url, $remotes) = @_;
754 foreach my $repo_id (keys %$remotes) {
755 my $u = $remotes->{$repo_id}->{url
} or next;
757 $existing = $repo_id;
763 sub init_remote_config
{
764 my ($self, $url) = @_;
765 $url =~ s!/+$!!; # strip trailing slash
766 my $r = read_all_remotes
();
767 my $existing = find_existing_remote
($url, $r);
769 print STDERR
"Using existing ",
770 "[svn-remote \"$existing\"]\n";
771 $self->{repo_id
} = $existing;
773 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
774 $existing = find_existing_remote
($min_url, $r);
776 print STDERR
"Using existing ",
777 "[svn-remote \"$existing\"]\n";
778 $self->{repo_id
} = $existing;
780 if ($min_url ne $url) {
781 print STDERR
"Using higher level of URL: ",
782 "$url => $min_url\n";
783 my $old_path = $self->{path
};
784 $self->{path
} = $url;
785 $self->{path
} =~ s!^\Q$min_url\E/*!!;
786 if (length $old_path) {
787 $self->{path
} .= "/$old_path";
794 # verify that we aren't overwriting anything:
796 command_oneline
('config', '--get',
797 "svn-remote.$self->{repo_id}.url")
799 if ($orig_url && ($orig_url ne $url)) {
800 die "svn-remote.$self->{repo_id}.url already set: ",
801 "$orig_url\nwanted to set to: $url\n";
804 my ($xrepo_id, $xpath) = find_ref
($self->refname);
805 if (defined $xpath) {
806 die "svn-remote.$xrepo_id.fetch already set to track ",
807 "$xpath:refs/remotes/", $self->refname, "\n";
809 command_noisy
('config',
810 "svn-remote.$self->{repo_id}.url", $url);
811 command_noisy
('config', '--add',
812 "svn-remote.$self->{repo_id}.fetch",
813 "$self->{path}:".$self->refname);
818 my ($class, $url, $path, $repo_id, $ref_id) = @_;
819 my $self = _new
($class, $repo_id, $ref_id, $path);
821 $self->init_remote_config($url);
828 foreach (command
(qw
/config -l/)) {
829 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
830 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
831 my ($repo_id, $path, $ref) = ($1, $2, $3);
832 if ($ref eq $ref_id) {
833 $path = '' if ($path =~ m
#^\./?#);
834 return ($repo_id, $path);
837 (undef, undef, undef);
841 my ($class, $ref_id, $repo_id, $path) = @_;
842 if (defined $ref_id && !defined $repo_id && !defined $path) {
843 ($repo_id, $path) = find_ref
($ref_id);
844 if (!defined $repo_id) {
845 die "Could not find a \"svn-remote.*.fetch\" key ",
846 "in the repository configuration matching: ",
847 "refs/remotes/$ref_id\n";
850 my $self = _new
($class, $repo_id, $ref_id, $path);
851 if (!defined $self->{path
} || !length $self->{path
}) {
852 my $fetch = command_oneline
('config', '--get',
853 "svn-remote.$repo_id.fetch",
854 ":refs/remotes/$ref_id\$") or
855 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
856 "\":refs/remotes/$ref_id\$\" in config\n";
857 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
859 $self->{url
} = command_oneline
('config', '--get',
860 "svn-remote.$repo_id.url") or
861 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
865 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
869 $self->{ra
} ||= Git
::SVN
::Ra
->new($self->{url
});
874 my $repos_root = $self->ra->{repos_root
};
875 return $self->{path
} if ($self->{url
} eq $repos_root);
876 my $url = $self->{url
} .
877 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
878 $url =~ s!^\Q$repos_root\E/*!!g;
882 sub copy_remote_ref
{
884 my $origin = $::_cp_remote ?
$::_cp_remote
: 'origin';
885 my $ref = $self->refname;
886 if (command
('ls-remote', $origin, $ref)) {
887 command_noisy
('fetch', $origin, "$ref:$ref");
888 } elsif ($::_cp_remote
&& !$::_upgrade
) {
889 die "Unable to find remote reference: $ref on $origin\n";
893 sub traverse_ignore
{
894 my ($self, $fh, $path, $r) = @_;
896 my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
898 $p =~ s
#^\Q$self->{ra}->{svn_path}\E/##;
899 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
900 if (my $s = $props->{'svn:ignore'}) {
901 $s =~ s/[\r\n]+/\n/g;
903 if (length $p == 0) {
908 print $fh "/$p/$s\n";
911 foreach (sort keys %$dirent) {
912 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
913 $self->traverse_ignore($fh, "$path/$_", $r);
917 # returns the newest SVN revision number and newest commit SHA1
918 sub last_rev_commit
{
920 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
921 return ($self->{last_rev
}, $self->{last_commit
});
923 my $c = ::verify_ref
($self->refname.'^0');
925 my $rev = (::cmt_metadata
($c))[1];
927 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
931 my $offset = -41; # from tail
933 open my $fh, '<', $self->{db_path
} or
934 croak
"$self->{db_path} not readable: $!\n";
935 seek $fh, $offset, 2;
937 defined $rl or return (undef, undef);
939 while ($c ne $rl && tell $fh != 0) {
941 seek $fh, $offset, 2;
943 defined $rl or return (undef, undef);
947 croak
$! if ($rev < 0);
948 $rev = ($rev - 41) / 41;
949 close $fh or croak
$!;
950 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
955 my ($self, $base) = @_;
956 my $head = $self->ra->get_latest_revnum;
957 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
958 return ($base + 1, $head) if (defined $base);
961 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
962 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
963 if ($::_revision
=~ /^BASE:(\d+)$/) {
964 return ($base + 1, $1) if (defined $base);
967 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
968 die "revision argument: $::_revision not understood by git-svn\n",
969 "Try using the command-line svn client instead\n";
973 my ($self, $sub) = @_;
974 my $old_index = $ENV{GIT_INDEX_FILE
};
975 $ENV{GIT_INDEX_FILE
} = $self->{index};
978 $ENV{GIT_INDEX_FILE
} = $old_index;
980 delete $ENV{GIT_INDEX_FILE
};
982 wantarray ?
@ret : $ret[0];
985 sub assert_index_clean
{
986 my ($self, $treeish) = @_;
988 $self->tmp_index_do(sub {
989 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
990 my $x = command_oneline
('write-tree');
991 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
992 /^tree ($::sha1)/mo);
994 unlink $self->{index} or croak
$!;
995 command_noisy
('read-tree', $treeish);
997 $x = command_oneline
('write-tree');
999 ::fatal
"trees ($treeish) $y != $x\n",
1000 "Something is seriously wrong...\n";
1005 sub get_commit_parents
{
1006 my ($self, $log_entry, @parents) = @_;
1007 my (%seen, @ret, @tmp);
1008 # commit parents can be conditionally bound to a particular
1009 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1010 foreach my $p (@parents) {
1011 next unless defined $p;
1012 if ($p =~ /^(\d+)=($::sha1_short)$/o) {
1013 push @tmp, $2 if $1 == $log_entry->{revision
};
1015 push @tmp, $p if $p =~ /^$::sha1_short$/o;
1018 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1021 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1022 while (my $p = shift @tmp) {
1026 # MAXPARENT is defined to 16 in commit-tree.c:
1030 die "r$log_entry->{revision}: No room for parents:\n\t",
1031 join("\n\t", @tmp), "\n";
1038 $self->ra->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
1042 my ($self, $log_entry, @parents) = @_;
1043 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1044 croak
"$log_entry->{revision} = $c already exists! ",
1045 "Why are we refetching it?\n";
1047 my $author = $log_entry->{author
};
1048 my ($name, $email) = (defined $::users
{$author} ? @
{$::users
{$author}}
1049 : ($author, "$author\@".$self->ra->uuid));
1050 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1051 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1052 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1054 my $tree = $log_entry->{tree
};
1055 if (!defined $tree) {
1056 $tree = $self->tmp_index_do(sub {
1057 command_oneline
('write-tree') });
1059 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1061 my @exec = ('git-commit-tree', $tree);
1062 foreach ($self->get_commit_parents($log_entry, @parents)) {
1063 push @exec, '-p', $_;
1065 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1067 print $msg_fh $log_entry->{log} or croak
$!;
1068 print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
1069 $log_entry->{revision
}, ' ',
1070 $self->ra->uuid, "\n" or croak
$!;
1071 $msg_fh->flush == 0 or croak
$!;
1072 close $msg_fh or croak
$!;
1073 chomp(my $commit = do { local $/; <$out_fh> });
1074 close $out_fh or croak
$!;
1077 if ($commit !~ /^$::sha1$/o) {
1078 die "Failed to commit, invalid sha1: $commit\n";
1081 command_noisy
('update-ref',$self->refname, $commit);
1082 $self->rev_db_set($log_entry->{revision
}, $commit);
1084 $self->{last_rev
} = $log_entry->{revision
};
1085 $self->{last_commit
} = $commit;
1086 print "r$log_entry->{revision} = $commit\n";
1091 my ($self, $r0, $r1) = @_;
1092 return 1 if $r0 == $r1;
1094 $self->ra->get_log([$self->{path
}], $r0, $r1,
1095 0, 0, 1, sub { $nr++ });
1096 return 0 if ($nr > 1);
1100 sub find_parent_branch
{
1101 my ($self, $paths, $rev) = @_;
1102 return undef unless $::_follow_parent
;
1104 # look for a parent from another branch:
1105 my $abs_path = '/'.$self->rel_path;
1106 my $i = $paths->{$abs_path} or goto not_found
;
1107 my $branch_from = $i->copyfrom_path or goto not_found
;
1108 my $r = $i->copyfrom_rev;
1109 my $repos_root = $self->ra->{repos_root
};
1110 my $url = $self->ra->{url
};
1111 my $new_url = $repos_root . $branch_from;
1112 print STDERR
"Found possible branch point: ",
1113 "$new_url => ", $self->full_url, ", $r\n";
1114 $branch_from =~ s
#^/##;
1115 my $remotes = read_all_remotes
();
1117 foreach my $repo_id (keys %$remotes) {
1118 my $u = $remotes->{$repo_id}->{url
} or next;
1120 my $fetch = $remotes->{$repo_id}->{fetch
};
1121 foreach my $f (keys %$fetch) {
1122 next if $f ne $branch_from;
1123 $gs = Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1129 my $ref_id = $branch_from;
1130 $ref_id .= "\@$r" if find_ref
($ref_id);
1131 # just grow a tail if we're not unique enough :x
1132 $ref_id .= '-' while find_ref
($ref_id);
1133 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id);
1135 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1136 if ($::_follow_parent
&& (!defined $r0 || !defined $parent)) {
1138 my $log_entry = eval { $gs->do_fetch(undef, $_) };
1139 $gs->do_git_commit($log_entry) if $log_entry;
1141 ($r0, $parent) = $gs->last_rev_commit;
1143 if (defined $r0 && defined $parent && $gs->revisions_eq($r0, $r)) {
1144 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1145 $self->assert_index_clean($parent);
1147 if ($self->ra->can_do_switch) {
1148 print STDERR
"Following parent with do_switch\n";
1149 # do_switch works with svn/trunk >= r22312, but that
1150 # is not included with SVN 1.4.2 (the latest version
1151 # at the moment), so we can't rely on it
1152 $self->{last_commit
} = $parent;
1153 $ed = SVN
::Git
::Fetcher
->new($self);
1154 $gs->ra->gs_do_switch($r0, $rev, $gs->{path
}, 1,
1155 $self->full_url, $ed)
1156 or die "SVN connection failed somewhere...\n";
1158 print STDERR
"Following parent with do_update\n";
1159 $ed = SVN
::Git
::Fetcher
->new($self);
1160 $self->ra->gs_do_update($rev, $rev, $self->{path
},
1162 or die "SVN connection failed somewhere...\n";
1164 return $self->make_log_entry($rev, [$parent], $ed);
1167 print STDERR
"Branch parent for path: '$abs_path' not found\n";
1168 return undef unless $paths;
1169 foreach my $p (sort keys %$paths) {
1170 print STDERR
' ', $p->action, ' ', $p;
1171 if (my $cp_from = $p->copyfrom_path) {
1172 print STDERR
"(from $cp_from:", $p->copyfrom_rev, ')';
1180 my ($self, $paths, $rev) = @_;
1182 my ($last_rev, @parents);
1183 if ($self->{last_commit
}) {
1184 $ed = SVN
::Git
::Fetcher
->new($self);
1185 $last_rev = $self->{last_rev
};
1186 $ed->{c
} = $self->{last_commit
};
1187 @parents = ($self->{last_commit
});
1190 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1193 $ed = SVN
::Git
::Fetcher
->new($self);
1195 unless ($self->ra->gs_do_update($last_rev, $rev,
1196 $self->{path
}, 1, $ed)) {
1197 die "SVN connection failed somewhere...\n";
1199 $self->make_log_entry($rev, \
@parents, $ed);
1202 sub write_untracked
{
1203 my ($self, $rev, $fh, $untracked) = @_;
1205 print $fh "r$rev\n" or croak
$!;
1206 $h = $untracked->{empty
};
1207 foreach (sort keys %$h) {
1208 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1209 print $fh " $act: ", uri_encode
($_), "\n" or croak
$!;
1210 warn "W: $act: $_\n";
1212 foreach my $t (qw
/dir_prop file_prop/) {
1213 $h = $untracked->{$t} or next;
1214 foreach my $path (sort keys %$h) {
1215 my $ppath = $path eq '' ?
'.' : $path;
1216 foreach my $prop (sort keys %{$h->{$path}}) {
1217 next if $SKIP_PROP{$prop};
1218 my $v = $h->{$path}->{$prop};
1221 uri_encode
($ppath), ' ',
1222 uri_encode
($prop), ' ',
1223 uri_encode
($v), "\n"
1227 uri_encode
($ppath), ' ',
1228 uri_encode
($prop), "\n"
1234 foreach my $t (qw
/absent_file absent_directory/) {
1235 $h = $untracked->{$t} or next;
1236 foreach my $parent (sort keys %$h) {
1237 foreach my $path (sort @
{$h->{$parent}}) {
1239 uri_encode
("$parent/$path"), "\n"
1241 warn "W: $t: $parent/$path ",
1242 "Insufficient permissions?\n";
1248 sub parse_svn_date
{
1249 my $date = shift || return '+0000 1970-01-01 00:00:00';
1250 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1251 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1252 croak
"Unable to parse date: $date\n";
1253 "+0000 $Y-$m-$d $H:$M:$S";
1258 if (!defined $author || length $author == 0) {
1259 $author = '(no author)';
1261 if (defined $::_authors
&& ! defined $::users
{$author}) {
1262 die "Author: $author not defined in $::_authors file\n";
1267 sub make_log_entry
{
1268 my ($self, $rev, $parents, $untracked) = @_;
1269 my $rp = $self->ra->rev_proplist($rev);
1270 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1271 revprops
=> $rp, log => '');
1272 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1273 $self->write_untracked($rev, $un, $untracked);
1274 foreach (sort keys %$rp) {
1276 if (/^svn:(author|date|log)$/) {
1277 $log_entry{$1} = $v;
1279 print $un " rev_prop: ", uri_encode
($_), ' ',
1280 uri_encode
($v), "\n";
1283 close $un or croak
$!;
1284 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1285 $log_entry{author
} = check_author
($log_entry{author
});
1286 $log_entry{log} .= "\n";
1291 my ($self, @parents) = @_;
1292 my ($last_rev, $last_commit) = $self->last_rev_commit;
1293 my ($base, $head) = $self->parse_revision($last_rev);
1294 return if ($base > $head);
1295 if (defined $last_commit) {
1296 $self->assert_index_clean($last_commit);
1299 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
1300 my $err_handler = $SVN::Error
::handler
;
1301 $SVN::Error
::handler
= \
&skip_unknown_revs
;
1304 $self->ra->get_log([$self->{path
}], $min, $max, 0, 1, 1, sub {
1305 my ($paths, $rev, $author, $date, $log) = @_;
1306 push @revs, [ $paths, $rev ] });
1308 my $log_entry = $self->do_fetch(@
$_);
1309 $self->do_git_commit($log_entry, @parents);
1311 last if $max >= $head;
1314 $max = $head if ($max > $head);
1316 $SVN::Error
::handler
= $err_handler;
1320 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1321 # TODO: enable and test optimized commits:
1322 if (0 && $rev == ($self->{last_rev
} + 1)) {
1323 $log_entry->{revision
} = $rev;
1324 $log_entry->{author
} = $author;
1325 $self->do_git_commit($log_entry, "$rev=$tree");
1327 $self->fetch("$rev=$tree");
1332 my ($self, $tree) = (shift, shift);
1333 my $log_entry = ::get_commit_entry
($tree);
1334 unless ($self->{last_rev
}) {
1335 fatal
("Must have an existing revision to commit\n");
1337 my $pool = SVN
::Pool
->new;
1338 my $ed = SVN
::Git
::Editor
->new({ r
=> $self->{last_rev
},
1339 ra
=> $self->ra->dup,
1340 svn_path
=> $self->ra->{svn_path
}
1342 $self->ra->get_commit_editor(
1343 $log_entry->{log}, sub {
1344 $self->set_tree_cb($log_entry,
1348 my $mods = $ed->apply_diff($self->{last_commit
}, $tree);
1350 print "No changes\nr$self->{last_rev} = $tree\n";
1355 sub skip_unknown_revs
{
1357 my $errno = $err->apr_err();
1358 # Maybe the branch we're tracking didn't
1359 # exist when the repo started, so it's
1360 # not an error if it doesn't, just continue
1362 # Wonderfully consistent library, eh?
1363 # 160013 - svn:// and file://
1364 # 175002 - http(s)://
1365 # 175007 - http(s):// (this repo required authorization, too...)
1366 # More codes may be discovered later...
1367 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
1370 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
1374 # Tie::File seems to be prone to offset errors if revisions get sparse,
1375 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1376 # one of my favorite modules is out :< Next up would be one of the DBM
1377 # modules, but I'm not sure which is most portable... So I'll just
1378 # go with something that's plain-text, but still capable of
1379 # being randomly accessed. So here's my ultra-simple fixed-width
1380 # database. All records are 40 characters + "\n", so it's easy to seek
1381 # to a revision: (41 * rev) is the byte offset.
1382 # A record of 40 0s denotes an empty revision.
1383 # And yes, it's still pretty fast (faster than Tie::File).
1386 my ($self, $rev, $commit) = @_;
1387 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
1388 open my $fh, '+<', $self->{db_path
} or croak
$!;
1389 my $offset = $rev * 41;
1390 # assume that append is the common case:
1391 seek $fh, 0, 2 or croak
$!;
1393 if ($pos < $offset) {
1394 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41)
1397 seek $fh, $offset, 0 or croak
$!;
1398 print $fh $commit,"\n" or croak
$!;
1399 close $fh or croak
$!;
1403 my ($self, $rev) = @_;
1405 my $offset = $rev * 41;
1406 open my $fh, '<', $self->{db_path
} or croak
$!;
1407 if (seek $fh, $offset, 0) {
1408 $ret = readline $fh;
1411 $ret = undef if ($ret =~ /^0{40}$/);
1414 close $fh or croak
$!;
1418 sub find_rev_before
{
1419 my ($self, $rev, $eq_ok) = @_;
1420 --$rev unless $eq_ok;
1422 if (my $c = $self->rev_db_get($rev)) {
1427 return (undef, undef);
1431 my ($class, $repo_id, $ref_id, $path) = @_;
1432 unless (defined $repo_id && length $repo_id) {
1433 $repo_id = $Git::SVN
::default_repo_id
;
1435 unless (defined $ref_id && length $ref_id) {
1436 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
1438 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
1439 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
1440 $_[3] = $path = '' unless (defined $path);
1442 unless (-f
"$dir/.rev_db") {
1443 open my $fh, '>>', "$dir/.rev_db" or croak
$!;
1444 close $fh or croak
$!;
1446 bless { ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
1448 db_path
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
1453 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1457 package Git
::SVN
::Prompt
;
1461 use vars qw
/$_no_auth_cache $_username/;
1464 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1465 $may_save = undef if $_no_auth_cache;
1466 $default_username = $_username if defined $_username;
1467 if (defined $default_username && length $default_username) {
1468 if (defined $realm && length $realm) {
1469 print STDERR
"Authentication realm: $realm\n";
1472 $cred->username($default_username);
1474 username
($cred, $realm, $may_save, $pool);
1476 $cred->password(_read_password
("Password for '" .
1477 $cred->username . "': ", $realm));
1478 $cred->may_save($may_save);
1479 $SVN::_Core
::SVN_NO_ERROR
;
1482 sub ssl_server_trust
{
1483 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1484 $may_save = undef if $_no_auth_cache;
1485 print STDERR
"Error validating server certificate for '$realm':\n";
1486 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
1487 print STDERR
" - The certificate is not issued by a trusted ",
1488 "authority. Use the\n",
1489 " fingerprint to validate the certificate manually!\n";
1491 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
1492 print STDERR
" - The certificate hostname does not match.\n";
1494 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
1495 print STDERR
" - The certificate is not yet valid.\n";
1497 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
1498 print STDERR
" - The certificate has expired.\n";
1500 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
1501 print STDERR
" - The certificate has an unknown error.\n";
1504 "Certificate information:\n".
1505 " - Hostname: %s\n".
1506 " - Valid: from %s until %s\n".
1508 " - Fingerprint: %s\n",
1509 map $cert_info->$_, qw(hostname valid_from valid_until
1510 issuer_dname fingerprint);
1513 print STDERR
$may_save ?
1514 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1515 "(R)eject or accept (t)emporarily? ";
1517 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
1518 if ($choice =~ /^t$/i) {
1519 $cred->may_save(undef);
1520 } elsif ($choice =~ /^r$/i) {
1522 } elsif ($may_save && $choice =~ /^p$/i) {
1523 $cred->may_save($may_save);
1527 $cred->accepted_failures($failures);
1528 $SVN::_Core
::SVN_NO_ERROR
;
1531 sub ssl_client_cert
{
1532 my ($cred, $realm, $may_save, $pool) = @_;
1533 $may_save = undef if $_no_auth_cache;
1534 print STDERR
"Client certificate filename: ";
1536 chomp(my $filename = <STDIN
>);
1537 $cred->cert_file($filename);
1538 $cred->may_save($may_save);
1539 $SVN::_Core
::SVN_NO_ERROR
;
1542 sub ssl_client_cert_pw
{
1543 my ($cred, $realm, $may_save, $pool) = @_;
1544 $may_save = undef if $_no_auth_cache;
1545 $cred->password(_read_password
("Password: ", $realm));
1546 $cred->may_save($may_save);
1547 $SVN::_Core
::SVN_NO_ERROR
;
1551 my ($cred, $realm, $may_save, $pool) = @_;
1552 $may_save = undef if $_no_auth_cache;
1553 if (defined $realm && length $realm) {
1554 print STDERR
"Authentication realm: $realm\n";
1557 if (defined $_username) {
1558 $username = $_username;
1560 print STDERR
"Username: ";
1562 chomp($username = <STDIN
>);
1564 $cred->username($username);
1565 $cred->may_save($may_save);
1566 $SVN::_Core
::SVN_NO_ERROR
;
1569 sub _read_password
{
1570 my ($prompt, $realm) = @_;
1571 print STDERR
$prompt;
1573 require Term
::ReadKey
;
1574 Term
::ReadKey
::ReadMode
('noecho');
1576 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
1577 last if $key =~ /[\012\015]/; # \n\r
1580 Term
::ReadKey
::ReadMode
('restore');
1590 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1597 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
1602 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
1603 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1604 $SVN::Node
::none
.$SVN::Node
::file
.
1605 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1606 $SVN::Auth
::SSL
::CNMISMATCH
.
1607 $SVN::Auth
::SSL
::NOTYETVALID
.
1608 $SVN::Auth
::SSL
::EXPIRED
.
1609 $SVN::Auth
::SSL
::UNKNOWNCA
.
1610 $SVN::Auth
::SSL
::OTHER
;
1613 package SVN
::Git
::Fetcher
;
1620 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
1622 my ($class, $git_svn) = @_;
1623 my $self = SVN
::Delta
::Editor
->new;
1624 bless $self, $class;
1625 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
1626 $self->{empty
} = {};
1627 $self->{dir_prop
} = {};
1628 $self->{file_prop
} = {};
1629 $self->{absent_dir
} = {};
1630 $self->{absent_file
} = {};
1631 ($self->{gui
}, $self->{ctx
}) = $git_svn->tmp_index_do(
1632 sub { command_input_pipe
(qw
/update-index -z --index-info/) } );
1633 require Digest
::MD5
;
1637 sub set_path_strip
{
1638 my ($self, $path) = @_;
1639 $self->{path_strip
} = qr/^\Q$path\E\/?
/;
1646 sub open_directory
{
1647 my ($self, $path, $pb, $rev) = @_;
1652 my ($self, $path) = @_;
1653 $path =~ s!$self->{path_strip}!! if $self->{path_strip
};
1658 my ($self, $path, $rev, $pb) = @_;
1659 my $gui = $self->{gui
};
1661 my $gpath = $self->git_path($path);
1662 # remove entire directories.
1663 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
1664 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
1666 $self->{c
}, '--', $gpath);
1669 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
1670 print "\tD\t$_\n" unless $self->{q
};
1672 print "\tD\t$gpath/\n" unless $self->{q
};
1673 command_close_pipe
($ls, $ctx);
1674 $self->{empty
}->{$path} = 0
1676 print $gui '0 ',0 x
40,"\t",$gpath,"\0" or croak
$!;
1677 print "\tD\t$gpath\n" unless $self->{q
};
1683 my ($self, $path, $pb, $rev) = @_;
1684 my $gpath = $self->git_path($path);
1685 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
1686 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
1687 unless (defined $mode && defined $blob) {
1688 die "$path was not found in commit $self->{c} (r$rev)\n";
1690 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
1691 pool
=> SVN
::Pool
->new, action
=> 'M' };
1695 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
1696 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1697 delete $self->{empty
}->{$dir};
1698 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
1699 pool
=> SVN
::Pool
->new, action
=> 'A' };
1703 my ($self, $path, $cp_path, $cp_rev) = @_;
1704 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1705 delete $self->{empty
}->{$dir};
1706 $self->{empty
}->{$path} = 1;
1710 sub change_dir_prop
{
1711 my ($self, $db, $prop, $value) = @_;
1712 $self->{dir_prop
}->{$db->{path
}} ||= {};
1713 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
1717 sub absent_directory
{
1718 my ($self, $path, $pb) = @_;
1719 $self->{absent_dir
}->{$pb->{path
}} ||= [];
1720 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
1725 my ($self, $path, $pb) = @_;
1726 $self->{absent_file
}->{$pb->{path
}} ||= [];
1727 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
1731 sub change_file_prop
{
1732 my ($self, $fb, $prop, $value) = @_;
1733 if ($prop eq 'svn:executable') {
1734 if ($fb->{mode_b
} != 120000) {
1735 $fb->{mode_b
} = defined $value ?
100755 : 100644;
1737 } elsif ($prop eq 'svn:special') {
1738 $fb->{mode_b
} = defined $value ?
120000 : 100644;
1740 $self->{file_prop
}->{$fb->{path
}} ||= {};
1741 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
1746 sub apply_textdelta
{
1747 my ($self, $fb, $exp) = @_;
1748 my $fh = IO
::File
->new_tmpfile;
1750 # $fh gets auto-closed() by SVN::TxDelta::apply(),
1751 # (but $base does not,) so dup() it for reading in close_file
1752 open my $dup, '<&', $fh or croak
$!;
1753 my $base = IO
::File
->new_tmpfile;
1754 $base->autoflush(1);
1756 defined (my $pid = fork) or croak
$!;
1758 open STDOUT
, '>&', $base or croak
$!;
1759 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
1760 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
1766 seek $base, 0, 0 or croak
$!;
1767 my $md5 = Digest
::MD5
->new;
1768 $md5->addfile($base);
1769 my $got = $md5->hexdigest;
1770 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
1772 " got: $got\n" if ($got ne $exp);
1775 seek $base, 0, 0 or croak
$!;
1777 $fb->{base
} = $base;
1778 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
1782 my ($self, $fb, $exp) = @_;
1784 my $path = $self->git_path($fb->{path
});
1785 if (my $fh = $fb->{fh
}) {
1786 seek($fh, 0, 0) or croak
$!;
1787 my $md5 = Digest
::MD5
->new;
1789 my $got = $md5->hexdigest;
1790 die "Checksum mismatch: $path\n",
1791 "expected: $exp\n got: $got\n" if ($got ne $exp);
1792 seek($fh, 0, 0) or croak
$!;
1793 if ($fb->{mode_b
} == 120000) {
1794 read($fh, my $buf, 5) == 5 or croak
$!;
1795 $buf eq 'link ' or die "$path has mode 120000",
1796 "but is not a link\n";
1798 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
1800 open STDIN
, '<&', $fh or croak
$!;
1801 exec qw
/git-hash-object -w --stdin/ or croak
$!;
1803 chomp($hash = do { local $/; <$out> });
1804 close $out or croak
$!;
1805 close $fh or croak
$!;
1806 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
1807 close $fb->{base
} or croak
$!;
1809 $hash = $fb->{blob
} or die "no blob information\n";
1812 my $gui = $self->{gui
};
1813 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
1814 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $self->{q
};
1820 eval { command_close_pipe
($self->{gui
}, $self->{ctx
}) };
1821 $self->SUPER::abort_edit
(@_);
1826 command_close_pipe
($self->{gui
}, $self->{ctx
});
1827 $self->{git_commit_ok
} = 1;
1828 $self->SUPER::close_edit
(@_);
1831 package SVN
::Git
::Editor
;
1840 my $git_svn = shift;
1841 my $self = SVN
::Delta
::Editor
->new(@_);
1842 bless $self, $class;
1843 foreach (qw
/svn_path r ra/) {
1844 die "$_ required!\n" unless (defined $git_svn->{$_});
1845 $self->{$_} = $git_svn->{$_};
1847 $self->{pool
} = SVN
::Pool
->new;
1848 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
1850 require Digest
::MD5
;
1855 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
1859 (defined $_[1] && length $_[1]) ?
$_[1] : ''
1863 my ($self, $path) = @_;
1864 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
1868 my ($self, $tree_b) = @_;
1869 my $rm = $self->{rm
};
1870 delete $rm->{''}; # we never delete the url we're tracking
1873 foreach (keys %$rm) {
1874 my @d = split m
#/#, $_;
1878 $c .= '/' . shift @d;
1882 delete $rm->{$self->{svn_path
}};
1883 delete $rm->{''}; # we never delete the url we're tracking
1886 my ($fh, $ctx) = command_output_pipe
(
1887 qw
/ls-tree --name-only -r -z/, $tree_b);
1891 my @dn = split m
#/#, $_;
1893 delete $rm->{join '/', @dn};
1900 command_close_pipe
($fh, $ctx);
1902 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
1903 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
1904 $self->close_directory($bat->{$d}, $p);
1905 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
1906 print "\tD+\t$d/\n" unless $::_q
;
1907 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
1912 sub open_or_add_dir
{
1913 my ($self, $full_path, $baton) = @_;
1914 my $t = $self->{ra
}->check_path($full_path, $self->{r
});
1915 if ($t == $SVN::Node
::none
) {
1916 return $self->add_directory($full_path, $baton,
1917 undef, -1, $self->{pool
});
1918 } elsif ($t == $SVN::Node
::dir
) {
1919 return $self->open_directory($full_path, $baton,
1920 $self->{r
}, $self->{pool
});
1922 print STDERR
"$full_path already exists in repository at ",
1923 "r$self->{r} and it is not a directory (",
1924 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
1929 my ($self, $path) = @_;
1930 my $bat = $self->{bat
};
1931 $path = $self->repo_path($path);
1932 return $bat->{''} unless (length $path);
1933 my @p = split m
#/+#, $path;
1935 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
1938 $c .= '/' . shift @p;
1939 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
1945 my ($self, $m) = @_;
1946 my ($dir, $file) = split_path
($m->{file_b
});
1947 my $pbat = $self->ensure_path($dir);
1948 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1950 print "\tA\t$m->{file_b}\n" unless $::_q
;
1951 $self->chg_file($fbat, $m);
1952 $self->close_file($fbat,undef,$self->{pool
});
1956 my ($self, $m) = @_;
1957 my ($dir, $file) = split_path
($m->{file_b
});
1958 my $pbat = $self->ensure_path($dir);
1959 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1960 $self->url_path($m->{file_a
}), $self->{r
});
1961 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1962 $self->chg_file($fbat, $m);
1963 $self->close_file($fbat,undef,$self->{pool
});
1967 my ($self, $path, $pbat) = @_;
1968 my $rpath = $self->repo_path($path);
1969 my ($dir, $file) = split_path
($rpath);
1970 $self->{rm
}->{$dir} = 1;
1971 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
1975 my ($self, $m) = @_;
1976 my ($dir, $file) = split_path
($m->{file_b
});
1977 my $pbat = $self->ensure_path($dir);
1978 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1979 $self->url_path($m->{file_a
}), $self->{r
});
1980 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1981 $self->chg_file($fbat, $m);
1982 $self->close_file($fbat,undef,$self->{pool
});
1984 ($dir, $file) = split_path
($m->{file_a
});
1985 $pbat = $self->ensure_path($dir);
1986 $self->delete_entry($m->{file_a
}, $pbat);
1990 my ($self, $m) = @_;
1991 my ($dir, $file) = split_path
($m->{file_b
});
1992 my $pbat = $self->ensure_path($dir);
1993 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
1994 $pbat,$self->{r
},$self->{pool
});
1995 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
1996 $self->chg_file($fbat, $m);
1997 $self->close_file($fbat,undef,$self->{pool
});
2000 sub T
{ shift->M(@_) }
2002 sub change_file_prop
{
2003 my ($self, $fbat, $pname, $pval) = @_;
2004 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2008 my ($self, $fbat, $m) = @_;
2009 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2010 $self->change_file_prop($fbat,'svn:executable','*');
2011 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2012 $self->change_file_prop($fbat,'svn:executable',undef);
2014 my $fh = IO
::File
->new_tmpfile or croak
$!;
2015 if ($m->{mode_b
} =~ /^120/) {
2016 print $fh 'link ' or croak
$!;
2017 $self->change_file_prop($fbat,'svn:special','*');
2018 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2019 $self->change_file_prop($fbat,'svn:special',undef);
2021 defined(my $pid = fork) or croak
$!;
2023 open STDOUT
, '>&', $fh or croak
$!;
2024 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2028 $fh->flush == 0 or croak
$!;
2029 seek $fh, 0, 0 or croak
$!;
2031 my $md5 = Digest
::MD5
->new;
2032 $md5->addfile($fh) or croak
$!;
2033 seek $fh, 0, 0 or croak
$!;
2035 my $exp = $md5->hexdigest;
2036 my $pool = SVN
::Pool
->new;
2037 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2038 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2039 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2042 close $fh or croak
$!;
2046 my ($self, $m) = @_;
2047 my ($dir, $file) = split_path
($m->{file_b
});
2048 my $pbat = $self->ensure_path($dir);
2049 print "\tD\t$m->{file_b}\n" unless $::_q
;
2050 $self->delete_entry($m->{file_b
}, $pbat);
2055 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2056 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2057 $self->close_directory($bat->{$_}, $p);
2059 $self->SUPER::close_edit
($p);
2065 $self->SUPER::abort_edit
($self->{pool
});
2066 $self->{pool
}->clear;
2069 # this drives the editor
2071 my ($self, $tree_a, $tree_b) = @_;
2072 my @diff_tree = qw(diff-tree -z -r);
2073 if ($::_cp_similarity
) {
2074 push @diff_tree, "-C$::_cp_similarity";
2076 push @diff_tree, '-C';
2078 push @diff_tree, '--find-copies-harder' if $::_find_copies_harder
;
2079 push @diff_tree, "-l$::_l" if defined $::_l
;
2080 push @diff_tree, $tree_a, $tree_b;
2081 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2086 while (<$diff_fh>) {
2087 chomp $_; # this gets rid of the trailing "\0"
2088 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2089 $::sha1\s
($::sha1
)\s
2090 ([MTCRAD
])\d
*$/xo
) {
2091 push @mods, { mode_a
=> $1, mode_b
=> $2,
2092 sha1_b
=> $3, chg
=> $4 };
2093 if ($4 =~ /^(?:C|R)$/) {
2098 } elsif ($state eq 'file_a') {
2099 my $x = $mods[$#mods] or croak
"Empty array\n";
2100 if ($x->{chg
} !~ /^(?:C|R)$/) {
2101 croak
"Error parsing $_, $x->{chg}\n";
2105 } elsif ($state eq 'file_b') {
2106 my $x = $mods[$#mods] or croak
"Empty array\n";
2107 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2108 croak
"Error parsing $_, $x->{chg}\n";
2110 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2111 croak
"Error parsing $_, $x->{chg}\n";
2116 croak
"Error parsing $_\n";
2119 command_close_pipe
($diff_fh, $ctx);
2122 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2123 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @mods) {
2125 if (defined $o{$f}) {
2128 fatal
("Invalid change type: $f\n");
2131 $self->rmdirs($tree_b) if $::_rmdir
;
2140 package Git
::SVN
::Ra
;
2141 use vars qw
/@ISA $config_dir/;
2144 my ($can_do_switch);
2148 # enforce temporary pool usage for some simple functions
2150 foreach (qw
/get_latest_revnum rev_proplist get_file
2151 check_path get_dir get_uuid get_repos_root
/) {
2154 my \$pool = SVN::Pool->new;
2155 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2157 wantarray ? \@ret : \$ret[0]; }\n";
2163 my ($class, $url) = @_;
2165 return $RA{$url} if $RA{$url};
2167 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2168 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2169 SVN
::Client
::get_simple_provider
(),
2170 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2171 SVN
::Client
::get_simple_prompt_provider
(
2172 \
&Git
::SVN
::Prompt
::simple
, 2),
2173 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2174 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2175 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2176 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2177 SVN
::Client
::get_username_provider
(),
2178 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2179 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2180 SVN
::Client
::get_username_prompt_provider
(
2181 \
&Git
::SVN
::Prompt
::username
, 2),
2183 my $config = SVN
::Core
::config_get_config
($config_dir);
2184 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2186 pool
=> SVN
::Pool
->new,
2187 auth_provider_callbacks
=> $callbacks);
2188 $self->{svn_path
} = $url;
2189 $self->{repos_root
} = $self->get_repos_root;
2190 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E/*##;
2191 $RA{$url} = bless $self, $class;
2195 # do not call the real DESTROY since we store ourselves in %RA
2200 my $dup = SVN
::Ra
->new(pool
=> SVN
::Pool
->new,
2201 map { $_ => $self->{$_} } qw
/config url
2202 auth auth_provider_callbacks repos_root svn_path
/);
2203 bless $dup, ref $self;
2207 my ($self, @args) = @_;
2208 my $pool = SVN
::Pool
->new;
2209 $args[4]-- if $args[4] && ! $::_follow_parent
;
2210 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2211 my $ret = $self->SUPER::get_log
(@args, $pool);
2216 sub get_commit_editor
{
2217 my ($self, $log, $cb, $pool) = @_;
2218 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2219 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2224 $self->{uuid
} ||= $self->get_uuid;
2228 my ($self, $rev_a, $rev_b, $path, $recurse, $editor) = @_;
2229 my $pool = SVN
::Pool
->new;
2230 $editor->set_path_strip($path);
2231 my $reporter = $self->do_update($rev_b, $path, $recurse,
2233 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2234 my $new = ($rev_a == $rev_b);
2235 $reporter->set_path('', $rev_a, $new, @lock, $pool);
2236 $reporter->finish_report($pool);
2238 $editor->{git_commit_ok
};
2242 my ($self, $rev_a, $rev_b, $path, $recurse, $url_b, $editor) = @_;
2243 my $pool = SVN
::Pool
->new;
2244 $editor->set_path_strip($path);
2245 my $reporter = $self->do_switch($rev_b, $path, $recurse,
2246 $url_b, $editor, $pool);
2247 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2248 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2249 $reporter->finish_report($pool);
2251 $editor->{git_commit_ok
};
2256 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
2257 my $url = $self->{repos_root
};
2258 my @components = split(m!/!, $self->{svn_path
});
2261 $url .= "/$c" if length $c;
2262 eval { (ref $self)->new($url)->get_latest_revnum };
2263 } while ($@
&& ($c = shift @components));
2269 unless (defined $can_do_switch) {
2270 my $pool = SVN
::Pool
->new;
2272 $self->do_switch(1, '', 0, $self->{url
},
2273 SVN
::Delta
::Editor
->new, $pool);
2278 $rep->abort_report($pool);
2286 package Git
::SVN
::Log
;
2289 use POSIX qw
/strftime/;
2290 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
2291 %rusers $show_commit $incremental/;
2296 return 1 if defined $c->{r
};
2297 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
2298 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
2299 my @log = command
(qw
/cat-file commit/, $c->{c
});
2300 shift @log while ($log[0] ne "\n");
2302 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
2304 (undef, $c->{r
}, undef) = ::extract_metadata
(
2305 (grep(/^git-svn-id: /, @log))[-1]);
2307 return defined $c->{r
};
2313 $dcvar = 'color.diff';
2314 $dc = `git-config --get $dcvar`;
2316 # nothing at all; fallback to "diff.color"
2317 $dcvar = 'diff.color';
2318 $dc = `git-config --get $dcvar`;
2321 if ($dc eq 'auto') {
2323 $pc = `git-config --get color.pager`;
2325 # does not have it -- fallback to pager.color
2326 $pc = `git-config --bool --get pager.color`;
2329 $pc = `git-config --bool --get color.pager`;
2335 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
2336 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
2340 return 0 if $dc eq 'never';
2341 return 1 if $dc eq 'always';
2342 chomp($dc = `git-config --bool --get $dcvar`);
2343 return ($dc eq 'true');
2346 sub git_svn_log_cmd
{
2347 my ($r_min, $r_max) = @_;
2348 my $gs = Git
::SVN
->_new;
2349 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
2351 push @cmd, '-r' unless $non_recursive;
2352 push @cmd, qw
/--raw --name-status/ if $verbose;
2353 push @cmd, '--color' if log_use_color
();
2354 return @cmd unless defined $r_max;
2355 if ($r_max == $r_min) {
2356 push @cmd, '--max-count=1';
2357 if (my $c = $gs->rev_db_get($r_max)) {
2361 my ($c_min, $c_max);
2362 $c_max = $gs->rev_db_get($r_max);
2363 $c_min = $gs->rev_db_get($r_min);
2364 if (defined $c_min && defined $c_max) {
2365 if ($r_max > $r_max) {
2366 push @cmd, "$c_min..$c_max";
2368 push @cmd, "$c_max..$c_min";
2370 } elsif ($r_max > $r_min) {
2379 # adapted from pager.c
2381 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
2382 if (!defined $pager) {
2384 } elsif (length $pager == 0 || $pager eq 'cat') {
2390 return unless -t
*STDOUT
;
2391 pipe my $rfd, my $wfd or return;
2392 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
2394 open STDOUT
, '>&', $wfd or
2395 ::fatal
"Can't redirect to stdout: $!\n";
2398 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
2399 $ENV{LESS
} ||= 'FRSX';
2400 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
2403 sub get_author_info
{
2404 my ($dest, $author, $t, $tz) = @_;
2405 $author =~ s/(?:^\s*|\s*$)//g;
2406 $dest->{a_raw
} = $author;
2409 $au = $rusers{$author} || undef;
2412 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
2417 # Date::Parse isn't in the standard Perl distro :(
2418 if ($tz =~ s/^\+//) {
2419 $t += ::tz_to_s_offset
($tz);
2420 } elsif ($tz =~ s/^\-//) {
2421 $t -= ::tz_to_s_offset
($tz);
2423 $dest->{t_utc
} = $t;
2426 sub process_commit
{
2427 my ($c, $r_min, $r_max, $defer) = @_;
2428 if (defined $r_min && defined $r_max) {
2429 if ($r_min == $c->{r
} && $r_min == $r_max) {
2433 return 1 if $r_min == $r_max;
2434 if ($r_min < $r_max) {
2435 # we need to reverse the print order
2436 return 0 if (defined $limit && --$limit < 0);
2440 if ($r_min != $r_max) {
2441 return 1 if ($r_min < $c->{r
});
2442 return 1 if ($r_max > $c->{r
});
2445 return 0 if (defined $limit && --$limit < 0);
2454 if (my $l = $c->{l
}) {
2455 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2458 $l_fmt ||= 'A' . length($c->{r
});
2459 print 'r',pack($l_fmt, $c->{r
}),' | ';
2460 print "$c->{c} | " if $show_commit;
2463 show_commit_normal
($c);
2467 sub show_commit_changed_paths
{
2469 return unless $c->{changed
};
2470 print "Changed paths:\n", @
{$c->{changed
}};
2473 sub show_commit_normal
{
2475 print '-' x72
, "\nr$c->{r} | ";
2476 print "$c->{c} | " if $show_commit;
2477 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2478 localtime($c->{t_utc
})), ' | ';
2481 if (my $l = $c->{l
}) {
2482 while ($l->[$#$l] eq "\n" && $#$l > 0
2483 && $l->[($#$l - 1)] eq "\n") {
2486 $nr_line = scalar @
$l;
2488 print "1 line\n\n\n";
2490 if ($nr_line == 1) {
2491 $nr_line = '1 line';
2493 $nr_line .= ' lines';
2495 print $nr_line, "\n";
2496 show_commit_changed_paths
($c);
2498 print $_ foreach @
$l;
2502 show_commit_changed_paths
($c);
2506 foreach my $x (qw
/raw diff/) {
2509 print $_ foreach @
{$c->{$x}}
2516 my ($r_min, $r_max);
2517 my $r_last = -1; # prevent dupes
2523 if (defined $::_revision
) {
2524 if ($::_revision
=~ /^(\d+):(\d+)$/) {
2525 ($r_min, $r_max) = ($1, $2);
2526 } elsif ($::_revision
=~ /^\d+$/) {
2527 $r_min = $r_max = $::_revision
;
2529 ::fatal
"-r$::_revision is not supported, use ",
2530 "standard \'git log\' arguments instead\n";
2535 @args = (git_svn_log_cmd
($r_min, $r_max), @args);
2536 my $log = command_output_pipe
(@args);
2539 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
2541 if (/^${esc_color}commit ($::sha1_short)/o) {
2543 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
2545 process_commit
($c, $r_min, $r_max, \
@k) or
2550 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
2551 get_author_info
($c, $1, $2, $3);
2552 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
2554 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
2555 push @
{$c->{raw
}}, $_;
2556 } elsif (/^${esc_color}[ACRMDT]\t/) {
2557 # we could add $SVN->{svn_path} here, but that requires
2558 # remote access at the moment (repo_path_split)...
2559 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
2560 push @
{$c->{changed
}}, $_;
2561 } elsif (/^${esc_color}diff /o) {
2563 push @
{$c->{diff
}}, $_;
2565 push @
{$c->{diff
}}, $_;
2566 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
2567 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
2568 } elsif (s/^${esc_color} //o) {
2569 push @
{$c->{l
}}, $_;
2572 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
2574 process_commit
($c, $r_min, $r_max, \
@k);
2580 process_commit
($_, $r_min, $r_max) foreach reverse @k;
2584 print '-' x72
,"\n" unless $incremental || $oneline;
2587 package Git
::SVN
::Migration
;
2588 # these version numbers do NOT correspond to actual version numbers
2589 # of git nor git-svn. They are just relative.
2591 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
2593 # v1 layout: .git/$id/info/url, refs/remotes/$id
2595 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
2597 # v3 layout: .git/svn/$id, refs/remotes/$id
2598 # - info/url may remain for backwards compatibility
2599 # - this is what we migrate up to this layout automatically,
2600 # - this will be used by git svn init on single branches
2602 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
2603 # - this is only created for newly multi-init-ed
2604 # repositories. Similar in spirit to the
2605 # --use-separate-remotes option in git-clone (now default)
2606 # - we do not automatically migrate to this (following
2607 # the example set by core git)
2611 use File
::Path qw
/mkpath/;
2612 use File
::Basename qw
/dirname basename/;
2613 use vars qw
/$_minimize/;
2615 sub migrate_from_v0
{
2616 my $git_dir = $ENV{GIT_DIR
};
2617 return undef unless -d
$git_dir;
2618 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2622 my ($id, $orig_ref) = ($_, $_);
2623 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
2624 next unless -f
"$git_dir/$id/info/url";
2625 my $new_ref = "refs/remotes/$id";
2626 if (::verify_ref
("$new_ref^0")) {
2627 print STDERR
"W: $orig_ref is probably an old ",
2628 "branch used by an ancient version of ",
2630 "However, $new_ref also exists.\n",
2631 "We will not be able ",
2632 "to use this branch until this ",
2633 "ambiguity is resolved.\n";
2636 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
2637 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
2638 command_noisy
('update-ref', $new_ref, $orig_ref);
2639 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
2642 command_close_pipe
($fh, $ctx);
2643 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
2647 sub migrate_from_v1
{
2648 my $git_dir = $ENV{GIT_DIR
};
2650 return $migrated unless -d
$git_dir;
2651 my $svn_dir = "$git_dir/svn";
2653 # just in case somebody used 'svn' as their $id at some point...
2654 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
2656 print STDERR
"Migrating from a git-svn v1 layout...\n";
2658 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
2659 "$svn_dir\n\t(required for this version ",
2660 "($::VERSION) of git-svn) does not. exist\n";
2661 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2664 next unless $x =~ s
#^refs/remotes/##;
2666 next unless -f
"$git_dir/$x/info/url";
2667 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
2669 my $dn = dirname
("$git_dir/svn/$x");
2670 mkpath
([$dn]) unless -d
$dn;
2671 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
2672 mkpath
(["$git_dir/svn/svn"]);
2673 print STDERR
" - $git_dir/$x/info => ",
2674 "$git_dir/svn/$x/info\n";
2675 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
2677 # don't worry too much about these, they probably
2678 # don't exist with repos this old (save for index,
2679 # and we can easily regenerate that)
2680 foreach my $f (qw
/unhandled.log index .rev_db/) {
2681 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
2684 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
2685 rename "$git_dir/$x", "$git_dir/svn/$x" or
2690 command_close_pipe
($fh, $ctx);
2691 print STDERR
"Done migrating from a git-svn v1 layout\n";
2696 my ($l_map, $pfx, $path) = @_;
2698 foreach (<$path/*>) {
2699 if (-r
"$_/info/url") {
2700 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2701 my $ref_id = $pfx . basename
$_;
2702 my $url = ::file_to_s
("$_/info/url");
2703 $l_map->{$ref_id} = $url;
2710 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
2711 read_old_urls
($l_map, $x, $_);
2715 sub migrate_from_v2
{
2716 my @cfg = command
(qw
/config -l/);
2717 return if grep /^svn-remote\..+\.url=/, @cfg;
2719 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
2722 foreach my $ref_id (sort keys %l_map) {
2723 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
2729 sub minimize_connections
{
2730 my $r = Git
::SVN
::read_all_remotes
();
2732 my $root_repos = {};
2733 foreach my $repo_id (keys %$r) {
2734 my $url = $r->{$repo_id}->{url
} or next;
2735 my $fetch = $r->{$repo_id}->{fetch
} or next;
2736 my $ra = Git
::SVN
::Ra
->new($url);
2738 # skip existing cases where we already connect to the root
2739 if (($ra->{url
} eq $ra->{repos_root
}) ||
2740 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
2742 $root_repos->{$ra->{url
}} = $repo_id;
2746 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
2747 my $root_path = $ra->{url
};
2748 $root_path =~ s
#^\Q$ra->{repos_root}\E/*##;
2749 foreach my $path (keys %$fetch) {
2750 my $ref_id = $fetch->{$path};
2751 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
2753 # make sure we can read when connecting to
2754 # a higher level of a repository
2755 my ($last_rev, undef) = $gs->last_rev_commit;
2756 if (!defined $last_rev) {
2758 $root_ra->get_latest_revnum;
2762 my $new = $root_path;
2763 $new .= length $path ?
"/$path" : '';
2765 $root_ra->get_log([$new], $last_rev, $last_rev,
2769 $new_urls->{$ra->{repos_root
}}->{$new} =
2770 { ref_id
=> $ref_id,
2771 old_repo_id
=> $repo_id,
2772 old_path
=> $path };
2777 foreach my $url (keys %$new_urls) {
2778 # see if we can re-use an existing [svn-remote "repo_id"]
2779 # instead of creating a(n ugly) new section:
2780 my $repo_id = $root_repos->{$url} ||
2781 Git
::SVN
::sanitize_remote_name
($url);
2783 my $fetch = $new_urls->{$url};
2784 foreach my $path (keys %$fetch) {
2785 my $x = $fetch->{$path};
2786 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
2787 my $pfx = "svn-remote.$x->{old_repo_id}";
2789 my $old_fetch = quotemeta("$x->{old_path}:".
2790 "refs/remotes/$x->{ref_id}");
2791 command_noisy
(qw
/config --unset/,
2792 "$pfx.fetch", '^'. $old_fetch . '$');
2793 delete $r->{$x->{old_repo_id
}}->
2794 {fetch
}->{$x->{old_path
}};
2795 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
2796 command_noisy
(qw
/config --unset/,
2798 push @emptied, $x->{old_repo_id
}
2803 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
2804 "$ENV{GIT_DIR}/config";
2806 The following [svn-remote] sections in your config file ($file) are empty
2807 and can be safely removed:
2809 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
2813 sub migration_check
{
2817 minimize_connections
() if $_minimize;
2824 $log_entry hashref as returned by libsvn_log_entry
()
2826 log => 'whitespace-formatted log entry
2827 ', # trailing newline is preserved
2828 revision
=> '8', # integer
2829 date
=> '2004-02-24T17:01:44.108345Z', # commit date
2830 author
=> 'committer name'
2833 @mods = array of diff
-index line hashes
, each element represents one line
2834 of diff
-index output
2836 diff
-index line
($m hash
)
2838 mode_a
=> first column of diff
-index output
, no leading
':',
2839 mode_b
=> second column of diff
-index output
,
2840 sha1_b
=> sha1sum of the final blob
,
2841 chg
=> change type
[MCRADT
],
2842 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
2843 file_b
=> new
/current file name of a file
(any chg
)
2847 # retval of read_url_paths{,_all}();
2849 # repository root url
2850 'https://svn.musicpd.org' => {
2851 # repository path # GIT_SVN_ID
2852 'mpd/trunk' => 'trunk',
2853 'mpd/tags/0.11.5' => 'tags/0.11.5',
2858 I don
't trust the each() function on unless I created %hash myself
2859 because the internal iterator may not have started at base.