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/;
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 Getopt
::Long
::Configure
('pass_through') if $cmd eq 'log';
172 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
173 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
174 'id|i=s' => \
$Git::SVN
::default_ref_id
,
175 'svn-remote|remote|R=s' => sub {
176 $Git::SVN
::no_reuse_existing
= 1;
177 $Git::SVN
::default_repo_id
= $_[1] });
178 exit 1 if (!$rv && $cmd ne 'log');
181 version
() if $_version;
182 usage
(1) unless defined $cmd;
183 load_authors
() if $_authors;
185 # make sure we're always running
186 unless ($cmd =~ /(?:clone|init|multi-init)$/) {
187 unless (-d
$ENV{GIT_DIR
}) {
188 if ($git_dir_user_set) {
189 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
190 "but it is not a directory\n";
192 my $git_dir = delete $ENV{GIT_DIR
};
193 chomp(my $cdup = command_oneline
(qw
/rev-parse --show-cdup/));
194 unless (length $cdup) {
195 die "Already at toplevel, but $git_dir ",
196 "not found '$cdup'\n";
198 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
199 unless (-d
$git_dir) {
200 die "$git_dir still not found after going to ",
203 $ENV{GIT_DIR
} = $git_dir;
206 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
207 Git
::SVN
::Migration
::migration_check
();
209 Git
::SVN
::init_vars
();
211 Git
::SVN
::verify_remotes_sanity
();
212 $cmd{$cmd}->[0]->(@ARGV);
215 post_fetch_checkout
();
218 ####################### primary functions ######################
220 my $exit = shift || 0;
221 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
223 git
-svn
- bidirectional operations between a single Subversion tree
and git
224 Usage
: $0 <command
> [options
] [arguments
]\n
226 print $fd "Available commands:\n" unless $cmd;
228 foreach (sort keys %cmd) {
229 next if $cmd && $cmd ne $_;
230 next if /^multi-/; # don't show deprecated commands
231 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
232 foreach (keys %{$cmd{$_}->[2]}) {
233 # mixed-case options are for .git/config only
234 next if /[A-Z]/ && /^[a-z]+$/i;
235 # prints out arguments as they should be passed:
236 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
237 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
239 split /\|/,$_)," $x\n";
243 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
244 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
245 one git repository and want to keep them separate. See git-svn(1) for more
252 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
257 unless (-d $ENV{GIT_DIR}) {
258 my @init_db = ('init
');
259 push @init_db, "--template=$_template" if defined $_template;
260 if (defined $_shared) {
261 if ($_shared =~ /[a-z]/) {
262 push @init_db, "--shared=$_shared";
264 push @init_db, "--shared";
267 command_noisy(@init_db);
270 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
271 foreach my $i (keys %icv) {
272 die "'$set' and '$i' cannot both be set\n" if $set;
273 next unless defined $icv{$i};
274 command_noisy('config
', "$pfx.$i", $icv{$i});
280 my $repo_path = shift or return;
281 mkpath([$repo_path]) unless -d $repo_path;
282 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
283 $ENV{GIT_DIR} = '.git';
287 my ($url, $path) = @_;
288 if (!defined $path &&
289 (defined $_trunk || defined $_branches || defined $_tags) &&
290 $url !~ m#^[a-z\+]+://#) {
293 $path = basename($url) if !defined $path || !length $path;
294 cmd_init($url, $path);
295 Git::SVN::fetch_all($Git::SVN::default_repo_id);
299 if (defined $_trunk || defined $_branches || defined $_tags) {
300 return cmd_multi_init(@_);
302 my $url = shift or die "SVN repository location required
",
303 "as a command
-line argument
\n";
307 Git::SVN->init($url);
311 if (grep /^\d+=./, @_) {
312 die "'<rev>=<commit>' fetch arguments are
",
313 "no longer supported
.\n";
317 die "Usage
: $0 fetch
[--all
] [svn
-remote
]\n";
319 $remote ||= $Git::SVN::default_repo_id;
323 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
329 if ($_stdin || !@commits) {
330 print "Reading from stdin
...\n";
333 if (/\b($sha1_short)\b/o) {
334 unshift @commits, $1;
339 foreach my $c (@commits) {
340 my @tmp = command('rev-parse',$c);
341 if (scalar @tmp == 1) {
343 } elsif (scalar @tmp > 1) {
344 push @revs, reverse(command('rev-list',@tmp));
346 fatal "Failed to rev
-parse
$c\n";
349 my $gs = Git::SVN->new;
350 my ($r_last, $cmt_last) = $gs->last_rev_commit;
352 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
353 fatal "There are new revisions that were fetched
",
354 "and need to be merged
(or acknowledged
) ",
355 "before committing
.\nlast rev
: $r_last\n",
356 " current
: $gs->{last_rev
}\n";
358 $gs->set_tree($_) foreach @revs;
359 print "Done committing
",scalar @revs," revisions to SVN
\n";
366 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
368 die "Unable to determine upstream SVN information from
",
373 foreach my $d (@refs) {
374 if (!verify_ref("$d~1")) {
376 "has
no parent commit
, and therefore
",
377 "nothing to diff against
.\n",
378 "You should be working from a repository
",
379 "originally created by git
-svn
\n";
381 unless (defined $last_rev) {
382 (undef, $last_rev, undef) = cmt_metadata("$d~1");
383 unless (defined $last_rev) {
384 fatal "Unable to extract revision information
",
385 "from commit
$d~1\n";
389 print "diff
-tree
$d~1 $d\n";
391 my %ed_opts = ( r => $last_rev,
392 log => get_commit_entry($d)->{log},
393 ra => Git::SVN::Ra->new($url),
397 print "Committed r
$_[0]\n";
398 $last_rev = $_[0]; },
400 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
401 print "No changes
\n$d~1 == $d\n";
407 warn "Could
not determine fetch information
for $url\n",
408 "Will
not attempt to fetch
and rebase commits
.\n",
409 "This probably means you have useSvmProps
and should
\n",
410 "now resync your SVN
::Mirror repository
.\n";
413 $_fetch_all ? $gs->fetch_all : $gs->fetch;
414 # we always want to rebase against the current HEAD, not any
415 # head that was passed to us
416 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
419 @finish = rebase_cmd();
420 print STDERR "W
: HEAD
and ", $gs->refname, " differ
, ",
421 "using
@finish:\n", "@diff";
423 print "No changes between current HEAD
and ",
424 $gs->refname, "\nResetting to the latest
",
426 @finish = qw/reset --mixed/;
428 command_noisy(@finish, $gs->refname);
432 command_noisy(qw/update-index --refresh/);
433 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
435 die "Unable to determine upstream SVN information from
",
436 "working tree history
\n";
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, $rev, $uuid, $gs) = working_head_info('HEAD');
451 $gs ||= 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 ($fh, $ctx) = command_output_pipe
('rev-list', $head);
776 my ($url, $rev, $uuid) = cmt_metadata
($_);
777 if (defined $url && defined $rev) {
778 if (my $gs = Git
::SVN
->find_by_url($url)) {
779 my $c = $gs->rev_db_get($rev);
780 if ($c && $c eq $_) {
781 close $fh; # break the pipe
782 return ($url, $rev, $uuid, $gs);
786 unshift @
$refs, $_ if $refs;
788 command_close_pipe
($fh, $ctx);
789 (undef, undef, undef, undef);
795 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
796 $_repack $_repack_flags $_use_svm_props $_head
797 $_use_svnsync_props $no_reuse_existing/;
799 use File
::Path qw
/mkpath/;
800 use File
::Copy qw
/copy/;
804 # properties that we do not log:
807 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
808 svn
:special svn
:executable
809 svn
:entry
:committed
-rev
810 svn
:entry
:last-author
812 svn
:entry
:committed
-date
/;
814 # some options are read globally, but can be overridden locally
815 # per [svn-remote "..."] section. Command-line options will *NOT*
816 # override options set in an [svn-remote "..."] section
818 foreach (qw
/follow_parent no_metadata use_svm_props
819 use_svnsync_props
/) {
824 return \$self->{-$_} if exists \$self->{-$_};
825 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
826 eval { command_oneline(qw/config --get/, \$k) };
828 \$self->{-$_} = \$Git::SVN::_$_;
830 my \$v = command_oneline(qw/config --bool/,\$k);
831 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
833 return \$self->{-$_} }\n";
840 END { unlink keys %LOCKFILES if %LOCKFILES }
842 sub resolve_local_globs
{
843 my ($url, $fetch, $glob_spec) = @_;
844 return unless defined $glob_spec;
845 my $ref = $glob_spec->{ref};
846 my $path = $glob_spec->{path
};
847 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
848 next unless m
#^refs/remotes/$ref->{regex}$#;
850 my $pathname = $path->full_path($p);
851 my $refname = $ref->full_path($p);
852 if (my $existing = $fetch->{$pathname}) {
853 if ($existing ne $refname) {
854 die "Refspec conflict:\n",
855 "existing: refs/remotes/$existing\n",
856 " globbed: refs/remotes/$refname\n";
858 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
859 $u =~ s!^\Q$url\E(/|$)!! or die
860 "refs/remotes/$refname: '$url' not found in '$u'\n";
861 if ($pathname ne $u) {
862 warn "W: Refspec glob conflict ",
863 "(ref: refs/remotes/$refname):\n",
864 "expected path: $pathname\n",
866 "Continuing ahead with $u\n";
870 $fetch->{$pathname} = $refname;
875 sub parse_revision_argument
{
876 my ($base, $head) = @_;
877 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
878 return ($base, $head);
880 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
881 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
882 return ($head, $head) if ($::_revision
eq 'HEAD');
883 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
884 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
885 die "revision argument: $::_revision not understood by git-svn\n";
889 my ($repo_id, $remotes) = @_;
893 $repo_id = $gs->{repo_id
};
895 $remotes ||= read_all_remotes
();
896 my $remote = $remotes->{$repo_id} or
897 die "[svn-remote \"$repo_id\"] unknown\n";
898 my $fetch = $remote->{fetch
};
899 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
901 my $ra = Git
::SVN
::Ra
->new($url);
902 my $uuid = $ra->get_uuid;
903 my $head = $ra->get_latest_revnum;
904 my $base = defined $fetch ?
$head : 0;
906 # read the max revs for wildcard expansion (branches/*, tags/*)
907 foreach my $t (qw
/branches tags/) {
908 defined $remote->{$t} or next;
909 push @globs, $remote->{$t};
910 my $max_rev = eval { tmp_config
(qw
/--int --get/,
911 "svn-remote.$repo_id.${t}-maxRev") };
912 if (defined $max_rev && ($max_rev < $base)) {
914 } elsif (!defined $max_rev) {
920 foreach my $p (sort keys %$fetch) {
921 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
922 my $lr = $gs->rev_db_max;
924 $base = $lr if ($lr < $base);
930 ($base, $head) = parse_revision_argument
($base, $head);
931 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
934 sub read_all_remotes
{
936 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
937 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
938 $r->{$1}->{fetch
}->{$2} = $3;
939 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
940 $r->{$1}->{url
} = $2;
941 } elsif (m
!^(.+)\
.(branches
|tags
)=
942 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
943 my ($p, $g) = ($3, $4);
944 my $rs = $r->{$1}->{$2} = {
947 path
=> Git
::SVN
::GlobSpec
->new($p),
948 ref => Git
::SVN
::GlobSpec
->new($g) };
949 if (length($rs->{ref}->{right
}) != 0) {
950 die "The '*' glob character must be the last ",
951 "character of '$g'\n";
959 if (defined $_repack) {
960 $_repack = 1000 if ($_repack <= 0);
961 $_repack_nr = $_repack;
962 $_repack_flags ||= '-d';
966 sub verify_remotes_sanity
{
967 return unless -d
$ENV{GIT_DIR
};
969 foreach (command
(qw
/config -l/)) {
970 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
972 die "Remote ref refs/remote/$1 is tracked by",
973 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
974 "Please resolve this ambiguity in ",
975 "your git configuration file before ",
983 # we allow more chars than remotes2config.sh...
984 sub sanitize_remote_name
{
986 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
990 sub find_existing_remote
{
991 my ($url, $remotes) = @_;
992 return undef if $no_reuse_existing;
994 foreach my $repo_id (keys %$remotes) {
995 my $u = $remotes->{$repo_id}->{url
} or next;
997 $existing = $repo_id;
1003 sub init_remote_config
{
1004 my ($self, $url, $no_write) = @_;
1005 $url =~ s!/+$!!; # strip trailing slash
1006 my $r = read_all_remotes
();
1007 my $existing = find_existing_remote
($url, $r);
1009 unless ($no_write) {
1010 print STDERR
"Using existing ",
1011 "[svn-remote \"$existing\"]\n";
1013 $self->{repo_id
} = $existing;
1015 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1016 $existing = find_existing_remote
($min_url, $r);
1018 unless ($no_write) {
1019 print STDERR
"Using existing ",
1020 "[svn-remote \"$existing\"]\n";
1022 $self->{repo_id
} = $existing;
1024 if ($min_url ne $url) {
1025 unless ($no_write) {
1026 print STDERR
"Using higher level of URL: ",
1027 "$url => $min_url\n";
1029 my $old_path = $self->{path
};
1030 $self->{path
} = $url;
1031 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1032 if (length $old_path) {
1033 $self->{path
} .= "/$old_path";
1040 # verify that we aren't overwriting anything:
1042 command_oneline
('config', '--get',
1043 "svn-remote.$self->{repo_id}.url")
1045 if ($orig_url && ($orig_url ne $url)) {
1046 die "svn-remote.$self->{repo_id}.url already set: ",
1047 "$orig_url\nwanted to set to: $url\n";
1050 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1051 if (defined $xpath) {
1052 die "svn-remote.$xrepo_id.fetch already set to track ",
1053 "$xpath:refs/remotes/", $self->refname, "\n";
1055 unless ($no_write) {
1056 command_noisy
('config',
1057 "svn-remote.$self->{repo_id}.url", $url);
1058 command_noisy
('config', '--add',
1059 "svn-remote.$self->{repo_id}.fetch",
1060 "$self->{path}:".$self->refname);
1062 $self->{url
} = $url;
1065 sub find_by_url
{ # repos_root and, path are optional
1066 my ($class, $full_url, $repos_root, $path) = @_;
1067 return undef unless defined $full_url;
1068 my $remotes = read_all_remotes
();
1069 if (defined $full_url && defined $repos_root && !defined $path) {
1071 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1073 foreach my $repo_id (keys %$remotes) {
1074 my $u = $remotes->{$repo_id}->{url
} or next;
1075 next if defined $repos_root && $repos_root ne $u;
1077 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1078 foreach (qw
/branches tags/) {
1079 resolve_local_globs
($u, $fetch,
1080 $remotes->{$repo_id}->{$_});
1083 unless (defined $p) {
1085 $p =~ s
#^\Q$u\E(?:/|$)## or next;
1087 foreach my $f (keys %$fetch) {
1089 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1096 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1097 my $self = _new
($class, $repo_id, $ref_id, $path);
1099 $self->init_remote_config($url, $no_write);
1106 foreach (command
(qw
/config -l/)) {
1107 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1108 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1109 my ($repo_id, $path, $ref) = ($1, $2, $3);
1110 if ($ref eq $ref_id) {
1111 $path = '' if ($path =~ m
#^\./?#);
1112 return ($repo_id, $path);
1115 (undef, undef, undef);
1119 my ($class, $ref_id, $repo_id, $path) = @_;
1120 if (defined $ref_id && !defined $repo_id && !defined $path) {
1121 ($repo_id, $path) = find_ref
($ref_id);
1122 if (!defined $repo_id) {
1123 die "Could not find a \"svn-remote.*.fetch\" key ",
1124 "in the repository configuration matching: ",
1125 "refs/remotes/$ref_id\n";
1128 my $self = _new
($class, $repo_id, $ref_id, $path);
1129 if (!defined $self->{path
} || !length $self->{path
}) {
1130 my $fetch = command_oneline
('config', '--get',
1131 "svn-remote.$repo_id.fetch",
1132 ":refs/remotes/$ref_id\$") or
1133 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1134 "\":refs/remotes/$ref_id\$\" in config\n";
1135 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1137 $self->{url
} = command_oneline
('config', '--get',
1138 "svn-remote.$repo_id.url") or
1139 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1144 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
1148 return $self->{svm
}->{uuid
} if $self->svm;
1150 unless ($self->{svm
}) {
1151 die "SVM UUID not cached, and reading remotely failed\n";
1153 $self->{svm
}->{uuid
};
1158 return $self->{svm
} if $self->{svm
};
1160 # see if we have it in our config, first:
1162 my $section = "svn-remote.$self->{repo_id}";
1164 source
=> tmp_config
('--get', "$section.svm-source"),
1165 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1166 replace
=> tmp_config
('--get', "$section.svm-replace"),
1169 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1170 $self->{svm
} = $svm;
1176 my ($self, $ra) = @_;
1177 return $ra if $self->svm;
1179 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1180 "(svm:source, svm:uuid) ",
1181 "from the following URLs:\n" );
1182 sub read_svm_props
{
1183 my ($self, $ra, $path, $r) = @_;
1184 my $props = ($ra->get_dir($path, $r))[2];
1185 my $src = $props->{'svm:source'};
1186 my $uuid = $props->{'svm:uuid'};
1187 return undef if (!$src || !$uuid);
1191 $uuid =~ m{^[0-9a-f\-]{30,}$}
1192 or die "doesn't look right - svm:uuid is '$uuid'\n";
1194 # the '!' is used to mark the repos_root!/relative/path
1195 $src =~ s{/?!/?}{/};
1196 $src =~ s{/+$}{}; # no trailing slashes please
1197 # username is of no interest
1198 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1200 my $replace = $ra->{url
};
1201 $replace .= "/$path" if length $path;
1203 my $section = "svn-remote.$self->{repo_id}";
1204 tmp_config
("$section.svm-source", $src);
1205 tmp_config
("$section.svm-replace", $replace);
1206 tmp_config
("$section.svm-uuid", $uuid);
1214 my $r = $ra->get_latest_revnum;
1215 my $path = $self->{path
};
1217 while (length $path) {
1218 unless ($tried{"$self->{url}/$path"}) {
1219 return $ra if $self->read_svm_props($ra, $path, $r);
1220 $tried{"$self->{url}/$path"} = 1;
1222 $path =~ s
#/?[^/]+$##;
1224 die "Path: '$path' should be ''\n" if $path ne '';
1225 return $ra if $self->read_svm_props($ra, $path, $r);
1226 $tried{"$self->{url}/$path"} = 1;
1228 if ($ra->{repos_root
} eq $self->{url
}) {
1229 die @err, (map { " $_\n" } keys %tried), "\n";
1232 # nope, make sure we're connected to the repository root:
1235 $path = $ra->{svn_path
};
1236 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1237 while (length $path) {
1238 unless ($tried{"$ra->{url}/$path"}) {
1239 $ok = $self->read_svm_props($ra, $path, $r);
1241 $tried{"$ra->{url}/$path"} = 1;
1243 $path =~ s
#/?[^/]+$##;
1245 die "Path: '$path' should be ''\n" if $path ne '';
1246 $ok ||= $self->read_svm_props($ra, $path, $r);
1247 $tried{"$ra->{url}/$path"} = 1;
1249 die @err, (map { " $_\n" } keys %tried), "\n";
1251 Git
::SVN
::Ra
->new($self->{url
});
1256 return $self->{svnsync
} if $self->{svnsync
};
1258 if ($self->no_metadata) {
1259 die "Can't have both 'noMetadata' and ",
1260 "'useSvnsyncProps' options set!\n";
1262 if ($self->rewrite_root) {
1263 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1268 # see if we have it in our config, first:
1270 my $section = "svn-remote.$self->{repo_id}";
1272 url
=> tmp_config
('--get', "$section.svnsync-url"),
1273 uuid
=> tmp_config
('--get', "$section.svnsync-uuid"),
1276 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1277 return $self->{svnsync
} = $svnsync;
1280 my $err = "useSvnsyncProps set, but failed to read " .
1281 "svnsync property: svn:sync-from-";
1282 my $rp = $self->ra->rev_proplist(0);
1284 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1285 $url =~ m{^[a-z\+]+://} or
1286 die "doesn't look right - svn:sync-from-url is '$url'\n";
1288 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1289 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1290 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1292 my $section = "svn-remote.$self->{repo_id}";
1293 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1294 tmp_config
('--add', "$section.svnsync-url", $url);
1295 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1298 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1299 # remote lookup (useful for 'git svn log').
1302 unless ($self->{ra_uuid
}) {
1303 my $key = "svn-remote.$self->{repo_id}.uuid";
1304 my $uuid = eval { tmp_config
('--get', $key) };
1305 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1306 $self->{ra_uuid
} = $uuid;
1308 die "ra_uuid called without URL\n" unless $self->{url
};
1309 $self->{ra_uuid
} = $self->ra->get_uuid;
1310 tmp_config
('--add', $key, $self->{ra_uuid
});
1318 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1319 if ($self->use_svm_props && !$self->{svm
}) {
1320 if ($self->no_metadata) {
1321 die "Can't have both 'noMetadata' and ",
1322 "'useSvmProps' options set!\n";
1323 } elsif ($self->use_svnsync_props) {
1324 die "Can't have both 'useSvnsyncProps' and ",
1325 "'useSvmProps' options set!\n";
1327 $ra = $self->_set_svm_vars($ra);
1328 $self->{-want_revprops
} = 1;
1335 my $repos_root = $self->ra->{repos_root
};
1336 return $self->{path
} if ($self->{url
} eq $repos_root);
1337 my $url = $self->{url
} .
1338 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
1339 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1343 sub traverse_ignore
{
1344 my ($self, $fh, $path, $r) = @_;
1347 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
1349 $p =~ s
#^\Q$self->{path}\E(/|$)##;
1350 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
1351 if (my $s = $props->{'svn:ignore'}) {
1352 $s =~ s/[\r\n]+/\n/g;
1354 if (length $p == 0) {
1358 $s =~ s
#\n#\n/$p/#g;
1359 print $fh "/$p/$s\n";
1362 foreach (sort keys %$dirent) {
1363 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
1364 $self->traverse_ignore($fh, "$path/$_", $r);
1368 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1369 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1371 # returns the newest SVN revision number and newest commit SHA1
1372 sub last_rev_commit
{
1374 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1375 return ($self->{last_rev
}, $self->{last_commit
});
1377 my $c = ::verify_ref
($self->refname.'^0');
1378 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1379 my $rev = (::cmt_metadata
($c))[1];
1381 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1385 my $db_path = $self->db_path;
1386 unless (-e
$db_path) {
1387 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1388 return (undef, undef);
1390 my $offset = -41; # from tail
1392 open my $fh, '<', $db_path or croak
"$db_path not readable: $!\n";
1393 sysseek($fh, $offset, 2); # don't care for errors
1394 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1396 while (('0' x40
) eq $rl && sysseek($fh, 0, 1) != 0) {
1398 sysseek($fh, $offset, 2); # don't care for errors
1399 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1402 if ($c && $c ne $rl) {
1403 die "$db_path and ", $self->refname,
1404 " inconsistent!:\n$c != $rl\n";
1406 my $rev = sysseek($fh, 0, 1) or croak
$!;
1407 $rev = ($rev - 41) / 41;
1408 close $fh or croak
$!;
1409 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1413 sub get_fetch_range
{
1414 my ($self, $min, $max) = @_;
1415 $max ||= $self->ra->get_latest_revnum;
1416 $min ||= $self->rev_db_max;
1422 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1423 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1424 if (-e
$old_def_config && ! -e
$config) {
1425 rename $old_def_config, $config or
1426 die "Failed rename $old_def_config => $config: $!\n";
1428 my $old_config = $ENV{GIT_CONFIG
};
1429 $ENV{GIT_CONFIG
} = $config;
1432 unless (-f
$config) {
1434 open my $fh, '>', $config or
1435 die "Can't open $config: $!\n";
1436 print $fh "; This file is used internally by ",
1438 "Couldn't write to $config: $!\n";
1439 print $fh "; You should not have to edit it\n" or
1440 die "Couldn't write to $config: $!\n";
1441 close $fh or die "Couldn't close $config: $!\n";
1443 command
('config', @args);
1446 if (defined $old_config) {
1447 $ENV{GIT_CONFIG
} = $old_config;
1449 delete $ENV{GIT_CONFIG
};
1452 wantarray ?
@ret : $ret[0];
1456 my ($self, $sub) = @_;
1457 my $old_index = $ENV{GIT_INDEX_FILE
};
1458 $ENV{GIT_INDEX_FILE
} = $self->{index};
1461 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
1462 mkpath
([$dir]) unless -d
$dir;
1466 if (defined $old_index) {
1467 $ENV{GIT_INDEX_FILE
} = $old_index;
1469 delete $ENV{GIT_INDEX_FILE
};
1472 wantarray ?
@ret : $ret[0];
1475 sub assert_index_clean
{
1476 my ($self, $treeish) = @_;
1478 $self->tmp_index_do(sub {
1479 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1480 my $x = command_oneline
('write-tree');
1481 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1482 /^tree ($::sha1)/mo);
1485 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1486 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1487 command_noisy
('read-tree', $treeish);
1488 $x = command_oneline
('write-tree');
1490 ::fatal
"trees ($treeish) $y != $x\n",
1491 "Something is seriously wrong...\n";
1496 sub get_commit_parents
{
1497 my ($self, $log_entry) = @_;
1498 my (%seen, @ret, @tmp);
1499 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1500 if (my $ip = $self->{inject_parents
}) {
1501 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
1505 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1508 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1509 while (my $p = shift @tmp) {
1513 # MAXPARENT is defined to 16 in commit-tree.c:
1517 die "r$log_entry->{revision}: No room for parents:\n\t",
1518 join("\n\t", @tmp), "\n";
1525 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
1526 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1527 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
1530 if ($rwr !~ m
#^[a-z\+]+://#) {
1531 die "$rwr is not a valid URL (key: $k)\n";
1534 $self->{-rewrite_root
} = $rwr;
1539 ($self->rewrite_root || $self->{url
}) .
1540 (length $self->{path
} ?
'/' . $self->{path
} : '');
1545 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
1549 my ($self, $log_entry) = @_;
1550 my $lr = $self->last_rev;
1551 if (defined $lr && $lr >= $log_entry->{revision
}) {
1552 die "Last fetched revision of ", $self->refname,
1553 " was r$lr, but we are about to fetch: ",
1554 "r$log_entry->{revision}!\n";
1556 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1557 croak
"$log_entry->{revision} = $c already exists! ",
1558 "Why are we refetching it?\n";
1560 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $log_entry->{name
};
1561 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
1562 $log_entry->{email
};
1563 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1565 my $tree = $log_entry->{tree
};
1566 if (!defined $tree) {
1567 $tree = $self->tmp_index_do(sub {
1568 command_oneline
('write-tree') });
1570 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1572 my @exec = ('git-commit-tree', $tree);
1573 foreach ($self->get_commit_parents($log_entry)) {
1574 push @exec, '-p', $_;
1576 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1578 print $msg_fh $log_entry->{log} or croak
$!;
1579 unless ($self->no_metadata) {
1580 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1583 $msg_fh->flush == 0 or croak
$!;
1584 close $msg_fh or croak
$!;
1585 chomp(my $commit = do { local $/; <$out_fh> });
1586 close $out_fh or croak
$!;
1589 if ($commit !~ /^$::sha1$/o) {
1590 die "Failed to commit, invalid sha1: $commit\n";
1593 $self->rev_db_set($log_entry->{revision
}, $commit, 1);
1595 $self->{last_rev
} = $log_entry->{revision
};
1596 $self->{last_commit
} = $commit;
1597 print "r$log_entry->{revision}";
1598 if (defined $log_entry->{svm_revision
}) {
1599 print " (\@$log_entry->{svm_revision})";
1600 $self->rev_db_set($log_entry->{svm_revision
}, $commit,
1601 0, $self->svm_uuid);
1603 print " = $commit ($self->{ref_id})\n";
1604 if (defined $_repack && (--$_repack_nr == 0)) {
1605 $_repack_nr = $_repack;
1606 # repack doesn't use any arguments with spaces in them, does it?
1607 print "Running git repack $_repack_flags ...\n";
1608 command_noisy
('repack', split(/\s+/, $_repack_flags));
1609 print "Done repacking\n";
1615 my ($self, $paths, $r) = @_;
1616 return 1 if $self->{path
} eq '';
1617 if (my $path = $paths->{"/$self->{path}"}) {
1618 return ($path->{action
} eq 'D') ?
0 : 1;
1620 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
1621 if (grep /$self->{path_regex}/, keys %$paths) {
1625 foreach (split m
#/#, $self->{path}) {
1627 next unless ($paths->{$c} &&
1628 ($paths->{$c}->{action
} =~ /^[AR]$/));
1629 if ($self->ra->check_path($self->{path
}, $r) ==
1637 sub find_parent_branch
{
1638 my ($self, $paths, $rev) = @_;
1639 return undef unless $self->follow_parent;
1640 unless (defined $paths) {
1641 my $err_handler = $SVN::Error
::handler
;
1642 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
1643 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
1645 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
1646 $SVN::Error
::handler
= $err_handler;
1648 return undef unless defined $paths;
1650 # look for a parent from another branch:
1651 my @b_path_components = split m
#/#, $self->rel_path;
1652 my @a_path_components;
1654 while (@b_path_components) {
1655 $i = $paths->{'/'.join('/', @b_path_components)};
1656 last if $i && defined $i->{copyfrom_path
};
1657 unshift(@a_path_components, pop(@b_path_components));
1659 return undef unless defined $i && defined $i->{copyfrom_path
};
1660 my $branch_from = $i->{copyfrom_path
};
1661 if (@a_path_components) {
1662 print STDERR
"branch_from: $branch_from => ";
1663 $branch_from .= '/'.join('/', @a_path_components);
1664 print STDERR
$branch_from, "\n";
1666 my $r = $i->{copyfrom_rev
};
1667 my $repos_root = $self->ra->{repos_root
};
1668 my $url = $self->ra->{url
};
1669 my $new_url = $repos_root . $branch_from;
1670 print STDERR
"Found possible branch point: ",
1671 "$new_url => ", $self->full_url, ", $r\n";
1672 $branch_from =~ s
#^/##;
1673 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
1675 my $ref_id = $self->{ref_id
};
1676 $ref_id =~ s/\@\d+$//;
1678 # just grow a tail if we're not unique enough :x
1679 $ref_id .= '-' while find_ref
($ref_id);
1680 print STDERR
"Initializing parent: $ref_id\n";
1681 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id, 1);
1683 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1684 if (!defined $r0 || !defined $parent) {
1686 ($r0, $parent) = $gs->last_rev_commit;
1688 if (defined $r0 && defined $parent) {
1689 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1691 if ($self->ra->can_do_switch) {
1692 $self->assert_index_clean($parent);
1693 print STDERR
"Following parent with do_switch\n";
1694 # do_switch works with svn/trunk >= r22312, but that
1695 # is not included with SVN 1.4.3 (the latest version
1696 # at the moment), so we can't rely on it
1697 $self->{last_commit
} = $parent;
1698 $ed = SVN
::Git
::Fetcher
->new($self);
1699 $gs->ra->gs_do_switch($r0, $rev, $gs,
1700 $self->full_url, $ed)
1701 or die "SVN connection failed somewhere...\n";
1703 print STDERR
"Following parent with do_update\n";
1704 $ed = SVN
::Git
::Fetcher
->new($self);
1705 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1706 or die "SVN connection failed somewhere...\n";
1708 print STDERR
"Successfully followed parent\n";
1709 return $self->make_log_entry($rev, [$parent], $ed);
1715 my ($self, $paths, $rev) = @_;
1717 my ($last_rev, @parents);
1718 if (my $lc = $self->last_commit) {
1719 # we can have a branch that was deleted, then re-added
1720 # under the same name but copied from another path, in
1721 # which case we'll have multiple parents (we don't
1722 # want to break the original ref, nor lose copypath info):
1723 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1724 push @
{$log_entry->{parents
}}, $lc;
1727 $ed = SVN
::Git
::Fetcher
->new($self);
1728 $last_rev = $self->{last_rev
};
1733 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1736 $ed = SVN
::Git
::Fetcher
->new($self);
1738 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1739 die "SVN connection failed somewhere...\n";
1741 $self->make_log_entry($rev, \
@parents, $ed);
1745 my ($self, $ed) = @_;
1747 my $h = $ed->{empty
};
1748 foreach (sort keys %$h) {
1749 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1750 push @out, " $act: " . uri_encode
($_);
1751 warn "W: $act: $_\n";
1753 foreach my $t (qw
/dir_prop file_prop/) {
1754 $h = $ed->{$t} or next;
1755 foreach my $path (sort keys %$h) {
1756 my $ppath = $path eq '' ?
'.' : $path;
1757 foreach my $prop (sort keys %{$h->{$path}}) {
1758 next if $SKIP_PROP{$prop};
1759 my $v = $h->{$path}->{$prop};
1760 my $t_ppath_prop = "$t: " .
1761 uri_encode
($ppath) . ' ' .
1764 push @out, " +$t_ppath_prop " .
1767 push @out, " -$t_ppath_prop";
1772 foreach my $t (qw
/absent_file absent_directory/) {
1773 $h = $ed->{$t} or next;
1774 foreach my $parent (sort keys %$h) {
1775 foreach my $path (sort @
{$h->{$parent}}) {
1776 push @out, " $t: " .
1777 uri_encode
("$parent/$path");
1778 warn "W: $t: $parent/$path ",
1779 "Insufficient permissions?\n";
1786 sub parse_svn_date
{
1787 my $date = shift || return '+0000 1970-01-01 00:00:00';
1788 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1789 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1790 croak
"Unable to parse date: $date\n";
1791 "+0000 $Y-$m-$d $H:$M:$S";
1796 if (!defined $author || length $author == 0) {
1797 $author = '(no author)';
1799 if (defined $::_authors
&& ! defined $::users
{$author}) {
1800 die "Author: $author not defined in $::_authors file\n";
1805 sub make_log_entry
{
1806 my ($self, $rev, $parents, $ed) = @_;
1807 my $untracked = $self->get_untracked($ed);
1809 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1810 print $un "r$rev\n" or croak
$!;
1811 print $un $_, "\n" foreach @
$untracked;
1812 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1816 my $logged = delete $self->{logged_rev_props
};
1817 if (!$logged || $self->{-want_revprops
}) {
1818 my $rp = $self->ra->rev_proplist($rev);
1819 foreach (sort keys %$rp) {
1821 if (/^svn:(author|date|log)$/) {
1822 $log_entry{$1} = $v;
1823 } elsif ($_ eq 'svm:headrev') {
1826 print $un " rev_prop: ", uri_encode
($_), ' ',
1827 uri_encode
($v), "\n";
1831 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1833 close $un or croak
$!;
1835 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1836 $log_entry{log} .= "\n";
1837 my $author = $log_entry{author
} = check_author
($log_entry{author
});
1838 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
1840 if (defined $headrev && $self->use_svm_props) {
1841 if ($self->rewrite_root) {
1842 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1845 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
1846 # we don't want "SVM: initializing mirror for junk" ...
1847 return undef if $r == 0;
1848 my $svm = $self->svm;
1849 if ($uuid ne $svm->{uuid
}) {
1850 die "UUID mismatch on SVM path:\n",
1851 "expected: $svm->{uuid}\n",
1854 my $full_url = $self->full_url;
1855 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1856 die "Failed to replace '$svm->{replace}' with ",
1857 "'$svm->{source}' in $full_url\n";
1858 # throw away username for storing in records
1859 remove_username
($full_url);
1860 $log_entry{metadata
} = "$full_url\@$r $uuid";
1861 $log_entry{svm_revision
} = $r;
1862 $email ||= "$author\@$uuid"
1863 } elsif ($self->use_svnsync_props) {
1864 my $full_url = $self->svnsync->{url
};
1865 $full_url .= "/$self->{path}" if length $self->{path
};
1866 my $uuid = $self->svnsync->{uuid
};
1867 $log_entry{metadata
} = "$full_url\@$rev $uuid";
1868 $email ||= "$author\@$uuid"
1870 $log_entry{metadata
} = $self->metadata_url. "\@$rev " .
1871 $self->ra->get_uuid;
1872 $email ||= "$author\@" . $self->ra->get_uuid;
1874 $log_entry{name
} = $name;
1875 $log_entry{email
} = $email;
1880 my ($self, $min_rev, $max_rev, @parents) = @_;
1881 my ($last_rev, $last_commit) = $self->last_rev_commit;
1882 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1883 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1887 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1888 $self->{inject_parents
} = { $rev => $tree };
1889 $self->fetch(undef, undef);
1893 my ($self, $tree) = (shift, shift);
1894 my $log_entry = ::get_commit_entry
($tree);
1895 unless ($self->{last_rev
}) {
1896 fatal
("Must have an existing revision to commit\n");
1898 my %ed_opts = ( r
=> $self->{last_rev
},
1899 log => $log_entry->{log},
1901 tree_a
=> $self->{last_commit
},
1904 $self->set_tree_cb($log_entry, $tree, @_) },
1905 svn_path
=> $self->{path
} );
1906 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
1907 print "No changes\nr$self->{last_rev} = $tree\n";
1913 my $db_path = $self->db_path;
1914 return if (-e
$db_path && ! -z
$db_path);
1915 return unless ::verify_ref
($self->refname.'^0');
1916 if (-f
$self->{db_root
}) {
1917 rename $self->{db_root
}, $db_path or die
1918 "rename $self->{db_root} => $db_path failed: $!\n";
1919 my ($dir, $base) = ($db_path =~ m
#^(.*?)/?([^/]+)$#);
1920 symlink $base, $self->{db_root
} or die
1921 "symlink $base => $self->{db_root} failed: $!\n";
1924 print "Rebuilding $db_path ...\n";
1925 my ($rev_list, $ctx) = command_output_pipe
("rev-list", $self->refname);
1927 my $full_url = $self->full_url;
1928 remove_username
($full_url);
1930 while (<$rev_list>) {
1933 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1934 my ($url, $rev, $uuid) = ::cmt_metadata
($c);
1935 remove_username
($url);
1937 # ignore merges (from set-tree)
1938 next if (!defined $rev || !$uuid);
1940 # if we merged or otherwise started elsewhere, this is
1941 # how we break out of it
1942 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1943 ($full_url && $url && ($url ne $full_url))) {
1947 $svn_uuid ||= $uuid;
1949 $self->rev_db_set($rev, $c);
1950 print "r$rev = $c\n";
1952 command_close_pipe
($rev_list, $ctx);
1953 print "Done rebuilding $db_path\n";
1957 # Tie::File seems to be prone to offset errors if revisions get sparse,
1958 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1959 # one of my favorite modules is out :< Next up would be one of the DBM
1960 # modules, but I'm not sure which is most portable... So I'll just
1961 # go with something that's plain-text, but still capable of
1962 # being randomly accessed. So here's my ultra-simple fixed-width
1963 # database. All records are 40 characters + "\n", so it's easy to seek
1964 # to a revision: (41 * rev) is the byte offset.
1965 # A record of 40 0s denotes an empty revision.
1966 # And yes, it's still pretty fast (faster than Tie::File).
1967 # These files are disposable unless noMetadata or useSvmProps is set
1970 my ($fh, $rev, $commit) = @_;
1971 my $offset = $rev * 41;
1972 # assume that append is the common case:
1973 seek $fh, 0, 2 or croak
$!;
1975 if ($pos < $offset) {
1976 for (1 .. (($offset - $pos) / 41)) {
1977 print $fh (('0' x
40),"\n") or croak
$!;
1980 seek $fh, $offset, 0 or croak
$!;
1981 print $fh $commit,"\n" or croak
$!;
1987 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1988 mkpath
([$dir]) unless -d
$dir;
1989 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1990 close $fh or die "Couldn't close (create) $path: $!\n";
1995 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1996 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1997 my $db = $self->db_path($uuid);
1998 my $db_lock = "$db.lock";
2001 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2002 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
2006 $LOCKFILES{$db_lock} = 1;
2008 # both of these options make our .rev_db file very, very important
2009 # and we can't afford to lose it because rebuild() won't work
2010 if ($self->use_svm_props || $self->no_metadata) {
2012 copy
($db, $db_lock) or die "rev_db_set(@_): ",
2014 "$db => $db_lock ($!)\n";
2016 rename $db, $db_lock or die "rev_db_set(@_): ",
2017 "Failed to rename: ",
2018 "$db => $db_lock ($!)\n";
2020 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2021 _rev_db_set
($fh, $rev, $commit);
2023 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2024 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2026 close $fh or croak
$!;
2029 command_noisy
('update-ref', '-m', "r$rev",
2030 $self->refname, $commit);
2032 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2033 "$db_lock => $db ($!)\n";
2034 delete $LOCKFILES{$db_lock};
2036 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2037 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2038 kill $sig, $$ if defined $sig;
2045 my $db_path = $self->db_path;
2046 my @stat = stat $db_path or return 0;
2047 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2048 my $max = $stat[7] / 41;
2049 (($max > 0) ?
$max - 1 : 0);
2053 my ($self, $rev, $uuid) = @_;
2055 my $offset = $rev * 41;
2056 my $db_path = $self->db_path($uuid);
2057 return undef unless -e
$db_path;
2058 open my $fh, '<', $db_path or croak
$!;
2059 if (sysseek($fh, $offset, 0) == $offset) {
2060 my $read = sysread($fh, $ret, 40);
2061 $ret = undef if ($read != 40 || $ret eq ('0'x40
));
2063 close $fh or croak
$!;
2067 sub find_rev_before
{
2068 my ($self, $rev, $eq_ok) = @_;
2069 --$rev unless $eq_ok;
2071 if (my $c = $self->rev_db_get($rev)) {
2076 return (undef, undef);
2080 my ($class, $repo_id, $ref_id, $path) = @_;
2081 unless (defined $repo_id && length $repo_id) {
2082 $repo_id = $Git::SVN
::default_repo_id
;
2084 unless (defined $ref_id && length $ref_id) {
2085 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2087 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
2088 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2089 $_[3] = $path = '' unless (defined $path);
2090 mkpath
(["$ENV{GIT_DIR}/svn"]);
2092 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2093 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2094 db_root
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
2098 my ($self, $uuid) = @_;
2099 $uuid ||= $self->ra_uuid;
2100 "$self->{db_root}.$uuid";
2105 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2109 sub remove_username
{
2110 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2113 package Git
::SVN
::Prompt
;
2117 use vars qw
/$_no_auth_cache $_username/;
2120 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2121 $may_save = undef if $_no_auth_cache;
2122 $default_username = $_username if defined $_username;
2123 if (defined $default_username && length $default_username) {
2124 if (defined $realm && length $realm) {
2125 print STDERR
"Authentication realm: $realm\n";
2128 $cred->username($default_username);
2130 username
($cred, $realm, $may_save, $pool);
2132 $cred->password(_read_password
("Password for '" .
2133 $cred->username . "': ", $realm));
2134 $cred->may_save($may_save);
2135 $SVN::_Core
::SVN_NO_ERROR
;
2138 sub ssl_server_trust
{
2139 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2140 $may_save = undef if $_no_auth_cache;
2141 print STDERR
"Error validating server certificate for '$realm':\n";
2142 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2143 print STDERR
" - The certificate is not issued by a trusted ",
2144 "authority. Use the\n",
2145 " fingerprint to validate the certificate manually!\n";
2147 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2148 print STDERR
" - The certificate hostname does not match.\n";
2150 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2151 print STDERR
" - The certificate is not yet valid.\n";
2153 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2154 print STDERR
" - The certificate has expired.\n";
2156 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2157 print STDERR
" - The certificate has an unknown error.\n";
2160 "Certificate information:\n".
2161 " - Hostname: %s\n".
2162 " - Valid: from %s until %s\n".
2164 " - Fingerprint: %s\n",
2165 map $cert_info->$_, qw(hostname valid_from valid_until
2166 issuer_dname fingerprint);
2169 print STDERR
$may_save ?
2170 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2171 "(R)eject or accept (t)emporarily? ";
2173 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2174 if ($choice =~ /^t$/i) {
2175 $cred->may_save(undef);
2176 } elsif ($choice =~ /^r$/i) {
2178 } elsif ($may_save && $choice =~ /^p$/i) {
2179 $cred->may_save($may_save);
2183 $cred->accepted_failures($failures);
2184 $SVN::_Core
::SVN_NO_ERROR
;
2187 sub ssl_client_cert
{
2188 my ($cred, $realm, $may_save, $pool) = @_;
2189 $may_save = undef if $_no_auth_cache;
2190 print STDERR
"Client certificate filename: ";
2192 chomp(my $filename = <STDIN
>);
2193 $cred->cert_file($filename);
2194 $cred->may_save($may_save);
2195 $SVN::_Core
::SVN_NO_ERROR
;
2198 sub ssl_client_cert_pw
{
2199 my ($cred, $realm, $may_save, $pool) = @_;
2200 $may_save = undef if $_no_auth_cache;
2201 $cred->password(_read_password
("Password: ", $realm));
2202 $cred->may_save($may_save);
2203 $SVN::_Core
::SVN_NO_ERROR
;
2207 my ($cred, $realm, $may_save, $pool) = @_;
2208 $may_save = undef if $_no_auth_cache;
2209 if (defined $realm && length $realm) {
2210 print STDERR
"Authentication realm: $realm\n";
2213 if (defined $_username) {
2214 $username = $_username;
2216 print STDERR
"Username: ";
2218 chomp($username = <STDIN
>);
2220 $cred->username($username);
2221 $cred->may_save($may_save);
2222 $SVN::_Core
::SVN_NO_ERROR
;
2225 sub _read_password
{
2226 my ($prompt, $realm) = @_;
2227 print STDERR
$prompt;
2229 require Term
::ReadKey
;
2230 Term
::ReadKey
::ReadMode
('noecho');
2232 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2233 last if $key =~ /[\012\015]/; # \n\r
2236 Term
::ReadKey
::ReadMode
('restore');
2245 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2246 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2247 $SVN::Node
::none
.$SVN::Node
::file
.
2248 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2249 $SVN::Auth
::SSL
::CNMISMATCH
.
2250 $SVN::Auth
::SSL
::NOTYETVALID
.
2251 $SVN::Auth
::SSL
::EXPIRED
.
2252 $SVN::Auth
::SSL
::UNKNOWNCA
.
2253 $SVN::Auth
::SSL
::OTHER
;
2256 package SVN
::Git
::Fetcher
;
2264 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2266 my ($class, $git_svn) = @_;
2267 my $self = SVN
::Delta
::Editor
->new;
2268 bless $self, $class;
2269 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
2270 $self->{empty
} = {};
2271 $self->{dir_prop
} = {};
2272 $self->{file_prop
} = {};
2273 $self->{absent_dir
} = {};
2274 $self->{absent_file
} = {};
2275 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
2279 sub set_path_strip
{
2280 my ($self, $path) = @_;
2281 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
2288 sub open_directory
{
2289 my ($self, $path, $pb, $rev) = @_;
2294 my ($self, $path) = @_;
2295 if ($self->{path_strip
}) {
2296 $path =~ s!$self->{path_strip}!! or
2297 die "Failed to strip path '$path' ($self->{path_strip})\n";
2303 my ($self, $path, $rev, $pb) = @_;
2305 my $gpath = $self->git_path($path);
2306 return undef if ($gpath eq '');
2308 # remove entire directories.
2309 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
2310 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2312 $self->{c
}, '--', $gpath);
2316 $self->{gii
}->remove($_);
2317 print "\tD\t$_\n" unless $::_q
;
2319 print "\tD\t$gpath/\n" unless $::_q
;
2320 command_close_pipe
($ls, $ctx);
2321 $self->{empty
}->{$path} = 0
2323 $self->{gii
}->remove($gpath);
2324 print "\tD\t$gpath\n" unless $::_q
;
2330 my ($self, $path, $pb, $rev) = @_;
2331 my $gpath = $self->git_path($path);
2332 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
2333 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2334 unless (defined $mode && defined $blob) {
2335 die "$path was not found in commit $self->{c} (r$rev)\n";
2337 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2338 pool
=> SVN
::Pool
->new, action
=> 'M' };
2342 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2343 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2344 delete $self->{empty
}->{$dir};
2345 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2346 pool
=> SVN
::Pool
->new, action
=> 'A' };
2350 my ($self, $path, $cp_path, $cp_rev) = @_;
2351 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2352 delete $self->{empty
}->{$dir};
2353 $self->{empty
}->{$path} = 1;
2357 sub change_dir_prop
{
2358 my ($self, $db, $prop, $value) = @_;
2359 $self->{dir_prop
}->{$db->{path
}} ||= {};
2360 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2364 sub absent_directory
{
2365 my ($self, $path, $pb) = @_;
2366 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2367 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2372 my ($self, $path, $pb) = @_;
2373 $self->{absent_file
}->{$pb->{path
}} ||= [];
2374 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2378 sub change_file_prop
{
2379 my ($self, $fb, $prop, $value) = @_;
2380 if ($prop eq 'svn:executable') {
2381 if ($fb->{mode_b
} != 120000) {
2382 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2384 } elsif ($prop eq 'svn:special') {
2385 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2387 $self->{file_prop
}->{$fb->{path
}} ||= {};
2388 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2393 sub apply_textdelta
{
2394 my ($self, $fb, $exp) = @_;
2395 my $fh = IO
::File
->new_tmpfile;
2397 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2398 # (but $base does not,) so dup() it for reading in close_file
2399 open my $dup, '<&', $fh or croak
$!;
2400 my $base = IO
::File
->new_tmpfile;
2401 $base->autoflush(1);
2403 defined (my $pid = fork) or croak
$!;
2405 open STDOUT
, '>&', $base or croak
$!;
2406 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2407 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2413 seek $base, 0, 0 or croak
$!;
2414 my $md5 = Digest
::MD5
->new;
2415 $md5->addfile($base);
2416 my $got = $md5->hexdigest;
2417 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2419 " got: $got\n" if ($got ne $exp);
2422 seek $base, 0, 0 or croak
$!;
2424 $fb->{base
} = $base;
2425 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2429 my ($self, $fb, $exp) = @_;
2431 my $path = $self->git_path($fb->{path
});
2432 if (my $fh = $fb->{fh
}) {
2433 seek($fh, 0, 0) or croak
$!;
2434 my $md5 = Digest
::MD5
->new;
2436 my $got = $md5->hexdigest;
2437 die "Checksum mismatch: $path\n",
2438 "expected: $exp\n got: $got\n" if ($got ne $exp);
2439 seek($fh, 0, 0) or croak
$!;
2440 if ($fb->{mode_b
} == 120000) {
2441 read($fh, my $buf, 5) == 5 or croak
$!;
2442 $buf eq 'link ' or die "$path has mode 120000",
2443 "but is not a link\n";
2445 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2447 open STDIN
, '<&', $fh or croak
$!;
2448 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2450 chomp($hash = do { local $/; <$out> });
2451 close $out or croak
$!;
2452 close $fh or croak
$!;
2453 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2454 close $fb->{base
} or croak
$!;
2456 $hash = $fb->{blob
} or die "no blob information\n";
2459 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
2460 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
2466 $self->{nr
} = $self->{gii
}->{nr
};
2467 delete $self->{gii
};
2468 $self->SUPER::abort_edit
(@_);
2473 $self->{git_commit_ok
} = 1;
2474 $self->{nr
} = $self->{gii
}->{nr
};
2475 delete $self->{gii
};
2476 $self->SUPER::close_edit
(@_);
2479 package SVN
::Git
::Editor
;
2480 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
2488 my ($class, $opts) = @_;
2489 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
2490 die "$_ required!\n" unless (defined $opts->{$_});
2493 my $pool = SVN
::Pool
->new;
2494 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
2495 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
2498 # $opts->{ra} functions should not be used after this:
2499 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
2500 $opts->{editor_cb
}, $pool);
2501 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
2502 bless $self, $class;
2503 foreach (qw
/svn_path r tree_a tree_b/) {
2504 $self->{$_} = $opts->{$_};
2506 $self->{url
} = $opts->{ra
}->{url
};
2507 $self->{mods
} = $mods;
2508 $self->{types
} = $types;
2509 $self->{pool
} = $pool;
2510 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2512 $self->{path_prefix
} = length $self->{svn_path
} ?
2513 "$self->{svn_path}/" : '';
2518 my ($tree_a, $tree_b) = @_;
2519 my @diff_tree = qw(diff-tree -z -r);
2520 if ($_cp_similarity) {
2521 push @diff_tree, "-C$_cp_similarity";
2523 push @diff_tree, '-C';
2525 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2526 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
2527 push @diff_tree, $tree_a, $tree_b;
2528 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2532 while (<$diff_fh>) {
2533 chomp $_; # this gets rid of the trailing "\0"
2534 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2535 $::sha1\s
($::sha1
)\s
2536 ([MTCRAD
])\d
*$/xo
) {
2537 push @mods, { mode_a
=> $1, mode_b
=> $2,
2538 sha1_b
=> $3, chg
=> $4 };
2539 if ($4 =~ /^(?:C|R)$/) {
2544 } elsif ($state eq 'file_a') {
2545 my $x = $mods[$#mods] or croak
"Empty array\n";
2546 if ($x->{chg
} !~ /^(?:C|R)$/) {
2547 croak
"Error parsing $_, $x->{chg}\n";
2551 } elsif ($state eq 'file_b') {
2552 my $x = $mods[$#mods] or croak
"Empty array\n";
2553 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2554 croak
"Error parsing $_, $x->{chg}\n";
2556 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2557 croak
"Error parsing $_, $x->{chg}\n";
2562 croak
"Error parsing $_\n";
2565 command_close_pipe
($diff_fh, $ctx);
2569 sub check_diff_paths
{
2570 my ($ra, $pfx, $rev, $mods) = @_;
2572 $pfx .= '/' if length $pfx;
2574 sub type_diff_paths
{
2575 my ($ra, $types, $path, $rev) = @_;
2576 my @p = split m
#/+#, $path;
2578 unless (defined $types->{$c}) {
2579 $types->{$c} = $ra->check_path($c, $rev);
2582 $c .= '/' . shift @p;
2583 next if defined $types->{$c};
2584 $types->{$c} = $ra->check_path($c, $rev);
2588 foreach my $m (@
$mods) {
2589 foreach my $f (qw
/file_a file_b/) {
2590 next unless defined $m->{$f};
2591 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
2592 if (length $pfx.$dir && ! defined $types{$dir}) {
2593 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
2601 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2605 my ($self, $path) = @_;
2606 $self->{path_prefix
}.(defined $path ?
$path : '');
2610 my ($self, $path) = @_;
2611 $self->{url
} . '/' . $self->repo_path($path);
2616 my $rm = $self->{rm
};
2617 delete $rm->{''}; # we never delete the url we're tracking
2620 foreach (keys %$rm) {
2621 my @d = split m
#/#, $_;
2625 $c .= '/' . shift @d;
2629 delete $rm->{$self->{svn_path
}};
2630 delete $rm->{''}; # we never delete the url we're tracking
2633 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
2638 my @dn = split m
#/#, $_;
2640 delete $rm->{join '/', @dn};
2647 command_close_pipe
($fh, $ctx);
2649 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2650 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2651 $self->close_directory($bat->{$d}, $p);
2652 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2653 print "\tD+\t$d/\n" unless $::_q
;
2654 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2659 sub open_or_add_dir
{
2660 my ($self, $full_path, $baton) = @_;
2661 my $t = $self->{types
}->{$full_path};
2663 die "$full_path not known in r$self->{r} or we have a bug!\n";
2665 if ($t == $SVN::Node
::none
) {
2666 return $self->add_directory($full_path, $baton,
2667 undef, -1, $self->{pool
});
2668 } elsif ($t == $SVN::Node
::dir
) {
2669 return $self->open_directory($full_path, $baton,
2670 $self->{r
}, $self->{pool
});
2672 print STDERR
"$full_path already exists in repository at ",
2673 "r$self->{r} and it is not a directory (",
2674 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2679 my ($self, $path) = @_;
2680 my $bat = $self->{bat
};
2681 my $repo_path = $self->repo_path($path);
2682 return $bat->{''} unless (length $repo_path);
2683 my @p = split m
#/+#, $repo_path;
2685 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2688 $c .= '/' . shift @p;
2689 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2695 my ($self, $m) = @_;
2696 my ($dir, $file) = split_path
($m->{file_b
});
2697 my $pbat = $self->ensure_path($dir);
2698 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2700 print "\tA\t$m->{file_b}\n" unless $::_q
;
2701 $self->chg_file($fbat, $m);
2702 $self->close_file($fbat,undef,$self->{pool
});
2706 my ($self, $m) = @_;
2707 my ($dir, $file) = split_path
($m->{file_b
});
2708 my $pbat = $self->ensure_path($dir);
2709 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2710 $self->url_path($m->{file_a
}), $self->{r
});
2711 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2712 $self->chg_file($fbat, $m);
2713 $self->close_file($fbat,undef,$self->{pool
});
2717 my ($self, $path, $pbat) = @_;
2718 my $rpath = $self->repo_path($path);
2719 my ($dir, $file) = split_path
($rpath);
2720 $self->{rm
}->{$dir} = 1;
2721 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2725 my ($self, $m) = @_;
2726 my ($dir, $file) = split_path
($m->{file_b
});
2727 my $pbat = $self->ensure_path($dir);
2728 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2729 $self->url_path($m->{file_a
}), $self->{r
});
2730 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2731 $self->chg_file($fbat, $m);
2732 $self->close_file($fbat,undef,$self->{pool
});
2734 ($dir, $file) = split_path
($m->{file_a
});
2735 $pbat = $self->ensure_path($dir);
2736 $self->delete_entry($m->{file_a
}, $pbat);
2740 my ($self, $m) = @_;
2741 my ($dir, $file) = split_path
($m->{file_b
});
2742 my $pbat = $self->ensure_path($dir);
2743 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2744 $pbat,$self->{r
},$self->{pool
});
2745 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
2746 $self->chg_file($fbat, $m);
2747 $self->close_file($fbat,undef,$self->{pool
});
2750 sub T
{ shift->M(@_) }
2752 sub change_file_prop
{
2753 my ($self, $fbat, $pname, $pval) = @_;
2754 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2758 my ($self, $fbat, $m) = @_;
2759 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2760 $self->change_file_prop($fbat,'svn:executable','*');
2761 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2762 $self->change_file_prop($fbat,'svn:executable',undef);
2764 my $fh = IO
::File
->new_tmpfile or croak
$!;
2765 if ($m->{mode_b
} =~ /^120/) {
2766 print $fh 'link ' or croak
$!;
2767 $self->change_file_prop($fbat,'svn:special','*');
2768 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2769 $self->change_file_prop($fbat,'svn:special',undef);
2771 defined(my $pid = fork) or croak
$!;
2773 open STDOUT
, '>&', $fh or croak
$!;
2774 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2778 $fh->flush == 0 or croak
$!;
2779 seek $fh, 0, 0 or croak
$!;
2781 my $md5 = Digest
::MD5
->new;
2782 $md5->addfile($fh) or croak
$!;
2783 seek $fh, 0, 0 or croak
$!;
2785 my $exp = $md5->hexdigest;
2786 my $pool = SVN
::Pool
->new;
2787 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2788 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2789 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2792 close $fh or croak
$!;
2796 my ($self, $m) = @_;
2797 my ($dir, $file) = split_path
($m->{file_b
});
2798 my $pbat = $self->ensure_path($dir);
2799 print "\tD\t$m->{file_b}\n" unless $::_q
;
2800 $self->delete_entry($m->{file_b
}, $pbat);
2805 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2806 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2807 $self->close_directory($bat->{$_}, $p);
2809 $self->SUPER::close_edit
($p);
2815 $self->SUPER::abort_edit
($self->{pool
});
2820 $self->SUPER::DESTROY
(@_);
2821 $self->{pool
}->clear;
2824 # this drives the editor
2827 my $mods = $self->{mods
};
2828 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2829 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
2831 if (defined $o{$f}) {
2834 fatal
("Invalid change type: $f\n");
2837 $self->rmdirs if $_rmdir;
2843 return scalar @
$mods;
2846 package Git
::SVN
::Ra
;
2847 use vars qw
/@ISA $config_dir $_log_window_size/;
2850 my ($can_do_switch, %ignored_err, $RA);
2853 # enforce temporary pool usage for some simple functions
2855 foreach (qw
/get_latest_revnum get_uuid get_repos_root/) {
2858 my \$pool = SVN::Pool->new;
2859 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2861 wantarray ? \@ret : \$ret[0]; }\n";
2864 # get_dir needs $pool held in cache for dirents to work,
2865 # check_path is cacheable and rev_proplist is close enough
2867 foreach (qw
/check_path get_dir rev_proplist/) {
2868 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2871 my \$k = join(\"\\0\", \@_);
2872 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2873 return wantarray ? \@\$x : \$x->[0];
2875 my \$pool = SVN::Pool->new;
2876 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2877 if (\$r != \$${_}_rev) {
2878 \%${_}_cache = ( pool => [] );
2881 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2882 push \@{\$${_}_cache{pool}}, \$pool;
2883 wantarray ? \@ret : \$ret[0]; }\n";
2890 my ($class, $url) = @_;
2892 return $RA if ($RA && $RA->{url
} eq $url);
2893 $RA->{pool
}->clear if $RA;
2895 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2896 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2897 SVN
::Client
::get_simple_provider
(),
2898 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2899 SVN
::Client
::get_simple_prompt_provider
(
2900 \
&Git
::SVN
::Prompt
::simple
, 2),
2901 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2902 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2903 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2904 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2905 SVN
::Client
::get_username_provider
(),
2906 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2907 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2908 SVN
::Client
::get_username_prompt_provider
(
2909 \
&Git
::SVN
::Prompt
::username
, 2),
2911 my $config = SVN
::Core
::config_get_config
($config_dir);
2912 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2914 pool
=> SVN
::Pool
->new,
2915 auth_provider_callbacks
=> $callbacks);
2916 $self->{svn_path
} = $url;
2917 $self->{repos_root
} = $self->get_repos_root;
2918 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
2919 $RA = bless $self, $class;
2923 # do not call the real DESTROY since we store ourselves in $RA
2927 my ($self, @args) = @_;
2928 my $pool = SVN
::Pool
->new;
2929 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2930 my $ret = $self->SUPER::get_log
(@args, $pool);
2935 sub get_commit_editor
{
2936 my ($self, $log, $cb, $pool) = @_;
2937 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2938 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2942 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2943 my $new = ($rev_a == $rev_b);
2944 my $path = $gs->{path
};
2946 if ($new && -e
$gs->{index}) {
2947 unlink $gs->{index} or die
2948 "Couldn't unlink index: $gs->{index}: $!\n";
2950 my $pool = SVN
::Pool
->new;
2951 $editor->set_path_strip($path);
2952 my (@pc) = split m
#/#, $path;
2953 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
2955 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2957 # Since we can't rely on svn_ra_reparent being available, we'll
2958 # just have to do some magic with set_path to make it so
2959 # we only want a partial path.
2961 my $final = join('/', @pc);
2963 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2964 $sp .= '/' if length $sp;
2967 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2969 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2971 $reporter->finish_report($pool);
2973 $editor->{git_commit_ok
};
2976 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2977 # svn_ra_reparent didn't work before 1.4)
2979 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2980 my $path = $gs->{path
};
2981 my $pool = SVN
::Pool
->new;
2983 my $full_url = $self->{url
};
2984 my $old_url = $full_url;
2985 $full_url .= "/$path" if length $path;
2986 my ($ra, $reparented);
2987 if ($old_url ne $full_url) {
2988 if ($old_url !~ m
#^svn(\+ssh)?://#) {
2989 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
2991 $self->{url
} = $full_url;
2994 $ra = Git
::SVN
::Ra
->new($full_url);
2998 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2999 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3000 $reporter->set_path('', $rev_a, 0, @lock, $pool);
3001 $reporter->finish_report($pool);
3004 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
3005 $self->{url
} = $old_url;
3009 $editor->{git_commit_ok
};
3012 sub gs_fetch_loop_common
{
3013 my ($self, $base, $head, $gsv, $globs) = @_;
3014 return if ($base > $head);
3015 my $inc = $_log_window_size;
3016 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
3018 my $common_max = scalar @
$gsv;
3020 foreach my $gs (@
$gsv) {
3021 my @tmp = split m
#/#, $gs->{path};
3024 $p .= length($p) ?
"/$_" : $_;
3030 $common_max += scalar @
$globs;
3031 foreach my $glob (@
$globs) {
3032 my @tmp = split m
#/#, $glob->{path}->{left};
3035 $p .= length($p) ?
"/$_" : $_;
3041 my $longest_path = '';
3042 foreach (sort {length $b <=> length $a} keys %common) {
3043 if ($common{$_} == $common_max) {
3051 my $err_handler = $SVN::Error
::handler
;
3052 $SVN::Error
::handler
= sub {
3054 skip_unknown_revs
($err);
3057 my ($paths, $r, $author, $date, $log) = @_;
3058 [ dup_changed_paths
($paths),
3059 { author
=> $author, date
=> $date, log => $log } ];
3061 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3062 sub { $revs{$_[1]} = _cb
(@_) });
3063 if ($err && $max >= $head) {
3064 print STDERR
"Path '$longest_path' ",
3065 "was probably deleted:\n",
3066 $err->expanded_message,
3067 "\nWill attempt to follow ",
3068 "revisions r$min .. r$max ",
3069 "committed before the deletion\n";
3071 while (--$hi >= $min) {
3073 $self->get_log([$longest_path], $min, $hi,
3076 $revs{$_[1]} = _cb
(@_) });
3078 print STDERR
"r$min .. r$ok OK\n";
3083 $SVN::Error
::handler
= $err_handler;
3085 my %exists = map { $_->{path
} => $_ } @
$gsv;
3086 foreach my $r (sort {$a <=> $b} keys %revs) {
3087 my ($paths, $logged) = @
{$revs{$r}};
3089 foreach my $gs ($self->match_globs(\
%exists, $paths,
3091 if ($gs->rev_db_max >= $r) {
3094 next unless $gs->match_paths($paths, $r);
3095 $gs->{logged_rev_props
} = $logged;
3096 if (my $last_commit = $gs->last_commit) {
3097 $gs->assert_index_clean($last_commit);
3099 my $log_entry = $gs->do_fetch($paths, $r);
3101 $gs->do_git_commit($log_entry);
3104 foreach my $g (@
$globs) {
3105 my $k = "svn-remote.$g->{remote}." .
3107 Git
::SVN
::tmp_config
($k, $r);
3110 # pre-fill the .rev_db since it'll eventually get filled in
3111 # with '0' x40 if something new gets committed
3112 foreach my $gs (@
$gsv) {
3113 next if defined $gs->rev_db_get($max);
3114 $gs->rev_db_set($max, 0 x40
);
3116 foreach my $g (@
$globs) {
3117 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3118 Git
::SVN
::tmp_config
($k, $max);
3120 last if $max >= $head;
3123 $max = $head if ($max > $head);
3128 my ($self, $exists, $paths, $globs, $r) = @_;
3131 my ($self, $exists, $g, $r) = @_;
3132 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
3133 return unless scalar @x == 3;
3134 my $dirents = $x[0];
3135 foreach my $de (keys %$dirents) {
3136 next if $dirents->{$de}->kind != $SVN::Node
::dir
;
3137 my $p = $g->{path
}->full_path($de);
3138 next if $exists->{$p};
3139 next if (length $g->{path
}->{right
} &&
3140 ($self->check_path($p, $r) !=
3142 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
3143 $g->{ref}->full_path($de), 1);
3146 foreach my $g (@
$globs) {
3147 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3148 if ($path->{action
} =~ /^[AR]$/) {
3149 get_dir_check
($self, $exists, $g, $r);
3152 foreach (keys %$paths) {
3153 if (/$g->{path}->{left_regex}/ &&
3154 !/$g->{path}->{regex}/) {
3155 next if $paths->{$_}->{action
} !~ /^[AR]$/;
3156 get_dir_check
($self, $exists, $g, $r);
3158 next unless /$g->{path}->{regex}/;
3160 my $pathname = $g->{path
}->full_path($p);
3161 next if $exists->{$pathname};
3162 $exists->{$pathname} = Git
::SVN
->init(
3163 $self->{url
}, $pathname, undef,
3164 $g->{ref}->full_path($p), 1);
3167 foreach (split m
#/#, $g->{path}->{left}) {
3169 next unless ($paths->{$c} &&
3170 ($paths->{$c}->{action
} =~ /^[AR]$/));
3171 get_dir_check
($self, $exists, $g, $r);
3179 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
3180 my $url = $self->{repos_root
};
3181 my @components = split(m!/!, $self->{svn_path
});
3184 $url .= "/$c" if length $c;
3185 eval { (ref $self)->new($url)->get_latest_revnum };
3186 } while ($@
&& ($c = shift @components));
3192 unless (defined $can_do_switch) {
3193 my $pool = SVN
::Pool
->new;
3195 $self->do_switch(1, '', 0, $self->{url
},
3196 SVN
::Delta
::Editor
->new, $pool);
3201 $rep->abort_report($pool);
3209 sub skip_unknown_revs
{
3211 my $errno = $err->apr_err();
3212 # Maybe the branch we're tracking didn't
3213 # exist when the repo started, so it's
3214 # not an error if it doesn't, just continue
3216 # Wonderfully consistent library, eh?
3217 # 160013 - svn:// and file://
3218 # 175002 - http(s)://
3219 # 175007 - http(s):// (this repo required authorization, too...)
3220 # More codes may be discovered later...
3221 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3222 my $err_key = $err->expanded_message;
3223 # revision numbers change every time, filter them out
3224 $err_key =~ s/\d+/\0/g;
3225 $err_key = "$errno\0$err_key";
3226 unless ($ignored_err{$err_key}) {
3227 warn "W: Ignoring error from SVN, path probably ",
3228 "does not exist: ($errno): ",
3229 $err->expanded_message,"\n";
3230 $ignored_err{$err_key} = 1;
3234 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3237 # svn_log_changed_path_t objects passed to get_log are likely to be
3238 # overwritten even if only the refs are copied to an external variable,
3239 # so we should dup the structures in their entirety. Using an externally
3240 # passed pool (instead of our temporary and quickly cleared pool in
3241 # Git::SVN::Ra) does not help matters at all...
3242 sub dup_changed_paths
{
3244 return undef unless $paths;
3246 foreach my $p (keys %$paths) {
3247 my $i = $paths->{$p};
3248 my %s = map { $_ => $i->$_ }
3249 qw
/copyfrom_path copyfrom_rev action/;
3255 package Git
::SVN
::Log
;
3258 use POSIX qw
/strftime/;
3259 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
3260 %rusers $show_commit $incremental/;
3265 return 1 if defined $c->{r
};
3267 # big commit message got truncated by the 16k pretty buffer in rev-list
3268 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
3269 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
3271 my @log = command
(qw
/cat-file commit/, $c->{c
});
3273 # shift off the headers
3274 shift @log while ($log[0] ne '');
3277 # TODO: make $c->{l} not have a trailing newline in the future
3278 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
3280 (undef, $c->{r
}, undef) = ::extract_metadata
(
3281 (grep(/^git-svn-id: /, @log))[-1]);
3283 return defined $c->{r
};
3289 $dcvar = 'color.diff';
3290 $dc = `git-config --get $dcvar`;
3292 # nothing at all; fallback to "diff.color"
3293 $dcvar = 'diff.color';
3294 $dc = `git-config --get $dcvar`;
3297 if ($dc eq 'auto') {
3299 $pc = `git-config --get color.pager`;
3301 # does not have it -- fallback to pager.color
3302 $pc = `git-config --bool --get pager.color`;
3305 $pc = `git-config --bool --get color.pager`;
3311 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
3312 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
3316 return 0 if $dc eq 'never';
3317 return 1 if $dc eq 'always';
3318 chomp($dc = `git-config --bool --get $dcvar`);
3319 return ($dc eq 'true');
3322 sub git_svn_log_cmd
{
3323 my ($r_min, $r_max, @args) = @_;
3325 foreach my $x (@args) {
3327 next unless ::verify_ref
("$x^0");
3332 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
3333 $gs ||= Git
::SVN
->_new;
3334 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
3336 push @cmd, '-r' unless $non_recursive;
3337 push @cmd, qw
/--raw --name-status/ if $verbose;
3338 push @cmd, '--color' if log_use_color
();
3339 return @cmd unless defined $r_max;
3340 if ($r_max == $r_min) {
3341 push @cmd, '--max-count=1';
3342 if (my $c = $gs->rev_db_get($r_max)) {
3346 my ($c_min, $c_max);
3347 $c_max = $gs->rev_db_get($r_max);
3348 $c_min = $gs->rev_db_get($r_min);
3349 if (defined $c_min && defined $c_max) {
3350 if ($r_max > $r_max) {
3351 push @cmd, "$c_min..$c_max";
3353 push @cmd, "$c_max..$c_min";
3355 } elsif ($r_max > $r_min) {
3364 # adapted from pager.c
3366 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
3367 if (!defined $pager) {
3369 } elsif (length $pager == 0 || $pager eq 'cat') {
3375 return unless -t
*STDOUT
;
3376 pipe my $rfd, my $wfd or return;
3377 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
3379 open STDOUT
, '>&', $wfd or
3380 ::fatal
"Can't redirect to stdout: $!\n";
3383 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
3384 $ENV{LESS
} ||= 'FRSX';
3385 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
3388 sub tz_to_s_offset
{
3391 return ($1 * 60) + ($tz * 3600);
3394 sub get_author_info
{
3395 my ($dest, $author, $t, $tz) = @_;
3396 $author =~ s/(?:^\s*|\s*$)//g;
3397 $dest->{a_raw
} = $author;
3400 $au = $rusers{$author} || undef;
3403 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3408 # Date::Parse isn't in the standard Perl distro :(
3409 if ($tz =~ s/^\+//) {
3410 $t += tz_to_s_offset
($tz);
3411 } elsif ($tz =~ s/^\-//) {
3412 $t -= tz_to_s_offset
($tz);
3414 $dest->{t_utc
} = $t;
3417 sub process_commit
{
3418 my ($c, $r_min, $r_max, $defer) = @_;
3419 if (defined $r_min && defined $r_max) {
3420 if ($r_min == $c->{r
} && $r_min == $r_max) {
3424 return 1 if $r_min == $r_max;
3425 if ($r_min < $r_max) {
3426 # we need to reverse the print order
3427 return 0 if (defined $limit && --$limit < 0);
3431 if ($r_min != $r_max) {
3432 return 1 if ($r_min < $c->{r
});
3433 return 1 if ($r_max > $c->{r
});
3436 return 0 if (defined $limit && --$limit < 0);
3445 if (my $l = $c->{l
}) {
3446 while ($l->[0] =~ /^\s*$/) { shift @
$l }
3449 $l_fmt ||= 'A' . length($c->{r
});
3450 print 'r',pack($l_fmt, $c->{r
}),' | ';
3451 print "$c->{c} | " if $show_commit;
3454 show_commit_normal
($c);
3458 sub show_commit_changed_paths
{
3460 return unless $c->{changed
};
3461 print "Changed paths:\n", @
{$c->{changed
}};
3464 sub show_commit_normal
{
3466 print '-' x72
, "\nr$c->{r} | ";
3467 print "$c->{c} | " if $show_commit;
3468 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3469 localtime($c->{t_utc
})), ' | ';
3472 if (my $l = $c->{l
}) {
3473 while ($l->[$#$l] eq "\n" && $#$l > 0
3474 && $l->[($#$l - 1)] eq "\n") {
3477 $nr_line = scalar @
$l;
3479 print "1 line\n\n\n";
3481 if ($nr_line == 1) {
3482 $nr_line = '1 line';
3484 $nr_line .= ' lines';
3486 print $nr_line, "\n";
3487 show_commit_changed_paths
($c);
3489 print $_ foreach @
$l;
3493 show_commit_changed_paths
($c);
3497 foreach my $x (qw
/raw stat diff/) {
3500 print $_ foreach @
{$c->{$x}}
3507 my ($r_min, $r_max);
3508 my $r_last = -1; # prevent dupes
3514 if (defined $::_revision
) {
3515 if ($::_revision
=~ /^(\d+):(\d+)$/) {
3516 ($r_min, $r_max) = ($1, $2);
3517 } elsif ($::_revision
=~ /^\d+$/) {
3518 $r_min = $r_max = $::_revision
;
3520 ::fatal
"-r$::_revision is not supported, use ",
3521 "standard \'git log\' arguments instead\n";
3526 @args = (git_svn_log_cmd
($r_min, $r_max, @args), @args);
3527 my $log = command_output_pipe
(@args);
3529 my (@k, $c, $d, $stat);
3530 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3532 if (/^${esc_color}commit ($::sha1_short)/o) {
3534 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
3536 process_commit
($c, $r_min, $r_max, \
@k) or
3541 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3542 get_author_info
($c, $1, $2, $3);
3543 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3545 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3546 push @
{$c->{raw
}}, $_;
3547 } elsif (/^${esc_color}[ACRMDT]\t/) {
3548 # we could add $SVN->{svn_path} here, but that requires
3549 # remote access at the moment (repo_path_split)...
3550 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3551 push @
{$c->{changed
}}, $_;
3552 } elsif (/^${esc_color}diff /o) {
3554 push @
{$c->{diff
}}, $_;
3556 push @
{$c->{diff
}}, $_;
3557 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
3558 $esc_color*[\
+\
-]*$esc_color$/x
) {
3560 push @
{$c->{stat}}, $_;
3561 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3562 push @
{$c->{stat}}, $_;
3564 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3565 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
3566 } elsif (s/^${esc_color} //o) {
3567 push @
{$c->{l
}}, $_;
3570 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
3572 process_commit
($c, $r_min, $r_max, \
@k);
3578 process_commit
($_, $r_min, $r_max) foreach reverse @k;
3582 print '-' x72
,"\n" unless $incremental || $oneline;
3585 package Git
::SVN
::Migration
;
3586 # these version numbers do NOT correspond to actual version numbers
3587 # of git nor git-svn. They are just relative.
3589 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3591 # v1 layout: .git/$id/info/url, refs/remotes/$id
3593 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3595 # v3 layout: .git/svn/$id, refs/remotes/$id
3596 # - info/url may remain for backwards compatibility
3597 # - this is what we migrate up to this layout automatically,
3598 # - this will be used by git svn init on single branches
3599 # v3.1 layout (auto migrated):
3600 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3601 # for backwards compatibility
3603 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3604 # - this is only created for newly multi-init-ed
3605 # repositories. Similar in spirit to the
3606 # --use-separate-remotes option in git-clone (now default)
3607 # - we do not automatically migrate to this (following
3608 # the example set by core git)
3612 use File
::Path qw
/mkpath/;
3613 use File
::Basename qw
/dirname basename/;
3614 use vars qw
/$_minimize/;
3616 sub migrate_from_v0
{
3617 my $git_dir = $ENV{GIT_DIR
};
3618 return undef unless -d
$git_dir;
3619 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3623 my ($id, $orig_ref) = ($_, $_);
3624 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
3625 next unless -f
"$git_dir/$id/info/url";
3626 my $new_ref = "refs/remotes/$id";
3627 if (::verify_ref
("$new_ref^0")) {
3628 print STDERR
"W: $orig_ref is probably an old ",
3629 "branch used by an ancient version of ",
3631 "However, $new_ref also exists.\n",
3632 "We will not be able ",
3633 "to use this branch until this ",
3634 "ambiguity is resolved.\n";
3637 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
3638 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
3639 command_noisy
('update-ref', $new_ref, $orig_ref);
3640 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
3643 command_close_pipe
($fh, $ctx);
3644 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
3648 sub migrate_from_v1
{
3649 my $git_dir = $ENV{GIT_DIR
};
3651 return $migrated unless -d
$git_dir;
3652 my $svn_dir = "$git_dir/svn";
3654 # just in case somebody used 'svn' as their $id at some point...
3655 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
3657 print STDERR
"Migrating from a git-svn v1 layout...\n";
3659 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
3660 "$svn_dir\n\t(required for this version ",
3661 "($::VERSION) of git-svn) does not. exist\n";
3662 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3665 next unless $x =~ s
#^refs/remotes/##;
3667 next unless -f
"$git_dir/$x/info/url";
3668 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
3670 my $dn = dirname
("$git_dir/svn/$x");
3671 mkpath
([$dn]) unless -d
$dn;
3672 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3673 mkpath
(["$git_dir/svn/svn"]);
3674 print STDERR
" - $git_dir/$x/info => ",
3675 "$git_dir/svn/$x/info\n";
3676 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3678 # don't worry too much about these, they probably
3679 # don't exist with repos this old (save for index,
3680 # and we can easily regenerate that)
3681 foreach my $f (qw
/unhandled.log index .rev_db/) {
3682 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3685 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
3686 rename "$git_dir/$x", "$git_dir/svn/$x" or
3691 command_close_pipe
($fh, $ctx);
3692 print STDERR
"Done migrating from a git-svn v1 layout\n";
3697 my ($l_map, $pfx, $path) = @_;
3699 foreach (<$path/*>) {
3700 if (-r
"$_/info/url") {
3701 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
3702 my $ref_id = $pfx . basename
$_;
3703 my $url = ::file_to_s
("$_/info/url");
3704 $l_map->{$ref_id} = $url;
3711 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3712 read_old_urls
($l_map, $x, $_);
3716 sub migrate_from_v2
{
3717 my @cfg = command
(qw
/config -l/);
3718 return if grep /^svn-remote\..+\.url=/, @cfg;
3720 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
3723 foreach my $ref_id (sort keys %l_map) {
3724 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
3726 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3733 sub minimize_connections
{
3734 my $r = Git
::SVN
::read_all_remotes
();
3736 my $root_repos = {};
3737 foreach my $repo_id (keys %$r) {
3738 my $url = $r->{$repo_id}->{url
} or next;
3739 my $fetch = $r->{$repo_id}->{fetch
} or next;
3740 my $ra = Git
::SVN
::Ra
->new($url);
3742 # skip existing cases where we already connect to the root
3743 if (($ra->{url
} eq $ra->{repos_root
}) ||
3744 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
3746 $root_repos->{$ra->{url
}} = $repo_id;
3750 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
3751 my $root_path = $ra->{url
};
3752 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
3753 foreach my $path (keys %$fetch) {
3754 my $ref_id = $fetch->{$path};
3755 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
3757 # make sure we can read when connecting to
3758 # a higher level of a repository
3759 my ($last_rev, undef) = $gs->last_rev_commit;
3760 if (!defined $last_rev) {
3762 $root_ra->get_latest_revnum;
3766 my $new = $root_path;
3767 $new .= length $path ?
"/$path" : '';
3769 $root_ra->get_log([$new], $last_rev, $last_rev,
3773 $new_urls->{$ra->{repos_root
}}->{$new} =
3774 { ref_id
=> $ref_id,
3775 old_repo_id
=> $repo_id,
3776 old_path
=> $path };
3781 foreach my $url (keys %$new_urls) {
3782 # see if we can re-use an existing [svn-remote "repo_id"]
3783 # instead of creating a(n ugly) new section:
3784 my $repo_id = $root_repos->{$url} ||
3785 Git
::SVN
::sanitize_remote_name
($url);
3787 my $fetch = $new_urls->{$url};
3788 foreach my $path (keys %$fetch) {
3789 my $x = $fetch->{$path};
3790 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
3791 my $pfx = "svn-remote.$x->{old_repo_id}";
3793 my $old_fetch = quotemeta("$x->{old_path}:".
3794 "refs/remotes/$x->{ref_id}");
3795 command_noisy
(qw
/config --unset/,
3796 "$pfx.fetch", '^'. $old_fetch . '$');
3797 delete $r->{$x->{old_repo_id
}}->
3798 {fetch
}->{$x->{old_path
}};
3799 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
3800 command_noisy
(qw
/config --unset/,
3802 push @emptied, $x->{old_repo_id
}
3807 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
3808 "$ENV{GIT_DIR}/config";
3810 The following [svn-remote] sections in your config file ($file) are empty
3811 and can be safely removed:
3813 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
3817 sub migration_check
{
3821 minimize_connections
() if $_minimize;
3824 package Git
::IndexInfo
;
3827 use Git qw
/command_input_pipe command_close_pipe/;
3831 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
3832 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
3836 my ($self, $path) = @_;
3837 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
3838 return ++$self->{nr
};
3844 my ($self, $mode, $hash, $path) = @_;
3845 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
3846 return ++$self->{nr
};
3853 command_close_pipe
($self->{gui
}, $self->{ctx
});
3856 package Git
::SVN
::GlobSpec
;
3861 my ($class, $glob) = @_;
3863 $re =~ s!/+$!!g; # no need for trailing slashes
3864 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3865 my ($left, $right) = ($1, $2);
3867 die "Only one '*' wildcard expansion ",
3868 "is supported (got $nr): '$glob'\n";
3869 } elsif ($nr == 0) {
3870 die "One '*' is needed for glob: '$glob'\n";
3872 $re = quotemeta($left) . $re . quotemeta($right);
3873 if (length $left && !($left =~ s!/+$!!g)) {
3874 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3876 if (length $right && !($right =~ s!^/+!!g)) {
3877 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3879 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
3880 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
3881 regex
=> qr/$re/, glob => $glob }, $class;
3885 my ($self, $path) = @_;
3886 return (length $self->{left
} ?
"$self->{left}/" : '') .
3887 $path . (length $self->{right
} ?
"/$self->{right}" : '');
3895 $remotes = { # returned by read_all_remotes()
3897 # svn-remote.svn.url=https://svn.musicpd.org
3898 url
=> 'https://svn.musicpd.org',
3899 # svn-remote.svn.fetch=mpd/trunk:trunk
3901 'mpd/trunk' => 'trunk',
3903 # svn-remote.svn.tags=mpd/tags/*:tags/*
3908 regex
=> qr!mpd/tags/([^/]+)$!,
3914 regex
=> qr!tags/([^/]+)$!,
3921 $log_entry hashref as returned by libsvn_log_entry
()
3923 log => 'whitespace-formatted log entry
3924 ', # trailing newline is preserved
3925 revision
=> '8', # integer
3926 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3927 author
=> 'committer name'
3931 # this is generated by generate_diff();
3932 @mods = array of diff
-index line hashes
, each element represents one line
3933 of diff
-index output
3935 diff
-index line
($m hash
)
3937 mode_a
=> first column of diff
-index output
, no leading
':',
3938 mode_b
=> second column of diff
-index output
,
3939 sha1_b
=> sha1sum of the final blob
,
3940 chg
=> change type
[MCRADT
],
3941 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3942 file_b
=> new
/current file name of a file
(any chg
)
3946 # retval of read_url_paths{,_all}();
3948 # repository root url
3949 'https://svn.musicpd.org' => {
3950 # repository path # GIT_SVN_ID
3951 'mpd/trunk' => 'trunk',
3952 'mpd/tags/0.11.5' => 'tags/0.11.5',
3957 I don
't trust the each() function on unless I created %hash myself
3958 because the internal iterator may not have started at base.