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 $_repository
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';
40 use File
::Basename qw
/dirname basename/;
41 use File
::Path qw
/mkpath/;
42 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
47 # import functions from Git into our packages, en masse
49 foreach (qw
/command command_oneline command_noisy command_output_pipe
50 command_input_pipe command_close_pipe
/) {
51 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
52 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_format, $_commit_url, $_tag);
70 $Git::SVN
::_follow_parent
= 1;
71 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
72 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
73 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
74 'ignore-paths=s' => \
$SVN::Git
::Fetcher
::_ignore_regex
);
75 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
76 'authors-file|A=s' => \
$_authors,
77 'repack:i' => \
$Git::SVN
::_repack
,
78 'noMetadata' => \
$Git::SVN
::_no_metadata
,
79 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
80 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
81 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
82 'no-checkout' => \
$_no_checkout,
84 'repack-flags|repack-args|repack-opts=s' =>
85 \
$Git::SVN
::_repack_flags
,
86 'use-log-author' => \
$Git::SVN
::_use_log_author
,
87 'add-author-from' => \
$Git::SVN
::_add_author_from
,
88 'localtime' => \
$Git::SVN
::_localtime
,
91 my ($_trunk, $_tags, $_branches, $_stdlayout);
93 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
94 'trunk|T=s' => \
$_trunk, 'tags|t=s' => \
$_tags,
95 'branches|b=s' => \
$_branches, 'prefix=s' => \
$_prefix,
96 'stdlayout|s' => \
$_stdlayout,
97 'minimize-url|m' => \
$Git::SVN
::_minimize_url
,
98 'no-metadata' => sub { $icv{noMetadata
} = 1 },
99 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
100 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
101 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
103 my %cmt_opts = ( 'edit|e' => \
$_edit,
104 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
105 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
106 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
107 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
111 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
112 { 'revision|r=s' => \
$_revision,
113 'fetch-all|all' => \
$_fetch_all,
115 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
116 { 'revision|r=s' => \
$_revision,
117 %fc_opts, %init_opts } ],
118 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
119 " (requires URL argument)",
121 'multi-init' => [ \
&cmd_multi_init
,
122 "Deprecated alias for ".
123 "'$0 init -T<trunk> -b<branches> -t<tags>'",
125 dcommit
=> [ \
&cmd_dcommit
,
126 'Commit several diffs to merge with upstream',
127 { 'merge|m|M' => \
$_merge,
128 'strategy|s=s' => \
$_strategy,
129 'verbose|v' => \
$_verbose,
130 'dry-run|n' => \
$_dry_run,
131 'fetch-all|all' => \
$_fetch_all,
132 'commit-url=s' => \
$_commit_url,
133 'revision|r=i' => \
$_revision,
134 'no-rebase' => \
$_no_rebase,
135 %cmt_opts, %fc_opts } ],
136 branch
=> [ \
&cmd_branch
,
137 'Create a branch in the SVN repository',
138 { 'message|m=s' => \
$_message,
139 'dry-run|n' => \
$_dry_run,
140 'tag|t' => \
$_tag } ],
141 tag
=> [ sub { $_tag = 1; cmd_branch
(@_) },
142 'Create a tag in the SVN repository',
143 { 'message|m=s' => \
$_message,
144 'dry-run|n' => \
$_dry_run } ],
145 'set-tree' => [ \
&cmd_set_tree
,
146 "Set an SVN repository to a git tree-ish",
147 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
148 'create-ignore' => [ \
&cmd_create_ignore
,
149 'Create a .gitignore per svn:ignore',
150 { 'revision|r=i' => \
$_revision
152 'propget' => [ \
&cmd_propget
,
153 'Print the value of a property on a file or directory',
154 { 'revision|r=i' => \
$_revision } ],
155 'proplist' => [ \
&cmd_proplist
,
156 'List all properties of a file or directory',
157 { 'revision|r=i' => \
$_revision } ],
158 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
159 { 'revision|r=i' => \
$_revision
161 'show-externals' => [ \
&cmd_show_externals
, "Show svn:externals listings",
162 { 'revision|r=i' => \
$_revision
164 'multi-fetch' => [ \
&cmd_multi_fetch
,
165 "Deprecated alias for $0 fetch --all",
166 { 'revision|r=s' => \
$_revision, %fc_opts } ],
167 'migrate' => [ sub { },
168 # no-op, we automatically run this anyways,
169 'Migrate configuration/metadata/layout from
170 previous versions of git-svn',
171 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
173 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
174 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
175 'revision|r=s' => \
$_revision,
176 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
177 'incremental' => \
$Git::SVN
::Log
::incremental
,
178 'oneline' => \
$Git::SVN
::Log
::oneline
,
179 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
180 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
181 'authors-file|A=s' => \
$_authors,
182 'color' => \
$Git::SVN
::Log
::color
,
183 'pager=s' => \
$Git::SVN
::Log
::pager
185 'find-rev' => [ \
&cmd_find_rev
,
186 "Translate between SVN revision numbers and tree-ish",
188 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
189 { 'merge|m|M' => \
$_merge,
190 'verbose|v' => \
$_verbose,
191 'strategy|s=s' => \
$_strategy,
192 'local|l' => \
$_local,
193 'fetch-all|all' => \
$_fetch_all,
194 'dry-run|n' => \
$_dry_run,
196 'commit-diff' => [ \
&cmd_commit_diff
,
197 'Commit a diff between two trees',
198 { 'message|m=s' => \
$_message,
199 'file|F=s' => \
$_file,
200 'revision|r=s' => \
$_revision,
202 'info' => [ \
&cmd_info
,
203 "Show info about the latest SVN revision
204 on the current branch",
205 { 'url' => \
$_url, } ],
206 'blame' => [ \
&Git
::SVN
::Log
::cmd_blame
,
207 "Show what revision and author last modified each line of a file",
208 { 'git-format' => \
$_git_format } ],
212 for (my $i = 0; $i < @ARGV; $i++) {
213 if (defined $cmd{$ARGV[$i]}) {
220 # make sure we're always running at the top-level working directory
221 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
222 unless (-d
$ENV{GIT_DIR
}) {
223 if ($git_dir_user_set) {
224 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
225 "but it is not a directory\n";
227 my $git_dir = delete $ENV{GIT_DIR
};
230 $cdup = command_oneline
(qw
/rev-parse --show-cdup/);
231 $git_dir = '.' unless ($cdup);
232 chomp $cdup if ($cdup);
233 $cdup = "." unless ($cdup && length $cdup);
234 } "Already at toplevel, but $git_dir not found\n";
235 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
236 unless (-d
$git_dir) {
237 die "$git_dir still not found after going to ",
240 $ENV{GIT_DIR
} = $git_dir;
242 $_repository = Git
->repository(Repository
=> $ENV{GIT_DIR
});
245 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
247 read_repo_config
(\
%opts);
248 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
249 Getopt
::Long
::Configure
('pass_through');
251 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
252 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
253 'id|i=s' => \
$Git::SVN
::default_ref_id
,
254 'svn-remote|remote|R=s' => sub {
255 $Git::SVN
::no_reuse_existing
= 1;
256 $Git::SVN
::default_repo_id
= $_[1] });
257 exit 1 if (!$rv && $cmd && $cmd ne 'log');
260 version
() if $_version;
261 usage
(1) unless defined $cmd;
262 load_authors
() if $_authors;
264 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
265 Git
::SVN
::Migration
::migration_check
();
267 Git
::SVN
::init_vars
();
269 Git
::SVN
::verify_remotes_sanity
();
270 $cmd{$cmd}->[0]->(@ARGV);
273 post_fetch_checkout
();
276 ####################### primary functions ######################
278 my $exit = shift || 0;
279 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
281 git
-svn
- bidirectional operations between a single Subversion tree
and git
282 Usage
: git svn
<command
> [options
] [arguments
]\n
284 print $fd "Available commands:\n" unless $cmd;
286 foreach (sort keys %cmd) {
287 next if $cmd && $cmd ne $_;
288 next if /^multi-/; # don't show deprecated commands
289 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
290 foreach (sort keys %{$cmd{$_}->[2]}) {
291 # mixed-case options are for .git/config only
292 next if /[A-Z]/ && /^[a-z]+$/i;
293 # prints out arguments as they should be passed:
294 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
295 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
297 split /\|/,$_)," $x\n";
301 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
302 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
303 one git repository and want to keep them separate. See git-svn(1) for more
310 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
315 unless (-d $ENV{GIT_DIR}) {
316 my @init_db = ('init
');
317 push @init_db, "--template=$_template" if defined $_template;
318 if (defined $_shared) {
319 if ($_shared =~ /[a-z]/) {
320 push @init_db, "--shared=$_shared";
322 push @init_db, "--shared";
325 command_noisy(@init_db);
326 $_repository = Git->repository(Repository => ".git");
329 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
330 foreach my $i (keys %icv) {
331 die "'$set' and '$i' cannot both be set\n" if $set;
332 next unless defined $icv{$i};
333 command_noisy('config
', "$pfx.$i", $icv{$i});
339 my $repo_path = shift or return;
340 mkpath([$repo_path]) unless -d $repo_path;
341 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
342 $ENV{GIT_DIR} = '.git';
343 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
347 my ($url, $path) = @_;
348 if (!defined $path &&
349 (defined $_trunk || defined $_branches || defined $_tags ||
350 defined $_stdlayout) &&
351 $url !~ m#^[a-z\+]+://#) {
354 $path = basename($url) if !defined $path || !length $path;
355 cmd_init($url, $path);
356 Git::SVN::fetch_all($Git::SVN::default_repo_id);
360 if (defined $_stdlayout) {
361 $_trunk = 'trunk' if (!defined $_trunk);
362 $_tags = 'tags' if (!defined $_tags);
363 $_branches = 'branches' if (!defined $_branches);
365 if (defined $_trunk || defined $_branches || defined $_tags) {
366 return cmd_multi_init(@_);
368 my $url = shift or die "SVN repository location required
",
369 "as a command
-line argument
\n";
373 Git::SVN->init($url);
377 if (grep /^\d+=./, @_) {
378 die "'<rev>=<commit>' fetch arguments are
",
379 "no longer supported
.\n";
383 die "Usage
: $0 fetch
[--all
] [svn
-remote
]\n";
385 $remote ||= $Git::SVN::default_repo_id;
389 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
395 if ($_stdin || !@commits) {
396 print "Reading from stdin
...\n";
399 if (/\b($sha1_short)\b/o) {
400 unshift @commits, $1;
405 foreach my $c (@commits) {
406 my @tmp = command('rev-parse',$c);
407 if (scalar @tmp == 1) {
409 } elsif (scalar @tmp > 1) {
410 push @revs, reverse(command('rev-list',@tmp));
412 fatal "Failed to rev
-parse
$c";
415 my $gs = Git::SVN->new;
416 my ($r_last, $cmt_last) = $gs->last_rev_commit;
418 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
419 fatal "There are new revisions that were fetched
",
420 "and need to be merged
(or acknowledged
) ",
421 "before committing
.\nlast rev
: $r_last\n",
422 " current
: $gs->{last_rev
}";
424 $gs->set_tree($_) foreach @revs;
425 print "Done committing
",scalar @revs," revisions to SVN
\n";
431 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
432 'Cannot dcommit with a dirty index. Commit your changes first, '
433 . "or stash them with
`git stash'.\n";
436 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
438 die "Unable to determine upstream SVN information from ",
439 "$head history.\nPerhaps the repository is empty.";
442 if (defined $_commit_url) {
445 $url = eval { command_oneline('config', '--get',
446 "svn-remote.$gs->{repo_id}.commiturl") };
452 my $last_rev = $_revision if defined $_revision;
454 print "Committing to $url ...\n";
456 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
457 if ($_no_rebase && scalar(@$linear_refs) > 1) {
458 warn "Attempting to commit more than one change while ",
459 "--no-rebase is enabled.\n",
460 "If these changes depend on each other, re-running ",
461 "without --no-rebase may be required."
463 my $expect_url = $url;
464 Git::SVN::remove_username($expect_url);
466 my $d = shift @$linear_refs or last;
467 unless (defined $last_rev) {
468 (undef, $last_rev, undef) = cmt_metadata("$d~1");
469 unless (defined $last_rev) {
470 fatal "Unable to extract revision information ",
475 print "diff-tree $d~1 $d\n";
478 my %ed_opts = ( r => $last_rev,
479 log => get_commit_entry($d)->{log},
480 ra => Git::SVN::Ra->new($url),
481 config => SVN::Core::config_get_config(
482 $Git::SVN::Ra::config_dir
487 print "Committed r$_[0]\n";
491 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
492 print "No changes\n$d~1 == $d\n";
493 } elsif ($parents->{$d} && @{$parents->{$d}}) {
494 $gs->{inject_parents_dcommit}->{$cmt_rev} =
497 $_fetch_all ? $gs->fetch_all : $gs->fetch;
498 $last_rev = $cmt_rev;
501 # we always want to rebase against the current HEAD,
502 # not any head that was passed to us
503 my @diff = command('diff-tree', $d,
507 @finish = rebase_cmd();
508 print STDERR "W: $d and ", $gs->refname,
509 " differ, using @finish:\n",
510 join("\n", @diff), "\n";
512 print "No changes between current HEAD and ",
514 "\nResetting to the latest ",
516 @finish = qw/reset --mixed/;
518 command_noisy(@finish, $gs->refname);
521 my ($url_, $rev_, $uuid_, $gs_) =
522 working_head_info($head, \@refs);
523 my ($linear_refs_, $parents_) =
524 linearize_history($gs_, \@refs);
525 if (scalar(@$linear_refs) !=
526 scalar(@$linear_refs_)) {
527 fatal "# of revisions changed ",
529 join("\n", @$linear_refs),
531 join("\n", @$linear_refs_), "\n",
532 'If you are attempting to commit ',
533 "merges, try running:\n\t",
534 'git rebase --interactive',
535 '--preserve-merges ',
537 "\nBefore dcommitting";
539 if ($url_ ne $expect_url) {
540 fatal "URL mismatch after rebase: ",
541 "$url_ != $expect_url";
543 if ($uuid_ ne $uuid) {
544 fatal "uuid mismatch after rebase: ",
549 for ($i = 0; $i < scalar @$linear_refs; $i++) {
550 my $new = $linear_refs_->[$i] or next;
552 $parents->{$linear_refs->[$i]};
564 my ($branch_name, $head) = @_;
566 unless (defined $branch_name && length $branch_name) {
567 die(($_tag ? "tag" : "branch") . " name required\n");
571 my ($src, $rev, undef, $gs) = working_head_info($head);
573 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
574 my $glob = $remote->{ $_tag ? 'tags' : 'branches' };
575 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
576 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
578 my $ctx = SVN::Client->new(
579 auth => Git::SVN::Ra::_auth_providers(),
581 ${ $_[0] } = defined $_message
583 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
589 $ctx->ls($dst, 'HEAD', 0);
590 } and die "branch ${branch_name} already exists\n";
592 print "Copying ${src} at r${rev} to ${dst}...\n";
593 $ctx->copy($src, $rev, $dst)
600 my $revision_or_hash = shift or die "SVN or git revision required ",
601 "as a command-line argument\n";
603 if ($revision_or_hash =~ /^r\d+$/) {
607 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
609 die "Unable to determine upstream SVN information from ",
612 my $desired_revision = substr($revision_or_hash, 1);
613 $result = $gs->rev_map_get($desired_revision, $uuid);
615 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
618 print "$result\n" if $result;
622 command_noisy(qw/update-index --refresh/);
623 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
625 die "Unable to determine upstream SVN information from ",
626 "working tree history\n";
629 print "Remote Branch: " . $gs->refname . "\n";
630 print "SVN URL: " . $url . "\n";
633 if (command(qw/diff-index HEAD --/)) {
634 print STDERR "Cannot rebase with uncommited changes:\n";
635 command_noisy('status');
639 # rebase will checkout for us, so no need to do it explicitly
640 $_no_checkout = 'true';
641 $_fetch_all ? $gs->fetch_all : $gs->fetch;
643 command_noisy(rebase_cmd(), $gs->refname);
646 sub cmd_show_ignore {
647 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
648 $gs ||= Git::SVN->new;
649 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
650 $gs->prop_walk($gs->{path}, $r, sub {
651 my ($gs, $path, $props) = @_;
652 print STDOUT "\n# $path\n";
653 my $s = $props->{'svn:ignore'} or return;
654 $s =~ s/[\r\n]+/\n/g;
661 sub cmd_show_externals {
662 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
663 $gs ||= Git::SVN->new;
664 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
665 $gs->prop_walk($gs->{path}, $r, sub {
666 my ($gs, $path, $props) = @_;
667 print STDOUT "\n# $path\n";
668 my $s = $props->{'svn:externals'} or return;
669 $s =~ s/[\r\n]+/\n/g;
676 sub cmd_create_ignore {
677 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
678 $gs ||= Git::SVN->new;
679 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
680 $gs->prop_walk($gs->{path}, $r, sub {
681 my ($gs, $path, $props) = @_;
682 # $path is of the form /path/to/dir/
684 # SVN can have attributes on empty directories,
685 # which git won't track
686 mkpath([$path]) unless -d $path;
687 my $ignore = $path . '.gitignore';
688 my $s = $props->{'svn:ignore'} or return;
689 open(GITIGNORE, '>', $ignore)
690 or fatal("Failed to open `$ignore' for writing: $!");
691 $s =~ s/[\r\n]+/\n/g;
693 # Prefix all patterns so that the ignore doesn't apply
694 # to sub-directories.
696 print GITIGNORE
"$s\n";
698 or fatal
("Failed to close `$ignore': $!");
699 command_noisy
('add', '-f', $ignore);
703 sub canonicalize_path
{
705 my $dot_slash_added = 0;
706 if (substr($path, 0, 1) ne "/") {
707 $path = "./" . $path;
708 $dot_slash_added = 1;
710 # File::Spec->canonpath doesn't collapse x/../y into y (for a
711 # good reason), so let's do this manually.
713 $path =~ s
#/\.(?:/|$)#/#g;
714 $path =~ s
#/[^/]+/\.\.##g;
716 $path =~ s
#^\./## if $dot_slash_added;
724 # Helper for cmd_propget and cmd_proplist below.
727 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
728 $gs ||= Git
::SVN
->new;
730 # prefix THE PATH by the sub-directory from which the user
732 $path = $cmd_dir_prefix . $path;
733 fatal
("No such file or directory: $path") unless -e
$path;
734 my $is_dir = -d
$path ?
1 : 0;
735 $path = $gs->{path
} . '/' . $path;
737 # canonicalize the path (otherwise libsvn will abort or fail to
739 $path = canonicalize_path
($path);
741 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
744 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
747 (undef, $props) = $gs->ra->get_file($path, $r, undef);
752 # cmd_propget (PROP, PATH)
753 # ------------------------
754 # Print the SVN property PROP for PATH.
756 my ($prop, $path) = @_;
757 $path = '.' if not defined $path;
758 usage
(1) if not defined $prop;
759 my $props = get_svnprops
($path);
760 if (not defined $props->{$prop}) {
761 fatal
("`$path' does not have a `$prop' SVN property.");
763 print $props->{$prop} . "\n";
766 # cmd_proplist (PATH)
767 # -------------------
768 # Print the list of SVN properties for PATH.
771 $path = '.' if not defined $path;
772 my $props = get_svnprops
($path);
773 print "Properties on '$path':\n";
774 foreach (sort keys %{$props}) {
781 unless (defined $_trunk || defined $_branches || defined $_tags) {
785 # there are currently some bugs that prevent multi-init/multi-fetch
786 # setups from working well without this.
787 $Git::SVN
::_minimize_url
= 1;
789 $_prefix = '' unless defined $_prefix;
795 if (defined $_trunk) {
796 my $trunk_ref = $_prefix . 'trunk';
797 # try both old-style and new-style lookups:
798 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
800 my ($trunk_url, $trunk_path) =
801 complete_svn_url
($url, $_trunk);
802 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
806 return unless defined $_branches || defined $_tags;
807 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
808 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
809 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
812 sub cmd_multi_fetch
{
813 my $remotes = Git
::SVN
::read_all_remotes
();
814 foreach my $repo_id (sort keys %$remotes) {
815 if ($remotes->{$repo_id}->{url
}) {
816 Git
::SVN
::fetch_all
($repo_id, $remotes);
821 # this command is special because it requires no metadata
822 sub cmd_commit_diff
{
823 my ($ta, $tb, $url) = @_;
824 my $usage = "Usage: $0 commit-diff -r<revision> ".
825 "<tree-ish> <tree-ish> [<URL>]";
826 fatal
($usage) if (!defined $ta || !defined $tb);
829 my $gs = eval { Git
::SVN
->new };
831 fatal
("Needed URL or usable git-svn --id in ",
832 "the command-line\n", $usage);
835 $svn_path = $gs->{path
};
837 unless (defined $_revision) {
838 fatal
("-r|--revision is a required argument\n", $usage);
840 if (defined $_message && defined $_file) {
841 fatal
("Both --message/-m and --file/-F specified ",
842 "for the commit message.\n",
843 "I have no idea what you mean");
845 if (defined $_file) {
846 $_message = file_to_s
($_file);
848 $_message ||= get_commit_entry
($tb)->{log};
850 my $ra ||= Git
::SVN
::Ra
->new($url);
853 $r = $ra->get_latest_revnum;
854 } elsif ($r !~ /^\d+$/) {
855 die "revision argument: $r not understood by git-svn\n";
857 my %ed_opts = ( r
=> $r,
862 editor_cb
=> sub { print "Committed r$_[0]\n" },
863 svn_path
=> $svn_path );
864 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
865 print "No changes\n$ta == $tb\n";
869 sub escape_uri_only
{
872 foreach (split m{/}, $uri) {
873 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
881 if ($url =~ m
#^([^:]+)://([^/]*)(.*)$#) {
882 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
883 $url = "$scheme://$domain$uri";
889 my $path = canonicalize_path
(defined($_[0]) ?
$_[0] : ".");
890 my $fullpath = canonicalize_path
($cmd_dir_prefix . $path);
892 die "Too many arguments specified\n";
895 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
897 if (!$file_type && !$diff_status) {
898 print STDERR
"svn: '$path' is not under version control\n";
902 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
904 die "Unable to determine upstream SVN information from ",
905 "working tree history\n";
908 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
909 $path = "." if $path eq "";
911 my $full_url = $url . ($fullpath eq "" ?
"" : "/$fullpath");
914 print escape_url
($full_url), "\n";
918 my $result = "Path: $path\n";
919 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
920 $result .= "URL: " . escape_url
($full_url) . "\n";
923 my $repos_root = $gs->repos_root;
924 Git
::SVN
::remove_username
($repos_root);
925 $result .= "Repository Root: " . escape_url
($repos_root) . "\n";
928 $result .= "Repository Root: (offline)\n";
930 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
931 ($SVN::Core
::VERSION
le '1.5.4' || $file_type ne "dir");
932 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
934 $result .= "Node Kind: " .
935 ($file_type eq "dir" ?
"directory" : "file") . "\n";
937 my $schedule = $diff_status eq "A"
939 : ($diff_status eq "D" ?
"delete" : "normal");
940 $result .= "Schedule: $schedule\n";
942 if ($diff_status eq "A") {
947 my ($lc_author, $lc_rev, $lc_date_utc);
948 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $fullpath);
949 my $log = command_output_pipe
(@args);
950 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
952 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
954 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
955 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
956 (undef, $lc_rev, undef) = ::extract_metadata
($1);
961 Git
::SVN
::Log
::set_local_timezone
();
963 $result .= "Last Changed Author: $lc_author\n";
964 $result .= "Last Changed Rev: $lc_rev\n";
965 $result .= "Last Changed Date: " .
966 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
968 if ($file_type ne "dir") {
969 my $text_last_updated_date =
970 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
972 "Text Last Updated: " .
973 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
976 if ($diff_status eq "D") {
978 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
979 if ($file_type eq "link") {
980 my $file_name = <$fh>;
981 $checksum = md5sum
("link $file_name");
983 $checksum = md5sum
($fh);
985 command_close_pipe
($fh, $ctx);
986 } elsif ($file_type eq "link") {
988 command
(qw(cat-file blob), "HEAD:$path");
990 md5sum
("link " . $file_name);
992 open FILE
, "<", $path or die $!;
993 $checksum = md5sum
(\
*FILE
);
994 close FILE
or die $!;
996 $result .= "Checksum: " . $checksum . "\n";
1002 ########################### utility functions #########################
1005 my @cmd = qw
/rebase/;
1006 push @cmd, '-v' if $_verbose;
1007 push @cmd, qw
/--merge/ if $_merge;
1008 push @cmd, "--strategy=$_strategy" if $_strategy;
1012 sub post_fetch_checkout
{
1013 return if $_no_checkout;
1014 my $gs = $Git::SVN
::_head
or return;
1015 return if verify_ref
('refs/heads/master^0');
1017 my $valid_head = verify_ref
('HEAD^0');
1018 command_noisy
(qw(update-ref refs/heads/master), $gs->refname);
1019 return if ($valid_head || !verify_ref
('HEAD^0'));
1021 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
1022 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
1023 return if -f
$index;
1025 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
1026 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
1027 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
1028 print STDERR
"Checked out HEAD:\n ",
1029 $gs->full_url, " r", $gs->last_rev, "\n";
1032 sub complete_svn_url
{
1033 my ($url, $path) = @_;
1035 if ($path !~ m
#^[a-z\+]+://#) {
1036 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
1037 fatal
("E: '$path' is not a complete URL ",
1038 "and a separate URL is not specified");
1040 return ($url, $path);
1045 sub complete_url_ls_init
{
1046 my ($ra, $repo_path, $switch, $pfx) = @_;
1047 unless ($repo_path) {
1048 print STDERR
"W: $switch not specified\n";
1051 $repo_path =~ s
#/+$##;
1052 if ($repo_path =~ m
#^[a-z\+]+://#) {
1053 $ra = Git
::SVN
::Ra
->new($repo_path);
1056 $repo_path =~ s
#^/+##;
1058 fatal
("E: '$repo_path' is not a complete URL ",
1059 "and a separate URL is not specified");
1062 my $url = $ra->{url
};
1063 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
1064 my $k = "svn-remote.$gs->{repo_id}.url";
1065 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
1066 if ($orig_url && ($orig_url ne $gs->{url
})) {
1067 die "$k already set: $orig_url\n",
1068 "wanted to set to: $gs->{url}\n";
1070 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
1071 my $remote_path = "$ra->{svn_path}/$repo_path";
1072 $remote_path =~ s
#/+#/#g;
1073 $remote_path =~ s
#^/##g;
1074 $remote_path .= "/*" if $remote_path !~ /\
*/;
1075 my ($n) = ($switch =~ /^--(\w+)/);
1076 if (length $pfx && $pfx !~ m
#/$#) {
1077 die "--prefix='$pfx' must have a trailing slash '/'\n";
1079 command_noisy
('config',
1080 "svn-remote.$gs->{repo_id}.$n",
1081 "$remote_path:refs/remotes/$pfx*" .
1082 ('/*' x
(($remote_path =~ tr
/*/*/) - 1)) );
1087 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1088 { STDERR
=> 0 }); };
1091 sub get_tree_from_treeish
{
1093 # $treeish can be a symbolic ref, too:
1094 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1096 while ($type eq 'tag') {
1097 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1099 if ($type eq 'commit') {
1100 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1102 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1103 die "Unable to get tree from $treeish\n" unless $expected;
1104 } elsif ($type eq 'tree') {
1105 $expected = $treeish;
1107 die "$treeish is a $type, expected tree, tag or commit\n";
1112 sub get_commit_entry
{
1113 my ($treeish) = shift;
1114 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
1115 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1116 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1117 open my $log_fh, '>', $commit_editmsg or croak
$!;
1119 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1120 if ($type eq 'commit' || $type eq 'tag') {
1121 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1129 $in_msg = 1 if (/^\s*$/);
1130 $author = $1 if (/^author (.*>)/);
1131 } elsif (/^git-svn-id: /) {
1132 # skip this for now, we regenerate the
1133 # correct one on re-fetch anyways
1134 # TODO: set *:merge properties or like...
1136 if (/^From:/ || /^Signed-off-by:/) {
1142 $msgbuf =~ s/\s+$//s;
1143 if ($Git::SVN
::_add_author_from
&& defined($author)
1145 $msgbuf .= "\n\nFrom: $author";
1147 print $log_fh $msgbuf or croak
$!;
1148 command_close_pipe
($msg_fh, $ctx);
1150 close $log_fh or croak
$!;
1152 if ($_edit || ($type eq 'tree')) {
1153 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1154 # TODO: strip out spaces, comments, like git-commit.sh
1155 system($editor, $commit_editmsg);
1157 rename $commit_editmsg, $commit_msg or croak
$!;
1159 # SVN requires messages to be UTF-8 when entering the repo
1161 open $log_fh, '<', $commit_msg or croak
$!;
1163 chomp($log_entry{log} = <$log_fh>);
1165 if (my $enc = Git
::config
('i18n.commitencoding')) {
1167 Encode
::from_to
($log_entry{log}, $enc, 'UTF-8');
1169 close $log_fh or croak
$!;
1176 my ($str, $file, $mode) = @_;
1177 open my $fd,'>',$file or croak
$!;
1178 print $fd $str,"\n" or croak
$!;
1179 close $fd or croak
$!;
1180 chmod ($mode &~ umask, $file) if (defined $mode);
1185 open my $fd,'<',$file or croak
"$!: file: $file\n";
1188 close $fd or croak
$!;
1193 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1195 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1196 my $log = $cmd eq 'log';
1197 while (<$authors>) {
1199 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1200 my ($user, $name, $email) = ($1, $2, $3);
1202 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1204 $users{$user} = [$name, $email];
1207 close $authors or croak
$!;
1210 # convert GetOpt::Long specs for use by git-config
1211 sub read_repo_config
{
1212 return unless -d
$ENV{GIT_DIR
};
1215 foreach my $o (keys %$opts) {
1216 # if we have mixedCase and a long option-only, then
1217 # it's a config-only variable that we don't need for
1219 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1220 my $v = $opts->{$o};
1221 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1223 my $arg = 'git config';
1224 $arg .= ' --int' if ($o =~ /[:=]i$/);
1225 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1226 if (ref $v eq 'ARRAY') {
1227 chomp(my @tmp = `$arg --get-all svn.$key`);
1230 chomp(my $tmp = `$arg --get svn.$key`);
1231 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1236 delete @
$opts{@config_only} if @config_only;
1239 sub extract_metadata
{
1240 my $id = shift or return (undef, undef, undef);
1241 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1243 if (!defined $rev || !$uuid || !$url) {
1244 # some of the original repositories I made had
1245 # identifiers like this:
1246 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1248 return ($url, $rev, $uuid);
1252 return extract_metadata
((grep(/^git-svn-id: /,
1253 command
(qw
/cat-file commit/, shift)))[-1]);
1256 sub working_head_info
{
1257 my ($head, $refs) = @_;
1258 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
1259 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1263 if ( m{^commit ($::sha1)$} ) {
1264 unshift @
$refs, $hash if $hash and $refs;
1268 next unless s{^\s*(git-svn-id:)}{$1};
1269 my ($url, $rev, $uuid) = extract_metadata
($_);
1270 if (defined $url && defined $rev) {
1271 next if $max{$url} and $max{$url} < $rev;
1272 if (my $gs = Git
::SVN
->find_by_url($url)) {
1273 my $c = $gs->rev_map_get($rev, $uuid);
1274 if ($c && $c eq $hash) {
1275 close $fh; # break the pipe
1276 return ($url, $rev, $uuid, $gs);
1278 $max{$url} ||= $gs->rev_map_max;
1283 command_close_pipe
($fh, $ctx);
1284 (undef, undef, undef, undef);
1287 sub read_commit_parents
{
1288 my ($parents, $c) = @_;
1289 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1290 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1291 @
{$parents->{$c}} = split(/ /, $p);
1294 sub linearize_history
{
1295 my ($gs, $refs) = @_;
1297 foreach my $c (@
$refs) {
1298 read_commit_parents
(\
%parents, $c);
1303 my $last_svn_commit = $gs->last_commit;
1304 foreach my $c (reverse @
$refs) {
1305 next if $c eq $last_svn_commit;
1308 unshift @linear_refs, $c;
1311 # we only want the first parent to diff against for linear
1312 # history, we save the rest to inject when we finalize the
1314 my $fp_a = verify_ref
("$c~1");
1315 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1316 if (!$fp_a || !$fp_b) {
1318 "has no parent commit, and therefore ",
1319 "nothing to diff against.\n",
1320 "You should be working from a repository ",
1321 "originally created by git-svn\n";
1323 if ($fp_a ne $fp_b) {
1324 die "$c~1 = $fp_a, however parsing commit $c ",
1325 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1328 foreach my $p (@
{$parents{$c}}) {
1332 (\
@linear_refs, \
%parents);
1335 sub find_file_type_and_diff_status
{
1337 return ('dir', '') if $path eq '';
1340 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1341 my $diff_status = (split(' ', $diff_output))[0] || "";
1343 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1345 return (undef, undef) if !$diff_status && !$ls_tree;
1347 if ($diff_status eq "A") {
1348 return ("link", $diff_status) if -l
$path;
1349 return ("dir", $diff_status) if -d
$path;
1350 return ("file", $diff_status);
1353 my $mode = (split(' ', $ls_tree))[0] || "";
1355 return ("link", $diff_status) if $mode eq "120000";
1356 return ("dir", $diff_status) if $mode eq "040000";
1357 return ("file", $diff_status);
1363 my $md5 = Digest
::MD5
->new();
1364 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
1365 $md5->addfile($arg) or croak
$!;
1366 } elsif ($ref eq 'SCALAR') {
1367 $md5->add($$arg) or croak
$!;
1369 $md5->add($arg) or croak
$!;
1371 ::fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1373 return $md5->hexdigest();
1379 use Fcntl qw
/:DEFAULT :seek/;
1380 use constant rev_map_fmt
=> 'NH40';
1381 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
1382 $_repack $_repack_flags $_use_svm_props $_head
1383 $_use_svnsync_props $no_reuse_existing $_minimize_url
1384 $_use_log_author $_add_author_from $_localtime/;
1386 use File
::Path qw
/mkpath/;
1387 use File
::Copy qw
/copy/;
1390 my ($_gc_nr, $_gc_period);
1392 # properties that we do not log:
1395 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
1396 svn
:special svn
:executable
1397 svn
:entry
:committed
-rev
1398 svn
:entry
:last-author
1400 svn
:entry
:committed
-date
/;
1402 # some options are read globally, but can be overridden locally
1403 # per [svn-remote "..."] section. Command-line options will *NOT*
1404 # override options set in an [svn-remote "..."] section
1406 for my $option (qw
/follow_parent no_metadata use_svm_props
1407 use_svnsync_props
/) {
1410 my $prop = "-$option";
1413 return $self->{$prop} if exists $self->{$prop};
1414 my $k = "svn-remote.$self->{repo_id}.$key";
1415 eval { command_oneline
(qw
/config --get/, $k) };
1417 $self->{$prop} = ${"Git::SVN::_$option"};
1419 my $v = command_oneline
(qw
/config --bool/,$k);
1420 $self->{$prop} = $v eq 'false' ?
0 : 1;
1422 return $self->{$prop};
1428 my (%LOCKFILES, %INDEX_FILES);
1430 unlink keys %LOCKFILES if %LOCKFILES;
1431 unlink keys %INDEX_FILES if %INDEX_FILES;
1434 sub resolve_local_globs
{
1435 my ($url, $fetch, $glob_spec) = @_;
1436 return unless defined $glob_spec;
1437 my $ref = $glob_spec->{ref};
1438 my $path = $glob_spec->{path
};
1439 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
1440 next unless m
#^refs/remotes/$ref->{regex}$#;
1442 my $pathname = desanitize_refname
($path->full_path($p));
1443 my $refname = desanitize_refname
($ref->full_path($p));
1444 if (my $existing = $fetch->{$pathname}) {
1445 if ($existing ne $refname) {
1446 die "Refspec conflict:\n",
1447 "existing: refs/remotes/$existing\n",
1448 " globbed: refs/remotes/$refname\n";
1450 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
1451 $u =~ s!^\Q$url\E(/|$)!! or die
1452 "refs/remotes/$refname: '$url' not found in '$u'\n";
1453 if ($pathname ne $u) {
1454 warn "W: Refspec glob conflict ",
1455 "(ref: refs/remotes/$refname):\n",
1456 "expected path: $pathname\n",
1458 "Continuing ahead with $u\n";
1462 $fetch->{$pathname} = $refname;
1467 sub parse_revision_argument
{
1468 my ($base, $head) = @_;
1469 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
1470 return ($base, $head);
1472 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
1473 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
1474 return ($head, $head) if ($::_revision
eq 'HEAD');
1475 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
1476 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
1477 die "revision argument: $::_revision not understood by git-svn\n";
1481 my ($repo_id, $remotes) = @_;
1485 $repo_id = $gs->{repo_id
};
1487 $remotes ||= read_all_remotes
();
1488 my $remote = $remotes->{$repo_id} or
1489 die "[svn-remote \"$repo_id\"] unknown\n";
1490 my $fetch = $remote->{fetch
};
1491 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
1493 my $ra = Git
::SVN
::Ra
->new($url);
1494 my $uuid = $ra->get_uuid;
1495 my $head = $ra->get_latest_revnum;
1496 my $base = defined $fetch ?
$head : 0;
1498 # read the max revs for wildcard expansion (branches/*, tags/*)
1499 foreach my $t (qw
/branches tags/) {
1500 defined $remote->{$t} or next;
1501 push @globs, $remote->{$t};
1502 my $max_rev = eval { tmp_config
(qw
/--int --get/,
1503 "svn-remote.$repo_id.${t}-maxRev") };
1504 if (defined $max_rev && ($max_rev < $base)) {
1506 } elsif (!defined $max_rev) {
1512 foreach my $p (sort keys %$fetch) {
1513 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
1514 my $lr = $gs->rev_map_max;
1516 $base = $lr if ($lr < $base);
1522 ($base, $head) = parse_revision_argument
($base, $head);
1523 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
1526 sub read_all_remotes
{
1528 my $use_svm_props = eval { command_oneline
(qw
/config
--bool
1529 svn
.useSvmProps
/) };
1530 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
1531 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
1532 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1533 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1534 die("svn-remote.$remote: remote ref '$_remote_ref' "
1535 . "must start with 'refs/remotes/'\n")
1536 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1537 my $remote_ref = $1;
1538 $local_ref =~ s{^/}{};
1539 $r->{$remote}->{fetch
}->{$local_ref} = $remote_ref;
1540 $r->{$remote}->{svm
} = {} if $use_svm_props;
1541 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1542 $r->{$1}->{svm
} = {};
1543 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1544 $r->{$1}->{url
} = $2;
1545 } elsif (m
!^(.+)\
.(branches
|tags
)=
1546 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
1547 my ($p, $g) = ($3, $4);
1548 my $rs = $r->{$1}->{$2} = {
1551 path
=> Git
::SVN
::GlobSpec
->new($p),
1552 ref => Git
::SVN
::GlobSpec
->new($g) };
1553 if (length($rs->{ref}->{right
}) != 0) {
1554 die "The '*' glob character must be the last ",
1555 "character of '$g'\n";
1561 if (defined $r->{$_}->{svm
}) {
1564 my $section = "svn-remote.$_";
1566 source
=> tmp_config
('--get',
1567 "$section.svm-source"),
1568 replace
=> tmp_config
('--get',
1569 "$section.svm-replace"),
1572 $r->{$_}->{svm
} = $svm;
1580 $_gc_nr = $_gc_period = 1000;
1581 if (defined $_repack || defined $_repack_flags) {
1582 warn "Repack options are obsolete; they have no effect.\n";
1586 sub verify_remotes_sanity
{
1587 return unless -d
$ENV{GIT_DIR
};
1589 foreach (command
(qw
/config -l/)) {
1590 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1592 die "Remote ref refs/remote/$1 is tracked by",
1593 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1594 "Please resolve this ambiguity in ",
1595 "your git configuration file before ",
1603 sub find_existing_remote
{
1604 my ($url, $remotes) = @_;
1605 return undef if $no_reuse_existing;
1607 foreach my $repo_id (keys %$remotes) {
1608 my $u = $remotes->{$repo_id}->{url
} or next;
1610 $existing = $repo_id;
1616 sub init_remote_config
{
1617 my ($self, $url, $no_write) = @_;
1618 $url =~ s!/+$!!; # strip trailing slash
1619 my $r = read_all_remotes
();
1620 my $existing = find_existing_remote
($url, $r);
1622 unless ($no_write) {
1623 print STDERR
"Using existing ",
1624 "[svn-remote \"$existing\"]\n";
1626 $self->{repo_id
} = $existing;
1627 } elsif ($_minimize_url) {
1628 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1629 $existing = find_existing_remote
($min_url, $r);
1631 unless ($no_write) {
1632 print STDERR
"Using existing ",
1633 "[svn-remote \"$existing\"]\n";
1635 $self->{repo_id
} = $existing;
1637 if ($min_url ne $url) {
1638 unless ($no_write) {
1639 print STDERR
"Using higher level of URL: ",
1640 "$url => $min_url\n";
1642 my $old_path = $self->{path
};
1643 $self->{path
} = $url;
1644 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1645 if (length $old_path) {
1646 $self->{path
} .= "/$old_path";
1653 # verify that we aren't overwriting anything:
1655 command_oneline
('config', '--get',
1656 "svn-remote.$self->{repo_id}.url")
1658 if ($orig_url && ($orig_url ne $url)) {
1659 die "svn-remote.$self->{repo_id}.url already set: ",
1660 "$orig_url\nwanted to set to: $url\n";
1663 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1664 if (defined $xpath) {
1665 die "svn-remote.$xrepo_id.fetch already set to track ",
1666 "$xpath:refs/remotes/", $self->refname, "\n";
1668 unless ($no_write) {
1669 command_noisy
('config',
1670 "svn-remote.$self->{repo_id}.url", $url);
1671 $self->{path
} =~ s{^/}{};
1672 command_noisy
('config', '--add',
1673 "svn-remote.$self->{repo_id}.fetch",
1674 "$self->{path}:".$self->refname);
1676 $self->{url
} = $url;
1679 sub find_by_url
{ # repos_root and, path are optional
1680 my ($class, $full_url, $repos_root, $path) = @_;
1682 return undef unless defined $full_url;
1683 remove_username
($full_url);
1684 remove_username
($repos_root) if defined $repos_root;
1685 my $remotes = read_all_remotes
();
1686 if (defined $full_url && defined $repos_root && !defined $path) {
1688 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1690 foreach my $repo_id (keys %$remotes) {
1691 my $u = $remotes->{$repo_id}->{url
} or next;
1692 remove_username
($u);
1693 next if defined $repos_root && $repos_root ne $u;
1695 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1696 foreach (qw
/branches tags/) {
1697 resolve_local_globs
($u, $fetch,
1698 $remotes->{$repo_id}->{$_});
1701 my $rwr = rewrite_root
({repo_id
=> $repo_id});
1702 my $svm = $remotes->{$repo_id}->{svm
}
1703 if defined $remotes->{$repo_id}->{svm
};
1704 unless (defined $p) {
1710 remove_username
($z);
1711 } elsif (defined $svm) {
1712 $z = $svm->{source
};
1713 $prefix = $svm->{replace
};
1714 $prefix =~ s
#^\Q$u\E(?:/|$)##;
1717 $p =~ s
#^\Q$z\E(?:/|$)#$prefix# or next;
1719 foreach my $f (keys %$fetch) {
1721 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1728 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1729 my $self = _new
($class, $repo_id, $ref_id, $path);
1731 $self->init_remote_config($url, $no_write);
1738 foreach (command
(qw
/config -l/)) {
1739 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1740 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1741 my ($repo_id, $path, $ref) = ($1, $2, $3);
1742 if ($ref eq $ref_id) {
1743 $path = '' if ($path =~ m
#^\./?#);
1744 return ($repo_id, $path);
1747 (undef, undef, undef);
1751 my ($class, $ref_id, $repo_id, $path) = @_;
1752 if (defined $ref_id && !defined $repo_id && !defined $path) {
1753 ($repo_id, $path) = find_ref
($ref_id);
1754 if (!defined $repo_id) {
1755 die "Could not find a \"svn-remote.*.fetch\" key ",
1756 "in the repository configuration matching: ",
1757 "refs/remotes/$ref_id\n";
1760 my $self = _new
($class, $repo_id, $ref_id, $path);
1761 if (!defined $self->{path
} || !length $self->{path
}) {
1762 my $fetch = command_oneline
('config', '--get',
1763 "svn-remote.$repo_id.fetch",
1764 ":refs/remotes/$ref_id\$") or
1765 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1766 "\":refs/remotes/$ref_id\$\" in config\n";
1767 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1769 $self->{url
} = command_oneline
('config', '--get',
1770 "svn-remote.$repo_id.url") or
1771 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1777 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1779 # It cannot end with a slash /, we'll throw up on this because
1780 # SVN can't have directories with a slash in their name, either:
1781 if ($refname =~ m{/$}) {
1782 die "ref: '$refname' ends with a trailing slash, this is ",
1783 "not permitted by git nor Subversion\n";
1786 # It cannot have ASCII control character space, tilde ~, caret ^,
1787 # colon :, question-mark ?, asterisk *, space, or open bracket [
1790 # Additionally, % must be escaped because it is used for escaping
1791 # and we want our escaped refname to be reversible
1792 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1794 # no slash-separated component can begin with a dot .
1796 $refname =~ s{/\.}{/%2E}g;
1798 # It cannot have two consecutive dots .. anywhere
1800 $refname =~ s{\.\.}{%2E%2E}g;
1805 sub desanitize_refname
{
1807 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1813 return $self->{svm
}->{uuid
} if $self->svm;
1815 unless ($self->{svm
}) {
1816 die "SVM UUID not cached, and reading remotely failed\n";
1818 $self->{svm
}->{uuid
};
1823 return $self->{svm
} if $self->{svm
};
1825 # see if we have it in our config, first:
1827 my $section = "svn-remote.$self->{repo_id}";
1829 source
=> tmp_config
('--get', "$section.svm-source"),
1830 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1831 replace
=> tmp_config
('--get', "$section.svm-replace"),
1834 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1835 $self->{svm
} = $svm;
1841 my ($self, $ra) = @_;
1842 return $ra if $self->svm;
1844 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1845 "(svm:source, svm:uuid) ",
1846 "from the following URLs:\n" );
1847 sub read_svm_props
{
1848 my ($self, $ra, $path, $r) = @_;
1849 my $props = ($ra->get_dir($path, $r))[2];
1850 my $src = $props->{'svm:source'};
1851 my $uuid = $props->{'svm:uuid'};
1852 return undef if (!$src || !$uuid);
1856 $uuid =~ m{^[0-9a-f\-]{30,}$}
1857 or die "doesn't look right - svm:uuid is '$uuid'\n";
1859 # the '!' is used to mark the repos_root!/relative/path
1860 $src =~ s{/?!/?}{/};
1861 $src =~ s{/+$}{}; # no trailing slashes please
1862 # username is of no interest
1863 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1865 my $replace = $ra->{url
};
1866 $replace .= "/$path" if length $path;
1868 my $section = "svn-remote.$self->{repo_id}";
1869 tmp_config
("$section.svm-source", $src);
1870 tmp_config
("$section.svm-replace", $replace);
1871 tmp_config
("$section.svm-uuid", $uuid);
1879 my $r = $ra->get_latest_revnum;
1880 my $path = $self->{path
};
1882 while (length $path) {
1883 unless ($tried{"$self->{url}/$path"}) {
1884 return $ra if $self->read_svm_props($ra, $path, $r);
1885 $tried{"$self->{url}/$path"} = 1;
1887 $path =~ s
#/?[^/]+$##;
1889 die "Path: '$path' should be ''\n" if $path ne '';
1890 return $ra if $self->read_svm_props($ra, $path, $r);
1891 $tried{"$self->{url}/$path"} = 1;
1893 if ($ra->{repos_root
} eq $self->{url
}) {
1894 die @err, (map { " $_\n" } keys %tried), "\n";
1897 # nope, make sure we're connected to the repository root:
1900 $path = $ra->{svn_path
};
1901 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1902 while (length $path) {
1903 unless ($tried{"$ra->{url}/$path"}) {
1904 $ok = $self->read_svm_props($ra, $path, $r);
1906 $tried{"$ra->{url}/$path"} = 1;
1908 $path =~ s
#/?[^/]+$##;
1910 die "Path: '$path' should be ''\n" if $path ne '';
1911 $ok ||= $self->read_svm_props($ra, $path, $r);
1912 $tried{"$ra->{url}/$path"} = 1;
1914 die @err, (map { " $_\n" } keys %tried), "\n";
1916 Git
::SVN
::Ra
->new($self->{url
});
1921 return $self->{svnsync
} if $self->{svnsync
};
1923 if ($self->no_metadata) {
1924 die "Can't have both 'noMetadata' and ",
1925 "'useSvnsyncProps' options set!\n";
1927 if ($self->rewrite_root) {
1928 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1933 # see if we have it in our config, first:
1935 my $section = "svn-remote.$self->{repo_id}";
1937 my $url = tmp_config
('--get', "$section.svnsync-url");
1938 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1939 die "doesn't look right - svn:sync-from-url is '$url'\n";
1941 my $uuid = tmp_config
('--get', "$section.svnsync-uuid");
1942 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1943 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1945 $svnsync = { url
=> $url, uuid
=> $uuid }
1947 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1948 return $self->{svnsync
} = $svnsync;
1951 my $err = "useSvnsyncProps set, but failed to read " .
1952 "svnsync property: svn:sync-from-";
1953 my $rp = $self->ra->rev_proplist(0);
1955 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1956 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1957 die "doesn't look right - svn:sync-from-url is '$url'\n";
1959 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1960 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1961 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1963 my $section = "svn-remote.$self->{repo_id}";
1964 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1965 tmp_config
('--add', "$section.svnsync-url", $url);
1966 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1969 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1970 # remote lookup (useful for 'git svn log').
1973 unless ($self->{ra_uuid
}) {
1974 my $key = "svn-remote.$self->{repo_id}.uuid";
1975 my $uuid = eval { tmp_config
('--get', $key) };
1976 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1977 $self->{ra_uuid
} = $uuid;
1979 die "ra_uuid called without URL\n" unless $self->{url
};
1980 $self->{ra_uuid
} = $self->ra->get_uuid;
1981 tmp_config
('--add', $key, $self->{ra_uuid
});
1987 sub _set_repos_root
{
1988 my ($self, $repos_root) = @_;
1989 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1990 $repos_root ||= $self->ra->{repos_root
};
1991 tmp_config
($k, $repos_root);
1997 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1998 eval { tmp_config
('--get', $k) } || $self->_set_repos_root;
2003 my $ra = Git
::SVN
::Ra
->new($self->{url
});
2004 $self->_set_repos_root($ra->{repos_root
});
2005 if ($self->use_svm_props && !$self->{svm
}) {
2006 if ($self->no_metadata) {
2007 die "Can't have both 'noMetadata' and ",
2008 "'useSvmProps' options set!\n";
2009 } elsif ($self->use_svnsync_props) {
2010 die "Can't have both 'useSvnsyncProps' and ",
2011 "'useSvmProps' options set!\n";
2013 $ra = $self->_set_svm_vars($ra);
2014 $self->{-want_revprops
} = 1;
2021 my $repos_root = $self->ra->{repos_root
};
2022 return $self->{path
} if ($self->{url
} eq $repos_root);
2023 my $url = $self->{url
} .
2024 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
2025 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2029 # prop_walk(PATH, REV, SUB)
2030 # -------------------------
2031 # Recursively traverse PATH at revision REV and invoke SUB for each
2032 # directory that contains a SVN property. SUB will be invoked as
2033 # follows: &SUB(gs, path, props); where `gs' is this instance of
2034 # Git::SVN, `path' the path to the directory where the properties
2035 # `props' were found. The `path' will be relative to point of checkout,
2036 # that is, if url://repo/trunk is the current Git branch, and that
2037 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
2038 # as `path' (note the trailing `/').
2040 my ($self, $path, $rev, $sub) = @_;
2043 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2046 # Strip the irrelevant part of the path.
2047 $p =~ s
#^/+\Q$self->{path}\E(/|$)#/#;
2048 # Ensure the path is terminated by a `/'.
2051 # The properties contain all the internal SVN stuff nobody
2052 # (usually) cares about.
2053 my $interesting_props = 0;
2054 foreach (keys %{$props}) {
2055 # If it doesn't start with `svn:', it must be a
2056 # user-defined property.
2057 ++$interesting_props and next if $_ !~ /^svn:/;
2058 # FIXME: Fragile, if SVN adds new public properties,
2059 # this needs to be updated.
2060 ++$interesting_props if /^svn
:(?
:ignore
|keywords
|executable
2061 |eol
-style
|mime
-type
2062 |externals
|needs
-lock)$/x
;
2064 &$sub($self, $p, $props) if $interesting_props;
2066 foreach (sort keys %$dirent) {
2067 next if $dirent->{$_}->{kind
} != $SVN::Node
::dir
;
2068 $self->prop_walk($self->{path
} . $p . $_, $rev, $sub);
2072 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
2073 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
2075 # returns the newest SVN revision number and newest commit SHA1
2076 sub last_rev_commit
{
2078 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
2079 return ($self->{last_rev
}, $self->{last_commit
});
2081 my $c = ::verify_ref
($self->refname.'^0');
2082 if ($c && !$self->use_svm_props && !$self->no_metadata) {
2083 my $rev = (::cmt_metadata
($c))[1];
2085 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
2089 my $map_path = $self->map_path;
2090 unless (-e
$map_path) {
2091 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
2092 return (undef, undef);
2094 my ($rev, $commit) = $self->rev_map_max(1);
2095 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $commit);
2096 return ($rev, $commit);
2099 sub get_fetch_range
{
2100 my ($self, $min, $max) = @_;
2101 $max ||= $self->ra->get_latest_revnum;
2102 $min ||= $self->rev_map_max;
2108 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2109 my $config = "$ENV{GIT_DIR}/svn/.metadata";
2110 if (! -f
$config && -f
$old_def_config) {
2111 rename $old_def_config, $config or
2112 die "Failed rename $old_def_config => $config: $!\n";
2114 my $old_config = $ENV{GIT_CONFIG
};
2115 $ENV{GIT_CONFIG
} = $config;
2118 unless (-f
$config) {
2120 open my $fh, '>', $config or
2121 die "Can't open $config: $!\n";
2122 print $fh "; This file is used internally by ",
2124 "Couldn't write to $config: $!\n";
2125 print $fh "; You should not have to edit it\n" or
2126 die "Couldn't write to $config: $!\n";
2127 close $fh or die "Couldn't close $config: $!\n";
2129 command
('config', @args);
2132 if (defined $old_config) {
2133 $ENV{GIT_CONFIG
} = $old_config;
2135 delete $ENV{GIT_CONFIG
};
2138 wantarray ?
@ret : $ret[0];
2142 my ($self, $sub) = @_;
2143 my $old_index = $ENV{GIT_INDEX_FILE
};
2144 $ENV{GIT_INDEX_FILE
} = $self->{index};
2147 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
2148 mkpath
([$dir]) unless -d
$dir;
2152 if (defined $old_index) {
2153 $ENV{GIT_INDEX_FILE
} = $old_index;
2155 delete $ENV{GIT_INDEX_FILE
};
2158 wantarray ?
@ret : $ret[0];
2161 sub assert_index_clean
{
2162 my ($self, $treeish) = @_;
2164 $self->tmp_index_do(sub {
2165 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
2166 my $x = command_oneline
('write-tree');
2167 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
2168 /^tree ($::sha1)/mo);
2171 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2172 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2173 command_noisy
('read-tree', $treeish);
2174 $x = command_oneline
('write-tree');
2176 ::fatal
"trees ($treeish) $y != $x\n",
2177 "Something is seriously wrong...";
2182 sub get_commit_parents
{
2183 my ($self, $log_entry) = @_;
2184 my (%seen, @ret, @tmp);
2185 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2186 if (my $ip = $self->{inject_parents
}) {
2187 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
2191 if (my $cur = ::verify_ref
($self->refname.'^0')) {
2194 if (my $ipd = $self->{inject_parents_dcommit
}) {
2195 if (my $commit = delete $ipd->{$log_entry->{revision
}}) {
2196 push @tmp, @
$commit;
2199 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
2200 while (my $p = shift @tmp) {
2204 # MAXPARENT is defined to 16 in commit-tree.c:
2208 die "r$log_entry->{revision}: No room for parents:\n\t",
2209 join("\n\t", @tmp), "\n";
2216 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
2217 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2218 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
2221 if ($rwr !~ m
#^[a-z\+]+://#) {
2222 die "$rwr is not a valid URL (key: $k)\n";
2225 $self->{-rewrite_root
} = $rwr;
2230 ($self->rewrite_root || $self->{url
}) .
2231 (length $self->{path
} ?
'/' . $self->{path
} : '');
2236 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
2240 sub set_commit_header_env
{
2241 my ($log_entry) = @_;
2243 foreach my $ned (qw
/NAME EMAIL DATE/) {
2244 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2245 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2249 $ENV{GIT_AUTHOR_NAME
} = $log_entry->{name
};
2250 $ENV{GIT_AUTHOR_EMAIL
} = $log_entry->{email
};
2251 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
2253 $ENV{GIT_COMMITTER_NAME
} = (defined $log_entry->{commit_name
})
2254 ?
$log_entry->{commit_name
}
2255 : $log_entry->{name
};
2256 $ENV{GIT_COMMITTER_EMAIL
} = (defined $log_entry->{commit_email
})
2257 ?
$log_entry->{commit_email
}
2258 : $log_entry->{email
};
2262 sub restore_commit_header_env
{
2264 foreach my $ned (qw
/NAME EMAIL DATE/) {
2265 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2266 my $k = "GIT_${ac}_${ned}";
2267 if (defined $env->{$k}) {
2268 $ENV{$k} = $env->{$k};
2277 command_noisy
('gc', '--auto');
2281 my ($self, $log_entry) = @_;
2282 my $lr = $self->last_rev;
2283 if (defined $lr && $lr >= $log_entry->{revision
}) {
2284 die "Last fetched revision of ", $self->refname,
2285 " was r$lr, but we are about to fetch: ",
2286 "r$log_entry->{revision}!\n";
2288 if (my $c = $self->rev_map_get($log_entry->{revision
})) {
2289 croak
"$log_entry->{revision} = $c already exists! ",
2290 "Why are we refetching it?\n";
2292 my $old_env = set_commit_header_env
($log_entry);
2293 my $tree = $log_entry->{tree
};
2294 if (!defined $tree) {
2295 $tree = $self->tmp_index_do(sub {
2296 command_oneline
('write-tree') });
2298 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2300 my @exec = ('git', 'commit-tree', $tree);
2301 foreach ($self->get_commit_parents($log_entry)) {
2302 push @exec, '-p', $_;
2304 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2308 # we always get UTF-8 from SVN, but we may want our commits in
2309 # a different encoding.
2310 if (my $enc = Git
::config
('i18n.commitencoding')) {
2312 Encode
::from_to
($log_entry->{log}, 'UTF-8', $enc);
2314 print $msg_fh $log_entry->{log} or croak
$!;
2315 restore_commit_header_env
($old_env);
2316 unless ($self->no_metadata) {
2317 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2320 $msg_fh->flush == 0 or croak
$!;
2321 close $msg_fh or croak
$!;
2322 chomp(my $commit = do { local $/; <$out_fh> });
2323 close $out_fh or croak
$!;
2326 if ($commit !~ /^$::sha1$/o) {
2327 die "Failed to commit, invalid sha1: $commit\n";
2330 $self->rev_map_set($log_entry->{revision
}, $commit, 1);
2332 $self->{last_rev
} = $log_entry->{revision
};
2333 $self->{last_commit
} = $commit;
2334 print "r$log_entry->{revision}";
2335 if (defined $log_entry->{svm_revision
}) {
2336 print " (\@$log_entry->{svm_revision})";
2337 $self->rev_map_set($log_entry->{svm_revision
}, $commit,
2338 0, $self->svm_uuid);
2340 print " = $commit ($self->{ref_id})\n";
2341 if (--$_gc_nr == 0) {
2342 $_gc_nr = $_gc_period;
2349 my ($self, $paths, $r) = @_;
2350 return 1 if $self->{path
} eq '';
2351 if (my $path = $paths->{"/$self->{path}"}) {
2352 return ($path->{action
} eq 'D') ?
0 : 1;
2354 my $repos_root = $self->ra->{repos_root
};
2355 my $extended_path = $self->{url
} . '/' . $self->{path
};
2356 $extended_path =~ s
#^\Q$repos_root\E(/|$)##;
2357 $self->{path_regex
} ||= qr/^\/\Q
$extended_path\E\
//;
2358 if (grep /$self->{path_regex}/, keys %$paths) {
2362 foreach (split m
#/#, $self->{path}) {
2364 next unless ($paths->{$c} &&
2365 ($paths->{$c}->{action
} =~ /^[AR]$/));
2366 if ($self->ra->check_path($self->{path
}, $r) ==
2374 sub find_parent_branch
{
2375 my ($self, $paths, $rev) = @_;
2376 return undef unless $self->follow_parent;
2377 unless (defined $paths) {
2378 my $err_handler = $SVN::Error
::handler
;
2379 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
2380 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
2382 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
2383 $SVN::Error
::handler
= $err_handler;
2385 return undef unless defined $paths;
2387 # look for a parent from another branch:
2388 my @b_path_components = split m
#/#, $self->rel_path;
2389 my @a_path_components;
2391 while (@b_path_components) {
2392 $i = $paths->{'/'.join('/', @b_path_components)};
2393 last if $i && defined $i->{copyfrom_path
};
2394 unshift(@a_path_components, pop(@b_path_components));
2396 return undef unless defined $i && defined $i->{copyfrom_path
};
2397 my $branch_from = $i->{copyfrom_path
};
2398 if (@a_path_components) {
2399 print STDERR
"branch_from: $branch_from => ";
2400 $branch_from .= '/'.join('/', @a_path_components);
2401 print STDERR
$branch_from, "\n";
2403 my $r = $i->{copyfrom_rev
};
2404 my $repos_root = $self->ra->{repos_root
};
2405 my $url = $self->ra->{url
};
2406 my $new_url = $repos_root . $branch_from;
2407 print STDERR
"Found possible branch point: ",
2408 "$new_url => ", $self->full_url, ", $r\n";
2409 $branch_from =~ s
#^/##;
2410 my $gs = $self->other_gs($new_url, $url, $repos_root,
2411 $branch_from, $r, $self->{ref_id
});
2412 my ($r0, $parent) = $gs->find_rev_before($r, 1);
2415 if (!defined $r0 || !defined $parent) {
2416 ($base, $head) = parse_revision_argument
(0, $r);
2419 $gs->ra->get_log([$gs->{path
}], $r0 + 1, $r, 1,
2420 0, 1, sub { $base = $_[1] - 1 });
2423 if (defined $base && $base <= $r) {
2424 $gs->fetch($base, $r);
2426 ($r0, $parent) = $gs->find_rev_before($r, 1);
2428 if (defined $r0 && defined $parent) {
2429 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
2431 if ($self->ra->can_do_switch) {
2432 $self->assert_index_clean($parent);
2433 print STDERR
"Following parent with do_switch\n";
2434 # do_switch works with svn/trunk >= r22312, but that
2435 # is not included with SVN 1.4.3 (the latest version
2436 # at the moment), so we can't rely on it
2437 $self->{last_rev
} = $r0;
2438 $self->{last_commit
} = $parent;
2439 $ed = SVN
::Git
::Fetcher
->new($self, $gs->{path
});
2440 $gs->ra->gs_do_switch($r0, $rev, $gs,
2441 $self->full_url, $ed)
2442 or die "SVN connection failed somewhere...\n";
2443 } elsif ($self->ra->trees_match($new_url, $r0,
2444 $self->full_url, $rev)) {
2445 print STDERR
"Trees match:\n",
2447 " ${\$self->full_url}\@$rev\n",
2448 "Following parent with no changes\n";
2449 $self->tmp_index_do(sub {
2450 command_noisy
('read-tree', $parent);
2452 $self->{last_commit
} = $parent;
2454 print STDERR
"Following parent with do_update\n";
2455 $ed = SVN
::Git
::Fetcher
->new($self);
2456 $self->ra->gs_do_update($rev, $rev, $self, $ed)
2457 or die "SVN connection failed somewhere...\n";
2459 print STDERR
"Successfully followed parent\n";
2460 return $self->make_log_entry($rev, [$parent], $ed);
2466 my ($self, $paths, $rev) = @_;
2468 my ($last_rev, @parents);
2469 if (my $lc = $self->last_commit) {
2470 # we can have a branch that was deleted, then re-added
2471 # under the same name but copied from another path, in
2472 # which case we'll have multiple parents (we don't
2473 # want to break the original ref, nor lose copypath info):
2474 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2475 push @
{$log_entry->{parents
}}, $lc;
2478 $ed = SVN
::Git
::Fetcher
->new($self);
2479 $last_rev = $self->{last_rev
};
2484 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2487 $ed = SVN
::Git
::Fetcher
->new($self);
2489 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
2490 die "SVN connection failed somewhere...\n";
2492 $self->make_log_entry($rev, \
@parents, $ed);
2496 my ($self, $ed) = @_;
2498 my $h = $ed->{empty
};
2499 foreach (sort keys %$h) {
2500 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2501 push @out, " $act: " . uri_encode
($_);
2502 warn "W: $act: $_\n";
2504 foreach my $t (qw
/dir_prop file_prop/) {
2505 $h = $ed->{$t} or next;
2506 foreach my $path (sort keys %$h) {
2507 my $ppath = $path eq '' ?
'.' : $path;
2508 foreach my $prop (sort keys %{$h->{$path}}) {
2509 next if $SKIP_PROP{$prop};
2510 my $v = $h->{$path}->{$prop};
2511 my $t_ppath_prop = "$t: " .
2512 uri_encode
($ppath) . ' ' .
2515 push @out, " +$t_ppath_prop " .
2518 push @out, " -$t_ppath_prop";
2523 foreach my $t (qw
/absent_file absent_directory/) {
2524 $h = $ed->{$t} or next;
2525 foreach my $parent (sort keys %$h) {
2526 foreach my $path (sort @
{$h->{$parent}}) {
2527 push @out, " $t: " .
2528 uri_encode
("$parent/$path");
2529 warn "W: $t: $parent/$path ",
2530 "Insufficient permissions?\n";
2537 # parse_svn_date(DATE)
2538 # --------------------
2539 # Given a date (in UTC) from Subversion, return a string in the format
2540 # "<TZ Offset> <local date/time>" that Git will use.
2542 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
2543 # is true we'll convert it to the local timezone instead.
2544 sub parse_svn_date
{
2545 my $date = shift || return '+0000 1970-01-01 00:00:00';
2546 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2547 (\d\d
)\
:(\d\d
)\
:(\d\d
)\
.\d
*Z
$/x
) or
2548 croak
"Unable to parse date: $date\n";
2549 my $parsed_date; # Set next.
2551 if ($Git::SVN
::_localtime
) {
2552 # Translate the Subversion datetime to an epoch time.
2553 # Begin by switching ourselves to $date's timezone, UTC.
2554 my $old_env_TZ = $ENV{TZ
};
2558 POSIX
::strftime
('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2560 # Determine our local timezone (including DST) at the
2561 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2562 # value of TZ, if any, at the time we were run.
2563 if (defined $Git::SVN
::Log
::TZ
) {
2564 $ENV{TZ
} = $Git::SVN
::Log
::TZ
;
2570 POSIX
::strftime
('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2572 # This converts $epoch_in_UTC into our local timezone.
2573 my ($sec, $min, $hour, $mday, $mon, $year,
2574 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2576 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2577 $our_TZ, $year + 1900, $mon + 1,
2578 $mday, $hour, $min, $sec);
2580 # Reset us to the timezone in effect when we entered
2582 if (defined $old_env_TZ) {
2583 $ENV{TZ
} = $old_env_TZ;
2588 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2591 return $parsed_date;
2595 my ($self, $new_url, $url, $repos_root,
2596 $branch_from, $r, $old_ref_id) = @_;
2597 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
2599 my $ref_id = $old_ref_id;
2600 $ref_id =~ s/\@\d+$//;
2602 # just grow a tail if we're not unique enough :x
2603 $ref_id .= '-' while find_ref
($ref_id);
2604 print STDERR
"Initializing parent: $ref_id\n";
2605 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2606 if ($u =~ s
#^\Q$url\E(/|$)##) {
2609 $repo_id = $self->{repo_id
};
2611 $gs = Git
::SVN
->init($u, $p, $repo_id, $ref_id, 1);
2618 if (!defined $author || length $author == 0) {
2619 $author = '(no author)';
2620 } elsif (defined $::_authors
&& ! defined $::users
{$author}) {
2621 die "Author: $author not defined in $::_authors file\n";
2626 sub make_log_entry
{
2627 my ($self, $rev, $parents, $ed) = @_;
2628 my $untracked = $self->get_untracked($ed);
2630 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
2631 print $un "r$rev\n" or croak
$!;
2632 print $un $_, "\n" foreach @
$untracked;
2633 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
2637 my $logged = delete $self->{logged_rev_props
};
2638 if (!$logged || $self->{-want_revprops
}) {
2639 my $rp = $self->ra->rev_proplist($rev);
2640 foreach (sort keys %$rp) {
2642 if (/^svn:(author|date|log)$/) {
2643 $log_entry{$1} = $v;
2644 } elsif ($_ eq 'svm:headrev') {
2647 print $un " rev_prop: ", uri_encode
($_), ' ',
2648 uri_encode
($v), "\n";
2652 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
2654 close $un or croak
$!;
2656 $log_entry{date
} = parse_svn_date
($log_entry{date
});
2657 $log_entry{log} .= "\n";
2658 my $author = $log_entry{author
} = check_author
($log_entry{author
});
2659 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
2662 my ($commit_name, $commit_email) = ($name, $email);
2663 if ($_use_log_author) {
2665 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2667 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2670 if (!defined $name_field) {
2671 if (!defined $email) {
2674 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
2675 ($name, $email) = ($1, $2);
2676 } elsif ($name_field =~ /(.*)@/) {
2677 ($name, $email) = ($1, $name_field);
2679 ($name, $email) = ($name_field, $name_field);
2682 if (defined $headrev && $self->use_svm_props) {
2683 if ($self->rewrite_root) {
2684 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2687 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
2688 # we don't want "SVM: initializing mirror for junk" ...
2689 return undef if $r == 0;
2690 my $svm = $self->svm;
2691 if ($uuid ne $svm->{uuid
}) {
2692 die "UUID mismatch on SVM path:\n",
2693 "expected: $svm->{uuid}\n",
2696 my $full_url = $self->full_url;
2697 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2698 die "Failed to replace '$svm->{replace}' with ",
2699 "'$svm->{source}' in $full_url\n";
2700 # throw away username for storing in records
2701 remove_username
($full_url);
2702 $log_entry{metadata
} = "$full_url\@$r $uuid";
2703 $log_entry{svm_revision
} = $r;
2704 $email ||= "$author\@$uuid";
2705 $commit_email ||= "$author\@$uuid";
2706 } elsif ($self->use_svnsync_props) {
2707 my $full_url = $self->svnsync->{url
};
2708 $full_url .= "/$self->{path}" if length $self->{path
};
2709 remove_username
($full_url);
2710 my $uuid = $self->svnsync->{uuid
};
2711 $log_entry{metadata
} = "$full_url\@$rev $uuid";
2712 $email ||= "$author\@$uuid";
2713 $commit_email ||= "$author\@$uuid";
2715 my $url = $self->metadata_url;
2716 remove_username
($url);
2717 $log_entry{metadata
} = "$url\@$rev " .
2718 $self->ra->get_uuid;
2719 $email ||= "$author\@" . $self->ra->get_uuid;
2720 $commit_email ||= "$author\@" . $self->ra->get_uuid;
2722 $log_entry{name
} = $name;
2723 $log_entry{email
} = $email;
2724 $log_entry{commit_name
} = $commit_name;
2725 $log_entry{commit_email
} = $commit_email;
2730 my ($self, $min_rev, $max_rev, @parents) = @_;
2731 my ($last_rev, $last_commit) = $self->last_rev_commit;
2732 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
2733 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
2737 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2738 $self->{inject_parents
} = { $rev => $tree };
2739 $self->fetch(undef, undef);
2743 my ($self, $tree) = (shift, shift);
2744 my $log_entry = ::get_commit_entry
($tree);
2745 unless ($self->{last_rev
}) {
2746 ::fatal
("Must have an existing revision to commit");
2748 my %ed_opts = ( r
=> $self->{last_rev
},
2749 log => $log_entry->{log},
2751 tree_a
=> $self->{last_commit
},
2754 $self->set_tree_cb($log_entry, $tree, @_) },
2755 svn_path
=> $self->{path
} );
2756 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
2757 print "No changes\nr$self->{last_rev} = $tree\n";
2761 sub rebuild_from_rev_db
{
2762 my ($self, $path) = @_;
2764 open my $fh, '<', $path or croak
"open: $!";
2765 binmode $fh or croak
"binmode: $!";
2767 length($_) == 41 or croak
"inconsistent size in ($_) != 41";
2770 next if $_ eq ('0' x
40);
2771 $self->rev_map_set($r, $_);
2774 close $fh or croak
"close: $!";
2775 unlink $path or croak
"unlink: $!";
2780 my $map_path = $self->map_path;
2781 my $partial = (-e
$map_path && ! -z
$map_path);
2782 return unless ::verify_ref
($self->refname.'^0');
2783 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
2784 my $rev_db = $self->rev_db_path;
2785 $self->rebuild_from_rev_db($rev_db);
2786 if ($self->use_svm_props) {
2787 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2788 $self->rebuild_from_rev_db($svm_rev_db);
2790 $self->unlink_rev_db_symlink;
2793 print "Rebuilding $map_path ...\n" if (!$partial);
2794 my ($base_rev, $head) = ($partial ?
$self->rev_map_max_norebuild(1) :
2797 command_output_pipe
(qw
/rev-list --pretty=raw --no-color --reverse/,
2798 ($head ?
"$head.." : "") . $self->refname,
2800 my $metadata_url = $self->metadata_url;
2801 remove_username
($metadata_url);
2802 my $svn_uuid = $self->ra_uuid;
2805 if ( m{^commit ($::sha1)$} ) {
2809 next unless s{^\s*(git-svn-id:)}{$1};
2810 my ($url, $rev, $uuid) = ::extract_metadata
($_);
2811 remove_username
($url);
2813 # ignore merges (from set-tree)
2814 next if (!defined $rev || !$uuid);
2816 # if we merged or otherwise started elsewhere, this is
2817 # how we break out of it
2818 if (($uuid ne $svn_uuid) ||
2819 ($metadata_url && $url && ($url ne $metadata_url))) {
2822 if ($partial && $head) {
2823 print "Partial-rebuilding $map_path ...\n";
2824 print "Currently at $base_rev = $head\n";
2828 $self->rev_map_set($rev, $c);
2829 print "r$rev = $c\n";
2831 command_close_pipe
($log, $ctx);
2832 print "Done rebuilding $map_path\n" if (!$partial || !$head);
2833 my $rev_db_path = $self->rev_db_path;
2834 if (-f
$self->rev_db_path) {
2835 unlink $self->rev_db_path or croak
"unlink: $!";
2837 $self->unlink_rev_db_symlink;
2841 # Tie::File seems to be prone to offset errors if revisions get sparse,
2842 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2843 # one of my favorite modules is out :< Next up would be one of the DBM
2844 # modules, but I'm not sure which is most portable...
2846 # This is the replacement for the rev_db format, which was too big
2847 # and inefficient for large repositories with a lot of sparse history
2850 # The format is this:
2851 # - 24 bytes for every record,
2852 # * 4 bytes for the integer representing an SVN revision number
2853 # * 20 bytes representing the sha1 of a git commit
2854 # - No empty padding records like the old format
2855 # (except the last record, which can be overwritten)
2856 # - new records are written append-only since SVN revision numbers
2857 # increase monotonically
2858 # - lookups on SVN revision number are done via a binary search
2859 # - Piping the file to xxd -c24 is a good way of dumping it for
2860 # viewing or editing (piped back through xxd -r), should the need
2862 # - The last record can be padding revision with an all-zero sha1
2863 # This is used to optimize fetch performance when using multiple
2864 # "fetch" directives in .git/config
2866 # These files are disposable unless noMetadata or useSvmProps is set
2869 my ($fh, $rev, $commit) = @_;
2871 binmode $fh or croak
"binmode: $!";
2872 my $size = (stat($fh))[7];
2873 ($size % 24) == 0 or croak
"inconsistent size: $size";
2877 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
2878 my $read = sysread($fh, my $buf, 24) or croak
"read: $!";
2879 $read == 24 or croak
"read only $read bytes (!= 24)";
2880 my ($last_rev, $last_commit) = unpack(rev_map_fmt
, $buf);
2881 if ($last_commit eq ('0' x40
)) {
2883 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
2884 $read = sysread($fh, $buf, 24) or
2887 croak
"read only $read bytes (!= 24)";
2888 ($last_rev, $last_commit) =
2889 unpack(rev_map_fmt
, $buf);
2890 if ($last_commit eq ('0' x40
)) {
2891 croak
"inconsistent .rev_map\n";
2894 if ($last_rev >= $rev) {
2895 croak
"last_rev is higher!: $last_rev >= $rev";
2900 sysseek($fh, $wr_offset, SEEK_END
) or croak
"seek: $!";
2901 syswrite($fh, pack(rev_map_fmt
, $rev, $commit), 24) == 24 or
2908 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2909 mkpath
([$dir]) unless -d
$dir;
2910 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2911 close $fh or die "Couldn't close (create) $path: $!\n";
2916 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2917 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2918 my $db = $self->map_path($uuid);
2919 my $db_lock = "$db.lock";
2922 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2923 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
2927 $LOCKFILES{$db_lock} = 1;
2929 # both of these options make our .rev_db file very, very important
2930 # and we can't afford to lose it because rebuild() won't work
2931 if ($self->use_svm_props || $self->no_metadata) {
2933 copy
($db, $db_lock) or die "rev_map_set(@_): ",
2935 "$db => $db_lock ($!)\n";
2937 rename $db, $db_lock or die "rev_map_set(@_): ",
2938 "Failed to rename: ",
2939 "$db => $db_lock ($!)\n";
2942 sysopen(my $fh, $db_lock, O_RDWR
| O_CREAT
)
2943 or croak
"Couldn't open $db_lock: $!\n";
2944 _rev_map_set
($fh, $rev, $commit);
2946 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2947 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2949 close $fh or croak
$!;
2952 command_noisy
('update-ref', '-m', "r$rev",
2953 $self->refname, $commit);
2955 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
2956 "$db_lock => $db ($!)\n";
2957 delete $LOCKFILES{$db_lock};
2959 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2960 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2961 kill $sig, $$ if defined $sig;
2965 # If want_commit, this will return an array of (rev, commit) where
2966 # commit _must_ be a valid commit in the archive.
2967 # Otherwise, it'll return the max revision (whether or not the
2968 # commit is valid or just a 0x40 placeholder).
2970 my ($self, $want_commit) = @_;
2972 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2973 $want_commit ?
($r, $c) : $r;
2976 sub rev_map_max_norebuild
{
2977 my ($self, $want_commit) = @_;
2978 my $map_path = $self->map_path;
2979 stat $map_path or return $want_commit ?
(0, undef) : 0;
2980 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
2981 binmode $fh or croak
"binmode: $!";
2982 my $size = (stat($fh))[7];
2983 ($size % 24) == 0 or croak
"inconsistent size: $size";
2986 close $fh or croak
"close: $!";
2987 return $want_commit ?
(0, undef) : 0;
2990 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
2991 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
2992 my ($r, $c) = unpack(rev_map_fmt
, $buf);
2993 if ($want_commit && $c eq ('0' x40
)) {
2995 return $want_commit ?
(0, undef) : 0;
2997 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
2998 sysread($fh, $buf, 24) == 24 or croak
"read: $!";
2999 ($r, $c) = unpack(rev_map_fmt
, $buf);
3000 if ($c eq ('0'x40
)) {
3001 croak
"Penultimate record is all-zeroes in $map_path";
3004 close $fh or croak
"close: $!";
3005 $want_commit ?
($r, $c) : $r;
3009 my ($self, $rev, $uuid) = @_;
3010 my $map_path = $self->map_path($uuid);
3011 return undef unless -e
$map_path;
3013 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
3014 binmode $fh or croak
"binmode: $!";
3015 my $size = (stat($fh))[7];
3016 ($size % 24) == 0 or croak
"inconsistent size: $size";
3019 close $fh or croak
"close: $fh";
3023 my ($l, $u) = (0, $size - 24);
3027 my $i = int(($l/24 + $u/24) / 2) * 24;
3028 sysseek($fh, $i, SEEK_SET
) or croak
"seek: $!";
3029 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
3030 my ($r, $c) = unpack('NH40', $buf);
3034 } elsif ($r > $rev) {
3036 } else { # $r == $rev
3037 close($fh) or croak
"close: $!";
3038 return $c eq ('0' x
40) ?
undef : $c;
3041 close($fh) or croak
"close: $!";
3045 # Finds the first svn revision that exists on (if $eq_ok is true) or
3046 # before $rev for the current branch. It will not search any lower
3047 # than $min_rev. Returns the git commit hash and svn revision number
3048 # if found, else (undef, undef).
3049 sub find_rev_before
{
3050 my ($self, $rev, $eq_ok, $min_rev) = @_;
3051 --$rev unless $eq_ok;
3053 while ($rev >= $min_rev) {
3054 if (my $c = $self->rev_map_get($rev)) {
3059 return (undef, undef);
3062 # Finds the first svn revision that exists on (if $eq_ok is true) or
3063 # after $rev for the current branch. It will not search any higher
3064 # than $max_rev. Returns the git commit hash and svn revision number
3065 # if found, else (undef, undef).
3066 sub find_rev_after
{
3067 my ($self, $rev, $eq_ok, $max_rev) = @_;
3068 ++$rev unless $eq_ok;
3069 $max_rev ||= $self->rev_map_max;
3070 while ($rev <= $max_rev) {
3071 if (my $c = $self->rev_map_get($rev)) {
3076 return (undef, undef);
3080 my ($class, $repo_id, $ref_id, $path) = @_;
3081 unless (defined $repo_id && length $repo_id) {
3082 $repo_id = $Git::SVN
::default_repo_id
;
3084 unless (defined $ref_id && length $ref_id) {
3085 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
3088 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3089 $_[3] = $path = '' unless (defined $path);
3090 mkpath
(["$ENV{GIT_DIR}/svn"]);
3092 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
3093 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
3094 map_root
=> "$dir/.rev_map", repo_id
=> $repo_id }, $class;
3097 # for read-only access of old .rev_db formats
3098 sub unlink_rev_db_symlink
{
3100 my $link = $self->rev_db_path;
3101 $link =~ s/\.[\w-]+$// or croak
"missing UUID at the end of $link";
3103 unlink $link or croak
"unlink: $link failed!";
3108 my ($self, $uuid) = @_;
3109 my $db_path = $self->map_path($uuid);
3110 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3111 or croak
"map_path: $db_path does not contain '/.rev_map.' !";
3115 # the new replacement for .rev_db
3117 my ($self, $uuid) = @_;
3118 $uuid ||= $self->ra_uuid;
3119 "$self->{map_root}.$uuid";
3124 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3128 sub remove_username
{
3129 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3132 package Git
::SVN
::Prompt
;
3136 use vars qw
/$_no_auth_cache $_username/;
3139 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3140 $may_save = undef if $_no_auth_cache;
3141 $default_username = $_username if defined $_username;
3142 if (defined $default_username && length $default_username) {
3143 if (defined $realm && length $realm) {
3144 print STDERR
"Authentication realm: $realm\n";
3147 $cred->username($default_username);
3149 username
($cred, $realm, $may_save, $pool);
3151 $cred->password(_read_password
("Password for '" .
3152 $cred->username . "': ", $realm));
3153 $cred->may_save($may_save);
3154 $SVN::_Core
::SVN_NO_ERROR
;
3157 sub ssl_server_trust
{
3158 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3159 $may_save = undef if $_no_auth_cache;
3160 print STDERR
"Error validating server certificate for '$realm':\n";
3163 # All variables SVN::Auth::SSL::* are used only once,
3164 # so we're shutting up Perl warnings about this.
3165 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
3166 print STDERR
" - The certificate is not issued ",
3167 "by a trusted authority. Use the\n",
3168 " fingerprint to validate ",
3169 "the certificate manually!\n";
3171 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
3172 print STDERR
" - The certificate hostname ",
3173 "does not match.\n";
3175 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
3176 print STDERR
" - The certificate is not yet valid.\n";
3178 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
3179 print STDERR
" - The certificate has expired.\n";
3181 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
3182 print STDERR
" - The certificate has ",
3183 "an unknown error.\n";
3185 } # no warnings 'once'
3187 "Certificate information:\n".
3188 " - Hostname: %s\n".
3189 " - Valid: from %s until %s\n".
3191 " - Fingerprint: %s\n",
3192 map $cert_info->$_, qw(hostname valid_from valid_until
3193 issuer_dname fingerprint);
3196 print STDERR
$may_save ?
3197 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3198 "(R)eject or accept (t)emporarily? ";
3200 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
3201 if ($choice =~ /^t$/i) {
3202 $cred->may_save(undef);
3203 } elsif ($choice =~ /^r$/i) {
3205 } elsif ($may_save && $choice =~ /^p$/i) {
3206 $cred->may_save($may_save);
3210 $cred->accepted_failures($failures);
3211 $SVN::_Core
::SVN_NO_ERROR
;
3214 sub ssl_client_cert
{
3215 my ($cred, $realm, $may_save, $pool) = @_;
3216 $may_save = undef if $_no_auth_cache;
3217 print STDERR
"Client certificate filename: ";
3219 chomp(my $filename = <STDIN
>);
3220 $cred->cert_file($filename);
3221 $cred->may_save($may_save);
3222 $SVN::_Core
::SVN_NO_ERROR
;
3225 sub ssl_client_cert_pw
{
3226 my ($cred, $realm, $may_save, $pool) = @_;
3227 $may_save = undef if $_no_auth_cache;
3228 $cred->password(_read_password
("Password: ", $realm));
3229 $cred->may_save($may_save);
3230 $SVN::_Core
::SVN_NO_ERROR
;
3234 my ($cred, $realm, $may_save, $pool) = @_;
3235 $may_save = undef if $_no_auth_cache;
3236 if (defined $realm && length $realm) {
3237 print STDERR
"Authentication realm: $realm\n";
3240 if (defined $_username) {
3241 $username = $_username;
3243 print STDERR
"Username: ";
3245 chomp($username = <STDIN
>);
3247 $cred->username($username);
3248 $cred->may_save($may_save);
3249 $SVN::_Core
::SVN_NO_ERROR
;
3252 sub _read_password
{
3253 my ($prompt, $realm) = @_;
3254 print STDERR
$prompt;
3256 require Term
::ReadKey
;
3257 Term
::ReadKey
::ReadMode
('noecho');
3259 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
3260 last if $key =~ /[\012\015]/; # \n\r
3263 Term
::ReadKey
::ReadMode
('restore');
3269 package SVN
::Git
::Fetcher
;
3274 use File
::Temp qw
/tempfile/;
3276 use vars qw
/$_ignore_regex/;
3278 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
3280 my ($class, $git_svn, $switch_path) = @_;
3281 my $self = SVN
::Delta
::Editor
->new;
3282 bless $self, $class;
3283 if (exists $git_svn->{last_commit
}) {
3284 $self->{c
} = $git_svn->{last_commit
};
3285 $self->{empty_symlinks
} =
3286 _mark_empty_symlinks
($git_svn, $switch_path);
3288 $self->{empty
} = {};
3289 $self->{dir_prop
} = {};
3290 $self->{file_prop
} = {};
3291 $self->{absent_dir
} = {};
3292 $self->{absent_file
} = {};
3293 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
3297 # this uses the Ra object, so it must be called before do_{switch,update},
3298 # not inside them (when the Git::SVN::Fetcher object is passed) to
3299 # do_{switch,update}
3300 sub _mark_empty_symlinks
{
3301 my ($git_svn, $switch_path) = @_;
3302 my $bool = Git
::config_bool
('svn.brokenSymlinkWorkaround');
3303 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
3306 my ($rev, $cmt) = $git_svn->last_rev_commit;
3307 return {} unless ($rev && $cmt);
3309 # allow the warning to be printed for each revision we fetch to
3310 # ensure the user sees it. The user can also disable the workaround
3311 # on the repository even while git svn is running and the next
3312 # revision fetched will skip this expensive function.
3313 my $printed_warning;
3314 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3315 my ($ls, $ctx) = command_output_pipe
(qw
/ls-tree -r -z/, $cmt);
3317 my $pfx = defined($switch_path) ?
$switch_path : $git_svn->{path
};
3318 $pfx .= '/' if length($pfx);
3321 s/\A100644 blob $empty_blob\t//o or next;
3322 unless ($printed_warning) {
3323 print STDERR
"Scanning for empty symlinks, ",
3324 "this may take a while if you have ",
3325 "many empty files\n",
3326 "You may disable this with `",
3327 "git config svn.brokenSymlinkWorkaround ",
3329 "This may be done in a different ",
3330 "terminal without restarting ",
3332 $printed_warning = 1;
3335 my (undef, $props) =
3336 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3337 if ($props->{'svn:special'}) {
3341 command_close_pipe
($ls, $ctx);
3345 # returns true if a given path is inside a ".git" directory
3347 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3350 # return value: 0 -- don't ignore, 1 -- ignore
3351 sub is_path_ignored
{
3353 return 1 if in_dot_git
($path);
3354 return 0 unless defined($_ignore_regex);
3355 return 1 if $path =~ m!$_ignore_regex!o;
3359 sub set_path_strip
{
3360 my ($self, $path) = @_;
3361 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
3368 sub open_directory
{
3369 my ($self, $path, $pb, $rev) = @_;
3374 my ($self, $path) = @_;
3375 if ($self->{path_strip
}) {
3376 $path =~ s!$self->{path_strip}!! or
3377 die "Failed to strip path '$path' ($self->{path_strip})\n";
3383 my ($self, $path, $rev, $pb) = @_;
3384 return undef if is_path_ignored
($path);
3386 my $gpath = $self->git_path($path);
3387 return undef if ($gpath eq '');
3389 # remove entire directories.
3390 my ($tree) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3391 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3393 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3399 my $rmpath = "$gpath/$_";
3400 $self->{gii
}->remove($rmpath);
3401 print "\tD\t$rmpath\n" unless $::_q
;
3403 print "\tD\t$gpath/\n" unless $::_q
;
3404 command_close_pipe
($ls, $ctx);
3405 $self->{empty
}->{$path} = 0
3407 $self->{gii
}->remove($gpath);
3408 print "\tD\t$gpath\n" unless $::_q
;
3414 my ($self, $path, $pb, $rev) = @_;
3417 goto out
if is_path_ignored
($path);
3419 my $gpath = $self->git_path($path);
3420 ($mode, $blob) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3421 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
3422 unless (defined $mode && defined $blob) {
3423 die "$path was not found in commit $self->{c} (r$rev)\n";
3425 if ($mode eq '100644' && $self->{empty_symlinks
}->{$path}) {
3429 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
3430 pool
=> SVN
::Pool
->new, action
=> 'M' };
3434 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3437 if (!is_path_ignored
($path)) {
3438 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3439 delete $self->{empty
}->{$dir};
3442 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode,
3443 pool
=> SVN
::Pool
->new, action
=> 'A' };
3447 my ($self, $path, $cp_path, $cp_rev) = @_;
3448 goto out
if is_path_ignored
($path);
3449 my $gpath = $self->git_path($path);
3451 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3457 $self->{gii
}->remove($_);
3458 print "\tD\t$_\n" unless $::_q
;
3460 command_close_pipe
($ls, $ctx);
3461 $self->{empty
}->{$path} = 0;
3463 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3464 delete $self->{empty
}->{$dir};
3465 $self->{empty
}->{$path} = 1;
3470 sub change_dir_prop
{
3471 my ($self, $db, $prop, $value) = @_;
3472 return undef if is_path_ignored
($db->{path
});
3473 $self->{dir_prop
}->{$db->{path
}} ||= {};
3474 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
3478 sub absent_directory
{
3479 my ($self, $path, $pb) = @_;
3480 return undef if is_path_ignored
($path);
3481 $self->{absent_dir
}->{$pb->{path
}} ||= [];
3482 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
3487 my ($self, $path, $pb) = @_;
3488 return undef if is_path_ignored
($path);
3489 $self->{absent_file
}->{$pb->{path
}} ||= [];
3490 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
3494 sub change_file_prop
{
3495 my ($self, $fb, $prop, $value) = @_;
3496 return undef if is_path_ignored
($fb->{path
});
3497 if ($prop eq 'svn:executable') {
3498 if ($fb->{mode_b
} != 120000) {
3499 $fb->{mode_b
} = defined $value ?
100755 : 100644;
3501 } elsif ($prop eq 'svn:special') {
3502 $fb->{mode_b
} = defined $value ?
120000 : 100644;
3504 $self->{file_prop
}->{$fb->{path
}} ||= {};
3505 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
3510 sub apply_textdelta
{
3511 my ($self, $fb, $exp) = @_;
3512 return undef if is_path_ignored
($fb->{path
});
3513 my $fh = $::_repository
->temp_acquire('svn_delta');
3514 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3515 # (but $base does not,) so dup() it for reading in close_file
3516 open my $dup, '<&', $fh or croak
$!;
3517 my $base = $::_repository
->temp_acquire('git_blob');
3520 my ($base_is_link, $size);
3522 if ($fb->{mode_a
} eq '120000' &&
3523 ! $self->{empty_symlinks
}->{$fb->{path
}}) {
3524 print $base 'link ' or die "print $!\n";
3528 $size = $::_repository
->cat_blob($fb->{blob
}, $base);
3529 die "Failed to read object $fb->{blob}" if ($size < 0);
3532 seek $base, 0, 0 or croak
$!;
3533 my $got = ::md5sum
($base);
3535 my $err = "Checksum mismatch: ".
3536 "$fb->{path} $fb->{blob}\n" .
3537 "expected: $exp\n" .
3539 if ($base_is_link) {
3541 "Retrying... (possibly ",
3542 "a bad symlink from SVN)\n";
3543 $::_repository
->temp_reset($base);
3551 seek $base, 0, 0 or croak
$!;
3553 $fb->{base
} = $base;
3554 [ SVN
::TxDelta
::apply
($base, $dup, undef, $fb->{path
}, $fb->{pool
}) ];
3558 my ($self, $fb, $exp) = @_;
3559 return undef if is_path_ignored
($fb->{path
});
3562 my $path = $self->git_path($fb->{path
});
3563 if (my $fh = $fb->{fh
}) {
3565 seek($fh, 0, 0) or croak
$!;
3566 my $got = ::md5sum
($fh);
3568 die "Checksum mismatch: $path\n",
3569 "expected: $exp\n got: $got\n";
3572 if ($fb->{mode_b
} == 120000) {
3573 sysseek($fh, 0, 0) or croak
$!;
3574 my $rd = sysread($fh, my $buf, 5);
3577 croak
"sysread: $!\n";
3578 } elsif ($rd == 0) {
3579 warn "$path has mode 120000",
3580 " but it points to nothing\n",
3581 "converting to an empty file with mode",
3583 $fb->{mode_b
} = '100644';
3584 } elsif ($buf ne 'link ') {
3585 warn "$path has mode 120000",
3586 " but is not a link\n";
3588 my $tmp_fh = $::_repository
->temp_acquire(
3591 while ($res = sysread($fh, my $str, 1024)) {
3592 my $out = syswrite($tmp_fh, $str, $res);
3593 defined($out) && $out == $res
3595 Git
::temp_path
($tmp_fh),
3598 defined $res or croak
$!;
3600 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3601 Git
::temp_release
($tmp_fh, 1);
3605 $hash = $::_repository
->hash_and_insert_object(
3606 Git
::temp_path
($fh));
3607 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3609 Git
::temp_release
($fb->{base
}, 1);
3610 Git
::temp_release
($fh, 1);
3612 $hash = $fb->{blob
} or die "no blob information\n";
3615 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
3616 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
3622 $self->{nr
} = $self->{gii
}->{nr
};
3623 delete $self->{gii
};
3624 $self->SUPER::abort_edit
(@_);
3629 $self->{git_commit_ok
} = 1;
3630 $self->{nr
} = $self->{gii
}->{nr
};
3631 delete $self->{gii
};
3632 $self->SUPER::close_edit
(@_);
3635 package SVN
::Git
::Editor
;
3636 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
3643 my ($class, $opts) = @_;
3644 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
3645 die "$_ required!\n" unless (defined $opts->{$_});
3648 my $pool = SVN
::Pool
->new;
3649 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
3650 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
3653 # $opts->{ra} functions should not be used after this:
3654 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
3655 $opts->{editor_cb
}, $pool);
3656 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
3657 bless $self, $class;
3658 foreach (qw
/svn_path r tree_a tree_b/) {
3659 $self->{$_} = $opts->{$_};
3661 $self->{url
} = $opts->{ra
}->{url
};
3662 $self->{mods
} = $mods;
3663 $self->{types
} = $types;
3664 $self->{pool
} = $pool;
3665 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3667 $self->{path_prefix
} = length $self->{svn_path
} ?
3668 "$self->{svn_path}/" : '';
3669 $self->{config
} = $opts->{config
};
3674 my ($tree_a, $tree_b) = @_;
3675 my @diff_tree = qw(diff-tree -z -r);
3676 if ($_cp_similarity) {
3677 push @diff_tree, "-C$_cp_similarity";
3679 push @diff_tree, '-C';
3681 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3682 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
3683 push @diff_tree, $tree_a, $tree_b;
3684 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
3688 while (<$diff_fh>) {
3689 chomp $_; # this gets rid of the trailing "\0"
3690 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
3691 ($::sha1
)\s
($::sha1
)\s
3692 ([MTCRAD
])\d
*$/xo
) {
3693 push @mods, { mode_a
=> $1, mode_b
=> $2,
3694 sha1_a
=> $3, sha1_b
=> $4,
3696 if ($5 =~ /^(?:C|R)$/) {
3701 } elsif ($state eq 'file_a') {
3702 my $x = $mods[$#mods] or croak
"Empty array\n";
3703 if ($x->{chg
} !~ /^(?:C|R)$/) {
3704 croak
"Error parsing $_, $x->{chg}\n";
3708 } elsif ($state eq 'file_b') {
3709 my $x = $mods[$#mods] or croak
"Empty array\n";
3710 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
3711 croak
"Error parsing $_, $x->{chg}\n";
3713 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
3714 croak
"Error parsing $_, $x->{chg}\n";
3719 croak
"Error parsing $_\n";
3722 command_close_pipe
($diff_fh, $ctx);
3726 sub check_diff_paths
{
3727 my ($ra, $pfx, $rev, $mods) = @_;
3729 $pfx .= '/' if length $pfx;
3731 sub type_diff_paths
{
3732 my ($ra, $types, $path, $rev) = @_;
3733 my @p = split m
#/+#, $path;
3735 unless (defined $types->{$c}) {
3736 $types->{$c} = $ra->check_path($c, $rev);
3739 $c .= '/' . shift @p;
3740 next if defined $types->{$c};
3741 $types->{$c} = $ra->check_path($c, $rev);
3745 foreach my $m (@
$mods) {
3746 foreach my $f (qw
/file_a file_b/) {
3747 next unless defined $m->{$f};
3748 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
3749 if (length $pfx.$dir && ! defined $types{$dir}) {
3750 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
3758 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3762 my ($self, $path) = @_;
3763 $self->{path_prefix
}.(defined $path ?
$path : '');
3767 my ($self, $path) = @_;
3768 if ($self->{url
} =~ m
#^https?://#) {
3769 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3771 $self->{url
} . '/' . $self->repo_path($path);
3776 my $rm = $self->{rm
};
3777 delete $rm->{''}; # we never delete the url we're tracking
3780 foreach (keys %$rm) {
3781 my @d = split m
#/#, $_;
3785 $c .= '/' . shift @d;
3789 delete $rm->{$self->{svn_path
}};
3790 delete $rm->{''}; # we never delete the url we're tracking
3793 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
3798 my @dn = split m
#/#, $_;
3800 delete $rm->{join '/', @dn};
3807 command_close_pipe
($fh, $ctx);
3809 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3810 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3811 $self->close_directory($bat->{$d}, $p);
3812 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3813 print "\tD+\t$d/\n" unless $::_q
;
3814 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3819 sub open_or_add_dir
{
3820 my ($self, $full_path, $baton) = @_;
3821 my $t = $self->{types
}->{$full_path};
3823 die "$full_path not known in r$self->{r} or we have a bug!\n";
3827 # SVN::Node::none and SVN::Node::file are used only once,
3828 # so we're shutting up Perl's warnings about them.
3829 if ($t == $SVN::Node
::none
) {
3830 return $self->add_directory($full_path, $baton,
3831 undef, -1, $self->{pool
});
3832 } elsif ($t == $SVN::Node
::dir
) {
3833 return $self->open_directory($full_path, $baton,
3834 $self->{r
}, $self->{pool
});
3835 } # no warnings 'once'
3836 print STDERR
"$full_path already exists in repository at ",
3837 "r$self->{r} and it is not a directory (",
3838 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3839 } # no warnings 'once'
3844 my ($self, $path) = @_;
3845 my $bat = $self->{bat
};
3846 my $repo_path = $self->repo_path($path);
3847 return $bat->{''} unless (length $repo_path);
3848 my @p = split m
#/+#, $repo_path;
3850 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3853 $c .= '/' . shift @p;
3854 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3859 # Subroutine to convert a globbing pattern to a regular expression.
3860 # From perl cookbook.
3862 my $globstr = shift;
3863 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3864 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3865 return '^' . $globstr . '$';
3868 sub check_autoprop
{
3869 my ($self, $pattern, $properties, $file, $fbat) = @_;
3870 # Convert the globbing pattern to a regular expression.
3871 my $regex = glob2pat
($pattern);
3872 # Check if the pattern matches the file name.
3873 if($file =~ m/($regex)/) {
3874 # Parse the list of properties to set.
3875 my @props = split(/;/, $properties);
3876 foreach my $prop (@props) {
3877 # Parse 'name=value' syntax and set the property.
3878 if ($prop =~ /([^=]+)=(.*)/) {
3879 my ($n,$v) = ($1,$2);
3883 $self->change_file_prop($fbat, $n, $v);
3889 sub apply_autoprops
{
3890 my ($self, $file, $fbat) = @_;
3891 my $conf_t = ${$self->{config
}}{'config'};
3893 # Check [miscellany]/enable-auto-props in svn configuration.
3894 if (SVN
::_Core
::svn_config_get_bool
(
3896 $SVN::_Core
::SVN_CONFIG_SECTION_MISCELLANY
,
3897 $SVN::_Core
::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS
,
3899 # Auto-props are enabled. Enumerate them to look for matches.
3900 my $callback = sub {
3901 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3903 SVN
::_Core
::svn_config_enumerate
(
3905 $SVN::_Core
::SVN_CONFIG_SECTION_AUTO_PROPS
,
3911 my ($self, $m) = @_;
3912 my ($dir, $file) = split_path
($m->{file_b
});
3913 my $pbat = $self->ensure_path($dir);
3914 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3916 print "\tA\t$m->{file_b}\n" unless $::_q
;
3917 $self->apply_autoprops($file, $fbat);
3918 $self->chg_file($fbat, $m);
3919 $self->close_file($fbat,undef,$self->{pool
});
3923 my ($self, $m) = @_;
3924 my ($dir, $file) = split_path
($m->{file_b
});
3925 my $pbat = $self->ensure_path($dir);
3926 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3927 $self->url_path($m->{file_a
}), $self->{r
});
3928 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3929 $self->chg_file($fbat, $m);
3930 $self->close_file($fbat,undef,$self->{pool
});
3934 my ($self, $path, $pbat) = @_;
3935 my $rpath = $self->repo_path($path);
3936 my ($dir, $file) = split_path
($rpath);
3937 $self->{rm
}->{$dir} = 1;
3938 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3942 my ($self, $m) = @_;
3943 my ($dir, $file) = split_path
($m->{file_b
});
3944 my $pbat = $self->ensure_path($dir);
3945 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3946 $self->url_path($m->{file_a
}), $self->{r
});
3947 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3948 $self->apply_autoprops($file, $fbat);
3949 $self->chg_file($fbat, $m);
3950 $self->close_file($fbat,undef,$self->{pool
});
3952 ($dir, $file) = split_path
($m->{file_a
});
3953 $pbat = $self->ensure_path($dir);
3954 $self->delete_entry($m->{file_a
}, $pbat);
3958 my ($self, $m) = @_;
3959 my ($dir, $file) = split_path
($m->{file_b
});
3960 my $pbat = $self->ensure_path($dir);
3961 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3962 $pbat,$self->{r
},$self->{pool
});
3963 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
3964 $self->chg_file($fbat, $m);
3965 $self->close_file($fbat,undef,$self->{pool
});
3968 sub T
{ shift->M(@_) }
3970 sub change_file_prop
{
3971 my ($self, $fbat, $pname, $pval) = @_;
3972 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3975 sub _chg_file_get_blob
($$$$) {
3976 my ($self, $fbat, $m, $which) = @_;
3977 my $fh = $::_repository
->temp_acquire("git_blob_$which");
3978 if ($m->{"mode_$which"} =~ /^120/) {
3979 print $fh 'link ' or croak
$!;
3980 $self->change_file_prop($fbat,'svn:special','*');
3981 } elsif ($m->{mode_a
} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
3982 $self->change_file_prop($fbat,'svn:special',undef);
3984 my $blob = $m->{"sha1_$which"};
3985 return ($fh,) if ($blob =~ /^0{40}$/);
3986 my $size = $::_repository
->cat_blob($blob, $fh);
3987 croak
"Failed to read object $blob" if ($size < 0);
3988 $fh->flush == 0 or croak
$!;
3989 seek $fh, 0, 0 or croak
$!;
3991 my $exp = ::md5sum
($fh);
3992 seek $fh, 0, 0 or croak
$!;
3997 my ($self, $fbat, $m) = @_;
3998 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3999 $self->change_file_prop($fbat,'svn:executable','*');
4000 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
4001 $self->change_file_prop($fbat,'svn:executable',undef);
4003 my ($fh_a, $exp_a) = _chg_file_get_blob
$self, $fbat, $m, 'a';
4004 my ($fh_b, $exp_b) = _chg_file_get_blob
$self, $fbat, $m, 'b';
4005 my $pool = SVN
::Pool
->new;
4006 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4008 my $txstream = SVN
::TxDelta
::new
($fh_a, $fh_b, $pool);
4009 my $res = SVN
::TxDelta
::send_txstream
($txstream, @
$atd, $pool);
4011 die "Unexpected result from send_txstream: $res\n",
4012 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4015 my $got = SVN
::TxDelta
::send_stream
($fh_b, @
$atd, $pool);
4016 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4017 if ($got ne $exp_b);
4019 Git
::temp_release
($fh_b, 1);
4020 Git
::temp_release
($fh_a, 1);
4025 my ($self, $m) = @_;
4026 my ($dir, $file) = split_path
($m->{file_b
});
4027 my $pbat = $self->ensure_path($dir);
4028 print "\tD\t$m->{file_b}\n" unless $::_q
;
4029 $self->delete_entry($m->{file_b
}, $pbat);
4034 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
4035 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
4037 $self->close_directory($bat->{$_}, $p);
4039 $self->close_directory($bat->{''}, $p);
4040 $self->SUPER::close_edit
($p);
4046 $self->SUPER::abort_edit
($self->{pool
});
4051 $self->SUPER::DESTROY
(@_);
4052 $self->{pool
}->clear;
4055 # this drives the editor
4058 my $mods = $self->{mods
};
4059 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
4060 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
4062 if (defined $o{$f}) {
4065 fatal
("Invalid change type: $f");
4068 $self->rmdirs if $_rmdir;
4074 return scalar @
$mods;
4077 package Git
::SVN
::Ra
;
4078 use vars qw
/@ISA $config_dir $_log_window_size/;
4081 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
4084 # enforce temporary pool usage for some simple functions
4086 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
4088 my $SUPER = "SUPER::$f";
4091 my $pool = SVN
::Pool
->new;
4092 my @ret = $self->$SUPER(@_,$pool);
4094 wantarray ?
@ret : $ret[0];
4099 sub _auth_providers
() {
4101 SVN
::Client
::get_simple_provider
(),
4102 SVN
::Client
::get_ssl_server_trust_file_provider
(),
4103 SVN
::Client
::get_simple_prompt_provider
(
4104 \
&Git
::SVN
::Prompt
::simple
, 2),
4105 SVN
::Client
::get_ssl_client_cert_file_provider
(),
4106 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
4107 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
4108 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
4109 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
4110 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
4111 SVN
::Client
::get_username_provider
(),
4112 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
4113 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
4114 SVN
::Client
::get_username_prompt_provider
(
4115 \
&Git
::SVN
::Prompt
::username
, 2)
4119 sub escape_uri_only
{
4122 foreach (split m{/}, $uri) {
4123 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
4131 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
4132 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
4133 $url = "$scheme://$domain$uri";
4139 my ($class, $url) = @_;
4141 return $RA if ($RA && $RA->{url
} eq $url);
4143 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
4144 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
4145 my $config = SVN
::Core
::config_get_config
($config_dir);
4147 my $dont_store_passwords = 1;
4148 my $conf_t = ${$config}{'config'};
4151 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4152 # produces warnings that variables are used only once.
4153 # I had not found the better way to shut them up, so
4154 # the warnings of type 'once' are disabled in this block.
4155 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4156 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4157 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
4159 SVN
::_Core
::svn_auth_set_parameter
($baton,
4160 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
4161 bless (\
$dont_store_passwords, "_p_void"));
4163 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4164 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4165 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
4167 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
4169 } # no warnings 'once'
4170 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
4172 pool
=> SVN
::Pool
->new,
4173 auth_provider_callbacks
=> $callbacks);
4174 $self->{url
} = $url;
4175 $self->{svn_path
} = $url;
4176 $self->{repos_root
} = $self->get_repos_root;
4177 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
4178 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
4179 get_dir
=> { r
=> 0, data
=> {} } };
4180 $RA = bless $self, $class;
4184 my ($self, $path, $r) = @_;
4185 my $cache = $self->{cache
}->{check_path
};
4186 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
4187 return $cache->{data
}->{$path};
4189 my $pool = SVN
::Pool
->new;
4190 my $t = $self->SUPER::check_path
($path, $r, $pool);
4192 if ($r != $cache->{r
}) {
4193 %{$cache->{data
}} = ();
4196 $cache->{data
}->{$path} = $t;
4200 my ($self, $dir, $r) = @_;
4201 my $cache = $self->{cache
}->{get_dir
};
4202 if ($r == $cache->{r
}) {
4203 if (my $x = $cache->{data
}->{$dir}) {
4204 return wantarray ? @
$x : $x->[0];
4207 my $pool = SVN
::Pool
->new;
4208 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
4209 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
4211 if ($r != $cache->{r
}) {
4212 %{$cache->{data
}} = ();
4215 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
4216 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
4220 # do not call the real DESTROY since we store ourselves in $RA
4223 # get_log(paths, start, end, limit,
4224 # discover_changed_paths, strict_node_history, receiver)
4226 my ($self, @args) = @_;
4227 my $pool = SVN
::Pool
->new;
4229 # the limit parameter was not supported in SVN 1.1.x, so we
4230 # drop it. Therefore, the receiver callback passed to it
4231 # is made aware of this limitation by being wrapped if
4232 # the limit passed to is being wrapped.
4233 if ($SVN::Core
::VERSION
le '1.2.0') {
4234 my $limit = splice(@args, 3, 1);
4236 my $receiver = pop @args;
4237 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4240 my $ret = $self->SUPER::get_log
(@args, $pool);
4246 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4247 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
4248 my $out = IO
::File
->new_tmpfile;
4250 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4251 # $ctx->diff(), so we'll create a default one
4252 my $pool = SVN
::Pool
->new_default_sub;
4254 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4255 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4257 my $ret = (($out->stat)[7] == 0);
4258 close $out or croak
$!;
4263 sub get_commit_editor
{
4264 my ($self, $log, $cb, $pool) = @_;
4265 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
4266 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
4270 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4271 my $new = ($rev_a == $rev_b);
4272 my $path = $gs->{path
};
4274 if ($new && -e
$gs->{index}) {
4275 unlink $gs->{index} or die
4276 "Couldn't unlink index: $gs->{index}: $!\n";
4278 my $pool = SVN
::Pool
->new;
4279 $editor->set_path_strip($path);
4280 my (@pc) = split m
#/#, $path;
4281 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
4283 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4285 # Since we can't rely on svn_ra_reparent being available, we'll
4286 # just have to do some magic with set_path to make it so
4287 # we only want a partial path.
4289 my $final = join('/', @pc);
4291 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4292 $sp .= '/' if length $sp;
4295 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4297 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4299 $reporter->finish_report($pool);
4301 $editor->{git_commit_ok
};
4304 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4305 # svn_ra_reparent didn't work before 1.4)
4307 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4308 my $path = $gs->{path
};
4309 my $pool = SVN
::Pool
->new;
4311 my $full_url = $self->{url
};
4312 my $old_url = $full_url;
4313 $full_url .= '/' . escape_uri_only
($path) if length $path;
4314 my ($ra, $reparented);
4316 if ($old_url =~ m
#^svn(\+ssh)?://#) {
4320 $ra = Git
::SVN
::Ra
->new($full_url);
4322 } elsif ($old_url ne $full_url) {
4323 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
4324 $self->{url
} = $full_url;
4329 $url_b = escape_url
($url_b);
4330 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
4331 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4332 $reporter->set_path('', $rev_a, 0, @lock, $pool);
4333 $reporter->finish_report($pool);
4336 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
4337 $self->{url
} = $old_url;
4341 $editor->{git_commit_ok
};
4344 sub longest_common_path
{
4345 my ($gsv, $globs) = @_;
4347 my $common_max = scalar @
$gsv;
4349 foreach my $gs (@
$gsv) {
4350 my @tmp = split m
#/#, $gs->{path};
4353 $p .= length($p) ?
"/$_" : $_;
4359 $common_max += scalar @
$globs;
4360 foreach my $glob (@
$globs) {
4361 my @tmp = split m
#/#, $glob->{path}->{left};
4364 $p .= length($p) ?
"/$_" : $_;
4370 my $longest_path = '';
4371 foreach (sort {length $b <=> length $a} keys %common) {
4372 if ($common{$_} == $common_max) {
4380 sub gs_fetch_loop_common
{
4381 my ($self, $base, $head, $gsv, $globs) = @_;
4382 return if ($base > $head);
4383 my $inc = $_log_window_size;
4384 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
4385 my $longest_path = longest_common_path
($gsv, $globs);
4386 my $ra_url = $self->{url
};
4390 my $err_handler = $SVN::Error
::handler
;
4391 $SVN::Error
::handler
= sub {
4393 skip_unknown_revs
($err);
4396 my ($paths, $r, $author, $date, $log) = @_;
4397 [ dup_changed_paths
($paths),
4398 { author
=> $author, date
=> $date, log => $log } ];
4400 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4401 sub { $revs{$_[1]} = _cb
(@_) });
4403 print "Checked through r$max\r";
4405 if ($err && $max >= $head) {
4406 print STDERR
"Path '$longest_path' ",
4407 "was probably deleted:\n",
4408 $err->expanded_message,
4409 "\nWill attempt to follow ",
4410 "revisions r$min .. r$max ",
4411 "committed before the deletion\n";
4413 while (--$hi >= $min) {
4415 $self->get_log([$longest_path], $min, $hi,
4418 $revs{$_[1]} = _cb
(@_) });
4420 print STDERR
"r$min .. r$ok OK\n";
4425 $SVN::Error
::handler
= $err_handler;
4427 my %exists = map { $_->{path
} => $_ } @
$gsv;
4428 foreach my $r (sort {$a <=> $b} keys %revs) {
4429 my ($paths, $logged) = @
{$revs{$r}};
4431 foreach my $gs ($self->match_globs(\
%exists, $paths,
4433 if ($gs->rev_map_max >= $r) {
4436 next unless $gs->match_paths($paths, $r);
4437 $gs->{logged_rev_props
} = $logged;
4438 if (my $last_commit = $gs->last_commit) {
4439 $gs->assert_index_clean($last_commit);
4441 my $log_entry = $gs->do_fetch($paths, $r);
4443 $gs->do_git_commit($log_entry);
4445 $INDEX_FILES{$gs->{index}} = 1;
4447 foreach my $g (@
$globs) {
4448 my $k = "svn-remote.$g->{remote}." .
4450 Git
::SVN
::tmp_config
($k, $r);
4456 $self = Git
::SVN
::Ra
->new($ra_url);
4457 $ra_invalid = undef;
4460 # pre-fill the .rev_db since it'll eventually get filled in
4461 # with '0' x40 if something new gets committed
4462 foreach my $gs (@
$gsv) {
4463 next if $gs->rev_map_max >= $max;
4464 next if defined $gs->rev_map_get($max);
4465 $gs->rev_map_set($max, 0 x40
);
4467 foreach my $g (@
$globs) {
4468 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4469 Git
::SVN
::tmp_config
($k, $max);
4471 last if $max >= $head;
4474 $max = $head if ($max > $head);
4479 sub get_dir_globbed
{
4480 my ($self, $left, $depth, $r) = @_;
4482 my @x = eval { $self->get_dir($left, $r) };
4483 return unless scalar @x == 3;
4484 my $dirents = $x[0];
4486 foreach my $de (keys %$dirents) {
4487 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
4489 my @args = ("$left/$de", $depth - 1, $r);
4490 foreach my $dir ($self->get_dir_globbed(@args)) {
4491 push @finalents, "$de/$dir";
4494 push @finalents, $de;
4501 my ($self, $exists, $paths, $globs, $r) = @_;
4504 my ($self, $exists, $g, $r) = @_;
4506 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
4507 $g->{path
}->{depth
},
4510 foreach my $de (@dirs) {
4511 my $p = $g->{path
}->full_path($de);
4512 next if $exists->{$p};
4513 next if (length $g->{path
}->{right
} &&
4514 ($self->check_path($p, $r) !=
4516 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
4517 $g->{ref}->full_path($de), 1);
4520 foreach my $g (@
$globs) {
4521 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4522 if ($path->{action
} =~ /^[AR]$/) {
4523 get_dir_check
($self, $exists, $g, $r);
4526 foreach (keys %$paths) {
4527 if (/$g->{path}->{left_regex}/ &&
4528 !/$g->{path}->{regex}/) {
4529 next if $paths->{$_}->{action
} !~ /^[AR]$/;
4530 get_dir_check
($self, $exists, $g, $r);
4532 next unless /$g->{path}->{regex}/;
4534 my $pathname = $g->{path
}->full_path($p);
4535 next if $exists->{$pathname};
4536 next if ($self->check_path($pathname, $r) !=
4538 $exists->{$pathname} = Git
::SVN
->init(
4539 $self->{url
}, $pathname, undef,
4540 $g->{ref}->full_path($p), 1);
4543 foreach (split m
#/#, $g->{path}->{left}) {
4545 next unless ($paths->{$c} &&
4546 ($paths->{$c}->{action
} =~ /^[AR]$/));
4547 get_dir_check
($self, $exists, $g, $r);
4555 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
4556 my $url = $self->{repos_root
};
4557 my @components = split(m!/!, $self->{svn_path
});
4560 $url .= "/$c" if length $c;
4561 eval { (ref $self)->new($url)->get_latest_revnum };
4562 } while ($@
&& ($c = shift @components));
4568 unless (defined $can_do_switch) {
4569 my $pool = SVN
::Pool
->new;
4571 $self->do_switch(1, '', 0, $self->{url
},
4572 SVN
::Delta
::Editor
->new, $pool);
4577 $rep->abort_report($pool);
4585 sub skip_unknown_revs
{
4587 my $errno = $err->apr_err();
4588 # Maybe the branch we're tracking didn't
4589 # exist when the repo started, so it's
4590 # not an error if it doesn't, just continue
4592 # Wonderfully consistent library, eh?
4593 # 160013 - svn:// and file://
4594 # 175002 - http(s)://
4595 # 175007 - http(s):// (this repo required authorization, too...)
4596 # More codes may be discovered later...
4597 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
4598 my $err_key = $err->expanded_message;
4599 # revision numbers change every time, filter them out
4600 $err_key =~ s/\d+/\0/g;
4601 $err_key = "$errno\0$err_key";
4602 unless ($ignored_err{$err_key}) {
4603 warn "W: Ignoring error from SVN, path probably ",
4604 "does not exist: ($errno): ",
4605 $err->expanded_message,"\n";
4606 warn "W: Do not be alarmed at the above message ",
4607 "git-svn is just searching aggressively for ",
4609 "This may take a while on large repositories\n";
4610 $ignored_err{$err_key} = 1;
4614 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4617 # svn_log_changed_path_t objects passed to get_log are likely to be
4618 # overwritten even if only the refs are copied to an external variable,
4619 # so we should dup the structures in their entirety. Using an externally
4620 # passed pool (instead of our temporary and quickly cleared pool in
4621 # Git::SVN::Ra) does not help matters at all...
4622 sub dup_changed_paths
{
4624 return undef unless $paths;
4626 foreach my $p (keys %$paths) {
4627 my $i = $paths->{$p};
4628 my %s = map { $_ => $i->$_ }
4629 qw
/copyfrom_path copyfrom_rev action/;
4635 package Git
::SVN
::Log
;
4638 use POSIX qw
/strftime/;
4640 use constant commit_log_separator
=> ('-' x
72) . "\n";
4641 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
4642 %rusers $show_commit $incremental/;
4647 return 1 if defined $c->{r
};
4649 # big commit message got truncated by the 16k pretty buffer in rev-list
4650 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
4651 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
4653 my @log = command
(qw
/cat-file commit/, $c->{c
});
4655 # shift off the headers
4656 shift @log while ($log[0] ne '');
4659 # TODO: make $c->{l} not have a trailing newline in the future
4660 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
4662 (undef, $c->{r
}, undef) = ::extract_metadata
(
4663 (grep(/^git-svn-id: /, @log))[-1]);
4665 return defined $c->{r
};
4669 return $color || Git
->repository->get_colorbool('color.diff');
4672 sub git_svn_log_cmd
{
4673 my ($r_min, $r_max, @args) = @_;
4675 my (@files, @log_opts);
4676 foreach my $x (@args) {
4677 if ($x eq '--' || @files) {
4680 if (::verify_ref
("$x^0")) {
4688 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
4689 $gs ||= Git
::SVN
->_new;
4690 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
4692 push @cmd, '-r' unless $non_recursive;
4693 push @cmd, qw
/--raw --name-status/ if $verbose;
4694 push @cmd, '--color' if log_use_color
();
4695 push @cmd, @log_opts;
4696 if (defined $r_max && $r_max == $r_min) {
4697 push @cmd, '--max-count=1';
4698 if (my $c = $gs->rev_map_get($r_max)) {
4701 } elsif (defined $r_max) {
4702 if ($r_max < $r_min) {
4703 ($r_min, $r_max) = ($r_max, $r_min);
4705 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4706 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4707 # If there are no commits in the range, both $c_max and $c_min
4708 # will be undefined. If there is at least 1 commit in the
4709 # range, both will be defined.
4710 return () if !defined $c_min || !defined $c_max;
4711 if ($c_min eq $c_max) {
4712 push @cmd, '--max-count=1', $c_min;
4714 push @cmd, '--boundary', "$c_min..$c_max";
4717 return (@cmd, @files);
4720 # adapted from pager.c
4722 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
4723 if (!defined $pager) {
4725 } elsif (length $pager == 0 || $pager eq 'cat') {
4728 $ENV{GIT_PAGER_IN_USE
} = defined($pager);
4732 return unless -t
*STDOUT
&& defined $pager;
4733 pipe my ($rfd, $wfd) or return;
4734 defined(my $pid = fork) or ::fatal
"Can't fork: $!";
4736 open STDOUT
, '>&', $wfd or
4737 ::fatal
"Can't redirect to stdout: $!";
4740 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!";
4741 $ENV{LESS
} ||= 'FRSX';
4742 exec $pager or ::fatal
"Can't run pager: $! ($pager)";
4745 sub format_svn_date
{
4746 # some systmes don't handle or mishandle %z, so be creative.
4747 my $t = shift || time;
4748 my $gm = timelocal
(gmtime($t));
4749 my $sign = qw( + + - )[ $t <=> $gm ];
4750 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
4751 return strftime
("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
4754 sub parse_git_date
{
4756 # Date::Parse isn't in the standard Perl distro :(
4757 if ($tz =~ s/^\+//) {
4758 $t += tz_to_s_offset
($tz);
4759 } elsif ($tz =~ s/^\-//) {
4760 $t -= tz_to_s_offset
($tz);
4765 sub set_local_timezone
{
4773 sub tz_to_s_offset
{
4776 return ($1 * 60) + ($tz * 3600);
4779 sub get_author_info
{
4780 my ($dest, $author, $t, $tz) = @_;
4781 $author =~ s/(?:^\s*|\s*$)//g;
4782 $dest->{a_raw
} = $author;
4785 $au = $rusers{$author} || undef;
4788 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4793 $dest->{t_utc
} = parse_git_date
($t, $tz);
4796 sub process_commit
{
4797 my ($c, $r_min, $r_max, $defer) = @_;
4798 if (defined $r_min && defined $r_max) {
4799 if ($r_min == $c->{r
} && $r_min == $r_max) {
4803 return 1 if $r_min == $r_max;
4804 if ($r_min < $r_max) {
4805 # we need to reverse the print order
4806 return 0 if (defined $limit && --$limit < 0);
4810 if ($r_min != $r_max) {
4811 return 1 if ($r_min < $c->{r
});
4812 return 1 if ($r_max > $c->{r
});
4815 return 0 if (defined $limit && --$limit < 0);
4824 if (my $l = $c->{l
}) {
4825 while ($l->[0] =~ /^\s*$/) { shift @
$l }
4828 $l_fmt ||= 'A' . length($c->{r
});
4829 print 'r',pack($l_fmt, $c->{r
}),' | ';
4830 print "$c->{c} | " if $show_commit;
4833 show_commit_normal
($c);
4837 sub show_commit_changed_paths
{
4839 return unless $c->{changed
};
4840 print "Changed paths:\n", @
{$c->{changed
}};
4843 sub show_commit_normal
{
4845 print commit_log_separator
, "r$c->{r} | ";
4846 print "$c->{c} | " if $show_commit;
4847 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
4850 if (my $l = $c->{l
}) {
4851 while ($l->[$#$l] eq "\n" && $#$l > 0
4852 && $l->[($#$l - 1)] eq "\n") {
4855 $nr_line = scalar @
$l;
4857 print "1 line\n\n\n";
4859 if ($nr_line == 1) {
4860 $nr_line = '1 line';
4862 $nr_line .= ' lines';
4864 print $nr_line, "\n";
4865 show_commit_changed_paths
($c);
4867 print $_ foreach @
$l;
4871 show_commit_changed_paths
($c);
4875 foreach my $x (qw
/raw stat diff/) {
4878 print $_ foreach @
{$c->{$x}}
4885 my ($r_min, $r_max);
4886 my $r_last = -1; # prevent dupes
4887 set_local_timezone
();
4888 if (defined $::_revision
) {
4889 if ($::_revision
=~ /^(\d+):(\d+)$/) {
4890 ($r_min, $r_max) = ($1, $2);
4891 } elsif ($::_revision
=~ /^\d+$/) {
4892 $r_min = $r_max = $::_revision
;
4894 ::fatal
"-r$::_revision is not supported, use ",
4895 "standard 'git log' arguments instead";
4900 @args = git_svn_log_cmd
($r_min, $r_max, @args);
4902 print commit_log_separator
unless $incremental || $oneline;
4905 my $log = command_output_pipe
(@args);
4907 my (@k, $c, $d, $stat);
4908 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4910 if (/^${esc_color}commit -?($::sha1_short)/o) {
4912 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
4914 process_commit
($c, $r_min, $r_max, \
@k) or
4919 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4920 get_author_info
($c, $1, $2, $3);
4921 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4923 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4924 push @
{$c->{raw
}}, $_;
4925 } elsif (/^${esc_color}[ACRMDT]\t/) {
4926 # we could add $SVN->{svn_path} here, but that requires
4927 # remote access at the moment (repo_path_split)...
4928 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4929 push @
{$c->{changed
}}, $_;
4930 } elsif (/^${esc_color}diff /o) {
4932 push @
{$c->{diff
}}, $_;
4934 push @
{$c->{diff
}}, $_;
4935 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
4936 $esc_color*[\
+\
-]*$esc_color$/x
) {
4938 push @
{$c->{stat}}, $_;
4939 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4940 push @
{$c->{stat}}, $_;
4942 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4943 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
4944 } elsif (s/^${esc_color} //o) {
4945 push @
{$c->{l
}}, $_;
4948 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
4950 process_commit
($c, $r_min, $r_max, \
@k);
4953 ($r_min, $r_max) = ($r_max, $r_min);
4954 process_commit
($_, $r_min, $r_max) foreach reverse @k;
4958 print commit_log_separator
unless $incremental || $oneline;
4967 my ($fh, $ctx, $rev);
4970 ($fh, $ctx) = command_output_pipe
('blame', @_, $path);
4971 while (my $line = <$fh>) {
4972 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4973 # Uncommitted edits show up as a rev ID of
4974 # all zeros, which we can't look up with
4977 (undef, $rev, undef) =
4979 $rev = '0' if (!$rev);
4983 $rev = sprintf('%-10s', $rev);
4984 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4989 ($fh, $ctx) = command_output_pipe
('blame', '-p', @_, 'HEAD',
4993 while (my $line = <$fh>) {
4994 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4996 (undef, $rev, undef) = ::cmt_metadata
($1);
4997 $rev = '0' if (!$rev);
4999 elsif ($line =~ /^author (.*)/) {
5000 $authors{$rev} = $1;
5001 $authors{$rev} =~ s/\s/_/g;
5003 elsif ($line =~ /^\t(.*)$/) {
5004 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5008 command_close_pipe
($fh, $ctx);
5011 package Git
::SVN
::Migration
;
5012 # these version numbers do NOT correspond to actual version numbers
5013 # of git nor git-svn. They are just relative.
5015 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5017 # v1 layout: .git/$id/info/url, refs/remotes/$id
5019 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5021 # v3 layout: .git/svn/$id, refs/remotes/$id
5022 # - info/url may remain for backwards compatibility
5023 # - this is what we migrate up to this layout automatically,
5024 # - this will be used by git svn init on single branches
5025 # v3.1 layout (auto migrated):
5026 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5027 # for backwards compatibility
5029 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5030 # - this is only created for newly multi-init-ed
5031 # repositories. Similar in spirit to the
5032 # --use-separate-remotes option in git-clone (now default)
5033 # - we do not automatically migrate to this (following
5034 # the example set by core git)
5036 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
5037 # - newer, more-efficient format that uses 24-bytes per record
5038 # with no filler space.
5039 # - use xxd -c24 < .rev_map.$UUID to view and debug
5040 # - This is a one-way migration, repositories updated to the
5041 # new format will not be able to use old git-svn without
5042 # rebuilding the .rev_db. Rebuilding the rev_db is not
5043 # possible if noMetadata or useSvmProps are set; but should
5044 # be no problem for users that use the (sensible) defaults.
5048 use File
::Path qw
/mkpath/;
5049 use File
::Basename qw
/dirname basename/;
5050 use vars qw
/$_minimize/;
5052 sub migrate_from_v0
{
5053 my $git_dir = $ENV{GIT_DIR
};
5054 return undef unless -d
$git_dir;
5055 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5059 my ($id, $orig_ref) = ($_, $_);
5060 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
5061 next unless -f
"$git_dir/$id/info/url";
5062 my $new_ref = "refs/remotes/$id";
5063 if (::verify_ref
("$new_ref^0")) {
5064 print STDERR
"W: $orig_ref is probably an old ",
5065 "branch used by an ancient version of ",
5067 "However, $new_ref also exists.\n",
5068 "We will not be able ",
5069 "to use this branch until this ",
5070 "ambiguity is resolved.\n";
5073 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
5074 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
5075 command_noisy
('update-ref', $new_ref, $orig_ref);
5076 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
5079 command_close_pipe
($fh, $ctx);
5080 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
5084 sub migrate_from_v1
{
5085 my $git_dir = $ENV{GIT_DIR
};
5087 return $migrated unless -d
$git_dir;
5088 my $svn_dir = "$git_dir/svn";
5090 # just in case somebody used 'svn' as their $id at some point...
5091 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
5093 print STDERR
"Migrating from a git-svn v1 layout...\n";
5095 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
5096 "$svn_dir\n\t(required for this version ",
5097 "($::VERSION) of git-svn) does not exist.\n";
5098 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5101 next unless $x =~ s
#^refs/remotes/##;
5103 next unless -f
"$git_dir/$x/info/url";
5104 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
5106 my $dn = dirname
("$git_dir/svn/$x");
5107 mkpath
([$dn]) unless -d
$dn;
5108 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5109 mkpath
(["$git_dir/svn/svn"]);
5110 print STDERR
" - $git_dir/$x/info => ",
5111 "$git_dir/svn/$x/info\n";
5112 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5114 # don't worry too much about these, they probably
5115 # don't exist with repos this old (save for index,
5116 # and we can easily regenerate that)
5117 foreach my $f (qw
/unhandled.log index .rev_db/) {
5118 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5121 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
5122 rename "$git_dir/$x", "$git_dir/svn/$x" or
5127 command_close_pipe
($fh, $ctx);
5128 print STDERR
"Done migrating from a git-svn v1 layout\n";
5133 my ($l_map, $pfx, $path) = @_;
5135 foreach (<$path/*>) {
5136 if (-r
"$_/info/url") {
5137 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
5138 my $ref_id = $pfx . basename
$_;
5139 my $url = ::file_to_s
("$_/info/url");
5140 $l_map->{$ref_id} = $url;
5147 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5148 read_old_urls
($l_map, $x, $_);
5152 sub migrate_from_v2
{
5153 my @cfg = command
(qw
/config -l/);
5154 return if grep /^svn-remote\..+\.url=/, @cfg;
5156 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
5159 foreach my $ref_id (sort keys %l_map) {
5160 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
5162 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5169 sub minimize_connections
{
5170 my $r = Git
::SVN
::read_all_remotes
();
5172 my $root_repos = {};
5173 foreach my $repo_id (keys %$r) {
5174 my $url = $r->{$repo_id}->{url
} or next;
5175 my $fetch = $r->{$repo_id}->{fetch
} or next;
5176 my $ra = Git
::SVN
::Ra
->new($url);
5178 # skip existing cases where we already connect to the root
5179 if (($ra->{url
} eq $ra->{repos_root
}) ||
5180 ($ra->{repos_root
} eq $repo_id)) {
5181 $root_repos->{$ra->{url
}} = $repo_id;
5185 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
5186 my $root_path = $ra->{url
};
5187 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
5188 foreach my $path (keys %$fetch) {
5189 my $ref_id = $fetch->{$path};
5190 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
5192 # make sure we can read when connecting to
5193 # a higher level of a repository
5194 my ($last_rev, undef) = $gs->last_rev_commit;
5195 if (!defined $last_rev) {
5197 $root_ra->get_latest_revnum;
5201 my $new = $root_path;
5202 $new .= length $path ?
"/$path" : '';
5204 $root_ra->get_log([$new], $last_rev, $last_rev,
5208 $new_urls->{$ra->{repos_root
}}->{$new} =
5209 { ref_id
=> $ref_id,
5210 old_repo_id
=> $repo_id,
5211 old_path
=> $path };
5216 foreach my $url (keys %$new_urls) {
5217 # see if we can re-use an existing [svn-remote "repo_id"]
5218 # instead of creating a(n ugly) new section:
5219 my $repo_id = $root_repos->{$url} || $url;
5221 my $fetch = $new_urls->{$url};
5222 foreach my $path (keys %$fetch) {
5223 my $x = $fetch->{$path};
5224 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
5225 my $pfx = "svn-remote.$x->{old_repo_id}";
5227 my $old_fetch = quotemeta("$x->{old_path}:".
5228 "refs/remotes/$x->{ref_id}");
5229 command_noisy
(qw
/config --unset/,
5230 "$pfx.fetch", '^'. $old_fetch . '$');
5231 delete $r->{$x->{old_repo_id
}}->
5232 {fetch
}->{$x->{old_path
}};
5233 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
5234 command_noisy
(qw
/config --unset/,
5236 push @emptied, $x->{old_repo_id
}
5241 my $file = $ENV{GIT_CONFIG
} || "$ENV{GIT_DIR}/config";
5243 The following [svn-remote] sections in your config file ($file) are empty
5244 and can be safely removed:
5246 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
5250 sub migration_check
{
5254 minimize_connections
() if $_minimize;
5257 package Git
::IndexInfo
;
5260 use Git qw
/command_input_pipe command_close_pipe/;
5264 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
5265 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
5269 my ($self, $path) = @_;
5270 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
5271 return ++$self->{nr
};
5277 my ($self, $mode, $hash, $path) = @_;
5278 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
5279 return ++$self->{nr
};
5286 command_close_pipe
($self->{gui
}, $self->{ctx
});
5289 package Git
::SVN
::GlobSpec
;
5294 my ($class, $glob) = @_;
5296 $re =~ s!/+$!!g; # no need for trailing slashes
5297 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5299 my ($left, $right) = ($1, $3);
5301 my $depth = $re =~ tr/*/*/;
5302 if ($depth != $temp =~ tr/*/*/) {
5303 die "Only one set of wildcard directories " .
5304 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5307 die "One '*' is needed for glob: '$glob'\n";
5309 $re =~ s!\*!\[^/\]*!g;
5310 $re = quotemeta($left) . "($re)" . quotemeta($right);
5311 if (length $left && !($left =~ s!/+$!!g)) {
5312 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5314 if (length $right && !($right =~ s!^/+!!g)) {
5315 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5317 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
5318 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
5319 regex
=> qr/$re/, glob => $glob, depth
=> $depth }, $class;
5323 my ($self, $path) = @_;
5324 return (length $self->{left
} ?
"$self->{left}/" : '') .
5325 $path . (length $self->{right
} ?
"/$self->{right}" : '');
5333 $remotes = { # returned by read_all_remotes()
5335 # svn-remote.svn.url=https://svn.musicpd.org
5336 url
=> 'https://svn.musicpd.org',
5337 # svn-remote.svn.fetch=mpd/trunk:trunk
5339 'mpd/trunk' => 'trunk',
5341 # svn-remote.svn.tags=mpd/tags/*:tags/*
5346 regex
=> qr!mpd/tags/([^/]+)$!,
5352 regex
=> qr!tags/([^/]+)$!,
5359 $log_entry hashref as returned by libsvn_log_entry
()
5361 log => 'whitespace-formatted log entry
5362 ', # trailing newline is preserved
5363 revision
=> '8', # integer
5364 date
=> '2004-02-24T17:01:44.108345Z', # commit date
5365 author
=> 'committer name'
5369 # this is generated by generate_diff();
5370 @mods = array of diff
-index line hashes
, each element represents one line
5371 of diff
-index output
5373 diff
-index line
($m hash
)
5375 mode_a
=> first column of diff
-index output
, no leading
':',
5376 mode_b
=> second column of diff
-index output
,
5377 sha1_b
=> sha1sum of the final blob
,
5378 chg
=> change type
[MCRADT
],
5379 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
5380 file_b
=> new
/current file name of a file
(any chg
)
5384 # retval of read_url_paths{,_all}();
5386 # repository root url
5387 'https://svn.musicpd.org' => {
5388 # repository path # GIT_SVN_ID
5389 'mpd/trunk' => 'trunk',
5390 'mpd/tags/0.11.5' => 'tags/0.11.5',
5395 I don
't trust the each() function on unless I created %hash myself
5396 because the internal iterator may not have started at base.