2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $sha1 $sha1_short $_revision
9 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
10 $VERSION = '@@GIT_VERSION@@';
12 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR
};
13 $ENV{GIT_DIR
} ||= '.git';
14 $Git::SVN
::default_repo_id
= 'svn';
15 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
16 $Git::SVN
::Ra
::_log_window_size
= 100;
18 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
20 $| = 1; # unbuffer STDOUT
22 sub fatal
(@
) { print STDERR
@_; exit 1 }
23 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
26 if ($SVN::Core
::VERSION
lt '1.1.0') {
27 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
29 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
30 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
31 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
34 use File
::Basename qw
/dirname basename/;
35 use File
::Path qw
/mkpath/;
36 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
42 foreach (qw
/command command_oneline command_noisy command_output_pipe
43 command_input_pipe command_close_pipe
/) {
44 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
45 "*Git::SVN::Migration::$_ = ".
46 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
53 $sha1 = qr/[a-f\d]{40}/;
54 $sha1_short = qr/[a-f\d]{4,40}/;
55 my ($_stdin, $_help, $_edit,
58 $_version, $_fetch_all,
59 $_merge, $_strategy, $_dry_run,
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 'fetch-all|all' => \
$_fetch_all,
150 'commit-diff' => [ \
&cmd_commit_diff
,
151 'Commit a diff between two trees',
152 { 'message|m=s' => \
$_message,
153 'file|F=s' => \
$_file,
154 'revision|r=s' => \
$_revision,
159 for (my $i = 0; $i < @ARGV; $i++) {
160 if (defined $cmd{$ARGV[$i]}) {
167 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
169 read_repo_config
(\
%opts);
170 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
171 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
172 'id|i=s' => \
$Git::SVN
::default_ref_id
,
173 'svn-remote|remote|R=s' => sub {
174 $Git::SVN
::no_reuse_existing
= 1;
175 $Git::SVN
::default_repo_id
= $_[1] });
176 exit 1 if (!$rv && $cmd ne 'log');
179 version
() if $_version;
180 usage
(1) unless defined $cmd;
181 load_authors
() if $_authors;
183 # make sure we're always running
184 unless ($cmd =~ /(?:clone|init|multi-init)$/) {
185 unless (-d
$ENV{GIT_DIR
}) {
186 if ($git_dir_user_set) {
187 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
188 "but it is not a directory\n";
190 my $git_dir = delete $ENV{GIT_DIR
};
191 chomp(my $cdup = command_oneline
(qw
/rev-parse --show-cdup/));
192 unless (length $cdup) {
193 die "Already at toplevel, but $git_dir ",
194 "not found '$cdup'\n";
196 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
197 unless (-d
$git_dir) {
198 die "$git_dir still not found after going to ",
201 $ENV{GIT_DIR
} = $git_dir;
204 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
205 Git
::SVN
::Migration
::migration_check
();
207 Git
::SVN
::init_vars
();
209 Git
::SVN
::verify_remotes_sanity
();
210 $cmd{$cmd}->[0]->(@ARGV);
213 post_fetch_checkout
();
216 ####################### primary functions ######################
218 my $exit = shift || 0;
219 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
221 git
-svn
- bidirectional operations between a single Subversion tree
and git
222 Usage
: $0 <command
> [options
] [arguments
]\n
224 print $fd "Available commands:\n" unless $cmd;
226 foreach (sort keys %cmd) {
227 next if $cmd && $cmd ne $_;
228 next if /^multi-/; # don't show deprecated commands
229 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
230 foreach (keys %{$cmd{$_}->[2]}) {
231 # prints out arguments as they should be passed:
232 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
233 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
235 split /\|/,$_)," $x\n";
239 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
240 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
241 one git repository and want to keep them separate. See git-svn(1) for more
248 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
253 unless (-d $ENV{GIT_DIR}) {
254 my @init_db = ('init
');
255 push @init_db, "--template=$_template" if defined $_template;
256 if (defined $_shared) {
257 if ($_shared =~ /[a-z]/) {
258 push @init_db, "--shared=$_shared";
260 push @init_db, "--shared";
263 command_noisy(@init_db);
266 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
267 foreach my $i (keys %icv) {
268 die "'$set' and '$i' cannot both be set\n" if $set;
269 next unless defined $icv{$i};
270 command_noisy('config
', "$pfx.$i", $icv{$i});
276 my $repo_path = shift or return;
277 mkpath([$repo_path]) unless -d $repo_path;
278 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
279 $ENV{GIT_DIR} = $repo_path . "/.git
";
283 my ($url, $path) = @_;
284 if (!defined $path &&
285 (defined $_trunk || defined $_branches || defined $_tags) &&
286 $url !~ m#^[a-z\+]+://#) {
289 warn "--path
: $path\n" if defined $path;
290 $path = basename($url) if !defined $path || !length $path;
291 warn "++path
: $path\n" if defined $path;
293 chdir $path or die "Couldn
't chdir to $path\n";
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) = working_head_info($head, \@refs);
368 unless (defined $url && defined $rev && defined $uuid) {
369 die "Unable to determine upstream SVN information from ",
372 my $gs = Git::SVN->find_by_url($url);
374 foreach my $d (@refs) {
375 if (!verify_ref("$d~1")) {
377 "has no parent commit, and therefore ",
378 "nothing to diff against.\n",
379 "You should be working from a repository ",
380 "originally created by git-svn\n";
382 unless (defined $last_rev) {
383 (undef, $last_rev, undef) = cmt_metadata("$d~1");
384 unless (defined $last_rev) {
385 fatal "Unable to extract revision information ",
386 "from commit $d~1\n";
390 print "diff-tree $d~1 $d\n";
392 my %ed_opts = ( r => $last_rev,
393 log => get_commit_entry($d)->{log},
394 ra => Git::SVN::Ra->new($url),
398 print "Committed r$_[0]\n";
399 $last_rev = $_[0]; },
401 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
402 print "No changes\n$d~1 == $d\n";
408 warn "Could not determine fetch information for $url\n",
409 "Will not attempt to fetch and rebase commits.\n",
410 "This probably means you have useSvmProps and should\n",
411 "now resync your SVN::Mirror repository.\n";
414 $_fetch_all ? $gs->fetch_all : $gs->fetch;
415 # we always want to rebase against the current HEAD, not any
416 # head that was passed to us
417 my @diff = command('diff
-tree
', 'HEAD
', $gs->refname, '--');
420 @finish = rebase_cmd();
421 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
422 "using @finish:\n", "@diff";
424 print "No changes between current HEAD and ",
425 $gs->refname, "\nResetting to the latest ",
427 @finish = qw/reset --mixed/;
429 command_noisy(@finish, $gs->refname);
433 command_noisy(qw/update-index --refresh/);
434 my $url = (working_head_info('HEAD
'))[0];
436 die "Unable to determine upstream SVN information from ",
437 "working tree history\n";
440 my $gs = Git::SVN->find_by_url($url);
441 if (command(qw/diff-index HEAD --/)) {
442 print STDERR "Cannot rebase with uncommited changes:\n";
443 command_noisy('status
');
446 $_fetch_all ? $gs->fetch_all : $gs->fetch;
447 command_noisy(rebase_cmd(), $gs->refname);
450 sub cmd_show_ignore {
451 my $url = (::working_head_info('HEAD
'))[0];
452 my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
453 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
454 $gs->traverse_ignore(\*STDOUT, '', $r);
459 unless (defined $_trunk || defined $_branches || defined $_tags) {
463 $_prefix = '' unless defined $_prefix;
468 if (defined $_trunk) {
469 my $trunk_ref = $_prefix . 'trunk
';
470 # try both old-style and new-style lookups:
471 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
473 my ($trunk_url, $trunk_path) =
474 complete_svn_url($url, $_trunk);
475 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
479 return unless defined $_branches || defined $_tags;
480 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
481 complete_url_ls_init($ra, $_branches, '--branches
/-b
', $_prefix);
482 complete_url_ls_init($ra, $_tags, '--tags
/-t', $_prefix . 'tags/');
485 sub cmd_multi_fetch {
486 my $remotes = Git::SVN::read_all_remotes();
487 foreach my $repo_id (sort keys %$remotes) {
488 if ($remotes->{$repo_id}->{url}) {
489 Git::SVN::fetch_all($repo_id, $remotes);
494 # this command is special because it requires no metadata
495 sub cmd_commit_diff {
496 my ($ta, $tb, $url) = @_;
497 my $usage = "Usage: $0 commit-diff -r<revision> ".
498 "<tree-ish> <tree-ish> [<URL>]\n";
499 fatal($usage) if (!defined $ta || !defined $tb);
502 my $gs = eval { Git::SVN->new };
504 fatal("Needed URL or usable git-svn --id in ",
505 "the command-line\n", $usage);
508 $svn_path = $gs->{path};
510 unless (defined $_revision) {
511 fatal("-r|--revision is a required argument\n", $usage);
513 if (defined $_message && defined $_file) {
514 fatal("Both --message/-m and --file/-F specified ",
515 "for the commit message.\n",
516 "I have no idea what you mean\n");
518 if (defined $_file) {
519 $_message = file_to_s($_file);
521 $_message ||= get_commit_entry($tb)->{log};
523 my $ra ||= Git::SVN::Ra->new($url);
524 $svn_path ||= $ra->{svn_path};
527 $r = $ra->get_latest_revnum;
528 } elsif ($r !~ /^\d+$/) {
529 die "revision argument: $r not understood by git-svn\n";
531 my %ed_opts = ( r => $r,
536 editor_cb => sub { print "Committed r$_[0]\n" },
537 svn_path => $svn_path );
538 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
539 print "No changes\n$ta == $tb\n";
543 ########################### utility functions #########################
546 my @cmd = qw/rebase/;
547 push @cmd, '-v
' if $_verbose;
548 push @cmd, qw/--merge/ if $_merge;
549 push @cmd, "--strategy=$_strategy" if $_strategy;
553 sub post_fetch_checkout {
554 return if $_no_checkout;
555 my $gs = $Git::SVN::_head or return;
556 return if verify_ref('refs
/heads/master
^0');
558 my $valid_head = verify_ref('HEAD
^0');
559 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
560 return if ($valid_head || !verify_ref
('HEAD^0'));
562 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
563 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
566 chomp(my $bare = `git config --bool --get core.bare`);
567 return if $bare eq 'true';
568 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
569 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
570 print STDERR
"Checked out HEAD:\n ",
571 $gs->full_url, " r", $gs->last_rev, "\n";
574 sub complete_svn_url
{
575 my ($url, $path) = @_;
577 if ($path !~ m
#^[a-z\+]+://#) {
578 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
579 fatal
("E: '$path' is not a complete URL ",
580 "and a separate URL is not specified\n");
582 return ($url, $path);
587 sub complete_url_ls_init
{
588 my ($ra, $repo_path, $switch, $pfx) = @_;
589 unless ($repo_path) {
590 print STDERR
"W: $switch not specified\n";
593 $repo_path =~ s
#/+$##;
594 if ($repo_path =~ m
#^[a-z\+]+://#) {
595 $ra = Git
::SVN
::Ra
->new($repo_path);
598 $repo_path =~ s
#^/+##;
600 fatal
("E: '$repo_path' is not a complete URL ",
601 "and a separate URL is not specified\n");
604 my $url = $ra->{url
};
605 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
606 my $k = "svn-remote.$gs->{repo_id}.url";
607 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
608 if ($orig_url && ($orig_url ne $gs->{url
})) {
609 die "$k already set: $orig_url\n",
610 "wanted to set to: $gs->{url}\n";
612 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
613 my $remote_path = "$ra->{svn_path}/$repo_path/*";
614 $remote_path =~ s
#/+#/#g;
615 $remote_path =~ s
#^/##g;
616 my ($n) = ($switch =~ /^--(\w+)/);
617 if (length $pfx && $pfx !~ m
#/$#) {
618 die "--prefix='$pfx' must have a trailing slash '/'\n";
620 command_noisy
('config', "svn-remote.$gs->{repo_id}.$n",
621 "$remote_path:refs/remotes/$pfx*");
626 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
630 sub get_tree_from_treeish
{
632 # $treeish can be a symbolic ref, too:
633 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
635 while ($type eq 'tag') {
636 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
638 if ($type eq 'commit') {
639 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
641 ($expected) = ($expected =~ /^tree ($sha1)$/o);
642 die "Unable to get tree from $treeish\n" unless $expected;
643 } elsif ($type eq 'tree') {
644 $expected = $treeish;
646 die "$treeish is a $type, expected tree, tag or commit\n";
651 sub get_commit_entry
{
652 my ($treeish) = shift;
653 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
654 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
655 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
656 open my $log_fh, '>', $commit_editmsg or croak
$!;
658 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
659 if ($type eq 'commit' || $type eq 'tag') {
660 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
665 $in_msg = 1 if (/^\s*$/);
666 } elsif (/^git-svn-id: /) {
667 # skip this for now, we regenerate the
668 # correct one on re-fetch anyways
669 # TODO: set *:merge properties or like...
671 print $log_fh $_ or croak
$!;
674 command_close_pipe
($msg_fh, $ctx);
676 close $log_fh or croak
$!;
678 if ($_edit || ($type eq 'tree')) {
679 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
680 # TODO: strip out spaces, comments, like git-commit.sh
681 system($editor, $commit_editmsg);
683 rename $commit_editmsg, $commit_msg or croak
$!;
684 open $log_fh, '<', $commit_msg or croak
$!;
685 { local $/; chomp($log_entry{log} = <$log_fh>); }
686 close $log_fh or croak
$!;
692 my ($str, $file, $mode) = @_;
693 open my $fd,'>',$file or croak
$!;
694 print $fd $str,"\n" or croak
$!;
695 close $fd or croak
$!;
696 chmod ($mode &~ umask, $file) if (defined $mode);
701 open my $fd,'<',$file or croak
"$!: file: $file\n";
704 close $fd or croak
$!;
709 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
711 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
712 my $log = $cmd eq 'log';
715 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
716 my ($user, $name, $email) = ($1, $2, $3);
718 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
720 $users{$user} = [$name, $email];
723 close $authors or croak
$!;
726 # convert GetOpt::Long specs for use by git-config
727 sub read_repo_config
{
728 return unless -d
$ENV{GIT_DIR
};
731 foreach my $o (keys %$opts) {
732 # if we have mixedCase and a long option-only, then
733 # it's a config-only variable that we don't need for
735 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
737 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
739 my $arg = 'git-config';
740 $arg .= ' --int' if ($o =~ /[:=]i$/);
741 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
742 if (ref $v eq 'ARRAY') {
743 chomp(my @tmp = `$arg --get-all svn.$key`);
746 chomp(my $tmp = `$arg --get svn.$key`);
747 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
752 delete @
$opts{@config_only} if @config_only;
755 sub extract_metadata
{
756 my $id = shift or return (undef, undef, undef);
757 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
759 if (!defined $rev || !$uuid || !$url) {
760 # some of the original repositories I made had
761 # identifiers like this:
762 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
764 return ($url, $rev, $uuid);
768 return extract_metadata
((grep(/^git-svn-id: /,
769 command
(qw
/cat-file commit/, shift)))[-1]);
772 sub working_head_info
{
773 my ($head, $refs) = @_;
774 my ($url, $rev, $uuid);
775 my ($fh, $ctx) = command_output_pipe
('rev-list', $head);
778 ($url, $rev, $uuid) = cmt_metadata
($_);
779 last if (defined $url && defined $rev && defined $uuid);
780 unshift @
$refs, $_ if $refs;
782 close $fh; # break the pipe
789 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
790 $_repack $_repack_flags $_use_svm_props $_head
791 $_use_svnsync_props $no_reuse_existing/;
793 use File
::Path qw
/mkpath/;
794 use File
::Copy qw
/copy/;
798 # properties that we do not log:
801 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
802 svn
:special svn
:executable
803 svn
:entry
:committed
-rev
804 svn
:entry
:last-author
806 svn
:entry
:committed
-date
/;
808 # some options are read globally, but can be overridden locally
809 # per [svn-remote "..."] section. Command-line options will *NOT*
810 # override options set in an [svn-remote "..."] section
812 foreach (qw
/follow_parent no_metadata use_svm_props
813 use_svnsync_props
/) {
818 return \$self->{-$_} if exists \$self->{-$_};
819 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
820 eval { command_oneline(qw/config --get/, \$k) };
822 \$self->{-$_} = \$Git::SVN::_$_;
824 my \$v = command_oneline(qw/config --bool/,\$k);
825 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
827 return \$self->{-$_} }\n";
834 END { unlink keys %LOCKFILES if %LOCKFILES }
836 sub resolve_local_globs
{
837 my ($url, $fetch, $glob_spec) = @_;
838 return unless defined $glob_spec;
839 my $ref = $glob_spec->{ref};
840 my $path = $glob_spec->{path
};
841 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
842 next unless m
#^refs/remotes/$ref->{regex}$#;
844 my $pathname = $path->full_path($p);
845 my $refname = $ref->full_path($p);
846 if (my $existing = $fetch->{$pathname}) {
847 if ($existing ne $refname) {
848 die "Refspec conflict:\n",
849 "existing: refs/remotes/$existing\n",
850 " globbed: refs/remotes/$refname\n";
852 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
853 $u =~ s!^\Q$url\E(/|$)!! or die
854 "refs/remotes/$refname: '$url' not found in '$u'\n";
855 if ($pathname ne $u) {
856 warn "W: Refspec glob conflict ",
857 "(ref: refs/remotes/$refname):\n",
858 "expected path: $pathname\n",
860 "Continuing ahead with $u\n";
864 $fetch->{$pathname} = $refname;
869 sub parse_revision_argument
{
870 my ($base, $head) = @_;
871 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
872 return ($base, $head);
874 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
875 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
876 return ($head, $head) if ($::_revision
eq 'HEAD');
877 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
878 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
879 die "revision argument: $::_revision not understood by git-svn\n";
883 my ($repo_id, $remotes) = @_;
887 $repo_id = $gs->{repo_id
};
889 $remotes ||= read_all_remotes
();
890 my $remote = $remotes->{$repo_id} or
891 die "[svn-remote \"$repo_id\"] unknown\n";
892 my $fetch = $remote->{fetch
};
893 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
895 my $ra = Git
::SVN
::Ra
->new($url);
896 my $uuid = $ra->get_uuid;
897 my $head = $ra->get_latest_revnum;
898 my $base = defined $fetch ?
$head : 0;
900 # read the max revs for wildcard expansion (branches/*, tags/*)
901 foreach my $t (qw
/branches tags/) {
902 defined $remote->{$t} or next;
903 push @globs, $remote->{$t};
904 my $max_rev = eval { tmp_config
(qw
/--int --get/,
905 "svn-remote.$repo_id.${t}-maxRev") };
906 if (defined $max_rev && ($max_rev < $base)) {
908 } elsif (!defined $max_rev) {
914 foreach my $p (sort keys %$fetch) {
915 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
916 my $lr = $gs->rev_db_max;
918 $base = $lr if ($lr < $base);
924 ($base, $head) = parse_revision_argument
($base, $head);
925 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
928 sub read_all_remotes
{
930 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
931 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
932 $r->{$1}->{fetch
}->{$2} = $3;
933 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
934 $r->{$1}->{url
} = $2;
935 } elsif (m
!^(.+)\
.(branches
|tags
)=
936 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
937 my ($p, $g) = ($3, $4);
938 my $rs = $r->{$1}->{$2} = {
941 path
=> Git
::SVN
::GlobSpec
->new($p),
942 ref => Git
::SVN
::GlobSpec
->new($g) };
943 if (length($rs->{ref}->{right
}) != 0) {
944 die "The '*' glob character must be the last ",
945 "character of '$g'\n";
953 if (defined $_repack) {
954 $_repack = 1000 if ($_repack <= 0);
955 $_repack_nr = $_repack;
956 $_repack_flags ||= '-d';
960 sub verify_remotes_sanity
{
961 return unless -d
$ENV{GIT_DIR
};
963 foreach (command
(qw
/config -l/)) {
964 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
966 die "Remote ref refs/remote/$1 is tracked by",
967 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
968 "Please resolve this ambiguity in ",
969 "your git configuration file before ",
977 # we allow more chars than remotes2config.sh...
978 sub sanitize_remote_name
{
980 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
984 sub find_existing_remote
{
985 my ($url, $remotes) = @_;
986 return undef if $no_reuse_existing;
988 foreach my $repo_id (keys %$remotes) {
989 my $u = $remotes->{$repo_id}->{url
} or next;
991 $existing = $repo_id;
997 sub init_remote_config
{
998 my ($self, $url, $no_write) = @_;
999 $url =~ s!/+$!!; # strip trailing slash
1000 my $r = read_all_remotes
();
1001 my $existing = find_existing_remote
($url, $r);
1003 unless ($no_write) {
1004 print STDERR
"Using existing ",
1005 "[svn-remote \"$existing\"]\n";
1007 $self->{repo_id
} = $existing;
1009 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1010 $existing = find_existing_remote
($min_url, $r);
1012 unless ($no_write) {
1013 print STDERR
"Using existing ",
1014 "[svn-remote \"$existing\"]\n";
1016 $self->{repo_id
} = $existing;
1018 if ($min_url ne $url) {
1019 unless ($no_write) {
1020 print STDERR
"Using higher level of URL: ",
1021 "$url => $min_url\n";
1023 my $old_path = $self->{path
};
1024 $self->{path
} = $url;
1025 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1026 if (length $old_path) {
1027 $self->{path
} .= "/$old_path";
1034 # verify that we aren't overwriting anything:
1036 command_oneline
('config', '--get',
1037 "svn-remote.$self->{repo_id}.url")
1039 if ($orig_url && ($orig_url ne $url)) {
1040 die "svn-remote.$self->{repo_id}.url already set: ",
1041 "$orig_url\nwanted to set to: $url\n";
1044 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1045 if (defined $xpath) {
1046 die "svn-remote.$xrepo_id.fetch already set to track ",
1047 "$xpath:refs/remotes/", $self->refname, "\n";
1049 unless ($no_write) {
1050 command_noisy
('config',
1051 "svn-remote.$self->{repo_id}.url", $url);
1052 command_noisy
('config', '--add',
1053 "svn-remote.$self->{repo_id}.fetch",
1054 "$self->{path}:".$self->refname);
1056 $self->{url
} = $url;
1059 sub find_by_url
{ # repos_root and, path are optional
1060 my ($class, $full_url, $repos_root, $path) = @_;
1061 return undef unless defined $full_url;
1062 my $remotes = read_all_remotes
();
1063 if (defined $full_url && defined $repos_root && !defined $path) {
1065 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1067 foreach my $repo_id (keys %$remotes) {
1068 my $u = $remotes->{$repo_id}->{url
} or next;
1069 next if defined $repos_root && $repos_root ne $u;
1071 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1072 foreach (qw
/branches tags/) {
1073 resolve_local_globs
($u, $fetch,
1074 $remotes->{$repo_id}->{$_});
1077 unless (defined $p) {
1079 $p =~ s
#^\Q$u\E(?:/|$)## or next;
1081 foreach my $f (keys %$fetch) {
1083 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1090 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1091 my $self = _new
($class, $repo_id, $ref_id, $path);
1093 $self->init_remote_config($url, $no_write);
1100 foreach (command
(qw
/config -l/)) {
1101 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1102 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1103 my ($repo_id, $path, $ref) = ($1, $2, $3);
1104 if ($ref eq $ref_id) {
1105 $path = '' if ($path =~ m
#^\./?#);
1106 return ($repo_id, $path);
1109 (undef, undef, undef);
1113 my ($class, $ref_id, $repo_id, $path) = @_;
1114 if (defined $ref_id && !defined $repo_id && !defined $path) {
1115 ($repo_id, $path) = find_ref
($ref_id);
1116 if (!defined $repo_id) {
1117 die "Could not find a \"svn-remote.*.fetch\" key ",
1118 "in the repository configuration matching: ",
1119 "refs/remotes/$ref_id\n";
1122 my $self = _new
($class, $repo_id, $ref_id, $path);
1123 if (!defined $self->{path
} || !length $self->{path
}) {
1124 my $fetch = command_oneline
('config', '--get',
1125 "svn-remote.$repo_id.fetch",
1126 ":refs/remotes/$ref_id\$") or
1127 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1128 "\":refs/remotes/$ref_id\$\" in config\n";
1129 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1131 $self->{url
} = command_oneline
('config', '--get',
1132 "svn-remote.$repo_id.url") or
1133 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1138 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
1142 return $self->{svm
}->{uuid
} if $self->svm;
1144 unless ($self->{svm
}) {
1145 die "SVM UUID not cached, and reading remotely failed\n";
1147 $self->{svm
}->{uuid
};
1152 return $self->{svm
} if $self->{svm
};
1154 # see if we have it in our config, first:
1156 my $section = "svn-remote.$self->{repo_id}";
1158 source
=> tmp_config
('--get', "$section.svm-source"),
1159 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1160 replace
=> tmp_config
('--get', "$section.svm-replace"),
1163 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1164 $self->{svm
} = $svm;
1170 my ($self, $ra) = @_;
1171 return $ra if $self->svm;
1173 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1174 "(svm:source, svm:uuid) ",
1175 "from the following URLs:\n" );
1176 sub read_svm_props
{
1177 my ($self, $ra, $path, $r) = @_;
1178 my $props = ($ra->get_dir($path, $r))[2];
1179 my $src = $props->{'svm:source'};
1180 my $uuid = $props->{'svm:uuid'};
1181 return undef if (!$src || !$uuid);
1185 $uuid =~ m{^[0-9a-f\-]{30,}$}
1186 or die "doesn't look right - svm:uuid is '$uuid'\n";
1188 # the '!' is used to mark the repos_root!/relative/path
1189 $src =~ s{/?!/?}{/};
1190 $src =~ s{/+$}{}; # no trailing slashes please
1191 # username is of no interest
1192 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1194 my $replace = $ra->{url
};
1195 $replace .= "/$path" if length $path;
1197 my $section = "svn-remote.$self->{repo_id}";
1198 tmp_config
("$section.svm-source", $src);
1199 tmp_config
("$section.svm-replace", $replace);
1200 tmp_config
("$section.svm-uuid", $uuid);
1208 my $r = $ra->get_latest_revnum;
1209 my $path = $self->{path
};
1211 while (length $path) {
1212 unless ($tried{"$self->{url}/$path"}) {
1213 return $ra if $self->read_svm_props($ra, $path, $r);
1214 $tried{"$self->{url}/$path"} = 1;
1216 $path =~ s
#/?[^/]+$##;
1218 die "Path: '$path' should be ''\n" if $path ne '';
1219 return $ra if $self->read_svm_props($ra, $path, $r);
1220 $tried{"$self->{url}/$path"} = 1;
1222 if ($ra->{repos_root
} eq $self->{url
}) {
1223 die @err, (map { " $_\n" } keys %tried), "\n";
1226 # nope, make sure we're connected to the repository root:
1229 $path = $ra->{svn_path
};
1230 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1231 while (length $path) {
1232 unless ($tried{"$ra->{url}/$path"}) {
1233 $ok = $self->read_svm_props($ra, $path, $r);
1235 $tried{"$ra->{url}/$path"} = 1;
1237 $path =~ s
#/?[^/]+$##;
1239 die "Path: '$path' should be ''\n" if $path ne '';
1240 $ok ||= $self->read_svm_props($ra, $path, $r);
1241 $tried{"$ra->{url}/$path"} = 1;
1243 die @err, (map { " $_\n" } keys %tried), "\n";
1245 Git
::SVN
::Ra
->new($self->{url
});
1250 return $self->{svnsync
} if $self->{svnsync
};
1252 if ($self->no_metadata) {
1253 die "Can't have both 'noMetadata' and ",
1254 "'useSvnsyncProps' options set!\n";
1256 if ($self->rewrite_root) {
1257 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1262 # see if we have it in our config, first:
1264 my $section = "svn-remote.$self->{repo_id}";
1266 url
=> tmp_config
('--get', "$section.svnsync-url"),
1267 uuid
=> tmp_config
('--get', "$section.svnsync-uuid"),
1270 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1271 return $self->{svnsync
} = $svnsync;
1274 my $err = "useSvnsyncProps set, but failed to read " .
1275 "svnsync property: svn:sync-from-";
1276 my $rp = $self->ra->rev_proplist(0);
1278 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1279 $url =~ m{^[a-z\+]+://} or
1280 die "doesn't look right - svn:sync-from-url is '$url'\n";
1282 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1283 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1284 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1286 my $section = "svn-remote.$self->{repo_id}";
1287 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1288 tmp_config
('--add', "$section.svnsync-url", $url);
1289 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1292 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1293 # remote lookup (useful for 'git svn log').
1296 unless ($self->{ra_uuid
}) {
1297 my $key = "svn-remote.$self->{repo_id}.uuid";
1298 my $uuid = eval { tmp_config
('--get', $key) };
1299 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1300 $self->{ra_uuid
} = $uuid;
1302 die "ra_uuid called without URL\n" unless $self->{url
};
1303 $self->{ra_uuid
} = $self->ra->get_uuid;
1304 tmp_config
('--add', $key, $self->{ra_uuid
});
1312 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1313 if ($self->use_svm_props && !$self->{svm
}) {
1314 if ($self->no_metadata) {
1315 die "Can't have both 'noMetadata' and ",
1316 "'useSvmProps' options set!\n";
1317 } elsif ($self->use_svnsync_props) {
1318 die "Can't have both 'useSvnsyncProps' and ",
1319 "'useSvmProps' options set!\n";
1321 $ra = $self->_set_svm_vars($ra);
1322 $self->{-want_revprops
} = 1;
1329 my $repos_root = $self->ra->{repos_root
};
1330 return $self->{path
} if ($self->{url
} eq $repos_root);
1331 die "BUG: rel_path failed! repos_root: $repos_root, Ra URL: ",
1332 $self->ra->{url
}, " path: $self->{path}, URL: $self->{url}\n";
1335 sub traverse_ignore
{
1336 my ($self, $fh, $path, $r) = @_;
1339 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
1341 $p =~ s
#^\Q$ra->{svn_path}\E/##;
1342 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
1343 if (my $s = $props->{'svn:ignore'}) {
1344 $s =~ s/[\r\n]+/\n/g;
1346 if (length $p == 0) {
1350 $s =~ s
#\n#\n/$p/#g;
1351 print $fh "/$p/$s\n";
1354 foreach (sort keys %$dirent) {
1355 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
1356 $self->traverse_ignore($fh, "$path/$_", $r);
1360 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1361 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1363 # returns the newest SVN revision number and newest commit SHA1
1364 sub last_rev_commit
{
1366 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1367 return ($self->{last_rev
}, $self->{last_commit
});
1369 my $c = ::verify_ref
($self->refname.'^0');
1370 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1371 my $rev = (::cmt_metadata
($c))[1];
1373 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1377 my $db_path = $self->db_path;
1378 unless (-e
$db_path) {
1379 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1380 return (undef, undef);
1382 my $offset = -41; # from tail
1384 open my $fh, '<', $db_path or croak
"$db_path not readable: $!\n";
1385 sysseek($fh, $offset, 2); # don't care for errors
1386 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1388 while (('0' x40
) eq $rl && sysseek($fh, 0, 1) != 0) {
1390 sysseek($fh, $offset, 2); # don't care for errors
1391 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1394 if ($c && $c ne $rl) {
1395 die "$db_path and ", $self->refname,
1396 " inconsistent!:\n$c != $rl\n";
1398 my $rev = sysseek($fh, 0, 1) or croak
$!;
1399 $rev = ($rev - 41) / 41;
1400 close $fh or croak
$!;
1401 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1405 sub get_fetch_range
{
1406 my ($self, $min, $max) = @_;
1407 $max ||= $self->ra->get_latest_revnum;
1408 $min ||= $self->rev_db_max;
1414 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1415 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1416 if (-e
$old_def_config && ! -e
$config) {
1417 rename $old_def_config, $config or
1418 die "Failed rename $old_def_config => $config: $!\n";
1420 my $old_config = $ENV{GIT_CONFIG
};
1421 $ENV{GIT_CONFIG
} = $config;
1424 unless (-f
$config) {
1426 open my $fh, '>', $config or
1427 die "Can't open $config: $!\n";
1428 print $fh "; This file is used internally by ",
1430 "Couldn't write to $config: $!\n";
1431 print $fh "; You should not have to edit it\n" or
1432 die "Couldn't write to $config: $!\n";
1433 close $fh or die "Couldn't close $config: $!\n";
1435 command
('config', @args);
1438 if (defined $old_config) {
1439 $ENV{GIT_CONFIG
} = $old_config;
1441 delete $ENV{GIT_CONFIG
};
1444 wantarray ?
@ret : $ret[0];
1448 my ($self, $sub) = @_;
1449 my $old_index = $ENV{GIT_INDEX_FILE
};
1450 $ENV{GIT_INDEX_FILE
} = $self->{index};
1453 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
1454 mkpath
([$dir]) unless -d
$dir;
1458 if (defined $old_index) {
1459 $ENV{GIT_INDEX_FILE
} = $old_index;
1461 delete $ENV{GIT_INDEX_FILE
};
1464 wantarray ?
@ret : $ret[0];
1467 sub assert_index_clean
{
1468 my ($self, $treeish) = @_;
1470 $self->tmp_index_do(sub {
1471 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1472 my $x = command_oneline
('write-tree');
1473 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1474 /^tree ($::sha1)/mo);
1477 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1478 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1479 command_noisy
('read-tree', $treeish);
1480 $x = command_oneline
('write-tree');
1482 ::fatal
"trees ($treeish) $y != $x\n",
1483 "Something is seriously wrong...\n";
1488 sub get_commit_parents
{
1489 my ($self, $log_entry) = @_;
1490 my (%seen, @ret, @tmp);
1491 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1492 if (my $ip = $self->{inject_parents
}) {
1493 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
1497 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1500 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1501 while (my $p = shift @tmp) {
1505 # MAXPARENT is defined to 16 in commit-tree.c:
1509 die "r$log_entry->{revision}: No room for parents:\n\t",
1510 join("\n\t", @tmp), "\n";
1517 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
1518 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1519 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
1522 if ($rwr !~ m
#^[a-z\+]+://#) {
1523 die "$rwr is not a valid URL (key: $k)\n";
1526 $self->{-rewrite_root
} = $rwr;
1531 ($self->rewrite_root || $self->{url
}) .
1532 (length $self->{path
} ?
'/' . $self->{path
} : '');
1537 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
1541 my ($self, $log_entry) = @_;
1542 my $lr = $self->last_rev;
1543 if (defined $lr && $lr >= $log_entry->{revision
}) {
1544 die "Last fetched revision of ", $self->refname,
1545 " was r$lr, but we are about to fetch: ",
1546 "r$log_entry->{revision}!\n";
1548 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1549 croak
"$log_entry->{revision} = $c already exists! ",
1550 "Why are we refetching it?\n";
1552 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $log_entry->{name
};
1553 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
1554 $log_entry->{email
};
1555 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1557 my $tree = $log_entry->{tree
};
1558 if (!defined $tree) {
1559 $tree = $self->tmp_index_do(sub {
1560 command_oneline
('write-tree') });
1562 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1564 my @exec = ('git-commit-tree', $tree);
1565 foreach ($self->get_commit_parents($log_entry)) {
1566 push @exec, '-p', $_;
1568 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1570 print $msg_fh $log_entry->{log} or croak
$!;
1571 unless ($self->no_metadata) {
1572 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1575 $msg_fh->flush == 0 or croak
$!;
1576 close $msg_fh or croak
$!;
1577 chomp(my $commit = do { local $/; <$out_fh> });
1578 close $out_fh or croak
$!;
1581 if ($commit !~ /^$::sha1$/o) {
1582 die "Failed to commit, invalid sha1: $commit\n";
1585 $self->rev_db_set($log_entry->{revision
}, $commit, 1);
1587 $self->{last_rev
} = $log_entry->{revision
};
1588 $self->{last_commit
} = $commit;
1589 print "r$log_entry->{revision}";
1590 if (defined $log_entry->{svm_revision
}) {
1591 print " (\@$log_entry->{svm_revision})";
1592 $self->rev_db_set($log_entry->{svm_revision
}, $commit,
1593 0, $self->svm_uuid);
1595 print " = $commit ($self->{ref_id})\n";
1596 if (defined $_repack && (--$_repack_nr == 0)) {
1597 $_repack_nr = $_repack;
1598 # repack doesn't use any arguments with spaces in them, does it?
1599 print "Running git repack $_repack_flags ...\n";
1600 command_noisy
('repack', split(/\s+/, $_repack_flags));
1601 print "Done repacking\n";
1607 my ($self, $paths, $r) = @_;
1608 return 1 if $self->{path
} eq '';
1609 if (my $path = $paths->{"/$self->{path}"}) {
1610 return ($path->{action
} eq 'D') ?
0 : 1;
1612 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
1613 if (grep /$self->{path_regex}/, keys %$paths) {
1617 foreach (split m
#/#, $self->{path}) {
1619 next unless ($paths->{$c} &&
1620 ($paths->{$c}->{action
} =~ /^[AR]$/));
1621 if ($self->ra->check_path($self->{path
}, $r) ==
1629 sub find_parent_branch
{
1630 my ($self, $paths, $rev) = @_;
1631 return undef unless $self->follow_parent;
1632 unless (defined $paths) {
1633 my $err_handler = $SVN::Error
::handler
;
1634 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
1635 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
1637 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
1638 $SVN::Error
::handler
= $err_handler;
1640 return undef unless defined $paths;
1642 # look for a parent from another branch:
1643 my @b_path_components = split m
#/#, $self->rel_path;
1644 my @a_path_components;
1646 while (@b_path_components) {
1647 $i = $paths->{'/'.join('/', @b_path_components)};
1648 last if $i && defined $i->{copyfrom_path
};
1649 unshift(@a_path_components, pop(@b_path_components));
1651 return undef unless defined $i && defined $i->{copyfrom_path
};
1652 my $branch_from = $i->{copyfrom_path
};
1653 if (@a_path_components) {
1654 print STDERR
"branch_from: $branch_from => ";
1655 $branch_from .= '/'.join('/', @a_path_components);
1656 print STDERR
$branch_from, "\n";
1658 my $r = $i->{copyfrom_rev
};
1659 my $repos_root = $self->ra->{repos_root
};
1660 my $url = $self->ra->{url
};
1661 my $new_url = $repos_root . $branch_from;
1662 print STDERR
"Found possible branch point: ",
1663 "$new_url => ", $self->full_url, ", $r\n";
1664 $branch_from =~ s
#^/##;
1665 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
1667 my $ref_id = $self->{ref_id
};
1668 $ref_id =~ s/\@\d+$//;
1670 # just grow a tail if we're not unique enough :x
1671 $ref_id .= '-' while find_ref
($ref_id);
1672 print STDERR
"Initializing parent: $ref_id\n";
1673 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id, 1);
1675 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1676 if (!defined $r0 || !defined $parent) {
1678 ($r0, $parent) = $gs->last_rev_commit;
1680 if (defined $r0 && defined $parent) {
1681 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1682 $self->assert_index_clean($parent);
1684 if ($self->ra->can_do_switch) {
1685 print STDERR
"Following parent with do_switch\n";
1686 # do_switch works with svn/trunk >= r22312, but that
1687 # is not included with SVN 1.4.3 (the latest version
1688 # at the moment), so we can't rely on it
1689 $self->{last_commit
} = $parent;
1690 $ed = SVN
::Git
::Fetcher
->new($self);
1691 $gs->ra->gs_do_switch($r0, $rev, $gs,
1692 $self->full_url, $ed)
1693 or die "SVN connection failed somewhere...\n";
1695 print STDERR
"Following parent with do_update\n";
1696 $ed = SVN
::Git
::Fetcher
->new($self);
1697 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1698 or die "SVN connection failed somewhere...\n";
1700 print STDERR
"Successfully followed parent\n";
1701 return $self->make_log_entry($rev, [$parent], $ed);
1707 my ($self, $paths, $rev) = @_;
1709 my ($last_rev, @parents);
1710 if (my $lc = $self->last_commit) {
1711 # we can have a branch that was deleted, then re-added
1712 # under the same name but copied from another path, in
1713 # which case we'll have multiple parents (we don't
1714 # want to break the original ref, nor lose copypath info):
1715 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1716 push @
{$log_entry->{parents
}}, $lc;
1719 $ed = SVN
::Git
::Fetcher
->new($self);
1720 $last_rev = $self->{last_rev
};
1725 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1728 $ed = SVN
::Git
::Fetcher
->new($self);
1730 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1731 die "SVN connection failed somewhere...\n";
1733 $self->make_log_entry($rev, \
@parents, $ed);
1737 my ($self, $ed) = @_;
1739 my $h = $ed->{empty
};
1740 foreach (sort keys %$h) {
1741 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1742 push @out, " $act: " . uri_encode
($_);
1743 warn "W: $act: $_\n";
1745 foreach my $t (qw
/dir_prop file_prop/) {
1746 $h = $ed->{$t} or next;
1747 foreach my $path (sort keys %$h) {
1748 my $ppath = $path eq '' ?
'.' : $path;
1749 foreach my $prop (sort keys %{$h->{$path}}) {
1750 next if $SKIP_PROP{$prop};
1751 my $v = $h->{$path}->{$prop};
1752 my $t_ppath_prop = "$t: " .
1753 uri_encode
($ppath) . ' ' .
1756 push @out, " +$t_ppath_prop " .
1759 push @out, " -$t_ppath_prop";
1764 foreach my $t (qw
/absent_file absent_directory/) {
1765 $h = $ed->{$t} or next;
1766 foreach my $parent (sort keys %$h) {
1767 foreach my $path (sort @
{$h->{$parent}}) {
1768 push @out, " $t: " .
1769 uri_encode
("$parent/$path");
1770 warn "W: $t: $parent/$path ",
1771 "Insufficient permissions?\n";
1778 sub parse_svn_date
{
1779 my $date = shift || return '+0000 1970-01-01 00:00:00';
1780 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1781 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1782 croak
"Unable to parse date: $date\n";
1783 "+0000 $Y-$m-$d $H:$M:$S";
1788 if (!defined $author || length $author == 0) {
1789 $author = '(no author)';
1791 if (defined $::_authors
&& ! defined $::users
{$author}) {
1792 die "Author: $author not defined in $::_authors file\n";
1797 sub make_log_entry
{
1798 my ($self, $rev, $parents, $ed) = @_;
1799 my $untracked = $self->get_untracked($ed);
1801 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1802 print $un "r$rev\n" or croak
$!;
1803 print $un $_, "\n" foreach @
$untracked;
1804 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1808 my $logged = delete $self->{logged_rev_props
};
1809 if (!$logged || $self->{-want_revprops
}) {
1810 my $rp = $self->ra->rev_proplist($rev);
1811 foreach (sort keys %$rp) {
1813 if (/^svn:(author|date|log)$/) {
1814 $log_entry{$1} = $v;
1815 } elsif ($_ eq 'svm:headrev') {
1818 print $un " rev_prop: ", uri_encode
($_), ' ',
1819 uri_encode
($v), "\n";
1823 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1825 close $un or croak
$!;
1827 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1828 $log_entry{log} .= "\n";
1829 my $author = $log_entry{author
} = check_author
($log_entry{author
});
1830 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
1832 if (defined $headrev && $self->use_svm_props) {
1833 if ($self->rewrite_root) {
1834 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1837 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
1838 # we don't want "SVM: initializing mirror for junk" ...
1839 return undef if $r == 0;
1840 my $svm = $self->svm;
1841 if ($uuid ne $svm->{uuid
}) {
1842 die "UUID mismatch on SVM path:\n",
1843 "expected: $svm->{uuid}\n",
1846 my $full_url = $self->full_url;
1847 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1848 die "Failed to replace '$svm->{replace}' with ",
1849 "'$svm->{source}' in $full_url\n";
1850 $log_entry{metadata
} = "$full_url\@$r $uuid";
1851 $log_entry{svm_revision
} = $r;
1852 $email ||= "$author\@$uuid"
1853 } elsif ($self->use_svnsync_props) {
1854 my $full_url = $self->svnsync->{url
};
1855 $full_url .= "/$self->{path}" if length $self->{path
};
1856 my $uuid = $self->svnsync->{uuid
};
1857 $log_entry{metadata
} = "$full_url\@$rev $uuid";
1858 $email ||= "$author\@$uuid"
1860 $log_entry{metadata
} = $self->metadata_url. "\@$rev " .
1861 $self->ra->get_uuid;
1862 $email ||= "$author\@" . $self->ra->get_uuid;
1864 $log_entry{name
} = $name;
1865 $log_entry{email
} = $email;
1870 my ($self, $min_rev, $max_rev, @parents) = @_;
1871 my ($last_rev, $last_commit) = $self->last_rev_commit;
1872 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1873 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1877 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1878 $self->{inject_parents
} = { $rev => $tree };
1879 $self->fetch(undef, undef);
1883 my ($self, $tree) = (shift, shift);
1884 my $log_entry = ::get_commit_entry
($tree);
1885 unless ($self->{last_rev
}) {
1886 fatal
("Must have an existing revision to commit\n");
1888 my %ed_opts = ( r
=> $self->{last_rev
},
1889 log => $log_entry->{log},
1891 tree_a
=> $self->{last_commit
},
1894 $self->set_tree_cb($log_entry, $tree, @_) },
1895 svn_path
=> $self->{path
} );
1896 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
1897 print "No changes\nr$self->{last_rev} = $tree\n";
1903 my $db_path = $self->db_path;
1904 return if (-e
$db_path && ! -z
$db_path);
1905 return unless ::verify_ref
($self->refname.'^0');
1906 if (-f
$self->{db_root
}) {
1907 rename $self->{db_root
}, $db_path or die
1908 "rename $self->{db_root} => $db_path failed: $!\n";
1909 my ($dir, $base) = ($db_path =~ m
#^(.*?)/?([^/]+)$#);
1910 symlink $base, $self->{db_root
} or die
1911 "symlink $base => $self->{db_root} failed: $!\n";
1914 print "Rebuilding $db_path ...\n";
1915 my ($rev_list, $ctx) = command_output_pipe
("rev-list", $self->refname);
1917 my $full_url = $self->full_url;
1919 while (<$rev_list>) {
1922 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1923 my ($url, $rev, $uuid) = ::cmt_metadata
($c);
1925 # ignore merges (from set-tree)
1926 next if (!defined $rev || !$uuid);
1928 # if we merged or otherwise started elsewhere, this is
1929 # how we break out of it
1930 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1931 ($full_url && $url && ($url ne $full_url))) {
1935 $svn_uuid ||= $uuid;
1937 $self->rev_db_set($rev, $c);
1938 print "r$rev = $c\n";
1940 command_close_pipe
($rev_list, $ctx);
1941 print "Done rebuilding $db_path\n";
1945 # Tie::File seems to be prone to offset errors if revisions get sparse,
1946 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1947 # one of my favorite modules is out :< Next up would be one of the DBM
1948 # modules, but I'm not sure which is most portable... So I'll just
1949 # go with something that's plain-text, but still capable of
1950 # being randomly accessed. So here's my ultra-simple fixed-width
1951 # database. All records are 40 characters + "\n", so it's easy to seek
1952 # to a revision: (41 * rev) is the byte offset.
1953 # A record of 40 0s denotes an empty revision.
1954 # And yes, it's still pretty fast (faster than Tie::File).
1955 # These files are disposable unless noMetadata or useSvmProps is set
1958 my ($fh, $rev, $commit) = @_;
1959 my $offset = $rev * 41;
1960 # assume that append is the common case:
1961 seek $fh, 0, 2 or croak
$!;
1963 if ($pos < $offset) {
1964 for (1 .. (($offset - $pos) / 41)) {
1965 print $fh (('0' x
40),"\n") or croak
$!;
1968 seek $fh, $offset, 0 or croak
$!;
1969 print $fh $commit,"\n" or croak
$!;
1975 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1976 mkpath
([$dir]) unless -d
$dir;
1977 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1978 close $fh or die "Couldn't close (create) $path: $!\n";
1983 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1984 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1985 my $db = $self->db_path($uuid);
1986 my $db_lock = "$db.lock";
1989 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
1990 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
1994 $LOCKFILES{$db_lock} = 1;
1996 # both of these options make our .rev_db file very, very important
1997 # and we can't afford to lose it because rebuild() won't work
1998 if ($self->use_svm_props || $self->no_metadata) {
2000 copy
($db, $db_lock) or die "rev_db_set(@_): ",
2002 "$db => $db_lock ($!)\n";
2004 rename $db, $db_lock or die "rev_db_set(@_): ",
2005 "Failed to rename: ",
2006 "$db => $db_lock ($!)\n";
2008 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2009 _rev_db_set
($fh, $rev, $commit);
2011 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2012 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2014 close $fh or croak
$!;
2017 command_noisy
('update-ref', '-m', "r$rev",
2018 $self->refname, $commit);
2020 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2021 "$db_lock => $db ($!)\n";
2022 delete $LOCKFILES{$db_lock};
2024 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2025 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2026 kill $sig, $$ if defined $sig;
2033 my $db_path = $self->db_path;
2034 my @stat = stat $db_path or return 0;
2035 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2036 my $max = $stat[7] / 41;
2037 (($max > 0) ?
$max - 1 : 0);
2041 my ($self, $rev, $uuid) = @_;
2043 my $offset = $rev * 41;
2044 my $db_path = $self->db_path($uuid);
2045 return undef unless -e
$db_path;
2046 open my $fh, '<', $db_path or croak
$!;
2047 if (sysseek($fh, $offset, 0) == $offset) {
2048 my $read = sysread($fh, $ret, 40);
2049 $ret = undef if ($read != 40 || $ret eq ('0'x40
));
2051 close $fh or croak
$!;
2055 sub find_rev_before
{
2056 my ($self, $rev, $eq_ok) = @_;
2057 --$rev unless $eq_ok;
2059 if (my $c = $self->rev_db_get($rev)) {
2064 return (undef, undef);
2068 my ($class, $repo_id, $ref_id, $path) = @_;
2069 unless (defined $repo_id && length $repo_id) {
2070 $repo_id = $Git::SVN
::default_repo_id
;
2072 unless (defined $ref_id && length $ref_id) {
2073 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2075 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
2076 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2077 $_[3] = $path = '' unless (defined $path);
2078 mkpath
(["$ENV{GIT_DIR}/svn"]);
2080 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2081 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2082 db_root
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
2086 my ($self, $uuid) = @_;
2087 $uuid ||= $self->ra_uuid;
2088 "$self->{db_root}.$uuid";
2093 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2097 package Git
::SVN
::Prompt
;
2101 use vars qw
/$_no_auth_cache $_username/;
2104 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2105 $may_save = undef if $_no_auth_cache;
2106 $default_username = $_username if defined $_username;
2107 if (defined $default_username && length $default_username) {
2108 if (defined $realm && length $realm) {
2109 print STDERR
"Authentication realm: $realm\n";
2112 $cred->username($default_username);
2114 username
($cred, $realm, $may_save, $pool);
2116 $cred->password(_read_password
("Password for '" .
2117 $cred->username . "': ", $realm));
2118 $cred->may_save($may_save);
2119 $SVN::_Core
::SVN_NO_ERROR
;
2122 sub ssl_server_trust
{
2123 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2124 $may_save = undef if $_no_auth_cache;
2125 print STDERR
"Error validating server certificate for '$realm':\n";
2126 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2127 print STDERR
" - The certificate is not issued by a trusted ",
2128 "authority. Use the\n",
2129 " fingerprint to validate the certificate manually!\n";
2131 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2132 print STDERR
" - The certificate hostname does not match.\n";
2134 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2135 print STDERR
" - The certificate is not yet valid.\n";
2137 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2138 print STDERR
" - The certificate has expired.\n";
2140 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2141 print STDERR
" - The certificate has an unknown error.\n";
2144 "Certificate information:\n".
2145 " - Hostname: %s\n".
2146 " - Valid: from %s until %s\n".
2148 " - Fingerprint: %s\n",
2149 map $cert_info->$_, qw(hostname valid_from valid_until
2150 issuer_dname fingerprint);
2153 print STDERR
$may_save ?
2154 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2155 "(R)eject or accept (t)emporarily? ";
2157 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2158 if ($choice =~ /^t$/i) {
2159 $cred->may_save(undef);
2160 } elsif ($choice =~ /^r$/i) {
2162 } elsif ($may_save && $choice =~ /^p$/i) {
2163 $cred->may_save($may_save);
2167 $cred->accepted_failures($failures);
2168 $SVN::_Core
::SVN_NO_ERROR
;
2171 sub ssl_client_cert
{
2172 my ($cred, $realm, $may_save, $pool) = @_;
2173 $may_save = undef if $_no_auth_cache;
2174 print STDERR
"Client certificate filename: ";
2176 chomp(my $filename = <STDIN
>);
2177 $cred->cert_file($filename);
2178 $cred->may_save($may_save);
2179 $SVN::_Core
::SVN_NO_ERROR
;
2182 sub ssl_client_cert_pw
{
2183 my ($cred, $realm, $may_save, $pool) = @_;
2184 $may_save = undef if $_no_auth_cache;
2185 $cred->password(_read_password
("Password: ", $realm));
2186 $cred->may_save($may_save);
2187 $SVN::_Core
::SVN_NO_ERROR
;
2191 my ($cred, $realm, $may_save, $pool) = @_;
2192 $may_save = undef if $_no_auth_cache;
2193 if (defined $realm && length $realm) {
2194 print STDERR
"Authentication realm: $realm\n";
2197 if (defined $_username) {
2198 $username = $_username;
2200 print STDERR
"Username: ";
2202 chomp($username = <STDIN
>);
2204 $cred->username($username);
2205 $cred->may_save($may_save);
2206 $SVN::_Core
::SVN_NO_ERROR
;
2209 sub _read_password
{
2210 my ($prompt, $realm) = @_;
2211 print STDERR
$prompt;
2213 require Term
::ReadKey
;
2214 Term
::ReadKey
::ReadMode
('noecho');
2216 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2217 last if $key =~ /[\012\015]/; # \n\r
2220 Term
::ReadKey
::ReadMode
('restore');
2229 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2230 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2231 $SVN::Node
::none
.$SVN::Node
::file
.
2232 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2233 $SVN::Auth
::SSL
::CNMISMATCH
.
2234 $SVN::Auth
::SSL
::NOTYETVALID
.
2235 $SVN::Auth
::SSL
::EXPIRED
.
2236 $SVN::Auth
::SSL
::UNKNOWNCA
.
2237 $SVN::Auth
::SSL
::OTHER
;
2240 package SVN
::Git
::Fetcher
;
2248 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2250 my ($class, $git_svn) = @_;
2251 my $self = SVN
::Delta
::Editor
->new;
2252 bless $self, $class;
2253 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
2254 $self->{empty
} = {};
2255 $self->{dir_prop
} = {};
2256 $self->{file_prop
} = {};
2257 $self->{absent_dir
} = {};
2258 $self->{absent_file
} = {};
2259 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
2263 sub set_path_strip
{
2264 my ($self, $path) = @_;
2265 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
2272 sub open_directory
{
2273 my ($self, $path, $pb, $rev) = @_;
2278 my ($self, $path) = @_;
2279 if ($self->{path_strip
}) {
2280 $path =~ s!$self->{path_strip}!! or
2281 die "Failed to strip path '$path' ($self->{path_strip})\n";
2287 my ($self, $path, $rev, $pb) = @_;
2289 my $gpath = $self->git_path($path);
2290 return undef if ($gpath eq '');
2292 # remove entire directories.
2293 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
2294 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2296 $self->{c
}, '--', $gpath);
2300 $self->{gii
}->remove($_);
2301 print "\tD\t$_\n" unless $::_q
;
2303 print "\tD\t$gpath/\n" unless $::_q
;
2304 command_close_pipe
($ls, $ctx);
2305 $self->{empty
}->{$path} = 0
2307 $self->{gii
}->remove($gpath);
2308 print "\tD\t$gpath\n" unless $::_q
;
2314 my ($self, $path, $pb, $rev) = @_;
2315 my $gpath = $self->git_path($path);
2316 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
2317 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2318 unless (defined $mode && defined $blob) {
2319 die "$path was not found in commit $self->{c} (r$rev)\n";
2321 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2322 pool
=> SVN
::Pool
->new, action
=> 'M' };
2326 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2327 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2328 delete $self->{empty
}->{$dir};
2329 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2330 pool
=> SVN
::Pool
->new, action
=> 'A' };
2334 my ($self, $path, $cp_path, $cp_rev) = @_;
2335 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2336 delete $self->{empty
}->{$dir};
2337 $self->{empty
}->{$path} = 1;
2341 sub change_dir_prop
{
2342 my ($self, $db, $prop, $value) = @_;
2343 $self->{dir_prop
}->{$db->{path
}} ||= {};
2344 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2348 sub absent_directory
{
2349 my ($self, $path, $pb) = @_;
2350 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2351 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2356 my ($self, $path, $pb) = @_;
2357 $self->{absent_file
}->{$pb->{path
}} ||= [];
2358 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2362 sub change_file_prop
{
2363 my ($self, $fb, $prop, $value) = @_;
2364 if ($prop eq 'svn:executable') {
2365 if ($fb->{mode_b
} != 120000) {
2366 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2368 } elsif ($prop eq 'svn:special') {
2369 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2371 $self->{file_prop
}->{$fb->{path
}} ||= {};
2372 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2377 sub apply_textdelta
{
2378 my ($self, $fb, $exp) = @_;
2379 my $fh = IO
::File
->new_tmpfile;
2381 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2382 # (but $base does not,) so dup() it for reading in close_file
2383 open my $dup, '<&', $fh or croak
$!;
2384 my $base = IO
::File
->new_tmpfile;
2385 $base->autoflush(1);
2387 defined (my $pid = fork) or croak
$!;
2389 open STDOUT
, '>&', $base or croak
$!;
2390 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2391 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2397 seek $base, 0, 0 or croak
$!;
2398 my $md5 = Digest
::MD5
->new;
2399 $md5->addfile($base);
2400 my $got = $md5->hexdigest;
2401 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2403 " got: $got\n" if ($got ne $exp);
2406 seek $base, 0, 0 or croak
$!;
2408 $fb->{base
} = $base;
2409 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2413 my ($self, $fb, $exp) = @_;
2415 my $path = $self->git_path($fb->{path
});
2416 if (my $fh = $fb->{fh
}) {
2417 seek($fh, 0, 0) or croak
$!;
2418 my $md5 = Digest
::MD5
->new;
2420 my $got = $md5->hexdigest;
2421 die "Checksum mismatch: $path\n",
2422 "expected: $exp\n got: $got\n" if ($got ne $exp);
2423 seek($fh, 0, 0) or croak
$!;
2424 if ($fb->{mode_b
} == 120000) {
2425 read($fh, my $buf, 5) == 5 or croak
$!;
2426 $buf eq 'link ' or die "$path has mode 120000",
2427 "but is not a link\n";
2429 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2431 open STDIN
, '<&', $fh or croak
$!;
2432 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2434 chomp($hash = do { local $/; <$out> });
2435 close $out or croak
$!;
2436 close $fh or croak
$!;
2437 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2438 close $fb->{base
} or croak
$!;
2440 $hash = $fb->{blob
} or die "no blob information\n";
2443 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
2444 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
2450 $self->{nr
} = $self->{gii
}->{nr
};
2451 delete $self->{gii
};
2452 $self->SUPER::abort_edit
(@_);
2457 $self->{git_commit_ok
} = 1;
2458 $self->{nr
} = $self->{gii
}->{nr
};
2459 delete $self->{gii
};
2460 $self->SUPER::close_edit
(@_);
2463 package SVN
::Git
::Editor
;
2464 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
2472 my ($class, $opts) = @_;
2473 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
2474 die "$_ required!\n" unless (defined $opts->{$_});
2477 my $pool = SVN
::Pool
->new;
2478 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
2479 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
2482 # $opts->{ra} functions should not be used after this:
2483 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
2484 $opts->{editor_cb
}, $pool);
2485 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
2486 bless $self, $class;
2487 foreach (qw
/svn_path r tree_a tree_b/) {
2488 $self->{$_} = $opts->{$_};
2490 $self->{url
} = $opts->{ra
}->{url
};
2491 $self->{mods
} = $mods;
2492 $self->{types
} = $types;
2493 $self->{pool
} = $pool;
2494 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
2496 $self->{path_prefix
} = length $self->{svn_path
} ?
2497 "$self->{svn_path}/" : '';
2502 my ($tree_a, $tree_b) = @_;
2503 my @diff_tree = qw(diff-tree -z -r);
2504 if ($_cp_similarity) {
2505 push @diff_tree, "-C$_cp_similarity";
2507 push @diff_tree, '-C';
2509 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2510 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
2511 push @diff_tree, $tree_a, $tree_b;
2512 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2516 while (<$diff_fh>) {
2517 chomp $_; # this gets rid of the trailing "\0"
2518 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2519 $::sha1\s
($::sha1
)\s
2520 ([MTCRAD
])\d
*$/xo
) {
2521 push @mods, { mode_a
=> $1, mode_b
=> $2,
2522 sha1_b
=> $3, chg
=> $4 };
2523 if ($4 =~ /^(?:C|R)$/) {
2528 } elsif ($state eq 'file_a') {
2529 my $x = $mods[$#mods] or croak
"Empty array\n";
2530 if ($x->{chg
} !~ /^(?:C|R)$/) {
2531 croak
"Error parsing $_, $x->{chg}\n";
2535 } elsif ($state eq 'file_b') {
2536 my $x = $mods[$#mods] or croak
"Empty array\n";
2537 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2538 croak
"Error parsing $_, $x->{chg}\n";
2540 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2541 croak
"Error parsing $_, $x->{chg}\n";
2546 croak
"Error parsing $_\n";
2549 command_close_pipe
($diff_fh, $ctx);
2553 sub check_diff_paths
{
2554 my ($ra, $pfx, $rev, $mods) = @_;
2556 $pfx .= '/' if length $pfx;
2558 sub type_diff_paths
{
2559 my ($ra, $types, $path, $rev) = @_;
2560 my @p = split m
#/+#, $path;
2562 unless (defined $types->{$c}) {
2563 $types->{$c} = $ra->check_path($c, $rev);
2566 $c .= '/' . shift @p;
2567 next if defined $types->{$c};
2568 $types->{$c} = $ra->check_path($c, $rev);
2572 foreach my $m (@
$mods) {
2573 foreach my $f (qw
/file_a file_b/) {
2574 next unless defined $m->{$f};
2575 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
2576 if (length $pfx.$dir && ! defined $types{$dir}) {
2577 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
2585 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
2589 my ($self, $path) = @_;
2590 $self->{path_prefix
}.(defined $path ?
$path : '');
2594 my ($self, $path) = @_;
2595 $self->{url
} . '/' . $self->repo_path($path);
2600 my $rm = $self->{rm
};
2601 delete $rm->{''}; # we never delete the url we're tracking
2604 foreach (keys %$rm) {
2605 my @d = split m
#/#, $_;
2609 $c .= '/' . shift @d;
2613 delete $rm->{$self->{svn_path
}};
2614 delete $rm->{''}; # we never delete the url we're tracking
2617 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
2622 my @dn = split m
#/#, $_;
2624 delete $rm->{join '/', @dn};
2631 command_close_pipe
($fh, $ctx);
2633 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
2634 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2635 $self->close_directory($bat->{$d}, $p);
2636 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
2637 print "\tD+\t$d/\n" unless $::_q
;
2638 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
2643 sub open_or_add_dir
{
2644 my ($self, $full_path, $baton) = @_;
2645 my $t = $self->{types
}->{$full_path};
2647 die "$full_path not known in r$self->{r} or we have a bug!\n";
2649 if ($t == $SVN::Node
::none
) {
2650 return $self->add_directory($full_path, $baton,
2651 undef, -1, $self->{pool
});
2652 } elsif ($t == $SVN::Node
::dir
) {
2653 return $self->open_directory($full_path, $baton,
2654 $self->{r
}, $self->{pool
});
2656 print STDERR
"$full_path already exists in repository at ",
2657 "r$self->{r} and it is not a directory (",
2658 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
2663 my ($self, $path) = @_;
2664 my $bat = $self->{bat
};
2665 my $repo_path = $self->repo_path($path);
2666 return $bat->{''} unless (length $repo_path);
2667 my @p = split m
#/+#, $repo_path;
2669 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2672 $c .= '/' . shift @p;
2673 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2679 my ($self, $m) = @_;
2680 my ($dir, $file) = split_path
($m->{file_b
});
2681 my $pbat = $self->ensure_path($dir);
2682 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2684 print "\tA\t$m->{file_b}\n" unless $::_q
;
2685 $self->chg_file($fbat, $m);
2686 $self->close_file($fbat,undef,$self->{pool
});
2690 my ($self, $m) = @_;
2691 my ($dir, $file) = split_path
($m->{file_b
});
2692 my $pbat = $self->ensure_path($dir);
2693 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2694 $self->url_path($m->{file_a
}), $self->{r
});
2695 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2696 $self->chg_file($fbat, $m);
2697 $self->close_file($fbat,undef,$self->{pool
});
2701 my ($self, $path, $pbat) = @_;
2702 my $rpath = $self->repo_path($path);
2703 my ($dir, $file) = split_path
($rpath);
2704 $self->{rm
}->{$dir} = 1;
2705 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
2709 my ($self, $m) = @_;
2710 my ($dir, $file) = split_path
($m->{file_b
});
2711 my $pbat = $self->ensure_path($dir);
2712 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
2713 $self->url_path($m->{file_a
}), $self->{r
});
2714 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
2715 $self->chg_file($fbat, $m);
2716 $self->close_file($fbat,undef,$self->{pool
});
2718 ($dir, $file) = split_path
($m->{file_a
});
2719 $pbat = $self->ensure_path($dir);
2720 $self->delete_entry($m->{file_a
}, $pbat);
2724 my ($self, $m) = @_;
2725 my ($dir, $file) = split_path
($m->{file_b
});
2726 my $pbat = $self->ensure_path($dir);
2727 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
2728 $pbat,$self->{r
},$self->{pool
});
2729 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
2730 $self->chg_file($fbat, $m);
2731 $self->close_file($fbat,undef,$self->{pool
});
2734 sub T
{ shift->M(@_) }
2736 sub change_file_prop
{
2737 my ($self, $fbat, $pname, $pval) = @_;
2738 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2742 my ($self, $fbat, $m) = @_;
2743 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2744 $self->change_file_prop($fbat,'svn:executable','*');
2745 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2746 $self->change_file_prop($fbat,'svn:executable',undef);
2748 my $fh = IO
::File
->new_tmpfile or croak
$!;
2749 if ($m->{mode_b
} =~ /^120/) {
2750 print $fh 'link ' or croak
$!;
2751 $self->change_file_prop($fbat,'svn:special','*');
2752 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2753 $self->change_file_prop($fbat,'svn:special',undef);
2755 defined(my $pid = fork) or croak
$!;
2757 open STDOUT
, '>&', $fh or croak
$!;
2758 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2762 $fh->flush == 0 or croak
$!;
2763 seek $fh, 0, 0 or croak
$!;
2765 my $md5 = Digest
::MD5
->new;
2766 $md5->addfile($fh) or croak
$!;
2767 seek $fh, 0, 0 or croak
$!;
2769 my $exp = $md5->hexdigest;
2770 my $pool = SVN
::Pool
->new;
2771 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2772 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2773 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2776 close $fh or croak
$!;
2780 my ($self, $m) = @_;
2781 my ($dir, $file) = split_path
($m->{file_b
});
2782 my $pbat = $self->ensure_path($dir);
2783 print "\tD\t$m->{file_b}\n" unless $::_q
;
2784 $self->delete_entry($m->{file_b
}, $pbat);
2789 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2790 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2791 $self->close_directory($bat->{$_}, $p);
2793 $self->SUPER::close_edit
($p);
2799 $self->SUPER::abort_edit
($self->{pool
});
2804 $self->SUPER::DESTROY
(@_);
2805 $self->{pool
}->clear;
2808 # this drives the editor
2811 my $mods = $self->{mods
};
2812 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2813 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
2815 if (defined $o{$f}) {
2818 fatal
("Invalid change type: $f\n");
2821 $self->rmdirs if $_rmdir;
2827 return scalar @
$mods;
2830 package Git
::SVN
::Ra
;
2831 use vars qw
/@ISA $config_dir $_log_window_size/;
2834 my ($can_do_switch);
2838 # enforce temporary pool usage for some simple functions
2840 foreach (qw
/get_latest_revnum get_uuid get_repos_root/) {
2843 my \$pool = SVN::Pool->new;
2844 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2846 wantarray ? \@ret : \$ret[0]; }\n";
2849 # get_dir needs $pool held in cache for dirents to work,
2850 # check_path is cacheable and rev_proplist is close enough
2852 foreach (qw
/check_path get_dir rev_proplist/) {
2853 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2856 my \$k = join(\"\\0\", \@_);
2857 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2858 return wantarray ? \@\$x : \$x->[0];
2860 my \$pool = SVN::Pool->new;
2861 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2862 if (\$r != \$${_}_rev) {
2863 \%${_}_cache = ( pool => [] );
2866 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2867 push \@{\$${_}_cache{pool}}, \$pool;
2868 wantarray ? \@ret : \$ret[0]; }\n";
2875 my ($class, $url) = @_;
2877 return $RA if ($RA && $RA->{url
} eq $url);
2879 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2880 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2881 SVN
::Client
::get_simple_provider
(),
2882 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2883 SVN
::Client
::get_simple_prompt_provider
(
2884 \
&Git
::SVN
::Prompt
::simple
, 2),
2885 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2886 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2887 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2888 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2889 SVN
::Client
::get_username_provider
(),
2890 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2891 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2892 SVN
::Client
::get_username_prompt_provider
(
2893 \
&Git
::SVN
::Prompt
::username
, 2),
2895 my $config = SVN
::Core
::config_get_config
($config_dir);
2896 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2898 pool
=> SVN
::Pool
->new,
2899 auth_provider_callbacks
=> $callbacks);
2900 $self->{svn_path
} = $url;
2901 $self->{repos_root
} = $self->get_repos_root;
2902 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
2903 $RA = bless $self, $class;
2907 # do not call the real DESTROY since we store ourselves in $RA
2911 my ($self, @args) = @_;
2912 my $pool = SVN
::Pool
->new;
2913 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2914 my $ret = $self->SUPER::get_log
(@args, $pool);
2919 sub get_commit_editor
{
2920 my ($self, $log, $cb, $pool) = @_;
2921 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2922 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2926 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2927 my $new = ($rev_a == $rev_b);
2928 my $path = $gs->{path
};
2930 my $pool = SVN
::Pool
->new;
2931 $editor->set_path_strip($path);
2932 my (@pc) = split m
#/#, $path;
2933 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
2935 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2937 # Since we can't rely on svn_ra_reparent being available, we'll
2938 # just have to do some magic with set_path to make it so
2939 # we only want a partial path.
2941 my $final = join('/', @pc);
2943 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2944 $sp .= '/' if length $sp;
2947 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2949 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2951 $reporter->finish_report($pool);
2953 $editor->{git_commit_ok
};
2956 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2957 # svn_ra_reparent didn't work before 1.4)
2959 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2960 my $path = $gs->{path
};
2961 my $pool = SVN
::Pool
->new;
2963 my $full_url = $self->{url
};
2964 my $old_url = $full_url;
2965 $full_url .= "/$path" if length $path;
2966 my ($ra, $reparented);
2967 if ($old_url ne $full_url) {
2968 if ($old_url !~ m
#^svn(\+ssh)?://#) {
2969 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
2971 $self->{url
} = $full_url;
2974 $ra = Git
::SVN
::Ra
->new($full_url);
2978 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2979 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2980 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2981 $reporter->finish_report($pool);
2984 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
2985 $self->{url
} = $old_url;
2989 $editor->{git_commit_ok
};
2992 sub gs_fetch_loop_common
{
2993 my ($self, $base, $head, $gsv, $globs) = @_;
2994 return if ($base > $head);
2995 my $inc = $_log_window_size;
2996 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
2998 my $common_max = scalar @
$gsv;
3000 foreach my $gs (@
$gsv) {
3001 my @tmp = split m
#/#, $gs->{path};
3004 $p .= length($p) ?
"/$_" : $_;
3010 $common_max += scalar @
$globs;
3011 foreach my $glob (@
$globs) {
3012 my @tmp = split m
#/#, $glob->{path}->{left};
3015 $p .= length($p) ?
"/$_" : $_;
3021 my $longest_path = '';
3022 foreach (sort {length $b <=> length $a} keys %common) {
3023 if ($common{$_} == $common_max) {
3031 my $err_handler = $SVN::Error
::handler
;
3032 $SVN::Error
::handler
= sub {
3034 skip_unknown_revs
($err);
3037 my ($paths, $r, $author, $date, $log) = @_;
3038 [ dup_changed_paths
($paths),
3039 { author
=> $author, date
=> $date, log => $log } ];
3041 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3042 sub { $revs{$_[1]} = _cb
(@_) });
3043 if ($err && $max >= $head) {
3044 print STDERR
"Path '$longest_path' ",
3045 "was probably deleted:\n",
3046 $err->expanded_message,
3047 "\nWill attempt to follow ",
3048 "revisions r$min .. r$max ",
3049 "committed before the deletion\n";
3051 while (--$hi >= $min) {
3053 $self->get_log([$longest_path], $min, $hi,
3056 $revs{$_[1]} = _cb
(@_) });
3058 print STDERR
"r$min .. r$ok OK\n";
3063 $SVN::Error
::handler
= $err_handler;
3065 my %exists = map { $_->{path
} => $_ } @
$gsv;
3066 foreach my $r (sort {$a <=> $b} keys %revs) {
3067 my ($paths, $logged) = @
{$revs{$r}};
3069 foreach my $gs ($self->match_globs(\
%exists, $paths,
3071 if ($gs->rev_db_max >= $r) {
3074 next unless $gs->match_paths($paths, $r);
3075 $gs->{logged_rev_props
} = $logged;
3076 if (my $last_commit = $gs->last_commit) {
3077 $gs->assert_index_clean($last_commit);
3079 my $log_entry = $gs->do_fetch($paths, $r);
3081 $gs->do_git_commit($log_entry);
3084 foreach my $g (@
$globs) {
3085 my $k = "svn-remote.$g->{remote}." .
3087 Git
::SVN
::tmp_config
($k, $r);
3090 # pre-fill the .rev_db since it'll eventually get filled in
3091 # with '0' x40 if something new gets committed
3092 foreach my $gs (@
$gsv) {
3093 next if defined $gs->rev_db_get($max);
3094 $gs->rev_db_set($max, 0 x40
);
3096 foreach my $g (@
$globs) {
3097 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3098 Git
::SVN
::tmp_config
($k, $max);
3100 last if $max >= $head;
3103 $max = $head if ($max > $head);
3108 my ($self, $exists, $paths, $globs, $r) = @_;
3111 my ($self, $exists, $g, $r) = @_;
3112 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
3113 return unless scalar @x == 3;
3114 my $dirents = $x[0];
3115 foreach my $de (keys %$dirents) {
3116 next if $dirents->{$de}->kind != $SVN::Node
::dir
;
3117 my $p = $g->{path
}->full_path($de);
3118 next if $exists->{$p};
3119 next if (length $g->{path
}->{right
} &&
3120 ($self->check_path($p, $r) !=
3122 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
3123 $g->{ref}->full_path($de), 1);
3126 foreach my $g (@
$globs) {
3127 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3128 if ($path->{action
} =~ /^[AR]$/) {
3129 get_dir_check
($self, $exists, $g, $r);
3132 foreach (keys %$paths) {
3133 if (/$g->{path}->{left_regex}/ &&
3134 !/$g->{path}->{regex}/) {
3135 next if $paths->{$_}->{action
} !~ /^[AR]$/;
3136 get_dir_check
($self, $exists, $g, $r);
3138 next unless /$g->{path}->{regex}/;
3140 my $pathname = $g->{path
}->full_path($p);
3141 next if $exists->{$pathname};
3142 $exists->{$pathname} = Git
::SVN
->init(
3143 $self->{url
}, $pathname, undef,
3144 $g->{ref}->full_path($p), 1);
3147 foreach (split m
#/#, $g->{path}->{left}) {
3149 next unless ($paths->{$c} &&
3150 ($paths->{$c}->{action
} =~ /^[AR]$/));
3151 get_dir_check
($self, $exists, $g, $r);
3159 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
3160 my $url = $self->{repos_root
};
3161 my @components = split(m!/!, $self->{svn_path
});
3164 $url .= "/$c" if length $c;
3165 eval { (ref $self)->new($url)->get_latest_revnum };
3166 } while ($@
&& ($c = shift @components));
3172 unless (defined $can_do_switch) {
3173 my $pool = SVN
::Pool
->new;
3175 $self->do_switch(1, '', 0, $self->{url
},
3176 SVN
::Delta
::Editor
->new, $pool);
3181 $rep->abort_report($pool);
3189 sub skip_unknown_revs
{
3191 my $errno = $err->apr_err();
3192 # Maybe the branch we're tracking didn't
3193 # exist when the repo started, so it's
3194 # not an error if it doesn't, just continue
3196 # Wonderfully consistent library, eh?
3197 # 160013 - svn:// and file://
3198 # 175002 - http(s)://
3199 # 175007 - http(s):// (this repo required authorization, too...)
3200 # More codes may be discovered later...
3201 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3202 warn "W: Ignoring error from SVN, path probably ",
3203 "does not exist: ($errno): ",
3204 $err->expanded_message,"\n";
3207 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3210 # svn_log_changed_path_t objects passed to get_log are likely to be
3211 # overwritten even if only the refs are copied to an external variable,
3212 # so we should dup the structures in their entirety. Using an externally
3213 # passed pool (instead of our temporary and quickly cleared pool in
3214 # Git::SVN::Ra) does not help matters at all...
3215 sub dup_changed_paths
{
3217 return undef unless $paths;
3219 foreach my $p (keys %$paths) {
3220 my $i = $paths->{$p};
3221 my %s = map { $_ => $i->$_ }
3222 qw
/copyfrom_path copyfrom_rev action/;
3228 package Git
::SVN
::Log
;
3231 use POSIX qw
/strftime/;
3232 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
3233 %rusers $show_commit $incremental/;
3238 return 1 if defined $c->{r
};
3239 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
3240 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
3241 my @log = command
(qw
/cat-file commit/, $c->{c
});
3242 shift @log while ($log[0] ne "\n");
3244 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
3246 (undef, $c->{r
}, undef) = ::extract_metadata
(
3247 (grep(/^git-svn-id: /, @log))[-1]);
3249 return defined $c->{r
};
3255 $dcvar = 'color.diff';
3256 $dc = `git-config --get $dcvar`;
3258 # nothing at all; fallback to "diff.color"
3259 $dcvar = 'diff.color';
3260 $dc = `git-config --get $dcvar`;
3263 if ($dc eq 'auto') {
3265 $pc = `git-config --get color.pager`;
3267 # does not have it -- fallback to pager.color
3268 $pc = `git-config --bool --get pager.color`;
3271 $pc = `git-config --bool --get color.pager`;
3277 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
3278 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
3282 return 0 if $dc eq 'never';
3283 return 1 if $dc eq 'always';
3284 chomp($dc = `git-config --bool --get $dcvar`);
3285 return ($dc eq 'true');
3288 sub git_svn_log_cmd
{
3289 my ($r_min, $r_max, @args) = @_;
3291 foreach my $x (@args) {
3293 next unless ::verify_ref
("$x^0");
3298 my $url = (::working_head_info
($head))[0];
3299 my $gs = Git
::SVN
->find_by_url($url) || Git
::SVN
->_new;
3300 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
3302 push @cmd, '-r' unless $non_recursive;
3303 push @cmd, qw
/--raw --name-status/ if $verbose;
3304 push @cmd, '--color' if log_use_color
();
3305 return @cmd unless defined $r_max;
3306 if ($r_max == $r_min) {
3307 push @cmd, '--max-count=1';
3308 if (my $c = $gs->rev_db_get($r_max)) {
3312 my ($c_min, $c_max);
3313 $c_max = $gs->rev_db_get($r_max);
3314 $c_min = $gs->rev_db_get($r_min);
3315 if (defined $c_min && defined $c_max) {
3316 if ($r_max > $r_max) {
3317 push @cmd, "$c_min..$c_max";
3319 push @cmd, "$c_max..$c_min";
3321 } elsif ($r_max > $r_min) {
3330 # adapted from pager.c
3332 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
3333 if (!defined $pager) {
3335 } elsif (length $pager == 0 || $pager eq 'cat') {
3341 return unless -t
*STDOUT
;
3342 pipe my $rfd, my $wfd or return;
3343 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
3345 open STDOUT
, '>&', $wfd or
3346 ::fatal
"Can't redirect to stdout: $!\n";
3349 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
3350 $ENV{LESS
} ||= 'FRSX';
3351 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
3354 sub tz_to_s_offset
{
3357 return ($1 * 60) + ($tz * 3600);
3360 sub get_author_info
{
3361 my ($dest, $author, $t, $tz) = @_;
3362 $author =~ s/(?:^\s*|\s*$)//g;
3363 $dest->{a_raw
} = $author;
3366 $au = $rusers{$author} || undef;
3369 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3374 # Date::Parse isn't in the standard Perl distro :(
3375 if ($tz =~ s/^\+//) {
3376 $t += tz_to_s_offset
($tz);
3377 } elsif ($tz =~ s/^\-//) {
3378 $t -= tz_to_s_offset
($tz);
3380 $dest->{t_utc
} = $t;
3383 sub process_commit
{
3384 my ($c, $r_min, $r_max, $defer) = @_;
3385 if (defined $r_min && defined $r_max) {
3386 if ($r_min == $c->{r
} && $r_min == $r_max) {
3390 return 1 if $r_min == $r_max;
3391 if ($r_min < $r_max) {
3392 # we need to reverse the print order
3393 return 0 if (defined $limit && --$limit < 0);
3397 if ($r_min != $r_max) {
3398 return 1 if ($r_min < $c->{r
});
3399 return 1 if ($r_max > $c->{r
});
3402 return 0 if (defined $limit && --$limit < 0);
3411 if (my $l = $c->{l
}) {
3412 while ($l->[0] =~ /^\s*$/) { shift @
$l }
3415 $l_fmt ||= 'A' . length($c->{r
});
3416 print 'r',pack($l_fmt, $c->{r
}),' | ';
3417 print "$c->{c} | " if $show_commit;
3420 show_commit_normal
($c);
3424 sub show_commit_changed_paths
{
3426 return unless $c->{changed
};
3427 print "Changed paths:\n", @
{$c->{changed
}};
3430 sub show_commit_normal
{
3432 print '-' x72
, "\nr$c->{r} | ";
3433 print "$c->{c} | " if $show_commit;
3434 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3435 localtime($c->{t_utc
})), ' | ';
3438 if (my $l = $c->{l
}) {
3439 while ($l->[$#$l] eq "\n" && $#$l > 0
3440 && $l->[($#$l - 1)] eq "\n") {
3443 $nr_line = scalar @
$l;
3445 print "1 line\n\n\n";
3447 if ($nr_line == 1) {
3448 $nr_line = '1 line';
3450 $nr_line .= ' lines';
3452 print $nr_line, "\n";
3453 show_commit_changed_paths
($c);
3455 print $_ foreach @
$l;
3459 show_commit_changed_paths
($c);
3463 foreach my $x (qw
/raw stat diff/) {
3466 print $_ foreach @
{$c->{$x}}
3473 my ($r_min, $r_max);
3474 my $r_last = -1; # prevent dupes
3480 if (defined $::_revision
) {
3481 if ($::_revision
=~ /^(\d+):(\d+)$/) {
3482 ($r_min, $r_max) = ($1, $2);
3483 } elsif ($::_revision
=~ /^\d+$/) {
3484 $r_min = $r_max = $::_revision
;
3486 ::fatal
"-r$::_revision is not supported, use ",
3487 "standard \'git log\' arguments instead\n";
3492 @args = (git_svn_log_cmd
($r_min, $r_max, @args), @args);
3493 my $log = command_output_pipe
(@args);
3495 my (@k, $c, $d, $stat);
3496 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3498 if (/^${esc_color}commit ($::sha1_short)/o) {
3500 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
3502 process_commit
($c, $r_min, $r_max, \
@k) or
3507 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3508 get_author_info
($c, $1, $2, $3);
3509 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3511 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3512 push @
{$c->{raw
}}, $_;
3513 } elsif (/^${esc_color}[ACRMDT]\t/) {
3514 # we could add $SVN->{svn_path} here, but that requires
3515 # remote access at the moment (repo_path_split)...
3516 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3517 push @
{$c->{changed
}}, $_;
3518 } elsif (/^${esc_color}diff /o) {
3520 push @
{$c->{diff
}}, $_;
3522 push @
{$c->{diff
}}, $_;
3523 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
3524 $esc_color*[\
+\
-]*$esc_color$/x
) {
3526 push @
{$c->{stat}}, $_;
3527 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3528 push @
{$c->{stat}}, $_;
3530 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3531 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
3532 } elsif (s/^${esc_color} //o) {
3533 push @
{$c->{l
}}, $_;
3536 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
3538 process_commit
($c, $r_min, $r_max, \
@k);
3544 process_commit
($_, $r_min, $r_max) foreach reverse @k;
3548 print '-' x72
,"\n" unless $incremental || $oneline;
3551 package Git
::SVN
::Migration
;
3552 # these version numbers do NOT correspond to actual version numbers
3553 # of git nor git-svn. They are just relative.
3555 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3557 # v1 layout: .git/$id/info/url, refs/remotes/$id
3559 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3561 # v3 layout: .git/svn/$id, refs/remotes/$id
3562 # - info/url may remain for backwards compatibility
3563 # - this is what we migrate up to this layout automatically,
3564 # - this will be used by git svn init on single branches
3565 # v3.1 layout (auto migrated):
3566 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3567 # for backwards compatibility
3569 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3570 # - this is only created for newly multi-init-ed
3571 # repositories. Similar in spirit to the
3572 # --use-separate-remotes option in git-clone (now default)
3573 # - we do not automatically migrate to this (following
3574 # the example set by core git)
3578 use File
::Path qw
/mkpath/;
3579 use File
::Basename qw
/dirname basename/;
3580 use vars qw
/$_minimize/;
3582 sub migrate_from_v0
{
3583 my $git_dir = $ENV{GIT_DIR
};
3584 return undef unless -d
$git_dir;
3585 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3589 my ($id, $orig_ref) = ($_, $_);
3590 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
3591 next unless -f
"$git_dir/$id/info/url";
3592 my $new_ref = "refs/remotes/$id";
3593 if (::verify_ref
("$new_ref^0")) {
3594 print STDERR
"W: $orig_ref is probably an old ",
3595 "branch used by an ancient version of ",
3597 "However, $new_ref also exists.\n",
3598 "We will not be able ",
3599 "to use this branch until this ",
3600 "ambiguity is resolved.\n";
3603 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
3604 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
3605 command_noisy
('update-ref', $new_ref, $orig_ref);
3606 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
3609 command_close_pipe
($fh, $ctx);
3610 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
3614 sub migrate_from_v1
{
3615 my $git_dir = $ENV{GIT_DIR
};
3617 return $migrated unless -d
$git_dir;
3618 my $svn_dir = "$git_dir/svn";
3620 # just in case somebody used 'svn' as their $id at some point...
3621 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
3623 print STDERR
"Migrating from a git-svn v1 layout...\n";
3625 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
3626 "$svn_dir\n\t(required for this version ",
3627 "($::VERSION) of git-svn) does not. exist\n";
3628 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
3631 next unless $x =~ s
#^refs/remotes/##;
3633 next unless -f
"$git_dir/$x/info/url";
3634 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
3636 my $dn = dirname
("$git_dir/svn/$x");
3637 mkpath
([$dn]) unless -d
$dn;
3638 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3639 mkpath
(["$git_dir/svn/svn"]);
3640 print STDERR
" - $git_dir/$x/info => ",
3641 "$git_dir/svn/$x/info\n";
3642 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3644 # don't worry too much about these, they probably
3645 # don't exist with repos this old (save for index,
3646 # and we can easily regenerate that)
3647 foreach my $f (qw
/unhandled.log index .rev_db/) {
3648 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3651 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
3652 rename "$git_dir/$x", "$git_dir/svn/$x" or
3657 command_close_pipe
($fh, $ctx);
3658 print STDERR
"Done migrating from a git-svn v1 layout\n";
3663 my ($l_map, $pfx, $path) = @_;
3665 foreach (<$path/*>) {
3666 if (-r
"$_/info/url") {
3667 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
3668 my $ref_id = $pfx . basename
$_;
3669 my $url = ::file_to_s
("$_/info/url");
3670 $l_map->{$ref_id} = $url;
3677 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3678 read_old_urls
($l_map, $x, $_);
3682 sub migrate_from_v2
{
3683 my @cfg = command
(qw
/config -l/);
3684 return if grep /^svn-remote\..+\.url=/, @cfg;
3686 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
3689 foreach my $ref_id (sort keys %l_map) {
3690 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
3692 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3699 sub minimize_connections
{
3700 my $r = Git
::SVN
::read_all_remotes
();
3702 my $root_repos = {};
3703 foreach my $repo_id (keys %$r) {
3704 my $url = $r->{$repo_id}->{url
} or next;
3705 my $fetch = $r->{$repo_id}->{fetch
} or next;
3706 my $ra = Git
::SVN
::Ra
->new($url);
3708 # skip existing cases where we already connect to the root
3709 if (($ra->{url
} eq $ra->{repos_root
}) ||
3710 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
3712 $root_repos->{$ra->{url
}} = $repo_id;
3716 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
3717 my $root_path = $ra->{url
};
3718 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
3719 foreach my $path (keys %$fetch) {
3720 my $ref_id = $fetch->{$path};
3721 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
3723 # make sure we can read when connecting to
3724 # a higher level of a repository
3725 my ($last_rev, undef) = $gs->last_rev_commit;
3726 if (!defined $last_rev) {
3728 $root_ra->get_latest_revnum;
3732 my $new = $root_path;
3733 $new .= length $path ?
"/$path" : '';
3735 $root_ra->get_log([$new], $last_rev, $last_rev,
3739 $new_urls->{$ra->{repos_root
}}->{$new} =
3740 { ref_id
=> $ref_id,
3741 old_repo_id
=> $repo_id,
3742 old_path
=> $path };
3747 foreach my $url (keys %$new_urls) {
3748 # see if we can re-use an existing [svn-remote "repo_id"]
3749 # instead of creating a(n ugly) new section:
3750 my $repo_id = $root_repos->{$url} ||
3751 Git
::SVN
::sanitize_remote_name
($url);
3753 my $fetch = $new_urls->{$url};
3754 foreach my $path (keys %$fetch) {
3755 my $x = $fetch->{$path};
3756 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
3757 my $pfx = "svn-remote.$x->{old_repo_id}";
3759 my $old_fetch = quotemeta("$x->{old_path}:".
3760 "refs/remotes/$x->{ref_id}");
3761 command_noisy
(qw
/config --unset/,
3762 "$pfx.fetch", '^'. $old_fetch . '$');
3763 delete $r->{$x->{old_repo_id
}}->
3764 {fetch
}->{$x->{old_path
}};
3765 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
3766 command_noisy
(qw
/config --unset/,
3768 push @emptied, $x->{old_repo_id
}
3773 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
3774 "$ENV{GIT_DIR}/config";
3776 The following [svn-remote] sections in your config file ($file) are empty
3777 and can be safely removed:
3779 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
3783 sub migration_check
{
3787 minimize_connections
() if $_minimize;
3790 package Git
::IndexInfo
;
3793 use Git qw
/command_input_pipe command_close_pipe/;
3797 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
3798 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
3802 my ($self, $path) = @_;
3803 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
3804 return ++$self->{nr
};
3810 my ($self, $mode, $hash, $path) = @_;
3811 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
3812 return ++$self->{nr
};
3819 command_close_pipe
($self->{gui
}, $self->{ctx
});
3822 package Git
::SVN
::GlobSpec
;
3827 my ($class, $glob) = @_;
3829 $re =~ s!/+$!!g; # no need for trailing slashes
3830 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3831 my ($left, $right) = ($1, $2);
3833 die "Only one '*' wildcard expansion ",
3834 "is supported (got $nr): '$glob'\n";
3835 } elsif ($nr == 0) {
3836 die "One '*' is needed for glob: '$glob'\n";
3838 $re = quotemeta($left) . $re . quotemeta($right);
3839 if (length $left && !($left =~ s!/+$!!g)) {
3840 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3842 if (length $right && !($right =~ s!^/+!!g)) {
3843 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3845 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
3846 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
3847 regex
=> qr/$re/, glob => $glob }, $class;
3851 my ($self, $path) = @_;
3852 return (length $self->{left
} ?
"$self->{left}/" : '') .
3853 $path . (length $self->{right
} ?
"/$self->{right}" : '');
3861 $remotes = { # returned by read_all_remotes()
3863 # svn-remote.svn.url=https://svn.musicpd.org
3864 url
=> 'https://svn.musicpd.org',
3865 # svn-remote.svn.fetch=mpd/trunk:trunk
3867 'mpd/trunk' => 'trunk',
3869 # svn-remote.svn.tags=mpd/tags/*:tags/*
3874 regex
=> qr!mpd/tags/([^/]+)$!,
3880 regex
=> qr!tags/([^/]+)$!,
3887 $log_entry hashref as returned by libsvn_log_entry
()
3889 log => 'whitespace-formatted log entry
3890 ', # trailing newline is preserved
3891 revision
=> '8', # integer
3892 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3893 author
=> 'committer name'
3897 # this is generated by generate_diff();
3898 @mods = array of diff
-index line hashes
, each element represents one line
3899 of diff
-index output
3901 diff
-index line
($m hash
)
3903 mode_a
=> first column of diff
-index output
, no leading
':',
3904 mode_b
=> second column of diff
-index output
,
3905 sha1_b
=> sha1sum of the final blob
,
3906 chg
=> change type
[MCRADT
],
3907 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3908 file_b
=> new
/current file name of a file
(any chg
)
3912 # retval of read_url_paths{,_all}();
3914 # repository root url
3915 'https://svn.musicpd.org' => {
3916 # repository path # GIT_SVN_ID
3917 'mpd/trunk' => 'trunk',
3918 'mpd/tags/0.11.5' => 'tags/0.11.5',
3923 I don
't trust the each() function on unless I created %hash myself
3924 because the internal iterator may not have started at base.