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;
23 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
25 $| = 1; # unbuffer STDOUT
27 sub fatal
(@
) { print STDERR
"@_\n"; exit 1 }
28 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
31 if ($SVN::Core
::VERSION
lt '1.1.0') {
32 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
34 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
35 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
36 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
40 use File
::Basename qw
/dirname basename/;
41 use File
::Path qw
/mkpath/;
43 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
48 # import functions from Git into our packages, en masse
50 foreach (qw
/command command_oneline command_noisy command_output_pipe
51 command_input_pipe command_close_pipe
52 command_bidi_pipe command_close_bidi_pipe
/) {
53 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
54 Git::SVN::Migration Git::SVN::Log Git::SVN),
56 *{"${package}::$_"} = \
&{"Git::$_"};
63 $sha1 = qr/[a-f\d]{40}/;
64 $sha1_short = qr/[a-f\d]{4,40}/;
65 my ($_stdin, $_help, $_edit,
66 $_message, $_file, $_branch_dest,
68 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
69 $_merge, $_strategy, $_dry_run, $_local,
70 $_prefix, $_no_checkout, $_url, $_verbose,
71 $_git_format, $_commit_url, $_tag);
72 $Git::SVN
::_follow_parent
= 1;
74 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
75 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
76 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
77 'ignore-paths=s' => \
$SVN::Git
::Fetcher
::_ignore_regex
);
78 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
79 'authors-file|A=s' => \
$_authors,
80 'authors-prog=s' => \
$_authors_prog,
81 'repack:i' => \
$Git::SVN
::_repack
,
82 'noMetadata' => \
$Git::SVN
::_no_metadata
,
83 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
84 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
85 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
86 'no-checkout' => \
$_no_checkout,
88 'repack-flags|repack-args|repack-opts=s' =>
89 \
$Git::SVN
::_repack_flags
,
90 'use-log-author' => \
$Git::SVN
::_use_log_author
,
91 'add-author-from' => \
$Git::SVN
::_add_author_from
,
92 'localtime' => \
$Git::SVN
::_localtime
,
95 my ($_trunk, @_tags, @_branches, $_stdlayout);
97 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
98 'trunk|T=s' => \
$_trunk, 'tags|t=s@' => \
@_tags,
99 'branches|b=s@' => \
@_branches, 'prefix=s' => \
$_prefix,
100 'stdlayout|s' => \
$_stdlayout,
101 'minimize-url|m' => \
$Git::SVN
::_minimize_url
,
102 'no-metadata' => sub { $icv{noMetadata
} = 1 },
103 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
104 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
105 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
107 my %cmt_opts = ( 'edit|e' => \
$_edit,
108 'rmdir' => \
$SVN::Git
::Editor
::_rmdir
,
109 'find-copies-harder' => \
$SVN::Git
::Editor
::_find_copies_harder
,
110 'l=i' => \
$SVN::Git
::Editor
::_rename_limit
,
111 'copy-similarity|C=i'=> \
$SVN::Git
::Editor
::_cp_similarity
115 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
116 { 'revision|r=s' => \
$_revision,
117 'fetch-all|all' => \
$_fetch_all,
118 'parent|p' => \
$_fetch_parent,
120 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
121 { 'revision|r=s' => \
$_revision,
122 %fc_opts, %init_opts } ],
123 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
124 " (requires URL argument)",
126 'multi-init' => [ \
&cmd_multi_init
,
127 "Deprecated alias for ".
128 "'$0 init -T<trunk> -b<branches> -t<tags>'",
130 dcommit
=> [ \
&cmd_dcommit
,
131 'Commit several diffs to merge with upstream',
132 { 'merge|m|M' => \
$_merge,
133 'strategy|s=s' => \
$_strategy,
134 'verbose|v' => \
$_verbose,
135 'dry-run|n' => \
$_dry_run,
136 'fetch-all|all' => \
$_fetch_all,
137 'commit-url=s' => \
$_commit_url,
138 'revision|r=i' => \
$_revision,
139 'no-rebase' => \
$_no_rebase,
140 %cmt_opts, %fc_opts } ],
141 branch
=> [ \
&cmd_branch
,
142 'Create a branch in the SVN repository',
143 { 'message|m=s' => \
$_message,
144 'destination|d=s' => \
$_branch_dest,
145 'dry-run|n' => \
$_dry_run,
146 'tag|t' => \
$_tag } ],
147 tag
=> [ sub { $_tag = 1; cmd_branch
(@_) },
148 'Create a tag in the SVN repository',
149 { 'message|m=s' => \
$_message,
150 'destination|d=s' => \
$_branch_dest,
151 'dry-run|n' => \
$_dry_run } ],
152 'set-tree' => [ \
&cmd_set_tree
,
153 "Set an SVN repository to a git tree-ish",
154 { 'stdin' => \
$_stdin, %cmt_opts, %fc_opts, } ],
155 'create-ignore' => [ \
&cmd_create_ignore
,
156 'Create a .gitignore per svn:ignore',
157 { 'revision|r=i' => \
$_revision
159 'propget' => [ \
&cmd_propget
,
160 'Print the value of a property on a file or directory',
161 { 'revision|r=i' => \
$_revision } ],
162 'proplist' => [ \
&cmd_proplist
,
163 'List all properties of a file or directory',
164 { 'revision|r=i' => \
$_revision } ],
165 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
166 { 'revision|r=i' => \
$_revision
168 'show-externals' => [ \
&cmd_show_externals
, "Show svn:externals listings",
169 { 'revision|r=i' => \
$_revision
171 'multi-fetch' => [ \
&cmd_multi_fetch
,
172 "Deprecated alias for $0 fetch --all",
173 { 'revision|r=s' => \
$_revision, %fc_opts } ],
174 'migrate' => [ sub { },
175 # no-op, we automatically run this anyways,
176 'Migrate configuration/metadata/layout from
177 previous versions of git-svn',
178 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
180 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
181 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
182 'revision|r=s' => \
$_revision,
183 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
184 'incremental' => \
$Git::SVN
::Log
::incremental
,
185 'oneline' => \
$Git::SVN
::Log
::oneline
,
186 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
187 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
188 'authors-file|A=s' => \
$_authors,
189 'color' => \
$Git::SVN
::Log
::color
,
190 'pager=s' => \
$Git::SVN
::Log
::pager
192 'find-rev' => [ \
&cmd_find_rev
,
193 "Translate between SVN revision numbers and tree-ish",
195 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
196 { 'merge|m|M' => \
$_merge,
197 'verbose|v' => \
$_verbose,
198 'strategy|s=s' => \
$_strategy,
199 'local|l' => \
$_local,
200 'fetch-all|all' => \
$_fetch_all,
201 'dry-run|n' => \
$_dry_run,
203 'commit-diff' => [ \
&cmd_commit_diff
,
204 'Commit a diff between two trees',
205 { 'message|m=s' => \
$_message,
206 'file|F=s' => \
$_file,
207 'revision|r=s' => \
$_revision,
209 'info' => [ \
&cmd_info
,
210 "Show info about the latest SVN revision
211 on the current branch",
212 { 'url' => \
$_url, } ],
213 'blame' => [ \
&Git
::SVN
::Log
::cmd_blame
,
214 "Show what revision and author last modified each line of a file",
215 { 'git-format' => \
$_git_format } ],
216 'reset' => [ \
&cmd_reset
,
217 "Undo fetches back to the specified SVN revision",
218 { 'revision|r=s' => \
$_revision,
219 'parent|p' => \
$_fetch_parent } ],
223 for (my $i = 0; $i < @ARGV; $i++) {
224 if (defined $cmd{$ARGV[$i]}) {
228 } elsif ($ARGV[$i] eq 'help') {
234 # make sure we're always running at the top-level working directory
235 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
236 unless (-d
$ENV{GIT_DIR
}) {
237 if ($git_dir_user_set) {
238 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
239 "but it is not a directory\n";
241 my $git_dir = delete $ENV{GIT_DIR
};
244 $cdup = command_oneline
(qw
/rev-parse --show-cdup/);
245 $git_dir = '.' unless ($cdup);
246 chomp $cdup if ($cdup);
247 $cdup = "." unless ($cdup && length $cdup);
248 } "Already at toplevel, but $git_dir not found\n";
249 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
250 unless (-d
$git_dir) {
251 die "$git_dir still not found after going to ",
254 $ENV{GIT_DIR
} = $git_dir;
256 $_repository = Git
->repository(Repository
=> $ENV{GIT_DIR
});
259 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
261 read_repo_config
(\
%opts);
262 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
263 Getopt
::Long
::Configure
('pass_through');
265 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version,
266 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
267 'id|i=s' => \
$Git::SVN
::default_ref_id
,
268 'svn-remote|remote|R=s' => sub {
269 $Git::SVN
::no_reuse_existing
= 1;
270 $Git::SVN
::default_repo_id
= $_[1] });
271 exit 1 if (!$rv && $cmd && $cmd ne 'log');
274 version
() if $_version;
275 usage
(1) unless defined $cmd;
276 load_authors
() if $_authors;
277 if (defined $_authors_prog) {
278 $_authors_prog = "'" . File
::Spec
->rel2abs($_authors_prog) . "'";
281 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
282 Git
::SVN
::Migration
::migration_check
();
284 Git
::SVN
::init_vars
();
286 Git
::SVN
::verify_remotes_sanity
();
287 $cmd{$cmd}->[0]->(@ARGV);
290 post_fetch_checkout
();
293 ####################### primary functions ######################
295 my $exit = shift || 0;
296 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
298 git
-svn
- bidirectional operations between a single Subversion tree
and git
299 Usage
: git svn
<command
> [options
] [arguments
]\n
301 print $fd "Available commands:\n" unless $cmd;
303 foreach (sort keys %cmd) {
304 next if $cmd && $cmd ne $_;
305 next if /^multi-/; # don't show deprecated commands
306 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
307 foreach (sort keys %{$cmd{$_}->[2]}) {
308 # mixed-case options are for .git/config only
309 next if /[A-Z]/ && /^[a-z]+$/i;
310 # prints out arguments as they should be passed:
311 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
312 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
314 split /\|/,$_)," $x\n";
318 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
319 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
320 one git repository and want to keep them separate. See git-svn(1) for more
327 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
332 unless (-d $ENV{GIT_DIR}) {
333 my @init_db = ('init
');
334 push @init_db, "--template=$_template" if defined $_template;
335 if (defined $_shared) {
336 if ($_shared =~ /[a-z]/) {
337 push @init_db, "--shared=$_shared";
339 push @init_db, "--shared";
342 command_noisy(@init_db);
343 $_repository = Git->repository(Repository => ".git");
345 command_noisy('config
', 'core
.autocrlf
', 'false
');
347 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
348 foreach my $i (keys %icv) {
349 die "'$set' and '$i' cannot both be set\n" if $set;
350 next unless defined $icv{$i};
351 command_noisy('config
', "$pfx.$i", $icv{$i});
354 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
355 command_noisy('config
', "$pfx.ignore-paths", $$ignore_regex)
356 if defined $$ignore_regex;
360 my $repo_path = shift or return;
361 mkpath([$repo_path]) unless -d $repo_path;
362 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
363 $ENV{GIT_DIR} = '.git';
364 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
368 my ($url, $path) = @_;
369 if (!defined $path &&
370 (defined $_trunk || @_branches || @_tags ||
371 defined $_stdlayout) &&
372 $url !~ m#^[a-z\+]+://#) {
375 $path = basename($url) if !defined $path || !length $path;
376 cmd_init($url, $path);
377 Git::SVN::fetch_all($Git::SVN::default_repo_id);
378 command_oneline('config', 'svn.authorsfile', $_authors) if $_authors;
382 if (defined $_stdlayout) {
383 $_trunk = 'trunk' if (!defined $_trunk);
384 @_tags = 'tags' if (! @_tags);
385 @_branches = 'branches' if (! @_branches);
387 if (defined $_trunk || @_branches || @_tags) {
388 return cmd_multi_init(@_);
390 my $url = shift or die "SVN repository location required
",
391 "as a command
-line argument
\n";
392 $url = canonicalize_url($url);
396 Git::SVN->init($url);
400 if (grep /^\d+=./, @_) {
401 die "'<rev>=<commit>' fetch arguments are
",
402 "no longer supported
.\n";
406 die "Usage
: $0 fetch
[--all
] [--parent
] [svn
-remote
]\n";
408 if ($_fetch_parent) {
409 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
411 die "Unable to determine upstream SVN information from
",
412 "working tree history
\n";
414 # just fetch, don't checkout.
415 $_no_checkout = 'true';
416 $_fetch_all ? $gs->fetch_all : $gs->fetch;
417 } elsif ($_fetch_all) {
420 $remote ||= $Git::SVN::default_repo_id;
421 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
427 if ($_stdin || !@commits) {
428 print "Reading from stdin
...\n";
431 if (/\b($sha1_short)\b/o) {
432 unshift @commits, $1;
437 foreach my $c (@commits) {
438 my @tmp = command('rev-parse',$c);
439 if (scalar @tmp == 1) {
441 } elsif (scalar @tmp > 1) {
442 push @revs, reverse(command('rev-list',@tmp));
444 fatal "Failed to rev
-parse
$c";
447 my $gs = Git::SVN->new;
448 my ($r_last, $cmt_last) = $gs->last_rev_commit;
450 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
451 fatal "There are new revisions that were fetched
",
452 "and need to be merged
(or acknowledged
) ",
453 "before committing
.\nlast rev
: $r_last\n",
454 " current
: $gs->{last_rev
}";
456 $gs->set_tree($_) foreach @revs;
457 print "Done committing
",scalar @revs," revisions to SVN
\n";
463 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
464 'Cannot dcommit with a dirty index. Commit your changes first, '
465 . "or stash them with
`git stash'.\n";
469 if ($head ne 'HEAD') {
471 command_oneline([qw/symbolic-ref -q HEAD/])
474 $old_head =~ s{^refs/heads/}{};
476 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
478 command(['checkout', $head], STDERR => 0);
482 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
484 die "Unable to determine upstream SVN information from ",
485 "$head history.\nPerhaps the repository is empty.";
488 if (defined $_commit_url) {
491 $url = eval { command_oneline('config', '--get',
492 "svn-remote.$gs->{repo_id}.commiturl") };
498 my $last_rev = $_revision if defined $_revision;
500 print "Committing to $url ...\n";
502 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
503 if ($_no_rebase && scalar(@$linear_refs) > 1) {
504 warn "Attempting to commit more than one change while ",
505 "--no-rebase is enabled.\n",
506 "If these changes depend on each other, re-running ",
507 "without --no-rebase may be required."
509 my $expect_url = $url;
510 Git::SVN::remove_username($expect_url);
512 my $d = shift @$linear_refs or last;
513 unless (defined $last_rev) {
514 (undef, $last_rev, undef) = cmt_metadata("$d~1");
515 unless (defined $last_rev) {
516 fatal "Unable to extract revision information ",
521 print "diff-tree $d~1 $d\n";
524 my %ed_opts = ( r => $last_rev,
525 log => get_commit_entry($d)->{log},
526 ra => Git::SVN::Ra->new($url),
527 config => SVN::Core::config_get_config(
528 $Git::SVN::Ra::config_dir
533 print "Committed r$_[0]\n";
537 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
538 print "No changes\n$d~1 == $d\n";
539 } elsif ($parents->{$d} && @{$parents->{$d}}) {
540 $gs->{inject_parents_dcommit}->{$cmt_rev} =
543 $_fetch_all ? $gs->fetch_all : $gs->fetch;
544 $last_rev = $cmt_rev;
547 # we always want to rebase against the current HEAD,
548 # not any head that was passed to us
549 my @diff = command('diff-tree', $d,
553 @finish = rebase_cmd();
554 print STDERR "W: $d and ", $gs->refname,
555 " differ, using @finish:\n",
556 join("\n", @diff), "\n";
558 print "No changes between current HEAD and ",
560 "\nResetting to the latest ",
562 @finish = qw/reset --mixed/;
564 command_noisy(@finish, $gs->refname);
567 my ($url_, $rev_, $uuid_, $gs_) =
568 working_head_info('HEAD', \@refs);
569 my ($linear_refs_, $parents_) =
570 linearize_history($gs_, \@refs);
571 if (scalar(@$linear_refs) !=
572 scalar(@$linear_refs_)) {
573 fatal "# of revisions changed ",
575 join("\n", @$linear_refs),
577 join("\n", @$linear_refs_), "\n",
578 'If you are attempting to commit ',
579 "merges, try running:\n\t",
580 'git rebase --interactive',
581 '--preserve-merges ',
583 "\nBefore dcommitting";
585 if ($url_ ne $expect_url) {
586 fatal "URL mismatch after rebase: ",
587 "$url_ != $expect_url";
589 if ($uuid_ ne $uuid) {
590 fatal "uuid mismatch after rebase: ",
595 for ($i = 0; $i < scalar @$linear_refs; $i++) {
596 my $new = $linear_refs_->[$i] or next;
598 $parents->{$linear_refs->[$i]};
608 my $new_head = command_oneline(qw/rev-parse HEAD/);
609 my $new_is_symbolic = eval {
610 command_oneline(qw/symbolic-ref -q HEAD/);
612 if ($new_is_symbolic) {
613 print "dcommitted the branch ", $head, "\n";
615 print "dcommitted on a detached HEAD because you gave ",
616 "a revision argument.\n",
617 "The rewritten commit is: ", $new_head, "\n";
619 command(['checkout', $old_head], STDERR => 0);
626 my ($branch_name, $head) = @_;
628 unless (defined $branch_name && length $branch_name) {
629 die(($_tag ? "tag" : "branch") . " name required\n");
633 my ($src, $rev, undef, $gs) = working_head_info($head);
635 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
636 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
638 if ($#{$allglobs} == 0) {
639 $glob = $allglobs->[0];
641 unless(defined $_branch_dest) {
643 $_tag ? "tag" : "branch",
644 " paths defined for Subversion repository.\n",
645 "You must specify where you want to create the ",
646 $_tag ? "tag" : "branch",
647 " with the --destination argument.\n";
649 foreach my $g (@{$allglobs}) {
650 # SVN::Git::Editor could probably be moved to Git.pm..
651 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
652 if ($_branch_dest =~ /$re/) {
657 unless (defined $glob) {
659 $_tag ? "tag" : "branch",
660 " destination $_branch_dest\n";
663 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
664 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
666 my $ctx = SVN::Client->new(
667 auth => Git::SVN::Ra::_auth_providers(),
669 ${ $_[0] } = defined $_message
671 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
677 $ctx->ls($dst, 'HEAD', 0);
678 } and die "branch ${branch_name} already exists\n";
680 print "Copying ${src} at r${rev} to ${dst}...\n";
681 $ctx->copy($src, $rev, $dst)
688 my $revision_or_hash = shift or die "SVN or git revision required ",
689 "as a command-line argument\n";
691 if ($revision_or_hash =~ /^r\d+$/) {
695 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
697 die "Unable to determine upstream SVN information from ",
700 my $desired_revision = substr($revision_or_hash, 1);
701 $result = $gs->rev_map_get($desired_revision, $uuid);
703 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
706 print "$result\n" if $result;
710 command_noisy(qw/update-index --refresh/);
711 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
713 die "Unable to determine upstream SVN information from ",
714 "working tree history\n";
717 print "Remote Branch: " . $gs->refname . "\n";
718 print "SVN URL: " . $url . "\n";
721 if (command(qw/diff-index HEAD --/)) {
722 print STDERR "Cannot rebase with uncommited changes:\n";
723 command_noisy('status');
727 # rebase will checkout for us, so no need to do it explicitly
728 $_no_checkout = 'true';
729 $_fetch_all ? $gs->fetch_all : $gs->fetch;
731 command_noisy(rebase_cmd(), $gs->refname);
734 sub cmd_show_ignore {
735 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
736 $gs ||= Git::SVN->new;
737 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
738 $gs->prop_walk($gs->{path}, $r, sub {
739 my ($gs, $path, $props) = @_;
740 print STDOUT "\n# $path\n";
741 my $s = $props->{'svn:ignore'} or return;
742 $s =~ s/[\r\n]+/\n/g;
749 sub cmd_show_externals {
750 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
751 $gs ||= Git::SVN->new;
752 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
753 $gs->prop_walk($gs->{path}, $r, sub {
754 my ($gs, $path, $props) = @_;
755 print STDOUT "\n# $path\n";
756 my $s = $props->{'svn:externals'} or return;
757 $s =~ s/[\r\n]+/\n/g;
764 sub cmd_create_ignore {
765 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
766 $gs ||= Git::SVN->new;
767 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
768 $gs->prop_walk($gs->{path}, $r, sub {
769 my ($gs, $path, $props) = @_;
770 # $path is of the form /path/to/dir/
772 # SVN can have attributes on empty directories,
773 # which git won't track
774 mkpath([$path]) unless -d $path;
775 my $ignore = $path . '.gitignore';
776 my $s = $props->{'svn:ignore'} or return;
777 open(GITIGNORE, '>', $ignore)
778 or fatal("Failed to open `$ignore' for writing: $!");
779 $s =~ s/[\r\n]+/\n/g;
781 # Prefix all patterns so that the ignore doesn't apply
782 # to sub-directories.
784 print GITIGNORE
"$s\n";
786 or fatal
("Failed to close `$ignore': $!");
787 command_noisy
('add', '-f', $ignore);
791 sub canonicalize_path
{
793 my $dot_slash_added = 0;
794 if (substr($path, 0, 1) ne "/") {
795 $path = "./" . $path;
796 $dot_slash_added = 1;
798 # File::Spec->canonpath doesn't collapse x/../y into y (for a
799 # good reason), so let's do this manually.
801 $path =~ s
#/\.(?:/|$)#/#g;
802 $path =~ s
#/[^/]+/\.\.##g;
804 $path =~ s
#^\./## if $dot_slash_added;
810 sub canonicalize_url
{
812 $url =~ s
#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
818 # Helper for cmd_propget and cmd_proplist below.
821 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
822 $gs ||= Git
::SVN
->new;
824 # prefix THE PATH by the sub-directory from which the user
826 $path = $cmd_dir_prefix . $path;
827 fatal
("No such file or directory: $path") unless -e
$path;
828 my $is_dir = -d
$path ?
1 : 0;
829 $path = $gs->{path
} . '/' . $path;
831 # canonicalize the path (otherwise libsvn will abort or fail to
833 $path = canonicalize_path
($path);
835 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
838 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
841 (undef, $props) = $gs->ra->get_file($path, $r, undef);
846 # cmd_propget (PROP, PATH)
847 # ------------------------
848 # Print the SVN property PROP for PATH.
850 my ($prop, $path) = @_;
851 $path = '.' if not defined $path;
852 usage
(1) if not defined $prop;
853 my $props = get_svnprops
($path);
854 if (not defined $props->{$prop}) {
855 fatal
("`$path' does not have a `$prop' SVN property.");
857 print $props->{$prop} . "\n";
860 # cmd_proplist (PATH)
861 # -------------------
862 # Print the list of SVN properties for PATH.
865 $path = '.' if not defined $path;
866 my $props = get_svnprops
($path);
867 print "Properties on '$path':\n";
868 foreach (sort keys %{$props}) {
875 unless (defined $_trunk || @_branches || @_tags) {
879 # there are currently some bugs that prevent multi-init/multi-fetch
880 # setups from working well without this.
881 $Git::SVN
::_minimize_url
= 1;
883 $_prefix = '' unless defined $_prefix;
885 $url = canonicalize_url
($url);
889 if (defined $_trunk) {
890 my $trunk_ref = $_prefix . 'trunk';
891 # try both old-style and new-style lookups:
892 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
894 my ($trunk_url, $trunk_path) =
895 complete_svn_url
($url, $_trunk);
896 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
900 return unless @_branches || @_tags;
901 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
902 foreach my $path (@_branches) {
903 complete_url_ls_init
($ra, $path, '--branches/-b', $_prefix);
905 foreach my $path (@_tags) {
906 complete_url_ls_init
($ra, $path, '--tags/-t', $_prefix.'tags/');
910 sub cmd_multi_fetch
{
911 my $remotes = Git
::SVN
::read_all_remotes
();
912 foreach my $repo_id (sort keys %$remotes) {
913 if ($remotes->{$repo_id}->{url
}) {
914 Git
::SVN
::fetch_all
($repo_id, $remotes);
919 # this command is special because it requires no metadata
920 sub cmd_commit_diff
{
921 my ($ta, $tb, $url) = @_;
922 my $usage = "Usage: $0 commit-diff -r<revision> ".
923 "<tree-ish> <tree-ish> [<URL>]";
924 fatal
($usage) if (!defined $ta || !defined $tb);
927 my $gs = eval { Git
::SVN
->new };
929 fatal
("Needed URL or usable git-svn --id in ",
930 "the command-line\n", $usage);
933 $svn_path = $gs->{path
};
935 unless (defined $_revision) {
936 fatal
("-r|--revision is a required argument\n", $usage);
938 if (defined $_message && defined $_file) {
939 fatal
("Both --message/-m and --file/-F specified ",
940 "for the commit message.\n",
941 "I have no idea what you mean");
943 if (defined $_file) {
944 $_message = file_to_s
($_file);
946 $_message ||= get_commit_entry
($tb)->{log};
948 my $ra ||= Git
::SVN
::Ra
->new($url);
951 $r = $ra->get_latest_revnum;
952 } elsif ($r !~ /^\d+$/) {
953 die "revision argument: $r not understood by git-svn\n";
955 my %ed_opts = ( r
=> $r,
960 editor_cb
=> sub { print "Committed r$_[0]\n" },
961 svn_path
=> $svn_path );
962 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
963 print "No changes\n$ta == $tb\n";
967 sub escape_uri_only
{
970 foreach (split m{/}, $uri) {
971 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
979 if ($url =~ m
#^([^:]+)://([^/]*)(.*)$#) {
980 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
981 $url = "$scheme://$domain$uri";
987 my $path = canonicalize_path
(defined($_[0]) ?
$_[0] : ".");
988 my $fullpath = canonicalize_path
($cmd_dir_prefix . $path);
990 die "Too many arguments specified\n";
993 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
995 if (!$file_type && !$diff_status) {
996 print STDERR
"svn: '$path' is not under version control\n";
1000 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1002 die "Unable to determine upstream SVN information from ",
1003 "working tree history\n";
1006 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1007 $path = "." if $path eq "";
1009 my $full_url = $url . ($fullpath eq "" ?
"" : "/$fullpath");
1012 print escape_url
($full_url), "\n";
1016 my $result = "Path: $path\n";
1017 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
1018 $result .= "URL: " . escape_url
($full_url) . "\n";
1021 my $repos_root = $gs->repos_root;
1022 Git
::SVN
::remove_username
($repos_root);
1023 $result .= "Repository Root: " . escape_url
($repos_root) . "\n";
1026 $result .= "Repository Root: (offline)\n";
1028 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1029 ($SVN::Core
::VERSION
le '1.5.4' || $file_type ne "dir");
1030 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
1032 $result .= "Node Kind: " .
1033 ($file_type eq "dir" ?
"directory" : "file") . "\n";
1035 my $schedule = $diff_status eq "A"
1037 : ($diff_status eq "D" ?
"delete" : "normal");
1038 $result .= "Schedule: $schedule\n";
1040 if ($diff_status eq "A") {
1041 print $result, "\n";
1045 my ($lc_author, $lc_rev, $lc_date_utc);
1046 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $fullpath);
1047 my $log = command_output_pipe
(@args);
1048 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1050 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1052 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
1053 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1054 (undef, $lc_rev, undef) = ::extract_metadata
($1);
1059 Git
::SVN
::Log
::set_local_timezone
();
1061 $result .= "Last Changed Author: $lc_author\n";
1062 $result .= "Last Changed Rev: $lc_rev\n";
1063 $result .= "Last Changed Date: " .
1064 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
1066 if ($file_type ne "dir") {
1067 my $text_last_updated_date =
1068 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
1070 "Text Last Updated: " .
1071 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
1074 if ($diff_status eq "D") {
1076 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
1077 if ($file_type eq "link") {
1078 my $file_name = <$fh>;
1079 $checksum = md5sum
("link $file_name");
1081 $checksum = md5sum
($fh);
1083 command_close_pipe
($fh, $ctx);
1084 } elsif ($file_type eq "link") {
1086 command
(qw(cat-file blob), "HEAD:$path");
1088 md5sum
("link " . $file_name);
1090 open FILE
, "<", $path or die $!;
1091 $checksum = md5sum
(\
*FILE
);
1092 close FILE
or die $!;
1094 $result .= "Checksum: " . $checksum . "\n";
1097 print $result, "\n";
1101 my $target = shift || $_revision or die "SVN revision required\n";
1102 $target = $1 if $target =~ /^r(\d+)$/;
1103 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1104 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1106 die "Unable to determine upstream SVN information from ".
1109 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1110 $gs->rev_map_set($r, $c, 'reset', $uuid);
1111 print "r$r = $c ($gs->{ref_id})\n";
1114 ########################### utility functions #########################
1117 my @cmd = qw
/rebase/;
1118 push @cmd, '-v' if $_verbose;
1119 push @cmd, qw
/--merge/ if $_merge;
1120 push @cmd, "--strategy=$_strategy" if $_strategy;
1124 sub post_fetch_checkout
{
1125 return if $_no_checkout;
1126 my $gs = $Git::SVN
::_head
or return;
1127 return if verify_ref
('refs/heads/master^0');
1129 my $valid_head = verify_ref
('HEAD^0');
1130 command_noisy
(qw(update-ref refs/heads/master), $gs->refname);
1131 return if ($valid_head || !verify_ref
('HEAD^0'));
1133 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
1134 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
1135 return if -f
$index;
1137 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
1138 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
1139 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
1140 print STDERR
"Checked out HEAD:\n ",
1141 $gs->full_url, " r", $gs->last_rev, "\n";
1144 sub complete_svn_url
{
1145 my ($url, $path) = @_;
1147 if ($path !~ m
#^[a-z\+]+://#) {
1148 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
1149 fatal
("E: '$path' is not a complete URL ",
1150 "and a separate URL is not specified");
1152 return ($url, $path);
1157 sub complete_url_ls_init
{
1158 my ($ra, $repo_path, $switch, $pfx) = @_;
1159 unless ($repo_path) {
1160 print STDERR
"W: $switch not specified\n";
1163 $repo_path =~ s
#/+$##;
1164 if ($repo_path =~ m
#^[a-z\+]+://#) {
1165 $ra = Git
::SVN
::Ra
->new($repo_path);
1168 $repo_path =~ s
#^/+##;
1170 fatal
("E: '$repo_path' is not a complete URL ",
1171 "and a separate URL is not specified");
1174 my $url = $ra->{url
};
1175 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
1176 my $k = "svn-remote.$gs->{repo_id}.url";
1177 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
1178 if ($orig_url && ($orig_url ne $gs->{url
})) {
1179 die "$k already set: $orig_url\n",
1180 "wanted to set to: $gs->{url}\n";
1182 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
1183 my $remote_path = "$ra->{svn_path}/$repo_path";
1184 $remote_path =~ s
#/+#/#g;
1185 $remote_path =~ s
#^/##g;
1186 $remote_path .= "/*" if $remote_path !~ /\
*/;
1187 my ($n) = ($switch =~ /^--(\w+)/);
1188 if (length $pfx && $pfx !~ m
#/$#) {
1189 die "--prefix='$pfx' must have a trailing slash '/'\n";
1191 command_noisy
('config',
1193 "svn-remote.$gs->{repo_id}.$n",
1194 "$remote_path:refs/remotes/$pfx*" .
1195 ('/*' x
(($remote_path =~ tr
/*/*/) - 1)) );
1200 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1201 { STDERR
=> 0 }); };
1204 sub get_tree_from_treeish
{
1206 # $treeish can be a symbolic ref, too:
1207 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1209 while ($type eq 'tag') {
1210 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1212 if ($type eq 'commit') {
1213 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1215 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1216 die "Unable to get tree from $treeish\n" unless $expected;
1217 } elsif ($type eq 'tree') {
1218 $expected = $treeish;
1220 die "$treeish is a $type, expected tree, tag or commit\n";
1225 sub get_commit_entry
{
1226 my ($treeish) = shift;
1227 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
1228 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1229 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1230 open my $log_fh, '>', $commit_editmsg or croak
$!;
1232 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1233 if ($type eq 'commit' || $type eq 'tag') {
1234 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1242 $in_msg = 1 if (/^\s*$/);
1243 $author = $1 if (/^author (.*>)/);
1244 } elsif (/^git-svn-id: /) {
1245 # skip this for now, we regenerate the
1246 # correct one on re-fetch anyways
1247 # TODO: set *:merge properties or like...
1249 if (/^From:/ || /^Signed-off-by:/) {
1255 $msgbuf =~ s/\s+$//s;
1256 if ($Git::SVN
::_add_author_from
&& defined($author)
1258 $msgbuf .= "\n\nFrom: $author";
1260 print $log_fh $msgbuf or croak
$!;
1261 command_close_pipe
($msg_fh, $ctx);
1263 close $log_fh or croak
$!;
1265 if ($_edit || ($type eq 'tree')) {
1266 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1267 # TODO: strip out spaces, comments, like git-commit.sh
1268 system($editor, $commit_editmsg);
1270 rename $commit_editmsg, $commit_msg or croak
$!;
1273 # SVN requires messages to be UTF-8 when entering the repo
1275 open $log_fh, '<', $commit_msg or croak
$!;
1277 chomp($log_entry{log} = <$log_fh>);
1279 my $enc = Git
::config
('i18n.commitencoding') || 'UTF-8';
1280 my $msg = $log_entry{log};
1282 eval { $msg = Encode
::decode
($enc, $msg, 1) };
1284 die "Could not decode as $enc:\n", $msg,
1285 "\nPerhaps you need to set i18n.commitencoding\n";
1288 eval { $msg = Encode
::encode
('UTF-8', $msg, 1) };
1289 die "Could not encode as UTF-8:\n$msg\n" if $@
;
1291 $log_entry{log} = $msg;
1293 close $log_fh or croak
$!;
1300 my ($str, $file, $mode) = @_;
1301 open my $fd,'>',$file or croak
$!;
1302 print $fd $str,"\n" or croak
$!;
1303 close $fd or croak
$!;
1304 chmod ($mode &~ umask, $file) if (defined $mode);
1309 open my $fd,'<',$file or croak
"$!: file: $file\n";
1312 close $fd or croak
$!;
1317 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1319 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1320 my $log = $cmd eq 'log';
1321 while (<$authors>) {
1323 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1324 my ($user, $name, $email) = ($1, $2, $3);
1326 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1328 $users{$user} = [$name, $email];
1331 close $authors or croak
$!;
1334 # convert GetOpt::Long specs for use by git-config
1335 sub read_repo_config
{
1336 return unless -d
$ENV{GIT_DIR
};
1339 foreach my $o (keys %$opts) {
1340 # if we have mixedCase and a long option-only, then
1341 # it's a config-only variable that we don't need for
1343 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1344 my $v = $opts->{$o};
1345 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1347 my $arg = 'git config';
1348 $arg .= ' --int' if ($o =~ /[:=]i$/);
1349 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1350 if (ref $v eq 'ARRAY') {
1351 chomp(my @tmp = `$arg --get-all svn.$key`);
1354 chomp(my $tmp = `$arg --get svn.$key`);
1355 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1360 delete @
$opts{@config_only} if @config_only;
1363 sub extract_metadata
{
1364 my $id = shift or return (undef, undef, undef);
1365 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1367 if (!defined $rev || !$uuid || !$url) {
1368 # some of the original repositories I made had
1369 # identifiers like this:
1370 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1372 return ($url, $rev, $uuid);
1376 return extract_metadata
((grep(/^git-svn-id: /,
1377 command
(qw
/cat-file commit/, shift)))[-1]);
1380 sub cmt_sha2rev_batch
{
1382 my ($pid, $in, $out, $ctx) = command_bidi_pipe
(qw
/cat-file --batch/);
1385 foreach my $sha (@
{$list}) {
1388 print $out $sha, "\n";
1390 while (my $line = <$in>) {
1391 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1394 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1398 } elsif ($line =~ /^(git-svn-id: )/) {
1399 my (undef, $rev, undef) =
1400 extract_metadata
($line);
1404 $size -= length($line);
1405 last if ($size == 0);
1409 command_close_bidi_pipe
($pid, $in, $out, $ctx);
1414 sub working_head_info
{
1415 my ($head, $refs) = @_;
1416 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
1417 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1421 if ( m{^commit ($::sha1)$} ) {
1422 unshift @
$refs, $hash if $hash and $refs;
1426 next unless s{^\s*(git-svn-id:)}{$1};
1427 my ($url, $rev, $uuid) = extract_metadata
($_);
1428 if (defined $url && defined $rev) {
1429 next if $max{$url} and $max{$url} < $rev;
1430 if (my $gs = Git
::SVN
->find_by_url($url)) {
1431 my $c = $gs->rev_map_get($rev, $uuid);
1432 if ($c && $c eq $hash) {
1433 close $fh; # break the pipe
1434 return ($url, $rev, $uuid, $gs);
1436 $max{$url} ||= $gs->rev_map_max;
1441 command_close_pipe
($fh, $ctx);
1442 (undef, undef, undef, undef);
1445 sub read_commit_parents
{
1446 my ($parents, $c) = @_;
1447 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1448 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1449 @
{$parents->{$c}} = split(/ /, $p);
1452 sub linearize_history
{
1453 my ($gs, $refs) = @_;
1455 foreach my $c (@
$refs) {
1456 read_commit_parents
(\
%parents, $c);
1461 my $last_svn_commit = $gs->last_commit;
1462 foreach my $c (reverse @
$refs) {
1463 next if $c eq $last_svn_commit;
1466 unshift @linear_refs, $c;
1469 # we only want the first parent to diff against for linear
1470 # history, we save the rest to inject when we finalize the
1472 my $fp_a = verify_ref
("$c~1");
1473 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1474 if (!$fp_a || !$fp_b) {
1476 "has no parent commit, and therefore ",
1477 "nothing to diff against.\n",
1478 "You should be working from a repository ",
1479 "originally created by git-svn\n";
1481 if ($fp_a ne $fp_b) {
1482 die "$c~1 = $fp_a, however parsing commit $c ",
1483 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1486 foreach my $p (@
{$parents{$c}}) {
1490 (\
@linear_refs, \
%parents);
1493 sub find_file_type_and_diff_status
{
1495 return ('dir', '') if $path eq '';
1498 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1499 my $diff_status = (split(' ', $diff_output))[0] || "";
1501 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1503 return (undef, undef) if !$diff_status && !$ls_tree;
1505 if ($diff_status eq "A") {
1506 return ("link", $diff_status) if -l
$path;
1507 return ("dir", $diff_status) if -d
$path;
1508 return ("file", $diff_status);
1511 my $mode = (split(' ', $ls_tree))[0] || "";
1513 return ("link", $diff_status) if $mode eq "120000";
1514 return ("dir", $diff_status) if $mode eq "040000";
1515 return ("file", $diff_status);
1521 my $md5 = Digest
::MD5
->new();
1522 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
1523 $md5->addfile($arg) or croak
$!;
1524 } elsif ($ref eq 'SCALAR') {
1525 $md5->add($$arg) or croak
$!;
1527 $md5->add($arg) or croak
$!;
1529 ::fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1531 return $md5->hexdigest();
1537 use Fcntl qw
/:DEFAULT :seek/;
1538 use constant rev_map_fmt
=> 'NH40';
1539 use vars qw
/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
1540 $_repack $_repack_flags $_use_svm_props $_head
1541 $_use_svnsync_props $no_reuse_existing $_minimize_url
1542 $_use_log_author $_add_author_from $_localtime/;
1544 use File
::Path qw
/mkpath/;
1545 use File
::Copy qw
/copy/;
1548 my ($_gc_nr, $_gc_period);
1550 # properties that we do not log:
1553 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
1554 svn
:special svn
:executable
1555 svn
:entry
:committed
-rev
1556 svn
:entry
:last-author
1558 svn
:entry
:committed
-date
/;
1560 # some options are read globally, but can be overridden locally
1561 # per [svn-remote "..."] section. Command-line options will *NOT*
1562 # override options set in an [svn-remote "..."] section
1564 for my $option (qw
/follow_parent no_metadata use_svm_props
1565 use_svnsync_props
/) {
1568 my $prop = "-$option";
1571 return $self->{$prop} if exists $self->{$prop};
1572 my $k = "svn-remote.$self->{repo_id}.$key";
1573 eval { command_oneline
(qw
/config --get/, $k) };
1575 $self->{$prop} = ${"Git::SVN::_$option"};
1577 my $v = command_oneline
(qw
/config --bool/,$k);
1578 $self->{$prop} = $v eq 'false' ?
0 : 1;
1580 return $self->{$prop};
1586 my (%LOCKFILES, %INDEX_FILES);
1588 unlink keys %LOCKFILES if %LOCKFILES;
1589 unlink keys %INDEX_FILES if %INDEX_FILES;
1592 sub resolve_local_globs
{
1593 my ($url, $fetch, $glob_spec) = @_;
1594 return unless defined $glob_spec;
1595 my $ref = $glob_spec->{ref};
1596 my $path = $glob_spec->{path
};
1597 foreach (command
(qw
#for-each-ref --format=%(refname) refs/remotes#)) {
1598 next unless m
#^refs/remotes/$ref->{regex}$#;
1600 my $pathname = desanitize_refname
($path->full_path($p));
1601 my $refname = desanitize_refname
($ref->full_path($p));
1602 if (my $existing = $fetch->{$pathname}) {
1603 if ($existing ne $refname) {
1604 die "Refspec conflict:\n",
1605 "existing: refs/remotes/$existing\n",
1606 " globbed: refs/remotes/$refname\n";
1608 my $u = (::cmt_metadata
("refs/remotes/$refname"))[0];
1609 $u =~ s!^\Q$url\E(/|$)!! or die
1610 "refs/remotes/$refname: '$url' not found in '$u'\n";
1611 if ($pathname ne $u) {
1612 warn "W: Refspec glob conflict ",
1613 "(ref: refs/remotes/$refname):\n",
1614 "expected path: $pathname\n",
1616 "Continuing ahead with $u\n";
1620 $fetch->{$pathname} = $refname;
1625 sub parse_revision_argument
{
1626 my ($base, $head) = @_;
1627 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
1628 return ($base, $head);
1630 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
1631 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
1632 return ($head, $head) if ($::_revision
eq 'HEAD');
1633 return ($base, $1) if ($::_revision
=~ /^BASE:(\d+)$/);
1634 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
1635 die "revision argument: $::_revision not understood by git-svn\n";
1639 my ($repo_id, $remotes) = @_;
1643 $repo_id = $gs->{repo_id
};
1645 $remotes ||= read_all_remotes
();
1646 my $remote = $remotes->{$repo_id} or
1647 die "[svn-remote \"$repo_id\"] unknown\n";
1648 my $fetch = $remote->{fetch
};
1649 my $url = $remote->{url
} or die "svn-remote.$repo_id.url not defined\n";
1651 my $ra = Git
::SVN
::Ra
->new($url);
1652 my $uuid = $ra->get_uuid;
1653 my $head = $ra->get_latest_revnum;
1654 my $base = defined $fetch ?
$head : 0;
1656 # read the max revs for wildcard expansion (branches/*, tags/*)
1657 foreach my $t (qw
/branches tags/) {
1658 defined $remote->{$t} or next;
1659 push @globs, @
{$remote->{$t}};
1661 my $max_rev = eval { tmp_config
(qw
/--int --get/,
1662 "svn-remote.$repo_id.${t}-maxRev") };
1663 if (defined $max_rev && ($max_rev < $base)) {
1665 } elsif (!defined $max_rev) {
1671 foreach my $p (sort keys %$fetch) {
1672 my $gs = Git
::SVN
->new($fetch->{$p}, $repo_id, $p);
1673 my $lr = $gs->rev_map_max;
1675 $base = $lr if ($lr < $base);
1681 ($base, $head) = parse_revision_argument
($base, $head);
1682 $ra->gs_fetch_loop_common($base, $head, \
@gs, \
@globs);
1685 sub read_all_remotes
{
1687 my $use_svm_props = eval { command_oneline
(qw
/config
--bool
1688 svn
.useSvmProps
/) };
1689 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
1690 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
1691 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1692 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1693 die("svn-remote.$remote: remote ref '$_remote_ref' "
1694 . "must start with 'refs/remotes/'\n")
1695 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1696 my $remote_ref = $1;
1697 $local_ref =~ s{^/}{};
1698 $r->{$remote}->{fetch
}->{$local_ref} = $remote_ref;
1699 $r->{$remote}->{svm
} = {} if $use_svm_props;
1700 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1701 $r->{$1}->{svm
} = {};
1702 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1703 $r->{$1}->{url
} = $2;
1704 } elsif (m
!^(.+)\
.(branches
|tags
)=
1705 (.*):refs
/remotes
/(.+)\s
*$/!x
) {
1706 my ($p, $g) = ($3, $4);
1710 path
=> Git
::SVN
::GlobSpec
->new($p),
1711 ref => Git
::SVN
::GlobSpec
->new($g) };
1712 if (length($rs->{ref}->{right
}) != 0) {
1713 die "The '*' glob character must be the last ",
1714 "character of '$g'\n";
1716 push @
{ $r->{$1}->{$2} }, $rs;
1721 if (defined $r->{$_}->{svm
}) {
1724 my $section = "svn-remote.$_";
1726 source
=> tmp_config
('--get',
1727 "$section.svm-source"),
1728 replace
=> tmp_config
('--get',
1729 "$section.svm-replace"),
1732 $r->{$_}->{svm
} = $svm;
1740 $_gc_nr = $_gc_period = 1000;
1741 if (defined $_repack || defined $_repack_flags) {
1742 warn "Repack options are obsolete; they have no effect.\n";
1746 sub verify_remotes_sanity
{
1747 return unless -d
$ENV{GIT_DIR
};
1749 foreach (command
(qw
/config -l/)) {
1750 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1752 die "Remote ref refs/remote/$1 is tracked by",
1753 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1754 "Please resolve this ambiguity in ",
1755 "your git configuration file before ",
1763 sub find_existing_remote
{
1764 my ($url, $remotes) = @_;
1765 return undef if $no_reuse_existing;
1767 foreach my $repo_id (keys %$remotes) {
1768 my $u = $remotes->{$repo_id}->{url
} or next;
1770 $existing = $repo_id;
1776 sub init_remote_config
{
1777 my ($self, $url, $no_write) = @_;
1778 $url =~ s!/+$!!; # strip trailing slash
1779 my $r = read_all_remotes
();
1780 my $existing = find_existing_remote
($url, $r);
1782 unless ($no_write) {
1783 print STDERR
"Using existing ",
1784 "[svn-remote \"$existing\"]\n";
1786 $self->{repo_id
} = $existing;
1787 } elsif ($_minimize_url) {
1788 my $min_url = Git
::SVN
::Ra
->new($url)->minimize_url;
1789 $existing = find_existing_remote
($min_url, $r);
1791 unless ($no_write) {
1792 print STDERR
"Using existing ",
1793 "[svn-remote \"$existing\"]\n";
1795 $self->{repo_id
} = $existing;
1797 if ($min_url ne $url) {
1798 unless ($no_write) {
1799 print STDERR
"Using higher level of URL: ",
1800 "$url => $min_url\n";
1802 my $old_path = $self->{path
};
1803 $self->{path
} = $url;
1804 $self->{path
} =~ s!^\Q$min_url\E(/|$)!!;
1805 if (length $old_path) {
1806 $self->{path
} .= "/$old_path";
1813 # verify that we aren't overwriting anything:
1815 command_oneline
('config', '--get',
1816 "svn-remote.$self->{repo_id}.url")
1818 if ($orig_url && ($orig_url ne $url)) {
1819 die "svn-remote.$self->{repo_id}.url already set: ",
1820 "$orig_url\nwanted to set to: $url\n";
1823 my ($xrepo_id, $xpath) = find_ref
($self->refname);
1824 if (defined $xpath) {
1825 die "svn-remote.$xrepo_id.fetch already set to track ",
1826 "$xpath:refs/remotes/", $self->refname, "\n";
1828 unless ($no_write) {
1829 command_noisy
('config',
1830 "svn-remote.$self->{repo_id}.url", $url);
1831 $self->{path
} =~ s{^/}{};
1832 command_noisy
('config', '--add',
1833 "svn-remote.$self->{repo_id}.fetch",
1834 "$self->{path}:".$self->refname);
1836 $self->{url
} = $url;
1839 sub find_by_url
{ # repos_root and, path are optional
1840 my ($class, $full_url, $repos_root, $path) = @_;
1842 return undef unless defined $full_url;
1843 remove_username
($full_url);
1844 remove_username
($repos_root) if defined $repos_root;
1845 my $remotes = read_all_remotes
();
1846 if (defined $full_url && defined $repos_root && !defined $path) {
1848 $path =~ s
#^\Q$repos_root\E(?:/|$)##;
1850 foreach my $repo_id (keys %$remotes) {
1851 my $u = $remotes->{$repo_id}->{url
} or next;
1852 remove_username
($u);
1853 next if defined $repos_root && $repos_root ne $u;
1855 my $fetch = $remotes->{$repo_id}->{fetch
} || {};
1856 foreach my $t (qw
/branches tags/) {
1857 foreach my $globspec (@
{$remotes->{$repo_id}->{$t}}) {
1858 resolve_local_globs
($u, $fetch, $globspec);
1862 my $rwr = rewrite_root
({repo_id
=> $repo_id});
1863 my $svm = $remotes->{$repo_id}->{svm
}
1864 if defined $remotes->{$repo_id}->{svm
};
1865 unless (defined $p) {
1871 remove_username
($z);
1872 } elsif (defined $svm) {
1873 $z = $svm->{source
};
1874 $prefix = $svm->{replace
};
1875 $prefix =~ s
#^\Q$u\E(?:/|$)##;
1878 $p =~ s
#^\Q$z\E(?:/|$)#$prefix# or next;
1880 foreach my $f (keys %$fetch) {
1882 return Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1889 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1890 my $self = _new
($class, $repo_id, $ref_id, $path);
1892 $self->init_remote_config($url, $no_write);
1899 foreach (command
(qw
/config -l/)) {
1900 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
1901 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
1902 my ($repo_id, $path, $ref) = ($1, $2, $3);
1903 if ($ref eq $ref_id) {
1904 $path = '' if ($path =~ m
#^\./?#);
1905 return ($repo_id, $path);
1908 (undef, undef, undef);
1912 my ($class, $ref_id, $repo_id, $path) = @_;
1913 if (defined $ref_id && !defined $repo_id && !defined $path) {
1914 ($repo_id, $path) = find_ref
($ref_id);
1915 if (!defined $repo_id) {
1916 die "Could not find a \"svn-remote.*.fetch\" key ",
1917 "in the repository configuration matching: ",
1918 "refs/remotes/$ref_id\n";
1921 my $self = _new
($class, $repo_id, $ref_id, $path);
1922 if (!defined $self->{path
} || !length $self->{path
}) {
1923 my $fetch = command_oneline
('config', '--get',
1924 "svn-remote.$repo_id.fetch",
1925 ":refs/remotes/$ref_id\$") or
1926 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1927 "\":refs/remotes/$ref_id\$\" in config\n";
1928 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
1930 $self->{url
} = command_oneline
('config', '--get',
1931 "svn-remote.$repo_id.url") or
1932 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1938 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1940 # It cannot end with a slash /, we'll throw up on this because
1941 # SVN can't have directories with a slash in their name, either:
1942 if ($refname =~ m{/$}) {
1943 die "ref: '$refname' ends with a trailing slash, this is ",
1944 "not permitted by git nor Subversion\n";
1947 # It cannot have ASCII control character space, tilde ~, caret ^,
1948 # colon :, question-mark ?, asterisk *, space, or open bracket [
1951 # Additionally, % must be escaped because it is used for escaping
1952 # and we want our escaped refname to be reversible
1953 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1955 # no slash-separated component can begin with a dot .
1957 $refname =~ s{/\.}{/%2E}g;
1959 # It cannot have two consecutive dots .. anywhere
1961 $refname =~ s{\.\.}{%2E%2E}g;
1966 sub desanitize_refname
{
1968 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1974 return $self->{svm
}->{uuid
} if $self->svm;
1976 unless ($self->{svm
}) {
1977 die "SVM UUID not cached, and reading remotely failed\n";
1979 $self->{svm
}->{uuid
};
1984 return $self->{svm
} if $self->{svm
};
1986 # see if we have it in our config, first:
1988 my $section = "svn-remote.$self->{repo_id}";
1990 source
=> tmp_config
('--get', "$section.svm-source"),
1991 uuid
=> tmp_config
('--get', "$section.svm-uuid"),
1992 replace
=> tmp_config
('--get', "$section.svm-replace"),
1995 if ($svm && $svm->{source
} && $svm->{uuid
} && $svm->{replace
}) {
1996 $self->{svm
} = $svm;
2002 my ($self, $ra) = @_;
2003 return $ra if $self->svm;
2005 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
2006 "(svm:source, svm:uuid) ",
2007 "from the following URLs:\n" );
2008 sub read_svm_props
{
2009 my ($self, $ra, $path, $r) = @_;
2010 my $props = ($ra->get_dir($path, $r))[2];
2011 my $src = $props->{'svm:source'};
2012 my $uuid = $props->{'svm:uuid'};
2013 return undef if (!$src || !$uuid);
2017 $uuid =~ m{^[0-9a-f\-]{30,}$}
2018 or die "doesn't look right - svm:uuid is '$uuid'\n";
2020 # the '!' is used to mark the repos_root!/relative/path
2021 $src =~ s{/?!/?}{/};
2022 $src =~ s{/+$}{}; # no trailing slashes please
2023 # username is of no interest
2024 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
2026 my $replace = $ra->{url
};
2027 $replace .= "/$path" if length $path;
2029 my $section = "svn-remote.$self->{repo_id}";
2030 tmp_config
("$section.svm-source", $src);
2031 tmp_config
("$section.svm-replace", $replace);
2032 tmp_config
("$section.svm-uuid", $uuid);
2040 my $r = $ra->get_latest_revnum;
2041 my $path = $self->{path
};
2043 while (length $path) {
2044 unless ($tried{"$self->{url}/$path"}) {
2045 return $ra if $self->read_svm_props($ra, $path, $r);
2046 $tried{"$self->{url}/$path"} = 1;
2048 $path =~ s
#/?[^/]+$##;
2050 die "Path: '$path' should be ''\n" if $path ne '';
2051 return $ra if $self->read_svm_props($ra, $path, $r);
2052 $tried{"$self->{url}/$path"} = 1;
2054 if ($ra->{repos_root
} eq $self->{url
}) {
2055 die @err, (map { " $_\n" } keys %tried), "\n";
2058 # nope, make sure we're connected to the repository root:
2061 $path = $ra->{svn_path
};
2062 $ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
2063 while (length $path) {
2064 unless ($tried{"$ra->{url}/$path"}) {
2065 $ok = $self->read_svm_props($ra, $path, $r);
2067 $tried{"$ra->{url}/$path"} = 1;
2069 $path =~ s
#/?[^/]+$##;
2071 die "Path: '$path' should be ''\n" if $path ne '';
2072 $ok ||= $self->read_svm_props($ra, $path, $r);
2073 $tried{"$ra->{url}/$path"} = 1;
2075 die @err, (map { " $_\n" } keys %tried), "\n";
2077 Git
::SVN
::Ra
->new($self->{url
});
2082 return $self->{svnsync
} if $self->{svnsync
};
2084 if ($self->no_metadata) {
2085 die "Can't have both 'noMetadata' and ",
2086 "'useSvnsyncProps' options set!\n";
2088 if ($self->rewrite_root) {
2089 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2094 # see if we have it in our config, first:
2096 my $section = "svn-remote.$self->{repo_id}";
2098 my $url = tmp_config
('--get', "$section.svnsync-url");
2099 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2100 die "doesn't look right - svn:sync-from-url is '$url'\n";
2102 my $uuid = tmp_config
('--get', "$section.svnsync-uuid");
2103 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
2104 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2106 $svnsync = { url
=> $url, uuid
=> $uuid }
2108 if ($svnsync && $svnsync->{url
} && $svnsync->{uuid
}) {
2109 return $self->{svnsync
} = $svnsync;
2112 my $err = "useSvnsyncProps set, but failed to read " .
2113 "svnsync property: svn:sync-from-";
2114 my $rp = $self->ra->rev_proplist(0);
2116 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
2117 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2118 die "doesn't look right - svn:sync-from-url is '$url'\n";
2120 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
2121 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
2122 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2124 my $section = "svn-remote.$self->{repo_id}";
2125 tmp_config
('--add', "$section.svnsync-uuid", $uuid);
2126 tmp_config
('--add', "$section.svnsync-url", $url);
2127 return $self->{svnsync
} = { url
=> $url, uuid
=> $uuid };
2130 # this allows us to memoize our SVN::Ra UUID locally and avoid a
2131 # remote lookup (useful for 'git svn log').
2134 unless ($self->{ra_uuid
}) {
2135 my $key = "svn-remote.$self->{repo_id}.uuid";
2136 my $uuid = eval { tmp_config
('--get', $key) };
2137 if (!$@
&& $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
2138 $self->{ra_uuid
} = $uuid;
2140 die "ra_uuid called without URL\n" unless $self->{url
};
2141 $self->{ra_uuid
} = $self->ra->get_uuid;
2142 tmp_config
('--add', $key, $self->{ra_uuid
});
2148 sub _set_repos_root
{
2149 my ($self, $repos_root) = @_;
2150 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2151 $repos_root ||= $self->ra->{repos_root
};
2152 tmp_config
($k, $repos_root);
2158 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2159 eval { tmp_config
('--get', $k) } || $self->_set_repos_root;
2164 my $ra = Git
::SVN
::Ra
->new($self->{url
});
2165 $self->_set_repos_root($ra->{repos_root
});
2166 if ($self->use_svm_props && !$self->{svm
}) {
2167 if ($self->no_metadata) {
2168 die "Can't have both 'noMetadata' and ",
2169 "'useSvmProps' options set!\n";
2170 } elsif ($self->use_svnsync_props) {
2171 die "Can't have both 'useSvnsyncProps' and ",
2172 "'useSvmProps' options set!\n";
2174 $ra = $self->_set_svm_vars($ra);
2175 $self->{-want_revprops
} = 1;
2182 my $repos_root = $self->ra->{repos_root
};
2183 return $self->{path
} if ($self->{url
} eq $repos_root);
2184 my $url = $self->{url
} .
2185 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
2186 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2190 # prop_walk(PATH, REV, SUB)
2191 # -------------------------
2192 # Recursively traverse PATH at revision REV and invoke SUB for each
2193 # directory that contains a SVN property. SUB will be invoked as
2194 # follows: &SUB(gs, path, props); where `gs' is this instance of
2195 # Git::SVN, `path' the path to the directory where the properties
2196 # `props' were found. The `path' will be relative to point of checkout,
2197 # that is, if url://repo/trunk is the current Git branch, and that
2198 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
2199 # as `path' (note the trailing `/').
2201 my ($self, $path, $rev, $sub) = @_;
2204 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2207 # Strip the irrelevant part of the path.
2208 $p =~ s
#^/+\Q$self->{path}\E(/|$)#/#;
2209 # Ensure the path is terminated by a `/'.
2212 # The properties contain all the internal SVN stuff nobody
2213 # (usually) cares about.
2214 my $interesting_props = 0;
2215 foreach (keys %{$props}) {
2216 # If it doesn't start with `svn:', it must be a
2217 # user-defined property.
2218 ++$interesting_props and next if $_ !~ /^svn:/;
2219 # FIXME: Fragile, if SVN adds new public properties,
2220 # this needs to be updated.
2221 ++$interesting_props if /^svn
:(?
:ignore
|keywords
|executable
2222 |eol
-style
|mime
-type
2223 |externals
|needs
-lock)$/x
;
2225 &$sub($self, $p, $props) if $interesting_props;
2227 foreach (sort keys %$dirent) {
2228 next if $dirent->{$_}->{kind
} != $SVN::Node
::dir
;
2229 $self->prop_walk($self->{path
} . $p . $_, $rev, $sub);
2233 sub last_rev
{ ($_[0]->last_rev_commit)[0] }
2234 sub last_commit
{ ($_[0]->last_rev_commit)[1] }
2236 # returns the newest SVN revision number and newest commit SHA1
2237 sub last_rev_commit
{
2239 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
2240 return ($self->{last_rev
}, $self->{last_commit
});
2242 my $c = ::verify_ref
($self->refname.'^0');
2243 if ($c && !$self->use_svm_props && !$self->no_metadata) {
2244 my $rev = (::cmt_metadata
($c))[1];
2246 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
2250 my $map_path = $self->map_path;
2251 unless (-e
$map_path) {
2252 ($self->{last_rev
}, $self->{last_commit
}) = (undef, undef);
2253 return (undef, undef);
2255 my ($rev, $commit) = $self->rev_map_max(1);
2256 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $commit);
2257 return ($rev, $commit);
2260 sub get_fetch_range
{
2261 my ($self, $min, $max) = @_;
2262 $max ||= $self->ra->get_latest_revnum;
2263 $min ||= $self->rev_map_max;
2269 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2270 my $config = "$ENV{GIT_DIR}/svn/.metadata";
2271 if (! -f
$config && -f
$old_def_config) {
2272 rename $old_def_config, $config or
2273 die "Failed rename $old_def_config => $config: $!\n";
2275 my $old_config = $ENV{GIT_CONFIG
};
2276 $ENV{GIT_CONFIG
} = $config;
2279 unless (-f
$config) {
2281 open my $fh, '>', $config or
2282 die "Can't open $config: $!\n";
2283 print $fh "; This file is used internally by ",
2285 "Couldn't write to $config: $!\n";
2286 print $fh "; You should not have to edit it\n" or
2287 die "Couldn't write to $config: $!\n";
2288 close $fh or die "Couldn't close $config: $!\n";
2290 command
('config', @args);
2293 if (defined $old_config) {
2294 $ENV{GIT_CONFIG
} = $old_config;
2296 delete $ENV{GIT_CONFIG
};
2299 wantarray ?
@ret : $ret[0];
2303 my ($self, $sub) = @_;
2304 my $old_index = $ENV{GIT_INDEX_FILE
};
2305 $ENV{GIT_INDEX_FILE
} = $self->{index};
2308 my ($dir, $base) = ($self->{index} =~ m
#^(.*?)/?([^/]+)$#);
2309 mkpath
([$dir]) unless -d
$dir;
2313 if (defined $old_index) {
2314 $ENV{GIT_INDEX_FILE
} = $old_index;
2316 delete $ENV{GIT_INDEX_FILE
};
2319 wantarray ?
@ret : $ret[0];
2322 sub assert_index_clean
{
2323 my ($self, $treeish) = @_;
2325 $self->tmp_index_do(sub {
2326 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
2327 my $x = command_oneline
('write-tree');
2328 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
2329 /^tree ($::sha1)/mo);
2332 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2333 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2334 command_noisy
('read-tree', $treeish);
2335 $x = command_oneline
('write-tree');
2337 ::fatal
"trees ($treeish) $y != $x\n",
2338 "Something is seriously wrong...";
2343 sub get_commit_parents
{
2344 my ($self, $log_entry) = @_;
2345 my (%seen, @ret, @tmp);
2346 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2347 if (my $ip = $self->{inject_parents
}) {
2348 if (my $commit = delete $ip->{$log_entry->{revision
}}) {
2352 if (my $cur = ::verify_ref
($self->refname.'^0')) {
2355 if (my $ipd = $self->{inject_parents_dcommit
}) {
2356 if (my $commit = delete $ipd->{$log_entry->{revision
}}) {
2357 push @tmp, @
$commit;
2360 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
2361 while (my $p = shift @tmp) {
2365 # MAXPARENT is defined to 16 in commit-tree.c:
2369 die "r$log_entry->{revision}: No room for parents:\n\t",
2370 join("\n\t", @tmp), "\n";
2377 return $self->{-rewrite_root
} if exists $self->{-rewrite_root
};
2378 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2379 my $rwr = eval { command_oneline
(qw
/config --get/, $k) };
2382 if ($rwr !~ m
#^[a-z\+]+://#) {
2383 die "$rwr is not a valid URL (key: $k)\n";
2386 $self->{-rewrite_root
} = $rwr;
2391 ($self->rewrite_root || $self->{url
}) .
2392 (length $self->{path
} ?
'/' . $self->{path
} : '');
2397 $self->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
2401 sub set_commit_header_env
{
2402 my ($log_entry) = @_;
2404 foreach my $ned (qw
/NAME EMAIL DATE/) {
2405 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2406 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2410 $ENV{GIT_AUTHOR_NAME
} = $log_entry->{name
};
2411 $ENV{GIT_AUTHOR_EMAIL
} = $log_entry->{email
};
2412 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
2414 $ENV{GIT_COMMITTER_NAME
} = (defined $log_entry->{commit_name
})
2415 ?
$log_entry->{commit_name
}
2416 : $log_entry->{name
};
2417 $ENV{GIT_COMMITTER_EMAIL
} = (defined $log_entry->{commit_email
})
2418 ?
$log_entry->{commit_email
}
2419 : $log_entry->{email
};
2423 sub restore_commit_header_env
{
2425 foreach my $ned (qw
/NAME EMAIL DATE/) {
2426 foreach my $ac (qw
/AUTHOR COMMITTER/) {
2427 my $k = "GIT_${ac}_${ned}";
2428 if (defined $env->{$k}) {
2429 $ENV{$k} = $env->{$k};
2438 command_noisy
('gc', '--auto');
2442 my ($self, $log_entry) = @_;
2443 my $lr = $self->last_rev;
2444 if (defined $lr && $lr >= $log_entry->{revision
}) {
2445 die "Last fetched revision of ", $self->refname,
2446 " was r$lr, but we are about to fetch: ",
2447 "r$log_entry->{revision}!\n";
2449 if (my $c = $self->rev_map_get($log_entry->{revision
})) {
2450 croak
"$log_entry->{revision} = $c already exists! ",
2451 "Why are we refetching it?\n";
2453 my $old_env = set_commit_header_env
($log_entry);
2454 my $tree = $log_entry->{tree
};
2455 if (!defined $tree) {
2456 $tree = $self->tmp_index_do(sub {
2457 command_oneline
('write-tree') });
2459 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2461 my @exec = ('git', 'commit-tree', $tree);
2462 foreach ($self->get_commit_parents($log_entry)) {
2463 push @exec, '-p', $_;
2465 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2469 # we always get UTF-8 from SVN, but we may want our commits in
2470 # a different encoding.
2471 if (my $enc = Git
::config
('i18n.commitencoding')) {
2473 Encode
::from_to
($log_entry->{log}, 'UTF-8', $enc);
2475 print $msg_fh $log_entry->{log} or croak
$!;
2476 restore_commit_header_env
($old_env);
2477 unless ($self->no_metadata) {
2478 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2481 $msg_fh->flush == 0 or croak
$!;
2482 close $msg_fh or croak
$!;
2483 chomp(my $commit = do { local $/; <$out_fh> });
2484 close $out_fh or croak
$!;
2487 if ($commit !~ /^$::sha1$/o) {
2488 die "Failed to commit, invalid sha1: $commit\n";
2491 $self->rev_map_set($log_entry->{revision
}, $commit, 1);
2493 $self->{last_rev
} = $log_entry->{revision
};
2494 $self->{last_commit
} = $commit;
2495 print "r$log_entry->{revision}" unless $::_q
> 1;
2496 if (defined $log_entry->{svm_revision
}) {
2497 print " (\@$log_entry->{svm_revision})" unless $::_q
> 1;
2498 $self->rev_map_set($log_entry->{svm_revision
}, $commit,
2499 0, $self->svm_uuid);
2501 print " = $commit ($self->{ref_id})\n" unless $::_q
> 1;
2502 if (--$_gc_nr == 0) {
2503 $_gc_nr = $_gc_period;
2510 my ($self, $paths, $r) = @_;
2511 return 1 if $self->{path
} eq '';
2512 if (my $path = $paths->{"/$self->{path}"}) {
2513 return ($path->{action
} eq 'D') ?
0 : 1;
2515 my $repos_root = $self->ra->{repos_root
};
2516 my $extended_path = $self->{url
} . '/' . $self->{path
};
2517 $extended_path =~ s
#^\Q$repos_root\E(/|$)##;
2518 $self->{path_regex
} ||= qr/^\/\Q
$extended_path\E\
//;
2519 if (grep /$self->{path_regex}/, keys %$paths) {
2523 foreach (split m
#/#, $self->{path}) {
2525 next unless ($paths->{$c} &&
2526 ($paths->{$c}->{action
} =~ /^[AR]$/));
2527 if ($self->ra->check_path($self->{path
}, $r) ==
2535 sub find_parent_branch
{
2536 my ($self, $paths, $rev) = @_;
2537 return undef unless $self->follow_parent;
2538 unless (defined $paths) {
2539 my $err_handler = $SVN::Error
::handler
;
2540 $SVN::Error
::handler
= \
&Git
::SVN
::Ra
::skip_unknown_revs
;
2541 $self->ra->get_log([$self->{path
}], $rev, $rev, 0, 1, 1,
2542 sub { $paths = $_[0] });
2543 $SVN::Error
::handler
= $err_handler;
2545 return undef unless defined $paths;
2547 # look for a parent from another branch:
2548 my @b_path_components = split m
#/#, $self->rel_path;
2549 my @a_path_components;
2551 while (@b_path_components) {
2552 $i = $paths->{'/'.join('/', @b_path_components)};
2553 last if $i && defined $i->{copyfrom_path
};
2554 unshift(@a_path_components, pop(@b_path_components));
2556 return undef unless defined $i && defined $i->{copyfrom_path
};
2557 my $branch_from = $i->{copyfrom_path
};
2558 if (@a_path_components) {
2559 print STDERR
"branch_from: $branch_from => ";
2560 $branch_from .= '/'.join('/', @a_path_components);
2561 print STDERR
$branch_from, "\n";
2563 my $r = $i->{copyfrom_rev
};
2564 my $repos_root = $self->ra->{repos_root
};
2565 my $url = $self->ra->{url
};
2566 my $new_url = $repos_root . $branch_from;
2567 print STDERR
"Found possible branch point: ",
2568 "$new_url => ", $self->full_url, ", $r\n";
2569 $branch_from =~ s
#^/##;
2570 my $gs = $self->other_gs($new_url, $url, $repos_root,
2571 $branch_from, $r, $self->{ref_id
});
2572 my ($r0, $parent) = $gs->find_rev_before($r, 1);
2575 if (!defined $r0 || !defined $parent) {
2576 ($base, $head) = parse_revision_argument
(0, $r);
2579 $gs->ra->get_log([$gs->{path
}], $r0 + 1, $r, 1,
2580 0, 1, sub { $base = $_[1] - 1 });
2583 if (defined $base && $base <= $r) {
2584 $gs->fetch($base, $r);
2586 ($r0, $parent) = $gs->find_rev_before($r, 1);
2588 if (defined $r0 && defined $parent) {
2589 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
2591 if ($self->ra->can_do_switch) {
2592 $self->assert_index_clean($parent);
2593 print STDERR
"Following parent with do_switch\n";
2594 # do_switch works with svn/trunk >= r22312, but that
2595 # is not included with SVN 1.4.3 (the latest version
2596 # at the moment), so we can't rely on it
2597 $self->{last_rev
} = $r0;
2598 $self->{last_commit
} = $parent;
2599 $ed = SVN
::Git
::Fetcher
->new($self, $gs->{path
});
2600 $gs->ra->gs_do_switch($r0, $rev, $gs,
2601 $self->full_url, $ed)
2602 or die "SVN connection failed somewhere...\n";
2603 } elsif ($self->ra->trees_match($new_url, $r0,
2604 $self->full_url, $rev)) {
2605 print STDERR
"Trees match:\n",
2607 " ${\$self->full_url}\@$rev\n",
2608 "Following parent with no changes\n";
2609 $self->tmp_index_do(sub {
2610 command_noisy
('read-tree', $parent);
2612 $self->{last_commit
} = $parent;
2614 print STDERR
"Following parent with do_update\n";
2615 $ed = SVN
::Git
::Fetcher
->new($self);
2616 $self->ra->gs_do_update($rev, $rev, $self, $ed)
2617 or die "SVN connection failed somewhere...\n";
2619 print STDERR
"Successfully followed parent\n";
2620 return $self->make_log_entry($rev, [$parent], $ed);
2626 my ($self, $paths, $rev) = @_;
2628 my ($last_rev, @parents);
2629 if (my $lc = $self->last_commit) {
2630 # we can have a branch that was deleted, then re-added
2631 # under the same name but copied from another path, in
2632 # which case we'll have multiple parents (we don't
2633 # want to break the original ref, nor lose copypath info):
2634 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2635 push @
{$log_entry->{parents
}}, $lc;
2638 $ed = SVN
::Git
::Fetcher
->new($self);
2639 $last_rev = $self->{last_rev
};
2644 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2647 $ed = SVN
::Git
::Fetcher
->new($self);
2649 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
2650 die "SVN connection failed somewhere...\n";
2652 $self->make_log_entry($rev, \
@parents, $ed);
2656 my ($self, $ed) = @_;
2658 my $h = $ed->{empty
};
2659 foreach (sort keys %$h) {
2660 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
2661 push @out, " $act: " . uri_encode
($_);
2662 warn "W: $act: $_\n";
2664 foreach my $t (qw
/dir_prop file_prop/) {
2665 $h = $ed->{$t} or next;
2666 foreach my $path (sort keys %$h) {
2667 my $ppath = $path eq '' ?
'.' : $path;
2668 foreach my $prop (sort keys %{$h->{$path}}) {
2669 next if $SKIP_PROP{$prop};
2670 my $v = $h->{$path}->{$prop};
2671 my $t_ppath_prop = "$t: " .
2672 uri_encode
($ppath) . ' ' .
2675 push @out, " +$t_ppath_prop " .
2678 push @out, " -$t_ppath_prop";
2683 foreach my $t (qw
/absent_file absent_directory/) {
2684 $h = $ed->{$t} or next;
2685 foreach my $parent (sort keys %$h) {
2686 foreach my $path (sort @
{$h->{$parent}}) {
2687 push @out, " $t: " .
2688 uri_encode
("$parent/$path");
2689 warn "W: $t: $parent/$path ",
2690 "Insufficient permissions?\n";
2697 # parse_svn_date(DATE)
2698 # --------------------
2699 # Given a date (in UTC) from Subversion, return a string in the format
2700 # "<TZ Offset> <local date/time>" that Git will use.
2702 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
2703 # is true we'll convert it to the local timezone instead.
2704 sub parse_svn_date
{
2705 my $date = shift || return '+0000 1970-01-01 00:00:00';
2706 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2707 (\d\d
)\
:(\d\d
)\
:(\d\d
)\
.\d
*Z
$/x
) or
2708 croak
"Unable to parse date: $date\n";
2709 my $parsed_date; # Set next.
2711 if ($Git::SVN
::_localtime
) {
2712 # Translate the Subversion datetime to an epoch time.
2713 # Begin by switching ourselves to $date's timezone, UTC.
2714 my $old_env_TZ = $ENV{TZ
};
2718 POSIX
::strftime
('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2720 # Determine our local timezone (including DST) at the
2721 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2722 # value of TZ, if any, at the time we were run.
2723 if (defined $Git::SVN
::Log
::TZ
) {
2724 $ENV{TZ
} = $Git::SVN
::Log
::TZ
;
2730 POSIX
::strftime
('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2732 # This converts $epoch_in_UTC into our local timezone.
2733 my ($sec, $min, $hour, $mday, $mon, $year,
2734 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2736 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2737 $our_TZ, $year + 1900, $mon + 1,
2738 $mday, $hour, $min, $sec);
2740 # Reset us to the timezone in effect when we entered
2742 if (defined $old_env_TZ) {
2743 $ENV{TZ
} = $old_env_TZ;
2748 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2751 return $parsed_date;
2755 my ($self, $new_url, $url, $repos_root,
2756 $branch_from, $r, $old_ref_id) = @_;
2757 my $gs = Git
::SVN
->find_by_url($new_url, $repos_root, $branch_from);
2759 my $ref_id = $old_ref_id;
2760 $ref_id =~ s/\@\d+$//;
2762 # just grow a tail if we're not unique enough :x
2763 $ref_id .= '-' while find_ref
($ref_id);
2764 print STDERR
"Initializing parent: $ref_id\n";
2765 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2766 if ($u =~ s
#^\Q$url\E(/|$)##) {
2769 $repo_id = $self->{repo_id
};
2771 $gs = Git
::SVN
->init($u, $p, $repo_id, $ref_id, 1);
2776 sub call_authors_prog
{
2777 my ($orig_author) = @_;
2778 my $author = `$::_authors_prog $orig_author`;
2780 die "$::_authors_prog failed with exit code $?\n"
2782 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2783 my ($name, $email) = ($1, $2);
2784 $email = undef if length $2 == 0;
2785 return [$name, $email];
2787 die "Author: $orig_author: $::_authors_prog returned "
2788 . "invalid author format: $author\n";
2794 if (!defined $author || length $author == 0) {
2795 $author = '(no author)';
2797 if (!defined $::users
{$author}) {
2798 if (defined $::_authors_prog
) {
2799 $::users
{$author} = call_authors_prog
($author);
2800 } elsif (defined $::_authors
) {
2801 die "Author: $author not defined in $::_authors file\n";
2807 sub make_log_entry
{
2808 my ($self, $rev, $parents, $ed) = @_;
2809 my $untracked = $self->get_untracked($ed);
2811 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
2812 print $un "r$rev\n" or croak
$!;
2813 print $un $_, "\n" foreach @
$untracked;
2814 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
2818 my $logged = delete $self->{logged_rev_props
};
2819 if (!$logged || $self->{-want_revprops
}) {
2820 my $rp = $self->ra->rev_proplist($rev);
2821 foreach (sort keys %$rp) {
2823 if (/^svn:(author|date|log)$/) {
2824 $log_entry{$1} = $v;
2825 } elsif ($_ eq 'svm:headrev') {
2828 print $un " rev_prop: ", uri_encode
($_), ' ',
2829 uri_encode
($v), "\n";
2833 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
2835 close $un or croak
$!;
2837 $log_entry{date
} = parse_svn_date
($log_entry{date
});
2838 $log_entry{log} .= "\n";
2839 my $author = $log_entry{author
} = check_author
($log_entry{author
});
2840 my ($name, $email) = defined $::users
{$author} ? @
{$::users
{$author}}
2843 my ($commit_name, $commit_email) = ($name, $email);
2844 if ($_use_log_author) {
2846 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2848 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2851 if (!defined $name_field) {
2852 if (!defined $email) {
2855 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
2856 ($name, $email) = ($1, $2);
2857 } elsif ($name_field =~ /(.*)@/) {
2858 ($name, $email) = ($1, $name_field);
2860 ($name, $email) = ($name_field, $name_field);
2863 if (defined $headrev && $self->use_svm_props) {
2864 if ($self->rewrite_root) {
2865 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2868 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d
+)$};
2869 # we don't want "SVM: initializing mirror for junk" ...
2870 return undef if $r == 0;
2871 my $svm = $self->svm;
2872 if ($uuid ne $svm->{uuid
}) {
2873 die "UUID mismatch on SVM path:\n",
2874 "expected: $svm->{uuid}\n",
2877 my $full_url = $self->full_url;
2878 $full_url =~ s
#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2879 die "Failed to replace '$svm->{replace}' with ",
2880 "'$svm->{source}' in $full_url\n";
2881 # throw away username for storing in records
2882 remove_username
($full_url);
2883 $log_entry{metadata
} = "$full_url\@$r $uuid";
2884 $log_entry{svm_revision
} = $r;
2885 $email ||= "$author\@$uuid";
2886 $commit_email ||= "$author\@$uuid";
2887 } elsif ($self->use_svnsync_props) {
2888 my $full_url = $self->svnsync->{url
};
2889 $full_url .= "/$self->{path}" if length $self->{path
};
2890 remove_username
($full_url);
2891 my $uuid = $self->svnsync->{uuid
};
2892 $log_entry{metadata
} = "$full_url\@$rev $uuid";
2893 $email ||= "$author\@$uuid";
2894 $commit_email ||= "$author\@$uuid";
2896 my $url = $self->metadata_url;
2897 remove_username
($url);
2898 $log_entry{metadata
} = "$url\@$rev " .
2899 $self->ra->get_uuid;
2900 $email ||= "$author\@" . $self->ra->get_uuid;
2901 $commit_email ||= "$author\@" . $self->ra->get_uuid;
2903 $log_entry{name
} = $name;
2904 $log_entry{email
} = $email;
2905 $log_entry{commit_name
} = $commit_name;
2906 $log_entry{commit_email
} = $commit_email;
2911 my ($self, $min_rev, $max_rev, @parents) = @_;
2912 my ($last_rev, $last_commit) = $self->last_rev_commit;
2913 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
2914 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
2918 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2919 $self->{inject_parents
} = { $rev => $tree };
2920 $self->fetch(undef, undef);
2924 my ($self, $tree) = (shift, shift);
2925 my $log_entry = ::get_commit_entry
($tree);
2926 unless ($self->{last_rev
}) {
2927 ::fatal
("Must have an existing revision to commit");
2929 my %ed_opts = ( r
=> $self->{last_rev
},
2930 log => $log_entry->{log},
2932 tree_a
=> $self->{last_commit
},
2935 $self->set_tree_cb($log_entry, $tree, @_) },
2936 svn_path
=> $self->{path
} );
2937 if (!SVN
::Git
::Editor
->new(\
%ed_opts)->apply_diff) {
2938 print "No changes\nr$self->{last_rev} = $tree\n";
2942 sub rebuild_from_rev_db
{
2943 my ($self, $path) = @_;
2945 open my $fh, '<', $path or croak
"open: $!";
2946 binmode $fh or croak
"binmode: $!";
2948 length($_) == 41 or croak
"inconsistent size in ($_) != 41";
2951 next if $_ eq ('0' x
40);
2952 $self->rev_map_set($r, $_);
2955 close $fh or croak
"close: $!";
2956 unlink $path or croak
"unlink: $!";
2961 my $map_path = $self->map_path;
2962 my $partial = (-e
$map_path && ! -z
$map_path);
2963 return unless ::verify_ref
($self->refname.'^0');
2964 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
2965 my $rev_db = $self->rev_db_path;
2966 $self->rebuild_from_rev_db($rev_db);
2967 if ($self->use_svm_props) {
2968 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2969 $self->rebuild_from_rev_db($svm_rev_db);
2971 $self->unlink_rev_db_symlink;
2974 print "Rebuilding $map_path ...\n" if (!$partial);
2975 my ($base_rev, $head) = ($partial ?
$self->rev_map_max_norebuild(1) :
2978 command_output_pipe
(qw
/rev-list --pretty=raw --no-color --reverse/,
2979 ($head ?
"$head.." : "") . $self->refname,
2981 my $metadata_url = $self->metadata_url;
2982 remove_username
($metadata_url);
2983 my $svn_uuid = $self->ra_uuid;
2986 if ( m{^commit ($::sha1)$} ) {
2990 next unless s{^\s*(git-svn-id:)}{$1};
2991 my ($url, $rev, $uuid) = ::extract_metadata
($_);
2992 remove_username
($url);
2994 # ignore merges (from set-tree)
2995 next if (!defined $rev || !$uuid);
2997 # if we merged or otherwise started elsewhere, this is
2998 # how we break out of it
2999 if (($uuid ne $svn_uuid) ||
3000 ($metadata_url && $url && ($url ne $metadata_url))) {
3003 if ($partial && $head) {
3004 print "Partial-rebuilding $map_path ...\n";
3005 print "Currently at $base_rev = $head\n";
3009 $self->rev_map_set($rev, $c);
3010 print "r$rev = $c\n";
3012 command_close_pipe
($log, $ctx);
3013 print "Done rebuilding $map_path\n" if (!$partial || !$head);
3014 my $rev_db_path = $self->rev_db_path;
3015 if (-f
$self->rev_db_path) {
3016 unlink $self->rev_db_path or croak
"unlink: $!";
3018 $self->unlink_rev_db_symlink;
3022 # Tie::File seems to be prone to offset errors if revisions get sparse,
3023 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3024 # one of my favorite modules is out :< Next up would be one of the DBM
3025 # modules, but I'm not sure which is most portable...
3027 # This is the replacement for the rev_db format, which was too big
3028 # and inefficient for large repositories with a lot of sparse history
3031 # The format is this:
3032 # - 24 bytes for every record,
3033 # * 4 bytes for the integer representing an SVN revision number
3034 # * 20 bytes representing the sha1 of a git commit
3035 # - No empty padding records like the old format
3036 # (except the last record, which can be overwritten)
3037 # - new records are written append-only since SVN revision numbers
3038 # increase monotonically
3039 # - lookups on SVN revision number are done via a binary search
3040 # - Piping the file to xxd -c24 is a good way of dumping it for
3041 # viewing or editing (piped back through xxd -r), should the need
3043 # - The last record can be padding revision with an all-zero sha1
3044 # This is used to optimize fetch performance when using multiple
3045 # "fetch" directives in .git/config
3047 # These files are disposable unless noMetadata or useSvmProps is set
3050 my ($fh, $rev, $commit) = @_;
3052 binmode $fh or croak
"binmode: $!";
3053 my $size = (stat($fh))[7];
3054 ($size % 24) == 0 or croak
"inconsistent size: $size";
3058 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
3059 my $read = sysread($fh, my $buf, 24) or croak
"read: $!";
3060 $read == 24 or croak
"read only $read bytes (!= 24)";
3061 my ($last_rev, $last_commit) = unpack(rev_map_fmt
, $buf);
3062 if ($last_commit eq ('0' x40
)) {
3064 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
3065 $read = sysread($fh, $buf, 24) or
3068 croak
"read only $read bytes (!= 24)";
3069 ($last_rev, $last_commit) =
3070 unpack(rev_map_fmt
, $buf);
3071 if ($last_commit eq ('0' x40
)) {
3072 croak
"inconsistent .rev_map\n";
3075 if ($last_rev >= $rev) {
3076 croak
"last_rev is higher!: $last_rev >= $rev";
3081 sysseek($fh, $wr_offset, SEEK_END
) or croak
"seek: $!";
3082 syswrite($fh, pack(rev_map_fmt
, $rev, $commit), 24) == 24 or
3086 sub _rev_map_reset
{
3087 my ($fh, $rev, $commit) = @_;
3088 my $c = _rev_map_get
($fh, $rev);
3089 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3090 my $offset = sysseek($fh, 0, SEEK_CUR
) or croak
"seek: $!";
3091 truncate $fh, $offset or croak
"truncate: $!";
3097 my ($dir, $base) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3098 mkpath
([$dir]) unless -d
$dir;
3099 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3100 close $fh or die "Couldn't close (create) $path: $!\n";
3105 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3106 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
3107 my $db = $self->map_path($uuid);
3108 my $db_lock = "$db.lock";
3112 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
3113 $SIG{USR1
} = $SIG{USR2
} = sub { $sig = $_[0] };
3117 $LOCKFILES{$db_lock} = 1;
3119 # both of these options make our .rev_db file very, very important
3120 # and we can't afford to lose it because rebuild() won't work
3121 if ($self->use_svm_props || $self->no_metadata) {
3123 copy
($db, $db_lock) or die "rev_map_set(@_): ",
3125 "$db => $db_lock ($!)\n";
3127 rename $db, $db_lock or die "rev_map_set(@_): ",
3128 "Failed to rename: ",
3129 "$db => $db_lock ($!)\n";
3132 sysopen(my $fh, $db_lock, O_RDWR
| O_CREAT
)
3133 or croak
"Couldn't open $db_lock: $!\n";
3134 $update_ref eq 'reset' ? _rev_map_reset
($fh, $rev, $commit) :
3135 _rev_map_set
($fh, $rev, $commit);
3137 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3138 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3140 close $fh or croak
$!;
3144 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3145 command_noisy
('update-ref', '-m', "r$rev$note",
3146 $self->refname, $commit);
3148 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
3149 "$db_lock => $db ($!)\n";
3150 delete $LOCKFILES{$db_lock};
3152 $SIG{INT
} = $SIG{HUP
} = $SIG{TERM
} = $SIG{ALRM
} = $SIG{PIPE
} =
3153 $SIG{USR1
} = $SIG{USR2
} = 'DEFAULT';
3154 kill $sig, $$ if defined $sig;
3158 # If want_commit, this will return an array of (rev, commit) where
3159 # commit _must_ be a valid commit in the archive.
3160 # Otherwise, it'll return the max revision (whether or not the
3161 # commit is valid or just a 0x40 placeholder).
3163 my ($self, $want_commit) = @_;
3165 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3166 $want_commit ?
($r, $c) : $r;
3169 sub rev_map_max_norebuild
{
3170 my ($self, $want_commit) = @_;
3171 my $map_path = $self->map_path;
3172 stat $map_path or return $want_commit ?
(0, undef) : 0;
3173 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
3174 binmode $fh or croak
"binmode: $!";
3175 my $size = (stat($fh))[7];
3176 ($size % 24) == 0 or croak
"inconsistent size: $size";
3179 close $fh or croak
"close: $!";
3180 return $want_commit ?
(0, undef) : 0;
3183 sysseek($fh, -24, SEEK_END
) or croak
"seek: $!";
3184 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
3185 my ($r, $c) = unpack(rev_map_fmt
, $buf);
3186 if ($want_commit && $c eq ('0' x40
)) {
3188 return $want_commit ?
(0, undef) : 0;
3190 sysseek($fh, -48, SEEK_END
) or croak
"seek: $!";
3191 sysread($fh, $buf, 24) == 24 or croak
"read: $!";
3192 ($r, $c) = unpack(rev_map_fmt
, $buf);
3193 if ($c eq ('0'x40
)) {
3194 croak
"Penultimate record is all-zeroes in $map_path";
3197 close $fh or croak
"close: $!";
3198 $want_commit ?
($r, $c) : $r;
3202 my ($self, $rev, $uuid) = @_;
3203 my $map_path = $self->map_path($uuid);
3204 return undef unless -e
$map_path;
3206 sysopen(my $fh, $map_path, O_RDONLY
) or croak
"open: $!";
3207 my $c = _rev_map_get
($fh, $rev);
3208 close($fh) or croak
"close: $!";
3213 my ($fh, $rev) = @_;
3215 binmode $fh or croak
"binmode: $!";
3216 my $size = (stat($fh))[7];
3217 ($size % 24) == 0 or croak
"inconsistent size: $size";
3223 my ($l, $u) = (0, $size - 24);
3227 my $i = int(($l/24 + $u/24) / 2) * 24;
3228 sysseek($fh, $i, SEEK_SET
) or croak
"seek: $!";
3229 sysread($fh, my $buf, 24) == 24 or croak
"read: $!";
3230 my ($r, $c) = unpack('NH40', $buf);
3234 } elsif ($r > $rev) {
3236 } else { # $r == $rev
3237 return $c eq ('0' x
40) ?
undef : $c;
3243 # Finds the first svn revision that exists on (if $eq_ok is true) or
3244 # before $rev for the current branch. It will not search any lower
3245 # than $min_rev. Returns the git commit hash and svn revision number
3246 # if found, else (undef, undef).
3247 sub find_rev_before
{
3248 my ($self, $rev, $eq_ok, $min_rev) = @_;
3249 --$rev unless $eq_ok;
3251 my $max_rev = $self->rev_map_max;
3252 $rev = $max_rev if ($rev > $max_rev);
3253 while ($rev >= $min_rev) {
3254 if (my $c = $self->rev_map_get($rev)) {
3259 return (undef, undef);
3262 # Finds the first svn revision that exists on (if $eq_ok is true) or
3263 # after $rev for the current branch. It will not search any higher
3264 # than $max_rev. Returns the git commit hash and svn revision number
3265 # if found, else (undef, undef).
3266 sub find_rev_after
{
3267 my ($self, $rev, $eq_ok, $max_rev) = @_;
3268 ++$rev unless $eq_ok;
3269 $max_rev ||= $self->rev_map_max;
3270 while ($rev <= $max_rev) {
3271 if (my $c = $self->rev_map_get($rev)) {
3276 return (undef, undef);
3280 my ($class, $repo_id, $ref_id, $path) = @_;
3281 unless (defined $repo_id && length $repo_id) {
3282 $repo_id = $Git::SVN
::default_repo_id
;
3284 unless (defined $ref_id && length $ref_id) {
3285 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
3288 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3289 $_[3] = $path = '' unless (defined $path);
3290 mkpath
(["$ENV{GIT_DIR}/svn"]);
3292 ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
3293 path
=> $path, config
=> "$ENV{GIT_DIR}/svn/config",
3294 map_root
=> "$dir/.rev_map", repo_id
=> $repo_id }, $class;
3297 # for read-only access of old .rev_db formats
3298 sub unlink_rev_db_symlink
{
3300 my $link = $self->rev_db_path;
3301 $link =~ s/\.[\w-]+$// or croak
"missing UUID at the end of $link";
3303 unlink $link or croak
"unlink: $link failed!";
3308 my ($self, $uuid) = @_;
3309 my $db_path = $self->map_path($uuid);
3310 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3311 or croak
"map_path: $db_path does not contain '/.rev_map.' !";
3315 # the new replacement for .rev_db
3317 my ($self, $uuid) = @_;
3318 $uuid ||= $self->ra_uuid;
3319 "$self->{map_root}.$uuid";
3324 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3328 sub remove_username
{
3329 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3332 package Git
::SVN
::Prompt
;
3336 use vars qw
/$_no_auth_cache $_username/;
3339 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3340 $may_save = undef if $_no_auth_cache;
3341 $default_username = $_username if defined $_username;
3342 if (defined $default_username && length $default_username) {
3343 if (defined $realm && length $realm) {
3344 print STDERR
"Authentication realm: $realm\n";
3347 $cred->username($default_username);
3349 username
($cred, $realm, $may_save, $pool);
3351 $cred->password(_read_password
("Password for '" .
3352 $cred->username . "': ", $realm));
3353 $cred->may_save($may_save);
3354 $SVN::_Core
::SVN_NO_ERROR
;
3357 sub ssl_server_trust
{
3358 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3359 $may_save = undef if $_no_auth_cache;
3360 print STDERR
"Error validating server certificate for '$realm':\n";
3363 # All variables SVN::Auth::SSL::* are used only once,
3364 # so we're shutting up Perl warnings about this.
3365 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
3366 print STDERR
" - The certificate is not issued ",
3367 "by a trusted authority. Use the\n",
3368 " fingerprint to validate ",
3369 "the certificate manually!\n";
3371 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
3372 print STDERR
" - The certificate hostname ",
3373 "does not match.\n";
3375 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
3376 print STDERR
" - The certificate is not yet valid.\n";
3378 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
3379 print STDERR
" - The certificate has expired.\n";
3381 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
3382 print STDERR
" - The certificate has ",
3383 "an unknown error.\n";
3385 } # no warnings 'once'
3387 "Certificate information:\n".
3388 " - Hostname: %s\n".
3389 " - Valid: from %s until %s\n".
3391 " - Fingerprint: %s\n",
3392 map $cert_info->$_, qw(hostname valid_from valid_until
3393 issuer_dname fingerprint);
3396 print STDERR
$may_save ?
3397 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3398 "(R)eject or accept (t)emporarily? ";
3400 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
3401 if ($choice =~ /^t$/i) {
3402 $cred->may_save(undef);
3403 } elsif ($choice =~ /^r$/i) {
3405 } elsif ($may_save && $choice =~ /^p$/i) {
3406 $cred->may_save($may_save);
3410 $cred->accepted_failures($failures);
3411 $SVN::_Core
::SVN_NO_ERROR
;
3414 sub ssl_client_cert
{
3415 my ($cred, $realm, $may_save, $pool) = @_;
3416 $may_save = undef if $_no_auth_cache;
3417 print STDERR
"Client certificate filename: ";
3419 chomp(my $filename = <STDIN
>);
3420 $cred->cert_file($filename);
3421 $cred->may_save($may_save);
3422 $SVN::_Core
::SVN_NO_ERROR
;
3425 sub ssl_client_cert_pw
{
3426 my ($cred, $realm, $may_save, $pool) = @_;
3427 $may_save = undef if $_no_auth_cache;
3428 $cred->password(_read_password
("Password: ", $realm));
3429 $cred->may_save($may_save);
3430 $SVN::_Core
::SVN_NO_ERROR
;
3434 my ($cred, $realm, $may_save, $pool) = @_;
3435 $may_save = undef if $_no_auth_cache;
3436 if (defined $realm && length $realm) {
3437 print STDERR
"Authentication realm: $realm\n";
3440 if (defined $_username) {
3441 $username = $_username;
3443 print STDERR
"Username: ";
3445 chomp($username = <STDIN
>);
3447 $cred->username($username);
3448 $cred->may_save($may_save);
3449 $SVN::_Core
::SVN_NO_ERROR
;
3452 sub _read_password
{
3453 my ($prompt, $realm) = @_;
3454 print STDERR
$prompt;
3456 require Term
::ReadKey
;
3457 Term
::ReadKey
::ReadMode
('noecho');
3459 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
3460 last if $key =~ /[\012\015]/; # \n\r
3463 Term
::ReadKey
::ReadMode
('restore');
3469 package SVN
::Git
::Fetcher
;
3474 use File
::Temp qw
/tempfile/;
3476 use vars qw
/$_ignore_regex/;
3478 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
3480 my ($class, $git_svn, $switch_path) = @_;
3481 my $self = SVN
::Delta
::Editor
->new;
3482 bless $self, $class;
3483 if (exists $git_svn->{last_commit
}) {
3484 $self->{c
} = $git_svn->{last_commit
};
3485 $self->{empty_symlinks
} =
3486 _mark_empty_symlinks
($git_svn, $switch_path);
3488 $self->{ignore_regex
} = eval { command_oneline
('config', '--get',
3489 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
3490 $self->{empty
} = {};
3491 $self->{dir_prop
} = {};
3492 $self->{file_prop
} = {};
3493 $self->{absent_dir
} = {};
3494 $self->{absent_file
} = {};
3495 $self->{gii
} = $git_svn->tmp_index_do(sub { Git
::IndexInfo
->new });
3499 # this uses the Ra object, so it must be called before do_{switch,update},
3500 # not inside them (when the Git::SVN::Fetcher object is passed) to
3501 # do_{switch,update}
3502 sub _mark_empty_symlinks
{
3503 my ($git_svn, $switch_path) = @_;
3504 my $bool = Git
::config_bool
('svn.brokenSymlinkWorkaround');
3505 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
3508 my ($rev, $cmt) = $git_svn->last_rev_commit;
3509 return {} unless ($rev && $cmt);
3511 # allow the warning to be printed for each revision we fetch to
3512 # ensure the user sees it. The user can also disable the workaround
3513 # on the repository even while git svn is running and the next
3514 # revision fetched will skip this expensive function.
3515 my $printed_warning;
3516 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3517 my ($ls, $ctx) = command_output_pipe
(qw
/ls-tree -r -z/, $cmt);
3519 my $pfx = defined($switch_path) ?
$switch_path : $git_svn->{path
};
3520 $pfx .= '/' if length($pfx);
3523 s/\A100644 blob $empty_blob\t//o or next;
3524 unless ($printed_warning) {
3525 print STDERR
"Scanning for empty symlinks, ",
3526 "this may take a while if you have ",
3527 "many empty files\n",
3528 "You may disable this with `",
3529 "git config svn.brokenSymlinkWorkaround ",
3531 "This may be done in a different ",
3532 "terminal without restarting ",
3534 $printed_warning = 1;
3537 my (undef, $props) =
3538 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3539 if ($props->{'svn:special'}) {
3543 command_close_pipe
($ls, $ctx);
3547 # returns true if a given path is inside a ".git" directory
3549 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3552 # return value: 0 -- don't ignore, 1 -- ignore
3553 sub is_path_ignored
{
3554 my ($self, $path) = @_;
3555 return 1 if in_dot_git
($path);
3556 return 1 if defined($self->{ignore_regex
}) &&
3557 $path =~ m!$self->{ignore_regex}!;
3558 return 0 unless defined($_ignore_regex);
3559 return 1 if $path =~ m!$_ignore_regex!o;
3563 sub set_path_strip
{
3564 my ($self, $path) = @_;
3565 $self->{path_strip
} = qr/^\Q$path\E(\/|$)/ if length $path;
3572 sub open_directory
{
3573 my ($self, $path, $pb, $rev) = @_;
3578 my ($self, $path) = @_;
3579 if ($self->{path_strip
}) {
3580 $path =~ s!$self->{path_strip}!! or
3581 die "Failed to strip path '$path' ($self->{path_strip})\n";
3587 my ($self, $path, $rev, $pb) = @_;
3588 return undef if $self->is_path_ignored($path);
3590 my $gpath = $self->git_path($path);
3591 return undef if ($gpath eq '');
3593 # remove entire directories.
3594 my ($tree) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3595 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3597 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3603 my $rmpath = "$gpath/$_";
3604 $self->{gii
}->remove($rmpath);
3605 print "\tD\t$rmpath\n" unless $::_q
;
3607 print "\tD\t$gpath/\n" unless $::_q
;
3608 command_close_pipe
($ls, $ctx);
3609 $self->{empty
}->{$path} = 0
3611 $self->{gii
}->remove($gpath);
3612 print "\tD\t$gpath\n" unless $::_q
;
3618 my ($self, $path, $pb, $rev) = @_;
3621 goto out
if $self->is_path_ignored($path);
3623 my $gpath = $self->git_path($path);
3624 ($mode, $blob) = (command
('ls-tree', '-z', $self->{c
}, "./$gpath")
3625 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
3626 unless (defined $mode && defined $blob) {
3627 die "$path was not found in commit $self->{c} (r$rev)\n";
3629 if ($mode eq '100644' && $self->{empty_symlinks
}->{$path}) {
3633 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
3634 pool
=> SVN
::Pool
->new, action
=> 'M' };
3638 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3641 if (!$self->is_path_ignored($path)) {
3642 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3643 delete $self->{empty
}->{$dir};
3646 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode,
3647 pool
=> SVN
::Pool
->new, action
=> 'A' };
3651 my ($self, $path, $cp_path, $cp_rev) = @_;
3652 goto out
if $self->is_path_ignored($path);
3653 my $gpath = $self->git_path($path);
3655 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
3661 $self->{gii
}->remove($_);
3662 print "\tD\t$_\n" unless $::_q
;
3664 command_close_pipe
($ls, $ctx);
3665 $self->{empty
}->{$path} = 0;
3667 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
3668 delete $self->{empty
}->{$dir};
3669 $self->{empty
}->{$path} = 1;
3674 sub change_dir_prop
{
3675 my ($self, $db, $prop, $value) = @_;
3676 return undef if $self->is_path_ignored($db->{path
});
3677 $self->{dir_prop
}->{$db->{path
}} ||= {};
3678 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
3682 sub absent_directory
{
3683 my ($self, $path, $pb) = @_;
3684 return undef if $self->is_path_ignored($path);
3685 $self->{absent_dir
}->{$pb->{path
}} ||= [];
3686 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
3691 my ($self, $path, $pb) = @_;
3692 return undef if $self->is_path_ignored($path);
3693 $self->{absent_file
}->{$pb->{path
}} ||= [];
3694 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
3698 sub change_file_prop
{
3699 my ($self, $fb, $prop, $value) = @_;
3700 return undef if $self->is_path_ignored($fb->{path
});
3701 if ($prop eq 'svn:executable') {
3702 if ($fb->{mode_b
} != 120000) {
3703 $fb->{mode_b
} = defined $value ?
100755 : 100644;
3705 } elsif ($prop eq 'svn:special') {
3706 $fb->{mode_b
} = defined $value ?
120000 : 100644;
3708 $self->{file_prop
}->{$fb->{path
}} ||= {};
3709 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
3714 sub apply_textdelta
{
3715 my ($self, $fb, $exp) = @_;
3716 return undef if $self->is_path_ignored($fb->{path
});
3717 my $fh = $::_repository
->temp_acquire('svn_delta');
3718 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3719 # (but $base does not,) so dup() it for reading in close_file
3720 open my $dup, '<&', $fh or croak
$!;
3721 my $base = $::_repository
->temp_acquire('git_blob');
3724 my ($base_is_link, $size);
3726 if ($fb->{mode_a
} eq '120000' &&
3727 ! $self->{empty_symlinks
}->{$fb->{path
}}) {
3728 print $base 'link ' or die "print $!\n";
3732 $size = $::_repository
->cat_blob($fb->{blob
}, $base);
3733 die "Failed to read object $fb->{blob}" if ($size < 0);
3736 seek $base, 0, 0 or croak
$!;
3737 my $got = ::md5sum
($base);
3739 my $err = "Checksum mismatch: ".
3740 "$fb->{path} $fb->{blob}\n" .
3741 "expected: $exp\n" .
3743 if ($base_is_link) {
3745 "Retrying... (possibly ",
3746 "a bad symlink from SVN)\n";
3747 $::_repository
->temp_reset($base);
3755 seek $base, 0, 0 or croak
$!;
3757 $fb->{base
} = $base;
3758 [ SVN
::TxDelta
::apply
($base, $dup, undef, $fb->{path
}, $fb->{pool
}) ];
3762 my ($self, $fb, $exp) = @_;
3763 return undef if $self->is_path_ignored($fb->{path
});
3766 my $path = $self->git_path($fb->{path
});
3767 if (my $fh = $fb->{fh
}) {
3769 seek($fh, 0, 0) or croak
$!;
3770 my $got = ::md5sum
($fh);
3772 die "Checksum mismatch: $path\n",
3773 "expected: $exp\n got: $got\n";
3776 if ($fb->{mode_b
} == 120000) {
3777 sysseek($fh, 0, 0) or croak
$!;
3778 my $rd = sysread($fh, my $buf, 5);
3781 croak
"sysread: $!\n";
3782 } elsif ($rd == 0) {
3783 warn "$path has mode 120000",
3784 " but it points to nothing\n",
3785 "converting to an empty file with mode",
3787 $fb->{mode_b
} = '100644';
3788 } elsif ($buf ne 'link ') {
3789 warn "$path has mode 120000",
3790 " but is not a link\n";
3792 my $tmp_fh = $::_repository
->temp_acquire(
3795 while ($res = sysread($fh, my $str, 1024)) {
3796 my $out = syswrite($tmp_fh, $str, $res);
3797 defined($out) && $out == $res
3799 Git
::temp_path
($tmp_fh),
3802 defined $res or croak
$!;
3804 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3805 Git
::temp_release
($tmp_fh, 1);
3809 $hash = $::_repository
->hash_and_insert_object(
3810 Git
::temp_path
($fh));
3811 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3813 Git
::temp_release
($fb->{base
}, 1);
3814 Git
::temp_release
($fh, 1);
3816 $hash = $fb->{blob
} or die "no blob information\n";
3819 $self->{gii
}->update($fb->{mode_b
}, $hash, $path) or croak
$!;
3820 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $::_q
;
3826 $self->{nr
} = $self->{gii
}->{nr
};
3827 delete $self->{gii
};
3828 $self->SUPER::abort_edit
(@_);
3833 $self->{git_commit_ok
} = 1;
3834 $self->{nr
} = $self->{gii
}->{nr
};
3835 delete $self->{gii
};
3836 $self->SUPER::close_edit
(@_);
3839 package SVN
::Git
::Editor
;
3840 use vars qw
/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
3847 my ($class, $opts) = @_;
3848 foreach (qw
/svn_path r ra tree_a tree_b log editor_cb/) {
3849 die "$_ required!\n" unless (defined $opts->{$_});
3852 my $pool = SVN
::Pool
->new;
3853 my $mods = generate_diff
($opts->{tree_a
}, $opts->{tree_b
});
3854 my $types = check_diff_paths
($opts->{ra
}, $opts->{svn_path
},
3857 # $opts->{ra} functions should not be used after this:
3858 my @ce = $opts->{ra
}->get_commit_editor($opts->{log},
3859 $opts->{editor_cb
}, $pool);
3860 my $self = SVN
::Delta
::Editor
->new(@ce, $pool);
3861 bless $self, $class;
3862 foreach (qw
/svn_path r tree_a tree_b/) {
3863 $self->{$_} = $opts->{$_};
3865 $self->{url
} = $opts->{ra
}->{url
};
3866 $self->{mods
} = $mods;
3867 $self->{types
} = $types;
3868 $self->{pool
} = $pool;
3869 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3871 $self->{path_prefix
} = length $self->{svn_path
} ?
3872 "$self->{svn_path}/" : '';
3873 $self->{config
} = $opts->{config
};
3878 my ($tree_a, $tree_b) = @_;
3879 my @diff_tree = qw(diff-tree -z -r);
3880 if ($_cp_similarity) {
3881 push @diff_tree, "-C$_cp_similarity";
3883 push @diff_tree, '-C';
3885 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3886 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
3887 push @diff_tree, $tree_a, $tree_b;
3888 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
3892 while (<$diff_fh>) {
3893 chomp $_; # this gets rid of the trailing "\0"
3894 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
3895 ($::sha1
)\s
($::sha1
)\s
3896 ([MTCRAD
])\d
*$/xo
) {
3897 push @mods, { mode_a
=> $1, mode_b
=> $2,
3898 sha1_a
=> $3, sha1_b
=> $4,
3900 if ($5 =~ /^(?:C|R)$/) {
3905 } elsif ($state eq 'file_a') {
3906 my $x = $mods[$#mods] or croak
"Empty array\n";
3907 if ($x->{chg
} !~ /^(?:C|R)$/) {
3908 croak
"Error parsing $_, $x->{chg}\n";
3912 } elsif ($state eq 'file_b') {
3913 my $x = $mods[$#mods] or croak
"Empty array\n";
3914 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
3915 croak
"Error parsing $_, $x->{chg}\n";
3917 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
3918 croak
"Error parsing $_, $x->{chg}\n";
3923 croak
"Error parsing $_\n";
3926 command_close_pipe
($diff_fh, $ctx);
3930 sub check_diff_paths
{
3931 my ($ra, $pfx, $rev, $mods) = @_;
3933 $pfx .= '/' if length $pfx;
3935 sub type_diff_paths
{
3936 my ($ra, $types, $path, $rev) = @_;
3937 my @p = split m
#/+#, $path;
3939 unless (defined $types->{$c}) {
3940 $types->{$c} = $ra->check_path($c, $rev);
3943 $c .= '/' . shift @p;
3944 next if defined $types->{$c};
3945 $types->{$c} = $ra->check_path($c, $rev);
3949 foreach my $m (@
$mods) {
3950 foreach my $f (qw
/file_a file_b/) {
3951 next unless defined $m->{$f};
3952 my ($dir) = ($m->{$f} =~ m
#^(.*?)/?(?:[^/]+)$#);
3953 if (length $pfx.$dir && ! defined $types{$dir}) {
3954 type_diff_paths
($ra, \
%types, $pfx.$dir, $rev);
3962 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3966 my ($self, $path) = @_;
3967 $self->{path_prefix
}.(defined $path ?
$path : '');
3971 my ($self, $path) = @_;
3972 if ($self->{url
} =~ m
#^https?://#) {
3973 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3975 $self->{url
} . '/' . $self->repo_path($path);
3980 my $rm = $self->{rm
};
3981 delete $rm->{''}; # we never delete the url we're tracking
3984 foreach (keys %$rm) {
3985 my @d = split m
#/#, $_;
3989 $c .= '/' . shift @d;
3993 delete $rm->{$self->{svn_path
}};
3994 delete $rm->{''}; # we never delete the url we're tracking
3997 my ($fh, $ctx) = command_output_pipe
(qw
/ls-tree --name-only -r -z/,
4002 my @dn = split m
#/#, $_;
4004 delete $rm->{join '/', @dn};
4011 command_close_pipe
($fh, $ctx);
4013 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
4014 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4015 $self->close_directory($bat->{$d}, $p);
4016 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
4017 print "\tD+\t$d/\n" unless $::_q
;
4018 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
4023 sub open_or_add_dir
{
4024 my ($self, $full_path, $baton) = @_;
4025 my $t = $self->{types
}->{$full_path};
4027 die "$full_path not known in r$self->{r} or we have a bug!\n";
4031 # SVN::Node::none and SVN::Node::file are used only once,
4032 # so we're shutting up Perl's warnings about them.
4033 if ($t == $SVN::Node
::none
) {
4034 return $self->add_directory($full_path, $baton,
4035 undef, -1, $self->{pool
});
4036 } elsif ($t == $SVN::Node
::dir
) {
4037 return $self->open_directory($full_path, $baton,
4038 $self->{r
}, $self->{pool
});
4039 } # no warnings 'once'
4040 print STDERR
"$full_path already exists in repository at ",
4041 "r$self->{r} and it is not a directory (",
4042 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
4043 } # no warnings 'once'
4048 my ($self, $path) = @_;
4049 my $bat = $self->{bat
};
4050 my $repo_path = $self->repo_path($path);
4051 return $bat->{''} unless (length $repo_path);
4052 my @p = split m
#/+#, $repo_path;
4054 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4057 $c .= '/' . shift @p;
4058 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4063 # Subroutine to convert a globbing pattern to a regular expression.
4064 # From perl cookbook.
4066 my $globstr = shift;
4067 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4068 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4069 return '^' . $globstr . '$';
4072 sub check_autoprop
{
4073 my ($self, $pattern, $properties, $file, $fbat) = @_;
4074 # Convert the globbing pattern to a regular expression.
4075 my $regex = glob2pat
($pattern);
4076 # Check if the pattern matches the file name.
4077 if($file =~ m/($regex)/) {
4078 # Parse the list of properties to set.
4079 my @props = split(/;/, $properties);
4080 foreach my $prop (@props) {
4081 # Parse 'name=value' syntax and set the property.
4082 if ($prop =~ /([^=]+)=(.*)/) {
4083 my ($n,$v) = ($1,$2);
4087 $self->change_file_prop($fbat, $n, $v);
4093 sub apply_autoprops
{
4094 my ($self, $file, $fbat) = @_;
4095 my $conf_t = ${$self->{config
}}{'config'};
4097 # Check [miscellany]/enable-auto-props in svn configuration.
4098 if (SVN
::_Core
::svn_config_get_bool
(
4100 $SVN::_Core
::SVN_CONFIG_SECTION_MISCELLANY
,
4101 $SVN::_Core
::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS
,
4103 # Auto-props are enabled. Enumerate them to look for matches.
4104 my $callback = sub {
4105 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4107 SVN
::_Core
::svn_config_enumerate
(
4109 $SVN::_Core
::SVN_CONFIG_SECTION_AUTO_PROPS
,
4115 my ($self, $m) = @_;
4116 my ($dir, $file) = split_path
($m->{file_b
});
4117 my $pbat = $self->ensure_path($dir);
4118 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4120 print "\tA\t$m->{file_b}\n" unless $::_q
;
4121 $self->apply_autoprops($file, $fbat);
4122 $self->chg_file($fbat, $m);
4123 $self->close_file($fbat,undef,$self->{pool
});
4127 my ($self, $m) = @_;
4128 my ($dir, $file) = split_path
($m->{file_b
});
4129 my $pbat = $self->ensure_path($dir);
4130 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4131 $self->url_path($m->{file_a
}), $self->{r
});
4132 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
4133 $self->chg_file($fbat, $m);
4134 $self->close_file($fbat,undef,$self->{pool
});
4138 my ($self, $path, $pbat) = @_;
4139 my $rpath = $self->repo_path($path);
4140 my ($dir, $file) = split_path
($rpath);
4141 $self->{rm
}->{$dir} = 1;
4142 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
4146 my ($self, $m) = @_;
4147 my ($dir, $file) = split_path
($m->{file_b
});
4148 my $pbat = $self->ensure_path($dir);
4149 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
4150 $self->url_path($m->{file_a
}), $self->{r
});
4151 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
4152 $self->apply_autoprops($file, $fbat);
4153 $self->chg_file($fbat, $m);
4154 $self->close_file($fbat,undef,$self->{pool
});
4156 ($dir, $file) = split_path
($m->{file_a
});
4157 $pbat = $self->ensure_path($dir);
4158 $self->delete_entry($m->{file_a
}, $pbat);
4162 my ($self, $m) = @_;
4163 my ($dir, $file) = split_path
($m->{file_b
});
4164 my $pbat = $self->ensure_path($dir);
4165 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
4166 $pbat,$self->{r
},$self->{pool
});
4167 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
4168 $self->chg_file($fbat, $m);
4169 $self->close_file($fbat,undef,$self->{pool
});
4172 sub T
{ shift->M(@_) }
4174 sub change_file_prop
{
4175 my ($self, $fbat, $pname, $pval) = @_;
4176 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
4179 sub _chg_file_get_blob
($$$$) {
4180 my ($self, $fbat, $m, $which) = @_;
4181 my $fh = $::_repository
->temp_acquire("git_blob_$which");
4182 if ($m->{"mode_$which"} =~ /^120/) {
4183 print $fh 'link ' or croak
$!;
4184 $self->change_file_prop($fbat,'svn:special','*');
4185 } elsif ($m->{mode_a
} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4186 $self->change_file_prop($fbat,'svn:special',undef);
4188 my $blob = $m->{"sha1_$which"};
4189 return ($fh,) if ($blob =~ /^0{40}$/);
4190 my $size = $::_repository
->cat_blob($blob, $fh);
4191 croak
"Failed to read object $blob" if ($size < 0);
4192 $fh->flush == 0 or croak
$!;
4193 seek $fh, 0, 0 or croak
$!;
4195 my $exp = ::md5sum
($fh);
4196 seek $fh, 0, 0 or croak
$!;
4201 my ($self, $fbat, $m) = @_;
4202 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
4203 $self->change_file_prop($fbat,'svn:executable','*');
4204 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
4205 $self->change_file_prop($fbat,'svn:executable',undef);
4207 my ($fh_a, $exp_a) = _chg_file_get_blob
$self, $fbat, $m, 'a';
4208 my ($fh_b, $exp_b) = _chg_file_get_blob
$self, $fbat, $m, 'b';
4209 my $pool = SVN
::Pool
->new;
4210 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4212 my $txstream = SVN
::TxDelta
::new
($fh_a, $fh_b, $pool);
4213 my $res = SVN
::TxDelta
::send_txstream
($txstream, @
$atd, $pool);
4215 die "Unexpected result from send_txstream: $res\n",
4216 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4219 my $got = SVN
::TxDelta
::send_stream
($fh_b, @
$atd, $pool);
4220 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4221 if ($got ne $exp_b);
4223 Git
::temp_release
($fh_b, 1);
4224 Git
::temp_release
($fh_a, 1);
4229 my ($self, $m) = @_;
4230 my ($dir, $file) = split_path
($m->{file_b
});
4231 my $pbat = $self->ensure_path($dir);
4232 print "\tD\t$m->{file_b}\n" unless $::_q
;
4233 $self->delete_entry($m->{file_b
}, $pbat);
4238 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
4239 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
4241 $self->close_directory($bat->{$_}, $p);
4243 $self->close_directory($bat->{''}, $p);
4244 $self->SUPER::close_edit
($p);
4250 $self->SUPER::abort_edit
($self->{pool
});
4255 $self->SUPER::DESTROY
(@_);
4256 $self->{pool
}->clear;
4259 # this drives the editor
4262 my $mods = $self->{mods
};
4263 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
4264 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
4266 if (defined $o{$f}) {
4269 fatal
("Invalid change type: $f");
4272 $self->rmdirs if $_rmdir;
4278 return scalar @
$mods;
4281 package Git
::SVN
::Ra
;
4282 use vars qw
/@ISA $config_dir $_log_window_size/;
4285 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
4288 # enforce temporary pool usage for some simple functions
4290 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
4292 my $SUPER = "SUPER::$f";
4295 my $pool = SVN
::Pool
->new;
4296 my @ret = $self->$SUPER(@_,$pool);
4298 wantarray ?
@ret : $ret[0];
4303 sub _auth_providers
() {
4305 SVN
::Client
::get_simple_provider
(),
4306 SVN
::Client
::get_ssl_server_trust_file_provider
(),
4307 SVN
::Client
::get_simple_prompt_provider
(
4308 \
&Git
::SVN
::Prompt
::simple
, 2),
4309 SVN
::Client
::get_ssl_client_cert_file_provider
(),
4310 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
4311 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
4312 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
4313 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
4314 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
4315 SVN
::Client
::get_username_provider
(),
4316 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
4317 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
4318 SVN
::Client
::get_username_prompt_provider
(
4319 \
&Git
::SVN
::Prompt
::username
, 2)
4323 sub escape_uri_only
{
4326 foreach (split m{/}, $uri) {
4327 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
4335 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
4336 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
4337 $url = "$scheme://$domain$uri";
4343 my ($class, $url) = @_;
4345 return $RA if ($RA && $RA->{url
} eq $url);
4347 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
4348 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
4349 my $config = SVN
::Core
::config_get_config
($config_dir);
4351 my $dont_store_passwords = 1;
4352 my $conf_t = ${$config}{'config'};
4355 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4356 # produces warnings that variables are used only once.
4357 # I had not found the better way to shut them up, so
4358 # the warnings of type 'once' are disabled in this block.
4359 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4360 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4361 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
4363 SVN
::_Core
::svn_auth_set_parameter
($baton,
4364 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
4365 bless (\
$dont_store_passwords, "_p_void"));
4367 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
4368 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
4369 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
4371 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
4373 } # no warnings 'once'
4374 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
4376 pool
=> SVN
::Pool
->new,
4377 auth_provider_callbacks
=> $callbacks);
4378 $self->{url
} = $url;
4379 $self->{svn_path
} = $url;
4380 $self->{repos_root
} = $self->get_repos_root;
4381 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
4382 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
4383 get_dir
=> { r
=> 0, data
=> {} } };
4384 $RA = bless $self, $class;
4388 my ($self, $path, $r) = @_;
4389 my $cache = $self->{cache
}->{check_path
};
4390 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
4391 return $cache->{data
}->{$path};
4393 my $pool = SVN
::Pool
->new;
4394 my $t = $self->SUPER::check_path
($path, $r, $pool);
4396 if ($r != $cache->{r
}) {
4397 %{$cache->{data
}} = ();
4400 $cache->{data
}->{$path} = $t;
4404 my ($self, $dir, $r) = @_;
4405 my $cache = $self->{cache
}->{get_dir
};
4406 if ($r == $cache->{r
}) {
4407 if (my $x = $cache->{data
}->{$dir}) {
4408 return wantarray ? @
$x : $x->[0];
4411 my $pool = SVN
::Pool
->new;
4412 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
4413 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
4415 if ($r != $cache->{r
}) {
4416 %{$cache->{data
}} = ();
4419 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
4420 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
4424 # do not call the real DESTROY since we store ourselves in $RA
4427 # get_log(paths, start, end, limit,
4428 # discover_changed_paths, strict_node_history, receiver)
4430 my ($self, @args) = @_;
4431 my $pool = SVN
::Pool
->new;
4433 # svn_log_changed_path_t objects passed to get_log are likely to be
4434 # overwritten even if only the refs are copied to an external variable,
4435 # so we should dup the structures in their entirety. Using an
4436 # externally passed pool (instead of our temporary and quickly cleared
4437 # pool in Git::SVN::Ra) does not help matters at all...
4438 my $receiver = pop @args;
4440 my ($paths) = $_[0];
4441 return &$receiver(@_) unless $paths;
4443 foreach my $p (keys %$paths) {
4444 my $i = $paths->{$p};
4445 my %s = map { $_ => $i->$_ }
4446 qw
/copyfrom_path copyfrom_rev action/;
4453 # the limit parameter was not supported in SVN 1.1.x, so we
4454 # drop it. Therefore, the receiver callback passed to it
4455 # is made aware of this limitation by being wrapped if
4456 # the limit passed to is being wrapped.
4457 if ($SVN::Core
::VERSION
le '1.2.0') {
4458 my $limit = splice(@args, 3, 1);
4460 my $receiver = pop @args;
4461 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4464 my $ret = $self->SUPER::get_log
(@args, $pool);
4470 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4471 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
4472 my $out = IO
::File
->new_tmpfile;
4474 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4475 # $ctx->diff(), so we'll create a default one
4476 my $pool = SVN
::Pool
->new_default_sub;
4478 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4479 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4481 my $ret = (($out->stat)[7] == 0);
4482 close $out or croak
$!;
4487 sub get_commit_editor
{
4488 my ($self, $log, $cb, $pool) = @_;
4489 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
4490 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
4494 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4495 my $new = ($rev_a == $rev_b);
4496 my $path = $gs->{path
};
4498 if ($new && -e
$gs->{index}) {
4499 unlink $gs->{index} or die
4500 "Couldn't unlink index: $gs->{index}: $!\n";
4502 my $pool = SVN
::Pool
->new;
4503 $editor->set_path_strip($path);
4504 my (@pc) = split m
#/#, $path;
4505 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
4507 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4509 # Since we can't rely on svn_ra_reparent being available, we'll
4510 # just have to do some magic with set_path to make it so
4511 # we only want a partial path.
4513 my $final = join('/', @pc);
4515 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4516 $sp .= '/' if length $sp;
4519 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4521 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4523 $reporter->finish_report($pool);
4525 $editor->{git_commit_ok
};
4528 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4529 # svn_ra_reparent didn't work before 1.4)
4531 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4532 my $path = $gs->{path
};
4533 my $pool = SVN
::Pool
->new;
4535 my $full_url = $self->{url
};
4536 my $old_url = $full_url;
4537 $full_url .= '/' . escape_uri_only
($path) if length $path;
4538 my ($ra, $reparented);
4540 if ($old_url =~ m
#^svn(\+ssh)?://#) {
4544 $ra = Git
::SVN
::Ra
->new($full_url);
4546 } elsif ($old_url ne $full_url) {
4547 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
4548 $self->{url
} = $full_url;
4553 $url_b = escape_url
($url_b);
4554 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
4555 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
4556 $reporter->set_path('', $rev_a, 0, @lock, $pool);
4557 $reporter->finish_report($pool);
4560 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
4561 $self->{url
} = $old_url;
4565 $editor->{git_commit_ok
};
4568 sub longest_common_path
{
4569 my ($gsv, $globs) = @_;
4571 my $common_max = scalar @
$gsv;
4573 foreach my $gs (@
$gsv) {
4574 my @tmp = split m
#/#, $gs->{path};
4577 $p .= length($p) ?
"/$_" : $_;
4583 $common_max += scalar @
$globs;
4584 foreach my $glob (@
$globs) {
4585 my @tmp = split m
#/#, $glob->{path}->{left};
4588 $p .= length($p) ?
"/$_" : $_;
4594 my $longest_path = '';
4595 foreach (sort {length $b <=> length $a} keys %common) {
4596 if ($common{$_} == $common_max) {
4604 sub gs_fetch_loop_common
{
4605 my ($self, $base, $head, $gsv, $globs) = @_;
4606 return if ($base > $head);
4607 my $inc = $_log_window_size;
4608 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
4609 my $longest_path = longest_common_path
($gsv, $globs);
4610 my $ra_url = $self->{url
};
4611 my $find_trailing_edge;
4615 my $err_handler = $SVN::Error
::handler
;
4616 $SVN::Error
::handler
= sub {
4618 skip_unknown_revs
($err);
4621 my ($paths, $r, $author, $date, $log) = @_;
4623 { author
=> $author, date
=> $date, log => $log } ];
4625 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4626 sub { $revs{$_[1]} = _cb
(@_) });
4628 print "Checked through r$max\r";
4630 $find_trailing_edge = 1;
4632 if ($err and $find_trailing_edge) {
4633 print STDERR
"Path '$longest_path' ",
4634 "was probably deleted:\n",
4635 $err->expanded_message,
4636 "\nWill attempt to follow ",
4637 "revisions r$min .. r$max ",
4638 "committed before the deletion\n";
4640 while (--$hi >= $min) {
4642 $self->get_log([$longest_path], $min, $hi,
4645 $revs{$_[1]} = _cb
(@_) });
4647 print STDERR
"r$min .. r$ok OK\n";
4651 $find_trailing_edge = 0;
4653 $SVN::Error
::handler
= $err_handler;
4655 my %exists = map { $_->{path
} => $_ } @
$gsv;
4656 foreach my $r (sort {$a <=> $b} keys %revs) {
4657 my ($paths, $logged) = @
{$revs{$r}};
4659 foreach my $gs ($self->match_globs(\
%exists, $paths,
4661 if ($gs->rev_map_max >= $r) {
4664 next unless $gs->match_paths($paths, $r);
4665 $gs->{logged_rev_props
} = $logged;
4666 if (my $last_commit = $gs->last_commit) {
4667 $gs->assert_index_clean($last_commit);
4669 my $log_entry = $gs->do_fetch($paths, $r);
4671 $gs->do_git_commit($log_entry);
4673 $INDEX_FILES{$gs->{index}} = 1;
4675 foreach my $g (@
$globs) {
4676 my $k = "svn-remote.$g->{remote}." .
4678 Git
::SVN
::tmp_config
($k, $r);
4684 $self = Git
::SVN
::Ra
->new($ra_url);
4685 $ra_invalid = undef;
4688 # pre-fill the .rev_db since it'll eventually get filled in
4689 # with '0' x40 if something new gets committed
4690 foreach my $gs (@
$gsv) {
4691 next if $gs->rev_map_max >= $max;
4692 next if defined $gs->rev_map_get($max);
4693 $gs->rev_map_set($max, 0 x40
);
4695 foreach my $g (@
$globs) {
4696 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4697 Git
::SVN
::tmp_config
($k, $max);
4699 last if $max >= $head;
4702 $max = $head if ($max > $head);
4707 sub get_dir_globbed
{
4708 my ($self, $left, $depth, $r) = @_;
4710 my @x = eval { $self->get_dir($left, $r) };
4711 return unless scalar @x == 3;
4712 my $dirents = $x[0];
4714 foreach my $de (keys %$dirents) {
4715 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
4717 my @args = ("$left/$de", $depth - 1, $r);
4718 foreach my $dir ($self->get_dir_globbed(@args)) {
4719 push @finalents, "$de/$dir";
4722 push @finalents, $de;
4729 my ($self, $exists, $paths, $globs, $r) = @_;
4732 my ($self, $exists, $g, $r) = @_;
4734 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
4735 $g->{path
}->{depth
},
4738 foreach my $de (@dirs) {
4739 my $p = $g->{path
}->full_path($de);
4740 next if $exists->{$p};
4741 next if (length $g->{path
}->{right
} &&
4742 ($self->check_path($p, $r) !=
4744 $exists->{$p} = Git
::SVN
->init($self->{url
}, $p, undef,
4745 $g->{ref}->full_path($de), 1);
4748 foreach my $g (@
$globs) {
4749 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4750 if ($path->{action
} =~ /^[AR]$/) {
4751 get_dir_check
($self, $exists, $g, $r);
4754 foreach (keys %$paths) {
4755 if (/$g->{path}->{left_regex}/ &&
4756 !/$g->{path}->{regex}/) {
4757 next if $paths->{$_}->{action
} !~ /^[AR]$/;
4758 get_dir_check
($self, $exists, $g, $r);
4760 next unless /$g->{path}->{regex}/;
4762 my $pathname = $g->{path
}->full_path($p);
4763 next if $exists->{$pathname};
4764 next if ($self->check_path($pathname, $r) !=
4766 $exists->{$pathname} = Git
::SVN
->init(
4767 $self->{url
}, $pathname, undef,
4768 $g->{ref}->full_path($p), 1);
4771 foreach (split m
#/#, $g->{path}->{left}) {
4773 next unless ($paths->{$c} &&
4774 ($paths->{$c}->{action
} =~ /^[AR]$/));
4775 get_dir_check
($self, $exists, $g, $r);
4783 return $self->{url
} if ($self->{url
} eq $self->{repos_root
});
4784 my $url = $self->{repos_root
};
4785 my @components = split(m!/!, $self->{svn_path
});
4788 $url .= "/$c" if length $c;
4789 eval { (ref $self)->new($url)->get_latest_revnum };
4790 } while ($@
&& ($c = shift @components));
4796 unless (defined $can_do_switch) {
4797 my $pool = SVN
::Pool
->new;
4799 $self->do_switch(1, '', 0, $self->{url
},
4800 SVN
::Delta
::Editor
->new, $pool);
4805 $rep->abort_report($pool);
4813 sub skip_unknown_revs
{
4815 my $errno = $err->apr_err();
4816 # Maybe the branch we're tracking didn't
4817 # exist when the repo started, so it's
4818 # not an error if it doesn't, just continue
4820 # Wonderfully consistent library, eh?
4821 # 160013 - svn:// and file://
4822 # 175002 - http(s)://
4823 # 175007 - http(s):// (this repo required authorization, too...)
4824 # More codes may be discovered later...
4825 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
4826 my $err_key = $err->expanded_message;
4827 # revision numbers change every time, filter them out
4828 $err_key =~ s/\d+/\0/g;
4829 $err_key = "$errno\0$err_key";
4830 unless ($ignored_err{$err_key}) {
4831 warn "W: Ignoring error from SVN, path probably ",
4832 "does not exist: ($errno): ",
4833 $err->expanded_message,"\n";
4834 warn "W: Do not be alarmed at the above message ",
4835 "git-svn is just searching aggressively for ",
4837 "This may take a while on large repositories\n";
4838 $ignored_err{$err_key} = 1;
4842 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4845 package Git
::SVN
::Log
;
4848 use POSIX qw
/strftime/;
4850 use constant commit_log_separator
=> ('-' x
72) . "\n";
4851 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
4852 %rusers $show_commit $incremental/;
4857 return 1 if defined $c->{r
};
4859 # big commit message got truncated by the 16k pretty buffer in rev-list
4860 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
4861 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
4863 my @log = command
(qw
/cat-file commit/, $c->{c
});
4865 # shift off the headers
4866 shift @log while ($log[0] ne '');
4869 # TODO: make $c->{l} not have a trailing newline in the future
4870 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
4872 (undef, $c->{r
}, undef) = ::extract_metadata
(
4873 (grep(/^git-svn-id: /, @log))[-1]);
4875 return defined $c->{r
};
4879 return $color || Git
->repository->get_colorbool('color.diff');
4882 sub git_svn_log_cmd
{
4883 my ($r_min, $r_max, @args) = @_;
4885 my (@files, @log_opts);
4886 foreach my $x (@args) {
4887 if ($x eq '--' || @files) {
4890 if (::verify_ref
("$x^0")) {
4898 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
4899 $gs ||= Git
::SVN
->_new;
4900 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
4902 push @cmd, '-r' unless $non_recursive;
4903 push @cmd, qw
/--raw --name-status/ if $verbose;
4904 push @cmd, '--color' if log_use_color
();
4905 push @cmd, @log_opts;
4906 if (defined $r_max && $r_max == $r_min) {
4907 push @cmd, '--max-count=1';
4908 if (my $c = $gs->rev_map_get($r_max)) {
4911 } elsif (defined $r_max) {
4912 if ($r_max < $r_min) {
4913 ($r_min, $r_max) = ($r_max, $r_min);
4915 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4916 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4917 # If there are no commits in the range, both $c_max and $c_min
4918 # will be undefined. If there is at least 1 commit in the
4919 # range, both will be defined.
4920 return () if !defined $c_min || !defined $c_max;
4921 if ($c_min eq $c_max) {
4922 push @cmd, '--max-count=1', $c_min;
4924 push @cmd, '--boundary', "$c_min..$c_max";
4927 return (@cmd, @files);
4930 # adapted from pager.c
4932 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
4933 if (!defined $pager) {
4935 } elsif (length $pager == 0 || $pager eq 'cat') {
4938 $ENV{GIT_PAGER_IN_USE
} = defined($pager);
4942 return unless -t
*STDOUT
&& defined $pager;
4943 pipe my ($rfd, $wfd) or return;
4944 defined(my $pid = fork) or ::fatal
"Can't fork: $!";
4946 open STDOUT
, '>&', $wfd or
4947 ::fatal
"Can't redirect to stdout: $!";
4950 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!";
4951 $ENV{LESS
} ||= 'FRSX';
4952 exec $pager or ::fatal
"Can't run pager: $! ($pager)";
4955 sub format_svn_date
{
4956 # some systmes don't handle or mishandle %z, so be creative.
4957 my $t = shift || time;
4958 my $gm = timelocal
(gmtime($t));
4959 my $sign = qw( + + - )[ $t <=> $gm ];
4960 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
4961 return strftime
("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
4964 sub parse_git_date
{
4966 # Date::Parse isn't in the standard Perl distro :(
4967 if ($tz =~ s/^\+//) {
4968 $t += tz_to_s_offset
($tz);
4969 } elsif ($tz =~ s/^\-//) {
4970 $t -= tz_to_s_offset
($tz);
4975 sub set_local_timezone
{
4983 sub tz_to_s_offset
{
4986 return ($1 * 60) + ($tz * 3600);
4989 sub get_author_info
{
4990 my ($dest, $author, $t, $tz) = @_;
4991 $author =~ s/(?:^\s*|\s*$)//g;
4992 $dest->{a_raw
} = $author;
4995 $au = $rusers{$author} || undef;
4998 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5003 $dest->{t_utc
} = parse_git_date
($t, $tz);
5006 sub process_commit
{
5007 my ($c, $r_min, $r_max, $defer) = @_;
5008 if (defined $r_min && defined $r_max) {
5009 if ($r_min == $c->{r
} && $r_min == $r_max) {
5013 return 1 if $r_min == $r_max;
5014 if ($r_min < $r_max) {
5015 # we need to reverse the print order
5016 return 0 if (defined $limit && --$limit < 0);
5020 if ($r_min != $r_max) {
5021 return 1 if ($r_min < $c->{r
});
5022 return 1 if ($r_max > $c->{r
});
5025 return 0 if (defined $limit && --$limit < 0);
5034 if (my $l = $c->{l
}) {
5035 while ($l->[0] =~ /^\s*$/) { shift @
$l }
5038 $l_fmt ||= 'A' . length($c->{r
});
5039 print 'r',pack($l_fmt, $c->{r
}),' | ';
5040 print "$c->{c} | " if $show_commit;
5043 show_commit_normal
($c);
5047 sub show_commit_changed_paths
{
5049 return unless $c->{changed
};
5050 print "Changed paths:\n", @
{$c->{changed
}};
5053 sub show_commit_normal
{
5055 print commit_log_separator
, "r$c->{r} | ";
5056 print "$c->{c} | " if $show_commit;
5057 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
5060 if (my $l = $c->{l
}) {
5061 while ($l->[$#$l] eq "\n" && $#$l > 0
5062 && $l->[($#$l - 1)] eq "\n") {
5065 $nr_line = scalar @
$l;
5067 print "1 line\n\n\n";
5069 if ($nr_line == 1) {
5070 $nr_line = '1 line';
5072 $nr_line .= ' lines';
5074 print $nr_line, "\n";
5075 show_commit_changed_paths
($c);
5077 print $_ foreach @
$l;
5081 show_commit_changed_paths
($c);
5085 foreach my $x (qw
/raw stat diff/) {
5088 print $_ foreach @
{$c->{$x}}
5095 my ($r_min, $r_max);
5096 my $r_last = -1; # prevent dupes
5097 set_local_timezone
();
5098 if (defined $::_revision
) {
5099 if ($::_revision
=~ /^(\d+):(\d+)$/) {
5100 ($r_min, $r_max) = ($1, $2);
5101 } elsif ($::_revision
=~ /^\d+$/) {
5102 $r_min = $r_max = $::_revision
;
5104 ::fatal
"-r$::_revision is not supported, use ",
5105 "standard 'git log' arguments instead";
5110 @args = git_svn_log_cmd
($r_min, $r_max, @args);
5112 print commit_log_separator
unless $incremental || $oneline;
5115 my $log = command_output_pipe
(@args);
5117 my (@k, $c, $d, $stat);
5118 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5120 if (/^${esc_color}commit -?($::sha1_short)/o) {
5122 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
5124 process_commit
($c, $r_min, $r_max, \
@k) or
5129 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5130 get_author_info
($c, $1, $2, $3);
5131 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5133 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5134 push @
{$c->{raw
}}, $_;
5135 } elsif (/^${esc_color}[ACRMDT]\t/) {
5136 # we could add $SVN->{svn_path} here, but that requires
5137 # remote access at the moment (repo_path_split)...
5138 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5139 push @
{$c->{changed
}}, $_;
5140 } elsif (/^${esc_color}diff /o) {
5142 push @
{$c->{diff
}}, $_;
5144 push @
{$c->{diff
}}, $_;
5145 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
5146 $esc_color*[\
+\
-]*$esc_color$/x
) {
5148 push @
{$c->{stat}}, $_;
5149 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5150 push @
{$c->{stat}}, $_;
5152 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5153 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
5154 } elsif (s/^${esc_color} //o) {
5155 push @
{$c->{l
}}, $_;
5158 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
5160 process_commit
($c, $r_min, $r_max, \
@k);
5163 ($r_min, $r_max) = ($r_max, $r_min);
5164 process_commit
($_, $r_min, $r_max) foreach reverse @k;
5168 print commit_log_separator
unless $incremental || $oneline;
5177 my ($fh, $ctx, $rev);
5180 ($fh, $ctx) = command_output_pipe
('blame', @_, $path);
5181 while (my $line = <$fh>) {
5182 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5183 # Uncommitted edits show up as a rev ID of
5184 # all zeros, which we can't look up with
5187 (undef, $rev, undef) =
5189 $rev = '0' if (!$rev);
5193 $rev = sprintf('%-10s', $rev);
5194 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5199 ($fh, $ctx) = command_output_pipe
('blame', '-p', @_, 'HEAD',
5204 my %dsha; #distinct sha keys
5206 while (my $line = <$fh>) {
5207 push @buffer, $line;
5208 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5213 my $s2r = ::cmt_sha2rev_batch
([keys %dsha]);
5215 foreach my $line (@buffer) {
5216 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5218 $rev = '0' if (!$rev)
5220 elsif ($line =~ /^author (.*)/) {
5221 $authors{$rev} = $1;
5222 $authors{$rev} =~ s/\s/_/g;
5224 elsif ($line =~ /^\t(.*)$/) {
5225 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5229 command_close_pipe
($fh, $ctx);
5232 package Git
::SVN
::Migration
;
5233 # these version numbers do NOT correspond to actual version numbers
5234 # of git nor git-svn. They are just relative.
5236 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5238 # v1 layout: .git/$id/info/url, refs/remotes/$id
5240 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5242 # v3 layout: .git/svn/$id, refs/remotes/$id
5243 # - info/url may remain for backwards compatibility
5244 # - this is what we migrate up to this layout automatically,
5245 # - this will be used by git svn init on single branches
5246 # v3.1 layout (auto migrated):
5247 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5248 # for backwards compatibility
5250 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5251 # - this is only created for newly multi-init-ed
5252 # repositories. Similar in spirit to the
5253 # --use-separate-remotes option in git-clone (now default)
5254 # - we do not automatically migrate to this (following
5255 # the example set by core git)
5257 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
5258 # - newer, more-efficient format that uses 24-bytes per record
5259 # with no filler space.
5260 # - use xxd -c24 < .rev_map.$UUID to view and debug
5261 # - This is a one-way migration, repositories updated to the
5262 # new format will not be able to use old git-svn without
5263 # rebuilding the .rev_db. Rebuilding the rev_db is not
5264 # possible if noMetadata or useSvmProps are set; but should
5265 # be no problem for users that use the (sensible) defaults.
5269 use File
::Path qw
/mkpath/;
5270 use File
::Basename qw
/dirname basename/;
5271 use vars qw
/$_minimize/;
5273 sub migrate_from_v0
{
5274 my $git_dir = $ENV{GIT_DIR
};
5275 return undef unless -d
$git_dir;
5276 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5280 my ($id, $orig_ref) = ($_, $_);
5281 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
5282 next unless -f
"$git_dir/$id/info/url";
5283 my $new_ref = "refs/remotes/$id";
5284 if (::verify_ref
("$new_ref^0")) {
5285 print STDERR
"W: $orig_ref is probably an old ",
5286 "branch used by an ancient version of ",
5288 "However, $new_ref also exists.\n",
5289 "We will not be able ",
5290 "to use this branch until this ",
5291 "ambiguity is resolved.\n";
5294 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
5295 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
5296 command_noisy
('update-ref', $new_ref, $orig_ref);
5297 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
5300 command_close_pipe
($fh, $ctx);
5301 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
5305 sub migrate_from_v1
{
5306 my $git_dir = $ENV{GIT_DIR
};
5308 return $migrated unless -d
$git_dir;
5309 my $svn_dir = "$git_dir/svn";
5311 # just in case somebody used 'svn' as their $id at some point...
5312 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
5314 print STDERR
"Migrating from a git-svn v1 layout...\n";
5316 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
5317 "$svn_dir\n\t(required for this version ",
5318 "($::VERSION) of git-svn) does not exist.\n";
5319 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
5322 next unless $x =~ s
#^refs/remotes/##;
5324 next unless -f
"$git_dir/$x/info/url";
5325 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
5327 my $dn = dirname
("$git_dir/svn/$x");
5328 mkpath
([$dn]) unless -d
$dn;
5329 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5330 mkpath
(["$git_dir/svn/svn"]);
5331 print STDERR
" - $git_dir/$x/info => ",
5332 "$git_dir/svn/$x/info\n";
5333 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5335 # don't worry too much about these, they probably
5336 # don't exist with repos this old (save for index,
5337 # and we can easily regenerate that)
5338 foreach my $f (qw
/unhandled.log index .rev_db/) {
5339 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5342 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
5343 rename "$git_dir/$x", "$git_dir/svn/$x" or
5348 command_close_pipe
($fh, $ctx);
5349 print STDERR
"Done migrating from a git-svn v1 layout\n";
5354 my ($l_map, $pfx, $path) = @_;
5356 foreach (<$path/*>) {
5357 if (-r
"$_/info/url") {
5358 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
5359 my $ref_id = $pfx . basename
$_;
5360 my $url = ::file_to_s
("$_/info/url");
5361 $l_map->{$ref_id} = $url;
5368 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5369 read_old_urls
($l_map, $x, $_);
5373 sub migrate_from_v2
{
5374 my @cfg = command
(qw
/config -l/);
5375 return if grep /^svn-remote\..+\.url=/, @cfg;
5377 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
5380 foreach my $ref_id (sort keys %l_map) {
5381 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
5383 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5390 sub minimize_connections
{
5391 my $r = Git
::SVN
::read_all_remotes
();
5393 my $root_repos = {};
5394 foreach my $repo_id (keys %$r) {
5395 my $url = $r->{$repo_id}->{url
} or next;
5396 my $fetch = $r->{$repo_id}->{fetch
} or next;
5397 my $ra = Git
::SVN
::Ra
->new($url);
5399 # skip existing cases where we already connect to the root
5400 if (($ra->{url
} eq $ra->{repos_root
}) ||
5401 ($ra->{repos_root
} eq $repo_id)) {
5402 $root_repos->{$ra->{url
}} = $repo_id;
5406 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
5407 my $root_path = $ra->{url
};
5408 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
5409 foreach my $path (keys %$fetch) {
5410 my $ref_id = $fetch->{$path};
5411 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
5413 # make sure we can read when connecting to
5414 # a higher level of a repository
5415 my ($last_rev, undef) = $gs->last_rev_commit;
5416 if (!defined $last_rev) {
5418 $root_ra->get_latest_revnum;
5422 my $new = $root_path;
5423 $new .= length $path ?
"/$path" : '';
5425 $root_ra->get_log([$new], $last_rev, $last_rev,
5429 $new_urls->{$ra->{repos_root
}}->{$new} =
5430 { ref_id
=> $ref_id,
5431 old_repo_id
=> $repo_id,
5432 old_path
=> $path };
5437 foreach my $url (keys %$new_urls) {
5438 # see if we can re-use an existing [svn-remote "repo_id"]
5439 # instead of creating a(n ugly) new section:
5440 my $repo_id = $root_repos->{$url} || $url;
5442 my $fetch = $new_urls->{$url};
5443 foreach my $path (keys %$fetch) {
5444 my $x = $fetch->{$path};
5445 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
5446 my $pfx = "svn-remote.$x->{old_repo_id}";
5448 my $old_fetch = quotemeta("$x->{old_path}:".
5449 "refs/remotes/$x->{ref_id}");
5450 command_noisy
(qw
/config --unset/,
5451 "$pfx.fetch", '^'. $old_fetch . '$');
5452 delete $r->{$x->{old_repo_id
}}->
5453 {fetch
}->{$x->{old_path
}};
5454 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
5455 command_noisy
(qw
/config --unset/,
5457 push @emptied, $x->{old_repo_id
}
5462 my $file = $ENV{GIT_CONFIG
} || "$ENV{GIT_DIR}/config";
5464 The following [svn-remote] sections in your config file ($file) are empty
5465 and can be safely removed:
5467 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
5471 sub migration_check
{
5475 minimize_connections
() if $_minimize;
5478 package Git
::IndexInfo
;
5481 use Git qw
/command_input_pipe command_close_pipe/;
5485 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
5486 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
5490 my ($self, $path) = @_;
5491 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
5492 return ++$self->{nr
};
5498 my ($self, $mode, $hash, $path) = @_;
5499 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
5500 return ++$self->{nr
};
5507 command_close_pipe
($self->{gui
}, $self->{ctx
});
5510 package Git
::SVN
::GlobSpec
;
5515 my ($class, $glob) = @_;
5517 $re =~ s!/+$!!g; # no need for trailing slashes
5518 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5520 my ($left, $right) = ($1, $3);
5522 my $depth = $re =~ tr/*/*/;
5523 if ($depth != $temp =~ tr/*/*/) {
5524 die "Only one set of wildcard directories " .
5525 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5528 die "One '*' is needed for glob: '$glob'\n";
5530 $re =~ s!\*!\[^/\]*!g;
5531 $re = quotemeta($left) . "($re)" . quotemeta($right);
5532 if (length $left && !($left =~ s!/+$!!g)) {
5533 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5535 if (length $right && !($right =~ s!^/+!!g)) {
5536 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5538 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
5539 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
5540 regex
=> qr/$re/, glob => $glob, depth
=> $depth }, $class;
5544 my ($self, $path) = @_;
5545 return (length $self->{left
} ?
"$self->{left}/" : '') .
5546 $path . (length $self->{right
} ?
"/$self->{right}" : '');
5554 $remotes = { # returned by read_all_remotes()
5556 # svn-remote.svn.url=https://svn.musicpd.org
5557 url
=> 'https://svn.musicpd.org',
5558 # svn-remote.svn.fetch=mpd/trunk:trunk
5560 'mpd/trunk' => 'trunk',
5562 # svn-remote.svn.tags=mpd/tags/*:tags/*
5567 regex
=> qr!mpd/tags/([^/]+)$!,
5573 regex
=> qr!tags/([^/]+)$!,
5580 $log_entry hashref as returned by libsvn_log_entry
()
5582 log => 'whitespace-formatted log entry
5583 ', # trailing newline is preserved
5584 revision
=> '8', # integer
5585 date
=> '2004-02-24T17:01:44.108345Z', # commit date
5586 author
=> 'committer name'
5590 # this is generated by generate_diff();
5591 @mods = array of diff
-index line hashes
, each element represents one line
5592 of diff
-index output
5594 diff
-index line
($m hash
)
5596 mode_a
=> first column of diff
-index output
, no leading
':',
5597 mode_b
=> second column of diff
-index output
,
5598 sha1_b
=> sha1sum of the final blob
,
5599 chg
=> change type
[MCRADT
],
5600 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
5601 file_b
=> new
/current file name of a file
(any chg
)
5605 # retval of read_url_paths{,_all}();
5607 # repository root url
5608 'https://svn.musicpd.org' => {
5609 # repository path # GIT_SVN_ID
5610 'mpd/trunk' => 'trunk',
5611 'mpd/tags/0.11.5' => 'tags/0.11.5',
5616 I don
't trust the each() function on unless I created %hash myself
5617 because the internal iterator may not have started at base.