2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $sha1 $sha1_short $_revision
9 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
10 $VERSION = '@@GIT_VERSION@@';
12 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR
};
13 $ENV{GIT_DIR
} ||= '.git';
14 $Git::SVN
::default_repo_id
= 'svn';
15 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
16 $Git::SVN
::Ra
::_log_window_size
= 100;
18 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
20 $| = 1; # unbuffer STDOUT
22 sub fatal
(@
) { print STDERR
@_; exit 1 }
23 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
26 if ($SVN::Core
::VERSION
lt '1.1.0') {
27 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
29 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
30 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
31 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
34 use File
::Basename qw
/dirname basename/;
35 use File
::Path qw
/mkpath/;
36 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
42 foreach (qw
/command command_oneline command_noisy command_output_pipe
43 command_input_pipe command_close_pipe
/) {
44 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
45 "*Git::SVN::Migration::$_ = ".
46 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
53 $sha1 = qr/[a-f\d]{40}/;
54 $sha1_short = qr/[a-f\d]{4,40}/;
55 my ($_stdin, $_help, $_edit,
58 $_version, $_fetch_all,
59 $_merge, $_strategy, $_dry_run, $_local,
60 $_prefix, $_no_checkout, $_verbose);
61 $Git::SVN
::_follow_parent
= 1;
62 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
63 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
64 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
);
65 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
66 'authors-file|A=s' => \
$_authors,
67 'repack:i' => \
$Git::SVN
::_repack
,
68 'noMetadata' => \
$Git::SVN
::_no_metadata
,
69 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
70 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
71 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
72 'no-checkout' => \
$_no_checkout,
74 'repack-flags|repack-args|repack-opts=s' =>
75 \
$Git::SVN
::_repack_flags
,
78 my ($_trunk, $_tags, $_branches);
80 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
81 'trunk|T=s' => \
$_trunk, 'tags|t=s' => \
$_tags,
82 'branches|b=s' => \
$_branches, 'prefix=s' => \
$_prefix,
83 'no-metadata' => sub { $icv{noMetadata
} = 1 },
84 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
85 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
86 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
88 my %cmt_opts = ( 'edit|e' => \
$_edit,
89 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
90 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
91 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
92 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
96 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
97 { 'revision|r=s' => \
$_revision,
98 'fetch-all|all' => \
$_fetch_all,
100 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
101 { 'revision|r=s' => \
$_revision,
102 %fc_opts, %init_opts } ],
103 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
104 " (requires URL argument)",
106 'multi-init' => [ \
&cmd_multi_init
,
107 "Deprecated alias for ".
108 "'$0 init -T<trunk> -b<branches> -t<tags>'",
110 dcommit
=> [ \
&cmd_dcommit
,
111 'Commit several diffs to merge with upstream',
112 { 'merge|m|M' => \
$_merge,
113 'strategy|s=s' => \
$_strategy,
114 'verbose|v' => \
$_verbose,
115 'dry-run|n' => \
$_dry_run,
116 'fetch-all|all' => \
$_fetch_all,
117 %cmt_opts, %fc_opts } ],
118 'set-tree' => [ \
&cmd_set_tree
,
119 "Set an SVN repository to a git tree-ish",
120 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
121 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
122 { 'revision|r=i' => \
$_revision } ],
123 'multi-fetch' => [ \
&cmd_multi_fetch
,
124 "Deprecated alias for $0 fetch --all",
125 { 'revision|r=s' => \
$_revision, %fc_opts } ],
126 'migrate' => [ sub { },
127 # no-op, we automatically run this anyways,
128 'Migrate configuration/metadata/layout from
129 previous versions of git-svn',
130 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
132 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
133 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
134 'revision|r=s' => \
$_revision,
135 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
136 'incremental' => \
$Git::SVN
::Log
::incremental
,
137 'oneline' => \
$Git::SVN
::Log
::oneline
,
138 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
139 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
140 'authors-file|A=s' => \
$_authors,
141 'color' => \
$Git::SVN
::Log
::color
,
142 'pager=s' => \
$Git::SVN
::Log
::pager
,
144 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
145 { 'merge|m|M' => \
$_merge,
146 'verbose|v' => \
$_verbose,
147 'strategy|s=s' => \
$_strategy,
148 'local|l' => \
$_local,
149 'fetch-all|all' => \
$_fetch_all,
151 'commit-diff' => [ \
&cmd_commit_diff
,
152 'Commit a diff between two trees',
153 { 'message|m=s' => \
$_message,
154 'file|F=s' => \
$_file,
155 'revision|r=s' => \
$_revision,
160 for (my $i = 0; $i < @ARGV; $i++) {
161 if (defined $cmd{$ARGV[$i]}) {
168 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
170 read_repo_config
(\
%opts);
171 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
172 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
173 'id|i=s' => \
$Git::SVN
::default_ref_id
,
174 'svn-remote|remote|R=s' => sub {
175 $Git::SVN
::no_reuse_existing
= 1;
176 $Git::SVN
::default_repo_id
= $_[1] });
177 exit 1 if (!$rv && $cmd ne 'log');
180 version
() if $_version;
181 usage
(1) unless defined $cmd;
182 load_authors
() if $_authors;
184 # make sure we're always running
185 unless ($cmd =~ /(?:clone|init|multi-init)$/) {
186 unless (-d
$ENV{GIT_DIR
}) {
187 if ($git_dir_user_set) {
188 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
189 "but it is not a directory\n";
191 my $git_dir = delete $ENV{GIT_DIR
};
192 chomp(my $cdup = command_oneline
(qw
/rev-parse --show-cdup/));
193 unless (length $cdup) {
194 die "Already at toplevel, but $git_dir ",
195 "not found '$cdup'\n";
197 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
198 unless (-d
$git_dir) {
199 die "$git_dir still not found after going to ",
202 $ENV{GIT_DIR
} = $git_dir;
205 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
206 Git
::SVN
::Migration
::migration_check
();
208 Git
::SVN
::init_vars
();
210 Git
::SVN
::verify_remotes_sanity
();
211 $cmd{$cmd}->[0]->(@ARGV);
214 post_fetch_checkout
();
217 ####################### primary functions ######################
219 my $exit = shift || 0;
220 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
222 git
-svn
- bidirectional operations between a single Subversion tree
and git
223 Usage
: $0 <command
> [options
] [arguments
]\n
225 print $fd "Available commands:\n" unless $cmd;
227 foreach (sort keys %cmd) {
228 next if $cmd && $cmd ne $_;
229 next if /^multi-/; # don't show deprecated commands
230 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
231 foreach (keys %{$cmd{$_}->[2]}) {
232 # prints out arguments as they should be passed:
233 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
234 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
236 split /\|/,$_)," $x\n";
240 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
241 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
242 one git repository and want to keep them separate. See git-svn(1) for more
249 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
254 unless (-d $ENV{GIT_DIR}) {
255 my @init_db = ('init
');
256 push @init_db, "--template=$_template" if defined $_template;
257 if (defined $_shared) {
258 if ($_shared =~ /[a-z]/) {
259 push @init_db, "--shared=$_shared";
261 push @init_db, "--shared";
264 command_noisy(@init_db);
267 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
268 foreach my $i (keys %icv) {
269 die "'$set' and '$i' cannot both be set\n" if $set;
270 next unless defined $icv{$i};
271 command_noisy('config
', "$pfx.$i", $icv{$i});
277 my $repo_path = shift or return;
278 mkpath([$repo_path]) unless -d $repo_path;
279 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
280 $ENV{GIT_DIR} = '.git';
284 my ($url, $path) = @_;
285 if (!defined $path &&
286 (defined $_trunk || defined $_branches || defined $_tags) &&
287 $url !~ m#^[a-z\+]+://#) {
290 $path = basename($url) if !defined $path || !length $path;
291 cmd_init($url, $path);
292 Git::SVN::fetch_all($Git::SVN::default_repo_id);
296 if (defined $_trunk || defined $_branches || defined $_tags) {
297 return cmd_multi_init(@_);
299 my $url = shift or die "SVN repository location required
",
300 "as a command
-line argument
\n";
304 Git::SVN->init($url);
308 if (grep /^\d+=./, @_) {
309 die "'<rev>=<commit>' fetch arguments are
",
310 "no longer supported
.\n";
314 die "Usage
: $0 fetch
[--all
] [svn
-remote
]\n";
316 $remote ||= $Git::SVN::default_repo_id;
320 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
326 if ($_stdin || !@commits) {
327 print "Reading from stdin
...\n";
330 if (/\b($sha1_short)\b/o) {
331 unshift @commits, $1;
336 foreach my $c (@commits) {
337 my @tmp = command('rev-parse',$c);
338 if (scalar @tmp == 1) {
340 } elsif (scalar @tmp > 1) {
341 push @revs, reverse(command('rev-list',@tmp));
343 fatal "Failed to rev
-parse
$c\n";
346 my $gs = Git::SVN->new;
347 my ($r_last, $cmt_last) = $gs->last_rev_commit;
349 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
350 fatal "There are new revisions that were fetched
",
351 "and need to be merged
(or acknowledged
) ",
352 "before committing
.\nlast rev
: $r_last\n",
353 " current
: $gs->{last_rev
}\n";
355 $gs->set_tree($_) foreach @revs;
356 print "Done committing
",scalar @revs," revisions to SVN
\n";
363 my ($url, $rev, $uuid) = working_head_info($head, \@refs);
365 unless (defined $url && defined $rev && defined $uuid) {
366 die "Unable to determine upstream SVN information from
",
369 my $gs = Git::SVN->find_by_url($url);
371 foreach my $d (@refs) {
372 if (!verify_ref("$d~1")) {
374 "has
no parent commit
, and therefore
",
375 "nothing to diff against
.\n",
376 "You should be working from a repository
",
377 "originally created by git
-svn
\n";
379 unless (defined $last_rev) {
380 (undef, $last_rev, undef) = cmt_metadata("$d~1");
381 unless (defined $last_rev) {
382 fatal "Unable to extract revision information
",
383 "from commit
$d~1\n";
387 print "diff
-tree
$d~1 $d\n";
389 my %ed_opts = ( r => $last_rev,
390 log => get_commit_entry($d)->{log},
391 ra => Git::SVN::Ra->new($url),
395 print "Committed r
$_[0]\n";
396 $last_rev = $_[0]; },
398 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
399 print "No changes
\n$d~1 == $d\n";
405 warn "Could
not determine fetch information
for $url\n",
406 "Will
not attempt to fetch
and rebase commits
.\n",
407 "This probably means you have useSvmProps
and should
\n",
408 "now resync your SVN
::Mirror repository
.\n";
411 $_fetch_all ? $gs->fetch_all : $gs->fetch;
412 # we always want to rebase against the current HEAD, not any
413 # head that was passed to us
414 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
417 @finish = rebase_cmd();
418 print STDERR "W
: HEAD
and ", $gs->refname, " differ
, ",
419 "using
@finish:\n", "@diff";
421 print "No changes between current HEAD
and ",
422 $gs->refname, "\nResetting to the latest
",
424 @finish = qw/reset --mixed/;
426 command_noisy(@finish, $gs->refname);
430 command_noisy(qw/update-index --refresh/);
431 my $url = (working_head_info('HEAD'))[0];
433 die "Unable to determine upstream SVN information from
",
434 "working tree history
\n";
437 my $gs = Git::SVN->find_by_url($url);
439 die "Unable to determine remote information from URL
: $url\n";
441 if (command(qw/diff-index HEAD --/)) {
442 print STDERR "Cannot rebase with uncommited changes
:\n";
443 command_noisy('status');
447 $_fetch_all ? $gs->fetch_all : $gs->fetch;
449 command_noisy(rebase_cmd(), $gs->refname);
452 sub cmd_show_ignore {
453 my $url = (::working_head_info('HEAD'))[0];
454 my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
455 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
456 $gs->traverse_ignore(\*STDOUT, $gs->{path}, $r);
461 unless (defined $_trunk || defined $_branches || defined $_tags) {
464 $_prefix = '' unless defined $_prefix;
470 if (defined $_trunk) {
471 my $trunk_ref = $_prefix . 'trunk';
472 # try both old-style and new-style lookups:
473 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
475 my ($trunk_url, $trunk_path) =
476 complete_svn_url($url, $_trunk);
477 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
481 return unless defined $_branches || defined $_tags;
482 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
483 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
484 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
487 sub cmd_multi_fetch {
488 my $remotes = Git::SVN::read_all_remotes();
489 foreach my $repo_id (sort keys %$remotes) {
490 if ($remotes->{$repo_id}->{url}) {
491 Git::SVN::fetch_all($repo_id, $remotes);
496 # this command is special because it requires no metadata
497 sub cmd_commit_diff {
498 my ($ta, $tb, $url) = @_;
499 my $usage = "Usage
: $0 commit
-diff
-r
<revision
> ".
500 "<tree
-ish
> <tree
-ish
> [<URL
>]\n";
501 fatal($usage) if (!defined $ta || !defined $tb);
504 my $gs = eval { Git::SVN->new };
506 fatal("Needed URL
or usable git
-svn
--id
in ",
507 "the command
-line
\n", $usage);
510 $svn_path = $gs->{path};
512 unless (defined $_revision) {
513 fatal("-r
|--revision is a required argument
\n", $usage);
515 if (defined $_message && defined $_file) {
516 fatal("Both
--message
/-m and --file/-F specified
",
517 "for the commit message
.\n",
518 "I have
no idea what you mean
\n");
520 if (defined $_file) {
521 $_message = file_to_s($_file);
523 $_message ||= get_commit_entry($tb)->{log};
525 my $ra ||= Git::SVN::Ra->new($url);
526 $svn_path ||= $ra->{svn_path};
529 $r = $ra->get_latest_revnum;
530 } elsif ($r !~ /^\d+$/) {
531 die "revision argument
: $r not understood by git
-svn
\n";
533 my %ed_opts = ( r => $r,
538 editor_cb => sub { print "Committed r
$_[0]\n" },
539 svn_path => $svn_path );
540 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
541 print "No changes
\n$ta == $tb\n";
545 ########################### utility functions #########################
548 my @cmd = qw/rebase/;
549 push @cmd, '-v' if $_verbose;
550 push @cmd, qw/--merge/ if $_merge;
551 push @cmd, "--strategy
=$_strategy" if $_strategy;
555 sub post_fetch_checkout {
556 return if $_no_checkout;
557 my $gs = $Git::SVN::_head or return;
558 return if verify_ref('refs/heads/master^0');
560 my $valid_head = verify_ref('HEAD^0');
561 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
562 return if ($valid_head || !verify_ref
('HEAD^0'));
564 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
565 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
568 chomp(my $bare = `git config --bool --get core.bare`);
569 return if $bare eq 'true';
570 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
571 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
572 print STDERR
"Checked out HEAD:\n ",
573 $gs->full_url, " r", $gs->last_rev, "\n";
576 sub complete_svn_url
{
577 my ($url, $path) = @_;
579 if ($path !~ m
#^[a-z\+]+://#) {
580 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
581 fatal
("E: '$path' is not a complete URL ",
582 "and a separate URL is not specified\n");
584 return ($url, $path);
589 sub complete_url_ls_init
{
590 my ($ra, $repo_path, $switch, $pfx) = @_;
591 unless ($repo_path) {
592 print STDERR
"W: $switch not specified\n";
595 $repo_path =~ s
#/+$##;
596 if ($repo_path =~ m
#^[a-z\+]+://#) {
597 $ra = Git
::SVN
::Ra
->new($repo_path);
600 $repo_path =~ s
#^/+##;
602 fatal
("E: '$repo_path' is not a complete URL ",
603 "and a separate URL is not specified\n");
606 my $url = $ra->{url
};
607 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
608 my $k = "svn-remote.$gs->{repo_id}.url";
609 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
610 if ($orig_url && ($orig_url ne $gs->{url
})) {
611 die "$k already set: $orig_url\n",
612 "wanted to set to: $gs->{url}\n";
614 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
615 my $remote_path = "$ra->{svn_path}/$repo_path/*";
616 $remote_path =~ s
#/+#/#g;
617 $remote_path =~ s
#^/##g;
618 my ($n) = ($switch =~ /^--(\w+)/);
619 if (length $pfx && $pfx !~ m
#/$#) {
620 die "--prefix='$pfx' must have a trailing slash '/'\n";
622 command_noisy
('config', "svn-remote.$gs->{repo_id}.$n",
623 "$remote_path:refs/remotes/$pfx*");
628 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
632 sub get_tree_from_treeish
{
634 # $treeish can be a symbolic ref, too:
635 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
637 while ($type eq 'tag') {
638 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
640 if ($type eq 'commit') {
641 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
643 ($expected) = ($expected =~ /^tree ($sha1)$/o);
644 die "Unable to get tree from $treeish\n" unless $expected;
645 } elsif ($type eq 'tree') {
646 $expected = $treeish;
648 die "$treeish is a $type, expected tree, tag or commit\n";
653 sub get_commit_entry
{
654 my ($treeish) = shift;
655 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
656 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
657 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
658 open my $log_fh, '>', $commit_editmsg or croak
$!;
660 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
661 if ($type eq 'commit' || $type eq 'tag') {
662 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
667 $in_msg = 1 if (/^\s*$/);
668 } elsif (/^git-svn-id: /) {
669 # skip this for now, we regenerate the
670 # correct one on re-fetch anyways
671 # TODO: set *:merge properties or like...
673 print $log_fh $_ or croak
$!;
676 command_close_pipe
($msg_fh, $ctx);
678 close $log_fh or croak
$!;
680 if ($_edit || ($type eq 'tree')) {
681 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
682 # TODO: strip out spaces, comments, like git-commit.sh
683 system($editor, $commit_editmsg);
685 rename $commit_editmsg, $commit_msg or croak
$!;
686 open $log_fh, '<', $commit_msg or croak
$!;
687 { local $/; chomp($log_entry{log} = <$log_fh>); }
688 close $log_fh or croak
$!;
694 my ($str, $file, $mode) = @_;
695 open my $fd,'>',$file or croak
$!;
696 print $fd $str,"\n" or croak
$!;
697 close $fd or croak
$!;
698 chmod ($mode &~ umask, $file) if (defined $mode);
703 open my $fd,'<',$file or croak
"$!: file: $file\n";
706 close $fd or croak
$!;
711 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
713 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
714 my $log = $cmd eq 'log';
717 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
718 my ($user, $name, $email) = ($1, $2, $3);
720 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
722 $users{$user} = [$name, $email];
725 close $authors or croak
$!;
728 # convert GetOpt::Long specs for use by git-config
729 sub read_repo_config
{
730 return unless -d
$ENV{GIT_DIR
};
733 foreach my $o (keys %$opts) {
734 # if we have mixedCase and a long option-only, then
735 # it's a config-only variable that we don't need for
737 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
739 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
741 my $arg = 'git-config';
742 $arg .= ' --int' if ($o =~ /[:=]i$/);
743 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
744 if (ref $v eq 'ARRAY') {
745 chomp(my @tmp = `$arg --get-all svn.$key`);
748 chomp(my $tmp = `$arg --get svn.$key`);
749 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
754 delete @
$opts{@config_only} if @config_only;
757 sub extract_metadata
{
758 my $id = shift or return (undef, undef, undef);
759 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
761 if (!defined $rev || !$uuid || !$url) {
762 # some of the original repositories I made had
763 # identifiers like this:
764 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
766 return ($url, $rev, $uuid);
770 return extract_metadata
((grep(/^git-svn-id: /,
771 command
(qw
/cat-file commit/, shift)))[-1]);
774 sub working_head_info
{
775 my ($head, $refs) = @_;
776 my ($url, $rev, $uuid);
777 my ($fh, $ctx) = command_output_pipe
('rev-list', $head);
780 ($url, $rev, $uuid) = cmt_metadata
($_);
781 last if (defined $url && defined $rev && defined $uuid);
782 unshift @
$refs, $_ if $refs;
784 close $fh; # break the pipe
791 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
792 $_repack $_repack_flags $_use_svm_props $_head
793 $_use_svnsync_props $no_reuse_existing/;
795 use File
::Path qw
/mkpath/;
796 use File
::Copy qw
/copy/;
800 # properties that we do not log:
803 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
804 svn
:special svn
:executable
805 svn
:entry
:committed
-rev
806 svn
:entry
:last-author
808 svn
:entry
:committed
-date
/;
810 # some options are read globally, but can be overridden locally
811 # per [svn-remote "..."] section. Command-line options will *NOT*
812 # override options set in an [svn-remote "..."] section
814 foreach (qw
/follow_parent no_metadata use_svm_props
815 use_svnsync_props
/) {
820 return \$self->{-$_} if exists \$self->{-$_};
821 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
822 eval { command_oneline(qw/config --get/, \$k) };
824 \$self->{-$_} = \$Git::SVN::_$_;
826 my \$v = command_oneline(qw/config --bool/,\$k);
827 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
829 return \$self->{-$_} }\n";
836 END { unlink keys %LOCKFILES if %LOCKFILES }
838 sub resolve_local_globs
{
839 my ($url, $fetch, $glob_spec) = @_;
840 return unless defined $glob_spec;
841 my $ref = $glob_spec->{ref};
842 my $path = $glob_spec->{path
};
843 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
844 next unless m
#^refs/remotes/$ref->{regex}$#;
846 my $pathname = $path->full_path($p);
847 my $refname = $ref->full_path($p);
848 if (my $existing = $fetch->{$pathname}) {
849 if ($existing ne $refname) {
850 die "Refspec conflict:\n",
851 "existing: refs/remotes/$existing\n",
852 " globbed: refs/remotes/$refname\n";
854 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
855 $u =~ s!^\Q$url\E(/|$)!! or die
856 "refs/remotes/$refname: '$url' not found in '$u'\n";
857 if ($pathname ne $u) {
858 warn "W: Refspec glob conflict ",
859 "(ref: refs/remotes/$refname):\n",
860 "expected path: $pathname\n",
862 "Continuing ahead with $u\n";
866 $fetch->{$pathname} = $refname;
871 sub parse_revision_argument
{
872 my ($base, $head) = @_;
873 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
874 return ($base, $head);
876 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
877 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
878 return ($head, $head) if ($::_revision
eq 'HEAD');
879 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
880 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
881 die "revision argument: $::_revision not understood by git-svn\n";
885 my ($repo_id, $remotes) = @_;
889 $repo_id = $gs->{repo_id
};
891 $remotes ||= read_all_remotes
();
892 my $remote = $remotes->{$repo_id} or
893 die "[svn-remote \"$repo_id\"] unknown\n";
894 my $fetch = $remote->{fetch
};
895 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
897 my $ra = Git
::SVN
::Ra
->new($url);
898 my $uuid = $ra->get_uuid;
899 my $head = $ra->get_latest_revnum;
900 my $base = defined $fetch ?
$head : 0;
902 # read the max revs for wildcard expansion (branches/*, tags/*)
903 foreach my $t (qw
/branches tags/) {
904 defined $remote->{$t} or next;
905 push @globs, $remote->{$t};
906 my $max_rev = eval { tmp_config
(qw
/--int --get/,
907 "svn-remote.$repo_id.${t}-maxRev") };
908 if (defined $max_rev && ($max_rev < $base)) {
910 } elsif (!defined $max_rev) {
916 foreach my $p (sort keys %$fetch) {
917 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
918 my $lr = $gs->rev_db_max;
920 $base = $lr if ($lr < $base);
926 ($base, $head) = parse_revision_argument
($base, $head);
927 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
930 sub read_all_remotes
{
932 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
933 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
934 $r->{$1}->{fetch
}->{$2} = $3;
935 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
936 $r->{$1}->{url
} = $2;
937 } elsif (m
!^(.+)\
.(branches
|tags
)=
938 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
939 my ($p, $g) = ($3, $4);
940 my $rs = $r->{$1}->{$2} = {
943 path
=> Git
::SVN
::GlobSpec
->new($p),
944 ref => Git
::SVN
::GlobSpec
->new($g) };
945 if (length($rs->{ref}->{right
}) != 0) {
946 die "The '*' glob character must be the last ",
947 "character of '$g'\n";
955 if (defined $_repack) {
956 $_repack = 1000 if ($_repack <= 0);
957 $_repack_nr = $_repack;
958 $_repack_flags ||= '-d';
962 sub verify_remotes_sanity
{
963 return unless -d
$ENV{GIT_DIR
};
965 foreach (command
(qw
/config -l/)) {
966 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
968 die "Remote ref refs/remote/$1 is tracked by",
969 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
970 "Please resolve this ambiguity in ",
971 "your git configuration file before ",
979 # we allow more chars than remotes2config.sh...
980 sub sanitize_remote_name
{
982 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
986 sub find_existing_remote
{
987 my ($url, $remotes) = @_;
988 return undef if $no_reuse_existing;
990 foreach my $repo_id (keys %$remotes) {
991 my $u = $remotes->{$repo_id}->{url
} or next;
993 $existing = $repo_id;
999 sub init_remote_config
{
1000 my ($self, $url, $no_write) = @_;
1001 $url =~ s!/+$!!; # strip trailing slash
1002 my $r = read_all_remotes
();
1003 my $existing = find_existing_remote
($url, $r);
1005 unless ($no_write) {
1006 print STDERR
"Using existing ",
1007 "[svn-remote \"$existing\"]\n";
1009 $self->{repo_id
} = $existing;
1011 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1012 $existing = find_existing_remote
($min_url, $r);
1014 unless ($no_write) {
1015 print STDERR
"Using existing ",
1016 "[svn-remote \"$existing\"]\n";
1018 $self->{repo_id
} = $existing;
1020 if ($min_url ne $url) {
1021 unless ($no_write) {
1022 print STDERR
"Using higher level of URL: ",
1023 "$url => $min_url\n";
1025 my $old_path = $self->{path
};
1026 $self->{path
} = $url;
1027 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1028 if (length $old_path) {
1029 $self->{path
} .= "/$old_path";
1036 # verify that we aren't overwriting anything:
1038 command_oneline
('config', '--get',
1039 "svn-remote.$self->{repo_id}.url")
1041 if ($orig_url && ($orig_url ne $url)) {
1042 die "svn-remote.$self->{repo_id}.url already set: ",
1043 "$orig_url\nwanted to set to: $url\n";
1046 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1047 if (defined $xpath) {
1048 die "svn-remote.$xrepo_id.fetch already set to track ",
1049 "$xpath:refs/remotes/", $self->refname, "\n";
1051 unless ($no_write) {
1052 command_noisy
('config',
1053 "svn-remote.$self->{repo_id}.url", $url);
1054 command_noisy
('config', '--add',
1055 "svn-remote.$self->{repo_id}.fetch",
1056 "$self->{path}:".$self->refname);
1058 $self->{url
} = $url;
1061 sub find_by_url
{ # repos_root and, path are optional
1062 my ($class, $full_url, $repos_root, $path) = @_;
1063 return undef unless defined $full_url;
1064 my $remotes = read_all_remotes
();
1065 if (defined $full_url && defined $repos_root && !defined $path) {
1067 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1069 foreach my $repo_id (keys %$remotes) {
1070 my $u = $remotes->{$repo_id}->{url
} or next;
1071 next if defined $repos_root && $repos_root ne $u;
1073 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1074 foreach (qw
/branches tags/) {
1075 resolve_local_globs
($u, $fetch,
1076 $remotes->{$repo_id}->{$_});
1079 unless (defined $p) {
1081 $p =~ s
#^\Q$u\E(?:/|$)## or next;
1083 foreach my $f (keys %$fetch) {
1085 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1092 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1093 my $self = _new
($class, $repo_id, $ref_id, $path);
1095 $self->init_remote_config($url, $no_write);
1102 foreach (command
(qw
/config -l/)) {
1103 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1104 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1105 my ($repo_id, $path, $ref) = ($1, $2, $3);
1106 if ($ref eq $ref_id) {
1107 $path = '' if ($path =~ m
#^\./?#);
1108 return ($repo_id, $path);
1111 (undef, undef, undef);
1115 my ($class, $ref_id, $repo_id, $path) = @_;
1116 if (defined $ref_id && !defined $repo_id && !defined $path) {
1117 ($repo_id, $path) = find_ref
($ref_id);
1118 if (!defined $repo_id) {
1119 die "Could not find a \"svn-remote.*.fetch\" key ",
1120 "in the repository configuration matching: ",
1121 "refs/remotes/$ref_id\n";
1124 my $self = _new
($class, $repo_id, $ref_id, $path);
1125 if (!defined $self->{path
} || !length $self->{path
}) {
1126 my $fetch = command_oneline
('config', '--get',
1127 "svn-remote.$repo_id.fetch",
1128 ":refs/remotes/$ref_id\$") or
1129 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1130 "\":refs/remotes/$ref_id\$\" in config\n";
1131 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1133 $self->{url
} = command_oneline
('config', '--get',
1134 "svn-remote.$repo_id.url") or
1135 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1140 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
1144 return $self->{svm
}->{uuid
} if $self->svm;
1146 unless ($self->{svm
}) {
1147 die "SVM UUID not cached, and reading remotely failed\n";
1149 $self->{svm
}->{uuid
};
1154 return $self->{svm
} if $self->{svm
};
1156 # see if we have it in our config, first:
1158 my $section = "svn-remote.$self->{repo_id}";
1160 source
=> tmp_config
('--get', "$section.svm-source"),
1161 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1162 replace
=> tmp_config
('--get', "$section.svm-replace"),
1165 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1166 $self->{svm
} = $svm;
1172 my ($self, $ra) = @_;
1173 return $ra if $self->svm;
1175 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1176 "(svm:source, svm:uuid) ",
1177 "from the following URLs:\n" );
1178 sub read_svm_props
{
1179 my ($self, $ra, $path, $r) = @_;
1180 my $props = ($ra->get_dir($path, $r))[2];
1181 my $src = $props->{'svm:source'};
1182 my $uuid = $props->{'svm:uuid'};
1183 return undef if (!$src || !$uuid);
1187 $uuid =~ m{^[0-9a-f\-]{30,}$}
1188 or die "doesn't look right - svm:uuid is '$uuid'\n";
1190 # the '!' is used to mark the repos_root!/relative/path
1191 $src =~ s{/?!/?}{/};
1192 $src =~ s{/+$}{}; # no trailing slashes please
1193 # username is of no interest
1194 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1196 my $replace = $ra->{url
};
1197 $replace .= "/$path" if length $path;
1199 my $section = "svn-remote.$self->{repo_id}";
1200 tmp_config
("$section.svm-source", $src);
1201 tmp_config
("$section.svm-replace", $replace);
1202 tmp_config
("$section.svm-uuid", $uuid);
1210 my $r = $ra->get_latest_revnum;
1211 my $path = $self->{path
};
1213 while (length $path) {
1214 unless ($tried{"$self->{url}/$path"}) {
1215 return $ra if $self->read_svm_props($ra, $path, $r);
1216 $tried{"$self->{url}/$path"} = 1;
1218 $path =~ s
#/?[^/]+$##;
1220 die "Path: '$path' should be ''\n" if $path ne '';
1221 return $ra if $self->read_svm_props($ra, $path, $r);
1222 $tried{"$self->{url}/$path"} = 1;
1224 if ($ra->{repos_root
} eq $self->{url
}) {
1225 die @err, (map { " $_\n" } keys %tried), "\n";
1228 # nope, make sure we're connected to the repository root:
1231 $path = $ra->{svn_path
};
1232 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1233 while (length $path) {
1234 unless ($tried{"$ra->{url}/$path"}) {
1235 $ok = $self->read_svm_props($ra, $path, $r);
1237 $tried{"$ra->{url}/$path"} = 1;
1239 $path =~ s
#/?[^/]+$##;
1241 die "Path: '$path' should be ''\n" if $path ne '';
1242 $ok ||= $self->read_svm_props($ra, $path, $r);
1243 $tried{"$ra->{url}/$path"} = 1;
1245 die @err, (map { " $_\n" } keys %tried), "\n";
1247 Git
::SVN
::Ra
->new($self->{url
});
1252 return $self->{svnsync
} if $self->{svnsync
};
1254 if ($self->no_metadata) {
1255 die "Can't have both 'noMetadata' and ",
1256 "'useSvnsyncProps' options set!\n";
1258 if ($self->rewrite_root) {
1259 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1264 # see if we have it in our config, first:
1266 my $section = "svn-remote.$self->{repo_id}";
1268 url
=> tmp_config
('--get', "$section.svnsync-url"),
1269 uuid
=> tmp_config
('--get', "$section.svnsync-uuid"),
1272 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1273 return $self->{svnsync
} = $svnsync;
1276 my $err = "useSvnsyncProps set, but failed to read " .
1277 "svnsync property: svn:sync-from-";
1278 my $rp = $self->ra->rev_proplist(0);
1280 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1281 $url =~ m{^[a-z\+]+://} or
1282 die "doesn't look right - svn:sync-from-url is '$url'\n";
1284 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1285 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1286 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1288 my $section = "svn-remote.$self->{repo_id}";
1289 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1290 tmp_config
('--add', "$section.svnsync-url", $url);
1291 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1294 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1295 # remote lookup (useful for 'git svn log').
1298 unless ($self->{ra_uuid
}) {
1299 my $key = "svn-remote.$self->{repo_id}.uuid";
1300 my $uuid = eval { tmp_config
('--get', $key) };
1301 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1302 $self->{ra_uuid
} = $uuid;
1304 die "ra_uuid called without URL\n" unless $self->{url
};
1305 $self->{ra_uuid
} = $self->ra->get_uuid;
1306 tmp_config
('--add', $key, $self->{ra_uuid
});
1314 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1315 if ($self->use_svm_props && !$self->{svm
}) {
1316 if ($self->no_metadata) {
1317 die "Can't have both 'noMetadata' and ",
1318 "'useSvmProps' options set!\n";
1319 } elsif ($self->use_svnsync_props) {
1320 die "Can't have both 'useSvnsyncProps' and ",
1321 "'useSvmProps' options set!\n";
1323 $ra = $self->_set_svm_vars($ra);
1324 $self->{-want_revprops
} = 1;
1331 my $repos_root = $self->ra->{repos_root
};
1332 return $self->{path
} if ($self->{url
} eq $repos_root);
1333 my $url = $self->{url
} .
1334 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
1335 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1339 sub traverse_ignore
{
1340 my ($self, $fh, $path, $r) = @_;
1343 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
1345 $p =~ s
#^\Q$self->{path}\E(/|$)##;
1346 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
1347 if (my $s = $props->{'svn:ignore'}) {
1348 $s =~ s/[\r\n]+/\n/g;
1350 if (length $p == 0) {
1354 $s =~ s
#\n#\n/$p/#g;
1355 print $fh "/$p/$s\n";
1358 foreach (sort keys %$dirent) {
1359 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
1360 $self->traverse_ignore($fh, "$path/$_", $r);
1364 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1365 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1367 # returns the newest SVN revision number and newest commit SHA1
1368 sub last_rev_commit
{
1370 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1371 return ($self->{last_rev
}, $self->{last_commit
});
1373 my $c = ::verify_ref
($self->refname.'^0');
1374 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1375 my $rev = (::cmt_metadata
($c))[1];
1377 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1381 my $db_path = $self->db_path;
1382 unless (-e
$db_path) {
1383 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1384 return (undef, undef);
1386 my $offset = -41; # from tail
1388 open my $fh, '<', $db_path or croak
"$db_path not readable: $!\n";
1389 sysseek($fh, $offset, 2); # don't care for errors
1390 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1392 while (('0' x40
) eq $rl && sysseek($fh, 0, 1) != 0) {
1394 sysseek($fh, $offset, 2); # don't care for errors
1395 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1398 if ($c && $c ne $rl) {
1399 die "$db_path and ", $self->refname,
1400 " inconsistent!:\n$c != $rl\n";
1402 my $rev = sysseek($fh, 0, 1) or croak
$!;
1403 $rev = ($rev - 41) / 41;
1404 close $fh or croak
$!;
1405 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1409 sub get_fetch_range
{
1410 my ($self, $min, $max) = @_;
1411 $max ||= $self->ra->get_latest_revnum;
1412 $min ||= $self->rev_db_max;
1418 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1419 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1420 if (-e
$old_def_config && ! -e
$config) {
1421 rename $old_def_config, $config or
1422 die "Failed rename $old_def_config => $config: $!\n";
1424 my $old_config = $ENV{GIT_CONFIG
};
1425 $ENV{GIT_CONFIG
} = $config;
1428 unless (-f
$config) {
1430 open my $fh, '>', $config or
1431 die "Can't open $config: $!\n";
1432 print $fh "; This file is used internally by ",
1434 "Couldn't write to $config: $!\n";
1435 print $fh "; You should not have to edit it\n" or
1436 die "Couldn't write to $config: $!\n";
1437 close $fh or die "Couldn't close $config: $!\n";
1439 command
('config', @args);
1442 if (defined $old_config) {
1443 $ENV{GIT_CONFIG
} = $old_config;
1445 delete $ENV{GIT_CONFIG
};
1448 wantarray ?
@ret : $ret[0];
1452 my ($self, $sub) = @_;
1453 my $old_index = $ENV{GIT_INDEX_FILE
};
1454 $ENV{GIT_INDEX_FILE
} = $self->{index};
1457 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
1458 mkpath
([$dir]) unless -d
$dir;
1462 if (defined $old_index) {
1463 $ENV{GIT_INDEX_FILE
} = $old_index;
1465 delete $ENV{GIT_INDEX_FILE
};
1468 wantarray ?
@ret : $ret[0];
1471 sub assert_index_clean
{
1472 my ($self, $treeish) = @_;
1474 $self->tmp_index_do(sub {
1475 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1476 my $x = command_oneline
('write-tree');
1477 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1478 /^tree ($::sha1)/mo);
1481 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1482 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1483 command_noisy
('read-tree', $treeish);
1484 $x = command_oneline
('write-tree');
1486 ::fatal
"trees ($treeish) $y != $x\n",
1487 "Something is seriously wrong...\n";
1492 sub get_commit_parents
{
1493 my ($self, $log_entry) = @_;
1494 my (%seen, @ret, @tmp);
1495 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1496 if (my $ip = $self->{inject_parents
}) {
1497 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
1501 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1504 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1505 while (my $p = shift @tmp) {
1509 # MAXPARENT is defined to 16 in commit-tree.c:
1513 die "r$log_entry->{revision}: No room for parents:\n\t",
1514 join("\n\t", @tmp), "\n";
1521 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
1522 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1523 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
1526 if ($rwr !~ m
#^[a-z\+]+://#) {
1527 die "$rwr is not a valid URL (key: $k)\n";
1530 $self->{-rewrite_root
} = $rwr;
1535 ($self->rewrite_root || $self->{url
}) .
1536 (length $self->{path
} ?
'/' . $self->{path
} : '');
1541 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
1545 my ($self, $log_entry) = @_;
1546 my $lr = $self->last_rev;
1547 if (defined $lr && $lr >= $log_entry->{revision
}) {
1548 die "Last fetched revision of ", $self->refname,
1549 " was r$lr, but we are about to fetch: ",
1550 "r$log_entry->{revision}!\n";
1552 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1553 croak
"$log_entry->{revision} = $c already exists! ",
1554 "Why are we refetching it?\n";
1556 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $log_entry->{name
};
1557 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
1558 $log_entry->{email
};
1559 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1561 my $tree = $log_entry->{tree
};
1562 if (!defined $tree) {
1563 $tree = $self->tmp_index_do(sub {
1564 command_oneline
('write-tree') });
1566 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1568 my @exec = ('git-commit-tree', $tree);
1569 foreach ($self->get_commit_parents($log_entry)) {
1570 push @exec, '-p', $_;
1572 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1574 print $msg_fh $log_entry->{log} or croak
$!;
1575 unless ($self->no_metadata) {
1576 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1579 $msg_fh->flush == 0 or croak
$!;
1580 close $msg_fh or croak
$!;
1581 chomp(my $commit = do { local $/; <$out_fh> });
1582 close $out_fh or croak
$!;
1585 if ($commit !~ /^$::sha1$/o) {
1586 die "Failed to commit, invalid sha1: $commit\n";
1589 $self->rev_db_set($log_entry->{revision
}, $commit, 1);
1591 $self->{last_rev
} = $log_entry->{revision
};
1592 $self->{last_commit
} = $commit;
1593 print "r$log_entry->{revision}";
1594 if (defined $log_entry->{svm_revision
}) {
1595 print " (\@$log_entry->{svm_revision})";
1596 $self->rev_db_set($log_entry->{svm_revision
}, $commit,
1597 0, $self->svm_uuid);
1599 print " = $commit ($self->{ref_id})\n";
1600 if (defined $_repack && (--$_repack_nr == 0)) {
1601 $_repack_nr = $_repack;
1602 # repack doesn't use any arguments with spaces in them, does it?
1603 print "Running git repack $_repack_flags ...\n";
1604 command_noisy
('repack', split(/\s+/, $_repack_flags));
1605 print "Done repacking\n";
1611 my ($self, $paths, $r) = @_;
1612 return 1 if $self->{path
} eq '';
1613 if (my $path = $paths->{"/$self->{path}"}) {
1614 return ($path->{action
} eq 'D') ?
0 : 1;
1616 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
1617 if (grep /$self->{path_regex}/, keys %$paths) {
1621 foreach (split m
#/#, $self->{path}) {
1623 next unless ($paths->{$c} &&
1624 ($paths->{$c}->{action
} =~ /^[AR]$/));
1625 if ($self->ra->check_path($self->{path
}, $r) ==
1633 sub find_parent_branch
{
1634 my ($self, $paths, $rev) = @_;
1635 return undef unless $self->follow_parent;
1636 unless (defined $paths) {
1637 my $err_handler = $SVN::Error
::handler
;
1638 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
1639 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
1641 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
1642 $SVN::Error
::handler
= $err_handler;
1644 return undef unless defined $paths;
1646 # look for a parent from another branch:
1647 my @b_path_components = split m
#/#, $self->rel_path;
1648 my @a_path_components;
1650 while (@b_path_components) {
1651 $i = $paths->{'/'.join('/', @b_path_components)};
1652 last if $i && defined $i->{copyfrom_path
};
1653 unshift(@a_path_components, pop(@b_path_components));
1655 return undef unless defined $i && defined $i->{copyfrom_path
};
1656 my $branch_from = $i->{copyfrom_path
};
1657 if (@a_path_components) {
1658 print STDERR
"branch_from: $branch_from => ";
1659 $branch_from .= '/'.join('/', @a_path_components);
1660 print STDERR
$branch_from, "\n";
1662 my $r = $i->{copyfrom_rev
};
1663 my $repos_root = $self->ra->{repos_root
};
1664 my $url = $self->ra->{url
};
1665 my $new_url = $repos_root . $branch_from;
1666 print STDERR
"Found possible branch point: ",
1667 "$new_url => ", $self->full_url, ", $r\n";
1668 $branch_from =~ s
#^/##;
1669 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
1671 my $ref_id = $self->{ref_id
};
1672 $ref_id =~ s/\@\d+$//;
1674 # just grow a tail if we're not unique enough :x
1675 $ref_id .= '-' while find_ref
($ref_id);
1676 print STDERR
"Initializing parent: $ref_id\n";
1677 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id, 1);
1679 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1680 if (!defined $r0 || !defined $parent) {
1682 ($r0, $parent) = $gs->last_rev_commit;
1684 if (defined $r0 && defined $parent) {
1685 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1687 if ($self->ra->can_do_switch) {
1688 $self->assert_index_clean($parent);
1689 print STDERR
"Following parent with do_switch\n";
1690 # do_switch works with svn/trunk >= r22312, but that
1691 # is not included with SVN 1.4.3 (the latest version
1692 # at the moment), so we can't rely on it
1693 $self->{last_commit
} = $parent;
1694 $ed = SVN
::Git
::Fetcher
->new($self);
1695 $gs->ra->gs_do_switch($r0, $rev, $gs,
1696 $self->full_url, $ed)
1697 or die "SVN connection failed somewhere...\n";
1699 print STDERR
"Following parent with do_update\n";
1700 $ed = SVN
::Git
::Fetcher
->new($self);
1701 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1702 or die "SVN connection failed somewhere...\n";
1704 print STDERR
"Successfully followed parent\n";
1705 return $self->make_log_entry($rev, [$parent], $ed);
1711 my ($self, $paths, $rev) = @_;
1713 my ($last_rev, @parents);
1714 if (my $lc = $self->last_commit) {
1715 # we can have a branch that was deleted, then re-added
1716 # under the same name but copied from another path, in
1717 # which case we'll have multiple parents (we don't
1718 # want to break the original ref, nor lose copypath info):
1719 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1720 push @
{$log_entry->{parents
}}, $lc;
1723 $ed = SVN
::Git
::Fetcher
->new($self);
1724 $last_rev = $self->{last_rev
};
1729 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1732 $ed = SVN
::Git
::Fetcher
->new($self);
1734 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1735 die "SVN connection failed somewhere...\n";
1737 $self->make_log_entry($rev, \
@parents, $ed);
1741 my ($self, $ed) = @_;
1743 my $h = $ed->{empty
};
1744 foreach (sort keys %$h) {
1745 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1746 push @out, " $act: " . uri_encode
($_);
1747 warn "W: $act: $_\n";
1749 foreach my $t (qw
/dir_prop file_prop/) {
1750 $h = $ed->{$t} or next;
1751 foreach my $path (sort keys %$h) {
1752 my $ppath = $path eq '' ?
'.' : $path;
1753 foreach my $prop (sort keys %{$h->{$path}}) {
1754 next if $SKIP_PROP{$prop};
1755 my $v = $h->{$path}->{$prop};
1756 my $t_ppath_prop = "$t: " .
1757 uri_encode
($ppath) . ' ' .
1760 push @out, " +$t_ppath_prop " .
1763 push @out, " -$t_ppath_prop";
1768 foreach my $t (qw
/absent_file absent_directory/) {
1769 $h = $ed->{$t} or next;
1770 foreach my $parent (sort keys %$h) {
1771 foreach my $path (sort @
{$h->{$parent}}) {
1772 push @out, " $t: " .
1773 uri_encode
("$parent/$path");
1774 warn "W: $t: $parent/$path ",
1775 "Insufficient permissions?\n";
1782 sub parse_svn_date
{
1783 my $date = shift || return '+0000 1970-01-01 00:00:00';
1784 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1785 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1786 croak
"Unable to parse date: $date\n";
1787 "+0000 $Y-$m-$d $H:$M:$S";
1792 if (!defined $author || length $author == 0) {
1793 $author = '(no author)';
1795 if (defined $::_authors
&& ! defined $::users
{$author}) {
1796 die "Author: $author not defined in $::_authors file\n";
1801 sub make_log_entry
{
1802 my ($self, $rev, $parents, $ed) = @_;
1803 my $untracked = $self->get_untracked($ed);
1805 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1806 print $un "r$rev\n" or croak
$!;
1807 print $un $_, "\n" foreach @
$untracked;
1808 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1812 my $logged = delete $self->{logged_rev_props
};
1813 if (!$logged || $self->{-want_revprops
}) {
1814 my $rp = $self->ra->rev_proplist($rev);
1815 foreach (sort keys %$rp) {
1817 if (/^svn:(author|date|log)$/) {
1818 $log_entry{$1} = $v;
1819 } elsif ($_ eq 'svm:headrev') {
1822 print $un " rev_prop: ", uri_encode
($_), ' ',
1823 uri_encode
($v), "\n";
1827 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1829 close $un or croak
$!;
1831 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1832 $log_entry{log} .= "\n";
1833 my $author = $log_entry{author
} = check_author
($log_entry{author
});
1834 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
1836 if (defined $headrev && $self->use_svm_props) {
1837 if ($self->rewrite_root) {
1838 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1841 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
1842 # we don't want "SVM: initializing mirror for junk" ...
1843 return undef if $r == 0;
1844 my $svm = $self->svm;
1845 if ($uuid ne $svm->{uuid
}) {
1846 die "UUID mismatch on SVM path:\n",
1847 "expected: $svm->{uuid}\n",
1850 my $full_url = $self->full_url;
1851 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1852 die "Failed to replace '$svm->{replace}' with ",
1853 "'$svm->{source}' in $full_url\n";
1854 # throw away username for storing in records
1855 remove_username
($full_url);
1856 $log_entry{metadata
} = "$full_url\@$r $uuid";
1857 $log_entry{svm_revision
} = $r;
1858 $email ||= "$author\@$uuid"
1859 } elsif ($self->use_svnsync_props) {
1860 my $full_url = $self->svnsync->{url
};
1861 $full_url .= "/$self->{path}" if length $self->{path
};
1862 my $uuid = $self->svnsync->{uuid
};
1863 $log_entry{metadata
} = "$full_url\@$rev $uuid";
1864 $email ||= "$author\@$uuid"
1866 $log_entry{metadata
} = $self->metadata_url. "\@$rev " .
1867 $self->ra->get_uuid;
1868 $email ||= "$author\@" . $self->ra->get_uuid;
1870 $log_entry{name
} = $name;
1871 $log_entry{email
} = $email;
1876 my ($self, $min_rev, $max_rev, @parents) = @_;
1877 my ($last_rev, $last_commit) = $self->last_rev_commit;
1878 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1879 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1883 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1884 $self->{inject_parents
} = { $rev => $tree };
1885 $self->fetch(undef, undef);
1889 my ($self, $tree) = (shift, shift);
1890 my $log_entry = ::get_commit_entry
($tree);
1891 unless ($self->{last_rev
}) {
1892 fatal
("Must have an existing revision to commit\n");
1894 my %ed_opts = ( r
=> $self->{last_rev
},
1895 log => $log_entry->{log},
1897 tree_a
=> $self->{last_commit
},
1900 $self->set_tree_cb($log_entry, $tree, @_) },
1901 svn_path
=> $self->{path
} );
1902 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
1903 print "No changes\nr$self->{last_rev} = $tree\n";
1909 my $db_path = $self->db_path;
1910 return if (-e
$db_path && ! -z
$db_path);
1911 return unless ::verify_ref
($self->refname.'^0');
1912 if (-f
$self->{db_root
}) {
1913 rename $self->{db_root
}, $db_path or die
1914 "rename $self->{db_root} => $db_path failed: $!\n";
1915 my ($dir, $base) = ($db_path =~ m
#^(.*?)/?([^/]+)$#);
1916 symlink $base, $self->{db_root
} or die
1917 "symlink $base => $self->{db_root} failed: $!\n";
1920 print "Rebuilding $db_path ...\n";
1921 my ($rev_list, $ctx) = command_output_pipe
("rev-list", $self->refname);
1923 my $full_url = $self->full_url;
1924 remove_username
($full_url);
1926 while (<$rev_list>) {
1929 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1930 my ($url, $rev, $uuid) = ::cmt_metadata
($c);
1931 remove_username
($url);
1933 # ignore merges (from set-tree)
1934 next if (!defined $rev || !$uuid);
1936 # if we merged or otherwise started elsewhere, this is
1937 # how we break out of it
1938 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1939 ($full_url && $url && ($url ne $full_url))) {
1943 $svn_uuid ||= $uuid;
1945 $self->rev_db_set($rev, $c);
1946 print "r$rev = $c\n";
1948 command_close_pipe
($rev_list, $ctx);
1949 print "Done rebuilding $db_path\n";
1953 # Tie::File seems to be prone to offset errors if revisions get sparse,
1954 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1955 # one of my favorite modules is out :< Next up would be one of the DBM
1956 # modules, but I'm not sure which is most portable... So I'll just
1957 # go with something that's plain-text, but still capable of
1958 # being randomly accessed. So here's my ultra-simple fixed-width
1959 # database. All records are 40 characters + "\n", so it's easy to seek
1960 # to a revision: (41 * rev) is the byte offset.
1961 # A record of 40 0s denotes an empty revision.
1962 # And yes, it's still pretty fast (faster than Tie::File).
1963 # These files are disposable unless noMetadata or useSvmProps is set
1966 my ($fh, $rev, $commit) = @_;
1967 my $offset = $rev * 41;
1968 # assume that append is the common case:
1969 seek $fh, 0, 2 or croak
$!;
1971 if ($pos < $offset) {
1972 for (1 .. (($offset - $pos) / 41)) {
1973 print $fh (('0' x
40),"\n") or croak
$!;
1976 seek $fh, $offset, 0 or croak
$!;
1977 print $fh $commit,"\n" or croak
$!;
1983 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1984 mkpath
([$dir]) unless -d
$dir;
1985 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1986 close $fh or die "Couldn't close (create) $path: $!\n";
1991 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1992 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1993 my $db = $self->db_path($uuid);
1994 my $db_lock = "$db.lock";
1997 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
1998 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
2002 $LOCKFILES{$db_lock} = 1;
2004 # both of these options make our .rev_db file very, very important
2005 # and we can't afford to lose it because rebuild() won't work
2006 if ($self->use_svm_props || $self->no_metadata) {
2008 copy
($db, $db_lock) or die "rev_db_set(@_): ",
2010 "$db => $db_lock ($!)\n";
2012 rename $db, $db_lock or die "rev_db_set(@_): ",
2013 "Failed to rename: ",
2014 "$db => $db_lock ($!)\n";
2016 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2017 _rev_db_set
($fh, $rev, $commit);
2019 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2020 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2022 close $fh or croak
$!;
2025 command_noisy
('update-ref', '-m', "r$rev",
2026 $self->refname, $commit);
2028 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2029 "$db_lock => $db ($!)\n";
2030 delete $LOCKFILES{$db_lock};
2032 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2033 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2034 kill $sig, $$ if defined $sig;
2041 my $db_path = $self->db_path;
2042 my @stat = stat $db_path or return 0;
2043 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2044 my $max = $stat[7] / 41;
2045 (($max > 0) ?
$max - 1 : 0);
2049 my ($self, $rev, $uuid) = @_;
2051 my $offset = $rev * 41;
2052 my $db_path = $self->db_path($uuid);
2053 return undef unless -e
$db_path;
2054 open my $fh, '<', $db_path or croak
$!;
2055 if (sysseek($fh, $offset, 0) == $offset) {
2056 my $read = sysread($fh, $ret, 40);
2057 $ret = undef if ($read != 40 || $ret eq ('0'x40
));
2059 close $fh or croak
$!;
2063 sub find_rev_before
{
2064 my ($self, $rev, $eq_ok) = @_;
2065 --$rev unless $eq_ok;
2067 if (my $c = $self->rev_db_get($rev)) {
2072 return (undef, undef);
2076 my ($class, $repo_id, $ref_id, $path) = @_;
2077 unless (defined $repo_id && length $repo_id) {
2078 $repo_id = $Git::SVN
::default_repo_id
;
2080 unless (defined $ref_id && length $ref_id) {
2081 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2083 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
2084 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2085 $_[3] = $path = '' unless (defined $path);
2086 mkpath
(["$ENV{GIT_DIR}/svn"]);
2088 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2089 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2090 db_root
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
2094 my ($self, $uuid) = @_;
2095 $uuid ||= $self->ra_uuid;
2096 "$self->{db_root}.$uuid";
2101 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2105 sub remove_username
{
2106 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2109 package Git
::SVN
::Prompt
;
2113 use vars qw
/$_no_auth_cache $_username/;
2116 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2117 $may_save = undef if $_no_auth_cache;
2118 $default_username = $_username if defined $_username;
2119 if (defined $default_username && length $default_username) {
2120 if (defined $realm && length $realm) {
2121 print STDERR
"Authentication realm: $realm\n";
2124 $cred->username($default_username);
2126 username
($cred, $realm, $may_save, $pool);
2128 $cred->password(_read_password
("Password for '" .
2129 $cred->username . "': ", $realm));
2130 $cred->may_save($may_save);
2131 $SVN::_Core
::SVN_NO_ERROR
;
2134 sub ssl_server_trust
{
2135 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2136 $may_save = undef if $_no_auth_cache;
2137 print STDERR
"Error validating server certificate for '$realm':\n";
2138 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2139 print STDERR
" - The certificate is not issued by a trusted ",
2140 "authority. Use the\n",
2141 " fingerprint to validate the certificate manually!\n";
2143 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2144 print STDERR
" - The certificate hostname does not match.\n";
2146 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2147 print STDERR
" - The certificate is not yet valid.\n";
2149 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2150 print STDERR
" - The certificate has expired.\n";
2152 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2153 print STDERR
" - The certificate has an unknown error.\n";
2156 "Certificate information:\n".
2157 " - Hostname: %s\n".
2158 " - Valid: from %s until %s\n".
2160 " - Fingerprint: %s\n",
2161 map $cert_info->$_, qw(hostname valid_from valid_until
2162 issuer_dname fingerprint);
2165 print STDERR
$may_save ?
2166 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2167 "(R)eject or accept (t)emporarily? ";
2169 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2170 if ($choice =~ /^t$/i) {
2171 $cred->may_save(undef);
2172 } elsif ($choice =~ /^r$/i) {
2174 } elsif ($may_save && $choice =~ /^p$/i) {
2175 $cred->may_save($may_save);
2179 $cred->accepted_failures($failures);
2180 $SVN::_Core
::SVN_NO_ERROR
;
2183 sub ssl_client_cert
{
2184 my ($cred, $realm, $may_save, $pool) = @_;
2185 $may_save = undef if $_no_auth_cache;
2186 print STDERR
"Client certificate filename: ";
2188 chomp(my $filename = <STDIN
>);
2189 $cred->cert_file($filename);
2190 $cred->may_save($may_save);
2191 $SVN::_Core
::SVN_NO_ERROR
;
2194 sub ssl_client_cert_pw
{
2195 my ($cred, $realm, $may_save, $pool) = @_;
2196 $may_save = undef if $_no_auth_cache;
2197 $cred->password(_read_password
("Password: ", $realm));
2198 $cred->may_save($may_save);
2199 $SVN::_Core
::SVN_NO_ERROR
;
2203 my ($cred, $realm, $may_save, $pool) = @_;
2204 $may_save = undef if $_no_auth_cache;
2205 if (defined $realm && length $realm) {
2206 print STDERR
"Authentication realm: $realm\n";
2209 if (defined $_username) {
2210 $username = $_username;
2212 print STDERR
"Username: ";
2214 chomp($username = <STDIN
>);
2216 $cred->username($username);
2217 $cred->may_save($may_save);
2218 $SVN::_Core
::SVN_NO_ERROR
;
2221 sub _read_password
{
2222 my ($prompt, $realm) = @_;
2223 print STDERR
$prompt;
2225 require Term
::ReadKey
;
2226 Term
::ReadKey
::ReadMode
('noecho');
2228 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2229 last if $key =~ /[\012\015]/; # \n\r
2232 Term
::ReadKey
::ReadMode
('restore');
2241 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2242 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2243 $SVN::Node
::none
.$SVN::Node
::file
.
2244 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2245 $SVN::Auth
::SSL
::CNMISMATCH
.
2246 $SVN::Auth
::SSL
::NOTYETVALID
.
2247 $SVN::Auth
::SSL
::EXPIRED
.
2248 $SVN::Auth
::SSL
::UNKNOWNCA
.
2249 $SVN::Auth
::SSL
::OTHER
;
2252 package SVN
::Git
::Fetcher
;
2260 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2262 my ($class, $git_svn) = @_;
2263 my $self = SVN
::Delta
::Editor
->new;
2264 bless $self, $class;
2265 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
2266 $self->{empty
} = {};
2267 $self->{dir_prop
} = {};
2268 $self->{file_prop
} = {};
2269 $self->{absent_dir
} = {};
2270 $self->{absent_file
} = {};
2271 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
2275 sub set_path_strip
{
2276 my ($self, $path) = @_;
2277 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
2284 sub open_directory
{
2285 my ($self, $path, $pb, $rev) = @_;
2290 my ($self, $path) = @_;
2291 if ($self->{path_strip
}) {
2292 $path =~ s!$self->{path_strip}!! or
2293 die "Failed to strip path '$path' ($self->{path_strip})\n";
2299 my ($self, $path, $rev, $pb) = @_;
2301 my $gpath = $self->git_path($path);
2302 return undef if ($gpath eq '');
2304 # remove entire directories.
2305 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
2306 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2308 $self->{c
}, '--', $gpath);
2312 $self->{gii
}->remove($_);
2313 print "\tD\t$_\n" unless $::_q
;
2315 print "\tD\t$gpath/\n" unless $::_q
;
2316 command_close_pipe
($ls, $ctx);
2317 $self->{empty
}->{$path} = 0
2319 $self->{gii
}->remove($gpath);
2320 print "\tD\t$gpath\n" unless $::_q
;
2326 my ($self, $path, $pb, $rev) = @_;
2327 my $gpath = $self->git_path($path);
2328 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
2329 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2330 unless (defined $mode && defined $blob) {
2331 die "$path was not found in commit $self->{c} (r$rev)\n";
2333 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2334 pool
=> SVN
::Pool
->new, action
=> 'M' };
2338 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2339 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2340 delete $self->{empty
}->{$dir};
2341 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2342 pool
=> SVN
::Pool
->new, action
=> 'A' };
2346 my ($self, $path, $cp_path, $cp_rev) = @_;
2347 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2348 delete $self->{empty
}->{$dir};
2349 $self->{empty
}->{$path} = 1;
2353 sub change_dir_prop
{
2354 my ($self, $db, $prop, $value) = @_;
2355 $self->{dir_prop
}->{$db->{path
}} ||= {};
2356 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2360 sub absent_directory
{
2361 my ($self, $path, $pb) = @_;
2362 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2363 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2368 my ($self, $path, $pb) = @_;
2369 $self->{absent_file
}->{$pb->{path
}} ||= [];
2370 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2374 sub change_file_prop
{
2375 my ($self, $fb, $prop, $value) = @_;
2376 if ($prop eq 'svn:executable') {
2377 if ($fb->{mode_b
} != 120000) {
2378 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2380 } elsif ($prop eq 'svn:special') {
2381 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2383 $self->{file_prop
}->{$fb->{path
}} ||= {};
2384 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2389 sub apply_textdelta
{
2390 my ($self, $fb, $exp) = @_;
2391 my $fh = IO
::File
->new_tmpfile;
2393 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2394 # (but $base does not,) so dup() it for reading in close_file
2395 open my $dup, '<&', $fh or croak
$!;
2396 my $base = IO
::File
->new_tmpfile;
2397 $base->autoflush(1);
2399 defined (my $pid = fork) or croak
$!;
2401 open STDOUT
, '>&', $base or croak
$!;
2402 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2403 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2409 seek $base, 0, 0 or croak
$!;
2410 my $md5 = Digest
::MD5
->new;
2411 $md5->addfile($base);
2412 my $got = $md5->hexdigest;
2413 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2415 " got: $got\n" if ($got ne $exp);
2418 seek $base, 0, 0 or croak
$!;
2420 $fb->{base
} = $base;
2421 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2425 my ($self, $fb, $exp) = @_;
2427 my $path = $self->git_path($fb->{path
});
2428 if (my $fh = $fb->{fh
}) {
2429 seek($fh, 0, 0) or croak
$!;
2430 my $md5 = Digest
::MD5
->new;
2432 my $got = $md5->hexdigest;
2433 die "Checksum mismatch: $path\n",
2434 "expected: $exp\n got: $got\n" if ($got ne $exp);
2435 seek($fh, 0, 0) or croak
$!;
2436 if ($fb->{mode_b
} == 120000) {
2437 read($fh, my $buf, 5) == 5 or croak
$!;
2438 $buf eq 'link ' or die "$path has mode 120000",
2439 "but is not a link\n";
2441 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2443 open STDIN
, '<&', $fh or croak
$!;
2444 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2446 chomp($hash = do { local $/; <$out> });
2447 close $out or croak
$!;
2448 close $fh or croak
$!;
2449 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2450 close $fb->{base
} or croak
$!;
2452 $hash = $fb->{blob
} or die "no blob information\n";
2455 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
2456 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
2462 $self->{nr
} = $self->{gii
}->{nr
};
2463 delete $self->{gii
};
2464 $self->SUPER::abort_edit
(@_);
2469 $self->{git_commit_ok
} = 1;
2470 $self->{nr
} = $self->{gii
}->{nr
};
2471 delete $self->{gii
};
2472 $self->SUPER::close_edit
(@_);
2475 package SVN
::Git
::Editor
;
2476 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
2484 my ($class, $opts) = @_;
2485 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
2486 die "$_ required!\n" unless (defined $opts->{$_});
2489 my $pool = SVN
::Pool
->new;
2490 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
2491 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
2494 # $opts->{ra} functions should not be used after this:
2495 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
2496 $opts->{editor_cb
}, $pool);
2497 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
2498 bless $self, $class;
2499 foreach (qw
/svn_path r tree_a tree_b/) {
2500 $self->{$_} = $opts->{$_};
2502 $self->{url
} = $opts->{ra
}->{url
};
2503 $self->{mods
} = $mods;
2504 $self->{types
} = $types;
2505 $self->{pool
} = $pool;
2506 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2508 $self->{path_prefix
} = length $self->{svn_path
} ?
2509 "$self->{svn_path}/" : '';
2514 my ($tree_a, $tree_b) = @_;
2515 my @diff_tree = qw(diff-tree -z -r);
2516 if ($_cp_similarity) {
2517 push @diff_tree, "-C$_cp_similarity";
2519 push @diff_tree, '-C';
2521 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2522 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
2523 push @diff_tree, $tree_a, $tree_b;
2524 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2528 while (<$diff_fh>) {
2529 chomp $_; # this gets rid of the trailing "\0"
2530 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2531 $::sha1\s
($::sha1
)\s
2532 ([MTCRAD
])\d
*$/xo
) {
2533 push @mods, { mode_a
=> $1, mode_b
=> $2,
2534 sha1_b
=> $3, chg
=> $4 };
2535 if ($4 =~ /^(?:C|R)$/) {
2540 } elsif ($state eq 'file_a') {
2541 my $x = $mods[$#mods] or croak
"Empty array\n";
2542 if ($x->{chg
} !~ /^(?:C|R)$/) {
2543 croak
"Error parsing $_, $x->{chg}\n";
2547 } elsif ($state eq 'file_b') {
2548 my $x = $mods[$#mods] or croak
"Empty array\n";
2549 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2550 croak
"Error parsing $_, $x->{chg}\n";
2552 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2553 croak
"Error parsing $_, $x->{chg}\n";
2558 croak
"Error parsing $_\n";
2561 command_close_pipe
($diff_fh, $ctx);
2565 sub check_diff_paths
{
2566 my ($ra, $pfx, $rev, $mods) = @_;
2568 $pfx .= '/' if length $pfx;
2570 sub type_diff_paths
{
2571 my ($ra, $types, $path, $rev) = @_;
2572 my @p = split m
#/+#, $path;
2574 unless (defined $types->{$c}) {
2575 $types->{$c} = $ra->check_path($c, $rev);
2578 $c .= '/' . shift @p;
2579 next if defined $types->{$c};
2580 $types->{$c} = $ra->check_path($c, $rev);
2584 foreach my $m (@
$mods) {
2585 foreach my $f (qw
/file_a file_b/) {
2586 next unless defined $m->{$f};
2587 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
2588 if (length $pfx.$dir && ! defined $types{$dir}) {
2589 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
2597 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2601 my ($self, $path) = @_;
2602 $self->{path_prefix
}.(defined $path ?
$path : '');
2606 my ($self, $path) = @_;
2607 $self->{url
} . '/' . $self->repo_path($path);
2612 my $rm = $self->{rm
};
2613 delete $rm->{''}; # we never delete the url we're tracking
2616 foreach (keys %$rm) {
2617 my @d = split m
#/#, $_;
2621 $c .= '/' . shift @d;
2625 delete $rm->{$self->{svn_path
}};
2626 delete $rm->{''}; # we never delete the url we're tracking
2629 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
2634 my @dn = split m
#/#, $_;
2636 delete $rm->{join '/', @dn};
2643 command_close_pipe
($fh, $ctx);
2645 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2646 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2647 $self->close_directory($bat->{$d}, $p);
2648 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2649 print "\tD+\t$d/\n" unless $::_q
;
2650 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2655 sub open_or_add_dir
{
2656 my ($self, $full_path, $baton) = @_;
2657 my $t = $self->{types
}->{$full_path};
2659 die "$full_path not known in r$self->{r} or we have a bug!\n";
2661 if ($t == $SVN::Node
::none
) {
2662 return $self->add_directory($full_path, $baton,
2663 undef, -1, $self->{pool
});
2664 } elsif ($t == $SVN::Node
::dir
) {
2665 return $self->open_directory($full_path, $baton,
2666 $self->{r
}, $self->{pool
});
2668 print STDERR
"$full_path already exists in repository at ",
2669 "r$self->{r} and it is not a directory (",
2670 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2675 my ($self, $path) = @_;
2676 my $bat = $self->{bat
};
2677 my $repo_path = $self->repo_path($path);
2678 return $bat->{''} unless (length $repo_path);
2679 my @p = split m
#/+#, $repo_path;
2681 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2684 $c .= '/' . shift @p;
2685 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2691 my ($self, $m) = @_;
2692 my ($dir, $file) = split_path
($m->{file_b
});
2693 my $pbat = $self->ensure_path($dir);
2694 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2696 print "\tA\t$m->{file_b}\n" unless $::_q
;
2697 $self->chg_file($fbat, $m);
2698 $self->close_file($fbat,undef,$self->{pool
});
2702 my ($self, $m) = @_;
2703 my ($dir, $file) = split_path
($m->{file_b
});
2704 my $pbat = $self->ensure_path($dir);
2705 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2706 $self->url_path($m->{file_a
}), $self->{r
});
2707 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2708 $self->chg_file($fbat, $m);
2709 $self->close_file($fbat,undef,$self->{pool
});
2713 my ($self, $path, $pbat) = @_;
2714 my $rpath = $self->repo_path($path);
2715 my ($dir, $file) = split_path
($rpath);
2716 $self->{rm
}->{$dir} = 1;
2717 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2721 my ($self, $m) = @_;
2722 my ($dir, $file) = split_path
($m->{file_b
});
2723 my $pbat = $self->ensure_path($dir);
2724 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2725 $self->url_path($m->{file_a
}), $self->{r
});
2726 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2727 $self->chg_file($fbat, $m);
2728 $self->close_file($fbat,undef,$self->{pool
});
2730 ($dir, $file) = split_path
($m->{file_a
});
2731 $pbat = $self->ensure_path($dir);
2732 $self->delete_entry($m->{file_a
}, $pbat);
2736 my ($self, $m) = @_;
2737 my ($dir, $file) = split_path
($m->{file_b
});
2738 my $pbat = $self->ensure_path($dir);
2739 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2740 $pbat,$self->{r
},$self->{pool
});
2741 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
2742 $self->chg_file($fbat, $m);
2743 $self->close_file($fbat,undef,$self->{pool
});
2746 sub T
{ shift->M(@_) }
2748 sub change_file_prop
{
2749 my ($self, $fbat, $pname, $pval) = @_;
2750 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2754 my ($self, $fbat, $m) = @_;
2755 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2756 $self->change_file_prop($fbat,'svn:executable','*');
2757 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2758 $self->change_file_prop($fbat,'svn:executable',undef);
2760 my $fh = IO
::File
->new_tmpfile or croak
$!;
2761 if ($m->{mode_b
} =~ /^120/) {
2762 print $fh 'link ' or croak
$!;
2763 $self->change_file_prop($fbat,'svn:special','*');
2764 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2765 $self->change_file_prop($fbat,'svn:special',undef);
2767 defined(my $pid = fork) or croak
$!;
2769 open STDOUT
, '>&', $fh or croak
$!;
2770 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2774 $fh->flush == 0 or croak
$!;
2775 seek $fh, 0, 0 or croak
$!;
2777 my $md5 = Digest
::MD5
->new;
2778 $md5->addfile($fh) or croak
$!;
2779 seek $fh, 0, 0 or croak
$!;
2781 my $exp = $md5->hexdigest;
2782 my $pool = SVN
::Pool
->new;
2783 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2784 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2785 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2788 close $fh or croak
$!;
2792 my ($self, $m) = @_;
2793 my ($dir, $file) = split_path
($m->{file_b
});
2794 my $pbat = $self->ensure_path($dir);
2795 print "\tD\t$m->{file_b}\n" unless $::_q
;
2796 $self->delete_entry($m->{file_b
}, $pbat);
2801 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2802 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2803 $self->close_directory($bat->{$_}, $p);
2805 $self->SUPER::close_edit
($p);
2811 $self->SUPER::abort_edit
($self->{pool
});
2816 $self->SUPER::DESTROY
(@_);
2817 $self->{pool
}->clear;
2820 # this drives the editor
2823 my $mods = $self->{mods
};
2824 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2825 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
2827 if (defined $o{$f}) {
2830 fatal
("Invalid change type: $f\n");
2833 $self->rmdirs if $_rmdir;
2839 return scalar @
$mods;
2842 package Git
::SVN
::Ra
;
2843 use vars qw
/@ISA $config_dir $_log_window_size/;
2846 my ($can_do_switch, %ignored_err, $RA);
2849 # enforce temporary pool usage for some simple functions
2851 foreach (qw
/get_latest_revnum get_uuid get_repos_root/) {
2854 my \$pool = SVN::Pool->new;
2855 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2857 wantarray ? \@ret : \$ret[0]; }\n";
2860 # get_dir needs $pool held in cache for dirents to work,
2861 # check_path is cacheable and rev_proplist is close enough
2863 foreach (qw
/check_path get_dir rev_proplist/) {
2864 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2867 my \$k = join(\"\\0\", \@_);
2868 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2869 return wantarray ? \@\$x : \$x->[0];
2871 my \$pool = SVN::Pool->new;
2872 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2873 if (\$r != \$${_}_rev) {
2874 \%${_}_cache = ( pool => [] );
2877 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2878 push \@{\$${_}_cache{pool}}, \$pool;
2879 wantarray ? \@ret : \$ret[0]; }\n";
2886 my ($class, $url) = @_;
2888 return $RA if ($RA && $RA->{url
} eq $url);
2889 $RA->{pool
}->clear if $RA;
2891 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2892 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2893 SVN
::Client
::get_simple_provider
(),
2894 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2895 SVN
::Client
::get_simple_prompt_provider
(
2896 \
&Git
::SVN
::Prompt
::simple
, 2),
2897 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2898 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2899 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2900 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2901 SVN
::Client
::get_username_provider
(),
2902 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2903 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2904 SVN
::Client
::get_username_prompt_provider
(
2905 \
&Git
::SVN
::Prompt
::username
, 2),
2907 my $config = SVN
::Core
::config_get_config
($config_dir);
2908 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2910 pool
=> SVN
::Pool
->new,
2911 auth_provider_callbacks
=> $callbacks);
2912 $self->{svn_path
} = $url;
2913 $self->{repos_root
} = $self->get_repos_root;
2914 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
2915 $RA = bless $self, $class;
2919 # do not call the real DESTROY since we store ourselves in $RA
2923 my ($self, @args) = @_;
2924 my $pool = SVN
::Pool
->new;
2925 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2926 my $ret = $self->SUPER::get_log
(@args, $pool);
2931 sub get_commit_editor
{
2932 my ($self, $log, $cb, $pool) = @_;
2933 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2934 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2938 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2939 my $new = ($rev_a == $rev_b);
2940 my $path = $gs->{path
};
2942 if ($new && -e
$gs->{index}) {
2943 unlink $gs->{index} or die
2944 "Couldn't unlink index: $gs->{index}: $!\n";
2946 my $pool = SVN
::Pool
->new;
2947 $editor->set_path_strip($path);
2948 my (@pc) = split m
#/#, $path;
2949 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
2951 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2953 # Since we can't rely on svn_ra_reparent being available, we'll
2954 # just have to do some magic with set_path to make it so
2955 # we only want a partial path.
2957 my $final = join('/', @pc);
2959 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2960 $sp .= '/' if length $sp;
2963 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2965 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2967 $reporter->finish_report($pool);
2969 $editor->{git_commit_ok
};
2972 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2973 # svn_ra_reparent didn't work before 1.4)
2975 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2976 my $path = $gs->{path
};
2977 my $pool = SVN
::Pool
->new;
2979 my $full_url = $self->{url
};
2980 my $old_url = $full_url;
2981 $full_url .= "/$path" if length $path;
2982 my ($ra, $reparented);
2983 if ($old_url ne $full_url) {
2984 if ($old_url !~ m
#^svn(\+ssh)?://#) {
2985 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
2987 $self->{url
} = $full_url;
2990 $ra = Git
::SVN
::Ra
->new($full_url);
2994 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2995 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2996 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2997 $reporter->finish_report($pool);
3000 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
3001 $self->{url
} = $old_url;
3005 $editor->{git_commit_ok
};
3008 sub gs_fetch_loop_common
{
3009 my ($self, $base, $head, $gsv, $globs) = @_;
3010 return if ($base > $head);
3011 my $inc = $_log_window_size;
3012 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
3014 my $common_max = scalar @
$gsv;
3016 foreach my $gs (@
$gsv) {
3017 my @tmp = split m
#/#, $gs->{path};
3020 $p .= length($p) ?
"/$_" : $_;
3026 $common_max += scalar @
$globs;
3027 foreach my $glob (@
$globs) {
3028 my @tmp = split m
#/#, $glob->{path}->{left};
3031 $p .= length($p) ?
"/$_" : $_;
3037 my $longest_path = '';
3038 foreach (sort {length $b <=> length $a} keys %common) {
3039 if ($common{$_} == $common_max) {
3047 my $err_handler = $SVN::Error
::handler
;
3048 $SVN::Error
::handler
= sub {
3050 skip_unknown_revs
($err);
3053 my ($paths, $r, $author, $date, $log) = @_;
3054 [ dup_changed_paths
($paths),
3055 { author
=> $author, date
=> $date, log => $log } ];
3057 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3058 sub { $revs{$_[1]} = _cb
(@_) });
3059 if ($err && $max >= $head) {
3060 print STDERR
"Path '$longest_path' ",
3061 "was probably deleted:\n",
3062 $err->expanded_message,
3063 "\nWill attempt to follow ",
3064 "revisions r$min .. r$max ",
3065 "committed before the deletion\n";
3067 while (--$hi >= $min) {
3069 $self->get_log([$longest_path], $min, $hi,
3072 $revs{$_[1]} = _cb
(@_) });
3074 print STDERR
"r$min .. r$ok OK\n";
3079 $SVN::Error
::handler
= $err_handler;
3081 my %exists = map { $_->{path
} => $_ } @
$gsv;
3082 foreach my $r (sort {$a <=> $b} keys %revs) {
3083 my ($paths, $logged) = @
{$revs{$r}};
3085 foreach my $gs ($self->match_globs(\
%exists, $paths,
3087 if ($gs->rev_db_max >= $r) {
3090 next unless $gs->match_paths($paths, $r);
3091 $gs->{logged_rev_props
} = $logged;
3092 if (my $last_commit = $gs->last_commit) {
3093 $gs->assert_index_clean($last_commit);
3095 my $log_entry = $gs->do_fetch($paths, $r);
3097 $gs->do_git_commit($log_entry);
3100 foreach my $g (@
$globs) {
3101 my $k = "svn-remote.$g->{remote}." .
3103 Git
::SVN
::tmp_config
($k, $r);
3106 # pre-fill the .rev_db since it'll eventually get filled in
3107 # with '0' x40 if something new gets committed
3108 foreach my $gs (@
$gsv) {
3109 next if defined $gs->rev_db_get($max);
3110 $gs->rev_db_set($max, 0 x40
);
3112 foreach my $g (@
$globs) {
3113 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3114 Git
::SVN
::tmp_config
($k, $max);
3116 last if $max >= $head;
3119 $max = $head if ($max > $head);
3124 my ($self, $exists, $paths, $globs, $r) = @_;
3127 my ($self, $exists, $g, $r) = @_;
3128 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
3129 return unless scalar @x == 3;
3130 my $dirents = $x[0];
3131 foreach my $de (keys %$dirents) {
3132 next if $dirents->{$de}->kind != $SVN::Node
::dir
;
3133 my $p = $g->{path
}->full_path($de);
3134 next if $exists->{$p};
3135 next if (length $g->{path
}->{right
} &&
3136 ($self->check_path($p, $r) !=
3138 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
3139 $g->{ref}->full_path($de), 1);
3142 foreach my $g (@
$globs) {
3143 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3144 if ($path->{action
} =~ /^[AR]$/) {
3145 get_dir_check
($self, $exists, $g, $r);
3148 foreach (keys %$paths) {
3149 if (/$g->{path}->{left_regex}/ &&
3150 !/$g->{path}->{regex}/) {
3151 next if $paths->{$_}->{action
} !~ /^[AR]$/;
3152 get_dir_check
($self, $exists, $g, $r);
3154 next unless /$g->{path}->{regex}/;
3156 my $pathname = $g->{path
}->full_path($p);
3157 next if $exists->{$pathname};
3158 $exists->{$pathname} = Git
::SVN
->init(
3159 $self->{url
}, $pathname, undef,
3160 $g->{ref}->full_path($p), 1);
3163 foreach (split m
#/#, $g->{path}->{left}) {
3165 next unless ($paths->{$c} &&
3166 ($paths->{$c}->{action
} =~ /^[AR]$/));
3167 get_dir_check
($self, $exists, $g, $r);
3175 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
3176 my $url = $self->{repos_root
};
3177 my @components = split(m!/!, $self->{svn_path
});
3180 $url .= "/$c" if length $c;
3181 eval { (ref $self)->new($url)->get_latest_revnum };
3182 } while ($@
&& ($c = shift @components));
3188 unless (defined $can_do_switch) {
3189 my $pool = SVN
::Pool
->new;
3191 $self->do_switch(1, '', 0, $self->{url
},
3192 SVN
::Delta
::Editor
->new, $pool);
3197 $rep->abort_report($pool);
3205 sub skip_unknown_revs
{
3207 my $errno = $err->apr_err();
3208 # Maybe the branch we're tracking didn't
3209 # exist when the repo started, so it's
3210 # not an error if it doesn't, just continue
3212 # Wonderfully consistent library, eh?
3213 # 160013 - svn:// and file://
3214 # 175002 - http(s)://
3215 # 175007 - http(s):// (this repo required authorization, too...)
3216 # More codes may be discovered later...
3217 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3218 my $err_key = $err->expanded_message;
3219 # revision numbers change every time, filter them out
3220 $err_key =~ s/\d+/\0/g;
3221 $err_key = "$errno\0$err_key";
3222 unless ($ignored_err{$err_key}) {
3223 warn "W: Ignoring error from SVN, path probably ",
3224 "does not exist: ($errno): ",
3225 $err->expanded_message,"\n";
3226 $ignored_err{$err_key} = 1;
3230 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3233 # svn_log_changed_path_t objects passed to get_log are likely to be
3234 # overwritten even if only the refs are copied to an external variable,
3235 # so we should dup the structures in their entirety. Using an externally
3236 # passed pool (instead of our temporary and quickly cleared pool in
3237 # Git::SVN::Ra) does not help matters at all...
3238 sub dup_changed_paths
{
3240 return undef unless $paths;
3242 foreach my $p (keys %$paths) {
3243 my $i = $paths->{$p};
3244 my %s = map { $_ => $i->$_ }
3245 qw
/copyfrom_path copyfrom_rev action/;
3251 package Git
::SVN
::Log
;
3254 use POSIX qw
/strftime/;
3255 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
3256 %rusers $show_commit $incremental/;
3261 return 1 if defined $c->{r
};
3262 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
3263 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
3264 my @log = command
(qw
/cat-file commit/, $c->{c
});
3265 shift @log while ($log[0] ne "\n");
3267 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
3269 (undef, $c->{r
}, undef) = ::extract_metadata
(
3270 (grep(/^git-svn-id: /, @log))[-1]);
3272 return defined $c->{r
};
3278 $dcvar = 'color.diff';
3279 $dc = `git-config --get $dcvar`;
3281 # nothing at all; fallback to "diff.color"
3282 $dcvar = 'diff.color';
3283 $dc = `git-config --get $dcvar`;
3286 if ($dc eq 'auto') {
3288 $pc = `git-config --get color.pager`;
3290 # does not have it -- fallback to pager.color
3291 $pc = `git-config --bool --get pager.color`;
3294 $pc = `git-config --bool --get color.pager`;
3300 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
3301 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
3305 return 0 if $dc eq 'never';
3306 return 1 if $dc eq 'always';
3307 chomp($dc = `git-config --bool --get $dcvar`);
3308 return ($dc eq 'true');
3311 sub git_svn_log_cmd
{
3312 my ($r_min, $r_max, @args) = @_;
3314 foreach my $x (@args) {
3316 next unless ::verify_ref
("$x^0");
3321 my $url = (::working_head_info
($head))[0];
3322 my $gs = Git
::SVN
->find_by_url($url) || Git
::SVN
->_new;
3323 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
3325 push @cmd, '-r' unless $non_recursive;
3326 push @cmd, qw
/--raw --name-status/ if $verbose;
3327 push @cmd, '--color' if log_use_color
();
3328 return @cmd unless defined $r_max;
3329 if ($r_max == $r_min) {
3330 push @cmd, '--max-count=1';
3331 if (my $c = $gs->rev_db_get($r_max)) {
3335 my ($c_min, $c_max);
3336 $c_max = $gs->rev_db_get($r_max);
3337 $c_min = $gs->rev_db_get($r_min);
3338 if (defined $c_min && defined $c_max) {
3339 if ($r_max > $r_max) {
3340 push @cmd, "$c_min..$c_max";
3342 push @cmd, "$c_max..$c_min";
3344 } elsif ($r_max > $r_min) {
3353 # adapted from pager.c
3355 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
3356 if (!defined $pager) {
3358 } elsif (length $pager == 0 || $pager eq 'cat') {
3364 return unless -t
*STDOUT
;
3365 pipe my $rfd, my $wfd or return;
3366 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
3368 open STDOUT
, '>&', $wfd or
3369 ::fatal
"Can't redirect to stdout: $!\n";
3372 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
3373 $ENV{LESS
} ||= 'FRSX';
3374 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
3377 sub tz_to_s_offset
{
3380 return ($1 * 60) + ($tz * 3600);
3383 sub get_author_info
{
3384 my ($dest, $author, $t, $tz) = @_;
3385 $author =~ s/(?:^\s*|\s*$)//g;
3386 $dest->{a_raw
} = $author;
3389 $au = $rusers{$author} || undef;
3392 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3397 # Date::Parse isn't in the standard Perl distro :(
3398 if ($tz =~ s/^\+//) {
3399 $t += tz_to_s_offset
($tz);
3400 } elsif ($tz =~ s/^\-//) {
3401 $t -= tz_to_s_offset
($tz);
3403 $dest->{t_utc
} = $t;
3406 sub process_commit
{
3407 my ($c, $r_min, $r_max, $defer) = @_;
3408 if (defined $r_min && defined $r_max) {
3409 if ($r_min == $c->{r
} && $r_min == $r_max) {
3413 return 1 if $r_min == $r_max;
3414 if ($r_min < $r_max) {
3415 # we need to reverse the print order
3416 return 0 if (defined $limit && --$limit < 0);
3420 if ($r_min != $r_max) {
3421 return 1 if ($r_min < $c->{r
});
3422 return 1 if ($r_max > $c->{r
});
3425 return 0 if (defined $limit && --$limit < 0);
3434 if (my $l = $c->{l
}) {
3435 while ($l->[0] =~ /^\s*$/) { shift @
$l }
3438 $l_fmt ||= 'A' . length($c->{r
});
3439 print 'r',pack($l_fmt, $c->{r
}),' | ';
3440 print "$c->{c} | " if $show_commit;
3443 show_commit_normal
($c);
3447 sub show_commit_changed_paths
{
3449 return unless $c->{changed
};
3450 print "Changed paths:\n", @
{$c->{changed
}};
3453 sub show_commit_normal
{
3455 print '-' x72
, "\nr$c->{r} | ";
3456 print "$c->{c} | " if $show_commit;
3457 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3458 localtime($c->{t_utc
})), ' | ';
3461 if (my $l = $c->{l
}) {
3462 while ($l->[$#$l] eq "\n" && $#$l > 0
3463 && $l->[($#$l - 1)] eq "\n") {
3466 $nr_line = scalar @
$l;
3468 print "1 line\n\n\n";
3470 if ($nr_line == 1) {
3471 $nr_line = '1 line';
3473 $nr_line .= ' lines';
3475 print $nr_line, "\n";
3476 show_commit_changed_paths
($c);
3478 print $_ foreach @
$l;
3482 show_commit_changed_paths
($c);
3486 foreach my $x (qw
/raw stat diff/) {
3489 print $_ foreach @
{$c->{$x}}
3496 my ($r_min, $r_max);
3497 my $r_last = -1; # prevent dupes
3503 if (defined $::_revision
) {
3504 if ($::_revision
=~ /^(\d+):(\d+)$/) {
3505 ($r_min, $r_max) = ($1, $2);
3506 } elsif ($::_revision
=~ /^\d+$/) {
3507 $r_min = $r_max = $::_revision
;
3509 ::fatal
"-r$::_revision is not supported, use ",
3510 "standard \'git log\' arguments instead\n";
3515 @args = (git_svn_log_cmd
($r_min, $r_max, @args), @args);
3516 my $log = command_output_pipe
(@args);
3518 my (@k, $c, $d, $stat);
3519 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3521 if (/^${esc_color}commit ($::sha1_short)/o) {
3523 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
3525 process_commit
($c, $r_min, $r_max, \
@k) or
3530 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3531 get_author_info
($c, $1, $2, $3);
3532 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3534 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3535 push @
{$c->{raw
}}, $_;
3536 } elsif (/^${esc_color}[ACRMDT]\t/) {
3537 # we could add $SVN->{svn_path} here, but that requires
3538 # remote access at the moment (repo_path_split)...
3539 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3540 push @
{$c->{changed
}}, $_;
3541 } elsif (/^${esc_color}diff /o) {
3543 push @
{$c->{diff
}}, $_;
3545 push @
{$c->{diff
}}, $_;
3546 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
3547 $esc_color*[\
+\
-]*$esc_color$/x
) {
3549 push @
{$c->{stat}}, $_;
3550 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3551 push @
{$c->{stat}}, $_;
3553 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3554 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
3555 } elsif (s/^${esc_color} //o) {
3556 push @
{$c->{l
}}, $_;
3559 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
3561 process_commit
($c, $r_min, $r_max, \
@k);
3567 process_commit
($_, $r_min, $r_max) foreach reverse @k;
3571 print '-' x72
,"\n" unless $incremental || $oneline;
3574 package Git
::SVN
::Migration
;
3575 # these version numbers do NOT correspond to actual version numbers
3576 # of git nor git-svn. They are just relative.
3578 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3580 # v1 layout: .git/$id/info/url, refs/remotes/$id
3582 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3584 # v3 layout: .git/svn/$id, refs/remotes/$id
3585 # - info/url may remain for backwards compatibility
3586 # - this is what we migrate up to this layout automatically,
3587 # - this will be used by git svn init on single branches
3588 # v3.1 layout (auto migrated):
3589 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3590 # for backwards compatibility
3592 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3593 # - this is only created for newly multi-init-ed
3594 # repositories. Similar in spirit to the
3595 # --use-separate-remotes option in git-clone (now default)
3596 # - we do not automatically migrate to this (following
3597 # the example set by core git)
3601 use File
::Path qw
/mkpath/;
3602 use File
::Basename qw
/dirname basename/;
3603 use vars qw
/$_minimize/;
3605 sub migrate_from_v0
{
3606 my $git_dir = $ENV{GIT_DIR
};
3607 return undef unless -d
$git_dir;
3608 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3612 my ($id, $orig_ref) = ($_, $_);
3613 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
3614 next unless -f
"$git_dir/$id/info/url";
3615 my $new_ref = "refs/remotes/$id";
3616 if (::verify_ref
("$new_ref^0")) {
3617 print STDERR
"W: $orig_ref is probably an old ",
3618 "branch used by an ancient version of ",
3620 "However, $new_ref also exists.\n",
3621 "We will not be able ",
3622 "to use this branch until this ",
3623 "ambiguity is resolved.\n";
3626 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
3627 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
3628 command_noisy
('update-ref', $new_ref, $orig_ref);
3629 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
3632 command_close_pipe
($fh, $ctx);
3633 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
3637 sub migrate_from_v1
{
3638 my $git_dir = $ENV{GIT_DIR
};
3640 return $migrated unless -d
$git_dir;
3641 my $svn_dir = "$git_dir/svn";
3643 # just in case somebody used 'svn' as their $id at some point...
3644 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
3646 print STDERR
"Migrating from a git-svn v1 layout...\n";
3648 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
3649 "$svn_dir\n\t(required for this version ",
3650 "($::VERSION) of git-svn) does not. exist\n";
3651 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3654 next unless $x =~ s
#^refs/remotes/##;
3656 next unless -f
"$git_dir/$x/info/url";
3657 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
3659 my $dn = dirname
("$git_dir/svn/$x");
3660 mkpath
([$dn]) unless -d
$dn;
3661 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3662 mkpath
(["$git_dir/svn/svn"]);
3663 print STDERR
" - $git_dir/$x/info => ",
3664 "$git_dir/svn/$x/info\n";
3665 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3667 # don't worry too much about these, they probably
3668 # don't exist with repos this old (save for index,
3669 # and we can easily regenerate that)
3670 foreach my $f (qw
/unhandled.log index .rev_db/) {
3671 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3674 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
3675 rename "$git_dir/$x", "$git_dir/svn/$x" or
3680 command_close_pipe
($fh, $ctx);
3681 print STDERR
"Done migrating from a git-svn v1 layout\n";
3686 my ($l_map, $pfx, $path) = @_;
3688 foreach (<$path/*>) {
3689 if (-r
"$_/info/url") {
3690 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
3691 my $ref_id = $pfx . basename
$_;
3692 my $url = ::file_to_s
("$_/info/url");
3693 $l_map->{$ref_id} = $url;
3700 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3701 read_old_urls
($l_map, $x, $_);
3705 sub migrate_from_v2
{
3706 my @cfg = command
(qw
/config -l/);
3707 return if grep /^svn-remote\..+\.url=/, @cfg;
3709 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
3712 foreach my $ref_id (sort keys %l_map) {
3713 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
3715 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3722 sub minimize_connections
{
3723 my $r = Git
::SVN
::read_all_remotes
();
3725 my $root_repos = {};
3726 foreach my $repo_id (keys %$r) {
3727 my $url = $r->{$repo_id}->{url
} or next;
3728 my $fetch = $r->{$repo_id}->{fetch
} or next;
3729 my $ra = Git
::SVN
::Ra
->new($url);
3731 # skip existing cases where we already connect to the root
3732 if (($ra->{url
} eq $ra->{repos_root
}) ||
3733 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
3735 $root_repos->{$ra->{url
}} = $repo_id;
3739 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
3740 my $root_path = $ra->{url
};
3741 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
3742 foreach my $path (keys %$fetch) {
3743 my $ref_id = $fetch->{$path};
3744 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
3746 # make sure we can read when connecting to
3747 # a higher level of a repository
3748 my ($last_rev, undef) = $gs->last_rev_commit;
3749 if (!defined $last_rev) {
3751 $root_ra->get_latest_revnum;
3755 my $new = $root_path;
3756 $new .= length $path ?
"/$path" : '';
3758 $root_ra->get_log([$new], $last_rev, $last_rev,
3762 $new_urls->{$ra->{repos_root
}}->{$new} =
3763 { ref_id
=> $ref_id,
3764 old_repo_id
=> $repo_id,
3765 old_path
=> $path };
3770 foreach my $url (keys %$new_urls) {
3771 # see if we can re-use an existing [svn-remote "repo_id"]
3772 # instead of creating a(n ugly) new section:
3773 my $repo_id = $root_repos->{$url} ||
3774 Git
::SVN
::sanitize_remote_name
($url);
3776 my $fetch = $new_urls->{$url};
3777 foreach my $path (keys %$fetch) {
3778 my $x = $fetch->{$path};
3779 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
3780 my $pfx = "svn-remote.$x->{old_repo_id}";
3782 my $old_fetch = quotemeta("$x->{old_path}:".
3783 "refs/remotes/$x->{ref_id}");
3784 command_noisy
(qw
/config --unset/,
3785 "$pfx.fetch", '^'. $old_fetch . '$');
3786 delete $r->{$x->{old_repo_id
}}->
3787 {fetch
}->{$x->{old_path
}};
3788 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
3789 command_noisy
(qw
/config --unset/,
3791 push @emptied, $x->{old_repo_id
}
3796 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
3797 "$ENV{GIT_DIR}/config";
3799 The following [svn-remote] sections in your config file ($file) are empty
3800 and can be safely removed:
3802 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
3806 sub migration_check
{
3810 minimize_connections
() if $_minimize;
3813 package Git
::IndexInfo
;
3816 use Git qw
/command_input_pipe command_close_pipe/;
3820 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
3821 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
3825 my ($self, $path) = @_;
3826 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
3827 return ++$self->{nr
};
3833 my ($self, $mode, $hash, $path) = @_;
3834 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
3835 return ++$self->{nr
};
3842 command_close_pipe
($self->{gui
}, $self->{ctx
});
3845 package Git
::SVN
::GlobSpec
;
3850 my ($class, $glob) = @_;
3852 $re =~ s!/+$!!g; # no need for trailing slashes
3853 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3854 my ($left, $right) = ($1, $2);
3856 die "Only one '*' wildcard expansion ",
3857 "is supported (got $nr): '$glob'\n";
3858 } elsif ($nr == 0) {
3859 die "One '*' is needed for glob: '$glob'\n";
3861 $re = quotemeta($left) . $re . quotemeta($right);
3862 if (length $left && !($left =~ s!/+$!!g)) {
3863 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3865 if (length $right && !($right =~ s!^/+!!g)) {
3866 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3868 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
3869 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
3870 regex
=> qr/$re/, glob => $glob }, $class;
3874 my ($self, $path) = @_;
3875 return (length $self->{left
} ?
"$self->{left}/" : '') .
3876 $path . (length $self->{right
} ?
"/$self->{right}" : '');
3884 $remotes = { # returned by read_all_remotes()
3886 # svn-remote.svn.url=https://svn.musicpd.org
3887 url
=> 'https://svn.musicpd.org',
3888 # svn-remote.svn.fetch=mpd/trunk:trunk
3890 'mpd/trunk' => 'trunk',
3892 # svn-remote.svn.tags=mpd/tags/*:tags/*
3897 regex
=> qr!mpd/tags/([^/]+)$!,
3903 regex
=> qr!tags/([^/]+)$!,
3910 $log_entry hashref as returned by libsvn_log_entry
()
3912 log => 'whitespace-formatted log entry
3913 ', # trailing newline is preserved
3914 revision
=> '8', # integer
3915 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3916 author
=> 'committer name'
3920 # this is generated by generate_diff();
3921 @mods = array of diff
-index line hashes
, each element represents one line
3922 of diff
-index output
3924 diff
-index line
($m hash
)
3926 mode_a
=> first column of diff
-index output
, no leading
':',
3927 mode_b
=> second column of diff
-index output
,
3928 sha1_b
=> sha1sum of the final blob
,
3929 chg
=> change type
[MCRADT
],
3930 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3931 file_b
=> new
/current file name of a file
(any chg
)
3935 # retval of read_url_paths{,_all}();
3937 # repository root url
3938 'https://svn.musicpd.org' => {
3939 # repository path # GIT_SVN_ID
3940 'mpd/trunk' => 'trunk',
3941 'mpd/tags/0.11.5' => 'tags/0.11.5',
3946 I don
't trust the each() function on unless I created %hash myself
3947 because the internal iterator may not have started at base.