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
8 $_q $_authors $_authors_prog %users/;
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;
22 $Git::SVN
::_minimize_url
= 'unset';
24 if (! exists $ENV{SVN_SSH
}) {
25 if (exists $ENV{GIT_SSH
}) {
26 $ENV{SVN_SSH
} = $ENV{GIT_SSH
};
28 $ENV{SVN_SSH
} =~ s/\\/\\\\/g;
33 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
35 $| = 1; # unbuffer STDOUT
37 sub fatal
(@
) { print STDERR
"@_\n"; exit 1 }
38 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
41 if ($SVN::Core
::VERSION
lt '1.1.0') {
42 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
44 my $can_compress = eval { require Compress
::Zlib
; 1};
45 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
46 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
47 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
51 use File
::Basename qw
/dirname basename/;
52 use File
::Path qw
/mkpath/;
55 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
60 # import functions from Git into our packages, en masse
62 foreach (qw
/command command_oneline command_noisy command_output_pipe
63 command_input_pipe command_close_pipe
64 command_bidi_pipe command_close_bidi_pipe
/) {
65 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
66 Git::SVN::Migration Git::SVN::Log Git::SVN),
68 *{"${package}::$_"} = \
&{"Git::$_"};
75 $sha1 = qr/[a-f\d]{40}/;
76 $sha1_short = qr/[a-f\d]{4,40}/;
77 my ($_stdin, $_help, $_edit,
78 $_message, $_file, $_branch_dest,
80 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
81 $_merge, $_strategy, $_dry_run, $_local,
82 $_prefix, $_no_checkout, $_url, $_verbose,
83 $_git_format, $_commit_url, $_tag);
84 $Git::SVN
::_follow_parent
= 1;
86 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
87 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
88 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
89 'ignore-paths=s' => \
$SVN::Git
::Fetcher
::_ignore_regex
);
90 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
91 'authors-file|A=s' => \
$_authors,
92 'authors-prog=s' => \
$_authors_prog,
93 'repack:i' => \
$Git::SVN
::_repack
,
94 'noMetadata' => \
$Git::SVN
::_no_metadata
,
95 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
96 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
97 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
98 'no-checkout' => \
$_no_checkout,
100 'repack-flags|repack-args|repack-opts=s' =>
101 \
$Git::SVN
::_repack_flags
,
102 'use-log-author' => \
$Git::SVN
::_use_log_author
,
103 'add-author-from' => \
$Git::SVN
::_add_author_from
,
104 'localtime' => \
$Git::SVN
::_localtime
,
107 my ($_trunk, @_tags, @_branches, $_stdlayout);
109 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
110 'trunk|T=s' => \
$_trunk, 'tags|t=s@' => \
@_tags,
111 'branches|b=s@' => \
@_branches, 'prefix=s' => \
$_prefix,
112 'stdlayout|s' => \
$_stdlayout,
113 'minimize-url|m!' => \
$Git::SVN
::_minimize_url
,
114 'no-metadata' => sub { $icv{noMetadata
} = 1 },
115 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
116 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
117 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
119 my %cmt_opts = ( 'edit|e' => \
$_edit,
120 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
121 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
122 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
123 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
127 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
128 { 'revision|r=s' => \
$_revision,
129 'fetch-all|all' => \
$_fetch_all,
130 'parent|p' => \
$_fetch_parent,
132 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
133 { 'revision|r=s' => \
$_revision,
134 %fc_opts, %init_opts } ],
135 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
136 " (requires URL argument)",
138 'multi-init' => [ \
&cmd_multi_init
,
139 "Deprecated alias for ".
140 "'$0 init -T<trunk> -b<branches> -t<tags>'",
142 dcommit
=> [ \
&cmd_dcommit
,
143 'Commit several diffs to merge with upstream',
144 { 'merge|m|M' => \
$_merge,
145 'strategy|s=s' => \
$_strategy,
146 'verbose|v' => \
$_verbose,
147 'dry-run|n' => \
$_dry_run,
148 'fetch-all|all' => \
$_fetch_all,
149 'commit-url=s' => \
$_commit_url,
150 'revision|r=i' => \
$_revision,
151 'no-rebase' => \
$_no_rebase,
152 %cmt_opts, %fc_opts } ],
153 branch
=> [ \
&cmd_branch
,
154 'Create a branch in the SVN repository',
155 { 'message|m=s' => \
$_message,
156 'destination|d=s' => \
$_branch_dest,
157 'dry-run|n' => \
$_dry_run,
158 'tag|t' => \
$_tag } ],
159 tag
=> [ sub { $_tag = 1; cmd_branch
(@_) },
160 'Create a tag in the SVN repository',
161 { 'message|m=s' => \
$_message,
162 'destination|d=s' => \
$_branch_dest,
163 'dry-run|n' => \
$_dry_run } ],
164 'set-tree' => [ \
&cmd_set_tree
,
165 "Set an SVN repository to a git tree-ish",
166 { 'stdin' => \
$_stdin, %cmt_opts, %fc_opts, } ],
167 'create-ignore' => [ \
&cmd_create_ignore
,
168 'Create a .gitignore per svn:ignore',
169 { 'revision|r=i' => \
$_revision
171 'mkdirs' => [ \
&cmd_mkdirs
,
172 "recreate empty directories after a checkout",
173 { 'revision|r=i' => \
$_revision } ],
174 'propget' => [ \
&cmd_propget
,
175 'Print the value of a property on a file or directory',
176 { 'revision|r=i' => \
$_revision } ],
177 'proplist' => [ \
&cmd_proplist
,
178 'List all properties of a file or directory',
179 { 'revision|r=i' => \
$_revision } ],
180 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
181 { 'revision|r=i' => \
$_revision
183 'show-externals' => [ \
&cmd_show_externals
, "Show svn:externals listings",
184 { 'revision|r=i' => \
$_revision
186 'multi-fetch' => [ \
&cmd_multi_fetch
,
187 "Deprecated alias for $0 fetch --all",
188 { 'revision|r=s' => \
$_revision, %fc_opts } ],
189 'migrate' => [ sub { },
190 # no-op, we automatically run this anyways,
191 'Migrate configuration/metadata/layout from
192 previous versions of git-svn',
193 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
195 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
196 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
197 'revision|r=s' => \
$_revision,
198 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
199 'incremental' => \
$Git::SVN
::Log
::incremental
,
200 'oneline' => \
$Git::SVN
::Log
::oneline
,
201 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
202 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
203 'authors-file|A=s' => \
$_authors,
204 'color' => \
$Git::SVN
::Log
::color
,
205 'pager=s' => \
$Git::SVN
::Log
::pager
207 'find-rev' => [ \
&cmd_find_rev
,
208 "Translate between SVN revision numbers and tree-ish",
210 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
211 { 'merge|m|M' => \
$_merge,
212 'verbose|v' => \
$_verbose,
213 'strategy|s=s' => \
$_strategy,
214 'local|l' => \
$_local,
215 'fetch-all|all' => \
$_fetch_all,
216 'dry-run|n' => \
$_dry_run,
218 'commit-diff' => [ \
&cmd_commit_diff
,
219 'Commit a diff between two trees',
220 { 'message|m=s' => \
$_message,
221 'file|F=s' => \
$_file,
222 'revision|r=s' => \
$_revision,
224 'info' => [ \
&cmd_info
,
225 "Show info about the latest SVN revision
226 on the current branch",
227 { 'url' => \
$_url, } ],
228 'blame' => [ \
&Git
::SVN
::Log
::cmd_blame
,
229 "Show what revision and author last modified each line of a file",
230 { 'git-format' => \
$_git_format } ],
231 'reset' => [ \
&cmd_reset
,
232 "Undo fetches back to the specified SVN revision",
233 { 'revision|r=s' => \
$_revision,
234 'parent|p' => \
$_fetch_parent } ],
236 "Compress unhandled.log files in .git/svn and remove " .
237 "index files in .git/svn",
242 for (my $i = 0; $i < @ARGV; $i++) {
243 if (defined $cmd{$ARGV[$i]}) {
247 } elsif ($ARGV[$i] eq 'help') {
253 # make sure we're always running at the top-level working directory
254 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
255 unless (-d
$ENV{GIT_DIR
}) {
256 if ($git_dir_user_set) {
257 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
258 "but it is not a directory\n";
260 my $git_dir = delete $ENV{GIT_DIR
};
263 $cdup = command_oneline
(qw
/rev-parse --show-cdup/);
264 $git_dir = '.' unless ($cdup);
265 chomp $cdup if ($cdup);
266 $cdup = "." unless ($cdup && length $cdup);
267 } "Already at toplevel, but $git_dir not found\n";
268 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
269 unless (-d
$git_dir) {
270 die "$git_dir still not found after going to ",
273 $ENV{GIT_DIR
} = $git_dir;
275 $_repository = Git
->repository(Repository
=> $ENV{GIT_DIR
});
278 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
280 read_git_config
(\
%opts);
281 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
282 Getopt
::Long
::Configure
('pass_through');
284 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
285 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
286 'id|i=s' => \
$Git::SVN
::default_ref_id
,
287 'svn-remote|remote|R=s' => sub {
288 $Git::SVN
::no_reuse_existing
= 1;
289 $Git::SVN
::default_repo_id
= $_[1] });
290 exit 1 if (!$rv && $cmd && $cmd ne 'log');
293 version
() if $_version;
294 usage
(1) unless defined $cmd;
295 load_authors
() if $_authors;
296 if (defined $_authors_prog) {
297 $_authors_prog = "'" . File
::Spec
->rel2abs($_authors_prog) . "'";
300 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
301 Git
::SVN
::Migration
::migration_check
();
303 Git
::SVN
::init_vars
();
305 Git
::SVN
::verify_remotes_sanity
();
306 $cmd{$cmd}->[0]->(@ARGV);
309 post_fetch_checkout
();
312 ####################### primary functions ######################
314 my $exit = shift || 0;
315 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
317 git
-svn
- bidirectional operations between a single Subversion tree
and git
318 Usage
: git svn
<command
> [options
] [arguments
]\n
320 print $fd "Available commands:\n" unless $cmd;
322 foreach (sort keys %cmd) {
323 next if $cmd && $cmd ne $_;
324 next if /^multi-/; # don't show deprecated commands
325 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
326 foreach (sort keys %{$cmd{$_}->[2]}) {
327 # mixed-case options are for .git/config only
328 next if /[A-Z]/ && /^[a-z]+$/i;
329 # prints out arguments as they should be passed:
330 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
331 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
333 split /\|/,$_)," $x\n";
337 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
338 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
339 one git repository and want to keep them separate. See git-svn(1) for more
346 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
351 unless (-d $ENV{GIT_DIR}) {
352 my @init_db = ('init
');
353 push @init_db, "--template=$_template" if defined $_template;
354 if (defined $_shared) {
355 if ($_shared =~ /[a-z]/) {
356 push @init_db, "--shared=$_shared";
358 push @init_db, "--shared";
361 command_noisy(@init_db);
362 $_repository = Git->repository(Repository => ".git");
364 command_noisy('config
', 'core
.autocrlf
', 'false
');
366 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
367 foreach my $i (keys %icv) {
368 die "'$set' and '$i' cannot both be set\n" if $set;
369 next unless defined $icv{$i};
370 command_noisy('config
', "$pfx.$i", $icv{$i});
373 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
374 command_noisy('config
', "$pfx.ignore-paths", $$ignore_regex)
375 if defined $$ignore_regex;
379 my $repo_path = shift or return;
380 mkpath([$repo_path]) unless -d $repo_path;
381 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
382 $ENV{GIT_DIR} = '.git';
383 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
387 my ($url, $path) = @_;
388 if (!defined $path &&
389 (defined $_trunk || @_branches || @_tags ||
390 defined $_stdlayout) &&
391 $url !~ m#^[a-z\+]+://#) {
394 $path = basename($url) if !defined $path || !length $path;
395 cmd_init($url, $path);
396 Git::SVN::fetch_all($Git::SVN::default_repo_id);
397 command_oneline('config', 'svn.authorsfile', $_authors) if $_authors;
401 if (defined $_stdlayout) {
402 $_trunk = 'trunk' if (!defined $_trunk);
403 @_tags = 'tags' if (! @_tags);
404 @_branches = 'branches' if (! @_branches);
406 if (defined $_trunk || @_branches || @_tags) {
407 return cmd_multi_init(@_);
409 my $url = shift or die "SVN repository location required
",
410 "as a command
-line argument
\n";
411 $url = canonicalize_url($url);
415 if ($Git::SVN::_minimize_url eq 'unset') {
416 $Git::SVN::_minimize_url = 0;
419 Git::SVN->init($url);
423 if (grep /^\d+=./, @_) {
424 die "'<rev>=<commit>' fetch arguments are
",
425 "no longer supported
.\n";
429 die "Usage
: $0 fetch
[--all
] [--parent
] [svn
-remote
]\n";
431 $Git::SVN::no_reuse_existing = undef;
432 if ($_fetch_parent) {
433 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
435 die "Unable to determine upstream SVN information from
",
436 "working tree history
\n";
438 # just fetch, don't checkout.
439 $_no_checkout = 'true';
440 $_fetch_all ? $gs->fetch_all : $gs->fetch;
441 } elsif ($_fetch_all) {
444 $remote ||= $Git::SVN::default_repo_id;
445 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
451 if ($_stdin || !@commits) {
452 print "Reading from stdin
...\n";
455 if (/\b($sha1_short)\b/o) {
456 unshift @commits, $1;
461 foreach my $c (@commits) {
462 my @tmp = command('rev-parse',$c);
463 if (scalar @tmp == 1) {
465 } elsif (scalar @tmp > 1) {
466 push @revs, reverse(command('rev-list',@tmp));
468 fatal "Failed to rev
-parse
$c";
471 my $gs = Git::SVN->new;
472 my ($r_last, $cmt_last) = $gs->last_rev_commit;
474 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
475 fatal "There are new revisions that were fetched
",
476 "and need to be merged
(or acknowledged
) ",
477 "before committing
.\nlast rev
: $r_last\n",
478 " current
: $gs->{last_rev
}";
480 $gs->set_tree($_) foreach @revs;
481 print "Done committing
",scalar @revs," revisions to SVN
\n";
487 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
488 'Cannot dcommit with a dirty index. Commit your changes first, '
489 . "or stash them with
`git stash'.\n";
493 if ($head ne 'HEAD') {
495 command_oneline([qw/symbolic-ref -q HEAD/])
498 $old_head =~ s{^refs/heads/}{};
500 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
502 command(['checkout', $head], STDERR => 0);
506 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
508 die "Unable to determine upstream SVN information from ",
509 "$head history.\nPerhaps the repository is empty.";
512 if (defined $_commit_url) {
515 $url = eval { command_oneline('config', '--get',
516 "svn-remote.$gs->{repo_id}.commiturl") };
522 my $last_rev = $_revision if defined $_revision;
524 print "Committing to $url ...\n";
526 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
527 if ($_no_rebase && scalar(@$linear_refs) > 1) {
528 warn "Attempting to commit more than one change while ",
529 "--no-rebase is enabled.\n",
530 "If these changes depend on each other, re-running ",
531 "without --no-rebase may be required."
533 my $expect_url = $url;
534 Git::SVN::remove_username($expect_url);
536 my $d = shift @$linear_refs or last;
537 unless (defined $last_rev) {
538 (undef, $last_rev, undef) = cmt_metadata("$d~1");
539 unless (defined $last_rev) {
540 fatal "Unable to extract revision information ",
545 print "diff-tree $d~1 $d\n";
548 my %ed_opts = ( r => $last_rev,
549 log => get_commit_entry($d)->{log},
550 ra => Git::SVN::Ra->new($url),
551 config => SVN::Core::config_get_config(
552 $Git::SVN::Ra::config_dir
557 print "Committed r$_[0]\n";
561 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
562 print "No changes\n$d~1 == $d\n";
563 } elsif ($parents->{$d} && @{$parents->{$d}}) {
564 $gs->{inject_parents_dcommit}->{$cmt_rev} =
567 $_fetch_all ? $gs->fetch_all : $gs->fetch;
568 $last_rev = $cmt_rev;
571 # we always want to rebase against the current HEAD,
572 # not any head that was passed to us
573 my @diff = command('diff-tree', $d,
577 @finish = rebase_cmd();
578 print STDERR "W: $d and ", $gs->refname,
579 " differ, using @finish:\n",
580 join("\n", @diff), "\n";
582 print "No changes between current HEAD and ",
584 "\nResetting to the latest ",
586 @finish = qw/reset --mixed/;
588 command_noisy(@finish, $gs->refname);
591 my ($url_, $rev_, $uuid_, $gs_) =
592 working_head_info('HEAD', \@refs);
593 my ($linear_refs_, $parents_) =
594 linearize_history($gs_, \@refs);
595 if (scalar(@$linear_refs) !=
596 scalar(@$linear_refs_)) {
597 fatal "# of revisions changed ",
599 join("\n", @$linear_refs),
601 join("\n", @$linear_refs_), "\n",
602 'If you are attempting to commit ',
603 "merges, try running:\n\t",
604 'git rebase --interactive',
605 '--preserve-merges ',
607 "\nBefore dcommitting";
609 if ($url_ ne $expect_url) {
610 if ($url_ eq $gs->metadata_url) {
612 "Accepting rewritten URL:",
616 "URL mismatch after rebase:",
617 " $url_ != $expect_url";
620 if ($uuid_ ne $uuid) {
621 fatal "uuid mismatch after rebase: ",
626 for ($i = 0; $i < scalar @$linear_refs; $i++) {
627 my $new = $linear_refs_->[$i] or next;
629 $parents->{$linear_refs->[$i]};
639 my $new_head = command_oneline(qw/rev-parse HEAD/);
640 my $new_is_symbolic = eval {
641 command_oneline(qw/symbolic-ref -q HEAD/);
643 if ($new_is_symbolic) {
644 print "dcommitted the branch ", $head, "\n";
646 print "dcommitted on a detached HEAD because you gave ",
647 "a revision argument.\n",
648 "The rewritten commit is: ", $new_head, "\n";
650 command(['checkout', $old_head], STDERR => 0);
657 my ($branch_name, $head) = @_;
659 unless (defined $branch_name && length $branch_name) {
660 die(($_tag ? "tag" : "branch") . " name required\n");
664 my ($src, $rev, undef, $gs) = working_head_info($head);
666 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
667 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
669 if ($#{$allglobs} == 0) {
670 $glob = $allglobs->[0];
672 unless(defined $_branch_dest) {
674 $_tag ? "tag" : "branch",
675 " paths defined for Subversion repository.\n",
676 "You must specify where you want to create the ",
677 $_tag ? "tag" : "branch",
678 " with the --destination argument.\n";
680 foreach my $g (@{$allglobs}) {
681 # SVN::Git::Editor could probably be moved to Git.pm..
682 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
683 if ($_branch_dest =~ /$re/) {
688 unless (defined $glob) {
689 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
690 foreach my $g (@{$allglobs}) {
691 $g->{path}->{left} =~ /$dest_re/ or next;
693 die "Ambiguous destination: ",
694 $_branch_dest, "\nmatches both '",
695 $glob->{path}->{left}, "' and '",
696 $g->{path}->{left}, "'\n";
700 unless (defined $glob) {
702 $_tag ? "tag" : "branch",
703 " destination $_branch_dest\n";
707 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
708 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
710 my $ctx = SVN::Client->new(
711 auth => Git::SVN::Ra::_auth_providers(),
713 ${ $_[0] } = defined $_message
715 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
721 $ctx->ls($dst, 'HEAD', 0);
722 } and die "branch ${branch_name} already exists\n";
724 print "Copying ${src} at r${rev} to ${dst}...\n";
725 $ctx->copy($src, $rev, $dst)
732 my $revision_or_hash = shift or die "SVN or git revision required ",
733 "as a command-line argument\n";
735 if ($revision_or_hash =~ /^r\d+$/) {
739 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
741 die "Unable to determine upstream SVN information from ",
744 my $desired_revision = substr($revision_or_hash, 1);
745 $result = $gs->rev_map_get($desired_revision, $uuid);
747 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
750 print "$result\n" if $result;
754 command_noisy(qw/update-index --refresh/);
755 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
757 die "Unable to determine upstream SVN information from ",
758 "working tree history\n";
761 print "Remote Branch: " . $gs->refname . "\n";
762 print "SVN URL: " . $url . "\n";
765 if (command(qw/diff-index HEAD --/)) {
766 print STDERR "Cannot rebase with uncommited changes:\n";
767 command_noisy('status');
771 # rebase will checkout for us, so no need to do it explicitly
772 $_no_checkout = 'true';
773 $_fetch_all ? $gs->fetch_all : $gs->fetch;
775 command_noisy(rebase_cmd(), $gs->refname);
779 sub cmd_show_ignore {
780 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
781 $gs ||= Git::SVN->new;
782 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
783 $gs->prop_walk($gs->{path}, $r, sub {
784 my ($gs, $path, $props) = @_;
785 print STDOUT "\n# $path\n";
786 my $s = $props->{'svn:ignore'} or return;
787 $s =~ s/[\r\n]+/\n/g;
795 sub cmd_show_externals {
796 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
797 $gs ||= Git::SVN->new;
798 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
799 $gs->prop_walk($gs->{path}, $r, sub {
800 my ($gs, $path, $props) = @_;
801 print STDOUT "\n# $path\n";
802 my $s = $props->{'svn:externals'} or return;
803 $s =~ s/[\r\n]+/\n/g;
810 sub cmd_create_ignore {
811 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
812 $gs ||= Git::SVN->new;
813 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
814 $gs->prop_walk($gs->{path}, $r, sub {
815 my ($gs, $path, $props) = @_;
816 # $path is of the form /path/to/dir/
818 # SVN can have attributes on empty directories,
819 # which git won't track
820 mkpath([$path]) unless -d $path;
821 my $ignore = $path . '.gitignore';
822 my $s = $props->{'svn:ignore'} or return;
823 open(GITIGNORE, '>', $ignore)
824 or fatal("Failed to open `$ignore' for writing: $!");
825 $s =~ s/[\r\n]+/\n/g;
828 # Prefix all patterns so that the ignore doesn't apply
829 # to sub-directories.
831 print GITIGNORE
"$s\n";
833 or fatal
("Failed to close `$ignore': $!");
834 command_noisy
('add', '-f', $ignore);
839 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
840 $gs ||= Git
::SVN
->new;
841 $gs->mkemptydirs($_revision);
844 sub canonicalize_path
{
846 my $dot_slash_added = 0;
847 if (substr($path, 0, 1) ne "/") {
848 $path = "./" . $path;
849 $dot_slash_added = 1;
851 # File::Spec->canonpath doesn't collapse x/../y into y (for a
852 # good reason), so let's do this manually.
854 $path =~ s
#/\.(?:/|$)#/#g;
855 $path =~ s
#/[^/]+/\.\.##g;
857 $path =~ s
#^\./## if $dot_slash_added;
863 sub canonicalize_url
{
865 $url =~ s
#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
871 # Helper for cmd_propget and cmd_proplist below.
874 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
875 $gs ||= Git
::SVN
->new;
877 # prefix THE PATH by the sub-directory from which the user
879 $path = $cmd_dir_prefix . $path;
880 fatal
("No such file or directory: $path") unless -e
$path;
881 my $is_dir = -d
$path ?
1 : 0;
882 $path = $gs->{path
} . '/' . $path;
884 # canonicalize the path (otherwise libsvn will abort or fail to
886 $path = canonicalize_path
($path);
888 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
891 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
894 (undef, $props) = $gs->ra->get_file($path, $r, undef);
899 # cmd_propget (PROP, PATH)
900 # ------------------------
901 # Print the SVN property PROP for PATH.
903 my ($prop, $path) = @_;
904 $path = '.' if not defined $path;
905 usage
(1) if not defined $prop;
906 my $props = get_svnprops
($path);
907 if (not defined $props->{$prop}) {
908 fatal
("`$path' does not have a `$prop' SVN property.");
910 print $props->{$prop} . "\n";
913 # cmd_proplist (PATH)
914 # -------------------
915 # Print the list of SVN properties for PATH.
918 $path = '.' if not defined $path;
919 my $props = get_svnprops
($path);
920 print "Properties on '$path':\n";
921 foreach (sort keys %{$props}) {
928 unless (defined $_trunk || @_branches || @_tags) {
932 $_prefix = '' unless defined $_prefix;
934 $url = canonicalize_url
($url);
938 if (defined $_trunk) {
939 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
940 # try both old-style and new-style lookups:
941 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
943 my ($trunk_url, $trunk_path) =
944 complete_svn_url
($url, $_trunk);
945 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
949 return unless @_branches || @_tags;
950 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
951 foreach my $path (@_branches) {
952 complete_url_ls_init
($ra, $path, '--branches/-b', $_prefix);
954 foreach my $path (@_tags) {
955 complete_url_ls_init
($ra, $path, '--tags/-t', $_prefix.'tags/');
959 sub cmd_multi_fetch
{
960 $Git::SVN
::no_reuse_existing
= undef;
961 my $remotes = Git
::SVN
::read_all_remotes
();
962 foreach my $repo_id (sort keys %$remotes) {
963 if ($remotes->{$repo_id}->{url
}) {
964 Git
::SVN
::fetch_all
($repo_id, $remotes);
969 # this command is special because it requires no metadata
970 sub cmd_commit_diff
{
971 my ($ta, $tb, $url) = @_;
972 my $usage = "Usage: $0 commit-diff -r<revision> ".
973 "<tree-ish> <tree-ish> [<URL>]";
974 fatal
($usage) if (!defined $ta || !defined $tb);
977 my $gs = eval { Git
::SVN
->new };
979 fatal
("Needed URL or usable git-svn --id in ",
980 "the command-line\n", $usage);
983 $svn_path = $gs->{path
};
985 unless (defined $_revision) {
986 fatal
("-r|--revision is a required argument\n", $usage);
988 if (defined $_message && defined $_file) {
989 fatal
("Both --message/-m and --file/-F specified ",
990 "for the commit message.\n",
991 "I have no idea what you mean");
993 if (defined $_file) {
994 $_message = file_to_s
($_file);
996 $_message ||= get_commit_entry
($tb)->{log};
998 my $ra ||= Git
::SVN
::Ra
->new($url);
1001 $r = $ra->get_latest_revnum;
1002 } elsif ($r !~ /^\d+$/) {
1003 die "revision argument: $r not understood by git-svn\n";
1005 my %ed_opts = ( r
=> $r,
1010 editor_cb
=> sub { print "Committed r$_[0]\n" },
1011 svn_path
=> $svn_path );
1012 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
1013 print "No changes\n$ta == $tb\n";
1017 sub escape_uri_only
{
1020 foreach (split m{/}, $uri) {
1021 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
1029 if ($url =~ m
#^([^:]+)://([^/]*)(.*)$#) {
1030 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
1031 $url = "$scheme://$domain$uri";
1037 my $path = canonicalize_path
(defined($_[0]) ?
$_[0] : ".");
1038 my $fullpath = canonicalize_path
($cmd_dir_prefix . $path);
1040 die "Too many arguments specified\n";
1043 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
1045 if (!$file_type && !$diff_status) {
1046 print STDERR
"svn: '$path' is not under version control\n";
1050 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1052 die "Unable to determine upstream SVN information from ",
1053 "working tree history\n";
1056 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1057 $path = "." if $path eq "";
1059 my $full_url = $url . ($fullpath eq "" ?
"" : "/$fullpath");
1062 print escape_url
($full_url), "\n";
1066 my $result = "Path: $path\n";
1067 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
1068 $result .= "URL: " . escape_url
($full_url) . "\n";
1071 my $repos_root = $gs->repos_root;
1072 Git
::SVN
::remove_username
($repos_root);
1073 $result .= "Repository Root: " . escape_url
($repos_root) . "\n";
1076 $result .= "Repository Root: (offline)\n";
1078 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1079 ($SVN::Core
::VERSION
le '1.5.4' || $file_type ne "dir");
1080 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
1082 $result .= "Node Kind: " .
1083 ($file_type eq "dir" ?
"directory" : "file") . "\n";
1085 my $schedule = $diff_status eq "A"
1087 : ($diff_status eq "D" ?
"delete" : "normal");
1088 $result .= "Schedule: $schedule\n";
1090 if ($diff_status eq "A") {
1091 print $result, "\n";
1095 my ($lc_author, $lc_rev, $lc_date_utc);
1096 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $fullpath);
1097 my $log = command_output_pipe
(@args);
1098 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1100 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1102 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
1103 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1104 (undef, $lc_rev, undef) = ::extract_metadata
($1);
1109 Git
::SVN
::Log
::set_local_timezone
();
1111 $result .= "Last Changed Author: $lc_author\n";
1112 $result .= "Last Changed Rev: $lc_rev\n";
1113 $result .= "Last Changed Date: " .
1114 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
1116 if ($file_type ne "dir") {
1117 my $text_last_updated_date =
1118 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
1120 "Text Last Updated: " .
1121 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
1124 if ($diff_status eq "D") {
1126 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
1127 if ($file_type eq "link") {
1128 my $file_name = <$fh>;
1129 $checksum = md5sum
("link $file_name");
1131 $checksum = md5sum
($fh);
1133 command_close_pipe
($fh, $ctx);
1134 } elsif ($file_type eq "link") {
1136 command
(qw(cat-file blob), "HEAD:$path");
1138 md5sum
("link " . $file_name);
1140 open FILE
, "<", $path or die $!;
1141 $checksum = md5sum
(\
*FILE
);
1142 close FILE
or die $!;
1144 $result .= "Checksum: " . $checksum . "\n";
1147 print $result, "\n";
1151 my $target = shift || $_revision or die "SVN revision required\n";
1152 $target = $1 if $target =~ /^r(\d+)$/;
1153 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1154 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1156 die "Unable to determine upstream SVN information from ".
1159 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1160 $gs->rev_map_set($r, $c, 'reset', $uuid);
1161 print "r$r = $c ($gs->{ref_id})\n";
1165 if (!$can_compress) {
1166 warn "Compress::Zlib could not be found; unhandled.log " .
1167 "files will not be compressed.\n";
1169 find
({ wanted
=> \
&gc_directory
, no_chdir
=> 1}, "$ENV{GIT_DIR}/svn");
1172 ########################### utility functions #########################
1175 my @cmd = qw
/rebase/;
1176 push @cmd, '-v' if $_verbose;
1177 push @cmd, qw
/--merge/ if $_merge;
1178 push @cmd, "--strategy=$_strategy" if $_strategy;
1182 sub post_fetch_checkout
{
1183 return if $_no_checkout;
1184 my $gs = $Git::SVN
::_head
or return;
1185 return if verify_ref
('refs/heads/master^0');
1187 # look for "trunk" ref if it exists
1188 my $remote = Git
::SVN
::read_all_remotes
()->{$gs->{repo_id
}};
1189 my $fetch = $remote->{fetch
};
1191 foreach my $p (keys %$fetch) {
1192 basename
($fetch->{$p}) eq 'trunk' or next;
1193 $gs = Git
::SVN
->new($fetch->{$p}, $gs->{repo_id
}, $p);
1198 my $valid_head = verify_ref
('HEAD^0');
1199 command_noisy
(qw(update-ref refs/heads/master), $gs->refname);
1200 return if ($valid_head || !verify_ref
('HEAD^0'));
1202 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
1203 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
1204 return if -f
$index;
1206 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
1207 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
1208 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
1209 print STDERR
"Checked out HEAD:\n ",
1210 $gs->full_url, " r", $gs->last_rev, "\n";
1211 $gs->mkemptydirs($gs->last_rev);
1214 sub complete_svn_url
{
1215 my ($url, $path) = @_;
1217 if ($path !~ m
#^[a-z\+]+://#) {
1218 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
1219 fatal
("E: '$path' is not a complete URL ",
1220 "and a separate URL is not specified");
1222 return ($url, $path);
1227 sub complete_url_ls_init
{
1228 my ($ra, $repo_path, $switch, $pfx) = @_;
1229 unless ($repo_path) {
1230 print STDERR
"W: $switch not specified\n";
1233 $repo_path =~ s
#/+$##;
1234 if ($repo_path =~ m
#^[a-z\+]+://#) {
1235 $ra = Git
::SVN
::Ra
->new($repo_path);
1238 $repo_path =~ s
#^/+##;
1240 fatal
("E: '$repo_path' is not a complete URL ",
1241 "and a separate URL is not specified");
1244 my $url = $ra->{url
};
1245 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
1246 my $k = "svn-remote.$gs->{repo_id}.url";
1247 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
1248 if ($orig_url && ($orig_url ne $gs->{url
})) {
1249 die "$k already set: $orig_url\n",
1250 "wanted to set to: $gs->{url}\n";
1252 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
1253 my $remote_path = "$gs->{path}/$repo_path";
1254 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
1255 $remote_path =~ s
#/+#/#g;
1256 $remote_path =~ s
#^/##g;
1257 $remote_path .= "/*" if $remote_path !~ /\
*/;
1258 my ($n) = ($switch =~ /^--(\w+)/);
1259 if (length $pfx && $pfx !~ m
#/$#) {
1260 die "--prefix='$pfx' must have a trailing slash '/'\n";
1262 command_noisy
('config',
1264 "svn-remote.$gs->{repo_id}.$n",
1265 "$remote_path:refs/remotes/$pfx*" .
1266 ('/*' x
(($remote_path =~ tr
/*/*/) - 1)) );
1271 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1272 { STDERR
=> 0 }); };
1275 sub get_tree_from_treeish
{
1277 # $treeish can be a symbolic ref, too:
1278 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1280 while ($type eq 'tag') {
1281 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1283 if ($type eq 'commit') {
1284 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1286 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1287 die "Unable to get tree from $treeish\n" unless $expected;
1288 } elsif ($type eq 'tree') {
1289 $expected = $treeish;
1291 die "$treeish is a $type, expected tree, tag or commit\n";
1296 sub get_commit_entry
{
1297 my ($treeish) = shift;
1298 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
1299 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1300 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1301 open my $log_fh, '>', $commit_editmsg or croak
$!;
1303 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1304 if ($type eq 'commit' || $type eq 'tag') {
1305 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1313 $in_msg = 1 if (/^\s*$/);
1314 $author = $1 if (/^author (.*>)/);
1315 } elsif (/^git-svn-id: /) {
1316 # skip this for now, we regenerate the
1317 # correct one on re-fetch anyways
1318 # TODO: set *:merge properties or like...
1320 if (/^From:/ || /^Signed-off-by:/) {
1326 $msgbuf =~ s/\s+$//s;
1327 if ($Git::SVN
::_add_author_from
&& defined($author)
1329 $msgbuf .= "\n\nFrom: $author";
1331 print $log_fh $msgbuf or croak
$!;
1332 command_close_pipe
($msg_fh, $ctx);
1334 close $log_fh or croak
$!;
1336 if ($_edit || ($type eq 'tree')) {
1337 chomp(my $editor = command_oneline
(qw(var GIT_EDITOR)));
1338 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
1340 rename $commit_editmsg, $commit_msg or croak
$!;
1343 # SVN requires messages to be UTF-8 when entering the repo
1345 open $log_fh, '<', $commit_msg or croak
$!;
1347 chomp($log_entry{log} = <$log_fh>);
1349 my $enc = Git
::config
('i18n.commitencoding') || 'UTF-8';
1350 my $msg = $log_entry{log};
1352 eval { $msg = Encode
::decode
($enc, $msg, 1) };
1354 die "Could not decode as $enc:\n", $msg,
1355 "\nPerhaps you need to set i18n.commitencoding\n";
1358 eval { $msg = Encode
::encode
('UTF-8', $msg, 1) };
1359 die "Could not encode as UTF-8:\n$msg\n" if $@
;
1361 $log_entry{log} = $msg;
1363 close $log_fh or croak
$!;
1370 my ($str, $file, $mode) = @_;
1371 open my $fd,'>',$file or croak
$!;
1372 print $fd $str,"\n" or croak
$!;
1373 close $fd or croak
$!;
1374 chmod ($mode &~ umask, $file) if (defined $mode);
1379 open my $fd,'<',$file or croak
"$!: file: $file\n";
1382 close $fd or croak
$!;
1387 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1389 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1390 my $log = $cmd eq 'log';
1391 while (<$authors>) {
1393 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1394 my ($user, $name, $email) = ($1, $2, $3);
1396 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1398 $users{$user} = [$name, $email];
1401 close $authors or croak
$!;
1404 # convert GetOpt::Long specs for use by git-config
1405 sub read_git_config
{
1408 foreach my $o (keys %$opts) {
1409 # if we have mixedCase and a long option-only, then
1410 # it's a config-only variable that we don't need for
1412 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1413 my $v = $opts->{$o};
1414 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1416 my $arg = 'git config';
1417 $arg .= ' --int' if ($o =~ /[:=]i$/);
1418 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1419 if (ref $v eq 'ARRAY') {
1420 chomp(my @tmp = `$arg --get-all svn.$key`);
1423 chomp(my $tmp = `$arg --get svn.$key`);
1424 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1429 delete @
$opts{@config_only} if @config_only;
1432 sub extract_metadata
{
1433 my $id = shift or return (undef, undef, undef);
1434 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1435 \s
([a
-f\d\
-]+)$/ix
);
1436 if (!defined $rev || !$uuid || !$url) {
1437 # some of the original repositories I made had
1438 # identifiers like this:
1439 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
1441 return ($url, $rev, $uuid);
1445 return extract_metadata
((grep(/^git-svn-id: /,
1446 command
(qw
/cat-file commit/, shift)))[-1]);
1449 sub cmt_sha2rev_batch
{
1451 my ($pid, $in, $out, $ctx) = command_bidi_pipe
(qw
/cat-file --batch/);
1454 foreach my $sha (@
{$list}) {
1457 print $out $sha, "\n";
1459 while (my $line = <$in>) {
1460 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1463 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1467 } elsif ($line =~ /^(git-svn-id: )/) {
1468 my (undef, $rev, undef) =
1469 extract_metadata
($line);
1473 $size -= length($line);
1474 last if ($size == 0);
1478 command_close_bidi_pipe
($pid, $in, $out, $ctx);
1483 sub working_head_info
{
1484 my ($head, $refs) = @_;
1485 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
1486 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1490 if ( m{^commit ($::sha1)$} ) {
1491 unshift @
$refs, $hash if $hash and $refs;
1495 next unless s{^\s*(git-svn-id:)}{$1};
1496 my ($url, $rev, $uuid) = extract_metadata
($_);
1497 if (defined $url && defined $rev) {
1498 next if $max{$url} and $max{$url} < $rev;
1499 if (my $gs = Git
::SVN
->find_by_url($url)) {
1500 my $c = $gs->rev_map_get($rev, $uuid);
1501 if ($c && $c eq $hash) {
1502 close $fh; # break the pipe
1503 return ($url, $rev, $uuid, $gs);
1505 $max{$url} ||= $gs->rev_map_max;
1510 command_close_pipe
($fh, $ctx);
1511 (undef, undef, undef, undef);
1514 sub read_commit_parents
{
1515 my ($parents, $c) = @_;
1516 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1517 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1518 @
{$parents->{$c}} = split(/ /, $p);
1521 sub linearize_history
{
1522 my ($gs, $refs) = @_;
1524 foreach my $c (@
$refs) {
1525 read_commit_parents
(\
%parents, $c);
1530 my $last_svn_commit = $gs->last_commit;
1531 foreach my $c (reverse @
$refs) {
1532 next if $c eq $last_svn_commit;
1535 unshift @linear_refs, $c;
1538 # we only want the first parent to diff against for linear
1539 # history, we save the rest to inject when we finalize the
1541 my $fp_a = verify_ref
("$c~1");
1542 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1543 if (!$fp_a || !$fp_b) {
1545 "has no parent commit, and therefore ",
1546 "nothing to diff against.\n",
1547 "You should be working from a repository ",
1548 "originally created by git-svn\n";
1550 if ($fp_a ne $fp_b) {
1551 die "$c~1 = $fp_a, however parsing commit $c ",
1552 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1555 foreach my $p (@
{$parents{$c}}) {
1559 (\
@linear_refs, \
%parents);
1562 sub find_file_type_and_diff_status
{
1564 return ('dir', '') if $path eq '';
1567 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1568 my $diff_status = (split(' ', $diff_output))[0] || "";
1570 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1572 return (undef, undef) if !$diff_status && !$ls_tree;
1574 if ($diff_status eq "A") {
1575 return ("link", $diff_status) if -l
$path;
1576 return ("dir", $diff_status) if -d
$path;
1577 return ("file", $diff_status);
1580 my $mode = (split(' ', $ls_tree))[0] || "";
1582 return ("link", $diff_status) if $mode eq "120000";
1583 return ("dir", $diff_status) if $mode eq "040000";
1584 return ("file", $diff_status);
1590 my $md5 = Digest
::MD5
->new();
1591 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
1592 $md5->addfile($arg) or croak
$!;
1593 } elsif ($ref eq 'SCALAR') {
1594 $md5->add($$arg) or croak
$!;
1596 $md5->add($arg) or croak
$!;
1598 ::fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1600 return $md5->hexdigest();
1604 if ($can_compress && -f
$_ && basename
($_) eq "unhandled.log") {
1605 my $out_filename = $_ . ".gz";
1606 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1608 my $gz = Compress
::Zlib
::gzopen
($out_filename, "ab") or
1609 die "Unable to open $out_filename: $!\n";
1612 while ($res = sysread($in_fh, my $str, 1024)) {
1613 $gz->gzwrite($str) or
1614 die "Unable to write: ".$gz->gzerror()."!\n";
1616 unlink $_ or die "unlink $File::Find::name: $!\n";
1617 } elsif (-f
$_ && basename
($_) eq "index") {
1618 unlink $_ or die "unlink $_: $!\n";
1625 use Fcntl qw
/:DEFAULT :seek/;
1626 use constant rev_map_fmt
=> 'NH40';
1627 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
1628 $_repack $_repack_flags $_use_svm_props $_head
1629 $_use_svnsync_props $no_reuse_existing $_minimize_url
1630 $_use_log_author $_add_author_from $_localtime/;
1632 use File
::Path qw
/mkpath/;
1633 use File
::Copy qw
/copy/;
1636 my ($_gc_nr, $_gc_period);
1638 # properties that we do not log:
1641 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
1642 svn
:special svn
:executable
1643 svn
:entry
:committed
-rev
1644 svn
:entry
:last-author
1646 svn
:entry
:committed
-date
/;
1648 # some options are read globally, but can be overridden locally
1649 # per [svn-remote "..."] section. Command-line options will *NOT*
1650 # override options set in an [svn-remote "..."] section
1652 for my $option (qw
/follow_parent no_metadata use_svm_props
1653 use_svnsync_props
/) {
1656 my $prop = "-$option";
1659 return $self->{$prop} if exists $self->{$prop};
1660 my $k = "svn-remote.$self->{repo_id}.$key";
1661 eval { command_oneline
(qw
/config --get/, $k) };
1663 $self->{$prop} = ${"Git::SVN::_$option"};
1665 my $v = command_oneline
(qw
/config --bool/,$k);
1666 $self->{$prop} = $v eq 'false' ?
0 : 1;
1668 return $self->{$prop};
1674 my (%LOCKFILES, %INDEX_FILES);
1676 unlink keys %LOCKFILES if %LOCKFILES;
1677 unlink keys %INDEX_FILES if %INDEX_FILES;
1680 sub resolve_local_globs
{
1681 my ($url, $fetch, $glob_spec) = @_;
1682 return unless defined $glob_spec;
1683 my $ref = $glob_spec->{ref};
1684 my $path = $glob_spec->{path
};
1685 foreach (command
(qw
#for-each-ref --format=%(refname) refs/#)) {
1686 next unless m
#^$ref->{regex}$#;
1688 my $pathname = desanitize_refname
($path->full_path($p));
1689 my $refname = desanitize_refname
($ref->full_path($p));
1690 if (my $existing = $fetch->{$pathname}) {
1691 if ($existing ne $refname) {
1692 die "Refspec conflict:\n",
1693 "existing: $existing\n",
1694 " globbed: $refname\n";
1696 my $u = (::cmt_metadata
("$refname"))[0];
1697 $u =~ s!^\Q$url\E(/|$)!! or die
1698 "$refname: '$url' not found in '$u'\n";
1699 if ($pathname ne $u) {
1700 warn "W: Refspec glob conflict ",
1701 "(ref: $refname):\n",
1702 "expected path: $pathname\n",
1704 "Continuing ahead with $u\n";
1708 $fetch->{$pathname} = $refname;
1713 sub parse_revision_argument
{
1714 my ($base, $head) = @_;
1715 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
1716 return ($base, $head);
1718 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
1719 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
1720 return ($head, $head) if ($::_revision
eq 'HEAD');
1721 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
1722 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
1723 die "revision argument: $::_revision not understood by git-svn\n";
1727 my ($repo_id, $remotes) = @_;
1731 $repo_id = $gs->{repo_id
};
1733 $remotes ||= read_all_remotes
();
1734 my $remote = $remotes->{$repo_id} or
1735 die "[svn-remote \"$repo_id\"] unknown\n";
1736 my $fetch = $remote->{fetch
};
1737 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
1739 my $ra = Git
::SVN
::Ra
->new($url);
1740 my $uuid = $ra->get_uuid;
1741 my $head = $ra->get_latest_revnum;
1742 $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] });
1743 my $base = defined $fetch ?
$head : 0;
1745 # read the max revs for wildcard expansion (branches/*, tags/*)
1746 foreach my $t (qw
/branches tags/) {
1747 defined $remote->{$t} or next;
1748 push @globs, @
{$remote->{$t}};
1750 my $max_rev = eval { tmp_config
(qw
/--int --get/,
1751 "svn-remote.$repo_id.${t}-maxRev") };
1752 if (defined $max_rev && ($max_rev < $base)) {
1754 } elsif (!defined $max_rev) {
1760 foreach my $p (sort keys %$fetch) {
1761 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
1762 my $lr = $gs->rev_map_max;
1764 $base = $lr if ($lr < $base);
1770 ($base, $head) = parse_revision_argument
($base, $head);
1771 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
1774 sub read_all_remotes
{
1776 my $use_svm_props = eval { command_oneline
(qw
/config
--bool
1777 svn
.useSvmProps
/) };
1778 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
1779 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
1780 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
1781 if (m!^(.+)\.fetch=$svn_refspec$!) {
1782 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1783 die("svn-remote.$remote: remote ref '$remote_ref' "
1784 . "must start with 'refs/'\n")
1785 unless $remote_ref =~ m{^refs/};
1786 $r->{$remote}->{fetch
}->{$local_ref} = $remote_ref;
1787 $r->{$remote}->{svm
} = {} if $use_svm_props;
1788 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1789 $r->{$1}->{svm
} = {};
1790 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1791 $r->{$1}->{url
} = $2;
1792 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1793 my ($remote, $t, $local_ref, $remote_ref) =
1795 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1796 . "must start with 'refs/'\n")
1797 unless $remote_ref =~ m{^refs/};
1801 path
=> Git
::SVN
::GlobSpec
->new($local_ref),
1802 ref => Git
::SVN
::GlobSpec
->new($remote_ref) };
1803 if (length($rs->{ref}->{right
}) != 0) {
1804 die "The '*' glob character must be the last ",
1805 "character of '$remote_ref'\n";
1807 push @
{ $r->{$remote}->{$t} }, $rs;
1812 if (defined $r->{$_}->{svm
}) {
1815 my $section = "svn-remote.$_";
1817 source
=> tmp_config
('--get',
1818 "$section.svm-source"),
1819 replace
=> tmp_config
('--get',
1820 "$section.svm-replace"),
1823 $r->{$_}->{svm
} = $svm;
1831 $_gc_nr = $_gc_period = 1000;
1832 if (defined $_repack || defined $_repack_flags) {
1833 warn "Repack options are obsolete; they have no effect.\n";
1837 sub verify_remotes_sanity
{
1838 return unless -d
$ENV{GIT_DIR
};
1840 foreach (command
(qw
/config -l/)) {
1841 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1843 die "Remote ref refs/remote/$1 is tracked by",
1844 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1845 "Please resolve this ambiguity in ",
1846 "your git configuration file before ",
1854 sub find_existing_remote
{
1855 my ($url, $remotes) = @_;
1856 return undef if $no_reuse_existing;
1858 foreach my $repo_id (keys %$remotes) {
1859 my $u = $remotes->{$repo_id}->{url
} or next;
1861 $existing = $repo_id;
1867 sub init_remote_config
{
1868 my ($self, $url, $no_write) = @_;
1869 $url =~ s!/+$!!; # strip trailing slash
1870 my $r = read_all_remotes
();
1871 my $existing = find_existing_remote
($url, $r);
1873 unless ($no_write) {
1874 print STDERR
"Using existing ",
1875 "[svn-remote \"$existing\"]\n";
1877 $self->{repo_id
} = $existing;
1878 } elsif ($_minimize_url) {
1879 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1880 $existing = find_existing_remote
($min_url, $r);
1882 unless ($no_write) {
1883 print STDERR
"Using existing ",
1884 "[svn-remote \"$existing\"]\n";
1886 $self->{repo_id
} = $existing;
1888 if ($min_url ne $url) {
1889 unless ($no_write) {
1890 print STDERR
"Using higher level of URL: ",
1891 "$url => $min_url\n";
1893 my $old_path = $self->{path
};
1894 $self->{path
} = $url;
1895 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1896 if (length $old_path) {
1897 $self->{path
} .= "/$old_path";
1904 # verify that we aren't overwriting anything:
1906 command_oneline
('config', '--get',
1907 "svn-remote.$self->{repo_id}.url")
1909 if ($orig_url && ($orig_url ne $url)) {
1910 die "svn-remote.$self->{repo_id}.url already set: ",
1911 "$orig_url\nwanted to set to: $url\n";
1914 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1915 if (!$no_write && defined $xpath) {
1916 die "svn-remote.$xrepo_id.fetch already set to track ",
1917 "$xpath:", $self->refname, "\n";
1919 unless ($no_write) {
1920 command_noisy
('config',
1921 "svn-remote.$self->{repo_id}.url", $url);
1922 $self->{path
} =~ s{^/}{};
1923 $self->{path
} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
1924 command_noisy
('config', '--add',
1925 "svn-remote.$self->{repo_id}.fetch",
1926 "$self->{path}:".$self->refname);
1928 $self->{url
} = $url;
1931 sub find_by_url
{ # repos_root and, path are optional
1932 my ($class, $full_url, $repos_root, $path) = @_;
1934 return undef unless defined $full_url;
1935 remove_username
($full_url);
1936 remove_username
($repos_root) if defined $repos_root;
1937 my $remotes = read_all_remotes
();
1938 if (defined $full_url && defined $repos_root && !defined $path) {
1940 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1942 foreach my $repo_id (keys %$remotes) {
1943 my $u = $remotes->{$repo_id}->{url
} or next;
1944 remove_username
($u);
1945 next if defined $repos_root && $repos_root ne $u;
1947 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1948 foreach my $t (qw
/branches tags/) {
1949 foreach my $globspec (@
{$remotes->{$repo_id}->{$t}}) {
1950 resolve_local_globs
($u, $fetch, $globspec);
1954 my $rwr = rewrite_root
({repo_id
=> $repo_id});
1955 my $svm = $remotes->{$repo_id}->{svm
}
1956 if defined $remotes->{$repo_id}->{svm
};
1957 unless (defined $p) {
1963 remove_username
($z);
1964 } elsif (defined $svm) {
1965 $z = $svm->{source
};
1966 $prefix = $svm->{replace
};
1967 $prefix =~ s
#^\Q$u\E(?:/|$)##;
1970 $p =~ s
#^\Q$z\E(?:/|$)#$prefix# or next;
1972 foreach my $f (keys %$fetch) {
1974 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1981 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1982 my $self = _new
($class, $repo_id, $ref_id, $path);
1984 $self->init_remote_config($url, $no_write);
1991 foreach (command
(qw
/config -l/)) {
1992 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1993 \s
*(.*?
)\s
*:\s
*(.+?
)\s
*$!x
;
1994 my ($repo_id, $path, $ref) = ($1, $2, $3);
1995 if ($ref eq $ref_id) {
1996 $path = '' if ($path =~ m
#^\./?#);
1997 return ($repo_id, $path);
2000 (undef, undef, undef);
2004 my ($class, $ref_id, $repo_id, $path) = @_;
2005 if (defined $ref_id && !defined $repo_id && !defined $path) {
2006 ($repo_id, $path) = find_ref
($ref_id);
2007 if (!defined $repo_id) {
2008 die "Could not find a \"svn-remote.*.fetch\" key ",
2009 "in the repository configuration matching: ",
2013 my $self = _new
($class, $repo_id, $ref_id, $path);
2014 if (!defined $self->{path
} || !length $self->{path
}) {
2015 my $fetch = command_oneline
('config', '--get',
2016 "svn-remote.$repo_id.fetch",
2018 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
2019 "\":$ref_id\$\" in config\n";
2020 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
2022 $self->{url
} = command_oneline
('config', '--get',
2023 "svn-remote.$repo_id.url") or
2024 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
2030 my ($refname) = $_[0]->{ref_id
} ;
2032 # It cannot end with a slash /, we'll throw up on this because
2033 # SVN can't have directories with a slash in their name, either:
2034 if ($refname =~ m{/$}) {
2035 die "ref: '$refname' ends with a trailing slash, this is ",
2036 "not permitted by git nor Subversion\n";
2039 # It cannot have ASCII control character space, tilde ~, caret ^,
2040 # colon :, question-mark ?, asterisk *, space, or open bracket [
2043 # Additionally, % must be escaped because it is used for escaping
2044 # and we want our escaped refname to be reversible
2045 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2047 # no slash-separated component can begin with a dot .
2049 $refname =~ s{/\.}{/%2E}g;
2051 # It cannot have two consecutive dots .. anywhere
2053 $refname =~ s{\.\.}{%2E%2E}g;
2058 sub desanitize_refname
{
2060 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2066 return $self->{svm
}->{uuid
} if $self->svm;
2068 unless ($self->{svm
}) {
2069 die "SVM UUID not cached, and reading remotely failed\n";
2071 $self->{svm
}->{uuid
};
2076 return $self->{svm
} if $self->{svm
};
2078 # see if we have it in our config, first:
2080 my $section = "svn-remote.$self->{repo_id}";
2082 source
=> tmp_config
('--get', "$section.svm-source"),
2083 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
2084 replace
=> tmp_config
('--get', "$section.svm-replace"),
2087 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
2088 $self->{svm
} = $svm;
2094 my ($self, $ra) = @_;
2095 return $ra if $self->svm;
2097 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
2098 "(svm:source, svm:uuid) ",
2099 "from the following URLs:\n" );
2100 sub read_svm_props
{
2101 my ($self, $ra, $path, $r) = @_;
2102 my $props = ($ra->get_dir($path, $r))[2];
2103 my $src = $props->{'svm:source'};
2104 my $uuid = $props->{'svm:uuid'};
2105 return undef if (!$src || !$uuid);
2109 $uuid =~ m{^[0-9a-f\-]{30,}$}i
2110 or die "doesn't look right - svm:uuid is '$uuid'\n";
2112 # the '!' is used to mark the repos_root!/relative/path
2113 $src =~ s{/?!/?}{/};
2114 $src =~ s{/+$}{}; # no trailing slashes please
2115 # username is of no interest
2116 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
2118 my $replace = $ra->{url
};
2119 $replace .= "/$path" if length $path;
2121 my $section = "svn-remote.$self->{repo_id}";
2122 tmp_config
("$section.svm-source", $src);
2123 tmp_config
("$section.svm-replace", $replace);
2124 tmp_config
("$section.svm-uuid", $uuid);
2132 my $r = $ra->get_latest_revnum;
2133 my $path = $self->{path
};
2135 while (length $path) {
2136 unless ($tried{"$self->{url}/$path"}) {
2137 return $ra if $self->read_svm_props($ra, $path, $r);
2138 $tried{"$self->{url}/$path"} = 1;
2140 $path =~ s
#/?[^/]+$##;
2142 die "Path: '$path' should be ''\n" if $path ne '';
2143 return $ra if $self->read_svm_props($ra, $path, $r);
2144 $tried{"$self->{url}/$path"} = 1;
2146 if ($ra->{repos_root
} eq $self->{url
}) {
2147 die @err, (map { " $_\n" } keys %tried), "\n";
2150 # nope, make sure we're connected to the repository root:
2153 $path = $ra->{svn_path
};
2154 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
2155 while (length $path) {
2156 unless ($tried{"$ra->{url}/$path"}) {
2157 $ok = $self->read_svm_props($ra, $path, $r);
2159 $tried{"$ra->{url}/$path"} = 1;
2161 $path =~ s
#/?[^/]+$##;
2163 die "Path: '$path' should be ''\n" if $path ne '';
2164 $ok ||= $self->read_svm_props($ra, $path, $r);
2165 $tried{"$ra->{url}/$path"} = 1;
2167 die @err, (map { " $_\n" } keys %tried), "\n";
2169 Git
::SVN
::Ra
->new($self->{url
});
2174 return $self->{svnsync
} if $self->{svnsync
};
2176 if ($self->no_metadata) {
2177 die "Can't have both 'noMetadata' and ",
2178 "'useSvnsyncProps' options set!\n";
2180 if ($self->rewrite_root) {
2181 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2186 # see if we have it in our config, first:
2188 my $section = "svn-remote.$self->{repo_id}";
2190 my $url = tmp_config
('--get', "$section.svnsync-url");
2191 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2192 die "doesn't look right - svn:sync-from-url is '$url'\n";
2194 my $uuid = tmp_config
('--get', "$section.svnsync-uuid");
2195 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i
) or
2196 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2198 $svnsync = { url
=> $url, uuid
=> $uuid }
2200 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
2201 return $self->{svnsync
} = $svnsync;
2204 my $err = "useSvnsyncProps set, but failed to read " .
2205 "svnsync property: svn:sync-from-";
2206 my $rp = $self->ra->rev_proplist(0);
2208 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
2209 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2210 die "doesn't look right - svn:sync-from-url is '$url'\n";
2212 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
2213 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i
) or
2214 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2216 my $section = "svn-remote.$self->{repo_id}";
2217 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
2218 tmp_config
('--add', "$section.svnsync-url", $url);
2219 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
2222 # this allows us to memoize our SVN::Ra UUID locally and avoid a
2223 # remote lookup (useful for 'git svn log').
2226 unless ($self->{ra_uuid
}) {
2227 my $key = "svn-remote.$self->{repo_id}.uuid";
2228 my $uuid = eval { tmp_config
('--get', $key) };
2229 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
2230 $self->{ra_uuid
} = $uuid;
2232 die "ra_uuid called without URL\n" unless $self->{url
};
2233 $self->{ra_uuid
} = $self->ra->get_uuid;
2234 tmp_config
('--add', $key, $self->{ra_uuid
});
2240 sub _set_repos_root
{
2241 my ($self, $repos_root) = @_;
2242 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2243 $repos_root ||= $self->ra->{repos_root
};
2244 tmp_config
($k, $repos_root);
2250 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2251 eval { tmp_config
('--get', $k) } || $self->_set_repos_root;
2256 my $ra = Git
::SVN
::Ra
->new($self->{url
});
2257 $self->_set_repos_root($ra->{repos_root
});
2258 if ($self->use_svm_props && !$self->{svm
}) {
2259 if ($self->no_metadata) {
2260 die "Can't have both 'noMetadata' and ",
2261 "'useSvmProps' options set!\n";
2262 } elsif ($self->use_svnsync_props) {
2263 die "Can't have both 'useSvnsyncProps' and ",
2264 "'useSvmProps' options set!\n";
2266 $ra = $self->_set_svm_vars($ra);
2267 $self->{-want_revprops
} = 1;
2272 # prop_walk(PATH, REV, SUB)
2273 # -------------------------
2274 # Recursively traverse PATH at revision REV and invoke SUB for each
2275 # directory that contains a SVN property. SUB will be invoked as
2276 # follows: &SUB(gs, path, props); where `gs' is this instance of
2277 # Git::SVN, `path' the path to the directory where the properties
2278 # `props' were found. The `path' will be relative to point of checkout,
2279 # that is, if url://repo/trunk is the current Git branch, and that
2280 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
2281 # as `path' (note the trailing `/').
2283 my ($self, $path, $rev, $sub) = @_;
2286 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2289 # Strip the irrelevant part of the path.
2290 $p =~ s
#^/+\Q$self->{path}\E(/|$)#/#;
2291 # Ensure the path is terminated by a `/'.
2294 # The properties contain all the internal SVN stuff nobody
2295 # (usually) cares about.
2296 my $interesting_props = 0;
2297 foreach (keys %{$props}) {
2298 # If it doesn't start with `svn:', it must be a
2299 # user-defined property.
2300 ++$interesting_props and next if $_ !~ /^svn:/;
2301 # FIXME: Fragile, if SVN adds new public properties,
2302 # this needs to be updated.
2303 ++$interesting_props if /^svn
:(?
:ignore
|keywords
|executable
2304 |eol
-style
|mime
-type
2305 |externals
|needs
-lock)$/x
;
2307 &$sub($self, $p, $props) if $interesting_props;
2309 foreach (sort keys %$dirent) {
2310 next if $dirent->{$_}->{kind
} != $SVN::Node
::dir
;
2311 $self->prop_walk($self->{path
} . $p . $_, $rev, $sub);
2315 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
2316 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
2318 # returns the newest SVN revision number and newest commit SHA1
2319 sub last_rev_commit
{
2321 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
2322 return ($self->{last_rev
}, $self->{last_commit
});
2324 my $c = ::verify_ref
($self->refname.'^0');
2325 if ($c && !$self->use_svm_props && !$self->no_metadata) {
2326 my $rev = (::cmt_metadata
($c))[1];
2328 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
2332 my $map_path = $self->map_path;
2333 unless (-e
$map_path) {
2334 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
2335 return (undef, undef);
2337 my ($rev, $commit) = $self->rev_map_max(1);
2338 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $commit);
2339 return ($rev, $commit);
2342 sub get_fetch_range
{
2343 my ($self, $min, $max) = @_;
2344 $max ||= $self->ra->get_latest_revnum;
2345 $min ||= $self->rev_map_max;
2351 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2352 my $config = "$ENV{GIT_DIR}/svn/.metadata";
2353 if (! -f
$config && -f
$old_def_config) {
2354 rename $old_def_config, $config or
2355 die "Failed rename $old_def_config => $config: $!\n";
2357 my $old_config = $ENV{GIT_CONFIG
};
2358 $ENV{GIT_CONFIG
} = $config;
2361 unless (-f
$config) {
2363 open my $fh, '>', $config or
2364 die "Can't open $config: $!\n";
2365 print $fh "; This file is used internally by ",
2367 "Couldn't write to $config: $!\n";
2368 print $fh "; You should not have to edit it\n" or
2369 die "Couldn't write to $config: $!\n";
2370 close $fh or die "Couldn't close $config: $!\n";
2372 command
('config', @args);
2375 if (defined $old_config) {
2376 $ENV{GIT_CONFIG
} = $old_config;
2378 delete $ENV{GIT_CONFIG
};
2381 wantarray ?
@ret : $ret[0];
2385 my ($self, $sub) = @_;
2386 my $old_index = $ENV{GIT_INDEX_FILE
};
2387 $ENV{GIT_INDEX_FILE
} = $self->{index};
2390 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
2391 mkpath
([$dir]) unless -d
$dir;
2395 if (defined $old_index) {
2396 $ENV{GIT_INDEX_FILE
} = $old_index;
2398 delete $ENV{GIT_INDEX_FILE
};
2401 wantarray ?
@ret : $ret[0];
2404 sub assert_index_clean
{
2405 my ($self, $treeish) = @_;
2407 $self->tmp_index_do(sub {
2408 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
2409 my $x = command_oneline
('write-tree');
2410 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
2411 /^tree ($::sha1)/mo);
2414 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2415 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2416 command_noisy
('read-tree', $treeish);
2417 $x = command_oneline
('write-tree');
2419 ::fatal
"trees ($treeish) $y != $x\n",
2420 "Something is seriously wrong...";
2425 sub get_commit_parents
{
2426 my ($self, $log_entry) = @_;
2427 my (%seen, @ret, @tmp);
2428 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2429 if (my $ip = $self->{inject_parents
}) {
2430 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
2434 if (my $cur = ::verify_ref
($self->refname.'^0')) {
2437 if (my $ipd = $self->{inject_parents_dcommit
}) {
2438 if (my $commit = delete $ipd->{$log_entry->{revision
}}) {
2439 push @tmp, @
$commit;
2442 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
2443 while (my $p = shift @tmp) {
2447 # MAXPARENT is defined to 16 in commit-tree.c:
2451 die "r$log_entry->{revision}: No room for parents:\n\t",
2452 join("\n\t", @tmp), "\n";
2459 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
2460 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2461 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
2464 if ($rwr !~ m
#^[a-z\+]+://#) {
2465 die "$rwr is not a valid URL (key: $k)\n";
2468 $self->{-rewrite_root
} = $rwr;
2473 ($self->rewrite_root || $self->{url
}) .
2474 (length $self->{path
} ?
'/' . $self->{path
} : '');
2479 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
2483 sub set_commit_header_env
{
2484 my ($log_entry) = @_;
2486 foreach my $ned (qw
/NAME EMAIL DATE/) {
2487 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2488 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2492 $ENV{GIT_AUTHOR_NAME
} = $log_entry->{name
};
2493 $ENV{GIT_AUTHOR_EMAIL
} = $log_entry->{email
};
2494 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
2496 $ENV{GIT_COMMITTER_NAME
} = (defined $log_entry->{commit_name
})
2497 ?
$log_entry->{commit_name
}
2498 : $log_entry->{name
};
2499 $ENV{GIT_COMMITTER_EMAIL
} = (defined $log_entry->{commit_email
})
2500 ?
$log_entry->{commit_email
}
2501 : $log_entry->{email
};
2505 sub restore_commit_header_env
{
2507 foreach my $ned (qw
/NAME EMAIL DATE/) {
2508 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2509 my $k = "GIT_${ac}_${ned}";
2510 if (defined $env->{$k}) {
2511 $ENV{$k} = $env->{$k};
2520 command_noisy
('gc', '--auto');
2524 my ($self, $log_entry) = @_;
2525 my $lr = $self->last_rev;
2526 if (defined $lr && $lr >= $log_entry->{revision
}) {
2527 die "Last fetched revision of ", $self->refname,
2528 " was r$lr, but we are about to fetch: ",
2529 "r$log_entry->{revision}!\n";
2531 if (my $c = $self->rev_map_get($log_entry->{revision
})) {
2532 croak
"$log_entry->{revision} = $c already exists! ",
2533 "Why are we refetching it?\n";
2535 my $old_env = set_commit_header_env
($log_entry);
2536 my $tree = $log_entry->{tree
};
2537 if (!defined $tree) {
2538 $tree = $self->tmp_index_do(sub {
2539 command_oneline
('write-tree') });
2541 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2543 my @exec = ('git', 'commit-tree', $tree);
2544 foreach ($self->get_commit_parents($log_entry)) {
2545 push @exec, '-p', $_;
2547 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2551 # we always get UTF-8 from SVN, but we may want our commits in
2552 # a different encoding.
2553 if (my $enc = Git
::config
('i18n.commitencoding')) {
2555 Encode
::from_to
($log_entry->{log}, 'UTF-8', $enc);
2557 print $msg_fh $log_entry->{log} or croak
$!;
2558 restore_commit_header_env
($old_env);
2559 unless ($self->no_metadata) {
2560 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2563 $msg_fh->flush == 0 or croak
$!;
2564 close $msg_fh or croak
$!;
2565 chomp(my $commit = do { local $/; <$out_fh> });
2566 close $out_fh or croak
$!;
2569 if ($commit !~ /^$::sha1$/o) {
2570 die "Failed to commit, invalid sha1: $commit\n";
2573 $self->rev_map_set($log_entry->{revision
}, $commit, 1);
2575 $self->{last_rev
} = $log_entry->{revision
};
2576 $self->{last_commit
} = $commit;
2577 print "r$log_entry->{revision}" unless $::_q
> 1;
2578 if (defined $log_entry->{svm_revision
}) {
2579 print " (\@$log_entry->{svm_revision})" unless $::_q
> 1;
2580 $self->rev_map_set($log_entry->{svm_revision
}, $commit,
2581 0, $self->svm_uuid);
2583 print " = $commit ($self->{ref_id})\n" unless $::_q
> 1;
2584 if (--$_gc_nr == 0) {
2585 $_gc_nr = $_gc_period;
2592 my ($self, $paths, $r) = @_;
2593 return 1 if $self->{path
} eq '';
2594 if (my $path = $paths->{"/$self->{path}"}) {
2595 return ($path->{action
} eq 'D') ?
0 : 1;
2597 $self->{path_regex
} ||= qr/^\/\Q
$self->{path
}\E\
//;
2598 if (grep /$self->{path_regex}/, keys %$paths) {
2602 foreach (split m
#/#, $self->{path}) {
2604 next unless ($paths->{$c} &&
2605 ($paths->{$c}->{action
} =~ /^[AR]$/));
2606 if ($self->ra->check_path($self->{path
}, $r) ==
2614 sub find_parent_branch
{
2615 my ($self, $paths, $rev) = @_;
2616 return undef unless $self->follow_parent;
2617 unless (defined $paths) {
2618 my $err_handler = $SVN::Error
::handler
;
2619 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
2620 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1,
2621 sub { $paths = $_[0] });
2622 $SVN::Error
::handler
= $err_handler;
2624 return undef unless defined $paths;
2626 # look for a parent from another branch:
2627 my @b_path_components = split m
#/#, $self->{path};
2628 my @a_path_components;
2630 while (@b_path_components) {
2631 $i = $paths->{'/'.join('/', @b_path_components)};
2632 last if $i && defined $i->{copyfrom_path
};
2633 unshift(@a_path_components, pop(@b_path_components));
2635 return undef unless defined $i && defined $i->{copyfrom_path
};
2636 my $branch_from = $i->{copyfrom_path
};
2637 if (@a_path_components) {
2638 print STDERR
"branch_from: $branch_from => ";
2639 $branch_from .= '/'.join('/', @a_path_components);
2640 print STDERR
$branch_from, "\n";
2642 my $r = $i->{copyfrom_rev
};
2643 my $repos_root = $self->ra->{repos_root
};
2644 my $url = $self->ra->{url
};
2645 my $new_url = $url . $branch_from;
2646 print STDERR
"Found possible branch point: ",
2647 "$new_url => ", $self->full_url, ", $r\n"
2649 $branch_from =~ s
#^/##;
2650 my $gs = $self->other_gs($new_url, $url,
2651 $branch_from, $r, $self->{ref_id
});
2652 my ($r0, $parent) = $gs->find_rev_before($r, 1);
2655 if (!defined $r0 || !defined $parent) {
2656 ($base, $head) = parse_revision_argument
(0, $r);
2659 $gs->ra->get_log([$gs->{path
}], $r0 + 1, $r, 1,
2660 0, 1, sub { $base = $_[1] - 1 });
2663 if (defined $base && $base <= $r) {
2664 $gs->fetch($base, $r);
2666 ($r0, $parent) = $gs->find_rev_before($r, 1);
2668 if (defined $r0 && defined $parent) {
2669 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n"
2672 if ($self->ra->can_do_switch) {
2673 $self->assert_index_clean($parent);
2674 print STDERR
"Following parent with do_switch\n"
2676 # do_switch works with svn/trunk >= r22312, but that
2677 # is not included with SVN 1.4.3 (the latest version
2678 # at the moment), so we can't rely on it
2679 $self->{last_rev
} = $r0;
2680 $self->{last_commit
} = $parent;
2681 $ed = SVN
::Git
::Fetcher
->new($self, $gs->{path
});
2682 $gs->ra->gs_do_switch($r0, $rev, $gs,
2683 $self->full_url, $ed)
2684 or die "SVN connection failed somewhere...\n";
2685 } elsif ($self->ra->trees_match($new_url, $r0,
2686 $self->full_url, $rev)) {
2687 print STDERR
"Trees match:\n",
2689 " ${\$self->full_url}\@$rev\n",
2690 "Following parent with no changes\n"
2692 $self->tmp_index_do(sub {
2693 command_noisy
('read-tree', $parent);
2695 $self->{last_commit
} = $parent;
2697 print STDERR
"Following parent with do_update\n"
2699 $ed = SVN
::Git
::Fetcher
->new($self);
2700 $self->ra->gs_do_update($rev, $rev, $self, $ed)
2701 or die "SVN connection failed somewhere...\n";
2703 print STDERR
"Successfully followed parent\n" unless $::_q
> 1;
2704 return $self->make_log_entry($rev, [$parent], $ed);
2710 my ($self, $paths, $rev) = @_;
2712 my ($last_rev, @parents);
2713 if (my $lc = $self->last_commit) {
2714 # we can have a branch that was deleted, then re-added
2715 # under the same name but copied from another path, in
2716 # which case we'll have multiple parents (we don't
2717 # want to break the original ref, nor lose copypath info):
2718 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2719 push @
{$log_entry->{parents
}}, $lc;
2722 $ed = SVN
::Git
::Fetcher
->new($self);
2723 $last_rev = $self->{last_rev
};
2728 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2731 $ed = SVN
::Git
::Fetcher
->new($self);
2733 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
2734 die "SVN connection failed somewhere...\n";
2736 $self->make_log_entry($rev, \
@parents, $ed);
2740 my ($self, $r) = @_;
2741 my %empty_dirs = ();
2743 open my $fh, '<', "$self->{dir}/unhandled.log" or return;
2744 binmode $fh or croak
"binmode: $!";
2746 if (defined $r && /^r(\d+)$/) {
2748 } elsif (/^ \+empty_dir: (.+)$/) {
2749 $empty_dirs{$1} = 1;
2750 } elsif (/^ \-empty_dir: (.+)$/) {
2751 delete $empty_dirs{$1};
2755 foreach my $d (sort keys %empty_dirs) {
2756 $d = uri_decode
($d);
2759 warn "$d exists but is not a directory\n";
2761 print "creating empty directory: $d\n";
2768 my ($self, $ed) = @_;
2770 my $h = $ed->{empty
};
2771 foreach (sort keys %$h) {
2772 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2773 push @out, " $act: " . uri_encode
($_);
2774 warn "W: $act: $_\n";
2776 foreach my $t (qw
/dir_prop file_prop/) {
2777 $h = $ed->{$t} or next;
2778 foreach my $path (sort keys %$h) {
2779 my $ppath = $path eq '' ?
'.' : $path;
2780 foreach my $prop (sort keys %{$h->{$path}}) {
2781 next if $SKIP_PROP{$prop};
2782 my $v = $h->{$path}->{$prop};
2783 my $t_ppath_prop = "$t: " .
2784 uri_encode
($ppath) . ' ' .
2787 push @out, " +$t_ppath_prop " .
2790 push @out, " -$t_ppath_prop";
2795 foreach my $t (qw
/absent_file absent_directory/) {
2796 $h = $ed->{$t} or next;
2797 foreach my $parent (sort keys %$h) {
2798 foreach my $path (sort @
{$h->{$parent}}) {
2799 push @out, " $t: " .
2800 uri_encode
("$parent/$path");
2801 warn "W: $t: $parent/$path ",
2802 "Insufficient permissions?\n";
2809 # parse_svn_date(DATE)
2810 # --------------------
2811 # Given a date (in UTC) from Subversion, return a string in the format
2812 # "<TZ Offset> <local date/time>" that Git will use.
2814 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
2815 # is true we'll convert it to the local timezone instead.
2816 sub parse_svn_date
{
2817 my $date = shift || return '+0000 1970-01-01 00:00:00';
2818 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2819 (\d\d
)\
:(\d\d
)\
:(\d\d
)\
.\d
*Z
$/x
) or
2820 croak
"Unable to parse date: $date\n";
2821 my $parsed_date; # Set next.
2823 if ($Git::SVN
::_localtime
) {
2824 # Translate the Subversion datetime to an epoch time.
2825 # Begin by switching ourselves to $date's timezone, UTC.
2826 my $old_env_TZ = $ENV{TZ
};
2830 POSIX
::strftime
('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2832 # Determine our local timezone (including DST) at the
2833 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2834 # value of TZ, if any, at the time we were run.
2835 if (defined $Git::SVN
::Log
::TZ
) {
2836 $ENV{TZ
} = $Git::SVN
::Log
::TZ
;
2842 POSIX
::strftime
('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2844 # This converts $epoch_in_UTC into our local timezone.
2845 my ($sec, $min, $hour, $mday, $mon, $year,
2846 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2848 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2849 $our_TZ, $year + 1900, $mon + 1,
2850 $mday, $hour, $min, $sec);
2852 # Reset us to the timezone in effect when we entered
2854 if (defined $old_env_TZ) {
2855 $ENV{TZ
} = $old_env_TZ;
2860 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2863 return $parsed_date;
2867 my ($self, $new_url, $url,
2868 $branch_from, $r, $old_ref_id) = @_;
2869 my $gs = Git
::SVN
->find_by_url($new_url, $url, $branch_from);
2871 my $ref_id = $old_ref_id;
2872 $ref_id =~ s/\@\d+$//;
2874 # just grow a tail if we're not unique enough :x
2875 $ref_id .= '-' while find_ref
($ref_id);
2876 print STDERR
"Initializing parent: $ref_id\n" unless $::_q
> 1;
2877 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2878 if ($u =~ s
#^\Q$url\E(/|$)##) {
2881 $repo_id = $self->{repo_id
};
2883 $gs = Git
::SVN
->init($u, $p, $repo_id, $ref_id, 1);
2888 sub call_authors_prog
{
2889 my ($orig_author) = @_;
2890 $orig_author = command_oneline
('rev-parse', '--sq-quote', $orig_author);
2891 my $author = `$::_authors_prog $orig_author`;
2893 die "$::_authors_prog failed with exit code $?\n"
2895 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2896 my ($name, $email) = ($1, $2);
2897 $email = undef if length $2 == 0;
2898 return [$name, $email];
2900 die "Author: $orig_author: $::_authors_prog returned "
2901 . "invalid author format: $author\n";
2907 if (!defined $author || length $author == 0) {
2908 $author = '(no author)';
2910 if (!defined $::users
{$author}) {
2911 if (defined $::_authors_prog
) {
2912 $::users
{$author} = call_authors_prog
($author);
2913 } elsif (defined $::_authors
) {
2914 die "Author: $author not defined in $::_authors file\n";
2920 sub find_extra_svk_parents
{
2921 my ($self, $ed, $tickets, $parents) = @_;
2922 # aha! svk:merge property changed...
2923 my @tickets = split "\n", $tickets;
2925 for my $ticket ( @tickets ) {
2926 my ($uuid, $path, $rev) = split /:/, $ticket;
2927 if ( $uuid eq $self->ra_uuid ) {
2928 my $url = $self->rewrite_root || $self->{url
};
2929 my $repos_root = $url;
2930 my $branch_from = $path;
2931 $branch_from =~ s{^/}{};
2932 my $gs = $self->other_gs($repos_root."/".$branch_from,
2937 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
2938 # wahey! we found it, but it might be
2940 push @known_parents, $commit;
2944 for my $parent ( @known_parents ) {
2945 my @cmd = ('rev-list', $parent, map { "^$_" } @
$parents );
2946 my ($msg_fh, $ctx) = command_output_pipe
(@cmd);
2948 while ( <$msg_fh> ) {
2951 command_close_pipe
($msg_fh, $ctx);
2954 "Found merge parent (svk:merge ticket): $parent\n";
2955 push @
$parents, $parent;
2960 # note: this function should only be called if the various dirprops
2961 # have actually changed
2962 sub find_extra_svn_parents
{
2963 my ($self, $ed, $mergeinfo, $parents) = @_;
2964 # aha! svk:merge property changed...
2966 # We first search for merged tips which are not in our
2967 # history. Then, we figure out which git revisions are in
2968 # that tip, but not this revision. If all of those revisions
2969 # are now marked as merge, we can add the tip as a parent.
2970 my @merges = split "\n", $mergeinfo;
2972 my @merged_commit_ranges;
2973 my $url = $self->rewrite_root || $self->{url
};
2974 for my $merge ( @merges ) {
2975 my ($source, $revs) = split ":", $merge;
2978 my $gs = Git
::SVN
->find_by_url($url.$source, $url, $path);
2980 warn "Couldn't find revmap for $url$source\n";
2983 my @ranges = split ",", $revs;
2984 my ($tip, $tip_commit);
2986 for my $range ( @ranges ) {
2987 my ($bottom, $top) = split "-", $range;
2990 $gs->rev_map_get($bottom, $self->ra_uuid) ||
2991 $gs->rev_map_get($bottom+1, $self->ra_uuid);
2993 for (; !$top_commit && $top >= $bottom; --$top) {
2995 $gs->rev_map_get($top, $self->ra_uuid);
2998 unless ($top_commit and $bottom_commit) {
2999 warn "W:unknown path/rev in svn:mergeinfo "
3000 ."dirprop: $source:$range\n";
3004 push @merged_commit_ranges,
3005 "$bottom_commit..$top_commit";
3007 if ( !defined $tip or $top > $tip ) {
3009 $tip_commit = $top_commit;
3012 unless (!$tip_commit or
3013 grep { $_ eq $tip_commit } @
$parents ) {
3014 push @merge_tips, $tip_commit;
3016 push @merge_tips, undef;
3019 for my $merge_tip ( @merge_tips ) {
3020 my $spec = shift @merges;
3021 next unless $merge_tip;
3022 my @cmd = ('rev-list', "-1", $merge_tip,
3023 "--not", @
$parents );
3024 my ($msg_fh, $ctx) = command_output_pipe
(@cmd);
3026 while ( <$msg_fh> ) {
3029 command_close_pipe
($msg_fh, $ctx);
3031 push @cmd, @merged_commit_ranges;
3032 my ($msg_fh, $ctx) = command_output_pipe
(@cmd);
3034 while ( <$msg_fh> ) {
3037 command_close_pipe
($msg_fh, $ctx);
3039 warn "W:svn cherry-pick ignored ($spec)\n";
3042 "Found merge parent (svn:mergeinfo prop): ",
3044 push @
$parents, $merge_tip;
3050 sub make_log_entry
{
3051 my ($self, $rev, $parents, $ed) = @_;
3052 my $untracked = $self->get_untracked($ed);
3054 my @parents = @
$parents;
3055 my $ps = $ed->{path_strip
} || "";
3056 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop
}} ) {
3057 my $props = $ed->{dir_prop
}{$path};
3058 if ( $props->{"svk:merge"} ) {
3059 $self->find_extra_svk_parents
3060 ($ed, $props->{"svk:merge"}, \
@parents);
3062 if ( $props->{"svn:mergeinfo"} ) {
3063 $self->find_extra_svn_parents
3065 $props->{"svn:mergeinfo"},
3070 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
3071 print $un "r$rev\n" or croak
$!;
3072 print $un $_, "\n" foreach @
$untracked;
3073 my %log_entry = ( parents
=> \
@parents, revision
=> $rev,
3077 my $logged = delete $self->{logged_rev_props
};
3078 if (!$logged || $self->{-want_revprops
}) {
3079 my $rp = $self->ra->rev_proplist($rev);
3080 foreach (sort keys %$rp) {
3082 if (/^svn:(author|date|log)$/) {
3083 $log_entry{$1} = $v;
3084 } elsif ($_ eq 'svm:headrev') {
3087 print $un " rev_prop: ", uri_encode
($_), ' ',
3088 uri_encode
($v), "\n";
3092 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
3094 close $un or croak
$!;
3096 $log_entry{date
} = parse_svn_date
($log_entry{date
});
3097 $log_entry{log} .= "\n";
3098 my $author = $log_entry{author
} = check_author
($log_entry{author
});
3099 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
3102 my ($commit_name, $commit_email) = ($name, $email);
3103 if ($_use_log_author) {
3105 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3107 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3110 if (!defined $name_field) {
3111 if (!defined $email) {
3114 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
3115 ($name, $email) = ($1, $2);
3116 } elsif ($name_field =~ /(.*)@/) {
3117 ($name, $email) = ($1, $name_field);
3119 ($name, $email) = ($name_field, $name_field);
3122 if (defined $headrev && $self->use_svm_props) {
3123 if ($self->rewrite_root) {
3124 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3127 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$}i
;
3128 # we don't want "SVM: initializing mirror for junk" ...
3129 return undef if $r == 0;
3130 my $svm = $self->svm;
3131 if ($uuid ne $svm->{uuid
}) {
3132 die "UUID mismatch on SVM path:\n",
3133 "expected: $svm->{uuid}\n",
3136 my $full_url = $self->full_url;
3137 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3138 die "Failed to replace '$svm->{replace}' with ",
3139 "'$svm->{source}' in $full_url\n";
3140 # throw away username for storing in records
3141 remove_username
($full_url);
3142 $log_entry{metadata
} = "$full_url\@$r $uuid";
3143 $log_entry{svm_revision
} = $r;
3144 $email ||= "$author\@$uuid";
3145 $commit_email ||= "$author\@$uuid";
3146 } elsif ($self->use_svnsync_props) {
3147 my $full_url = $self->svnsync->{url
};
3148 $full_url .= "/$self->{path}" if length $self->{path
};
3149 remove_username
($full_url);
3150 my $uuid = $self->svnsync->{uuid
};
3151 $log_entry{metadata
} = "$full_url\@$rev $uuid";
3152 $email ||= "$author\@$uuid";
3153 $commit_email ||= "$author\@$uuid";
3155 my $url = $self->metadata_url;
3156 remove_username
($url);
3157 $log_entry{metadata
} = "$url\@$rev " .
3158 $self->ra->get_uuid;
3159 $email ||= "$author\@" . $self->ra->get_uuid;
3160 $commit_email ||= "$author\@" . $self->ra->get_uuid;
3162 $log_entry{name
} = $name;
3163 $log_entry{email
} = $email;
3164 $log_entry{commit_name
} = $commit_name;
3165 $log_entry{commit_email
} = $commit_email;
3170 my ($self, $min_rev, $max_rev, @parents) = @_;
3171 my ($last_rev, $last_commit) = $self->last_rev_commit;
3172 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
3173 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
3177 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
3178 $self->{inject_parents
} = { $rev => $tree };
3179 $self->fetch(undef, undef);
3183 my ($self, $tree) = (shift, shift);
3184 my $log_entry = ::get_commit_entry
($tree);
3185 unless ($self->{last_rev
}) {
3186 ::fatal
("Must have an existing revision to commit");
3188 my %ed_opts = ( r
=> $self->{last_rev
},
3189 log => $log_entry->{log},
3191 tree_a
=> $self->{last_commit
},
3194 $self->set_tree_cb($log_entry, $tree, @_) },
3195 svn_path
=> $self->{path
} );
3196 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
3197 print "No changes\nr$self->{last_rev} = $tree\n";
3201 sub rebuild_from_rev_db
{
3202 my ($self, $path) = @_;
3204 open my $fh, '<', $path or croak
"open: $!";
3205 binmode $fh or croak
"binmode: $!";
3207 length($_) == 41 or croak
"inconsistent size in ($_) != 41";
3210 next if $_ eq ('0' x
40);
3211 $self->rev_map_set($r, $_);
3214 close $fh or croak
"close: $!";
3215 unlink $path or croak
"unlink: $!";
3220 my $map_path = $self->map_path;
3221 my $partial = (-e
$map_path && ! -z
$map_path);
3222 return unless ::verify_ref
($self->refname.'^0');
3223 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
3224 my $rev_db = $self->rev_db_path;
3225 $self->rebuild_from_rev_db($rev_db);
3226 if ($self->use_svm_props) {
3227 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3228 $self->rebuild_from_rev_db($svm_rev_db);
3230 $self->unlink_rev_db_symlink;
3233 print "Rebuilding $map_path ...\n" if (!$partial);
3234 my ($base_rev, $head) = ($partial ?
$self->rev_map_max_norebuild(1) :
3237 command_output_pipe
(qw
/rev-list --pretty=raw --no-color --reverse/,
3238 ($head ?
"$head.." : "") . $self->refname,
3240 my $metadata_url = $self->metadata_url;
3241 remove_username
($metadata_url);
3242 my $svn_uuid = $self->ra_uuid;
3245 if ( m{^commit ($::sha1)$} ) {
3249 next unless s{^\s*(git-svn-id:)}{$1};
3250 my ($url, $rev, $uuid) = ::extract_metadata
($_);
3251 remove_username
($url);
3253 # ignore merges (from set-tree)
3254 next if (!defined $rev || !$uuid);
3256 # if we merged or otherwise started elsewhere, this is
3257 # how we break out of it
3258 if (($uuid ne $svn_uuid) ||
3259 ($metadata_url && $url && ($url ne $metadata_url))) {
3262 if ($partial && $head) {
3263 print "Partial-rebuilding $map_path ...\n";
3264 print "Currently at $base_rev = $head\n";
3268 $self->rev_map_set($rev, $c);
3269 print "r$rev = $c\n";
3271 command_close_pipe
($log, $ctx);
3272 print "Done rebuilding $map_path\n" if (!$partial || !$head);
3273 my $rev_db_path = $self->rev_db_path;
3274 if (-f
$self->rev_db_path) {
3275 unlink $self->rev_db_path or croak
"unlink: $!";
3277 $self->unlink_rev_db_symlink;
3281 # Tie::File seems to be prone to offset errors if revisions get sparse,
3282 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3283 # one of my favorite modules is out :< Next up would be one of the DBM
3284 # modules, but I'm not sure which is most portable...
3286 # This is the replacement for the rev_db format, which was too big
3287 # and inefficient for large repositories with a lot of sparse history
3290 # The format is this:
3291 # - 24 bytes for every record,
3292 # * 4 bytes for the integer representing an SVN revision number
3293 # * 20 bytes representing the sha1 of a git commit
3294 # - No empty padding records like the old format
3295 # (except the last record, which can be overwritten)
3296 # - new records are written append-only since SVN revision numbers
3297 # increase monotonically
3298 # - lookups on SVN revision number are done via a binary search
3299 # - Piping the file to xxd -c24 is a good way of dumping it for
3300 # viewing or editing (piped back through xxd -r), should the need
3302 # - The last record can be padding revision with an all-zero sha1
3303 # This is used to optimize fetch performance when using multiple
3304 # "fetch" directives in .git/config
3306 # These files are disposable unless noMetadata or useSvmProps is set
3309 my ($fh, $rev, $commit) = @_;
3311 binmode $fh or croak
"binmode: $!";
3312 my $size = (stat($fh))[7];
3313 ($size % 24) == 0 or croak
"inconsistent size: $size";
3317 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
3318 my $read = sysread($fh, my $buf, 24) or croak
"read: $!";
3319 $read == 24 or croak
"read only $read bytes (!= 24)";
3320 my ($last_rev, $last_commit) = unpack(rev_map_fmt
, $buf);
3321 if ($last_commit eq ('0' x40
)) {
3323 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
3324 $read = sysread($fh, $buf, 24) or
3327 croak
"read only $read bytes (!= 24)";
3328 ($last_rev, $last_commit) =
3329 unpack(rev_map_fmt
, $buf);
3330 if ($last_commit eq ('0' x40
)) {
3331 croak
"inconsistent .rev_map\n";
3334 if ($last_rev >= $rev) {
3335 croak
"last_rev is higher!: $last_rev >= $rev";
3340 sysseek($fh, $wr_offset, SEEK_END
) or croak
"seek: $!";
3341 syswrite($fh, pack(rev_map_fmt
, $rev, $commit), 24) == 24 or
3345 sub _rev_map_reset
{
3346 my ($fh, $rev, $commit) = @_;
3347 my $c = _rev_map_get
($fh, $rev);
3348 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3349 my $offset = sysseek($fh, 0, SEEK_CUR
) or croak
"seek: $!";
3350 truncate $fh, $offset or croak
"truncate: $!";
3356 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3357 mkpath
([$dir]) unless -d
$dir;
3358 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3359 close $fh or die "Couldn't close (create) $path: $!\n";
3364 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3365 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
3366 my $db = $self->map_path($uuid);
3367 my $db_lock = "$db.lock";
3371 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
3372 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
3376 $LOCKFILES{$db_lock} = 1;
3378 # both of these options make our .rev_db file very, very important
3379 # and we can't afford to lose it because rebuild() won't work
3380 if ($self->use_svm_props || $self->no_metadata) {
3382 copy
($db, $db_lock) or die "rev_map_set(@_): ",
3384 "$db => $db_lock ($!)\n";
3386 rename $db, $db_lock or die "rev_map_set(@_): ",
3387 "Failed to rename: ",
3388 "$db => $db_lock ($!)\n";
3391 sysopen(my $fh, $db_lock, O_RDWR
| O_CREAT
)
3392 or croak
"Couldn't open $db_lock: $!\n";
3393 $update_ref eq 'reset' ? _rev_map_reset
($fh, $rev, $commit) :
3394 _rev_map_set
($fh, $rev, $commit);
3396 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3397 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3399 close $fh or croak
$!;
3403 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3404 command_noisy
('update-ref', '-m', "r$rev$note",
3405 $self->refname, $commit);
3407 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
3408 "$db_lock => $db ($!)\n";
3409 delete $LOCKFILES{$db_lock};
3411 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
3412 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
3413 kill $sig, $$ if defined $sig;
3417 # If want_commit, this will return an array of (rev, commit) where
3418 # commit _must_ be a valid commit in the archive.
3419 # Otherwise, it'll return the max revision (whether or not the
3420 # commit is valid or just a 0x40 placeholder).
3422 my ($self, $want_commit) = @_;
3424 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3425 $want_commit ?
($r, $c) : $r;
3428 sub rev_map_max_norebuild
{
3429 my ($self, $want_commit) = @_;
3430 my $map_path = $self->map_path;
3431 stat $map_path or return $want_commit ?
(0, undef) : 0;
3432 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
3433 binmode $fh or croak
"binmode: $!";
3434 my $size = (stat($fh))[7];
3435 ($size % 24) == 0 or croak
"inconsistent size: $size";
3438 close $fh or croak
"close: $!";
3439 return $want_commit ?
(0, undef) : 0;
3442 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
3443 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
3444 my ($r, $c) = unpack(rev_map_fmt
, $buf);
3445 if ($want_commit && $c eq ('0' x40
)) {
3447 return $want_commit ?
(0, undef) : 0;
3449 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
3450 sysread($fh, $buf, 24) == 24 or croak
"read: $!";
3451 ($r, $c) = unpack(rev_map_fmt
, $buf);
3452 if ($c eq ('0'x40
)) {
3453 croak
"Penultimate record is all-zeroes in $map_path";
3456 close $fh or croak
"close: $!";
3457 $want_commit ?
($r, $c) : $r;
3461 my ($self, $rev, $uuid) = @_;
3462 my $map_path = $self->map_path($uuid);
3463 return undef unless -e
$map_path;
3465 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
3466 my $c = _rev_map_get
($fh, $rev);
3467 close($fh) or croak
"close: $!";
3472 my ($fh, $rev) = @_;
3474 binmode $fh or croak
"binmode: $!";
3475 my $size = (stat($fh))[7];
3476 ($size % 24) == 0 or croak
"inconsistent size: $size";
3482 my ($l, $u) = (0, $size - 24);
3486 my $i = int(($l/24 + $u/24) / 2) * 24;
3487 sysseek($fh, $i, SEEK_SET
) or croak
"seek: $!";
3488 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
3489 my ($r, $c) = unpack(rev_map_fmt
, $buf);
3493 } elsif ($r > $rev) {
3495 } else { # $r == $rev
3496 return $c eq ('0' x
40) ?
undef : $c;
3502 # Finds the first svn revision that exists on (if $eq_ok is true) or
3503 # before $rev for the current branch. It will not search any lower
3504 # than $min_rev. Returns the git commit hash and svn revision number
3505 # if found, else (undef, undef).
3506 sub find_rev_before
{
3507 my ($self, $rev, $eq_ok, $min_rev) = @_;
3508 --$rev unless $eq_ok;
3510 my $max_rev = $self->rev_map_max;
3511 $rev = $max_rev if ($rev > $max_rev);
3512 while ($rev >= $min_rev) {
3513 if (my $c = $self->rev_map_get($rev)) {
3518 return (undef, undef);
3521 # Finds the first svn revision that exists on (if $eq_ok is true) or
3522 # after $rev for the current branch. It will not search any higher
3523 # than $max_rev. Returns the git commit hash and svn revision number
3524 # if found, else (undef, undef).
3525 sub find_rev_after
{
3526 my ($self, $rev, $eq_ok, $max_rev) = @_;
3527 ++$rev unless $eq_ok;
3528 $max_rev ||= $self->rev_map_max;
3529 while ($rev <= $max_rev) {
3530 if (my $c = $self->rev_map_get($rev)) {
3535 return (undef, undef);
3539 my ($class, $repo_id, $ref_id, $path) = @_;
3540 unless (defined $repo_id && length $repo_id) {
3541 $repo_id = $Git::SVN
::default_repo_id
;
3543 unless (defined $ref_id && length $ref_id) {
3544 $_prefix = '' unless defined($_prefix);
3546 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
3549 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3551 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3552 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3553 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3554 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3555 if (-d
$old_dir && ! -d
$dir) {
3560 $_[3] = $path = '' unless (defined $path);
3563 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
3564 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
3565 map_root
=> "$dir/.rev_map", repo_id
=> $repo_id }, $class;
3568 # for read-only access of old .rev_db formats
3569 sub unlink_rev_db_symlink
{
3571 my $link = $self->rev_db_path;
3572 $link =~ s/\.[\w-]+$// or croak
"missing UUID at the end of $link";
3574 unlink $link or croak
"unlink: $link failed!";
3579 my ($self, $uuid) = @_;
3580 my $db_path = $self->map_path($uuid);
3581 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3582 or croak
"map_path: $db_path does not contain '/.rev_map.' !";
3586 # the new replacement for .rev_db
3588 my ($self, $uuid) = @_;
3589 $uuid ||= $self->ra_uuid;
3590 "$self->{map_root}.$uuid";
3595 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3601 $f =~ s
#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
3605 sub remove_username
{
3606 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3609 package Git
::SVN
::Prompt
;
3613 use vars qw
/$_no_auth_cache $_username/;
3616 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3617 $may_save = undef if $_no_auth_cache;
3618 $default_username = $_username if defined $_username;
3619 if (defined $default_username && length $default_username) {
3620 if (defined $realm && length $realm) {
3621 print STDERR
"Authentication realm: $realm\n";
3624 $cred->username($default_username);
3626 username
($cred, $realm, $may_save, $pool);
3628 $cred->password(_read_password
("Password for '" .
3629 $cred->username . "': ", $realm));
3630 $cred->may_save($may_save);
3631 $SVN::_Core
::SVN_NO_ERROR
;
3634 sub ssl_server_trust
{
3635 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3636 $may_save = undef if $_no_auth_cache;
3637 print STDERR
"Error validating server certificate for '$realm':\n";
3640 # All variables SVN::Auth::SSL::* are used only once,
3641 # so we're shutting up Perl warnings about this.
3642 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
3643 print STDERR
" - The certificate is not issued ",
3644 "by a trusted authority. Use the\n",
3645 " fingerprint to validate ",
3646 "the certificate manually!\n";
3648 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
3649 print STDERR
" - The certificate hostname ",
3650 "does not match.\n";
3652 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
3653 print STDERR
" - The certificate is not yet valid.\n";
3655 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
3656 print STDERR
" - The certificate has expired.\n";
3658 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
3659 print STDERR
" - The certificate has ",
3660 "an unknown error.\n";
3662 } # no warnings 'once'
3664 "Certificate information:\n".
3665 " - Hostname: %s\n".
3666 " - Valid: from %s until %s\n".
3668 " - Fingerprint: %s\n",
3669 map $cert_info->$_, qw(hostname valid_from valid_until
3670 issuer_dname fingerprint);
3673 print STDERR
$may_save ?
3674 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3675 "(R)eject or accept (t)emporarily? ";
3677 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
3678 if ($choice =~ /^t$/i) {
3679 $cred->may_save(undef);
3680 } elsif ($choice =~ /^r$/i) {
3682 } elsif ($may_save && $choice =~ /^p$/i) {
3683 $cred->may_save($may_save);
3687 $cred->accepted_failures($failures);
3688 $SVN::_Core
::SVN_NO_ERROR
;
3691 sub ssl_client_cert
{
3692 my ($cred, $realm, $may_save, $pool) = @_;
3693 $may_save = undef if $_no_auth_cache;
3694 print STDERR
"Client certificate filename: ";
3696 chomp(my $filename = <STDIN
>);
3697 $cred->cert_file($filename);
3698 $cred->may_save($may_save);
3699 $SVN::_Core
::SVN_NO_ERROR
;
3702 sub ssl_client_cert_pw
{
3703 my ($cred, $realm, $may_save, $pool) = @_;
3704 $may_save = undef if $_no_auth_cache;
3705 $cred->password(_read_password
("Password: ", $realm));
3706 $cred->may_save($may_save);
3707 $SVN::_Core
::SVN_NO_ERROR
;
3711 my ($cred, $realm, $may_save, $pool) = @_;
3712 $may_save = undef if $_no_auth_cache;
3713 if (defined $realm && length $realm) {
3714 print STDERR
"Authentication realm: $realm\n";
3717 if (defined $_username) {
3718 $username = $_username;
3720 print STDERR
"Username: ";
3722 chomp($username = <STDIN
>);
3724 $cred->username($username);
3725 $cred->may_save($may_save);
3726 $SVN::_Core
::SVN_NO_ERROR
;
3729 sub _read_password
{
3730 my ($prompt, $realm) = @_;
3731 print STDERR
$prompt;
3733 require Term
::ReadKey
;
3734 Term
::ReadKey
::ReadMode
('noecho');
3736 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
3737 last if $key =~ /[\012\015]/; # \n\r
3740 Term
::ReadKey
::ReadMode
('restore');
3746 package SVN
::Git
::Fetcher
;
3751 use File
::Temp qw
/tempfile/;
3753 use vars qw
/$_ignore_regex/;
3755 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
3757 my ($class, $git_svn, $switch_path) = @_;
3758 my $self = SVN
::Delta
::Editor
->new;
3759 bless $self, $class;
3760 if (exists $git_svn->{last_commit
}) {
3761 $self->{c
} = $git_svn->{last_commit
};
3762 $self->{empty_symlinks
} =
3763 _mark_empty_symlinks
($git_svn, $switch_path);
3765 $self->{ignore_regex
} = eval { command_oneline
('config', '--get',
3766 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
3767 $self->{empty
} = {};
3768 $self->{dir_prop
} = {};
3769 $self->{file_prop
} = {};
3770 $self->{absent_dir
} = {};
3771 $self->{absent_file
} = {};
3772 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
3776 # this uses the Ra object, so it must be called before do_{switch,update},
3777 # not inside them (when the Git::SVN::Fetcher object is passed) to
3778 # do_{switch,update}
3779 sub _mark_empty_symlinks
{
3780 my ($git_svn, $switch_path) = @_;
3781 my $bool = Git
::config_bool
('svn.brokenSymlinkWorkaround');
3782 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
3785 my ($rev, $cmt) = $git_svn->last_rev_commit;
3786 return {} unless ($rev && $cmt);
3788 # allow the warning to be printed for each revision we fetch to
3789 # ensure the user sees it. The user can also disable the workaround
3790 # on the repository even while git svn is running and the next
3791 # revision fetched will skip this expensive function.
3792 my $printed_warning;
3793 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3794 my ($ls, $ctx) = command_output_pipe
(qw
/ls-tree -r -z/, $cmt);
3796 my $pfx = defined($switch_path) ?
$switch_path : $git_svn->{path
};
3797 $pfx .= '/' if length($pfx);
3800 s/\A100644 blob $empty_blob\t//o or next;
3801 unless ($printed_warning) {
3802 print STDERR
"Scanning for empty symlinks, ",
3803 "this may take a while if you have ",
3804 "many empty files\n",
3805 "You may disable this with `",
3806 "git config svn.brokenSymlinkWorkaround ",
3808 "This may be done in a different ",
3809 "terminal without restarting ",
3811 $printed_warning = 1;
3814 my (undef, $props) =
3815 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3816 if ($props->{'svn:special'}) {
3820 command_close_pipe
($ls, $ctx);
3824 # returns true if a given path is inside a ".git" directory
3826 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3829 # return value: 0 -- don't ignore, 1 -- ignore
3830 sub is_path_ignored
{
3831 my ($self, $path) = @_;
3832 return 1 if in_dot_git
($path);
3833 return 1 if defined($self->{ignore_regex
}) &&
3834 $path =~ m!$self->{ignore_regex}!;
3835 return 0 unless defined($_ignore_regex);
3836 return 1 if $path =~ m!$_ignore_regex!o;
3840 sub set_path_strip
{
3841 my ($self, $path) = @_;
3842 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
3849 sub open_directory
{
3850 my ($self, $path, $pb, $rev) = @_;
3855 my ($self, $path) = @_;
3856 if ($self->{path_strip
}) {
3857 $path =~ s!$self->{path_strip}!! or
3858 die "Failed to strip path '$path' ($self->{path_strip})\n";
3864 my ($self, $path, $rev, $pb) = @_;
3865 return undef if $self->is_path_ignored($path);
3867 my $gpath = $self->git_path($path);
3868 return undef if ($gpath eq '');
3870 # remove entire directories.
3871 my ($tree) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3872 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3874 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3880 my $rmpath = "$gpath/$_";
3881 $self->{gii
}->remove($rmpath);
3882 print "\tD\t$rmpath\n" unless $::_q
;
3884 print "\tD\t$gpath/\n" unless $::_q
;
3885 command_close_pipe
($ls, $ctx);
3886 $self->{empty
}->{$path} = 0
3888 $self->{gii
}->remove($gpath);
3889 print "\tD\t$gpath\n" unless $::_q
;
3895 my ($self, $path, $pb, $rev) = @_;
3898 goto out
if $self->is_path_ignored($path);
3900 my $gpath = $self->git_path($path);
3901 ($mode, $blob) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3902 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
3903 unless (defined $mode && defined $blob) {
3904 die "$path was not found in commit $self->{c} (r$rev)\n";
3906 if ($mode eq '100644' && $self->{empty_symlinks
}->{$path}) {
3910 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
3911 pool
=> SVN
::Pool
->new, action
=> 'M' };
3915 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3918 if (!$self->is_path_ignored($path)) {
3919 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3920 delete $self->{empty
}->{$dir};
3923 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode,
3924 pool
=> SVN
::Pool
->new, action
=> 'A' };
3928 my ($self, $path, $cp_path, $cp_rev) = @_;
3929 goto out
if $self->is_path_ignored($path);
3930 my $gpath = $self->git_path($path);
3932 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3938 $self->{gii
}->remove($_);
3939 print "\tD\t$_\n" unless $::_q
;
3941 command_close_pipe
($ls, $ctx);
3942 $self->{empty
}->{$path} = 0;
3944 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3945 delete $self->{empty
}->{$dir};
3946 $self->{empty
}->{$path} = 1;
3951 sub change_dir_prop
{
3952 my ($self, $db, $prop, $value) = @_;
3953 return undef if $self->is_path_ignored($db->{path
});
3954 $self->{dir_prop
}->{$db->{path
}} ||= {};
3955 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
3959 sub absent_directory
{
3960 my ($self, $path, $pb) = @_;
3961 return undef if $self->is_path_ignored($path);
3962 $self->{absent_dir
}->{$pb->{path
}} ||= [];
3963 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
3968 my ($self, $path, $pb) = @_;
3969 return undef if $self->is_path_ignored($path);
3970 $self->{absent_file
}->{$pb->{path
}} ||= [];
3971 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
3975 sub change_file_prop
{
3976 my ($self, $fb, $prop, $value) = @_;
3977 return undef if $self->is_path_ignored($fb->{path
});
3978 if ($prop eq 'svn:executable') {
3979 if ($fb->{mode_b
} != 120000) {
3980 $fb->{mode_b
} = defined $value ?
100755 : 100644;
3982 } elsif ($prop eq 'svn:special') {
3983 $fb->{mode_b
} = defined $value ?
120000 : 100644;
3985 $self->{file_prop
}->{$fb->{path
}} ||= {};
3986 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
3991 sub apply_textdelta
{
3992 my ($self, $fb, $exp) = @_;
3993 return undef if $self->is_path_ignored($fb->{path
});
3994 my $fh = $::_repository
->temp_acquire('svn_delta');
3995 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3996 # (but $base does not,) so dup() it for reading in close_file
3997 open my $dup, '<&', $fh or croak
$!;
3998 my $base = $::_repository
->temp_acquire('git_blob');
4001 my ($base_is_link, $size);
4003 if ($fb->{mode_a
} eq '120000' &&
4004 ! $self->{empty_symlinks
}->{$fb->{path
}}) {
4005 print $base 'link ' or die "print $!\n";
4009 $size = $::_repository
->cat_blob($fb->{blob
}, $base);
4010 die "Failed to read object $fb->{blob}" if ($size < 0);
4013 seek $base, 0, 0 or croak
$!;
4014 my $got = ::md5sum
($base);
4016 my $err = "Checksum mismatch: ".
4017 "$fb->{path} $fb->{blob}\n" .
4018 "expected: $exp\n" .
4020 if ($base_is_link) {
4022 "Retrying... (possibly ",
4023 "a bad symlink from SVN)\n";
4024 $::_repository
->temp_reset($base);
4032 seek $base, 0, 0 or croak
$!;
4034 $fb->{base
} = $base;
4035 [ SVN
::TxDelta
::apply
($base, $dup, undef, $fb->{path
}, $fb->{pool
}) ];
4039 my ($self, $fb, $exp) = @_;
4040 return undef if $self->is_path_ignored($fb->{path
});
4043 my $path = $self->git_path($fb->{path
});
4044 if (my $fh = $fb->{fh
}) {
4046 seek($fh, 0, 0) or croak
$!;
4047 my $got = ::md5sum
($fh);
4049 die "Checksum mismatch: $path\n",
4050 "expected: $exp\n got: $got\n";
4053 if ($fb->{mode_b
} == 120000) {
4054 sysseek($fh, 0, 0) or croak
$!;
4055 my $rd = sysread($fh, my $buf, 5);
4058 croak
"sysread: $!\n";
4059 } elsif ($rd == 0) {
4060 warn "$path has mode 120000",
4061 " but it points to nothing\n",
4062 "converting to an empty file with mode",
4064 $fb->{mode_b
} = '100644';
4065 } elsif ($buf ne 'link ') {
4066 warn "$path has mode 120000",
4067 " but is not a link\n";
4069 my $tmp_fh = $::_repository
->temp_acquire(
4072 while ($res = sysread($fh, my $str, 1024)) {
4073 my $out = syswrite($tmp_fh, $str, $res);
4074 defined($out) && $out == $res
4076 Git
::temp_path
($tmp_fh),
4079 defined $res or croak
$!;
4081 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4082 Git
::temp_release
($tmp_fh, 1);
4086 $hash = $::_repository
->hash_and_insert_object(
4087 Git
::temp_path
($fh));
4088 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
4090 Git
::temp_release
($fb->{base
}, 1);
4091 Git
::temp_release
($fh, 1);
4093 $hash = $fb->{blob
} or die "no blob information\n";
4096 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
4097 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
4103 $self->{nr
} = $self->{gii
}->{nr
};
4104 delete $self->{gii
};
4105 $self->SUPER::abort_edit
(@_);
4110 $self->{git_commit_ok
} = 1;
4111 $self->{nr
} = $self->{gii
}->{nr
};
4112 delete $self->{gii
};
4113 $self->SUPER::close_edit
(@_);
4116 package SVN
::Git
::Editor
;
4117 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
4124 my ($class, $opts) = @_;
4125 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
4126 die "$_ required!\n" unless (defined $opts->{$_});
4129 my $pool = SVN
::Pool
->new;
4130 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
4131 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
4134 # $opts->{ra} functions should not be used after this:
4135 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
4136 $opts->{editor_cb
}, $pool);
4137 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
4138 bless $self, $class;
4139 foreach (qw
/svn_path r tree_a tree_b/) {
4140 $self->{$_} = $opts->{$_};
4142 $self->{url
} = $opts->{ra
}->{url
};
4143 $self->{mods
} = $mods;
4144 $self->{types
} = $types;
4145 $self->{pool
} = $pool;
4146 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
4148 $self->{path_prefix
} = length $self->{svn_path
} ?
4149 "$self->{svn_path}/" : '';
4150 $self->{config
} = $opts->{config
};
4155 my ($tree_a, $tree_b) = @_;
4156 my @diff_tree = qw(diff-tree -z -r);
4157 if ($_cp_similarity) {
4158 push @diff_tree, "-C$_cp_similarity";
4160 push @diff_tree, '-C';
4162 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4163 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
4164 push @diff_tree, $tree_a, $tree_b;
4165 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
4169 while (<$diff_fh>) {
4170 chomp $_; # this gets rid of the trailing "\0"
4171 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
4172 ($::sha1
)\s
($::sha1
)\s
4173 ([MTCRAD
])\d
*$/xo
) {
4174 push @mods, { mode_a
=> $1, mode_b
=> $2,
4175 sha1_a
=> $3, sha1_b
=> $4,
4177 if ($5 =~ /^(?:C|R)$/) {
4182 } elsif ($state eq 'file_a') {
4183 my $x = $mods[$#mods] or croak
"Empty array\n";
4184 if ($x->{chg
} !~ /^(?:C|R)$/) {
4185 croak
"Error parsing $_, $x->{chg}\n";
4189 } elsif ($state eq 'file_b') {
4190 my $x = $mods[$#mods] or croak
"Empty array\n";
4191 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
4192 croak
"Error parsing $_, $x->{chg}\n";
4194 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
4195 croak
"Error parsing $_, $x->{chg}\n";
4200 croak
"Error parsing $_\n";
4203 command_close_pipe
($diff_fh, $ctx);
4207 sub check_diff_paths
{
4208 my ($ra, $pfx, $rev, $mods) = @_;
4210 $pfx .= '/' if length $pfx;
4212 sub type_diff_paths
{
4213 my ($ra, $types, $path, $rev) = @_;
4214 my @p = split m
#/+#, $path;
4216 unless (defined $types->{$c}) {
4217 $types->{$c} = $ra->check_path($c, $rev);
4220 $c .= '/' . shift @p;
4221 next if defined $types->{$c};
4222 $types->{$c} = $ra->check_path($c, $rev);
4226 foreach my $m (@
$mods) {
4227 foreach my $f (qw
/file_a file_b/) {
4228 next unless defined $m->{$f};
4229 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
4230 if (length $pfx.$dir && ! defined $types{$dir}) {
4231 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
4239 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
4243 my ($self, $path) = @_;
4244 $self->{path_prefix
}.(defined $path ?
$path : '');
4248 my ($self, $path) = @_;
4249 if ($self->{url
} =~ m
#^https?://#) {
4250 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
4252 $self->{url
} . '/' . $self->repo_path($path);
4257 my $rm = $self->{rm
};
4258 delete $rm->{''}; # we never delete the url we're tracking
4261 foreach (keys %$rm) {
4262 my @d = split m
#/#, $_;
4266 $c .= '/' . shift @d;
4270 delete $rm->{$self->{svn_path
}};
4271 delete $rm->{''}; # we never delete the url we're tracking
4274 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
4279 my @dn = split m
#/#, $_;
4281 delete $rm->{join '/', @dn};
4288 command_close_pipe
($fh, $ctx);
4290 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
4291 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4292 $self->close_directory($bat->{$d}, $p);
4293 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
4294 print "\tD+\t$d/\n" unless $::_q
;
4295 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
4300 sub open_or_add_dir
{
4301 my ($self, $full_path, $baton) = @_;
4302 my $t = $self->{types
}->{$full_path};
4304 die "$full_path not known in r$self->{r} or we have a bug!\n";
4308 # SVN::Node::none and SVN::Node::file are used only once,
4309 # so we're shutting up Perl's warnings about them.
4310 if ($t == $SVN::Node
::none
) {
4311 return $self->add_directory($full_path, $baton,
4312 undef, -1, $self->{pool
});
4313 } elsif ($t == $SVN::Node
::dir
) {
4314 return $self->open_directory($full_path, $baton,
4315 $self->{r
}, $self->{pool
});
4316 } # no warnings 'once'
4317 print STDERR
"$full_path already exists in repository at ",
4318 "r$self->{r} and it is not a directory (",
4319 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
4320 } # no warnings 'once'
4325 my ($self, $path) = @_;
4326 my $bat = $self->{bat
};
4327 my $repo_path = $self->repo_path($path);
4328 return $bat->{''} unless (length $repo_path);
4329 my @p = split m
#/+#, $repo_path;
4331 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4334 $c .= '/' . shift @p;
4335 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4340 # Subroutine to convert a globbing pattern to a regular expression.
4341 # From perl cookbook.
4343 my $globstr = shift;
4344 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4345 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4346 return '^' . $globstr . '$';
4349 sub check_autoprop
{
4350 my ($self, $pattern, $properties, $file, $fbat) = @_;
4351 # Convert the globbing pattern to a regular expression.
4352 my $regex = glob2pat
($pattern);
4353 # Check if the pattern matches the file name.
4354 if($file =~ m/($regex)/) {
4355 # Parse the list of properties to set.
4356 my @props = split(/;/, $properties);
4357 foreach my $prop (@props) {
4358 # Parse 'name=value' syntax and set the property.
4359 if ($prop =~ /([^=]+)=(.*)/) {
4360 my ($n,$v) = ($1,$2);
4364 $self->change_file_prop($fbat, $n, $v);
4370 sub apply_autoprops
{
4371 my ($self, $file, $fbat) = @_;
4372 my $conf_t = ${$self->{config
}}{'config'};
4374 # Check [miscellany]/enable-auto-props in svn configuration.
4375 if (SVN
::_Core
::svn_config_get_bool
(
4377 $SVN::_Core
::SVN_CONFIG_SECTION_MISCELLANY
,
4378 $SVN::_Core
::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS
,
4380 # Auto-props are enabled. Enumerate them to look for matches.
4381 my $callback = sub {
4382 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4384 SVN
::_Core
::svn_config_enumerate
(
4386 $SVN::_Core
::SVN_CONFIG_SECTION_AUTO_PROPS
,
4392 my ($self, $m) = @_;
4393 my ($dir, $file) = split_path
($m->{file_b
});
4394 my $pbat = $self->ensure_path($dir);
4395 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4397 print "\tA\t$m->{file_b}\n" unless $::_q
;
4398 $self->apply_autoprops($file, $fbat);
4399 $self->chg_file($fbat, $m);
4400 $self->close_file($fbat,undef,$self->{pool
});
4404 my ($self, $m) = @_;
4405 my ($dir, $file) = split_path
($m->{file_b
});
4406 my $pbat = $self->ensure_path($dir);
4407 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4408 $self->url_path($m->{file_a
}), $self->{r
});
4409 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
4410 $self->chg_file($fbat, $m);
4411 $self->close_file($fbat,undef,$self->{pool
});
4415 my ($self, $path, $pbat) = @_;
4416 my $rpath = $self->repo_path($path);
4417 my ($dir, $file) = split_path
($rpath);
4418 $self->{rm
}->{$dir} = 1;
4419 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
4423 my ($self, $m) = @_;
4424 my ($dir, $file) = split_path
($m->{file_b
});
4425 my $pbat = $self->ensure_path($dir);
4426 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4427 $self->url_path($m->{file_a
}), $self->{r
});
4428 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
4429 $self->apply_autoprops($file, $fbat);
4430 $self->chg_file($fbat, $m);
4431 $self->close_file($fbat,undef,$self->{pool
});
4433 ($dir, $file) = split_path
($m->{file_a
});
4434 $pbat = $self->ensure_path($dir);
4435 $self->delete_entry($m->{file_a
}, $pbat);
4439 my ($self, $m) = @_;
4440 my ($dir, $file) = split_path
($m->{file_b
});
4441 my $pbat = $self->ensure_path($dir);
4442 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
4443 $pbat,$self->{r
},$self->{pool
});
4444 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
4445 $self->chg_file($fbat, $m);
4446 $self->close_file($fbat,undef,$self->{pool
});
4449 sub T
{ shift->M(@_) }
4451 sub change_file_prop
{
4452 my ($self, $fbat, $pname, $pval) = @_;
4453 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
4456 sub _chg_file_get_blob
($$$$) {
4457 my ($self, $fbat, $m, $which) = @_;
4458 my $fh = $::_repository
->temp_acquire("git_blob_$which");
4459 if ($m->{"mode_$which"} =~ /^120/) {
4460 print $fh 'link ' or croak
$!;
4461 $self->change_file_prop($fbat,'svn:special','*');
4462 } elsif ($m->{mode_a
} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4463 $self->change_file_prop($fbat,'svn:special',undef);
4465 my $blob = $m->{"sha1_$which"};
4466 return ($fh,) if ($blob =~ /^0{40}$/);
4467 my $size = $::_repository
->cat_blob($blob, $fh);
4468 croak
"Failed to read object $blob" if ($size < 0);
4469 $fh->flush == 0 or croak
$!;
4470 seek $fh, 0, 0 or croak
$!;
4472 my $exp = ::md5sum
($fh);
4473 seek $fh, 0, 0 or croak
$!;
4478 my ($self, $fbat, $m) = @_;
4479 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
4480 $self->change_file_prop($fbat,'svn:executable','*');
4481 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
4482 $self->change_file_prop($fbat,'svn:executable',undef);
4484 my ($fh_a, $exp_a) = _chg_file_get_blob
$self, $fbat, $m, 'a';
4485 my ($fh_b, $exp_b) = _chg_file_get_blob
$self, $fbat, $m, 'b';
4486 my $pool = SVN
::Pool
->new;
4487 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4489 my $txstream = SVN
::TxDelta
::new
($fh_a, $fh_b, $pool);
4490 my $res = SVN
::TxDelta
::send_txstream
($txstream, @
$atd, $pool);
4492 die "Unexpected result from send_txstream: $res\n",
4493 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4496 my $got = SVN
::TxDelta
::send_stream
($fh_b, @
$atd, $pool);
4497 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4498 if ($got ne $exp_b);
4500 Git
::temp_release
($fh_b, 1);
4501 Git
::temp_release
($fh_a, 1);
4506 my ($self, $m) = @_;
4507 my ($dir, $file) = split_path
($m->{file_b
});
4508 my $pbat = $self->ensure_path($dir);
4509 print "\tD\t$m->{file_b}\n" unless $::_q
;
4510 $self->delete_entry($m->{file_b
}, $pbat);
4515 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
4516 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
4518 $self->close_directory($bat->{$_}, $p);
4520 $self->close_directory($bat->{''}, $p);
4521 $self->SUPER::close_edit
($p);
4527 $self->SUPER::abort_edit
($self->{pool
});
4532 $self->SUPER::DESTROY
(@_);
4533 $self->{pool
}->clear;
4536 # this drives the editor
4539 my $mods = $self->{mods
};
4540 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
4541 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
4543 if (defined $o{$f}) {
4546 fatal
("Invalid change type: $f");
4549 $self->rmdirs if $_rmdir;
4555 return scalar @
$mods;
4558 package Git
::SVN
::Ra
;
4559 use vars qw
/@ISA $config_dir $_log_window_size/;
4562 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
4565 # enforce temporary pool usage for some simple functions
4567 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
4569 my $SUPER = "SUPER::$f";
4572 my $pool = SVN
::Pool
->new;
4573 my @ret = $self->$SUPER(@_,$pool);
4575 wantarray ?
@ret : $ret[0];
4580 sub _auth_providers
() {
4582 SVN
::Client
::get_simple_provider
(),
4583 SVN
::Client
::get_ssl_server_trust_file_provider
(),
4584 SVN
::Client
::get_simple_prompt_provider
(
4585 \
&Git
::SVN
::Prompt
::simple
, 2),
4586 SVN
::Client
::get_ssl_client_cert_file_provider
(),
4587 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
4588 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
4589 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
4590 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
4591 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
4592 SVN
::Client
::get_username_provider
(),
4593 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
4594 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
4595 SVN
::Client
::get_username_prompt_provider
(
4596 \
&Git
::SVN
::Prompt
::username
, 2)
4600 sub escape_uri_only
{
4603 foreach (split m{/}, $uri) {
4604 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
4612 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
4613 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
4614 $url = "$scheme://$domain$uri";
4620 my ($class, $url) = @_;
4622 return $RA if ($RA && $RA->{url
} eq $url);
4624 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
4625 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
4626 my $config = SVN
::Core
::config_get_config
($config_dir);
4628 my $dont_store_passwords = 1;
4629 my $conf_t = ${$config}{'config'};
4632 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4633 # produces warnings that variables are used only once.
4634 # I had not found the better way to shut them up, so
4635 # the warnings of type 'once' are disabled in this block.
4636 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4637 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4638 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
4640 SVN
::_Core
::svn_auth_set_parameter
($baton,
4641 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
4642 bless (\
$dont_store_passwords, "_p_void"));
4644 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4645 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4646 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
4648 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
4650 } # no warnings 'once'
4651 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
4653 pool
=> SVN
::Pool
->new,
4654 auth_provider_callbacks
=> $callbacks);
4655 $self->{url
} = $url;
4656 $self->{svn_path
} = $url;
4657 $self->{repos_root
} = $self->get_repos_root;
4658 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
4659 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
4660 get_dir
=> { r
=> 0, data
=> {} } };
4661 $RA = bless $self, $class;
4665 my ($self, $path, $r) = @_;
4666 my $cache = $self->{cache
}->{check_path
};
4667 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
4668 return $cache->{data
}->{$path};
4670 my $pool = SVN
::Pool
->new;
4671 my $t = $self->SUPER::check_path
($path, $r, $pool);
4673 if ($r != $cache->{r
}) {
4674 %{$cache->{data
}} = ();
4677 $cache->{data
}->{$path} = $t;
4681 my ($self, $dir, $r) = @_;
4682 my $cache = $self->{cache
}->{get_dir
};
4683 if ($r == $cache->{r
}) {
4684 if (my $x = $cache->{data
}->{$dir}) {
4685 return wantarray ? @
$x : $x->[0];
4688 my $pool = SVN
::Pool
->new;
4689 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
4690 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
4692 if ($r != $cache->{r
}) {
4693 %{$cache->{data
}} = ();
4696 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
4697 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
4701 # do not call the real DESTROY since we store ourselves in $RA
4704 # get_log(paths, start, end, limit,
4705 # discover_changed_paths, strict_node_history, receiver)
4707 my ($self, @args) = @_;
4708 my $pool = SVN
::Pool
->new;
4710 # svn_log_changed_path_t objects passed to get_log are likely to be
4711 # overwritten even if only the refs are copied to an external variable,
4712 # so we should dup the structures in their entirety. Using an
4713 # externally passed pool (instead of our temporary and quickly cleared
4714 # pool in Git::SVN::Ra) does not help matters at all...
4715 my $receiver = pop @args;
4716 my $prefix = "/".$self->{svn_path
};
4717 $prefix =~ s
#/+($)##;
4718 my $prefix_regex = qr
#^\Q$prefix\E#;
4720 my ($paths) = $_[0];
4721 return &$receiver(@_) unless $paths;
4723 foreach my $p (keys %$paths) {
4724 my $i = $paths->{$p};
4725 # Make path relative to our url, not repos_root
4726 $p =~ s/$prefix_regex//;
4727 my %s = map { $_ => $i->$_; }
4728 qw
/copyfrom_path copyfrom_rev action/;
4729 if ($s{'copyfrom_path'}) {
4730 $s{'copyfrom_path'} =~ s/$prefix_regex//;
4738 # the limit parameter was not supported in SVN 1.1.x, so we
4739 # drop it. Therefore, the receiver callback passed to it
4740 # is made aware of this limitation by being wrapped if
4741 # the limit passed to is being wrapped.
4742 if ($SVN::Core
::VERSION
le '1.2.0') {
4743 my $limit = splice(@args, 3, 1);
4745 my $receiver = pop @args;
4746 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4749 my $ret = $self->SUPER::get_log
(@args, $pool);
4755 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4756 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
4757 my $out = IO
::File
->new_tmpfile;
4759 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4760 # $ctx->diff(), so we'll create a default one
4761 my $pool = SVN
::Pool
->new_default_sub;
4763 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4764 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4766 my $ret = (($out->stat)[7] == 0);
4767 close $out or croak
$!;
4772 sub get_commit_editor
{
4773 my ($self, $log, $cb, $pool) = @_;
4774 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
4775 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
4779 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4780 my $new = ($rev_a == $rev_b);
4781 my $path = $gs->{path
};
4783 if ($new && -e
$gs->{index}) {
4784 unlink $gs->{index} or die
4785 "Couldn't unlink index: $gs->{index}: $!\n";
4787 my $pool = SVN
::Pool
->new;
4788 $editor->set_path_strip($path);
4789 my (@pc) = split m
#/#, $path;
4790 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
4792 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4794 # Since we can't rely on svn_ra_reparent being available, we'll
4795 # just have to do some magic with set_path to make it so
4796 # we only want a partial path.
4798 my $final = join('/', @pc);
4800 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4801 $sp .= '/' if length $sp;
4804 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4806 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4808 $reporter->finish_report($pool);
4810 $editor->{git_commit_ok
};
4813 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4814 # svn_ra_reparent didn't work before 1.4)
4816 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4817 my $path = $gs->{path
};
4818 my $pool = SVN
::Pool
->new;
4820 my $full_url = $self->{url
};
4821 my $old_url = $full_url;
4822 $full_url .= '/' . $path if length $path;
4823 my ($ra, $reparented);
4825 if ($old_url =~ m
#^svn(\+ssh)?://# ||
4826 ($full_url =~ m
#^https?://# &&
4827 escape_url
($full_url) ne $full_url)) {
4831 $ra = Git
::SVN
::Ra
->new($full_url);
4833 } elsif ($old_url ne $full_url) {
4834 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
4835 $self->{url
} = $full_url;
4840 $url_b = escape_url
($url_b);
4841 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
4842 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4843 $reporter->set_path('', $rev_a, 0, @lock, $pool);
4844 $reporter->finish_report($pool);
4847 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
4848 $self->{url
} = $old_url;
4852 $editor->{git_commit_ok
};
4855 sub longest_common_path
{
4856 my ($gsv, $globs) = @_;
4858 my $common_max = scalar @
$gsv;
4860 foreach my $gs (@
$gsv) {
4861 my @tmp = split m
#/#, $gs->{path};
4864 $p .= length($p) ?
"/$_" : $_;
4870 $common_max += scalar @
$globs;
4871 foreach my $glob (@
$globs) {
4872 my @tmp = split m
#/#, $glob->{path}->{left};
4875 $p .= length($p) ?
"/$_" : $_;
4881 my $longest_path = '';
4882 foreach (sort {length $b <=> length $a} keys %common) {
4883 if ($common{$_} == $common_max) {
4891 sub gs_fetch_loop_common
{
4892 my ($self, $base, $head, $gsv, $globs) = @_;
4893 return if ($base > $head);
4894 my $inc = $_log_window_size;
4895 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
4896 my $longest_path = longest_common_path
($gsv, $globs);
4897 my $ra_url = $self->{url
};
4898 my $find_trailing_edge;
4902 my $err_handler = $SVN::Error
::handler
;
4903 $SVN::Error
::handler
= sub {
4905 skip_unknown_revs
($err);
4908 my ($paths, $r, $author, $date, $log) = @_;
4910 { author
=> $author, date
=> $date, log => $log } ];
4912 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4913 sub { $revs{$_[1]} = _cb
(@_) });
4915 print "Checked through r$max\r";
4917 $find_trailing_edge = 1;
4919 if ($err and $find_trailing_edge) {
4920 print STDERR
"Path '$longest_path' ",
4921 "was probably deleted:\n",
4922 $err->expanded_message,
4923 "\nWill attempt to follow ",
4924 "revisions r$min .. r$max ",
4925 "committed before the deletion\n";
4927 while (--$hi >= $min) {
4929 $self->get_log([$longest_path], $min, $hi,
4932 $revs{$_[1]} = _cb
(@_) });
4934 print STDERR
"r$min .. r$ok OK\n";
4938 $find_trailing_edge = 0;
4940 $SVN::Error
::handler
= $err_handler;
4942 my %exists = map { $_->{path
} => $_ } @
$gsv;
4943 foreach my $r (sort {$a <=> $b} keys %revs) {
4944 my ($paths, $logged) = @
{$revs{$r}};
4946 foreach my $gs ($self->match_globs(\
%exists, $paths,
4948 if ($gs->rev_map_max >= $r) {
4951 next unless $gs->match_paths($paths, $r);
4952 $gs->{logged_rev_props
} = $logged;
4953 if (my $last_commit = $gs->last_commit) {
4954 $gs->assert_index_clean($last_commit);
4956 my $log_entry = $gs->do_fetch($paths, $r);
4958 $gs->do_git_commit($log_entry);
4960 $INDEX_FILES{$gs->{index}} = 1;
4962 foreach my $g (@
$globs) {
4963 my $k = "svn-remote.$g->{remote}." .
4965 Git
::SVN
::tmp_config
($k, $r);
4971 $self = Git
::SVN
::Ra
->new($ra_url);
4972 $ra_invalid = undef;
4975 # pre-fill the .rev_db since it'll eventually get filled in
4976 # with '0' x40 if something new gets committed
4977 foreach my $gs (@
$gsv) {
4978 next if $gs->rev_map_max >= $max;
4979 next if defined $gs->rev_map_get($max);
4980 $gs->rev_map_set($max, 0 x40
);
4982 foreach my $g (@
$globs) {
4983 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4984 Git
::SVN
::tmp_config
($k, $max);
4986 last if $max >= $head;
4989 $max = $head if ($max > $head);
4994 sub get_dir_globbed
{
4995 my ($self, $left, $depth, $r) = @_;
4997 my @x = eval { $self->get_dir($left, $r) };
4998 return unless scalar @x == 3;
4999 my $dirents = $x[0];
5001 foreach my $de (keys %$dirents) {
5002 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
5004 my @args = ("$left/$de", $depth - 1, $r);
5005 foreach my $dir ($self->get_dir_globbed(@args)) {
5006 push @finalents, "$de/$dir";
5009 push @finalents, $de;
5016 my ($self, $exists, $paths, $globs, $r) = @_;
5019 my ($self, $exists, $g, $r) = @_;
5021 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
5022 $g->{path
}->{depth
},
5025 foreach my $de (@dirs) {
5026 my $p = $g->{path
}->full_path($de);
5027 next if $exists->{$p};
5028 next if (length $g->{path
}->{right
} &&
5029 ($self->check_path($p, $r) !=
5031 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
5032 $g->{ref}->full_path($de), 1);
5035 foreach my $g (@
$globs) {
5036 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5037 if ($path->{action
} =~ /^[AR]$/) {
5038 get_dir_check
($self, $exists, $g, $r);
5041 foreach (keys %$paths) {
5042 if (/$g->{path}->{left_regex}/ &&
5043 !/$g->{path}->{regex}/) {
5044 next if $paths->{$_}->{action
} !~ /^[AR]$/;
5045 get_dir_check
($self, $exists, $g, $r);
5047 next unless /$g->{path}->{regex}/;
5049 my $pathname = $g->{path
}->full_path($p);
5050 next if $exists->{$pathname};
5051 next if ($self->check_path($pathname, $r) !=
5053 $exists->{$pathname} = Git
::SVN
->init(
5054 $self->{url
}, $pathname, undef,
5055 $g->{ref}->full_path($p), 1);
5058 foreach (split m
#/#, $g->{path}->{left}) {
5060 next unless ($paths->{$c} &&
5061 ($paths->{$c}->{action
} =~ /^[AR]$/));
5062 get_dir_check
($self, $exists, $g, $r);
5070 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
5071 my $url = $self->{repos_root
};
5072 my @components = split(m!/!, $self->{svn_path
});
5075 $url .= "/$c" if length $c;
5077 my $ra = (ref $self)->new($url);
5078 my $latest = $ra->get_latest_revnum;
5079 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5081 } while ($@
&& ($c = shift @components));
5087 unless (defined $can_do_switch) {
5088 my $pool = SVN
::Pool
->new;
5090 $self->do_switch(1, '', 0, $self->{url
},
5091 SVN
::Delta
::Editor
->new, $pool);
5096 $rep->abort_report($pool);
5104 sub skip_unknown_revs
{
5106 my $errno = $err->apr_err();
5107 # Maybe the branch we're tracking didn't
5108 # exist when the repo started, so it's
5109 # not an error if it doesn't, just continue
5111 # Wonderfully consistent library, eh?
5112 # 160013 - svn:// and file://
5113 # 175002 - http(s)://
5114 # 175007 - http(s):// (this repo required authorization, too...)
5115 # More codes may be discovered later...
5116 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
5117 my $err_key = $err->expanded_message;
5118 # revision numbers change every time, filter them out
5119 $err_key =~ s/\d+/\0/g;
5120 $err_key = "$errno\0$err_key";
5121 unless ($ignored_err{$err_key}) {
5122 warn "W: Ignoring error from SVN, path probably ",
5123 "does not exist: ($errno): ",
5124 $err->expanded_message,"\n";
5125 warn "W: Do not be alarmed at the above message ",
5126 "git-svn is just searching aggressively for ",
5128 "This may take a while on large repositories\n";
5129 $ignored_err{$err_key} = 1;
5133 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5136 package Git
::SVN
::Log
;
5139 use POSIX qw
/strftime/;
5141 use constant commit_log_separator
=> ('-' x
72) . "\n";
5142 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
5143 %rusers $show_commit $incremental/;
5148 return 1 if defined $c->{r
};
5150 # big commit message got truncated by the 16k pretty buffer in rev-list
5151 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
5152 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
5154 my @log = command
(qw
/cat-file commit/, $c->{c
});
5156 # shift off the headers
5157 shift @log while ($log[0] ne '');
5160 # TODO: make $c->{l} not have a trailing newline in the future
5161 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
5163 (undef, $c->{r
}, undef) = ::extract_metadata
(
5164 (grep(/^git-svn-id: /, @log))[-1]);
5166 return defined $c->{r
};
5170 return $color || Git
->repository->get_colorbool('color.diff');
5173 sub git_svn_log_cmd
{
5174 my ($r_min, $r_max, @args) = @_;
5176 my (@files, @log_opts);
5177 foreach my $x (@args) {
5178 if ($x eq '--' || @files) {
5181 if (::verify_ref
("$x^0")) {
5189 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
5190 $gs ||= Git
::SVN
->_new;
5191 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
5193 push @cmd, '-r' unless $non_recursive;
5194 push @cmd, qw
/--raw --name-status/ if $verbose;
5195 push @cmd, '--color' if log_use_color
();
5196 push @cmd, @log_opts;
5197 if (defined $r_max && $r_max == $r_min) {
5198 push @cmd, '--max-count=1';
5199 if (my $c = $gs->rev_map_get($r_max)) {
5202 } elsif (defined $r_max) {
5203 if ($r_max < $r_min) {
5204 ($r_min, $r_max) = ($r_max, $r_min);
5206 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5207 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5208 # If there are no commits in the range, both $c_max and $c_min
5209 # will be undefined. If there is at least 1 commit in the
5210 # range, both will be defined.
5211 return () if !defined $c_min || !defined $c_max;
5212 if ($c_min eq $c_max) {
5213 push @cmd, '--max-count=1', $c_min;
5215 push @cmd, '--boundary', "$c_min..$c_max";
5218 return (@cmd, @files);
5221 # adapted from pager.c
5223 chomp(my $pager = command_oneline
(qw(var GIT_PAGER)));
5224 if ($pager eq 'cat') {
5227 $ENV{GIT_PAGER_IN_USE
} = defined($pager);
5231 return unless -t
*STDOUT
&& defined $pager;
5232 pipe my ($rfd, $wfd) or return;
5233 defined(my $pid = fork) or ::fatal
"Can't fork: $!";
5235 open STDOUT
, '>&', $wfd or
5236 ::fatal
"Can't redirect to stdout: $!";
5239 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!";
5240 $ENV{LESS
} ||= 'FRSX';
5241 exec $pager or ::fatal
"Can't run pager: $! ($pager)";
5244 sub format_svn_date
{
5245 # some systmes don't handle or mishandle %z, so be creative.
5246 my $t = shift || time;
5247 my $gm = timelocal
(gmtime($t));
5248 my $sign = qw( + + - )[ $t <=> $gm ];
5249 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5250 return strftime
("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
5253 sub parse_git_date
{
5255 # Date::Parse isn't in the standard Perl distro :(
5256 if ($tz =~ s/^\+//) {
5257 $t += tz_to_s_offset
($tz);
5258 } elsif ($tz =~ s/^\-//) {
5259 $t -= tz_to_s_offset
($tz);
5264 sub set_local_timezone
{
5272 sub tz_to_s_offset
{
5275 return ($1 * 60) + ($tz * 3600);
5278 sub get_author_info
{
5279 my ($dest, $author, $t, $tz) = @_;
5280 $author =~ s/(?:^\s*|\s*$)//g;
5281 $dest->{a_raw
} = $author;
5284 $au = $rusers{$author} || undef;
5287 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5292 $dest->{t_utc
} = parse_git_date
($t, $tz);
5295 sub process_commit
{
5296 my ($c, $r_min, $r_max, $defer) = @_;
5297 if (defined $r_min && defined $r_max) {
5298 if ($r_min == $c->{r
} && $r_min == $r_max) {
5302 return 1 if $r_min == $r_max;
5303 if ($r_min < $r_max) {
5304 # we need to reverse the print order
5305 return 0 if (defined $limit && --$limit < 0);
5309 if ($r_min != $r_max) {
5310 return 1 if ($r_min < $c->{r
});
5311 return 1 if ($r_max > $c->{r
});
5314 return 0 if (defined $limit && --$limit < 0);
5323 if (my $l = $c->{l
}) {
5324 while ($l->[0] =~ /^\s*$/) { shift @
$l }
5327 $l_fmt ||= 'A' . length($c->{r
});
5328 print 'r',pack($l_fmt, $c->{r
}),' | ';
5329 print "$c->{c} | " if $show_commit;
5332 show_commit_normal
($c);
5336 sub show_commit_changed_paths
{
5338 return unless $c->{changed
};
5339 print "Changed paths:\n", @
{$c->{changed
}};
5342 sub show_commit_normal
{
5344 print commit_log_separator
, "r$c->{r} | ";
5345 print "$c->{c} | " if $show_commit;
5346 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
5349 if (my $l = $c->{l
}) {
5350 while ($l->[$#$l] eq "\n" && $#$l > 0
5351 && $l->[($#$l - 1)] eq "\n") {
5354 $nr_line = scalar @
$l;
5356 print "1 line\n\n\n";
5358 if ($nr_line == 1) {
5359 $nr_line = '1 line';
5361 $nr_line .= ' lines';
5363 print $nr_line, "\n";
5364 show_commit_changed_paths
($c);
5366 print $_ foreach @
$l;
5370 show_commit_changed_paths
($c);
5374 foreach my $x (qw
/raw stat diff/) {
5377 print $_ foreach @
{$c->{$x}}
5384 my ($r_min, $r_max);
5385 my $r_last = -1; # prevent dupes
5386 set_local_timezone
();
5387 if (defined $::_revision
) {
5388 if ($::_revision
=~ /^(\d+):(\d+)$/) {
5389 ($r_min, $r_max) = ($1, $2);
5390 } elsif ($::_revision
=~ /^\d+$/) {
5391 $r_min = $r_max = $::_revision
;
5393 ::fatal
"-r$::_revision is not supported, use ",
5394 "standard 'git log' arguments instead";
5399 @args = git_svn_log_cmd
($r_min, $r_max, @args);
5401 print commit_log_separator
unless $incremental || $oneline;
5404 my $log = command_output_pipe
(@args);
5406 my (@k, $c, $d, $stat);
5407 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5409 if (/^${esc_color}commit -?($::sha1_short)/o) {
5411 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
5413 process_commit
($c, $r_min, $r_max, \
@k) or
5418 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5419 get_author_info
($c, $1, $2, $3);
5420 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5422 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5423 push @
{$c->{raw
}}, $_;
5424 } elsif (/^${esc_color}[ACRMDT]\t/) {
5425 # we could add $SVN->{svn_path} here, but that requires
5426 # remote access at the moment (repo_path_split)...
5427 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5428 push @
{$c->{changed
}}, $_;
5429 } elsif (/^${esc_color}diff /o) {
5431 push @
{$c->{diff
}}, $_;
5433 push @
{$c->{diff
}}, $_;
5434 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
5435 $esc_color*[\
+\
-]*$esc_color$/x
) {
5437 push @
{$c->{stat}}, $_;
5438 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5439 push @
{$c->{stat}}, $_;
5441 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5442 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
5443 } elsif (s/^${esc_color} //o) {
5444 push @
{$c->{l
}}, $_;
5447 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
5449 process_commit
($c, $r_min, $r_max, \
@k);
5452 ($r_min, $r_max) = ($r_max, $r_min);
5453 process_commit
($_, $r_min, $r_max) foreach reverse @k;
5457 print commit_log_separator
unless $incremental || $oneline;
5466 my ($fh, $ctx, $rev);
5469 ($fh, $ctx) = command_output_pipe
('blame', @_, $path);
5470 while (my $line = <$fh>) {
5471 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5472 # Uncommitted edits show up as a rev ID of
5473 # all zeros, which we can't look up with
5476 (undef, $rev, undef) =
5478 $rev = '0' if (!$rev);
5482 $rev = sprintf('%-10s', $rev);
5483 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5488 ($fh, $ctx) = command_output_pipe
('blame', '-p', @_, 'HEAD',
5493 my %dsha; #distinct sha keys
5495 while (my $line = <$fh>) {
5496 push @buffer, $line;
5497 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5502 my $s2r = ::cmt_sha2rev_batch
([keys %dsha]);
5504 foreach my $line (@buffer) {
5505 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5507 $rev = '0' if (!$rev)
5509 elsif ($line =~ /^author (.*)/) {
5510 $authors{$rev} = $1;
5511 $authors{$rev} =~ s/\s/_/g;
5513 elsif ($line =~ /^\t(.*)$/) {
5514 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5518 command_close_pipe
($fh, $ctx);
5521 package Git
::SVN
::Migration
;
5522 # these version numbers do NOT correspond to actual version numbers
5523 # of git nor git-svn. They are just relative.
5525 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5527 # v1 layout: .git/$id/info/url, refs/remotes/$id
5529 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5531 # v3 layout: .git/svn/$id, refs/remotes/$id
5532 # - info/url may remain for backwards compatibility
5533 # - this is what we migrate up to this layout automatically,
5534 # - this will be used by git svn init on single branches
5535 # v3.1 layout (auto migrated):
5536 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5537 # for backwards compatibility
5539 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5540 # - this is only created for newly multi-init-ed
5541 # repositories. Similar in spirit to the
5542 # --use-separate-remotes option in git-clone (now default)
5543 # - we do not automatically migrate to this (following
5544 # the example set by core git)
5546 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
5547 # - newer, more-efficient format that uses 24-bytes per record
5548 # with no filler space.
5549 # - use xxd -c24 < .rev_map.$UUID to view and debug
5550 # - This is a one-way migration, repositories updated to the
5551 # new format will not be able to use old git-svn without
5552 # rebuilding the .rev_db. Rebuilding the rev_db is not
5553 # possible if noMetadata or useSvmProps are set; but should
5554 # be no problem for users that use the (sensible) defaults.
5558 use File
::Path qw
/mkpath/;
5559 use File
::Basename qw
/dirname basename/;
5560 use vars qw
/$_minimize/;
5562 sub migrate_from_v0
{
5563 my $git_dir = $ENV{GIT_DIR
};
5564 return undef unless -d
$git_dir;
5565 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5569 my ($id, $orig_ref) = ($_, $_);
5570 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
5571 next unless -f
"$git_dir/$id/info/url";
5572 my $new_ref = "refs/remotes/$id";
5573 if (::verify_ref
("$new_ref^0")) {
5574 print STDERR
"W: $orig_ref is probably an old ",
5575 "branch used by an ancient version of ",
5577 "However, $new_ref also exists.\n",
5578 "We will not be able ",
5579 "to use this branch until this ",
5580 "ambiguity is resolved.\n";
5583 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
5584 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
5585 command_noisy
('update-ref', $new_ref, $orig_ref);
5586 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
5589 command_close_pipe
($fh, $ctx);
5590 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
5594 sub migrate_from_v1
{
5595 my $git_dir = $ENV{GIT_DIR
};
5597 return $migrated unless -d
$git_dir;
5598 my $svn_dir = "$git_dir/svn";
5600 # just in case somebody used 'svn' as their $id at some point...
5601 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
5603 print STDERR
"Migrating from a git-svn v1 layout...\n";
5605 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
5606 "$svn_dir\n\t(required for this version ",
5607 "($::VERSION) of git-svn) does not exist.\n";
5608 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5611 next unless $x =~ s
#^refs/remotes/##;
5613 next unless -f
"$git_dir/$x/info/url";
5614 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
5616 my $dn = dirname
("$git_dir/svn/$x");
5617 mkpath
([$dn]) unless -d
$dn;
5618 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5619 mkpath
(["$git_dir/svn/svn"]);
5620 print STDERR
" - $git_dir/$x/info => ",
5621 "$git_dir/svn/$x/info\n";
5622 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5624 # don't worry too much about these, they probably
5625 # don't exist with repos this old (save for index,
5626 # and we can easily regenerate that)
5627 foreach my $f (qw
/unhandled.log index .rev_db/) {
5628 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5631 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
5632 rename "$git_dir/$x", "$git_dir/svn/$x" or
5637 command_close_pipe
($fh, $ctx);
5638 print STDERR
"Done migrating from a git-svn v1 layout\n";
5643 my ($l_map, $pfx, $path) = @_;
5645 foreach (<$path/*>) {
5646 if (-r
"$_/info/url") {
5647 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
5648 my $ref_id = $pfx . basename
$_;
5649 my $url = ::file_to_s
("$_/info/url");
5650 $l_map->{$ref_id} = $url;
5657 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5658 read_old_urls
($l_map, $x, $_);
5662 sub migrate_from_v2
{
5663 my @cfg = command
(qw
/config -l/);
5664 return if grep /^svn-remote\..+\.url=/, @cfg;
5666 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
5669 foreach my $ref_id (sort keys %l_map) {
5670 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
5672 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5679 sub minimize_connections
{
5680 my $r = Git
::SVN
::read_all_remotes
();
5682 my $root_repos = {};
5683 foreach my $repo_id (keys %$r) {
5684 my $url = $r->{$repo_id}->{url
} or next;
5685 my $fetch = $r->{$repo_id}->{fetch
} or next;
5686 my $ra = Git
::SVN
::Ra
->new($url);
5688 # skip existing cases where we already connect to the root
5689 if (($ra->{url
} eq $ra->{repos_root
}) ||
5690 ($ra->{repos_root
} eq $repo_id)) {
5691 $root_repos->{$ra->{url
}} = $repo_id;
5695 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
5696 my $root_path = $ra->{url
};
5697 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
5698 foreach my $path (keys %$fetch) {
5699 my $ref_id = $fetch->{$path};
5700 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
5702 # make sure we can read when connecting to
5703 # a higher level of a repository
5704 my ($last_rev, undef) = $gs->last_rev_commit;
5705 if (!defined $last_rev) {
5707 $root_ra->get_latest_revnum;
5711 my $new = $root_path;
5712 $new .= length $path ?
"/$path" : '';
5714 $root_ra->get_log([$new], $last_rev, $last_rev,
5718 $new_urls->{$ra->{repos_root
}}->{$new} =
5719 { ref_id
=> $ref_id,
5720 old_repo_id
=> $repo_id,
5721 old_path
=> $path };
5726 foreach my $url (keys %$new_urls) {
5727 # see if we can re-use an existing [svn-remote "repo_id"]
5728 # instead of creating a(n ugly) new section:
5729 my $repo_id = $root_repos->{$url} || $url;
5731 my $fetch = $new_urls->{$url};
5732 foreach my $path (keys %$fetch) {
5733 my $x = $fetch->{$path};
5734 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
5735 my $pfx = "svn-remote.$x->{old_repo_id}";
5737 my $old_fetch = quotemeta("$x->{old_path}:".
5739 command_noisy
(qw
/config --unset/,
5740 "$pfx.fetch", '^'. $old_fetch . '$');
5741 delete $r->{$x->{old_repo_id
}}->
5742 {fetch
}->{$x->{old_path
}};
5743 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
5744 command_noisy
(qw
/config --unset/,
5746 push @emptied, $x->{old_repo_id
}
5751 my $file = $ENV{GIT_CONFIG
} || "$ENV{GIT_DIR}/config";
5753 The following [svn-remote] sections in your config file ($file) are empty
5754 and can be safely removed:
5756 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
5760 sub migration_check
{
5764 minimize_connections
() if $_minimize;
5767 package Git
::IndexInfo
;
5770 use Git qw
/command_input_pipe command_close_pipe/;
5774 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
5775 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
5779 my ($self, $path) = @_;
5780 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
5781 return ++$self->{nr
};
5787 my ($self, $mode, $hash, $path) = @_;
5788 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
5789 return ++$self->{nr
};
5796 command_close_pipe
($self->{gui
}, $self->{ctx
});
5799 package Git
::SVN
::GlobSpec
;
5804 my ($class, $glob) = @_;
5806 $re =~ s!/+$!!g; # no need for trailing slashes
5807 $re =~ m!^([^*]*)(\*(?:/\*)*)(.*)$!;
5809 my ($left, $right) = ($1, $3);
5811 my $depth = $re =~ tr/*/*/;
5812 if ($depth != $temp =~ tr/*/*/) {
5813 die "Only one set of wildcard directories " .
5814 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5817 die "One '*' is needed for glob: '$glob'\n";
5819 $re =~ s!\*!\[^/\]*!g;
5820 $re = quotemeta($left) . "($re)" . quotemeta($right);
5821 if (length $left && !($left =~ s!/+$!!g)) {
5822 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5824 if (length $right && !($right =~ s!^/+!!g)) {
5825 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5827 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
5828 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
5829 regex
=> qr/$re/, glob => $glob, depth
=> $depth }, $class;
5833 my ($self, $path) = @_;
5834 return (length $self->{left
} ?
"$self->{left}/" : '') .
5835 $path . (length $self->{right
} ?
"/$self->{right}" : '');
5843 $remotes = { # returned by read_all_remotes()
5845 # svn-remote.svn.url=https://svn.musicpd.org
5846 url
=> 'https://svn.musicpd.org',
5847 # svn-remote.svn.fetch=mpd/trunk:trunk
5849 'mpd/trunk' => 'trunk',
5851 # svn-remote.svn.tags=mpd/tags/*:tags/*
5856 regex
=> qr!mpd/tags/([^/]+)$!,
5862 regex
=> qr!tags/([^/]+)$!,
5869 $log_entry hashref as returned by libsvn_log_entry
()
5871 log => 'whitespace-formatted log entry
5872 ', # trailing newline is preserved
5873 revision
=> '8', # integer
5874 date
=> '2004-02-24T17:01:44.108345Z', # commit date
5875 author
=> 'committer name'
5879 # this is generated by generate_diff();
5880 @mods = array of diff
-index line hashes
, each element represents one line
5881 of diff
-index output
5883 diff
-index line
($m hash
)
5885 mode_a
=> first column of diff
-index output
, no leading
':',
5886 mode_b
=> second column of diff
-index output
,
5887 sha1_b
=> sha1sum of the final blob
,
5888 chg
=> change type
[MCRADT
],
5889 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
5890 file_b
=> new
/current file name of a file
(any chg
)
5894 # retval of read_url_paths{,_all}();
5896 # repository root url
5897 'https://svn.musicpd.org' => {
5898 # repository path # GIT_SVN_ID
5899 'mpd/trunk' => 'trunk',
5900 'mpd/tags/0.11.5' => 'tags/0.11.5',
5905 I don
't trust the each() function on unless I created %hash myself
5906 because the internal iterator may not have started at base.