git-svn: cleanup: move process_rm around
[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
1ca72aef 7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
3397f9df 8 $GIT_SVN_INDEX $GIT_SVN
42d32870 9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
3397f9df 10$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
60d02ccc 11$VERSION = '@@GIT_VERSION@@';
13ccd6d4
EW
12
13use Cwd qw/abs_path/;
14$GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15$ENV{GIT_DIR} = $GIT_DIR;
16
ce475dfc 17my $LC_ALL = $ENV{LC_ALL};
79bb8d88 18my $TZ = $ENV{TZ};
3397f9df
EW
19# make sure the svn binary gives consistent output between locales and TZs:
20$ENV{TZ} = 'UTC';
21$ENV{LC_ALL} = 'C';
a00439ac 22$| = 1; # unbuffer STDOUT
3397f9df 23
d2a9a87b
EW
24# properties that we do not log:
25my %SKIP = ( 'svn:wc:ra_dav:version-url' => 1,
26 'svn:special' => 1,
27 'svn:executable' => 1,
28 'svn:entry:committed-rev' => 1,
29 'svn:entry:last-author' => 1,
30 'svn:entry:uuid' => 1,
31 'svn:entry:committed-date' => 1,
32);
33
6fda05ae 34sub fatal (@) { print STDERR @_; exit 1 }
b9c85187
EW
35require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
36require SVN::Ra;
37require SVN::Delta;
38if ($SVN::Core::VERSION lt '1.1.0') {
39 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
40}
41push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
42push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
3397f9df
EW
43use Carp qw/croak/;
44use IO::File qw//;
45use File::Basename qw/dirname basename/;
46use File::Path qw/mkpath/;
79bb8d88 47use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
e17512f3 48use POSIX qw/strftime/;
968bdf1f 49use IPC::Open3;
42d32870 50use Memoize;
aef4e921
EW
51use Git qw/command command_oneline command_noisy
52 command_output_pipe command_input_pipe command_close_pipe/;
42d32870 53memoize('revisions_eq');
c1927a85
EW
54memoize('cmt_metadata');
55memoize('get_commit_time');
a5e0cedc 56
b9c85187 57my ($SVN);
83e9940a 58
42d32870 59my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
3397f9df 60my $sha1 = qr/[a-f\d]{40}/;
ac8e0b91 61my $sha1_short = qr/[a-f\d]{4,40}/;
9aca0258 62my $_esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
72942938 63my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
1a82e793 64 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
80f50749 65 $_repack, $_repack_nr, $_repack_flags, $_q,
a00439ac 66 $_message, $_file, $_follow_parent, $_no_metadata,
9d55b41a 67 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
79bb8d88 68 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
b22d4497 69 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
30d055aa 70 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive,
d976acfd 71 $_config_dir,
ae410987 72 $_pager, $_color, $_prefix);
42d32870 73my (@_branch_from, %tree_map, %users, %rusers, %equiv);
b9c85187 74my ($_svn_can_do_switch);
883d0a78 75my @repo_path_split_cache;
3397f9df 76
eeb0abe0 77my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
69f0d91e 78 'branch|b=s' => \@_branch_from,
a00439ac 79 'follow-parent|follow' => \$_follow_parent,
bf78b1d8 80 'branch-all-refs|B' => \$_branch_all_refs,
dc5869c0
EW
81 'authors-file|A=s' => \$_authors,
82 'repack:i' => \$_repack,
a00439ac 83 'no-metadata' => \$_no_metadata,
80f50749 84 'quiet|q' => \$_q,
d976acfd 85 'username=s' => \$Git::SVN::Prompt::_username,
30d055aa 86 'config-dir=s' => \$_config_dir,
d976acfd 87 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
f7bae37f 88 'ignore-nodate' => \$_ignore_nodate,
dc5869c0 89 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
36f5b1f0 90
9d55b41a
EW
91my ($_trunk, $_tags, $_branches);
92my %multi_opts = ( 'trunk|T=s' => \$_trunk,
93 'tags|t=s' => \$_tags,
94 'branches|b=s' => \$_branches );
95my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
27e9fb8d
EW
96my %cmt_opts = ( 'edit|e' => \$_edit,
97 'rmdir' => \$_rmdir,
98 'find-copies-harder' => \$_find_copies_harder,
99 'l=i' => \$_l,
100 'copy-similarity|C=i'=> \$_cp_similarity
101);
9d55b41a 102
3397f9df 103my %cmd = (
2a3240be 104 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
eeb0abe0 105 { 'revision|r=s' => \$_revision, %fc_opts } ],
81c5a0e6 106 init => [ \&init, "Initialize a repo for tracking" .
f8ab6b73 107 " (requires URL argument)",
9d55b41a 108 \%init_opts ],
3289e86e
EW
109 dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
110 { 'merge|m|M' => \$_merge,
111 'strategy|s=s' => \$_strategy,
112 'dry-run|n' => \$_dry_run,
4b155223 113 %cmt_opts, %fc_opts } ],
3289e86e 114 'set-tree' => [ \&commit, "Set an SVN repository to a git tree-ish",
27e9fb8d 115 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
a5e0cedc
EW
116 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
117 { 'revision|r=i' => \$_revision } ],
eeb0abe0
EW
118 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
119 { 'no-ignore-externals' => \$_no_ignore_ext,
1a82e793 120 'copy-remote|remote=s' => \$_cp_remote,
eeb0abe0 121 'upgrade' => \$_upgrade } ],
9d55b41a
EW
122 'graft-branches' => [ \&graft_branches,
123 'Detect merges/branches from already imported history',
124 { 'merge-rx|m' => \@_opt_m,
c1927a85
EW
125 'branch|b=s' => \@_branch_from,
126 'branch-all-refs|B' => \$_branch_all_refs,
9d55b41a
EW
127 'no-default-regex' => \$_no_default_regex,
128 'no-graft-copy' => \$_no_graft_copy } ],
129 'multi-init' => [ \&multi_init,
130 'Initialize multiple trees (like git-svnimport)',
1ca7558d
EW
131 { %multi_opts, %init_opts,
132 'revision|r=i' => \$_revision,
d976acfd 133 'username=s' => \$Git::SVN::Prompt::_username,
1ca7558d 134 'config-dir=s' => \$_config_dir,
d976acfd 135 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
ae410987 136 'prefix=s' => \$_prefix,
1ca7558d 137 } ],
9d55b41a
EW
138 'multi-fetch' => [ \&multi_fetch,
139 'Fetch multiple trees (like git-svnimport)',
140 \%fc_opts ],
79bb8d88
EW
141 'log' => [ \&show_log, 'Show commit logs',
142 { 'limit=i' => \$_limit,
143 'revision|r=s' => \$_revision,
144 'verbose|v' => \$_verbose,
145 'incremental' => \$_incremental,
146 'oneline' => \$_oneline,
147 'show-commit' => \$_show_commit,
74a31a10 148 'non-recursive' => \$_non_recursive,
79bb8d88 149 'authors-file|A=s' => \$_authors,
9aca0258
EW
150 'color' => \$_color,
151 'pager=s' => \$_pager,
79bb8d88 152 } ],
27e9fb8d
EW
153 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
154 { 'message|m=s' => \$_message,
155 'file|F=s' => \$_file,
45bf473a 156 'revision|r=s' => \$_revision,
27e9fb8d 157 %cmt_opts } ],
3397f9df 158);
9d55b41a 159
3397f9df
EW
160my $cmd;
161for (my $i = 0; $i < @ARGV; $i++) {
162 if (defined $cmd{$ARGV[$i]}) {
163 $cmd = $ARGV[$i];
164 splice @ARGV, $i, 1;
165 last;
166 }
167};
168
448c81b4 169my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
a9612be2 170
b8c92cad 171read_repo_config(\%opts);
79bb8d88
EW
172my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
173 'version|V' => \$_version,
174 'id|i=s' => \$GIT_SVN);
175exit 1 if (!$rv && $cmd ne 'log');
6f0783cf 176
dc5869c0 177set_default_vals();
3397f9df 178usage(0) if $_help;
551ce28f 179version() if $_version;
eeb0abe0 180usage(1) unless defined $cmd;
b8c92cad 181init_vars();
eeb0abe0 182load_authors() if $_authors;
bf78b1d8 183load_all_refs() if $_branch_all_refs;
7b520e62 184migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
3397f9df
EW
185$cmd{$cmd}->[0]->(@ARGV);
186exit 0;
187
188####################### primary functions ######################
189sub usage {
190 my $exit = shift || 0;
191 my $fd = $exit ? \*STDERR : \*STDOUT;
192 print $fd <<"";
193git-svn - bidirectional operations between a single Subversion tree and git
194Usage: $0 <command> [options] [arguments]\n
448c81b4
EW
195
196 print $fd "Available commands:\n" unless $cmd;
3397f9df
EW
197
198 foreach (sort keys %cmd) {
448c81b4 199 next if $cmd && $cmd ne $_;
b203b769 200 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
448c81b4
EW
201 foreach (keys %{$cmd{$_}->[2]}) {
202 # prints out arguments as they should be passed:
b8c92cad 203 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
b203b769 204 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
448c81b4
EW
205 "--$_" : "-$_" }
206 split /\|/,$_)," $x\n";
207 }
3397f9df
EW
208 }
209 print $fd <<"";
448c81b4
EW
210\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
211arbitrary identifier if you're tracking multiple SVN branches/repositories in
212one git repository and want to keep them separate. See git-svn(1) for more
213information.
3397f9df
EW
214
215 exit $exit;
216}
217
551ce28f 218sub version {
7d60ab2c 219 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
551ce28f
EW
220 exit 0;
221}
222
3397f9df 223sub rebuild {
aef4e921 224 if (!verify_ref("refs/remotes/$GIT_SVN^0")) {
1a82e793
EW
225 copy_remote_ref();
226 }
3397f9df 227 $SVN_URL = shift or undef;
3397f9df 228 my $newest_rev = 0;
2beb3cdd 229 if ($_upgrade) {
aef4e921
EW
230 command_noisy('update-ref',"refs/remotes/$GIT_SVN","
231 $GIT_SVN-HEAD");
2beb3cdd
EW
232 } else {
233 check_upgrade_needed();
234 }
3397f9df 235
aef4e921
EW
236 my ($rev_list, $ctx) = command_output_pipe("rev-list",
237 "refs/remotes/$GIT_SVN");
2beb3cdd 238 my $latest;
3397f9df
EW
239 while (<$rev_list>) {
240 chomp;
241 my $c = $_;
242 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
aef4e921
EW
243 my @commit = grep(/^git-svn-id: /,
244 command(qw/cat-file commit/, $c));
3397f9df 245 next if (!@commit); # skip merges
79bb8d88 246 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
e70dc780 247 if (!defined $rev || !$uuid) {
79bb8d88
EW
248 croak "Unable to extract revision or UUID from ",
249 "$c, $commit[$#commit]\n";
3397f9df 250 }
2beb3cdd
EW
251
252 # if we merged or otherwise started elsewhere, this is
253 # how we break out of it
1ca72aef 254 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
779b1446 255 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
2beb3cdd 256
2beb3cdd 257 unless (defined $latest) {
3397f9df
EW
258 if (!$SVN_URL && !$url) {
259 croak "SVN repository location required: $url\n";
260 }
261 $SVN_URL ||= $url;
1d52aba8
EW
262 $SVN_UUID ||= $uuid;
263 setup_git_svn();
2beb3cdd 264 $latest = $rev;
3397f9df 265 }
42d32870
EW
266 revdb_set($REVDB, $rev, $c);
267 print "r$rev = $c\n";
3397f9df
EW
268 $newest_rev = $rev if ($rev > $newest_rev);
269 }
aef4e921 270 command_close_pipe($rev_list, $ctx);
3397f9df
EW
271}
272
273sub init {
03e0ea87 274 my $url = shift or die "SVN repository location required " .
81c5a0e6 275 "as a command-line argument\n";
03e0ea87
EW
276 $url =~ s!/+$!!; # strip trailing slash
277
278 if (my $repo_path = shift) {
279 unless (-d $repo_path) {
280 mkpath([$repo_path]);
281 }
282 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
283 init_vars();
284 }
285
286 $SVN_URL = $url;
3397f9df 287 unless (-d $GIT_DIR) {
5c94f87e 288 my @init_db = ('init');
f8ab6b73
EW
289 push @init_db, "--template=$_template" if defined $_template;
290 push @init_db, "--shared" if defined $_shared;
aef4e921 291 command_noisy(@init_db);
3397f9df
EW
292 }
293 setup_git_svn();
294}
295
2a3240be
EW
296sub cmd_fetch {
297 fetch_child_id($GIT_SVN, @_);
298}
299
3397f9df 300sub fetch {
2beb3cdd 301 check_upgrade_needed();
883d0a78 302 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
b9c85187 303 my $ret = fetch_lib(@_);
aef4e921
EW
304 if ($ret->{commit} && !verify_ref('refs/heads/master^0')) {
305 command_noisy(qw(update-ref refs/heads/master),$ret->{commit});
a5e0cedc
EW
306 }
307 return $ret;
308}
309
a5e0cedc
EW
310sub fetch_lib {
311 my (@parents) = @_;
312 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
747fa12c 313 $SVN ||= libsvn_connect($SVN_URL);
a5e0cedc
EW
314 my ($last_rev, $last_commit) = svn_grab_base_rev();
315 my ($base, $head) = libsvn_parse_revision($last_rev);
316 if ($base > $head) {
317 return { revision => $last_rev, commit => $last_commit }
318 }
319 my $index = set_index($GIT_SVN_INDEX);
320
321 # limit ourselves and also fork() since get_log won't release memory
322 # after processing a revision and SVN stuff seems to leak
323 my $inc = 1000;
324 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
325 read_uuid();
326 if (defined $last_commit) {
327 unless (-e $GIT_SVN_INDEX) {
aef4e921 328 command_noisy('read-tree', $last_commit);
a5e0cedc 329 }
aef4e921
EW
330 my $x = command_oneline('write-tree');
331 my ($y) = (command(qw/cat-file commit/, $last_commit)
a5e0cedc
EW
332 =~ /^tree ($sha1)/m);
333 if ($y ne $x) {
334 unlink $GIT_SVN_INDEX or croak $!;
aef4e921 335 command_noisy('read-tree', $last_commit);
a5e0cedc 336 }
aef4e921 337 $x = command_oneline('write-tree');
a5e0cedc
EW
338 if ($y ne $x) {
339 print STDERR "trees ($last_commit) $y != $x\n",
340 "Something is seriously wrong...\n";
341 }
342 }
343 while (1) {
344 # fork, because using SVN::Pool with get_log() still doesn't
345 # seem to help enough to keep memory usage down.
346 defined(my $pid = fork) or croak $!;
347 if (!$pid) {
348 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
a5e0cedc
EW
349
350 # Yes I'm perfectly aware that the fourth argument
351 # below is the limit revisions number. Unfortunately
352 # performance sucks with it enabled, so it's much
353 # faster to fetch revision ranges instead of relying
354 # on the limiter.
747fa12c 355 libsvn_get_log(libsvn_dup_ra($SVN), [''],
dc62e25c 356 $min, $max, 0, 1, 1,
a5e0cedc
EW
357 sub {
358 my $log_msg;
359 if ($last_commit) {
360 $log_msg = libsvn_fetch(
361 $last_commit, @_);
362 $last_commit = git_commit(
363 $log_msg,
364 $last_commit,
365 @parents);
366 } else {
367 $log_msg = libsvn_new_tree(@_);
368 $last_commit = git_commit(
369 $log_msg, @parents);
370 }
371 });
a5e0cedc
EW
372 exit 0;
373 }
374 waitpid $pid, 0;
375 croak $? if $?;
376 ($last_rev, $last_commit) = svn_grab_base_rev();
377 last if ($max >= $head);
378 $min = $max + 1;
379 $max += $inc;
380 $max = $head if ($max > $head);
ebdf7b95 381 $SVN = libsvn_connect($SVN_URL);
a5e0cedc
EW
382 }
383 restore_index($index);
384 return { revision => $last_rev, commit => $last_commit };
385}
386
3397f9df
EW
387sub commit {
388 my (@commits) = @_;
2beb3cdd 389 check_upgrade_needed();
3397f9df
EW
390 if ($_stdin || !@commits) {
391 print "Reading from stdin...\n";
392 @commits = ();
393 while (<STDIN>) {
1ca72aef 394 if (/\b($sha1_short)\b/o) {
3397f9df
EW
395 unshift @commits, $1;
396 }
397 }
398 }
399 my @revs;
8de010ad 400 foreach my $c (@commits) {
aef4e921 401 my @tmp = command('rev-parse',$c);
8de010ad
EW
402 if (scalar @tmp == 1) {
403 push @revs, $tmp[0];
404 } elsif (scalar @tmp > 1) {
aef4e921 405 push @revs, reverse(command('rev-list',@tmp));
8de010ad
EW
406 } else {
407 die "Failed to rev-parse $c\n";
408 }
3397f9df 409 }
b9c85187 410 commit_lib(@revs);
a5e0cedc
EW
411 print "Done committing ",scalar @revs," revisions to SVN\n";
412}
413
a5e0cedc
EW
414sub commit_lib {
415 my (@revs) = @_;
416 my ($r_last, $cmt_last) = svn_grab_base_rev();
417 defined $r_last or die "Must have an existing revision to commit\n";
cf7424b0 418 my $fetched = fetch();
a5e0cedc
EW
419 if ($r_last != $fetched->{revision}) {
420 print STDERR "There are new revisions that were fetched ",
421 "and need to be merged (or acknowledged) ",
422 "before committing.\n",
423 "last rev: $r_last\n",
424 " current: $fetched->{revision}\n";
425 exit 1;
426 }
427 read_uuid();
428 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
429 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
430
5a990e45 431 my $repo;
27e9fb8d 432 set_svn_commit_env();
a5e0cedc 433 foreach my $c (@revs) {
ec9d00d0
EW
434 my $log_msg = get_commit_message($c, $commit_msg);
435
a5e0cedc
EW
436 # fork for each commit because there's a memory leak I
437 # can't track down... (it's probably in the SVN code)
438 defined(my $pid = open my $fh, '-|') or croak $!;
439 if (!$pid) {
a5e0cedc
EW
440 my $ed = SVN::Git::Editor->new(
441 { r => $r_last,
747fa12c 442 ra => libsvn_dup_ra($SVN),
a5e0cedc 443 c => $c,
747fa12c 444 svn_path => $SVN->{svn_path},
a5e0cedc
EW
445 },
446 $SVN->get_commit_editor(
447 $log_msg->{msg},
448 sub {
449 libsvn_commit_cb(
450 @_, $c,
451 $log_msg->{msg},
452 $r_last,
453 $cmt_last)
454 },
455 @lock)
456 );
42d32870 457 my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
a5e0cedc
EW
458 if (@$mods == 0) {
459 print "No changes\nr$r_last = $cmt_last\n";
460 $ed->abort_edit;
461 } else {
462 $ed->close_edit;
463 }
464 exit 0;
465 }
466 my ($r_new, $cmt_new, $no);
467 while (<$fh>) {
468 print $_;
469 chomp;
470 if (/^r(\d+) = ($sha1)$/o) {
471 ($r_new, $cmt_new) = ($1, $2);
472 } elsif ($_ eq 'No changes') {
473 $no = 1;
474 }
475 }
d25c26e7 476 close $fh or exit 1;
a5e0cedc
EW
477 if (! defined $r_new && ! defined $cmt_new) {
478 unless ($no) {
479 die "Failed to parse revision information\n";
480 }
481 } else {
482 ($r_last, $cmt_last) = ($r_new, $cmt_new);
483 }
484 }
ec9d00d0 485 $ENV{LC_ALL} = 'C';
a5e0cedc
EW
486 unlink $commit_msg;
487}
8f22562c 488
b22d4497 489sub dcommit {
dd31da2f 490 my $head = shift || 'HEAD';
b22d4497 491 my $gs = "refs/remotes/$GIT_SVN";
aef4e921 492 my @refs = command(qw/rev-list --no-merges/, "$gs..$head");
45bf473a 493 my $last_rev;
b22d4497 494 foreach my $d (reverse @refs) {
aef4e921 495 if (!verify_ref("$d~1")) {
48d044b5
EW
496 die "Commit $d\n",
497 "has no parent commit, and therefore ",
498 "nothing to diff against.\n",
499 "You should be working from a repository ",
500 "originally created by git-svn\n";
501 }
45bf473a
EW
502 unless (defined $last_rev) {
503 (undef, $last_rev, undef) = cmt_metadata("$d~1");
504 unless (defined $last_rev) {
505 die "Unable to extract revision information ",
506 "from commit $d~1\n";
507 }
508 }
b22d4497
EW
509 if ($_dry_run) {
510 print "diff-tree $d~1 $d\n";
511 } else {
45bf473a
EW
512 if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
513 $last_rev = $r;
514 } # else: no changes, same $last_rev
b22d4497
EW
515 }
516 }
517 return if $_dry_run;
518 fetch();
c3a41037 519 my @diff = command('diff-tree', 'HEAD', $gs, '--');
b22d4497
EW
520 my @finish;
521 if (@diff) {
522 @finish = qw/rebase/;
523 push @finish, qw/--merge/ if $_merge;
524 push @finish, "--strategy=$_strategy" if $_strategy;
c3a41037 525 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
b22d4497 526 } else {
c3a41037 527 print "No changes between current HEAD and $gs\n",
dd31da2f 528 "Resetting to the latest $gs\n";
4769489a 529 @finish = qw/reset --mixed/;
b22d4497 530 }
aef4e921 531 command_noisy(@finish, $gs);
b22d4497
EW
532}
533
a5e0cedc 534sub show_ignore {
883d0a78 535 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
a5e0cedc 536 my $repo;
747fa12c 537 $SVN ||= libsvn_connect($SVN_URL);
a5e0cedc 538 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
fffe694d 539 libsvn_traverse_ignore(\*STDOUT, '', $r);
a5e0cedc
EW
540}
541
9d55b41a
EW
542sub graft_branches {
543 my $gr_file = "$GIT_DIR/info/grafts";
544 my ($grafts, $comments) = read_grafts($gr_file);
545 my $gr_sha1;
546
547 if (%$grafts) {
548 # temporarily disable our grafts file to make this idempotent
aef4e921 549 chomp($gr_sha1 = command(qw/hash-object -w/,$gr_file));
9d55b41a
EW
550 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
551 }
552
553 my $l_map = read_url_paths();
554 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
555 unless ($_no_default_regex) {
c1927a85
EW
556 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
557 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
558 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
9d55b41a
EW
559 }
560 foreach my $u (keys %$l_map) {
561 if (@re) {
562 foreach my $p (keys %{$l_map->{$u}}) {
c1927a85 563 graft_merge_msg($grafts,$l_map,$u,$p,@re);
9d55b41a
EW
564 }
565 }
a5e0cedc 566 unless ($_no_graft_copy) {
b9c85187 567 graft_file_copy_lib($grafts,$l_map,$u);
a5e0cedc 568 }
9d55b41a 569 }
c1927a85 570 graft_tree_joins($grafts);
9d55b41a
EW
571
572 write_grafts($grafts, $comments, $gr_file);
573 unlink "$gr_file~$gr_sha1" if $gr_sha1;
574}
575
576sub multi_init {
577 my $url = shift;
98327e58
EW
578 unless (defined $_trunk || defined $_branches || defined $_tags) {
579 usage(1);
9d55b41a 580 }
98327e58
EW
581 if (defined $_trunk) {
582 my $trunk_url = complete_svn_url($url, $_trunk);
583 my $ch_id;
584 if ($GIT_SVN eq 'git-svn') {
585 $ch_id = 1;
586 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
587 }
588 init_vars();
589 unless (-d $GIT_SVN_DIR) {
590 if ($ch_id) {
591 print "GIT_SVN_ID set to 'trunk' for ",
592 "$trunk_url ($_trunk)\n";
593 }
594 init($trunk_url);
e0d10e1c 595 command_noisy('config', 'svn.trunk', $trunk_url);
98327e58 596 }
c35b96e7 597 }
ae410987
EW
598 $_prefix = '' unless defined $_prefix;
599 complete_url_ls_init($url, $_branches, '--branches/-b', $_prefix);
600 complete_url_ls_init($url, $_tags, '--tags/-t', $_prefix . 'tags/');
9d55b41a
EW
601}
602
603sub multi_fetch {
604 # try to do trunk first, since branches/tags
605 # may be descended from it.
cf7424b0
EW
606 if (-e "$GIT_DIR/svn/trunk/info/url") {
607 fetch_child_id('trunk', @_);
9d55b41a
EW
608 }
609 rec_fetch('', "$GIT_DIR/svn", @_);
610}
611
79bb8d88
EW
612sub show_log {
613 my (@args) = @_;
614 my ($r_min, $r_max);
615 my $r_last = -1; # prevent dupes
616 rload_authors() if $_authors;
617 if (defined $TZ) {
618 $ENV{TZ} = $TZ;
619 } else {
620 delete $ENV{TZ};
621 }
622 if (defined $_revision) {
623 if ($_revision =~ /^(\d+):(\d+)$/) {
624 ($r_min, $r_max) = ($1, $2);
625 } elsif ($_revision =~ /^\d+$/) {
626 $r_min = $r_max = $_revision;
627 } else {
628 print STDERR "-r$_revision is not supported, use ",
629 "standard \'git log\' arguments instead\n";
630 exit 1;
631 }
632 }
633
9aca0258 634 config_pager();
aef4e921
EW
635 @args = (git_svn_log_cmd($r_min, $r_max), @args);
636 my $log = command_output_pipe(@args);
9aca0258 637 run_pager();
79bb8d88 638 my (@k, $c, $d);
c0d48222 639
79bb8d88 640 while (<$log>) {
9aca0258 641 if (/^${_esc_color}commit ($sha1_short)/o) {
79bb8d88 642 my $cmt = $1;
c0d48222 643 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
79bb8d88
EW
644 $r_last = $c->{r};
645 process_commit($c, $r_min, $r_max, \@k) or
646 goto out;
647 }
648 $d = undef;
649 $c = { c => $cmt };
9aca0258 650 } elsif (/^${_esc_color}author (.+) (\d+) ([\-\+]?\d+)$/) {
79bb8d88 651 get_author_info($c, $1, $2, $3);
9aca0258 652 } elsif (/^${_esc_color}(?:tree|parent|committer) /) {
79bb8d88 653 # ignore
9aca0258 654 } elsif (/^${_esc_color}:\d{6} \d{6} $sha1_short/o) {
79bb8d88 655 push @{$c->{raw}}, $_;
9aca0258 656 } elsif (/^${_esc_color}[ACRMDT]\t/) {
747fa12c 657 # we could add $SVN->{svn_path} here, but that requires
74a31a10 658 # remote access at the moment (repo_path_split)...
9aca0258 659 s#^(${_esc_color})([ACRMDT])\t#$1 $2 #;
74a31a10 660 push @{$c->{changed}}, $_;
9aca0258 661 } elsif (/^${_esc_color}diff /) {
79bb8d88
EW
662 $d = 1;
663 push @{$c->{diff}}, $_;
664 } elsif ($d) {
665 push @{$c->{diff}}, $_;
9aca0258 666 } elsif (/^${_esc_color} (git-svn-id:.+)$/) {
74a31a10 667 ($c->{url}, $c->{r}, undef) = extract_metadata($1);
9aca0258 668 } elsif (s/^${_esc_color} //) {
79bb8d88
EW
669 push @{$c->{l}}, $_;
670 }
671 }
672 if ($c && defined $c->{r} && $c->{r} != $r_last) {
673 $r_last = $c->{r};
674 process_commit($c, $r_min, $r_max, \@k);
675 }
676 if (@k) {
677 my $swap = $r_max;
678 $r_max = $r_min;
679 $r_min = $swap;
680 process_commit($_, $r_min, $r_max) foreach reverse @k;
681 }
682out:
22600a25 683 close $log;
79bb8d88
EW
684 print '-' x72,"\n" unless $_incremental || $_oneline;
685}
686
27e9fb8d
EW
687sub commit_diff_usage {
688 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
689 exit 1
690}
691
692sub commit_diff {
27e9fb8d
EW
693 my $ta = shift or commit_diff_usage();
694 my $tb = shift or commit_diff_usage();
695 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
696 print STDERR "Needed URL or usable git-svn id command-line\n";
697 commit_diff_usage();
698 }
e70dc780
EW
699 my $r = shift;
700 unless (defined $r) {
701 if (defined $_revision) {
702 $r = $_revision
703 } else {
704 die "-r|--revision is a required argument\n";
705 }
706 }
27e9fb8d
EW
707 if (defined $_message && defined $_file) {
708 print STDERR "Both --message/-m and --file/-F specified ",
709 "for the commit message.\n",
710 "I have no idea what you mean\n";
711 exit 1;
712 }
713 if (defined $_file) {
4ad4515d 714 $_message = file_to_s($_file);
27e9fb8d
EW
715 } else {
716 $_message ||= get_commit_message($tb,
717 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
718 }
747fa12c 719 $SVN ||= libsvn_connect($SVN_URL);
45bf473a
EW
720 if ($r eq 'HEAD') {
721 $r = $SVN->get_latest_revnum;
722 } elsif ($r !~ /^\d+$/) {
723 die "revision argument: $r not understood by git-svn\n";
724 }
27e9fb8d 725 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
45bf473a
EW
726 my $rev_committed;
727 my $ed = SVN::Git::Editor->new({ r => $r,
747fa12c
EW
728 ra => libsvn_dup_ra($SVN),
729 c => $tb,
730 svn_path => $SVN->{svn_path}
27e9fb8d
EW
731 },
732 $SVN->get_commit_editor($_message,
45bf473a
EW
733 sub {
734 $rev_committed = $_[0];
735 print "Committed $_[0]\n";
736 }, @lock)
27e9fb8d 737 );
d25c26e7
EW
738 eval {
739 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
740 if (@$mods == 0) {
741 print "No changes\n$ta == $tb\n";
742 $ed->abort_edit;
743 } else {
744 $ed->close_edit;
745 }
746 };
747 fatal "$@\n" if $@;
5f641ccc 748 $_message = $_file = undef;
45bf473a 749 return $rev_committed;
27e9fb8d
EW
750}
751
3397f9df
EW
752########################### utility functions #########################
753
c0d48222
EW
754sub cmt_showable {
755 my ($c) = @_;
756 return 1 if defined $c->{r};
757 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
758 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
aef4e921 759 my @msg = command(qw/cat-file commit/, $c->{c});
c0d48222
EW
760 shift @msg while ($msg[0] ne "\n");
761 shift @msg;
762 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
763
764 (undef, $c->{r}, undef) = extract_metadata(
765 (grep(/^git-svn-id: /, @msg))[-1]);
766 }
767 return defined $c->{r};
768}
769
9aca0258
EW
770sub log_use_color {
771 return 1 if $_color;
62e1aeab
JH
772 my ($dc, $dcvar);
773 $dcvar = 'color.diff';
e0d10e1c 774 $dc = `git-config --get $dcvar`;
62e1aeab
JH
775 if ($dc eq '') {
776 # nothing at all; fallback to "diff.color"
777 $dcvar = 'diff.color';
e0d10e1c 778 $dc = `git-config --get $dcvar`;
62e1aeab
JH
779 }
780 chomp($dc);
9aca0258 781 if ($dc eq 'auto') {
62e1aeab 782 my $pc;
e0d10e1c 783 $pc = `git-config --get color.pager`;
62e1aeab
JH
784 if ($pc eq '') {
785 # does not have it -- fallback to pager.color
e0d10e1c 786 $pc = `git-config --bool --get pager.color`;
62e1aeab
JH
787 }
788 else {
e0d10e1c 789 $pc = `git-config --bool --get color.pager`;
62e1aeab
JH
790 if ($?) {
791 $pc = 'false';
792 }
793 }
794 chomp($pc);
795 if (-t *STDOUT || (defined $_pager && $pc eq 'true')) {
9aca0258
EW
796 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
797 }
798 return 0;
799 }
800 return 0 if $dc eq 'never';
801 return 1 if $dc eq 'always';
e0d10e1c 802 chomp($dc = `git-config --bool --get $dcvar`);
62e1aeab 803 return ($dc eq 'true');
9aca0258
EW
804}
805
c0d48222
EW
806sub git_svn_log_cmd {
807 my ($r_min, $r_max) = @_;
aef4e921 808 my @cmd = (qw/log --abbrev-commit --pretty=raw
c0d48222 809 --default/, "refs/remotes/$GIT_SVN");
74a31a10
EW
810 push @cmd, '-r' unless $_non_recursive;
811 push @cmd, qw/--raw --name-status/ if $_verbose;
9aca0258 812 push @cmd, '--color' if log_use_color();
c0d48222
EW
813 return @cmd unless defined $r_max;
814 if ($r_max == $r_min) {
815 push @cmd, '--max-count=1';
816 if (my $c = revdb_get($REVDB, $r_max)) {
817 push @cmd, $c;
818 }
819 } else {
820 my ($c_min, $c_max);
821 $c_max = revdb_get($REVDB, $r_max);
822 $c_min = revdb_get($REVDB, $r_min);
74a31a10 823 if (defined $c_min && defined $c_max) {
c0d48222
EW
824 if ($r_max > $r_max) {
825 push @cmd, "$c_min..$c_max";
826 } else {
827 push @cmd, "$c_max..$c_min";
828 }
829 } elsif ($r_max > $r_min) {
830 push @cmd, $c_max;
831 } else {
832 push @cmd, $c_min;
833 }
834 }
835 return @cmd;
836}
837
cf7424b0
EW
838sub fetch_child_id {
839 my $id = shift;
840 print "Fetching $id\n";
841 my $ref = "$GIT_DIR/refs/remotes/$id";
a00439ac 842 defined(my $pid = open my $fh, '-|') or croak $!;
cf7424b0
EW
843 if (!$pid) {
844 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
845 init_vars();
846 fetch(@_);
847 exit 0;
848 }
cf7424b0 849 while (<$fh>) {
a00439ac 850 print $_;
2a3240be 851 check_repack() if (/^r\d+ = $sha1/o);
cf7424b0 852 }
a00439ac 853 close $fh or croak $?;
cf7424b0
EW
854}
855
9d55b41a
EW
856sub rec_fetch {
857 my ($pfx, $p, @args) = @_;
858 my @dir;
859 foreach (sort <$p/*>) {
860 if (-r "$_/info/url") {
861 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
862 my $id = $pfx . basename $_;
863 next if $id eq 'trunk';
cf7424b0 864 fetch_child_id($id, @args);
9d55b41a
EW
865 } elsif (-d $_) {
866 push @dir, $_;
867 }
868 }
869 foreach (@dir) {
870 my $x = $_;
871 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
872 rec_fetch($x, $_);
873 }
874}
875
98327e58
EW
876sub complete_svn_url {
877 my ($url, $path) = @_;
878 $path =~ s#/+$##;
879 $url =~ s#/+$## if $url;
880 if ($path !~ m#^[a-z\+]+://#) {
881 $path = '/' . $path if ($path !~ m#^/#);
882 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
883 fatal("E: '$path' is not a complete URL ",
884 "and a separate URL is not specified\n");
885 }
886 $path = $url . $path;
887 }
888 return $path;
889}
890
9d55b41a 891sub complete_url_ls_init {
98327e58
EW
892 my ($url, $path, $switch, $pfx) = @_;
893 unless ($path) {
9d55b41a
EW
894 print STDERR "W: $switch not specified\n";
895 return;
896 }
98327e58
EW
897 my $full_url = complete_svn_url($url, $path);
898 my @ls = libsvn_ls_fullurl($full_url);
9d55b41a
EW
899 defined(my $pid = fork) or croak $!;
900 if (!$pid) {
98327e58 901 foreach my $u (map { "$full_url/$_" } (grep m!/$!, @ls)) {
9d55b41a 902 $u =~ s#/+$##;
98327e58 903 if ($u !~ m!\Q$full_url\E/(.+)$!) {
9d55b41a
EW
904 print STDERR "W: Unrecognized URL: $u\n";
905 die "This should never happen\n";
906 }
c35b96e7 907 # don't try to init already existing refs
9d55b41a 908 my $id = $pfx.$1;
9d55b41a
EW
909 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
910 init_vars();
c35b96e7
EW
911 unless (-d $GIT_SVN_DIR) {
912 print "init $u => $id\n";
913 init($u);
914 }
9d55b41a
EW
915 }
916 exit 0;
917 }
918 waitpid $pid, 0;
919 croak $? if $?;
c35b96e7 920 my ($n) = ($switch =~ /^--(\w+)/);
e0d10e1c 921 command_noisy('config', "svn.$n", $full_url);
9d55b41a
EW
922}
923
924sub common_prefix {
925 my $paths = shift;
926 my %common;
927 foreach (@$paths) {
928 my @tmp = split m#/#, $_;
929 my $p = '';
930 while (my $x = shift @tmp) {
931 $p .= "/$x";
932 $common{$p} ||= 0;
933 $common{$p}++;
934 }
935 }
936 foreach (sort {length $b <=> length $a} keys %common) {
937 if ($common{$_} == @$paths) {
938 return $_;
939 }
940 }
941 return '';
942}
943
c1927a85
EW
944# grafts set here are 'stronger' in that they're based on actual tree
945# matches, and won't be deleted from merge-base checking in write_grafts()
946sub graft_tree_joins {
947 my $grafts = shift;
948 map_tree_joins() if (@_branch_from && !%tree_map);
949 return unless %tree_map;
950
951 git_svn_each(sub {
952 my $i = shift;
aef4e921
EW
953 my @args = (qw/rev-list --pretty=raw/, "refs/remotes/$i");
954 my ($fh, $ctx) = command_output_pipe(@args);
c1927a85
EW
955 while (<$fh>) {
956 next unless /^commit ($sha1)$/o;
957 my $c = $1;
958 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
959 next unless $tree_map{$t};
960
961 my $l;
962 do {
963 $l = readline $fh;
964 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
965
966 my ($s, $tz) = ($1, $2);
967 if ($tz =~ s/^\+//) {
968 $s += tz_to_s_offset($tz);
969 } elsif ($tz =~ s/^\-//) {
970 $s -= tz_to_s_offset($tz);
971 }
972
973 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
974
975 foreach my $p (@{$tree_map{$t}}) {
976 next if $p eq $c;
aef4e921 977 my $mb = eval { command('merge-base', $c, $p) };
c1927a85
EW
978 next unless ($@ || $?);
979 if (defined $r_a) {
980 # see if SVN says it's a relative
981 my ($url_b, $r_b, $uuid_b) =
982 cmt_metadata($p);
983 next if (defined $url_b &&
984 defined $url_a &&
985 ($url_a eq $url_b) &&
986 ($uuid_a eq $uuid_b));
987 if ($uuid_a eq $uuid_b) {
988 if ($r_b < $r_a) {
989 $grafts->{$c}->{$p} = 2;
990 next;
991 } elsif ($r_b > $r_a) {
992 $grafts->{$p}->{$c} = 2;
993 next;
994 }
995 }
996 }
997 my $ct = get_commit_time($p);
998 if ($ct < $s) {
999 $grafts->{$c}->{$p} = 2;
1000 } elsif ($ct > $s) {
1001 $grafts->{$p}->{$c} = 2;
1002 }
1003 # what should we do when $ct == $s ?
1004 }
1005 }
aef4e921 1006 command_close_pipe($fh, $ctx);
c1927a85
EW
1007 });
1008}
1009
a5e0cedc
EW
1010sub graft_file_copy_lib {
1011 my ($grafts, $l_map, $u) = @_;
1012 my $tree_paths = $l_map->{$u};
1013 my $pfx = common_prefix([keys %$tree_paths]);
1014 my ($repo, $path) = repo_path_split($u.$pfx);
747fa12c 1015 $SVN = libsvn_connect($repo);
a5e0cedc
EW
1016
1017 my ($base, $head) = libsvn_parse_revision();
1018 my $inc = 1000;
1019 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
42d32870
EW
1020 my $eh = $SVN::Error::handler;
1021 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
a5e0cedc
EW
1022 while (1) {
1023 my $pool = SVN::Pool->new;
747fa12c 1024 libsvn_get_log(libsvn_dup_ra($SVN), [$path],
0864e3ba 1025 $min, $max, 0, 2, 1,
a5e0cedc
EW
1026 sub {
1027 libsvn_graft_file_copies($grafts, $tree_paths,
1028 $path, @_);
1029 }, $pool);
1030 $pool->clear;
1031 last if ($max >= $head);
1032 $min = $max + 1;
1033 $max += $inc;
1034 $max = $head if ($max > $head);
1035 }
42d32870 1036 $SVN::Error::handler = $eh;
a5e0cedc
EW
1037}
1038
9d55b41a
EW
1039sub process_merge_msg_matches {
1040 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1041 my (@strong, @weak);
1042 foreach (@matches) {
1043 # merging with ourselves is not interesting
1044 next if $_ eq $p;
1045 if ($l_map->{$u}->{$_}) {
1046 push @strong, $_;
1047 } else {
1048 push @weak, $_;
1049 }
1050 }
1051 foreach my $w (@weak) {
1052 last if @strong;
1053 # no exact match, use branch name as regexp.
1054 my $re = qr/\Q$w\E/i;
1055 foreach (keys %{$l_map->{$u}}) {
1056 if (/$re/) {
c1927a85 1057 push @strong, $l_map->{$u}->{$_};
9d55b41a
EW
1058 last;
1059 }
1060 }
1061 last if @strong;
1062 $w = basename($w);
1063 $re = qr/\Q$w\E/i;
1064 foreach (keys %{$l_map->{$u}}) {
1065 if (/$re/) {
c1927a85 1066 push @strong, $l_map->{$u}->{$_};
9d55b41a
EW
1067 last;
1068 }
1069 }
1070 }
1071 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1072 \s(?:[a-f\d\-]+)$/xsm);
1073 unless (defined $rev) {
1074 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1075 \@(?:[a-f\d\-]+)/xsm);
1076 return unless defined $rev;
1077 }
1078 foreach my $m (@strong) {
c1927a85 1079 my ($r0, $s0) = find_rev_before($rev, $m, 1);
9d55b41a
EW
1080 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1081 }
1082}
1083
1084sub graft_merge_msg {
1085 my ($grafts, $l_map, $u, $p, @re) = @_;
1086
1087 my $x = $l_map->{$u}->{$p};
9a5e4075 1088 my $rl = rev_list_raw("refs/remotes/$x");
9d55b41a
EW
1089 while (my $c = next_rev_list_entry($rl)) {
1090 foreach my $re (@re) {
1091 my (@br) = ($c->{m} =~ /$re/g);
1092 next unless @br;
1093 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1094 }
1095 }
1096}
1097
1d52aba8
EW
1098sub read_uuid {
1099 return if $SVN_UUID;
b9c85187
EW
1100 my $pool = SVN::Pool->new;
1101 $SVN_UUID = $SVN->get_uuid($pool);
1102 $pool->clear;
883d0a78
EW
1103}
1104
aef4e921
EW
1105sub verify_ref {
1106 my ($ref) = @_;
2c5c1d53
EW
1107 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1108 { STDERR => 0 }); };
aef4e921
EW
1109}
1110
883d0a78
EW
1111sub repo_path_split {
1112 my $full_url = shift;
1113 $full_url =~ s#/+$##;
1114
1115 foreach (@repo_path_split_cache) {
1116 if ($full_url =~ s#$_##) {
1117 my $u = $1;
1118 $full_url =~ s#^/+##;
1119 return ($u, $full_url);
1120 }
1121 }
b9c85187
EW
1122 my $tmp = libsvn_connect($full_url);
1123 return ($tmp->{repos_root}, $tmp->{svn_path});
1d52aba8
EW
1124}
1125
3397f9df
EW
1126sub setup_git_svn {
1127 defined $SVN_URL or croak "SVN repository location required\n";
1128 unless (-d $GIT_DIR) {
1129 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1130 }
883d0a78
EW
1131 mkpath([$GIT_SVN_DIR]);
1132 mkpath(["$GIT_SVN_DIR/info"]);
42d32870
EW
1133 open my $fh, '>>',$REVDB or croak $!;
1134 close $fh;
883d0a78 1135 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
3397f9df 1136
3397f9df
EW
1137}
1138
a5e0cedc 1139sub get_tree_from_treeish {
cf52b8f0
EW
1140 my ($treeish) = @_;
1141 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
aef4e921 1142 my $type = command_oneline(qw/cat-file -t/, $treeish);
cf52b8f0
EW
1143 my $expected;
1144 while ($type eq 'tag') {
aef4e921 1145 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
cf52b8f0
EW
1146 }
1147 if ($type eq 'commit') {
aef4e921
EW
1148 $expected = (grep /^tree /, command(qw/cat-file commit/,
1149 $treeish))[0];
cf52b8f0
EW
1150 ($expected) = ($expected =~ /^tree ($sha1)$/);
1151 die "Unable to get tree from $treeish\n" unless $expected;
1152 } elsif ($type eq 'tree') {
1153 $expected = $treeish;
1154 } else {
1155 die "$treeish is a $type, expected tree, tag or commit\n";
1156 }
a5e0cedc
EW
1157 return $expected;
1158}
1159
aef4e921
EW
1160sub get_diff {
1161 my ($from, $treeish) = @_;
aef4e921
EW
1162 print "diff-tree $from $treeish\n";
1163 my @diff_tree = qw(diff-tree -z -r);
1164 if ($_cp_similarity) {
1165 push @diff_tree, "-C$_cp_similarity";
1166 } else {
1167 push @diff_tree, '-C';
1168 }
1169 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1170 push @diff_tree, "-l$_l" if defined $_l;
1171 push @diff_tree, $from, $treeish;
1172 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3397f9df
EW
1173 local $/ = "\0";
1174 my $state = 'meta';
1175 my @mods;
1176 while (<$diff_fh>) {
1177 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
1178 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1179 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1180 push @mods, { mode_a => $1, mode_b => $2,
1181 sha1_b => $3, chg => $4 };
1182 if ($4 =~ /^(?:C|R)$/) {
1183 $state = 'file_a';
1184 } else {
1185 $state = 'file_b';
1186 }
1187 } elsif ($state eq 'file_a') {
cf52b8f0 1188 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 1189 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 1190 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
1191 }
1192 $x->{file_a} = $_;
1193 $state = 'file_b';
1194 } elsif ($state eq 'file_b') {
cf52b8f0 1195 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 1196 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 1197 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
1198 }
1199 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 1200 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
1201 }
1202 $x->{file_b} = $_;
1203 $state = 'meta';
1204 } else {
cf52b8f0 1205 croak "Error parsing $_\n";
3397f9df
EW
1206 }
1207 }
aef4e921 1208 command_close_pipe($diff_fh, $ctx);
3397f9df
EW
1209 return \@mods;
1210}
1211
a5e0cedc 1212sub libsvn_checkout_tree {
42d32870
EW
1213 my ($from, $treeish, $ed) = @_;
1214 my $mods = get_diff($from, $treeish);
a5e0cedc
EW
1215 return $mods unless (scalar @$mods);
1216 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1217 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1218 my $f = $m->{chg};
1219 if (defined $o{$f}) {
80f50749 1220 $ed->$f($m, $_q);
a5e0cedc
EW
1221 } else {
1222 croak "Invalid change type: $f\n";
1223 }
1224 }
80f50749 1225 $ed->rmdirs($_q) if $_rmdir;
a5e0cedc
EW
1226 return $mods;
1227}
1228
a5e0cedc
EW
1229sub get_commit_message {
1230 my ($commit, $commit_msg) = (@_);
e17512f3 1231 my %log_msg = ( msg => '' );
ac8e0b91 1232 open my $msg, '>', $commit_msg or croak $!;
3397f9df 1233
aef4e921 1234 my $type = command_oneline(qw/cat-file -t/, $commit);
4ad4515d 1235 if ($type eq 'commit' || $type eq 'tag') {
aef4e921
EW
1236 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
1237 $type, $commit);
3397f9df
EW
1238 my $in_msg = 0;
1239 while (<$msg_fh>) {
1240 if (!$in_msg) {
1241 $in_msg = 1 if (/^\s*$/);
df746c5a
EW
1242 } elsif (/^git-svn-id: /) {
1243 # skip this, we regenerate the correct one
1244 # on re-fetch anyways
3397f9df
EW
1245 } else {
1246 print $msg $_ or croak $!;
1247 }
1248 }
aef4e921 1249 command_close_pipe($msg_fh, $ctx);
3397f9df
EW
1250 }
1251 close $msg or croak $!;
1252
1253 if ($_edit || ($type eq 'tree')) {
1254 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1255 system($editor, $commit_msg);
1256 }
ac8e0b91
EW
1257
1258 # file_to_s removes all trailing newlines, so just use chomp() here:
1259 open $msg, '<', $commit_msg or croak $!;
1260 { local $/; chomp($log_msg{msg} = <$msg>); }
1261 close $msg or croak $!;
1262
a5e0cedc
EW
1263 return \%log_msg;
1264}
1265
27e9fb8d
EW
1266sub set_svn_commit_env {
1267 if (defined $LC_ALL) {
1268 $ENV{LC_ALL} = $LC_ALL;
1269 } else {
1270 delete $ENV{LC_ALL};
1271 }
1272}
1273
9d55b41a 1274sub rev_list_raw {
aef4e921
EW
1275 my ($fh, $c) = command_output_pipe(qw/rev-list --pretty=raw/, @_);
1276 return { fh => $fh, ctx => $c, t => { } };
9d55b41a
EW
1277}
1278
1279sub next_rev_list_entry {
1280 my $rl = shift;
1281 my $fh = $rl->{fh};
1282 my $x = $rl->{t};
1283 while (<$fh>) {
1284 if (/^commit ($sha1)$/o) {
1285 if ($x->{c}) {
1286 $rl->{t} = { c => $1 };
1287 return $x;
1288 } else {
1289 $x->{c} = $1;
1290 }
1291 } elsif (/^parent ($sha1)$/o) {
1292 $x->{p}->{$1} = 1;
1293 } elsif (s/^ //) {
1294 $x->{m} ||= '';
1295 $x->{m} .= $_;
1296 }
1297 }
aef4e921 1298 command_close_pipe($fh, $rl->{ctx});
9d55b41a
EW
1299 return ($x != $rl->{t}) ? $x : undef;
1300}
1301
3397f9df
EW
1302sub s_to_file {
1303 my ($str, $file, $mode) = @_;
1304 open my $fd,'>',$file or croak $!;
1305 print $fd $str,"\n" or croak $!;
1306 close $fd or croak $!;
1307 chmod ($mode &~ umask, $file) if (defined $mode);
1308}
1309
1310sub file_to_s {
1311 my $file = shift;
1312 open my $fd,'<',$file or croak "$!: file: $file\n";
1313 local $/;
1314 my $ret = <$fd>;
1315 close $fd or croak $!;
1316 $ret =~ s/\s*$//s;
1317 return $ret;
1318}
1319
1320sub assert_revision_unknown {
42d32870
EW
1321 my $r = shift;
1322 if (my $c = revdb_get($REVDB, $r)) {
1323 croak "$r = $c already exists! Why are we refetching it?";
3397f9df
EW
1324 }
1325}
1326
3397f9df
EW
1327sub git_commit {
1328 my ($log_msg, @parents) = @_;
1329 assert_revision_unknown($log_msg->{revision});
69f0d91e
EW
1330 map_tree_joins() if (@_branch_from && !%tree_map);
1331
a5e0cedc
EW
1332 my (@tmp_parents, @exec_parents, %seen_parent);
1333 if (my $lparents = $log_msg->{parents}) {
1334 @tmp_parents = @$lparents
1335 }
3397f9df
EW
1336 # commit parents can be conditionally bound to a particular
1337 # svn revision via: "svn_revno=commit_sha1", filter them out here:
3397f9df
EW
1338 foreach my $p (@parents) {
1339 next unless defined $p;
1340 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1341 if ($1 == $log_msg->{revision}) {
a5e0cedc 1342 push @tmp_parents, $2;
3397f9df
EW
1343 }
1344 } else {
a5e0cedc 1345 push @tmp_parents, $p if $p =~ /$sha1_short/o;
3397f9df
EW
1346 }
1347 }
a5e0cedc
EW
1348 my $tree = $log_msg->{tree};
1349 if (!defined $tree) {
1350 my $index = set_index($GIT_SVN_INDEX);
aef4e921 1351 $tree = command_oneline('write-tree');
b8c92cad 1352 croak $? if $?;
a5e0cedc
EW
1353 restore_index($index);
1354 }
a00439ac
EW
1355 # just in case we clobber the existing ref, we still want that ref
1356 # as our parent:
aef4e921 1357 if (my $cur = verify_ref("refs/remotes/$GIT_SVN^0")) {
c53d696b 1358 chomp $cur;
a00439ac
EW
1359 push @tmp_parents, $cur;
1360 }
1361
a5e0cedc 1362 if (exists $tree_map{$tree}) {
c1927a85
EW
1363 foreach my $p (@{$tree_map{$tree}}) {
1364 my $skip;
1365 foreach (@tmp_parents) {
1366 # see if a common parent is found
aef4e921 1367 my $mb = eval { command('merge-base', $_, $p) };
c1927a85
EW
1368 next if ($@ || $?);
1369 $skip = 1;
1370 last;
1371 }
1372 next if $skip;
1373 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
1374 next if (($SVN_UUID eq $uuid_p) &&
1375 ($log_msg->{revision} > $r_p));
1376 next if (defined $url_p && defined $SVN_URL &&
1377 ($SVN_UUID eq $uuid_p) &&
1378 ($url_p eq $SVN_URL));
1379 push @tmp_parents, $p;
1380 }
a5e0cedc
EW
1381 }
1382 foreach (@tmp_parents) {
1383 next if $seen_parent{$_};
1384 $seen_parent{$_} = 1;
1385 push @exec_parents, $_;
1386 # MAXPARENT is defined to 16 in commit-tree.c:
1387 last if @exec_parents > 16;
1388 }
1389
a00439ac
EW
1390 set_commit_env($log_msg);
1391 my @exec = ('git-commit-tree', $tree);
1392 push @exec, '-p', $_ foreach @exec_parents;
1393 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1394 or croak $!;
1395 print $msg_fh $log_msg->{msg} or croak $!;
1396 unless ($_no_metadata) {
1397 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1ca72aef 1398 " $SVN_UUID\n" or croak $!;
3397f9df 1399 }
a00439ac
EW
1400 $msg_fh->flush == 0 or croak $!;
1401 close $msg_fh or croak $!;
3397f9df 1402 chomp(my $commit = do { local $/; <$out_fh> });
a00439ac
EW
1403 close $out_fh or croak $!;
1404 waitpid $pid, 0;
1405 croak $? if $?;
3397f9df 1406 if ($commit !~ /^$sha1$/o) {
a00439ac 1407 die "Failed to commit, invalid sha1: $commit\n";
3397f9df 1408 }
aef4e921 1409 command_noisy('update-ref',"refs/remotes/$GIT_SVN",$commit);
42d32870
EW
1410 revdb_set($REVDB, $log_msg->{revision}, $commit);
1411
a5e0cedc 1412 # this output is read via pipe, do not change:
3397f9df 1413 print "r$log_msg->{revision} = $commit\n";
cf7424b0
EW
1414 return $commit;
1415}
1416
1417sub check_repack {
dc5869c0
EW
1418 if ($_repack && (--$_repack_nr == 0)) {
1419 $_repack_nr = $_repack;
aef4e921
EW
1420 # repack doesn't use any arguments with spaces in them, does it?
1421 command_noisy('repack', split(/\s+/, $_repack_flags));
dc5869c0 1422 }
3397f9df
EW
1423}
1424
a9612be2 1425sub set_commit_env {
1ca72aef 1426 my ($log_msg) = @_;
a9612be2 1427 my $author = $log_msg->{author};
a5e0cedc
EW
1428 if (!defined $author || length $author == 0) {
1429 $author = '(no author)';
1430 }
a9612be2 1431 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1ca72aef 1432 : ($author,"$author\@$SVN_UUID");
a9612be2
EW
1433 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1434 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1435 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1436}
1437
2beb3cdd 1438sub check_upgrade_needed {
cf7424b0 1439 if (!-r $REVDB) {
1a82e793 1440 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
cf7424b0
EW
1441 open my $fh, '>>',$REVDB or croak $!;
1442 close $fh;
1443 }
aef4e921
EW
1444 return unless eval {
1445 command([qw/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
1446 {STDERR => 0});
2beb3cdd 1447 };
aef4e921 1448 my $head = eval { command('rev-parse',"refs/remotes/$GIT_SVN") };
2beb3cdd
EW
1449 if ($@ || !$head) {
1450 print STDERR "Please run: $0 rebuild --upgrade\n";
1451 exit 1;
1452 }
1453}
1454
69f0d91e
EW
1455# fills %tree_map with a reverse mapping of trees to commits. Useful
1456# for finding parents to commit on.
1457sub map_tree_joins {
098749d9 1458 my %seen;
69f0d91e 1459 foreach my $br (@_branch_from) {
aef4e921
EW
1460 my $pipe = command_output_pipe(qw/rev-list
1461 --topo-order --pretty=raw/, $br);
69f0d91e
EW
1462 while (<$pipe>) {
1463 if (/^commit ($sha1)$/o) {
1464 my $commit = $1;
098749d9
EW
1465
1466 # if we've seen a commit,
1467 # we've seen its parents
1468 last if $seen{$commit};
69f0d91e
EW
1469 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1470 unless (defined $tree) {
1471 die "Failed to parse commit $commit\n";
1472 }
1473 push @{$tree_map{$tree}}, $commit;
098749d9 1474 $seen{$commit} = 1;
69f0d91e
EW
1475 }
1476 }
22600a25 1477 close $pipe;
69f0d91e
EW
1478 }
1479}
1480
bf78b1d8
EW
1481sub load_all_refs {
1482 if (@_branch_from) {
1483 print STDERR '--branch|-b parameters are ignored when ',
1484 "--branch-all-refs|-B is passed\n";
1485 }
1486
1487 # don't worry about rev-list on non-commit objects/tags,
1488 # it shouldn't blow up if a ref is a blob or tree...
aef4e921 1489 @_branch_from = command(qw/rev-parse --symbolic --all/);
bf78b1d8
EW
1490}
1491
eeb0abe0
EW
1492# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1493sub load_authors {
1494 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1495 while (<$authors>) {
1496 chomp;
8815788e 1497 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
eeb0abe0
EW
1498 my ($user, $name, $email) = ($1, $2, $3);
1499 $users{$user} = [$name, $email];
1500 }
1501 close $authors or croak $!;
1502}
1503
79bb8d88
EW
1504sub rload_authors {
1505 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1506 while (<$authors>) {
1507 chomp;
1508 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1509 my ($user, $name, $email) = ($1, $2, $3);
1510 $rusers{"$name <$email>"} = $user;
1511 }
1512 close $authors or croak $!;
1513}
1514
9d55b41a
EW
1515sub git_svn_each {
1516 my $sub = shift;
aef4e921 1517 foreach (command(qw/rev-parse --symbolic --all/)) {
9d55b41a
EW
1518 next unless s#^refs/remotes/##;
1519 chomp $_;
1520 next unless -f "$GIT_DIR/svn/$_/info/url";
1521 &$sub($_);
1522 }
1523}
1524
42d32870
EW
1525sub migrate_revdb {
1526 git_svn_each(sub {
1527 my $id = shift;
1528 defined(my $pid = fork) or croak $!;
1529 if (!$pid) {
1530 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
1531 init_vars();
1532 exit 0 if -r $REVDB;
1533 print "Upgrading svn => git mapping...\n";
1a82e793 1534 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
42d32870
EW
1535 open my $fh, '>>',$REVDB or croak $!;
1536 close $fh;
1537 rebuild();
1538 print "Done upgrading. You may now delete the ",
1539 "deprecated $GIT_SVN_DIR/revs directory\n";
1540 exit 0;
1541 }
1542 waitpid $pid, 0;
1543 croak $? if $?;
1544 });
1545}
1546
883d0a78 1547sub migration_check {
42d32870 1548 migrate_revdb() unless (-e $REVDB);
883d0a78
EW
1549 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
1550 print "Upgrading repository...\n";
1551 unless (-d "$GIT_DIR/svn") {
1552 mkdir "$GIT_DIR/svn" or croak $!;
1553 }
1554 print "Data from a previous version of git-svn exists, but\n\t",
1555 "$GIT_SVN_DIR\n\t(required for this version ",
1556 "($VERSION) of git-svn) does not.\n";
1557
aef4e921 1558 foreach my $x (command(qw/rev-parse --symbolic --all/)) {
883d0a78
EW
1559 next unless $x =~ s#^refs/remotes/##;
1560 chomp $x;
1561 next unless -f "$GIT_DIR/$x/info/url";
1562 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
1563 next unless $u;
1564 my $dn = dirname("$GIT_DIR/svn/$x");
1565 mkpath([$dn]) unless -d $dn;
1566 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
883d0a78 1567 }
42d32870 1568 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
883d0a78
EW
1569 print "Done upgrading.\n";
1570}
1571
9d55b41a 1572sub find_rev_before {
42d32870
EW
1573 my ($r, $id, $eq_ok) = @_;
1574 my $f = "$GIT_DIR/svn/$id/.rev_db";
cf7424b0
EW
1575 return (undef,undef) unless -r $f;
1576 --$r unless $eq_ok;
42d32870
EW
1577 while ($r > 0) {
1578 if (my $c = revdb_get($f, $r)) {
1579 return ($r, $c);
1580 }
1581 --$r;
9d55b41a
EW
1582 }
1583 return (undef, undef);
1584}
1585
b8c92cad
EW
1586sub init_vars {
1587 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1588 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
42d32870 1589 $REVDB = "$GIT_SVN_DIR/.rev_db";
b8c92cad
EW
1590 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1591 $SVN_URL = undef;
b8c92cad 1592 $SVN_WC = "$GIT_SVN_DIR/tree";
c1927a85 1593 %tree_map = ();
b8c92cad
EW
1594}
1595
e0d10e1c 1596# convert GetOpt::Long specs for use by git-config
b8c92cad
EW
1597sub read_repo_config {
1598 return unless -d $GIT_DIR;
1599 my $opts = shift;
1600 foreach my $o (keys %$opts) {
1601 my $v = $opts->{$o};
1602 my ($key) = ($o =~ /^([a-z\-]+)/);
1603 $key =~ s/-//g;
e0d10e1c 1604 my $arg = 'git-config';
b8c92cad
EW
1605 $arg .= ' --int' if ($o =~ /[:=]i$/);
1606 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1607 if (ref $v eq 'ARRAY') {
1608 chomp(my @tmp = `$arg --get-all svn.$key`);
1609 @$v = @tmp if @tmp;
1610 } else {
1611 chomp(my $tmp = `$arg --get svn.$key`);
7774284a 1612 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
b8c92cad
EW
1613 $$v = $tmp;
1614 }
1615 }
1616 }
1617}
1618
dc5869c0
EW
1619sub set_default_vals {
1620 if (defined $_repack) {
1621 $_repack = 1000 if ($_repack <= 0);
1622 $_repack_nr = $_repack;
cf7424b0 1623 $_repack_flags ||= '-d';
dc5869c0
EW
1624 }
1625}
1626
9d55b41a
EW
1627sub read_grafts {
1628 my $gr_file = shift;
1629 my ($grafts, $comments) = ({}, {});
1630 if (open my $fh, '<', $gr_file) {
1631 my @tmp;
1632 while (<$fh>) {
1633 if (/^($sha1)\s+/) {
1634 my $c = $1;
1635 if (@tmp) {
1636 @{$comments->{$c}} = @tmp;
1637 @tmp = ();
1638 }
1639 foreach my $p (split /\s+/, $_) {
1640 $grafts->{$c}->{$p} = 1;
1641 }
1642 } else {
1643 push @tmp, $_;
1644 }
1645 }
1646 close $fh or croak $!;
1647 @{$comments->{'END'}} = @tmp if @tmp;
1648 }
1649 return ($grafts, $comments);
1650}
1651
1652sub write_grafts {
1653 my ($grafts, $comments, $gr_file) = @_;
1654
1655 open my $fh, '>', $gr_file or croak $!;
1656 foreach my $c (sort keys %$grafts) {
1657 if ($comments->{$c}) {
1658 print $fh $_ foreach @{$comments->{$c}};
1659 }
1660 my $p = $grafts->{$c};
c1927a85 1661 my %x; # real parents
9d55b41a 1662 delete $p->{$c}; # commits are not self-reproducing...
aef4e921 1663 my $ch = command_output_pipe(qw/cat-file commit/, $c);
9d55b41a 1664 while (<$ch>) {
c1927a85
EW
1665 if (/^parent ($sha1)/) {
1666 $x{$1} = $p->{$1} = 1;
9d55b41a 1667 } else {
c1927a85 1668 last unless /^\S/;
9d55b41a
EW
1669 }
1670 }
22600a25 1671 close $ch; # breaking the pipe
c1927a85
EW
1672
1673 # if real parents are the only ones in the grafts, drop it
1674 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1675
1676 my (@ip, @jp, $mb);
1677 my %del = %x;
1678 @ip = @jp = keys %$p;
1679 foreach my $i (@ip) {
1680 next if $del{$i} || $p->{$i} == 2;
1681 foreach my $j (@jp) {
1682 next if $i eq $j || $del{$j} || $p->{$j} == 2;
aef4e921 1683 $mb = eval { command('merge-base', $i, $j) };
c1927a85
EW
1684 next unless $mb;
1685 chomp $mb;
1686 next if $x{$mb};
1687 if ($mb eq $j) {
1688 delete $p->{$i};
1689 $del{$i} = 1;
1690 } elsif ($mb eq $i) {
1691 delete $p->{$j};
1692 $del{$j} = 1;
1693 }
1694 }
1695 }
1696
1697 # if real parents are the only ones in the grafts, drop it
1698 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
1699
9d55b41a
EW
1700 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1701 }
1702 if ($comments->{'END'}) {
1703 print $fh $_ foreach @{$comments->{'END'}};
1704 }
1705 close $fh or croak $!;
1706}
1707
a00439ac
EW
1708sub read_url_paths_all {
1709 my ($l_map, $pfx, $p) = @_;
1710 my @dir;
1711 foreach (<$p/*>) {
1712 if (-r "$_/info/url") {
1713 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
1714 my $id = $pfx . basename $_;
1715 my $url = file_to_s("$_/info/url");
1716 my ($u, $p) = repo_path_split($url);
1717 $l_map->{$u}->{$p} = $id;
1718 } elsif (-d $_) {
1719 push @dir, $_;
1720 }
1721 }
1722 foreach (@dir) {
1723 my $x = $_;
1724 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
1725 read_url_paths_all($l_map, $x, $_);
1726 }
1727}
1728
1729# this one only gets ids that have been imported, not new ones
9d55b41a
EW
1730sub read_url_paths {
1731 my $l_map = {};
1732 git_svn_each(sub { my $x = shift;
6c5cda89
EW
1733 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
1734 my ($u, $p) = repo_path_split($url);
9d55b41a
EW
1735 $l_map->{$u}->{$p} = $x;
1736 });
1737 return $l_map;
1738}
1739
79bb8d88 1740sub extract_metadata {
c1927a85 1741 my $id = shift or return (undef, undef, undef);
79bb8d88
EW
1742 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
1743 \s([a-f\d\-]+)$/x);
e70dc780 1744 if (!defined $rev || !$uuid || !$url) {
79bb8d88 1745 # some of the original repositories I made had
82e5a82f 1746 # identifiers like this:
79bb8d88
EW
1747 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1748 }
1749 return ($url, $rev, $uuid);
1750}
1751
c1927a85
EW
1752sub cmt_metadata {
1753 return extract_metadata((grep(/^git-svn-id: /,
aef4e921 1754 command(qw/cat-file commit/, shift)))[-1]);
c1927a85
EW
1755}
1756
1757sub get_commit_time {
1758 my $cmt = shift;
aef4e921 1759 my $fh = command_output_pipe(qw/rev-list --pretty=raw -n1/, $cmt);
c1927a85
EW
1760 while (<$fh>) {
1761 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
1762 my ($s, $tz) = ($1, $2);
1763 if ($tz =~ s/^\+//) {
1764 $s += tz_to_s_offset($tz);
1765 } elsif ($tz =~ s/^\-//) {
1766 $s -= tz_to_s_offset($tz);
1767 }
22600a25 1768 close $fh;
c1927a85
EW
1769 return $s;
1770 }
1771 die "Can't get commit time for commit: $cmt\n";
1772}
1773
79bb8d88
EW
1774sub tz_to_s_offset {
1775 my ($tz) = @_;
1776 $tz =~ s/(\d\d)$//;
1777 return ($1 * 60) + ($tz * 3600);
1778}
1779
9aca0258
EW
1780# adapted from pager.c
1781sub config_pager {
1782 $_pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
1783 if (!defined $_pager) {
1784 $_pager = 'less';
1785 } elsif (length $_pager == 0 || $_pager eq 'cat') {
1786 $_pager = undef;
79bb8d88 1787 }
9aca0258
EW
1788}
1789
1790sub run_pager {
1791 return unless -t *STDOUT;
79bb8d88
EW
1792 pipe my $rfd, my $wfd or return;
1793 defined(my $pid = fork) or croak $!;
1794 if (!$pid) {
1795 open STDOUT, '>&', $wfd or croak $!;
1796 return;
1797 }
1798 open STDIN, '<&', $rfd or croak $!;
9aca0258
EW
1799 $ENV{LESS} ||= 'FRSX';
1800 exec $_pager or croak "Can't run pager: $! ($_pager)\n";
79bb8d88
EW
1801}
1802
1803sub get_author_info {
1804 my ($dest, $author, $t, $tz) = @_;
1805 $author =~ s/(?:^\s*|\s*$)//g;
c0d48222 1806 $dest->{a_raw} = $author;
79bb8d88
EW
1807 my $_a;
1808 if ($_authors) {
1809 $_a = $rusers{$author} || undef;
1810 }
1811 if (!$_a) {
1812 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1813 }
1814 $dest->{t} = $t;
1815 $dest->{tz} = $tz;
1816 $dest->{a} = $_a;
1817 # Date::Parse isn't in the standard Perl distro :(
1818 if ($tz =~ s/^\+//) {
1819 $t += tz_to_s_offset($tz);
1820 } elsif ($tz =~ s/^\-//) {
1821 $t -= tz_to_s_offset($tz);
1822 }
1823 $dest->{t_utc} = $t;
1824}
1825
1826sub process_commit {
1827 my ($c, $r_min, $r_max, $defer) = @_;
1828 if (defined $r_min && defined $r_max) {
1829 if ($r_min == $c->{r} && $r_min == $r_max) {
1830 show_commit($c);
1831 return 0;
1832 }
1833 return 1 if $r_min == $r_max;
1834 if ($r_min < $r_max) {
1835 # we need to reverse the print order
1836 return 0 if (defined $_limit && --$_limit < 0);
1837 push @$defer, $c;
1838 return 1;
1839 }
1840 if ($r_min != $r_max) {
1841 return 1 if ($r_min < $c->{r});
1842 return 1 if ($r_max > $c->{r});
1843 }
1844 }
1845 return 0 if (defined $_limit && --$_limit < 0);
1846 show_commit($c);
1847 return 1;
1848}
1849
1850sub show_commit {
1851 my $c = shift;
1852 if ($_oneline) {
1853 my $x = "\n";
1854 if (my $l = $c->{l}) {
1855 while ($l->[0] =~ /^\s*$/) { shift @$l }
1856 $x = $l->[0];
1857 }
1858 $_l_fmt ||= 'A' . length($c->{r});
1859 print 'r',pack($_l_fmt, $c->{r}),' | ';
1860 print "$c->{c} | " if $_show_commit;
1861 print $x;
1862 } else {
1863 show_commit_normal($c);
1864 }
1865}
1866
74a31a10
EW
1867sub show_commit_changed_paths {
1868 my ($c) = @_;
1869 return unless $c->{changed};
1870 print "Changed paths:\n", @{$c->{changed}};
1871}
1872
79bb8d88
EW
1873sub show_commit_normal {
1874 my ($c) = @_;
1875 print '-' x72, "\nr$c->{r} | ";
1876 print "$c->{c} | " if $_show_commit;
1877 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
1878 localtime($c->{t_utc})), ' | ';
1879 my $nr_line = 0;
1880
1881 if (my $l = $c->{l}) {
74a31a10
EW
1882 while ($l->[$#$l] eq "\n" && $#$l > 0
1883 && $l->[($#$l - 1)] eq "\n") {
79bb8d88
EW
1884 pop @$l;
1885 }
1886 $nr_line = scalar @$l;
1887 if (!$nr_line) {
1888 print "1 line\n\n\n";
1889 } else {
1890 if ($nr_line == 1) {
1891 $nr_line = '1 line';
1892 } else {
1893 $nr_line .= ' lines';
1894 }
74a31a10
EW
1895 print $nr_line, "\n";
1896 show_commit_changed_paths($c);
1897 print "\n";
79bb8d88
EW
1898 print $_ foreach @$l;
1899 }
1900 } else {
74a31a10
EW
1901 print "1 line\n";
1902 show_commit_changed_paths($c);
1903 print "\n";
79bb8d88
EW
1904
1905 }
1906 foreach my $x (qw/raw diff/) {
1907 if ($c->{$x}) {
1908 print "\n";
1909 print $_ foreach @{$c->{$x}}
1910 }
1911 }
1912}
1913
d976acfd
EW
1914package Git::SVN::Prompt;
1915use strict;
1916use warnings;
1917require SVN::Core;
1918use vars qw/$_no_auth_cache $_username/;
1919
1920sub simple {
30d055aa
EW
1921 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1922 $may_save = undef if $_no_auth_cache;
1923 $default_username = $_username if defined $_username;
1924 if (defined $default_username && length $default_username) {
1925 if (defined $realm && length $realm) {
6f729591
EW
1926 print STDERR "Authentication realm: $realm\n";
1927 STDERR->flush;
30d055aa
EW
1928 }
1929 $cred->username($default_username);
1930 } else {
d976acfd 1931 username($cred, $realm, $may_save, $pool);
30d055aa
EW
1932 }
1933 $cred->password(_read_password("Password for '" .
1934 $cred->username . "': ", $realm));
1935 $cred->may_save($may_save);
1936 $SVN::_Core::SVN_NO_ERROR;
1937}
1938
d976acfd 1939sub ssl_server_trust {
30d055aa
EW
1940 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1941 $may_save = undef if $_no_auth_cache;
6f729591 1942 print STDERR "Error validating server certificate for '$realm':\n";
30d055aa 1943 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
6f729591 1944 print STDERR " - The certificate is not issued by a trusted ",
30d055aa
EW
1945 "authority. Use the\n",
1946 " fingerprint to validate the certificate manually!\n";
1947 }
1948 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
6f729591 1949 print STDERR " - The certificate hostname does not match.\n";
30d055aa
EW
1950 }
1951 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
6f729591 1952 print STDERR " - The certificate is not yet valid.\n";
30d055aa
EW
1953 }
1954 if ($failures & $SVN::Auth::SSL::EXPIRED) {
6f729591 1955 print STDERR " - The certificate has expired.\n";
30d055aa
EW
1956 }
1957 if ($failures & $SVN::Auth::SSL::OTHER) {
6f729591 1958 print STDERR " - The certificate has an unknown error.\n";
30d055aa 1959 }
6f729591
EW
1960 printf STDERR
1961 "Certificate information:\n".
30d055aa
EW
1962 " - Hostname: %s\n".
1963 " - Valid: from %s until %s\n".
1964 " - Issuer: %s\n".
1965 " - Fingerprint: %s\n",
1966 map $cert_info->$_, qw(hostname valid_from valid_until
6f729591 1967 issuer_dname fingerprint);
30d055aa
EW
1968 my $choice;
1969prompt:
6f729591 1970 print STDERR $may_save ?
30d055aa
EW
1971 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1972 "(R)eject or accept (t)emporarily? ";
6f729591 1973 STDERR->flush;
30d055aa
EW
1974 $choice = lc(substr(<STDIN> || 'R', 0, 1));
1975 if ($choice =~ /^t$/i) {
1976 $cred->may_save(undef);
1977 } elsif ($choice =~ /^r$/i) {
1978 return -1;
1979 } elsif ($may_save && $choice =~ /^p$/i) {
1980 $cred->may_save($may_save);
1981 } else {
1982 goto prompt;
1983 }
1984 $cred->accepted_failures($failures);
1985 $SVN::_Core::SVN_NO_ERROR;
1986}
1987
d976acfd 1988sub ssl_client_cert {
30d055aa
EW
1989 my ($cred, $realm, $may_save, $pool) = @_;
1990 $may_save = undef if $_no_auth_cache;
6f729591
EW
1991 print STDERR "Client certificate filename: ";
1992 STDERR->flush;
30d055aa
EW
1993 chomp(my $filename = <STDIN>);
1994 $cred->cert_file($filename);
1995 $cred->may_save($may_save);
1996 $SVN::_Core::SVN_NO_ERROR;
1997}
1998
d976acfd 1999sub ssl_client_cert_pw {
30d055aa
EW
2000 my ($cred, $realm, $may_save, $pool) = @_;
2001 $may_save = undef if $_no_auth_cache;
2002 $cred->password(_read_password("Password: ", $realm));
2003 $cred->may_save($may_save);
2004 $SVN::_Core::SVN_NO_ERROR;
2005}
2006
d976acfd 2007sub username {
30d055aa
EW
2008 my ($cred, $realm, $may_save, $pool) = @_;
2009 $may_save = undef if $_no_auth_cache;
2010 if (defined $realm && length $realm) {
6f729591 2011 print STDERR "Authentication realm: $realm\n";
30d055aa
EW
2012 }
2013 my $username;
2014 if (defined $_username) {
2015 $username = $_username;
2016 } else {
6f729591
EW
2017 print STDERR "Username: ";
2018 STDERR->flush;
30d055aa
EW
2019 chomp($username = <STDIN>);
2020 }
2021 $cred->username($username);
2022 $cred->may_save($may_save);
2023 $SVN::_Core::SVN_NO_ERROR;
2024}
2025
2026sub _read_password {
2027 my ($prompt, $realm) = @_;
6f729591
EW
2028 print STDERR $prompt;
2029 STDERR->flush;
30d055aa
EW
2030 require Term::ReadKey;
2031 Term::ReadKey::ReadMode('noecho');
2032 my $password = '';
2033 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2034 last if $key =~ /[\012\015]/; # \n\r
2035 $password .= $key;
2036 }
2037 Term::ReadKey::ReadMode('restore');
6f729591
EW
2038 print STDERR "\n";
2039 STDERR->flush;
30d055aa
EW
2040 $password;
2041}
2042
d976acfd
EW
2043package main;
2044
a5e0cedc
EW
2045sub libsvn_connect {
2046 my ($url) = @_;
747fa12c
EW
2047 SVN::_Core::svn_config_ensure($_config_dir, undef);
2048 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
2049 SVN::Client::get_simple_provider(),
2050 SVN::Client::get_ssl_server_trust_file_provider(),
2051 SVN::Client::get_simple_prompt_provider(
d976acfd 2052 \&Git::SVN::Prompt::simple, 2),
747fa12c 2053 SVN::Client::get_ssl_client_cert_prompt_provider(
d976acfd 2054 \&Git::SVN::Prompt::ssl_client_cert, 2),
747fa12c 2055 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
d976acfd 2056 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
747fa12c
EW
2057 SVN::Client::get_username_provider(),
2058 SVN::Client::get_ssl_server_trust_prompt_provider(
d976acfd 2059 \&Git::SVN::Prompt::ssl_server_trust),
747fa12c 2060 SVN::Client::get_username_prompt_provider(
d976acfd 2061 \&Git::SVN::username, 2),
747fa12c 2062 ]);
6f23ebf6 2063 my $config = SVN::Core::config_get_config($_config_dir);
747fa12c 2064 my $ra = SVN::Ra->new(url => $url, auth => $baton,
6f23ebf6 2065 config => $config,
747fa12c
EW
2066 pool => SVN::Pool->new,
2067 auth_provider_callbacks => $callbacks);
2068 $ra->{svn_path} = $url;
2069 $ra->{repos_root} = $ra->get_repos_root;
2070 $ra->{svn_path} =~ s#^\Q$ra->{repos_root}\E/*##;
2071 push @repo_path_split_cache, qr/^(\Q$ra->{repos_root}\E)/;
2072 return $ra;
2073}
2074
a552db3a
EW
2075sub libsvn_can_do_switch {
2076 unless (defined $_svn_can_do_switch) {
2077 my $pool = SVN::Pool->new;
2078 my $rep = eval {
2079 $SVN->do_switch(1, '', 0, $SVN->{url},
2080 SVN::Delta::Editor->new, $pool);
2081 };
2082 if ($@) {
2083 $_svn_can_do_switch = 0;
2084 } else {
2085 $rep->abort_report($pool);
2086 $_svn_can_do_switch = 1;
2087 }
2088 $pool->clear;
2089 }
2090 $_svn_can_do_switch;
2091}
2092
747fa12c
EW
2093sub libsvn_dup_ra {
2094 my ($ra) = @_;
6f23ebf6
EW
2095 SVN::Ra->new(map { $_ => $ra->{$_} } qw/config url
2096 auth auth_provider_callbacks repos_root svn_path/);
a5e0cedc
EW
2097}
2098
d2a9a87b
EW
2099sub uri_encode {
2100 my ($f) = @_;
2101 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2102 $f
2103}
2104
2105sub uri_decode {
2106 my ($f) = @_;
2107 $f =~ tr/+/ /;
2108 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
2109 $f
2110}
2111
a5e0cedc 2112sub libsvn_log_entry {
d2a9a87b 2113 my ($rev, $author, $date, $msg, $parents, $untracked) = @_;
a5e0cedc
EW
2114 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2115 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2116 or die "Unable to parse date: $date\n";
35985004
EW
2117 if (defined $author && length $author > 0 &&
2118 defined $_authors && ! defined $users{$author}) {
a5e0cedc
EW
2119 die "Author: $author not defined in $_authors file\n";
2120 }
308906fa 2121 $msg = '' if ($rev == 0 && !defined $msg);
d2a9a87b
EW
2122
2123 open my $un, '>>', "$GIT_SVN_DIR/unhandled.log" or croak $!;
2124 my $h;
2125 print $un "r$rev\n" or croak $!;
2126 $h = $untracked->{empty};
2127 foreach (sort keys %$h) {
2128 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
2129 print $un " $act: ", uri_encode($_), "\n" or croak $!;
2130 warn "W: $act: $_\n";
2131 }
2132 foreach my $t (qw/dir_prop file_prop/) {
2133 $h = $untracked->{$t} or next;
2134 foreach my $path (sort keys %$h) {
2135 my $ppath = $path eq '' ? '.' : $path;
2136 foreach my $prop (sort keys %{$h->{$path}}) {
2137 next if $SKIP{$prop};
2138 my $v = $h->{$path}->{$prop};
2139 if (defined $v) {
2140 print $un " +$t: ",
2141 uri_encode($ppath), ' ',
2142 uri_encode($prop), ' ',
2143 uri_encode($v), "\n"
2144 or croak $!;
2145 } else {
2146 print $un " -$t: ",
2147 uri_encode($ppath), ' ',
2148 uri_encode($prop), "\n"
2149 or croak $!;
2150 }
2151 }
2152 }
2153 }
2154 foreach my $t (qw/absent_file absent_directory/) {
2155 $h = $untracked->{$t} or next;
2156 foreach my $parent (sort keys %$h) {
2157 foreach my $path (sort @{$h->{$parent}}) {
2158 print $un " $t: ",
2159 uri_encode("$parent/$path"), "\n"
2160 or croak $!;
2161 warn "W: $t: $parent/$path ",
2162 "Insufficient permissions?\n";
2163 }
2164 }
2165 }
2166
2167 # revprops (make this optional? it's an extra network trip...)
2168 my $pool = SVN::Pool->new;
2169 my $rp = $SVN->rev_proplist($rev, $pool);
2170 foreach (sort keys %$rp) {
2171 next if /^svn:(?:author|date|log)$/;
2172 print $un " rev_prop: ", uri_encode($_), ' ',
2173 uri_encode($rp->{$_}), "\n";
2174 }
2175 $pool->clear;
2176 close $un or croak $!;
2177
2178 { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2179 author => $author, msg => $msg."\n", parents => $parents || [],
2180 revprops => $rp }
a5e0cedc
EW
2181}
2182
a5e0cedc 2183sub libsvn_fetch {
27a1a801
EW
2184 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2185 my $pool = SVN::Pool->new;
0864e3ba 2186 my $ed = SVN::Git::Fetcher->new({ c => $last_commit, q => $_q });
27a1a801
EW
2187 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2188 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2189 my (undef, $last_rev, undef) = cmt_metadata($last_commit);
2190 $reporter->set_path('', $last_rev, 0, @lock, $pool);
2191 $reporter->finish_report($pool);
2192 $pool->clear;
dad73c0b
EW
2193 unless ($ed->{git_commit_ok}) {
2194 die "SVN connection failed somewhere...\n";
2195 }
d2a9a87b 2196 libsvn_log_entry($rev, $author, $date, $msg, [$last_commit], $ed);
27a1a801
EW
2197}
2198
a5e0cedc 2199sub svn_grab_base_rev {
aef4e921
EW
2200 my $c = eval { command_oneline([qw/rev-parse --verify/,
2201 "refs/remotes/$GIT_SVN^0"],
2202 { STDERR => 0 }) };
a5e0cedc 2203 if (defined $c && length $c) {
c1927a85 2204 my ($url, $rev, $uuid) = cmt_metadata($c);
a00439ac
EW
2205 return ($rev, $c) if defined $rev;
2206 }
2207 if ($_no_metadata) {
2208 my $offset = -41; # from tail
2209 my $rl;
2210 open my $fh, '<', $REVDB or
2211 die "--no-metadata specified and $REVDB not readable\n";
2212 seek $fh, $offset, 2;
2213 $rl = readline $fh;
2214 defined $rl or return (undef, undef);
2215 chomp $rl;
2216 while ($c ne $rl && tell $fh != 0) {
2217 $offset -= 41;
2218 seek $fh, $offset, 2;
2219 $rl = readline $fh;
2220 defined $rl or return (undef, undef);
2221 chomp $rl;
2222 }
2223 my $rev = tell $fh;
2224 croak $! if ($rev < -1);
2225 $rev = ($rev - 41) / 41;
2226 close $fh or croak $!;
a5e0cedc
EW
2227 return ($rev, $c);
2228 }
2229 return (undef, undef);
2230}
2231
2232sub libsvn_parse_revision {
2233 my $base = shift;
2234 my $head = $SVN->get_latest_revnum();
2235 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2236 return ($base + 1, $head) if (defined $base);
2237 return (0, $head);
2238 }
2239 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2240 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2241 if ($_revision =~ /^BASE:(\d+)$/) {
2242 return ($base + 1, $1) if (defined $base);
2243 return (0, $head);
2244 }
2245 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2246 die "revision argument: $_revision not understood by git-svn\n",
2247 "Try using the command-line svn client instead\n";
2248}
2249
a5e0cedc
EW
2250sub libsvn_traverse_ignore {
2251 my ($fh, $path, $r) = @_;
2252 $path =~ s#^/+##g;
2253 my $pool = SVN::Pool->new;
2254 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2255 my $p = $path;
747fa12c 2256 $p =~ s#^\Q$SVN->{svn_path}\E/##;
a5e0cedc
EW
2257 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2258 if (my $s = $props->{'svn:ignore'}) {
2259 $s =~ s/[\r\n]+/\n/g;
2260 chomp $s;
2261 if (length $p == 0) {
2262 $s =~ s#\n#\n/$p#g;
2263 print $fh "/$s\n";
2264 } else {
2265 $s =~ s#\n#\n/$p/#g;
2266 print $fh "/$p/$s\n";
2267 }
2268 }
2269 foreach (sort keys %$dirent) {
2270 next if $dirent->{$_}->kind != $SVN::Node::dir;
2271 libsvn_traverse_ignore($fh, "$path/$_", $r);
2272 }
2273 $pool->clear;
2274}
2275
42d32870
EW
2276sub revisions_eq {
2277 my ($path, $r0, $r1) = @_;
2278 return 1 if $r0 == $r1;
2279 my $nr = 0;
b9c85187
EW
2280 # should be OK to use Pool here (r1 - r0) should be small
2281 my $pool = SVN::Pool->new;
2282 libsvn_get_log($SVN, [$path], $r0, $r1,
2283 0, 0, 1, sub {$nr++}, $pool);
2284 $pool->clear;
42d32870
EW
2285 return 0 if ($nr > 1);
2286 return 1;
2287}
2288
2289sub libsvn_find_parent_branch {
a5e0cedc 2290 my ($paths, $rev, $author, $date, $msg) = @_;
747fa12c 2291 my $svn_path = '/'.$SVN->{svn_path};
a5e0cedc
EW
2292
2293 # look for a parent from another branch:
cf7424b0
EW
2294 my $i = $paths->{$svn_path} or return;
2295 my $branch_from = $i->copyfrom_path or return;
2296 my $r = $i->copyfrom_rev;
2297 print STDERR "Found possible branch point: ",
2298 "$branch_from => $svn_path, $r\n";
2299 $branch_from =~ s#^/##;
a00439ac
EW
2300 my $l_map = {};
2301 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
747fa12c 2302 my $url = $SVN->{repos_root};
cf7424b0 2303 defined $l_map->{$url} or return;
a00439ac
EW
2304 my $id = $l_map->{$url}->{$branch_from};
2305 if (!defined $id && $_follow_parent) {
2306 print STDERR "Following parent: $branch_from\@$r\n";
2307 # auto create a new branch and follow it
2308 $id = basename($branch_from);
2309 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2310 while (-r "$GIT_DIR/svn/$id") {
2311 # just grow a tail if we're not unique enough :x
2312 $id .= '-';
2313 }
2314 }
2315 return unless defined $id;
2316
cf7424b0 2317 my ($r0, $parent) = find_rev_before($r,$id,1);
a00439ac
EW
2318 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2319 defined(my $pid = fork) or croak $!;
2320 if (!$pid) {
2321 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2322 init_vars();
2323 $SVN_URL = "$url/$branch_from";
747fa12c 2324 $SVN = undef;
a00439ac
EW
2325 setup_git_svn();
2326 # we can't assume SVN_URL exists at r+1:
2327 $_revision = "0:$r";
2328 fetch_lib();
2329 exit 0;
2330 }
2331 waitpid $pid, 0;
2332 croak $? if $?;
2333 ($r0, $parent) = find_rev_before($r,$id,1);
2334 }
cf7424b0
EW
2335 return unless (defined $r0 && defined $parent);
2336 if (revisions_eq($branch_from, $r0, $r)) {
2337 unlink $GIT_SVN_INDEX;
a00439ac 2338 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
aef4e921 2339 command_noisy('read-tree', $parent);
a552db3a 2340 unless (libsvn_can_do_switch()) {
ed92f170
EW
2341 return _libsvn_new_tree($paths, $rev, $author, $date,
2342 $msg, [$parent]);
a552db3a
EW
2343 }
2344 # do_switch works with svn/trunk >= r22312, but that is not
2345 # included with SVN 1.4.2 (the latest version at the moment),
2346 # so we can't rely on it.
2347 my $ra = libsvn_connect("$url/$branch_from");
aef4e921 2348 my $ed = SVN::Git::Fetcher->new({c => $parent, q => $_q });
a552db3a
EW
2349 my $pool = SVN::Pool->new;
2350 my $reporter = $ra->do_switch($rev, '', 1, $SVN->{url},
2351 $ed, $pool);
2352 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2353 $reporter->set_path('', $r0, 0, @lock, $pool);
2354 $reporter->finish_report($pool);
2355 $pool->clear;
2356 unless ($ed->{git_commit_ok}) {
2357 die "SVN connection failed somewhere...\n";
2358 }
2359 return libsvn_log_entry($rev, $author, $date, $msg, [$parent]);
cf7424b0
EW
2360 }
2361 print STDERR "Nope, branch point not imported or unknown\n";
42d32870
EW
2362 return undef;
2363}
2364
dc62e25c
EW
2365sub libsvn_get_log {
2366 my ($ra, @args) = @_;
ed92f170 2367 $args[4]-- if $args[4] && ! $_follow_parent;
dc62e25c
EW
2368 if ($SVN::Core::VERSION le '1.2.0') {
2369 splice(@args, 3, 1);
2370 }
2371 $ra->get_log(@args);
2372}
2373
42d32870
EW
2374sub libsvn_new_tree {
2375 if (my $log_entry = libsvn_find_parent_branch(@_)) {
2376 return $log_entry;
2377 }
ed92f170
EW
2378 my ($paths, $rev, $author, $date, $msg) = @_; # $pool is last
2379 _libsvn_new_tree($paths, $rev, $author, $date, $msg, []);
2380}
2381
2382sub _libsvn_new_tree {
2383 my ($paths, $rev, $author, $date, $msg, $parents) = @_;
2384 my $pool = SVN::Pool->new;
2385 my $ed = SVN::Git::Fetcher->new({q => $_q});
2386 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2387 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2388 $reporter->set_path('', $rev, 1, @lock, $pool);
2389 $reporter->finish_report($pool);
2390 $pool->clear;
2391 unless ($ed->{git_commit_ok}) {
2392 die "SVN connection failed somewhere...\n";
27a1a801 2393 }
ed92f170 2394 libsvn_log_entry($rev, $author, $date, $msg, $parents, $ed);
a5e0cedc
EW
2395}
2396
2397sub find_graft_path_commit {
2398 my ($tree_paths, $p1, $r1) = @_;
2399 foreach my $x (keys %$tree_paths) {
2400 next unless ($p1 =~ /^\Q$x\E/);
2401 my $i = $tree_paths->{$x};
42d32870
EW
2402 my ($r0, $parent) = find_rev_before($r1,$i,1);
2403 return $parent if (defined $r0 && $r0 == $r1);
a5e0cedc
EW
2404 print STDERR "r$r1 of $i not imported\n";
2405 next;
2406 }
2407 return undef;
2408}
2409
2410sub find_graft_path_parents {
2411 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2412 foreach my $x (keys %$tree_paths) {
2413 next unless ($p0 =~ /^\Q$x\E/);
2414 my $i = $tree_paths->{$x};
42d32870
EW
2415 my ($r, $parent) = find_rev_before($r0, $i, 1);
2416 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
c1927a85
EW
2417 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
2418 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
2419 next if ($url_a && $url_b && $url_a eq $url_b &&
2420 $uuid_b eq $uuid_a);
42d32870 2421 $grafts->{$c}->{$parent} = 1;
a5e0cedc 2422 }
a5e0cedc
EW
2423 }
2424}
2425
2426sub libsvn_graft_file_copies {
2427 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2428 foreach (keys %$paths) {
2429 my $i = $paths->{$_};
2430 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2431 $i->copyfrom_rev);
2432 next unless (defined $p0 && defined $r0);
2433
2434 my $p1 = $_;
2435 $p1 =~ s#^/##;
2436 $p0 =~ s#^/##;
2437 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
2438 next unless $c;
2439 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
2440 }
2441}
2442
2443sub set_index {
2444 my $old = $ENV{GIT_INDEX_FILE};
2445 $ENV{GIT_INDEX_FILE} = shift;
2446 return $old;
2447}
2448
2449sub restore_index {
2450 my ($old) = @_;
2451 if (defined $old) {
2452 $ENV{GIT_INDEX_FILE} = $old;
2453 } else {
2454 delete $ENV{GIT_INDEX_FILE};
2455 }
2456}
2457
2458sub libsvn_commit_cb {
2459 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
42d32870 2460 if ($_optimize_commits && $rev == ($r_last + 1)) {
a5e0cedc
EW
2461 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
2462 $log->{tree} = get_tree_from_treeish($c);
2463 my $cmt = git_commit($log, $cmt_last, $c);
aef4e921 2464 my @diff = command('diff-tree', $cmt, $c);
a5e0cedc
EW
2465 if (@diff) {
2466 print STDERR "Trees differ: $cmt $c\n",
2467 join('',@diff),"\n";
2468 exit 1;
2469 }
2470 } else {
cf7424b0 2471 fetch("$rev=$c");
a5e0cedc
EW
2472 }
2473}
2474
2475sub libsvn_ls_fullurl {
2476 my $fullurl = shift;
1ca7558d 2477 my $ra = libsvn_connect($fullurl);
a5e0cedc
EW
2478 my @ret;
2479 my $pool = SVN::Pool->new;
1ca7558d
EW
2480 my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
2481 my ($dirent, undef, undef) = $ra->get_dir('', $r, $pool);
4a4d94b2 2482 foreach my $d (sort keys %$dirent) {
a5e0cedc
EW
2483 if ($dirent->{$d}->kind == $SVN::Node::dir) {
2484 push @ret, "$d/"; # add '/' for compat with cli svn
2485 }
2486 }
2487 $pool->clear;
2488 return @ret;
2489}
2490
2491
2492sub libsvn_skip_unknown_revs {
2493 my $err = shift;
2494 my $errno = $err->apr_err();
2495 # Maybe the branch we're tracking didn't
2496 # exist when the repo started, so it's
2497 # not an error if it doesn't, just continue
2498 #
2499 # Wonderfully consistent library, eh?
2500 # 160013 - svn:// and file://
2501 # 175002 - http(s)://
747fa12c 2502 # 175007 - http(s):// (this repo required authorization, too...)
a5e0cedc 2503 # More codes may be discovered later...
747fa12c 2504 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
a5e0cedc
EW
2505 return;
2506 }
2507 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2508};
2509
42d32870
EW
2510# Tie::File seems to be prone to offset errors if revisions get sparse,
2511# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2512# one of my favorite modules is out :< Next up would be one of the DBM
2513# modules, but I'm not sure which is most portable... So I'll just
2514# go with something that's plain-text, but still capable of
2515# being randomly accessed. So here's my ultra-simple fixed-width
2516# database. All records are 40 characters + "\n", so it's easy to seek
2517# to a revision: (41 * rev) is the byte offset.
2518# A record of 40 0s denotes an empty revision.
2519# And yes, it's still pretty fast (faster than Tie::File).
2520sub revdb_set {
2521 my ($file, $rev, $commit) = @_;
2522 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
2523 open my $fh, '+<', $file or croak $!;
2524 my $offset = $rev * 41;
2525 # assume that append is the common case:
2526 seek $fh, 0, 2 or croak $!;
2527 my $pos = tell $fh;
2528 if ($pos < $offset) {
2529 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
2530 }
2531 seek $fh, $offset, 0 or croak $!;
2532 print $fh $commit,"\n";
2533 close $fh or croak $!;
2534}
2535
2536sub revdb_get {
2537 my ($file, $rev) = @_;
2538 my $ret;
2539 my $offset = $rev * 41;
2540 open my $fh, '<', $file or croak $!;
2541 seek $fh, $offset, 0;
2542 if (tell $fh == $offset) {
2543 $ret = readline $fh;
2544 if (defined $ret) {
2545 chomp $ret;
2546 $ret = undef if ($ret =~ /^0{40}$/);
2547 }
2548 }
2549 close $fh or croak $!;
2550 return $ret;
2551}
2552
1a82e793
EW
2553sub copy_remote_ref {
2554 my $origin = $_cp_remote ? $_cp_remote : 'origin';
2555 my $ref = "refs/remotes/$GIT_SVN";
aef4e921
EW
2556 if (command('ls-remote', $origin, $ref)) {
2557 command_noisy('fetch', $origin, "$ref:$ref");
a35a0458 2558 } elsif ($_cp_remote && !$_upgrade) {
1a82e793
EW
2559 die "Unable to find remote reference: ",
2560 "refs/remotes/$GIT_SVN on $origin\n";
2561 }
2562}
b9c85187
EW
2563
2564{
2565 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2566 $SVN::Node::dir.$SVN::Node::unknown.
2567 $SVN::Node::none.$SVN::Node::file.
2568 $SVN::Node::dir.$SVN::Node::unknown.
2569 $SVN::Auth::SSL::CNMISMATCH.
2570 $SVN::Auth::SSL::NOTYETVALID.
2571 $SVN::Auth::SSL::EXPIRED.
2572 $SVN::Auth::SSL::UNKNOWNCA.
2573 $SVN::Auth::SSL::OTHER;
2574}
2575
27a1a801
EW
2576package SVN::Git::Fetcher;
2577use vars qw/@ISA/;
2578use strict;
2579use warnings;
2580use Carp qw/croak/;
2581use IO::File qw//;
aef4e921
EW
2582use Git qw/command command_oneline command_noisy
2583 command_output_pipe command_input_pipe command_close_pipe/;
27a1a801
EW
2584
2585# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2586sub new {
2587 my ($class, $git_svn) = @_;
2588 my $self = SVN::Delta::Editor->new;
2589 bless $self, $class;
27a1a801 2590 $self->{c} = $git_svn->{c} if exists $git_svn->{c};
0864e3ba 2591 $self->{q} = $git_svn->{q};
d2a9a87b
EW
2592 $self->{empty} = {};
2593 $self->{dir_prop} = {};
2594 $self->{file_prop} = {};
2595 $self->{absent_dir} = {};
2596 $self->{absent_file} = {};
aef4e921
EW
2597 ($self->{gui}, $self->{ctx}) = command_input_pipe(
2598 qw/update-index -z --index-info/);
27a1a801
EW
2599 require Digest::MD5;
2600 $self;
2601}
2602
d2a9a87b
EW
2603sub open_root {
2604 { path => '' };
2605}
2606
2607sub open_directory {
2608 my ($self, $path, $pb, $rev) = @_;
2609 { path => $path };
2610}
2611
27a1a801
EW
2612sub delete_entry {
2613 my ($self, $path, $rev, $pb) = @_;
4a87db0e
EW
2614 my $gui = $self->{gui};
2615
2616 # remove entire directories.
2617 if (command('ls-tree', $self->{c}, '--', $path) =~ /^040000 tree/) {
2618 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2619 -r --name-only -z/,
2620 $self->{c}, '--', $path);
2621 local $/ = "\0";
2622 while (<$ls>) {
2623 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2624 print "\tD\t$_\n" unless $self->{q};
2625 }
2626 print "\tD\t$path/\n" unless $self->{q};
2627 command_close_pipe($ls, $ctx);
2628 $self->{empty}->{$path} = 0
2629 } else {
2630 print $gui '0 ',0 x 40,"\t",$path,"\0" or croak $!;
2631 print "\tD\t$path\n" unless $self->{q};
2632 }
27a1a801
EW
2633 undef;
2634}
2635
2636sub open_file {
2637 my ($self, $path, $pb, $rev) = @_;
aef4e921 2638 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--',$path)
27a1a801 2639 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
006ede5e
EW
2640 unless (defined $mode && defined $blob) {
2641 die "$path was not found in commit $self->{c} (r$rev)\n";
2642 }
27a1a801 2643 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
0864e3ba 2644 pool => SVN::Pool->new, action => 'M' };
27a1a801
EW
2645}
2646
2647sub add_file {
2648 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
d2a9a87b
EW
2649 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2650 delete $self->{empty}->{$dir};
27a1a801 2651 { path => $path, mode_a => 100644, mode_b => 100644,
0864e3ba 2652 pool => SVN::Pool->new, action => 'A' };
27a1a801
EW
2653}
2654
d2a9a87b
EW
2655sub add_directory {
2656 my ($self, $path, $cp_path, $cp_rev) = @_;
2657 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2658 delete $self->{empty}->{$dir};
2659 $self->{empty}->{$path} = 1;
2660 { path => $path };
2661}
2662
2663sub change_dir_prop {
2664 my ($self, $db, $prop, $value) = @_;
2665 $self->{dir_prop}->{$db->{path}} ||= {};
2666 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2667 undef;
2668}
2669
2670sub absent_directory {
2671 my ($self, $path, $pb) = @_;
2672 $self->{absent_dir}->{$pb->{path}} ||= [];
2673 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2674 undef;
2675}
2676
2677sub absent_file {
2678 my ($self, $path, $pb) = @_;
2679 $self->{absent_file}->{$pb->{path}} ||= [];
2680 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2681 undef;
2682}
2683
27a1a801
EW
2684sub change_file_prop {
2685 my ($self, $fb, $prop, $value) = @_;
2686 if ($prop eq 'svn:executable') {
2687 if ($fb->{mode_b} != 120000) {
2688 $fb->{mode_b} = defined $value ? 100755 : 100644;
2689 }
2690 } elsif ($prop eq 'svn:special') {
2691 $fb->{mode_b} = defined $value ? 120000 : 100644;
d2a9a87b
EW
2692 } else {
2693 $self->{file_prop}->{$fb->{path}} ||= {};
2694 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
27a1a801
EW
2695 }
2696 undef;
2697}
2698
2699sub apply_textdelta {
2700 my ($self, $fb, $exp) = @_;
2701 my $fh = IO::File->new_tmpfile;
2702 $fh->autoflush(1);
2703 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2704 # (but $base does not,) so dup() it for reading in close_file
2705 open my $dup, '<&', $fh or croak $!;
2706 my $base = IO::File->new_tmpfile;
2707 $base->autoflush(1);
2708 if ($fb->{blob}) {
2709 defined (my $pid = fork) or croak $!;
2710 if (!$pid) {
2711 open STDOUT, '>&', $base or croak $!;
2712 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2713 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2714 }
2715 waitpid $pid, 0;
2716 croak $? if $?;
2717
2718 if (defined $exp) {
2719 seek $base, 0, 0 or croak $!;
2720 my $md5 = Digest::MD5->new;
2721 $md5->addfile($base);
2722 my $got = $md5->hexdigest;
2723 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2724 "expected: $exp\n",
2725 " got: $got\n" if ($got ne $exp);
2726 }
2727 }
2728 seek $base, 0, 0 or croak $!;
2729 $fb->{fh} = $dup;
2730 $fb->{base} = $base;
2731 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2732}
2733
2734sub close_file {
2735 my ($self, $fb, $exp) = @_;
2736 my $hash;
2737 my $path = $fb->{path};
2738 if (my $fh = $fb->{fh}) {
2739 seek($fh, 0, 0) or croak $!;
2740 my $md5 = Digest::MD5->new;
2741 $md5->addfile($fh);
2742 my $got = $md5->hexdigest;
2743 die "Checksum mismatch: $path\n",
2744 "expected: $exp\n got: $got\n" if ($got ne $exp);
2745 seek($fh, 0, 0) or croak $!;
2746 if ($fb->{mode_b} == 120000) {
2747 read($fh, my $buf, 5) == 5 or croak $!;
2748 $buf eq 'link ' or die "$path has mode 120000",
2749 "but is not a link\n";
2750 }
2751 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2752 if (!$pid) {
2753 open STDIN, '<&', $fh or croak $!;
2754 exec qw/git-hash-object -w --stdin/ or croak $!;
2755 }
2756 chomp($hash = do { local $/; <$out> });
2757 close $out or croak $!;
2758 close $fh or croak $!;
2759 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2760 close $fb->{base} or croak $!;
2761 } else {
2762 $hash = $fb->{blob} or die "no blob information\n";
2763 }
2764 $fb->{pool}->clear;
2765 my $gui = $self->{gui};
2766 print $gui "$fb->{mode_b} $hash\t$path\0" or croak $!;
0864e3ba 2767 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $self->{q};
27a1a801
EW
2768 undef;
2769}
2770
2771sub abort_edit {
2772 my $self = shift;
aef4e921 2773 eval { command_close_pipe($self->{gui}, $self->{ctx}) };
27a1a801
EW
2774 $self->SUPER::abort_edit(@_);
2775}
2776
2777sub close_edit {
2778 my $self = shift;
aef4e921 2779 command_close_pipe($self->{gui}, $self->{ctx});
dad73c0b 2780 $self->{git_commit_ok} = 1;
27a1a801
EW
2781 $self->SUPER::close_edit(@_);
2782}
1a82e793 2783
a5e0cedc
EW
2784package SVN::Git::Editor;
2785use vars qw/@ISA/;
2786use strict;
2787use warnings;
2788use Carp qw/croak/;
2789use IO::File;
aef4e921
EW
2790use Git qw/command command_oneline command_noisy
2791 command_output_pipe command_input_pipe command_close_pipe/;
a5e0cedc
EW
2792
2793sub new {
2794 my $class = shift;
2795 my $git_svn = shift;
2796 my $self = SVN::Delta::Editor->new(@_);
2797 bless $self, $class;
2798 foreach (qw/svn_path c r ra /) {
2799 die "$_ required!\n" unless (defined $git_svn->{$_});
2800 $self->{$_} = $git_svn->{$_};
2801 }
2802 $self->{pool} = SVN::Pool->new;
2803 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2804 $self->{rm} = { };
2805 require Digest::MD5;
2806 return $self;
2807}
2808
2809sub split_path {
2810 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2811}
2812
2813sub repo_path {
747fa12c 2814 (defined $_[1] && length $_[1]) ? $_[1] : ''
a5e0cedc
EW
2815}
2816
2817sub url_path {
2818 my ($self, $path) = @_;
2819 $self->{ra}->{url} . '/' . $self->repo_path($path);
2820}
2821
2822sub rmdirs {
80f50749 2823 my ($self, $q) = @_;
a5e0cedc
EW
2824 my $rm = $self->{rm};
2825 delete $rm->{''}; # we never delete the url we're tracking
2826 return unless %$rm;
2827
2828 foreach (keys %$rm) {
2829 my @d = split m#/#, $_;
2830 my $c = shift @d;
2831 $rm->{$c} = 1;
2832 while (@d) {
2833 $c .= '/' . shift @d;
2834 $rm->{$c} = 1;
2835 }
2836 }
2837 delete $rm->{$self->{svn_path}};
2838 delete $rm->{''}; # we never delete the url we're tracking
2839 return unless %$rm;
2840
aef4e921
EW
2841 my ($fh, $ctx) = command_output_pipe(
2842 qw/ls-tree --name-only -r -z/, $self->{c});
a5e0cedc
EW
2843 local $/ = "\0";
2844 while (<$fh>) {
2845 chomp;
747fa12c 2846 my @dn = split m#/#, $_;
c07eee1f
EW
2847 while (pop @dn) {
2848 delete $rm->{join '/', @dn};
2849 }
2850 unless (%$rm) {
22600a25 2851 close $fh;
c07eee1f
EW
2852 return;
2853 }
a5e0cedc 2854 }
aef4e921 2855 command_close_pipe($fh, $ctx);
c07eee1f 2856
a5e0cedc
EW
2857 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2858 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2859 $self->close_directory($bat->{$d}, $p);
2860 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
cea70cf8 2861 print "\tD+\t$d/\n" unless $q;
a5e0cedc
EW
2862 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2863 delete $bat->{$d};
2864 }
2865}
2866
2867sub open_or_add_dir {
2868 my ($self, $full_path, $baton) = @_;
2869 my $p = SVN::Pool->new;
2870 my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
2871 $p->clear;
2872 if ($t == $SVN::Node::none) {
2873 return $self->add_directory($full_path, $baton,
2874 undef, -1, $self->{pool});
2875 } elsif ($t == $SVN::Node::dir) {
2876 return $self->open_directory($full_path, $baton,
2877 $self->{r}, $self->{pool});
2878 }
2879 print STDERR "$full_path already exists in repository at ",
2880 "r$self->{r} and it is not a directory (",
2881 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2882 exit 1;
2883}
2884
2885sub ensure_path {
2886 my ($self, $path) = @_;
2887 my $bat = $self->{bat};
2888 $path = $self->repo_path($path);
2889 return $bat->{''} unless (length $path);
2890 my @p = split m#/+#, $path;
2891 my $c = shift @p;
2892 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2893 while (@p) {
2894 my $c0 = $c;
2895 $c .= '/' . shift @p;
2896 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2897 }
2898 return $bat->{$c};
2899}
2900
2901sub A {
80f50749 2902 my ($self, $m, $q) = @_;
a5e0cedc
EW
2903 my ($dir, $file) = split_path($m->{file_b});
2904 my $pbat = $self->ensure_path($dir);
2905 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2906 undef, -1);
80f50749 2907 print "\tA\t$m->{file_b}\n" unless $q;
a5e0cedc
EW
2908 $self->chg_file($fbat, $m);
2909 $self->close_file($fbat,undef,$self->{pool});
2910}
2911
2912sub C {
80f50749 2913 my ($self, $m, $q) = @_;
a5e0cedc
EW
2914 my ($dir, $file) = split_path($m->{file_b});
2915 my $pbat = $self->ensure_path($dir);
2916 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2917 $self->url_path($m->{file_a}), $self->{r});
80f50749 2918 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
a5e0cedc
EW
2919 $self->chg_file($fbat, $m);
2920 $self->close_file($fbat,undef,$self->{pool});
2921}
2922
2923sub delete_entry {
2924 my ($self, $path, $pbat) = @_;
2925 my $rpath = $self->repo_path($path);
2926 my ($dir, $file) = split_path($rpath);
2927 $self->{rm}->{$dir} = 1;
2928 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2929}
2930
2931sub R {
80f50749 2932 my ($self, $m, $q) = @_;
a5e0cedc
EW
2933 my ($dir, $file) = split_path($m->{file_b});
2934 my $pbat = $self->ensure_path($dir);
2935 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2936 $self->url_path($m->{file_a}), $self->{r});
80f50749 2937 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
a5e0cedc
EW
2938 $self->chg_file($fbat, $m);
2939 $self->close_file($fbat,undef,$self->{pool});
2940
2941 ($dir, $file) = split_path($m->{file_a});
2942 $pbat = $self->ensure_path($dir);
2943 $self->delete_entry($m->{file_a}, $pbat);
2944}
2945
2946sub M {
80f50749 2947 my ($self, $m, $q) = @_;
a5e0cedc
EW
2948 my ($dir, $file) = split_path($m->{file_b});
2949 my $pbat = $self->ensure_path($dir);
2950 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2951 $pbat,$self->{r},$self->{pool});
80f50749 2952 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
a5e0cedc
EW
2953 $self->chg_file($fbat, $m);
2954 $self->close_file($fbat,undef,$self->{pool});
2955}
2956
2957sub T { shift->M(@_) }
2958
2959sub change_file_prop {
2960 my ($self, $fbat, $pname, $pval) = @_;
2961 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2962}
2963
2964sub chg_file {
2965 my ($self, $fbat, $m) = @_;
2966 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2967 $self->change_file_prop($fbat,'svn:executable','*');
2968 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2969 $self->change_file_prop($fbat,'svn:executable',undef);
2970 }
2971 my $fh = IO::File->new_tmpfile or croak $!;
2972 if ($m->{mode_b} =~ /^120/) {
2973 print $fh 'link ' or croak $!;
2974 $self->change_file_prop($fbat,'svn:special','*');
2975 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2976 $self->change_file_prop($fbat,'svn:special',undef);
2977 }
2978 defined(my $pid = fork) or croak $!;
2979 if (!$pid) {
2980 open STDOUT, '>&', $fh or croak $!;
2981 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2982 }
2983 waitpid $pid, 0;
2984 croak $? if $?;
2985 $fh->flush == 0 or croak $!;
2986 seek $fh, 0, 0 or croak $!;
2987
2988 my $md5 = Digest::MD5->new;
2989 $md5->addfile($fh) or croak $!;
2990 seek $fh, 0, 0 or croak $!;
2991
2992 my $exp = $md5->hexdigest;
f7197dff
EW
2993 my $pool = SVN::Pool->new;
2994 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2995 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
a5e0cedc 2996 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
f7197dff 2997 $pool->clear;
a5e0cedc
EW
2998
2999 close $fh or croak $!;
3000}
3001
3002sub D {
80f50749 3003 my ($self, $m, $q) = @_;
a5e0cedc
EW
3004 my ($dir, $file) = split_path($m->{file_b});
3005 my $pbat = $self->ensure_path($dir);
80f50749 3006 print "\tD\t$m->{file_b}\n" unless $q;
a5e0cedc
EW
3007 $self->delete_entry($m->{file_b}, $pbat);
3008}
3009
3010sub close_edit {
3011 my ($self) = @_;
3012 my ($p,$bat) = ($self->{pool}, $self->{bat});
3013 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3014 $self->close_directory($bat->{$_}, $p);
3015 }
3016 $self->SUPER::close_edit($p);
3017 $p->clear;
3018}
3019
3020sub abort_edit {
3021 my ($self) = @_;
3022 $self->SUPER::abort_edit($self->{pool});
3023 $self->{pool}->clear;
3024}
3025
3397f9df
EW
3026__END__
3027
3028Data structures:
3029
b9c85187 3030$log_msg hashref as returned by libsvn_log_entry()
3397f9df
EW
3031{
3032 msg => 'whitespace-formatted log entry
3033', # trailing newline is preserved
3034 revision => '8', # integer
3035 date => '2004-02-24T17:01:44.108345Z', # commit date
3036 author => 'committer name'
3037};
3038
3397f9df
EW
3039@mods = array of diff-index line hashes, each element represents one line
3040 of diff-index output
3041
3042diff-index line ($m hash)
3043{
3044 mode_a => first column of diff-index output, no leading ':',
3045 mode_b => second column of diff-index output,
3046 sha1_b => sha1sum of the final blob,
ac8e0b91 3047 chg => change type [MCRADT],
3397f9df
EW
3048 file_a => original file name of a file (iff chg is 'C' or 'R')
3049 file_b => new/current file name of a file (any chg)
3050}
3051;
a5e0cedc 3052
a00439ac
EW
3053# retval of read_url_paths{,_all}();
3054$l_map = {
3055 # repository root url
3056 'https://svn.musicpd.org' => {
3057 # repository path # GIT_SVN_ID
3058 'mpd/trunk' => 'trunk',
3059 'mpd/tags/0.11.5' => 'tags/0.11.5',
3060 },
3061}
3062
a5e0cedc
EW
3063Notes:
3064 I don't trust the each() function on unless I created %hash myself
3065 because the internal iterator may not have started at base.