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);
438 if (command(qw/diff-index HEAD --/)) {
439 print STDERR "Cannot rebase with uncommited changes
:\n";
440 command_noisy('status');
444 $_fetch_all ? $gs->fetch_all : $gs->fetch;
446 command_noisy(rebase_cmd(), $gs->refname);
449 sub cmd_show_ignore {
450 my $url = (::working_head_info('HEAD'))[0];
451 my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
452 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
453 $gs->traverse_ignore(\*STDOUT, $gs->{path}, $r);
458 unless (defined $_trunk || defined $_branches || defined $_tags) {
461 $_prefix = '' unless defined $_prefix;
467 if (defined $_trunk) {
468 my $trunk_ref = $_prefix . 'trunk';
469 # try both old-style and new-style lookups:
470 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
472 my ($trunk_url, $trunk_path) =
473 complete_svn_url($url, $_trunk);
474 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
478 return unless defined $_branches || defined $_tags;
479 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
480 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
481 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
484 sub cmd_multi_fetch {
485 my $remotes = Git::SVN::read_all_remotes();
486 foreach my $repo_id (sort keys %$remotes) {
487 if ($remotes->{$repo_id}->{url}) {
488 Git::SVN::fetch_all($repo_id, $remotes);
493 # this command is special because it requires no metadata
494 sub cmd_commit_diff {
495 my ($ta, $tb, $url) = @_;
496 my $usage = "Usage
: $0 commit
-diff
-r
<revision
> ".
497 "<tree
-ish
> <tree
-ish
> [<URL
>]\n";
498 fatal($usage) if (!defined $ta || !defined $tb);
501 my $gs = eval { Git::SVN->new };
503 fatal("Needed URL
or usable git
-svn
--id
in ",
504 "the command
-line
\n", $usage);
507 $svn_path = $gs->{path};
509 unless (defined $_revision) {
510 fatal("-r
|--revision is a required argument
\n", $usage);
512 if (defined $_message && defined $_file) {
513 fatal("Both
--message
/-m and --file/-F specified
",
514 "for the commit message
.\n",
515 "I have
no idea what you mean
\n");
517 if (defined $_file) {
518 $_message = file_to_s($_file);
520 $_message ||= get_commit_entry($tb)->{log};
522 my $ra ||= Git::SVN::Ra->new($url);
523 $svn_path ||= $ra->{svn_path};
526 $r = $ra->get_latest_revnum;
527 } elsif ($r !~ /^\d+$/) {
528 die "revision argument
: $r not understood by git
-svn
\n";
530 my %ed_opts = ( r => $r,
535 editor_cb => sub { print "Committed r
$_[0]\n" },
536 svn_path => $svn_path );
537 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
538 print "No changes
\n$ta == $tb\n";
542 ########################### utility functions #########################
545 my @cmd = qw/rebase/;
546 push @cmd, '-v' if $_verbose;
547 push @cmd, qw/--merge/ if $_merge;
548 push @cmd, "--strategy
=$_strategy" if $_strategy;
552 sub post_fetch_checkout {
553 return if $_no_checkout;
554 my $gs = $Git::SVN::_head or return;
555 return if verify_ref('refs/heads/master^0');
557 my $valid_head = verify_ref('HEAD^0');
558 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
559 return if ($valid_head || !verify_ref
('HEAD^0'));
561 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
562 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
565 chomp(my $bare = `git config --bool --get core.bare`);
566 return if $bare eq 'true';
567 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
568 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
569 print STDERR
"Checked out HEAD:\n ",
570 $gs->full_url, " r", $gs->last_rev, "\n";
573 sub complete_svn_url
{
574 my ($url, $path) = @_;
576 if ($path !~ m
#^[a-z\+]+://#) {
577 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
578 fatal
("E: '$path' is not a complete URL ",
579 "and a separate URL is not specified\n");
581 return ($url, $path);
586 sub complete_url_ls_init
{
587 my ($ra, $repo_path, $switch, $pfx) = @_;
588 unless ($repo_path) {
589 print STDERR
"W: $switch not specified\n";
592 $repo_path =~ s
#/+$##;
593 if ($repo_path =~ m
#^[a-z\+]+://#) {
594 $ra = Git
::SVN
::Ra
->new($repo_path);
597 $repo_path =~ s
#^/+##;
599 fatal
("E: '$repo_path' is not a complete URL ",
600 "and a separate URL is not specified\n");
603 my $url = $ra->{url
};
604 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
605 my $k = "svn-remote.$gs->{repo_id}.url";
606 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
607 if ($orig_url && ($orig_url ne $gs->{url
})) {
608 die "$k already set: $orig_url\n",
609 "wanted to set to: $gs->{url}\n";
611 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
612 my $remote_path = "$ra->{svn_path}/$repo_path/*";
613 $remote_path =~ s
#/+#/#g;
614 $remote_path =~ s
#^/##g;
615 my ($n) = ($switch =~ /^--(\w+)/);
616 if (length $pfx && $pfx !~ m
#/$#) {
617 die "--prefix='$pfx' must have a trailing slash '/'\n";
619 command_noisy
('config', "svn-remote.$gs->{repo_id}.$n",
620 "$remote_path:refs/remotes/$pfx*");
625 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
629 sub get_tree_from_treeish
{
631 # $treeish can be a symbolic ref, too:
632 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
634 while ($type eq 'tag') {
635 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
637 if ($type eq 'commit') {
638 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
640 ($expected) = ($expected =~ /^tree ($sha1)$/o);
641 die "Unable to get tree from $treeish\n" unless $expected;
642 } elsif ($type eq 'tree') {
643 $expected = $treeish;
645 die "$treeish is a $type, expected tree, tag or commit\n";
650 sub get_commit_entry
{
651 my ($treeish) = shift;
652 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
653 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
654 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
655 open my $log_fh, '>', $commit_editmsg or croak
$!;
657 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
658 if ($type eq 'commit' || $type eq 'tag') {
659 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
664 $in_msg = 1 if (/^\s*$/);
665 } elsif (/^git-svn-id: /) {
666 # skip this for now, we regenerate the
667 # correct one on re-fetch anyways
668 # TODO: set *:merge properties or like...
670 print $log_fh $_ or croak
$!;
673 command_close_pipe
($msg_fh, $ctx);
675 close $log_fh or croak
$!;
677 if ($_edit || ($type eq 'tree')) {
678 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
679 # TODO: strip out spaces, comments, like git-commit.sh
680 system($editor, $commit_editmsg);
682 rename $commit_editmsg, $commit_msg or croak
$!;
683 open $log_fh, '<', $commit_msg or croak
$!;
684 { local $/; chomp($log_entry{log} = <$log_fh>); }
685 close $log_fh or croak
$!;
691 my ($str, $file, $mode) = @_;
692 open my $fd,'>',$file or croak
$!;
693 print $fd $str,"\n" or croak
$!;
694 close $fd or croak
$!;
695 chmod ($mode &~ umask, $file) if (defined $mode);
700 open my $fd,'<',$file or croak
"$!: file: $file\n";
703 close $fd or croak
$!;
708 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
710 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
711 my $log = $cmd eq 'log';
714 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
715 my ($user, $name, $email) = ($1, $2, $3);
717 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
719 $users{$user} = [$name, $email];
722 close $authors or croak
$!;
725 # convert GetOpt::Long specs for use by git-config
726 sub read_repo_config
{
727 return unless -d
$ENV{GIT_DIR
};
730 foreach my $o (keys %$opts) {
731 # if we have mixedCase and a long option-only, then
732 # it's a config-only variable that we don't need for
734 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
736 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
738 my $arg = 'git-config';
739 $arg .= ' --int' if ($o =~ /[:=]i$/);
740 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
741 if (ref $v eq 'ARRAY') {
742 chomp(my @tmp = `$arg --get-all svn.$key`);
745 chomp(my $tmp = `$arg --get svn.$key`);
746 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
751 delete @
$opts{@config_only} if @config_only;
754 sub extract_metadata
{
755 my $id = shift or return (undef, undef, undef);
756 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
758 if (!defined $rev || !$uuid || !$url) {
759 # some of the original repositories I made had
760 # identifiers like this:
761 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
763 return ($url, $rev, $uuid);
767 return extract_metadata
((grep(/^git-svn-id: /,
768 command
(qw
/cat-file commit/, shift)))[-1]);
771 sub working_head_info
{
772 my ($head, $refs) = @_;
773 my ($url, $rev, $uuid);
774 my ($fh, $ctx) = command_output_pipe
('rev-list', $head);
777 ($url, $rev, $uuid) = cmt_metadata
($_);
778 last if (defined $url && defined $rev && defined $uuid);
779 unshift @
$refs, $_ if $refs;
781 close $fh; # break the pipe
788 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
789 $_repack $_repack_flags $_use_svm_props $_head
790 $_use_svnsync_props $no_reuse_existing/;
792 use File
::Path qw
/mkpath/;
793 use File
::Copy qw
/copy/;
797 # properties that we do not log:
800 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
801 svn
:special svn
:executable
802 svn
:entry
:committed
-rev
803 svn
:entry
:last-author
805 svn
:entry
:committed
-date
/;
807 # some options are read globally, but can be overridden locally
808 # per [svn-remote "..."] section. Command-line options will *NOT*
809 # override options set in an [svn-remote "..."] section
811 foreach (qw
/follow_parent no_metadata use_svm_props
812 use_svnsync_props
/) {
817 return \$self->{-$_} if exists \$self->{-$_};
818 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
819 eval { command_oneline(qw/config --get/, \$k) };
821 \$self->{-$_} = \$Git::SVN::_$_;
823 my \$v = command_oneline(qw/config --bool/,\$k);
824 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
826 return \$self->{-$_} }\n";
833 END { unlink keys %LOCKFILES if %LOCKFILES }
835 sub resolve_local_globs
{
836 my ($url, $fetch, $glob_spec) = @_;
837 return unless defined $glob_spec;
838 my $ref = $glob_spec->{ref};
839 my $path = $glob_spec->{path
};
840 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
841 next unless m
#^refs/remotes/$ref->{regex}$#;
843 my $pathname = $path->full_path($p);
844 my $refname = $ref->full_path($p);
845 if (my $existing = $fetch->{$pathname}) {
846 if ($existing ne $refname) {
847 die "Refspec conflict:\n",
848 "existing: refs/remotes/$existing\n",
849 " globbed: refs/remotes/$refname\n";
851 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
852 $u =~ s!^\Q$url\E(/|$)!! or die
853 "refs/remotes/$refname: '$url' not found in '$u'\n";
854 if ($pathname ne $u) {
855 warn "W: Refspec glob conflict ",
856 "(ref: refs/remotes/$refname):\n",
857 "expected path: $pathname\n",
859 "Continuing ahead with $u\n";
863 $fetch->{$pathname} = $refname;
868 sub parse_revision_argument
{
869 my ($base, $head) = @_;
870 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
871 return ($base, $head);
873 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
874 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
875 return ($head, $head) if ($::_revision
eq 'HEAD');
876 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
877 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
878 die "revision argument: $::_revision not understood by git-svn\n";
882 my ($repo_id, $remotes) = @_;
886 $repo_id = $gs->{repo_id
};
888 $remotes ||= read_all_remotes
();
889 my $remote = $remotes->{$repo_id} or
890 die "[svn-remote \"$repo_id\"] unknown\n";
891 my $fetch = $remote->{fetch
};
892 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
894 my $ra = Git
::SVN
::Ra
->new($url);
895 my $uuid = $ra->get_uuid;
896 my $head = $ra->get_latest_revnum;
897 my $base = defined $fetch ?
$head : 0;
899 # read the max revs for wildcard expansion (branches/*, tags/*)
900 foreach my $t (qw
/branches tags/) {
901 defined $remote->{$t} or next;
902 push @globs, $remote->{$t};
903 my $max_rev = eval { tmp_config
(qw
/--int --get/,
904 "svn-remote.$repo_id.${t}-maxRev") };
905 if (defined $max_rev && ($max_rev < $base)) {
907 } elsif (!defined $max_rev) {
913 foreach my $p (sort keys %$fetch) {
914 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
915 my $lr = $gs->rev_db_max;
917 $base = $lr if ($lr < $base);
923 ($base, $head) = parse_revision_argument
($base, $head);
924 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
927 sub read_all_remotes
{
929 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
930 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
931 $r->{$1}->{fetch
}->{$2} = $3;
932 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
933 $r->{$1}->{url
} = $2;
934 } elsif (m
!^(.+)\
.(branches
|tags
)=
935 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
936 my ($p, $g) = ($3, $4);
937 my $rs = $r->{$1}->{$2} = {
940 path
=> Git
::SVN
::GlobSpec
->new($p),
941 ref => Git
::SVN
::GlobSpec
->new($g) };
942 if (length($rs->{ref}->{right
}) != 0) {
943 die "The '*' glob character must be the last ",
944 "character of '$g'\n";
952 if (defined $_repack) {
953 $_repack = 1000 if ($_repack <= 0);
954 $_repack_nr = $_repack;
955 $_repack_flags ||= '-d';
959 sub verify_remotes_sanity
{
960 return unless -d
$ENV{GIT_DIR
};
962 foreach (command
(qw
/config -l/)) {
963 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
965 die "Remote ref refs/remote/$1 is tracked by",
966 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
967 "Please resolve this ambiguity in ",
968 "your git configuration file before ",
976 # we allow more chars than remotes2config.sh...
977 sub sanitize_remote_name
{
979 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
983 sub find_existing_remote
{
984 my ($url, $remotes) = @_;
985 return undef if $no_reuse_existing;
987 foreach my $repo_id (keys %$remotes) {
988 my $u = $remotes->{$repo_id}->{url
} or next;
990 $existing = $repo_id;
996 sub init_remote_config
{
997 my ($self, $url, $no_write) = @_;
998 $url =~ s!/+$!!; # strip trailing slash
999 my $r = read_all_remotes
();
1000 my $existing = find_existing_remote
($url, $r);
1002 unless ($no_write) {
1003 print STDERR
"Using existing ",
1004 "[svn-remote \"$existing\"]\n";
1006 $self->{repo_id
} = $existing;
1008 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1009 $existing = find_existing_remote
($min_url, $r);
1011 unless ($no_write) {
1012 print STDERR
"Using existing ",
1013 "[svn-remote \"$existing\"]\n";
1015 $self->{repo_id
} = $existing;
1017 if ($min_url ne $url) {
1018 unless ($no_write) {
1019 print STDERR
"Using higher level of URL: ",
1020 "$url => $min_url\n";
1022 my $old_path = $self->{path
};
1023 $self->{path
} = $url;
1024 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1025 if (length $old_path) {
1026 $self->{path
} .= "/$old_path";
1033 # verify that we aren't overwriting anything:
1035 command_oneline
('config', '--get',
1036 "svn-remote.$self->{repo_id}.url")
1038 if ($orig_url && ($orig_url ne $url)) {
1039 die "svn-remote.$self->{repo_id}.url already set: ",
1040 "$orig_url\nwanted to set to: $url\n";
1043 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1044 if (defined $xpath) {
1045 die "svn-remote.$xrepo_id.fetch already set to track ",
1046 "$xpath:refs/remotes/", $self->refname, "\n";
1048 unless ($no_write) {
1049 command_noisy
('config',
1050 "svn-remote.$self->{repo_id}.url", $url);
1051 command_noisy
('config', '--add',
1052 "svn-remote.$self->{repo_id}.fetch",
1053 "$self->{path}:".$self->refname);
1055 $self->{url
} = $url;
1058 sub find_by_url
{ # repos_root and, path are optional
1059 my ($class, $full_url, $repos_root, $path) = @_;
1060 return undef unless defined $full_url;
1061 my $remotes = read_all_remotes
();
1062 if (defined $full_url && defined $repos_root && !defined $path) {
1064 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1066 foreach my $repo_id (keys %$remotes) {
1067 my $u = $remotes->{$repo_id}->{url
} or next;
1068 next if defined $repos_root && $repos_root ne $u;
1070 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1071 foreach (qw
/branches tags/) {
1072 resolve_local_globs
($u, $fetch,
1073 $remotes->{$repo_id}->{$_});
1076 unless (defined $p) {
1078 $p =~ s
#^\Q$u\E(?:/|$)## or next;
1080 foreach my $f (keys %$fetch) {
1082 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1089 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1090 my $self = _new
($class, $repo_id, $ref_id, $path);
1092 $self->init_remote_config($url, $no_write);
1099 foreach (command
(qw
/config -l/)) {
1100 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1101 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1102 my ($repo_id, $path, $ref) = ($1, $2, $3);
1103 if ($ref eq $ref_id) {
1104 $path = '' if ($path =~ m
#^\./?#);
1105 return ($repo_id, $path);
1108 (undef, undef, undef);
1112 my ($class, $ref_id, $repo_id, $path) = @_;
1113 if (defined $ref_id && !defined $repo_id && !defined $path) {
1114 ($repo_id, $path) = find_ref
($ref_id);
1115 if (!defined $repo_id) {
1116 die "Could not find a \"svn-remote.*.fetch\" key ",
1117 "in the repository configuration matching: ",
1118 "refs/remotes/$ref_id\n";
1121 my $self = _new
($class, $repo_id, $ref_id, $path);
1122 if (!defined $self->{path
} || !length $self->{path
}) {
1123 my $fetch = command_oneline
('config', '--get',
1124 "svn-remote.$repo_id.fetch",
1125 ":refs/remotes/$ref_id\$") or
1126 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1127 "\":refs/remotes/$ref_id\$\" in config\n";
1128 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1130 $self->{url
} = command_oneline
('config', '--get',
1131 "svn-remote.$repo_id.url") or
1132 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1137 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
1141 return $self->{svm
}->{uuid
} if $self->svm;
1143 unless ($self->{svm
}) {
1144 die "SVM UUID not cached, and reading remotely failed\n";
1146 $self->{svm
}->{uuid
};
1151 return $self->{svm
} if $self->{svm
};
1153 # see if we have it in our config, first:
1155 my $section = "svn-remote.$self->{repo_id}";
1157 source
=> tmp_config
('--get', "$section.svm-source"),
1158 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1159 replace
=> tmp_config
('--get', "$section.svm-replace"),
1162 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1163 $self->{svm
} = $svm;
1169 my ($self, $ra) = @_;
1170 return $ra if $self->svm;
1172 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1173 "(svm:source, svm:uuid) ",
1174 "from the following URLs:\n" );
1175 sub read_svm_props
{
1176 my ($self, $ra, $path, $r) = @_;
1177 my $props = ($ra->get_dir($path, $r))[2];
1178 my $src = $props->{'svm:source'};
1179 my $uuid = $props->{'svm:uuid'};
1180 return undef if (!$src || !$uuid);
1184 $uuid =~ m{^[0-9a-f\-]{30,}$}
1185 or die "doesn't look right - svm:uuid is '$uuid'\n";
1187 # the '!' is used to mark the repos_root!/relative/path
1188 $src =~ s{/?!/?}{/};
1189 $src =~ s{/+$}{}; # no trailing slashes please
1190 # username is of no interest
1191 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1193 my $replace = $ra->{url
};
1194 $replace .= "/$path" if length $path;
1196 my $section = "svn-remote.$self->{repo_id}";
1197 tmp_config
("$section.svm-source", $src);
1198 tmp_config
("$section.svm-replace", $replace);
1199 tmp_config
("$section.svm-uuid", $uuid);
1207 my $r = $ra->get_latest_revnum;
1208 my $path = $self->{path
};
1210 while (length $path) {
1211 unless ($tried{"$self->{url}/$path"}) {
1212 return $ra if $self->read_svm_props($ra, $path, $r);
1213 $tried{"$self->{url}/$path"} = 1;
1215 $path =~ s
#/?[^/]+$##;
1217 die "Path: '$path' should be ''\n" if $path ne '';
1218 return $ra if $self->read_svm_props($ra, $path, $r);
1219 $tried{"$self->{url}/$path"} = 1;
1221 if ($ra->{repos_root
} eq $self->{url
}) {
1222 die @err, (map { " $_\n" } keys %tried), "\n";
1225 # nope, make sure we're connected to the repository root:
1228 $path = $ra->{svn_path
};
1229 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1230 while (length $path) {
1231 unless ($tried{"$ra->{url}/$path"}) {
1232 $ok = $self->read_svm_props($ra, $path, $r);
1234 $tried{"$ra->{url}/$path"} = 1;
1236 $path =~ s
#/?[^/]+$##;
1238 die "Path: '$path' should be ''\n" if $path ne '';
1239 $ok ||= $self->read_svm_props($ra, $path, $r);
1240 $tried{"$ra->{url}/$path"} = 1;
1242 die @err, (map { " $_\n" } keys %tried), "\n";
1244 Git
::SVN
::Ra
->new($self->{url
});
1249 return $self->{svnsync
} if $self->{svnsync
};
1251 if ($self->no_metadata) {
1252 die "Can't have both 'noMetadata' and ",
1253 "'useSvnsyncProps' options set!\n";
1255 if ($self->rewrite_root) {
1256 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1261 # see if we have it in our config, first:
1263 my $section = "svn-remote.$self->{repo_id}";
1265 url
=> tmp_config
('--get', "$section.svnsync-url"),
1266 uuid
=> tmp_config
('--get', "$section.svnsync-uuid"),
1269 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1270 return $self->{svnsync
} = $svnsync;
1273 my $err = "useSvnsyncProps set, but failed to read " .
1274 "svnsync property: svn:sync-from-";
1275 my $rp = $self->ra->rev_proplist(0);
1277 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1278 $url =~ m{^[a-z\+]+://} or
1279 die "doesn't look right - svn:sync-from-url is '$url'\n";
1281 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1282 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1283 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1285 my $section = "svn-remote.$self->{repo_id}";
1286 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1287 tmp_config
('--add', "$section.svnsync-url", $url);
1288 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1291 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1292 # remote lookup (useful for 'git svn log').
1295 unless ($self->{ra_uuid
}) {
1296 my $key = "svn-remote.$self->{repo_id}.uuid";
1297 my $uuid = eval { tmp_config
('--get', $key) };
1298 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1299 $self->{ra_uuid
} = $uuid;
1301 die "ra_uuid called without URL\n" unless $self->{url
};
1302 $self->{ra_uuid
} = $self->ra->get_uuid;
1303 tmp_config
('--add', $key, $self->{ra_uuid
});
1311 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1312 if ($self->use_svm_props && !$self->{svm
}) {
1313 if ($self->no_metadata) {
1314 die "Can't have both 'noMetadata' and ",
1315 "'useSvmProps' options set!\n";
1316 } elsif ($self->use_svnsync_props) {
1317 die "Can't have both 'useSvnsyncProps' and ",
1318 "'useSvmProps' options set!\n";
1320 $ra = $self->_set_svm_vars($ra);
1321 $self->{-want_revprops
} = 1;
1328 my $repos_root = $self->ra->{repos_root
};
1329 return $self->{path
} if ($self->{url
} eq $repos_root);
1330 my $url = $self->{url
} .
1331 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
1332 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1336 sub traverse_ignore
{
1337 my ($self, $fh, $path, $r) = @_;
1340 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
1342 $p =~ s
#^\Q$self->{path}\E(/|$)##;
1343 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
1344 if (my $s = $props->{'svn:ignore'}) {
1345 $s =~ s/[\r\n]+/\n/g;
1347 if (length $p == 0) {
1351 $s =~ s
#\n#\n/$p/#g;
1352 print $fh "/$p/$s\n";
1355 foreach (sort keys %$dirent) {
1356 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
1357 $self->traverse_ignore($fh, "$path/$_", $r);
1361 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1362 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1364 # returns the newest SVN revision number and newest commit SHA1
1365 sub last_rev_commit
{
1367 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1368 return ($self->{last_rev
}, $self->{last_commit
});
1370 my $c = ::verify_ref
($self->refname.'^0');
1371 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1372 my $rev = (::cmt_metadata
($c))[1];
1374 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1378 my $db_path = $self->db_path;
1379 unless (-e
$db_path) {
1380 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1381 return (undef, undef);
1383 my $offset = -41; # from tail
1385 open my $fh, '<', $db_path or croak
"$db_path not readable: $!\n";
1386 sysseek($fh, $offset, 2); # don't care for errors
1387 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1389 while (('0' x40
) eq $rl && sysseek($fh, 0, 1) != 0) {
1391 sysseek($fh, $offset, 2); # don't care for errors
1392 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1395 if ($c && $c ne $rl) {
1396 die "$db_path and ", $self->refname,
1397 " inconsistent!:\n$c != $rl\n";
1399 my $rev = sysseek($fh, 0, 1) or croak
$!;
1400 $rev = ($rev - 41) / 41;
1401 close $fh or croak
$!;
1402 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1406 sub get_fetch_range
{
1407 my ($self, $min, $max) = @_;
1408 $max ||= $self->ra->get_latest_revnum;
1409 $min ||= $self->rev_db_max;
1415 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1416 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1417 if (-e
$old_def_config && ! -e
$config) {
1418 rename $old_def_config, $config or
1419 die "Failed rename $old_def_config => $config: $!\n";
1421 my $old_config = $ENV{GIT_CONFIG
};
1422 $ENV{GIT_CONFIG
} = $config;
1425 unless (-f
$config) {
1427 open my $fh, '>', $config or
1428 die "Can't open $config: $!\n";
1429 print $fh "; This file is used internally by ",
1431 "Couldn't write to $config: $!\n";
1432 print $fh "; You should not have to edit it\n" or
1433 die "Couldn't write to $config: $!\n";
1434 close $fh or die "Couldn't close $config: $!\n";
1436 command
('config', @args);
1439 if (defined $old_config) {
1440 $ENV{GIT_CONFIG
} = $old_config;
1442 delete $ENV{GIT_CONFIG
};
1445 wantarray ?
@ret : $ret[0];
1449 my ($self, $sub) = @_;
1450 my $old_index = $ENV{GIT_INDEX_FILE
};
1451 $ENV{GIT_INDEX_FILE
} = $self->{index};
1454 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
1455 mkpath
([$dir]) unless -d
$dir;
1459 if (defined $old_index) {
1460 $ENV{GIT_INDEX_FILE
} = $old_index;
1462 delete $ENV{GIT_INDEX_FILE
};
1465 wantarray ?
@ret : $ret[0];
1468 sub assert_index_clean
{
1469 my ($self, $treeish) = @_;
1471 $self->tmp_index_do(sub {
1472 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1473 my $x = command_oneline
('write-tree');
1474 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1475 /^tree ($::sha1)/mo);
1478 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1479 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1480 command_noisy
('read-tree', $treeish);
1481 $x = command_oneline
('write-tree');
1483 ::fatal
"trees ($treeish) $y != $x\n",
1484 "Something is seriously wrong...\n";
1489 sub get_commit_parents
{
1490 my ($self, $log_entry) = @_;
1491 my (%seen, @ret, @tmp);
1492 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1493 if (my $ip = $self->{inject_parents
}) {
1494 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
1498 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1501 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1502 while (my $p = shift @tmp) {
1506 # MAXPARENT is defined to 16 in commit-tree.c:
1510 die "r$log_entry->{revision}: No room for parents:\n\t",
1511 join("\n\t", @tmp), "\n";
1518 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
1519 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1520 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
1523 if ($rwr !~ m
#^[a-z\+]+://#) {
1524 die "$rwr is not a valid URL (key: $k)\n";
1527 $self->{-rewrite_root
} = $rwr;
1532 ($self->rewrite_root || $self->{url
}) .
1533 (length $self->{path
} ?
'/' . $self->{path
} : '');
1538 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
1542 my ($self, $log_entry) = @_;
1543 my $lr = $self->last_rev;
1544 if (defined $lr && $lr >= $log_entry->{revision
}) {
1545 die "Last fetched revision of ", $self->refname,
1546 " was r$lr, but we are about to fetch: ",
1547 "r$log_entry->{revision}!\n";
1549 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1550 croak
"$log_entry->{revision} = $c already exists! ",
1551 "Why are we refetching it?\n";
1553 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $log_entry->{name
};
1554 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
1555 $log_entry->{email
};
1556 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1558 my $tree = $log_entry->{tree
};
1559 if (!defined $tree) {
1560 $tree = $self->tmp_index_do(sub {
1561 command_oneline
('write-tree') });
1563 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1565 my @exec = ('git-commit-tree', $tree);
1566 foreach ($self->get_commit_parents($log_entry)) {
1567 push @exec, '-p', $_;
1569 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1571 print $msg_fh $log_entry->{log} or croak
$!;
1572 unless ($self->no_metadata) {
1573 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1576 $msg_fh->flush == 0 or croak
$!;
1577 close $msg_fh or croak
$!;
1578 chomp(my $commit = do { local $/; <$out_fh> });
1579 close $out_fh or croak
$!;
1582 if ($commit !~ /^$::sha1$/o) {
1583 die "Failed to commit, invalid sha1: $commit\n";
1586 $self->rev_db_set($log_entry->{revision
}, $commit, 1);
1588 $self->{last_rev
} = $log_entry->{revision
};
1589 $self->{last_commit
} = $commit;
1590 print "r$log_entry->{revision}";
1591 if (defined $log_entry->{svm_revision
}) {
1592 print " (\@$log_entry->{svm_revision})";
1593 $self->rev_db_set($log_entry->{svm_revision
}, $commit,
1594 0, $self->svm_uuid);
1596 print " = $commit ($self->{ref_id})\n";
1597 if (defined $_repack && (--$_repack_nr == 0)) {
1598 $_repack_nr = $_repack;
1599 # repack doesn't use any arguments with spaces in them, does it?
1600 print "Running git repack $_repack_flags ...\n";
1601 command_noisy
('repack', split(/\s+/, $_repack_flags));
1602 print "Done repacking\n";
1608 my ($self, $paths, $r) = @_;
1609 return 1 if $self->{path
} eq '';
1610 if (my $path = $paths->{"/$self->{path}"}) {
1611 return ($path->{action
} eq 'D') ?
0 : 1;
1613 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
1614 if (grep /$self->{path_regex}/, keys %$paths) {
1618 foreach (split m
#/#, $self->{path}) {
1620 next unless ($paths->{$c} &&
1621 ($paths->{$c}->{action
} =~ /^[AR]$/));
1622 if ($self->ra->check_path($self->{path
}, $r) ==
1630 sub find_parent_branch
{
1631 my ($self, $paths, $rev) = @_;
1632 return undef unless $self->follow_parent;
1633 unless (defined $paths) {
1634 my $err_handler = $SVN::Error
::handler
;
1635 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
1636 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
1638 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
1639 $SVN::Error
::handler
= $err_handler;
1641 return undef unless defined $paths;
1643 # look for a parent from another branch:
1644 my @b_path_components = split m
#/#, $self->rel_path;
1645 my @a_path_components;
1647 while (@b_path_components) {
1648 $i = $paths->{'/'.join('/', @b_path_components)};
1649 last if $i && defined $i->{copyfrom_path
};
1650 unshift(@a_path_components, pop(@b_path_components));
1652 return undef unless defined $i && defined $i->{copyfrom_path
};
1653 my $branch_from = $i->{copyfrom_path
};
1654 if (@a_path_components) {
1655 print STDERR
"branch_from: $branch_from => ";
1656 $branch_from .= '/'.join('/', @a_path_components);
1657 print STDERR
$branch_from, "\n";
1659 my $r = $i->{copyfrom_rev
};
1660 my $repos_root = $self->ra->{repos_root
};
1661 my $url = $self->ra->{url
};
1662 my $new_url = $repos_root . $branch_from;
1663 print STDERR
"Found possible branch point: ",
1664 "$new_url => ", $self->full_url, ", $r\n";
1665 $branch_from =~ s
#^/##;
1666 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
1668 my $ref_id = $self->{ref_id
};
1669 $ref_id =~ s/\@\d+$//;
1671 # just grow a tail if we're not unique enough :x
1672 $ref_id .= '-' while find_ref
($ref_id);
1673 print STDERR
"Initializing parent: $ref_id\n";
1674 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id, 1);
1676 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1677 if (!defined $r0 || !defined $parent) {
1679 ($r0, $parent) = $gs->last_rev_commit;
1681 if (defined $r0 && defined $parent) {
1682 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1684 if ($self->ra->can_do_switch) {
1685 $self->assert_index_clean($parent);
1686 print STDERR
"Following parent with do_switch\n";
1687 # do_switch works with svn/trunk >= r22312, but that
1688 # is not included with SVN 1.4.3 (the latest version
1689 # at the moment), so we can't rely on it
1690 $self->{last_commit
} = $parent;
1691 $ed = SVN
::Git
::Fetcher
->new($self);
1692 $gs->ra->gs_do_switch($r0, $rev, $gs,
1693 $self->full_url, $ed)
1694 or die "SVN connection failed somewhere...\n";
1696 print STDERR
"Following parent with do_update\n";
1697 $ed = SVN
::Git
::Fetcher
->new($self);
1698 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1699 or die "SVN connection failed somewhere...\n";
1701 print STDERR
"Successfully followed parent\n";
1702 return $self->make_log_entry($rev, [$parent], $ed);
1708 my ($self, $paths, $rev) = @_;
1710 my ($last_rev, @parents);
1711 if (my $lc = $self->last_commit) {
1712 # we can have a branch that was deleted, then re-added
1713 # under the same name but copied from another path, in
1714 # which case we'll have multiple parents (we don't
1715 # want to break the original ref, nor lose copypath info):
1716 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1717 push @
{$log_entry->{parents
}}, $lc;
1720 $ed = SVN
::Git
::Fetcher
->new($self);
1721 $last_rev = $self->{last_rev
};
1726 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1729 $ed = SVN
::Git
::Fetcher
->new($self);
1731 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1732 die "SVN connection failed somewhere...\n";
1734 $self->make_log_entry($rev, \
@parents, $ed);
1738 my ($self, $ed) = @_;
1740 my $h = $ed->{empty
};
1741 foreach (sort keys %$h) {
1742 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1743 push @out, " $act: " . uri_encode
($_);
1744 warn "W: $act: $_\n";
1746 foreach my $t (qw
/dir_prop file_prop/) {
1747 $h = $ed->{$t} or next;
1748 foreach my $path (sort keys %$h) {
1749 my $ppath = $path eq '' ?
'.' : $path;
1750 foreach my $prop (sort keys %{$h->{$path}}) {
1751 next if $SKIP_PROP{$prop};
1752 my $v = $h->{$path}->{$prop};
1753 my $t_ppath_prop = "$t: " .
1754 uri_encode
($ppath) . ' ' .
1757 push @out, " +$t_ppath_prop " .
1760 push @out, " -$t_ppath_prop";
1765 foreach my $t (qw
/absent_file absent_directory/) {
1766 $h = $ed->{$t} or next;
1767 foreach my $parent (sort keys %$h) {
1768 foreach my $path (sort @
{$h->{$parent}}) {
1769 push @out, " $t: " .
1770 uri_encode
("$parent/$path");
1771 warn "W: $t: $parent/$path ",
1772 "Insufficient permissions?\n";
1779 sub parse_svn_date
{
1780 my $date = shift || return '+0000 1970-01-01 00:00:00';
1781 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1782 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1783 croak
"Unable to parse date: $date\n";
1784 "+0000 $Y-$m-$d $H:$M:$S";
1789 if (!defined $author || length $author == 0) {
1790 $author = '(no author)';
1792 if (defined $::_authors
&& ! defined $::users
{$author}) {
1793 die "Author: $author not defined in $::_authors file\n";
1798 sub make_log_entry
{
1799 my ($self, $rev, $parents, $ed) = @_;
1800 my $untracked = $self->get_untracked($ed);
1802 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1803 print $un "r$rev\n" or croak
$!;
1804 print $un $_, "\n" foreach @
$untracked;
1805 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1809 my $logged = delete $self->{logged_rev_props
};
1810 if (!$logged || $self->{-want_revprops
}) {
1811 my $rp = $self->ra->rev_proplist($rev);
1812 foreach (sort keys %$rp) {
1814 if (/^svn:(author|date|log)$/) {
1815 $log_entry{$1} = $v;
1816 } elsif ($_ eq 'svm:headrev') {
1819 print $un " rev_prop: ", uri_encode
($_), ' ',
1820 uri_encode
($v), "\n";
1824 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1826 close $un or croak
$!;
1828 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1829 $log_entry{log} .= "\n";
1830 my $author = $log_entry{author
} = check_author
($log_entry{author
});
1831 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
1833 if (defined $headrev && $self->use_svm_props) {
1834 if ($self->rewrite_root) {
1835 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1838 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
1839 # we don't want "SVM: initializing mirror for junk" ...
1840 return undef if $r == 0;
1841 my $svm = $self->svm;
1842 if ($uuid ne $svm->{uuid
}) {
1843 die "UUID mismatch on SVM path:\n",
1844 "expected: $svm->{uuid}\n",
1847 my $full_url = $self->full_url;
1848 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1849 die "Failed to replace '$svm->{replace}' with ",
1850 "'$svm->{source}' in $full_url\n";
1851 # throw away username for storing in records
1852 remove_username
($full_url);
1853 $log_entry{metadata
} = "$full_url\@$r $uuid";
1854 $log_entry{svm_revision
} = $r;
1855 $email ||= "$author\@$uuid"
1856 } elsif ($self->use_svnsync_props) {
1857 my $full_url = $self->svnsync->{url
};
1858 $full_url .= "/$self->{path}" if length $self->{path
};
1859 my $uuid = $self->svnsync->{uuid
};
1860 $log_entry{metadata
} = "$full_url\@$rev $uuid";
1861 $email ||= "$author\@$uuid"
1863 $log_entry{metadata
} = $self->metadata_url. "\@$rev " .
1864 $self->ra->get_uuid;
1865 $email ||= "$author\@" . $self->ra->get_uuid;
1867 $log_entry{name
} = $name;
1868 $log_entry{email
} = $email;
1873 my ($self, $min_rev, $max_rev, @parents) = @_;
1874 my ($last_rev, $last_commit) = $self->last_rev_commit;
1875 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1876 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1880 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1881 $self->{inject_parents
} = { $rev => $tree };
1882 $self->fetch(undef, undef);
1886 my ($self, $tree) = (shift, shift);
1887 my $log_entry = ::get_commit_entry
($tree);
1888 unless ($self->{last_rev
}) {
1889 fatal
("Must have an existing revision to commit\n");
1891 my %ed_opts = ( r
=> $self->{last_rev
},
1892 log => $log_entry->{log},
1894 tree_a
=> $self->{last_commit
},
1897 $self->set_tree_cb($log_entry, $tree, @_) },
1898 svn_path
=> $self->{path
} );
1899 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
1900 print "No changes\nr$self->{last_rev} = $tree\n";
1906 my $db_path = $self->db_path;
1907 return if (-e
$db_path && ! -z
$db_path);
1908 return unless ::verify_ref
($self->refname.'^0');
1909 if (-f
$self->{db_root
}) {
1910 rename $self->{db_root
}, $db_path or die
1911 "rename $self->{db_root} => $db_path failed: $!\n";
1912 my ($dir, $base) = ($db_path =~ m
#^(.*?)/?([^/]+)$#);
1913 symlink $base, $self->{db_root
} or die
1914 "symlink $base => $self->{db_root} failed: $!\n";
1917 print "Rebuilding $db_path ...\n";
1918 my ($rev_list, $ctx) = command_output_pipe
("rev-list", $self->refname);
1920 my $full_url = $self->full_url;
1921 remove_username
($full_url);
1923 while (<$rev_list>) {
1926 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1927 my ($url, $rev, $uuid) = ::cmt_metadata
($c);
1928 remove_username
($url);
1930 # ignore merges (from set-tree)
1931 next if (!defined $rev || !$uuid);
1933 # if we merged or otherwise started elsewhere, this is
1934 # how we break out of it
1935 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1936 ($full_url && $url && ($url ne $full_url))) {
1940 $svn_uuid ||= $uuid;
1942 $self->rev_db_set($rev, $c);
1943 print "r$rev = $c\n";
1945 command_close_pipe
($rev_list, $ctx);
1946 print "Done rebuilding $db_path\n";
1950 # Tie::File seems to be prone to offset errors if revisions get sparse,
1951 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1952 # one of my favorite modules is out :< Next up would be one of the DBM
1953 # modules, but I'm not sure which is most portable... So I'll just
1954 # go with something that's plain-text, but still capable of
1955 # being randomly accessed. So here's my ultra-simple fixed-width
1956 # database. All records are 40 characters + "\n", so it's easy to seek
1957 # to a revision: (41 * rev) is the byte offset.
1958 # A record of 40 0s denotes an empty revision.
1959 # And yes, it's still pretty fast (faster than Tie::File).
1960 # These files are disposable unless noMetadata or useSvmProps is set
1963 my ($fh, $rev, $commit) = @_;
1964 my $offset = $rev * 41;
1965 # assume that append is the common case:
1966 seek $fh, 0, 2 or croak
$!;
1968 if ($pos < $offset) {
1969 for (1 .. (($offset - $pos) / 41)) {
1970 print $fh (('0' x
40),"\n") or croak
$!;
1973 seek $fh, $offset, 0 or croak
$!;
1974 print $fh $commit,"\n" or croak
$!;
1980 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1981 mkpath
([$dir]) unless -d
$dir;
1982 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1983 close $fh or die "Couldn't close (create) $path: $!\n";
1988 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1989 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1990 my $db = $self->db_path($uuid);
1991 my $db_lock = "$db.lock";
1994 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
1995 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
1999 $LOCKFILES{$db_lock} = 1;
2001 # both of these options make our .rev_db file very, very important
2002 # and we can't afford to lose it because rebuild() won't work
2003 if ($self->use_svm_props || $self->no_metadata) {
2005 copy
($db, $db_lock) or die "rev_db_set(@_): ",
2007 "$db => $db_lock ($!)\n";
2009 rename $db, $db_lock or die "rev_db_set(@_): ",
2010 "Failed to rename: ",
2011 "$db => $db_lock ($!)\n";
2013 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2014 _rev_db_set
($fh, $rev, $commit);
2016 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2017 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2019 close $fh or croak
$!;
2022 command_noisy
('update-ref', '-m', "r$rev",
2023 $self->refname, $commit);
2025 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2026 "$db_lock => $db ($!)\n";
2027 delete $LOCKFILES{$db_lock};
2029 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2030 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2031 kill $sig, $$ if defined $sig;
2038 my $db_path = $self->db_path;
2039 my @stat = stat $db_path or return 0;
2040 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2041 my $max = $stat[7] / 41;
2042 (($max > 0) ?
$max - 1 : 0);
2046 my ($self, $rev, $uuid) = @_;
2048 my $offset = $rev * 41;
2049 my $db_path = $self->db_path($uuid);
2050 return undef unless -e
$db_path;
2051 open my $fh, '<', $db_path or croak
$!;
2052 if (sysseek($fh, $offset, 0) == $offset) {
2053 my $read = sysread($fh, $ret, 40);
2054 $ret = undef if ($read != 40 || $ret eq ('0'x40
));
2056 close $fh or croak
$!;
2060 sub find_rev_before
{
2061 my ($self, $rev, $eq_ok) = @_;
2062 --$rev unless $eq_ok;
2064 if (my $c = $self->rev_db_get($rev)) {
2069 return (undef, undef);
2073 my ($class, $repo_id, $ref_id, $path) = @_;
2074 unless (defined $repo_id && length $repo_id) {
2075 $repo_id = $Git::SVN
::default_repo_id
;
2077 unless (defined $ref_id && length $ref_id) {
2078 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2080 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
2081 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2082 $_[3] = $path = '' unless (defined $path);
2083 mkpath
(["$ENV{GIT_DIR}/svn"]);
2085 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2086 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2087 db_root
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
2091 my ($self, $uuid) = @_;
2092 $uuid ||= $self->ra_uuid;
2093 "$self->{db_root}.$uuid";
2098 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2102 sub remove_username
{
2103 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2106 package Git
::SVN
::Prompt
;
2110 use vars qw
/$_no_auth_cache $_username/;
2113 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2114 $may_save = undef if $_no_auth_cache;
2115 $default_username = $_username if defined $_username;
2116 if (defined $default_username && length $default_username) {
2117 if (defined $realm && length $realm) {
2118 print STDERR
"Authentication realm: $realm\n";
2121 $cred->username($default_username);
2123 username
($cred, $realm, $may_save, $pool);
2125 $cred->password(_read_password
("Password for '" .
2126 $cred->username . "': ", $realm));
2127 $cred->may_save($may_save);
2128 $SVN::_Core
::SVN_NO_ERROR
;
2131 sub ssl_server_trust
{
2132 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2133 $may_save = undef if $_no_auth_cache;
2134 print STDERR
"Error validating server certificate for '$realm':\n";
2135 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2136 print STDERR
" - The certificate is not issued by a trusted ",
2137 "authority. Use the\n",
2138 " fingerprint to validate the certificate manually!\n";
2140 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2141 print STDERR
" - The certificate hostname does not match.\n";
2143 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2144 print STDERR
" - The certificate is not yet valid.\n";
2146 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2147 print STDERR
" - The certificate has expired.\n";
2149 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2150 print STDERR
" - The certificate has an unknown error.\n";
2153 "Certificate information:\n".
2154 " - Hostname: %s\n".
2155 " - Valid: from %s until %s\n".
2157 " - Fingerprint: %s\n",
2158 map $cert_info->$_, qw(hostname valid_from valid_until
2159 issuer_dname fingerprint);
2162 print STDERR
$may_save ?
2163 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2164 "(R)eject or accept (t)emporarily? ";
2166 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2167 if ($choice =~ /^t$/i) {
2168 $cred->may_save(undef);
2169 } elsif ($choice =~ /^r$/i) {
2171 } elsif ($may_save && $choice =~ /^p$/i) {
2172 $cred->may_save($may_save);
2176 $cred->accepted_failures($failures);
2177 $SVN::_Core
::SVN_NO_ERROR
;
2180 sub ssl_client_cert
{
2181 my ($cred, $realm, $may_save, $pool) = @_;
2182 $may_save = undef if $_no_auth_cache;
2183 print STDERR
"Client certificate filename: ";
2185 chomp(my $filename = <STDIN
>);
2186 $cred->cert_file($filename);
2187 $cred->may_save($may_save);
2188 $SVN::_Core
::SVN_NO_ERROR
;
2191 sub ssl_client_cert_pw
{
2192 my ($cred, $realm, $may_save, $pool) = @_;
2193 $may_save = undef if $_no_auth_cache;
2194 $cred->password(_read_password
("Password: ", $realm));
2195 $cred->may_save($may_save);
2196 $SVN::_Core
::SVN_NO_ERROR
;
2200 my ($cred, $realm, $may_save, $pool) = @_;
2201 $may_save = undef if $_no_auth_cache;
2202 if (defined $realm && length $realm) {
2203 print STDERR
"Authentication realm: $realm\n";
2206 if (defined $_username) {
2207 $username = $_username;
2209 print STDERR
"Username: ";
2211 chomp($username = <STDIN
>);
2213 $cred->username($username);
2214 $cred->may_save($may_save);
2215 $SVN::_Core
::SVN_NO_ERROR
;
2218 sub _read_password
{
2219 my ($prompt, $realm) = @_;
2220 print STDERR
$prompt;
2222 require Term
::ReadKey
;
2223 Term
::ReadKey
::ReadMode
('noecho');
2225 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2226 last if $key =~ /[\012\015]/; # \n\r
2229 Term
::ReadKey
::ReadMode
('restore');
2238 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2239 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2240 $SVN::Node
::none
.$SVN::Node
::file
.
2241 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2242 $SVN::Auth
::SSL
::CNMISMATCH
.
2243 $SVN::Auth
::SSL
::NOTYETVALID
.
2244 $SVN::Auth
::SSL
::EXPIRED
.
2245 $SVN::Auth
::SSL
::UNKNOWNCA
.
2246 $SVN::Auth
::SSL
::OTHER
;
2249 package SVN
::Git
::Fetcher
;
2257 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2259 my ($class, $git_svn) = @_;
2260 my $self = SVN
::Delta
::Editor
->new;
2261 bless $self, $class;
2262 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
2263 $self->{empty
} = {};
2264 $self->{dir_prop
} = {};
2265 $self->{file_prop
} = {};
2266 $self->{absent_dir
} = {};
2267 $self->{absent_file
} = {};
2268 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
2272 sub set_path_strip
{
2273 my ($self, $path) = @_;
2274 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
2281 sub open_directory
{
2282 my ($self, $path, $pb, $rev) = @_;
2287 my ($self, $path) = @_;
2288 if ($self->{path_strip
}) {
2289 $path =~ s!$self->{path_strip}!! or
2290 die "Failed to strip path '$path' ($self->{path_strip})\n";
2296 my ($self, $path, $rev, $pb) = @_;
2298 my $gpath = $self->git_path($path);
2299 return undef if ($gpath eq '');
2301 # remove entire directories.
2302 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
2303 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2305 $self->{c
}, '--', $gpath);
2309 $self->{gii
}->remove($_);
2310 print "\tD\t$_\n" unless $::_q
;
2312 print "\tD\t$gpath/\n" unless $::_q
;
2313 command_close_pipe
($ls, $ctx);
2314 $self->{empty
}->{$path} = 0
2316 $self->{gii
}->remove($gpath);
2317 print "\tD\t$gpath\n" unless $::_q
;
2323 my ($self, $path, $pb, $rev) = @_;
2324 my $gpath = $self->git_path($path);
2325 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
2326 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2327 unless (defined $mode && defined $blob) {
2328 die "$path was not found in commit $self->{c} (r$rev)\n";
2330 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2331 pool
=> SVN
::Pool
->new, action
=> 'M' };
2335 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2336 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2337 delete $self->{empty
}->{$dir};
2338 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2339 pool
=> SVN
::Pool
->new, action
=> 'A' };
2343 my ($self, $path, $cp_path, $cp_rev) = @_;
2344 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2345 delete $self->{empty
}->{$dir};
2346 $self->{empty
}->{$path} = 1;
2350 sub change_dir_prop
{
2351 my ($self, $db, $prop, $value) = @_;
2352 $self->{dir_prop
}->{$db->{path
}} ||= {};
2353 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2357 sub absent_directory
{
2358 my ($self, $path, $pb) = @_;
2359 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2360 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2365 my ($self, $path, $pb) = @_;
2366 $self->{absent_file
}->{$pb->{path
}} ||= [];
2367 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2371 sub change_file_prop
{
2372 my ($self, $fb, $prop, $value) = @_;
2373 if ($prop eq 'svn:executable') {
2374 if ($fb->{mode_b
} != 120000) {
2375 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2377 } elsif ($prop eq 'svn:special') {
2378 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2380 $self->{file_prop
}->{$fb->{path
}} ||= {};
2381 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2386 sub apply_textdelta
{
2387 my ($self, $fb, $exp) = @_;
2388 my $fh = IO
::File
->new_tmpfile;
2390 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2391 # (but $base does not,) so dup() it for reading in close_file
2392 open my $dup, '<&', $fh or croak
$!;
2393 my $base = IO
::File
->new_tmpfile;
2394 $base->autoflush(1);
2396 defined (my $pid = fork) or croak
$!;
2398 open STDOUT
, '>&', $base or croak
$!;
2399 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2400 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2406 seek $base, 0, 0 or croak
$!;
2407 my $md5 = Digest
::MD5
->new;
2408 $md5->addfile($base);
2409 my $got = $md5->hexdigest;
2410 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2412 " got: $got\n" if ($got ne $exp);
2415 seek $base, 0, 0 or croak
$!;
2417 $fb->{base
} = $base;
2418 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2422 my ($self, $fb, $exp) = @_;
2424 my $path = $self->git_path($fb->{path
});
2425 if (my $fh = $fb->{fh
}) {
2426 seek($fh, 0, 0) or croak
$!;
2427 my $md5 = Digest
::MD5
->new;
2429 my $got = $md5->hexdigest;
2430 die "Checksum mismatch: $path\n",
2431 "expected: $exp\n got: $got\n" if ($got ne $exp);
2432 seek($fh, 0, 0) or croak
$!;
2433 if ($fb->{mode_b
} == 120000) {
2434 read($fh, my $buf, 5) == 5 or croak
$!;
2435 $buf eq 'link ' or die "$path has mode 120000",
2436 "but is not a link\n";
2438 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2440 open STDIN
, '<&', $fh or croak
$!;
2441 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2443 chomp($hash = do { local $/; <$out> });
2444 close $out or croak
$!;
2445 close $fh or croak
$!;
2446 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2447 close $fb->{base
} or croak
$!;
2449 $hash = $fb->{blob
} or die "no blob information\n";
2452 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
2453 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
2459 $self->{nr
} = $self->{gii
}->{nr
};
2460 delete $self->{gii
};
2461 $self->SUPER::abort_edit
(@_);
2466 $self->{git_commit_ok
} = 1;
2467 $self->{nr
} = $self->{gii
}->{nr
};
2468 delete $self->{gii
};
2469 $self->SUPER::close_edit
(@_);
2472 package SVN
::Git
::Editor
;
2473 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
2481 my ($class, $opts) = @_;
2482 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
2483 die "$_ required!\n" unless (defined $opts->{$_});
2486 my $pool = SVN
::Pool
->new;
2487 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
2488 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
2491 # $opts->{ra} functions should not be used after this:
2492 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
2493 $opts->{editor_cb
}, $pool);
2494 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
2495 bless $self, $class;
2496 foreach (qw
/svn_path r tree_a tree_b/) {
2497 $self->{$_} = $opts->{$_};
2499 $self->{url
} = $opts->{ra
}->{url
};
2500 $self->{mods
} = $mods;
2501 $self->{types
} = $types;
2502 $self->{pool
} = $pool;
2503 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2505 $self->{path_prefix
} = length $self->{svn_path
} ?
2506 "$self->{svn_path}/" : '';
2511 my ($tree_a, $tree_b) = @_;
2512 my @diff_tree = qw(diff-tree -z -r);
2513 if ($_cp_similarity) {
2514 push @diff_tree, "-C$_cp_similarity";
2516 push @diff_tree, '-C';
2518 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2519 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
2520 push @diff_tree, $tree_a, $tree_b;
2521 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2525 while (<$diff_fh>) {
2526 chomp $_; # this gets rid of the trailing "\0"
2527 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2528 $::sha1\s
($::sha1
)\s
2529 ([MTCRAD
])\d
*$/xo
) {
2530 push @mods, { mode_a
=> $1, mode_b
=> $2,
2531 sha1_b
=> $3, chg
=> $4 };
2532 if ($4 =~ /^(?:C|R)$/) {
2537 } elsif ($state eq 'file_a') {
2538 my $x = $mods[$#mods] or croak
"Empty array\n";
2539 if ($x->{chg
} !~ /^(?:C|R)$/) {
2540 croak
"Error parsing $_, $x->{chg}\n";
2544 } elsif ($state eq 'file_b') {
2545 my $x = $mods[$#mods] or croak
"Empty array\n";
2546 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2547 croak
"Error parsing $_, $x->{chg}\n";
2549 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2550 croak
"Error parsing $_, $x->{chg}\n";
2555 croak
"Error parsing $_\n";
2558 command_close_pipe
($diff_fh, $ctx);
2562 sub check_diff_paths
{
2563 my ($ra, $pfx, $rev, $mods) = @_;
2565 $pfx .= '/' if length $pfx;
2567 sub type_diff_paths
{
2568 my ($ra, $types, $path, $rev) = @_;
2569 my @p = split m
#/+#, $path;
2571 unless (defined $types->{$c}) {
2572 $types->{$c} = $ra->check_path($c, $rev);
2575 $c .= '/' . shift @p;
2576 next if defined $types->{$c};
2577 $types->{$c} = $ra->check_path($c, $rev);
2581 foreach my $m (@
$mods) {
2582 foreach my $f (qw
/file_a file_b/) {
2583 next unless defined $m->{$f};
2584 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
2585 if (length $pfx.$dir && ! defined $types{$dir}) {
2586 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
2594 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2598 my ($self, $path) = @_;
2599 $self->{path_prefix
}.(defined $path ?
$path : '');
2603 my ($self, $path) = @_;
2604 $self->{url
} . '/' . $self->repo_path($path);
2609 my $rm = $self->{rm
};
2610 delete $rm->{''}; # we never delete the url we're tracking
2613 foreach (keys %$rm) {
2614 my @d = split m
#/#, $_;
2618 $c .= '/' . shift @d;
2622 delete $rm->{$self->{svn_path
}};
2623 delete $rm->{''}; # we never delete the url we're tracking
2626 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
2631 my @dn = split m
#/#, $_;
2633 delete $rm->{join '/', @dn};
2640 command_close_pipe
($fh, $ctx);
2642 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2643 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2644 $self->close_directory($bat->{$d}, $p);
2645 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2646 print "\tD+\t$d/\n" unless $::_q
;
2647 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2652 sub open_or_add_dir
{
2653 my ($self, $full_path, $baton) = @_;
2654 my $t = $self->{types
}->{$full_path};
2656 die "$full_path not known in r$self->{r} or we have a bug!\n";
2658 if ($t == $SVN::Node
::none
) {
2659 return $self->add_directory($full_path, $baton,
2660 undef, -1, $self->{pool
});
2661 } elsif ($t == $SVN::Node
::dir
) {
2662 return $self->open_directory($full_path, $baton,
2663 $self->{r
}, $self->{pool
});
2665 print STDERR
"$full_path already exists in repository at ",
2666 "r$self->{r} and it is not a directory (",
2667 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2672 my ($self, $path) = @_;
2673 my $bat = $self->{bat
};
2674 my $repo_path = $self->repo_path($path);
2675 return $bat->{''} unless (length $repo_path);
2676 my @p = split m
#/+#, $repo_path;
2678 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2681 $c .= '/' . shift @p;
2682 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2688 my ($self, $m) = @_;
2689 my ($dir, $file) = split_path
($m->{file_b
});
2690 my $pbat = $self->ensure_path($dir);
2691 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2693 print "\tA\t$m->{file_b}\n" unless $::_q
;
2694 $self->chg_file($fbat, $m);
2695 $self->close_file($fbat,undef,$self->{pool
});
2699 my ($self, $m) = @_;
2700 my ($dir, $file) = split_path
($m->{file_b
});
2701 my $pbat = $self->ensure_path($dir);
2702 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2703 $self->url_path($m->{file_a
}), $self->{r
});
2704 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2705 $self->chg_file($fbat, $m);
2706 $self->close_file($fbat,undef,$self->{pool
});
2710 my ($self, $path, $pbat) = @_;
2711 my $rpath = $self->repo_path($path);
2712 my ($dir, $file) = split_path
($rpath);
2713 $self->{rm
}->{$dir} = 1;
2714 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2718 my ($self, $m) = @_;
2719 my ($dir, $file) = split_path
($m->{file_b
});
2720 my $pbat = $self->ensure_path($dir);
2721 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2722 $self->url_path($m->{file_a
}), $self->{r
});
2723 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2724 $self->chg_file($fbat, $m);
2725 $self->close_file($fbat,undef,$self->{pool
});
2727 ($dir, $file) = split_path
($m->{file_a
});
2728 $pbat = $self->ensure_path($dir);
2729 $self->delete_entry($m->{file_a
}, $pbat);
2733 my ($self, $m) = @_;
2734 my ($dir, $file) = split_path
($m->{file_b
});
2735 my $pbat = $self->ensure_path($dir);
2736 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2737 $pbat,$self->{r
},$self->{pool
});
2738 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
2739 $self->chg_file($fbat, $m);
2740 $self->close_file($fbat,undef,$self->{pool
});
2743 sub T
{ shift->M(@_) }
2745 sub change_file_prop
{
2746 my ($self, $fbat, $pname, $pval) = @_;
2747 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2751 my ($self, $fbat, $m) = @_;
2752 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2753 $self->change_file_prop($fbat,'svn:executable','*');
2754 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2755 $self->change_file_prop($fbat,'svn:executable',undef);
2757 my $fh = IO
::File
->new_tmpfile or croak
$!;
2758 if ($m->{mode_b
} =~ /^120/) {
2759 print $fh 'link ' or croak
$!;
2760 $self->change_file_prop($fbat,'svn:special','*');
2761 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2762 $self->change_file_prop($fbat,'svn:special',undef);
2764 defined(my $pid = fork) or croak
$!;
2766 open STDOUT
, '>&', $fh or croak
$!;
2767 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2771 $fh->flush == 0 or croak
$!;
2772 seek $fh, 0, 0 or croak
$!;
2774 my $md5 = Digest
::MD5
->new;
2775 $md5->addfile($fh) or croak
$!;
2776 seek $fh, 0, 0 or croak
$!;
2778 my $exp = $md5->hexdigest;
2779 my $pool = SVN
::Pool
->new;
2780 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2781 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2782 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2785 close $fh or croak
$!;
2789 my ($self, $m) = @_;
2790 my ($dir, $file) = split_path
($m->{file_b
});
2791 my $pbat = $self->ensure_path($dir);
2792 print "\tD\t$m->{file_b}\n" unless $::_q
;
2793 $self->delete_entry($m->{file_b
}, $pbat);
2798 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2799 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2800 $self->close_directory($bat->{$_}, $p);
2802 $self->SUPER::close_edit
($p);
2808 $self->SUPER::abort_edit
($self->{pool
});
2813 $self->SUPER::DESTROY
(@_);
2814 $self->{pool
}->clear;
2817 # this drives the editor
2820 my $mods = $self->{mods
};
2821 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2822 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
2824 if (defined $o{$f}) {
2827 fatal
("Invalid change type: $f\n");
2830 $self->rmdirs if $_rmdir;
2836 return scalar @
$mods;
2839 package Git
::SVN
::Ra
;
2840 use vars qw
/@ISA $config_dir $_log_window_size/;
2843 my ($can_do_switch, %ignored_err, $RA);
2846 # enforce temporary pool usage for some simple functions
2848 foreach (qw
/get_latest_revnum get_uuid get_repos_root/) {
2851 my \$pool = SVN::Pool->new;
2852 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2854 wantarray ? \@ret : \$ret[0]; }\n";
2857 # get_dir needs $pool held in cache for dirents to work,
2858 # check_path is cacheable and rev_proplist is close enough
2860 foreach (qw
/check_path get_dir rev_proplist/) {
2861 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2864 my \$k = join(\"\\0\", \@_);
2865 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2866 return wantarray ? \@\$x : \$x->[0];
2868 my \$pool = SVN::Pool->new;
2869 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2870 if (\$r != \$${_}_rev) {
2871 \%${_}_cache = ( pool => [] );
2874 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2875 push \@{\$${_}_cache{pool}}, \$pool;
2876 wantarray ? \@ret : \$ret[0]; }\n";
2883 my ($class, $url) = @_;
2885 return $RA if ($RA && $RA->{url
} eq $url);
2886 $RA->{pool
}->clear if $RA;
2888 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2889 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2890 SVN
::Client
::get_simple_provider
(),
2891 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2892 SVN
::Client
::get_simple_prompt_provider
(
2893 \
&Git
::SVN
::Prompt
::simple
, 2),
2894 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2895 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2896 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2897 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2898 SVN
::Client
::get_username_provider
(),
2899 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2900 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2901 SVN
::Client
::get_username_prompt_provider
(
2902 \
&Git
::SVN
::Prompt
::username
, 2),
2904 my $config = SVN
::Core
::config_get_config
($config_dir);
2905 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2907 pool
=> SVN
::Pool
->new,
2908 auth_provider_callbacks
=> $callbacks);
2909 $self->{svn_path
} = $url;
2910 $self->{repos_root
} = $self->get_repos_root;
2911 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
2912 $RA = bless $self, $class;
2916 # do not call the real DESTROY since we store ourselves in $RA
2920 my ($self, @args) = @_;
2921 my $pool = SVN
::Pool
->new;
2922 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2923 my $ret = $self->SUPER::get_log
(@args, $pool);
2928 sub get_commit_editor
{
2929 my ($self, $log, $cb, $pool) = @_;
2930 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2931 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2935 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2936 my $new = ($rev_a == $rev_b);
2937 my $path = $gs->{path
};
2939 if ($new && -e
$gs->{index}) {
2940 unlink $gs->{index} or die
2941 "Couldn't unlink index: $gs->{index}: $!\n";
2943 my $pool = SVN
::Pool
->new;
2944 $editor->set_path_strip($path);
2945 my (@pc) = split m
#/#, $path;
2946 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
2948 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2950 # Since we can't rely on svn_ra_reparent being available, we'll
2951 # just have to do some magic with set_path to make it so
2952 # we only want a partial path.
2954 my $final = join('/', @pc);
2956 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2957 $sp .= '/' if length $sp;
2960 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2962 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2964 $reporter->finish_report($pool);
2966 $editor->{git_commit_ok
};
2969 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2970 # svn_ra_reparent didn't work before 1.4)
2972 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2973 my $path = $gs->{path
};
2974 my $pool = SVN
::Pool
->new;
2976 my $full_url = $self->{url
};
2977 my $old_url = $full_url;
2978 $full_url .= "/$path" if length $path;
2979 my ($ra, $reparented);
2980 if ($old_url ne $full_url) {
2981 if ($old_url !~ m
#^svn(\+ssh)?://#) {
2982 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
2984 $self->{url
} = $full_url;
2987 $ra = Git
::SVN
::Ra
->new($full_url);
2991 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2992 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2993 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2994 $reporter->finish_report($pool);
2997 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
2998 $self->{url
} = $old_url;
3002 $editor->{git_commit_ok
};
3005 sub gs_fetch_loop_common
{
3006 my ($self, $base, $head, $gsv, $globs) = @_;
3007 return if ($base > $head);
3008 my $inc = $_log_window_size;
3009 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
3011 my $common_max = scalar @
$gsv;
3013 foreach my $gs (@
$gsv) {
3014 my @tmp = split m
#/#, $gs->{path};
3017 $p .= length($p) ?
"/$_" : $_;
3023 $common_max += scalar @
$globs;
3024 foreach my $glob (@
$globs) {
3025 my @tmp = split m
#/#, $glob->{path}->{left};
3028 $p .= length($p) ?
"/$_" : $_;
3034 my $longest_path = '';
3035 foreach (sort {length $b <=> length $a} keys %common) {
3036 if ($common{$_} == $common_max) {
3044 my $err_handler = $SVN::Error
::handler
;
3045 $SVN::Error
::handler
= sub {
3047 skip_unknown_revs
($err);
3050 my ($paths, $r, $author, $date, $log) = @_;
3051 [ dup_changed_paths
($paths),
3052 { author
=> $author, date
=> $date, log => $log } ];
3054 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3055 sub { $revs{$_[1]} = _cb
(@_) });
3056 if ($err && $max >= $head) {
3057 print STDERR
"Path '$longest_path' ",
3058 "was probably deleted:\n",
3059 $err->expanded_message,
3060 "\nWill attempt to follow ",
3061 "revisions r$min .. r$max ",
3062 "committed before the deletion\n";
3064 while (--$hi >= $min) {
3066 $self->get_log([$longest_path], $min, $hi,
3069 $revs{$_[1]} = _cb
(@_) });
3071 print STDERR
"r$min .. r$ok OK\n";
3076 $SVN::Error
::handler
= $err_handler;
3078 my %exists = map { $_->{path
} => $_ } @
$gsv;
3079 foreach my $r (sort {$a <=> $b} keys %revs) {
3080 my ($paths, $logged) = @
{$revs{$r}};
3082 foreach my $gs ($self->match_globs(\
%exists, $paths,
3084 if ($gs->rev_db_max >= $r) {
3087 next unless $gs->match_paths($paths, $r);
3088 $gs->{logged_rev_props
} = $logged;
3089 if (my $last_commit = $gs->last_commit) {
3090 $gs->assert_index_clean($last_commit);
3092 my $log_entry = $gs->do_fetch($paths, $r);
3094 $gs->do_git_commit($log_entry);
3097 foreach my $g (@
$globs) {
3098 my $k = "svn-remote.$g->{remote}." .
3100 Git
::SVN
::tmp_config
($k, $r);
3103 # pre-fill the .rev_db since it'll eventually get filled in
3104 # with '0' x40 if something new gets committed
3105 foreach my $gs (@
$gsv) {
3106 next if defined $gs->rev_db_get($max);
3107 $gs->rev_db_set($max, 0 x40
);
3109 foreach my $g (@
$globs) {
3110 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3111 Git
::SVN
::tmp_config
($k, $max);
3113 last if $max >= $head;
3116 $max = $head if ($max > $head);
3121 my ($self, $exists, $paths, $globs, $r) = @_;
3124 my ($self, $exists, $g, $r) = @_;
3125 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
3126 return unless scalar @x == 3;
3127 my $dirents = $x[0];
3128 foreach my $de (keys %$dirents) {
3129 next if $dirents->{$de}->kind != $SVN::Node
::dir
;
3130 my $p = $g->{path
}->full_path($de);
3131 next if $exists->{$p};
3132 next if (length $g->{path
}->{right
} &&
3133 ($self->check_path($p, $r) !=
3135 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
3136 $g->{ref}->full_path($de), 1);
3139 foreach my $g (@
$globs) {
3140 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3141 if ($path->{action
} =~ /^[AR]$/) {
3142 get_dir_check
($self, $exists, $g, $r);
3145 foreach (keys %$paths) {
3146 if (/$g->{path}->{left_regex}/ &&
3147 !/$g->{path}->{regex}/) {
3148 next if $paths->{$_}->{action
} !~ /^[AR]$/;
3149 get_dir_check
($self, $exists, $g, $r);
3151 next unless /$g->{path}->{regex}/;
3153 my $pathname = $g->{path
}->full_path($p);
3154 next if $exists->{$pathname};
3155 $exists->{$pathname} = Git
::SVN
->init(
3156 $self->{url
}, $pathname, undef,
3157 $g->{ref}->full_path($p), 1);
3160 foreach (split m
#/#, $g->{path}->{left}) {
3162 next unless ($paths->{$c} &&
3163 ($paths->{$c}->{action
} =~ /^[AR]$/));
3164 get_dir_check
($self, $exists, $g, $r);
3172 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
3173 my $url = $self->{repos_root
};
3174 my @components = split(m!/!, $self->{svn_path
});
3177 $url .= "/$c" if length $c;
3178 eval { (ref $self)->new($url)->get_latest_revnum };
3179 } while ($@
&& ($c = shift @components));
3185 unless (defined $can_do_switch) {
3186 my $pool = SVN
::Pool
->new;
3188 $self->do_switch(1, '', 0, $self->{url
},
3189 SVN
::Delta
::Editor
->new, $pool);
3194 $rep->abort_report($pool);
3202 sub skip_unknown_revs
{
3204 my $errno = $err->apr_err();
3205 # Maybe the branch we're tracking didn't
3206 # exist when the repo started, so it's
3207 # not an error if it doesn't, just continue
3209 # Wonderfully consistent library, eh?
3210 # 160013 - svn:// and file://
3211 # 175002 - http(s)://
3212 # 175007 - http(s):// (this repo required authorization, too...)
3213 # More codes may be discovered later...
3214 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3215 my $err_key = $err->expanded_message;
3216 # revision numbers change every time, filter them out
3217 $err_key =~ s/\d+/\0/g;
3218 $err_key = "$errno\0$err_key";
3219 unless ($ignored_err{$err_key}) {
3220 warn "W: Ignoring error from SVN, path probably ",
3221 "does not exist: ($errno): ",
3222 $err->expanded_message,"\n";
3223 $ignored_err{$err_key} = 1;
3227 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3230 # svn_log_changed_path_t objects passed to get_log are likely to be
3231 # overwritten even if only the refs are copied to an external variable,
3232 # so we should dup the structures in their entirety. Using an externally
3233 # passed pool (instead of our temporary and quickly cleared pool in
3234 # Git::SVN::Ra) does not help matters at all...
3235 sub dup_changed_paths
{
3237 return undef unless $paths;
3239 foreach my $p (keys %$paths) {
3240 my $i = $paths->{$p};
3241 my %s = map { $_ => $i->$_ }
3242 qw
/copyfrom_path copyfrom_rev action/;
3248 package Git
::SVN
::Log
;
3251 use POSIX qw
/strftime/;
3252 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
3253 %rusers $show_commit $incremental/;
3258 return 1 if defined $c->{r
};
3259 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
3260 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
3261 my @log = command
(qw
/cat-file commit/, $c->{c
});
3262 shift @log while ($log[0] ne "\n");
3264 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
3266 (undef, $c->{r
}, undef) = ::extract_metadata
(
3267 (grep(/^git-svn-id: /, @log))[-1]);
3269 return defined $c->{r
};
3275 $dcvar = 'color.diff';
3276 $dc = `git-config --get $dcvar`;
3278 # nothing at all; fallback to "diff.color"
3279 $dcvar = 'diff.color';
3280 $dc = `git-config --get $dcvar`;
3283 if ($dc eq 'auto') {
3285 $pc = `git-config --get color.pager`;
3287 # does not have it -- fallback to pager.color
3288 $pc = `git-config --bool --get pager.color`;
3291 $pc = `git-config --bool --get color.pager`;
3297 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
3298 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
3302 return 0 if $dc eq 'never';
3303 return 1 if $dc eq 'always';
3304 chomp($dc = `git-config --bool --get $dcvar`);
3305 return ($dc eq 'true');
3308 sub git_svn_log_cmd
{
3309 my ($r_min, $r_max, @args) = @_;
3311 foreach my $x (@args) {
3313 next unless ::verify_ref
("$x^0");
3318 my $url = (::working_head_info
($head))[0];
3319 my $gs = Git
::SVN
->find_by_url($url) || Git
::SVN
->_new;
3320 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
3322 push @cmd, '-r' unless $non_recursive;
3323 push @cmd, qw
/--raw --name-status/ if $verbose;
3324 push @cmd, '--color' if log_use_color
();
3325 return @cmd unless defined $r_max;
3326 if ($r_max == $r_min) {
3327 push @cmd, '--max-count=1';
3328 if (my $c = $gs->rev_db_get($r_max)) {
3332 my ($c_min, $c_max);
3333 $c_max = $gs->rev_db_get($r_max);
3334 $c_min = $gs->rev_db_get($r_min);
3335 if (defined $c_min && defined $c_max) {
3336 if ($r_max > $r_max) {
3337 push @cmd, "$c_min..$c_max";
3339 push @cmd, "$c_max..$c_min";
3341 } elsif ($r_max > $r_min) {
3350 # adapted from pager.c
3352 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
3353 if (!defined $pager) {
3355 } elsif (length $pager == 0 || $pager eq 'cat') {
3361 return unless -t
*STDOUT
;
3362 pipe my $rfd, my $wfd or return;
3363 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
3365 open STDOUT
, '>&', $wfd or
3366 ::fatal
"Can't redirect to stdout: $!\n";
3369 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
3370 $ENV{LESS
} ||= 'FRSX';
3371 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
3374 sub tz_to_s_offset
{
3377 return ($1 * 60) + ($tz * 3600);
3380 sub get_author_info
{
3381 my ($dest, $author, $t, $tz) = @_;
3382 $author =~ s/(?:^\s*|\s*$)//g;
3383 $dest->{a_raw
} = $author;
3386 $au = $rusers{$author} || undef;
3389 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3394 # Date::Parse isn't in the standard Perl distro :(
3395 if ($tz =~ s/^\+//) {
3396 $t += tz_to_s_offset
($tz);
3397 } elsif ($tz =~ s/^\-//) {
3398 $t -= tz_to_s_offset
($tz);
3400 $dest->{t_utc
} = $t;
3403 sub process_commit
{
3404 my ($c, $r_min, $r_max, $defer) = @_;
3405 if (defined $r_min && defined $r_max) {
3406 if ($r_min == $c->{r
} && $r_min == $r_max) {
3410 return 1 if $r_min == $r_max;
3411 if ($r_min < $r_max) {
3412 # we need to reverse the print order
3413 return 0 if (defined $limit && --$limit < 0);
3417 if ($r_min != $r_max) {
3418 return 1 if ($r_min < $c->{r
});
3419 return 1 if ($r_max > $c->{r
});
3422 return 0 if (defined $limit && --$limit < 0);
3431 if (my $l = $c->{l
}) {
3432 while ($l->[0] =~ /^\s*$/) { shift @
$l }
3435 $l_fmt ||= 'A' . length($c->{r
});
3436 print 'r',pack($l_fmt, $c->{r
}),' | ';
3437 print "$c->{c} | " if $show_commit;
3440 show_commit_normal
($c);
3444 sub show_commit_changed_paths
{
3446 return unless $c->{changed
};
3447 print "Changed paths:\n", @
{$c->{changed
}};
3450 sub show_commit_normal
{
3452 print '-' x72
, "\nr$c->{r} | ";
3453 print "$c->{c} | " if $show_commit;
3454 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3455 localtime($c->{t_utc
})), ' | ';
3458 if (my $l = $c->{l
}) {
3459 while ($l->[$#$l] eq "\n" && $#$l > 0
3460 && $l->[($#$l - 1)] eq "\n") {
3463 $nr_line = scalar @
$l;
3465 print "1 line\n\n\n";
3467 if ($nr_line == 1) {
3468 $nr_line = '1 line';
3470 $nr_line .= ' lines';
3472 print $nr_line, "\n";
3473 show_commit_changed_paths
($c);
3475 print $_ foreach @
$l;
3479 show_commit_changed_paths
($c);
3483 foreach my $x (qw
/raw stat diff/) {
3486 print $_ foreach @
{$c->{$x}}
3493 my ($r_min, $r_max);
3494 my $r_last = -1; # prevent dupes
3500 if (defined $::_revision
) {
3501 if ($::_revision
=~ /^(\d+):(\d+)$/) {
3502 ($r_min, $r_max) = ($1, $2);
3503 } elsif ($::_revision
=~ /^\d+$/) {
3504 $r_min = $r_max = $::_revision
;
3506 ::fatal
"-r$::_revision is not supported, use ",
3507 "standard \'git log\' arguments instead\n";
3512 @args = (git_svn_log_cmd
($r_min, $r_max, @args), @args);
3513 my $log = command_output_pipe
(@args);
3515 my (@k, $c, $d, $stat);
3516 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3518 if (/^${esc_color}commit ($::sha1_short)/o) {
3520 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
3522 process_commit
($c, $r_min, $r_max, \
@k) or
3527 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3528 get_author_info
($c, $1, $2, $3);
3529 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3531 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3532 push @
{$c->{raw
}}, $_;
3533 } elsif (/^${esc_color}[ACRMDT]\t/) {
3534 # we could add $SVN->{svn_path} here, but that requires
3535 # remote access at the moment (repo_path_split)...
3536 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3537 push @
{$c->{changed
}}, $_;
3538 } elsif (/^${esc_color}diff /o) {
3540 push @
{$c->{diff
}}, $_;
3542 push @
{$c->{diff
}}, $_;
3543 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
3544 $esc_color*[\
+\
-]*$esc_color$/x
) {
3546 push @
{$c->{stat}}, $_;
3547 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3548 push @
{$c->{stat}}, $_;
3550 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3551 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
3552 } elsif (s/^${esc_color} //o) {
3553 push @
{$c->{l
}}, $_;
3556 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
3558 process_commit
($c, $r_min, $r_max, \
@k);
3564 process_commit
($_, $r_min, $r_max) foreach reverse @k;
3568 print '-' x72
,"\n" unless $incremental || $oneline;
3571 package Git
::SVN
::Migration
;
3572 # these version numbers do NOT correspond to actual version numbers
3573 # of git nor git-svn. They are just relative.
3575 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3577 # v1 layout: .git/$id/info/url, refs/remotes/$id
3579 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3581 # v3 layout: .git/svn/$id, refs/remotes/$id
3582 # - info/url may remain for backwards compatibility
3583 # - this is what we migrate up to this layout automatically,
3584 # - this will be used by git svn init on single branches
3585 # v3.1 layout (auto migrated):
3586 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3587 # for backwards compatibility
3589 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3590 # - this is only created for newly multi-init-ed
3591 # repositories. Similar in spirit to the
3592 # --use-separate-remotes option in git-clone (now default)
3593 # - we do not automatically migrate to this (following
3594 # the example set by core git)
3598 use File
::Path qw
/mkpath/;
3599 use File
::Basename qw
/dirname basename/;
3600 use vars qw
/$_minimize/;
3602 sub migrate_from_v0
{
3603 my $git_dir = $ENV{GIT_DIR
};
3604 return undef unless -d
$git_dir;
3605 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3609 my ($id, $orig_ref) = ($_, $_);
3610 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
3611 next unless -f
"$git_dir/$id/info/url";
3612 my $new_ref = "refs/remotes/$id";
3613 if (::verify_ref
("$new_ref^0")) {
3614 print STDERR
"W: $orig_ref is probably an old ",
3615 "branch used by an ancient version of ",
3617 "However, $new_ref also exists.\n",
3618 "We will not be able ",
3619 "to use this branch until this ",
3620 "ambiguity is resolved.\n";
3623 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
3624 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
3625 command_noisy
('update-ref', $new_ref, $orig_ref);
3626 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
3629 command_close_pipe
($fh, $ctx);
3630 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
3634 sub migrate_from_v1
{
3635 my $git_dir = $ENV{GIT_DIR
};
3637 return $migrated unless -d
$git_dir;
3638 my $svn_dir = "$git_dir/svn";
3640 # just in case somebody used 'svn' as their $id at some point...
3641 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
3643 print STDERR
"Migrating from a git-svn v1 layout...\n";
3645 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
3646 "$svn_dir\n\t(required for this version ",
3647 "($::VERSION) of git-svn) does not. exist\n";
3648 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3651 next unless $x =~ s
#^refs/remotes/##;
3653 next unless -f
"$git_dir/$x/info/url";
3654 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
3656 my $dn = dirname
("$git_dir/svn/$x");
3657 mkpath
([$dn]) unless -d
$dn;
3658 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3659 mkpath
(["$git_dir/svn/svn"]);
3660 print STDERR
" - $git_dir/$x/info => ",
3661 "$git_dir/svn/$x/info\n";
3662 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3664 # don't worry too much about these, they probably
3665 # don't exist with repos this old (save for index,
3666 # and we can easily regenerate that)
3667 foreach my $f (qw
/unhandled.log index .rev_db/) {
3668 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3671 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
3672 rename "$git_dir/$x", "$git_dir/svn/$x" or
3677 command_close_pipe
($fh, $ctx);
3678 print STDERR
"Done migrating from a git-svn v1 layout\n";
3683 my ($l_map, $pfx, $path) = @_;
3685 foreach (<$path/*>) {
3686 if (-r
"$_/info/url") {
3687 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
3688 my $ref_id = $pfx . basename
$_;
3689 my $url = ::file_to_s
("$_/info/url");
3690 $l_map->{$ref_id} = $url;
3697 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3698 read_old_urls
($l_map, $x, $_);
3702 sub migrate_from_v2
{
3703 my @cfg = command
(qw
/config -l/);
3704 return if grep /^svn-remote\..+\.url=/, @cfg;
3706 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
3709 foreach my $ref_id (sort keys %l_map) {
3710 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
3712 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3719 sub minimize_connections
{
3720 my $r = Git
::SVN
::read_all_remotes
();
3722 my $root_repos = {};
3723 foreach my $repo_id (keys %$r) {
3724 my $url = $r->{$repo_id}->{url
} or next;
3725 my $fetch = $r->{$repo_id}->{fetch
} or next;
3726 my $ra = Git
::SVN
::Ra
->new($url);
3728 # skip existing cases where we already connect to the root
3729 if (($ra->{url
} eq $ra->{repos_root
}) ||
3730 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
3732 $root_repos->{$ra->{url
}} = $repo_id;
3736 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
3737 my $root_path = $ra->{url
};
3738 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
3739 foreach my $path (keys %$fetch) {
3740 my $ref_id = $fetch->{$path};
3741 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
3743 # make sure we can read when connecting to
3744 # a higher level of a repository
3745 my ($last_rev, undef) = $gs->last_rev_commit;
3746 if (!defined $last_rev) {
3748 $root_ra->get_latest_revnum;
3752 my $new = $root_path;
3753 $new .= length $path ?
"/$path" : '';
3755 $root_ra->get_log([$new], $last_rev, $last_rev,
3759 $new_urls->{$ra->{repos_root
}}->{$new} =
3760 { ref_id
=> $ref_id,
3761 old_repo_id
=> $repo_id,
3762 old_path
=> $path };
3767 foreach my $url (keys %$new_urls) {
3768 # see if we can re-use an existing [svn-remote "repo_id"]
3769 # instead of creating a(n ugly) new section:
3770 my $repo_id = $root_repos->{$url} ||
3771 Git
::SVN
::sanitize_remote_name
($url);
3773 my $fetch = $new_urls->{$url};
3774 foreach my $path (keys %$fetch) {
3775 my $x = $fetch->{$path};
3776 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
3777 my $pfx = "svn-remote.$x->{old_repo_id}";
3779 my $old_fetch = quotemeta("$x->{old_path}:".
3780 "refs/remotes/$x->{ref_id}");
3781 command_noisy
(qw
/config --unset/,
3782 "$pfx.fetch", '^'. $old_fetch . '$');
3783 delete $r->{$x->{old_repo_id
}}->
3784 {fetch
}->{$x->{old_path
}};
3785 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
3786 command_noisy
(qw
/config --unset/,
3788 push @emptied, $x->{old_repo_id
}
3793 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
3794 "$ENV{GIT_DIR}/config";
3796 The following [svn-remote] sections in your config file ($file) are empty
3797 and can be safely removed:
3799 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
3803 sub migration_check
{
3807 minimize_connections
() if $_minimize;
3810 package Git
::IndexInfo
;
3813 use Git qw
/command_input_pipe command_close_pipe/;
3817 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
3818 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
3822 my ($self, $path) = @_;
3823 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
3824 return ++$self->{nr
};
3830 my ($self, $mode, $hash, $path) = @_;
3831 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
3832 return ++$self->{nr
};
3839 command_close_pipe
($self->{gui
}, $self->{ctx
});
3842 package Git
::SVN
::GlobSpec
;
3847 my ($class, $glob) = @_;
3849 $re =~ s!/+$!!g; # no need for trailing slashes
3850 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3851 my ($left, $right) = ($1, $2);
3853 die "Only one '*' wildcard expansion ",
3854 "is supported (got $nr): '$glob'\n";
3855 } elsif ($nr == 0) {
3856 die "One '*' is needed for glob: '$glob'\n";
3858 $re = quotemeta($left) . $re . quotemeta($right);
3859 if (length $left && !($left =~ s!/+$!!g)) {
3860 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3862 if (length $right && !($right =~ s!^/+!!g)) {
3863 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3865 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
3866 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
3867 regex
=> qr/$re/, glob => $glob }, $class;
3871 my ($self, $path) = @_;
3872 return (length $self->{left
} ?
"$self->{left}/" : '') .
3873 $path . (length $self->{right
} ?
"/$self->{right}" : '');
3881 $remotes = { # returned by read_all_remotes()
3883 # svn-remote.svn.url=https://svn.musicpd.org
3884 url
=> 'https://svn.musicpd.org',
3885 # svn-remote.svn.fetch=mpd/trunk:trunk
3887 'mpd/trunk' => 'trunk',
3889 # svn-remote.svn.tags=mpd/tags/*:tags/*
3894 regex
=> qr!mpd/tags/([^/]+)$!,
3900 regex
=> qr!tags/([^/]+)$!,
3907 $log_entry hashref as returned by libsvn_log_entry
()
3909 log => 'whitespace-formatted log entry
3910 ', # trailing newline is preserved
3911 revision
=> '8', # integer
3912 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3913 author
=> 'committer name'
3917 # this is generated by generate_diff();
3918 @mods = array of diff
-index line hashes
, each element represents one line
3919 of diff
-index output
3921 diff
-index line
($m hash
)
3923 mode_a
=> first column of diff
-index output
, no leading
':',
3924 mode_b
=> second column of diff
-index output
,
3925 sha1_b
=> sha1sum of the final blob
,
3926 chg
=> change type
[MCRADT
],
3927 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3928 file_b
=> new
/current file name of a file
(any chg
)
3932 # retval of read_url_paths{,_all}();
3934 # repository root url
3935 'https://svn.musicpd.org' => {
3936 # repository path # GIT_SVN_ID
3937 'mpd/trunk' => 'trunk',
3938 'mpd/tags/0.11.5' => 'tags/0.11.5',
3943 I don
't trust the each() function on unless I created %hash myself
3944 because the internal iterator may not have started at base.