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