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 # From which subdir have we been invoked?
13 my $cmd_dir_prefix = eval {
14 command_oneline
([qw
/rev-parse --show-prefix/], STDERR
=> 0)
17 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR
};
18 $ENV{GIT_DIR
} ||= '.git';
19 $Git::SVN
::default_repo_id
= 'svn';
20 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
21 $Git::SVN
::Ra
::_log_window_size
= 100;
23 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
25 $| = 1; # unbuffer STDOUT
27 sub fatal
(@
) { print STDERR
"@_\n"; exit 1 }
28 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
31 if ($SVN::Core
::VERSION
lt '1.1.0') {
32 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
34 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
35 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
36 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
39 use File
::Basename qw
/dirname basename/;
40 use File
::Path qw
/mkpath/;
41 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
46 # import functions from Git into our packages, en masse
48 foreach (qw
/command command_oneline command_noisy command_output_pipe
49 command_input_pipe command_close_pipe
/) {
50 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
51 Git::SVN::Migration Git::SVN::Log Git::SVN
54 *{"${package}::$_"} = \
&{"Git::$_"};
61 $sha1 = qr/[a-f\d]{40}/;
62 $sha1_short = qr/[a-f\d]{4,40}/;
63 my ($_stdin, $_help, $_edit,
66 $_version, $_fetch_all, $_no_rebase,
67 $_merge, $_strategy, $_dry_run, $_local,
68 $_prefix, $_no_checkout, $_url, $_verbose);
69 $Git::SVN
::_follow_parent
= 1;
70 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
71 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
72 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
);
73 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
74 'authors-file|A=s' => \
$_authors,
75 'repack:i' => \
$Git::SVN
::_repack
,
76 'noMetadata' => \
$Git::SVN
::_no_metadata
,
77 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
78 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
79 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
80 'no-checkout' => \
$_no_checkout,
82 'repack-flags|repack-args|repack-opts=s' =>
83 \
$Git::SVN
::_repack_flags
,
86 my ($_trunk, $_tags, $_branches, $_stdlayout);
88 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
89 'trunk|T=s' => \
$_trunk, 'tags|t=s' => \
$_tags,
90 'branches|b=s' => \
$_branches, 'prefix=s' => \
$_prefix,
91 'stdlayout|s' => \
$_stdlayout,
92 'minimize-url|m' => \
$Git::SVN
::_minimize_url
,
93 'no-metadata' => sub { $icv{noMetadata
} = 1 },
94 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
95 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
96 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
98 my %cmt_opts = ( 'edit|e' => \
$_edit,
99 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
100 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
101 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
102 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
106 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
107 { 'revision|r=s' => \
$_revision,
108 'fetch-all|all' => \
$_fetch_all,
110 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
111 { 'revision|r=s' => \
$_revision,
112 %fc_opts, %init_opts } ],
113 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
114 " (requires URL argument)",
116 'multi-init' => [ \
&cmd_multi_init
,
117 "Deprecated alias for ".
118 "'$0 init -T<trunk> -b<branches> -t<tags>'",
120 dcommit
=> [ \
&cmd_dcommit
,
121 'Commit several diffs to merge with upstream',
122 { 'merge|m|M' => \
$_merge,
123 'strategy|s=s' => \
$_strategy,
124 'verbose|v' => \
$_verbose,
125 'dry-run|n' => \
$_dry_run,
126 'fetch-all|all' => \
$_fetch_all,
127 'no-rebase' => \
$_no_rebase,
128 %cmt_opts, %fc_opts } ],
129 'set-tree' => [ \
&cmd_set_tree
,
130 "Set an SVN repository to a git tree-ish",
131 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
132 'create-ignore' => [ \
&cmd_create_ignore
,
133 'Create a .gitignore per svn:ignore',
134 { 'revision|r=i' => \
$_revision
136 'propget' => [ \
&cmd_propget
,
137 'Print the value of a property on a file or directory',
138 { 'revision|r=i' => \
$_revision } ],
139 'proplist' => [ \
&cmd_proplist
,
140 'List all properties of a file or directory',
141 { 'revision|r=i' => \
$_revision } ],
142 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
143 { 'revision|r=i' => \
$_revision
145 'multi-fetch' => [ \
&cmd_multi_fetch
,
146 "Deprecated alias for $0 fetch --all",
147 { 'revision|r=s' => \
$_revision, %fc_opts } ],
148 'migrate' => [ sub { },
149 # no-op, we automatically run this anyways,
150 'Migrate configuration/metadata/layout from
151 previous versions of git-svn',
152 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
154 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
155 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
156 'revision|r=s' => \
$_revision,
157 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
158 'incremental' => \
$Git::SVN
::Log
::incremental
,
159 'oneline' => \
$Git::SVN
::Log
::oneline
,
160 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
161 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
162 'authors-file|A=s' => \
$_authors,
163 'color' => \
$Git::SVN
::Log
::color
,
164 'pager=s' => \
$Git::SVN
::Log
::pager
166 'find-rev' => [ \
&cmd_find_rev
, "Translate between SVN revision numbers and tree-ish",
168 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
169 { 'merge|m|M' => \
$_merge,
170 'verbose|v' => \
$_verbose,
171 'strategy|s=s' => \
$_strategy,
172 'local|l' => \
$_local,
173 'fetch-all|all' => \
$_fetch_all,
175 'commit-diff' => [ \
&cmd_commit_diff
,
176 'Commit a diff between two trees',
177 { 'message|m=s' => \
$_message,
178 'file|F=s' => \
$_file,
179 'revision|r=s' => \
$_revision,
181 'info' => [ \
&cmd_info
,
182 "Show info about the latest SVN revision
183 on the current branch",
184 { 'url' => \
$_url, } ],
188 for (my $i = 0; $i < @ARGV; $i++) {
189 if (defined $cmd{$ARGV[$i]}) {
196 # make sure we're always running
197 unless ($cmd =~ /(?:clone|init|multi-init)$/) {
198 unless (-d
$ENV{GIT_DIR
}) {
199 if ($git_dir_user_set) {
200 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
201 "but it is not a directory\n";
203 my $git_dir = delete $ENV{GIT_DIR
};
204 chomp(my $cdup = command_oneline
(qw
/rev-parse --show-cdup/));
205 unless (length $cdup) {
206 die "Already at toplevel, but $git_dir ",
207 "not found '$cdup'\n";
209 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
210 unless (-d
$git_dir) {
211 die "$git_dir still not found after going to ",
214 $ENV{GIT_DIR
} = $git_dir;
218 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
220 read_repo_config
(\
%opts);
221 Getopt
::Long
::Configure
('pass_through') if ($cmd && $cmd eq 'log');
222 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
223 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
224 'id|i=s' => \
$Git::SVN
::default_ref_id
,
225 'svn-remote|remote|R=s' => sub {
226 $Git::SVN
::no_reuse_existing
= 1;
227 $Git::SVN
::default_repo_id
= $_[1] });
228 exit 1 if (!$rv && $cmd && $cmd ne 'log');
231 version
() if $_version;
232 usage
(1) unless defined $cmd;
233 load_authors
() if $_authors;
235 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
236 Git
::SVN
::Migration
::migration_check
();
238 Git
::SVN
::init_vars
();
240 Git
::SVN
::verify_remotes_sanity
();
241 $cmd{$cmd}->[0]->(@ARGV);
244 post_fetch_checkout
();
247 ####################### primary functions ######################
249 my $exit = shift || 0;
250 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
252 git
-svn
- bidirectional operations between a single Subversion tree
and git
253 Usage
: $0 <command
> [options
] [arguments
]\n
255 print $fd "Available commands:\n" unless $cmd;
257 foreach (sort keys %cmd) {
258 next if $cmd && $cmd ne $_;
259 next if /^multi-/; # don't show deprecated commands
260 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
261 foreach (sort keys %{$cmd{$_}->[2]}) {
262 # mixed-case options are for .git/config only
263 next if /[A-Z]/ && /^[a-z]+$/i;
264 # prints out arguments as they should be passed:
265 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
266 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
268 split /\|/,$_)," $x\n";
272 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
273 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
274 one git repository and want to keep them separate. See git-svn(1) for more
281 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
286 unless (-d $ENV{GIT_DIR}) {
287 my @init_db = ('init
');
288 push @init_db, "--template=$_template" if defined $_template;
289 if (defined $_shared) {
290 if ($_shared =~ /[a-z]/) {
291 push @init_db, "--shared=$_shared";
293 push @init_db, "--shared";
296 command_noisy(@init_db);
299 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
300 foreach my $i (keys %icv) {
301 die "'$set' and '$i' cannot both be set\n" if $set;
302 next unless defined $icv{$i};
303 command_noisy('config
', "$pfx.$i", $icv{$i});
309 my $repo_path = shift or return;
310 mkpath([$repo_path]) unless -d $repo_path;
311 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
312 $ENV{GIT_DIR} = '.git';
316 my ($url, $path) = @_;
317 if (!defined $path &&
318 (defined $_trunk || defined $_branches || defined $_tags ||
319 defined $_stdlayout) &&
320 $url !~ m#^[a-z\+]+://#) {
323 $path = basename($url) if !defined $path || !length $path;
324 cmd_init($url, $path);
325 Git::SVN::fetch_all($Git::SVN::default_repo_id);
329 if (defined $_stdlayout) {
330 $_trunk = 'trunk' if (!defined $_trunk);
331 $_tags = 'tags' if (!defined $_tags);
332 $_branches = 'branches' if (!defined $_branches);
334 if (defined $_trunk || defined $_branches || defined $_tags) {
335 return cmd_multi_init(@_);
337 my $url = shift or die "SVN repository location required
",
338 "as a command
-line argument
\n";
342 Git::SVN->init($url);
346 if (grep /^\d+=./, @_) {
347 die "'<rev>=<commit>' fetch arguments are
",
348 "no longer supported
.\n";
352 die "Usage
: $0 fetch
[--all
] [svn
-remote
]\n";
354 $remote ||= $Git::SVN::default_repo_id;
358 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
364 if ($_stdin || !@commits) {
365 print "Reading from stdin
...\n";
368 if (/\b($sha1_short)\b/o) {
369 unshift @commits, $1;
374 foreach my $c (@commits) {
375 my @tmp = command('rev-parse',$c);
376 if (scalar @tmp == 1) {
378 } elsif (scalar @tmp > 1) {
379 push @revs, reverse(command('rev-list',@tmp));
381 fatal "Failed to rev
-parse
$c";
384 my $gs = Git::SVN->new;
385 my ($r_last, $cmt_last) = $gs->last_rev_commit;
387 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
388 fatal "There are new revisions that were fetched
",
389 "and need to be merged
(or acknowledged
) ",
390 "before committing
.\nlast rev
: $r_last\n",
391 " current
: $gs->{last_rev
}";
393 $gs->set_tree($_) foreach @revs;
394 print "Done committing
",scalar @revs," revisions to SVN
\n";
399 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
400 'Cannot dcommit with a dirty index. Commit your changes first, '
401 . "or stash them with
`git stash'.\n";
404 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
405 print "Committing to $url ...\n";
407 die "Unable to determine upstream SVN information from ",
411 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
412 if ($_no_rebase && scalar(@$linear_refs) > 1) {
413 warn "Attempting to commit more than one change while ",
414 "--no-rebase is enabled.\n",
415 "If these changes depend on each other, re-running ",
416 "without --no-rebase will be required."
419 my $d = shift @$linear_refs or last;
420 unless (defined $last_rev) {
421 (undef, $last_rev, undef) = cmt_metadata("$d~1");
422 unless (defined $last_rev) {
423 fatal "Unable to extract revision information ",
428 print "diff-tree $d~1 $d\n";
431 my %ed_opts = ( r => $last_rev,
432 log => get_commit_entry($d)->{log},
433 ra => Git::SVN::Ra->new($gs->full_url),
434 config => SVN::Core::config_get_config(
435 $Git::SVN::Ra::config_dir
440 print "Committed r$_[0]\n";
444 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
445 print "No changes\n$d~1 == $d\n";
446 } elsif ($parents->{$d} && @{$parents->{$d}}) {
447 $gs->{inject_parents_dcommit}->{$cmt_rev} =
450 $_fetch_all ? $gs->fetch_all : $gs->fetch;
453 # we always want to rebase against the current HEAD,
454 # not any head that was passed to us
455 my @diff = command('diff-tree', $d,
459 @finish = rebase_cmd();
460 print STDERR "W: $d and ", $gs->refname,
461 " differ, using @finish:\n",
462 join("\n", @diff), "\n";
464 print "No changes between current HEAD and ",
466 "\nResetting to the latest ",
468 @finish = qw/reset --mixed/;
470 command_noisy(@finish, $gs->refname);
473 my ($url_, $rev_, $uuid_, $gs_) =
474 working_head_info($head, \@refs);
475 my ($linear_refs_, $parents_) =
476 linearize_history($gs_, \@refs);
477 if (scalar(@$linear_refs) !=
478 scalar(@$linear_refs_)) {
479 fatal "# of revisions changed ",
481 join("\n", @$linear_refs),
483 join("\n", @$linear_refs_), "\n",
484 'If you are attempting to commit ',
485 "merges, try running:\n\t",
486 'git rebase --interactive',
487 '--preserve-merges ',
489 "\nBefore dcommitting";
492 fatal "URL mismatch after rebase: ",
495 if ($uuid_ ne $uuid) {
496 fatal "uuid mismatch after rebase: ",
501 for ($i = 0; $i < scalar @$linear_refs; $i++) {
502 my $new = $linear_refs_->[$i] or next;
504 $parents->{$linear_refs->[$i]};
510 $last_rev = $cmt_rev;
516 my $revision_or_hash = shift;
518 if ($revision_or_hash =~ /^r\d+$/) {
522 my (undef, undef, undef, $gs) = working_head_info($head, \@refs);
524 die "Unable to determine upstream SVN information from ",
527 my $desired_revision = substr($revision_or_hash, 1);
528 $result = $gs->rev_db_get($desired_revision);
530 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
533 print "$result\n" if $result;
537 command_noisy(qw/update-index --refresh/);
538 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
540 die "Unable to determine upstream SVN information from ",
541 "working tree history\n";
543 if (command(qw/diff-index HEAD --/)) {
544 print STDERR "Cannot rebase with uncommited changes:\n";
545 command_noisy('status');
549 $_fetch_all ? $gs->fetch_all : $gs->fetch;
551 command_noisy(rebase_cmd(), $gs->refname);
554 sub cmd_show_ignore {
555 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
556 $gs ||= Git::SVN->new;
557 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
558 $gs->prop_walk($gs->{path}, $r, sub {
559 my ($gs, $path, $props) = @_;
560 print STDOUT "\n# $path\n";
561 my $s = $props->{'svn:ignore'} or return;
562 $s =~ s/[\r\n]+/\n/g;
569 sub cmd_create_ignore {
570 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
571 $gs ||= Git::SVN->new;
572 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
573 $gs->prop_walk($gs->{path}, $r, sub {
574 my ($gs, $path, $props) = @_;
575 # $path is of the form /path/to/dir/
576 my $ignore = '.' . $path . '.gitignore';
577 my $s = $props->{'svn:ignore'} or return;
578 open(GITIGNORE, '>', $ignore)
579 or fatal("Failed to open `$ignore' for writing: $!");
580 $s =~ s/[\r\n]+/\n/g;
582 # Prefix all patterns so that the ignore doesn't apply
583 # to sub-directories.
585 print GITIGNORE
"$s\n";
587 or fatal
("Failed to close `$ignore': $!");
588 command_noisy
('add', $ignore);
592 sub canonicalize_path
{
594 my $dot_slash_added = 0;
595 if (substr($path, 0, 1) ne "/") {
596 $path = "./" . $path;
597 $dot_slash_added = 1;
599 # File::Spec->canonpath doesn't collapse x/../y into y (for a
600 # good reason), so let's do this manually.
602 $path =~ s
#/\.(?:/|$)#/#g;
603 $path =~ s
#/[^/]+/\.\.##g;
605 $path =~ s
#^\./## if $dot_slash_added;
611 # Helper for cmd_propget and cmd_proplist below.
614 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
615 $gs ||= Git
::SVN
->new;
617 # prefix THE PATH by the sub-directory from which the user
619 $path = $cmd_dir_prefix . $path;
620 fatal
("No such file or directory: $path") unless -e
$path;
621 my $is_dir = -d
$path ?
1 : 0;
622 $path = $gs->{path
} . '/' . $path;
624 # canonicalize the path (otherwise libsvn will abort or fail to
626 $path = canonicalize_path
($path);
628 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
631 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
634 (undef, $props) = $gs->ra->get_file($path, $r, undef);
639 # cmd_propget (PROP, PATH)
640 # ------------------------
641 # Print the SVN property PROP for PATH.
643 my ($prop, $path) = @_;
644 $path = '.' if not defined $path;
645 usage
(1) if not defined $prop;
646 my $props = get_svnprops
($path);
647 if (not defined $props->{$prop}) {
648 fatal
("`$path' does not have a `$prop' SVN property.");
650 print $props->{$prop} . "\n";
653 # cmd_proplist (PATH)
654 # -------------------
655 # Print the list of SVN properties for PATH.
658 $path = '.' if not defined $path;
659 my $props = get_svnprops
($path);
660 print "Properties on '$path':\n";
661 foreach (sort keys %{$props}) {
668 unless (defined $_trunk || defined $_branches || defined $_tags) {
672 # there are currently some bugs that prevent multi-init/multi-fetch
673 # setups from working well without this.
674 $Git::SVN
::_minimize_url
= 1;
676 $_prefix = '' unless defined $_prefix;
682 if (defined $_trunk) {
683 my $trunk_ref = $_prefix . 'trunk';
684 # try both old-style and new-style lookups:
685 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
687 my ($trunk_url, $trunk_path) =
688 complete_svn_url
($url, $_trunk);
689 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
693 return unless defined $_branches || defined $_tags;
694 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
695 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
696 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
699 sub cmd_multi_fetch
{
700 my $remotes = Git
::SVN
::read_all_remotes
();
701 foreach my $repo_id (sort keys %$remotes) {
702 if ($remotes->{$repo_id}->{url
}) {
703 Git
::SVN
::fetch_all
($repo_id, $remotes);
708 # this command is special because it requires no metadata
709 sub cmd_commit_diff
{
710 my ($ta, $tb, $url) = @_;
711 my $usage = "Usage: $0 commit-diff -r<revision> ".
712 "<tree-ish> <tree-ish> [<URL>]";
713 fatal
($usage) if (!defined $ta || !defined $tb);
716 my $gs = eval { Git
::SVN
->new };
718 fatal
("Needed URL or usable git-svn --id in ",
719 "the command-line\n", $usage);
722 $svn_path = $gs->{path
};
724 unless (defined $_revision) {
725 fatal
("-r|--revision is a required argument\n", $usage);
727 if (defined $_message && defined $_file) {
728 fatal
("Both --message/-m and --file/-F specified ",
729 "for the commit message.\n",
730 "I have no idea what you mean");
732 if (defined $_file) {
733 $_message = file_to_s
($_file);
735 $_message ||= get_commit_entry
($tb)->{log};
737 my $ra ||= Git
::SVN
::Ra
->new($url);
738 $svn_path ||= $ra->{svn_path
};
741 $r = $ra->get_latest_revnum;
742 } elsif ($r !~ /^\d+$/) {
743 die "revision argument: $r not understood by git-svn\n";
745 my %ed_opts = ( r
=> $r,
750 editor_cb
=> sub { print "Committed r$_[0]\n" },
751 svn_path
=> $svn_path );
752 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
753 print "No changes\n$ta == $tb\n";
758 my $path = canonicalize_path
(shift or ".");
759 unless (scalar(@_) == 0) {
760 die "Too many arguments specified\n";
763 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
765 if (!$file_type && !$diff_status) {
766 print STDERR
"$path: (Not a versioned resource)\n\n";
770 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
772 die "Unable to determine upstream SVN information from ",
773 "working tree history\n";
775 my $full_url = $url . ($path eq "." ?
"" : "/$path");
778 print $full_url, "\n";
782 my $result = "Path: $path\n";
783 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
784 $result .= "URL: " . $full_url . "\n";
787 my $repos_root = $gs->repos_root;
788 Git
::SVN
::remove_username
($repos_root);
789 $result .= "Repository Root: $repos_root\n";
792 $result .= "Repository Root: (offline)\n";
794 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
795 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
797 $result .= "Node Kind: " .
798 ($file_type eq "dir" ?
"directory" : "file") . "\n";
800 my $schedule = $diff_status eq "A"
802 : ($diff_status eq "D" ?
"delete" : "normal");
803 $result .= "Schedule: $schedule\n";
805 if ($diff_status eq "A") {
810 my ($lc_author, $lc_rev, $lc_date_utc);
811 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $path);
812 my $log = command_output_pipe
(@args);
813 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
815 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
817 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
818 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
819 (undef, $lc_rev, undef) = ::extract_metadata
($1);
824 Git
::SVN
::Log
::set_local_timezone
();
826 $result .= "Last Changed Author: $lc_author\n";
827 $result .= "Last Changed Rev: $lc_rev\n";
828 $result .= "Last Changed Date: " .
829 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
831 if ($file_type ne "dir") {
832 my $text_last_updated_date =
833 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
835 "Text Last Updated: " .
836 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
839 if ($diff_status eq "D") {
841 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
842 if ($file_type eq "link") {
843 my $file_name = <$fh>;
844 $checksum = Git
::SVN
::Util
::md5sum
("link $file_name");
846 $checksum = Git
::SVN
::Util
::md5sum
($fh);
848 command_close_pipe
($fh, $ctx);
849 } elsif ($file_type eq "link") {
851 command
(qw(cat-file blob), "HEAD:$path");
853 Git
::SVN
::Util
::md5sum
("link " . $file_name);
855 open FILE
, "<", $path or die $!;
856 $checksum = Git
::SVN
::Util
::md5sum
(\
*FILE
);
857 close FILE
or die $!;
859 $result .= "Checksum: " . $checksum . "\n";
865 ########################### utility functions #########################
868 my @cmd = qw
/rebase/;
869 push @cmd, '-v' if $_verbose;
870 push @cmd, qw
/--merge/ if $_merge;
871 push @cmd, "--strategy=$_strategy" if $_strategy;
875 sub post_fetch_checkout
{
876 return if $_no_checkout;
877 my $gs = $Git::SVN
::_head
or return;
878 return if verify_ref
('refs/heads/master^0');
880 my $valid_head = verify_ref
('HEAD^0');
881 command_noisy
(qw(update-ref refs/heads/master), $gs->refname);
882 return if ($valid_head || !verify_ref
('HEAD^0'));
884 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
885 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
888 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
889 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
890 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
891 print STDERR
"Checked out HEAD:\n ",
892 $gs->full_url, " r", $gs->last_rev, "\n";
895 sub complete_svn_url
{
896 my ($url, $path) = @_;
898 if ($path !~ m
#^[a-z\+]+://#) {
899 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
900 fatal
("E: '$path' is not a complete URL ",
901 "and a separate URL is not specified");
903 return ($url, $path);
908 sub complete_url_ls_init
{
909 my ($ra, $repo_path, $switch, $pfx) = @_;
910 unless ($repo_path) {
911 print STDERR
"W: $switch not specified\n";
914 $repo_path =~ s
#/+$##;
915 if ($repo_path =~ m
#^[a-z\+]+://#) {
916 $ra = Git
::SVN
::Ra
->new($repo_path);
919 $repo_path =~ s
#^/+##;
921 fatal
("E: '$repo_path' is not a complete URL ",
922 "and a separate URL is not specified");
925 my $url = $ra->{url
};
926 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
927 my $k = "svn-remote.$gs->{repo_id}.url";
928 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
929 if ($orig_url && ($orig_url ne $gs->{url
})) {
930 die "$k already set: $orig_url\n",
931 "wanted to set to: $gs->{url}\n";
933 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
934 my $remote_path = "$ra->{svn_path}/$repo_path/*";
935 $remote_path =~ s
#/+#/#g;
936 $remote_path =~ s
#^/##g;
937 my ($n) = ($switch =~ /^--(\w+)/);
938 if (length $pfx && $pfx !~ m
#/$#) {
939 die "--prefix='$pfx' must have a trailing slash '/'\n";
941 command_noisy
('config', "svn-remote.$gs->{repo_id}.$n",
942 "$remote_path:refs/remotes/$pfx*");
947 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
951 sub get_tree_from_treeish
{
953 # $treeish can be a symbolic ref, too:
954 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
956 while ($type eq 'tag') {
957 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
959 if ($type eq 'commit') {
960 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
962 ($expected) = ($expected =~ /^tree ($sha1)$/o);
963 die "Unable to get tree from $treeish\n" unless $expected;
964 } elsif ($type eq 'tree') {
965 $expected = $treeish;
967 die "$treeish is a $type, expected tree, tag or commit\n";
972 sub get_commit_entry
{
973 my ($treeish) = shift;
974 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
975 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
976 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
977 open my $log_fh, '>', $commit_editmsg or croak
$!;
979 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
980 if ($type eq 'commit' || $type eq 'tag') {
981 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
986 $in_msg = 1 if (/^\s*$/);
987 } elsif (/^git-svn-id: /) {
988 # skip this for now, we regenerate the
989 # correct one on re-fetch anyways
990 # TODO: set *:merge properties or like...
992 print $log_fh $_ or croak
$!;
995 command_close_pipe
($msg_fh, $ctx);
997 close $log_fh or croak
$!;
999 if ($_edit || ($type eq 'tree')) {
1000 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1001 # TODO: strip out spaces, comments, like git-commit.sh
1002 system($editor, $commit_editmsg);
1004 rename $commit_editmsg, $commit_msg or croak
$!;
1005 open $log_fh, '<', $commit_msg or croak
$!;
1006 { local $/; chomp($log_entry{log} = <$log_fh>); }
1007 close $log_fh or croak
$!;
1013 my ($str, $file, $mode) = @_;
1014 open my $fd,'>',$file or croak
$!;
1015 print $fd $str,"\n" or croak
$!;
1016 close $fd or croak
$!;
1017 chmod ($mode &~ umask, $file) if (defined $mode);
1022 open my $fd,'<',$file or croak
"$!: file: $file\n";
1025 close $fd or croak
$!;
1030 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1032 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1033 my $log = $cmd eq 'log';
1034 while (<$authors>) {
1036 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1037 my ($user, $name, $email) = ($1, $2, $3);
1039 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1041 $users{$user} = [$name, $email];
1044 close $authors or croak
$!;
1047 # convert GetOpt::Long specs for use by git-config
1048 sub read_repo_config
{
1049 return unless -d
$ENV{GIT_DIR
};
1052 foreach my $o (keys %$opts) {
1053 # if we have mixedCase and a long option-only, then
1054 # it's a config-only variable that we don't need for
1056 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1057 my $v = $opts->{$o};
1058 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1060 my $arg = 'git-config';
1061 $arg .= ' --int' if ($o =~ /[:=]i$/);
1062 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1063 if (ref $v eq 'ARRAY') {
1064 chomp(my @tmp = `$arg --get-all svn.$key`);
1067 chomp(my $tmp = `$arg --get svn.$key`);
1068 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1073 delete @
$opts{@config_only} if @config_only;
1076 sub extract_metadata
{
1077 my $id = shift or return (undef, undef, undef);
1078 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1080 if (!defined $rev || !$uuid || !$url) {
1081 # some of the original repositories I made had
1082 # identifiers like this:
1083 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1085 return ($url, $rev, $uuid);
1089 return extract_metadata
((grep(/^git-svn-id: /,
1090 command
(qw
/cat-file commit/, shift)))[-1]);
1093 sub working_head_info
{
1094 my ($head, $refs) = @_;
1095 my @args = ('log', '--no-color', '--first-parent');
1096 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1100 if ( m{^commit ($::sha1)$} ) {
1101 unshift @
$refs, $hash if $hash and $refs;
1105 next unless s{^\s*(git-svn-id:)}{$1};
1106 my ($url, $rev, $uuid) = extract_metadata
($_);
1107 if (defined $url && defined $rev) {
1108 next if $max{$url} and $max{$url} < $rev;
1109 if (my $gs = Git
::SVN
->find_by_url($url)) {
1110 my $c = $gs->rev_db_get($rev);
1111 if ($c && $c eq $hash) {
1112 close $fh; # break the pipe
1113 return ($url, $rev, $uuid, $gs);
1115 $max{$url} ||= $gs->rev_db_max;
1120 command_close_pipe
($fh, $ctx);
1121 (undef, undef, undef, undef);
1124 sub read_commit_parents
{
1125 my ($parents, $c) = @_;
1126 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1127 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1128 @
{$parents->{$c}} = split(/ /, $p);
1131 sub linearize_history
{
1132 my ($gs, $refs) = @_;
1134 foreach my $c (@
$refs) {
1135 read_commit_parents
(\
%parents, $c);
1140 my $last_svn_commit = $gs->last_commit;
1141 foreach my $c (reverse @
$refs) {
1142 next if $c eq $last_svn_commit;
1145 unshift @linear_refs, $c;
1148 # we only want the first parent to diff against for linear
1149 # history, we save the rest to inject when we finalize the
1151 my $fp_a = verify_ref
("$c~1");
1152 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1153 if (!$fp_a || !$fp_b) {
1155 "has no parent commit, and therefore ",
1156 "nothing to diff against.\n",
1157 "You should be working from a repository ",
1158 "originally created by git-svn\n";
1160 if ($fp_a ne $fp_b) {
1161 die "$c~1 = $fp_a, however parsing commit $c ",
1162 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1165 foreach my $p (@
{$parents{$c}}) {
1169 (\
@linear_refs, \
%parents);
1172 sub find_file_type_and_diff_status
{
1174 return ('dir', '') if $path eq '.';
1177 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1178 my $diff_status = (split(' ', $diff_output))[0] || "";
1180 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1182 return (undef, undef) if !$diff_status && !$ls_tree;
1184 if ($diff_status eq "A") {
1185 return ("link", $diff_status) if -l
$path;
1186 return ("dir", $diff_status) if -d
$path;
1187 return ("file", $diff_status);
1190 my $mode = (split(' ', $ls_tree))[0] || "";
1192 return ("link", $diff_status) if $mode eq "120000";
1193 return ("dir", $diff_status) if $mode eq "040000";
1194 return ("file", $diff_status);
1197 package Git
::SVN
::Util
;
1205 my $md5 = Digest
::MD5
->new();
1206 if ($ref eq 'GLOB' || $ref eq 'IO::File') {
1207 $md5->addfile($arg) or croak
$!;
1208 } elsif ($ref eq 'SCALAR') {
1209 $md5->add($$arg) or croak
$!;
1211 $md5->add($arg) or croak
$!;
1213 ::fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1215 return $md5->hexdigest();
1221 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
1222 $_repack $_repack_flags $_use_svm_props $_head
1223 $_use_svnsync_props $no_reuse_existing $_minimize_url/;
1225 use File
::Path qw
/mkpath/;
1226 use File
::Copy qw
/copy/;
1230 # properties that we do not log:
1233 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
1234 svn
:special svn
:executable
1235 svn
:entry
:committed
-rev
1236 svn
:entry
:last-author
1238 svn
:entry
:committed
-date
/;
1240 # some options are read globally, but can be overridden locally
1241 # per [svn-remote "..."] section. Command-line options will *NOT*
1242 # override options set in an [svn-remote "..."] section
1244 for my $option (qw
/follow_parent no_metadata use_svm_props
1245 use_svnsync_props
/) {
1248 my $prop = "-$option";
1251 return $self->{$prop} if exists $self->{$prop};
1252 my $k = "svn-remote.$self->{repo_id}.$key";
1253 eval { command_oneline
(qw
/config --get/, $k) };
1255 $self->{$prop} = ${"Git::SVN::_$option"};
1257 my $v = command_oneline
(qw
/config --bool/,$k);
1258 $self->{$prop} = $v eq 'false' ?
0 : 1;
1260 return $self->{$prop};
1266 END { unlink keys %LOCKFILES if %LOCKFILES }
1268 sub resolve_local_globs
{
1269 my ($url, $fetch, $glob_spec) = @_;
1270 return unless defined $glob_spec;
1271 my $ref = $glob_spec->{ref};
1272 my $path = $glob_spec->{path
};
1273 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
1274 next unless m
#^refs/remotes/$ref->{regex}$#;
1276 my $pathname = desanitize_refname
($path->full_path($p));
1277 my $refname = desanitize_refname
($ref->full_path($p));
1278 if (my $existing = $fetch->{$pathname}) {
1279 if ($existing ne $refname) {
1280 die "Refspec conflict:\n",
1281 "existing: refs/remotes/$existing\n",
1282 " globbed: refs/remotes/$refname\n";
1284 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
1285 $u =~ s!^\Q$url\E(/|$)!! or die
1286 "refs/remotes/$refname: '$url' not found in '$u'\n";
1287 if ($pathname ne $u) {
1288 warn "W: Refspec glob conflict ",
1289 "(ref: refs/remotes/$refname):\n",
1290 "expected path: $pathname\n",
1292 "Continuing ahead with $u\n";
1296 $fetch->{$pathname} = $refname;
1301 sub parse_revision_argument
{
1302 my ($base, $head) = @_;
1303 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
1304 return ($base, $head);
1306 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
1307 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
1308 return ($head, $head) if ($::_revision
eq 'HEAD');
1309 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
1310 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
1311 die "revision argument: $::_revision not understood by git-svn\n";
1315 my ($repo_id, $remotes) = @_;
1319 $repo_id = $gs->{repo_id
};
1321 $remotes ||= read_all_remotes
();
1322 my $remote = $remotes->{$repo_id} or
1323 die "[svn-remote \"$repo_id\"] unknown\n";
1324 my $fetch = $remote->{fetch
};
1325 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
1327 my $ra = Git
::SVN
::Ra
->new($url);
1328 my $uuid = $ra->get_uuid;
1329 my $head = $ra->get_latest_revnum;
1330 my $base = defined $fetch ?
$head : 0;
1332 # read the max revs for wildcard expansion (branches/*, tags/*)
1333 foreach my $t (qw
/branches tags/) {
1334 defined $remote->{$t} or next;
1335 push @globs, $remote->{$t};
1336 my $max_rev = eval { tmp_config
(qw
/--int --get/,
1337 "svn-remote.$repo_id.${t}-maxRev") };
1338 if (defined $max_rev && ($max_rev < $base)) {
1340 } elsif (!defined $max_rev) {
1346 foreach my $p (sort keys %$fetch) {
1347 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
1348 my $lr = $gs->rev_db_max;
1350 $base = $lr if ($lr < $base);
1356 ($base, $head) = parse_revision_argument
($base, $head);
1357 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
1360 sub read_all_remotes
{
1362 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
1363 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
1364 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1365 $local_ref =~ s{^/}{};
1366 $r->{$remote}->{fetch
}->{$local_ref} = $remote_ref;
1367 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1368 $r->{$1}->{url
} = $2;
1369 } elsif (m
!^(.+)\
.(branches
|tags
)=
1370 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
1371 my ($p, $g) = ($3, $4);
1372 my $rs = $r->{$1}->{$2} = {
1375 path
=> Git
::SVN
::GlobSpec
->new($p),
1376 ref => Git
::SVN
::GlobSpec
->new($g) };
1377 if (length($rs->{ref}->{right
}) != 0) {
1378 die "The '*' glob character must be the last ",
1379 "character of '$g'\n";
1387 if (defined $_repack) {
1388 $_repack = 1000 if ($_repack <= 0);
1389 $_repack_nr = $_repack;
1390 $_repack_flags ||= '-d';
1394 sub verify_remotes_sanity
{
1395 return unless -d
$ENV{GIT_DIR
};
1397 foreach (command
(qw
/config -l/)) {
1398 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1400 die "Remote ref refs/remote/$1 is tracked by",
1401 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1402 "Please resolve this ambiguity in ",
1403 "your git configuration file before ",
1411 # we allow more chars than remotes2config.sh...
1412 sub sanitize_remote_name
{
1414 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
1418 sub find_existing_remote
{
1419 my ($url, $remotes) = @_;
1420 return undef if $no_reuse_existing;
1422 foreach my $repo_id (keys %$remotes) {
1423 my $u = $remotes->{$repo_id}->{url
} or next;
1425 $existing = $repo_id;
1431 sub init_remote_config
{
1432 my ($self, $url, $no_write) = @_;
1433 $url =~ s!/+$!!; # strip trailing slash
1434 my $r = read_all_remotes
();
1435 my $existing = find_existing_remote
($url, $r);
1437 unless ($no_write) {
1438 print STDERR
"Using existing ",
1439 "[svn-remote \"$existing\"]\n";
1441 $self->{repo_id
} = $existing;
1442 } elsif ($_minimize_url) {
1443 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1444 $existing = find_existing_remote
($min_url, $r);
1446 unless ($no_write) {
1447 print STDERR
"Using existing ",
1448 "[svn-remote \"$existing\"]\n";
1450 $self->{repo_id
} = $existing;
1452 if ($min_url ne $url) {
1453 unless ($no_write) {
1454 print STDERR
"Using higher level of URL: ",
1455 "$url => $min_url\n";
1457 my $old_path = $self->{path
};
1458 $self->{path
} = $url;
1459 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1460 if (length $old_path) {
1461 $self->{path
} .= "/$old_path";
1468 # verify that we aren't overwriting anything:
1470 command_oneline
('config', '--get',
1471 "svn-remote.$self->{repo_id}.url")
1473 if ($orig_url && ($orig_url ne $url)) {
1474 die "svn-remote.$self->{repo_id}.url already set: ",
1475 "$orig_url\nwanted to set to: $url\n";
1478 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1479 if (defined $xpath) {
1480 die "svn-remote.$xrepo_id.fetch already set to track ",
1481 "$xpath:refs/remotes/", $self->refname, "\n";
1483 unless ($no_write) {
1484 command_noisy
('config',
1485 "svn-remote.$self->{repo_id}.url", $url);
1486 $self->{path
} =~ s{^/}{};
1487 command_noisy
('config', '--add',
1488 "svn-remote.$self->{repo_id}.fetch",
1489 "$self->{path}:".$self->refname);
1491 $self->{url
} = $url;
1494 sub find_by_url
{ # repos_root and, path are optional
1495 my ($class, $full_url, $repos_root, $path) = @_;
1497 return undef unless defined $full_url;
1498 remove_username
($full_url);
1499 remove_username
($repos_root) if defined $repos_root;
1500 my $remotes = read_all_remotes
();
1501 if (defined $full_url && defined $repos_root && !defined $path) {
1503 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1505 foreach my $repo_id (keys %$remotes) {
1506 my $u = $remotes->{$repo_id}->{url
} or next;
1507 remove_username
($u);
1508 next if defined $repos_root && $repos_root ne $u;
1510 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1511 foreach (qw
/branches tags/) {
1512 resolve_local_globs
($u, $fetch,
1513 $remotes->{$repo_id}->{$_});
1516 unless (defined $p) {
1518 $p =~ s
#^\Q$u\E(?:/|$)## or next;
1520 foreach my $f (keys %$fetch) {
1522 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1529 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1530 my $self = _new
($class, $repo_id, $ref_id, $path);
1532 $self->init_remote_config($url, $no_write);
1539 foreach (command
(qw
/config -l/)) {
1540 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1541 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1542 my ($repo_id, $path, $ref) = ($1, $2, $3);
1543 if ($ref eq $ref_id) {
1544 $path = '' if ($path =~ m
#^\./?#);
1545 return ($repo_id, $path);
1548 (undef, undef, undef);
1552 my ($class, $ref_id, $repo_id, $path) = @_;
1553 if (defined $ref_id && !defined $repo_id && !defined $path) {
1554 ($repo_id, $path) = find_ref
($ref_id);
1555 if (!defined $repo_id) {
1556 die "Could not find a \"svn-remote.*.fetch\" key ",
1557 "in the repository configuration matching: ",
1558 "refs/remotes/$ref_id\n";
1561 my $self = _new
($class, $repo_id, $ref_id, $path);
1562 if (!defined $self->{path
} || !length $self->{path
}) {
1563 my $fetch = command_oneline
('config', '--get',
1564 "svn-remote.$repo_id.fetch",
1565 ":refs/remotes/$ref_id\$") or
1566 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1567 "\":refs/remotes/$ref_id\$\" in config\n";
1568 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1570 $self->{url
} = command_oneline
('config', '--get',
1571 "svn-remote.$repo_id.url") or
1572 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1578 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1580 # It cannot end with a slash /, we'll throw up on this because
1581 # SVN can't have directories with a slash in their name, either:
1582 if ($refname =~ m{/$}) {
1583 die "ref: '$refname' ends with a trailing slash, this is ",
1584 "not permitted by git nor Subversion\n";
1587 # It cannot have ASCII control character space, tilde ~, caret ^,
1588 # colon :, question-mark ?, asterisk *, space, or open bracket [
1591 # Additionally, % must be escaped because it is used for escaping
1592 # and we want our escaped refname to be reversible
1593 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1595 # no slash-separated component can begin with a dot .
1597 $refname =~ s{/\.}{/%2E}g;
1599 # It cannot have two consecutive dots .. anywhere
1601 $refname =~ s{\.\.}{%2E%2E}g;
1606 sub desanitize_refname
{
1608 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1614 return $self->{svm
}->{uuid
} if $self->svm;
1616 unless ($self->{svm
}) {
1617 die "SVM UUID not cached, and reading remotely failed\n";
1619 $self->{svm
}->{uuid
};
1624 return $self->{svm
} if $self->{svm
};
1626 # see if we have it in our config, first:
1628 my $section = "svn-remote.$self->{repo_id}";
1630 source
=> tmp_config
('--get', "$section.svm-source"),
1631 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1632 replace
=> tmp_config
('--get', "$section.svm-replace"),
1635 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1636 $self->{svm
} = $svm;
1642 my ($self, $ra) = @_;
1643 return $ra if $self->svm;
1645 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1646 "(svm:source, svm:uuid) ",
1647 "from the following URLs:\n" );
1648 sub read_svm_props
{
1649 my ($self, $ra, $path, $r) = @_;
1650 my $props = ($ra->get_dir($path, $r))[2];
1651 my $src = $props->{'svm:source'};
1652 my $uuid = $props->{'svm:uuid'};
1653 return undef if (!$src || !$uuid);
1657 $uuid =~ m{^[0-9a-f\-]{30,}$}
1658 or die "doesn't look right - svm:uuid is '$uuid'\n";
1660 # the '!' is used to mark the repos_root!/relative/path
1661 $src =~ s{/?!/?}{/};
1662 $src =~ s{/+$}{}; # no trailing slashes please
1663 # username is of no interest
1664 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1666 my $replace = $ra->{url
};
1667 $replace .= "/$path" if length $path;
1669 my $section = "svn-remote.$self->{repo_id}";
1670 tmp_config
("$section.svm-source", $src);
1671 tmp_config
("$section.svm-replace", $replace);
1672 tmp_config
("$section.svm-uuid", $uuid);
1680 my $r = $ra->get_latest_revnum;
1681 my $path = $self->{path
};
1683 while (length $path) {
1684 unless ($tried{"$self->{url}/$path"}) {
1685 return $ra if $self->read_svm_props($ra, $path, $r);
1686 $tried{"$self->{url}/$path"} = 1;
1688 $path =~ s
#/?[^/]+$##;
1690 die "Path: '$path' should be ''\n" if $path ne '';
1691 return $ra if $self->read_svm_props($ra, $path, $r);
1692 $tried{"$self->{url}/$path"} = 1;
1694 if ($ra->{repos_root
} eq $self->{url
}) {
1695 die @err, (map { " $_\n" } keys %tried), "\n";
1698 # nope, make sure we're connected to the repository root:
1701 $path = $ra->{svn_path
};
1702 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1703 while (length $path) {
1704 unless ($tried{"$ra->{url}/$path"}) {
1705 $ok = $self->read_svm_props($ra, $path, $r);
1707 $tried{"$ra->{url}/$path"} = 1;
1709 $path =~ s
#/?[^/]+$##;
1711 die "Path: '$path' should be ''\n" if $path ne '';
1712 $ok ||= $self->read_svm_props($ra, $path, $r);
1713 $tried{"$ra->{url}/$path"} = 1;
1715 die @err, (map { " $_\n" } keys %tried), "\n";
1717 Git
::SVN
::Ra
->new($self->{url
});
1722 return $self->{svnsync
} if $self->{svnsync
};
1724 if ($self->no_metadata) {
1725 die "Can't have both 'noMetadata' and ",
1726 "'useSvnsyncProps' options set!\n";
1728 if ($self->rewrite_root) {
1729 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1734 # see if we have it in our config, first:
1736 my $section = "svn-remote.$self->{repo_id}";
1738 url
=> tmp_config
('--get', "$section.svnsync-url"),
1739 uuid
=> tmp_config
('--get', "$section.svnsync-uuid"),
1742 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1743 return $self->{svnsync
} = $svnsync;
1746 my $err = "useSvnsyncProps set, but failed to read " .
1747 "svnsync property: svn:sync-from-";
1748 my $rp = $self->ra->rev_proplist(0);
1750 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1751 $url =~ m{^[a-z\+]+://} or
1752 die "doesn't look right - svn:sync-from-url is '$url'\n";
1754 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1755 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1756 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1758 my $section = "svn-remote.$self->{repo_id}";
1759 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1760 tmp_config
('--add', "$section.svnsync-url", $url);
1761 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1764 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1765 # remote lookup (useful for 'git svn log').
1768 unless ($self->{ra_uuid
}) {
1769 my $key = "svn-remote.$self->{repo_id}.uuid";
1770 my $uuid = eval { tmp_config
('--get', $key) };
1771 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1772 $self->{ra_uuid
} = $uuid;
1774 die "ra_uuid called without URL\n" unless $self->{url
};
1775 $self->{ra_uuid
} = $self->ra->get_uuid;
1776 tmp_config
('--add', $key, $self->{ra_uuid
});
1782 sub _set_repos_root
{
1783 my ($self, $repos_root) = @_;
1784 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1785 $repos_root ||= $self->ra->{repos_root
};
1786 tmp_config
($k, $repos_root);
1792 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1793 eval { tmp_config
('--get', $k) } || $self->_set_repos_root;
1798 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1799 $self->_set_repos_root($ra->{repos_root
});
1800 if ($self->use_svm_props && !$self->{svm
}) {
1801 if ($self->no_metadata) {
1802 die "Can't have both 'noMetadata' and ",
1803 "'useSvmProps' options set!\n";
1804 } elsif ($self->use_svnsync_props) {
1805 die "Can't have both 'useSvnsyncProps' and ",
1806 "'useSvmProps' options set!\n";
1808 $ra = $self->_set_svm_vars($ra);
1809 $self->{-want_revprops
} = 1;
1816 my $repos_root = $self->ra->{repos_root
};
1817 return $self->{path
} if ($self->{url
} eq $repos_root);
1818 my $url = $self->{url
} .
1819 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
1820 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1824 # prop_walk(PATH, REV, SUB)
1825 # -------------------------
1826 # Recursively traverse PATH at revision REV and invoke SUB for each
1827 # directory that contains a SVN property. SUB will be invoked as
1828 # follows: &SUB(gs, path, props); where `gs' is this instance of
1829 # Git::SVN, `path' the path to the directory where the properties
1830 # `props' were found. The `path' will be relative to point of checkout,
1831 # that is, if url://repo/trunk is the current Git branch, and that
1832 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
1833 # as `path' (note the trailing `/').
1835 my ($self, $path, $rev, $sub) = @_;
1837 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
1840 # Strip the irrelevant part of the path.
1841 $p =~ s
#^/+\Q$self->{path}\E(/|$)#/#;
1842 # Ensure the path is terminated by a `/'.
1845 # The properties contain all the internal SVN stuff nobody
1846 # (usually) cares about.
1847 my $interesting_props = 0;
1848 foreach (keys %{$props}) {
1849 # If it doesn't start with `svn:', it must be a
1850 # user-defined property.
1851 ++$interesting_props and next if $_ !~ /^svn:/;
1852 # FIXME: Fragile, if SVN adds new public properties,
1853 # this needs to be updated.
1854 ++$interesting_props if /^svn
:(?
:ignore
|keywords
|executable
1855 |eol
-style
|mime
-type
1856 |externals
|needs
-lock)$/x
;
1858 &$sub($self, $p, $props) if $interesting_props;
1860 foreach (sort keys %$dirent) {
1861 next if $dirent->{$_}->{kind
} != $SVN::Node
::dir
;
1862 $self->prop_walk($path . '/' . $_, $rev, $sub);
1866 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1867 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1869 # returns the newest SVN revision number and newest commit SHA1
1870 sub last_rev_commit
{
1872 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1873 return ($self->{last_rev
}, $self->{last_commit
});
1875 my $c = ::verify_ref
($self->refname.'^0');
1876 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1877 my $rev = (::cmt_metadata
($c))[1];
1879 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1883 my $db_path = $self->db_path;
1884 unless (-e
$db_path) {
1885 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1886 return (undef, undef);
1888 my $offset = -41; # from tail
1890 open my $fh, '<', $db_path or croak
"$db_path not readable: $!\n";
1891 sysseek($fh, $offset, 2); # don't care for errors
1892 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1894 while (('0' x40
) eq $rl && sysseek($fh, 0, 1) != 0) {
1896 sysseek($fh, $offset, 2); # don't care for errors
1897 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1900 if ($c && $c ne $rl) {
1901 die "$db_path and ", $self->refname,
1902 " inconsistent!:\n$c != $rl\n";
1904 my $rev = sysseek($fh, 0, 1) or croak
$!;
1905 $rev = ($rev - 41) / 41;
1906 close $fh or croak
$!;
1907 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1911 sub get_fetch_range
{
1912 my ($self, $min, $max) = @_;
1913 $max ||= $self->ra->get_latest_revnum;
1914 $min ||= $self->rev_db_max;
1920 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1921 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1922 if (! -f
$config && -f
$old_def_config) {
1923 rename $old_def_config, $config or
1924 die "Failed rename $old_def_config => $config: $!\n";
1926 my $old_config = $ENV{GIT_CONFIG
};
1927 $ENV{GIT_CONFIG
} = $config;
1930 unless (-f
$config) {
1932 open my $fh, '>', $config or
1933 die "Can't open $config: $!\n";
1934 print $fh "; This file is used internally by ",
1936 "Couldn't write to $config: $!\n";
1937 print $fh "; You should not have to edit it\n" or
1938 die "Couldn't write to $config: $!\n";
1939 close $fh or die "Couldn't close $config: $!\n";
1941 command
('config', @args);
1944 if (defined $old_config) {
1945 $ENV{GIT_CONFIG
} = $old_config;
1947 delete $ENV{GIT_CONFIG
};
1950 wantarray ?
@ret : $ret[0];
1954 my ($self, $sub) = @_;
1955 my $old_index = $ENV{GIT_INDEX_FILE
};
1956 $ENV{GIT_INDEX_FILE
} = $self->{index};
1959 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
1960 mkpath
([$dir]) unless -d
$dir;
1964 if (defined $old_index) {
1965 $ENV{GIT_INDEX_FILE
} = $old_index;
1967 delete $ENV{GIT_INDEX_FILE
};
1970 wantarray ?
@ret : $ret[0];
1973 sub assert_index_clean
{
1974 my ($self, $treeish) = @_;
1976 $self->tmp_index_do(sub {
1977 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1978 my $x = command_oneline
('write-tree');
1979 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1980 /^tree ($::sha1)/mo);
1983 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1984 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1985 command_noisy
('read-tree', $treeish);
1986 $x = command_oneline
('write-tree');
1988 ::fatal
"trees ($treeish) $y != $x\n",
1989 "Something is seriously wrong...";
1994 sub get_commit_parents
{
1995 my ($self, $log_entry) = @_;
1996 my (%seen, @ret, @tmp);
1997 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1998 if (my $ip = $self->{inject_parents
}) {
1999 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
2003 if (my $cur = ::verify_ref
($self->refname.'^0')) {
2006 if (my $ipd = $self->{inject_parents_dcommit
}) {
2007 if (my $commit = delete $ipd->{$log_entry->{revision
}}) {
2008 push @tmp, @
$commit;
2011 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
2012 while (my $p = shift @tmp) {
2016 # MAXPARENT is defined to 16 in commit-tree.c:
2020 die "r$log_entry->{revision}: No room for parents:\n\t",
2021 join("\n\t", @tmp), "\n";
2028 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
2029 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2030 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
2033 if ($rwr !~ m
#^[a-z\+]+://#) {
2034 die "$rwr is not a valid URL (key: $k)\n";
2037 $self->{-rewrite_root
} = $rwr;
2042 ($self->rewrite_root || $self->{url
}) .
2043 (length $self->{path
} ?
'/' . $self->{path
} : '');
2048 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
2052 my ($self, $log_entry) = @_;
2053 my $lr = $self->last_rev;
2054 if (defined $lr && $lr >= $log_entry->{revision
}) {
2055 die "Last fetched revision of ", $self->refname,
2056 " was r$lr, but we are about to fetch: ",
2057 "r$log_entry->{revision}!\n";
2059 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
2060 croak
"$log_entry->{revision} = $c already exists! ",
2061 "Why are we refetching it?\n";
2063 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $log_entry->{name
};
2064 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
2065 $log_entry->{email
};
2066 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
2068 my $tree = $log_entry->{tree
};
2069 if (!defined $tree) {
2070 $tree = $self->tmp_index_do(sub {
2071 command_oneline
('write-tree') });
2073 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2075 my @exec = ('git-commit-tree', $tree);
2076 foreach ($self->get_commit_parents($log_entry)) {
2077 push @exec, '-p', $_;
2079 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2081 print $msg_fh $log_entry->{log} or croak
$!;
2082 unless ($self->no_metadata) {
2083 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2086 $msg_fh->flush == 0 or croak
$!;
2087 close $msg_fh or croak
$!;
2088 chomp(my $commit = do { local $/; <$out_fh> });
2089 close $out_fh or croak
$!;
2092 if ($commit !~ /^$::sha1$/o) {
2093 die "Failed to commit, invalid sha1: $commit\n";
2096 $self->rev_db_set($log_entry->{revision
}, $commit, 1);
2098 $self->{last_rev
} = $log_entry->{revision
};
2099 $self->{last_commit
} = $commit;
2100 print "r$log_entry->{revision}";
2101 if (defined $log_entry->{svm_revision
}) {
2102 print " (\@$log_entry->{svm_revision})";
2103 $self->rev_db_set($log_entry->{svm_revision
}, $commit,
2104 0, $self->svm_uuid);
2106 print " = $commit ($self->{ref_id})\n";
2107 if (defined $_repack && (--$_repack_nr == 0)) {
2108 $_repack_nr = $_repack;
2109 # repack doesn't use any arguments with spaces in them, does it?
2110 print "Running git repack $_repack_flags ...\n";
2111 command_noisy
('repack', split(/\s+/, $_repack_flags));
2112 print "Done repacking\n";
2118 my ($self, $paths, $r) = @_;
2119 return 1 if $self->{path
} eq '';
2120 if (my $path = $paths->{"/$self->{path}"}) {
2121 return ($path->{action
} eq 'D') ?
0 : 1;
2123 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
2124 if (grep /$self->{path_regex}/, keys %$paths) {
2128 foreach (split m
#/#, $self->{path}) {
2130 next unless ($paths->{$c} &&
2131 ($paths->{$c}->{action
} =~ /^[AR]$/));
2132 if ($self->ra->check_path($self->{path
}, $r) ==
2140 sub find_parent_branch
{
2141 my ($self, $paths, $rev) = @_;
2142 return undef unless $self->follow_parent;
2143 unless (defined $paths) {
2144 my $err_handler = $SVN::Error
::handler
;
2145 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
2146 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
2148 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
2149 $SVN::Error
::handler
= $err_handler;
2151 return undef unless defined $paths;
2153 # look for a parent from another branch:
2154 my @b_path_components = split m
#/#, $self->rel_path;
2155 my @a_path_components;
2157 while (@b_path_components) {
2158 $i = $paths->{'/'.join('/', @b_path_components)};
2159 last if $i && defined $i->{copyfrom_path
};
2160 unshift(@a_path_components, pop(@b_path_components));
2162 return undef unless defined $i && defined $i->{copyfrom_path
};
2163 my $branch_from = $i->{copyfrom_path
};
2164 if (@a_path_components) {
2165 print STDERR
"branch_from: $branch_from => ";
2166 $branch_from .= '/'.join('/', @a_path_components);
2167 print STDERR
$branch_from, "\n";
2169 my $r = $i->{copyfrom_rev
};
2170 my $repos_root = $self->ra->{repos_root
};
2171 my $url = $self->ra->{url
};
2172 my $new_url = $repos_root . $branch_from;
2173 print STDERR
"Found possible branch point: ",
2174 "$new_url => ", $self->full_url, ", $r\n";
2175 $branch_from =~ s
#^/##;
2176 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
2178 my $ref_id = $self->{ref_id
};
2179 $ref_id =~ s/\@\d+$//;
2181 # just grow a tail if we're not unique enough :x
2182 $ref_id .= '-' while find_ref
($ref_id);
2183 print STDERR
"Initializing parent: $ref_id\n";
2184 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id, 1);
2186 my ($r0, $parent) = $gs->find_rev_before($r, 1);
2187 if (!defined $r0 || !defined $parent) {
2188 my ($base, $head) = parse_revision_argument
(0, $r);
2190 $gs->fetch($base, $r);
2192 ($r0, $parent) = $gs->last_rev_commit;
2194 if (defined $r0 && defined $parent) {
2195 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
2197 if ($self->ra->can_do_switch) {
2198 $self->assert_index_clean($parent);
2199 print STDERR
"Following parent with do_switch\n";
2200 # do_switch works with svn/trunk >= r22312, but that
2201 # is not included with SVN 1.4.3 (the latest version
2202 # at the moment), so we can't rely on it
2203 $self->{last_commit
} = $parent;
2204 $ed = SVN
::Git
::Fetcher
->new($self);
2205 $gs->ra->gs_do_switch($r0, $rev, $gs,
2206 $self->full_url, $ed)
2207 or die "SVN connection failed somewhere...\n";
2208 } elsif ($self->ra->trees_match($new_url, $r0,
2209 $self->full_url, $rev)) {
2210 print STDERR
"Trees match:\n",
2212 " ${\$self->full_url}\@$rev\n",
2213 "Following parent with no changes\n";
2214 $self->tmp_index_do(sub {
2215 command_noisy
('read-tree', $parent);
2217 $self->{last_commit
} = $parent;
2219 print STDERR
"Following parent with do_update\n";
2220 $ed = SVN
::Git
::Fetcher
->new($self);
2221 $self->ra->gs_do_update($rev, $rev, $self, $ed)
2222 or die "SVN connection failed somewhere...\n";
2224 print STDERR
"Successfully followed parent\n";
2225 return $self->make_log_entry($rev, [$parent], $ed);
2231 my ($self, $paths, $rev) = @_;
2233 my ($last_rev, @parents);
2234 if (my $lc = $self->last_commit) {
2235 # we can have a branch that was deleted, then re-added
2236 # under the same name but copied from another path, in
2237 # which case we'll have multiple parents (we don't
2238 # want to break the original ref, nor lose copypath info):
2239 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2240 push @
{$log_entry->{parents
}}, $lc;
2243 $ed = SVN
::Git
::Fetcher
->new($self);
2244 $last_rev = $self->{last_rev
};
2249 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2252 $ed = SVN
::Git
::Fetcher
->new($self);
2254 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
2255 die "SVN connection failed somewhere...\n";
2257 $self->make_log_entry($rev, \
@parents, $ed);
2261 my ($self, $ed) = @_;
2263 my $h = $ed->{empty
};
2264 foreach (sort keys %$h) {
2265 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2266 push @out, " $act: " . uri_encode
($_);
2267 warn "W: $act: $_\n";
2269 foreach my $t (qw
/dir_prop file_prop/) {
2270 $h = $ed->{$t} or next;
2271 foreach my $path (sort keys %$h) {
2272 my $ppath = $path eq '' ?
'.' : $path;
2273 foreach my $prop (sort keys %{$h->{$path}}) {
2274 next if $SKIP_PROP{$prop};
2275 my $v = $h->{$path}->{$prop};
2276 my $t_ppath_prop = "$t: " .
2277 uri_encode
($ppath) . ' ' .
2280 push @out, " +$t_ppath_prop " .
2283 push @out, " -$t_ppath_prop";
2288 foreach my $t (qw
/absent_file absent_directory/) {
2289 $h = $ed->{$t} or next;
2290 foreach my $parent (sort keys %$h) {
2291 foreach my $path (sort @
{$h->{$parent}}) {
2292 push @out, " $t: " .
2293 uri_encode
("$parent/$path");
2294 warn "W: $t: $parent/$path ",
2295 "Insufficient permissions?\n";
2302 sub parse_svn_date
{
2303 my $date = shift || return '+0000 1970-01-01 00:00:00';
2304 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2305 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
2306 croak
"Unable to parse date: $date\n";
2307 "+0000 $Y-$m-$d $H:$M:$S";
2312 if (!defined $author || length $author == 0) {
2313 $author = '(no author)';
2315 if (defined $::_authors
&& ! defined $::users
{$author}) {
2316 die "Author: $author not defined in $::_authors file\n";
2321 sub make_log_entry
{
2322 my ($self, $rev, $parents, $ed) = @_;
2323 my $untracked = $self->get_untracked($ed);
2325 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
2326 print $un "r$rev\n" or croak
$!;
2327 print $un $_, "\n" foreach @
$untracked;
2328 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
2332 my $logged = delete $self->{logged_rev_props
};
2333 if (!$logged || $self->{-want_revprops
}) {
2334 my $rp = $self->ra->rev_proplist($rev);
2335 foreach (sort keys %$rp) {
2337 if (/^svn:(author|date|log)$/) {
2338 $log_entry{$1} = $v;
2339 } elsif ($_ eq 'svm:headrev') {
2342 print $un " rev_prop: ", uri_encode
($_), ' ',
2343 uri_encode
($v), "\n";
2347 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
2349 close $un or croak
$!;
2351 $log_entry{date
} = parse_svn_date
($log_entry{date
});
2352 $log_entry{log} .= "\n";
2353 my $author = $log_entry{author
} = check_author
($log_entry{author
});
2354 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
2356 if (defined $headrev && $self->use_svm_props) {
2357 if ($self->rewrite_root) {
2358 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2361 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
2362 # we don't want "SVM: initializing mirror for junk" ...
2363 return undef if $r == 0;
2364 my $svm = $self->svm;
2365 if ($uuid ne $svm->{uuid
}) {
2366 die "UUID mismatch on SVM path:\n",
2367 "expected: $svm->{uuid}\n",
2370 my $full_url = $self->full_url;
2371 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2372 die "Failed to replace '$svm->{replace}' with ",
2373 "'$svm->{source}' in $full_url\n";
2374 # throw away username for storing in records
2375 remove_username
($full_url);
2376 $log_entry{metadata
} = "$full_url\@$r $uuid";
2377 $log_entry{svm_revision
} = $r;
2378 $email ||= "$author\@$uuid"
2379 } elsif ($self->use_svnsync_props) {
2380 my $full_url = $self->svnsync->{url
};
2381 $full_url .= "/$self->{path}" if length $self->{path
};
2382 remove_username
($full_url);
2383 my $uuid = $self->svnsync->{uuid
};
2384 $log_entry{metadata
} = "$full_url\@$rev $uuid";
2385 $email ||= "$author\@$uuid"
2387 my $url = $self->metadata_url;
2388 remove_username
($url);
2389 $log_entry{metadata
} = "$url\@$rev " .
2390 $self->ra->get_uuid;
2391 $email ||= "$author\@" . $self->ra->get_uuid;
2393 $log_entry{name
} = $name;
2394 $log_entry{email
} = $email;
2399 my ($self, $min_rev, $max_rev, @parents) = @_;
2400 my ($last_rev, $last_commit) = $self->last_rev_commit;
2401 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
2402 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
2406 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2407 $self->{inject_parents
} = { $rev => $tree };
2408 $self->fetch(undef, undef);
2412 my ($self, $tree) = (shift, shift);
2413 my $log_entry = ::get_commit_entry
($tree);
2414 unless ($self->{last_rev
}) {
2415 fatal
("Must have an existing revision to commit");
2417 my %ed_opts = ( r
=> $self->{last_rev
},
2418 log => $log_entry->{log},
2420 tree_a
=> $self->{last_commit
},
2423 $self->set_tree_cb($log_entry, $tree, @_) },
2424 svn_path
=> $self->{path
} );
2425 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
2426 print "No changes\nr$self->{last_rev} = $tree\n";
2432 my $db_path = $self->db_path;
2433 return if (-e
$db_path && ! -z
$db_path);
2434 return unless ::verify_ref
($self->refname.'^0');
2435 if (-f
$self->{db_root
}) {
2436 rename $self->{db_root
}, $db_path or die
2437 "rename $self->{db_root} => $db_path failed: $!\n";
2438 my ($dir, $base) = ($db_path =~ m
#^(.*?)/?([^/]+)$#);
2439 symlink $base, $self->{db_root
} or die
2440 "symlink $base => $self->{db_root} failed: $!\n";
2443 print "Rebuilding $db_path ...\n";
2444 my ($log, $ctx) = command_output_pipe
("log", '--no-color', $self->refname);
2446 my $full_url = $self->full_url;
2447 remove_username
($full_url);
2451 if ( m{^commit ($::sha1)$} ) {
2455 next unless s{^\s*(git-svn-id:)}{$1};
2456 my ($url, $rev, $uuid) = ::extract_metadata
($_);
2457 remove_username
($url);
2459 # ignore merges (from set-tree)
2460 next if (!defined $rev || !$uuid);
2462 # if we merged or otherwise started elsewhere, this is
2463 # how we break out of it
2464 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
2465 ($full_url && $url && ($url ne $full_url))) {
2469 $svn_uuid ||= $uuid;
2471 $self->rev_db_set($rev, $c);
2472 print "r$rev = $c\n";
2474 command_close_pipe
($log, $ctx);
2475 print "Done rebuilding $db_path\n";
2479 # Tie::File seems to be prone to offset errors if revisions get sparse,
2480 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2481 # one of my favorite modules is out :< Next up would be one of the DBM
2482 # modules, but I'm not sure which is most portable... So I'll just
2483 # go with something that's plain-text, but still capable of
2484 # being randomly accessed. So here's my ultra-simple fixed-width
2485 # database. All records are 40 characters + "\n", so it's easy to seek
2486 # to a revision: (41 * rev) is the byte offset.
2487 # A record of 40 0s denotes an empty revision.
2488 # And yes, it's still pretty fast (faster than Tie::File).
2489 # These files are disposable unless noMetadata or useSvmProps is set
2492 my ($fh, $rev, $commit) = @_;
2493 my $offset = $rev * 41;
2494 # assume that append is the common case:
2495 seek $fh, 0, 2 or croak
$!;
2497 if ($pos < $offset) {
2498 for (1 .. (($offset - $pos) / 41)) {
2499 print $fh (('0' x
40),"\n") or croak
$!;
2502 seek $fh, $offset, 0 or croak
$!;
2503 print $fh $commit,"\n" or croak
$!;
2509 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2510 mkpath
([$dir]) unless -d
$dir;
2511 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2512 close $fh or die "Couldn't close (create) $path: $!\n";
2517 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2518 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2519 my $db = $self->db_path($uuid);
2520 my $db_lock = "$db.lock";
2523 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2524 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
2528 $LOCKFILES{$db_lock} = 1;
2530 # both of these options make our .rev_db file very, very important
2531 # and we can't afford to lose it because rebuild() won't work
2532 if ($self->use_svm_props || $self->no_metadata) {
2534 copy
($db, $db_lock) or die "rev_db_set(@_): ",
2536 "$db => $db_lock ($!)\n";
2538 rename $db, $db_lock or die "rev_db_set(@_): ",
2539 "Failed to rename: ",
2540 "$db => $db_lock ($!)\n";
2542 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2543 _rev_db_set
($fh, $rev, $commit);
2545 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2546 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2548 close $fh or croak
$!;
2551 command_noisy
('update-ref', '-m', "r$rev",
2552 $self->refname, $commit);
2554 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2555 "$db_lock => $db ($!)\n";
2556 delete $LOCKFILES{$db_lock};
2558 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2559 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2560 kill $sig, $$ if defined $sig;
2567 my $db_path = $self->db_path;
2568 my @stat = stat $db_path or return 0;
2569 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2570 my $max = $stat[7] / 41;
2571 (($max > 0) ?
$max - 1 : 0);
2575 my ($self, $rev, $uuid) = @_;
2577 my $offset = $rev * 41;
2578 my $db_path = $self->db_path($uuid);
2579 return undef unless -e
$db_path;
2580 open my $fh, '<', $db_path or croak
$!;
2581 if (sysseek($fh, $offset, 0) == $offset) {
2582 my $read = sysread($fh, $ret, 40);
2583 $ret = undef if ($read != 40 || $ret eq ('0'x40
));
2585 close $fh or croak
$!;
2589 # Finds the first svn revision that exists on (if $eq_ok is true) or
2590 # before $rev for the current branch. It will not search any lower
2591 # than $min_rev. Returns the git commit hash and svn revision number
2592 # if found, else (undef, undef).
2593 sub find_rev_before
{
2594 my ($self, $rev, $eq_ok, $min_rev) = @_;
2595 --$rev unless $eq_ok;
2597 while ($rev >= $min_rev) {
2598 if (my $c = $self->rev_db_get($rev)) {
2603 return (undef, undef);
2606 # Finds the first svn revision that exists on (if $eq_ok is true) or
2607 # after $rev for the current branch. It will not search any higher
2608 # than $max_rev. Returns the git commit hash and svn revision number
2609 # if found, else (undef, undef).
2610 sub find_rev_after
{
2611 my ($self, $rev, $eq_ok, $max_rev) = @_;
2612 ++$rev unless $eq_ok;
2613 $max_rev ||= $self->rev_db_max();
2614 while ($rev <= $max_rev) {
2615 if (my $c = $self->rev_db_get($rev)) {
2620 return (undef, undef);
2624 my ($class, $repo_id, $ref_id, $path) = @_;
2625 unless (defined $repo_id && length $repo_id) {
2626 $repo_id = $Git::SVN
::default_repo_id
;
2628 unless (defined $ref_id && length $ref_id) {
2629 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2631 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
2632 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2633 $_[3] = $path = '' unless (defined $path);
2634 mkpath
(["$ENV{GIT_DIR}/svn"]);
2636 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2637 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2638 db_root
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
2642 my ($self, $uuid) = @_;
2643 $uuid ||= $self->ra_uuid;
2644 "$self->{db_root}.$uuid";
2649 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2653 sub remove_username
{
2654 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2657 package Git
::SVN
::Prompt
;
2661 use vars qw
/$_no_auth_cache $_username/;
2664 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2665 $may_save = undef if $_no_auth_cache;
2666 $default_username = $_username if defined $_username;
2667 if (defined $default_username && length $default_username) {
2668 if (defined $realm && length $realm) {
2669 print STDERR
"Authentication realm: $realm\n";
2672 $cred->username($default_username);
2674 username
($cred, $realm, $may_save, $pool);
2676 $cred->password(_read_password
("Password for '" .
2677 $cred->username . "': ", $realm));
2678 $cred->may_save($may_save);
2679 $SVN::_Core
::SVN_NO_ERROR
;
2682 sub ssl_server_trust
{
2683 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2684 $may_save = undef if $_no_auth_cache;
2685 print STDERR
"Error validating server certificate for '$realm':\n";
2688 # All variables SVN::Auth::SSL::* are used only once,
2689 # so we're shutting up Perl warnings about this.
2690 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2691 print STDERR
" - The certificate is not issued ",
2692 "by a trusted authority. Use the\n",
2693 " fingerprint to validate ",
2694 "the certificate manually!\n";
2696 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2697 print STDERR
" - The certificate hostname ",
2698 "does not match.\n";
2700 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2701 print STDERR
" - The certificate is not yet valid.\n";
2703 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2704 print STDERR
" - The certificate has expired.\n";
2706 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2707 print STDERR
" - The certificate has ",
2708 "an unknown error.\n";
2710 } # no warnings 'once'
2712 "Certificate information:\n".
2713 " - Hostname: %s\n".
2714 " - Valid: from %s until %s\n".
2716 " - Fingerprint: %s\n",
2717 map $cert_info->$_, qw(hostname valid_from valid_until
2718 issuer_dname fingerprint);
2721 print STDERR
$may_save ?
2722 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2723 "(R)eject or accept (t)emporarily? ";
2725 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2726 if ($choice =~ /^t$/i) {
2727 $cred->may_save(undef);
2728 } elsif ($choice =~ /^r$/i) {
2730 } elsif ($may_save && $choice =~ /^p$/i) {
2731 $cred->may_save($may_save);
2735 $cred->accepted_failures($failures);
2736 $SVN::_Core
::SVN_NO_ERROR
;
2739 sub ssl_client_cert
{
2740 my ($cred, $realm, $may_save, $pool) = @_;
2741 $may_save = undef if $_no_auth_cache;
2742 print STDERR
"Client certificate filename: ";
2744 chomp(my $filename = <STDIN
>);
2745 $cred->cert_file($filename);
2746 $cred->may_save($may_save);
2747 $SVN::_Core
::SVN_NO_ERROR
;
2750 sub ssl_client_cert_pw
{
2751 my ($cred, $realm, $may_save, $pool) = @_;
2752 $may_save = undef if $_no_auth_cache;
2753 $cred->password(_read_password
("Password: ", $realm));
2754 $cred->may_save($may_save);
2755 $SVN::_Core
::SVN_NO_ERROR
;
2759 my ($cred, $realm, $may_save, $pool) = @_;
2760 $may_save = undef if $_no_auth_cache;
2761 if (defined $realm && length $realm) {
2762 print STDERR
"Authentication realm: $realm\n";
2765 if (defined $_username) {
2766 $username = $_username;
2768 print STDERR
"Username: ";
2770 chomp($username = <STDIN
>);
2772 $cred->username($username);
2773 $cred->may_save($may_save);
2774 $SVN::_Core
::SVN_NO_ERROR
;
2777 sub _read_password
{
2778 my ($prompt, $realm) = @_;
2779 print STDERR
$prompt;
2781 require Term
::ReadKey
;
2782 Term
::ReadKey
::ReadMode
('noecho');
2784 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2785 last if $key =~ /[\012\015]/; # \n\r
2788 Term
::ReadKey
::ReadMode
('restore');
2794 package SVN
::Git
::Fetcher
;
2801 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2803 my ($class, $git_svn) = @_;
2804 my $self = SVN
::Delta
::Editor
->new;
2805 bless $self, $class;
2806 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
2807 $self->{empty
} = {};
2808 $self->{dir_prop
} = {};
2809 $self->{file_prop
} = {};
2810 $self->{absent_dir
} = {};
2811 $self->{absent_file
} = {};
2812 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
2816 sub set_path_strip
{
2817 my ($self, $path) = @_;
2818 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
2825 sub open_directory
{
2826 my ($self, $path, $pb, $rev) = @_;
2831 my ($self, $path) = @_;
2832 if ($self->{path_strip
}) {
2833 $path =~ s!$self->{path_strip}!! or
2834 die "Failed to strip path '$path' ($self->{path_strip})\n";
2840 my ($self, $path, $rev, $pb) = @_;
2842 my $gpath = $self->git_path($path);
2843 return undef if ($gpath eq '');
2845 # remove entire directories.
2846 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
2847 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
2849 $self->{c
}, '--', $gpath);
2853 $self->{gii
}->remove($_);
2854 print "\tD\t$_\n" unless $::_q
;
2856 print "\tD\t$gpath/\n" unless $::_q
;
2857 command_close_pipe
($ls, $ctx);
2858 $self->{empty
}->{$path} = 0
2860 $self->{gii
}->remove($gpath);
2861 print "\tD\t$gpath\n" unless $::_q
;
2867 my ($self, $path, $pb, $rev) = @_;
2868 my $gpath = $self->git_path($path);
2869 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
2870 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2871 unless (defined $mode && defined $blob) {
2872 die "$path was not found in commit $self->{c} (r$rev)\n";
2874 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
2875 pool
=> SVN
::Pool
->new, action
=> 'M' };
2879 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2880 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2881 delete $self->{empty
}->{$dir};
2882 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
2883 pool
=> SVN
::Pool
->new, action
=> 'A' };
2887 my ($self, $path, $cp_path, $cp_rev) = @_;
2888 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2889 delete $self->{empty
}->{$dir};
2890 $self->{empty
}->{$path} = 1;
2894 sub change_dir_prop
{
2895 my ($self, $db, $prop, $value) = @_;
2896 $self->{dir_prop
}->{$db->{path
}} ||= {};
2897 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
2901 sub absent_directory
{
2902 my ($self, $path, $pb) = @_;
2903 $self->{absent_dir
}->{$pb->{path
}} ||= [];
2904 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
2909 my ($self, $path, $pb) = @_;
2910 $self->{absent_file
}->{$pb->{path
}} ||= [];
2911 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
2915 sub change_file_prop
{
2916 my ($self, $fb, $prop, $value) = @_;
2917 if ($prop eq 'svn:executable') {
2918 if ($fb->{mode_b
} != 120000) {
2919 $fb->{mode_b
} = defined $value ?
100755 : 100644;
2921 } elsif ($prop eq 'svn:special') {
2922 $fb->{mode_b
} = defined $value ?
120000 : 100644;
2924 $self->{file_prop
}->{$fb->{path
}} ||= {};
2925 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
2930 sub apply_textdelta
{
2931 my ($self, $fb, $exp) = @_;
2932 my $fh = IO
::File
->new_tmpfile;
2934 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2935 # (but $base does not,) so dup() it for reading in close_file
2936 open my $dup, '<&', $fh or croak
$!;
2937 my $base = IO
::File
->new_tmpfile;
2938 $base->autoflush(1);
2940 defined (my $pid = fork) or croak
$!;
2942 open STDOUT
, '>&', $base or croak
$!;
2943 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
2944 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
2950 seek $base, 0, 0 or croak
$!;
2951 my $got = Git
::SVN
::Util
::md5sum
($base);
2952 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2954 " got: $got\n" if ($got ne $exp);
2957 seek $base, 0, 0 or croak
$!;
2959 $fb->{base
} = $base;
2960 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
2964 my ($self, $fb, $exp) = @_;
2966 my $path = $self->git_path($fb->{path
});
2967 if (my $fh = $fb->{fh
}) {
2969 seek($fh, 0, 0) or croak
$!;
2970 my $got = Git
::SVN
::Util
::md5sum
($fh);
2972 die "Checksum mismatch: $path\n",
2973 "expected: $exp\n got: $got\n";
2976 sysseek($fh, 0, 0) or croak
$!;
2977 if ($fb->{mode_b
} == 120000) {
2978 sysread($fh, my $buf, 5) == 5 or croak
$!;
2979 $buf eq 'link ' or die "$path has mode 120000",
2980 "but is not a link\n";
2982 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2984 open STDIN
, '<&', $fh or croak
$!;
2985 exec qw
/git-hash-object -w --stdin/ or croak
$!;
2987 chomp($hash = do { local $/; <$out> });
2988 close $out or croak
$!;
2989 close $fh or croak
$!;
2990 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2991 close $fb->{base
} or croak
$!;
2993 $hash = $fb->{blob
} or die "no blob information\n";
2996 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
2997 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
3003 $self->{nr
} = $self->{gii
}->{nr
};
3004 delete $self->{gii
};
3005 $self->SUPER::abort_edit
(@_);
3010 $self->{git_commit_ok
} = 1;
3011 $self->{nr
} = $self->{gii
}->{nr
};
3012 delete $self->{gii
};
3013 $self->SUPER::close_edit
(@_);
3016 package SVN
::Git
::Editor
;
3017 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
3024 my ($class, $opts) = @_;
3025 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
3026 die "$_ required!\n" unless (defined $opts->{$_});
3029 my $pool = SVN
::Pool
->new;
3030 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
3031 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
3034 # $opts->{ra} functions should not be used after this:
3035 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
3036 $opts->{editor_cb
}, $pool);
3037 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
3038 bless $self, $class;
3039 foreach (qw
/svn_path r tree_a tree_b/) {
3040 $self->{$_} = $opts->{$_};
3042 $self->{url
} = $opts->{ra
}->{url
};
3043 $self->{mods
} = $mods;
3044 $self->{types
} = $types;
3045 $self->{pool
} = $pool;
3046 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3048 $self->{path_prefix
} = length $self->{svn_path
} ?
3049 "$self->{svn_path}/" : '';
3054 my ($tree_a, $tree_b) = @_;
3055 my @diff_tree = qw(diff-tree -z -r);
3056 if ($_cp_similarity) {
3057 push @diff_tree, "-C$_cp_similarity";
3059 push @diff_tree, '-C';
3061 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3062 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
3063 push @diff_tree, $tree_a, $tree_b;
3064 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
3068 while (<$diff_fh>) {
3069 chomp $_; # this gets rid of the trailing "\0"
3070 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
3071 $::sha1\s
($::sha1
)\s
3072 ([MTCRAD
])\d
*$/xo
) {
3073 push @mods, { mode_a
=> $1, mode_b
=> $2,
3074 sha1_b
=> $3, chg
=> $4 };
3075 if ($4 =~ /^(?:C|R)$/) {
3080 } elsif ($state eq 'file_a') {
3081 my $x = $mods[$#mods] or croak
"Empty array\n";
3082 if ($x->{chg
} !~ /^(?:C|R)$/) {
3083 croak
"Error parsing $_, $x->{chg}\n";
3087 } elsif ($state eq 'file_b') {
3088 my $x = $mods[$#mods] or croak
"Empty array\n";
3089 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
3090 croak
"Error parsing $_, $x->{chg}\n";
3092 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
3093 croak
"Error parsing $_, $x->{chg}\n";
3098 croak
"Error parsing $_\n";
3101 command_close_pipe
($diff_fh, $ctx);
3105 sub check_diff_paths
{
3106 my ($ra, $pfx, $rev, $mods) = @_;
3108 $pfx .= '/' if length $pfx;
3110 sub type_diff_paths
{
3111 my ($ra, $types, $path, $rev) = @_;
3112 my @p = split m
#/+#, $path;
3114 unless (defined $types->{$c}) {
3115 $types->{$c} = $ra->check_path($c, $rev);
3118 $c .= '/' . shift @p;
3119 next if defined $types->{$c};
3120 $types->{$c} = $ra->check_path($c, $rev);
3124 foreach my $m (@
$mods) {
3125 foreach my $f (qw
/file_a file_b/) {
3126 next unless defined $m->{$f};
3127 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
3128 if (length $pfx.$dir && ! defined $types{$dir}) {
3129 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
3137 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3141 my ($self, $path) = @_;
3142 $self->{path_prefix
}.(defined $path ?
$path : '');
3146 my ($self, $path) = @_;
3147 if ($self->{url
} =~ m
#^https?://#) {
3148 $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3150 $self->{url
} . '/' . $self->repo_path($path);
3155 my $rm = $self->{rm
};
3156 delete $rm->{''}; # we never delete the url we're tracking
3159 foreach (keys %$rm) {
3160 my @d = split m
#/#, $_;
3164 $c .= '/' . shift @d;
3168 delete $rm->{$self->{svn_path
}};
3169 delete $rm->{''}; # we never delete the url we're tracking
3172 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
3177 my @dn = split m
#/#, $_;
3179 delete $rm->{join '/', @dn};
3186 command_close_pipe
($fh, $ctx);
3188 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3189 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3190 $self->close_directory($bat->{$d}, $p);
3191 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3192 print "\tD+\t$d/\n" unless $::_q
;
3193 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3198 sub open_or_add_dir
{
3199 my ($self, $full_path, $baton) = @_;
3200 my $t = $self->{types
}->{$full_path};
3202 die "$full_path not known in r$self->{r} or we have a bug!\n";
3206 # SVN::Node::none and SVN::Node::file are used only once,
3207 # so we're shutting up Perl's warnings about them.
3208 if ($t == $SVN::Node
::none
) {
3209 return $self->add_directory($full_path, $baton,
3210 undef, -1, $self->{pool
});
3211 } elsif ($t == $SVN::Node
::dir
) {
3212 return $self->open_directory($full_path, $baton,
3213 $self->{r
}, $self->{pool
});
3214 } # no warnings 'once'
3215 print STDERR
"$full_path already exists in repository at ",
3216 "r$self->{r} and it is not a directory (",
3217 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3218 } # no warnings 'once'
3223 my ($self, $path) = @_;
3224 my $bat = $self->{bat
};
3225 my $repo_path = $self->repo_path($path);
3226 return $bat->{''} unless (length $repo_path);
3227 my @p = split m
#/+#, $repo_path;
3229 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3232 $c .= '/' . shift @p;
3233 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3239 my ($self, $m) = @_;
3240 my ($dir, $file) = split_path
($m->{file_b
});
3241 my $pbat = $self->ensure_path($dir);
3242 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3244 print "\tA\t$m->{file_b}\n" unless $::_q
;
3245 $self->chg_file($fbat, $m);
3246 $self->close_file($fbat,undef,$self->{pool
});
3250 my ($self, $m) = @_;
3251 my ($dir, $file) = split_path
($m->{file_b
});
3252 my $pbat = $self->ensure_path($dir);
3253 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3254 $self->url_path($m->{file_a
}), $self->{r
});
3255 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3256 $self->chg_file($fbat, $m);
3257 $self->close_file($fbat,undef,$self->{pool
});
3261 my ($self, $path, $pbat) = @_;
3262 my $rpath = $self->repo_path($path);
3263 my ($dir, $file) = split_path
($rpath);
3264 $self->{rm
}->{$dir} = 1;
3265 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3269 my ($self, $m) = @_;
3270 my ($dir, $file) = split_path
($m->{file_b
});
3271 my $pbat = $self->ensure_path($dir);
3272 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3273 $self->url_path($m->{file_a
}), $self->{r
});
3274 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3275 $self->chg_file($fbat, $m);
3276 $self->close_file($fbat,undef,$self->{pool
});
3278 ($dir, $file) = split_path
($m->{file_a
});
3279 $pbat = $self->ensure_path($dir);
3280 $self->delete_entry($m->{file_a
}, $pbat);
3284 my ($self, $m) = @_;
3285 my ($dir, $file) = split_path
($m->{file_b
});
3286 my $pbat = $self->ensure_path($dir);
3287 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3288 $pbat,$self->{r
},$self->{pool
});
3289 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
3290 $self->chg_file($fbat, $m);
3291 $self->close_file($fbat,undef,$self->{pool
});
3294 sub T
{ shift->M(@_) }
3296 sub change_file_prop
{
3297 my ($self, $fbat, $pname, $pval) = @_;
3298 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3302 my ($self, $fbat, $m) = @_;
3303 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3304 $self->change_file_prop($fbat,'svn:executable','*');
3305 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3306 $self->change_file_prop($fbat,'svn:executable',undef);
3308 my $fh = IO
::File
->new_tmpfile or croak
$!;
3309 if ($m->{mode_b
} =~ /^120/) {
3310 print $fh 'link ' or croak
$!;
3311 $self->change_file_prop($fbat,'svn:special','*');
3312 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3313 $self->change_file_prop($fbat,'svn:special',undef);
3315 defined(my $pid = fork) or croak
$!;
3317 open STDOUT
, '>&', $fh or croak
$!;
3318 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3322 $fh->flush == 0 or croak
$!;
3323 seek $fh, 0, 0 or croak
$!;
3325 my $exp = Git
::SVN
::Util
::md5sum
($fh);
3326 seek $fh, 0, 0 or croak
$!;
3328 my $pool = SVN
::Pool
->new;
3329 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3330 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
3331 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3334 close $fh or croak
$!;
3338 my ($self, $m) = @_;
3339 my ($dir, $file) = split_path
($m->{file_b
});
3340 my $pbat = $self->ensure_path($dir);
3341 print "\tD\t$m->{file_b}\n" unless $::_q
;
3342 $self->delete_entry($m->{file_b
}, $pbat);
3347 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3348 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3350 $self->close_directory($bat->{$_}, $p);
3352 $self->close_directory($bat->{''}, $p);
3353 $self->SUPER::close_edit
($p);
3359 $self->SUPER::abort_edit
($self->{pool
});
3364 $self->SUPER::DESTROY
(@_);
3365 $self->{pool
}->clear;
3368 # this drives the editor
3371 my $mods = $self->{mods
};
3372 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
3373 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
3375 if (defined $o{$f}) {
3378 fatal
("Invalid change type: $f");
3381 $self->rmdirs if $_rmdir;
3387 return scalar @
$mods;
3390 package Git
::SVN
::Ra
;
3391 use vars qw
/@ISA $config_dir $_log_window_size/;
3394 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
3397 # enforce temporary pool usage for some simple functions
3399 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3400 my $SUPER = "SUPER::$f";
3403 my $pool = SVN
::Pool
->new;
3404 my @ret = $self->$SUPER(@_,$pool);
3406 wantarray ?
@ret : $ret[0];
3411 sub _auth_providers
() {
3413 SVN
::Client
::get_simple_provider
(),
3414 SVN
::Client
::get_ssl_server_trust_file_provider
(),
3415 SVN
::Client
::get_simple_prompt_provider
(
3416 \
&Git
::SVN
::Prompt
::simple
, 2),
3417 SVN
::Client
::get_ssl_client_cert_file_provider
(),
3418 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
3419 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
3420 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
3421 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
3422 SVN
::Client
::get_username_provider
(),
3423 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
3424 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
3425 SVN
::Client
::get_username_prompt_provider
(
3426 \
&Git
::SVN
::Prompt
::username
, 2)
3430 sub escape_uri_only
{
3433 foreach (split m{/}, $uri) {
3434 s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
3442 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
3443 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
3444 $url = "$scheme://$domain$uri";
3450 my ($class, $url) = @_;
3452 return $RA if ($RA && $RA->{url
} eq $url);
3454 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
3455 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
3456 my $config = SVN
::Core
::config_get_config
($config_dir);
3458 my $dont_store_passwords = 1;
3459 my $conf_t = ${$config}{'config'};
3462 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3463 # produces warnings that variables are used only once.
3464 # I had not found the better way to shut them up, so
3465 # the warnings of type 'once' are disabled in this block.
3466 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
3467 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
3468 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
3470 SVN
::_Core
::svn_auth_set_parameter
($baton,
3471 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
3472 bless (\
$dont_store_passwords, "_p_void"));
3474 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
3475 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
3476 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
3478 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
3480 } # no warnings 'once'
3481 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
3483 pool
=> SVN
::Pool
->new,
3484 auth_provider_callbacks
=> $callbacks);
3485 $self->{url
} = $url;
3486 $self->{svn_path
} = $url;
3487 $self->{repos_root
} = $self->get_repos_root;
3488 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
3489 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
3490 get_dir
=> { r
=> 0, data
=> {} } };
3491 $RA = bless $self, $class;
3495 my ($self, $path, $r) = @_;
3496 my $cache = $self->{cache
}->{check_path
};
3497 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
3498 return $cache->{data
}->{$path};
3500 my $pool = SVN
::Pool
->new;
3501 my $t = $self->SUPER::check_path
($path, $r, $pool);
3503 if ($r != $cache->{r
}) {
3504 %{$cache->{data
}} = ();
3507 $cache->{data
}->{$path} = $t;
3511 my ($self, $dir, $r) = @_;
3512 my $cache = $self->{cache
}->{get_dir
};
3513 if ($r == $cache->{r
}) {
3514 if (my $x = $cache->{data
}->{$dir}) {
3515 return wantarray ? @
$x : $x->[0];
3518 my $pool = SVN
::Pool
->new;
3519 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
3520 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
3522 if ($r != $cache->{r
}) {
3523 %{$cache->{data
}} = ();
3526 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
3527 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
3531 # do not call the real DESTROY since we store ourselves in $RA
3535 my ($self, @args) = @_;
3536 my $pool = SVN
::Pool
->new;
3537 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
3538 my $ret = $self->SUPER::get_log
(@args, $pool);
3544 my ($self, $url1, $rev1, $url2, $rev2) = @_;
3545 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
3546 my $out = IO
::File
->new_tmpfile;
3548 # older SVN (1.1.x) doesn't take $pool as the last parameter for
3549 # $ctx->diff(), so we'll create a default one
3550 my $pool = SVN
::Pool
->new_default_sub;
3552 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
3553 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
3555 my $ret = (($out->stat)[7] == 0);
3556 close $out or croak
$!;
3561 sub get_commit_editor
{
3562 my ($self, $log, $cb, $pool) = @_;
3563 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
3564 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
3568 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
3569 my $new = ($rev_a == $rev_b);
3570 my $path = $gs->{path
};
3572 if ($new && -e
$gs->{index}) {
3573 unlink $gs->{index} or die
3574 "Couldn't unlink index: $gs->{index}: $!\n";
3576 my $pool = SVN
::Pool
->new;
3577 $editor->set_path_strip($path);
3578 my (@pc) = split m
#/#, $path;
3579 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
3581 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3583 # Since we can't rely on svn_ra_reparent being available, we'll
3584 # just have to do some magic with set_path to make it so
3585 # we only want a partial path.
3587 my $final = join('/', @pc);
3589 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
3590 $sp .= '/' if length $sp;
3593 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
3595 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
3597 $reporter->finish_report($pool);
3599 $editor->{git_commit_ok
};
3602 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
3603 # svn_ra_reparent didn't work before 1.4)
3605 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
3606 my $path = $gs->{path
};
3607 my $pool = SVN
::Pool
->new;
3609 my $full_url = $self->{url
};
3610 my $old_url = $full_url;
3611 $full_url .= '/' . escape_uri_only
($path) if length $path;
3612 my ($ra, $reparented);
3613 if ($old_url ne $full_url) {
3614 if ($old_url !~ m
#^svn(\+ssh)?://#) {
3615 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
3617 $self->{url
} = $full_url;
3623 $ra = Git
::SVN
::Ra
->new($full_url);
3628 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
3629 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3630 $reporter->set_path('', $rev_a, 0, @lock, $pool);
3631 $reporter->finish_report($pool);
3634 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
3635 $self->{url
} = $old_url;
3639 $editor->{git_commit_ok
};
3642 sub longest_common_path
{
3643 my ($gsv, $globs) = @_;
3645 my $common_max = scalar @
$gsv;
3647 foreach my $gs (@
$gsv) {
3648 my @tmp = split m
#/#, $gs->{path};
3651 $p .= length($p) ?
"/$_" : $_;
3657 $common_max += scalar @
$globs;
3658 foreach my $glob (@
$globs) {
3659 my @tmp = split m
#/#, $glob->{path}->{left};
3662 $p .= length($p) ?
"/$_" : $_;
3668 my $longest_path = '';
3669 foreach (sort {length $b <=> length $a} keys %common) {
3670 if ($common{$_} == $common_max) {
3678 sub gs_fetch_loop_common
{
3679 my ($self, $base, $head, $gsv, $globs) = @_;
3680 return if ($base > $head);
3681 my $inc = $_log_window_size;
3682 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
3683 my $longest_path = longest_common_path
($gsv, $globs);
3684 my $ra_url = $self->{url
};
3688 my $err_handler = $SVN::Error
::handler
;
3689 $SVN::Error
::handler
= sub {
3691 skip_unknown_revs
($err);
3694 my ($paths, $r, $author, $date, $log) = @_;
3695 [ dup_changed_paths
($paths),
3696 { author
=> $author, date
=> $date, log => $log } ];
3698 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3699 sub { $revs{$_[1]} = _cb
(@_) });
3700 if ($err && $max >= $head) {
3701 print STDERR
"Path '$longest_path' ",
3702 "was probably deleted:\n",
3703 $err->expanded_message,
3704 "\nWill attempt to follow ",
3705 "revisions r$min .. r$max ",
3706 "committed before the deletion\n";
3708 while (--$hi >= $min) {
3710 $self->get_log([$longest_path], $min, $hi,
3713 $revs{$_[1]} = _cb
(@_) });
3715 print STDERR
"r$min .. r$ok OK\n";
3720 $SVN::Error
::handler
= $err_handler;
3722 my %exists = map { $_->{path
} => $_ } @
$gsv;
3723 foreach my $r (sort {$a <=> $b} keys %revs) {
3724 my ($paths, $logged) = @
{$revs{$r}};
3726 foreach my $gs ($self->match_globs(\
%exists, $paths,
3728 if ($gs->rev_db_max >= $r) {
3731 next unless $gs->match_paths($paths, $r);
3732 $gs->{logged_rev_props
} = $logged;
3733 if (my $last_commit = $gs->last_commit) {
3734 $gs->assert_index_clean($last_commit);
3736 my $log_entry = $gs->do_fetch($paths, $r);
3738 $gs->do_git_commit($log_entry);
3741 foreach my $g (@
$globs) {
3742 my $k = "svn-remote.$g->{remote}." .
3744 Git
::SVN
::tmp_config
($k, $r);
3750 $self = Git
::SVN
::Ra
->new($ra_url);
3751 $ra_invalid = undef;
3754 # pre-fill the .rev_db since it'll eventually get filled in
3755 # with '0' x40 if something new gets committed
3756 foreach my $gs (@
$gsv) {
3757 next if defined $gs->rev_db_get($max);
3758 $gs->rev_db_set($max, 0 x40
);
3760 foreach my $g (@
$globs) {
3761 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3762 Git
::SVN
::tmp_config
($k, $max);
3764 last if $max >= $head;
3767 $max = $head if ($max > $head);
3772 my ($self, $exists, $paths, $globs, $r) = @_;
3775 my ($self, $exists, $g, $r) = @_;
3776 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
3777 return unless scalar @x == 3;
3778 my $dirents = $x[0];
3779 foreach my $de (keys %$dirents) {
3780 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
3781 my $p = $g->{path
}->full_path($de);
3782 next if $exists->{$p};
3783 next if (length $g->{path
}->{right
} &&
3784 ($self->check_path($p, $r) !=
3786 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
3787 $g->{ref}->full_path($de), 1);
3790 foreach my $g (@
$globs) {
3791 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3792 if ($path->{action
} =~ /^[AR]$/) {
3793 get_dir_check
($self, $exists, $g, $r);
3796 foreach (keys %$paths) {
3797 if (/$g->{path}->{left_regex}/ &&
3798 !/$g->{path}->{regex}/) {
3799 next if $paths->{$_}->{action
} !~ /^[AR]$/;
3800 get_dir_check
($self, $exists, $g, $r);
3802 next unless /$g->{path}->{regex}/;
3804 my $pathname = $g->{path
}->full_path($p);
3805 next if $exists->{$pathname};
3806 next if ($self->check_path($pathname, $r) !=
3808 $exists->{$pathname} = Git
::SVN
->init(
3809 $self->{url
}, $pathname, undef,
3810 $g->{ref}->full_path($p), 1);
3813 foreach (split m
#/#, $g->{path}->{left}) {
3815 next unless ($paths->{$c} &&
3816 ($paths->{$c}->{action
} =~ /^[AR]$/));
3817 get_dir_check
($self, $exists, $g, $r);
3825 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
3826 my $url = $self->{repos_root
};
3827 my @components = split(m!/!, $self->{svn_path
});
3830 $url .= "/$c" if length $c;
3831 eval { (ref $self)->new($url)->get_latest_revnum };
3832 } while ($@
&& ($c = shift @components));
3838 unless (defined $can_do_switch) {
3839 my $pool = SVN
::Pool
->new;
3841 $self->do_switch(1, '', 0, $self->{url
},
3842 SVN
::Delta
::Editor
->new, $pool);
3847 $rep->abort_report($pool);
3855 sub skip_unknown_revs
{
3857 my $errno = $err->apr_err();
3858 # Maybe the branch we're tracking didn't
3859 # exist when the repo started, so it's
3860 # not an error if it doesn't, just continue
3862 # Wonderfully consistent library, eh?
3863 # 160013 - svn:// and file://
3864 # 175002 - http(s)://
3865 # 175007 - http(s):// (this repo required authorization, too...)
3866 # More codes may be discovered later...
3867 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3868 my $err_key = $err->expanded_message;
3869 # revision numbers change every time, filter them out
3870 $err_key =~ s/\d+/\0/g;
3871 $err_key = "$errno\0$err_key";
3872 unless ($ignored_err{$err_key}) {
3873 warn "W: Ignoring error from SVN, path probably ",
3874 "does not exist: ($errno): ",
3875 $err->expanded_message,"\n";
3876 $ignored_err{$err_key} = 1;
3880 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3883 # svn_log_changed_path_t objects passed to get_log are likely to be
3884 # overwritten even if only the refs are copied to an external variable,
3885 # so we should dup the structures in their entirety. Using an externally
3886 # passed pool (instead of our temporary and quickly cleared pool in
3887 # Git::SVN::Ra) does not help matters at all...
3888 sub dup_changed_paths
{
3890 return undef unless $paths;
3892 foreach my $p (keys %$paths) {
3893 my $i = $paths->{$p};
3894 my %s = map { $_ => $i->$_ }
3895 qw
/copyfrom_path copyfrom_rev action/;
3901 package Git
::SVN
::Log
;
3904 use POSIX qw
/strftime/;
3905 use constant commit_log_separator
=> ('-' x
72) . "\n";
3906 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
3907 %rusers $show_commit $incremental/;
3912 return 1 if defined $c->{r
};
3914 # big commit message got truncated by the 16k pretty buffer in rev-list
3915 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
3916 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
3918 my @log = command
(qw
/cat-file commit/, $c->{c
});
3920 # shift off the headers
3921 shift @log while ($log[0] ne '');
3924 # TODO: make $c->{l} not have a trailing newline in the future
3925 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
3927 (undef, $c->{r
}, undef) = ::extract_metadata
(
3928 (grep(/^git-svn-id: /, @log))[-1]);
3930 return defined $c->{r
};
3936 $dcvar = 'color.diff';
3937 $dc = `git-config --get $dcvar`;
3939 # nothing at all; fallback to "diff.color"
3940 $dcvar = 'diff.color';
3941 $dc = `git-config --get $dcvar`;
3944 if ($dc eq 'auto') {
3946 $pc = `git-config --get color.pager`;
3948 # does not have it -- fallback to pager.color
3949 $pc = `git-config --bool --get pager.color`;
3952 $pc = `git-config --bool --get color.pager`;
3958 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
3959 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
3963 return 0 if $dc eq 'never';
3964 return 1 if $dc eq 'always';
3965 chomp($dc = `git-config --bool --get $dcvar`);
3966 return ($dc eq 'true');
3969 sub git_svn_log_cmd
{
3970 my ($r_min, $r_max, @args) = @_;
3972 my (@files, @log_opts);
3973 foreach my $x (@args) {
3974 if ($x eq '--' || @files) {
3977 if (::verify_ref
("$x^0")) {
3985 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
3986 $gs ||= Git
::SVN
->_new;
3987 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
3989 push @cmd, '-r' unless $non_recursive;
3990 push @cmd, qw
/--raw --name-status/ if $verbose;
3991 push @cmd, '--color' if log_use_color
();
3992 push @cmd, @log_opts;
3993 if (defined $r_max && $r_max == $r_min) {
3994 push @cmd, '--max-count=1';
3995 if (my $c = $gs->rev_db_get($r_max)) {
3998 } elsif (defined $r_max) {
3999 if ($r_max < $r_min) {
4000 ($r_min, $r_max) = ($r_max, $r_min);
4002 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4003 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4004 # If there are no commits in the range, both $c_max and $c_min
4005 # will be undefined. If there is at least 1 commit in the
4006 # range, both will be defined.
4007 return () if !defined $c_min || !defined $c_max;
4008 if ($c_min eq $c_max) {
4009 push @cmd, '--max-count=1', $c_min;
4011 push @cmd, '--boundary', "$c_min..$c_max";
4014 return (@cmd, @files);
4017 # adapted from pager.c
4019 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
4020 if (!defined $pager) {
4022 } elsif (length $pager == 0 || $pager eq 'cat') {
4028 return unless -t
*STDOUT
&& defined $pager;
4029 pipe my $rfd, my $wfd or return;
4030 defined(my $pid = fork) or ::fatal
"Can't fork: $!";
4032 open STDOUT
, '>&', $wfd or
4033 ::fatal
"Can't redirect to stdout: $!";
4036 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!";
4037 $ENV{LESS
} ||= 'FRSX';
4038 exec $pager or ::fatal
"Can't run pager: $! ($pager)";
4041 sub format_svn_date
{
4042 return strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4045 sub parse_git_date
{
4047 # Date::Parse isn't in the standard Perl distro :(
4048 if ($tz =~ s/^\+//) {
4049 $t += tz_to_s_offset
($tz);
4050 } elsif ($tz =~ s/^\-//) {
4051 $t -= tz_to_s_offset
($tz);
4056 sub set_local_timezone
{
4064 sub tz_to_s_offset
{
4067 return ($1 * 60) + ($tz * 3600);
4070 sub get_author_info
{
4071 my ($dest, $author, $t, $tz) = @_;
4072 $author =~ s/(?:^\s*|\s*$)//g;
4073 $dest->{a_raw
} = $author;
4076 $au = $rusers{$author} || undef;
4079 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4084 $dest->{t_utc
} = parse_git_date
($t, $tz);
4087 sub process_commit
{
4088 my ($c, $r_min, $r_max, $defer) = @_;
4089 if (defined $r_min && defined $r_max) {
4090 if ($r_min == $c->{r
} && $r_min == $r_max) {
4094 return 1 if $r_min == $r_max;
4095 if ($r_min < $r_max) {
4096 # we need to reverse the print order
4097 return 0 if (defined $limit && --$limit < 0);
4101 if ($r_min != $r_max) {
4102 return 1 if ($r_min < $c->{r
});
4103 return 1 if ($r_max > $c->{r
});
4106 return 0 if (defined $limit && --$limit < 0);
4115 if (my $l = $c->{l
}) {
4116 while ($l->[0] =~ /^\s*$/) { shift @
$l }
4119 $l_fmt ||= 'A' . length($c->{r
});
4120 print 'r',pack($l_fmt, $c->{r
}),' | ';
4121 print "$c->{c} | " if $show_commit;
4124 show_commit_normal
($c);
4128 sub show_commit_changed_paths
{
4130 return unless $c->{changed
};
4131 print "Changed paths:\n", @
{$c->{changed
}};
4134 sub show_commit_normal
{
4136 print commit_log_separator
, "r$c->{r} | ";
4137 print "$c->{c} | " if $show_commit;
4138 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
4141 if (my $l = $c->{l
}) {
4142 while ($l->[$#$l] eq "\n" && $#$l > 0
4143 && $l->[($#$l - 1)] eq "\n") {
4146 $nr_line = scalar @
$l;
4148 print "1 line\n\n\n";
4150 if ($nr_line == 1) {
4151 $nr_line = '1 line';
4153 $nr_line .= ' lines';
4155 print $nr_line, "\n";
4156 show_commit_changed_paths
($c);
4158 print $_ foreach @
$l;
4162 show_commit_changed_paths
($c);
4166 foreach my $x (qw
/raw stat diff/) {
4169 print $_ foreach @
{$c->{$x}}
4176 my ($r_min, $r_max);
4177 my $r_last = -1; # prevent dupes
4178 set_local_timezone
();
4179 if (defined $::_revision
) {
4180 if ($::_revision
=~ /^(\d+):(\d+)$/) {
4181 ($r_min, $r_max) = ($1, $2);
4182 } elsif ($::_revision
=~ /^\d+$/) {
4183 $r_min = $r_max = $::_revision
;
4185 ::fatal
"-r$::_revision is not supported, use ",
4186 "standard 'git log' arguments instead";
4191 @args = git_svn_log_cmd
($r_min, $r_max, @args);
4193 print commit_log_separator
unless $incremental || $oneline;
4196 my $log = command_output_pipe
(@args);
4198 my (@k, $c, $d, $stat);
4199 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4201 if (/^${esc_color}commit -?($::sha1_short)/o) {
4203 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
4205 process_commit
($c, $r_min, $r_max, \
@k) or
4210 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4211 get_author_info
($c, $1, $2, $3);
4212 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4214 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4215 push @
{$c->{raw
}}, $_;
4216 } elsif (/^${esc_color}[ACRMDT]\t/) {
4217 # we could add $SVN->{svn_path} here, but that requires
4218 # remote access at the moment (repo_path_split)...
4219 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4220 push @
{$c->{changed
}}, $_;
4221 } elsif (/^${esc_color}diff /o) {
4223 push @
{$c->{diff
}}, $_;
4225 push @
{$c->{diff
}}, $_;
4226 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
4227 $esc_color*[\
+\
-]*$esc_color$/x
) {
4229 push @
{$c->{stat}}, $_;
4230 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4231 push @
{$c->{stat}}, $_;
4233 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4234 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
4235 } elsif (s/^${esc_color} //o) {
4236 push @
{$c->{l
}}, $_;
4239 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
4241 process_commit
($c, $r_min, $r_max, \
@k);
4244 ($r_min, $r_max) = ($r_max, $r_min);
4245 process_commit
($_, $r_min, $r_max) foreach reverse @k;
4249 print commit_log_separator
unless $incremental || $oneline;
4252 package Git
::SVN
::Migration
;
4253 # these version numbers do NOT correspond to actual version numbers
4254 # of git nor git-svn. They are just relative.
4256 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4258 # v1 layout: .git/$id/info/url, refs/remotes/$id
4260 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4262 # v3 layout: .git/svn/$id, refs/remotes/$id
4263 # - info/url may remain for backwards compatibility
4264 # - this is what we migrate up to this layout automatically,
4265 # - this will be used by git svn init on single branches
4266 # v3.1 layout (auto migrated):
4267 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4268 # for backwards compatibility
4270 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4271 # - this is only created for newly multi-init-ed
4272 # repositories. Similar in spirit to the
4273 # --use-separate-remotes option in git-clone (now default)
4274 # - we do not automatically migrate to this (following
4275 # the example set by core git)
4279 use File
::Path qw
/mkpath/;
4280 use File
::Basename qw
/dirname basename/;
4281 use vars qw
/$_minimize/;
4283 sub migrate_from_v0
{
4284 my $git_dir = $ENV{GIT_DIR
};
4285 return undef unless -d
$git_dir;
4286 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
4290 my ($id, $orig_ref) = ($_, $_);
4291 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
4292 next unless -f
"$git_dir/$id/info/url";
4293 my $new_ref = "refs/remotes/$id";
4294 if (::verify_ref
("$new_ref^0")) {
4295 print STDERR
"W: $orig_ref is probably an old ",
4296 "branch used by an ancient version of ",
4298 "However, $new_ref also exists.\n",
4299 "We will not be able ",
4300 "to use this branch until this ",
4301 "ambiguity is resolved.\n";
4304 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
4305 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
4306 command_noisy
('update-ref', $new_ref, $orig_ref);
4307 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
4310 command_close_pipe
($fh, $ctx);
4311 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
4315 sub migrate_from_v1
{
4316 my $git_dir = $ENV{GIT_DIR
};
4318 return $migrated unless -d
$git_dir;
4319 my $svn_dir = "$git_dir/svn";
4321 # just in case somebody used 'svn' as their $id at some point...
4322 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
4324 print STDERR
"Migrating from a git-svn v1 layout...\n";
4326 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
4327 "$svn_dir\n\t(required for this version ",
4328 "($::VERSION) of git-svn) does not. exist\n";
4329 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
4332 next unless $x =~ s
#^refs/remotes/##;
4334 next unless -f
"$git_dir/$x/info/url";
4335 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
4337 my $dn = dirname
("$git_dir/svn/$x");
4338 mkpath
([$dn]) unless -d
$dn;
4339 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4340 mkpath
(["$git_dir/svn/svn"]);
4341 print STDERR
" - $git_dir/$x/info => ",
4342 "$git_dir/svn/$x/info\n";
4343 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4345 # don't worry too much about these, they probably
4346 # don't exist with repos this old (save for index,
4347 # and we can easily regenerate that)
4348 foreach my $f (qw
/unhandled.log index .rev_db/) {
4349 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4352 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
4353 rename "$git_dir/$x", "$git_dir/svn/$x" or
4358 command_close_pipe
($fh, $ctx);
4359 print STDERR
"Done migrating from a git-svn v1 layout\n";
4364 my ($l_map, $pfx, $path) = @_;
4366 foreach (<$path/*>) {
4367 if (-r
"$_/info/url") {
4368 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
4369 my $ref_id = $pfx . basename
$_;
4370 my $url = ::file_to_s
("$_/info/url");
4371 $l_map->{$ref_id} = $url;
4378 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4379 read_old_urls
($l_map, $x, $_);
4383 sub migrate_from_v2
{
4384 my @cfg = command
(qw
/config -l/);
4385 return if grep /^svn-remote\..+\.url=/, @cfg;
4387 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
4390 foreach my $ref_id (sort keys %l_map) {
4391 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
4393 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4400 sub minimize_connections
{
4401 my $r = Git
::SVN
::read_all_remotes
();
4403 my $root_repos = {};
4404 foreach my $repo_id (keys %$r) {
4405 my $url = $r->{$repo_id}->{url
} or next;
4406 my $fetch = $r->{$repo_id}->{fetch
} or next;
4407 my $ra = Git
::SVN
::Ra
->new($url);
4409 # skip existing cases where we already connect to the root
4410 if (($ra->{url
} eq $ra->{repos_root
}) ||
4411 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
4413 $root_repos->{$ra->{url
}} = $repo_id;
4417 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
4418 my $root_path = $ra->{url
};
4419 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
4420 foreach my $path (keys %$fetch) {
4421 my $ref_id = $fetch->{$path};
4422 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
4424 # make sure we can read when connecting to
4425 # a higher level of a repository
4426 my ($last_rev, undef) = $gs->last_rev_commit;
4427 if (!defined $last_rev) {
4429 $root_ra->get_latest_revnum;
4433 my $new = $root_path;
4434 $new .= length $path ?
"/$path" : '';
4436 $root_ra->get_log([$new], $last_rev, $last_rev,
4440 $new_urls->{$ra->{repos_root
}}->{$new} =
4441 { ref_id
=> $ref_id,
4442 old_repo_id
=> $repo_id,
4443 old_path
=> $path };
4448 foreach my $url (keys %$new_urls) {
4449 # see if we can re-use an existing [svn-remote "repo_id"]
4450 # instead of creating a(n ugly) new section:
4451 my $repo_id = $root_repos->{$url} ||
4452 Git
::SVN
::sanitize_remote_name
($url);
4454 my $fetch = $new_urls->{$url};
4455 foreach my $path (keys %$fetch) {
4456 my $x = $fetch->{$path};
4457 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
4458 my $pfx = "svn-remote.$x->{old_repo_id}";
4460 my $old_fetch = quotemeta("$x->{old_path}:".
4461 "refs/remotes/$x->{ref_id}");
4462 command_noisy
(qw
/config --unset/,
4463 "$pfx.fetch", '^'. $old_fetch . '$');
4464 delete $r->{$x->{old_repo_id
}}->
4465 {fetch
}->{$x->{old_path
}};
4466 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
4467 command_noisy
(qw
/config --unset/,
4469 push @emptied, $x->{old_repo_id
}
4474 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
4475 "$ENV{GIT_DIR}/config";
4477 The following [svn-remote] sections in your config file ($file) are empty
4478 and can be safely removed:
4480 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
4484 sub migration_check
{
4488 minimize_connections
() if $_minimize;
4491 package Git
::IndexInfo
;
4494 use Git qw
/command_input_pipe command_close_pipe/;
4498 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
4499 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
4503 my ($self, $path) = @_;
4504 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
4505 return ++$self->{nr
};
4511 my ($self, $mode, $hash, $path) = @_;
4512 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
4513 return ++$self->{nr
};
4520 command_close_pipe
($self->{gui
}, $self->{ctx
});
4523 package Git
::SVN
::GlobSpec
;
4528 my ($class, $glob) = @_;
4530 $re =~ s!/+$!!g; # no need for trailing slashes
4531 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
4532 my ($left, $right) = ($1, $2);
4534 die "Only one '*' wildcard expansion ",
4535 "is supported (got $nr): '$glob'\n";
4536 } elsif ($nr == 0) {
4537 die "One '*' is needed for glob: '$glob'\n";
4539 $re = quotemeta($left) . $re . quotemeta($right);
4540 if (length $left && !($left =~ s!/+$!!g)) {
4541 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
4543 if (length $right && !($right =~ s!^/+!!g)) {
4544 die "Missing leading '/' on right side of: '$glob' ($right)\n";
4546 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
4547 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
4548 regex
=> qr/$re/, glob => $glob }, $class;
4552 my ($self, $path) = @_;
4553 return (length $self->{left
} ?
"$self->{left}/" : '') .
4554 $path . (length $self->{right
} ?
"/$self->{right}" : '');
4562 $remotes = { # returned by read_all_remotes()
4564 # svn-remote.svn.url=https://svn.musicpd.org
4565 url
=> 'https://svn.musicpd.org',
4566 # svn-remote.svn.fetch=mpd/trunk:trunk
4568 'mpd/trunk' => 'trunk',
4570 # svn-remote.svn.tags=mpd/tags/*:tags/*
4575 regex
=> qr!mpd/tags/([^/]+)$!,
4581 regex
=> qr!tags/([^/]+)$!,
4588 $log_entry hashref as returned by libsvn_log_entry
()
4590 log => 'whitespace-formatted log entry
4591 ', # trailing newline is preserved
4592 revision
=> '8', # integer
4593 date
=> '2004-02-24T17:01:44.108345Z', # commit date
4594 author
=> 'committer name'
4598 # this is generated by generate_diff();
4599 @mods = array of diff
-index line hashes
, each element represents one line
4600 of diff
-index output
4602 diff
-index line
($m hash
)
4604 mode_a
=> first column of diff
-index output
, no leading
':',
4605 mode_b
=> second column of diff
-index output
,
4606 sha1_b
=> sha1sum of the final blob
,
4607 chg
=> change type
[MCRADT
],
4608 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
4609 file_b
=> new
/current file name of a file
(any chg
)
4613 # retval of read_url_paths{,_all}();
4615 # repository root url
4616 'https://svn.musicpd.org' => {
4617 # repository path # GIT_SVN_ID
4618 'mpd/trunk' => 'trunk',
4619 'mpd/tags/0.11.5' => 'tags/0.11.5',
4624 I don
't trust the each() function on unless I created %hash myself
4625 because the internal iterator may not have started at base.