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