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,
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 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
75 'authors-file|A=s' => \
$_authors,
76 'repack:i' => \
$Git::SVN
::_repack
,
77 'noMetadata' => \
$Git::SVN
::_no_metadata
,
78 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
79 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
80 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
81 'no-checkout' => \
$_no_checkout,
83 'repack-flags|repack-args|repack-opts=s' =>
84 \
$Git::SVN
::_repack_flags
,
85 'use-log-author' => \
$Git::SVN
::_use_log_author
,
86 'add-author-from' => \
$Git::SVN
::_add_author_from
,
89 my ($_trunk, $_tags, $_branches, $_stdlayout);
91 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
92 'trunk|T=s' => \
$_trunk, 'tags|t=s' => \
$_tags,
93 'branches|b=s' => \
$_branches, 'prefix=s' => \
$_prefix,
94 'stdlayout|s' => \
$_stdlayout,
95 'minimize-url|m' => \
$Git::SVN
::_minimize_url
,
96 'no-metadata' => sub { $icv{noMetadata
} = 1 },
97 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
98 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
99 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
101 my %cmt_opts = ( 'edit|e' => \
$_edit,
102 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
103 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
104 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
105 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
109 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
110 { 'revision|r=s' => \
$_revision,
111 'fetch-all|all' => \
$_fetch_all,
113 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
114 { 'revision|r=s' => \
$_revision,
115 %fc_opts, %init_opts } ],
116 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
117 " (requires URL argument)",
119 'multi-init' => [ \
&cmd_multi_init
,
120 "Deprecated alias for ".
121 "'$0 init -T<trunk> -b<branches> -t<tags>'",
123 dcommit
=> [ \
&cmd_dcommit
,
124 'Commit several diffs to merge with upstream',
125 { 'merge|m|M' => \
$_merge,
126 'strategy|s=s' => \
$_strategy,
127 'verbose|v' => \
$_verbose,
128 'dry-run|n' => \
$_dry_run,
129 'fetch-all|all' => \
$_fetch_all,
130 'no-rebase' => \
$_no_rebase,
131 %cmt_opts, %fc_opts } ],
132 'set-tree' => [ \
&cmd_set_tree
,
133 "Set an SVN repository to a git tree-ish",
134 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
135 'create-ignore' => [ \
&cmd_create_ignore
,
136 'Create a .gitignore per svn:ignore',
137 { 'revision|r=i' => \
$_revision
139 'propget' => [ \
&cmd_propget
,
140 'Print the value of a property on a file or directory',
141 { 'revision|r=i' => \
$_revision } ],
142 'proplist' => [ \
&cmd_proplist
,
143 'List all properties of a file or directory',
144 { 'revision|r=i' => \
$_revision } ],
145 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
146 { 'revision|r=i' => \
$_revision
148 'show-externals' => [ \
&cmd_show_externals
, "Show svn:externals listings",
149 { 'revision|r=i' => \
$_revision
151 'multi-fetch' => [ \
&cmd_multi_fetch
,
152 "Deprecated alias for $0 fetch --all",
153 { 'revision|r=s' => \
$_revision, %fc_opts } ],
154 'migrate' => [ sub { },
155 # no-op, we automatically run this anyways,
156 'Migrate configuration/metadata/layout from
157 previous versions of git-svn',
158 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
160 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
161 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
162 'revision|r=s' => \
$_revision,
163 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
164 'incremental' => \
$Git::SVN
::Log
::incremental
,
165 'oneline' => \
$Git::SVN
::Log
::oneline
,
166 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
167 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
168 'authors-file|A=s' => \
$_authors,
169 'color' => \
$Git::SVN
::Log
::color
,
170 'pager=s' => \
$Git::SVN
::Log
::pager
172 'find-rev' => [ \
&cmd_find_rev
, "Translate between SVN revision numbers and tree-ish",
174 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
175 { 'merge|m|M' => \
$_merge,
176 'verbose|v' => \
$_verbose,
177 'strategy|s=s' => \
$_strategy,
178 'local|l' => \
$_local,
179 'fetch-all|all' => \
$_fetch_all,
180 'dry-run|n' => \
$_dry_run,
182 'commit-diff' => [ \
&cmd_commit_diff
,
183 'Commit a diff between two trees',
184 { 'message|m=s' => \
$_message,
185 'file|F=s' => \
$_file,
186 'revision|r=s' => \
$_revision,
188 'info' => [ \
&cmd_info
,
189 "Show info about the latest SVN revision
190 on the current branch",
191 { 'url' => \
$_url, } ],
192 'blame' => [ \
&Git
::SVN
::Log
::cmd_blame
,
193 "Show what revision and author last modified each line of a file",
194 { 'git-format' => \
$_git_format } ],
198 for (my $i = 0; $i < @ARGV; $i++) {
199 if (defined $cmd{$ARGV[$i]}) {
206 # make sure we're always running at the top-level working directory
207 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
208 unless (-d
$ENV{GIT_DIR
}) {
209 if ($git_dir_user_set) {
210 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
211 "but it is not a directory\n";
213 my $git_dir = delete $ENV{GIT_DIR
};
214 chomp(my $cdup = command_oneline
(qw
/rev-parse --show-cdup/));
215 unless (length $cdup) {
216 die "Already at toplevel, but $git_dir ",
217 "not found '$cdup'\n";
219 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
220 unless (-d
$git_dir) {
221 die "$git_dir still not found after going to ",
224 $ENV{GIT_DIR
} = $git_dir;
226 $_repository = Git
->repository(Repository
=> $ENV{GIT_DIR
});
229 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
231 read_repo_config
(\
%opts);
232 Getopt
::Long
::Configure
('pass_through') if ($cmd && ($cmd eq 'log' || $cmd eq 'blame'));
233 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
234 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
235 'id|i=s' => \
$Git::SVN
::default_ref_id
,
236 'svn-remote|remote|R=s' => sub {
237 $Git::SVN
::no_reuse_existing
= 1;
238 $Git::SVN
::default_repo_id
= $_[1] });
239 exit 1 if (!$rv && $cmd && $cmd ne 'log');
242 version
() if $_version;
243 usage
(1) unless defined $cmd;
244 load_authors
() if $_authors;
246 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
247 Git
::SVN
::Migration
::migration_check
();
249 Git
::SVN
::init_vars
();
251 Git
::SVN
::verify_remotes_sanity
();
252 $cmd{$cmd}->[0]->(@ARGV);
255 post_fetch_checkout
();
258 ####################### primary functions ######################
260 my $exit = shift || 0;
261 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
263 git
-svn
- bidirectional operations between a single Subversion tree
and git
264 Usage
: git svn
<command
> [options
] [arguments
]\n
266 print $fd "Available commands:\n" unless $cmd;
268 foreach (sort keys %cmd) {
269 next if $cmd && $cmd ne $_;
270 next if /^multi-/; # don't show deprecated commands
271 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
272 foreach (sort keys %{$cmd{$_}->[2]}) {
273 # mixed-case options are for .git/config only
274 next if /[A-Z]/ && /^[a-z]+$/i;
275 # prints out arguments as they should be passed:
276 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
277 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
279 split /\|/,$_)," $x\n";
283 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
284 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
285 one git repository and want to keep them separate. See git-svn(1) for more
292 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
297 unless (-d $ENV{GIT_DIR}) {
298 my @init_db = ('init
');
299 push @init_db, "--template=$_template" if defined $_template;
300 if (defined $_shared) {
301 if ($_shared =~ /[a-z]/) {
302 push @init_db, "--shared=$_shared";
304 push @init_db, "--shared";
307 command_noisy(@init_db);
308 $_repository = Git->repository(Repository => ".git");
311 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
312 foreach my $i (keys %icv) {
313 die "'$set' and '$i' cannot both be set\n" if $set;
314 next unless defined $icv{$i};
315 command_noisy('config
', "$pfx.$i", $icv{$i});
321 my $repo_path = shift or return;
322 mkpath([$repo_path]) unless -d $repo_path;
323 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
324 $ENV{GIT_DIR} = '.git';
325 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
329 my ($url, $path) = @_;
330 if (!defined $path &&
331 (defined $_trunk || defined $_branches || defined $_tags ||
332 defined $_stdlayout) &&
333 $url !~ m#^[a-z\+]+://#) {
336 $path = basename($url) if !defined $path || !length $path;
337 cmd_init($url, $path);
338 Git::SVN::fetch_all($Git::SVN::default_repo_id);
342 if (defined $_stdlayout) {
343 $_trunk = 'trunk' if (!defined $_trunk);
344 $_tags = 'tags' if (!defined $_tags);
345 $_branches = 'branches' if (!defined $_branches);
347 if (defined $_trunk || defined $_branches || defined $_tags) {
348 return cmd_multi_init(@_);
350 my $url = shift or die "SVN repository location required
",
351 "as a command
-line argument
\n";
355 Git::SVN->init($url);
359 if (grep /^\d+=./, @_) {
360 die "'<rev>=<commit>' fetch arguments are
",
361 "no longer supported
.\n";
365 die "Usage
: $0 fetch
[--all
] [svn
-remote
]\n";
367 $remote ||= $Git::SVN::default_repo_id;
371 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
377 if ($_stdin || !@commits) {
378 print "Reading from stdin
...\n";
381 if (/\b($sha1_short)\b/o) {
382 unshift @commits, $1;
387 foreach my $c (@commits) {
388 my @tmp = command('rev-parse',$c);
389 if (scalar @tmp == 1) {
391 } elsif (scalar @tmp > 1) {
392 push @revs, reverse(command('rev-list',@tmp));
394 fatal "Failed to rev
-parse
$c";
397 my $gs = Git::SVN->new;
398 my ($r_last, $cmt_last) = $gs->last_rev_commit;
400 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
401 fatal "There are new revisions that were fetched
",
402 "and need to be merged
(or acknowledged
) ",
403 "before committing
.\nlast rev
: $r_last\n",
404 " current
: $gs->{last_rev
}";
406 $gs->set_tree($_) foreach @revs;
407 print "Done committing
",scalar @revs," revisions to SVN
\n";
413 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
414 'Cannot dcommit with a dirty index. Commit your changes first, '
415 . "or stash them with
`git stash'.\n";
418 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
420 print "Committing to $url ...\n";
423 die "Unable to determine upstream SVN information from ",
424 "$head history.\nPerhaps the repository is empty.";
427 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
428 if ($_no_rebase && scalar(@$linear_refs) > 1) {
429 warn "Attempting to commit more than one change while ",
430 "--no-rebase is enabled.\n",
431 "If these changes depend on each other, re-running ",
432 "without --no-rebase may be required."
435 my $d = shift @$linear_refs or last;
436 unless (defined $last_rev) {
437 (undef, $last_rev, undef) = cmt_metadata("$d~1");
438 unless (defined $last_rev) {
439 fatal "Unable to extract revision information ",
444 print "diff-tree $d~1 $d\n";
447 my %ed_opts = ( r => $last_rev,
448 log => get_commit_entry($d)->{log},
449 ra => Git::SVN::Ra->new($gs->full_url),
450 config => SVN::Core::config_get_config(
451 $Git::SVN::Ra::config_dir
456 print "Committed r$_[0]\n";
460 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
461 print "No changes\n$d~1 == $d\n";
462 } elsif ($parents->{$d} && @{$parents->{$d}}) {
463 $gs->{inject_parents_dcommit}->{$cmt_rev} =
466 $_fetch_all ? $gs->fetch_all : $gs->fetch;
467 $last_rev = $cmt_rev;
470 # we always want to rebase against the current HEAD,
471 # not any head that was passed to us
472 my @diff = command('diff-tree', $d,
476 @finish = rebase_cmd();
477 print STDERR "W: $d and ", $gs->refname,
478 " differ, using @finish:\n",
479 join("\n", @diff), "\n";
481 print "No changes between current HEAD and ",
483 "\nResetting to the latest ",
485 @finish = qw/reset --mixed/;
487 command_noisy(@finish, $gs->refname);
490 my ($url_, $rev_, $uuid_, $gs_) =
491 working_head_info($head, \@refs);
492 my ($linear_refs_, $parents_) =
493 linearize_history($gs_, \@refs);
494 if (scalar(@$linear_refs) !=
495 scalar(@$linear_refs_)) {
496 fatal "# of revisions changed ",
498 join("\n", @$linear_refs),
500 join("\n", @$linear_refs_), "\n",
501 'If you are attempting to commit ',
502 "merges, try running:\n\t",
503 'git rebase --interactive',
504 '--preserve-merges ',
506 "\nBefore dcommitting";
509 fatal "URL mismatch after rebase: ",
512 if ($uuid_ ne $uuid) {
513 fatal "uuid mismatch after rebase: ",
518 for ($i = 0; $i < scalar @$linear_refs; $i++) {
519 my $new = $linear_refs_->[$i] or next;
521 $parents->{$linear_refs->[$i]};
533 my $revision_or_hash = shift or die "SVN or git revision required ",
534 "as a command-line argument\n";
536 if ($revision_or_hash =~ /^r\d+$/) {
540 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
542 die "Unable to determine upstream SVN information from ",
545 my $desired_revision = substr($revision_or_hash, 1);
546 $result = $gs->rev_map_get($desired_revision, $uuid);
548 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
551 print "$result\n" if $result;
555 command_noisy(qw/update-index --refresh/);
556 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
558 die "Unable to determine upstream SVN information from ",
559 "working tree history\n";
562 print "Remote Branch: " . $gs->refname . "\n";
563 print "SVN URL: " . $url . "\n";
566 if (command(qw/diff-index HEAD --/)) {
567 print STDERR "Cannot rebase with uncommited changes:\n";
568 command_noisy('status');
572 # rebase will checkout for us, so no need to do it explicitly
573 $_no_checkout = 'true';
574 $_fetch_all ? $gs->fetch_all : $gs->fetch;
576 command_noisy(rebase_cmd(), $gs->refname);
579 sub cmd_show_ignore {
580 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
581 $gs ||= Git::SVN->new;
582 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
583 $gs->prop_walk($gs->{path}, $r, sub {
584 my ($gs, $path, $props) = @_;
585 print STDOUT "\n# $path\n";
586 my $s = $props->{'svn:ignore'} or return;
587 $s =~ s/[\r\n]+/\n/g;
594 sub cmd_show_externals {
595 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
596 $gs ||= Git::SVN->new;
597 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
598 $gs->prop_walk($gs->{path}, $r, sub {
599 my ($gs, $path, $props) = @_;
600 print STDOUT "\n# $path\n";
601 my $s = $props->{'svn:externals'} or return;
602 $s =~ s/[\r\n]+/\n/g;
609 sub cmd_create_ignore {
610 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
611 $gs ||= Git::SVN->new;
612 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
613 $gs->prop_walk($gs->{path}, $r, sub {
614 my ($gs, $path, $props) = @_;
615 # $path is of the form /path/to/dir/
616 my $ignore = '.' . $path . '.gitignore';
617 my $s = $props->{'svn:ignore'} or return;
618 open(GITIGNORE, '>', $ignore)
619 or fatal("Failed to open `$ignore' for writing: $!");
620 $s =~ s/[\r\n]+/\n/g;
622 # Prefix all patterns so that the ignore doesn't apply
623 # to sub-directories.
625 print GITIGNORE
"$s\n";
627 or fatal
("Failed to close `$ignore': $!");
628 command_noisy
('add', '-f', $ignore);
632 sub canonicalize_path
{
634 my $dot_slash_added = 0;
635 if (substr($path, 0, 1) ne "/") {
636 $path = "./" . $path;
637 $dot_slash_added = 1;
639 # File::Spec->canonpath doesn't collapse x/../y into y (for a
640 # good reason), so let's do this manually.
642 $path =~ s
#/\.(?:/|$)#/#g;
643 $path =~ s
#/[^/]+/\.\.##g;
645 $path =~ s
#^\./## if $dot_slash_added;
653 # Helper for cmd_propget and cmd_proplist below.
656 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
657 $gs ||= Git
::SVN
->new;
659 # prefix THE PATH by the sub-directory from which the user
661 $path = $cmd_dir_prefix . $path;
662 fatal
("No such file or directory: $path") unless -e
$path;
663 my $is_dir = -d
$path ?
1 : 0;
664 $path = $gs->{path
} . '/' . $path;
666 # canonicalize the path (otherwise libsvn will abort or fail to
668 $path = canonicalize_path
($path);
670 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
673 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
676 (undef, $props) = $gs->ra->get_file($path, $r, undef);
681 # cmd_propget (PROP, PATH)
682 # ------------------------
683 # Print the SVN property PROP for PATH.
685 my ($prop, $path) = @_;
686 $path = '.' if not defined $path;
687 usage
(1) if not defined $prop;
688 my $props = get_svnprops
($path);
689 if (not defined $props->{$prop}) {
690 fatal
("`$path' does not have a `$prop' SVN property.");
692 print $props->{$prop} . "\n";
695 # cmd_proplist (PATH)
696 # -------------------
697 # Print the list of SVN properties for PATH.
700 $path = '.' if not defined $path;
701 my $props = get_svnprops
($path);
702 print "Properties on '$path':\n";
703 foreach (sort keys %{$props}) {
710 unless (defined $_trunk || defined $_branches || defined $_tags) {
714 # there are currently some bugs that prevent multi-init/multi-fetch
715 # setups from working well without this.
716 $Git::SVN
::_minimize_url
= 1;
718 $_prefix = '' unless defined $_prefix;
724 if (defined $_trunk) {
725 my $trunk_ref = $_prefix . 'trunk';
726 # try both old-style and new-style lookups:
727 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
729 my ($trunk_url, $trunk_path) =
730 complete_svn_url
($url, $_trunk);
731 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
735 return unless defined $_branches || defined $_tags;
736 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
737 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
738 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
741 sub cmd_multi_fetch
{
742 my $remotes = Git
::SVN
::read_all_remotes
();
743 foreach my $repo_id (sort keys %$remotes) {
744 if ($remotes->{$repo_id}->{url
}) {
745 Git
::SVN
::fetch_all
($repo_id, $remotes);
750 # this command is special because it requires no metadata
751 sub cmd_commit_diff
{
752 my ($ta, $tb, $url) = @_;
753 my $usage = "Usage: $0 commit-diff -r<revision> ".
754 "<tree-ish> <tree-ish> [<URL>]";
755 fatal
($usage) if (!defined $ta || !defined $tb);
758 my $gs = eval { Git
::SVN
->new };
760 fatal
("Needed URL or usable git-svn --id in ",
761 "the command-line\n", $usage);
764 $svn_path = $gs->{path
};
766 unless (defined $_revision) {
767 fatal
("-r|--revision is a required argument\n", $usage);
769 if (defined $_message && defined $_file) {
770 fatal
("Both --message/-m and --file/-F specified ",
771 "for the commit message.\n",
772 "I have no idea what you mean");
774 if (defined $_file) {
775 $_message = file_to_s
($_file);
777 $_message ||= get_commit_entry
($tb)->{log};
779 my $ra ||= Git
::SVN
::Ra
->new($url);
782 $r = $ra->get_latest_revnum;
783 } elsif ($r !~ /^\d+$/) {
784 die "revision argument: $r not understood by git-svn\n";
786 my %ed_opts = ( r
=> $r,
791 editor_cb
=> sub { print "Committed r$_[0]\n" },
792 svn_path
=> $svn_path );
793 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
794 print "No changes\n$ta == $tb\n";
799 my $path = canonicalize_path
(shift or ".");
800 unless (scalar(@_) == 0) {
801 die "Too many arguments specified\n";
804 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
806 if (!$file_type && !$diff_status) {
807 print STDERR
"$path: (Not a versioned resource)\n\n";
811 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
813 die "Unable to determine upstream SVN information from ",
814 "working tree history\n";
816 my $full_url = $url . ($path eq "." ?
"" : "/$path");
819 print $full_url, "\n";
823 my $result = "Path: $path\n";
824 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
825 $result .= "URL: " . $full_url . "\n";
828 my $repos_root = $gs->repos_root;
829 Git
::SVN
::remove_username
($repos_root);
830 $result .= "Repository Root: $repos_root\n";
833 $result .= "Repository Root: (offline)\n";
835 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
836 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
838 $result .= "Node Kind: " .
839 ($file_type eq "dir" ?
"directory" : "file") . "\n";
841 my $schedule = $diff_status eq "A"
843 : ($diff_status eq "D" ?
"delete" : "normal");
844 $result .= "Schedule: $schedule\n";
846 if ($diff_status eq "A") {
851 my ($lc_author, $lc_rev, $lc_date_utc);
852 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $path);
853 my $log = command_output_pipe
(@args);
854 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
856 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
858 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
859 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
860 (undef, $lc_rev, undef) = ::extract_metadata
($1);
865 Git
::SVN
::Log
::set_local_timezone
();
867 $result .= "Last Changed Author: $lc_author\n";
868 $result .= "Last Changed Rev: $lc_rev\n";
869 $result .= "Last Changed Date: " .
870 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
872 if ($file_type ne "dir") {
873 my $text_last_updated_date =
874 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
876 "Text Last Updated: " .
877 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
880 if ($diff_status eq "D") {
882 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
883 if ($file_type eq "link") {
884 my $file_name = <$fh>;
885 $checksum = md5sum
("link $file_name");
887 $checksum = md5sum
($fh);
889 command_close_pipe
($fh, $ctx);
890 } elsif ($file_type eq "link") {
892 command
(qw(cat-file blob), "HEAD:$path");
894 md5sum
("link " . $file_name);
896 open FILE
, "<", $path or die $!;
897 $checksum = md5sum
(\
*FILE
);
898 close FILE
or die $!;
900 $result .= "Checksum: " . $checksum . "\n";
906 ########################### utility functions #########################
909 my @cmd = qw
/rebase/;
910 push @cmd, '-v' if $_verbose;
911 push @cmd, qw
/--merge/ if $_merge;
912 push @cmd, "--strategy=$_strategy" if $_strategy;
916 sub post_fetch_checkout
{
917 return if $_no_checkout;
918 my $gs = $Git::SVN
::_head
or return;
919 return if verify_ref
('refs/heads/master^0');
921 my $valid_head = verify_ref
('HEAD^0');
922 command_noisy
(qw(update-ref refs/heads/master), $gs->refname);
923 return if ($valid_head || !verify_ref
('HEAD^0'));
925 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
926 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
929 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
930 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
931 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
932 print STDERR
"Checked out HEAD:\n ",
933 $gs->full_url, " r", $gs->last_rev, "\n";
936 sub complete_svn_url
{
937 my ($url, $path) = @_;
939 if ($path !~ m
#^[a-z\+]+://#) {
940 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
941 fatal
("E: '$path' is not a complete URL ",
942 "and a separate URL is not specified");
944 return ($url, $path);
949 sub complete_url_ls_init
{
950 my ($ra, $repo_path, $switch, $pfx) = @_;
951 unless ($repo_path) {
952 print STDERR
"W: $switch not specified\n";
955 $repo_path =~ s
#/+$##;
956 if ($repo_path =~ m
#^[a-z\+]+://#) {
957 $ra = Git
::SVN
::Ra
->new($repo_path);
960 $repo_path =~ s
#^/+##;
962 fatal
("E: '$repo_path' is not a complete URL ",
963 "and a separate URL is not specified");
966 my $url = $ra->{url
};
967 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
968 my $k = "svn-remote.$gs->{repo_id}.url";
969 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
970 if ($orig_url && ($orig_url ne $gs->{url
})) {
971 die "$k already set: $orig_url\n",
972 "wanted to set to: $gs->{url}\n";
974 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
975 my $remote_path = "$ra->{svn_path}/$repo_path";
976 $remote_path =~ s
#/+#/#g;
977 $remote_path =~ s
#^/##g;
978 $remote_path .= "/*" if $remote_path !~ /\
*/;
979 my ($n) = ($switch =~ /^--(\w+)/);
980 if (length $pfx && $pfx !~ m
#/$#) {
981 die "--prefix='$pfx' must have a trailing slash '/'\n";
983 command_noisy
('config', "svn-remote.$gs->{repo_id}.$n",
984 "$remote_path:refs/remotes/$pfx*");
989 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
993 sub get_tree_from_treeish
{
995 # $treeish can be a symbolic ref, too:
996 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
998 while ($type eq 'tag') {
999 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1001 if ($type eq 'commit') {
1002 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1004 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1005 die "Unable to get tree from $treeish\n" unless $expected;
1006 } elsif ($type eq 'tree') {
1007 $expected = $treeish;
1009 die "$treeish is a $type, expected tree, tag or commit\n";
1014 sub get_commit_entry
{
1015 my ($treeish) = shift;
1016 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
1017 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1018 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1019 open my $log_fh, '>', $commit_editmsg or croak
$!;
1021 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1022 if ($type eq 'commit' || $type eq 'tag') {
1023 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1031 $in_msg = 1 if (/^\s*$/);
1032 $author = $1 if (/^author (.*>)/);
1033 } elsif (/^git-svn-id: /) {
1034 # skip this for now, we regenerate the
1035 # correct one on re-fetch anyways
1036 # TODO: set *:merge properties or like...
1038 if (/^From:/ || /^Signed-off-by:/) {
1044 $msgbuf =~ s/\s+$//s;
1045 if ($Git::SVN
::_add_author_from
&& defined($author)
1047 $msgbuf .= "\n\nFrom: $author";
1049 print $log_fh $msgbuf or croak
$!;
1050 command_close_pipe
($msg_fh, $ctx);
1052 close $log_fh or croak
$!;
1054 if ($_edit || ($type eq 'tree')) {
1055 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1056 # TODO: strip out spaces, comments, like git-commit.sh
1057 system($editor, $commit_editmsg);
1059 rename $commit_editmsg, $commit_msg or croak
$!;
1060 open $log_fh, '<', $commit_msg or croak
$!;
1061 { local $/; chomp($log_entry{log} = <$log_fh>); }
1062 close $log_fh or croak
$!;
1068 my ($str, $file, $mode) = @_;
1069 open my $fd,'>',$file or croak
$!;
1070 print $fd $str,"\n" or croak
$!;
1071 close $fd or croak
$!;
1072 chmod ($mode &~ umask, $file) if (defined $mode);
1077 open my $fd,'<',$file or croak
"$!: file: $file\n";
1080 close $fd or croak
$!;
1085 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1087 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1088 my $log = $cmd eq 'log';
1089 while (<$authors>) {
1091 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1092 my ($user, $name, $email) = ($1, $2, $3);
1094 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1096 $users{$user} = [$name, $email];
1099 close $authors or croak
$!;
1102 # convert GetOpt::Long specs for use by git-config
1103 sub read_repo_config
{
1104 return unless -d
$ENV{GIT_DIR
};
1107 foreach my $o (keys %$opts) {
1108 # if we have mixedCase and a long option-only, then
1109 # it's a config-only variable that we don't need for
1111 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1112 my $v = $opts->{$o};
1113 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1115 my $arg = 'git-config';
1116 $arg .= ' --int' if ($o =~ /[:=]i$/);
1117 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1118 if (ref $v eq 'ARRAY') {
1119 chomp(my @tmp = `$arg --get-all svn.$key`);
1122 chomp(my $tmp = `$arg --get svn.$key`);
1123 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1128 delete @
$opts{@config_only} if @config_only;
1131 sub extract_metadata
{
1132 my $id = shift or return (undef, undef, undef);
1133 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1135 if (!defined $rev || !$uuid || !$url) {
1136 # some of the original repositories I made had
1137 # identifiers like this:
1138 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1140 return ($url, $rev, $uuid);
1144 return extract_metadata
((grep(/^git-svn-id: /,
1145 command
(qw
/cat-file commit/, shift)))[-1]);
1148 sub working_head_info
{
1149 my ($head, $refs) = @_;
1150 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
1151 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1155 if ( m{^commit ($::sha1)$} ) {
1156 unshift @
$refs, $hash if $hash and $refs;
1160 next unless s{^\s*(git-svn-id:)}{$1};
1161 my ($url, $rev, $uuid) = extract_metadata
($_);
1162 if (defined $url && defined $rev) {
1163 next if $max{$url} and $max{$url} < $rev;
1164 if (my $gs = Git
::SVN
->find_by_url($url)) {
1165 my $c = $gs->rev_map_get($rev, $uuid);
1166 if ($c && $c eq $hash) {
1167 close $fh; # break the pipe
1168 return ($url, $rev, $uuid, $gs);
1170 $max{$url} ||= $gs->rev_map_max;
1175 command_close_pipe
($fh, $ctx);
1176 (undef, undef, undef, undef);
1179 sub read_commit_parents
{
1180 my ($parents, $c) = @_;
1181 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1182 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1183 @
{$parents->{$c}} = split(/ /, $p);
1186 sub linearize_history
{
1187 my ($gs, $refs) = @_;
1189 foreach my $c (@
$refs) {
1190 read_commit_parents
(\
%parents, $c);
1195 my $last_svn_commit = $gs->last_commit;
1196 foreach my $c (reverse @
$refs) {
1197 next if $c eq $last_svn_commit;
1200 unshift @linear_refs, $c;
1203 # we only want the first parent to diff against for linear
1204 # history, we save the rest to inject when we finalize the
1206 my $fp_a = verify_ref
("$c~1");
1207 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1208 if (!$fp_a || !$fp_b) {
1210 "has no parent commit, and therefore ",
1211 "nothing to diff against.\n",
1212 "You should be working from a repository ",
1213 "originally created by git-svn\n";
1215 if ($fp_a ne $fp_b) {
1216 die "$c~1 = $fp_a, however parsing commit $c ",
1217 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1220 foreach my $p (@
{$parents{$c}}) {
1224 (\
@linear_refs, \
%parents);
1227 sub find_file_type_and_diff_status
{
1229 return ('dir', '') if $path eq '';
1232 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1233 my $diff_status = (split(' ', $diff_output))[0] || "";
1235 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1237 return (undef, undef) if !$diff_status && !$ls_tree;
1239 if ($diff_status eq "A") {
1240 return ("link", $diff_status) if -l
$path;
1241 return ("dir", $diff_status) if -d
$path;
1242 return ("file", $diff_status);
1245 my $mode = (split(' ', $ls_tree))[0] || "";
1247 return ("link", $diff_status) if $mode eq "120000";
1248 return ("dir", $diff_status) if $mode eq "040000";
1249 return ("file", $diff_status);
1255 my $md5 = Digest
::MD5
->new();
1256 if ($ref eq 'GLOB' || $ref eq 'IO::File') {
1257 $md5->addfile($arg) or croak
$!;
1258 } elsif ($ref eq 'SCALAR') {
1259 $md5->add($$arg) or croak
$!;
1261 $md5->add($arg) or croak
$!;
1263 ::fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1265 return $md5->hexdigest();
1271 use Fcntl qw
/:DEFAULT :seek/;
1272 use constant rev_map_fmt
=> 'NH40';
1273 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
1274 $_repack $_repack_flags $_use_svm_props $_head
1275 $_use_svnsync_props $no_reuse_existing $_minimize_url
1276 $_use_log_author $_add_author_from/;
1278 use File
::Path qw
/mkpath/;
1279 use File
::Copy qw
/copy/;
1282 my ($_gc_nr, $_gc_period);
1284 # properties that we do not log:
1287 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
1288 svn
:special svn
:executable
1289 svn
:entry
:committed
-rev
1290 svn
:entry
:last-author
1292 svn
:entry
:committed
-date
/;
1294 # some options are read globally, but can be overridden locally
1295 # per [svn-remote "..."] section. Command-line options will *NOT*
1296 # override options set in an [svn-remote "..."] section
1298 for my $option (qw
/follow_parent no_metadata use_svm_props
1299 use_svnsync_props
/) {
1302 my $prop = "-$option";
1305 return $self->{$prop} if exists $self->{$prop};
1306 my $k = "svn-remote.$self->{repo_id}.$key";
1307 eval { command_oneline
(qw
/config --get/, $k) };
1309 $self->{$prop} = ${"Git::SVN::_$option"};
1311 my $v = command_oneline
(qw
/config --bool/,$k);
1312 $self->{$prop} = $v eq 'false' ?
0 : 1;
1314 return $self->{$prop};
1319 my (%LOCKFILES, %INDEX_FILES);
1321 unlink keys %LOCKFILES if %LOCKFILES;
1322 unlink keys %INDEX_FILES if %INDEX_FILES;
1325 sub resolve_local_globs
{
1326 my ($url, $fetch, $glob_spec) = @_;
1327 return unless defined $glob_spec;
1328 my $ref = $glob_spec->{ref};
1329 my $path = $glob_spec->{path
};
1330 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
1331 next unless m
#^refs/remotes/$ref->{regex}$#;
1333 my $pathname = desanitize_refname
($path->full_path($p));
1334 my $refname = desanitize_refname
($ref->full_path($p));
1335 if (my $existing = $fetch->{$pathname}) {
1336 if ($existing ne $refname) {
1337 die "Refspec conflict:\n",
1338 "existing: refs/remotes/$existing\n",
1339 " globbed: refs/remotes/$refname\n";
1341 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
1342 $u =~ s!^\Q$url\E(/|$)!! or die
1343 "refs/remotes/$refname: '$url' not found in '$u'\n";
1344 if ($pathname ne $u) {
1345 warn "W: Refspec glob conflict ",
1346 "(ref: refs/remotes/$refname):\n",
1347 "expected path: $pathname\n",
1349 "Continuing ahead with $u\n";
1353 $fetch->{$pathname} = $refname;
1358 sub parse_revision_argument
{
1359 my ($base, $head) = @_;
1360 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
1361 return ($base, $head);
1363 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
1364 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
1365 return ($head, $head) if ($::_revision
eq 'HEAD');
1366 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
1367 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
1368 die "revision argument: $::_revision not understood by git-svn\n";
1372 my ($repo_id, $remotes) = @_;
1376 $repo_id = $gs->{repo_id
};
1378 $remotes ||= read_all_remotes
();
1379 my $remote = $remotes->{$repo_id} or
1380 die "[svn-remote \"$repo_id\"] unknown\n";
1381 my $fetch = $remote->{fetch
};
1382 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
1384 my $ra = Git
::SVN
::Ra
->new($url);
1385 my $uuid = $ra->get_uuid;
1386 my $head = $ra->get_latest_revnum;
1387 my $base = defined $fetch ?
$head : 0;
1389 # read the max revs for wildcard expansion (branches/*, tags/*)
1390 foreach my $t (qw
/branches tags/) {
1391 defined $remote->{$t} or next;
1392 push @globs, $remote->{$t};
1393 my $max_rev = eval { tmp_config
(qw
/--int --get/,
1394 "svn-remote.$repo_id.${t}-maxRev") };
1395 if (defined $max_rev && ($max_rev < $base)) {
1397 } elsif (!defined $max_rev) {
1403 foreach my $p (sort keys %$fetch) {
1404 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
1405 my $lr = $gs->rev_map_max;
1407 $base = $lr if ($lr < $base);
1413 ($base, $head) = parse_revision_argument
($base, $head);
1414 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
1417 sub read_all_remotes
{
1419 my $use_svm_props = eval { command_oneline
(qw
/config
--bool
1420 svn
.useSvmProps
/) };
1421 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
1422 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
1423 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1424 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1425 die("svn-remote.$remote: remote ref '$_remote_ref' "
1426 . "must start with 'refs/remotes/'\n")
1427 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1428 my $remote_ref = $1;
1429 $local_ref =~ s{^/}{};
1430 $r->{$remote}->{fetch
}->{$local_ref} = $remote_ref;
1431 $r->{$remote}->{svm
} = {} if $use_svm_props;
1432 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1433 $r->{$1}->{svm
} = {};
1434 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1435 $r->{$1}->{url
} = $2;
1436 } elsif (m
!^(.+)\
.(branches
|tags
)=
1437 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
1438 my ($p, $g) = ($3, $4);
1439 my $rs = $r->{$1}->{$2} = {
1442 path
=> Git
::SVN
::GlobSpec
->new($p),
1443 ref => Git
::SVN
::GlobSpec
->new($g) };
1444 if (length($rs->{ref}->{right
}) != 0) {
1445 die "The '*' glob character must be the last ",
1446 "character of '$g'\n";
1452 if (defined $r->{$_}->{svm
}) {
1455 my $section = "svn-remote.$_";
1457 source
=> tmp_config
('--get',
1458 "$section.svm-source"),
1459 replace
=> tmp_config
('--get',
1460 "$section.svm-replace"),
1463 $r->{$_}->{svm
} = $svm;
1471 $_gc_nr = $_gc_period = 1000;
1472 if (defined $_repack || defined $_repack_flags) {
1473 warn "Repack options are obsolete; they have no effect.\n";
1477 sub verify_remotes_sanity
{
1478 return unless -d
$ENV{GIT_DIR
};
1480 foreach (command
(qw
/config -l/)) {
1481 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1483 die "Remote ref refs/remote/$1 is tracked by",
1484 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1485 "Please resolve this ambiguity in ",
1486 "your git configuration file before ",
1494 sub find_existing_remote
{
1495 my ($url, $remotes) = @_;
1496 return undef if $no_reuse_existing;
1498 foreach my $repo_id (keys %$remotes) {
1499 my $u = $remotes->{$repo_id}->{url
} or next;
1501 $existing = $repo_id;
1507 sub init_remote_config
{
1508 my ($self, $url, $no_write) = @_;
1509 $url =~ s!/+$!!; # strip trailing slash
1510 my $r = read_all_remotes
();
1511 my $existing = find_existing_remote
($url, $r);
1513 unless ($no_write) {
1514 print STDERR
"Using existing ",
1515 "[svn-remote \"$existing\"]\n";
1517 $self->{repo_id
} = $existing;
1518 } elsif ($_minimize_url) {
1519 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1520 $existing = find_existing_remote
($min_url, $r);
1522 unless ($no_write) {
1523 print STDERR
"Using existing ",
1524 "[svn-remote \"$existing\"]\n";
1526 $self->{repo_id
} = $existing;
1528 if ($min_url ne $url) {
1529 unless ($no_write) {
1530 print STDERR
"Using higher level of URL: ",
1531 "$url => $min_url\n";
1533 my $old_path = $self->{path
};
1534 $self->{path
} = $url;
1535 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1536 if (length $old_path) {
1537 $self->{path
} .= "/$old_path";
1544 # verify that we aren't overwriting anything:
1546 command_oneline
('config', '--get',
1547 "svn-remote.$self->{repo_id}.url")
1549 if ($orig_url && ($orig_url ne $url)) {
1550 die "svn-remote.$self->{repo_id}.url already set: ",
1551 "$orig_url\nwanted to set to: $url\n";
1554 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1555 if (defined $xpath) {
1556 die "svn-remote.$xrepo_id.fetch already set to track ",
1557 "$xpath:refs/remotes/", $self->refname, "\n";
1559 unless ($no_write) {
1560 command_noisy
('config',
1561 "svn-remote.$self->{repo_id}.url", $url);
1562 $self->{path
} =~ s{^/}{};
1563 command_noisy
('config', '--add',
1564 "svn-remote.$self->{repo_id}.fetch",
1565 "$self->{path}:".$self->refname);
1567 $self->{url
} = $url;
1570 sub find_by_url
{ # repos_root and, path are optional
1571 my ($class, $full_url, $repos_root, $path) = @_;
1573 return undef unless defined $full_url;
1574 remove_username
($full_url);
1575 remove_username
($repos_root) if defined $repos_root;
1576 my $remotes = read_all_remotes
();
1577 if (defined $full_url && defined $repos_root && !defined $path) {
1579 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1581 foreach my $repo_id (keys %$remotes) {
1582 my $u = $remotes->{$repo_id}->{url
} or next;
1583 remove_username
($u);
1584 next if defined $repos_root && $repos_root ne $u;
1586 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1587 foreach (qw
/branches tags/) {
1588 resolve_local_globs
($u, $fetch,
1589 $remotes->{$repo_id}->{$_});
1592 my $rwr = rewrite_root
({repo_id
=> $repo_id});
1593 my $svm = $remotes->{$repo_id}->{svm
}
1594 if defined $remotes->{$repo_id}->{svm
};
1595 unless (defined $p) {
1601 } elsif (defined $svm) {
1602 $z = $svm->{source
};
1603 $prefix = $svm->{replace
};
1604 $prefix =~ s
#^\Q$u\E(?:/|$)##;
1607 $p =~ s
#^\Q$z\E(?:/|$)#$prefix# or next;
1609 foreach my $f (keys %$fetch) {
1611 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1618 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1619 my $self = _new
($class, $repo_id, $ref_id, $path);
1621 $self->init_remote_config($url, $no_write);
1628 foreach (command
(qw
/config -l/)) {
1629 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1630 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1631 my ($repo_id, $path, $ref) = ($1, $2, $3);
1632 if ($ref eq $ref_id) {
1633 $path = '' if ($path =~ m
#^\./?#);
1634 return ($repo_id, $path);
1637 (undef, undef, undef);
1641 my ($class, $ref_id, $repo_id, $path) = @_;
1642 if (defined $ref_id && !defined $repo_id && !defined $path) {
1643 ($repo_id, $path) = find_ref
($ref_id);
1644 if (!defined $repo_id) {
1645 die "Could not find a \"svn-remote.*.fetch\" key ",
1646 "in the repository configuration matching: ",
1647 "refs/remotes/$ref_id\n";
1650 my $self = _new
($class, $repo_id, $ref_id, $path);
1651 if (!defined $self->{path
} || !length $self->{path
}) {
1652 my $fetch = command_oneline
('config', '--get',
1653 "svn-remote.$repo_id.fetch",
1654 ":refs/remotes/$ref_id\$") or
1655 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1656 "\":refs/remotes/$ref_id\$\" in config\n";
1657 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1659 $self->{url
} = command_oneline
('config', '--get',
1660 "svn-remote.$repo_id.url") or
1661 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1667 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1669 # It cannot end with a slash /, we'll throw up on this because
1670 # SVN can't have directories with a slash in their name, either:
1671 if ($refname =~ m{/$}) {
1672 die "ref: '$refname' ends with a trailing slash, this is ",
1673 "not permitted by git nor Subversion\n";
1676 # It cannot have ASCII control character space, tilde ~, caret ^,
1677 # colon :, question-mark ?, asterisk *, space, or open bracket [
1680 # Additionally, % must be escaped because it is used for escaping
1681 # and we want our escaped refname to be reversible
1682 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1684 # no slash-separated component can begin with a dot .
1686 $refname =~ s{/\.}{/%2E}g;
1688 # It cannot have two consecutive dots .. anywhere
1690 $refname =~ s{\.\.}{%2E%2E}g;
1695 sub desanitize_refname
{
1697 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1703 return $self->{svm
}->{uuid
} if $self->svm;
1705 unless ($self->{svm
}) {
1706 die "SVM UUID not cached, and reading remotely failed\n";
1708 $self->{svm
}->{uuid
};
1713 return $self->{svm
} if $self->{svm
};
1715 # see if we have it in our config, first:
1717 my $section = "svn-remote.$self->{repo_id}";
1719 source
=> tmp_config
('--get', "$section.svm-source"),
1720 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1721 replace
=> tmp_config
('--get', "$section.svm-replace"),
1724 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1725 $self->{svm
} = $svm;
1731 my ($self, $ra) = @_;
1732 return $ra if $self->svm;
1734 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1735 "(svm:source, svm:uuid) ",
1736 "from the following URLs:\n" );
1737 sub read_svm_props
{
1738 my ($self, $ra, $path, $r) = @_;
1739 my $props = ($ra->get_dir($path, $r))[2];
1740 my $src = $props->{'svm:source'};
1741 my $uuid = $props->{'svm:uuid'};
1742 return undef if (!$src || !$uuid);
1746 $uuid =~ m{^[0-9a-f\-]{30,}$}
1747 or die "doesn't look right - svm:uuid is '$uuid'\n";
1749 # the '!' is used to mark the repos_root!/relative/path
1750 $src =~ s{/?!/?}{/};
1751 $src =~ s{/+$}{}; # no trailing slashes please
1752 # username is of no interest
1753 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1755 my $replace = $ra->{url
};
1756 $replace .= "/$path" if length $path;
1758 my $section = "svn-remote.$self->{repo_id}";
1759 tmp_config
("$section.svm-source", $src);
1760 tmp_config
("$section.svm-replace", $replace);
1761 tmp_config
("$section.svm-uuid", $uuid);
1769 my $r = $ra->get_latest_revnum;
1770 my $path = $self->{path
};
1772 while (length $path) {
1773 unless ($tried{"$self->{url}/$path"}) {
1774 return $ra if $self->read_svm_props($ra, $path, $r);
1775 $tried{"$self->{url}/$path"} = 1;
1777 $path =~ s
#/?[^/]+$##;
1779 die "Path: '$path' should be ''\n" if $path ne '';
1780 return $ra if $self->read_svm_props($ra, $path, $r);
1781 $tried{"$self->{url}/$path"} = 1;
1783 if ($ra->{repos_root
} eq $self->{url
}) {
1784 die @err, (map { " $_\n" } keys %tried), "\n";
1787 # nope, make sure we're connected to the repository root:
1790 $path = $ra->{svn_path
};
1791 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
1792 while (length $path) {
1793 unless ($tried{"$ra->{url}/$path"}) {
1794 $ok = $self->read_svm_props($ra, $path, $r);
1796 $tried{"$ra->{url}/$path"} = 1;
1798 $path =~ s
#/?[^/]+$##;
1800 die "Path: '$path' should be ''\n" if $path ne '';
1801 $ok ||= $self->read_svm_props($ra, $path, $r);
1802 $tried{"$ra->{url}/$path"} = 1;
1804 die @err, (map { " $_\n" } keys %tried), "\n";
1806 Git
::SVN
::Ra
->new($self->{url
});
1811 return $self->{svnsync
} if $self->{svnsync
};
1813 if ($self->no_metadata) {
1814 die "Can't have both 'noMetadata' and ",
1815 "'useSvnsyncProps' options set!\n";
1817 if ($self->rewrite_root) {
1818 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1823 # see if we have it in our config, first:
1825 my $section = "svn-remote.$self->{repo_id}";
1827 my $url = tmp_config
('--get', "$section.svnsync-url");
1828 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1829 die "doesn't look right - svn:sync-from-url is '$url'\n";
1831 my $uuid = tmp_config
('--get', "$section.svnsync-uuid");
1832 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1833 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1835 $svnsync = { url
=> $url, uuid
=> $uuid }
1837 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
1838 return $self->{svnsync
} = $svnsync;
1841 my $err = "useSvnsyncProps set, but failed to read " .
1842 "svnsync property: svn:sync-from-";
1843 my $rp = $self->ra->rev_proplist(0);
1845 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1846 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1847 die "doesn't look right - svn:sync-from-url is '$url'\n";
1849 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1850 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1851 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1853 my $section = "svn-remote.$self->{repo_id}";
1854 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
1855 tmp_config
('--add', "$section.svnsync-url", $url);
1856 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
1859 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1860 # remote lookup (useful for 'git svn log').
1863 unless ($self->{ra_uuid
}) {
1864 my $key = "svn-remote.$self->{repo_id}.uuid";
1865 my $uuid = eval { tmp_config
('--get', $key) };
1866 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1867 $self->{ra_uuid
} = $uuid;
1869 die "ra_uuid called without URL\n" unless $self->{url
};
1870 $self->{ra_uuid
} = $self->ra->get_uuid;
1871 tmp_config
('--add', $key, $self->{ra_uuid
});
1877 sub _set_repos_root
{
1878 my ($self, $repos_root) = @_;
1879 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1880 $repos_root ||= $self->ra->{repos_root
};
1881 tmp_config
($k, $repos_root);
1887 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1888 eval { tmp_config
('--get', $k) } || $self->_set_repos_root;
1893 my $ra = Git
::SVN
::Ra
->new($self->{url
});
1894 $self->_set_repos_root($ra->{repos_root
});
1895 if ($self->use_svm_props && !$self->{svm
}) {
1896 if ($self->no_metadata) {
1897 die "Can't have both 'noMetadata' and ",
1898 "'useSvmProps' options set!\n";
1899 } elsif ($self->use_svnsync_props) {
1900 die "Can't have both 'useSvnsyncProps' and ",
1901 "'useSvmProps' options set!\n";
1903 $ra = $self->_set_svm_vars($ra);
1904 $self->{-want_revprops
} = 1;
1911 my $repos_root = $self->ra->{repos_root
};
1912 return $self->{path
} if ($self->{url
} eq $repos_root);
1913 my $url = $self->{url
} .
1914 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
1915 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1919 # prop_walk(PATH, REV, SUB)
1920 # -------------------------
1921 # Recursively traverse PATH at revision REV and invoke SUB for each
1922 # directory that contains a SVN property. SUB will be invoked as
1923 # follows: &SUB(gs, path, props); where `gs' is this instance of
1924 # Git::SVN, `path' the path to the directory where the properties
1925 # `props' were found. The `path' will be relative to point of checkout,
1926 # that is, if url://repo/trunk is the current Git branch, and that
1927 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
1928 # as `path' (note the trailing `/').
1930 my ($self, $path, $rev, $sub) = @_;
1933 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
1936 # Strip the irrelevant part of the path.
1937 $p =~ s
#^/+\Q$self->{path}\E(/|$)#/#;
1938 # Ensure the path is terminated by a `/'.
1941 # The properties contain all the internal SVN stuff nobody
1942 # (usually) cares about.
1943 my $interesting_props = 0;
1944 foreach (keys %{$props}) {
1945 # If it doesn't start with `svn:', it must be a
1946 # user-defined property.
1947 ++$interesting_props and next if $_ !~ /^svn:/;
1948 # FIXME: Fragile, if SVN adds new public properties,
1949 # this needs to be updated.
1950 ++$interesting_props if /^svn
:(?
:ignore
|keywords
|executable
1951 |eol
-style
|mime
-type
1952 |externals
|needs
-lock)$/x
;
1954 &$sub($self, $p, $props) if $interesting_props;
1956 foreach (sort keys %$dirent) {
1957 next if $dirent->{$_}->{kind
} != $SVN::Node
::dir
;
1958 $self->prop_walk($self->{path
} . $p . $_, $rev, $sub);
1962 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
1963 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
1965 # returns the newest SVN revision number and newest commit SHA1
1966 sub last_rev_commit
{
1968 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
1969 return ($self->{last_rev
}, $self->{last_commit
});
1971 my $c = ::verify_ref
($self->refname.'^0');
1972 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1973 my $rev = (::cmt_metadata
($c))[1];
1975 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
1979 my $map_path = $self->map_path;
1980 unless (-e
$map_path) {
1981 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
1982 return (undef, undef);
1984 my ($rev, $commit) = $self->rev_map_max(1);
1985 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $commit);
1986 return ($rev, $commit);
1989 sub get_fetch_range
{
1990 my ($self, $min, $max) = @_;
1991 $max ||= $self->ra->get_latest_revnum;
1992 $min ||= $self->rev_map_max;
1998 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1999 my $config = "$ENV{GIT_DIR}/svn/.metadata";
2000 if (! -f
$config && -f
$old_def_config) {
2001 rename $old_def_config, $config or
2002 die "Failed rename $old_def_config => $config: $!\n";
2004 my $old_config = $ENV{GIT_CONFIG
};
2005 $ENV{GIT_CONFIG
} = $config;
2008 unless (-f
$config) {
2010 open my $fh, '>', $config or
2011 die "Can't open $config: $!\n";
2012 print $fh "; This file is used internally by ",
2014 "Couldn't write to $config: $!\n";
2015 print $fh "; You should not have to edit it\n" or
2016 die "Couldn't write to $config: $!\n";
2017 close $fh or die "Couldn't close $config: $!\n";
2019 command
('config', @args);
2022 if (defined $old_config) {
2023 $ENV{GIT_CONFIG
} = $old_config;
2025 delete $ENV{GIT_CONFIG
};
2028 wantarray ?
@ret : $ret[0];
2032 my ($self, $sub) = @_;
2033 my $old_index = $ENV{GIT_INDEX_FILE
};
2034 $ENV{GIT_INDEX_FILE
} = $self->{index};
2037 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
2038 mkpath
([$dir]) unless -d
$dir;
2042 if (defined $old_index) {
2043 $ENV{GIT_INDEX_FILE
} = $old_index;
2045 delete $ENV{GIT_INDEX_FILE
};
2048 wantarray ?
@ret : $ret[0];
2051 sub assert_index_clean
{
2052 my ($self, $treeish) = @_;
2054 $self->tmp_index_do(sub {
2055 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
2056 my $x = command_oneline
('write-tree');
2057 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
2058 /^tree ($::sha1)/mo);
2061 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2062 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2063 command_noisy
('read-tree', $treeish);
2064 $x = command_oneline
('write-tree');
2066 ::fatal
"trees ($treeish) $y != $x\n",
2067 "Something is seriously wrong...";
2072 sub get_commit_parents
{
2073 my ($self, $log_entry) = @_;
2074 my (%seen, @ret, @tmp);
2075 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2076 if (my $ip = $self->{inject_parents
}) {
2077 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
2081 if (my $cur = ::verify_ref
($self->refname.'^0')) {
2084 if (my $ipd = $self->{inject_parents_dcommit
}) {
2085 if (my $commit = delete $ipd->{$log_entry->{revision
}}) {
2086 push @tmp, @
$commit;
2089 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
2090 while (my $p = shift @tmp) {
2094 # MAXPARENT is defined to 16 in commit-tree.c:
2098 die "r$log_entry->{revision}: No room for parents:\n\t",
2099 join("\n\t", @tmp), "\n";
2106 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
2107 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2108 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
2111 if ($rwr !~ m
#^[a-z\+]+://#) {
2112 die "$rwr is not a valid URL (key: $k)\n";
2115 $self->{-rewrite_root
} = $rwr;
2120 ($self->rewrite_root || $self->{url
}) .
2121 (length $self->{path
} ?
'/' . $self->{path
} : '');
2126 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
2130 sub set_commit_header_env
{
2131 my ($log_entry) = @_;
2133 foreach my $ned (qw
/NAME EMAIL DATE/) {
2134 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2135 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2139 $ENV{GIT_AUTHOR_NAME
} = $log_entry->{name
};
2140 $ENV{GIT_AUTHOR_EMAIL
} = $log_entry->{email
};
2141 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
2143 $ENV{GIT_COMMITTER_NAME
} = (defined $log_entry->{commit_name
})
2144 ?
$log_entry->{commit_name
}
2145 : $log_entry->{name
};
2146 $ENV{GIT_COMMITTER_EMAIL
} = (defined $log_entry->{commit_email
})
2147 ?
$log_entry->{commit_email
}
2148 : $log_entry->{email
};
2152 sub restore_commit_header_env
{
2154 foreach my $ned (qw
/NAME EMAIL DATE/) {
2155 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2156 my $k = "GIT_${ac}_${ned}";
2157 if (defined $env->{$k}) {
2158 $ENV{$k} = $env->{$k};
2167 command_noisy
('gc', '--auto');
2171 my ($self, $log_entry) = @_;
2172 my $lr = $self->last_rev;
2173 if (defined $lr && $lr >= $log_entry->{revision
}) {
2174 die "Last fetched revision of ", $self->refname,
2175 " was r$lr, but we are about to fetch: ",
2176 "r$log_entry->{revision}!\n";
2178 if (my $c = $self->rev_map_get($log_entry->{revision
})) {
2179 croak
"$log_entry->{revision} = $c already exists! ",
2180 "Why are we refetching it?\n";
2182 my $old_env = set_commit_header_env
($log_entry);
2183 my $tree = $log_entry->{tree
};
2184 if (!defined $tree) {
2185 $tree = $self->tmp_index_do(sub {
2186 command_oneline
('write-tree') });
2188 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2190 my @exec = ('git-commit-tree', $tree);
2191 foreach ($self->get_commit_parents($log_entry)) {
2192 push @exec, '-p', $_;
2194 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2196 print $msg_fh $log_entry->{log} or croak
$!;
2197 restore_commit_header_env
($old_env);
2198 unless ($self->no_metadata) {
2199 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2202 $msg_fh->flush == 0 or croak
$!;
2203 close $msg_fh or croak
$!;
2204 chomp(my $commit = do { local $/; <$out_fh> });
2205 close $out_fh or croak
$!;
2208 if ($commit !~ /^$::sha1$/o) {
2209 die "Failed to commit, invalid sha1: $commit\n";
2212 $self->rev_map_set($log_entry->{revision
}, $commit, 1);
2214 $self->{last_rev
} = $log_entry->{revision
};
2215 $self->{last_commit
} = $commit;
2216 print "r$log_entry->{revision}";
2217 if (defined $log_entry->{svm_revision
}) {
2218 print " (\@$log_entry->{svm_revision})";
2219 $self->rev_map_set($log_entry->{svm_revision
}, $commit,
2220 0, $self->svm_uuid);
2222 print " = $commit ($self->{ref_id})\n";
2223 if (--$_gc_nr == 0) {
2224 $_gc_nr = $_gc_period;
2231 my ($self, $paths, $r) = @_;
2232 return 1 if $self->{path
} eq '';
2233 if (my $path = $paths->{"/$self->{path}"}) {
2234 return ($path->{action
} eq 'D') ?
0 : 1;
2236 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
2237 if (grep /$self->{path_regex}/, keys %$paths) {
2241 foreach (split m
#/#, $self->{path}) {
2243 next unless ($paths->{$c} &&
2244 ($paths->{$c}->{action
} =~ /^[AR]$/));
2245 if ($self->ra->check_path($self->{path
}, $r) ==
2253 sub find_parent_branch
{
2254 my ($self, $paths, $rev) = @_;
2255 return undef unless $self->follow_parent;
2256 unless (defined $paths) {
2257 my $err_handler = $SVN::Error
::handler
;
2258 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
2259 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1, sub {
2261 Git
::SVN
::Ra
::dup_changed_paths
($_[0]) });
2262 $SVN::Error
::handler
= $err_handler;
2264 return undef unless defined $paths;
2266 # look for a parent from another branch:
2267 my @b_path_components = split m
#/#, $self->rel_path;
2268 my @a_path_components;
2270 while (@b_path_components) {
2271 $i = $paths->{'/'.join('/', @b_path_components)};
2272 last if $i && defined $i->{copyfrom_path
};
2273 unshift(@a_path_components, pop(@b_path_components));
2275 return undef unless defined $i && defined $i->{copyfrom_path
};
2276 my $branch_from = $i->{copyfrom_path
};
2277 if (@a_path_components) {
2278 print STDERR
"branch_from: $branch_from => ";
2279 $branch_from .= '/'.join('/', @a_path_components);
2280 print STDERR
$branch_from, "\n";
2282 my $r = $i->{copyfrom_rev
};
2283 my $repos_root = $self->ra->{repos_root
};
2284 my $url = $self->ra->{url
};
2285 my $new_url = $repos_root . $branch_from;
2286 print STDERR
"Found possible branch point: ",
2287 "$new_url => ", $self->full_url, ", $r\n";
2288 $branch_from =~ s
#^/##;
2289 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
2291 my $ref_id = $self->{ref_id
};
2292 $ref_id =~ s/\@\d+$//;
2294 # just grow a tail if we're not unique enough :x
2295 $ref_id .= '-' while find_ref
($ref_id);
2296 print STDERR
"Initializing parent: $ref_id\n";
2297 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2298 if ($u =~ s
#^\Q$url\E(/|$)##) {
2301 $repo_id = $self->{repo_id
};
2303 $gs = Git
::SVN
->init($u, $p, $repo_id, $ref_id, 1);
2305 my ($r0, $parent) = $gs->find_rev_before($r, 1);
2306 if (!defined $r0 || !defined $parent) {
2307 my ($base, $head) = parse_revision_argument
(0, $r);
2309 $gs->fetch($base, $r);
2311 ($r0, $parent) = $gs->last_rev_commit;
2313 if (defined $r0 && defined $parent) {
2314 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
2316 if ($self->ra->can_do_switch) {
2317 $self->assert_index_clean($parent);
2318 print STDERR
"Following parent with do_switch\n";
2319 # do_switch works with svn/trunk >= r22312, but that
2320 # is not included with SVN 1.4.3 (the latest version
2321 # at the moment), so we can't rely on it
2322 $self->{last_commit
} = $parent;
2323 $ed = SVN
::Git
::Fetcher
->new($self);
2324 $gs->ra->gs_do_switch($r0, $rev, $gs,
2325 $self->full_url, $ed)
2326 or die "SVN connection failed somewhere...\n";
2327 } elsif ($self->ra->trees_match($new_url, $r0,
2328 $self->full_url, $rev)) {
2329 print STDERR
"Trees match:\n",
2331 " ${\$self->full_url}\@$rev\n",
2332 "Following parent with no changes\n";
2333 $self->tmp_index_do(sub {
2334 command_noisy
('read-tree', $parent);
2336 $self->{last_commit
} = $parent;
2338 print STDERR
"Following parent with do_update\n";
2339 $ed = SVN
::Git
::Fetcher
->new($self);
2340 $self->ra->gs_do_update($rev, $rev, $self, $ed)
2341 or die "SVN connection failed somewhere...\n";
2343 print STDERR
"Successfully followed parent\n";
2344 return $self->make_log_entry($rev, [$parent], $ed);
2350 my ($self, $paths, $rev) = @_;
2352 my ($last_rev, @parents);
2353 if (my $lc = $self->last_commit) {
2354 # we can have a branch that was deleted, then re-added
2355 # under the same name but copied from another path, in
2356 # which case we'll have multiple parents (we don't
2357 # want to break the original ref, nor lose copypath info):
2358 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2359 push @
{$log_entry->{parents
}}, $lc;
2362 $ed = SVN
::Git
::Fetcher
->new($self);
2363 $last_rev = $self->{last_rev
};
2368 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2371 $ed = SVN
::Git
::Fetcher
->new($self);
2373 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
2374 die "SVN connection failed somewhere...\n";
2376 $self->make_log_entry($rev, \
@parents, $ed);
2380 my ($self, $ed) = @_;
2382 my $h = $ed->{empty
};
2383 foreach (sort keys %$h) {
2384 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2385 push @out, " $act: " . uri_encode
($_);
2386 warn "W: $act: $_\n";
2388 foreach my $t (qw
/dir_prop file_prop/) {
2389 $h = $ed->{$t} or next;
2390 foreach my $path (sort keys %$h) {
2391 my $ppath = $path eq '' ?
'.' : $path;
2392 foreach my $prop (sort keys %{$h->{$path}}) {
2393 next if $SKIP_PROP{$prop};
2394 my $v = $h->{$path}->{$prop};
2395 my $t_ppath_prop = "$t: " .
2396 uri_encode
($ppath) . ' ' .
2399 push @out, " +$t_ppath_prop " .
2402 push @out, " -$t_ppath_prop";
2407 foreach my $t (qw
/absent_file absent_directory/) {
2408 $h = $ed->{$t} or next;
2409 foreach my $parent (sort keys %$h) {
2410 foreach my $path (sort @
{$h->{$parent}}) {
2411 push @out, " $t: " .
2412 uri_encode
("$parent/$path");
2413 warn "W: $t: $parent/$path ",
2414 "Insufficient permissions?\n";
2421 sub parse_svn_date
{
2422 my $date = shift || return '+0000 1970-01-01 00:00:00';
2423 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2424 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
2425 croak
"Unable to parse date: $date\n";
2426 "+0000 $Y-$m-$d $H:$M:$S";
2431 if (!defined $author || length $author == 0) {
2432 $author = '(no author)';
2433 } elsif (defined $::_authors
&& ! defined $::users
{$author}) {
2434 die "Author: $author not defined in $::_authors file\n";
2439 sub make_log_entry
{
2440 my ($self, $rev, $parents, $ed) = @_;
2441 my $untracked = $self->get_untracked($ed);
2443 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
2444 print $un "r$rev\n" or croak
$!;
2445 print $un $_, "\n" foreach @
$untracked;
2446 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
2450 my $logged = delete $self->{logged_rev_props
};
2451 if (!$logged || $self->{-want_revprops
}) {
2452 my $rp = $self->ra->rev_proplist($rev);
2453 foreach (sort keys %$rp) {
2455 if (/^svn:(author|date|log)$/) {
2456 $log_entry{$1} = $v;
2457 } elsif ($_ eq 'svm:headrev') {
2460 print $un " rev_prop: ", uri_encode
($_), ' ',
2461 uri_encode
($v), "\n";
2465 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
2467 close $un or croak
$!;
2469 $log_entry{date
} = parse_svn_date
($log_entry{date
});
2470 $log_entry{log} .= "\n";
2471 my $author = $log_entry{author
} = check_author
($log_entry{author
});
2472 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
2475 my ($commit_name, $commit_email) = ($name, $email);
2476 if ($_use_log_author) {
2478 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2480 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2483 if (!defined $name_field) {
2484 if (!defined $email) {
2487 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
2488 ($name, $email) = ($1, $2);
2489 } elsif ($name_field =~ /(.*)@/) {
2490 ($name, $email) = ($1, $name_field);
2492 ($name, $email) = ($name_field, $name_field);
2495 if (defined $headrev && $self->use_svm_props) {
2496 if ($self->rewrite_root) {
2497 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2500 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
2501 # we don't want "SVM: initializing mirror for junk" ...
2502 return undef if $r == 0;
2503 my $svm = $self->svm;
2504 if ($uuid ne $svm->{uuid
}) {
2505 die "UUID mismatch on SVM path:\n",
2506 "expected: $svm->{uuid}\n",
2509 my $full_url = $self->full_url;
2510 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2511 die "Failed to replace '$svm->{replace}' with ",
2512 "'$svm->{source}' in $full_url\n";
2513 # throw away username for storing in records
2514 remove_username
($full_url);
2515 $log_entry{metadata
} = "$full_url\@$r $uuid";
2516 $log_entry{svm_revision
} = $r;
2517 $email ||= "$author\@$uuid";
2518 $commit_email ||= "$author\@$uuid";
2519 } elsif ($self->use_svnsync_props) {
2520 my $full_url = $self->svnsync->{url
};
2521 $full_url .= "/$self->{path}" if length $self->{path
};
2522 remove_username
($full_url);
2523 my $uuid = $self->svnsync->{uuid
};
2524 $log_entry{metadata
} = "$full_url\@$rev $uuid";
2525 $email ||= "$author\@$uuid";
2526 $commit_email ||= "$author\@$uuid";
2528 my $url = $self->metadata_url;
2529 remove_username
($url);
2530 $log_entry{metadata
} = "$url\@$rev " .
2531 $self->ra->get_uuid;
2532 $email ||= "$author\@" . $self->ra->get_uuid;
2533 $commit_email ||= "$author\@" . $self->ra->get_uuid;
2535 $log_entry{name
} = $name;
2536 $log_entry{email
} = $email;
2537 $log_entry{commit_name
} = $commit_name;
2538 $log_entry{commit_email
} = $commit_email;
2543 my ($self, $min_rev, $max_rev, @parents) = @_;
2544 my ($last_rev, $last_commit) = $self->last_rev_commit;
2545 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
2546 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
2550 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2551 $self->{inject_parents
} = { $rev => $tree };
2552 $self->fetch(undef, undef);
2556 my ($self, $tree) = (shift, shift);
2557 my $log_entry = ::get_commit_entry
($tree);
2558 unless ($self->{last_rev
}) {
2559 fatal
("Must have an existing revision to commit");
2561 my %ed_opts = ( r
=> $self->{last_rev
},
2562 log => $log_entry->{log},
2564 tree_a
=> $self->{last_commit
},
2567 $self->set_tree_cb($log_entry, $tree, @_) },
2568 svn_path
=> $self->{path
} );
2569 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
2570 print "No changes\nr$self->{last_rev} = $tree\n";
2574 sub rebuild_from_rev_db
{
2575 my ($self, $path) = @_;
2577 open my $fh, '<', $path or croak
"open: $!";
2578 binmode $fh or croak
"binmode: $!";
2580 length($_) == 41 or croak
"inconsistent size in ($_) != 41";
2583 next if $_ eq ('0' x
40);
2584 $self->rev_map_set($r, $_);
2587 close $fh or croak
"close: $!";
2588 unlink $path or croak
"unlink: $!";
2593 my $map_path = $self->map_path;
2594 return if (-e
$map_path && ! -z
$map_path);
2595 return unless ::verify_ref
($self->refname.'^0');
2596 if ($self->use_svm_props || $self->no_metadata) {
2597 my $rev_db = $self->rev_db_path;
2598 $self->rebuild_from_rev_db($rev_db);
2599 if ($self->use_svm_props) {
2600 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2601 $self->rebuild_from_rev_db($svm_rev_db);
2603 $self->unlink_rev_db_symlink;
2606 print "Rebuilding $map_path ...\n";
2608 command_output_pipe
(qw
/rev-list --pretty=raw --no-color --reverse/,
2609 $self->refname, '--');
2610 my $metadata_url = $self->metadata_url;
2611 remove_username
($metadata_url);
2612 my $svn_uuid = $self->ra_uuid;
2615 if ( m{^commit ($::sha1)$} ) {
2619 next unless s{^\s*(git-svn-id:)}{$1};
2620 my ($url, $rev, $uuid) = ::extract_metadata
($_);
2621 remove_username
($url);
2623 # ignore merges (from set-tree)
2624 next if (!defined $rev || !$uuid);
2626 # if we merged or otherwise started elsewhere, this is
2627 # how we break out of it
2628 if (($uuid ne $svn_uuid) ||
2629 ($metadata_url && $url && ($url ne $metadata_url))) {
2633 $self->rev_map_set($rev, $c);
2634 print "r$rev = $c\n";
2636 command_close_pipe
($log, $ctx);
2637 print "Done rebuilding $map_path\n";
2638 my $rev_db_path = $self->rev_db_path;
2639 if (-f
$self->rev_db_path) {
2640 unlink $self->rev_db_path or croak
"unlink: $!";
2642 $self->unlink_rev_db_symlink;
2646 # Tie::File seems to be prone to offset errors if revisions get sparse,
2647 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2648 # one of my favorite modules is out :< Next up would be one of the DBM
2649 # modules, but I'm not sure which is most portable...
2651 # This is the replacement for the rev_db format, which was too big
2652 # and inefficient for large repositories with a lot of sparse history
2655 # The format is this:
2656 # - 24 bytes for every record,
2657 # * 4 bytes for the integer representing an SVN revision number
2658 # * 20 bytes representing the sha1 of a git commit
2659 # - No empty padding records like the old format
2660 # (except the last record, which can be overwritten)
2661 # - new records are written append-only since SVN revision numbers
2662 # increase monotonically
2663 # - lookups on SVN revision number are done via a binary search
2664 # - Piping the file to xxd -c24 is a good way of dumping it for
2665 # viewing or editing (piped back through xxd -r), should the need
2667 # - The last record can be padding revision with an all-zero sha1
2668 # This is used to optimize fetch performance when using multiple
2669 # "fetch" directives in .git/config
2671 # These files are disposable unless noMetadata or useSvmProps is set
2674 my ($fh, $rev, $commit) = @_;
2676 binmode $fh or croak
"binmode: $!";
2677 my $size = (stat($fh))[7];
2678 ($size % 24) == 0 or croak
"inconsistent size: $size";
2682 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
2683 my $read = sysread($fh, my $buf, 24) or croak
"read: $!";
2684 $read == 24 or croak
"read only $read bytes (!= 24)";
2685 my ($last_rev, $last_commit) = unpack(rev_map_fmt
, $buf);
2686 if ($last_commit eq ('0' x40
)) {
2688 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
2689 $read = sysread($fh, $buf, 24) or
2692 croak
"read only $read bytes (!= 24)";
2693 ($last_rev, $last_commit) =
2694 unpack(rev_map_fmt
, $buf);
2695 if ($last_commit eq ('0' x40
)) {
2696 croak
"inconsistent .rev_map\n";
2699 if ($last_rev >= $rev) {
2700 croak
"last_rev is higher!: $last_rev >= $rev";
2705 sysseek($fh, $wr_offset, SEEK_END
) or croak
"seek: $!";
2706 syswrite($fh, pack(rev_map_fmt
, $rev, $commit), 24) == 24 or
2713 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
2714 mkpath
([$dir]) unless -d
$dir;
2715 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2716 close $fh or die "Couldn't close (create) $path: $!\n";
2721 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2722 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2723 my $db = $self->map_path($uuid);
2724 my $db_lock = "$db.lock";
2727 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2728 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
2732 $LOCKFILES{$db_lock} = 1;
2734 # both of these options make our .rev_db file very, very important
2735 # and we can't afford to lose it because rebuild() won't work
2736 if ($self->use_svm_props || $self->no_metadata) {
2738 copy
($db, $db_lock) or die "rev_map_set(@_): ",
2740 "$db => $db_lock ($!)\n";
2742 rename $db, $db_lock or die "rev_map_set(@_): ",
2743 "Failed to rename: ",
2744 "$db => $db_lock ($!)\n";
2747 sysopen(my $fh, $db_lock, O_RDWR
| O_CREAT
)
2748 or croak
"Couldn't open $db_lock: $!\n";
2749 _rev_map_set
($fh, $rev, $commit);
2751 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2752 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2754 close $fh or croak
$!;
2757 command_noisy
('update-ref', '-m', "r$rev",
2758 $self->refname, $commit);
2760 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
2761 "$db_lock => $db ($!)\n";
2762 delete $LOCKFILES{$db_lock};
2764 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
2765 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
2766 kill $sig, $$ if defined $sig;
2770 # If want_commit, this will return an array of (rev, commit) where
2771 # commit _must_ be a valid commit in the archive.
2772 # Otherwise, it'll return the max revision (whether or not the
2773 # commit is valid or just a 0x40 placeholder).
2775 my ($self, $want_commit) = @_;
2777 my $map_path = $self->map_path;
2778 stat $map_path or return $want_commit ?
(0, undef) : 0;
2779 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
2780 binmode $fh or croak
"binmode: $!";
2781 my $size = (stat($fh))[7];
2782 ($size % 24) == 0 or croak
"inconsistent size: $size";
2785 close $fh or croak
"close: $!";
2786 return $want_commit ?
(0, undef) : 0;
2789 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
2790 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
2791 my ($r, $c) = unpack(rev_map_fmt
, $buf);
2792 if ($want_commit && $c eq ('0' x40
)) {
2794 return $want_commit ?
(0, undef) : 0;
2796 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
2797 sysread($fh, $buf, 24) == 24 or croak
"read: $!";
2798 ($r, $c) = unpack(rev_map_fmt
, $buf);
2799 if ($c eq ('0'x40
)) {
2800 croak
"Penultimate record is all-zeroes in $map_path";
2803 close $fh or croak
"close: $!";
2804 $want_commit ?
($r, $c) : $r;
2808 my ($self, $rev, $uuid) = @_;
2809 my $map_path = $self->map_path($uuid);
2810 return undef unless -e
$map_path;
2812 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
2813 binmode $fh or croak
"binmode: $!";
2814 my $size = (stat($fh))[7];
2815 ($size % 24) == 0 or croak
"inconsistent size: $size";
2818 close $fh or croak
"close: $fh";
2822 my ($l, $u) = (0, $size - 24);
2826 my $i = int(($l/24 + $u/24) / 2) * 24;
2827 sysseek($fh, $i, SEEK_SET
) or croak
"seek: $!";
2828 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
2829 my ($r, $c) = unpack('NH40', $buf);
2833 } elsif ($r > $rev) {
2835 } else { # $r == $rev
2836 close($fh) or croak
"close: $!";
2837 return $c eq ('0' x
40) ?
undef : $c;
2840 close($fh) or croak
"close: $!";
2844 # Finds the first svn revision that exists on (if $eq_ok is true) or
2845 # before $rev for the current branch. It will not search any lower
2846 # than $min_rev. Returns the git commit hash and svn revision number
2847 # if found, else (undef, undef).
2848 sub find_rev_before
{
2849 my ($self, $rev, $eq_ok, $min_rev) = @_;
2850 --$rev unless $eq_ok;
2852 while ($rev >= $min_rev) {
2853 if (my $c = $self->rev_map_get($rev)) {
2858 return (undef, undef);
2861 # Finds the first svn revision that exists on (if $eq_ok is true) or
2862 # after $rev for the current branch. It will not search any higher
2863 # than $max_rev. Returns the git commit hash and svn revision number
2864 # if found, else (undef, undef).
2865 sub find_rev_after
{
2866 my ($self, $rev, $eq_ok, $max_rev) = @_;
2867 ++$rev unless $eq_ok;
2868 $max_rev ||= $self->rev_map_max;
2869 while ($rev <= $max_rev) {
2870 if (my $c = $self->rev_map_get($rev)) {
2875 return (undef, undef);
2879 my ($class, $repo_id, $ref_id, $path) = @_;
2880 unless (defined $repo_id && length $repo_id) {
2881 $repo_id = $Git::SVN
::default_repo_id
;
2883 unless (defined $ref_id && length $ref_id) {
2884 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
2887 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2888 $_[3] = $path = '' unless (defined $path);
2889 mkpath
(["$ENV{GIT_DIR}/svn"]);
2891 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
2892 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
2893 map_root
=> "$dir/.rev_map", repo_id
=> $repo_id }, $class;
2896 # for read-only access of old .rev_db formats
2897 sub unlink_rev_db_symlink
{
2899 my $link = $self->rev_db_path;
2900 $link =~ s/\.[\w-]+$// or croak
"missing UUID at the end of $link";
2902 unlink $link or croak
"unlink: $link failed!";
2907 my ($self, $uuid) = @_;
2908 my $db_path = $self->map_path($uuid);
2909 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
2910 or croak
"map_path: $db_path does not contain '/.rev_map.' !";
2914 # the new replacement for .rev_db
2916 my ($self, $uuid) = @_;
2917 $uuid ||= $self->ra_uuid;
2918 "$self->{map_root}.$uuid";
2923 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2927 sub remove_username
{
2928 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2931 package Git
::SVN
::Prompt
;
2935 use vars qw
/$_no_auth_cache $_username/;
2938 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2939 $may_save = undef if $_no_auth_cache;
2940 $default_username = $_username if defined $_username;
2941 if (defined $default_username && length $default_username) {
2942 if (defined $realm && length $realm) {
2943 print STDERR
"Authentication realm: $realm\n";
2946 $cred->username($default_username);
2948 username
($cred, $realm, $may_save, $pool);
2950 $cred->password(_read_password
("Password for '" .
2951 $cred->username . "': ", $realm));
2952 $cred->may_save($may_save);
2953 $SVN::_Core
::SVN_NO_ERROR
;
2956 sub ssl_server_trust
{
2957 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2958 $may_save = undef if $_no_auth_cache;
2959 print STDERR
"Error validating server certificate for '$realm':\n";
2962 # All variables SVN::Auth::SSL::* are used only once,
2963 # so we're shutting up Perl warnings about this.
2964 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2965 print STDERR
" - The certificate is not issued ",
2966 "by a trusted authority. Use the\n",
2967 " fingerprint to validate ",
2968 "the certificate manually!\n";
2970 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2971 print STDERR
" - The certificate hostname ",
2972 "does not match.\n";
2974 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2975 print STDERR
" - The certificate is not yet valid.\n";
2977 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2978 print STDERR
" - The certificate has expired.\n";
2980 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2981 print STDERR
" - The certificate has ",
2982 "an unknown error.\n";
2984 } # no warnings 'once'
2986 "Certificate information:\n".
2987 " - Hostname: %s\n".
2988 " - Valid: from %s until %s\n".
2990 " - Fingerprint: %s\n",
2991 map $cert_info->$_, qw(hostname valid_from valid_until
2992 issuer_dname fingerprint);
2995 print STDERR
$may_save ?
2996 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2997 "(R)eject or accept (t)emporarily? ";
2999 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
3000 if ($choice =~ /^t$/i) {
3001 $cred->may_save(undef);
3002 } elsif ($choice =~ /^r$/i) {
3004 } elsif ($may_save && $choice =~ /^p$/i) {
3005 $cred->may_save($may_save);
3009 $cred->accepted_failures($failures);
3010 $SVN::_Core
::SVN_NO_ERROR
;
3013 sub ssl_client_cert
{
3014 my ($cred, $realm, $may_save, $pool) = @_;
3015 $may_save = undef if $_no_auth_cache;
3016 print STDERR
"Client certificate filename: ";
3018 chomp(my $filename = <STDIN
>);
3019 $cred->cert_file($filename);
3020 $cred->may_save($may_save);
3021 $SVN::_Core
::SVN_NO_ERROR
;
3024 sub ssl_client_cert_pw
{
3025 my ($cred, $realm, $may_save, $pool) = @_;
3026 $may_save = undef if $_no_auth_cache;
3027 $cred->password(_read_password
("Password: ", $realm));
3028 $cred->may_save($may_save);
3029 $SVN::_Core
::SVN_NO_ERROR
;
3033 my ($cred, $realm, $may_save, $pool) = @_;
3034 $may_save = undef if $_no_auth_cache;
3035 if (defined $realm && length $realm) {
3036 print STDERR
"Authentication realm: $realm\n";
3039 if (defined $_username) {
3040 $username = $_username;
3042 print STDERR
"Username: ";
3044 chomp($username = <STDIN
>);
3046 $cred->username($username);
3047 $cred->may_save($may_save);
3048 $SVN::_Core
::SVN_NO_ERROR
;
3051 sub _read_password
{
3052 my ($prompt, $realm) = @_;
3053 print STDERR
$prompt;
3055 require Term
::ReadKey
;
3056 Term
::ReadKey
::ReadMode
('noecho');
3058 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
3059 last if $key =~ /[\012\015]/; # \n\r
3062 Term
::ReadKey
::ReadMode
('restore');
3068 package SVN
::Git
::Fetcher
;
3073 use File
::Temp qw
/tempfile/;
3076 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
3078 my ($class, $git_svn) = @_;
3079 my $self = SVN
::Delta
::Editor
->new;
3080 bless $self, $class;
3081 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
3082 $self->{empty
} = {};
3083 $self->{dir_prop
} = {};
3084 $self->{file_prop
} = {};
3085 $self->{absent_dir
} = {};
3086 $self->{absent_file
} = {};
3087 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
3091 sub set_path_strip
{
3092 my ($self, $path) = @_;
3093 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
3100 sub open_directory
{
3101 my ($self, $path, $pb, $rev) = @_;
3106 my ($self, $path) = @_;
3107 if ($self->{path_strip
}) {
3108 $path =~ s!$self->{path_strip}!! or
3109 die "Failed to strip path '$path' ($self->{path_strip})\n";
3115 my ($self, $path, $rev, $pb) = @_;
3117 my $gpath = $self->git_path($path);
3118 return undef if ($gpath eq '');
3120 # remove entire directories.
3121 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
3122 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3124 $self->{c
}, '--', $gpath);
3128 $self->{gii
}->remove($_);
3129 print "\tD\t$_\n" unless $::_q
;
3131 print "\tD\t$gpath/\n" unless $::_q
;
3132 command_close_pipe
($ls, $ctx);
3133 $self->{empty
}->{$path} = 0
3135 $self->{gii
}->remove($gpath);
3136 print "\tD\t$gpath\n" unless $::_q
;
3142 my ($self, $path, $pb, $rev) = @_;
3143 my $gpath = $self->git_path($path);
3144 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
3145 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
3146 unless (defined $mode && defined $blob) {
3147 die "$path was not found in commit $self->{c} (r$rev)\n";
3149 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
3150 pool
=> SVN
::Pool
->new, action
=> 'M' };
3154 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3155 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3156 delete $self->{empty
}->{$dir};
3157 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
3158 pool
=> SVN
::Pool
->new, action
=> 'A' };
3162 my ($self, $path, $cp_path, $cp_rev) = @_;
3163 my $gpath = $self->git_path($path);
3165 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3171 $self->{gii
}->remove($_);
3172 print "\tD\t$_\n" unless $::_q
;
3174 command_close_pipe
($ls, $ctx);
3175 $self->{empty
}->{$path} = 0;
3177 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3178 delete $self->{empty
}->{$dir};
3179 $self->{empty
}->{$path} = 1;
3183 sub change_dir_prop
{
3184 my ($self, $db, $prop, $value) = @_;
3185 $self->{dir_prop
}->{$db->{path
}} ||= {};
3186 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
3190 sub absent_directory
{
3191 my ($self, $path, $pb) = @_;
3192 $self->{absent_dir
}->{$pb->{path
}} ||= [];
3193 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
3198 my ($self, $path, $pb) = @_;
3199 $self->{absent_file
}->{$pb->{path
}} ||= [];
3200 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
3204 sub change_file_prop
{
3205 my ($self, $fb, $prop, $value) = @_;
3206 if ($prop eq 'svn:executable') {
3207 if ($fb->{mode_b
} != 120000) {
3208 $fb->{mode_b
} = defined $value ?
100755 : 100644;
3210 } elsif ($prop eq 'svn:special') {
3211 $fb->{mode_b
} = defined $value ?
120000 : 100644;
3213 $self->{file_prop
}->{$fb->{path
}} ||= {};
3214 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
3219 sub apply_textdelta
{
3220 my ($self, $fb, $exp) = @_;
3221 my $fh = IO
::File
->new_tmpfile;
3223 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3224 # (but $base does not,) so dup() it for reading in close_file
3225 open my $dup, '<&', $fh or croak
$!;
3226 my $base = IO
::File
->new_tmpfile;
3227 $base->autoflush(1);
3229 print $base 'link ' if ($fb->{mode_a
} == 120000);
3230 my $size = $::_repository
->cat_blob($fb->{blob
}, $base);
3231 die "Failed to read object $fb->{blob}" if ($size < 0);
3234 seek $base, 0, 0 or croak
$!;
3235 my $got = ::md5sum
($base);
3236 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
3238 " got: $got\n" if ($got ne $exp);
3241 seek $base, 0, 0 or croak
$!;
3243 $fb->{base
} = $base;
3244 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
3248 my ($self, $fb, $exp) = @_;
3250 my $path = $self->git_path($fb->{path
});
3251 if (my $fh = $fb->{fh
}) {
3253 seek($fh, 0, 0) or croak
$!;
3254 my $got = ::md5sum
($fh);
3256 die "Checksum mismatch: $path\n",
3257 "expected: $exp\n got: $got\n";
3260 sysseek($fh, 0, 0) or croak
$!;
3261 if ($fb->{mode_b
} == 120000) {
3263 sysread($fh, my $buf, 5) == 5 or croak
$!;
3264 $buf eq 'link ' or die "$path has mode 120000",
3265 " but is not a link";
3269 sysseek($fh, 0, 0) or croak
$!;
3273 my ($tmp_fh, $tmp_filename) = File
::Temp
::tempfile
(UNLINK
=> 1);
3275 while ($result = sysread($fh, my $string, 1024)) {
3276 my $wrote = syswrite($tmp_fh, $string, $result);
3277 defined($wrote) && $wrote == $result
3278 or croak
("write $tmp_filename: $!\n");
3280 defined $result or croak
$!;
3281 close $tmp_fh or croak
$!;
3283 close $fh or croak
$!;
3285 $hash = $::_repository
->hash_and_insert_object($tmp_filename);
3286 unlink($tmp_filename);
3287 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3288 close $fb->{base
} or croak
$!;
3290 $hash = $fb->{blob
} or die "no blob information\n";
3293 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
3294 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
3300 $self->{nr
} = $self->{gii
}->{nr
};
3301 delete $self->{gii
};
3302 $self->SUPER::abort_edit
(@_);
3307 $self->{git_commit_ok
} = 1;
3308 $self->{nr
} = $self->{gii
}->{nr
};
3309 delete $self->{gii
};
3310 $self->SUPER::close_edit
(@_);
3313 package SVN
::Git
::Editor
;
3314 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
3321 my ($class, $opts) = @_;
3322 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
3323 die "$_ required!\n" unless (defined $opts->{$_});
3326 my $pool = SVN
::Pool
->new;
3327 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
3328 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
3331 # $opts->{ra} functions should not be used after this:
3332 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
3333 $opts->{editor_cb
}, $pool);
3334 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
3335 bless $self, $class;
3336 foreach (qw
/svn_path r tree_a tree_b/) {
3337 $self->{$_} = $opts->{$_};
3339 $self->{url
} = $opts->{ra
}->{url
};
3340 $self->{mods
} = $mods;
3341 $self->{types
} = $types;
3342 $self->{pool
} = $pool;
3343 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3345 $self->{path_prefix
} = length $self->{svn_path
} ?
3346 "$self->{svn_path}/" : '';
3347 $self->{config
} = $opts->{config
};
3352 my ($tree_a, $tree_b) = @_;
3353 my @diff_tree = qw(diff-tree -z -r);
3354 if ($_cp_similarity) {
3355 push @diff_tree, "-C$_cp_similarity";
3357 push @diff_tree, '-C';
3359 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3360 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
3361 push @diff_tree, $tree_a, $tree_b;
3362 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
3366 while (<$diff_fh>) {
3367 chomp $_; # this gets rid of the trailing "\0"
3368 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
3369 $::sha1\s
($::sha1
)\s
3370 ([MTCRAD
])\d
*$/xo
) {
3371 push @mods, { mode_a
=> $1, mode_b
=> $2,
3372 sha1_b
=> $3, chg
=> $4 };
3373 if ($4 =~ /^(?:C|R)$/) {
3378 } elsif ($state eq 'file_a') {
3379 my $x = $mods[$#mods] or croak
"Empty array\n";
3380 if ($x->{chg
} !~ /^(?:C|R)$/) {
3381 croak
"Error parsing $_, $x->{chg}\n";
3385 } elsif ($state eq 'file_b') {
3386 my $x = $mods[$#mods] or croak
"Empty array\n";
3387 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
3388 croak
"Error parsing $_, $x->{chg}\n";
3390 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
3391 croak
"Error parsing $_, $x->{chg}\n";
3396 croak
"Error parsing $_\n";
3399 command_close_pipe
($diff_fh, $ctx);
3403 sub check_diff_paths
{
3404 my ($ra, $pfx, $rev, $mods) = @_;
3406 $pfx .= '/' if length $pfx;
3408 sub type_diff_paths
{
3409 my ($ra, $types, $path, $rev) = @_;
3410 my @p = split m
#/+#, $path;
3412 unless (defined $types->{$c}) {
3413 $types->{$c} = $ra->check_path($c, $rev);
3416 $c .= '/' . shift @p;
3417 next if defined $types->{$c};
3418 $types->{$c} = $ra->check_path($c, $rev);
3422 foreach my $m (@
$mods) {
3423 foreach my $f (qw
/file_a file_b/) {
3424 next unless defined $m->{$f};
3425 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
3426 if (length $pfx.$dir && ! defined $types{$dir}) {
3427 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
3435 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3439 my ($self, $path) = @_;
3440 $self->{path_prefix
}.(defined $path ?
$path : '');
3444 my ($self, $path) = @_;
3445 if ($self->{url
} =~ m
#^https?://#) {
3446 $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3448 $self->{url
} . '/' . $self->repo_path($path);
3453 my $rm = $self->{rm
};
3454 delete $rm->{''}; # we never delete the url we're tracking
3457 foreach (keys %$rm) {
3458 my @d = split m
#/#, $_;
3462 $c .= '/' . shift @d;
3466 delete $rm->{$self->{svn_path
}};
3467 delete $rm->{''}; # we never delete the url we're tracking
3470 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
3475 my @dn = split m
#/#, $_;
3477 delete $rm->{join '/', @dn};
3484 command_close_pipe
($fh, $ctx);
3486 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3487 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3488 $self->close_directory($bat->{$d}, $p);
3489 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3490 print "\tD+\t$d/\n" unless $::_q
;
3491 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3496 sub open_or_add_dir
{
3497 my ($self, $full_path, $baton) = @_;
3498 my $t = $self->{types
}->{$full_path};
3500 die "$full_path not known in r$self->{r} or we have a bug!\n";
3504 # SVN::Node::none and SVN::Node::file are used only once,
3505 # so we're shutting up Perl's warnings about them.
3506 if ($t == $SVN::Node
::none
) {
3507 return $self->add_directory($full_path, $baton,
3508 undef, -1, $self->{pool
});
3509 } elsif ($t == $SVN::Node
::dir
) {
3510 return $self->open_directory($full_path, $baton,
3511 $self->{r
}, $self->{pool
});
3512 } # no warnings 'once'
3513 print STDERR
"$full_path already exists in repository at ",
3514 "r$self->{r} and it is not a directory (",
3515 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3516 } # no warnings 'once'
3521 my ($self, $path) = @_;
3522 my $bat = $self->{bat
};
3523 my $repo_path = $self->repo_path($path);
3524 return $bat->{''} unless (length $repo_path);
3525 my @p = split m
#/+#, $repo_path;
3527 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3530 $c .= '/' . shift @p;
3531 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3536 # Subroutine to convert a globbing pattern to a regular expression.
3537 # From perl cookbook.
3539 my $globstr = shift;
3540 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3541 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3542 return '^' . $globstr . '$';
3545 sub check_autoprop
{
3546 my ($self, $pattern, $properties, $file, $fbat) = @_;
3547 # Convert the globbing pattern to a regular expression.
3548 my $regex = glob2pat
($pattern);
3549 # Check if the pattern matches the file name.
3550 if($file =~ m/($regex)/) {
3551 # Parse the list of properties to set.
3552 my @props = split(/;/, $properties);
3553 foreach my $prop (@props) {
3554 # Parse 'name=value' syntax and set the property.
3555 if ($prop =~ /([^=]+)=(.*)/) {
3556 my ($n,$v) = ($1,$2);
3560 $self->change_file_prop($fbat, $n, $v);
3566 sub apply_autoprops
{
3567 my ($self, $file, $fbat) = @_;
3568 my $conf_t = ${$self->{config
}}{'config'};
3570 # Check [miscellany]/enable-auto-props in svn configuration.
3571 if (SVN
::_Core
::svn_config_get_bool
(
3573 $SVN::_Core
::SVN_CONFIG_SECTION_MISCELLANY
,
3574 $SVN::_Core
::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS
,
3576 # Auto-props are enabled. Enumerate them to look for matches.
3577 my $callback = sub {
3578 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3580 SVN
::_Core
::svn_config_enumerate
(
3582 $SVN::_Core
::SVN_CONFIG_SECTION_AUTO_PROPS
,
3588 my ($self, $m) = @_;
3589 my ($dir, $file) = split_path
($m->{file_b
});
3590 my $pbat = $self->ensure_path($dir);
3591 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3593 print "\tA\t$m->{file_b}\n" unless $::_q
;
3594 $self->apply_autoprops($file, $fbat);
3595 $self->chg_file($fbat, $m);
3596 $self->close_file($fbat,undef,$self->{pool
});
3600 my ($self, $m) = @_;
3601 my ($dir, $file) = split_path
($m->{file_b
});
3602 my $pbat = $self->ensure_path($dir);
3603 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3604 $self->url_path($m->{file_a
}), $self->{r
});
3605 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3606 $self->chg_file($fbat, $m);
3607 $self->close_file($fbat,undef,$self->{pool
});
3611 my ($self, $path, $pbat) = @_;
3612 my $rpath = $self->repo_path($path);
3613 my ($dir, $file) = split_path
($rpath);
3614 $self->{rm
}->{$dir} = 1;
3615 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3619 my ($self, $m) = @_;
3620 my ($dir, $file) = split_path
($m->{file_b
});
3621 my $pbat = $self->ensure_path($dir);
3622 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3623 $self->url_path($m->{file_a
}), $self->{r
});
3624 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
3625 $self->chg_file($fbat, $m);
3626 $self->close_file($fbat,undef,$self->{pool
});
3628 ($dir, $file) = split_path
($m->{file_a
});
3629 $pbat = $self->ensure_path($dir);
3630 $self->delete_entry($m->{file_a
}, $pbat);
3634 my ($self, $m) = @_;
3635 my ($dir, $file) = split_path
($m->{file_b
});
3636 my $pbat = $self->ensure_path($dir);
3637 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3638 $pbat,$self->{r
},$self->{pool
});
3639 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
3640 $self->chg_file($fbat, $m);
3641 $self->close_file($fbat,undef,$self->{pool
});
3644 sub T
{ shift->M(@_) }
3646 sub change_file_prop
{
3647 my ($self, $fbat, $pname, $pval) = @_;
3648 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3652 my ($self, $fbat, $m) = @_;
3653 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3654 $self->change_file_prop($fbat,'svn:executable','*');
3655 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3656 $self->change_file_prop($fbat,'svn:executable',undef);
3658 my $fh = IO
::File
->new_tmpfile or croak
$!;
3659 if ($m->{mode_b
} =~ /^120/) {
3660 print $fh 'link ' or croak
$!;
3661 $self->change_file_prop($fbat,'svn:special','*');
3662 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3663 $self->change_file_prop($fbat,'svn:special',undef);
3665 my $size = $::_repository
->cat_blob($m->{sha1_b
}, $fh);
3666 croak
"Failed to read object $m->{sha1_b}" if ($size < 0);
3667 $fh->flush == 0 or croak
$!;
3668 seek $fh, 0, 0 or croak
$!;
3670 my $exp = ::md5sum
($fh);
3671 seek $fh, 0, 0 or croak
$!;
3673 my $pool = SVN
::Pool
->new;
3674 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3675 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
3676 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3679 close $fh or croak
$!;
3683 my ($self, $m) = @_;
3684 my ($dir, $file) = split_path
($m->{file_b
});
3685 my $pbat = $self->ensure_path($dir);
3686 print "\tD\t$m->{file_b}\n" unless $::_q
;
3687 $self->delete_entry($m->{file_b
}, $pbat);
3692 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3693 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3695 $self->close_directory($bat->{$_}, $p);
3697 $self->close_directory($bat->{''}, $p);
3698 $self->SUPER::close_edit
($p);
3704 $self->SUPER::abort_edit
($self->{pool
});
3709 $self->SUPER::DESTROY
(@_);
3710 $self->{pool
}->clear;
3713 # this drives the editor
3716 my $mods = $self->{mods
};
3717 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
3718 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
3720 if (defined $o{$f}) {
3723 fatal
("Invalid change type: $f");
3726 $self->rmdirs if $_rmdir;
3732 return scalar @
$mods;
3735 package Git
::SVN
::Ra
;
3736 use vars qw
/@ISA $config_dir $_log_window_size/;
3739 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
3742 # enforce temporary pool usage for some simple functions
3744 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3745 my $SUPER = "SUPER::$f";
3748 my $pool = SVN
::Pool
->new;
3749 my @ret = $self->$SUPER(@_,$pool);
3751 wantarray ?
@ret : $ret[0];
3756 sub _auth_providers
() {
3758 SVN
::Client
::get_simple_provider
(),
3759 SVN
::Client
::get_ssl_server_trust_file_provider
(),
3760 SVN
::Client
::get_simple_prompt_provider
(
3761 \
&Git
::SVN
::Prompt
::simple
, 2),
3762 SVN
::Client
::get_ssl_client_cert_file_provider
(),
3763 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
3764 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
3765 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
3766 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
3767 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
3768 SVN
::Client
::get_username_provider
(),
3769 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
3770 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
3771 SVN
::Client
::get_username_prompt_provider
(
3772 \
&Git
::SVN
::Prompt
::username
, 2)
3776 sub escape_uri_only
{
3779 foreach (split m{/}, $uri) {
3780 s/([^\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
3788 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
3789 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
3790 $url = "$scheme://$domain$uri";
3796 my ($class, $url) = @_;
3798 return $RA if ($RA && $RA->{url
} eq $url);
3800 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
3801 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
3802 my $config = SVN
::Core
::config_get_config
($config_dir);
3804 my $dont_store_passwords = 1;
3805 my $conf_t = ${$config}{'config'};
3808 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3809 # produces warnings that variables are used only once.
3810 # I had not found the better way to shut them up, so
3811 # the warnings of type 'once' are disabled in this block.
3812 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
3813 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
3814 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
3816 SVN
::_Core
::svn_auth_set_parameter
($baton,
3817 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
3818 bless (\
$dont_store_passwords, "_p_void"));
3820 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
3821 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
3822 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
3824 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
3826 } # no warnings 'once'
3827 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
3829 pool
=> SVN
::Pool
->new,
3830 auth_provider_callbacks
=> $callbacks);
3831 $self->{url
} = $url;
3832 $self->{svn_path
} = $url;
3833 $self->{repos_root
} = $self->get_repos_root;
3834 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
3835 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
3836 get_dir
=> { r
=> 0, data
=> {} } };
3837 $RA = bless $self, $class;
3841 my ($self, $path, $r) = @_;
3842 my $cache = $self->{cache
}->{check_path
};
3843 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
3844 return $cache->{data
}->{$path};
3846 my $pool = SVN
::Pool
->new;
3847 my $t = $self->SUPER::check_path
($path, $r, $pool);
3849 if ($r != $cache->{r
}) {
3850 %{$cache->{data
}} = ();
3853 $cache->{data
}->{$path} = $t;
3857 my ($self, $dir, $r) = @_;
3858 my $cache = $self->{cache
}->{get_dir
};
3859 if ($r == $cache->{r
}) {
3860 if (my $x = $cache->{data
}->{$dir}) {
3861 return wantarray ? @
$x : $x->[0];
3864 my $pool = SVN
::Pool
->new;
3865 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
3866 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
3868 if ($r != $cache->{r
}) {
3869 %{$cache->{data
}} = ();
3872 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
3873 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
3877 # do not call the real DESTROY since we store ourselves in $RA
3881 my ($self, @args) = @_;
3882 my $pool = SVN
::Pool
->new;
3883 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
3884 my $ret = $self->SUPER::get_log
(@args, $pool);
3890 my ($self, $url1, $rev1, $url2, $rev2) = @_;
3891 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
3892 my $out = IO
::File
->new_tmpfile;
3894 # older SVN (1.1.x) doesn't take $pool as the last parameter for
3895 # $ctx->diff(), so we'll create a default one
3896 my $pool = SVN
::Pool
->new_default_sub;
3898 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
3899 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
3901 my $ret = (($out->stat)[7] == 0);
3902 close $out or croak
$!;
3907 sub get_commit_editor
{
3908 my ($self, $log, $cb, $pool) = @_;
3909 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
3910 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
3914 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
3915 my $new = ($rev_a == $rev_b);
3916 my $path = $gs->{path
};
3918 if ($new && -e
$gs->{index}) {
3919 unlink $gs->{index} or die
3920 "Couldn't unlink index: $gs->{index}: $!\n";
3922 my $pool = SVN
::Pool
->new;
3923 $editor->set_path_strip($path);
3924 my (@pc) = split m
#/#, $path;
3925 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
3927 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3929 # Since we can't rely on svn_ra_reparent being available, we'll
3930 # just have to do some magic with set_path to make it so
3931 # we only want a partial path.
3933 my $final = join('/', @pc);
3935 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
3936 $sp .= '/' if length $sp;
3939 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
3941 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
3943 $reporter->finish_report($pool);
3945 $editor->{git_commit_ok
};
3948 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
3949 # svn_ra_reparent didn't work before 1.4)
3951 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
3952 my $path = $gs->{path
};
3953 my $pool = SVN
::Pool
->new;
3955 my $full_url = $self->{url
};
3956 my $old_url = $full_url;
3957 $full_url .= '/' . escape_uri_only
($path) if length $path;
3958 my ($ra, $reparented);
3959 if ($old_url ne $full_url) {
3960 if ($old_url !~ m
#^svn(\+ssh)?://#) {
3961 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url,
3963 $self->{url
} = $full_url;
3969 $ra = Git
::SVN
::Ra
->new($full_url);
3974 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
3975 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3976 $reporter->set_path('', $rev_a, 0, @lock, $pool);
3977 $reporter->finish_report($pool);
3980 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
3981 $self->{url
} = $old_url;
3985 $editor->{git_commit_ok
};
3988 sub longest_common_path
{
3989 my ($gsv, $globs) = @_;
3991 my $common_max = scalar @
$gsv;
3993 foreach my $gs (@
$gsv) {
3994 my @tmp = split m
#/#, $gs->{path};
3997 $p .= length($p) ?
"/$_" : $_;
4003 $common_max += scalar @
$globs;
4004 foreach my $glob (@
$globs) {
4005 my @tmp = split m
#/#, $glob->{path}->{left};
4008 $p .= length($p) ?
"/$_" : $_;
4014 my $longest_path = '';
4015 foreach (sort {length $b <=> length $a} keys %common) {
4016 if ($common{$_} == $common_max) {
4024 sub gs_fetch_loop_common
{
4025 my ($self, $base, $head, $gsv, $globs) = @_;
4026 return if ($base > $head);
4027 my $inc = $_log_window_size;
4028 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
4029 my $longest_path = longest_common_path
($gsv, $globs);
4030 my $ra_url = $self->{url
};
4034 my $err_handler = $SVN::Error
::handler
;
4035 $SVN::Error
::handler
= sub {
4037 skip_unknown_revs
($err);
4040 my ($paths, $r, $author, $date, $log) = @_;
4041 [ dup_changed_paths
($paths),
4042 { author
=> $author, date
=> $date, log => $log } ];
4044 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4045 sub { $revs{$_[1]} = _cb
(@_) });
4046 if ($err && $max >= $head) {
4047 print STDERR
"Path '$longest_path' ",
4048 "was probably deleted:\n",
4049 $err->expanded_message,
4050 "\nWill attempt to follow ",
4051 "revisions r$min .. r$max ",
4052 "committed before the deletion\n";
4054 while (--$hi >= $min) {
4056 $self->get_log([$longest_path], $min, $hi,
4059 $revs{$_[1]} = _cb
(@_) });
4061 print STDERR
"r$min .. r$ok OK\n";
4066 $SVN::Error
::handler
= $err_handler;
4068 my %exists = map { $_->{path
} => $_ } @
$gsv;
4069 foreach my $r (sort {$a <=> $b} keys %revs) {
4070 my ($paths, $logged) = @
{$revs{$r}};
4072 foreach my $gs ($self->match_globs(\
%exists, $paths,
4074 if ($gs->rev_map_max >= $r) {
4077 next unless $gs->match_paths($paths, $r);
4078 $gs->{logged_rev_props
} = $logged;
4079 if (my $last_commit = $gs->last_commit) {
4080 $gs->assert_index_clean($last_commit);
4082 my $log_entry = $gs->do_fetch($paths, $r);
4084 $gs->do_git_commit($log_entry);
4086 $INDEX_FILES{$gs->{index}} = 1;
4088 foreach my $g (@
$globs) {
4089 my $k = "svn-remote.$g->{remote}." .
4091 Git
::SVN
::tmp_config
($k, $r);
4097 $self = Git
::SVN
::Ra
->new($ra_url);
4098 $ra_invalid = undef;
4101 # pre-fill the .rev_db since it'll eventually get filled in
4102 # with '0' x40 if something new gets committed
4103 foreach my $gs (@
$gsv) {
4104 next if $gs->rev_map_max >= $max;
4105 next if defined $gs->rev_map_get($max);
4106 $gs->rev_map_set($max, 0 x40
);
4108 foreach my $g (@
$globs) {
4109 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4110 Git
::SVN
::tmp_config
($k, $max);
4112 last if $max >= $head;
4115 $max = $head if ($max > $head);
4121 my ($self, $exists, $paths, $globs, $r) = @_;
4124 my ($self, $exists, $g, $r) = @_;
4125 my @x = eval { $self->get_dir($g->{path
}->{left
}, $r) };
4126 return unless scalar @x == 3;
4127 my $dirents = $x[0];
4128 foreach my $de (keys %$dirents) {
4129 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
4130 my $p = $g->{path
}->full_path($de);
4131 next if $exists->{$p};
4132 next if (length $g->{path
}->{right
} &&
4133 ($self->check_path($p, $r) !=
4135 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
4136 $g->{ref}->full_path($de), 1);
4139 foreach my $g (@
$globs) {
4140 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4141 if ($path->{action
} =~ /^[AR]$/) {
4142 get_dir_check
($self, $exists, $g, $r);
4145 foreach (keys %$paths) {
4146 if (/$g->{path}->{left_regex}/ &&
4147 !/$g->{path}->{regex}/) {
4148 next if $paths->{$_}->{action
} !~ /^[AR]$/;
4149 get_dir_check
($self, $exists, $g, $r);
4151 next unless /$g->{path}->{regex}/;
4153 my $pathname = $g->{path
}->full_path($p);
4154 next if $exists->{$pathname};
4155 next if ($self->check_path($pathname, $r) !=
4157 $exists->{$pathname} = Git
::SVN
->init(
4158 $self->{url
}, $pathname, undef,
4159 $g->{ref}->full_path($p), 1);
4162 foreach (split m
#/#, $g->{path}->{left}) {
4164 next unless ($paths->{$c} &&
4165 ($paths->{$c}->{action
} =~ /^[AR]$/));
4166 get_dir_check
($self, $exists, $g, $r);
4174 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
4175 my $url = $self->{repos_root
};
4176 my @components = split(m!/!, $self->{svn_path
});
4179 $url .= "/$c" if length $c;
4180 eval { (ref $self)->new($url)->get_latest_revnum };
4181 } while ($@
&& ($c = shift @components));
4187 unless (defined $can_do_switch) {
4188 my $pool = SVN
::Pool
->new;
4190 $self->do_switch(1, '', 0, $self->{url
},
4191 SVN
::Delta
::Editor
->new, $pool);
4196 $rep->abort_report($pool);
4204 sub skip_unknown_revs
{
4206 my $errno = $err->apr_err();
4207 # Maybe the branch we're tracking didn't
4208 # exist when the repo started, so it's
4209 # not an error if it doesn't, just continue
4211 # Wonderfully consistent library, eh?
4212 # 160013 - svn:// and file://
4213 # 175002 - http(s)://
4214 # 175007 - http(s):// (this repo required authorization, too...)
4215 # More codes may be discovered later...
4216 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
4217 my $err_key = $err->expanded_message;
4218 # revision numbers change every time, filter them out
4219 $err_key =~ s/\d+/\0/g;
4220 $err_key = "$errno\0$err_key";
4221 unless ($ignored_err{$err_key}) {
4222 warn "W: Ignoring error from SVN, path probably ",
4223 "does not exist: ($errno): ",
4224 $err->expanded_message,"\n";
4225 warn "W: Do not be alarmed at the above message ",
4226 "git-svn is just searching aggressively for ",
4228 "This may take a while on large repositories\n";
4229 $ignored_err{$err_key} = 1;
4233 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4236 # svn_log_changed_path_t objects passed to get_log are likely to be
4237 # overwritten even if only the refs are copied to an external variable,
4238 # so we should dup the structures in their entirety. Using an externally
4239 # passed pool (instead of our temporary and quickly cleared pool in
4240 # Git::SVN::Ra) does not help matters at all...
4241 sub dup_changed_paths
{
4243 return undef unless $paths;
4245 foreach my $p (keys %$paths) {
4246 my $i = $paths->{$p};
4247 my %s = map { $_ => $i->$_ }
4248 qw
/copyfrom_path copyfrom_rev action/;
4254 package Git
::SVN
::Log
;
4257 use POSIX qw
/strftime/;
4258 use constant commit_log_separator
=> ('-' x
72) . "\n";
4259 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
4260 %rusers $show_commit $incremental/;
4265 return 1 if defined $c->{r
};
4267 # big commit message got truncated by the 16k pretty buffer in rev-list
4268 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
4269 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
4271 my @log = command
(qw
/cat-file commit/, $c->{c
});
4273 # shift off the headers
4274 shift @log while ($log[0] ne '');
4277 # TODO: make $c->{l} not have a trailing newline in the future
4278 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
4280 (undef, $c->{r
}, undef) = ::extract_metadata
(
4281 (grep(/^git-svn-id: /, @log))[-1]);
4283 return defined $c->{r
};
4287 return $color || Git
->repository->get_colorbool('color.diff');
4290 sub git_svn_log_cmd
{
4291 my ($r_min, $r_max, @args) = @_;
4293 my (@files, @log_opts);
4294 foreach my $x (@args) {
4295 if ($x eq '--' || @files) {
4298 if (::verify_ref
("$x^0")) {
4306 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
4307 $gs ||= Git
::SVN
->_new;
4308 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
4310 push @cmd, '-r' unless $non_recursive;
4311 push @cmd, qw
/--raw --name-status/ if $verbose;
4312 push @cmd, '--color' if log_use_color
();
4313 push @cmd, @log_opts;
4314 if (defined $r_max && $r_max == $r_min) {
4315 push @cmd, '--max-count=1';
4316 if (my $c = $gs->rev_map_get($r_max)) {
4319 } elsif (defined $r_max) {
4320 if ($r_max < $r_min) {
4321 ($r_min, $r_max) = ($r_max, $r_min);
4323 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4324 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4325 # If there are no commits in the range, both $c_max and $c_min
4326 # will be undefined. If there is at least 1 commit in the
4327 # range, both will be defined.
4328 return () if !defined $c_min || !defined $c_max;
4329 if ($c_min eq $c_max) {
4330 push @cmd, '--max-count=1', $c_min;
4332 push @cmd, '--boundary', "$c_min..$c_max";
4335 return (@cmd, @files);
4338 # adapted from pager.c
4340 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
4341 if (!defined $pager) {
4343 } elsif (length $pager == 0 || $pager eq 'cat') {
4346 $ENV{GIT_PAGER_IN_USE
} = defined($pager);
4350 return unless -t
*STDOUT
&& defined $pager;
4351 pipe my $rfd, my $wfd or return;
4352 defined(my $pid = fork) or ::fatal
"Can't fork: $!";
4354 open STDOUT
, '>&', $wfd or
4355 ::fatal
"Can't redirect to stdout: $!";
4358 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!";
4359 $ENV{LESS
} ||= 'FRSX';
4360 exec $pager or ::fatal
"Can't run pager: $! ($pager)";
4363 sub format_svn_date
{
4364 return strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4367 sub parse_git_date
{
4369 # Date::Parse isn't in the standard Perl distro :(
4370 if ($tz =~ s/^\+//) {
4371 $t += tz_to_s_offset
($tz);
4372 } elsif ($tz =~ s/^\-//) {
4373 $t -= tz_to_s_offset
($tz);
4378 sub set_local_timezone
{
4386 sub tz_to_s_offset
{
4389 return ($1 * 60) + ($tz * 3600);
4392 sub get_author_info
{
4393 my ($dest, $author, $t, $tz) = @_;
4394 $author =~ s/(?:^\s*|\s*$)//g;
4395 $dest->{a_raw
} = $author;
4398 $au = $rusers{$author} || undef;
4401 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4406 $dest->{t_utc
} = parse_git_date
($t, $tz);
4409 sub process_commit
{
4410 my ($c, $r_min, $r_max, $defer) = @_;
4411 if (defined $r_min && defined $r_max) {
4412 if ($r_min == $c->{r
} && $r_min == $r_max) {
4416 return 1 if $r_min == $r_max;
4417 if ($r_min < $r_max) {
4418 # we need to reverse the print order
4419 return 0 if (defined $limit && --$limit < 0);
4423 if ($r_min != $r_max) {
4424 return 1 if ($r_min < $c->{r
});
4425 return 1 if ($r_max > $c->{r
});
4428 return 0 if (defined $limit && --$limit < 0);
4437 if (my $l = $c->{l
}) {
4438 while ($l->[0] =~ /^\s*$/) { shift @
$l }
4441 $l_fmt ||= 'A' . length($c->{r
});
4442 print 'r',pack($l_fmt, $c->{r
}),' | ';
4443 print "$c->{c} | " if $show_commit;
4446 show_commit_normal
($c);
4450 sub show_commit_changed_paths
{
4452 return unless $c->{changed
};
4453 print "Changed paths:\n", @
{$c->{changed
}};
4456 sub show_commit_normal
{
4458 print commit_log_separator
, "r$c->{r} | ";
4459 print "$c->{c} | " if $show_commit;
4460 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
4463 if (my $l = $c->{l
}) {
4464 while ($l->[$#$l] eq "\n" && $#$l > 0
4465 && $l->[($#$l - 1)] eq "\n") {
4468 $nr_line = scalar @
$l;
4470 print "1 line\n\n\n";
4472 if ($nr_line == 1) {
4473 $nr_line = '1 line';
4475 $nr_line .= ' lines';
4477 print $nr_line, "\n";
4478 show_commit_changed_paths
($c);
4480 print $_ foreach @
$l;
4484 show_commit_changed_paths
($c);
4488 foreach my $x (qw
/raw stat diff/) {
4491 print $_ foreach @
{$c->{$x}}
4498 my ($r_min, $r_max);
4499 my $r_last = -1; # prevent dupes
4500 set_local_timezone
();
4501 if (defined $::_revision
) {
4502 if ($::_revision
=~ /^(\d+):(\d+)$/) {
4503 ($r_min, $r_max) = ($1, $2);
4504 } elsif ($::_revision
=~ /^\d+$/) {
4505 $r_min = $r_max = $::_revision
;
4507 ::fatal
"-r$::_revision is not supported, use ",
4508 "standard 'git log' arguments instead";
4513 @args = git_svn_log_cmd
($r_min, $r_max, @args);
4515 print commit_log_separator
unless $incremental || $oneline;
4518 my $log = command_output_pipe
(@args);
4520 my (@k, $c, $d, $stat);
4521 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4523 if (/^${esc_color}commit -?($::sha1_short)/o) {
4525 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
4527 process_commit
($c, $r_min, $r_max, \
@k) or
4532 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4533 get_author_info
($c, $1, $2, $3);
4534 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4536 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4537 push @
{$c->{raw
}}, $_;
4538 } elsif (/^${esc_color}[ACRMDT]\t/) {
4539 # we could add $SVN->{svn_path} here, but that requires
4540 # remote access at the moment (repo_path_split)...
4541 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4542 push @
{$c->{changed
}}, $_;
4543 } elsif (/^${esc_color}diff /o) {
4545 push @
{$c->{diff
}}, $_;
4547 push @
{$c->{diff
}}, $_;
4548 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
4549 $esc_color*[\
+\
-]*$esc_color$/x
) {
4551 push @
{$c->{stat}}, $_;
4552 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4553 push @
{$c->{stat}}, $_;
4555 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4556 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
4557 } elsif (s/^${esc_color} //o) {
4558 push @
{$c->{l
}}, $_;
4561 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
4563 process_commit
($c, $r_min, $r_max, \
@k);
4566 ($r_min, $r_max) = ($r_max, $r_min);
4567 process_commit
($_, $r_min, $r_max) foreach reverse @k;
4571 print commit_log_separator
unless $incremental || $oneline;
4580 my ($fh, $ctx, $rev);
4583 ($fh, $ctx) = command_output_pipe
('blame', @_, $path);
4584 while (my $line = <$fh>) {
4585 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4586 # Uncommitted edits show up as a rev ID of
4587 # all zeros, which we can't look up with
4590 (undef, $rev, undef) =
4592 $rev = '0' if (!$rev);
4596 $rev = sprintf('%-10s', $rev);
4597 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4602 ($fh, $ctx) = command_output_pipe
('blame', '-p', @_, 'HEAD',
4606 while (my $line = <$fh>) {
4607 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4609 (undef, $rev, undef) = ::cmt_metadata
($1);
4610 $rev = '0' if (!$rev);
4612 elsif ($line =~ /^author (.*)/) {
4613 $authors{$rev} = $1;
4614 $authors{$rev} =~ s/\s/_/g;
4616 elsif ($line =~ /^\t(.*)$/) {
4617 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
4621 command_close_pipe
($fh, $ctx);
4624 package Git
::SVN
::Migration
;
4625 # these version numbers do NOT correspond to actual version numbers
4626 # of git nor git-svn. They are just relative.
4628 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4630 # v1 layout: .git/$id/info/url, refs/remotes/$id
4632 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4634 # v3 layout: .git/svn/$id, refs/remotes/$id
4635 # - info/url may remain for backwards compatibility
4636 # - this is what we migrate up to this layout automatically,
4637 # - this will be used by git svn init on single branches
4638 # v3.1 layout (auto migrated):
4639 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4640 # for backwards compatibility
4642 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4643 # - this is only created for newly multi-init-ed
4644 # repositories. Similar in spirit to the
4645 # --use-separate-remotes option in git-clone (now default)
4646 # - we do not automatically migrate to this (following
4647 # the example set by core git)
4649 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
4650 # - newer, more-efficient format that uses 24-bytes per record
4651 # with no filler space.
4652 # - use xxd -c24 < .rev_map.$UUID to view and debug
4653 # - This is a one-way migration, repositories updated to the
4654 # new format will not be able to use old git-svn without
4655 # rebuilding the .rev_db. Rebuilding the rev_db is not
4656 # possible if noMetadata or useSvmProps are set; but should
4657 # be no problem for users that use the (sensible) defaults.
4661 use File
::Path qw
/mkpath/;
4662 use File
::Basename qw
/dirname basename/;
4663 use vars qw
/$_minimize/;
4665 sub migrate_from_v0
{
4666 my $git_dir = $ENV{GIT_DIR
};
4667 return undef unless -d
$git_dir;
4668 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
4672 my ($id, $orig_ref) = ($_, $_);
4673 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
4674 next unless -f
"$git_dir/$id/info/url";
4675 my $new_ref = "refs/remotes/$id";
4676 if (::verify_ref
("$new_ref^0")) {
4677 print STDERR
"W: $orig_ref is probably an old ",
4678 "branch used by an ancient version of ",
4680 "However, $new_ref also exists.\n",
4681 "We will not be able ",
4682 "to use this branch until this ",
4683 "ambiguity is resolved.\n";
4686 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
4687 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
4688 command_noisy
('update-ref', $new_ref, $orig_ref);
4689 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
4692 command_close_pipe
($fh, $ctx);
4693 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
4697 sub migrate_from_v1
{
4698 my $git_dir = $ENV{GIT_DIR
};
4700 return $migrated unless -d
$git_dir;
4701 my $svn_dir = "$git_dir/svn";
4703 # just in case somebody used 'svn' as their $id at some point...
4704 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
4706 print STDERR
"Migrating from a git-svn v1 layout...\n";
4708 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
4709 "$svn_dir\n\t(required for this version ",
4710 "($::VERSION) of git-svn) does not exist.\n";
4711 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
4714 next unless $x =~ s
#^refs/remotes/##;
4716 next unless -f
"$git_dir/$x/info/url";
4717 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
4719 my $dn = dirname
("$git_dir/svn/$x");
4720 mkpath
([$dn]) unless -d
$dn;
4721 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4722 mkpath
(["$git_dir/svn/svn"]);
4723 print STDERR
" - $git_dir/$x/info => ",
4724 "$git_dir/svn/$x/info\n";
4725 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4727 # don't worry too much about these, they probably
4728 # don't exist with repos this old (save for index,
4729 # and we can easily regenerate that)
4730 foreach my $f (qw
/unhandled.log index .rev_db/) {
4731 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4734 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
4735 rename "$git_dir/$x", "$git_dir/svn/$x" or
4740 command_close_pipe
($fh, $ctx);
4741 print STDERR
"Done migrating from a git-svn v1 layout\n";
4746 my ($l_map, $pfx, $path) = @_;
4748 foreach (<$path/*>) {
4749 if (-r
"$_/info/url") {
4750 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
4751 my $ref_id = $pfx . basename
$_;
4752 my $url = ::file_to_s
("$_/info/url");
4753 $l_map->{$ref_id} = $url;
4760 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4761 read_old_urls
($l_map, $x, $_);
4765 sub migrate_from_v2
{
4766 my @cfg = command
(qw
/config -l/);
4767 return if grep /^svn-remote\..+\.url=/, @cfg;
4769 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
4772 foreach my $ref_id (sort keys %l_map) {
4773 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
4775 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4782 sub minimize_connections
{
4783 my $r = Git
::SVN
::read_all_remotes
();
4785 my $root_repos = {};
4786 foreach my $repo_id (keys %$r) {
4787 my $url = $r->{$repo_id}->{url
} or next;
4788 my $fetch = $r->{$repo_id}->{fetch
} or next;
4789 my $ra = Git
::SVN
::Ra
->new($url);
4791 # skip existing cases where we already connect to the root
4792 if (($ra->{url
} eq $ra->{repos_root
}) ||
4793 ($ra->{repos_root
} eq $repo_id)) {
4794 $root_repos->{$ra->{url
}} = $repo_id;
4798 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
4799 my $root_path = $ra->{url
};
4800 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
4801 foreach my $path (keys %$fetch) {
4802 my $ref_id = $fetch->{$path};
4803 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
4805 # make sure we can read when connecting to
4806 # a higher level of a repository
4807 my ($last_rev, undef) = $gs->last_rev_commit;
4808 if (!defined $last_rev) {
4810 $root_ra->get_latest_revnum;
4814 my $new = $root_path;
4815 $new .= length $path ?
"/$path" : '';
4817 $root_ra->get_log([$new], $last_rev, $last_rev,
4821 $new_urls->{$ra->{repos_root
}}->{$new} =
4822 { ref_id
=> $ref_id,
4823 old_repo_id
=> $repo_id,
4824 old_path
=> $path };
4829 foreach my $url (keys %$new_urls) {
4830 # see if we can re-use an existing [svn-remote "repo_id"]
4831 # instead of creating a(n ugly) new section:
4832 my $repo_id = $root_repos->{$url} || $url;
4834 my $fetch = $new_urls->{$url};
4835 foreach my $path (keys %$fetch) {
4836 my $x = $fetch->{$path};
4837 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
4838 my $pfx = "svn-remote.$x->{old_repo_id}";
4840 my $old_fetch = quotemeta("$x->{old_path}:".
4841 "refs/remotes/$x->{ref_id}");
4842 command_noisy
(qw
/config --unset/,
4843 "$pfx.fetch", '^'. $old_fetch . '$');
4844 delete $r->{$x->{old_repo_id
}}->
4845 {fetch
}->{$x->{old_path
}};
4846 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
4847 command_noisy
(qw
/config --unset/,
4849 push @emptied, $x->{old_repo_id
}
4854 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
4855 "$ENV{GIT_DIR}/config";
4857 The following [svn-remote] sections in your config file ($file) are empty
4858 and can be safely removed:
4860 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
4864 sub migration_check
{
4868 minimize_connections
() if $_minimize;
4871 package Git
::IndexInfo
;
4874 use Git qw
/command_input_pipe command_close_pipe/;
4878 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
4879 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
4883 my ($self, $path) = @_;
4884 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
4885 return ++$self->{nr
};
4891 my ($self, $mode, $hash, $path) = @_;
4892 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
4893 return ++$self->{nr
};
4900 command_close_pipe
($self->{gui
}, $self->{ctx
});
4903 package Git
::SVN
::GlobSpec
;
4908 my ($class, $glob) = @_;
4910 $re =~ s!/+$!!g; # no need for trailing slashes
4911 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
4912 my ($left, $right) = ($1, $2);
4914 die "Only one '*' wildcard expansion ",
4915 "is supported (got $nr): '$glob'\n";
4916 } elsif ($nr == 0) {
4917 die "One '*' is needed for glob: '$glob'\n";
4919 $re = quotemeta($left) . $re . quotemeta($right);
4920 if (length $left && !($left =~ s!/+$!!g)) {
4921 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
4923 if (length $right && !($right =~ s!^/+!!g)) {
4924 die "Missing leading '/' on right side of: '$glob' ($right)\n";
4926 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
4927 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
4928 regex
=> qr/$re/, glob => $glob }, $class;
4932 my ($self, $path) = @_;
4933 return (length $self->{left
} ?
"$self->{left}/" : '') .
4934 $path . (length $self->{right
} ?
"/$self->{right}" : '');
4942 $remotes = { # returned by read_all_remotes()
4944 # svn-remote.svn.url=https://svn.musicpd.org
4945 url
=> 'https://svn.musicpd.org',
4946 # svn-remote.svn.fetch=mpd/trunk:trunk
4948 'mpd/trunk' => 'trunk',
4950 # svn-remote.svn.tags=mpd/tags/*:tags/*
4955 regex
=> qr!mpd/tags/([^/]+)$!,
4961 regex
=> qr!tags/([^/]+)$!,
4968 $log_entry hashref as returned by libsvn_log_entry
()
4970 log => 'whitespace-formatted log entry
4971 ', # trailing newline is preserved
4972 revision
=> '8', # integer
4973 date
=> '2004-02-24T17:01:44.108345Z', # commit date
4974 author
=> 'committer name'
4978 # this is generated by generate_diff();
4979 @mods = array of diff
-index line hashes
, each element represents one line
4980 of diff
-index output
4982 diff
-index line
($m hash
)
4984 mode_a
=> first column of diff
-index output
, no leading
':',
4985 mode_b
=> second column of diff
-index output
,
4986 sha1_b
=> sha1sum of the final blob
,
4987 chg
=> change type
[MCRADT
],
4988 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
4989 file_b
=> new
/current file name of a file
(any chg
)
4993 # retval of read_url_paths{,_all}();
4995 # repository root url
4996 'https://svn.musicpd.org' => {
4997 # repository path # GIT_SVN_ID
4998 'mpd/trunk' => 'trunk',
4999 'mpd/tags/0.11.5' => 'tags/0.11.5',
5004 I don
't trust the each() function on unless I created %hash myself
5005 because the internal iterator may not have started at base.