Prepare Git::SVN for extraction into its own file.
[git] / git-svn.perl
1 #!/usr/bin/perl
2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
4 use 5.008;
5 use warnings;
6 use strict;
7 use vars qw/    $AUTHOR $VERSION
8                 $sha1 $sha1_short $_revision $_repository
9                 $_q $_authors $_authors_prog %users/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
12
13 use Git::SVN::Utils qw(fatal can_compress);
14
15 # From which subdir have we been invoked?
16 my $cmd_dir_prefix = eval {
17         command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
18 } || '';
19
20 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
21 $ENV{GIT_DIR} ||= '.git';
22 $Git::SVN::default_repo_id = 'svn';
23 $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
24 $Git::SVN::Ra::_log_window_size = 100;
25 $Git::SVN::_minimize_url = 'unset';
26
27 if (! exists $ENV{SVN_SSH} && exists $ENV{GIT_SSH}) {
28         $ENV{SVN_SSH} = $ENV{GIT_SSH};
29 }
30
31 if (exists $ENV{SVN_SSH} && $^O eq 'msys') {
32         $ENV{SVN_SSH} =~ s/\\/\\\\/g;
33         $ENV{SVN_SSH} =~ s/(.*)/"$1"/;
34 }
35
36 $Git::SVN::Log::TZ = $ENV{TZ};
37 $ENV{TZ} = 'UTC';
38 $| = 1; # unbuffer STDOUT
39
40 # All SVN commands do it.  Otherwise we may die on SIGPIPE when the remote
41 # repository decides to close the connection which we expect to be kept alive.
42 $SIG{PIPE} = 'IGNORE';
43
44 # Given a dot separated version number, "subtract" it from
45 # the SVN::Core::VERSION; non-negaitive return means the SVN::Core
46 # is at least at the version the caller asked for.
47 sub compare_svn_version {
48         my (@ours) = split(/\./, $SVN::Core::VERSION);
49         my (@theirs) = split(/\./, $_[0]);
50         my ($i, $diff);
51
52         for ($i = 0; $i < @ours && $i < @theirs; $i++) {
53                 $diff = $ours[$i] - $theirs[$i];
54                 return $diff if ($diff);
55         }
56         return 1 if ($i < @ours);
57         return -1 if ($i < @theirs);
58         return 0;
59 }
60
61 sub _req_svn {
62         require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
63         require SVN::Ra;
64         require SVN::Delta;
65         if (::compare_svn_version('1.1.0') < 0) {
66                 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
67         }
68 }
69
70 use Carp qw/croak/;
71 use Digest::MD5;
72 use IO::File qw//;
73 use File::Basename qw/dirname basename/;
74 use File::Path qw/mkpath/;
75 use File::Spec;
76 use File::Find;
77 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
78 use IPC::Open3;
79 use Git;
80 use Git::SVN::Editor qw//;
81 use Git::SVN::Fetcher qw//;
82 use Git::SVN::Ra qw//;
83 use Git::SVN::Prompt qw//;
84 use Memoize;  # core since 5.8.0, Jul 2002
85
86 BEGIN {
87         # import functions from Git into our packages, en masse
88         no strict 'refs';
89         foreach (qw/command command_oneline command_noisy command_output_pipe
90                     command_input_pipe command_close_pipe
91                     command_bidi_pipe command_close_bidi_pipe/) {
92                 for my $package ( qw(Git::SVN::Migration Git::SVN::Log),
93                         __PACKAGE__) {
94                         *{"${package}::$_"} = \&{"Git::$_"};
95                 }
96         }
97         Memoize::memoize 'Git::config';
98         Memoize::memoize 'Git::config_bool';
99 }
100
101 my ($SVN);
102
103 $sha1 = qr/[a-f\d]{40}/;
104 $sha1_short = qr/[a-f\d]{4,40}/;
105 my ($_stdin, $_help, $_edit,
106         $_message, $_file, $_branch_dest,
107         $_template, $_shared,
108         $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
109         $_merge, $_strategy, $_preserve_merges, $_dry_run, $_local,
110         $_prefix, $_no_checkout, $_url, $_verbose,
111         $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive);
112
113 # This is a refactoring artifact so Git::SVN can get at this git-svn switch.
114 sub opt_prefix { return $_prefix || '' }
115
116 $Git::SVN::_follow_parent = 1;
117 $Git::SVN::Fetcher::_placeholder_filename = ".gitignore";
118 $_q ||= 0;
119 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
120                     'config-dir=s' => \$Git::SVN::Ra::config_dir,
121                     'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
122                     'ignore-paths=s' => \$Git::SVN::Fetcher::_ignore_regex,
123                     'ignore-refs=s' => \$Git::SVN::Ra::_ignore_refs_regex );
124 my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
125                 'authors-file|A=s' => \$_authors,
126                 'authors-prog=s' => \$_authors_prog,
127                 'repack:i' => \$Git::SVN::_repack,
128                 'noMetadata' => \$Git::SVN::_no_metadata,
129                 'useSvmProps' => \$Git::SVN::_use_svm_props,
130                 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
131                 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
132                 'no-checkout' => \$_no_checkout,
133                 'quiet|q+' => \$_q,
134                 'repack-flags|repack-args|repack-opts=s' =>
135                    \$Git::SVN::_repack_flags,
136                 'use-log-author' => \$Git::SVN::_use_log_author,
137                 'add-author-from' => \$Git::SVN::_add_author_from,
138                 'localtime' => \$Git::SVN::_localtime,
139                 %remote_opts );
140
141 my ($_trunk, @_tags, @_branches, $_stdlayout);
142 my %icv;
143 my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
144                   'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
145                   'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
146                   'stdlayout|s' => \$_stdlayout,
147                   'minimize-url|m!' => \$Git::SVN::_minimize_url,
148                   'no-metadata' => sub { $icv{noMetadata} = 1 },
149                   'use-svm-props' => sub { $icv{useSvmProps} = 1 },
150                   'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
151                   'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
152                   'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] },
153                   %remote_opts );
154 my %cmt_opts = ( 'edit|e' => \$_edit,
155                 'rmdir' => \$Git::SVN::Editor::_rmdir,
156                 'find-copies-harder' => \$Git::SVN::Editor::_find_copies_harder,
157                 'l=i' => \$Git::SVN::Editor::_rename_limit,
158                 'copy-similarity|C=i'=> \$Git::SVN::Editor::_cp_similarity
159 );
160
161 my %cmd = (
162         fetch => [ \&cmd_fetch, "Download new revisions from SVN",
163                         { 'revision|r=s' => \$_revision,
164                           'fetch-all|all' => \$_fetch_all,
165                           'parent|p' => \$_fetch_parent,
166                            %fc_opts } ],
167         clone => [ \&cmd_clone, "Initialize and fetch revisions",
168                         { 'revision|r=s' => \$_revision,
169                           'preserve-empty-dirs' =>
170                                 \$Git::SVN::Fetcher::_preserve_empty_dirs,
171                           'placeholder-filename=s' =>
172                                 \$Git::SVN::Fetcher::_placeholder_filename,
173                            %fc_opts, %init_opts } ],
174         init => [ \&cmd_init, "Initialize a repo for tracking" .
175                           " (requires URL argument)",
176                           \%init_opts ],
177         'multi-init' => [ \&cmd_multi_init,
178                           "Deprecated alias for ".
179                           "'$0 init -T<trunk> -b<branches> -t<tags>'",
180                           \%init_opts ],
181         dcommit => [ \&cmd_dcommit,
182                      'Commit several diffs to merge with upstream',
183                         { 'merge|m|M' => \$_merge,
184                           'strategy|s=s' => \$_strategy,
185                           'verbose|v' => \$_verbose,
186                           'dry-run|n' => \$_dry_run,
187                           'fetch-all|all' => \$_fetch_all,
188                           'commit-url=s' => \$_commit_url,
189                           'revision|r=i' => \$_revision,
190                           'no-rebase' => \$_no_rebase,
191                           'mergeinfo=s' => \$_merge_info,
192                           'interactive|i' => \$_interactive,
193                         %cmt_opts, %fc_opts } ],
194         branch => [ \&cmd_branch,
195                     'Create a branch in the SVN repository',
196                     { 'message|m=s' => \$_message,
197                       'destination|d=s' => \$_branch_dest,
198                       'dry-run|n' => \$_dry_run,
199                       'tag|t' => \$_tag,
200                       'username=s' => \$Git::SVN::Prompt::_username,
201                       'commit-url=s' => \$_commit_url } ],
202         tag => [ sub { $_tag = 1; cmd_branch(@_) },
203                  'Create a tag in the SVN repository',
204                  { 'message|m=s' => \$_message,
205                    'destination|d=s' => \$_branch_dest,
206                    'dry-run|n' => \$_dry_run,
207                    'username=s' => \$Git::SVN::Prompt::_username,
208                    'commit-url=s' => \$_commit_url } ],
209         'set-tree' => [ \&cmd_set_tree,
210                         "Set an SVN repository to a git tree-ish",
211                         { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
212         'create-ignore' => [ \&cmd_create_ignore,
213                              'Create a .gitignore per svn:ignore',
214                              { 'revision|r=i' => \$_revision
215                              } ],
216         'mkdirs' => [ \&cmd_mkdirs ,
217                       "recreate empty directories after a checkout",
218                       { 'revision|r=i' => \$_revision } ],
219         'propget' => [ \&cmd_propget,
220                        'Print the value of a property on a file or directory',
221                        { 'revision|r=i' => \$_revision } ],
222         'proplist' => [ \&cmd_proplist,
223                        'List all properties of a file or directory',
224                        { 'revision|r=i' => \$_revision } ],
225         'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
226                         { 'revision|r=i' => \$_revision
227                         } ],
228         'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
229                         { 'revision|r=i' => \$_revision
230                         } ],
231         'multi-fetch' => [ \&cmd_multi_fetch,
232                            "Deprecated alias for $0 fetch --all",
233                            { 'revision|r=s' => \$_revision, %fc_opts } ],
234         'migrate' => [ sub { },
235                        # no-op, we automatically run this anyways,
236                        'Migrate configuration/metadata/layout from
237                         previous versions of git-svn',
238                        { 'minimize' => \$Git::SVN::Migration::_minimize,
239                          %remote_opts } ],
240         'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
241                         { 'limit=i' => \$Git::SVN::Log::limit,
242                           'revision|r=s' => \$_revision,
243                           'verbose|v' => \$Git::SVN::Log::verbose,
244                           'incremental' => \$Git::SVN::Log::incremental,
245                           'oneline' => \$Git::SVN::Log::oneline,
246                           'show-commit' => \$Git::SVN::Log::show_commit,
247                           'non-recursive' => \$Git::SVN::Log::non_recursive,
248                           'authors-file|A=s' => \$_authors,
249                           'color' => \$Git::SVN::Log::color,
250                           'pager=s' => \$Git::SVN::Log::pager
251                         } ],
252         'find-rev' => [ \&cmd_find_rev,
253                         "Translate between SVN revision numbers and tree-ish",
254                         {} ],
255         'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
256                         { 'merge|m|M' => \$_merge,
257                           'verbose|v' => \$_verbose,
258                           'strategy|s=s' => \$_strategy,
259                           'local|l' => \$_local,
260                           'fetch-all|all' => \$_fetch_all,
261                           'dry-run|n' => \$_dry_run,
262                           'preserve-merges|p' => \$_preserve_merges,
263                           %fc_opts } ],
264         'commit-diff' => [ \&cmd_commit_diff,
265                            'Commit a diff between two trees',
266                         { 'message|m=s' => \$_message,
267                           'file|F=s' => \$_file,
268                           'revision|r=s' => \$_revision,
269                         %cmt_opts } ],
270         'info' => [ \&cmd_info,
271                     "Show info about the latest SVN revision
272                      on the current branch",
273                     { 'url' => \$_url, } ],
274         'blame' => [ \&Git::SVN::Log::cmd_blame,
275                     "Show what revision and author last modified each line of a file",
276                     { 'git-format' => \$_git_format } ],
277         'reset' => [ \&cmd_reset,
278                      "Undo fetches back to the specified SVN revision",
279                      { 'revision|r=s' => \$_revision,
280                        'parent|p' => \$_fetch_parent } ],
281         'gc' => [ \&cmd_gc,
282                   "Compress unhandled.log files in .git/svn and remove " .
283                   "index files in .git/svn",
284                 {} ],
285 );
286
287 use Term::ReadLine;
288 package FakeTerm;
289 sub new {
290         my ($class, $reason) = @_;
291         return bless \$reason, shift;
292 }
293 sub readline {
294         my $self = shift;
295         die "Cannot use readline on FakeTerm: $$self";
296 }
297 package main;
298
299 my $term = eval {
300         $ENV{"GIT_SVN_NOTTY"}
301                 ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT
302                 : new Term::ReadLine 'git-svn';
303 };
304 if ($@) {
305         $term = new FakeTerm "$@: going non-interactive";
306 }
307
308 my $cmd;
309 for (my $i = 0; $i < @ARGV; $i++) {
310         if (defined $cmd{$ARGV[$i]}) {
311                 $cmd = $ARGV[$i];
312                 splice @ARGV, $i, 1;
313                 last;
314         } elsif ($ARGV[$i] eq 'help') {
315                 $cmd = $ARGV[$i+1];
316                 usage(0);
317         }
318 };
319
320 # make sure we're always running at the top-level working directory
321 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
322         unless (-d $ENV{GIT_DIR}) {
323                 if ($git_dir_user_set) {
324                         die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
325                             "but it is not a directory\n";
326                 }
327                 my $git_dir = delete $ENV{GIT_DIR};
328                 my $cdup = undef;
329                 git_cmd_try {
330                         $cdup = command_oneline(qw/rev-parse --show-cdup/);
331                         $git_dir = '.' unless ($cdup);
332                         chomp $cdup if ($cdup);
333                         $cdup = "." unless ($cdup && length $cdup);
334                 } "Already at toplevel, but $git_dir not found\n";
335                 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
336                 unless (-d $git_dir) {
337                         die "$git_dir still not found after going to ",
338                             "'$cdup'\n";
339                 }
340                 $ENV{GIT_DIR} = $git_dir;
341         }
342         $_repository = Git->repository(Repository => $ENV{GIT_DIR});
343 }
344
345 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
346
347 read_git_config(\%opts);
348 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
349         Getopt::Long::Configure('pass_through');
350 }
351 my $rv = GetOptions(%opts, 'h|H' => \$_help, 'version|V' => \$_version,
352                     'minimize-connections' => \$Git::SVN::Migration::_minimize,
353                     'id|i=s' => \$Git::SVN::default_ref_id,
354                     'svn-remote|remote|R=s' => sub {
355                        $Git::SVN::no_reuse_existing = 1;
356                        $Git::SVN::default_repo_id = $_[1] });
357 exit 1 if (!$rv && $cmd && $cmd ne 'log');
358
359 usage(0) if $_help;
360 version() if $_version;
361 usage(1) unless defined $cmd;
362 load_authors() if $_authors;
363 if (defined $_authors_prog) {
364         $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
365 }
366
367 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
368         Git::SVN::Migration::migration_check();
369 }
370 Git::SVN::init_vars();
371 eval {
372         Git::SVN::verify_remotes_sanity();
373         $cmd{$cmd}->[0]->(@ARGV);
374         post_fetch_checkout();
375 };
376 fatal $@ if $@;
377 exit 0;
378
379 ####################### primary functions ######################
380 sub usage {
381         my $exit = shift || 0;
382         my $fd = $exit ? \*STDERR : \*STDOUT;
383         print $fd <<"";
384 git-svn - bidirectional operations between a single Subversion tree and git
385 Usage: git svn <command> [options] [arguments]\n
386
387         print $fd "Available commands:\n" unless $cmd;
388
389         foreach (sort keys %cmd) {
390                 next if $cmd && $cmd ne $_;
391                 next if /^multi-/; # don't show deprecated commands
392                 print $fd '  ',pack('A17',$_),$cmd{$_}->[1],"\n";
393                 foreach (sort keys %{$cmd{$_}->[2]}) {
394                         # mixed-case options are for .git/config only
395                         next if /[A-Z]/ && /^[a-z]+$/i;
396                         # prints out arguments as they should be passed:
397                         my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
398                         print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
399                                                         "--$_" : "-$_" }
400                                                 split /\|/,$_)," $x\n";
401                 }
402         }
403         print $fd <<"";
404 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
405 arbitrary identifier if you're tracking multiple SVN branches/repositories in
406 one git repository and want to keep them separate.  See git-svn(1) for more
407 information.
408
409         exit $exit;
410 }
411
412 sub version {
413         ::_req_svn();
414         print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
415         exit 0;
416 }
417
418 sub ask {
419         my ($prompt, %arg) = @_;
420         my $valid_re = $arg{valid_re};
421         my $default = $arg{default};
422         my $resp;
423         my $i = 0;
424
425         if ( !( defined($term->IN)
426             && defined( fileno($term->IN) )
427             && defined( $term->OUT )
428             && defined( fileno($term->OUT) ) ) ){
429                 return defined($default) ? $default : undef;
430         }
431
432         while ($i++ < 10) {
433                 $resp = $term->readline($prompt);
434                 if (!defined $resp) { # EOF
435                         print "\n";
436                         return defined $default ? $default : undef;
437                 }
438                 if ($resp eq '' and defined $default) {
439                         return $default;
440                 }
441                 if (!defined $valid_re or $resp =~ /$valid_re/) {
442                         return $resp;
443                 }
444         }
445         return undef;
446 }
447
448 sub do_git_init_db {
449         unless (-d $ENV{GIT_DIR}) {
450                 my @init_db = ('init');
451                 push @init_db, "--template=$_template" if defined $_template;
452                 if (defined $_shared) {
453                         if ($_shared =~ /[a-z]/) {
454                                 push @init_db, "--shared=$_shared";
455                         } else {
456                                 push @init_db, "--shared";
457                         }
458                 }
459                 command_noisy(@init_db);
460                 $_repository = Git->repository(Repository => ".git");
461         }
462         my $set;
463         my $pfx = "svn-remote.$Git::SVN::default_repo_id";
464         foreach my $i (keys %icv) {
465                 die "'$set' and '$i' cannot both be set\n" if $set;
466                 next unless defined $icv{$i};
467                 command_noisy('config', "$pfx.$i", $icv{$i});
468                 $set = $i;
469         }
470         my $ignore_paths_regex = \$Git::SVN::Fetcher::_ignore_regex;
471         command_noisy('config', "$pfx.ignore-paths", $$ignore_paths_regex)
472                 if defined $$ignore_paths_regex;
473         my $ignore_refs_regex = \$Git::SVN::Ra::_ignore_refs_regex;
474         command_noisy('config', "$pfx.ignore-refs", $$ignore_refs_regex)
475                 if defined $$ignore_refs_regex;
476
477         if (defined $Git::SVN::Fetcher::_preserve_empty_dirs) {
478                 my $fname = \$Git::SVN::Fetcher::_placeholder_filename;
479                 command_noisy('config', "$pfx.preserve-empty-dirs", 'true');
480                 command_noisy('config', "$pfx.placeholder-filename", $$fname);
481         }
482 }
483
484 sub init_subdir {
485         my $repo_path = shift or return;
486         mkpath([$repo_path]) unless -d $repo_path;
487         chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
488         $ENV{GIT_DIR} = '.git';
489         $_repository = Git->repository(Repository => $ENV{GIT_DIR});
490 }
491
492 sub cmd_clone {
493         my ($url, $path) = @_;
494         if (!defined $path &&
495             (defined $_trunk || @_branches || @_tags ||
496              defined $_stdlayout) &&
497             $url !~ m#^[a-z\+]+://#) {
498                 $path = $url;
499         }
500         $path = basename($url) if !defined $path || !length $path;
501         my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
502         cmd_init($url, $path);
503         command_oneline('config', 'svn.authorsfile', $authors_absolute)
504             if $_authors;
505         Git::SVN::fetch_all($Git::SVN::default_repo_id);
506 }
507
508 sub cmd_init {
509         if (defined $_stdlayout) {
510                 $_trunk = 'trunk' if (!defined $_trunk);
511                 @_tags = 'tags' if (! @_tags);
512                 @_branches = 'branches' if (! @_branches);
513         }
514         if (defined $_trunk || @_branches || @_tags) {
515                 return cmd_multi_init(@_);
516         }
517         my $url = shift or die "SVN repository location required ",
518                                "as a command-line argument\n";
519         $url = canonicalize_url($url);
520         init_subdir(@_);
521         do_git_init_db();
522
523         if ($Git::SVN::_minimize_url eq 'unset') {
524                 $Git::SVN::_minimize_url = 0;
525         }
526
527         Git::SVN->init($url);
528 }
529
530 sub cmd_fetch {
531         if (grep /^\d+=./, @_) {
532                 die "'<rev>=<commit>' fetch arguments are ",
533                     "no longer supported.\n";
534         }
535         my ($remote) = @_;
536         if (@_ > 1) {
537                 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
538         }
539         $Git::SVN::no_reuse_existing = undef;
540         if ($_fetch_parent) {
541                 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
542                 unless ($gs) {
543                         die "Unable to determine upstream SVN information from ",
544                             "working tree history\n";
545                 }
546                 # just fetch, don't checkout.
547                 $_no_checkout = 'true';
548                 $_fetch_all ? $gs->fetch_all : $gs->fetch;
549         } elsif ($_fetch_all) {
550                 cmd_multi_fetch();
551         } else {
552                 $remote ||= $Git::SVN::default_repo_id;
553                 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
554         }
555 }
556
557 sub cmd_set_tree {
558         my (@commits) = @_;
559         if ($_stdin || !@commits) {
560                 print "Reading from stdin...\n";
561                 @commits = ();
562                 while (<STDIN>) {
563                         if (/\b($sha1_short)\b/o) {
564                                 unshift @commits, $1;
565                         }
566                 }
567         }
568         my @revs;
569         foreach my $c (@commits) {
570                 my @tmp = command('rev-parse',$c);
571                 if (scalar @tmp == 1) {
572                         push @revs, $tmp[0];
573                 } elsif (scalar @tmp > 1) {
574                         push @revs, reverse(command('rev-list',@tmp));
575                 } else {
576                         fatal "Failed to rev-parse $c";
577                 }
578         }
579         my $gs = Git::SVN->new;
580         my ($r_last, $cmt_last) = $gs->last_rev_commit;
581         $gs->fetch;
582         if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
583                 fatal "There are new revisions that were fetched ",
584                       "and need to be merged (or acknowledged) ",
585                       "before committing.\nlast rev: $r_last\n",
586                       " current: $gs->{last_rev}";
587         }
588         $gs->set_tree($_) foreach @revs;
589         print "Done committing ",scalar @revs," revisions to SVN\n";
590         unlink $gs->{index};
591 }
592
593 sub split_merge_info_range {
594         my ($range) = @_;
595         if ($range =~ /(\d+)-(\d+)/) {
596                 return (int($1), int($2));
597         } else {
598                 return (int($range), int($range));
599         }
600 }
601
602 sub combine_ranges {
603         my ($in) = @_;
604
605         my @fnums = ();
606         my @arr = split(/,/, $in);
607         for my $element (@arr) {
608                 my ($start, $end) = split_merge_info_range($element);
609                 push @fnums, $start;
610         }
611
612         my @sorted = @arr [ sort {
613                 $fnums[$a] <=> $fnums[$b]
614         } 0..$#arr ];
615
616         my @return = ();
617         my $last = -1;
618         my $first = -1;
619         for my $element (@sorted) {
620                 my ($start, $end) = split_merge_info_range($element);
621
622                 if ($last == -1) {
623                         $first = $start;
624                         $last = $end;
625                         next;
626                 }
627                 if ($start <= $last+1) {
628                         if ($end > $last) {
629                                 $last = $end;
630                         }
631                         next;
632                 }
633                 if ($first == $last) {
634                         push @return, "$first";
635                 } else {
636                         push @return, "$first-$last";
637                 }
638                 $first = $start;
639                 $last = $end;
640         }
641
642         if ($first != -1) {
643                 if ($first == $last) {
644                         push @return, "$first";
645                 } else {
646                         push @return, "$first-$last";
647                 }
648         }
649
650         return join(',', @return);
651 }
652
653 sub merge_revs_into_hash {
654         my ($hash, $minfo) = @_;
655         my @lines = split(' ', $minfo);
656
657         for my $line (@lines) {
658                 my ($branchpath, $revs) = split(/:/, $line);
659
660                 if (exists($hash->{$branchpath})) {
661                         # Merge the two revision sets
662                         my $combined = "$hash->{$branchpath},$revs";
663                         $hash->{$branchpath} = combine_ranges($combined);
664                 } else {
665                         # Just do range combining for consolidation
666                         $hash->{$branchpath} = combine_ranges($revs);
667                 }
668         }
669 }
670
671 sub merge_merge_info {
672         my ($mergeinfo_one, $mergeinfo_two) = @_;
673         my %result_hash = ();
674
675         merge_revs_into_hash(\%result_hash, $mergeinfo_one);
676         merge_revs_into_hash(\%result_hash, $mergeinfo_two);
677
678         my $result = '';
679         # Sort below is for consistency's sake
680         for my $branchname (sort keys(%result_hash)) {
681                 my $revlist = $result_hash{$branchname};
682                 $result .= "$branchname:$revlist\n"
683         }
684         return $result;
685 }
686
687 sub populate_merge_info {
688         my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_;
689
690         my %parentshash;
691         read_commit_parents(\%parentshash, $d);
692         my @parents = @{$parentshash{$d}};
693         if ($#parents > 0) {
694                 # Merge commit
695                 my $all_parents_ok = 1;
696                 my $aggregate_mergeinfo = '';
697                 my $rooturl = $gs->repos_root;
698
699                 if (defined($rewritten_parent)) {
700                         # Replace first parent with newly-rewritten version
701                         shift @parents;
702                         unshift @parents, $rewritten_parent;
703                 }
704
705                 foreach my $parent (@parents) {
706                         my ($branchurl, $svnrev, $paruuid) =
707                                 cmt_metadata($parent);
708
709                         unless (defined($svnrev)) {
710                                 # Should have been caught be preflight check
711                                 fatal "merge commit $d has ancestor $parent, but that change "
712                      ."does not have git-svn metadata!";
713                         }
714                         unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
715                                 fatal "commit $parent git-svn metadata changed mid-run!";
716                         }
717                         my $branchpath = $1;
718
719                         my $ra = Git::SVN::Ra->new($branchurl);
720                         my (undef, undef, $props) =
721                                 $ra->get_dir(canonicalize_path("."), $svnrev);
722                         my $par_mergeinfo = $props->{'svn:mergeinfo'};
723                         unless (defined $par_mergeinfo) {
724                                 $par_mergeinfo = '';
725                         }
726                         # Merge previous mergeinfo values
727                         $aggregate_mergeinfo =
728                                 merge_merge_info($aggregate_mergeinfo,
729                                                                  $par_mergeinfo, 0);
730
731                         next if $parent eq $parents[0]; # Skip first parent
732                         # Add new changes being placed in tree by merge
733                         my @cmd = (qw/rev-list --reverse/,
734                                            $parent, qw/--not/);
735                         foreach my $par (@parents) {
736                                 unless ($par eq $parent) {
737                                         push @cmd, $par;
738                                 }
739                         }
740                         my @revsin = ();
741                         my ($revlist, $ctx) = command_output_pipe(@cmd);
742                         while (<$revlist>) {
743                                 my $irev = $_;
744                                 chomp $irev;
745                                 my (undef, $csvnrev, undef) =
746                                         cmt_metadata($irev);
747                                 unless (defined $csvnrev) {
748                                         # A child is missing SVN annotations...
749                                         # this might be OK, or might not be.
750                                         warn "W:child $irev is merged into revision "
751                                                  ."$d but does not have git-svn metadata. "
752                                                  ."This means git-svn cannot determine the "
753                                                  ."svn revision numbers to place into the "
754                                                  ."svn:mergeinfo property. You must ensure "
755                                                  ."a branch is entirely committed to "
756                                                  ."SVN before merging it in order for "
757                                                  ."svn:mergeinfo population to function "
758                                                  ."properly";
759                                 }
760                                 push @revsin, $csvnrev;
761                         }
762                         command_close_pipe($revlist, $ctx);
763
764                         last unless $all_parents_ok;
765
766                         # We now have a list of all SVN revnos which are
767                         # merged by this particular parent. Integrate them.
768                         next if $#revsin == -1;
769                         my $newmergeinfo = "$branchpath:" . join(',', @revsin);
770                         $aggregate_mergeinfo =
771                                 merge_merge_info($aggregate_mergeinfo,
772                                                                  $newmergeinfo, 1);
773                 }
774                 if ($all_parents_ok and $aggregate_mergeinfo) {
775                         return $aggregate_mergeinfo;
776                 }
777         }
778
779         return undef;
780 }
781
782 sub cmd_dcommit {
783         my $head = shift;
784         command_noisy(qw/update-index --refresh/);
785         git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
786                 'Cannot dcommit with a dirty index.  Commit your changes first, '
787                 . "or stash them with `git stash'.\n";
788         $head ||= 'HEAD';
789
790         my $old_head;
791         if ($head ne 'HEAD') {
792                 $old_head = eval {
793                         command_oneline([qw/symbolic-ref -q HEAD/])
794                 };
795                 if ($old_head) {
796                         $old_head =~ s{^refs/heads/}{};
797                 } else {
798                         $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
799                 }
800                 command(['checkout', $head], STDERR => 0);
801         }
802
803         my @refs;
804         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
805         unless ($gs) {
806                 die "Unable to determine upstream SVN information from ",
807                     "$head history.\nPerhaps the repository is empty.";
808         }
809
810         if (defined $_commit_url) {
811                 $url = $_commit_url;
812         } else {
813                 $url = eval { command_oneline('config', '--get',
814                               "svn-remote.$gs->{repo_id}.commiturl") };
815                 if (!$url) {
816                         $url = $gs->full_pushurl
817                 }
818         }
819
820         my $last_rev = $_revision if defined $_revision;
821         if ($url) {
822                 print "Committing to $url ...\n";
823         }
824         my ($linear_refs, $parents) = linearize_history($gs, \@refs);
825         if ($_no_rebase && scalar(@$linear_refs) > 1) {
826                 warn "Attempting to commit more than one change while ",
827                      "--no-rebase is enabled.\n",
828                      "If these changes depend on each other, re-running ",
829                      "without --no-rebase may be required."
830         }
831
832         if (defined $_interactive){
833                 my $ask_default = "y";
834                 foreach my $d (@$linear_refs){
835                         my ($fh, $ctx) = command_output_pipe(qw(show --summary), "$d");
836                         while (<$fh>){
837                                 print $_;
838                         }
839                         command_close_pipe($fh, $ctx);
840                         $_ = ask("Commit this patch to SVN? ([y]es (default)|[n]o|[q]uit|[a]ll): ",
841                                  valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
842                                  default => $ask_default);
843                         die "Commit this patch reply required" unless defined $_;
844                         if (/^[nq]/i) {
845                                 exit(0);
846                         } elsif (/^a/i) {
847                                 last;
848                         }
849                 }
850         }
851
852         my $expect_url = $url;
853
854         my $push_merge_info = eval {
855                 command_oneline(qw/config --get svn.pushmergeinfo/)
856                 };
857         if (not defined($push_merge_info)
858                         or $push_merge_info eq "false"
859                         or $push_merge_info eq "no"
860                         or $push_merge_info eq "never") {
861                 $push_merge_info = 0;
862         }
863
864         unless (defined($_merge_info) || ! $push_merge_info) {
865                 # Preflight check of changes to ensure no issues with mergeinfo
866                 # This includes check for uncommitted-to-SVN parents
867                 # (other than the first parent, which we will handle),
868                 # information from different SVN repos, and paths
869                 # which are not underneath this repository root.
870                 my $rooturl = $gs->repos_root;
871                 foreach my $d (@$linear_refs) {
872                         my %parentshash;
873                         read_commit_parents(\%parentshash, $d);
874                         my @realparents = @{$parentshash{$d}};
875                         if ($#realparents > 0) {
876                                 # Merge commit
877                                 shift @realparents; # Remove/ignore first parent
878                                 foreach my $parent (@realparents) {
879                                         my ($branchurl, $svnrev, $paruuid) = cmt_metadata($parent);
880                                         unless (defined $paruuid) {
881                                                 # A parent is missing SVN annotations...
882                                                 # abort the whole operation.
883                                                 fatal "$parent is merged into revision $d, "
884                                                          ."but does not have git-svn metadata. "
885                                                          ."Either dcommit the branch or use a "
886                                                          ."local cherry-pick, FF merge, or rebase "
887                                                          ."instead of an explicit merge commit.";
888                                         }
889
890                                         unless ($paruuid eq $uuid) {
891                                                 # Parent has SVN metadata from different repository
892                                                 fatal "merge parent $parent for change $d has "
893                                                          ."git-svn uuid $paruuid, while current change "
894                                                          ."has uuid $uuid!";
895                                         }
896
897                                         unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
898                                                 # This branch is very strange indeed.
899                                                 fatal "merge parent $parent for $d is on branch "
900                                                          ."$branchurl, which is not under the "
901                                                          ."git-svn root $rooturl!";
902                                         }
903                                 }
904                         }
905                 }
906         }
907
908         my $rewritten_parent;
909         Git::SVN::remove_username($expect_url);
910         if (defined($_merge_info)) {
911                 $_merge_info =~ tr{ }{\n};
912         }
913         while (1) {
914                 my $d = shift @$linear_refs or last;
915                 unless (defined $last_rev) {
916                         (undef, $last_rev, undef) = cmt_metadata("$d~1");
917                         unless (defined $last_rev) {
918                                 fatal "Unable to extract revision information ",
919                                       "from commit $d~1";
920                         }
921                 }
922                 if ($_dry_run) {
923                         print "diff-tree $d~1 $d\n";
924                 } else {
925                         my $cmt_rev;
926
927                         unless (defined($_merge_info) || ! $push_merge_info) {
928                                 $_merge_info = populate_merge_info($d, $gs,
929                                                              $uuid,
930                                                              $linear_refs,
931                                                              $rewritten_parent);
932                         }
933
934                         my %ed_opts = ( r => $last_rev,
935                                         log => get_commit_entry($d)->{log},
936                                         ra => Git::SVN::Ra->new($url),
937                                         config => SVN::Core::config_get_config(
938                                                 $Git::SVN::Ra::config_dir
939                                         ),
940                                         tree_a => "$d~1",
941                                         tree_b => $d,
942                                         editor_cb => sub {
943                                                print "Committed r$_[0]\n";
944                                                $cmt_rev = $_[0];
945                                         },
946                                         mergeinfo => $_merge_info,
947                                         svn_path => '');
948                         if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
949                                 print "No changes\n$d~1 == $d\n";
950                         } elsif ($parents->{$d} && @{$parents->{$d}}) {
951                                 $gs->{inject_parents_dcommit}->{$cmt_rev} =
952                                                                $parents->{$d};
953                         }
954                         $_fetch_all ? $gs->fetch_all : $gs->fetch;
955                         $last_rev = $cmt_rev;
956                         next if $_no_rebase;
957
958                         # we always want to rebase against the current HEAD,
959                         # not any head that was passed to us
960                         my @diff = command('diff-tree', $d,
961                                            $gs->refname, '--');
962                         my @finish;
963                         if (@diff) {
964                                 @finish = rebase_cmd();
965                                 print STDERR "W: $d and ", $gs->refname,
966                                              " differ, using @finish:\n",
967                                              join("\n", @diff), "\n";
968                         } else {
969                                 print "No changes between current HEAD and ",
970                                       $gs->refname,
971                                       "\nResetting to the latest ",
972                                       $gs->refname, "\n";
973                                 @finish = qw/reset --mixed/;
974                         }
975                         command_noisy(@finish, $gs->refname);
976
977                         $rewritten_parent = command_oneline(qw/rev-parse HEAD/);
978
979                         if (@diff) {
980                                 @refs = ();
981                                 my ($url_, $rev_, $uuid_, $gs_) =
982                                               working_head_info('HEAD', \@refs);
983                                 my ($linear_refs_, $parents_) =
984                                               linearize_history($gs_, \@refs);
985                                 if (scalar(@$linear_refs) !=
986                                     scalar(@$linear_refs_)) {
987                                         fatal "# of revisions changed ",
988                                           "\nbefore:\n",
989                                           join("\n", @$linear_refs),
990                                           "\n\nafter:\n",
991                                           join("\n", @$linear_refs_), "\n",
992                                           'If you are attempting to commit ',
993                                           "merges, try running:\n\t",
994                                           'git rebase --interactive',
995                                           '--preserve-merges ',
996                                           $gs->refname,
997                                           "\nBefore dcommitting";
998                                 }
999                                 if ($url_ ne $expect_url) {
1000                                         if ($url_ eq $gs->metadata_url) {
1001                                                 print
1002                                                   "Accepting rewritten URL:",
1003                                                   " $url_\n";
1004                                         } else {
1005                                                 fatal
1006                                                   "URL mismatch after rebase:",
1007                                                   " $url_ != $expect_url";
1008                                         }
1009                                 }
1010                                 if ($uuid_ ne $uuid) {
1011                                         fatal "uuid mismatch after rebase: ",
1012                                               "$uuid_ != $uuid";
1013                                 }
1014                                 # remap parents
1015                                 my (%p, @l, $i);
1016                                 for ($i = 0; $i < scalar @$linear_refs; $i++) {
1017                                         my $new = $linear_refs_->[$i] or next;
1018                                         $p{$new} =
1019                                                 $parents->{$linear_refs->[$i]};
1020                                         push @l, $new;
1021                                 }
1022                                 $parents = \%p;
1023                                 $linear_refs = \@l;
1024                         }
1025                 }
1026         }
1027
1028         if ($old_head) {
1029                 my $new_head = command_oneline(qw/rev-parse HEAD/);
1030                 my $new_is_symbolic = eval {
1031                         command_oneline(qw/symbolic-ref -q HEAD/);
1032                 };
1033                 if ($new_is_symbolic) {
1034                         print "dcommitted the branch ", $head, "\n";
1035                 } else {
1036                         print "dcommitted on a detached HEAD because you gave ",
1037                               "a revision argument.\n",
1038                               "The rewritten commit is: ", $new_head, "\n";
1039                 }
1040                 command(['checkout', $old_head], STDERR => 0);
1041         }
1042
1043         unlink $gs->{index};
1044 }
1045
1046 sub cmd_branch {
1047         my ($branch_name, $head) = @_;
1048
1049         unless (defined $branch_name && length $branch_name) {
1050                 die(($_tag ? "tag" : "branch") . " name required\n");
1051         }
1052         $head ||= 'HEAD';
1053
1054         my (undef, $rev, undef, $gs) = working_head_info($head);
1055         my $src = $gs->full_pushurl;
1056
1057         my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1058         my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
1059         my $glob;
1060         if ($#{$allglobs} == 0) {
1061                 $glob = $allglobs->[0];
1062         } else {
1063                 unless(defined $_branch_dest) {
1064                         die "Multiple ",
1065                             $_tag ? "tag" : "branch",
1066                             " paths defined for Subversion repository.\n",
1067                             "You must specify where you want to create the ",
1068                             $_tag ? "tag" : "branch",
1069                             " with the --destination argument.\n";
1070                 }
1071                 foreach my $g (@{$allglobs}) {
1072                         my $re = Git::SVN::Editor::glob2pat($g->{path}->{left});
1073                         if ($_branch_dest =~ /$re/) {
1074                                 $glob = $g;
1075                                 last;
1076                         }
1077                 }
1078                 unless (defined $glob) {
1079                         my $dest_re = qr/\b\Q$_branch_dest\E\b/;
1080                         foreach my $g (@{$allglobs}) {
1081                                 $g->{path}->{left} =~ /$dest_re/ or next;
1082                                 if (defined $glob) {
1083                                         die "Ambiguous destination: ",
1084                                             $_branch_dest, "\nmatches both '",
1085                                             $glob->{path}->{left}, "' and '",
1086                                             $g->{path}->{left}, "'\n";
1087                                 }
1088                                 $glob = $g;
1089                         }
1090                         unless (defined $glob) {
1091                                 die "Unknown ",
1092                                     $_tag ? "tag" : "branch",
1093                                     " destination $_branch_dest\n";
1094                         }
1095                 }
1096         }
1097         my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
1098         my $url;
1099         if (defined $_commit_url) {
1100                 $url = $_commit_url;
1101         } else {
1102                 $url = eval { command_oneline('config', '--get',
1103                         "svn-remote.$gs->{repo_id}.commiturl") };
1104                 if (!$url) {
1105                         $url = $remote->{pushurl} || $remote->{url};
1106                 }
1107         }
1108         my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
1109
1110         if ($dst =~ /^https:/ && $src =~ /^http:/) {
1111                 $src=~s/^http:/https:/;
1112         }
1113
1114         ::_req_svn();
1115
1116         my $ctx = SVN::Client->new(
1117                 auth    => Git::SVN::Ra::_auth_providers(),
1118                 log_msg => sub {
1119                         ${ $_[0] } = defined $_message
1120                                 ? $_message
1121                                 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
1122                                 . $branch_name;
1123                 },
1124         );
1125
1126         eval {
1127                 $ctx->ls($dst, 'HEAD', 0);
1128         } and die "branch ${branch_name} already exists\n";
1129
1130         print "Copying ${src} at r${rev} to ${dst}...\n";
1131         $ctx->copy($src, $rev, $dst)
1132                 unless $_dry_run;
1133
1134         $gs->fetch_all;
1135 }
1136
1137 sub cmd_find_rev {
1138         my $revision_or_hash = shift or die "SVN or git revision required ",
1139                                             "as a command-line argument\n";
1140         my $result;
1141         if ($revision_or_hash =~ /^r\d+$/) {
1142                 my $head = shift;
1143                 $head ||= 'HEAD';
1144                 my @refs;
1145                 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
1146                 unless ($gs) {
1147                         die "Unable to determine upstream SVN information from ",
1148                             "$head history\n";
1149                 }
1150                 my $desired_revision = substr($revision_or_hash, 1);
1151                 $result = $gs->rev_map_get($desired_revision, $uuid);
1152         } else {
1153                 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
1154                 $result = $rev;
1155         }
1156         print "$result\n" if $result;
1157 }
1158
1159 sub auto_create_empty_directories {
1160         my ($gs) = @_;
1161         my $var = eval { command_oneline('config', '--get', '--bool',
1162                                          "svn-remote.$gs->{repo_id}.automkdirs") };
1163         # By default, create empty directories by consulting the unhandled log,
1164         # but allow setting it to 'false' to skip it.
1165         return !($var && $var eq 'false');
1166 }
1167
1168 sub cmd_rebase {
1169         command_noisy(qw/update-index --refresh/);
1170         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1171         unless ($gs) {
1172                 die "Unable to determine upstream SVN information from ",
1173                     "working tree history\n";
1174         }
1175         if ($_dry_run) {
1176                 print "Remote Branch: " . $gs->refname . "\n";
1177                 print "SVN URL: " . $url . "\n";
1178                 return;
1179         }
1180         if (command(qw/diff-index HEAD --/)) {
1181                 print STDERR "Cannot rebase with uncommited changes:\n";
1182                 command_noisy('status');
1183                 exit 1;
1184         }
1185         unless ($_local) {
1186                 # rebase will checkout for us, so no need to do it explicitly
1187                 $_no_checkout = 'true';
1188                 $_fetch_all ? $gs->fetch_all : $gs->fetch;
1189         }
1190         command_noisy(rebase_cmd(), $gs->refname);
1191         if (auto_create_empty_directories($gs)) {
1192                 $gs->mkemptydirs;
1193         }
1194 }
1195
1196 sub cmd_show_ignore {
1197         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1198         $gs ||= Git::SVN->new;
1199         my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1200         $gs->prop_walk($gs->{path}, $r, sub {
1201                 my ($gs, $path, $props) = @_;
1202                 print STDOUT "\n# $path\n";
1203                 my $s = $props->{'svn:ignore'} or return;
1204                 $s =~ s/[\r\n]+/\n/g;
1205                 $s =~ s/^\n+//;
1206                 chomp $s;
1207                 $s =~ s#^#$path#gm;
1208                 print STDOUT "$s\n";
1209         });
1210 }
1211
1212 sub cmd_show_externals {
1213         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1214         $gs ||= Git::SVN->new;
1215         my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1216         $gs->prop_walk($gs->{path}, $r, sub {
1217                 my ($gs, $path, $props) = @_;
1218                 print STDOUT "\n# $path\n";
1219                 my $s = $props->{'svn:externals'} or return;
1220                 $s =~ s/[\r\n]+/\n/g;
1221                 chomp $s;
1222                 $s =~ s#^#$path#gm;
1223                 print STDOUT "$s\n";
1224         });
1225 }
1226
1227 sub cmd_create_ignore {
1228         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1229         $gs ||= Git::SVN->new;
1230         my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1231         $gs->prop_walk($gs->{path}, $r, sub {
1232                 my ($gs, $path, $props) = @_;
1233                 # $path is of the form /path/to/dir/
1234                 $path = '.' . $path;
1235                 # SVN can have attributes on empty directories,
1236                 # which git won't track
1237                 mkpath([$path]) unless -d $path;
1238                 my $ignore = $path . '.gitignore';
1239                 my $s = $props->{'svn:ignore'} or return;
1240                 open(GITIGNORE, '>', $ignore)
1241                   or fatal("Failed to open `$ignore' for writing: $!");
1242                 $s =~ s/[\r\n]+/\n/g;
1243                 $s =~ s/^\n+//;
1244                 chomp $s;
1245                 # Prefix all patterns so that the ignore doesn't apply
1246                 # to sub-directories.
1247                 $s =~ s#^#/#gm;
1248                 print GITIGNORE "$s\n";
1249                 close(GITIGNORE)
1250                   or fatal("Failed to close `$ignore': $!");
1251                 command_noisy('add', '-f', $ignore);
1252         });
1253 }
1254
1255 sub cmd_mkdirs {
1256         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1257         $gs ||= Git::SVN->new;
1258         $gs->mkemptydirs($_revision);
1259 }
1260
1261 sub canonicalize_path {
1262         my ($path) = @_;
1263         my $dot_slash_added = 0;
1264         if (substr($path, 0, 1) ne "/") {
1265                 $path = "./" . $path;
1266                 $dot_slash_added = 1;
1267         }
1268         # File::Spec->canonpath doesn't collapse x/../y into y (for a
1269         # good reason), so let's do this manually.
1270         $path =~ s#/+#/#g;
1271         $path =~ s#/\.(?:/|$)#/#g;
1272         $path =~ s#/[^/]+/\.\.##g;
1273         $path =~ s#/$##g;
1274         $path =~ s#^\./## if $dot_slash_added;
1275         $path =~ s#^/##;
1276         $path =~ s#^\.$##;
1277         return $path;
1278 }
1279
1280 sub canonicalize_url {
1281         my ($url) = @_;
1282         $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
1283         return $url;
1284 }
1285
1286 # get_svnprops(PATH)
1287 # ------------------
1288 # Helper for cmd_propget and cmd_proplist below.
1289 sub get_svnprops {
1290         my $path = shift;
1291         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1292         $gs ||= Git::SVN->new;
1293
1294         # prefix THE PATH by the sub-directory from which the user
1295         # invoked us.
1296         $path = $cmd_dir_prefix . $path;
1297         fatal("No such file or directory: $path") unless -e $path;
1298         my $is_dir = -d $path ? 1 : 0;
1299         $path = $gs->{path} . '/' . $path;
1300
1301         # canonicalize the path (otherwise libsvn will abort or fail to
1302         # find the file)
1303         $path = canonicalize_path($path);
1304
1305         my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1306         my $props;
1307         if ($is_dir) {
1308                 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
1309         }
1310         else {
1311                 (undef, $props) = $gs->ra->get_file($path, $r, undef);
1312         }
1313         return $props;
1314 }
1315
1316 # cmd_propget (PROP, PATH)
1317 # ------------------------
1318 # Print the SVN property PROP for PATH.
1319 sub cmd_propget {
1320         my ($prop, $path) = @_;
1321         $path = '.' if not defined $path;
1322         usage(1) if not defined $prop;
1323         my $props = get_svnprops($path);
1324         if (not defined $props->{$prop}) {
1325                 fatal("`$path' does not have a `$prop' SVN property.");
1326         }
1327         print $props->{$prop} . "\n";
1328 }
1329
1330 # cmd_proplist (PATH)
1331 # -------------------
1332 # Print the list of SVN properties for PATH.
1333 sub cmd_proplist {
1334         my $path = shift;
1335         $path = '.' if not defined $path;
1336         my $props = get_svnprops($path);
1337         print "Properties on '$path':\n";
1338         foreach (sort keys %{$props}) {
1339                 print "  $_\n";
1340         }
1341 }
1342
1343 sub cmd_multi_init {
1344         my $url = shift;
1345         unless (defined $_trunk || @_branches || @_tags) {
1346                 usage(1);
1347         }
1348
1349         $_prefix = '' unless defined $_prefix;
1350         if (defined $url) {
1351                 $url = canonicalize_url($url);
1352                 init_subdir(@_);
1353         }
1354         do_git_init_db();
1355         if (defined $_trunk) {
1356                 $_trunk =~ s#^/+##;
1357                 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
1358                 # try both old-style and new-style lookups:
1359                 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
1360                 unless ($gs_trunk) {
1361                         my ($trunk_url, $trunk_path) =
1362                                               complete_svn_url($url, $_trunk);
1363                         $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
1364                                                    undef, $trunk_ref);
1365                 }
1366         }
1367         return unless @_branches || @_tags;
1368         my $ra = $url ? Git::SVN::Ra->new($url) : undef;
1369         foreach my $path (@_branches) {
1370                 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
1371         }
1372         foreach my $path (@_tags) {
1373                 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
1374         }
1375 }
1376
1377 sub cmd_multi_fetch {
1378         $Git::SVN::no_reuse_existing = undef;
1379         my $remotes = Git::SVN::read_all_remotes();
1380         foreach my $repo_id (sort keys %$remotes) {
1381                 if ($remotes->{$repo_id}->{url}) {
1382                         Git::SVN::fetch_all($repo_id, $remotes);
1383                 }
1384         }
1385 }
1386
1387 # this command is special because it requires no metadata
1388 sub cmd_commit_diff {
1389         my ($ta, $tb, $url) = @_;
1390         my $usage = "Usage: $0 commit-diff -r<revision> ".
1391                     "<tree-ish> <tree-ish> [<URL>]";
1392         fatal($usage) if (!defined $ta || !defined $tb);
1393         my $svn_path = '';
1394         if (!defined $url) {
1395                 my $gs = eval { Git::SVN->new };
1396                 if (!$gs) {
1397                         fatal("Needed URL or usable git-svn --id in ",
1398                               "the command-line\n", $usage);
1399                 }
1400                 $url = $gs->{url};
1401                 $svn_path = $gs->{path};
1402         }
1403         unless (defined $_revision) {
1404                 fatal("-r|--revision is a required argument\n", $usage);
1405         }
1406         if (defined $_message && defined $_file) {
1407                 fatal("Both --message/-m and --file/-F specified ",
1408                       "for the commit message.\n",
1409                       "I have no idea what you mean");
1410         }
1411         if (defined $_file) {
1412                 $_message = file_to_s($_file);
1413         } else {
1414                 $_message ||= get_commit_entry($tb)->{log};
1415         }
1416         my $ra ||= Git::SVN::Ra->new($url);
1417         my $r = $_revision;
1418         if ($r eq 'HEAD') {
1419                 $r = $ra->get_latest_revnum;
1420         } elsif ($r !~ /^\d+$/) {
1421                 die "revision argument: $r not understood by git-svn\n";
1422         }
1423         my %ed_opts = ( r => $r,
1424                         log => $_message,
1425                         ra => $ra,
1426                         tree_a => $ta,
1427                         tree_b => $tb,
1428                         editor_cb => sub { print "Committed r$_[0]\n" },
1429                         svn_path => $svn_path );
1430         if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
1431                 print "No changes\n$ta == $tb\n";
1432         }
1433 }
1434
1435 sub escape_uri_only {
1436         my ($uri) = @_;
1437         my @tmp;
1438         foreach (split m{/}, $uri) {
1439                 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
1440                 push @tmp, $_;
1441         }
1442         join('/', @tmp);
1443 }
1444
1445 sub escape_url {
1446         my ($url) = @_;
1447         if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1448                 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1449                 $url = "$scheme://$domain$uri";
1450         }
1451         $url;
1452 }
1453
1454 sub cmd_info {
1455         my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
1456         my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
1457         if (exists $_[1]) {
1458                 die "Too many arguments specified\n";
1459         }
1460
1461         my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1462
1463         if (!$file_type && !$diff_status) {
1464                 print STDERR "svn: '$path' is not under version control\n";
1465                 exit 1;
1466         }
1467
1468         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1469         unless ($gs) {
1470                 die "Unable to determine upstream SVN information from ",
1471                     "working tree history\n";
1472         }
1473
1474         # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1475         $path = "." if $path eq "";
1476
1477         my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
1478
1479         if ($_url) {
1480                 print escape_url($full_url), "\n";
1481                 return;
1482         }
1483
1484         my $result = "Path: $path\n";
1485         $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
1486         $result .= "URL: " . escape_url($full_url) . "\n";
1487
1488         eval {
1489                 my $repos_root = $gs->repos_root;
1490                 Git::SVN::remove_username($repos_root);
1491                 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
1492         };
1493         if ($@) {
1494                 $result .= "Repository Root: (offline)\n";
1495         }
1496         ::_req_svn();
1497         $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1498                 (::compare_svn_version('1.5.4') <= 0 || $file_type ne "dir");
1499         $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1500
1501         $result .= "Node Kind: " .
1502                    ($file_type eq "dir" ? "directory" : "file") . "\n";
1503
1504         my $schedule = $diff_status eq "A"
1505                        ? "add"
1506                        : ($diff_status eq "D" ? "delete" : "normal");
1507         $result .= "Schedule: $schedule\n";
1508
1509         if ($diff_status eq "A") {
1510                 print $result, "\n";
1511                 return;
1512         }
1513
1514         my ($lc_author, $lc_rev, $lc_date_utc);
1515         my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
1516         my $log = command_output_pipe(@args);
1517         my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1518         while (<$log>) {
1519                 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1520                         $lc_author = $1;
1521                         $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1522                 } elsif (/^${esc_color}    (git-svn-id:.+)$/o) {
1523                         (undef, $lc_rev, undef) = ::extract_metadata($1);
1524                 }
1525         }
1526         close $log;
1527
1528         Git::SVN::Log::set_local_timezone();
1529
1530         $result .= "Last Changed Author: $lc_author\n";
1531         $result .= "Last Changed Rev: $lc_rev\n";
1532         $result .= "Last Changed Date: " .
1533                    Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1534
1535         if ($file_type ne "dir") {
1536                 my $text_last_updated_date =
1537                     ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1538                 $result .=
1539                     "Text Last Updated: " .
1540                     Git::SVN::Log::format_svn_date($text_last_updated_date) .
1541                     "\n";
1542                 my $checksum;
1543                 if ($diff_status eq "D") {
1544                         my ($fh, $ctx) =
1545                             command_output_pipe(qw(cat-file blob), "HEAD:$path");
1546                         if ($file_type eq "link") {
1547                                 my $file_name = <$fh>;
1548                                 $checksum = md5sum("link $file_name");
1549                         } else {
1550                                 $checksum = md5sum($fh);
1551                         }
1552                         command_close_pipe($fh, $ctx);
1553                 } elsif ($file_type eq "link") {
1554                         my $file_name =
1555                             command(qw(cat-file blob), "HEAD:$path");
1556                         $checksum =
1557                             md5sum("link " . $file_name);
1558                 } else {
1559                         open FILE, "<", $path or die $!;
1560                         $checksum = md5sum(\*FILE);
1561                         close FILE or die $!;
1562                 }
1563                 $result .= "Checksum: " . $checksum . "\n";
1564         }
1565
1566         print $result, "\n";
1567 }
1568
1569 sub cmd_reset {
1570         my $target = shift || $_revision or die "SVN revision required\n";
1571         $target = $1 if $target =~ /^r(\d+)$/;
1572         $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1573         my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1574         unless ($gs) {
1575                 die "Unable to determine upstream SVN information from ".
1576                     "history\n";
1577         }
1578         my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1579         die "Cannot find SVN revision $target\n" unless defined($c);
1580         $gs->rev_map_set($r, $c, 'reset', $uuid);
1581         print "r$r = $c ($gs->{ref_id})\n";
1582 }
1583
1584 sub cmd_gc {
1585         if (!can_compress()) {
1586                 warn "Compress::Zlib could not be found; unhandled.log " .
1587                      "files will not be compressed.\n";
1588         }
1589         find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1590 }
1591
1592 ########################### utility functions #########################
1593
1594 sub rebase_cmd {
1595         my @cmd = qw/rebase/;
1596         push @cmd, '-v' if $_verbose;
1597         push @cmd, qw/--merge/ if $_merge;
1598         push @cmd, "--strategy=$_strategy" if $_strategy;
1599         push @cmd, "--preserve-merges" if $_preserve_merges;
1600         @cmd;
1601 }
1602
1603 sub post_fetch_checkout {
1604         return if $_no_checkout;
1605         return if verify_ref('HEAD^0');
1606         my $gs = $Git::SVN::_head or return;
1607
1608         # look for "trunk" ref if it exists
1609         my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1610         my $fetch = $remote->{fetch};
1611         if ($fetch) {
1612                 foreach my $p (keys %$fetch) {
1613                         basename($fetch->{$p}) eq 'trunk' or next;
1614                         $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1615                         last;
1616                 }
1617         }
1618
1619         command_noisy(qw(update-ref HEAD), $gs->refname);
1620         return unless verify_ref('HEAD^0');
1621
1622         return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1623         my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1624         return if -f $index;
1625
1626         return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
1627         return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1628         command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1629         print STDERR "Checked out HEAD:\n  ",
1630                      $gs->full_url, " r", $gs->last_rev, "\n";
1631         if (auto_create_empty_directories($gs)) {
1632                 $gs->mkemptydirs($gs->last_rev);
1633         }
1634 }
1635
1636 sub complete_svn_url {
1637         my ($url, $path) = @_;
1638         $path =~ s#/+$##;
1639         if ($path !~ m#^[a-z\+]+://#) {
1640                 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1641                         fatal("E: '$path' is not a complete URL ",
1642                               "and a separate URL is not specified");
1643                 }
1644                 return ($url, $path);
1645         }
1646         return ($path, '');
1647 }
1648
1649 sub complete_url_ls_init {
1650         my ($ra, $repo_path, $switch, $pfx) = @_;
1651         unless ($repo_path) {
1652                 print STDERR "W: $switch not specified\n";
1653                 return;
1654         }
1655         $repo_path =~ s#/+$##;
1656         if ($repo_path =~ m#^[a-z\+]+://#) {
1657                 $ra = Git::SVN::Ra->new($repo_path);
1658                 $repo_path = '';
1659         } else {
1660                 $repo_path =~ s#^/+##;
1661                 unless ($ra) {
1662                         fatal("E: '$repo_path' is not a complete URL ",
1663                               "and a separate URL is not specified");
1664                 }
1665         }
1666         my $url = $ra->{url};
1667         my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1668         my $k = "svn-remote.$gs->{repo_id}.url";
1669         my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1670         if ($orig_url && ($orig_url ne $gs->{url})) {
1671                 die "$k already set: $orig_url\n",
1672                     "wanted to set to: $gs->{url}\n";
1673         }
1674         command_oneline('config', $k, $gs->{url}) unless $orig_url;
1675         my $remote_path = "$gs->{path}/$repo_path";
1676         $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
1677         $remote_path =~ s#/+#/#g;
1678         $remote_path =~ s#^/##g;
1679         $remote_path .= "/*" if $remote_path !~ /\*/;
1680         my ($n) = ($switch =~ /^--(\w+)/);
1681         if (length $pfx && $pfx !~ m#/$#) {
1682                 die "--prefix='$pfx' must have a trailing slash '/'\n";
1683         }
1684         command_noisy('config',
1685                       '--add',
1686                       "svn-remote.$gs->{repo_id}.$n",
1687                       "$remote_path:refs/remotes/$pfx*" .
1688                         ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
1689 }
1690
1691 sub verify_ref {
1692         my ($ref) = @_;
1693         eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1694                                { STDERR => 0 }); };
1695 }
1696
1697 sub get_tree_from_treeish {
1698         my ($treeish) = @_;
1699         # $treeish can be a symbolic ref, too:
1700         my $type = command_oneline(qw/cat-file -t/, $treeish);
1701         my $expected;
1702         while ($type eq 'tag') {
1703                 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
1704         }
1705         if ($type eq 'commit') {
1706                 $expected = (grep /^tree /, command(qw/cat-file commit/,
1707                                                     $treeish))[0];
1708                 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1709                 die "Unable to get tree from $treeish\n" unless $expected;
1710         } elsif ($type eq 'tree') {
1711                 $expected = $treeish;
1712         } else {
1713                 die "$treeish is a $type, expected tree, tag or commit\n";
1714         }
1715         return $expected;
1716 }
1717
1718 sub get_commit_entry {
1719         my ($treeish) = shift;
1720         my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1721         my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1722         my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1723         open my $log_fh, '>', $commit_editmsg or croak $!;
1724
1725         my $type = command_oneline(qw/cat-file -t/, $treeish);
1726         if ($type eq 'commit' || $type eq 'tag') {
1727                 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
1728                                                          $type, $treeish);
1729                 my $in_msg = 0;
1730                 my $author;
1731                 my $saw_from = 0;
1732                 my $msgbuf = "";
1733                 while (<$msg_fh>) {
1734                         if (!$in_msg) {
1735                                 $in_msg = 1 if (/^\s*$/);
1736                                 $author = $1 if (/^author (.*>)/);
1737                         } elsif (/^git-svn-id: /) {
1738                                 # skip this for now, we regenerate the
1739                                 # correct one on re-fetch anyways
1740                                 # TODO: set *:merge properties or like...
1741                         } else {
1742                                 if (/^From:/ || /^Signed-off-by:/) {
1743                                         $saw_from = 1;
1744                                 }
1745                                 $msgbuf .= $_;
1746                         }
1747                 }
1748                 $msgbuf =~ s/\s+$//s;
1749                 if ($Git::SVN::_add_author_from && defined($author)
1750                     && !$saw_from) {
1751                         $msgbuf .= "\n\nFrom: $author";
1752                 }
1753                 print $log_fh $msgbuf or croak $!;
1754                 command_close_pipe($msg_fh, $ctx);
1755         }
1756         close $log_fh or croak $!;
1757
1758         if ($_edit || ($type eq 'tree')) {
1759                 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1760                 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
1761         }
1762         rename $commit_editmsg, $commit_msg or croak $!;
1763         {
1764                 require Encode;
1765                 # SVN requires messages to be UTF-8 when entering the repo
1766                 local $/;
1767                 open $log_fh, '<', $commit_msg or croak $!;
1768                 binmode $log_fh;
1769                 chomp($log_entry{log} = <$log_fh>);
1770
1771                 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1772                 my $msg = $log_entry{log};
1773
1774                 eval { $msg = Encode::decode($enc, $msg, 1) };
1775                 if ($@) {
1776                         die "Could not decode as $enc:\n", $msg,
1777                             "\nPerhaps you need to set i18n.commitencoding\n";
1778                 }
1779
1780                 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1781                 die "Could not encode as UTF-8:\n$msg\n" if $@;
1782
1783                 $log_entry{log} = $msg;
1784
1785                 close $log_fh or croak $!;
1786         }
1787         unlink $commit_msg;
1788         \%log_entry;
1789 }
1790
1791 sub s_to_file {
1792         my ($str, $file, $mode) = @_;
1793         open my $fd,'>',$file or croak $!;
1794         print $fd $str,"\n" or croak $!;
1795         close $fd or croak $!;
1796         chmod ($mode &~ umask, $file) if (defined $mode);
1797 }
1798
1799 sub file_to_s {
1800         my $file = shift;
1801         open my $fd,'<',$file or croak "$!: file: $file\n";
1802         local $/;
1803         my $ret = <$fd>;
1804         close $fd or croak $!;
1805         $ret =~ s/\s*$//s;
1806         return $ret;
1807 }
1808
1809 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1810 sub load_authors {
1811         open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1812         my $log = $cmd eq 'log';
1813         while (<$authors>) {
1814                 chomp;
1815                 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1816                 my ($user, $name, $email) = ($1, $2, $3);
1817                 if ($log) {
1818                         $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1819                 } else {
1820                         $users{$user} = [$name, $email];
1821                 }
1822         }
1823         close $authors or croak $!;
1824 }
1825
1826 # convert GetOpt::Long specs for use by git-config
1827 sub read_git_config {
1828         my $opts = shift;
1829         my @config_only;
1830         foreach my $o (keys %$opts) {
1831                 # if we have mixedCase and a long option-only, then
1832                 # it's a config-only variable that we don't need for
1833                 # the command-line.
1834                 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1835                 my $v = $opts->{$o};
1836                 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1837                 $key =~ s/-//g;
1838                 my $arg = 'git config';
1839                 $arg .= ' --int' if ($o =~ /[:=]i$/);
1840                 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1841                 if (ref $v eq 'ARRAY') {
1842                         chomp(my @tmp = `$arg --get-all svn.$key`);
1843                         @$v = @tmp if @tmp;
1844                 } else {
1845                         chomp(my $tmp = `$arg --get svn.$key`);
1846                         if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1847                                 $$v = $tmp;
1848                         }
1849                 }
1850         }
1851         delete @$opts{@config_only} if @config_only;
1852 }
1853
1854 sub extract_metadata {
1855         my $id = shift or return (undef, undef, undef);
1856         my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
1857                                                         \s([a-f\d\-]+)$/ix);
1858         if (!defined $rev || !$uuid || !$url) {
1859                 # some of the original repositories I made had
1860                 # identifiers like this:
1861                 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
1862         }
1863         return ($url, $rev, $uuid);
1864 }
1865
1866 sub cmt_metadata {
1867         return extract_metadata((grep(/^git-svn-id: /,
1868                 command(qw/cat-file commit/, shift)))[-1]);
1869 }
1870
1871 sub cmt_sha2rev_batch {
1872         my %s2r;
1873         my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1874         my $list = shift;
1875
1876         foreach my $sha (@{$list}) {
1877                 my $first = 1;
1878                 my $size = 0;
1879                 print $out $sha, "\n";
1880
1881                 while (my $line = <$in>) {
1882                         if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1883                                 last;
1884                         } elsif ($first &&
1885                                $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1886                                 $first = 0;
1887                                 $size = $1;
1888                                 next;
1889                         } elsif ($line =~ /^(git-svn-id: )/) {
1890                                 my (undef, $rev, undef) =
1891                                                       extract_metadata($line);
1892                                 $s2r{$sha} = $rev;
1893                         }
1894
1895                         $size -= length($line);
1896                         last if ($size == 0);
1897                 }
1898         }
1899
1900         command_close_bidi_pipe($pid, $in, $out, $ctx);
1901
1902         return \%s2r;
1903 }
1904
1905 sub working_head_info {
1906         my ($head, $refs) = @_;
1907         my @args = qw/rev-list --first-parent --pretty=medium/;
1908         my ($fh, $ctx) = command_output_pipe(@args, $head);
1909         my $hash;
1910         my %max;
1911         while (<$fh>) {
1912                 if ( m{^commit ($::sha1)$} ) {
1913                         unshift @$refs, $hash if $hash and $refs;
1914                         $hash = $1;
1915                         next;
1916                 }
1917                 next unless s{^\s*(git-svn-id:)}{$1};
1918                 my ($url, $rev, $uuid) = extract_metadata($_);
1919                 if (defined $url && defined $rev) {
1920                         next if $max{$url} and $max{$url} < $rev;
1921                         if (my $gs = Git::SVN->find_by_url($url)) {
1922                                 my $c = $gs->rev_map_get($rev, $uuid);
1923                                 if ($c && $c eq $hash) {
1924                                         close $fh; # break the pipe
1925                                         return ($url, $rev, $uuid, $gs);
1926                                 } else {
1927                                         $max{$url} ||= $gs->rev_map_max;
1928                                 }
1929                         }
1930                 }
1931         }
1932         command_close_pipe($fh, $ctx);
1933         (undef, undef, undef, undef);
1934 }
1935
1936 sub read_commit_parents {
1937         my ($parents, $c) = @_;
1938         chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1939         $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1940         @{$parents->{$c}} = split(/ /, $p);
1941 }
1942
1943 sub linearize_history {
1944         my ($gs, $refs) = @_;
1945         my %parents;
1946         foreach my $c (@$refs) {
1947                 read_commit_parents(\%parents, $c);
1948         }
1949
1950         my @linear_refs;
1951         my %skip = ();
1952         my $last_svn_commit = $gs->last_commit;
1953         foreach my $c (reverse @$refs) {
1954                 next if $c eq $last_svn_commit;
1955                 last if $skip{$c};
1956
1957                 unshift @linear_refs, $c;
1958                 $skip{$c} = 1;
1959
1960                 # we only want the first parent to diff against for linear
1961                 # history, we save the rest to inject when we finalize the
1962                 # svn commit
1963                 my $fp_a = verify_ref("$c~1");
1964                 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1965                 if (!$fp_a || !$fp_b) {
1966                         die "Commit $c\n",
1967                             "has no parent commit, and therefore ",
1968                             "nothing to diff against.\n",
1969                             "You should be working from a repository ",
1970                             "originally created by git-svn\n";
1971                 }
1972                 if ($fp_a ne $fp_b) {
1973                         die "$c~1 = $fp_a, however parsing commit $c ",
1974                             "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1975                 }
1976
1977                 foreach my $p (@{$parents{$c}}) {
1978                         $skip{$p} = 1;
1979                 }
1980         }
1981         (\@linear_refs, \%parents);
1982 }
1983
1984 sub find_file_type_and_diff_status {
1985         my ($path) = @_;
1986         return ('dir', '') if $path eq '';
1987
1988         my $diff_output =
1989             command_oneline(qw(diff --cached --name-status --), $path) || "";
1990         my $diff_status = (split(' ', $diff_output))[0] || "";
1991
1992         my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1993
1994         return (undef, undef) if !$diff_status && !$ls_tree;
1995
1996         if ($diff_status eq "A") {
1997                 return ("link", $diff_status) if -l $path;
1998                 return ("dir", $diff_status) if -d $path;
1999                 return ("file", $diff_status);
2000         }
2001
2002         my $mode = (split(' ', $ls_tree))[0] || "";
2003
2004         return ("link", $diff_status) if $mode eq "120000";
2005         return ("dir", $diff_status) if $mode eq "040000";
2006         return ("file", $diff_status);
2007 }
2008
2009 sub md5sum {
2010         my $arg = shift;
2011         my $ref = ref $arg;
2012         my $md5 = Digest::MD5->new();
2013         if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
2014                 $md5->addfile($arg) or croak $!;
2015         } elsif ($ref eq 'SCALAR') {
2016                 $md5->add($$arg) or croak $!;
2017         } elsif (!$ref) {
2018                 $md5->add($arg) or croak $!;
2019         } else {
2020                 fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
2021         }
2022         return $md5->hexdigest();
2023 }
2024
2025 sub gc_directory {
2026         if (can_compress() && -f $_ && basename($_) eq "unhandled.log") {
2027                 my $out_filename = $_ . ".gz";
2028                 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
2029                 binmode $in_fh;
2030                 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
2031                                 die "Unable to open $out_filename: $!\n";
2032
2033                 my $res;
2034                 while ($res = sysread($in_fh, my $str, 1024)) {
2035                         $gz->gzwrite($str) or
2036                                 die "Unable to write: ".$gz->gzerror()."!\n";
2037                 }
2038                 unlink $_ or die "unlink $File::Find::name: $!\n";
2039         } elsif (-f $_ && basename($_) eq "index") {
2040                 unlink $_ or die "unlink $_: $!\n";
2041         }
2042 }
2043
2044 {
2045 package Git::SVN;
2046 use strict;
2047 use warnings;
2048 use Fcntl qw/:DEFAULT :seek/;
2049 use constant rev_map_fmt => 'NH40';
2050 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
2051             $_repack $_repack_flags $_use_svm_props $_head
2052             $_use_svnsync_props $no_reuse_existing $_minimize_url
2053             $_use_log_author $_add_author_from $_localtime/;
2054 use Carp qw/croak/;
2055 use File::Path qw/mkpath/;
2056 use File::Copy qw/copy/;
2057 use IPC::Open3;
2058 use Time::Local;
2059 use Memoize;  # core since 5.8.0, Jul 2002
2060 use Memoize::Storable;
2061 use POSIX qw(:signal_h);
2062
2063 use Git qw(
2064     command
2065     command_oneline
2066     command_noisy
2067     command_output_pipe
2068     command_close_pipe
2069 );
2070 use Git::SVN::Utils qw(fatal can_compress);
2071
2072 my $can_use_yaml;
2073 BEGIN {
2074         $can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
2075 }
2076
2077 my ($_gc_nr, $_gc_period);
2078
2079 # properties that we do not log:
2080 my %SKIP_PROP;
2081 BEGIN {
2082         %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
2083                                         svn:special svn:executable
2084                                         svn:entry:committed-rev
2085                                         svn:entry:last-author
2086                                         svn:entry:uuid
2087                                         svn:entry:committed-date/;
2088
2089         # some options are read globally, but can be overridden locally
2090         # per [svn-remote "..."] section.  Command-line options will *NOT*
2091         # override options set in an [svn-remote "..."] section
2092         no strict 'refs';
2093         for my $option (qw/follow_parent no_metadata use_svm_props
2094                            use_svnsync_props/) {
2095                 my $key = $option;
2096                 $key =~ tr/_//d;
2097                 my $prop = "-$option";
2098                 *$option = sub {
2099                         my ($self) = @_;
2100                         return $self->{$prop} if exists $self->{$prop};
2101                         my $k = "svn-remote.$self->{repo_id}.$key";
2102                         eval { command_oneline(qw/config --get/, $k) };
2103                         if ($@) {
2104                                 $self->{$prop} = ${"Git::SVN::_$option"};
2105                         } else {
2106                                 my $v = command_oneline(qw/config --bool/,$k);
2107                                 $self->{$prop} = $v eq 'false' ? 0 : 1;
2108                         }
2109                         return $self->{$prop};
2110                 }
2111         }
2112 }
2113
2114
2115 my (%LOCKFILES, %INDEX_FILES);
2116 END {
2117         unlink keys %LOCKFILES if %LOCKFILES;
2118         unlink keys %INDEX_FILES if %INDEX_FILES;
2119 }
2120
2121 sub resolve_local_globs {
2122         my ($url, $fetch, $glob_spec) = @_;
2123         return unless defined $glob_spec;
2124         my $ref = $glob_spec->{ref};
2125         my $path = $glob_spec->{path};
2126         foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
2127                 next unless m#^$ref->{regex}$#;
2128                 my $p = $1;
2129                 my $pathname = desanitize_refname($path->full_path($p));
2130                 my $refname = desanitize_refname($ref->full_path($p));
2131                 if (my $existing = $fetch->{$pathname}) {
2132                         if ($existing ne $refname) {
2133                                 die "Refspec conflict:\n",
2134                                     "existing: $existing\n",
2135                                     " globbed: $refname\n";
2136                         }
2137                         my $u = (::cmt_metadata("$refname"))[0];
2138                         $u =~ s!^\Q$url\E(/|$)!! or die
2139                           "$refname: '$url' not found in '$u'\n";
2140                         if ($pathname ne $u) {
2141                                 warn "W: Refspec glob conflict ",
2142                                      "(ref: $refname):\n",
2143                                      "expected path: $pathname\n",
2144                                      "    real path: $u\n",
2145                                      "Continuing ahead with $u\n";
2146                                 next;
2147                         }
2148                 } else {
2149                         $fetch->{$pathname} = $refname;
2150                 }
2151         }
2152 }
2153
2154 sub parse_revision_argument {
2155         my ($base, $head) = @_;
2156         if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
2157                 return ($base, $head);
2158         }
2159         return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
2160         return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
2161         return ($head, $head) if ($::_revision eq 'HEAD');
2162         return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
2163         return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
2164         die "revision argument: $::_revision not understood by git-svn\n";
2165 }
2166
2167 sub fetch_all {
2168         my ($repo_id, $remotes) = @_;
2169         if (ref $repo_id) {
2170                 my $gs = $repo_id;
2171                 $repo_id = undef;
2172                 $repo_id = $gs->{repo_id};
2173         }
2174         $remotes ||= read_all_remotes();
2175         my $remote = $remotes->{$repo_id} or
2176                      die "[svn-remote \"$repo_id\"] unknown\n";
2177         my $fetch = $remote->{fetch};
2178         my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
2179         my (@gs, @globs);
2180         my $ra = Git::SVN::Ra->new($url);
2181         my $uuid = $ra->get_uuid;
2182         my $head = $ra->get_latest_revnum;
2183
2184         # ignore errors, $head revision may not even exist anymore
2185         eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
2186         warn "W: $@\n" if $@;
2187
2188         my $base = defined $fetch ? $head : 0;
2189
2190         # read the max revs for wildcard expansion (branches/*, tags/*)
2191         foreach my $t (qw/branches tags/) {
2192                 defined $remote->{$t} or next;
2193                 push @globs, @{$remote->{$t}};
2194
2195                 my $max_rev = eval { tmp_config(qw/--int --get/,
2196                                          "svn-remote.$repo_id.${t}-maxRev") };
2197                 if (defined $max_rev && ($max_rev < $base)) {
2198                         $base = $max_rev;
2199                 } elsif (!defined $max_rev) {
2200                         $base = 0;
2201                 }
2202         }
2203
2204         if ($fetch) {
2205                 foreach my $p (sort keys %$fetch) {
2206                         my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
2207                         my $lr = $gs->rev_map_max;
2208                         if (defined $lr) {
2209                                 $base = $lr if ($lr < $base);
2210                         }
2211                         push @gs, $gs;
2212                 }
2213         }
2214
2215         ($base, $head) = parse_revision_argument($base, $head);
2216         $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
2217 }
2218
2219 sub read_all_remotes {
2220         my $r = {};
2221         my $use_svm_props = eval { command_oneline(qw/config --bool
2222             svn.useSvmProps/) };
2223         $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
2224         my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
2225         foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
2226                 if (m!^(.+)\.fetch=$svn_refspec$!) {
2227                         my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
2228                         die("svn-remote.$remote: remote ref '$remote_ref' "
2229                             . "must start with 'refs/'\n")
2230                                 unless $remote_ref =~ m{^refs/};
2231                         $local_ref = uri_decode($local_ref);
2232                         $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
2233                         $r->{$remote}->{svm} = {} if $use_svm_props;
2234                 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
2235                         $r->{$1}->{svm} = {};
2236                 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
2237                         $r->{$1}->{url} = $2;
2238                 } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
2239                         $r->{$1}->{pushurl} = $2;
2240                 } elsif (m!^(.+)\.ignore-refs=\s*(.*)\s*$!) {
2241                         $r->{$1}->{ignore_refs_regex} = $2;
2242                 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
2243                         my ($remote, $t, $local_ref, $remote_ref) =
2244                                                              ($1, $2, $3, $4);
2245                         die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
2246                             . "must start with 'refs/'\n")
2247                                 unless $remote_ref =~ m{^refs/};
2248                         $local_ref = uri_decode($local_ref);
2249                         my $rs = {
2250                             t => $t,
2251                             remote => $remote,
2252                             path => Git::SVN::GlobSpec->new($local_ref, 1),
2253                             ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
2254                         if (length($rs->{ref}->{right}) != 0) {
2255                                 die "The '*' glob character must be the last ",
2256                                     "character of '$remote_ref'\n";
2257                         }
2258                         push @{ $r->{$remote}->{$t} }, $rs;
2259                 }
2260         }
2261
2262         map {
2263                 if (defined $r->{$_}->{svm}) {
2264                         my $svm;
2265                         eval {
2266                                 my $section = "svn-remote.$_";
2267                                 $svm = {
2268                                         source => tmp_config('--get',
2269                                             "$section.svm-source"),
2270                                         replace => tmp_config('--get',
2271                                             "$section.svm-replace"),
2272                                 }
2273                         };
2274                         $r->{$_}->{svm} = $svm;
2275                 }
2276         } keys %$r;
2277
2278         foreach my $remote (keys %$r) {
2279                 foreach ( grep { defined $_ }
2280                           map { $r->{$remote}->{$_} } qw(branches tags) ) {
2281                         foreach my $rs ( @$_ ) {
2282                                 $rs->{ignore_refs_regex} =
2283                                     $r->{$remote}->{ignore_refs_regex};
2284                         }
2285                 }
2286         }
2287
2288         $r;
2289 }
2290
2291 sub init_vars {
2292         $_gc_nr = $_gc_period = 1000;
2293         if (defined $_repack || defined $_repack_flags) {
2294                warn "Repack options are obsolete; they have no effect.\n";
2295         }
2296 }
2297
2298 sub verify_remotes_sanity {
2299         return unless -d $ENV{GIT_DIR};
2300         my %seen;
2301         foreach (command(qw/config -l/)) {
2302                 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
2303                         if ($seen{$1}) {
2304                                 die "Remote ref refs/remote/$1 is tracked by",
2305                                     "\n  \"$_\"\nand\n  \"$seen{$1}\"\n",
2306                                     "Please resolve this ambiguity in ",
2307                                     "your git configuration file before ",
2308                                     "continuing\n";
2309                         }
2310                         $seen{$1} = $_;
2311                 }
2312         }
2313 }
2314
2315 sub find_existing_remote {
2316         my ($url, $remotes) = @_;
2317         return undef if $no_reuse_existing;
2318         my $existing;
2319         foreach my $repo_id (keys %$remotes) {
2320                 my $u = $remotes->{$repo_id}->{url} or next;
2321                 next if $u ne $url;
2322                 $existing = $repo_id;
2323                 last;
2324         }
2325         $existing;
2326 }
2327
2328 sub init_remote_config {
2329         my ($self, $url, $no_write) = @_;
2330         $url =~ s!/+$!!; # strip trailing slash
2331         my $r = read_all_remotes();
2332         my $existing = find_existing_remote($url, $r);
2333         if ($existing) {
2334                 unless ($no_write) {
2335                         print STDERR "Using existing ",
2336                                      "[svn-remote \"$existing\"]\n";
2337                 }
2338                 $self->{repo_id} = $existing;
2339         } elsif ($_minimize_url) {
2340                 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
2341                 $existing = find_existing_remote($min_url, $r);
2342                 if ($existing) {
2343                         unless ($no_write) {
2344                                 print STDERR "Using existing ",
2345                                              "[svn-remote \"$existing\"]\n";
2346                         }
2347                         $self->{repo_id} = $existing;
2348                 }
2349                 if ($min_url ne $url) {
2350                         unless ($no_write) {
2351                                 print STDERR "Using higher level of URL: ",
2352                                              "$url => $min_url\n";
2353                         }
2354                         my $old_path = $self->{path};
2355                         $self->{path} = $url;
2356                         $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
2357                         if (length $old_path) {
2358                                 $self->{path} .= "/$old_path";
2359                         }
2360                         $url = $min_url;
2361                 }
2362         }
2363         my $orig_url;
2364         if (!$existing) {
2365                 # verify that we aren't overwriting anything:
2366                 $orig_url = eval {
2367                         command_oneline('config', '--get',
2368                                         "svn-remote.$self->{repo_id}.url")
2369                 };
2370                 if ($orig_url && ($orig_url ne $url)) {
2371                         die "svn-remote.$self->{repo_id}.url already set: ",
2372                             "$orig_url\nwanted to set to: $url\n";
2373                 }
2374         }
2375         my ($xrepo_id, $xpath) = find_ref($self->refname);
2376         if (!$no_write && defined $xpath) {
2377                 die "svn-remote.$xrepo_id.fetch already set to track ",
2378                     "$xpath:", $self->refname, "\n";
2379         }
2380         unless ($no_write) {
2381                 command_noisy('config',
2382                               "svn-remote.$self->{repo_id}.url", $url);
2383                 $self->{path} =~ s{^/}{};
2384                 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
2385                 command_noisy('config', '--add',
2386                               "svn-remote.$self->{repo_id}.fetch",
2387                               "$self->{path}:".$self->refname);
2388         }
2389         $self->{url} = $url;
2390 }
2391
2392 sub find_by_url { # repos_root and, path are optional
2393         my ($class, $full_url, $repos_root, $path) = @_;
2394
2395         return undef unless defined $full_url;
2396         remove_username($full_url);
2397         remove_username($repos_root) if defined $repos_root;
2398         my $remotes = read_all_remotes();
2399         if (defined $full_url && defined $repos_root && !defined $path) {
2400                 $path = $full_url;
2401                 $path =~ s#^\Q$repos_root\E(?:/|$)##;
2402         }
2403         foreach my $repo_id (keys %$remotes) {
2404                 my $u = $remotes->{$repo_id}->{url} or next;
2405                 remove_username($u);
2406                 next if defined $repos_root && $repos_root ne $u;
2407
2408                 my $fetch = $remotes->{$repo_id}->{fetch} || {};
2409                 foreach my $t (qw/branches tags/) {
2410                         foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
2411                                 resolve_local_globs($u, $fetch, $globspec);
2412                         }
2413                 }
2414                 my $p = $path;
2415                 my $rwr = rewrite_root({repo_id => $repo_id});
2416                 my $svm = $remotes->{$repo_id}->{svm}
2417                         if defined $remotes->{$repo_id}->{svm};
2418                 unless (defined $p) {
2419                         $p = $full_url;
2420                         my $z = $u;
2421                         my $prefix = '';
2422                         if ($rwr) {
2423                                 $z = $rwr;
2424                                 remove_username($z);
2425                         } elsif (defined $svm) {
2426                                 $z = $svm->{source};
2427                                 $prefix = $svm->{replace};
2428                                 $prefix =~ s#^\Q$u\E(?:/|$)##;
2429                                 $prefix =~ s#/$##;
2430                         }
2431                         $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
2432                 }
2433                 foreach my $f (keys %$fetch) {
2434                         next if $f ne $p;
2435                         return Git::SVN->new($fetch->{$f}, $repo_id, $f);
2436                 }
2437         }
2438         undef;
2439 }
2440
2441 sub init {
2442         my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
2443         my $self = _new($class, $repo_id, $ref_id, $path);
2444         if (defined $url) {
2445                 $self->init_remote_config($url, $no_write);
2446         }
2447         $self;
2448 }
2449
2450 sub find_ref {
2451         my ($ref_id) = @_;
2452         foreach (command(qw/config -l/)) {
2453                 next unless m!^svn-remote\.(.+)\.fetch=
2454                               \s*(.*?)\s*:\s*(.+?)\s*$!x;
2455                 my ($repo_id, $path, $ref) = ($1, $2, $3);
2456                 if ($ref eq $ref_id) {
2457                         $path = '' if ($path =~ m#^\./?#);
2458                         return ($repo_id, $path);
2459                 }
2460         }
2461         (undef, undef, undef);
2462 }
2463
2464 sub new {
2465         my ($class, $ref_id, $repo_id, $path) = @_;
2466         if (defined $ref_id && !defined $repo_id && !defined $path) {
2467                 ($repo_id, $path) = find_ref($ref_id);
2468                 if (!defined $repo_id) {
2469                         die "Could not find a \"svn-remote.*.fetch\" key ",
2470                             "in the repository configuration matching: ",
2471                             "$ref_id\n";
2472                 }
2473         }
2474         my $self = _new($class, $repo_id, $ref_id, $path);
2475         if (!defined $self->{path} || !length $self->{path}) {
2476                 my $fetch = command_oneline('config', '--get',
2477                                             "svn-remote.$repo_id.fetch",
2478                                             ":$ref_id\$") or
2479                      die "Failed to read \"svn-remote.$repo_id.fetch\" ",
2480                          "\":$ref_id\$\" in config\n";
2481                 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2482         }
2483         $self->{path} =~ s{/+}{/}g;
2484         $self->{path} =~ s{\A/}{};
2485         $self->{path} =~ s{/\z}{};
2486         $self->{url} = command_oneline('config', '--get',
2487                                        "svn-remote.$repo_id.url") or
2488                   die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
2489         $self->{pushurl} = eval { command_oneline('config', '--get',
2490                                   "svn-remote.$repo_id.pushurl") };
2491         $self->rebuild;
2492         $self;
2493 }
2494
2495 sub refname {
2496         my ($refname) = $_[0]->{ref_id} ;
2497
2498         # It cannot end with a slash /, we'll throw up on this because
2499         # SVN can't have directories with a slash in their name, either:
2500         if ($refname =~ m{/$}) {
2501                 die "ref: '$refname' ends with a trailing slash, this is ",
2502                     "not permitted by git nor Subversion\n";
2503         }
2504
2505         # It cannot have ASCII control character space, tilde ~, caret ^,
2506         # colon :, question-mark ?, asterisk *, space, or open bracket [
2507         # anywhere.
2508         #
2509         # Additionally, % must be escaped because it is used for escaping
2510         # and we want our escaped refname to be reversible
2511         $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2512
2513         # no slash-separated component can begin with a dot .
2514         # /.* becomes /%2E*
2515         $refname =~ s{/\.}{/%2E}g;
2516
2517         # It cannot have two consecutive dots .. anywhere
2518         # .. becomes %2E%2E
2519         $refname =~ s{\.\.}{%2E%2E}g;
2520
2521         # trailing dots and .lock are not allowed
2522         # .$ becomes %2E and .lock becomes %2Elock
2523         $refname =~ s{\.(?=$|lock$)}{%2E};
2524
2525         # the sequence @{ is used to access the reflog
2526         # @{ becomes %40{
2527         $refname =~ s{\@\{}{%40\{}g;
2528
2529         return $refname;
2530 }
2531
2532 sub desanitize_refname {
2533         my ($refname) = @_;
2534         $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2535         return $refname;
2536 }
2537
2538 sub svm_uuid {
2539         my ($self) = @_;
2540         return $self->{svm}->{uuid} if $self->svm;
2541         $self->ra;
2542         unless ($self->{svm}) {
2543                 die "SVM UUID not cached, and reading remotely failed\n";
2544         }
2545         $self->{svm}->{uuid};
2546 }
2547
2548 sub svm {
2549         my ($self) = @_;
2550         return $self->{svm} if $self->{svm};
2551         my $svm;
2552         # see if we have it in our config, first:
2553         eval {
2554                 my $section = "svn-remote.$self->{repo_id}";
2555                 $svm = {
2556                   source => tmp_config('--get', "$section.svm-source"),
2557                   uuid => tmp_config('--get', "$section.svm-uuid"),
2558                   replace => tmp_config('--get', "$section.svm-replace"),
2559                 }
2560         };
2561         if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2562                 $self->{svm} = $svm;
2563         }
2564         $self->{svm};
2565 }
2566
2567 sub _set_svm_vars {
2568         my ($self, $ra) = @_;
2569         return $ra if $self->svm;
2570
2571         my @err = ( "useSvmProps set, but failed to read SVM properties\n",
2572                     "(svm:source, svm:uuid) ",
2573                     "from the following URLs:\n" );
2574         sub read_svm_props {
2575                 my ($self, $ra, $path, $r) = @_;
2576                 my $props = ($ra->get_dir($path, $r))[2];
2577                 my $src = $props->{'svm:source'};
2578                 my $uuid = $props->{'svm:uuid'};
2579                 return undef if (!$src || !$uuid);
2580
2581                 chomp($src, $uuid);
2582
2583                 $uuid =~ m{^[0-9a-f\-]{30,}$}i
2584                     or die "doesn't look right - svm:uuid is '$uuid'\n";
2585
2586                 # the '!' is used to mark the repos_root!/relative/path
2587                 $src =~ s{/?!/?}{/};
2588                 $src =~ s{/+$}{}; # no trailing slashes please
2589                 # username is of no interest
2590                 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
2591
2592                 my $replace = $ra->{url};
2593                 $replace .= "/$path" if length $path;
2594
2595                 my $section = "svn-remote.$self->{repo_id}";
2596                 tmp_config("$section.svm-source", $src);
2597                 tmp_config("$section.svm-replace", $replace);
2598                 tmp_config("$section.svm-uuid", $uuid);
2599                 $self->{svm} = {
2600                         source => $src,
2601                         uuid => $uuid,
2602                         replace => $replace
2603                 };
2604         }
2605
2606         my $r = $ra->get_latest_revnum;
2607         my $path = $self->{path};
2608         my %tried;
2609         while (length $path) {
2610                 unless ($tried{"$self->{url}/$path"}) {
2611                         return $ra if $self->read_svm_props($ra, $path, $r);
2612                         $tried{"$self->{url}/$path"} = 1;
2613                 }
2614                 $path =~ s#/?[^/]+$##;
2615         }
2616         die "Path: '$path' should be ''\n" if $path ne '';
2617         return $ra if $self->read_svm_props($ra, $path, $r);
2618         $tried{"$self->{url}/$path"} = 1;
2619
2620         if ($ra->{repos_root} eq $self->{url}) {
2621                 die @err, (map { "  $_\n" } keys %tried), "\n";
2622         }
2623
2624         # nope, make sure we're connected to the repository root:
2625         my $ok;
2626         my @tried_b;
2627         $path = $ra->{svn_path};
2628         $ra = Git::SVN::Ra->new($ra->{repos_root});
2629         while (length $path) {
2630                 unless ($tried{"$ra->{url}/$path"}) {
2631                         $ok = $self->read_svm_props($ra, $path, $r);
2632                         last if $ok;
2633                         $tried{"$ra->{url}/$path"} = 1;
2634                 }
2635                 $path =~ s#/?[^/]+$##;
2636         }
2637         die "Path: '$path' should be ''\n" if $path ne '';
2638         $ok ||= $self->read_svm_props($ra, $path, $r);
2639         $tried{"$ra->{url}/$path"} = 1;
2640         if (!$ok) {
2641                 die @err, (map { "  $_\n" } keys %tried), "\n";
2642         }
2643         Git::SVN::Ra->new($self->{url});
2644 }
2645
2646 sub svnsync {
2647         my ($self) = @_;
2648         return $self->{svnsync} if $self->{svnsync};
2649
2650         if ($self->no_metadata) {
2651                 die "Can't have both 'noMetadata' and ",
2652                     "'useSvnsyncProps' options set!\n";
2653         }
2654         if ($self->rewrite_root) {
2655                 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2656                     "options set!\n";
2657         }
2658         if ($self->rewrite_uuid) {
2659                 die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
2660                     "options set!\n";
2661         }
2662
2663         my $svnsync;
2664         # see if we have it in our config, first:
2665         eval {
2666                 my $section = "svn-remote.$self->{repo_id}";
2667
2668                 my $url = tmp_config('--get', "$section.svnsync-url");
2669                 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2670                    die "doesn't look right - svn:sync-from-url is '$url'\n";
2671
2672                 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
2673                 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
2674                    die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2675
2676                 $svnsync = { url => $url, uuid => $uuid }
2677         };
2678         if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2679                 return $self->{svnsync} = $svnsync;
2680         }
2681
2682         my $err = "useSvnsyncProps set, but failed to read " .
2683                   "svnsync property: svn:sync-from-";
2684         my $rp = $self->ra->rev_proplist(0);
2685
2686         my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
2687         ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2688                    die "doesn't look right - svn:sync-from-url is '$url'\n";
2689
2690         my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
2691         ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
2692                    die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2693
2694         my $section = "svn-remote.$self->{repo_id}";
2695         tmp_config('--add', "$section.svnsync-uuid", $uuid);
2696         tmp_config('--add', "$section.svnsync-url", $url);
2697         return $self->{svnsync} = { url => $url, uuid => $uuid };
2698 }
2699
2700 # this allows us to memoize our SVN::Ra UUID locally and avoid a
2701 # remote lookup (useful for 'git svn log').
2702 sub ra_uuid {
2703         my ($self) = @_;
2704         unless ($self->{ra_uuid}) {
2705                 my $key = "svn-remote.$self->{repo_id}.uuid";
2706                 my $uuid = eval { tmp_config('--get', $key) };
2707                 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
2708                         $self->{ra_uuid} = $uuid;
2709                 } else {
2710                         die "ra_uuid called without URL\n" unless $self->{url};
2711                         $self->{ra_uuid} = $self->ra->get_uuid;
2712                         tmp_config('--add', $key, $self->{ra_uuid});
2713                 }
2714         }
2715         $self->{ra_uuid};
2716 }
2717
2718 sub _set_repos_root {
2719         my ($self, $repos_root) = @_;
2720         my $k = "svn-remote.$self->{repo_id}.reposRoot";
2721         $repos_root ||= $self->ra->{repos_root};
2722         tmp_config($k, $repos_root);
2723         $repos_root;
2724 }
2725
2726 sub repos_root {
2727         my ($self) = @_;
2728         my $k = "svn-remote.$self->{repo_id}.reposRoot";
2729         eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2730 }
2731
2732 sub ra {
2733         my ($self) = shift;
2734         my $ra = Git::SVN::Ra->new($self->{url});
2735         $self->_set_repos_root($ra->{repos_root});
2736         if ($self->use_svm_props && !$self->{svm}) {
2737                 if ($self->no_metadata) {
2738                         die "Can't have both 'noMetadata' and ",
2739                             "'useSvmProps' options set!\n";
2740                 } elsif ($self->use_svnsync_props) {
2741                         die "Can't have both 'useSvnsyncProps' and ",
2742                             "'useSvmProps' options set!\n";
2743                 }
2744                 $ra = $self->_set_svm_vars($ra);
2745                 $self->{-want_revprops} = 1;
2746         }
2747         $ra;
2748 }
2749
2750 # prop_walk(PATH, REV, SUB)
2751 # -------------------------
2752 # Recursively traverse PATH at revision REV and invoke SUB for each
2753 # directory that contains a SVN property.  SUB will be invoked as
2754 # follows:  &SUB(gs, path, props);  where `gs' is this instance of
2755 # Git::SVN, `path' the path to the directory where the properties
2756 # `props' were found.  The `path' will be relative to point of checkout,
2757 # that is, if url://repo/trunk is the current Git branch, and that
2758 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
2759 # as `path' (note the trailing `/').
2760 sub prop_walk {
2761         my ($self, $path, $rev, $sub) = @_;
2762
2763         $path =~ s#^/##;
2764         my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2765         $path =~ s#^/*#/#g;
2766         my $p = $path;
2767         # Strip the irrelevant part of the path.
2768         $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2769         # Ensure the path is terminated by a `/'.
2770         $p =~ s#/*$#/#;
2771
2772         # The properties contain all the internal SVN stuff nobody
2773         # (usually) cares about.
2774         my $interesting_props = 0;
2775         foreach (keys %{$props}) {
2776                 # If it doesn't start with `svn:', it must be a
2777                 # user-defined property.
2778                 ++$interesting_props and next if $_ !~ /^svn:/;
2779                 # FIXME: Fragile, if SVN adds new public properties,
2780                 # this needs to be updated.
2781                 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2782                                                  |eol-style|mime-type
2783                                                  |externals|needs-lock)$/x;
2784         }
2785         &$sub($self, $p, $props) if $interesting_props;
2786
2787         foreach (sort keys %$dirent) {
2788                 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
2789                 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
2790         }
2791 }
2792
2793 sub last_rev { ($_[0]->last_rev_commit)[0] }
2794 sub last_commit { ($_[0]->last_rev_commit)[1] }
2795
2796 # returns the newest SVN revision number and newest commit SHA1
2797 sub last_rev_commit {
2798         my ($self) = @_;
2799         if (defined $self->{last_rev} && defined $self->{last_commit}) {
2800                 return ($self->{last_rev}, $self->{last_commit});
2801         }
2802         my $c = ::verify_ref($self->refname.'^0');
2803         if ($c && !$self->use_svm_props && !$self->no_metadata) {
2804                 my $rev = (::cmt_metadata($c))[1];
2805                 if (defined $rev) {
2806                         ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2807                         return ($rev, $c);
2808                 }
2809         }
2810         my $map_path = $self->map_path;
2811         unless (-e $map_path) {
2812                 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2813                 return (undef, undef);
2814         }
2815         my ($rev, $commit) = $self->rev_map_max(1);
2816         ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2817         return ($rev, $commit);
2818 }
2819
2820 sub get_fetch_range {
2821         my ($self, $min, $max) = @_;
2822         $max ||= $self->ra->get_latest_revnum;
2823         $min ||= $self->rev_map_max;
2824         (++$min, $max);
2825 }
2826
2827 sub tmp_config {
2828         my (@args) = @_;
2829         my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2830         my $config = "$ENV{GIT_DIR}/svn/.metadata";
2831         if (! -f $config && -f $old_def_config) {
2832                 rename $old_def_config, $config or
2833                        die "Failed rename $old_def_config => $config: $!\n";
2834         }
2835         my $old_config = $ENV{GIT_CONFIG};
2836         $ENV{GIT_CONFIG} = $config;
2837         $@ = undef;
2838         my @ret = eval {
2839                 unless (-f $config) {
2840                         mkfile($config);
2841                         open my $fh, '>', $config or
2842                             die "Can't open $config: $!\n";
2843                         print $fh "; This file is used internally by ",
2844                                   "git-svn\n" or die
2845                                   "Couldn't write to $config: $!\n";
2846                         print $fh "; You should not have to edit it\n" or
2847                               die "Couldn't write to $config: $!\n";
2848                         close $fh or die "Couldn't close $config: $!\n";
2849                 }
2850                 command('config', @args);
2851         };
2852         my $err = $@;
2853         if (defined $old_config) {
2854                 $ENV{GIT_CONFIG} = $old_config;
2855         } else {
2856                 delete $ENV{GIT_CONFIG};
2857         }
2858         die $err if $err;
2859         wantarray ? @ret : $ret[0];
2860 }
2861
2862 sub tmp_index_do {
2863         my ($self, $sub) = @_;
2864         my $old_index = $ENV{GIT_INDEX_FILE};
2865         $ENV{GIT_INDEX_FILE} = $self->{index};
2866         $@ = undef;
2867         my @ret = eval {
2868                 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2869                 mkpath([$dir]) unless -d $dir;
2870                 &$sub;
2871         };
2872         my $err = $@;
2873         if (defined $old_index) {
2874                 $ENV{GIT_INDEX_FILE} = $old_index;
2875         } else {
2876                 delete $ENV{GIT_INDEX_FILE};
2877         }
2878         die $err if $err;
2879         wantarray ? @ret : $ret[0];
2880 }
2881
2882 sub assert_index_clean {
2883         my ($self, $treeish) = @_;
2884
2885         $self->tmp_index_do(sub {
2886                 command_noisy('read-tree', $treeish) unless -e $self->{index};
2887                 my $x = command_oneline('write-tree');
2888                 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2889                            /^tree ($::sha1)/mo);
2890                 return if $y eq $x;
2891
2892                 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2893                 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2894                 command_noisy('read-tree', $treeish);
2895                 $x = command_oneline('write-tree');
2896                 if ($y ne $x) {
2897                         fatal "trees ($treeish) $y != $x\n",
2898                               "Something is seriously wrong...";
2899                 }
2900         });
2901 }
2902
2903 sub get_commit_parents {
2904         my ($self, $log_entry) = @_;
2905         my (%seen, @ret, @tmp);
2906         # legacy support for 'set-tree'; this is only used by set_tree_cb:
2907         if (my $ip = $self->{inject_parents}) {
2908                 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2909                         push @tmp, $commit;
2910                 }
2911         }
2912         if (my $cur = ::verify_ref($self->refname.'^0')) {
2913                 push @tmp, $cur;
2914         }
2915         if (my $ipd = $self->{inject_parents_dcommit}) {
2916                 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2917                         push @tmp, @$commit;
2918                 }
2919         }
2920         push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
2921         while (my $p = shift @tmp) {
2922                 next if $seen{$p};
2923                 $seen{$p} = 1;
2924                 push @ret, $p;
2925         }
2926         @ret;
2927 }
2928
2929 sub rewrite_root {
2930         my ($self) = @_;
2931         return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2932         my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2933         my $rwr = eval { command_oneline(qw/config --get/, $k) };
2934         if ($rwr) {
2935                 $rwr =~ s#/+$##;
2936                 if ($rwr !~ m#^[a-z\+]+://#) {
2937                         die "$rwr is not a valid URL (key: $k)\n";
2938                 }
2939         }
2940         $self->{-rewrite_root} = $rwr;
2941 }
2942
2943 sub rewrite_uuid {
2944         my ($self) = @_;
2945         return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
2946         my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
2947         my $rwid = eval { command_oneline(qw/config --get/, $k) };
2948         if ($rwid) {
2949                 $rwid =~ s#/+$##;
2950                 if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
2951                         die "$rwid is not a valid UUID (key: $k)\n";
2952                 }
2953         }
2954         $self->{-rewrite_uuid} = $rwid;
2955 }
2956
2957 sub metadata_url {
2958         my ($self) = @_;
2959         ($self->rewrite_root || $self->{url}) .
2960            (length $self->{path} ? '/' . $self->{path} : '');
2961 }
2962
2963 sub full_url {
2964         my ($self) = @_;
2965         $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
2966 }
2967
2968 sub full_pushurl {
2969         my ($self) = @_;
2970         if ($self->{pushurl}) {
2971                 return $self->{pushurl} . (length $self->{path} ? '/' .
2972                        $self->{path} : '');
2973         } else {
2974                 return $self->full_url;
2975         }
2976 }
2977
2978 sub set_commit_header_env {
2979         my ($log_entry) = @_;
2980         my %env;
2981         foreach my $ned (qw/NAME EMAIL DATE/) {
2982                 foreach my $ac (qw/AUTHOR COMMITTER/) {
2983                         $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2984                 }
2985         }
2986
2987         $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2988         $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2989         $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2990
2991         $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2992                                                 ? $log_entry->{commit_name}
2993                                                 : $log_entry->{name};
2994         $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2995                                                 ? $log_entry->{commit_email}
2996                                                 : $log_entry->{email};
2997         \%env;
2998 }
2999
3000 sub restore_commit_header_env {
3001         my ($env) = @_;
3002         foreach my $ned (qw/NAME EMAIL DATE/) {
3003                 foreach my $ac (qw/AUTHOR COMMITTER/) {
3004                         my $k = "GIT_${ac}_${ned}";
3005                         if (defined $env->{$k}) {
3006                                 $ENV{$k} = $env->{$k};
3007                         } else {
3008                                 delete $ENV{$k};
3009                         }
3010                 }
3011         }
3012 }
3013
3014 sub gc {
3015         command_noisy('gc', '--auto');
3016 };
3017
3018 sub do_git_commit {
3019         my ($self, $log_entry) = @_;
3020         my $lr = $self->last_rev;
3021         if (defined $lr && $lr >= $log_entry->{revision}) {
3022                 die "Last fetched revision of ", $self->refname,
3023                     " was r$lr, but we are about to fetch: ",
3024                     "r$log_entry->{revision}!\n";
3025         }
3026         if (my $c = $self->rev_map_get($log_entry->{revision})) {
3027                 croak "$log_entry->{revision} = $c already exists! ",
3028                       "Why are we refetching it?\n";
3029         }
3030         my $old_env = set_commit_header_env($log_entry);
3031         my $tree = $log_entry->{tree};
3032         if (!defined $tree) {
3033                 $tree = $self->tmp_index_do(sub {
3034                                             command_oneline('write-tree') });
3035         }
3036         die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
3037
3038         my @exec = ('git', 'commit-tree', $tree);
3039         foreach ($self->get_commit_parents($log_entry)) {
3040                 push @exec, '-p', $_;
3041         }
3042         defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
3043                                                                    or croak $!;
3044         binmode $msg_fh;
3045
3046         # we always get UTF-8 from SVN, but we may want our commits in
3047         # a different encoding.
3048         if (my $enc = Git::config('i18n.commitencoding')) {
3049                 require Encode;
3050                 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
3051         }
3052         print $msg_fh $log_entry->{log} or croak $!;
3053         restore_commit_header_env($old_env);
3054         unless ($self->no_metadata) {
3055                 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
3056                               or croak $!;
3057         }
3058         $msg_fh->flush == 0 or croak $!;
3059         close $msg_fh or croak $!;
3060         chomp(my $commit = do { local $/; <$out_fh> });
3061         close $out_fh or croak $!;
3062         waitpid $pid, 0;
3063         croak $? if $?;
3064         if ($commit !~ /^$::sha1$/o) {
3065                 die "Failed to commit, invalid sha1: $commit\n";
3066         }
3067
3068         $self->rev_map_set($log_entry->{revision}, $commit, 1);
3069
3070         $self->{last_rev} = $log_entry->{revision};
3071         $self->{last_commit} = $commit;
3072         print "r$log_entry->{revision}" unless $::_q > 1;
3073         if (defined $log_entry->{svm_revision}) {
3074                  print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
3075                  $self->rev_map_set($log_entry->{svm_revision}, $commit,
3076                                    0, $self->svm_uuid);
3077         }
3078         print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
3079         if (--$_gc_nr == 0) {
3080                 $_gc_nr = $_gc_period;
3081                 gc();
3082         }
3083         return $commit;
3084 }
3085
3086 sub match_paths {
3087         my ($self, $paths, $r) = @_;
3088         return 1 if $self->{path} eq '';
3089         if (my $path = $paths->{"/$self->{path}"}) {
3090                 return ($path->{action} eq 'D') ? 0 : 1;
3091         }
3092         $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
3093         if (grep /$self->{path_regex}/, keys %$paths) {
3094                 return 1;
3095         }
3096         my $c = '';
3097         foreach (split m#/#, $self->{path}) {
3098                 $c .= "/$_";
3099                 next unless ($paths->{$c} &&
3100                              ($paths->{$c}->{action} =~ /^[AR]$/));
3101                 if ($self->ra->check_path($self->{path}, $r) ==
3102                     $SVN::Node::dir) {
3103                         return 1;
3104                 }
3105         }
3106         return 0;
3107 }
3108
3109 sub find_parent_branch {
3110         my ($self, $paths, $rev) = @_;
3111         return undef unless $self->follow_parent;
3112         unless (defined $paths) {
3113                 my $err_handler = $SVN::Error::handler;
3114                 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
3115                 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
3116                                    sub { $paths = $_[0] });
3117                 $SVN::Error::handler = $err_handler;
3118         }
3119         return undef unless defined $paths;
3120
3121         # look for a parent from another branch:
3122         my @b_path_components = split m#/#, $self->{path};
3123         my @a_path_components;
3124         my $i;
3125         while (@b_path_components) {
3126                 $i = $paths->{'/'.join('/', @b_path_components)};
3127                 last if $i && defined $i->{copyfrom_path};
3128                 unshift(@a_path_components, pop(@b_path_components));
3129         }
3130         return undef unless defined $i && defined $i->{copyfrom_path};
3131         my $branch_from = $i->{copyfrom_path};
3132         if (@a_path_components) {
3133                 print STDERR "branch_from: $branch_from => ";
3134                 $branch_from .= '/'.join('/', @a_path_components);
3135                 print STDERR $branch_from, "\n";
3136         }
3137         my $r = $i->{copyfrom_rev};
3138         my $repos_root = $self->ra->{repos_root};
3139         my $url = $self->ra->{url};
3140         my $new_url = $url . $branch_from;
3141         print STDERR  "Found possible branch point: ",
3142                       "$new_url => ", $self->full_url, ", $r\n"
3143                       unless $::_q > 1;
3144         $branch_from =~ s#^/##;
3145         my $gs = $self->other_gs($new_url, $url,
3146                                  $branch_from, $r, $self->{ref_id});
3147         my ($r0, $parent) = $gs->find_rev_before($r, 1);
3148         {
3149                 my ($base, $head);
3150                 if (!defined $r0 || !defined $parent) {
3151                         ($base, $head) = parse_revision_argument(0, $r);
3152                 } else {
3153                         if ($r0 < $r) {
3154                                 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
3155                                         0, 1, sub { $base = $_[1] - 1 });
3156                         }
3157                 }
3158                 if (defined $base && $base <= $r) {
3159                         $gs->fetch($base, $r);
3160                 }
3161                 ($r0, $parent) = $gs->find_rev_before($r, 1);
3162         }
3163         if (defined $r0 && defined $parent) {
3164                 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
3165                              unless $::_q > 1;
3166                 my $ed;
3167                 if ($self->ra->can_do_switch) {
3168                         $self->assert_index_clean($parent);
3169                         print STDERR "Following parent with do_switch\n"
3170                                      unless $::_q > 1;
3171                         # do_switch works with svn/trunk >= r22312, but that
3172                         # is not included with SVN 1.4.3 (the latest version
3173                         # at the moment), so we can't rely on it
3174                         $self->{last_rev} = $r0;
3175                         $self->{last_commit} = $parent;
3176                         $ed = Git::SVN::Fetcher->new($self, $gs->{path});
3177                         $gs->ra->gs_do_switch($r0, $rev, $gs,
3178                                               $self->full_url, $ed)
3179                           or die "SVN connection failed somewhere...\n";
3180                 } elsif ($self->ra->trees_match($new_url, $r0,
3181                                                 $self->full_url, $rev)) {
3182                         print STDERR "Trees match:\n",
3183                                      "  $new_url\@$r0\n",
3184                                      "  ${\$self->full_url}\@$rev\n",
3185                                      "Following parent with no changes\n"
3186                                      unless $::_q > 1;
3187                         $self->tmp_index_do(sub {
3188                             command_noisy('read-tree', $parent);
3189                         });
3190                         $self->{last_commit} = $parent;
3191                 } else {
3192                         print STDERR "Following parent with do_update\n"
3193                                      unless $::_q > 1;
3194                         $ed = Git::SVN::Fetcher->new($self);
3195                         $self->ra->gs_do_update($rev, $rev, $self, $ed)
3196                           or die "SVN connection failed somewhere...\n";
3197                 }
3198                 print STDERR "Successfully followed parent\n" unless $::_q > 1;
3199                 return $self->make_log_entry($rev, [$parent], $ed);
3200         }
3201         return undef;
3202 }
3203
3204 sub do_fetch {
3205         my ($self, $paths, $rev) = @_;
3206         my $ed;
3207         my ($last_rev, @parents);
3208         if (my $lc = $self->last_commit) {
3209                 # we can have a branch that was deleted, then re-added
3210                 # under the same name but copied from another path, in
3211                 # which case we'll have multiple parents (we don't
3212                 # want to break the original ref, nor lose copypath info):
3213                 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
3214                         push @{$log_entry->{parents}}, $lc;
3215                         return $log_entry;
3216                 }
3217                 $ed = Git::SVN::Fetcher->new($self);
3218                 $last_rev = $self->{last_rev};
3219                 $ed->{c} = $lc;
3220                 @parents = ($lc);
3221         } else {
3222                 $last_rev = $rev;
3223                 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
3224                         return $log_entry;
3225                 }
3226                 $ed = Git::SVN::Fetcher->new($self);
3227         }
3228         unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
3229                 die "SVN connection failed somewhere...\n";
3230         }
3231         $self->make_log_entry($rev, \@parents, $ed);
3232 }
3233
3234 sub mkemptydirs {
3235         my ($self, $r) = @_;
3236
3237         sub scan {
3238                 my ($r, $empty_dirs, $line) = @_;
3239                 if (defined $r && $line =~ /^r(\d+)$/) {
3240                         return 0 if $1 > $r;
3241                 } elsif ($line =~ /^  \+empty_dir: (.+)$/) {
3242                         $empty_dirs->{$1} = 1;
3243                 } elsif ($line =~ /^  \-empty_dir: (.+)$/) {
3244                         my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
3245                         delete @$empty_dirs{@d};
3246                 }
3247                 1; # continue
3248         };
3249
3250         my %empty_dirs = ();
3251         my $gz_file = "$self->{dir}/unhandled.log.gz";
3252         if (-f $gz_file) {
3253                 if (!can_compress()) {
3254                         warn "Compress::Zlib could not be found; ",
3255                              "empty directories in $gz_file will not be read\n";
3256                 } else {
3257                         my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
3258                                 die "Unable to open $gz_file: $!\n";
3259                         my $line;
3260                         while ($gz->gzreadline($line) > 0) {
3261                                 scan($r, \%empty_dirs, $line) or last;
3262                         }
3263                         $gz->gzclose;
3264                 }
3265         }
3266
3267         if (open my $fh, '<', "$self->{dir}/unhandled.log") {
3268                 binmode $fh or croak "binmode: $!";
3269                 while (<$fh>) {
3270                         scan($r, \%empty_dirs, $_) or last;
3271                 }
3272                 close $fh;
3273         }
3274
3275         my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
3276         foreach my $d (sort keys %empty_dirs) {
3277                 $d = uri_decode($d);
3278                 $d =~ s/$strip//;
3279                 next unless length($d);
3280                 next if -d $d;
3281                 if (-e $d) {
3282                         warn "$d exists but is not a directory\n";
3283                 } else {
3284                         print "creating empty directory: $d\n";
3285                         mkpath([$d]);
3286                 }
3287         }
3288 }
3289
3290 sub get_untracked {
3291         my ($self, $ed) = @_;
3292         my @out;
3293         my $h = $ed->{empty};
3294         foreach (sort keys %$h) {
3295                 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
3296                 push @out, "  $act: " . uri_encode($_);
3297                 warn "W: $act: $_\n";
3298         }
3299         foreach my $t (qw/dir_prop file_prop/) {
3300                 $h = $ed->{$t} or next;
3301                 foreach my $path (sort keys %$h) {
3302                         my $ppath = $path eq '' ? '.' : $path;
3303                         foreach my $prop (sort keys %{$h->{$path}}) {
3304                                 next if $SKIP_PROP{$prop};
3305                                 my $v = $h->{$path}->{$prop};
3306                                 my $t_ppath_prop = "$t: " .
3307                                                     uri_encode($ppath) . ' ' .
3308                                                     uri_encode($prop);
3309                                 if (defined $v) {
3310                                         push @out, "  +$t_ppath_prop " .
3311                                                    uri_encode($v);
3312                                 } else {
3313                                         push @out, "  -$t_ppath_prop";
3314                                 }
3315                         }
3316                 }
3317         }
3318         foreach my $t (qw/absent_file absent_directory/) {
3319                 $h = $ed->{$t} or next;
3320                 foreach my $parent (sort keys %$h) {
3321                         foreach my $path (sort @{$h->{$parent}}) {
3322                                 push @out, "  $t: " .
3323                                            uri_encode("$parent/$path");
3324                                 warn "W: $t: $parent/$path ",
3325                                      "Insufficient permissions?\n";
3326                         }
3327                 }
3328         }
3329         \@out;
3330 }
3331
3332 sub get_tz {
3333         # some systmes don't handle or mishandle %z, so be creative.
3334         my $t = shift || time;
3335         my $gm = timelocal(gmtime($t));
3336         my $sign = qw( + + - )[ $t <=> $gm ];
3337         return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
3338 }
3339
3340 # parse_svn_date(DATE)
3341 # --------------------
3342 # Given a date (in UTC) from Subversion, return a string in the format
3343 # "<TZ Offset> <local date/time>" that Git will use.
3344 #
3345 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
3346 # is true we'll convert it to the local timezone instead.
3347 sub parse_svn_date {
3348         my $date = shift || return '+0000 1970-01-01 00:00:00';
3349         my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
3350                                             (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
3351                                          croak "Unable to parse date: $date\n";
3352         my $parsed_date;    # Set next.
3353
3354         if ($Git::SVN::_localtime) {
3355                 # Translate the Subversion datetime to an epoch time.
3356                 # Begin by switching ourselves to $date's timezone, UTC.
3357                 my $old_env_TZ = $ENV{TZ};
3358                 $ENV{TZ} = 'UTC';
3359
3360                 my $epoch_in_UTC =
3361                     POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
3362
3363                 # Determine our local timezone (including DST) at the
3364                 # time of $epoch_in_UTC.  $Git::SVN::Log::TZ stored the
3365                 # value of TZ, if any, at the time we were run.
3366                 if (defined $Git::SVN::Log::TZ) {
3367                         $ENV{TZ} = $Git::SVN::Log::TZ;
3368                 } else {
3369                         delete $ENV{TZ};
3370                 }
3371
3372                 my $our_TZ = get_tz();
3373
3374                 # This converts $epoch_in_UTC into our local timezone.
3375                 my ($sec, $min, $hour, $mday, $mon, $year,
3376                     $wday, $yday, $isdst) = localtime($epoch_in_UTC);
3377
3378                 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
3379                                        $our_TZ, $year + 1900, $mon + 1,
3380                                        $mday, $hour, $min, $sec);
3381
3382                 # Reset us to the timezone in effect when we entered
3383                 # this routine.
3384                 if (defined $old_env_TZ) {
3385                         $ENV{TZ} = $old_env_TZ;
3386                 } else {
3387                         delete $ENV{TZ};
3388                 }
3389         } else {
3390                 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
3391         }
3392
3393         return $parsed_date;
3394 }
3395
3396 sub other_gs {
3397         my ($self, $new_url, $url,
3398             $branch_from, $r, $old_ref_id) = @_;
3399         my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
3400         unless ($gs) {
3401                 my $ref_id = $old_ref_id;
3402                 $ref_id =~ s/\@\d+-*$//;
3403                 $ref_id .= "\@$r";
3404                 # just grow a tail if we're not unique enough :x
3405                 $ref_id .= '-' while find_ref($ref_id);
3406                 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
3407                 if ($u =~ s#^\Q$url\E(/|$)##) {
3408                         $p = $u;
3409                         $u = $url;
3410                         $repo_id = $self->{repo_id};
3411                 }
3412                 while (1) {
3413                         # It is possible to tag two different subdirectories at
3414                         # the same revision.  If the url for an existing ref
3415                         # does not match, we must either find a ref with a
3416                         # matching url or create a new ref by growing a tail.
3417                         $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
3418                         my (undef, $max_commit) = $gs->rev_map_max(1);
3419                         last if (!$max_commit);
3420                         my ($url) = ::cmt_metadata($max_commit);
3421                         last if ($url eq $gs->metadata_url);
3422                         $ref_id .= '-';
3423                 }
3424                 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
3425         }
3426         $gs
3427 }
3428
3429 sub call_authors_prog {
3430         my ($orig_author) = @_;
3431         $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
3432         my $author = `$::_authors_prog $orig_author`;
3433         if ($? != 0) {
3434                 die "$::_authors_prog failed with exit code $?\n"
3435         }
3436         if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
3437                 my ($name, $email) = ($1, $2);
3438                 $email = undef if length $2 == 0;
3439                 return [$name, $email];
3440         } else {
3441                 die "Author: $orig_author: $::_authors_prog returned "
3442                         . "invalid author format: $author\n";
3443         }
3444 }
3445
3446 sub check_author {
3447         my ($author) = @_;
3448         if (!defined $author || length $author == 0) {
3449                 $author = '(no author)';
3450         }
3451         if (!defined $::users{$author}) {
3452                 if (defined $::_authors_prog) {
3453                         $::users{$author} = call_authors_prog($author);
3454                 } elsif (defined $::_authors) {
3455                         die "Author: $author not defined in $::_authors file\n";
3456                 }
3457         }
3458         $author;
3459 }
3460
3461 sub find_extra_svk_parents {
3462         my ($self, $ed, $tickets, $parents) = @_;
3463         # aha!  svk:merge property changed...
3464         my @tickets = split "\n", $tickets;
3465         my @known_parents;
3466         for my $ticket ( @tickets ) {
3467                 my ($uuid, $path, $rev) = split /:/, $ticket;
3468                 if ( $uuid eq $self->ra_uuid ) {
3469                         my $url = $self->{url};
3470                         my $repos_root = $url;
3471                         my $branch_from = $path;
3472                         $branch_from =~ s{^/}{};
3473                         my $gs = $self->other_gs($repos_root."/".$branch_from,
3474                                                  $url,
3475                                                  $branch_from,
3476                                                  $rev,
3477                                                  $self->{ref_id});
3478                         if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
3479                                 # wahey!  we found it, but it might be
3480                                 # an old one (!)
3481                                 push @known_parents, [ $rev, $commit ];
3482                         }
3483                 }
3484         }
3485         # Ordering matters; highest-numbered commit merge tickets
3486         # first, as they may account for later merge ticket additions
3487         # or changes.
3488         @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
3489         for my $parent ( @known_parents ) {
3490                 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
3491                 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3492                 my $new;
3493                 while ( <$msg_fh> ) {
3494                         $new=1;last;
3495                 }
3496                 command_close_pipe($msg_fh, $ctx);
3497                 if ( $new ) {
3498                         print STDERR
3499                             "Found merge parent (svk:merge ticket): $parent\n";
3500                         push @$parents, $parent;
3501                 }
3502         }
3503 }
3504
3505 sub lookup_svn_merge {
3506         my $uuid = shift;
3507         my $url = shift;
3508         my $merge = shift;
3509
3510         my ($source, $revs) = split ":", $merge;
3511         my $path = $source;
3512         $path =~ s{^/}{};
3513         my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3514         if ( !$gs ) {
3515                 warn "Couldn't find revmap for $url$source\n";
3516                 return;
3517         }
3518         my @ranges = split ",", $revs;
3519         my ($tip, $tip_commit);
3520         my @merged_commit_ranges;
3521         # find the tip
3522         for my $range ( @ranges ) {
3523                 my ($bottom, $top) = split "-", $range;
3524                 $top ||= $bottom;
3525                 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3526                 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
3527
3528                 unless ($top_commit and $bottom_commit) {
3529                         warn "W:unknown path/rev in svn:mergeinfo "
3530                                 ."dirprop: $source:$range\n";
3531                         next;
3532                 }
3533
3534                 if (scalar(command('rev-parse', "$bottom_commit^@"))) {
3535                         push @merged_commit_ranges,
3536                              "$bottom_commit^..$top_commit";
3537                 } else {
3538                         push @merged_commit_ranges, "$top_commit";
3539                 }
3540
3541                 if ( !defined $tip or $top > $tip ) {
3542                         $tip = $top;
3543                         $tip_commit = $top_commit;
3544                 }
3545         }
3546         return ($tip_commit, @merged_commit_ranges);
3547 }
3548
3549 sub _rev_list {
3550         my ($msg_fh, $ctx) = command_output_pipe(
3551                 "rev-list", @_,
3552                );
3553         my @rv;
3554         while ( <$msg_fh> ) {
3555                 chomp;
3556                 push @rv, $_;
3557         }
3558         command_close_pipe($msg_fh, $ctx);
3559         @rv;
3560 }
3561
3562 sub check_cherry_pick {
3563         my $base = shift;
3564         my $tip = shift;
3565         my $parents = shift;
3566         my @ranges = @_;
3567         my %commits = map { $_ => 1 }
3568                 _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--");
3569         for my $range ( @ranges ) {
3570                 delete @commits{_rev_list($range, "--")};
3571         }
3572         for my $commit (keys %commits) {
3573                 if (has_no_changes($commit)) {
3574                         delete $commits{$commit};
3575                 }
3576         }
3577         return (keys %commits);
3578 }
3579
3580 sub has_no_changes {
3581         my $commit = shift;
3582
3583         my @revs = split / /, command_oneline(
3584                 qw(rev-list --parents -1 -m), $commit);
3585
3586         # Commits with no parents, e.g. the start of a partial branch,
3587         # have changes by definition.
3588         return 1 if (@revs < 2);
3589
3590         # Commits with multiple parents, e.g a merge, have no changes
3591         # by definition.
3592         return 0 if (@revs > 2);
3593
3594         return (command_oneline("rev-parse", "$commit^{tree}") eq
3595                 command_oneline("rev-parse", "$commit~1^{tree}"));
3596 }
3597
3598 sub tie_for_persistent_memoization {
3599         my $hash = shift;
3600         my $path = shift;
3601
3602         if ($can_use_yaml) {
3603                 tie %$hash => 'Git::SVN::Memoize::YAML', "$path.yaml";
3604         } else {
3605                 tie %$hash => 'Memoize::Storable', "$path.db", 'nstore';
3606         }
3607 }
3608
3609 # The GIT_DIR environment variable is not always set until after the command
3610 # line arguments are processed, so we can't memoize in a BEGIN block.
3611 {
3612         my $memoized = 0;
3613
3614         sub memoize_svn_mergeinfo_functions {
3615                 return if $memoized;
3616                 $memoized = 1;
3617
3618                 my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
3619                 mkpath([$cache_path]) unless -d $cache_path;
3620
3621                 my %lookup_svn_merge_cache;
3622                 my %check_cherry_pick_cache;
3623                 my %has_no_changes_cache;
3624
3625                 tie_for_persistent_memoization(\%lookup_svn_merge_cache,
3626                     "$cache_path/lookup_svn_merge");
3627                 memoize 'lookup_svn_merge',
3628                         SCALAR_CACHE => 'FAULT',
3629                         LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
3630                 ;
3631
3632                 tie_for_persistent_memoization(\%check_cherry_pick_cache,
3633                     "$cache_path/check_cherry_pick");
3634                 memoize 'check_cherry_pick',
3635                         SCALAR_CACHE => 'FAULT',
3636                         LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
3637                 ;
3638
3639                 tie_for_persistent_memoization(\%has_no_changes_cache,
3640                     "$cache_path/has_no_changes");
3641                 memoize 'has_no_changes',
3642                         SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
3643                         LIST_CACHE => 'FAULT',
3644                 ;
3645         }
3646
3647         sub unmemoize_svn_mergeinfo_functions {
3648                 return if not $memoized;
3649                 $memoized = 0;
3650
3651                 Memoize::unmemoize 'lookup_svn_merge';
3652                 Memoize::unmemoize 'check_cherry_pick';
3653                 Memoize::unmemoize 'has_no_changes';
3654         }
3655
3656         Memoize::memoize 'Git::SVN::repos_root';
3657 }
3658
3659 END {
3660         # Force cache writeout explicitly instead of waiting for
3661         # global destruction to avoid segfault in Storable:
3662         # http://rt.cpan.org/Public/Bug/Display.html?id=36087
3663         unmemoize_svn_mergeinfo_functions();
3664 }
3665
3666 sub parents_exclude {
3667         my $parents = shift;
3668         my @commits = @_;
3669         return unless @commits;
3670
3671         my @excluded;
3672         my $excluded;
3673         do {
3674                 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
3675                 $excluded = command_oneline(@cmd);
3676                 if ( $excluded ) {
3677                         my @new;
3678                         my $found;
3679                         for my $commit ( @commits ) {
3680                                 if ( $commit eq $excluded ) {
3681                                         push @excluded, $commit;
3682                                         $found++;
3683                                         last;
3684                                 }
3685                                 else {
3686                                         push @new, $commit;
3687                                 }
3688                         }
3689                         die "saw commit '$excluded' in rev-list output, "
3690                                 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
3691                                         unless $found;
3692                         @commits = @new;
3693                 }
3694         }
3695                 while ($excluded and @commits);
3696
3697         return @excluded;
3698 }
3699
3700
3701 # note: this function should only be called if the various dirprops
3702 # have actually changed
3703 sub find_extra_svn_parents {
3704         my ($self, $ed, $mergeinfo, $parents) = @_;
3705         # aha!  svk:merge property changed...
3706
3707         memoize_svn_mergeinfo_functions();
3708
3709         # We first search for merged tips which are not in our
3710         # history.  Then, we figure out which git revisions are in
3711         # that tip, but not this revision.  If all of those revisions
3712         # are now marked as merge, we can add the tip as a parent.
3713         my @merges = split "\n", $mergeinfo;
3714         my @merge_tips;
3715         my $url = $self->{url};
3716         my $uuid = $self->ra_uuid;
3717         my %ranges;
3718         for my $merge ( @merges ) {
3719                 my ($tip_commit, @ranges) =
3720                         lookup_svn_merge( $uuid, $url, $merge );
3721                 unless (!$tip_commit or
3722                                 grep { $_ eq $tip_commit } @$parents ) {
3723                         push @merge_tips, $tip_commit;
3724                         $ranges{$tip_commit} = \@ranges;
3725                 } else {
3726                         push @merge_tips, undef;
3727                 }
3728         }
3729
3730         my %excluded = map { $_ => 1 }
3731                 parents_exclude($parents, grep { defined } @merge_tips);
3732
3733         # check merge tips for new parents
3734         my @new_parents;
3735         for my $merge_tip ( @merge_tips ) {
3736                 my $spec = shift @merges;
3737                 next unless $merge_tip and $excluded{$merge_tip};
3738
3739                 my $ranges = $ranges{$merge_tip};
3740
3741                 # check out 'new' tips
3742                 my $merge_base;
3743                 eval {
3744                         $merge_base = command_oneline(
3745                                 "merge-base",
3746                                 @$parents, $merge_tip,
3747                         );
3748                 };
3749                 if ($@) {
3750                         die "An error occurred during merge-base"
3751                                 unless $@->isa("Git::Error::Command");
3752
3753                         warn "W: Cannot find common ancestor between ".
3754                              "@$parents and $merge_tip. Ignoring merge info.\n";
3755                         next;
3756                 }
3757
3758                 # double check that there are no missing non-merge commits
3759                 my (@incomplete) = check_cherry_pick(
3760                         $merge_base, $merge_tip,
3761                         $parents,
3762                         @$ranges,
3763                        );
3764
3765                 if ( @incomplete ) {
3766                         warn "W:svn cherry-pick ignored ($spec) - missing "
3767                                 .@incomplete." commit(s) (eg $incomplete[0])\n";
3768                 } else {
3769                         warn
3770                                 "Found merge parent (svn:mergeinfo prop): ",
3771                                         $merge_tip, "\n";
3772                         push @new_parents, $merge_tip;
3773                 }
3774         }
3775
3776         # cater for merges which merge commits from multiple branches
3777         if ( @new_parents > 1 ) {
3778                 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
3779                         for ( my $j = 0; $j <= $#new_parents; $j++ ) {
3780                                 next if $i == $j;
3781                                 next unless $new_parents[$i];
3782                                 next unless $new_parents[$j];
3783                                 my $revs = command_oneline(
3784                                         "rev-list", "-1",
3785                                         "$new_parents[$i]..$new_parents[$j]",
3786                                        );
3787                                 if ( !$revs ) {
3788                                         undef($new_parents[$j]);
3789                                 }
3790                         }
3791                 }
3792         }
3793         push @$parents, grep { defined } @new_parents;
3794 }
3795
3796 sub make_log_entry {
3797         my ($self, $rev, $parents, $ed) = @_;
3798         my $untracked = $self->get_untracked($ed);
3799
3800         my @parents = @$parents;
3801         my $ps = $ed->{path_strip} || "";
3802         for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3803                 my $props = $ed->{dir_prop}{$path};
3804                 if ( $props->{"svk:merge"} ) {
3805                         $self->find_extra_svk_parents
3806                                 ($ed, $props->{"svk:merge"}, \@parents);
3807                 }
3808                 if ( $props->{"svn:mergeinfo"} ) {
3809                         $self->find_extra_svn_parents
3810                                 ($ed,
3811                                  $props->{"svn:mergeinfo"},
3812                                  \@parents);
3813                 }
3814         }
3815
3816         open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
3817         print $un "r$rev\n" or croak $!;
3818         print $un $_, "\n" foreach @$untracked;
3819         my %log_entry = ( parents => \@parents, revision => $rev,
3820                           log => '');
3821
3822         my $headrev;
3823         my $logged = delete $self->{logged_rev_props};
3824         if (!$logged || $self->{-want_revprops}) {
3825                 my $rp = $self->ra->rev_proplist($rev);
3826                 foreach (sort keys %$rp) {
3827                         my $v = $rp->{$_};
3828                         if (/^svn:(author|date|log)$/) {
3829                                 $log_entry{$1} = $v;
3830                         } elsif ($_ eq 'svm:headrev') {
3831                                 $headrev = $v;
3832                         } else {
3833                                 print $un "  rev_prop: ", uri_encode($_), ' ',
3834                                           uri_encode($v), "\n";
3835                         }
3836                 }
3837         } else {
3838                 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
3839         }
3840         close $un or croak $!;
3841
3842         $log_entry{date} = parse_svn_date($log_entry{date});
3843         $log_entry{log} .= "\n";
3844         my $author = $log_entry{author} = check_author($log_entry{author});
3845         my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
3846                                                        : ($author, undef);
3847
3848         my ($commit_name, $commit_email) = ($name, $email);
3849         if ($_use_log_author) {
3850                 my $name_field;
3851                 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3852                         $name_field = $1;
3853                 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3854                         $name_field = $1;
3855                 }
3856                 if (!defined $name_field) {
3857                         if (!defined $email) {
3858                                 $email = $name;
3859                         }
3860                 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
3861                         ($name, $email) = ($1, $2);
3862                 } elsif ($name_field =~ /(.*)@/) {
3863                         ($name, $email) = ($1, $name_field);
3864                 } else {
3865                         ($name, $email) = ($name_field, $name_field);
3866                 }
3867         }
3868         if (defined $headrev && $self->use_svm_props) {
3869                 if ($self->rewrite_root) {
3870                         die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3871                             "options set!\n";
3872                 }
3873                 if ($self->rewrite_uuid) {
3874                         die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
3875                             "options set!\n";
3876                 }
3877                 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
3878                 # we don't want "SVM: initializing mirror for junk" ...
3879                 return undef if $r == 0;
3880                 my $svm = $self->svm;
3881                 if ($uuid ne $svm->{uuid}) {
3882                         die "UUID mismatch on SVM path:\n",
3883                             "expected: $svm->{uuid}\n",
3884                             "     got: $uuid\n";
3885                 }
3886                 my $full_url = $self->full_url;
3887                 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3888                              die "Failed to replace '$svm->{replace}' with ",
3889                                  "'$svm->{source}' in $full_url\n";
3890                 # throw away username for storing in records
3891                 remove_username($full_url);
3892                 $log_entry{metadata} = "$full_url\@$r $uuid";
3893                 $log_entry{svm_revision} = $r;
3894                 $email ||= "$author\@$uuid";
3895                 $commit_email ||= "$author\@$uuid";
3896         } elsif ($self->use_svnsync_props) {
3897                 my $full_url = $self->svnsync->{url};
3898                 $full_url .= "/$self->{path}" if length $self->{path};
3899                 remove_username($full_url);
3900                 my $uuid = $self->svnsync->{uuid};
3901                 $log_entry{metadata} = "$full_url\@$rev $uuid";
3902                 $email ||= "$author\@$uuid";
3903                 $commit_email ||= "$author\@$uuid";
3904         } else {
3905                 my $url = $self->metadata_url;
3906                 remove_username($url);
3907                 my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
3908                 $log_entry{metadata} = "$url\@$rev " . $uuid;
3909                 $email ||= "$author\@" . $uuid;
3910                 $commit_email ||= "$author\@" . $uuid;
3911         }
3912         $log_entry{name} = $name;
3913         $log_entry{email} = $email;
3914         $log_entry{commit_name} = $commit_name;
3915         $log_entry{commit_email} = $commit_email;
3916         \%log_entry;
3917 }
3918
3919 sub fetch {
3920         my ($self, $min_rev, $max_rev, @parents) = @_;
3921         my ($last_rev, $last_commit) = $self->last_rev_commit;
3922         my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
3923         $self->ra->gs_fetch_loop_common($base, $head, [$self]);
3924 }
3925
3926 sub set_tree_cb {
3927         my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
3928         $self->{inject_parents} = { $rev => $tree };
3929         $self->fetch(undef, undef);
3930 }
3931
3932 sub set_tree {
3933         my ($self, $tree) = (shift, shift);
3934         my $log_entry = ::get_commit_entry($tree);
3935         unless ($self->{last_rev}) {
3936                 fatal("Must have an existing revision to commit");
3937         }
3938         my %ed_opts = ( r => $self->{last_rev},
3939                         log => $log_entry->{log},
3940                         ra => $self->ra,
3941                         tree_a => $self->{last_commit},
3942                         tree_b => $tree,
3943                         editor_cb => sub {
3944                                $self->set_tree_cb($log_entry, $tree, @_) },
3945                         svn_path => $self->{path} );
3946         if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
3947                 print "No changes\nr$self->{last_rev} = $tree\n";
3948         }
3949 }
3950
3951 sub rebuild_from_rev_db {
3952         my ($self, $path) = @_;
3953         my $r = -1;
3954         open my $fh, '<', $path or croak "open: $!";
3955         binmode $fh or croak "binmode: $!";
3956         while (<$fh>) {
3957                 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3958                 chomp($_);
3959                 ++$r;
3960                 next if $_ eq ('0' x 40);
3961                 $self->rev_map_set($r, $_);
3962                 print "r$r = $_\n";
3963         }
3964         close $fh or croak "close: $!";
3965         unlink $path or croak "unlink: $!";
3966 }
3967
3968 sub rebuild {
3969         my ($self) = @_;
3970         my $map_path = $self->map_path;
3971         my $partial = (-e $map_path && ! -z $map_path);
3972         return unless ::verify_ref($self->refname.'^0');
3973         if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
3974                 my $rev_db = $self->rev_db_path;
3975                 $self->rebuild_from_rev_db($rev_db);
3976                 if ($self->use_svm_props) {
3977                         my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3978                         $self->rebuild_from_rev_db($svm_rev_db);
3979                 }
3980                 $self->unlink_rev_db_symlink;
3981                 return;
3982         }
3983         print "Rebuilding $map_path ...\n" if (!$partial);
3984         my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3985                 (undef, undef));
3986         my ($log, $ctx) =
3987             command_output_pipe(qw/rev-list --pretty=raw --reverse/,
3988                                 ($head ? "$head.." : "") . $self->refname,
3989                                 '--');
3990         my $metadata_url = $self->metadata_url;
3991         remove_username($metadata_url);
3992         my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
3993         my $c;
3994         while (<$log>) {
3995                 if ( m{^commit ($::sha1)$} ) {
3996                         $c = $1;
3997                         next;
3998                 }
3999                 next unless s{^\s*(git-svn-id:)}{$1};
4000                 my ($url, $rev, $uuid) = ::extract_metadata($_);
4001                 remove_username($url);
4002
4003                 # ignore merges (from set-tree)
4004                 next if (!defined $rev || !$uuid);
4005
4006                 # if we merged or otherwise started elsewhere, this is
4007                 # how we break out of it
4008                 if (($uuid ne $svn_uuid) ||
4009                     ($metadata_url && $url && ($url ne $metadata_url))) {
4010                         next;
4011                 }
4012                 if ($partial && $head) {
4013                         print "Partial-rebuilding $map_path ...\n";
4014                         print "Currently at $base_rev = $head\n";
4015                         $head = undef;
4016                 }
4017
4018                 $self->rev_map_set($rev, $c);
4019                 print "r$rev = $c\n";
4020         }
4021         command_close_pipe($log, $ctx);
4022         print "Done rebuilding $map_path\n" if (!$partial || !$head);
4023         my $rev_db_path = $self->rev_db_path;
4024         if (-f $self->rev_db_path) {
4025                 unlink $self->rev_db_path or croak "unlink: $!";
4026         }
4027         $self->unlink_rev_db_symlink;
4028 }
4029
4030 # rev_map:
4031 # Tie::File seems to be prone to offset errors if revisions get sparse,
4032 # it's not that fast, either.  Tie::File is also not in Perl 5.6.  So
4033 # one of my favorite modules is out :<  Next up would be one of the DBM
4034 # modules, but I'm not sure which is most portable...
4035 #
4036 # This is the replacement for the rev_db format, which was too big
4037 # and inefficient for large repositories with a lot of sparse history
4038 # (mainly tags)
4039 #
4040 # The format is this:
4041 #   - 24 bytes for every record,
4042 #     * 4 bytes for the integer representing an SVN revision number
4043 #     * 20 bytes representing the sha1 of a git commit
4044 #   - No empty padding records like the old format
4045 #     (except the last record, which can be overwritten)
4046 #   - new records are written append-only since SVN revision numbers
4047 #     increase monotonically
4048 #   - lookups on SVN revision number are done via a binary search
4049 #   - Piping the file to xxd -c24 is a good way of dumping it for
4050 #     viewing or editing (piped back through xxd -r), should the need
4051 #     ever arise.
4052 #   - The last record can be padding revision with an all-zero sha1
4053 #     This is used to optimize fetch performance when using multiple
4054 #     "fetch" directives in .git/config
4055 #
4056 # These files are disposable unless noMetadata or useSvmProps is set
4057
4058 sub _rev_map_set {
4059         my ($fh, $rev, $commit) = @_;
4060
4061         binmode $fh or croak "binmode: $!";
4062         my $size = (stat($fh))[7];
4063         ($size % 24) == 0 or croak "inconsistent size: $size";
4064
4065         my $wr_offset = 0;
4066         if ($size > 0) {
4067                 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
4068                 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
4069                 $read == 24 or croak "read only $read bytes (!= 24)";
4070                 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
4071                 if ($last_commit eq ('0' x40)) {
4072                         if ($size >= 48) {
4073                                 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
4074                                 $read = sysread($fh, $buf, 24) or
4075                                     croak "read: $!";
4076                                 $read == 24 or
4077                                     croak "read only $read bytes (!= 24)";
4078                                 ($last_rev, $last_commit) =
4079                                     unpack(rev_map_fmt, $buf);
4080                                 if ($last_commit eq ('0' x40)) {
4081                                         croak "inconsistent .rev_map\n";
4082                                 }
4083                         }
4084                         if ($last_rev >= $rev) {
4085                                 croak "last_rev is higher!: $last_rev >= $rev";
4086                         }
4087                         $wr_offset = -24;
4088                 }
4089         }
4090         sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
4091         syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
4092           croak "write: $!";
4093 }
4094
4095 sub _rev_map_reset {
4096         my ($fh, $rev, $commit) = @_;
4097         my $c = _rev_map_get($fh, $rev);
4098         $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
4099         my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
4100         truncate $fh, $offset or croak "truncate: $!";
4101 }
4102
4103 sub mkfile {
4104         my ($path) = @_;
4105         unless (-e $path) {
4106                 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
4107                 mkpath([$dir]) unless -d $dir;
4108                 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
4109                 close $fh or die "Couldn't close (create) $path: $!\n";
4110         }
4111 }
4112
4113 sub rev_map_set {
4114         my ($self, $rev, $commit, $update_ref, $uuid) = @_;
4115         defined $commit or die "missing arg3\n";
4116         length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
4117         my $db = $self->map_path($uuid);
4118         my $db_lock = "$db.lock";
4119         my $sigmask;
4120         $update_ref ||= 0;
4121         if ($update_ref) {
4122                 $sigmask = POSIX::SigSet->new();
4123                 my $signew = POSIX::SigSet->new(SIGINT, SIGHUP, SIGTERM,
4124                         SIGALRM, SIGUSR1, SIGUSR2);
4125                 sigprocmask(SIG_BLOCK, $signew, $sigmask) or
4126                         croak "Can't block signals: $!";
4127         }
4128         mkfile($db);
4129
4130         $LOCKFILES{$db_lock} = 1;
4131         my $sync;
4132         # both of these options make our .rev_db file very, very important
4133         # and we can't afford to lose it because rebuild() won't work
4134         if ($self->use_svm_props || $self->no_metadata) {
4135                 $sync = 1;
4136                 copy($db, $db_lock) or die "rev_map_set(@_): ",
4137                                            "Failed to copy: ",
4138                                            "$db => $db_lock ($!)\n";
4139         } else {
4140                 rename $db, $db_lock or die "rev_map_set(@_): ",
4141                                             "Failed to rename: ",
4142                                             "$db => $db_lock ($!)\n";
4143         }
4144
4145         sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
4146              or croak "Couldn't open $db_lock: $!\n";
4147         $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
4148                                  _rev_map_set($fh, $rev, $commit);
4149         if ($sync) {
4150                 $fh->flush or die "Couldn't flush $db_lock: $!\n";
4151                 $fh->sync or die "Couldn't sync $db_lock: $!\n";
4152         }
4153         close $fh or croak $!;
4154         if ($update_ref) {
4155                 $_head = $self;
4156                 my $note = "";
4157                 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
4158                 command_noisy('update-ref', '-m', "r$rev$note",
4159                               $self->refname, $commit);
4160         }
4161         rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
4162                                     "$db_lock => $db ($!)\n";
4163         delete $LOCKFILES{$db_lock};
4164         if ($update_ref) {
4165                 sigprocmask(SIG_SETMASK, $sigmask) or
4166                         croak "Can't restore signal mask: $!";
4167         }
4168 }
4169
4170 # If want_commit, this will return an array of (rev, commit) where
4171 # commit _must_ be a valid commit in the archive.
4172 # Otherwise, it'll return the max revision (whether or not the
4173 # commit is valid or just a 0x40 placeholder).
4174 sub rev_map_max {
4175         my ($self, $want_commit) = @_;
4176         $self->rebuild;
4177         my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
4178         $want_commit ? ($r, $c) : $r;
4179 }
4180
4181 sub rev_map_max_norebuild {
4182         my ($self, $want_commit) = @_;
4183         my $map_path = $self->map_path;
4184         stat $map_path or return $want_commit ? (0, undef) : 0;
4185         sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
4186         binmode $fh or croak "binmode: $!";
4187         my $size = (stat($fh))[7];
4188         ($size % 24) == 0 or croak "inconsistent size: $size";
4189
4190         if ($size == 0) {
4191                 close $fh or croak "close: $!";
4192                 return $want_commit ? (0, undef) : 0;
4193         }
4194
4195         sysseek($fh, -24, SEEK_END) or croak "seek: $!";
4196         sysread($fh, my $buf, 24) == 24 or croak "read: $!";
4197         my ($r, $c) = unpack(rev_map_fmt, $buf);
4198         if ($want_commit && $c eq ('0' x40)) {
4199                 if ($size < 48) {
4200                         return $want_commit ? (0, undef) : 0;
4201                 }
4202                 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
4203                 sysread($fh, $buf, 24) == 24 or croak "read: $!";
4204                 ($r, $c) = unpack(rev_map_fmt, $buf);
4205                 if ($c eq ('0'x40)) {
4206                         croak "Penultimate record is all-zeroes in $map_path";
4207                 }
4208         }
4209         close $fh or croak "close: $!";
4210         $want_commit ? ($r, $c) : $r;
4211 }
4212
4213 sub rev_map_get {
4214         my ($self, $rev, $uuid) = @_;
4215         my $map_path = $self->map_path($uuid);
4216         return undef unless -e $map_path;
4217
4218         sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
4219         my $c = _rev_map_get($fh, $rev);
4220         close($fh) or croak "close: $!";
4221         $c
4222 }
4223
4224 sub _rev_map_get {
4225         my ($fh, $rev) = @_;
4226
4227         binmode $fh or croak "binmode: $!";
4228         my $size = (stat($fh))[7];
4229         ($size % 24) == 0 or croak "inconsistent size: $size";
4230
4231         if ($size == 0) {
4232                 return undef;
4233         }
4234
4235         my ($l, $u) = (0, $size - 24);
4236         my ($r, $c, $buf);
4237
4238         while ($l <= $u) {
4239                 my $i = int(($l/24 + $u/24) / 2) * 24;
4240                 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
4241                 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
4242                 my ($r, $c) = unpack(rev_map_fmt, $buf);
4243
4244                 if ($r < $rev) {
4245                         $l = $i + 24;
4246                 } elsif ($r > $rev) {
4247                         $u = $i - 24;
4248                 } else { # $r == $rev
4249                         return $c eq ('0' x 40) ? undef : $c;
4250                 }
4251         }
4252         undef;
4253 }
4254
4255 # Finds the first svn revision that exists on (if $eq_ok is true) or
4256 # before $rev for the current branch.  It will not search any lower
4257 # than $min_rev.  Returns the git commit hash and svn revision number
4258 # if found, else (undef, undef).
4259 sub find_rev_before {
4260         my ($self, $rev, $eq_ok, $min_rev) = @_;
4261         --$rev unless $eq_ok;
4262         $min_rev ||= 1;
4263         my $max_rev = $self->rev_map_max;
4264         $rev = $max_rev if ($rev > $max_rev);
4265         while ($rev >= $min_rev) {
4266                 if (my $c = $self->rev_map_get($rev)) {
4267                         return ($rev, $c);
4268                 }
4269                 --$rev;
4270         }
4271         return (undef, undef);
4272 }
4273
4274 # Finds the first svn revision that exists on (if $eq_ok is true) or
4275 # after $rev for the current branch.  It will not search any higher
4276 # than $max_rev.  Returns the git commit hash and svn revision number
4277 # if found, else (undef, undef).
4278 sub find_rev_after {
4279         my ($self, $rev, $eq_ok, $max_rev) = @_;
4280         ++$rev unless $eq_ok;
4281         $max_rev ||= $self->rev_map_max;
4282         while ($rev <= $max_rev) {
4283                 if (my $c = $self->rev_map_get($rev)) {
4284                         return ($rev, $c);
4285                 }
4286                 ++$rev;
4287         }
4288         return (undef, undef);
4289 }
4290
4291 sub _new {
4292         my ($class, $repo_id, $ref_id, $path) = @_;
4293         unless (defined $repo_id && length $repo_id) {
4294                 $repo_id = $default_repo_id;
4295         }
4296         unless (defined $ref_id && length $ref_id) {
4297                 # Access the prefix option from the git-svn main program if it's loaded.
4298                 my $prefix = defined &::opt_prefix ? ::opt_prefix() : "";
4299                 $_[2] = $ref_id =
4300                              "refs/remotes/$prefix$default_ref_id";
4301         }
4302         $_[1] = $repo_id;
4303         my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
4304
4305         # Older repos imported by us used $GIT_DIR/svn/foo instead of
4306         # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
4307         if ($ref_id =~ m{^refs/remotes/(.*)}) {
4308                 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
4309                 if (-d $old_dir && ! -d $dir) {
4310                         $dir = $old_dir;
4311                 }
4312         }
4313
4314         $_[3] = $path = '' unless (defined $path);
4315         mkpath([$dir]);
4316         bless {
4317                 ref_id => $ref_id, dir => $dir, index => "$dir/index",
4318                 path => $path, config => "$ENV{GIT_DIR}/svn/config",
4319                 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
4320 }
4321
4322 # for read-only access of old .rev_db formats
4323 sub unlink_rev_db_symlink {
4324         my ($self) = @_;
4325         my $link = $self->rev_db_path;
4326         $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
4327         if (-l $link) {
4328                 unlink $link or croak "unlink: $link failed!";
4329         }
4330 }
4331
4332 sub rev_db_path {
4333         my ($self, $uuid) = @_;
4334         my $db_path = $self->map_path($uuid);
4335         $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
4336             or croak "map_path: $db_path does not contain '/.rev_map.' !";
4337         $db_path;
4338 }
4339
4340 # the new replacement for .rev_db
4341 sub map_path {
4342         my ($self, $uuid) = @_;
4343         $uuid ||= $self->ra_uuid;
4344         "$self->{map_root}.$uuid";
4345 }
4346
4347 sub uri_encode {
4348         my ($f) = @_;
4349         $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
4350         $f
4351 }
4352
4353 sub uri_decode {
4354         my ($f) = @_;
4355         $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
4356         $f
4357 }
4358
4359 sub remove_username {
4360         $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
4361 }
4362 }
4363
4364 package Git::SVN::Log;
4365 use strict;
4366 use warnings;
4367 use Git::SVN::Utils qw(fatal);
4368 use POSIX qw/strftime/;
4369 use constant commit_log_separator => ('-' x 72) . "\n";
4370 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4371             %rusers $show_commit $incremental/;
4372 my $l_fmt;
4373
4374 sub cmt_showable {
4375         my ($c) = @_;
4376         return 1 if defined $c->{r};
4377
4378         # big commit message got truncated by the 16k pretty buffer in rev-list
4379         if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4380                                 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
4381                 @{$c->{l}} = ();
4382                 my @log = command(qw/cat-file commit/, $c->{c});
4383
4384                 # shift off the headers
4385                 shift @log while ($log[0] ne '');
4386                 shift @log;
4387
4388                 # TODO: make $c->{l} not have a trailing newline in the future
4389                 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
4390
4391                 (undef, $c->{r}, undef) = ::extract_metadata(
4392                                 (grep(/^git-svn-id: /, @log))[-1]);
4393         }
4394         return defined $c->{r};
4395 }
4396
4397 sub log_use_color {
4398         return $color || Git->repository->get_colorbool('color.diff');
4399 }
4400
4401 sub git_svn_log_cmd {
4402         my ($r_min, $r_max, @args) = @_;
4403         my $head = 'HEAD';
4404         my (@files, @log_opts);
4405         foreach my $x (@args) {
4406                 if ($x eq '--' || @files) {
4407                         push @files, $x;
4408                 } else {
4409                         if (::verify_ref("$x^0")) {
4410                                 $head = $x;
4411                         } else {
4412                                 push @log_opts, $x;
4413                         }
4414                 }
4415         }
4416
4417         my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4418         $gs ||= Git::SVN->_new;
4419         my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4420                    $gs->refname);
4421         push @cmd, '-r' unless $non_recursive;
4422         push @cmd, qw/--raw --name-status/ if $verbose;
4423         push @cmd, '--color' if log_use_color();
4424         push @cmd, @log_opts;
4425         if (defined $r_max && $r_max == $r_min) {
4426                 push @cmd, '--max-count=1';
4427                 if (my $c = $gs->rev_map_get($r_max)) {
4428                         push @cmd, $c;
4429                 }
4430         } elsif (defined $r_max) {
4431                 if ($r_max < $r_min) {
4432                         ($r_min, $r_max) = ($r_max, $r_min);
4433                 }
4434                 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4435                 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4436                 # If there are no commits in the range, both $c_max and $c_min
4437                 # will be undefined.  If there is at least 1 commit in the
4438                 # range, both will be defined.
4439                 return () if !defined $c_min || !defined $c_max;
4440                 if ($c_min eq $c_max) {
4441                         push @cmd, '--max-count=1', $c_min;
4442                 } else {
4443                         push @cmd, '--boundary', "$c_min..$c_max";
4444                 }
4445         }
4446         return (@cmd, @files);
4447 }
4448
4449 # adapted from pager.c
4450 sub config_pager {
4451         if (! -t *STDOUT) {
4452                 $ENV{GIT_PAGER_IN_USE} = 'false';
4453                 $pager = undef;
4454                 return;
4455         }
4456         chomp($pager = command_oneline(qw(var GIT_PAGER)));
4457         if ($pager eq 'cat') {
4458                 $pager = undef;
4459         }
4460         $ENV{GIT_PAGER_IN_USE} = defined($pager);
4461 }
4462
4463 sub run_pager {
4464         return unless defined $pager;
4465         pipe my ($rfd, $wfd) or return;
4466         defined(my $pid = fork) or fatal "Can't fork: $!";
4467         if (!$pid) {
4468                 open STDOUT, '>&', $wfd or
4469                                      fatal "Can't redirect to stdout: $!";
4470                 return;
4471         }
4472         open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
4473         $ENV{LESS} ||= 'FRSX';
4474         exec $pager or fatal "Can't run pager: $! ($pager)";
4475 }
4476
4477 sub format_svn_date {
4478         my $t = shift || time;
4479         my $gmoff = Git::SVN::get_tz($t);
4480         return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
4481 }
4482
4483 sub parse_git_date {
4484         my ($t, $tz) = @_;
4485         # Date::Parse isn't in the standard Perl distro :(
4486         if ($tz =~ s/^\+//) {
4487                 $t += tz_to_s_offset($tz);
4488         } elsif ($tz =~ s/^\-//) {
4489                 $t -= tz_to_s_offset($tz);
4490         }
4491         return $t;
4492 }
4493
4494 sub set_local_timezone {
4495         if (defined $TZ) {
4496                 $ENV{TZ} = $TZ;
4497         } else {
4498                 delete $ENV{TZ};
4499         }
4500 }
4501
4502 sub tz_to_s_offset {
4503         my ($tz) = @_;
4504         $tz =~ s/(\d\d)$//;
4505         return ($1 * 60) + ($tz * 3600);
4506 }
4507
4508 sub get_author_info {
4509         my ($dest, $author, $t, $tz) = @_;
4510         $author =~ s/(?:^\s*|\s*$)//g;
4511         $dest->{a_raw} = $author;
4512         my $au;
4513         if ($::_authors) {
4514                 $au = $rusers{$author} || undef;
4515         }
4516         if (!$au) {
4517                 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4518         }
4519         $dest->{t} = $t;
4520         $dest->{tz} = $tz;
4521         $dest->{a} = $au;
4522         $dest->{t_utc} = parse_git_date($t, $tz);
4523 }
4524
4525 sub process_commit {
4526         my ($c, $r_min, $r_max, $defer) = @_;
4527         if (defined $r_min && defined $r_max) {
4528                 if ($r_min == $c->{r} && $r_min == $r_max) {
4529                         show_commit($c);
4530                         return 0;
4531                 }
4532                 return 1 if $r_min == $r_max;
4533                 if ($r_min < $r_max) {
4534                         # we need to reverse the print order
4535                         return 0 if (defined $limit && --$limit < 0);
4536                         push @$defer, $c;
4537                         return 1;
4538                 }
4539                 if ($r_min != $r_max) {
4540                         return 1 if ($r_min < $c->{r});
4541                         return 1 if ($r_max > $c->{r});
4542                 }
4543         }
4544         return 0 if (defined $limit && --$limit < 0);
4545         show_commit($c);
4546         return 1;
4547 }
4548
4549 sub show_commit {
4550         my $c = shift;
4551         if ($oneline) {
4552                 my $x = "\n";
4553                 if (my $l = $c->{l}) {
4554                         while ($l->[0] =~ /^\s*$/) { shift @$l }
4555                         $x = $l->[0];
4556                 }
4557                 $l_fmt ||= 'A' . length($c->{r});
4558                 print 'r',pack($l_fmt, $c->{r}),' | ';
4559                 print "$c->{c} | " if $show_commit;
4560                 print $x;
4561         } else {
4562                 show_commit_normal($c);
4563         }
4564 }
4565
4566 sub show_commit_changed_paths {
4567         my ($c) = @_;
4568         return unless $c->{changed};
4569         print "Changed paths:\n", @{$c->{changed}};
4570 }
4571
4572 sub show_commit_normal {
4573         my ($c) = @_;
4574         print commit_log_separator, "r$c->{r} | ";
4575         print "$c->{c} | " if $show_commit;
4576         print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
4577         my $nr_line = 0;
4578
4579         if (my $l = $c->{l}) {
4580                 while ($l->[$#$l] eq "\n" && $#$l > 0
4581                                           && $l->[($#$l - 1)] eq "\n") {
4582                         pop @$l;
4583                 }
4584                 $nr_line = scalar @$l;
4585                 if (!$nr_line) {
4586                         print "1 line\n\n\n";
4587                 } else {
4588                         if ($nr_line == 1) {
4589                                 $nr_line = '1 line';
4590                         } else {
4591                                 $nr_line .= ' lines';
4592                         }
4593                         print $nr_line, "\n";
4594                         show_commit_changed_paths($c);
4595                         print "\n";
4596                         print $_ foreach @$l;
4597                 }
4598         } else {
4599                 print "1 line\n";
4600                 show_commit_changed_paths($c);
4601                 print "\n";
4602
4603         }
4604         foreach my $x (qw/raw stat diff/) {
4605                 if ($c->{$x}) {
4606                         print "\n";
4607                         print $_ foreach @{$c->{$x}}
4608                 }
4609         }
4610 }
4611
4612 sub cmd_show_log {
4613         my (@args) = @_;
4614         my ($r_min, $r_max);
4615         my $r_last = -1; # prevent dupes
4616         set_local_timezone();
4617         if (defined $::_revision) {
4618                 if ($::_revision =~ /^(\d+):(\d+)$/) {
4619                         ($r_min, $r_max) = ($1, $2);
4620                 } elsif ($::_revision =~ /^\d+$/) {
4621                         $r_min = $r_max = $::_revision;
4622                 } else {
4623                         fatal "-r$::_revision is not supported, use ",
4624                                 "standard 'git log' arguments instead";
4625                 }
4626         }
4627
4628         config_pager();
4629         @args = git_svn_log_cmd($r_min, $r_max, @args);
4630         if (!@args) {
4631                 print commit_log_separator unless $incremental || $oneline;
4632                 return;
4633         }
4634         my $log = command_output_pipe(@args);
4635         run_pager();
4636         my (@k, $c, $d, $stat);
4637         my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4638         while (<$log>) {
4639                 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
4640                         my $cmt = $1;
4641                         if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4642                                 $r_last = $c->{r};
4643                                 process_commit($c, $r_min, $r_max, \@k) or
4644                                                                 goto out;
4645                         }
4646                         $d = undef;
4647                         $c = { c => $cmt };
4648                 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4649                         get_author_info($c, $1, $2, $3);
4650                 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4651                         # ignore
4652                 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4653                         push @{$c->{raw}}, $_;
4654                 } elsif (/^${esc_color}[ACRMDT]\t/) {
4655                         # we could add $SVN->{svn_path} here, but that requires
4656                         # remote access at the moment (repo_path_split)...
4657                         s#^(${esc_color})([ACRMDT])\t#$1   $2 #o;
4658                         push @{$c->{changed}}, $_;
4659                 } elsif (/^${esc_color}diff /o) {
4660                         $d = 1;
4661                         push @{$c->{diff}}, $_;
4662                 } elsif ($d) {
4663                         push @{$c->{diff}}, $_;
4664                 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4665                           $esc_color*[\+\-]*$esc_color$/x) {
4666                         $stat = 1;
4667                         push @{$c->{stat}}, $_;
4668                 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4669                         push @{$c->{stat}}, $_;
4670                         $stat = undef;
4671                 } elsif (/^${esc_color}    (git-svn-id:.+)$/o) {
4672                         ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4673                 } elsif (s/^${esc_color}    //o) {
4674                         push @{$c->{l}}, $_;
4675                 }
4676         }
4677         if ($c && defined $c->{r} && $c->{r} != $r_last) {
4678                 $r_last = $c->{r};
4679                 process_commit($c, $r_min, $r_max, \@k);
4680         }
4681         if (@k) {
4682                 ($r_min, $r_max) = ($r_max, $r_min);
4683                 process_commit($_, $r_min, $r_max) foreach reverse @k;
4684         }
4685 out:
4686         close $log;
4687         print commit_log_separator unless $incremental || $oneline;
4688 }
4689
4690 sub cmd_blame {
4691         my $path = pop;
4692
4693         config_pager();
4694         run_pager();
4695
4696         my ($fh, $ctx, $rev);
4697
4698         if ($_git_format) {
4699                 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
4700                 while (my $line = <$fh>) {
4701                         if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4702                                 # Uncommitted edits show up as a rev ID of
4703                                 # all zeros, which we can't look up with
4704                                 # cmt_metadata
4705                                 if ($1 !~ /^0+$/) {
4706                                         (undef, $rev, undef) =
4707                                                 ::cmt_metadata($1);
4708                                         $rev = '0' if (!$rev);
4709                                 } else {
4710                                         $rev = '0';
4711                                 }
4712                                 $rev = sprintf('%-10s', $rev);
4713                                 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4714                         }
4715                         print $line;
4716                 }
4717         } else {
4718                 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
4719                                                   '--', $path);
4720                 my ($sha1);
4721                 my %authors;
4722                 my @buffer;
4723                 my %dsha; #distinct sha keys
4724
4725                 while (my $line = <$fh>) {
4726                         push @buffer, $line;
4727                         if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4728                                 $dsha{$1} = 1;
4729                         }
4730                 }
4731
4732                 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
4733
4734                 foreach my $line (@buffer) {
4735                         if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4736                                 $rev = $s2r->{$1};
4737                                 $rev = '0' if (!$rev)
4738                         }
4739                         elsif ($line =~ /^author (.*)/) {
4740                                 $authors{$rev} = $1;
4741                                 $authors{$rev} =~ s/\s/_/g;
4742                         }
4743                         elsif ($line =~ /^\t(.*)$/) {
4744                                 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
4745                         }
4746                 }
4747         }
4748         command_close_pipe($fh, $ctx);
4749 }
4750
4751 package Git::SVN::Migration;
4752 # these version numbers do NOT correspond to actual version numbers
4753 # of git nor git-svn.  They are just relative.
4754 #
4755 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4756 #
4757 # v1 layout: .git/$id/info/url, refs/remotes/$id
4758 #
4759 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4760 #
4761 # v3 layout: .git/svn/$id, refs/remotes/$id
4762 #            - info/url may remain for backwards compatibility
4763 #            - this is what we migrate up to this layout automatically,
4764 #            - this will be used by git svn init on single branches
4765 # v3.1 layout (auto migrated):
4766 #            - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4767 #              for backwards compatibility
4768 #
4769 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4770 #            - this is only created for newly multi-init-ed
4771 #              repositories.  Similar in spirit to the
4772 #              --use-separate-remotes option in git-clone (now default)
4773 #            - we do not automatically migrate to this (following
4774 #              the example set by core git)
4775 #
4776 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
4777 #            - newer, more-efficient format that uses 24-bytes per record
4778 #              with no filler space.
4779 #            - use xxd -c24 < .rev_map.$UUID to view and debug
4780 #            - This is a one-way migration, repositories updated to the
4781 #              new format will not be able to use old git-svn without
4782 #              rebuilding the .rev_db.  Rebuilding the rev_db is not
4783 #              possible if noMetadata or useSvmProps are set; but should
4784 #              be no problem for users that use the (sensible) defaults.
4785 use strict;
4786 use warnings;
4787 use Carp qw/croak/;
4788 use File::Path qw/mkpath/;
4789 use File::Basename qw/dirname basename/;
4790 use vars qw/$_minimize/;
4791
4792 sub migrate_from_v0 {
4793         my $git_dir = $ENV{GIT_DIR};
4794         return undef unless -d $git_dir;
4795         my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4796         my $migrated = 0;
4797         while (<$fh>) {
4798                 chomp;
4799                 my ($id, $orig_ref) = ($_, $_);
4800                 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
4801                 next unless -f "$git_dir/$id/info/url";
4802                 my $new_ref = "refs/remotes/$id";
4803                 if (::verify_ref("$new_ref^0")) {
4804                         print STDERR "W: $orig_ref is probably an old ",
4805                                      "branch used by an ancient version of ",
4806                                      "git-svn.\n",
4807                                      "However, $new_ref also exists.\n",
4808                                      "We will not be able ",
4809                                      "to use this branch until this ",
4810                                      "ambiguity is resolved.\n";
4811                         next;
4812                 }
4813                 print STDERR "Migrating from v0 layout...\n" if !$migrated;
4814                 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
4815                 command_noisy('update-ref', $new_ref, $orig_ref);
4816                 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
4817                 $migrated++;
4818         }
4819         command_close_pipe($fh, $ctx);
4820         print STDERR "Done migrating from v0 layout...\n" if $migrated;
4821         $migrated;
4822 }
4823
4824 sub migrate_from_v1 {
4825         my $git_dir = $ENV{GIT_DIR};
4826         my $migrated = 0;
4827         return $migrated unless -d $git_dir;
4828         my $svn_dir = "$git_dir/svn";
4829
4830         # just in case somebody used 'svn' as their $id at some point...
4831         return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
4832
4833         print STDERR "Migrating from a git-svn v1 layout...\n";
4834         mkpath([$svn_dir]);
4835         print STDERR "Data from a previous version of git-svn exists, but\n\t",
4836                      "$svn_dir\n\t(required for this version ",
4837                      "($::VERSION) of git-svn) does not exist.\n";
4838         my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4839         while (<$fh>) {
4840                 my $x = $_;
4841                 next unless $x =~ s#^refs/remotes/##;
4842                 chomp $x;
4843                 next unless -f "$git_dir/$x/info/url";
4844                 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
4845                 next unless $u;
4846                 my $dn = dirname("$git_dir/svn/$x");
4847                 mkpath([$dn]) unless -d $dn;
4848                 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4849                         mkpath(["$git_dir/svn/svn"]);
4850                         print STDERR " - $git_dir/$x/info => ",
4851                                         "$git_dir/svn/$x/info\n";
4852                         rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4853                                croak "$!: $x";
4854                         # don't worry too much about these, they probably
4855                         # don't exist with repos this old (save for index,
4856                         # and we can easily regenerate that)
4857                         foreach my $f (qw/unhandled.log index .rev_db/) {
4858                                 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4859                         }
4860                 } else {
4861                         print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
4862                         rename "$git_dir/$x", "$git_dir/svn/$x" or
4863                                croak "$!: $x";
4864                 }
4865                 $migrated++;
4866         }
4867         command_close_pipe($fh, $ctx);
4868         print STDERR "Done migrating from a git-svn v1 layout\n";
4869         $migrated;
4870 }
4871
4872 sub read_old_urls {
4873         my ($l_map, $pfx, $path) = @_;
4874         my @dir;
4875         foreach (<$path/*>) {
4876                 if (-r "$_/info/url") {
4877                         $pfx .= '/' if $pfx && $pfx !~ m!/$!;
4878                         my $ref_id = $pfx . basename $_;
4879                         my $url = ::file_to_s("$_/info/url");
4880                         $l_map->{$ref_id} = $url;
4881                 } elsif (-d $_) {
4882                         push @dir, $_;
4883                 }
4884         }
4885         foreach (@dir) {
4886                 my $x = $_;
4887                 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4888                 read_old_urls($l_map, $x, $_);
4889         }
4890 }
4891
4892 sub migrate_from_v2 {
4893         my @cfg = command(qw/config -l/);
4894         return if grep /^svn-remote\..+\.url=/, @cfg;
4895         my %l_map;
4896         read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
4897         my $migrated = 0;
4898
4899         foreach my $ref_id (sort keys %l_map) {
4900                 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
4901                 if ($@) {
4902                         Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4903                 }
4904                 $migrated++;
4905         }
4906         $migrated;
4907 }
4908
4909 sub minimize_connections {
4910         my $r = Git::SVN::read_all_remotes();
4911         my $new_urls = {};
4912         my $root_repos = {};
4913         foreach my $repo_id (keys %$r) {
4914                 my $url = $r->{$repo_id}->{url} or next;
4915                 my $fetch = $r->{$repo_id}->{fetch} or next;
4916                 my $ra = Git::SVN::Ra->new($url);
4917
4918                 # skip existing cases where we already connect to the root
4919                 if (($ra->{url} eq $ra->{repos_root}) ||
4920                     ($ra->{repos_root} eq $repo_id)) {
4921                         $root_repos->{$ra->{url}} = $repo_id;
4922                         next;
4923                 }
4924
4925                 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
4926                 my $root_path = $ra->{url};
4927                 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
4928                 foreach my $path (keys %$fetch) {
4929                         my $ref_id = $fetch->{$path};
4930                         my $gs = Git::SVN->new($ref_id, $repo_id, $path);
4931
4932                         # make sure we can read when connecting to
4933                         # a higher level of a repository
4934                         my ($last_rev, undef) = $gs->last_rev_commit;
4935                         if (!defined $last_rev) {
4936                                 $last_rev = eval {
4937                                         $root_ra->get_latest_revnum;
4938                                 };
4939                                 next if $@;
4940                         }
4941                         my $new = $root_path;
4942                         $new .= length $path ? "/$path" : '';
4943                         eval {
4944                                 $root_ra->get_log([$new], $last_rev, $last_rev,
4945                                                   0, 0, 1, sub { });
4946                         };
4947                         next if $@;
4948                         $new_urls->{$ra->{repos_root}}->{$new} =
4949                                 { ref_id => $ref_id,
4950                                   old_repo_id => $repo_id,
4951                                   old_path => $path };
4952                 }
4953         }
4954
4955         my @emptied;
4956         foreach my $url (keys %$new_urls) {
4957                 # see if we can re-use an existing [svn-remote "repo_id"]
4958                 # instead of creating a(n ugly) new section:
4959                 my $repo_id = $root_repos->{$url} || $url;
4960
4961                 my $fetch = $new_urls->{$url};
4962                 foreach my $path (keys %$fetch) {
4963                         my $x = $fetch->{$path};
4964                         Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
4965                         my $pfx = "svn-remote.$x->{old_repo_id}";
4966
4967                         my $old_fetch = quotemeta("$x->{old_path}:".
4968                                                   "$x->{ref_id}");
4969                         command_noisy(qw/config --unset/,
4970                                       "$pfx.fetch", '^'. $old_fetch . '$');
4971                         delete $r->{$x->{old_repo_id}}->
4972                                {fetch}->{$x->{old_path}};
4973                         if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
4974                                 command_noisy(qw/config --unset/,
4975                                               "$pfx.url");
4976                                 push @emptied, $x->{old_repo_id}
4977                         }
4978                 }
4979         }
4980         if (@emptied) {
4981                 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
4982                 print STDERR <<EOF;
4983 The following [svn-remote] sections in your config file ($file) are empty
4984 and can be safely removed:
4985 EOF
4986                 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
4987         }
4988 }
4989
4990 sub migration_check {
4991         migrate_from_v0();
4992         migrate_from_v1();
4993         migrate_from_v2();
4994         minimize_connections() if $_minimize;
4995 }
4996
4997 package Git::IndexInfo;
4998 use strict;
4999 use warnings;
5000 use Git qw/command_input_pipe command_close_pipe/;
5001
5002 sub new {
5003         my ($class) = @_;
5004         my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5005         bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5006 }
5007
5008 sub remove {
5009         my ($self, $path) = @_;
5010         if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5011                 return ++$self->{nr};
5012         }
5013         undef;
5014 }
5015
5016 sub update {
5017         my ($self, $mode, $hash, $path) = @_;
5018         if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5019                 return ++$self->{nr};
5020         }
5021         undef;
5022 }
5023
5024 sub DESTROY {
5025         my ($self) = @_;
5026         command_close_pipe($self->{gui}, $self->{ctx});
5027 }
5028
5029 package Git::SVN::GlobSpec;
5030 use strict;
5031 use warnings;
5032
5033 sub new {
5034         my ($class, $glob, $pattern_ok) = @_;
5035         my $re = $glob;
5036         $re =~ s!/+$!!g; # no need for trailing slashes
5037         my (@left, @right, @patterns);
5038         my $state = "left";
5039         my $die_msg = "Only one set of wildcard directories " .
5040                                 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5041         for my $part (split(m|/|, $glob)) {
5042                 if ($part =~ /\*/ && $part ne "*") {
5043                         die "Invalid pattern in '$glob': $part\n";
5044                 } elsif ($pattern_ok && $part =~ /[{}]/ &&
5045                          $part !~ /^\{[^{}]+\}/) {
5046                         die "Invalid pattern in '$glob': $part\n";
5047                 }
5048                 if ($part eq "*") {
5049                         die $die_msg if $state eq "right";
5050                         $state = "pattern";
5051                         push(@patterns, "[^/]*");
5052                 } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) {
5053                         die $die_msg if $state eq "right";
5054                         $state = "pattern";
5055                         my $p = quotemeta($1);
5056                         $p =~ s/\\,/|/g;
5057                         push(@patterns, "(?:$p)");
5058                 } else {
5059                         if ($state eq "left") {
5060                                 push(@left, $part);
5061                         } else {
5062                                 push(@right, $part);
5063                                 $state = "right";
5064                         }
5065                 }
5066         }
5067         my $depth = @patterns;
5068         if ($depth == 0) {
5069                 die "One '*' is needed in glob: '$glob'\n";
5070         }
5071         my $left = join('/', @left);
5072         my $right = join('/', @right);
5073         $re = join('/', @patterns);
5074         $re = join('\/',
5075                    grep(length, quotemeta($left), "($re)", quotemeta($right)));
5076         my $left_re = qr/^\/\Q$left\E(\/|$)/;
5077         bless { left => $left, right => $right, left_regex => $left_re,
5078                 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
5079 }
5080
5081 sub full_path {
5082         my ($self, $path) = @_;
5083         return (length $self->{left} ? "$self->{left}/" : '') .
5084                $path . (length $self->{right} ? "/$self->{right}" : '');
5085 }
5086
5087 __END__
5088
5089 Data structures:
5090
5091
5092 $remotes = { # returned by read_all_remotes()
5093         'svn' => {
5094                 # svn-remote.svn.url=https://svn.musicpd.org
5095                 url => 'https://svn.musicpd.org',
5096                 # svn-remote.svn.fetch=mpd/trunk:trunk
5097                 fetch => {
5098                         'mpd/trunk' => 'trunk',
5099                 },
5100                 # svn-remote.svn.tags=mpd/tags/*:tags/*
5101                 tags => {
5102                         path => {
5103                                 left => 'mpd/tags',
5104                                 right => '',
5105                                 regex => qr!mpd/tags/([^/]+)$!,
5106                                 glob => 'tags/*',
5107                         },
5108                         ref => {
5109                                 left => 'tags',
5110                                 right => '',
5111                                 regex => qr!tags/([^/]+)$!,
5112                                 glob => 'tags/*',
5113                         },
5114                 }
5115         }
5116 };
5117
5118 $log_entry hashref as returned by libsvn_log_entry()
5119 {
5120         log => 'whitespace-formatted log entry
5121 ',                                              # trailing newline is preserved
5122         revision => '8',                        # integer
5123         date => '2004-02-24T17:01:44.108345Z',  # commit date
5124         author => 'committer name'
5125 };
5126
5127
5128 # this is generated by generate_diff();
5129 @mods = array of diff-index line hashes, each element represents one line
5130         of diff-index output
5131
5132 diff-index line ($m hash)
5133 {
5134         mode_a => first column of diff-index output, no leading ':',
5135         mode_b => second column of diff-index output,
5136         sha1_b => sha1sum of the final blob,
5137         chg => change type [MCRADT],
5138         file_a => original file name of a file (iff chg is 'C' or 'R')
5139         file_b => new/current file name of a file (any chg)
5140 }
5141 ;
5142
5143 # retval of read_url_paths{,_all}();
5144 $l_map = {
5145         # repository root url
5146         'https://svn.musicpd.org' => {
5147                 # repository path               # GIT_SVN_ID
5148                 'mpd/trunk'             =>      'trunk',
5149                 'mpd/tags/0.11.5'       =>      'tags/0.11.5',
5150         },
5151 }
5152
5153 Notes:
5154         I don't trust the each() function on unless I created %hash myself
5155         because the internal iterator may not have started at base.