git-svn log: include commit log for the smallest revision in a range
[git/git.git] / git-svn.perl
CommitLineData
3397f9df 1#!/usr/bin/env perl
551ce28f
EW
2# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
3397f9df
EW
4use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
9760adcc
EW
7 $sha1 $sha1_short $_revision
8 $_q $_authors %users/;
3397f9df 9$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
60d02ccc 10$VERSION = '@@GIT_VERSION@@';
13ccd6d4 11
15153451
BS
12# From which subdir have we been invoked?
13my $cmd_dir_prefix = eval {
14 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15} || '';
16
5253dc33 17my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
706587fc 18$ENV{GIT_DIR} ||= '.git';
9fa00b65 19$Git::SVN::default_repo_id = 'svn';
8b8fc068 20$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
6af1db44 21$Git::SVN::Ra::_log_window_size = 100;
13ccd6d4 22
f8c9d1d2 23$Git::SVN::Log::TZ = $ENV{TZ};
3397f9df 24$ENV{TZ} = 'UTC';
a00439ac 25$| = 1; # unbuffer STDOUT
3397f9df 26
207f1a75 27sub fatal (@) { print STDERR "@_\n"; exit 1 }
b9c85187
EW
28require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
29require SVN::Ra;
30require SVN::Delta;
31if ($SVN::Core::VERSION lt '1.1.0') {
207f1a75 32 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
b9c85187 33}
d81bf827 34push @Git::SVN::Ra::ISA, 'SVN::Ra';
b9c85187
EW
35push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
36push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
3397f9df
EW
37use Carp qw/croak/;
38use IO::File qw//;
39use File::Basename qw/dirname basename/;
40use File::Path qw/mkpath/;
512b620b 41use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
968bdf1f 42use IPC::Open3;
336f1714 43use Git;
a5e0cedc 44
336f1714 45BEGIN {
c5f71ad0
SV
46 # import functions from Git into our packages, en masse
47 no strict 'refs';
336f1714
EW
48 foreach (qw/command command_oneline command_noisy command_output_pipe
49 command_input_pipe command_close_pipe/) {
c5f71ad0
SV
50 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
51 Git::SVN::Migration Git::SVN::Log Git::SVN),
52 __PACKAGE__) {
53 *{"${package}::$_"} = \&{"Git::$_"};
54 }
336f1714 55 }
336f1714
EW
56}
57
b9c85187 58my ($SVN);
83e9940a 59
f8c9d1d2
EW
60$sha1 = qr/[a-f\d]{40}/;
61$sha1_short = qr/[a-f\d]{4,40}/;
44320b9e 62my ($_stdin, $_help, $_edit,
9760adcc 63 $_message, $_file,
d05d72e0 64 $_template, $_shared,
171af110 65 $_version, $_fetch_all, $_no_rebase,
dee41f3e 66 $_merge, $_strategy, $_dry_run, $_local,
905f8b7d 67 $_prefix, $_no_checkout, $_verbose);
0bed5eaa 68$Git::SVN::_follow_parent = 1;
706587fc
EW
69my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
70 'config-dir=s' => \$Git::SVN::Ra::config_dir,
71 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
0bed5eaa 72my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
dc5869c0 73 'authors-file|A=s' => \$_authors,
ecc712dd 74 'repack:i' => \$Git::SVN::_repack,
97ae0911
EW
75 'noMetadata' => \$Git::SVN::_no_metadata,
76 'useSvmProps' => \$Git::SVN::_use_svm_props,
62e349d2 77 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
6af1db44 78 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
1e889ef3 79 'no-checkout' => \$_no_checkout,
80f50749 80 'quiet|q' => \$_q,
ecc712dd
EW
81 'repack-flags|repack-args|repack-opts=s' =>
82 \$Git::SVN::_repack_flags,
706587fc 83 %remote_opts );
36f5b1f0 84
8f728fb9 85my ($_trunk, $_tags, $_branches, $_stdlayout);
0dfaf0a4 86my %icv;
dadc6d2a
EW
87my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
88 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
89 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
8f728fb9 90 'stdlayout|s' => \$_stdlayout,
4a1bb4c3 91 'minimize-url|m' => \$Git::SVN::_minimize_url,
0dfaf0a4
EW
92 'no-metadata' => sub { $icv{noMetadata} = 1 },
93 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
94 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
95 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
dadc6d2a 96 %remote_opts );
27e9fb8d 97my %cmt_opts = ( 'edit|e' => \$_edit,
24e22aa8
EW
98 'rmdir' => \$SVN::Git::Editor::_rmdir,
99 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
100 'l=i' => \$SVN::Git::Editor::_rename_limit,
101 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
27e9fb8d 102);
9d55b41a 103
3397f9df 104my %cmd = (
2a3240be 105 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
e98671e5 106 { 'revision|r=s' => \$_revision,
905f8b7d 107 'fetch-all|all' => \$_fetch_all,
e98671e5 108 %fc_opts } ],
0425ea90
EW
109 clone => [ \&cmd_clone, "Initialize and fetch revisions",
110 { 'revision|r=s' => \$_revision,
111 %fc_opts, %init_opts } ],
d2866f9e 112 init => [ \&cmd_init, "Initialize a repo for tracking" .
f8ab6b73 113 " (requires URL argument)",
9d55b41a 114 \%init_opts ],
dadc6d2a
EW
115 'multi-init' => [ \&cmd_multi_init,
116 "Deprecated alias for ".
117 "'$0 init -T<trunk> -b<branches> -t<tags>'",
118 \%init_opts ],
d7ad3bed
EW
119 dcommit => [ \&cmd_dcommit,
120 'Commit several diffs to merge with upstream',
3289e86e
EW
121 { 'merge|m|M' => \$_merge,
122 'strategy|s=s' => \$_strategy,
905f8b7d 123 'verbose|v' => \$_verbose,
3289e86e 124 'dry-run|n' => \$_dry_run,
905f8b7d 125 'fetch-all|all' => \$_fetch_all,
171af110 126 'no-rebase' => \$_no_rebase,
4b155223 127 %cmt_opts, %fc_opts } ],
1ce255dc
EW
128 'set-tree' => [ \&cmd_set_tree,
129 "Set an SVN repository to a git tree-ish",
130 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
d05ddec5
BS
131 'create-ignore' => [ \&cmd_create_ignore,
132 'Create a .gitignore per svn:ignore',
133 { 'revision|r=i' => \$_revision
134 } ],
15153451
BS
135 'propget' => [ \&cmd_propget,
136 'Print the value of a property on a file or directory',
137 { 'revision|r=i' => \$_revision } ],
51e057cf
BS
138 'proplist' => [ \&cmd_proplist,
139 'List all properties of a file or directory',
140 { 'revision|r=i' => \$_revision } ],
5969cbe1 141 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
4dbfe2e9 142 { 'revision|r=i' => \$_revision
05b4df31 143 } ],
1c8443b0 144 'multi-fetch' => [ \&cmd_multi_fetch,
e98671e5
EW
145 "Deprecated alias for $0 fetch --all",
146 { 'revision|r=s' => \$_revision, %fc_opts } ],
706587fc
EW
147 'migrate' => [ sub { },
148 # no-op, we automatically run this anyways,
706587fc
EW
149 'Migrate configuration/metadata/layout from
150 previous versions of git-svn',
a836a0e1
EW
151 { 'minimize' => \$Git::SVN::Migration::_minimize,
152 %remote_opts } ],
f8c9d1d2
EW
153 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
154 { 'limit=i' => \$Git::SVN::Log::limit,
79bb8d88 155 'revision|r=s' => \$_revision,
f8c9d1d2
EW
156 'verbose|v' => \$Git::SVN::Log::verbose,
157 'incremental' => \$Git::SVN::Log::incremental,
158 'oneline' => \$Git::SVN::Log::oneline,
159 'show-commit' => \$Git::SVN::Log::show_commit,
160 'non-recursive' => \$Git::SVN::Log::non_recursive,
79bb8d88 161 'authors-file|A=s' => \$_authors,
f8c9d1d2 162 'color' => \$Git::SVN::Log::color,
4dbfe2e9 163 'pager=s' => \$Git::SVN::Log::pager
79bb8d88 164 } ],
26e60160 165 'find-rev' => [ \&cmd_find_rev, "Translate between SVN revision numbers and tree-ish",
4dbfe2e9 166 {} ],
905f8b7d
EW
167 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
168 { 'merge|m|M' => \$_merge,
169 'verbose|v' => \$_verbose,
170 'strategy|s=s' => \$_strategy,
dee41f3e 171 'local|l' => \$_local,
905f8b7d
EW
172 'fetch-all|all' => \$_fetch_all,
173 %fc_opts } ],
44320b9e
EW
174 'commit-diff' => [ \&cmd_commit_diff,
175 'Commit a diff between two trees',
27e9fb8d
EW
176 { 'message|m=s' => \$_message,
177 'file|F=s' => \$_file,
45bf473a 178 'revision|r=s' => \$_revision,
27e9fb8d 179 %cmt_opts } ],
3397f9df 180);
9d55b41a 181
3397f9df
EW
182my $cmd;
183for (my $i = 0; $i < @ARGV; $i++) {
184 if (defined $cmd{$ARGV[$i]}) {
185 $cmd = $ARGV[$i];
186 splice @ARGV, $i, 1;
187 last;
188 }
189};
190
448c81b4 191my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
a9612be2 192
b8c92cad 193read_repo_config(\%opts);
c284914a 194Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
9760adcc
EW
195my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
196 'minimize-connections' => \$Git::SVN::Migration::_minimize,
197 'id|i=s' => \$Git::SVN::default_ref_id,
befc9adc
EW
198 'svn-remote|remote|R=s' => sub {
199 $Git::SVN::no_reuse_existing = 1;
200 $Git::SVN::default_repo_id = $_[1] });
c284914a 201exit 1 if (!$rv && $cmd && $cmd ne 'log');
6f0783cf 202
3397f9df 203usage(0) if $_help;
551ce28f 204version() if $_version;
eeb0abe0
EW
205usage(1) unless defined $cmd;
206load_authors() if $_authors;
5253dc33
EW
207
208# make sure we're always running
209unless ($cmd =~ /(?:clone|init|multi-init)$/) {
210 unless (-d $ENV{GIT_DIR}) {
211 if ($git_dir_user_set) {
212 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
213 "but it is not a directory\n";
214 }
215 my $git_dir = delete $ENV{GIT_DIR};
216 chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
217 unless (length $cdup) {
218 die "Already at toplevel, but $git_dir ",
219 "not found '$cdup'\n";
220 }
221 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
222 unless (-d $git_dir) {
223 die "$git_dir still not found after going to ",
224 "'$cdup'\n";
225 }
226 $ENV{GIT_DIR} = $git_dir;
227 }
228}
0425ea90 229unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
706587fc
EW
230 Git::SVN::Migration::migration_check();
231}
ecc712dd 232Git::SVN::init_vars();
b805b44a
EW
233eval {
234 Git::SVN::verify_remotes_sanity();
235 $cmd{$cmd}->[0]->(@ARGV);
236};
237fatal $@ if $@;
1e889ef3 238post_fetch_checkout();
3397f9df
EW
239exit 0;
240
241####################### primary functions ######################
242sub usage {
243 my $exit = shift || 0;
244 my $fd = $exit ? \*STDERR : \*STDOUT;
245 print $fd <<"";
246git-svn - bidirectional operations between a single Subversion tree and git
247Usage: $0 <command> [options] [arguments]\n
448c81b4
EW
248
249 print $fd "Available commands:\n" unless $cmd;
3397f9df
EW
250
251 foreach (sort keys %cmd) {
448c81b4 252 next if $cmd && $cmd ne $_;
a836a0e1 253 next if /^multi-/; # don't show deprecated commands
b203b769 254 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
aa807bc2 255 foreach (sort keys %{$cmd{$_}->[2]}) {
512b620b
EW
256 # mixed-case options are for .git/config only
257 next if /[A-Z]/ && /^[a-z]+$/i;
448c81b4 258 # prints out arguments as they should be passed:
b8c92cad 259 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
b203b769 260 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
448c81b4
EW
261 "--$_" : "-$_" }
262 split /\|/,$_)," $x\n";
263 }
3397f9df
EW
264 }
265 print $fd <<"";
448c81b4
EW
266\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
267arbitrary identifier if you're tracking multiple SVN branches/repositories in
268one git repository and want to keep them separate. See git-svn(1) for more
269information.
3397f9df
EW
270
271 exit $exit;
272}
273
551ce28f 274sub version {
7d60ab2c 275 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
551ce28f
EW
276 exit 0;
277}
278
8164b652
EW
279sub do_git_init_db {
280 unless (-d $ENV{GIT_DIR}) {
281 my @init_db = ('init');
282 push @init_db, "--template=$_template" if defined $_template;
dadc6d2a
EW
283 if (defined $_shared) {
284 if ($_shared =~ /[a-z]/) {
285 push @init_db, "--shared=$_shared";
286 } else {
287 push @init_db, "--shared";
288 }
289 }
8164b652
EW
290 command_noisy(@init_db);
291 }
0dfaf0a4
EW
292 my $set;
293 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
294 foreach my $i (keys %icv) {
295 die "'$set' and '$i' cannot both be set\n" if $set;
296 next unless defined $icv{$i};
297 command_noisy('config', "$pfx.$i", $icv{$i});
298 $set = $i;
299 }
8164b652
EW
300}
301
dadc6d2a
EW
302sub init_subdir {
303 my $repo_path = shift or return;
304 mkpath([$repo_path]) unless -d $repo_path;
305 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
f30603fc 306 $ENV{GIT_DIR} = '.git';
dadc6d2a
EW
307}
308
0425ea90
EW
309sub cmd_clone {
310 my ($url, $path) = @_;
311 if (!defined $path &&
8f728fb9 312 (defined $_trunk || defined $_branches || defined $_tags ||
313 defined $_stdlayout) &&
0425ea90
EW
314 $url !~ m#^[a-z\+]+://#) {
315 $path = $url;
316 }
0425ea90 317 $path = basename($url) if !defined $path || !length $path;
f30603fc 318 cmd_init($url, $path);
0425ea90
EW
319 Git::SVN::fetch_all($Git::SVN::default_repo_id);
320}
321
d2866f9e 322sub cmd_init {
8f728fb9 323 if (defined $_stdlayout) {
324 $_trunk = 'trunk' if (!defined $_trunk);
325 $_tags = 'tags' if (!defined $_tags);
326 $_branches = 'branches' if (!defined $_branches);
327 }
dadc6d2a
EW
328 if (defined $_trunk || defined $_branches || defined $_tags) {
329 return cmd_multi_init(@_);
03e0ea87 330 }
dadc6d2a
EW
331 my $url = shift or die "SVN repository location required ",
332 "as a command-line argument\n";
333 init_subdir(@_);
8164b652 334 do_git_init_db();
03e0ea87 335
706587fc 336 Git::SVN->init($url);
3397f9df
EW
337}
338
2a3240be 339sub cmd_fetch {
e98671e5
EW
340 if (grep /^\d+=./, @_) {
341 die "'<rev>=<commit>' fetch arguments are ",
342 "no longer supported.\n";
07a1c950 343 }
e98671e5
EW
344 my ($remote) = @_;
345 if (@_ > 1) {
905f8b7d 346 die "Usage: $0 fetch [--all] [svn-remote]\n";
e98671e5
EW
347 }
348 $remote ||= $Git::SVN::default_repo_id;
349 if ($_fetch_all) {
350 cmd_multi_fetch();
351 } else {
352 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
1c8443b0 353 }
2a3240be
EW
354}
355
1ce255dc 356sub cmd_set_tree {
3397f9df
EW
357 my (@commits) = @_;
358 if ($_stdin || !@commits) {
359 print "Reading from stdin...\n";
360 @commits = ();
361 while (<STDIN>) {
1ca72aef 362 if (/\b($sha1_short)\b/o) {
3397f9df
EW
363 unshift @commits, $1;
364 }
365 }
366 }
367 my @revs;
8de010ad 368 foreach my $c (@commits) {
aef4e921 369 my @tmp = command('rev-parse',$c);
8de010ad
EW
370 if (scalar @tmp == 1) {
371 push @revs, $tmp[0];
372 } elsif (scalar @tmp > 1) {
aef4e921 373 push @revs, reverse(command('rev-list',@tmp));
8de010ad 374 } else {
207f1a75 375 fatal "Failed to rev-parse $c";
8de010ad 376 }
3397f9df 377 }
1ce255dc
EW
378 my $gs = Git::SVN->new;
379 my ($r_last, $cmt_last) = $gs->last_rev_commit;
380 $gs->fetch;
97f6987a 381 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
1ce255dc
EW
382 fatal "There are new revisions that were fetched ",
383 "and need to be merged (or acknowledged) ",
384 "before committing.\nlast rev: $r_last\n",
207f1a75 385 " current: $gs->{last_rev}";
a5e0cedc 386 }
1ce255dc
EW
387 $gs->set_tree($_) foreach @revs;
388 print "Done committing ",scalar @revs," revisions to SVN\n";
a5e0cedc 389}
8f22562c 390
d7ad3bed
EW
391sub cmd_dcommit {
392 my $head = shift;
c8cfa3e4
BS
393 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
394 'Cannot dcommit with a dirty index. Commit your changes first'
395 . "or stash them with `git stash'.\n";
d7ad3bed 396 $head ||= 'HEAD';
a8ae2623 397 my @refs;
13c823fb 398 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
15d54753 399 print "Committing to $url ...\n";
13c823fb 400 unless ($gs) {
a8ae2623 401 die "Unable to determine upstream SVN information from ",
905f8b7d 402 "$head history\n";
a8ae2623 403 }
45bf473a 404 my $last_rev;
733a65aa 405 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
751eb395
EW
406 if ($_no_rebase && scalar(@$linear_refs) > 1) {
407 warn "Attempting to commit more than one change while ",
408 "--no-rebase is enabled.\n",
409 "If these changes depend on each other, re-running ",
410 "without --no-rebase will be required."
411 }
c74d9acf
EW
412 while (1) {
413 my $d = shift @$linear_refs or last;
45bf473a
EW
414 unless (defined $last_rev) {
415 (undef, $last_rev, undef) = cmt_metadata("$d~1");
416 unless (defined $last_rev) {
d7ad3bed 417 fatal "Unable to extract revision information ",
207f1a75 418 "from commit $d~1";
45bf473a
EW
419 }
420 }
b22d4497
EW
421 if ($_dry_run) {
422 print "diff-tree $d~1 $d\n";
423 } else {
751eb395 424 my $cmt_rev;
d7ad3bed 425 my %ed_opts = ( r => $last_rev,
61395354 426 log => get_commit_entry($d)->{log},
645833b5 427 ra => Git::SVN::Ra->new($gs->full_url),
3caf320b
KA
428 config => SVN::Core::config_get_config(
429 $Git::SVN::Ra::config_dir
430 ),
61395354
EW
431 tree_a => "$d~1",
432 tree_b => $d,
433 editor_cb => sub {
434 print "Committed r$_[0]\n";
751eb395
EW
435 $cmt_rev = $_[0];
436 },
a8ae2623 437 svn_path => '');
61395354 438 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
d7ad3bed 439 print "No changes\n$d~1 == $d\n";
733a65aa 440 } elsif ($parents->{$d} && @{$parents->{$d}}) {
751eb395 441 $gs->{inject_parents_dcommit}->{$cmt_rev} =
733a65aa 442 $parents->{$d};
d7ad3bed 443 }
751eb395
EW
444 $_fetch_all ? $gs->fetch_all : $gs->fetch;
445 next if $_no_rebase;
446
447 # we always want to rebase against the current HEAD,
448 # not any head that was passed to us
c74d9acf 449 my @diff = command('diff-tree', $d,
751eb395
EW
450 $gs->refname, '--');
451 my @finish;
452 if (@diff) {
453 @finish = rebase_cmd();
c74d9acf 454 print STDERR "W: $d and ", $gs->refname,
751eb395 455 " differ, using @finish:\n",
c74d9acf 456 join("\n", @diff), "\n";
751eb395
EW
457 } else {
458 print "No changes between current HEAD and ",
459 $gs->refname,
460 "\nResetting to the latest ",
461 $gs->refname, "\n";
462 @finish = qw/reset --mixed/;
463 }
464 command_noisy(@finish, $gs->refname);
c74d9acf
EW
465 if (@diff) {
466 @refs = ();
467 my ($url_, $rev_, $uuid_, $gs_) =
468 working_head_info($head, \@refs);
469 my ($linear_refs_, $parents_) =
470 linearize_history($gs_, \@refs);
471 if (scalar(@$linear_refs) !=
472 scalar(@$linear_refs_)) {
473 fatal "# of revisions changed ",
474 "\nbefore:\n",
475 join("\n", @$linear_refs),
476 "\n\nafter:\n",
477 join("\n", @$linear_refs_), "\n",
478 'If you are attempting to commit ',
479 "merges, try running:\n\t",
480 'git rebase --interactive',
481 '--preserve-merges ',
482 $gs->refname,
483 "\nBefore dcommitting";
484 }
485 if ($url_ ne $url) {
486 fatal "URL mismatch after rebase: ",
487 "$url_ != $url";
488 }
489 if ($uuid_ ne $uuid) {
490 fatal "uuid mismatch after rebase: ",
491 "$uuid_ != $uuid";
492 }
493 # remap parents
494 my (%p, @l, $i);
495 for ($i = 0; $i < scalar @$linear_refs; $i++) {
496 my $new = $linear_refs_->[$i] or next;
497 $p{$new} =
498 $parents->{$linear_refs->[$i]};
499 push @l, $new;
500 }
501 $parents = \%p;
502 $linear_refs = \@l;
503 }
751eb395 504 $last_rev = $cmt_rev;
b22d4497
EW
505 }
506 }
b22d4497
EW
507}
508
26e60160
AR
509sub cmd_find_rev {
510 my $revision_or_hash = shift;
511 my $result;
512 if ($revision_or_hash =~ /^r\d+$/) {
b3cb7e45
AR
513 my $head = shift;
514 $head ||= 'HEAD';
515 my @refs;
516 my (undef, undef, undef, $gs) = working_head_info($head, \@refs);
517 unless ($gs) {
518 die "Unable to determine upstream SVN information from ",
519 "$head history\n";
26e60160 520 }
b3cb7e45
AR
521 my $desired_revision = substr($revision_or_hash, 1);
522 $result = $gs->rev_db_get($desired_revision);
26e60160
AR
523 } else {
524 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
525 $result = $rev;
526 }
527 print "$result\n" if $result;
528}
529
905f8b7d
EW
530sub cmd_rebase {
531 command_noisy(qw/update-index --refresh/);
13c823fb
EW
532 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
533 unless ($gs) {
905f8b7d
EW
534 die "Unable to determine upstream SVN information from ",
535 "working tree history\n";
536 }
905f8b7d
EW
537 if (command(qw/diff-index HEAD --/)) {
538 print STDERR "Cannot rebase with uncommited changes:\n";
539 command_noisy('status');
540 exit 1;
541 }
dee41f3e
EW
542 unless ($_local) {
543 $_fetch_all ? $gs->fetch_all : $gs->fetch;
544 }
905f8b7d
EW
545 command_noisy(rebase_cmd(), $gs->refname);
546}
547
5969cbe1 548sub cmd_show_ignore {
13c823fb
EW
549 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
550 $gs ||= Git::SVN->new;
5969cbe1 551 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
01bdab84
BS
552 $gs->prop_walk($gs->{path}, $r, sub {
553 my ($gs, $path, $props) = @_;
554 print STDOUT "\n# $path\n";
555 my $s = $props->{'svn:ignore'} or return;
556 $s =~ s/[\r\n]+/\n/g;
557 chomp $s;
558 $s =~ s#^#$path#gm;
559 print STDOUT "$s\n";
560 });
a5e0cedc
EW
561}
562
d05ddec5
BS
563sub cmd_create_ignore {
564 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
565 $gs ||= Git::SVN->new;
566 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
567 $gs->prop_walk($gs->{path}, $r, sub {
568 my ($gs, $path, $props) = @_;
569 # $path is of the form /path/to/dir/
570 my $ignore = '.' . $path . '.gitignore';
571 my $s = $props->{'svn:ignore'} or return;
572 open(GITIGNORE, '>', $ignore)
207f1a75 573 or fatal("Failed to open `$ignore' for writing: $!");
d05ddec5
BS
574 $s =~ s/[\r\n]+/\n/g;
575 chomp $s;
576 # Prefix all patterns so that the ignore doesn't apply
577 # to sub-directories.
578 $s =~ s#^#/#gm;
579 print GITIGNORE "$s\n";
580 close(GITIGNORE)
207f1a75 581 or fatal("Failed to close `$ignore': $!");
d05ddec5
BS
582 command_noisy('add', $ignore);
583 });
584}
585
15153451
BS
586# get_svnprops(PATH)
587# ------------------
51e057cf 588# Helper for cmd_propget and cmd_proplist below.
15153451
BS
589sub get_svnprops {
590 my $path = shift;
591 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
592 $gs ||= Git::SVN->new;
593
594 # prefix THE PATH by the sub-directory from which the user
595 # invoked us.
596 $path = $cmd_dir_prefix . $path;
207f1a75 597 fatal("No such file or directory: $path") unless -e $path;
15153451
BS
598 my $is_dir = -d $path ? 1 : 0;
599 $path = $gs->{path} . '/' . $path;
600
601 # canonicalize the path (otherwise libsvn will abort or fail to
602 # find the file)
603 # File::Spec->canonpath doesn't collapse x/../y into y (for a
604 # good reason), so let's do this manually.
605 $path =~ s#/+#/#g;
606 $path =~ s#/\.(?:/|$)#/#g;
607 $path =~ s#/[^/]+/\.\.##g;
608 $path =~ s#/$##g;
609
610 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
611 my $props;
612 if ($is_dir) {
613 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
614 }
615 else {
616 (undef, $props) = $gs->ra->get_file($path, $r, undef);
617 }
618 return $props;
619}
620
621# cmd_propget (PROP, PATH)
622# ------------------------
623# Print the SVN property PROP for PATH.
624sub cmd_propget {
625 my ($prop, $path) = @_;
626 $path = '.' if not defined $path;
627 usage(1) if not defined $prop;
628 my $props = get_svnprops($path);
629 if (not defined $props->{$prop}) {
207f1a75 630 fatal("`$path' does not have a `$prop' SVN property.");
15153451
BS
631 }
632 print $props->{$prop} . "\n";
633}
634
51e057cf
BS
635# cmd_proplist (PATH)
636# -------------------
637# Print the list of SVN properties for PATH.
638sub cmd_proplist {
639 my $path = shift;
640 $path = '.' if not defined $path;
641 my $props = get_svnprops($path);
642 print "Properties on '$path':\n";
643 foreach (sort keys %{$props}) {
644 print " $_\n";
645 }
646}
647
8164b652 648sub cmd_multi_init {
9d55b41a 649 my $url = shift;
98327e58
EW
650 unless (defined $_trunk || defined $_branches || defined $_tags) {
651 usage(1);
9d55b41a 652 }
dc431666
EW
653
654 # there are currently some bugs that prevent multi-init/multi-fetch
655 # setups from working well without this.
656 $Git::SVN::_minimize_url = 1;
657
8164b652 658 $_prefix = '' unless defined $_prefix;
dadc6d2a
EW
659 if (defined $url) {
660 $url =~ s#/+$##;
661 init_subdir(@_);
662 }
f30603fc 663 do_git_init_db();
98327e58 664 if (defined $_trunk) {
706587fc
EW
665 my $trunk_ref = $_prefix . 'trunk';
666 # try both old-style and new-style lookups:
667 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
8164b652 668 unless ($gs_trunk) {
706587fc
EW
669 my ($trunk_url, $trunk_path) =
670 complete_svn_url($url, $_trunk);
671 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
672 undef, $trunk_ref);
98327e58 673 }
c35b96e7 674 }
706587fc 675 return unless defined $_branches || defined $_tags;
e7db67e6
EW
676 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
677 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
678 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
9d55b41a
EW
679}
680
1c8443b0 681sub cmd_multi_fetch {
0af9c9f9
EW
682 my $remotes = Git::SVN::read_all_remotes();
683 foreach my $repo_id (sort keys %$remotes) {
db03cd24 684 if ($remotes->{$repo_id}->{url}) {
4bb9ed04
EW
685 Git::SVN::fetch_all($repo_id, $remotes);
686 }
9d55b41a 687 }
9d55b41a
EW
688}
689
44320b9e
EW
690# this command is special because it requires no metadata
691sub cmd_commit_diff {
692 my ($ta, $tb, $url) = @_;
693 my $usage = "Usage: $0 commit-diff -r<revision> ".
207f1a75 694 "<tree-ish> <tree-ish> [<URL>]";
44320b9e 695 fatal($usage) if (!defined $ta || !defined $tb);
d3a840dc 696 my $svn_path;
44320b9e
EW
697 if (!defined $url) {
698 my $gs = eval { Git::SVN->new };
699 if (!$gs) {
700 fatal("Needed URL or usable git-svn --id in ",
701 "the command-line\n", $usage);
702 }
703 $url = $gs->{url};
d3a840dc 704 $svn_path = $gs->{path};
44320b9e
EW
705 }
706 unless (defined $_revision) {
707 fatal("-r|--revision is a required argument\n", $usage);
708 }
709 if (defined $_message && defined $_file) {
710 fatal("Both --message/-m and --file/-F specified ",
711 "for the commit message.\n",
207f1a75 712 "I have no idea what you mean");
44320b9e
EW
713 }
714 if (defined $_file) {
715 $_message = file_to_s($_file);
716 } else {
717 $_message ||= get_commit_entry($tb)->{log};
718 }
719 my $ra ||= Git::SVN::Ra->new($url);
d3a840dc 720 $svn_path ||= $ra->{svn_path};
44320b9e
EW
721 my $r = $_revision;
722 if ($r eq 'HEAD') {
723 $r = $ra->get_latest_revnum;
724 } elsif ($r !~ /^\d+$/) {
725 die "revision argument: $r not understood by git-svn\n";
726 }
61395354
EW
727 my %ed_opts = ( r => $r,
728 log => $_message,
729 ra => $ra,
730 tree_a => $ta,
731 tree_b => $tb,
732 editor_cb => sub { print "Committed r$_[0]\n" },
733 svn_path => $svn_path );
734 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
44320b9e
EW
735 print "No changes\n$ta == $tb\n";
736 }
44320b9e
EW
737}
738
3397f9df
EW
739########################### utility functions #########################
740
905f8b7d
EW
741sub rebase_cmd {
742 my @cmd = qw/rebase/;
743 push @cmd, '-v' if $_verbose;
744 push @cmd, qw/--merge/ if $_merge;
745 push @cmd, "--strategy=$_strategy" if $_strategy;
746 @cmd;
747}
748
1e889ef3
EW
749sub post_fetch_checkout {
750 return if $_no_checkout;
751 my $gs = $Git::SVN::_head or return;
752 return if verify_ref('refs/heads/master^0');
753
754 my $valid_head = verify_ref('HEAD^0');
755 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
756 return if ($valid_head || !verify_ref('HEAD^0'));
757
758 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
759 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
760 return if -f $index;
761
7ae3df8c 762 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
1e889ef3
EW
763 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
764 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
765 print STDERR "Checked out HEAD:\n ",
766 $gs->full_url, " r", $gs->last_rev, "\n";
767}
768
98327e58
EW
769sub complete_svn_url {
770 my ($url, $path) = @_;
771 $path =~ s#/+$##;
98327e58 772 if ($path !~ m#^[a-z\+]+://#) {
98327e58
EW
773 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
774 fatal("E: '$path' is not a complete URL ",
207f1a75 775 "and a separate URL is not specified");
98327e58 776 }
706587fc 777 return ($url, $path);
98327e58 778 }
706587fc 779 return ($path, '');
98327e58
EW
780}
781
9d55b41a 782sub complete_url_ls_init {
706587fc
EW
783 my ($ra, $repo_path, $switch, $pfx) = @_;
784 unless ($repo_path) {
9d55b41a
EW
785 print STDERR "W: $switch not specified\n";
786 return;
787 }
706587fc
EW
788 $repo_path =~ s#/+$##;
789 if ($repo_path =~ m#^[a-z\+]+://#) {
790 $ra = Git::SVN::Ra->new($repo_path);
791 $repo_path = '';
e7db67e6 792 } else {
706587fc 793 $repo_path =~ s#^/+##;
e7db67e6 794 unless ($ra) {
706587fc 795 fatal("E: '$repo_path' is not a complete URL ",
207f1a75 796 "and a separate URL is not specified");
8164b652 797 }
e7db67e6 798 }
706587fc 799 my $url = $ra->{url};
b4d57e5e
EW
800 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
801 my $k = "svn-remote.$gs->{repo_id}.url";
802 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
803 if ($orig_url && ($orig_url ne $gs->{url})) {
804 die "$k already set: $orig_url\n",
805 "wanted to set to: $gs->{url}\n";
88cf4107 806 }
b4d57e5e
EW
807 command_oneline('config', $k, $gs->{url}) unless $orig_url;
808 my $remote_path = "$ra->{svn_path}/$repo_path/*";
809 $remote_path =~ s#/+#/#g;
810 $remote_path =~ s#^/##g;
811 my ($n) = ($switch =~ /^--(\w+)/);
812 if (length $pfx && $pfx !~ m#/$#) {
813 die "--prefix='$pfx' must have a trailing slash '/'\n";
9d55b41a 814 }
b4d57e5e
EW
815 command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
816 "$remote_path:refs/remotes/$pfx*");
9d55b41a
EW
817}
818
aef4e921
EW
819sub verify_ref {
820 my ($ref) = @_;
2c5c1d53
EW
821 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
822 { STDERR => 0 }); };
aef4e921
EW
823}
824
a5e0cedc 825sub get_tree_from_treeish {
cf52b8f0 826 my ($treeish) = @_;
44320b9e 827 # $treeish can be a symbolic ref, too:
aef4e921 828 my $type = command_oneline(qw/cat-file -t/, $treeish);
cf52b8f0
EW
829 my $expected;
830 while ($type eq 'tag') {
aef4e921 831 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
cf52b8f0
EW
832 }
833 if ($type eq 'commit') {
aef4e921
EW
834 $expected = (grep /^tree /, command(qw/cat-file commit/,
835 $treeish))[0];
44320b9e 836 ($expected) = ($expected =~ /^tree ($sha1)$/o);
cf52b8f0
EW
837 die "Unable to get tree from $treeish\n" unless $expected;
838 } elsif ($type eq 'tree') {
839 $expected = $treeish;
840 } else {
841 die "$treeish is a $type, expected tree, tag or commit\n";
842 }
a5e0cedc
EW
843 return $expected;
844}
845
44320b9e
EW
846sub get_commit_entry {
847 my ($treeish) = shift;
848 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
849 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
850 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
851 open my $log_fh, '>', $commit_editmsg or croak $!;
3397f9df 852
44320b9e 853 my $type = command_oneline(qw/cat-file -t/, $treeish);
4ad4515d 854 if ($type eq 'commit' || $type eq 'tag') {
aef4e921 855 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
44320b9e 856 $type, $treeish);
3397f9df
EW
857 my $in_msg = 0;
858 while (<$msg_fh>) {
859 if (!$in_msg) {
860 $in_msg = 1 if (/^\s*$/);
df746c5a 861 } elsif (/^git-svn-id: /) {
44320b9e
EW
862 # skip this for now, we regenerate the
863 # correct one on re-fetch anyways
864 # TODO: set *:merge properties or like...
3397f9df 865 } else {
44320b9e 866 print $log_fh $_ or croak $!;
3397f9df
EW
867 }
868 }
aef4e921 869 command_close_pipe($msg_fh, $ctx);
3397f9df 870 }
44320b9e 871 close $log_fh or croak $!;
3397f9df
EW
872
873 if ($_edit || ($type eq 'tree')) {
874 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
44320b9e
EW
875 # TODO: strip out spaces, comments, like git-commit.sh
876 system($editor, $commit_editmsg);
3397f9df 877 }
44320b9e
EW
878 rename $commit_editmsg, $commit_msg or croak $!;
879 open $log_fh, '<', $commit_msg or croak $!;
880 { local $/; chomp($log_entry{log} = <$log_fh>); }
881 close $log_fh or croak $!;
882 unlink $commit_msg;
883 \%log_entry;
a5e0cedc
EW
884}
885
3397f9df
EW
886sub s_to_file {
887 my ($str, $file, $mode) = @_;
888 open my $fd,'>',$file or croak $!;
889 print $fd $str,"\n" or croak $!;
890 close $fd or croak $!;
891 chmod ($mode &~ umask, $file) if (defined $mode);
892}
893
894sub file_to_s {
895 my $file = shift;
896 open my $fd,'<',$file or croak "$!: file: $file\n";
897 local $/;
898 my $ret = <$fd>;
899 close $fd or croak $!;
900 $ret =~ s/\s*$//s;
901 return $ret;
902}
903
eeb0abe0
EW
904# '<svn username> = real-name <email address>' mapping based on git-svnimport:
905sub load_authors {
906 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
f8c9d1d2 907 my $log = $cmd eq 'log';
eeb0abe0
EW
908 while (<$authors>) {
909 chomp;
575d025c 910 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
eeb0abe0 911 my ($user, $name, $email) = ($1, $2, $3);
f8c9d1d2
EW
912 if ($log) {
913 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
914 } else {
915 $users{$user} = [$name, $email];
916 }
79bb8d88
EW
917 }
918 close $authors or croak $!;
919}
920
e0d10e1c 921# convert GetOpt::Long specs for use by git-config
b8c92cad 922sub read_repo_config {
706587fc 923 return unless -d $ENV{GIT_DIR};
b8c92cad 924 my $opts = shift;
97ae0911 925 my @config_only;
b8c92cad 926 foreach my $o (keys %$opts) {
97ae0911
EW
927 # if we have mixedCase and a long option-only, then
928 # it's a config-only variable that we don't need for
929 # the command-line.
930 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
b8c92cad 931 my $v = $opts->{$o};
97ae0911 932 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
b8c92cad 933 $key =~ s/-//g;
e0d10e1c 934 my $arg = 'git-config';
b8c92cad
EW
935 $arg .= ' --int' if ($o =~ /[:=]i$/);
936 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
937 if (ref $v eq 'ARRAY') {
938 chomp(my @tmp = `$arg --get-all svn.$key`);
939 @$v = @tmp if @tmp;
940 } else {
941 chomp(my $tmp = `$arg --get svn.$key`);
7774284a 942 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
b8c92cad
EW
943 $$v = $tmp;
944 }
945 }
946 }
97ae0911 947 delete @$opts{@config_only} if @config_only;
b8c92cad
EW
948}
949
79bb8d88 950sub extract_metadata {
c1927a85 951 my $id = shift or return (undef, undef, undef);
3dfab993 952 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
79bb8d88 953 \s([a-f\d\-]+)$/x);
e70dc780 954 if (!defined $rev || !$uuid || !$url) {
79bb8d88 955 # some of the original repositories I made had
82e5a82f 956 # identifiers like this:
3dfab993 957 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
79bb8d88
EW
958 }
959 return ($url, $rev, $uuid);
960}
961
c1927a85
EW
962sub cmt_metadata {
963 return extract_metadata((grep(/^git-svn-id: /,
aef4e921 964 command(qw/cat-file commit/, shift)))[-1]);
c1927a85
EW
965}
966
905f8b7d
EW
967sub working_head_info {
968 my ($head, $refs) = @_;
4dbfe2e9 969 my @args = ('log', '--no-color', '--first-parent');
05b4df31 970 my ($fh, $ctx) = command_output_pipe(@args, $head);
3dfab993 971 my $hash;
40cb8f8f 972 my %max;
3dfab993
SV
973 while (<$fh>) {
974 if ( m{^commit ($::sha1)$} ) {
975 unshift @$refs, $hash if $hash and $refs;
976 $hash = $1;
977 next;
978 }
979 next unless s{^\s*(git-svn-id:)}{$1};
980 my ($url, $rev, $uuid) = extract_metadata($_);
13c823fb 981 if (defined $url && defined $rev) {
40cb8f8f 982 next if $max{$url} and $max{$url} < $rev;
13c823fb
EW
983 if (my $gs = Git::SVN->find_by_url($url)) {
984 my $c = $gs->rev_db_get($rev);
b03c7a63 985 if ($c && $c eq $hash) {
13c823fb
EW
986 close $fh; # break the pipe
987 return ($url, $rev, $uuid, $gs);
40cb8f8f
SV
988 } else {
989 $max{$url} ||= $gs->rev_db_max;
13c823fb
EW
990 }
991 }
992 }
905f8b7d 993 }
13c823fb
EW
994 command_close_pipe($fh, $ctx);
995 (undef, undef, undef, undef);
905f8b7d
EW
996}
997
733a65aa
EW
998sub read_commit_parents {
999 my ($parents, $c) = @_;
7b02b85a
EW
1000 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1001 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1002 @{$parents->{$c}} = split(/ /, $p);
733a65aa
EW
1003}
1004
1005sub linearize_history {
1006 my ($gs, $refs) = @_;
1007 my %parents;
1008 foreach my $c (@$refs) {
1009 read_commit_parents(\%parents, $c);
1010 }
1011
1012 my @linear_refs;
1013 my %skip = ();
1014 my $last_svn_commit = $gs->last_commit;
1015 foreach my $c (reverse @$refs) {
1016 next if $c eq $last_svn_commit;
1017 last if $skip{$c};
1018
1019 unshift @linear_refs, $c;
1020 $skip{$c} = 1;
1021
1022 # we only want the first parent to diff against for linear
1023 # history, we save the rest to inject when we finalize the
1024 # svn commit
1025 my $fp_a = verify_ref("$c~1");
1026 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1027 if (!$fp_a || !$fp_b) {
1028 die "Commit $c\n",
1029 "has no parent commit, and therefore ",
1030 "nothing to diff against.\n",
1031 "You should be working from a repository ",
1032 "originally created by git-svn\n";
1033 }
1034 if ($fp_a ne $fp_b) {
1035 die "$c~1 = $fp_a, however parsing commit $c ",
1036 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1037 }
1038
1039 foreach my $p (@{$parents{$c}}) {
1040 $skip{$p} = 1;
1041 }
1042 }
1043 (\@linear_refs, \%parents);
1044}
1045
9b981fc6
EW
1046package Git::SVN;
1047use strict;
1048use warnings;
ecc712dd 1049use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
62e349d2 1050 $_repack $_repack_flags $_use_svm_props $_head
4a1bb4c3 1051 $_use_svnsync_props $no_reuse_existing $_minimize_url/;
9b981fc6
EW
1052use Carp qw/croak/;
1053use File::Path qw/mkpath/;
373274f9 1054use File::Copy qw/copy/;
9b981fc6
EW
1055use IPC::Open3;
1056
ecc712dd 1057my $_repack_nr;
9b981fc6
EW
1058# properties that we do not log:
1059my %SKIP_PROP;
1060BEGIN {
1061 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1062 svn:special svn:executable
1063 svn:entry:committed-rev
1064 svn:entry:last-author
1065 svn:entry:uuid
1066 svn:entry:committed-date/;
91b03282
EW
1067
1068 # some options are read globally, but can be overridden locally
1069 # per [svn-remote "..."] section. Command-line options will *NOT*
1070 # override options set in an [svn-remote "..."] section
c5f71ad0
SV
1071 no strict 'refs';
1072 for my $option (qw/follow_parent no_metadata use_svm_props
1073 use_svnsync_props/) {
1074 my $key = $option;
91b03282 1075 $key =~ tr/_//d;
c5f71ad0
SV
1076 my $prop = "-$option";
1077 *$option = sub {
1078 my ($self) = @_;
1079 return $self->{$prop} if exists $self->{$prop};
1080 my $k = "svn-remote.$self->{repo_id}.$key";
1081 eval { command_oneline(qw/config --get/, $k) };
1082 if ($@) {
1083 $self->{$prop} = ${"Git::SVN::_$option"};
91b03282 1084 } else {
c5f71ad0
SV
1085 my $v = command_oneline(qw/config --bool/,$k);
1086 $self->{$prop} = $v eq 'false' ? 0 : 1;
91b03282 1087 }
c5f71ad0
SV
1088 return $self->{$prop};
1089 }
91b03282 1090 }
9b981fc6
EW
1091}
1092
373274f9
EW
1093my %LOCKFILES;
1094END { unlink keys %LOCKFILES if %LOCKFILES }
1095
4bb9ed04
EW
1096sub resolve_local_globs {
1097 my ($url, $fetch, $glob_spec) = @_;
1098 return unless defined $glob_spec;
1099 my $ref = $glob_spec->{ref};
1100 my $path = $glob_spec->{path};
1101 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1102 next unless m#^refs/remotes/$ref->{regex}$#;
1103 my $p = $1;
bf655fd7
RE
1104 my $pathname = desanitize_refname($path->full_path($p));
1105 my $refname = desanitize_refname($ref->full_path($p));
4bb9ed04
EW
1106 if (my $existing = $fetch->{$pathname}) {
1107 if ($existing ne $refname) {
1108 die "Refspec conflict:\n",
1109 "existing: refs/remotes/$existing\n",
1110 " globbed: refs/remotes/$refname\n";
1111 }
1112 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
4e9f6cc7 1113 $u =~ s!^\Q$url\E(/|$)!! or die
4bb9ed04
EW
1114 "refs/remotes/$refname: '$url' not found in '$u'\n";
1115 if ($pathname ne $u) {
1116 warn "W: Refspec glob conflict ",
1117 "(ref: refs/remotes/$refname):\n",
1118 "expected path: $pathname\n",
1119 " real path: $u\n",
1120 "Continuing ahead with $u\n";
1121 next;
1122 }
1123 } else {
4bb9ed04
EW
1124 $fetch->{$pathname} = $refname;
1125 }
1126 }
1127}
1128
e98671e5
EW
1129sub parse_revision_argument {
1130 my ($base, $head) = @_;
1131 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1132 return ($base, $head);
1133 }
1134 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1135 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1136 return ($head, $head) if ($::_revision eq 'HEAD');
1137 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1138 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1139 die "revision argument: $::_revision not understood by git-svn\n";
1140}
1141
0af9c9f9 1142sub fetch_all {
4bb9ed04 1143 my ($repo_id, $remotes) = @_;
905f8b7d
EW
1144 if (ref $repo_id) {
1145 my $gs = $repo_id;
1146 $repo_id = undef;
1147 $repo_id = $gs->{repo_id};
1148 }
1149 $remotes ||= read_all_remotes();
7447b4bc
EW
1150 my $remote = $remotes->{$repo_id} or
1151 die "[svn-remote \"$repo_id\"] unknown\n";
e518192f 1152 my $fetch = $remote->{fetch};
7447b4bc 1153 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
e518192f 1154 my (@gs, @globs);
0af9c9f9 1155 my $ra = Git::SVN::Ra->new($url);
26a62d57 1156 my $uuid = $ra->get_uuid;
0af9c9f9 1157 my $head = $ra->get_latest_revnum;
28710f74 1158 my $base = defined $fetch ? $head : 0;
e518192f
EW
1159
1160 # read the max revs for wildcard expansion (branches/*, tags/*)
1161 foreach my $t (qw/branches tags/) {
1162 defined $remote->{$t} or next;
1163 push @globs, $remote->{$t};
93f2689c
EW
1164 my $max_rev = eval { tmp_config(qw/--int --get/,
1165 "svn-remote.$repo_id.${t}-maxRev") };
1166 if (defined $max_rev && ($max_rev < $base)) {
1167 $base = $max_rev;
d6d3346b
EW
1168 } elsif (!defined $max_rev) {
1169 $base = 0;
e518192f
EW
1170 }
1171 }
1172
db03cd24
EW
1173 if ($fetch) {
1174 foreach my $p (sort keys %$fetch) {
1175 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
1176 my $lr = $gs->rev_db_max;
1177 if (defined $lr) {
1178 $base = $lr if ($lr < $base);
1179 }
1180 push @gs, $gs;
0af9c9f9 1181 }
0af9c9f9 1182 }
e98671e5
EW
1183
1184 ($base, $head) = parse_revision_argument($base, $head);
e518192f 1185 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
0af9c9f9
EW
1186}
1187
47e39c55
EW
1188sub read_all_remotes {
1189 my $r = {};
8b8fc068 1190 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
47e39c55 1191 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
46cf98ba
EW
1192 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1193 $local_ref =~ s{^/}{};
1194 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
47e39c55
EW
1195 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1196 $r->{$1}->{url} = $2;
4bb9ed04
EW
1197 } elsif (m!^(.+)\.(branches|tags)=
1198 (.*):refs/remotes/(.+)\s*$/!x) {
1199 my ($p, $g) = ($3, $4);
1200 my $rs = $r->{$1}->{$2} = {
e518192f 1201 t => $2,
93f2689c 1202 remote => $1,
4bb9ed04
EW
1203 path => Git::SVN::GlobSpec->new($p),
1204 ref => Git::SVN::GlobSpec->new($g) };
1205 if (length($rs->{ref}->{right}) != 0) {
1206 die "The '*' glob character must be the last ",
1207 "character of '$g'\n";
1208 }
47e39c55
EW
1209 }
1210 }
1211 $r;
1212}
1213
ecc712dd
EW
1214sub init_vars {
1215 if (defined $_repack) {
1216 $_repack = 1000 if ($_repack <= 0);
1217 $_repack_nr = $_repack;
1218 $_repack_flags ||= '-d';
1219 }
1220}
1221
b805b44a 1222sub verify_remotes_sanity {
536c4b09 1223 return unless -d $ENV{GIT_DIR};
b805b44a
EW
1224 my %seen;
1225 foreach (command(qw/config -l/)) {
1226 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1227 if ($seen{$1}) {
1228 die "Remote ref refs/remote/$1 is tracked by",
1229 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1230 "Please resolve this ambiguity in ",
1231 "your git configuration file before ",
1232 "continuing\n";
1233 }
1234 $seen{$1} = $_;
1235 }
1236 }
1237}
1238
47e39c55 1239# we allow more chars than remotes2config.sh...
706587fc
EW
1240sub sanitize_remote_name {
1241 my ($name) = @_;
47e39c55 1242 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
706587fc
EW
1243 $name;
1244}
1245
e6434f87
EW
1246sub find_existing_remote {
1247 my ($url, $remotes) = @_;
befc9adc 1248 return undef if $no_reuse_existing;
e6434f87
EW
1249 my $existing;
1250 foreach my $repo_id (keys %$remotes) {
1251 my $u = $remotes->{$repo_id}->{url} or next;
1252 next if $u ne $url;
1253 $existing = $repo_id;
1254 last;
1255 }
1256 $existing;
1257}
b805b44a 1258
e6434f87 1259sub init_remote_config {
d8115c51 1260 my ($self, $url, $no_write) = @_;
e6434f87
EW
1261 $url =~ s!/+$!!; # strip trailing slash
1262 my $r = read_all_remotes();
1263 my $existing = find_existing_remote($url, $r);
1264 if ($existing) {
e518192f
EW
1265 unless ($no_write) {
1266 print STDERR "Using existing ",
1267 "[svn-remote \"$existing\"]\n";
1268 }
e6434f87 1269 $self->{repo_id} = $existing;
4a1bb4c3 1270 } elsif ($_minimize_url) {
e6434f87
EW
1271 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1272 $existing = find_existing_remote($min_url, $r);
1273 if ($existing) {
e518192f
EW
1274 unless ($no_write) {
1275 print STDERR "Using existing ",
1276 "[svn-remote \"$existing\"]\n";
1277 }
e6434f87
EW
1278 $self->{repo_id} = $existing;
1279 }
1280 if ($min_url ne $url) {
e518192f
EW
1281 unless ($no_write) {
1282 print STDERR "Using higher level of URL: ",
1283 "$url => $min_url\n";
1284 }
e6434f87
EW
1285 my $old_path = $self->{path};
1286 $self->{path} = $url;
4e9f6cc7 1287 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
e6434f87
EW
1288 if (length $old_path) {
1289 $self->{path} .= "/$old_path";
1290 }
1291 $url = $min_url;
1292 }
1293 }
1294 my $orig_url;
1295 if (!$existing) {
b805b44a 1296 # verify that we aren't overwriting anything:
e6434f87 1297 $orig_url = eval {
706587fc 1298 command_oneline('config', '--get',
e6434f87 1299 "svn-remote.$self->{repo_id}.url")
706587fc 1300 };
b805b44a 1301 if ($orig_url && ($orig_url ne $url)) {
e6434f87 1302 die "svn-remote.$self->{repo_id}.url already set: ",
b805b44a
EW
1303 "$orig_url\nwanted to set to: $url\n";
1304 }
9b981fc6 1305 }
e6434f87
EW
1306 my ($xrepo_id, $xpath) = find_ref($self->refname);
1307 if (defined $xpath) {
1308 die "svn-remote.$xrepo_id.fetch already set to track ",
1309 "$xpath:refs/remotes/", $self->refname, "\n";
1310 }
d8115c51
EW
1311 unless ($no_write) {
1312 command_noisy('config',
1313 "svn-remote.$self->{repo_id}.url", $url);
46cf98ba 1314 $self->{path} =~ s{^/}{};
d8115c51
EW
1315 command_noisy('config', '--add',
1316 "svn-remote.$self->{repo_id}.fetch",
1317 "$self->{path}:".$self->refname);
1318 }
9b981fc6 1319 $self->{url} = $url;
e6434f87
EW
1320}
1321
a8ae2623
EW
1322sub find_by_url { # repos_root and, path are optional
1323 my ($class, $full_url, $repos_root, $path) = @_;
56973d20 1324
1a97a506 1325 return undef unless defined $full_url;
56973d20
AR
1326 remove_username($full_url);
1327 remove_username($repos_root) if defined $repos_root;
a8ae2623
EW
1328 my $remotes = read_all_remotes();
1329 if (defined $full_url && defined $repos_root && !defined $path) {
1330 $path = $full_url;
1331 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1332 }
1333 foreach my $repo_id (keys %$remotes) {
1334 my $u = $remotes->{$repo_id}->{url} or next;
56973d20 1335 remove_username($u);
a8ae2623
EW
1336 next if defined $repos_root && $repos_root ne $u;
1337
1338 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1339 foreach (qw/branches tags/) {
1340 resolve_local_globs($u, $fetch,
1341 $remotes->{$repo_id}->{$_});
1342 }
1343 my $p = $path;
1344 unless (defined $p) {
1345 $p = $full_url;
1346 $p =~ s#^\Q$u\E(?:/|$)## or next;
1347 }
1348 foreach my $f (keys %$fetch) {
1349 next if $f ne $p;
1350 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1351 }
1352 }
1353 undef;
1354}
1355
e6434f87 1356sub init {
d8115c51 1357 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
e6434f87
EW
1358 my $self = _new($class, $repo_id, $ref_id, $path);
1359 if (defined $url) {
d8115c51 1360 $self->init_remote_config($url, $no_write);
e6434f87 1361 }
9b981fc6
EW
1362 $self;
1363}
1364
706587fc
EW
1365sub find_ref {
1366 my ($ref_id) = @_;
1367 foreach (command(qw/config -l/)) {
1368 next unless m!^svn-remote\.(.+)\.fetch=
1369 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1370 my ($repo_id, $path, $ref) = ($1, $2, $3);
1371 if ($ref eq $ref_id) {
1372 $path = '' if ($path =~ m#^\./?#);
1373 return ($repo_id, $path);
1374 }
1375 }
1376 (undef, undef, undef);
1377}
1378
9b981fc6 1379sub new {
706587fc
EW
1380 my ($class, $ref_id, $repo_id, $path) = @_;
1381 if (defined $ref_id && !defined $repo_id && !defined $path) {
1382 ($repo_id, $path) = find_ref($ref_id);
1383 if (!defined $repo_id) {
1384 die "Could not find a \"svn-remote.*.fetch\" key ",
1385 "in the repository configuration matching: ",
1386 "refs/remotes/$ref_id\n";
1387 }
1388 }
1389 my $self = _new($class, $repo_id, $ref_id, $path);
8b8fc068
EW
1390 if (!defined $self->{path} || !length $self->{path}) {
1391 my $fetch = command_oneline('config', '--get',
1392 "svn-remote.$repo_id.fetch",
1393 ":refs/remotes/$ref_id\$") or
1394 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1395 "\":refs/remotes/$ref_id\$\" in config\n";
1396 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1397 }
706587fc
EW
1398 $self->{url} = command_oneline('config', '--get',
1399 "svn-remote.$repo_id.url") or
1400 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
d6d3346b 1401 $self->rebuild;
9b981fc6
EW
1402 $self;
1403}
1404
bf655fd7
RE
1405sub refname {
1406 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1407
1408 # It cannot end with a slash /, we'll throw up on this because
1409 # SVN can't have directories with a slash in their name, either:
1410 if ($refname =~ m{/$}) {
1411 die "ref: '$refname' ends with a trailing slash, this is ",
1412 "not permitted by git nor Subversion\n";
1413 }
1414
1415 # It cannot have ASCII control character space, tilde ~, caret ^,
1416 # colon :, question-mark ?, asterisk *, space, or open bracket [
1417 # anywhere.
1418 #
1419 # Additionally, % must be escaped because it is used for escaping
1420 # and we want our escaped refname to be reversible
1421 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1422
1423 # no slash-separated component can begin with a dot .
1424 # /.* becomes /%2E*
1425 $refname =~ s{/\.}{/%2E}g;
1426
1427 # It cannot have two consecutive dots .. anywhere
1428 # .. becomes %2E%2E
1429 $refname =~ s{\.\.}{%2E%2E}g;
1430
1431 return $refname;
1432}
1433
1434sub desanitize_refname {
1435 my ($refname) = @_;
1436 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1437 return $refname;
1438}
9b981fc6 1439
26a62d57
EW
1440sub svm_uuid {
1441 my ($self) = @_;
1442 return $self->{svm}->{uuid} if $self->svm;
1443 $self->ra;
1444 unless ($self->{svm}) {
1445 die "SVM UUID not cached, and reading remotely failed\n";
1446 }
1447 $self->{svm}->{uuid};
1448}
8a49ee97 1449
26a62d57
EW
1450sub svm {
1451 my ($self) = @_;
1452 return $self->{svm} if $self->{svm};
1453 my $svm;
8a49ee97
EW
1454 # see if we have it in our config, first:
1455 eval {
26a62d57
EW
1456 my $section = "svn-remote.$self->{repo_id}";
1457 $svm = {
93f2689c
EW
1458 source => tmp_config('--get', "$section.svm-source"),
1459 uuid => tmp_config('--get', "$section.svm-uuid"),
befc9adc 1460 replace => tmp_config('--get', "$section.svm-replace"),
8a49ee97
EW
1461 }
1462 };
befc9adc
EW
1463 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1464 $self->{svm} = $svm;
1465 }
26a62d57
EW
1466 $self->{svm};
1467}
1468
1469sub _set_svm_vars {
1470 my ($self, $ra) = @_;
db03cd24
EW
1471 return $ra if $self->svm;
1472
1473 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
befc9adc 1474 "(svm:source, svm:uuid) ",
db03cd24
EW
1475 "from the following URLs:\n" );
1476 sub read_svm_props {
befc9adc
EW
1477 my ($self, $ra, $path, $r) = @_;
1478 my $props = ($ra->get_dir($path, $r))[2];
db03cd24 1479 my $src = $props->{'svm:source'};
db03cd24 1480 my $uuid = $props->{'svm:uuid'};
befc9adc 1481 return undef if (!$src || !$uuid);
26a62d57 1482
befc9adc 1483 chomp($src, $uuid);
26a62d57 1484
db03cd24
EW
1485 $uuid =~ m{^[0-9a-f\-]{30,}$}
1486 or die "doesn't look right - svm:uuid is '$uuid'\n";
befc9adc
EW
1487
1488 # the '!' is used to mark the repos_root!/relative/path
1489 $src =~ s{/?!/?}{/};
db03cd24 1490 $src =~ s{/+$}{}; # no trailing slashes please
befc9adc 1491 # username is of no interest
8a49ee97 1492 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
8a49ee97 1493
befc9adc
EW
1494 my $replace = $ra->{url};
1495 $replace .= "/$path" if length $path;
1496
db03cd24 1497 my $section = "svn-remote.$self->{repo_id}";
befc9adc
EW
1498 tmp_config("$section.svm-source", $src);
1499 tmp_config("$section.svm-replace", $replace);
1500 tmp_config("$section.svm-uuid", $uuid);
1501 $self->{svm} = {
1502 source => $src,
1503 uuid => $uuid,
1504 replace => $replace
1505 };
db03cd24
EW
1506 }
1507
1508 my $r = $ra->get_latest_revnum;
1509 my $path = $self->{path};
befc9adc 1510 my %tried;
db03cd24 1511 while (length $path) {
befc9adc
EW
1512 unless ($tried{"$self->{url}/$path"}) {
1513 return $ra if $self->read_svm_props($ra, $path, $r);
1514 $tried{"$self->{url}/$path"} = 1;
db03cd24 1515 }
befc9adc 1516 $path =~ s#/?[^/]+$##;
8a49ee97 1517 }
befc9adc
EW
1518 die "Path: '$path' should be ''\n" if $path ne '';
1519 return $ra if $self->read_svm_props($ra, $path, $r);
1520 $tried{"$self->{url}/$path"} = 1;
db03cd24
EW
1521
1522 if ($ra->{repos_root} eq $self->{url}) {
befc9adc 1523 die @err, (map { " $_\n" } keys %tried), "\n";
db03cd24
EW
1524 }
1525
1526 # nope, make sure we're connected to the repository root:
1527 my $ok;
1528 my @tried_b;
1529 $path = $ra->{svn_path};
db03cd24
EW
1530 $ra = Git::SVN::Ra->new($ra->{repos_root});
1531 while (length $path) {
befc9adc
EW
1532 unless ($tried{"$ra->{url}/$path"}) {
1533 $ok = $self->read_svm_props($ra, $path, $r);
1534 last if $ok;
1535 $tried{"$ra->{url}/$path"} = 1;
1536 }
1537 $path =~ s#/?[^/]+$##;
db03cd24 1538 }
befc9adc
EW
1539 die "Path: '$path' should be ''\n" if $path ne '';
1540 $ok ||= $self->read_svm_props($ra, $path, $r);
1541 $tried{"$ra->{url}/$path"} = 1;
db03cd24 1542 if (!$ok) {
befc9adc 1543 die @err, (map { " $_\n" } keys %tried), "\n";
db03cd24
EW
1544 }
1545 Git::SVN::Ra->new($self->{url});
8a49ee97
EW
1546}
1547
62e349d2
EW
1548sub svnsync {
1549 my ($self) = @_;
1550 return $self->{svnsync} if $self->{svnsync};
1551
1552 if ($self->no_metadata) {
1553 die "Can't have both 'noMetadata' and ",
1554 "'useSvnsyncProps' options set!\n";
1555 }
1556 if ($self->rewrite_root) {
1557 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1558 "options set!\n";
1559 }
1560
1561 my $svnsync;
1562 # see if we have it in our config, first:
1563 eval {
1564 my $section = "svn-remote.$self->{repo_id}";
1565 $svnsync = {
1566 url => tmp_config('--get', "$section.svnsync-url"),
1567 uuid => tmp_config('--get', "$section.svnsync-uuid"),
1568 }
1569 };
1570 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1571 return $self->{svnsync} = $svnsync;
1572 }
1573
1574 my $err = "useSvnsyncProps set, but failed to read " .
1575 "svnsync property: svn:sync-from-";
1576 my $rp = $self->ra->rev_proplist(0);
1577
1578 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1579 $url =~ m{^[a-z\+]+://} or
1580 die "doesn't look right - svn:sync-from-url is '$url'\n";
1581
1582 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1583 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1584 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1585
1586 my $section = "svn-remote.$self->{repo_id}";
1587 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1588 tmp_config('--add', "$section.svnsync-url", $url);
1589 return $self->{svnsync} = { url => $url, uuid => $uuid };
1590}
1591
26a62d57
EW
1592# this allows us to memoize our SVN::Ra UUID locally and avoid a
1593# remote lookup (useful for 'git svn log').
1594sub ra_uuid {
1595 my ($self) = @_;
1596 unless ($self->{ra_uuid}) {
1597 my $key = "svn-remote.$self->{repo_id}.uuid";
1598 my $uuid = eval { tmp_config('--get', $key) };
1599 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1600 $self->{ra_uuid} = $uuid;
1601 } else {
1602 die "ra_uuid called without URL\n" unless $self->{url};
1603 $self->{ra_uuid} = $self->ra->get_uuid;
1604 tmp_config('--add', $key, $self->{ra_uuid});
1605 }
1606 }
1607 $self->{ra_uuid};
1608}
1609
9b981fc6
EW
1610sub ra {
1611 my ($self) = shift;
8a49ee97 1612 my $ra = Git::SVN::Ra->new($self->{url});
91b03282
EW
1613 if ($self->use_svm_props && !$self->{svm}) {
1614 if ($self->no_metadata) {
97ae0911
EW
1615 die "Can't have both 'noMetadata' and ",
1616 "'useSvmProps' options set!\n";
62e349d2
EW
1617 } elsif ($self->use_svnsync_props) {
1618 die "Can't have both 'useSvnsyncProps' and ",
1619 "'useSvmProps' options set!\n";
91b03282 1620 }
26a62d57 1621 $ra = $self->_set_svm_vars($ra);
8a49ee97
EW
1622 $self->{-want_revprops} = 1;
1623 }
1624 $ra;
9b981fc6
EW
1625}
1626
15710b6f
EW
1627sub rel_path {
1628 my ($self) = @_;
1629 my $repos_root = $self->ra->{repos_root};
1630 return $self->{path} if ($self->{url} eq $repos_root);
0b59451c
EW
1631 my $url = $self->{url} .
1632 (length $self->{path} ? "/$self->{path}" : $self->{path});
1633 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1634 $url;
15710b6f
EW
1635}
1636
01bdab84
BS
1637# prop_walk(PATH, REV, SUB)
1638# -------------------------
1639# Recursively traverse PATH at revision REV and invoke SUB for each
1640# directory that contains a SVN property. SUB will be invoked as
1641# follows: &SUB(gs, path, props); where `gs' is this instance of
1642# Git::SVN, `path' the path to the directory where the properties
1643# `props' were found. The `path' will be relative to point of checkout,
1644# that is, if url://repo/trunk is the current Git branch, and that
1645# directory contains a sub-directory `d', SUB will be invoked with `/d/'
1646# as `path' (note the trailing `/').
1647sub prop_walk {
1648 my ($self, $path, $rev, $sub) = @_;
1649
1650 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
1651 $path =~ s#^/*#/#g;
9b981fc6 1652 my $p = $path;
01bdab84
BS
1653 # Strip the irrelevant part of the path.
1654 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
1655 # Ensure the path is terminated by a `/'.
1656 $p =~ s#/*$#/#;
1657
1658 # The properties contain all the internal SVN stuff nobody
1659 # (usually) cares about.
1660 my $interesting_props = 0;
1661 foreach (keys %{$props}) {
1662 # If it doesn't start with `svn:', it must be a
1663 # user-defined property.
1664 ++$interesting_props and next if $_ !~ /^svn:/;
1665 # FIXME: Fragile, if SVN adds new public properties,
1666 # this needs to be updated.
1667 ++$interesting_props if /^svn:(?:ignore|keywords|executable
1668 |eol-style|mime-type
1669 |externals|needs-lock)$/x;
1670 }
1671 &$sub($self, $p, $props) if $interesting_props;
1672
9b981fc6 1673 foreach (sort keys %$dirent) {
0dc03d6a 1674 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
01bdab84 1675 $self->prop_walk($path . '/' . $_, $rev, $sub);
9b981fc6
EW
1676 }
1677}
1678
3ebe8df7
EW
1679sub last_rev { ($_[0]->last_rev_commit)[0] }
1680sub last_commit { ($_[0]->last_rev_commit)[1] }
1681
9b981fc6
EW
1682# returns the newest SVN revision number and newest commit SHA1
1683sub last_rev_commit {
1684 my ($self) = @_;
1685 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1686 return ($self->{last_rev}, $self->{last_commit});
1687 }
d2866f9e 1688 my $c = ::verify_ref($self->refname.'^0');
91b03282 1689 if ($c && !$self->use_svm_props && !$self->no_metadata) {
d2866f9e 1690 my $rev = (::cmt_metadata($c))[1];
9b981fc6
EW
1691 if (defined $rev) {
1692 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1693 return ($rev, $c);
1694 }
1695 }
26a62d57
EW
1696 my $db_path = $self->db_path;
1697 unless (-e $db_path) {
1698 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
1699 return (undef, undef);
1700 }
9b981fc6
EW
1701 my $offset = -41; # from tail
1702 my $rl;
26a62d57 1703 open my $fh, '<', $db_path or croak "$db_path not readable: $!\n";
ce4b4af7
EW
1704 sysseek($fh, $offset, 2); # don't care for errors
1705 sysread($fh, $rl, 41) == 41 or return (undef, undef);
9b981fc6 1706 chomp $rl;
ce4b4af7 1707 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
9b981fc6 1708 $offset -= 41;
ce4b4af7
EW
1709 sysseek($fh, $offset, 2); # don't care for errors
1710 sysread($fh, $rl, 41) == 41 or return (undef, undef);
9b981fc6
EW
1711 chomp $rl;
1712 }
91b03282 1713 if ($c && $c ne $rl) {
26a62d57 1714 die "$db_path and ", $self->refname,
9c93fee5
EW
1715 " inconsistent!:\n$c != $rl\n";
1716 }
ce4b4af7 1717 my $rev = sysseek($fh, 0, 1) or croak $!;
9b981fc6
EW
1718 $rev = ($rev - 41) / 41;
1719 close $fh or croak $!;
1720 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1721 return ($rev, $c);
1722}
1723
3ebe8df7
EW
1724sub get_fetch_range {
1725 my ($self, $min, $max) = @_;
1726 $max ||= $self->ra->get_latest_revnum;
9c93fee5 1727 $min ||= $self->rev_db_max;
3ebe8df7 1728 (++$min, $max);
9b981fc6
EW
1729}
1730
8a49ee97 1731sub tmp_config {
93f2689c 1732 my (@args) = @_;
b7e5348c
EW
1733 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1734 my $config = "$ENV{GIT_DIR}/svn/.metadata";
38570a47 1735 if (! -f $config && -f $old_def_config) {
b7e5348c
EW
1736 rename $old_def_config, $config or
1737 die "Failed rename $old_def_config => $config: $!\n";
1738 }
8a49ee97 1739 my $old_config = $ENV{GIT_CONFIG};
93f2689c 1740 $ENV{GIT_CONFIG} = $config;
8a49ee97 1741 $@ = undef;
b4d57e5e
EW
1742 my @ret = eval {
1743 unless (-f $config) {
1744 mkfile($config);
1745 open my $fh, '>', $config or
1746 die "Can't open $config: $!\n";
1747 print $fh "; This file is used internally by ",
1748 "git-svn\n" or die
1749 "Couldn't write to $config: $!\n";
1750 print $fh "; You should not have to edit it\n" or
1751 die "Couldn't write to $config: $!\n";
1752 close $fh or die "Couldn't close $config: $!\n";
1753 }
1754 command('config', @args);
1755 };
8a49ee97
EW
1756 my $err = $@;
1757 if (defined $old_config) {
1758 $ENV{GIT_CONFIG} = $old_config;
1759 } else {
1760 delete $ENV{GIT_CONFIG};
1761 }
1762 die $err if $err;
1763 wantarray ? @ret : $ret[0];
1764}
1765
9b981fc6
EW
1766sub tmp_index_do {
1767 my ($self, $sub) = @_;
1768 my $old_index = $ENV{GIT_INDEX_FILE};
1769 $ENV{GIT_INDEX_FILE} = $self->{index};
8a49ee97 1770 $@ = undef;
b4d57e5e
EW
1771 my @ret = eval {
1772 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1773 mkpath([$dir]) unless -d $dir;
1774 &$sub;
1775 };
8a49ee97
EW
1776 my $err = $@;
1777 if (defined $old_index) {
9b981fc6
EW
1778 $ENV{GIT_INDEX_FILE} = $old_index;
1779 } else {
1780 delete $ENV{GIT_INDEX_FILE};
1781 }
8a49ee97 1782 die $err if $err;
9b981fc6
EW
1783 wantarray ? @ret : $ret[0];
1784}
1785
1786sub assert_index_clean {
1787 my ($self, $treeish) = @_;
1788
1789 $self->tmp_index_do(sub {
1790 command_noisy('read-tree', $treeish) unless -e $self->{index};
1791 my $x = command_oneline('write-tree');
1792 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1793 /^tree ($::sha1)/mo);
e8d120bd
EW
1794 return if $y eq $x;
1795
1796 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1797 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1798 command_noisy('read-tree', $treeish);
9b981fc6
EW
1799 $x = command_oneline('write-tree');
1800 if ($y ne $x) {
1801 ::fatal "trees ($treeish) $y != $x\n",
207f1a75 1802 "Something is seriously wrong...";
9b981fc6
EW
1803 }
1804 });
1805}
1806
1807sub get_commit_parents {
0af9c9f9 1808 my ($self, $log_entry) = @_;
9b981fc6 1809 my (%seen, @ret, @tmp);
0af9c9f9
EW
1810 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1811 if (my $ip = $self->{inject_parents}) {
1812 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1813 push @tmp, $commit;
9b981fc6
EW
1814 }
1815 }
d2866f9e 1816 if (my $cur = ::verify_ref($self->refname.'^0')) {
9b981fc6
EW
1817 push @tmp, $cur;
1818 }
733a65aa
EW
1819 if (my $ipd = $self->{inject_parents_dcommit}) {
1820 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
1821 push @tmp, @$commit;
1822 }
1823 }
44320b9e 1824 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
9b981fc6
EW
1825 while (my $p = shift @tmp) {
1826 next if $seen{$p};
1827 $seen{$p} = 1;
1828 push @ret, $p;
1829 # MAXPARENT is defined to 16 in commit-tree.c:
1830 last if @ret >= 16;
1831 }
1832 if (@tmp) {
44320b9e 1833 die "r$log_entry->{revision}: No room for parents:\n\t",
9b981fc6
EW
1834 join("\n\t", @tmp), "\n";
1835 }
1836 @ret;
1837}
1838
aea736cc
EW
1839sub rewrite_root {
1840 my ($self) = @_;
1841 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
1842 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1843 my $rwr = eval { command_oneline(qw/config --get/, $k) };
1844 if ($rwr) {
1845 $rwr =~ s#/+$##;
1846 if ($rwr !~ m#^[a-z\+]+://#) {
1847 die "$rwr is not a valid URL (key: $k)\n";
1848 }
1849 }
1850 $self->{-rewrite_root} = $rwr;
1851}
1852
1853sub metadata_url {
1854 my ($self) = @_;
1855 ($self->rewrite_root || $self->{url}) .
1856 (length $self->{path} ? '/' . $self->{path} : '');
1857}
1858
706587fc 1859sub full_url {
9b981fc6 1860 my ($self) = @_;
5d3b7cd5 1861 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
9b981fc6
EW
1862}
1863
1864sub do_git_commit {
0af9c9f9 1865 my ($self, $log_entry) = @_;
8a603774
EW
1866 my $lr = $self->last_rev;
1867 if (defined $lr && $lr >= $log_entry->{revision}) {
1868 die "Last fetched revision of ", $self->refname,
1869 " was r$lr, but we are about to fetch: ",
1870 "r$log_entry->{revision}!\n";
1871 }
44320b9e
EW
1872 if (my $c = $self->rev_db_get($log_entry->{revision})) {
1873 croak "$log_entry->{revision} = $c already exists! ",
9b981fc6
EW
1874 "Why are we refetching it?\n";
1875 }
db03cd24
EW
1876 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
1877 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
1878 $log_entry->{email};
44320b9e 1879 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
9b981fc6 1880
44320b9e 1881 my $tree = $log_entry->{tree};
9b981fc6
EW
1882 if (!defined $tree) {
1883 $tree = $self->tmp_index_do(sub {
1884 command_oneline('write-tree') });
1885 }
1886 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1887
1888 my @exec = ('git-commit-tree', $tree);
0af9c9f9 1889 foreach ($self->get_commit_parents($log_entry)) {
9b981fc6
EW
1890 push @exec, '-p', $_;
1891 }
1892 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1893 or croak $!;
44320b9e 1894 print $msg_fh $log_entry->{log} or croak $!;
91b03282 1895 unless ($self->no_metadata) {
8a49ee97
EW
1896 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1897 or croak $!;
9760adcc 1898 }
9b981fc6
EW
1899 $msg_fh->flush == 0 or croak $!;
1900 close $msg_fh or croak $!;
1901 chomp(my $commit = do { local $/; <$out_fh> });
1902 close $out_fh or croak $!;
1903 waitpid $pid, 0;
1904 croak $? if $?;
1905 if ($commit !~ /^$::sha1$/o) {
1906 die "Failed to commit, invalid sha1: $commit\n";
1907 }
1908
373274f9 1909 $self->rev_db_set($log_entry->{revision}, $commit, 1);
9b981fc6 1910
44320b9e 1911 $self->{last_rev} = $log_entry->{revision};
9b981fc6 1912 $self->{last_commit} = $commit;
8a49ee97
EW
1913 print "r$log_entry->{revision}";
1914 if (defined $log_entry->{svm_revision}) {
1915 print " (\@$log_entry->{svm_revision})";
26a62d57
EW
1916 $self->rev_db_set($log_entry->{svm_revision}, $commit,
1917 0, $self->svm_uuid);
8a49ee97
EW
1918 }
1919 print " = $commit ($self->{ref_id})\n";
ecc712dd
EW
1920 if (defined $_repack && (--$_repack_nr == 0)) {
1921 $_repack_nr = $_repack;
1922 # repack doesn't use any arguments with spaces in them, does it?
1923 print "Running git repack $_repack_flags ...\n";
1924 command_noisy('repack', split(/\s+/, $_repack_flags));
1925 print "Done repacking\n";
1926 }
9b981fc6
EW
1927 return $commit;
1928}
1929
fbcc1737
EW
1930sub match_paths {
1931 my ($self, $paths, $r) = @_;
4e9f6cc7 1932 return 1 if $self->{path} eq '';
d542aedb
EW
1933 if (my $path = $paths->{"/$self->{path}"}) {
1934 return ($path->{action} eq 'D') ? 0 : 1;
1935 }
4e9f6cc7 1936 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
fbcc1737
EW
1937 if (grep /$self->{path_regex}/, keys %$paths) {
1938 return 1;
1939 }
1940 my $c = '';
1941 foreach (split m#/#, $self->{path}) {
1942 $c .= "/$_";
74a81227
EW
1943 next unless ($paths->{$c} &&
1944 ($paths->{$c}->{action} =~ /^[AR]$/));
e518192f
EW
1945 if ($self->ra->check_path($self->{path}, $r) ==
1946 $SVN::Node::dir) {
fbcc1737
EW
1947 return 1;
1948 }
1949 }
1950 return 0;
1951}
1952
15710b6f
EW
1953sub find_parent_branch {
1954 my ($self, $paths, $rev) = @_;
91b03282 1955 return undef unless $self->follow_parent;
e5a0b240 1956 unless (defined $paths) {
c7eba716
EW
1957 my $err_handler = $SVN::Error::handler;
1958 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
d4eff2bd
EW
1959 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
1960 $paths =
1961 Git::SVN::Ra::dup_changed_paths($_[0]) });
c7eba716 1962 $SVN::Error::handler = $err_handler;
e5a0b240
EW
1963 }
1964 return undef unless defined $paths;
15710b6f
EW
1965
1966 # look for a parent from another branch:
7f578c55
EW
1967 my @b_path_components = split m#/#, $self->rel_path;
1968 my @a_path_components;
1969 my $i;
1970 while (@b_path_components) {
1971 $i = $paths->{'/'.join('/', @b_path_components)};
74a81227 1972 last if $i && defined $i->{copyfrom_path};
7f578c55
EW
1973 unshift(@a_path_components, pop(@b_path_components));
1974 }
74a81227
EW
1975 return undef unless defined $i && defined $i->{copyfrom_path};
1976 my $branch_from = $i->{copyfrom_path};
7f578c55
EW
1977 if (@a_path_components) {
1978 print STDERR "branch_from: $branch_from => ";
1979 $branch_from .= '/'.join('/', @a_path_components);
1980 print STDERR $branch_from, "\n";
1981 }
3ebe8df7 1982 my $r = $i->{copyfrom_rev};
15710b6f
EW
1983 my $repos_root = $self->ra->{repos_root};
1984 my $url = $self->ra->{url};
1985 my $new_url = $repos_root . $branch_from;
1986 print STDERR "Found possible branch point: ",
1987 "$new_url => ", $self->full_url, ", $r\n";
1988 $branch_from =~ s#^/##;
a8ae2623 1989 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
15710b6f 1990 unless ($gs) {
ce2a0f2f
EW
1991 my $ref_id = $self->{ref_id};
1992 $ref_id =~ s/\@\d+$//;
1993 $ref_id .= "\@$r";
15710b6f
EW
1994 # just grow a tail if we're not unique enough :x
1995 $ref_id .= '-' while find_ref($ref_id);
ce2a0f2f 1996 print STDERR "Initializing parent: $ref_id\n";
d8115c51 1997 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
15710b6f
EW
1998 }
1999 my ($r0, $parent) = $gs->find_rev_before($r, 1);
91b03282 2000 if (!defined $r0 || !defined $parent) {
d627de6b
EW
2001 my ($base, $head) = parse_revision_argument(0, $r);
2002 if ($base <= $r) {
2003 $gs->fetch($base, $r);
2004 }
15710b6f
EW
2005 ($r0, $parent) = $gs->last_rev_commit;
2006 }
ef70de96 2007 if (defined $r0 && defined $parent) {
15710b6f 2008 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
15710b6f
EW
2009 my $ed;
2010 if ($self->ra->can_do_switch) {
2e5e2480 2011 $self->assert_index_clean($parent);
8b8fc068 2012 print STDERR "Following parent with do_switch\n";
15710b6f 2013 # do_switch works with svn/trunk >= r22312, but that
2b27f6c8 2014 # is not included with SVN 1.4.3 (the latest version
15710b6f
EW
2015 # at the moment), so we can't rely on it
2016 $self->{last_commit} = $parent;
2017 $ed = SVN::Git::Fetcher->new($self);
8a603774 2018 $gs->ra->gs_do_switch($r0, $rev, $gs,
15710b6f
EW
2019 $self->full_url, $ed)
2020 or die "SVN connection failed somewhere...\n";
9ff74e95
SW
2021 } elsif ($self->ra->trees_match($new_url, $r0,
2022 $self->full_url, $rev)) {
2023 print STDERR "Trees match:\n",
2024 " $new_url\@$r0\n",
2025 " ${\$self->full_url}\@$rev\n",
2026 "Following parent with no changes\n";
2027 $self->tmp_index_do(sub {
2028 command_noisy('read-tree', $parent);
2029 });
2030 $self->{last_commit} = $parent;
15710b6f 2031 } else {
8b8fc068 2032 print STDERR "Following parent with do_update\n";
15710b6f 2033 $ed = SVN::Git::Fetcher->new($self);
8a603774 2034 $self->ra->gs_do_update($rev, $rev, $self, $ed)
15710b6f
EW
2035 or die "SVN connection failed somewhere...\n";
2036 }
f7c3fc4a 2037 print STDERR "Successfully followed parent\n";
15710b6f
EW
2038 return $self->make_log_entry($rev, [$parent], $ed);
2039 }
15710b6f
EW
2040 return undef;
2041}
2042
9b981fc6 2043sub do_fetch {
706587fc 2044 my ($self, $paths, $rev) = @_;
15710b6f 2045 my $ed;
9b981fc6 2046 my ($last_rev, @parents);
b9dffd8c
EW
2047 if (my $lc = $self->last_commit) {
2048 # we can have a branch that was deleted, then re-added
2049 # under the same name but copied from another path, in
2050 # which case we'll have multiple parents (we don't
2051 # want to break the original ref, nor lose copypath info):
2052 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2053 push @{$log_entry->{parents}}, $lc;
2054 return $log_entry;
2055 }
15710b6f 2056 $ed = SVN::Git::Fetcher->new($self);
9b981fc6 2057 $last_rev = $self->{last_rev};
b9dffd8c
EW
2058 $ed->{c} = $lc;
2059 @parents = ($lc);
9b981fc6
EW
2060 } else {
2061 $last_rev = $rev;
15710b6f
EW
2062 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2063 return $log_entry;
2064 }
2065 $ed = SVN::Git::Fetcher->new($self);
9b981fc6 2066 }
8a603774 2067 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
9b981fc6
EW
2068 die "SVN connection failed somewhere...\n";
2069 }
2070 $self->make_log_entry($rev, \@parents, $ed);
2071}
2072
97f6987a
EW
2073sub get_untracked {
2074 my ($self, $ed) = @_;
2075 my @out;
2076 my $h = $ed->{empty};
9b981fc6
EW
2077 foreach (sort keys %$h) {
2078 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
97f6987a 2079 push @out, " $act: " . uri_encode($_);
9b981fc6
EW
2080 warn "W: $act: $_\n";
2081 }
2082 foreach my $t (qw/dir_prop file_prop/) {
97f6987a 2083 $h = $ed->{$t} or next;
9b981fc6
EW
2084 foreach my $path (sort keys %$h) {
2085 my $ppath = $path eq '' ? '.' : $path;
2086 foreach my $prop (sort keys %{$h->{$path}}) {
1ce255dc 2087 next if $SKIP_PROP{$prop};
9b981fc6 2088 my $v = $h->{$path}->{$prop};
97f6987a
EW
2089 my $t_ppath_prop = "$t: " .
2090 uri_encode($ppath) . ' ' .
2091 uri_encode($prop);
9b981fc6 2092 if (defined $v) {
97f6987a
EW
2093 push @out, " +$t_ppath_prop " .
2094 uri_encode($v);
9b981fc6 2095 } else {
97f6987a 2096 push @out, " -$t_ppath_prop";
9b981fc6
EW
2097 }
2098 }
2099 }
2100 }
2101 foreach my $t (qw/absent_file absent_directory/) {
97f6987a 2102 $h = $ed->{$t} or next;
9b981fc6
EW
2103 foreach my $parent (sort keys %$h) {
2104 foreach my $path (sort @{$h->{$parent}}) {
97f6987a
EW
2105 push @out, " $t: " .
2106 uri_encode("$parent/$path");
9b981fc6
EW
2107 warn "W: $t: $parent/$path ",
2108 "Insufficient permissions?\n";
2109 }
2110 }
2111 }
97f6987a 2112 \@out;
9b981fc6
EW
2113}
2114
1c8443b0
EW
2115sub parse_svn_date {
2116 my $date = shift || return '+0000 1970-01-01 00:00:00';
2117 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2118 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
2119 croak "Unable to parse date: $date\n";
2120 "+0000 $Y-$m-$d $H:$M:$S";
2121}
2122
2123sub check_author {
2124 my ($author) = @_;
2125 if (!defined $author || length $author == 0) {
2126 $author = '(no author)';
2127 }
2128 if (defined $::_authors && ! defined $::users{$author}) {
2129 die "Author: $author not defined in $::_authors file\n";
2130 }
2131 $author;
2132}
2133
9b981fc6 2134sub make_log_entry {
97f6987a
EW
2135 my ($self, $rev, $parents, $ed) = @_;
2136 my $untracked = $self->get_untracked($ed);
2137
9b981fc6 2138 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
97f6987a
EW
2139 print $un "r$rev\n" or croak $!;
2140 print $un $_, "\n" foreach @$untracked;
2141 my %log_entry = ( parents => $parents || [], revision => $rev,
2142 log => '');
fbcc1737 2143
8a49ee97 2144 my $headrev;
fbcc1737 2145 my $logged = delete $self->{logged_rev_props};
8a49ee97 2146 if (!$logged || $self->{-want_revprops}) {
fbcc1737
EW
2147 my $rp = $self->ra->rev_proplist($rev);
2148 foreach (sort keys %$rp) {
2149 my $v = $rp->{$_};
2150 if (/^svn:(author|date|log)$/) {
2151 $log_entry{$1} = $v;
8a49ee97
EW
2152 } elsif ($_ eq 'svm:headrev') {
2153 $headrev = $v;
fbcc1737
EW
2154 } else {
2155 print $un " rev_prop: ", uri_encode($_), ' ',
2156 uri_encode($v), "\n";
2157 }
9b981fc6 2158 }
fbcc1737
EW
2159 } else {
2160 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
9b981fc6
EW
2161 }
2162 close $un or croak $!;
97f6987a 2163
9b981fc6 2164 $log_entry{date} = parse_svn_date($log_entry{date});
9b981fc6 2165 $log_entry{log} .= "\n";
db03cd24
EW
2166 my $author = $log_entry{author} = check_author($log_entry{author});
2167 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
2168 : ($author, undef);
91b03282 2169 if (defined $headrev && $self->use_svm_props) {
aea736cc
EW
2170 if ($self->rewrite_root) {
2171 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2172 "options set!\n";
2173 }
8a49ee97 2174 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
befc9adc
EW
2175 # we don't want "SVM: initializing mirror for junk" ...
2176 return undef if $r == 0;
2177 my $svm = $self->svm;
2178 if ($uuid ne $svm->{uuid}) {
8a49ee97 2179 die "UUID mismatch on SVM path:\n",
befc9adc 2180 "expected: $svm->{uuid}\n",
8a49ee97
EW
2181 " got: $uuid\n";
2182 }
befc9adc
EW
2183 my $full_url = $self->full_url;
2184 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2185 die "Failed to replace '$svm->{replace}' with ",
2186 "'$svm->{source}' in $full_url\n";
18ea92bd
SV
2187 # throw away username for storing in records
2188 remove_username($full_url);
8a49ee97
EW
2189 $log_entry{metadata} = "$full_url\@$r $uuid";
2190 $log_entry{svm_revision} = $r;
db03cd24 2191 $email ||= "$author\@$uuid"
62e349d2
EW
2192 } elsif ($self->use_svnsync_props) {
2193 my $full_url = $self->svnsync->{url};
2194 $full_url .= "/$self->{path}" if length $self->{path};
ce118739 2195 remove_username($full_url);
62e349d2
EW
2196 my $uuid = $self->svnsync->{uuid};
2197 $log_entry{metadata} = "$full_url\@$rev $uuid";
2198 $email ||= "$author\@$uuid"
8a49ee97 2199 } else {
ce118739
AR
2200 my $url = $self->metadata_url;
2201 remove_username($url);
2202 $log_entry{metadata} = "$url\@$rev " .
26a62d57 2203 $self->ra->get_uuid;
db03cd24 2204 $email ||= "$author\@" . $self->ra->get_uuid;
8a49ee97 2205 }
db03cd24
EW
2206 $log_entry{name} = $name;
2207 $log_entry{email} = $email;
9b981fc6
EW
2208 \%log_entry;
2209}
2210
2211sub fetch {
3ebe8df7 2212 my ($self, $min_rev, $max_rev, @parents) = @_;
9b981fc6 2213 my ($last_rev, $last_commit) = $self->last_rev_commit;
3ebe8df7 2214 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
e518192f 2215 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
9b981fc6
EW
2216}
2217
2218sub set_tree_cb {
2219 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
490f49ea
EW
2220 $self->{inject_parents} = { $rev => $tree };
2221 $self->fetch(undef, undef);
9b981fc6
EW
2222}
2223
2224sub set_tree {
2225 my ($self, $tree) = (shift, shift);
1ce255dc 2226 my $log_entry = ::get_commit_entry($tree);
9b981fc6 2227 unless ($self->{last_rev}) {
207f1a75 2228 fatal("Must have an existing revision to commit");
9b981fc6 2229 }
61395354
EW
2230 my %ed_opts = ( r => $self->{last_rev},
2231 log => $log_entry->{log},
2232 ra => $self->ra,
2233 tree_a => $self->{last_commit},
2234 tree_b => $tree,
2235 editor_cb => sub {
2236 $self->set_tree_cb($log_entry, $tree, @_) },
2237 svn_path => $self->{path} );
2238 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
9b981fc6
EW
2239 print "No changes\nr$self->{last_rev} = $tree\n";
2240 }
9b981fc6
EW
2241}
2242
f0ecca10
EW
2243sub rebuild {
2244 my ($self) = @_;
26a62d57 2245 my $db_path = $self->db_path;
d6d3346b
EW
2246 return if (-e $db_path && ! -z $db_path);
2247 return unless ::verify_ref($self->refname.'^0');
26a62d57
EW
2248 if (-f $self->{db_root}) {
2249 rename $self->{db_root}, $db_path or die
2250 "rename $self->{db_root} => $db_path failed: $!\n";
2251 my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#);
2252 symlink $base, $self->{db_root} or die
2253 "symlink $base => $self->{db_root} failed: $!\n";
2254 return;
2255 }
2256 print "Rebuilding $db_path ...\n";
eeebd8d8 2257 my ($log, $ctx) = command_output_pipe("log", '--no-color', $self->refname);
f0ecca10
EW
2258 my $latest;
2259 my $full_url = $self->full_url;
18ea92bd 2260 remove_username($full_url);
f0ecca10 2261 my $svn_uuid;
3dfab993
SV
2262 my $c;
2263 while (<$log>) {
2264 if ( m{^commit ($::sha1)$} ) {
2265 $c = $1;
2266 next;
2267 }
2268 next unless s{^\s*(git-svn-id:)}{$1};
2269 my ($url, $rev, $uuid) = ::extract_metadata($_);
18ea92bd 2270 remove_username($url);
f0ecca10
EW
2271
2272 # ignore merges (from set-tree)
2273 next if (!defined $rev || !$uuid);
2274
2275 # if we merged or otherwise started elsewhere, this is
2276 # how we break out of it
2277 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
2278 ($full_url && $url && ($url ne $full_url))) {
2279 next;
2280 }
2281 $latest ||= $rev;
2282 $svn_uuid ||= $uuid;
2283
2284 $self->rev_db_set($rev, $c);
2285 print "r$rev = $c\n";
2286 }
3dfab993 2287 command_close_pipe($log, $ctx);
26a62d57 2288 print "Done rebuilding $db_path\n";
f0ecca10
EW
2289}
2290
9b981fc6
EW
2291# rev_db:
2292# Tie::File seems to be prone to offset errors if revisions get sparse,
2293# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2294# one of my favorite modules is out :< Next up would be one of the DBM
2295# modules, but I'm not sure which is most portable... So I'll just
2296# go with something that's plain-text, but still capable of
2297# being randomly accessed. So here's my ultra-simple fixed-width
2298# database. All records are 40 characters + "\n", so it's easy to seek
2299# to a revision: (41 * rev) is the byte offset.
2300# A record of 40 0s denotes an empty revision.
2301# And yes, it's still pretty fast (faster than Tie::File).
97ae0911 2302# These files are disposable unless noMetadata or useSvmProps is set
9b981fc6 2303
26a62d57
EW
2304sub _rev_db_set {
2305 my ($fh, $rev, $commit) = @_;
2306 my $offset = $rev * 41;
2307 # assume that append is the common case:
2308 seek $fh, 0, 2 or croak $!;
2309 my $pos = tell $fh;
2310 if ($pos < $offset) {
2311 for (1 .. (($offset - $pos) / 41)) {
2312 print $fh (('0' x 40),"\n") or croak $!;
2313 }
2314 }
2315 seek $fh, $offset, 0 or croak $!;
2316 print $fh $commit,"\n" or croak $!;
2317}
2318
2319sub mkfile {
2320 my ($path) = @_;
2321 unless (-e $path) {
2322 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2323 mkpath([$dir]) unless -d $dir;
2324 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2325 close $fh or die "Couldn't close (create) $path: $!\n";
2326 }
2327}
2328
9b981fc6 2329sub rev_db_set {
26a62d57
EW
2330 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2331 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2332 my $db = $self->db_path($uuid);
2333 my $db_lock = "$db.lock";
373274f9
EW
2334 my $sig;
2335 if ($update_ref) {
2336 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2337 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2338 }
26a62d57
EW
2339 mkfile($db);
2340
373274f9 2341 $LOCKFILES{$db_lock} = 1;
97ae0911 2342 my $sync;
97ae0911
EW
2343 # both of these options make our .rev_db file very, very important
2344 # and we can't afford to lose it because rebuild() won't work
2345 if ($self->use_svm_props || $self->no_metadata) {
2346 $sync = 1;
373274f9 2347 copy($db, $db_lock) or die "rev_db_set(@_): ",
26a62d57 2348 "Failed to copy: ",
373274f9
EW
2349 "$db => $db_lock ($!)\n";
2350 } else {
2351 rename $db, $db_lock or die "rev_db_set(@_): ",
26a62d57 2352 "Failed to rename: ",
373274f9
EW
2353 "$db => $db_lock ($!)\n";
2354 }
26a62d57
EW
2355 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2356 _rev_db_set($fh, $rev, $commit);
97ae0911
EW
2357 if ($sync) {
2358 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2359 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2360 }
9b981fc6 2361 close $fh or croak $!;
373274f9 2362 if ($update_ref) {
1e889ef3 2363 $_head = $self;
373274f9
EW
2364 command_noisy('update-ref', '-m', "r$rev",
2365 $self->refname, $commit);
2366 }
2367 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2368 "$db_lock => $db ($!)\n";
2369 delete $LOCKFILES{$db_lock};
2370 if ($update_ref) {
2371 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2372 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2373 kill $sig, $$ if defined $sig;
2374 }
9b981fc6
EW
2375}
2376
9c93fee5
EW
2377sub rev_db_max {
2378 my ($self) = @_;
d6d3346b 2379 $self->rebuild;
26a62d57
EW
2380 my $db_path = $self->db_path;
2381 my @stat = stat $db_path or return 0;
2382 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
9c93fee5
EW
2383 my $max = $stat[7] / 41;
2384 (($max > 0) ? $max - 1 : 0);
2385}
2386
9b981fc6 2387sub rev_db_get {
26a62d57 2388 my ($self, $rev, $uuid) = @_;
9b981fc6
EW
2389 my $ret;
2390 my $offset = $rev * 41;
26a62d57
EW
2391 my $db_path = $self->db_path($uuid);
2392 return undef unless -e $db_path;
2393 open my $fh, '<', $db_path or croak $!;
ce4b4af7
EW
2394 if (sysseek($fh, $offset, 0) == $offset) {
2395 my $read = sysread($fh, $ret, 40);
2396 $ret = undef if ($read != 40 || $ret eq ('0'x40));
9b981fc6
EW
2397 }
2398 close $fh or croak $!;
2399 $ret;
2400}
2401
15710b6f
EW
2402sub find_rev_before {
2403 my ($self, $rev, $eq_ok) = @_;
2404 --$rev unless $eq_ok;
2405 while ($rev > 0) {
2406 if (my $c = $self->rev_db_get($rev)) {
2407 return ($rev, $c);
2408 }
2409 --$rev;
2410 }
2411 return (undef, undef);
2412}
2413
9b981fc6 2414sub _new {
706587fc
EW
2415 my ($class, $repo_id, $ref_id, $path) = @_;
2416 unless (defined $repo_id && length $repo_id) {
2417 $repo_id = $Git::SVN::default_repo_id;
2418 }
2419 unless (defined $ref_id && length $ref_id) {
8b8fc068 2420 $_[2] = $ref_id = $Git::SVN::default_ref_id;
706587fc
EW
2421 }
2422 $_[1] = $repo_id = sanitize_remote_name($repo_id);
2423 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2424 $_[3] = $path = '' unless (defined $path);
b4d57e5e 2425 mkpath(["$ENV{GIT_DIR}/svn"]);
26a62d57
EW
2426 bless {
2427 ref_id => $ref_id, dir => $dir, index => "$dir/index",
8a49ee97 2428 path => $path, config => "$ENV{GIT_DIR}/svn/config",
26a62d57
EW
2429 db_root => "$dir/.rev_db", repo_id => $repo_id }, $class;
2430}
2431
2432sub db_path {
2433 my ($self, $uuid) = @_;
2434 $uuid ||= $self->ra_uuid;
2435 "$self->{db_root}.$uuid";
9b981fc6
EW
2436}
2437
1c8443b0
EW
2438sub uri_encode {
2439 my ($f) = @_;
2440 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2441 $f
2442}
9b981fc6 2443
18ea92bd
SV
2444sub remove_username {
2445 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2446}
2447
d976acfd
EW
2448package Git::SVN::Prompt;
2449use strict;
2450use warnings;
2451require SVN::Core;
2452use vars qw/$_no_auth_cache $_username/;
2453
2454sub simple {
30d055aa
EW
2455 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2456 $may_save = undef if $_no_auth_cache;
2457 $default_username = $_username if defined $_username;
2458 if (defined $default_username && length $default_username) {
2459 if (defined $realm && length $realm) {
6f729591
EW
2460 print STDERR "Authentication realm: $realm\n";
2461 STDERR->flush;
30d055aa
EW
2462 }
2463 $cred->username($default_username);
2464 } else {
d976acfd 2465 username($cred, $realm, $may_save, $pool);
30d055aa
EW
2466 }
2467 $cred->password(_read_password("Password for '" .
2468 $cred->username . "': ", $realm));
2469 $cred->may_save($may_save);
2470 $SVN::_Core::SVN_NO_ERROR;
2471}
2472
d976acfd 2473sub ssl_server_trust {
30d055aa
EW
2474 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2475 $may_save = undef if $_no_auth_cache;
6f729591 2476 print STDERR "Error validating server certificate for '$realm':\n";
fd499bcc
ER
2477 {
2478 no warnings 'once';
2479 # All variables SVN::Auth::SSL::* are used only once,
2480 # so we're shutting up Perl warnings about this.
2481 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
2482 print STDERR " - The certificate is not issued ",
2483 "by a trusted authority. Use the\n",
2484 " fingerprint to validate ",
2485 "the certificate manually!\n";
2486 }
2487 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
2488 print STDERR " - The certificate hostname ",
2489 "does not match.\n";
2490 }
2491 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
2492 print STDERR " - The certificate is not yet valid.\n";
2493 }
2494 if ($failures & $SVN::Auth::SSL::EXPIRED) {
2495 print STDERR " - The certificate has expired.\n";
2496 }
2497 if ($failures & $SVN::Auth::SSL::OTHER) {
2498 print STDERR " - The certificate has ",
2499 "an unknown error.\n";
2500 }
2501 } # no warnings 'once'
6f729591
EW
2502 printf STDERR
2503 "Certificate information:\n".
30d055aa
EW
2504 " - Hostname: %s\n".
2505 " - Valid: from %s until %s\n".
2506 " - Issuer: %s\n".
2507 " - Fingerprint: %s\n",
2508 map $cert_info->$_, qw(hostname valid_from valid_until
6f729591 2509 issuer_dname fingerprint);
30d055aa
EW
2510 my $choice;
2511prompt:
6f729591 2512 print STDERR $may_save ?
30d055aa
EW
2513 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2514 "(R)eject or accept (t)emporarily? ";
6f729591 2515 STDERR->flush;
30d055aa
EW
2516 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2517 if ($choice =~ /^t$/i) {
2518 $cred->may_save(undef);
2519 } elsif ($choice =~ /^r$/i) {
2520 return -1;
2521 } elsif ($may_save && $choice =~ /^p$/i) {
2522 $cred->may_save($may_save);
2523 } else {
2524 goto prompt;
2525 }
2526 $cred->accepted_failures($failures);
2527 $SVN::_Core::SVN_NO_ERROR;
2528}
2529
d976acfd 2530sub ssl_client_cert {
30d055aa
EW
2531 my ($cred, $realm, $may_save, $pool) = @_;
2532 $may_save = undef if $_no_auth_cache;
6f729591
EW
2533 print STDERR "Client certificate filename: ";
2534 STDERR->flush;
30d055aa
EW
2535 chomp(my $filename = <STDIN>);
2536 $cred->cert_file($filename);
2537 $cred->may_save($may_save);
2538 $SVN::_Core::SVN_NO_ERROR;
2539}
2540
d976acfd 2541sub ssl_client_cert_pw {
30d055aa
EW
2542 my ($cred, $realm, $may_save, $pool) = @_;
2543 $may_save = undef if $_no_auth_cache;
2544 $cred->password(_read_password("Password: ", $realm));
2545 $cred->may_save($may_save);
2546 $SVN::_Core::SVN_NO_ERROR;
2547}
2548
d976acfd 2549sub username {
30d055aa
EW
2550 my ($cred, $realm, $may_save, $pool) = @_;
2551 $may_save = undef if $_no_auth_cache;
2552 if (defined $realm && length $realm) {
6f729591 2553 print STDERR "Authentication realm: $realm\n";
30d055aa
EW
2554 }
2555 my $username;
2556 if (defined $_username) {
2557 $username = $_username;
2558 } else {
6f729591
EW
2559 print STDERR "Username: ";
2560 STDERR->flush;
30d055aa
EW
2561 chomp($username = <STDIN>);
2562 }
2563 $cred->username($username);
2564 $cred->may_save($may_save);
2565 $SVN::_Core::SVN_NO_ERROR;
2566}
2567
2568sub _read_password {
2569 my ($prompt, $realm) = @_;
6f729591
EW
2570 print STDERR $prompt;
2571 STDERR->flush;
30d055aa
EW
2572 require Term::ReadKey;
2573 Term::ReadKey::ReadMode('noecho');
2574 my $password = '';
2575 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2576 last if $key =~ /[\012\015]/; # \n\r
2577 $password .= $key;
2578 }
2579 Term::ReadKey::ReadMode('restore');
6f729591
EW
2580 print STDERR "\n";
2581 STDERR->flush;
30d055aa
EW
2582 $password;
2583}
2584
27a1a801
EW
2585package SVN::Git::Fetcher;
2586use vars qw/@ISA/;
2587use strict;
2588use warnings;
2589use Carp qw/croak/;
2590use IO::File qw//;
90c1b15d 2591use Digest::MD5;
27a1a801
EW
2592
2593# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2594sub new {
2595 my ($class, $git_svn) = @_;
2596 my $self = SVN::Delta::Editor->new;
2597 bless $self, $class;
1c8443b0 2598 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
d2a9a87b
EW
2599 $self->{empty} = {};
2600 $self->{dir_prop} = {};
2601 $self->{file_prop} = {};
2602 $self->{absent_dir} = {};
2603 $self->{absent_file} = {};
ef3cfaad 2604 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
27a1a801
EW
2605 $self;
2606}
2607
8b8fc068
EW
2608sub set_path_strip {
2609 my ($self, $path) = @_;
4e9f6cc7 2610 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
8b8fc068
EW
2611}
2612
d2a9a87b
EW
2613sub open_root {
2614 { path => '' };
2615}
2616
2617sub open_directory {
2618 my ($self, $path, $pb, $rev) = @_;
2619 { path => $path };
2620}
2621
706587fc
EW
2622sub git_path {
2623 my ($self, $path) = @_;
2b27f6c8
EW
2624 if ($self->{path_strip}) {
2625 $path =~ s!$self->{path_strip}!! or
2626 die "Failed to strip path '$path' ($self->{path_strip})\n";
2627 }
706587fc
EW
2628 $path;
2629}
2630
27a1a801
EW
2631sub delete_entry {
2632 my ($self, $path, $rev, $pb) = @_;
4a87db0e 2633
706587fc 2634 my $gpath = $self->git_path($path);
8a603774
EW
2635 return undef if ($gpath eq '');
2636
4a87db0e 2637 # remove entire directories.
706587fc 2638 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
4a87db0e
EW
2639 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2640 -r --name-only -z/,
706587fc 2641 $self->{c}, '--', $gpath);
4a87db0e
EW
2642 local $/ = "\0";
2643 while (<$ls>) {
ef3cfaad
EW
2644 chomp;
2645 $self->{gii}->remove($_);
9e3cdbd4 2646 print "\tD\t$_\n" unless $::_q;
4a87db0e 2647 }
9e3cdbd4 2648 print "\tD\t$gpath/\n" unless $::_q;
4a87db0e
EW
2649 command_close_pipe($ls, $ctx);
2650 $self->{empty}->{$path} = 0
2651 } else {
ef3cfaad 2652 $self->{gii}->remove($gpath);
9e3cdbd4 2653 print "\tD\t$gpath\n" unless $::_q;
4a87db0e 2654 }
27a1a801
EW
2655 undef;
2656}
2657
2658sub open_file {
2659 my ($self, $path, $pb, $rev) = @_;
706587fc
EW
2660 my $gpath = $self->git_path($path);
2661 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
27a1a801 2662 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
006ede5e
EW
2663 unless (defined $mode && defined $blob) {
2664 die "$path was not found in commit $self->{c} (r$rev)\n";
2665 }
27a1a801 2666 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
0864e3ba 2667 pool => SVN::Pool->new, action => 'M' };
27a1a801
EW
2668}
2669
2670sub add_file {
2671 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
d2a9a87b
EW
2672 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2673 delete $self->{empty}->{$dir};
27a1a801 2674 { path => $path, mode_a => 100644, mode_b => 100644,
0864e3ba 2675 pool => SVN::Pool->new, action => 'A' };
27a1a801
EW
2676}
2677
d2a9a87b
EW
2678sub add_directory {
2679 my ($self, $path, $cp_path, $cp_rev) = @_;
2680 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2681 delete $self->{empty}->{$dir};
2682 $self->{empty}->{$path} = 1;
2683 { path => $path };
2684}
2685
2686sub change_dir_prop {
2687 my ($self, $db, $prop, $value) = @_;
2688 $self->{dir_prop}->{$db->{path}} ||= {};
2689 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2690 undef;
2691}
2692
2693sub absent_directory {
2694 my ($self, $path, $pb) = @_;
2695 $self->{absent_dir}->{$pb->{path}} ||= [];
2696 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2697 undef;
2698}
2699
2700sub absent_file {
2701 my ($self, $path, $pb) = @_;
2702 $self->{absent_file}->{$pb->{path}} ||= [];
2703 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2704 undef;
2705}
2706
27a1a801
EW
2707sub change_file_prop {
2708 my ($self, $fb, $prop, $value) = @_;
2709 if ($prop eq 'svn:executable') {
2710 if ($fb->{mode_b} != 120000) {
2711 $fb->{mode_b} = defined $value ? 100755 : 100644;
2712 }
2713 } elsif ($prop eq 'svn:special') {
2714 $fb->{mode_b} = defined $value ? 120000 : 100644;
d2a9a87b
EW
2715 } else {
2716 $self->{file_prop}->{$fb->{path}} ||= {};
2717 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
27a1a801
EW
2718 }
2719 undef;
2720}
2721
2722sub apply_textdelta {
2723 my ($self, $fb, $exp) = @_;
2724 my $fh = IO::File->new_tmpfile;
2725 $fh->autoflush(1);
2726 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2727 # (but $base does not,) so dup() it for reading in close_file
2728 open my $dup, '<&', $fh or croak $!;
2729 my $base = IO::File->new_tmpfile;
2730 $base->autoflush(1);
2731 if ($fb->{blob}) {
2732 defined (my $pid = fork) or croak $!;
2733 if (!$pid) {
2734 open STDOUT, '>&', $base or croak $!;
2735 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2736 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2737 }
2738 waitpid $pid, 0;
2739 croak $? if $?;
2740
2741 if (defined $exp) {
2742 seek $base, 0, 0 or croak $!;
2743 my $md5 = Digest::MD5->new;
2744 $md5->addfile($base);
2745 my $got = $md5->hexdigest;
2746 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2747 "expected: $exp\n",
2748 " got: $got\n" if ($got ne $exp);
2749 }
2750 }
2751 seek $base, 0, 0 or croak $!;
2752 $fb->{fh} = $dup;
2753 $fb->{base} = $base;
2754 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2755}
2756
2757sub close_file {
2758 my ($self, $fb, $exp) = @_;
2759 my $hash;
706587fc 2760 my $path = $self->git_path($fb->{path});
27a1a801 2761 if (my $fh = $fb->{fh}) {
7faf0686
EW
2762 if (defined $exp) {
2763 seek($fh, 0, 0) or croak $!;
2764 my $md5 = Digest::MD5->new;
2765 $md5->addfile($fh);
2766 my $got = $md5->hexdigest;
2767 if ($got ne $exp) {
2768 die "Checksum mismatch: $path\n",
2769 "expected: $exp\n got: $got\n";
2770 }
2771 }
bcd8ee5b 2772 sysseek($fh, 0, 0) or croak $!;
27a1a801 2773 if ($fb->{mode_b} == 120000) {
bcd8ee5b 2774 sysread($fh, my $buf, 5) == 5 or croak $!;
27a1a801
EW
2775 $buf eq 'link ' or die "$path has mode 120000",
2776 "but is not a link\n";
2777 }
2778 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2779 if (!$pid) {
2780 open STDIN, '<&', $fh or croak $!;
2781 exec qw/git-hash-object -w --stdin/ or croak $!;
2782 }
2783 chomp($hash = do { local $/; <$out> });
2784 close $out or croak $!;
2785 close $fh or croak $!;
2786 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2787 close $fb->{base} or croak $!;
2788 } else {
2789 $hash = $fb->{blob} or die "no blob information\n";
2790 }
2791 $fb->{pool}->clear;
ef3cfaad 2792 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
9e3cdbd4 2793 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
27a1a801
EW
2794 undef;
2795}
2796
2797sub abort_edit {
2798 my $self = shift;
ef3cfaad
EW
2799 $self->{nr} = $self->{gii}->{nr};
2800 delete $self->{gii};
27a1a801
EW
2801 $self->SUPER::abort_edit(@_);
2802}
2803
2804sub close_edit {
2805 my $self = shift;
dad73c0b 2806 $self->{git_commit_ok} = 1;
ef3cfaad
EW
2807 $self->{nr} = $self->{gii}->{nr};
2808 delete $self->{gii};
27a1a801
EW
2809 $self->SUPER::close_edit(@_);
2810}
1a82e793 2811
a5e0cedc 2812package SVN::Git::Editor;
24e22aa8 2813use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
a5e0cedc
EW
2814use strict;
2815use warnings;
2816use Carp qw/croak/;
2817use IO::File;
90c1b15d 2818use Digest::MD5;
a5e0cedc
EW
2819
2820sub new {
61395354
EW
2821 my ($class, $opts) = @_;
2822 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
2823 die "$_ required!\n" unless (defined $opts->{$_});
2824 }
2825
2826 my $pool = SVN::Pool->new;
2827 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
2828 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
2829 $opts->{r}, $mods);
2830
2831 # $opts->{ra} functions should not be used after this:
2832 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
2833 $opts->{editor_cb}, $pool);
2834 my $self = SVN::Delta::Editor->new(@ce, $pool);
a5e0cedc 2835 bless $self, $class;
61395354
EW
2836 foreach (qw/svn_path r tree_a tree_b/) {
2837 $self->{$_} = $opts->{$_};
a5e0cedc 2838 }
61395354
EW
2839 $self->{url} = $opts->{ra}->{url};
2840 $self->{mods} = $mods;
2841 $self->{types} = $types;
2842 $self->{pool} = $pool;
a5e0cedc
EW
2843 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2844 $self->{rm} = { };
d3a840dc
EW
2845 $self->{path_prefix} = length $self->{svn_path} ?
2846 "$self->{svn_path}/" : '';
a5e0cedc
EW
2847 return $self;
2848}
2849
61395354
EW
2850sub generate_diff {
2851 my ($tree_a, $tree_b) = @_;
2852 my @diff_tree = qw(diff-tree -z -r);
24e22aa8
EW
2853 if ($_cp_similarity) {
2854 push @diff_tree, "-C$_cp_similarity";
61395354
EW
2855 } else {
2856 push @diff_tree, '-C';
2857 }
24e22aa8
EW
2858 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2859 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
61395354
EW
2860 push @diff_tree, $tree_a, $tree_b;
2861 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
2862 local $/ = "\0";
2863 my $state = 'meta';
2864 my @mods;
2865 while (<$diff_fh>) {
2866 chomp $_; # this gets rid of the trailing "\0"
2867 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
2868 $::sha1\s($::sha1)\s
2869 ([MTCRAD])\d*$/xo) {
2870 push @mods, { mode_a => $1, mode_b => $2,
2871 sha1_b => $3, chg => $4 };
2872 if ($4 =~ /^(?:C|R)$/) {
2873 $state = 'file_a';
2874 } else {
2875 $state = 'file_b';
2876 }
2877 } elsif ($state eq 'file_a') {
2878 my $x = $mods[$#mods] or croak "Empty array\n";
2879 if ($x->{chg} !~ /^(?:C|R)$/) {
2880 croak "Error parsing $_, $x->{chg}\n";
2881 }
2882 $x->{file_a} = $_;
2883 $state = 'file_b';
2884 } elsif ($state eq 'file_b') {
2885 my $x = $mods[$#mods] or croak "Empty array\n";
2886 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2887 croak "Error parsing $_, $x->{chg}\n";
2888 }
2889 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2890 croak "Error parsing $_, $x->{chg}\n";
2891 }
2892 $x->{file_b} = $_;
2893 $state = 'meta';
2894 } else {
2895 croak "Error parsing $_\n";
2896 }
2897 }
2898 command_close_pipe($diff_fh, $ctx);
2899 \@mods;
2900}
2901
2902sub check_diff_paths {
2903 my ($ra, $pfx, $rev, $mods) = @_;
2904 my %types;
2905 $pfx .= '/' if length $pfx;
2906
2907 sub type_diff_paths {
2908 my ($ra, $types, $path, $rev) = @_;
2909 my @p = split m#/+#, $path;
2910 my $c = shift @p;
2911 unless (defined $types->{$c}) {
2912 $types->{$c} = $ra->check_path($c, $rev);
2913 }
2914 while (@p) {
2915 $c .= '/' . shift @p;
2916 next if defined $types->{$c};
2917 $types->{$c} = $ra->check_path($c, $rev);
2918 }
2919 }
2920
2921 foreach my $m (@$mods) {
2922 foreach my $f (qw/file_a file_b/) {
2923 next unless defined $m->{$f};
2924 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
2925 if (length $pfx.$dir && ! defined $types{$dir}) {
2926 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
2927 }
2928 }
2929 }
2930 \%types;
2931}
2932
a5e0cedc
EW
2933sub split_path {
2934 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2935}
2936
2937sub repo_path {
d3a840dc
EW
2938 my ($self, $path) = @_;
2939 $self->{path_prefix}.(defined $path ? $path : '');
a5e0cedc
EW
2940}
2941
2942sub url_path {
2943 my ($self, $path) = @_;
29633bb9
EW
2944 if ($self->{url} =~ m#^https?://#) {
2945 $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
2946 }
6e8548cc 2947 $self->{url} . '/' . $self->repo_path($path);
a5e0cedc
EW
2948}
2949
2950sub rmdirs {
61395354 2951 my ($self) = @_;
a5e0cedc
EW
2952 my $rm = $self->{rm};
2953 delete $rm->{''}; # we never delete the url we're tracking
2954 return unless %$rm;
2955
2956 foreach (keys %$rm) {
2957 my @d = split m#/#, $_;
2958 my $c = shift @d;
2959 $rm->{$c} = 1;
2960 while (@d) {
2961 $c .= '/' . shift @d;
2962 $rm->{$c} = 1;
2963 }
2964 }
2965 delete $rm->{$self->{svn_path}};
2966 delete $rm->{''}; # we never delete the url we're tracking
2967 return unless %$rm;
2968
61395354
EW
2969 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2970 $self->{tree_b});
a5e0cedc
EW
2971 local $/ = "\0";
2972 while (<$fh>) {
2973 chomp;
747fa12c 2974 my @dn = split m#/#, $_;
c07eee1f
EW
2975 while (pop @dn) {
2976 delete $rm->{join '/', @dn};
2977 }
2978 unless (%$rm) {
22600a25 2979 close $fh;
c07eee1f
EW
2980 return;
2981 }
a5e0cedc 2982 }
aef4e921 2983 command_close_pipe($fh, $ctx);
c07eee1f 2984
a5e0cedc
EW
2985 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2986 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2987 $self->close_directory($bat->{$d}, $p);
2988 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
44320b9e 2989 print "\tD+\t$d/\n" unless $::_q;
a5e0cedc
EW
2990 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2991 delete $bat->{$d};
2992 }
2993}
2994
2995sub open_or_add_dir {
2996 my ($self, $full_path, $baton) = @_;
6e8548cc
EW
2997 my $t = $self->{types}->{$full_path};
2998 if (!defined $t) {
2999 die "$full_path not known in r$self->{r} or we have a bug!\n";
3000 }
fd499bcc
ER
3001 {
3002 no warnings 'once';
3003 # SVN::Node::none and SVN::Node::file are used only once,
3004 # so we're shutting up Perl's warnings about them.
3005 if ($t == $SVN::Node::none) {
3006 return $self->add_directory($full_path, $baton,
3007 undef, -1, $self->{pool});
3008 } elsif ($t == $SVN::Node::dir) {
3009 return $self->open_directory($full_path, $baton,
3010 $self->{r}, $self->{pool});
3011 } # no warnings 'once'
3012 print STDERR "$full_path already exists in repository at ",
3013 "r$self->{r} and it is not a directory (",
3014 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3015 } # no warnings 'once'
a5e0cedc
EW
3016 exit 1;
3017}
3018
3019sub ensure_path {
3020 my ($self, $path) = @_;
3021 my $bat = $self->{bat};
6e8548cc
EW
3022 my $repo_path = $self->repo_path($path);
3023 return $bat->{''} unless (length $repo_path);
3024 my @p = split m#/+#, $repo_path;
a5e0cedc
EW
3025 my $c = shift @p;
3026 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3027 while (@p) {
3028 my $c0 = $c;
3029 $c .= '/' . shift @p;
3030 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3031 }
3032 return $bat->{$c};
3033}
3034
3035sub A {
44320b9e 3036 my ($self, $m) = @_;
a5e0cedc
EW
3037 my ($dir, $file) = split_path($m->{file_b});
3038 my $pbat = $self->ensure_path($dir);
3039 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3040 undef, -1);
44320b9e 3041 print "\tA\t$m->{file_b}\n" unless $::_q;
a5e0cedc
EW
3042 $self->chg_file($fbat, $m);
3043 $self->close_file($fbat,undef,$self->{pool});
3044}
3045
3046sub C {
44320b9e 3047 my ($self, $m) = @_;
a5e0cedc
EW
3048 my ($dir, $file) = split_path($m->{file_b});
3049 my $pbat = $self->ensure_path($dir);
3050 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3051 $self->url_path($m->{file_a}), $self->{r});
44320b9e 3052 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
a5e0cedc
EW
3053 $self->chg_file($fbat, $m);
3054 $self->close_file($fbat,undef,$self->{pool});
3055}
3056
3057sub delete_entry {
3058 my ($self, $path, $pbat) = @_;
3059 my $rpath = $self->repo_path($path);
3060 my ($dir, $file) = split_path($rpath);
3061 $self->{rm}->{$dir} = 1;
3062 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3063}
3064
3065sub R {
44320b9e 3066 my ($self, $m) = @_;
a5e0cedc
EW
3067 my ($dir, $file) = split_path($m->{file_b});
3068 my $pbat = $self->ensure_path($dir);
3069 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3070 $self->url_path($m->{file_a}), $self->{r});
44320b9e 3071 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
a5e0cedc
EW
3072 $self->chg_file($fbat, $m);
3073 $self->close_file($fbat,undef,$self->{pool});
3074
3075 ($dir, $file) = split_path($m->{file_a});
3076 $pbat = $self->ensure_path($dir);
3077 $self->delete_entry($m->{file_a}, $pbat);
3078}
3079
3080sub M {
44320b9e 3081 my ($self, $m) = @_;
a5e0cedc
EW
3082 my ($dir, $file) = split_path($m->{file_b});
3083 my $pbat = $self->ensure_path($dir);
3084 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3085 $pbat,$self->{r},$self->{pool});
44320b9e 3086 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
a5e0cedc
EW
3087 $self->chg_file($fbat, $m);
3088 $self->close_file($fbat,undef,$self->{pool});
3089}
3090
3091sub T { shift->M(@_) }
3092
3093sub change_file_prop {
3094 my ($self, $fbat, $pname, $pval) = @_;
3095 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3096}
3097
3098sub chg_file {
3099 my ($self, $fbat, $m) = @_;
3100 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3101 $self->change_file_prop($fbat,'svn:executable','*');
3102 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3103 $self->change_file_prop($fbat,'svn:executable',undef);
3104 }
3105 my $fh = IO::File->new_tmpfile or croak $!;
3106 if ($m->{mode_b} =~ /^120/) {
3107 print $fh 'link ' or croak $!;
3108 $self->change_file_prop($fbat,'svn:special','*');
3109 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3110 $self->change_file_prop($fbat,'svn:special',undef);
3111 }
3112 defined(my $pid = fork) or croak $!;
3113 if (!$pid) {
3114 open STDOUT, '>&', $fh or croak $!;
3115 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3116 }
3117 waitpid $pid, 0;
3118 croak $? if $?;
3119 $fh->flush == 0 or croak $!;
3120 seek $fh, 0, 0 or croak $!;
3121
3122 my $md5 = Digest::MD5->new;
3123 $md5->addfile($fh) or croak $!;
3124 seek $fh, 0, 0 or croak $!;
3125
3126 my $exp = $md5->hexdigest;
f7197dff
EW
3127 my $pool = SVN::Pool->new;
3128 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3129 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
a5e0cedc 3130 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
f7197dff 3131 $pool->clear;
a5e0cedc
EW
3132
3133 close $fh or croak $!;
3134}
3135
3136sub D {
44320b9e 3137 my ($self, $m) = @_;
a5e0cedc
EW
3138 my ($dir, $file) = split_path($m->{file_b});
3139 my $pbat = $self->ensure_path($dir);
44320b9e 3140 print "\tD\t$m->{file_b}\n" unless $::_q;
a5e0cedc
EW
3141 $self->delete_entry($m->{file_b}, $pbat);
3142}
3143
3144sub close_edit {
3145 my ($self) = @_;
3146 my ($p,$bat) = ($self->{pool}, $self->{bat});
3147 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
6442754d 3148 next if $_ eq '';
a5e0cedc
EW
3149 $self->close_directory($bat->{$_}, $p);
3150 }
6442754d 3151 $self->close_directory($bat->{''}, $p);
a5e0cedc
EW
3152 $self->SUPER::close_edit($p);
3153 $p->clear;
3154}
3155
3156sub abort_edit {
3157 my ($self) = @_;
3158 $self->SUPER::abort_edit($self->{pool});
61395354
EW
3159}
3160
3161sub DESTROY {
3162 my $self = shift;
3163 $self->SUPER::DESTROY(@_);
a5e0cedc
EW
3164 $self->{pool}->clear;
3165}
3166
44320b9e
EW
3167# this drives the editor
3168sub apply_diff {
61395354
EW
3169 my ($self) = @_;
3170 my $mods = $self->{mods};
44320b9e 3171 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
6e8548cc 3172 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
44320b9e
EW
3173 my $f = $m->{chg};
3174 if (defined $o{$f}) {
3175 $self->$f($m);
3176 } else {
207f1a75 3177 fatal("Invalid change type: $f");
44320b9e
EW
3178 }
3179 }
24e22aa8 3180 $self->rmdirs if $_rmdir;
6e8548cc 3181 if (@$mods == 0) {
44320b9e
EW
3182 $self->abort_edit;
3183 } else {
3184 $self->close_edit;
3185 }
6e8548cc 3186 return scalar @$mods;
44320b9e
EW
3187}
3188
d81bf827 3189package Git::SVN::Ra;
6af1db44 3190use vars qw/@ISA $config_dir $_log_window_size/;
d81bf827
EW
3191use strict;
3192use warnings;
a51cdb0c 3193my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
d81bf827
EW
3194
3195BEGIN {
3196 # enforce temporary pool usage for some simple functions
c5f71ad0
SV
3197 no strict 'refs';
3198 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3199 my $SUPER = "SUPER::$f";
3200 *$f = sub {
3201 my $self = shift;
3202 my $pool = SVN::Pool->new;
3203 my @ret = $self->$SUPER(@_,$pool);
3204 $pool->clear;
3205 wantarray ? @ret : $ret[0];
3206 };
d81bf827 3207 }
d81bf827
EW
3208}
3209
9ff74e95
SW
3210sub _auth_providers () {
3211 [
3212 SVN::Client::get_simple_provider(),
3213 SVN::Client::get_ssl_server_trust_file_provider(),
3214 SVN::Client::get_simple_prompt_provider(
3215 \&Git::SVN::Prompt::simple, 2),
3216 SVN::Client::get_ssl_client_cert_file_provider(),
3217 SVN::Client::get_ssl_client_cert_prompt_provider(
3218 \&Git::SVN::Prompt::ssl_client_cert, 2),
3219 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
3220 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
3221 SVN::Client::get_username_provider(),
3222 SVN::Client::get_ssl_server_trust_prompt_provider(
3223 \&Git::SVN::Prompt::ssl_server_trust),
3224 SVN::Client::get_username_prompt_provider(
3225 \&Git::SVN::Prompt::username, 2)
3226 ]
3227}
3228
cfbe7ab3
EW
3229sub escape_uri_only {
3230 my ($uri) = @_;
3231 my @tmp;
3232 foreach (split m{/}, $uri) {
3233 s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
3234 push @tmp, $_;
3235 }
3236 join('/', @tmp);
3237}
3238
3239sub escape_url {
3240 my ($url) = @_;
3241 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
3242 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
3243 $url = "$scheme://$domain$uri";
3244 }
3245 $url;
3246}
3247
d81bf827
EW
3248sub new {
3249 my ($class, $url) = @_;
f6f09876 3250 $url =~ s!/+$!!;
5d3b7cd5 3251 return $RA if ($RA && $RA->{url} eq $url);
f6f09876 3252
d81bf827 3253 SVN::_Core::svn_config_ensure($config_dir, undef);
9ff74e95 3254 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
d81bf827 3255 my $config = SVN::Core::config_get_config($config_dir);
7730fbe6 3256 $RA = undef;
602015e0
ER
3257 my $dont_store_passwords = 1;
3258 my $conf_t = ${$config}{'config'};
3259 {
fd499bcc 3260 no warnings 'once';
602015e0
ER
3261 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3262 # produces warnings that variables are used only once.
3263 # I had not found the better way to shut them up, so
fd499bcc 3264 # the warnings of type 'once' are disabled in this block.
602015e0
ER
3265 if (SVN::_Core::svn_config_get_bool($conf_t,
3266 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3267 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
3268 1) == 0) {
3269 SVN::_Core::svn_auth_set_parameter($baton,
3270 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
3271 bless (\$dont_store_passwords, "_p_void"));
3272 }
3273 if (SVN::_Core::svn_config_get_bool($conf_t,
3274 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3275 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
3276 1) == 0) {
3277 $Git::SVN::Prompt::_no_auth_cache = 1;
3278 }
fd499bcc 3279 } # no warnings 'once'
cfbe7ab3 3280 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
d81bf827
EW
3281 config => $config,
3282 pool => SVN::Pool->new,
3283 auth_provider_callbacks => $callbacks);
cfbe7ab3 3284 $self->{url} = $url;
d81bf827
EW
3285 $self->{svn_path} = $url;
3286 $self->{repos_root} = $self->get_repos_root;
4e9f6cc7 3287 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
0dc03d6a
EW
3288 $self->{cache} = { check_path => { r => 0, data => {} },
3289 get_dir => { r => 0, data => {} } };
5d3b7cd5 3290 $RA = bless $self, $class;
d81bf827
EW
3291}
3292
0dc03d6a
EW
3293sub check_path {
3294 my ($self, $path, $r) = @_;
3295 my $cache = $self->{cache}->{check_path};
3296 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
3297 return $cache->{data}->{$path};
3298 }
3299 my $pool = SVN::Pool->new;
3300 my $t = $self->SUPER::check_path($path, $r, $pool);
3301 $pool->clear;
3302 if ($r != $cache->{r}) {
3303 %{$cache->{data}} = ();
3304 $cache->{r} = $r;
3305 }
3306 $cache->{data}->{$path} = $t;
3307}
3308
3309sub get_dir {
3310 my ($self, $dir, $r) = @_;
3311 my $cache = $self->{cache}->{get_dir};
3312 if ($r == $cache->{r}) {
3313 if (my $x = $cache->{data}->{$dir}) {
3314 return wantarray ? @$x : $x->[0];
3315 }
3316 }
3317 my $pool = SVN::Pool->new;
3318 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
3319 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
3320 $pool->clear;
3321 if ($r != $cache->{r}) {
3322 %{$cache->{data}} = ();
3323 $cache->{r} = $r;
3324 }
3325 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
3326 wantarray ? (\%dirents, $r, $props) : \%dirents;
3327}
3328
d81bf827 3329sub DESTROY {
5d3b7cd5 3330 # do not call the real DESTROY since we store ourselves in $RA
d81bf827
EW
3331}
3332
d81bf827
EW
3333sub get_log {
3334 my ($self, @args) = @_;
3335 my $pool = SVN::Pool->new;
d81bf827
EW
3336 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
3337 my $ret = $self->SUPER::get_log(@args, $pool);
3338 $pool->clear;
3339 $ret;
3340}
3341
9ff74e95
SW
3342sub trees_match {
3343 my ($self, $url1, $rev1, $url2, $rev2) = @_;
3344 my $ctx = SVN::Client->new(auth => _auth_providers);
3345 my $out = IO::File->new_tmpfile;
3346
3347 # older SVN (1.1.x) doesn't take $pool as the last parameter for
3348 # $ctx->diff(), so we'll create a default one
3349 my $pool = SVN::Pool->new_default_sub;
3350
3351 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
3352 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
3353 $out->flush;
3354 my $ret = (($out->stat)[7] == 0);
3355 close $out or croak $!;
3356
3357 $ret;
3358}
3359
d81bf827 3360sub get_commit_editor {
44320b9e 3361 my ($self, $log, $cb, $pool) = @_;
d81bf827 3362 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
44320b9e 3363 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
d81bf827
EW
3364}
3365
d81bf827 3366sub gs_do_update {
8a603774
EW
3367 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
3368 my $new = ($rev_a == $rev_b);
3369 my $path = $gs->{path};
3370
2e5e2480
EW
3371 if ($new && -e $gs->{index}) {
3372 unlink $gs->{index} or die
3373 "Couldn't unlink index: $gs->{index}: $!\n";
3374 }
d81bf827 3375 my $pool = SVN::Pool->new;
8b8fc068 3376 $editor->set_path_strip($path);
2b27f6c8
EW
3377 my (@pc) = split m#/#, $path;
3378 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
8a603774 3379 1, $editor, $pool);
d81bf827 3380 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2b27f6c8
EW
3381
3382 # Since we can't rely on svn_ra_reparent being available, we'll
3383 # just have to do some magic with set_path to make it so
3384 # we only want a partial path.
3385 my $sp = '';
3386 my $final = join('/', @pc);
3387 while (@pc) {
3388 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
3389 $sp .= '/' if length $sp;
3390 $sp .= shift @pc;
3391 }
3392 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
3393
2b27f6c8
EW
3394 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
3395
d81bf827
EW
3396 $reporter->finish_report($pool);
3397 $pool->clear;
3398 $editor->{git_commit_ok};
3399}
3400
2b27f6c8
EW
3401# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
3402# svn_ra_reparent didn't work before 1.4)
d81bf827 3403sub gs_do_switch {
8a603774
EW
3404 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
3405 my $path = $gs->{path};
d81bf827 3406 my $pool = SVN::Pool->new;
2b27f6c8
EW
3407
3408 my $full_url = $self->{url};
3409 my $old_url = $full_url;
cfbe7ab3 3410 $full_url .= '/' . escape_uri_only($path) if length $path;
5d3b7cd5
EW
3411 my ($ra, $reparented);
3412 if ($old_url ne $full_url) {
3413 if ($old_url !~ m#^svn(\+ssh)?://#) {
3414 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
3415 $pool);
3416 $self->{url} = $full_url;
3417 $reparented = 1;
3418 } else {
a51cdb0c
EW
3419 $_[0] = undef;
3420 $self = undef;
3421 $RA = undef;
5d3b7cd5 3422 $ra = Git::SVN::Ra->new($full_url);
a51cdb0c 3423 $ra_invalid = 1;
5d3b7cd5
EW
3424 }
3425 }
3426 $ra ||= $self;
8a603774 3427 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
d81bf827 3428 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
8b8fc068 3429 $reporter->set_path('', $rev_a, 0, @lock, $pool);
d81bf827 3430 $reporter->finish_report($pool);
2b27f6c8 3431
5d3b7cd5
EW
3432 if ($reparented) {
3433 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
3434 $self->{url} = $old_url;
3435 }
2b27f6c8 3436
d81bf827
EW
3437 $pool->clear;
3438 $editor->{git_commit_ok};
3439}
3440
b54a901e
EW
3441sub longest_common_path {
3442 my ($gsv, $globs) = @_;
d2ae1434 3443 my %common;
e518192f
EW
3444 my $common_max = scalar @$gsv;
3445
3446 foreach my $gs (@$gsv) {
d2ae1434
EW
3447 my @tmp = split m#/#, $gs->{path};
3448 my $p = '';
3449 foreach (@tmp) {
3450 $p .= length($p) ? "/$_" : $_;
3451 $common{$p} ||= 0;
3452 $common{$p}++;
3453 }
3454 }
e518192f
EW
3455 $globs ||= [];
3456 $common_max += scalar @$globs;
3457 foreach my $glob (@$globs) {
3458 my @tmp = split m#/#, $glob->{path}->{left};
3459 my $p = '';
3460 foreach (@tmp) {
3461 $p .= length($p) ? "/$_" : $_;
3462 $common{$p} ||= 0;
3463 $common{$p}++;
3464 }
3465 }
3466
d2ae1434
EW
3467 my $longest_path = '';
3468 foreach (sort {length $b <=> length $a} keys %common) {
e518192f 3469 if ($common{$_} == $common_max) {
d2ae1434
EW
3470 $longest_path = $_;
3471 last;
3472 }
0af9c9f9 3473 }
b54a901e
EW
3474 $longest_path;
3475}
3476
3477sub gs_fetch_loop_common {
3478 my ($self, $base, $head, $gsv, $globs) = @_;
3479 return if ($base > $head);
3480 my $inc = $_log_window_size;
3481 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
3482 my $longest_path = longest_common_path($gsv, $globs);
a51cdb0c 3483 my $ra_url = $self->{url};
0af9c9f9 3484 while (1) {
d4eff2bd 3485 my %revs;
d2ae1434 3486 my $err;
f7c3fc4a 3487 my $err_handler = $SVN::Error::handler;
d2ae1434
EW
3488 $SVN::Error::handler = sub {
3489 ($err) = @_;
3490 skip_unknown_revs($err);
3491 };
3492 sub _cb {
3493 my ($paths, $r, $author, $date, $log) = @_;
3494 [ dup_changed_paths($paths),
3495 { author => $author, date => $date, log => $log } ];
3496 }
3497 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3498 sub { $revs{$_[1]} = _cb(@_) });
3499 if ($err && $max >= $head) {
3500 print STDERR "Path '$longest_path' ",
3501 "was probably deleted:\n",
3502 $err->expanded_message,
3503 "\nWill attempt to follow ",
3504 "revisions r$min .. r$max ",
3505 "committed before the deletion\n";
3506 my $hi = $max;
3507 while (--$hi >= $min) {
3508 my $ok;
3509 $self->get_log([$longest_path], $min, $hi,
3510 0, 1, 1, sub {
3511 $ok ||= $_[1];
3512 $revs{$_[1]} = _cb(@_) });
3513 if ($ok) {
3514 print STDERR "r$min .. r$ok OK\n";
3515 last;
3516 }
3517 }
3518 }
d4eff2bd 3519 $SVN::Error::handler = $err_handler;
fbcc1737 3520
e518192f 3521 my %exists = map { $_->{path} => $_ } @$gsv;
d4eff2bd 3522 foreach my $r (sort {$a <=> $b} keys %revs) {
fbcc1737 3523 my ($paths, $logged) = @{$revs{$r}};
e518192f
EW
3524
3525 foreach my $gs ($self->match_globs(\%exists, $paths,
3526 $globs, $r)) {
fbcc1737
EW
3527 if ($gs->rev_db_max >= $r) {
3528 next;
3529 }
3530 next unless $gs->match_paths($paths, $r);
3531 $gs->{logged_rev_props} = $logged;
e8d120bd
EW
3532 if (my $last_commit = $gs->last_commit) {
3533 $gs->assert_index_clean($last_commit);
3534 }
fbcc1737
EW
3535 my $log_entry = $gs->do_fetch($paths, $r);
3536 if ($log_entry) {
0af9c9f9
EW
3537 $gs->do_git_commit($log_entry);
3538 }
3539 }
e518192f 3540 foreach my $g (@$globs) {
93f2689c
EW
3541 my $k = "svn-remote.$g->{remote}." .
3542 "$g->{t}-maxRev";
3543 Git::SVN::tmp_config($k, $r);
e518192f 3544 }
a51cdb0c
EW
3545 if ($ra_invalid) {
3546 $_[0] = undef;
3547 $self = undef;
3548 $RA = undef;
3549 $sel