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