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