perl / Git / SVN.pmon commit Merge branch 'maint-1.7.11' into maint (871e293)
   1package Git::SVN;
   2use strict;
   3use warnings;
   4use Fcntl qw/:DEFAULT :seek/;
   5use constant rev_map_fmt => 'NH40';
   6use vars qw/$_no_metadata
   7            $_repack $_repack_flags $_use_svm_props $_head
   8            $_use_svnsync_props $no_reuse_existing
   9            $_use_log_author $_add_author_from $_localtime/;
  10use Carp qw/croak/;
  11use File::Path qw/mkpath/;
  12use File::Copy qw/copy/;
  13use IPC::Open3;
  14use Time::Local;
  15use Memoize;  # core since 5.8.0, Jul 2002
  16use Memoize::Storable;
  17use POSIX qw(:signal_h);
  18
  19use Git qw(
  20    command
  21    command_oneline
  22    command_noisy
  23    command_output_pipe
  24    command_close_pipe
  25);
  26use Git::SVN::Utils qw(fatal can_compress);
  27
  28my $can_use_yaml;
  29BEGIN {
  30        $can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
  31}
  32
  33our $_follow_parent  = 1;
  34our $_minimize_url   = 'unset';
  35our $default_repo_id = 'svn';
  36our $default_ref_id  = $ENV{GIT_SVN_ID} || 'git-svn';
  37
  38my ($_gc_nr, $_gc_period);
  39
  40# properties that we do not log:
  41my %SKIP_PROP;
  42BEGIN {
  43        %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
  44                                        svn:special svn:executable
  45                                        svn:entry:committed-rev
  46                                        svn:entry:last-author
  47                                        svn:entry:uuid
  48                                        svn:entry:committed-date/;
  49
  50        # some options are read globally, but can be overridden locally
  51        # per [svn-remote "..."] section.  Command-line options will *NOT*
  52        # override options set in an [svn-remote "..."] section
  53        no strict 'refs';
  54        for my $option (qw/follow_parent no_metadata use_svm_props
  55                           use_svnsync_props/) {
  56                my $key = $option;
  57                $key =~ tr/_//d;
  58                my $prop = "-$option";
  59                *$option = sub {
  60                        my ($self) = @_;
  61                        return $self->{$prop} if exists $self->{$prop};
  62                        my $k = "svn-remote.$self->{repo_id}.$key";
  63                        eval { command_oneline(qw/config --get/, $k) };
  64                        if ($@) {
  65                                $self->{$prop} = ${"Git::SVN::_$option"};
  66                        } else {
  67                                my $v = command_oneline(qw/config --bool/,$k);
  68                                $self->{$prop} = $v eq 'false' ? 0 : 1;
  69                        }
  70                        return $self->{$prop};
  71                }
  72        }
  73}
  74
  75
  76my (%LOCKFILES, %INDEX_FILES);
  77END {
  78        unlink keys %LOCKFILES if %LOCKFILES;
  79        unlink keys %INDEX_FILES if %INDEX_FILES;
  80}
  81
  82sub resolve_local_globs {
  83        my ($url, $fetch, $glob_spec) = @_;
  84        return unless defined $glob_spec;
  85        my $ref = $glob_spec->{ref};
  86        my $path = $glob_spec->{path};
  87        foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
  88                next unless m#^$ref->{regex}$#;
  89                my $p = $1;
  90                my $pathname = desanitize_refname($path->full_path($p));
  91                my $refname = desanitize_refname($ref->full_path($p));
  92                if (my $existing = $fetch->{$pathname}) {
  93                        if ($existing ne $refname) {
  94                                die "Refspec conflict:\n",
  95                                    "existing: $existing\n",
  96                                    " globbed: $refname\n";
  97                        }
  98                        my $u = (::cmt_metadata("$refname"))[0];
  99                        $u =~ s!^\Q$url\E(/|$)!! or die
 100                          "$refname: '$url' not found in '$u'\n";
 101                        if ($pathname ne $u) {
 102                                warn "W: Refspec glob conflict ",
 103                                     "(ref: $refname):\n",
 104                                     "expected path: $pathname\n",
 105                                     "    real path: $u\n",
 106                                     "Continuing ahead with $u\n";
 107                                next;
 108                        }
 109                } else {
 110                        $fetch->{$pathname} = $refname;
 111                }
 112        }
 113}
 114
 115sub parse_revision_argument {
 116        my ($base, $head) = @_;
 117        if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
 118                return ($base, $head);
 119        }
 120        return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
 121        return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
 122        return ($head, $head) if ($::_revision eq 'HEAD');
 123        return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
 124        return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
 125        die "revision argument: $::_revision not understood by git-svn\n";
 126}
 127
 128sub fetch_all {
 129        my ($repo_id, $remotes) = @_;
 130        if (ref $repo_id) {
 131                my $gs = $repo_id;
 132                $repo_id = undef;
 133                $repo_id = $gs->{repo_id};
 134        }
 135        $remotes ||= read_all_remotes();
 136        my $remote = $remotes->{$repo_id} or
 137                     die "[svn-remote \"$repo_id\"] unknown\n";
 138        my $fetch = $remote->{fetch};
 139        my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
 140        my (@gs, @globs);
 141        my $ra = Git::SVN::Ra->new($url);
 142        my $uuid = $ra->get_uuid;
 143        my $head = $ra->get_latest_revnum;
 144
 145        # ignore errors, $head revision may not even exist anymore
 146        eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
 147        warn "W: $@\n" if $@;
 148
 149        my $base = defined $fetch ? $head : 0;
 150
 151        # read the max revs for wildcard expansion (branches/*, tags/*)
 152        foreach my $t (qw/branches tags/) {
 153                defined $remote->{$t} or next;
 154                push @globs, @{$remote->{$t}};
 155
 156                my $max_rev = eval { tmp_config(qw/--int --get/,
 157                                         "svn-remote.$repo_id.${t}-maxRev") };
 158                if (defined $max_rev && ($max_rev < $base)) {
 159                        $base = $max_rev;
 160                } elsif (!defined $max_rev) {
 161                        $base = 0;
 162                }
 163        }
 164
 165        if ($fetch) {
 166                foreach my $p (sort keys %$fetch) {
 167                        my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
 168                        my $lr = $gs->rev_map_max;
 169                        if (defined $lr) {
 170                                $base = $lr if ($lr < $base);
 171                        }
 172                        push @gs, $gs;
 173                }
 174        }
 175
 176        ($base, $head) = parse_revision_argument($base, $head);
 177        $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
 178}
 179
 180sub read_all_remotes {
 181        my $r = {};
 182        my $use_svm_props = eval { command_oneline(qw/config --bool
 183            svn.useSvmProps/) };
 184        $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
 185        my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
 186        foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
 187                if (m!^(.+)\.fetch=$svn_refspec$!) {
 188                        my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
 189                        die("svn-remote.$remote: remote ref '$remote_ref' "
 190                            . "must start with 'refs/'\n")
 191                                unless $remote_ref =~ m{^refs/};
 192                        $local_ref = uri_decode($local_ref);
 193                        $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
 194                        $r->{$remote}->{svm} = {} if $use_svm_props;
 195                } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
 196                        $r->{$1}->{svm} = {};
 197                } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
 198                        $r->{$1}->{url} = $2;
 199                } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
 200                        $r->{$1}->{pushurl} = $2;
 201                } elsif (m!^(.+)\.ignore-refs=\s*(.*)\s*$!) {
 202                        $r->{$1}->{ignore_refs_regex} = $2;
 203                } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
 204                        my ($remote, $t, $local_ref, $remote_ref) =
 205                                                             ($1, $2, $3, $4);
 206                        die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
 207                            . "must start with 'refs/'\n")
 208                                unless $remote_ref =~ m{^refs/};
 209                        $local_ref = uri_decode($local_ref);
 210
 211                        require Git::SVN::GlobSpec;
 212                        my $rs = {
 213                            t => $t,
 214                            remote => $remote,
 215                            path => Git::SVN::GlobSpec->new($local_ref, 1),
 216                            ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
 217                        if (length($rs->{ref}->{right}) != 0) {
 218                                die "The '*' glob character must be the last ",
 219                                    "character of '$remote_ref'\n";
 220                        }
 221                        push @{ $r->{$remote}->{$t} }, $rs;
 222                }
 223        }
 224
 225        map {
 226                if (defined $r->{$_}->{svm}) {
 227                        my $svm;
 228                        eval {
 229                                my $section = "svn-remote.$_";
 230                                $svm = {
 231                                        source => tmp_config('--get',
 232                                            "$section.svm-source"),
 233                                        replace => tmp_config('--get',
 234                                            "$section.svm-replace"),
 235                                }
 236                        };
 237                        $r->{$_}->{svm} = $svm;
 238                }
 239        } keys %$r;
 240
 241        foreach my $remote (keys %$r) {
 242                foreach ( grep { defined $_ }
 243                          map { $r->{$remote}->{$_} } qw(branches tags) ) {
 244                        foreach my $rs ( @$_ ) {
 245                                $rs->{ignore_refs_regex} =
 246                                    $r->{$remote}->{ignore_refs_regex};
 247                        }
 248                }
 249        }
 250
 251        $r;
 252}
 253
 254sub init_vars {
 255        $_gc_nr = $_gc_period = 1000;
 256        if (defined $_repack || defined $_repack_flags) {
 257               warn "Repack options are obsolete; they have no effect.\n";
 258        }
 259}
 260
 261sub verify_remotes_sanity {
 262        return unless -d $ENV{GIT_DIR};
 263        my %seen;
 264        foreach (command(qw/config -l/)) {
 265                if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
 266                        if ($seen{$1}) {
 267                                die "Remote ref refs/remote/$1 is tracked by",
 268                                    "\n  \"$_\"\nand\n  \"$seen{$1}\"\n",
 269                                    "Please resolve this ambiguity in ",
 270                                    "your git configuration file before ",
 271                                    "continuing\n";
 272                        }
 273                        $seen{$1} = $_;
 274                }
 275        }
 276}
 277
 278sub find_existing_remote {
 279        my ($url, $remotes) = @_;
 280        return undef if $no_reuse_existing;
 281        my $existing;
 282        foreach my $repo_id (keys %$remotes) {
 283                my $u = $remotes->{$repo_id}->{url} or next;
 284                next if $u ne $url;
 285                $existing = $repo_id;
 286                last;
 287        }
 288        $existing;
 289}
 290
 291sub init_remote_config {
 292        my ($self, $url, $no_write) = @_;
 293        $url =~ s!/+$!!; # strip trailing slash
 294        my $r = read_all_remotes();
 295        my $existing = find_existing_remote($url, $r);
 296        if ($existing) {
 297                unless ($no_write) {
 298                        print STDERR "Using existing ",
 299                                     "[svn-remote \"$existing\"]\n";
 300                }
 301                $self->{repo_id} = $existing;
 302        } elsif ($_minimize_url) {
 303                my $min_url = Git::SVN::Ra->new($url)->minimize_url;
 304                $existing = find_existing_remote($min_url, $r);
 305                if ($existing) {
 306                        unless ($no_write) {
 307                                print STDERR "Using existing ",
 308                                             "[svn-remote \"$existing\"]\n";
 309                        }
 310                        $self->{repo_id} = $existing;
 311                }
 312                if ($min_url ne $url) {
 313                        unless ($no_write) {
 314                                print STDERR "Using higher level of URL: ",
 315                                             "$url => $min_url\n";
 316                        }
 317                        my $old_path = $self->{path};
 318                        $self->{path} = $url;
 319                        $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
 320                        if (length $old_path) {
 321                                $self->{path} .= "/$old_path";
 322                        }
 323                        $url = $min_url;
 324                }
 325        }
 326        my $orig_url;
 327        if (!$existing) {
 328                # verify that we aren't overwriting anything:
 329                $orig_url = eval {
 330                        command_oneline('config', '--get',
 331                                        "svn-remote.$self->{repo_id}.url")
 332                };
 333                if ($orig_url && ($orig_url ne $url)) {
 334                        die "svn-remote.$self->{repo_id}.url already set: ",
 335                            "$orig_url\nwanted to set to: $url\n";
 336                }
 337        }
 338        my ($xrepo_id, $xpath) = find_ref($self->refname);
 339        if (!$no_write && defined $xpath) {
 340                die "svn-remote.$xrepo_id.fetch already set to track ",
 341                    "$xpath:", $self->refname, "\n";
 342        }
 343        unless ($no_write) {
 344                command_noisy('config',
 345                              "svn-remote.$self->{repo_id}.url", $url);
 346                $self->{path} =~ s{^/}{};
 347                $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
 348                command_noisy('config', '--add',
 349                              "svn-remote.$self->{repo_id}.fetch",
 350                              "$self->{path}:".$self->refname);
 351        }
 352        $self->{url} = $url;
 353}
 354
 355sub find_by_url { # repos_root and, path are optional
 356        my ($class, $full_url, $repos_root, $path) = @_;
 357
 358        return undef unless defined $full_url;
 359        remove_username($full_url);
 360        remove_username($repos_root) if defined $repos_root;
 361        my $remotes = read_all_remotes();
 362        if (defined $full_url && defined $repos_root && !defined $path) {
 363                $path = $full_url;
 364                $path =~ s#^\Q$repos_root\E(?:/|$)##;
 365        }
 366        foreach my $repo_id (keys %$remotes) {
 367                my $u = $remotes->{$repo_id}->{url} or next;
 368                remove_username($u);
 369                next if defined $repos_root && $repos_root ne $u;
 370
 371                my $fetch = $remotes->{$repo_id}->{fetch} || {};
 372                foreach my $t (qw/branches tags/) {
 373                        foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
 374                                resolve_local_globs($u, $fetch, $globspec);
 375                        }
 376                }
 377                my $p = $path;
 378                my $rwr = rewrite_root({repo_id => $repo_id});
 379                my $svm = $remotes->{$repo_id}->{svm}
 380                        if defined $remotes->{$repo_id}->{svm};
 381                unless (defined $p) {
 382                        $p = $full_url;
 383                        my $z = $u;
 384                        my $prefix = '';
 385                        if ($rwr) {
 386                                $z = $rwr;
 387                                remove_username($z);
 388                        } elsif (defined $svm) {
 389                                $z = $svm->{source};
 390                                $prefix = $svm->{replace};
 391                                $prefix =~ s#^\Q$u\E(?:/|$)##;
 392                                $prefix =~ s#/$##;
 393                        }
 394                        $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
 395                }
 396                foreach my $f (keys %$fetch) {
 397                        next if $f ne $p;
 398                        return Git::SVN->new($fetch->{$f}, $repo_id, $f);
 399                }
 400        }
 401        undef;
 402}
 403
 404sub init {
 405        my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
 406        my $self = _new($class, $repo_id, $ref_id, $path);
 407        if (defined $url) {
 408                $self->init_remote_config($url, $no_write);
 409        }
 410        $self;
 411}
 412
 413sub find_ref {
 414        my ($ref_id) = @_;
 415        foreach (command(qw/config -l/)) {
 416                next unless m!^svn-remote\.(.+)\.fetch=
 417                              \s*(.*?)\s*:\s*(.+?)\s*$!x;
 418                my ($repo_id, $path, $ref) = ($1, $2, $3);
 419                if ($ref eq $ref_id) {
 420                        $path = '' if ($path =~ m#^\./?#);
 421                        return ($repo_id, $path);
 422                }
 423        }
 424        (undef, undef, undef);
 425}
 426
 427sub new {
 428        my ($class, $ref_id, $repo_id, $path) = @_;
 429        if (defined $ref_id && !defined $repo_id && !defined $path) {
 430                ($repo_id, $path) = find_ref($ref_id);
 431                if (!defined $repo_id) {
 432                        die "Could not find a \"svn-remote.*.fetch\" key ",
 433                            "in the repository configuration matching: ",
 434                            "$ref_id\n";
 435                }
 436        }
 437        my $self = _new($class, $repo_id, $ref_id, $path);
 438        if (!defined $self->{path} || !length $self->{path}) {
 439                my $fetch = command_oneline('config', '--get',
 440                                            "svn-remote.$repo_id.fetch",
 441                                            ":$ref_id\$") or
 442                     die "Failed to read \"svn-remote.$repo_id.fetch\" ",
 443                         "\":$ref_id\$\" in config\n";
 444                ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
 445        }
 446        $self->{path} =~ s{/+}{/}g;
 447        $self->{path} =~ s{\A/}{};
 448        $self->{path} =~ s{/\z}{};
 449        $self->{url} = command_oneline('config', '--get',
 450                                       "svn-remote.$repo_id.url") or
 451                  die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
 452        $self->{pushurl} = eval { command_oneline('config', '--get',
 453                                  "svn-remote.$repo_id.pushurl") };
 454        $self->rebuild;
 455        $self;
 456}
 457
 458sub refname {
 459        my ($refname) = $_[0]->{ref_id} ;
 460
 461        # It cannot end with a slash /, we'll throw up on this because
 462        # SVN can't have directories with a slash in their name, either:
 463        if ($refname =~ m{/$}) {
 464                die "ref: '$refname' ends with a trailing slash, this is ",
 465                    "not permitted by git nor Subversion\n";
 466        }
 467
 468        # It cannot have ASCII control character space, tilde ~, caret ^,
 469        # colon :, question-mark ?, asterisk *, space, or open bracket [
 470        # anywhere.
 471        #
 472        # Additionally, % must be escaped because it is used for escaping
 473        # and we want our escaped refname to be reversible
 474        $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
 475
 476        # no slash-separated component can begin with a dot .
 477        # /.* becomes /%2E*
 478        $refname =~ s{/\.}{/%2E}g;
 479
 480        # It cannot have two consecutive dots .. anywhere
 481        # .. becomes %2E%2E
 482        $refname =~ s{\.\.}{%2E%2E}g;
 483
 484        # trailing dots and .lock are not allowed
 485        # .$ becomes %2E and .lock becomes %2Elock
 486        $refname =~ s{\.(?=$|lock$)}{%2E};
 487
 488        # the sequence @{ is used to access the reflog
 489        # @{ becomes %40{
 490        $refname =~ s{\@\{}{%40\{}g;
 491
 492        return $refname;
 493}
 494
 495sub desanitize_refname {
 496        my ($refname) = @_;
 497        $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
 498        return $refname;
 499}
 500
 501sub svm_uuid {
 502        my ($self) = @_;
 503        return $self->{svm}->{uuid} if $self->svm;
 504        $self->ra;
 505        unless ($self->{svm}) {
 506                die "SVM UUID not cached, and reading remotely failed\n";
 507        }
 508        $self->{svm}->{uuid};
 509}
 510
 511sub svm {
 512        my ($self) = @_;
 513        return $self->{svm} if $self->{svm};
 514        my $svm;
 515        # see if we have it in our config, first:
 516        eval {
 517                my $section = "svn-remote.$self->{repo_id}";
 518                $svm = {
 519                  source => tmp_config('--get', "$section.svm-source"),
 520                  uuid => tmp_config('--get', "$section.svm-uuid"),
 521                  replace => tmp_config('--get', "$section.svm-replace"),
 522                }
 523        };
 524        if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
 525                $self->{svm} = $svm;
 526        }
 527        $self->{svm};
 528}
 529
 530sub _set_svm_vars {
 531        my ($self, $ra) = @_;
 532        return $ra if $self->svm;
 533
 534        my @err = ( "useSvmProps set, but failed to read SVM properties\n",
 535                    "(svm:source, svm:uuid) ",
 536                    "from the following URLs:\n" );
 537        sub read_svm_props {
 538                my ($self, $ra, $path, $r) = @_;
 539                my $props = ($ra->get_dir($path, $r))[2];
 540                my $src = $props->{'svm:source'};
 541                my $uuid = $props->{'svm:uuid'};
 542                return undef if (!$src || !$uuid);
 543
 544                chomp($src, $uuid);
 545
 546                $uuid =~ m{^[0-9a-f\-]{30,}$}i
 547                    or die "doesn't look right - svm:uuid is '$uuid'\n";
 548
 549                # the '!' is used to mark the repos_root!/relative/path
 550                $src =~ s{/?!/?}{/};
 551                $src =~ s{/+$}{}; # no trailing slashes please
 552                # username is of no interest
 553                $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
 554
 555                my $replace = $ra->{url};
 556                $replace .= "/$path" if length $path;
 557
 558                my $section = "svn-remote.$self->{repo_id}";
 559                tmp_config("$section.svm-source", $src);
 560                tmp_config("$section.svm-replace", $replace);
 561                tmp_config("$section.svm-uuid", $uuid);
 562                $self->{svm} = {
 563                        source => $src,
 564                        uuid => $uuid,
 565                        replace => $replace
 566                };
 567        }
 568
 569        my $r = $ra->get_latest_revnum;
 570        my $path = $self->{path};
 571        my %tried;
 572        while (length $path) {
 573                unless ($tried{"$self->{url}/$path"}) {
 574                        return $ra if $self->read_svm_props($ra, $path, $r);
 575                        $tried{"$self->{url}/$path"} = 1;
 576                }
 577                $path =~ s#/?[^/]+$##;
 578        }
 579        die "Path: '$path' should be ''\n" if $path ne '';
 580        return $ra if $self->read_svm_props($ra, $path, $r);
 581        $tried{"$self->{url}/$path"} = 1;
 582
 583        if ($ra->{repos_root} eq $self->{url}) {
 584                die @err, (map { "  $_\n" } keys %tried), "\n";
 585        }
 586
 587        # nope, make sure we're connected to the repository root:
 588        my $ok;
 589        my @tried_b;
 590        $path = $ra->{svn_path};
 591        $ra = Git::SVN::Ra->new($ra->{repos_root});
 592        while (length $path) {
 593                unless ($tried{"$ra->{url}/$path"}) {
 594                        $ok = $self->read_svm_props($ra, $path, $r);
 595                        last if $ok;
 596                        $tried{"$ra->{url}/$path"} = 1;
 597                }
 598                $path =~ s#/?[^/]+$##;
 599        }
 600        die "Path: '$path' should be ''\n" if $path ne '';
 601        $ok ||= $self->read_svm_props($ra, $path, $r);
 602        $tried{"$ra->{url}/$path"} = 1;
 603        if (!$ok) {
 604                die @err, (map { "  $_\n" } keys %tried), "\n";
 605        }
 606        Git::SVN::Ra->new($self->{url});
 607}
 608
 609sub svnsync {
 610        my ($self) = @_;
 611        return $self->{svnsync} if $self->{svnsync};
 612
 613        if ($self->no_metadata) {
 614                die "Can't have both 'noMetadata' and ",
 615                    "'useSvnsyncProps' options set!\n";
 616        }
 617        if ($self->rewrite_root) {
 618                die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
 619                    "options set!\n";
 620        }
 621        if ($self->rewrite_uuid) {
 622                die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
 623                    "options set!\n";
 624        }
 625
 626        my $svnsync;
 627        # see if we have it in our config, first:
 628        eval {
 629                my $section = "svn-remote.$self->{repo_id}";
 630
 631                my $url = tmp_config('--get', "$section.svnsync-url");
 632                ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
 633                   die "doesn't look right - svn:sync-from-url is '$url'\n";
 634
 635                my $uuid = tmp_config('--get', "$section.svnsync-uuid");
 636                ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
 637                   die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
 638
 639                $svnsync = { url => $url, uuid => $uuid }
 640        };
 641        if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
 642                return $self->{svnsync} = $svnsync;
 643        }
 644
 645        my $err = "useSvnsyncProps set, but failed to read " .
 646                  "svnsync property: svn:sync-from-";
 647        my $rp = $self->ra->rev_proplist(0);
 648
 649        my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
 650        ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
 651                   die "doesn't look right - svn:sync-from-url is '$url'\n";
 652
 653        my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
 654        ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
 655                   die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
 656
 657        my $section = "svn-remote.$self->{repo_id}";
 658        tmp_config('--add', "$section.svnsync-uuid", $uuid);
 659        tmp_config('--add', "$section.svnsync-url", $url);
 660        return $self->{svnsync} = { url => $url, uuid => $uuid };
 661}
 662
 663# this allows us to memoize our SVN::Ra UUID locally and avoid a
 664# remote lookup (useful for 'git svn log').
 665sub ra_uuid {
 666        my ($self) = @_;
 667        unless ($self->{ra_uuid}) {
 668                my $key = "svn-remote.$self->{repo_id}.uuid";
 669                my $uuid = eval { tmp_config('--get', $key) };
 670                if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
 671                        $self->{ra_uuid} = $uuid;
 672                } else {
 673                        die "ra_uuid called without URL\n" unless $self->{url};
 674                        $self->{ra_uuid} = $self->ra->get_uuid;
 675                        tmp_config('--add', $key, $self->{ra_uuid});
 676                }
 677        }
 678        $self->{ra_uuid};
 679}
 680
 681sub _set_repos_root {
 682        my ($self, $repos_root) = @_;
 683        my $k = "svn-remote.$self->{repo_id}.reposRoot";
 684        $repos_root ||= $self->ra->{repos_root};
 685        tmp_config($k, $repos_root);
 686        $repos_root;
 687}
 688
 689sub repos_root {
 690        my ($self) = @_;
 691        my $k = "svn-remote.$self->{repo_id}.reposRoot";
 692        eval { tmp_config('--get', $k) } || $self->_set_repos_root;
 693}
 694
 695sub ra {
 696        my ($self) = shift;
 697        my $ra = Git::SVN::Ra->new($self->{url});
 698        $self->_set_repos_root($ra->{repos_root});
 699        if ($self->use_svm_props && !$self->{svm}) {
 700                if ($self->no_metadata) {
 701                        die "Can't have both 'noMetadata' and ",
 702                            "'useSvmProps' options set!\n";
 703                } elsif ($self->use_svnsync_props) {
 704                        die "Can't have both 'useSvnsyncProps' and ",
 705                            "'useSvmProps' options set!\n";
 706                }
 707                $ra = $self->_set_svm_vars($ra);
 708                $self->{-want_revprops} = 1;
 709        }
 710        $ra;
 711}
 712
 713# prop_walk(PATH, REV, SUB)
 714# -------------------------
 715# Recursively traverse PATH at revision REV and invoke SUB for each
 716# directory that contains a SVN property.  SUB will be invoked as
 717# follows:  &SUB(gs, path, props);  where `gs' is this instance of
 718# Git::SVN, `path' the path to the directory where the properties
 719# `props' were found.  The `path' will be relative to point of checkout,
 720# that is, if url://repo/trunk is the current Git branch, and that
 721# directory contains a sub-directory `d', SUB will be invoked with `/d/'
 722# as `path' (note the trailing `/').
 723sub prop_walk {
 724        my ($self, $path, $rev, $sub) = @_;
 725
 726        $path =~ s#^/##;
 727        my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
 728        $path =~ s#^/*#/#g;
 729        my $p = $path;
 730        # Strip the irrelevant part of the path.
 731        $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
 732        # Ensure the path is terminated by a `/'.
 733        $p =~ s#/*$#/#;
 734
 735        # The properties contain all the internal SVN stuff nobody
 736        # (usually) cares about.
 737        my $interesting_props = 0;
 738        foreach (keys %{$props}) {
 739                # If it doesn't start with `svn:', it must be a
 740                # user-defined property.
 741                ++$interesting_props and next if $_ !~ /^svn:/;
 742                # FIXME: Fragile, if SVN adds new public properties,
 743                # this needs to be updated.
 744                ++$interesting_props if /^svn:(?:ignore|keywords|executable
 745                                                 |eol-style|mime-type
 746                                                 |externals|needs-lock)$/x;
 747        }
 748        &$sub($self, $p, $props) if $interesting_props;
 749
 750        foreach (sort keys %$dirent) {
 751                next if $dirent->{$_}->{kind} != $SVN::Node::dir;
 752                $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
 753        }
 754}
 755
 756sub last_rev { ($_[0]->last_rev_commit)[0] }
 757sub last_commit { ($_[0]->last_rev_commit)[1] }
 758
 759# returns the newest SVN revision number and newest commit SHA1
 760sub last_rev_commit {
 761        my ($self) = @_;
 762        if (defined $self->{last_rev} && defined $self->{last_commit}) {
 763                return ($self->{last_rev}, $self->{last_commit});
 764        }
 765        my $c = ::verify_ref($self->refname.'^0');
 766        if ($c && !$self->use_svm_props && !$self->no_metadata) {
 767                my $rev = (::cmt_metadata($c))[1];
 768                if (defined $rev) {
 769                        ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
 770                        return ($rev, $c);
 771                }
 772        }
 773        my $map_path = $self->map_path;
 774        unless (-e $map_path) {
 775                ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
 776                return (undef, undef);
 777        }
 778        my ($rev, $commit) = $self->rev_map_max(1);
 779        ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
 780        return ($rev, $commit);
 781}
 782
 783sub get_fetch_range {
 784        my ($self, $min, $max) = @_;
 785        $max ||= $self->ra->get_latest_revnum;
 786        $min ||= $self->rev_map_max;
 787        (++$min, $max);
 788}
 789
 790sub tmp_config {
 791        my (@args) = @_;
 792        my $old_def_config = "$ENV{GIT_DIR}/svn/config";
 793        my $config = "$ENV{GIT_DIR}/svn/.metadata";
 794        if (! -f $config && -f $old_def_config) {
 795                rename $old_def_config, $config or
 796                       die "Failed rename $old_def_config => $config: $!\n";
 797        }
 798        my $old_config = $ENV{GIT_CONFIG};
 799        $ENV{GIT_CONFIG} = $config;
 800        $@ = undef;
 801        my @ret = eval {
 802                unless (-f $config) {
 803                        mkfile($config);
 804                        open my $fh, '>', $config or
 805                            die "Can't open $config: $!\n";
 806                        print $fh "; This file is used internally by ",
 807                                  "git-svn\n" or die
 808                                  "Couldn't write to $config: $!\n";
 809                        print $fh "; You should not have to edit it\n" or
 810                              die "Couldn't write to $config: $!\n";
 811                        close $fh or die "Couldn't close $config: $!\n";
 812                }
 813                command('config', @args);
 814        };
 815        my $err = $@;
 816        if (defined $old_config) {
 817                $ENV{GIT_CONFIG} = $old_config;
 818        } else {
 819                delete $ENV{GIT_CONFIG};
 820        }
 821        die $err if $err;
 822        wantarray ? @ret : $ret[0];
 823}
 824
 825sub tmp_index_do {
 826        my ($self, $sub) = @_;
 827        my $old_index = $ENV{GIT_INDEX_FILE};
 828        $ENV{GIT_INDEX_FILE} = $self->{index};
 829        $@ = undef;
 830        my @ret = eval {
 831                my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
 832                mkpath([$dir]) unless -d $dir;
 833                &$sub;
 834        };
 835        my $err = $@;
 836        if (defined $old_index) {
 837                $ENV{GIT_INDEX_FILE} = $old_index;
 838        } else {
 839                delete $ENV{GIT_INDEX_FILE};
 840        }
 841        die $err if $err;
 842        wantarray ? @ret : $ret[0];
 843}
 844
 845sub assert_index_clean {
 846        my ($self, $treeish) = @_;
 847
 848        $self->tmp_index_do(sub {
 849                command_noisy('read-tree', $treeish) unless -e $self->{index};
 850                my $x = command_oneline('write-tree');
 851                my ($y) = (command(qw/cat-file commit/, $treeish) =~
 852                           /^tree ($::sha1)/mo);
 853                return if $y eq $x;
 854
 855                warn "Index mismatch: $y != $x\nrereading $treeish\n";
 856                unlink $self->{index} or die "unlink $self->{index}: $!\n";
 857                command_noisy('read-tree', $treeish);
 858                $x = command_oneline('write-tree');
 859                if ($y ne $x) {
 860                        fatal "trees ($treeish) $y != $x\n",
 861                              "Something is seriously wrong...";
 862                }
 863        });
 864}
 865
 866sub get_commit_parents {
 867        my ($self, $log_entry) = @_;
 868        my (%seen, @ret, @tmp);
 869        # legacy support for 'set-tree'; this is only used by set_tree_cb:
 870        if (my $ip = $self->{inject_parents}) {
 871                if (my $commit = delete $ip->{$log_entry->{revision}}) {
 872                        push @tmp, $commit;
 873                }
 874        }
 875        if (my $cur = ::verify_ref($self->refname.'^0')) {
 876                push @tmp, $cur;
 877        }
 878        if (my $ipd = $self->{inject_parents_dcommit}) {
 879                if (my $commit = delete $ipd->{$log_entry->{revision}}) {
 880                        push @tmp, @$commit;
 881                }
 882        }
 883        push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
 884        while (my $p = shift @tmp) {
 885                next if $seen{$p};
 886                $seen{$p} = 1;
 887                push @ret, $p;
 888        }
 889        @ret;
 890}
 891
 892sub rewrite_root {
 893        my ($self) = @_;
 894        return $self->{-rewrite_root} if exists $self->{-rewrite_root};
 895        my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
 896        my $rwr = eval { command_oneline(qw/config --get/, $k) };
 897        if ($rwr) {
 898                $rwr =~ s#/+$##;
 899                if ($rwr !~ m#^[a-z\+]+://#) {
 900                        die "$rwr is not a valid URL (key: $k)\n";
 901                }
 902        }
 903        $self->{-rewrite_root} = $rwr;
 904}
 905
 906sub rewrite_uuid {
 907        my ($self) = @_;
 908        return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
 909        my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
 910        my $rwid = eval { command_oneline(qw/config --get/, $k) };
 911        if ($rwid) {
 912                $rwid =~ s#/+$##;
 913                if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
 914                        die "$rwid is not a valid UUID (key: $k)\n";
 915                }
 916        }
 917        $self->{-rewrite_uuid} = $rwid;
 918}
 919
 920sub metadata_url {
 921        my ($self) = @_;
 922        ($self->rewrite_root || $self->{url}) .
 923           (length $self->{path} ? '/' . $self->{path} : '');
 924}
 925
 926sub full_url {
 927        my ($self) = @_;
 928        $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
 929}
 930
 931sub full_pushurl {
 932        my ($self) = @_;
 933        if ($self->{pushurl}) {
 934                return $self->{pushurl} . (length $self->{path} ? '/' .
 935                       $self->{path} : '');
 936        } else {
 937                return $self->full_url;
 938        }
 939}
 940
 941sub set_commit_header_env {
 942        my ($log_entry) = @_;
 943        my %env;
 944        foreach my $ned (qw/NAME EMAIL DATE/) {
 945                foreach my $ac (qw/AUTHOR COMMITTER/) {
 946                        $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
 947                }
 948        }
 949
 950        $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
 951        $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
 952        $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
 953
 954        $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
 955                                                ? $log_entry->{commit_name}
 956                                                : $log_entry->{name};
 957        $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
 958                                                ? $log_entry->{commit_email}
 959                                                : $log_entry->{email};
 960        \%env;
 961}
 962
 963sub restore_commit_header_env {
 964        my ($env) = @_;
 965        foreach my $ned (qw/NAME EMAIL DATE/) {
 966                foreach my $ac (qw/AUTHOR COMMITTER/) {
 967                        my $k = "GIT_${ac}_${ned}";
 968                        if (defined $env->{$k}) {
 969                                $ENV{$k} = $env->{$k};
 970                        } else {
 971                                delete $ENV{$k};
 972                        }
 973                }
 974        }
 975}
 976
 977sub gc {
 978        command_noisy('gc', '--auto');
 979};
 980
 981sub do_git_commit {
 982        my ($self, $log_entry) = @_;
 983        my $lr = $self->last_rev;
 984        if (defined $lr && $lr >= $log_entry->{revision}) {
 985                die "Last fetched revision of ", $self->refname,
 986                    " was r$lr, but we are about to fetch: ",
 987                    "r$log_entry->{revision}!\n";
 988        }
 989        if (my $c = $self->rev_map_get($log_entry->{revision})) {
 990                croak "$log_entry->{revision} = $c already exists! ",
 991                      "Why are we refetching it?\n";
 992        }
 993        my $old_env = set_commit_header_env($log_entry);
 994        my $tree = $log_entry->{tree};
 995        if (!defined $tree) {
 996                $tree = $self->tmp_index_do(sub {
 997                                            command_oneline('write-tree') });
 998        }
 999        die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1000
1001        my @exec = ('git', 'commit-tree', $tree);
1002        foreach ($self->get_commit_parents($log_entry)) {
1003                push @exec, '-p', $_;
1004        }
1005        defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1006                                                                   or croak $!;
1007        binmode $msg_fh;
1008
1009        # we always get UTF-8 from SVN, but we may want our commits in
1010        # a different encoding.
1011        if (my $enc = Git::config('i18n.commitencoding')) {
1012                require Encode;
1013                Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
1014        }
1015        print $msg_fh $log_entry->{log} or croak $!;
1016        restore_commit_header_env($old_env);
1017        unless ($self->no_metadata) {
1018                print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1019                              or croak $!;
1020        }
1021        $msg_fh->flush == 0 or croak $!;
1022        close $msg_fh or croak $!;
1023        chomp(my $commit = do { local $/; <$out_fh> });
1024        close $out_fh or croak $!;
1025        waitpid $pid, 0;
1026        croak $? if $?;
1027        if ($commit !~ /^$::sha1$/o) {
1028                die "Failed to commit, invalid sha1: $commit\n";
1029        }
1030
1031        $self->rev_map_set($log_entry->{revision}, $commit, 1);
1032
1033        $self->{last_rev} = $log_entry->{revision};
1034        $self->{last_commit} = $commit;
1035        print "r$log_entry->{revision}" unless $::_q > 1;
1036        if (defined $log_entry->{svm_revision}) {
1037                 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
1038                 $self->rev_map_set($log_entry->{svm_revision}, $commit,
1039                                   0, $self->svm_uuid);
1040        }
1041        print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
1042        if (--$_gc_nr == 0) {
1043                $_gc_nr = $_gc_period;
1044                gc();
1045        }
1046        return $commit;
1047}
1048
1049sub match_paths {
1050        my ($self, $paths, $r) = @_;
1051        return 1 if $self->{path} eq '';
1052        if (my $path = $paths->{"/$self->{path}"}) {
1053                return ($path->{action} eq 'D') ? 0 : 1;
1054        }
1055        $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
1056        if (grep /$self->{path_regex}/, keys %$paths) {
1057                return 1;
1058        }
1059        my $c = '';
1060        foreach (split m#/#, $self->{path}) {
1061                $c .= "/$_";
1062                next unless ($paths->{$c} &&
1063                             ($paths->{$c}->{action} =~ /^[AR]$/));
1064                if ($self->ra->check_path($self->{path}, $r) ==
1065                    $SVN::Node::dir) {
1066                        return 1;
1067                }
1068        }
1069        return 0;
1070}
1071
1072sub find_parent_branch {
1073        my ($self, $paths, $rev) = @_;
1074        return undef unless $self->follow_parent;
1075        unless (defined $paths) {
1076                my $err_handler = $SVN::Error::handler;
1077                $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
1078                $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
1079                                   sub { $paths = $_[0] });
1080                $SVN::Error::handler = $err_handler;
1081        }
1082        return undef unless defined $paths;
1083
1084        # look for a parent from another branch:
1085        my @b_path_components = split m#/#, $self->{path};
1086        my @a_path_components;
1087        my $i;
1088        while (@b_path_components) {
1089                $i = $paths->{'/'.join('/', @b_path_components)};
1090                last if $i && defined $i->{copyfrom_path};
1091                unshift(@a_path_components, pop(@b_path_components));
1092        }
1093        return undef unless defined $i && defined $i->{copyfrom_path};
1094        my $branch_from = $i->{copyfrom_path};
1095        if (@a_path_components) {
1096                print STDERR "branch_from: $branch_from => ";
1097                $branch_from .= '/'.join('/', @a_path_components);
1098                print STDERR $branch_from, "\n";
1099        }
1100        my $r = $i->{copyfrom_rev};
1101        my $repos_root = $self->ra->{repos_root};
1102        my $url = $self->ra->{url};
1103        my $new_url = $url . $branch_from;
1104        print STDERR  "Found possible branch point: ",
1105                      "$new_url => ", $self->full_url, ", $r\n"
1106                      unless $::_q > 1;
1107        $branch_from =~ s#^/##;
1108        my $gs = $self->other_gs($new_url, $url,
1109                                 $branch_from, $r, $self->{ref_id});
1110        my ($r0, $parent) = $gs->find_rev_before($r, 1);
1111        {
1112                my ($base, $head);
1113                if (!defined $r0 || !defined $parent) {
1114                        ($base, $head) = parse_revision_argument(0, $r);
1115                } else {
1116                        if ($r0 < $r) {
1117                                $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
1118                                        0, 1, sub { $base = $_[1] - 1 });
1119                        }
1120                }
1121                if (defined $base && $base <= $r) {
1122                        $gs->fetch($base, $r);
1123                }
1124                ($r0, $parent) = $gs->find_rev_before($r, 1);
1125        }
1126        if (defined $r0 && defined $parent) {
1127                print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
1128                             unless $::_q > 1;
1129                my $ed;
1130                if ($self->ra->can_do_switch) {
1131                        $self->assert_index_clean($parent);
1132                        print STDERR "Following parent with do_switch\n"
1133                                     unless $::_q > 1;
1134                        # do_switch works with svn/trunk >= r22312, but that
1135                        # is not included with SVN 1.4.3 (the latest version
1136                        # at the moment), so we can't rely on it
1137                        $self->{last_rev} = $r0;
1138                        $self->{last_commit} = $parent;
1139                        $ed = Git::SVN::Fetcher->new($self, $gs->{path});
1140                        $gs->ra->gs_do_switch($r0, $rev, $gs,
1141                                              $self->full_url, $ed)
1142                          or die "SVN connection failed somewhere...\n";
1143                } elsif ($self->ra->trees_match($new_url, $r0,
1144                                                $self->full_url, $rev)) {
1145                        print STDERR "Trees match:\n",
1146                                     "  $new_url\@$r0\n",
1147                                     "  ${\$self->full_url}\@$rev\n",
1148                                     "Following parent with no changes\n"
1149                                     unless $::_q > 1;
1150                        $self->tmp_index_do(sub {
1151                            command_noisy('read-tree', $parent);
1152                        });
1153                        $self->{last_commit} = $parent;
1154                } else {
1155                        print STDERR "Following parent with do_update\n"
1156                                     unless $::_q > 1;
1157                        $ed = Git::SVN::Fetcher->new($self);
1158                        $self->ra->gs_do_update($rev, $rev, $self, $ed)
1159                          or die "SVN connection failed somewhere...\n";
1160                }
1161                print STDERR "Successfully followed parent\n" unless $::_q > 1;
1162                return $self->make_log_entry($rev, [$parent], $ed);
1163        }
1164        return undef;
1165}
1166
1167sub do_fetch {
1168        my ($self, $paths, $rev) = @_;
1169        my $ed;
1170        my ($last_rev, @parents);
1171        if (my $lc = $self->last_commit) {
1172                # we can have a branch that was deleted, then re-added
1173                # under the same name but copied from another path, in
1174                # which case we'll have multiple parents (we don't
1175                # want to break the original ref, nor lose copypath info):
1176                if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1177                        push @{$log_entry->{parents}}, $lc;
1178                        return $log_entry;
1179                }
1180                $ed = Git::SVN::Fetcher->new($self);
1181                $last_rev = $self->{last_rev};
1182                $ed->{c} = $lc;
1183                @parents = ($lc);
1184        } else {
1185                $last_rev = $rev;
1186                if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1187                        return $log_entry;
1188                }
1189                $ed = Git::SVN::Fetcher->new($self);
1190        }
1191        unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1192                die "SVN connection failed somewhere...\n";
1193        }
1194        $self->make_log_entry($rev, \@parents, $ed);
1195}
1196
1197sub mkemptydirs {
1198        my ($self, $r) = @_;
1199
1200        sub scan {
1201                my ($r, $empty_dirs, $line) = @_;
1202                if (defined $r && $line =~ /^r(\d+)$/) {
1203                        return 0 if $1 > $r;
1204                } elsif ($line =~ /^  \+empty_dir: (.+)$/) {
1205                        $empty_dirs->{$1} = 1;
1206                } elsif ($line =~ /^  \-empty_dir: (.+)$/) {
1207                        my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
1208                        delete @$empty_dirs{@d};
1209                }
1210                1; # continue
1211        };
1212
1213        my %empty_dirs = ();
1214        my $gz_file = "$self->{dir}/unhandled.log.gz";
1215        if (-f $gz_file) {
1216                if (!can_compress()) {
1217                        warn "Compress::Zlib could not be found; ",
1218                             "empty directories in $gz_file will not be read\n";
1219                } else {
1220                        my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
1221                                die "Unable to open $gz_file: $!\n";
1222                        my $line;
1223                        while ($gz->gzreadline($line) > 0) {
1224                                scan($r, \%empty_dirs, $line) or last;
1225                        }
1226                        $gz->gzclose;
1227                }
1228        }
1229
1230        if (open my $fh, '<', "$self->{dir}/unhandled.log") {
1231                binmode $fh or croak "binmode: $!";
1232                while (<$fh>) {
1233                        scan($r, \%empty_dirs, $_) or last;
1234                }
1235                close $fh;
1236        }
1237
1238        my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
1239        foreach my $d (sort keys %empty_dirs) {
1240                $d = uri_decode($d);
1241                $d =~ s/$strip//;
1242                next unless length($d);
1243                next if -d $d;
1244                if (-e $d) {
1245                        warn "$d exists but is not a directory\n";
1246                } else {
1247                        print "creating empty directory: $d\n";
1248                        mkpath([$d]);
1249                }
1250        }
1251}
1252
1253sub get_untracked {
1254        my ($self, $ed) = @_;
1255        my @out;
1256        my $h = $ed->{empty};
1257        foreach (sort keys %$h) {
1258                my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
1259                push @out, "  $act: " . uri_encode($_);
1260                warn "W: $act: $_\n";
1261        }
1262        foreach my $t (qw/dir_prop file_prop/) {
1263                $h = $ed->{$t} or next;
1264                foreach my $path (sort keys %$h) {
1265                        my $ppath = $path eq '' ? '.' : $path;
1266                        foreach my $prop (sort keys %{$h->{$path}}) {
1267                                next if $SKIP_PROP{$prop};
1268                                my $v = $h->{$path}->{$prop};
1269                                my $t_ppath_prop = "$t: " .
1270                                                    uri_encode($ppath) . ' ' .
1271                                                    uri_encode($prop);
1272                                if (defined $v) {
1273                                        push @out, "  +$t_ppath_prop " .
1274                                                   uri_encode($v);
1275                                } else {
1276                                        push @out, "  -$t_ppath_prop";
1277                                }
1278                        }
1279                }
1280        }
1281        foreach my $t (qw/absent_file absent_directory/) {
1282                $h = $ed->{$t} or next;
1283                foreach my $parent (sort keys %$h) {
1284                        foreach my $path (sort @{$h->{$parent}}) {
1285                                push @out, "  $t: " .
1286                                           uri_encode("$parent/$path");
1287                                warn "W: $t: $parent/$path ",
1288                                     "Insufficient permissions?\n";
1289                        }
1290                }
1291        }
1292        \@out;
1293}
1294
1295sub get_tz {
1296        # some systmes don't handle or mishandle %z, so be creative.
1297        my $t = shift || time;
1298        my $gm = timelocal(gmtime($t));
1299        my $sign = qw( + + - )[ $t <=> $gm ];
1300        return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
1301}
1302
1303# parse_svn_date(DATE)
1304# --------------------
1305# Given a date (in UTC) from Subversion, return a string in the format
1306# "<TZ Offset> <local date/time>" that Git will use.
1307#
1308# By default the parsed date will be in UTC; if $Git::SVN::_localtime
1309# is true we'll convert it to the local timezone instead.
1310sub parse_svn_date {
1311        my $date = shift || return '+0000 1970-01-01 00:00:00';
1312        my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1313                                            (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
1314                                         croak "Unable to parse date: $date\n";
1315        my $parsed_date;    # Set next.
1316
1317        if ($Git::SVN::_localtime) {
1318                # Translate the Subversion datetime to an epoch time.
1319                # Begin by switching ourselves to $date's timezone, UTC.
1320                my $old_env_TZ = $ENV{TZ};
1321                $ENV{TZ} = 'UTC';
1322
1323                my $epoch_in_UTC =
1324                    POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
1325
1326                # Determine our local timezone (including DST) at the
1327                # time of $epoch_in_UTC.  $Git::SVN::Log::TZ stored the
1328                # value of TZ, if any, at the time we were run.
1329                if (defined $Git::SVN::Log::TZ) {
1330                        $ENV{TZ} = $Git::SVN::Log::TZ;
1331                } else {
1332                        delete $ENV{TZ};
1333                }
1334
1335                my $our_TZ = get_tz();
1336
1337                # This converts $epoch_in_UTC into our local timezone.
1338                my ($sec, $min, $hour, $mday, $mon, $year,
1339                    $wday, $yday, $isdst) = localtime($epoch_in_UTC);
1340
1341                $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
1342                                       $our_TZ, $year + 1900, $mon + 1,
1343                                       $mday, $hour, $min, $sec);
1344
1345                # Reset us to the timezone in effect when we entered
1346                # this routine.
1347                if (defined $old_env_TZ) {
1348                        $ENV{TZ} = $old_env_TZ;
1349                } else {
1350                        delete $ENV{TZ};
1351                }
1352        } else {
1353                $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
1354        }
1355
1356        return $parsed_date;
1357}
1358
1359sub other_gs {
1360        my ($self, $new_url, $url,
1361            $branch_from, $r, $old_ref_id) = @_;
1362        my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
1363        unless ($gs) {
1364                my $ref_id = $old_ref_id;
1365                $ref_id =~ s/\@\d+-*$//;
1366                $ref_id .= "\@$r";
1367                # just grow a tail if we're not unique enough :x
1368                $ref_id .= '-' while find_ref($ref_id);
1369                my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
1370                if ($u =~ s#^\Q$url\E(/|$)##) {
1371                        $p = $u;
1372                        $u = $url;
1373                        $repo_id = $self->{repo_id};
1374                }
1375                while (1) {
1376                        # It is possible to tag two different subdirectories at
1377                        # the same revision.  If the url for an existing ref
1378                        # does not match, we must either find a ref with a
1379                        # matching url or create a new ref by growing a tail.
1380                        $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
1381                        my (undef, $max_commit) = $gs->rev_map_max(1);
1382                        last if (!$max_commit);
1383                        my ($url) = ::cmt_metadata($max_commit);
1384                        last if ($url eq $gs->metadata_url);
1385                        $ref_id .= '-';
1386                }
1387                print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
1388        }
1389        $gs
1390}
1391
1392sub call_authors_prog {
1393        my ($orig_author) = @_;
1394        $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
1395        my $author = `$::_authors_prog $orig_author`;
1396        if ($? != 0) {
1397                die "$::_authors_prog failed with exit code $?\n"
1398        }
1399        if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
1400                my ($name, $email) = ($1, $2);
1401                $email = undef if length $2 == 0;
1402                return [$name, $email];
1403        } else {
1404                die "Author: $orig_author: $::_authors_prog returned "
1405                        . "invalid author format: $author\n";
1406        }
1407}
1408
1409sub check_author {
1410        my ($author) = @_;
1411        if (!defined $author || length $author == 0) {
1412                $author = '(no author)';
1413        }
1414        if (!defined $::users{$author}) {
1415                if (defined $::_authors_prog) {
1416                        $::users{$author} = call_authors_prog($author);
1417                } elsif (defined $::_authors) {
1418                        die "Author: $author not defined in $::_authors file\n";
1419                }
1420        }
1421        $author;
1422}
1423
1424sub find_extra_svk_parents {
1425        my ($self, $ed, $tickets, $parents) = @_;
1426        # aha!  svk:merge property changed...
1427        my @tickets = split "\n", $tickets;
1428        my @known_parents;
1429        for my $ticket ( @tickets ) {
1430                my ($uuid, $path, $rev) = split /:/, $ticket;
1431                if ( $uuid eq $self->ra_uuid ) {
1432                        my $url = $self->{url};
1433                        my $repos_root = $url;
1434                        my $branch_from = $path;
1435                        $branch_from =~ s{^/}{};
1436                        my $gs = $self->other_gs($repos_root."/".$branch_from,
1437                                                 $url,
1438                                                 $branch_from,
1439                                                 $rev,
1440                                                 $self->{ref_id});
1441                        if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
1442                                # wahey!  we found it, but it might be
1443                                # an old one (!)
1444                                push @known_parents, [ $rev, $commit ];
1445                        }
1446                }
1447        }
1448        # Ordering matters; highest-numbered commit merge tickets
1449        # first, as they may account for later merge ticket additions
1450        # or changes.
1451        @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
1452        for my $parent ( @known_parents ) {
1453                my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
1454                my ($msg_fh, $ctx) = command_output_pipe(@cmd);
1455                my $new;
1456                while ( <$msg_fh> ) {
1457                        $new=1;last;
1458                }
1459                command_close_pipe($msg_fh, $ctx);
1460                if ( $new ) {
1461                        print STDERR
1462                            "Found merge parent (svk:merge ticket): $parent\n";
1463                        push @$parents, $parent;
1464                }
1465        }
1466}
1467
1468sub lookup_svn_merge {
1469        my $uuid = shift;
1470        my $url = shift;
1471        my $merge = shift;
1472
1473        my ($source, $revs) = split ":", $merge;
1474        my $path = $source;
1475        $path =~ s{^/}{};
1476        my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
1477        if ( !$gs ) {
1478                warn "Couldn't find revmap for $url$source\n";
1479                return;
1480        }
1481        my @ranges = split ",", $revs;
1482        my ($tip, $tip_commit);
1483        my @merged_commit_ranges;
1484        # find the tip
1485        for my $range ( @ranges ) {
1486                my ($bottom, $top) = split "-", $range;
1487                $top ||= $bottom;
1488                my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
1489                my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
1490
1491                unless ($top_commit and $bottom_commit) {
1492                        warn "W:unknown path/rev in svn:mergeinfo "
1493                                ."dirprop: $source:$range\n";
1494                        next;
1495                }
1496
1497                if (scalar(command('rev-parse', "$bottom_commit^@"))) {
1498                        push @merged_commit_ranges,
1499                             "$bottom_commit^..$top_commit";
1500                } else {
1501                        push @merged_commit_ranges, "$top_commit";
1502                }
1503
1504                if ( !defined $tip or $top > $tip ) {
1505                        $tip = $top;
1506                        $tip_commit = $top_commit;
1507                }
1508        }
1509        return ($tip_commit, @merged_commit_ranges);
1510}
1511
1512sub _rev_list {
1513        my ($msg_fh, $ctx) = command_output_pipe(
1514                "rev-list", @_,
1515               );
1516        my @rv;
1517        while ( <$msg_fh> ) {
1518                chomp;
1519                push @rv, $_;
1520        }
1521        command_close_pipe($msg_fh, $ctx);
1522        @rv;
1523}
1524
1525sub check_cherry_pick {
1526        my $base = shift;
1527        my $tip = shift;
1528        my $parents = shift;
1529        my @ranges = @_;
1530        my %commits = map { $_ => 1 }
1531                _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--");
1532        for my $range ( @ranges ) {
1533                delete @commits{_rev_list($range, "--")};
1534        }
1535        for my $commit (keys %commits) {
1536                if (has_no_changes($commit)) {
1537                        delete $commits{$commit};
1538                }
1539        }
1540        return (keys %commits);
1541}
1542
1543sub has_no_changes {
1544        my $commit = shift;
1545
1546        my @revs = split / /, command_oneline(
1547                qw(rev-list --parents -1 -m), $commit);
1548
1549        # Commits with no parents, e.g. the start of a partial branch,
1550        # have changes by definition.
1551        return 1 if (@revs < 2);
1552
1553        # Commits with multiple parents, e.g a merge, have no changes
1554        # by definition.
1555        return 0 if (@revs > 2);
1556
1557        return (command_oneline("rev-parse", "$commit^{tree}") eq
1558                command_oneline("rev-parse", "$commit~1^{tree}"));
1559}
1560
1561sub tie_for_persistent_memoization {
1562        my $hash = shift;
1563        my $path = shift;
1564
1565        if ($can_use_yaml) {
1566                tie %$hash => 'Git::SVN::Memoize::YAML', "$path.yaml";
1567        } else {
1568                tie %$hash => 'Memoize::Storable', "$path.db", 'nstore';
1569        }
1570}
1571
1572# The GIT_DIR environment variable is not always set until after the command
1573# line arguments are processed, so we can't memoize in a BEGIN block.
1574{
1575        my $memoized = 0;
1576
1577        sub memoize_svn_mergeinfo_functions {
1578                return if $memoized;
1579                $memoized = 1;
1580
1581                my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
1582                mkpath([$cache_path]) unless -d $cache_path;
1583
1584                my %lookup_svn_merge_cache;
1585                my %check_cherry_pick_cache;
1586                my %has_no_changes_cache;
1587
1588                tie_for_persistent_memoization(\%lookup_svn_merge_cache,
1589                    "$cache_path/lookup_svn_merge");
1590                memoize 'lookup_svn_merge',
1591                        SCALAR_CACHE => 'FAULT',
1592                        LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
1593                ;
1594
1595                tie_for_persistent_memoization(\%check_cherry_pick_cache,
1596                    "$cache_path/check_cherry_pick");
1597                memoize 'check_cherry_pick',
1598                        SCALAR_CACHE => 'FAULT',
1599                        LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
1600                ;
1601
1602                tie_for_persistent_memoization(\%has_no_changes_cache,
1603                    "$cache_path/has_no_changes");
1604                memoize 'has_no_changes',
1605                        SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
1606                        LIST_CACHE => 'FAULT',
1607                ;
1608        }
1609
1610        sub unmemoize_svn_mergeinfo_functions {
1611                return if not $memoized;
1612                $memoized = 0;
1613
1614                Memoize::unmemoize 'lookup_svn_merge';
1615                Memoize::unmemoize 'check_cherry_pick';
1616                Memoize::unmemoize 'has_no_changes';
1617        }
1618
1619        sub clear_memoized_mergeinfo_caches {
1620                die "Only call this method in non-memoized context" if ($memoized);
1621
1622                my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
1623                return unless -d $cache_path;
1624
1625                for my $cache_file (("$cache_path/lookup_svn_merge",
1626                                     "$cache_path/check_cherry_pick",
1627                                     "$cache_path/has_no_changes")) {
1628                        for my $suffix (qw(yaml db)) {
1629                                my $file = "$cache_file.$suffix";
1630                                next unless -e $file;
1631                                unlink($file) or die "unlink($file) failed: $!\n";
1632                        }
1633                }
1634        }
1635
1636
1637        Memoize::memoize 'Git::SVN::repos_root';
1638}
1639
1640END {
1641        # Force cache writeout explicitly instead of waiting for
1642        # global destruction to avoid segfault in Storable:
1643        # http://rt.cpan.org/Public/Bug/Display.html?id=36087
1644        unmemoize_svn_mergeinfo_functions();
1645}
1646
1647sub parents_exclude {
1648        my $parents = shift;
1649        my @commits = @_;
1650        return unless @commits;
1651
1652        my @excluded;
1653        my $excluded;
1654        do {
1655                my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
1656                $excluded = command_oneline(@cmd);
1657                if ( $excluded ) {
1658                        my @new;
1659                        my $found;
1660                        for my $commit ( @commits ) {
1661                                if ( $commit eq $excluded ) {
1662                                        push @excluded, $commit;
1663                                        $found++;
1664                                        last;
1665                                }
1666                                else {
1667                                        push @new, $commit;
1668                                }
1669                        }
1670                        die "saw commit '$excluded' in rev-list output, "
1671                                ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
1672                                        unless $found;
1673                        @commits = @new;
1674                }
1675        }
1676                while ($excluded and @commits);
1677
1678        return @excluded;
1679}
1680
1681
1682# note: this function should only be called if the various dirprops
1683# have actually changed
1684sub find_extra_svn_parents {
1685        my ($self, $ed, $mergeinfo, $parents) = @_;
1686        # aha!  svk:merge property changed...
1687
1688        memoize_svn_mergeinfo_functions();
1689
1690        # We first search for merged tips which are not in our
1691        # history.  Then, we figure out which git revisions are in
1692        # that tip, but not this revision.  If all of those revisions
1693        # are now marked as merge, we can add the tip as a parent.
1694        my @merges = split "\n", $mergeinfo;
1695        my @merge_tips;
1696        my $url = $self->{url};
1697        my $uuid = $self->ra_uuid;
1698        my %ranges;
1699        for my $merge ( @merges ) {
1700                my ($tip_commit, @ranges) =
1701                        lookup_svn_merge( $uuid, $url, $merge );
1702                unless (!$tip_commit or
1703                                grep { $_ eq $tip_commit } @$parents ) {
1704                        push @merge_tips, $tip_commit;
1705                        $ranges{$tip_commit} = \@ranges;
1706                } else {
1707                        push @merge_tips, undef;
1708                }
1709        }
1710
1711        my %excluded = map { $_ => 1 }
1712                parents_exclude($parents, grep { defined } @merge_tips);
1713
1714        # check merge tips for new parents
1715        my @new_parents;
1716        for my $merge_tip ( @merge_tips ) {
1717                my $spec = shift @merges;
1718                next unless $merge_tip and $excluded{$merge_tip};
1719
1720                my $ranges = $ranges{$merge_tip};
1721
1722                # check out 'new' tips
1723                my $merge_base;
1724                eval {
1725                        $merge_base = command_oneline(
1726                                "merge-base",
1727                                @$parents, $merge_tip,
1728                        );
1729                };
1730                if ($@) {
1731                        die "An error occurred during merge-base"
1732                                unless $@->isa("Git::Error::Command");
1733
1734                        warn "W: Cannot find common ancestor between ".
1735                             "@$parents and $merge_tip. Ignoring merge info.\n";
1736                        next;
1737                }
1738
1739                # double check that there are no missing non-merge commits
1740                my (@incomplete) = check_cherry_pick(
1741                        $merge_base, $merge_tip,
1742                        $parents,
1743                        @$ranges,
1744                       );
1745
1746                if ( @incomplete ) {
1747                        warn "W:svn cherry-pick ignored ($spec) - missing "
1748                                .@incomplete." commit(s) (eg $incomplete[0])\n";
1749                } else {
1750                        warn
1751                                "Found merge parent (svn:mergeinfo prop): ",
1752                                        $merge_tip, "\n";
1753                        push @new_parents, $merge_tip;
1754                }
1755        }
1756
1757        # cater for merges which merge commits from multiple branches
1758        if ( @new_parents > 1 ) {
1759                for ( my $i = 0; $i <= $#new_parents; $i++ ) {
1760                        for ( my $j = 0; $j <= $#new_parents; $j++ ) {
1761                                next if $i == $j;
1762                                next unless $new_parents[$i];
1763                                next unless $new_parents[$j];
1764                                my $revs = command_oneline(
1765                                        "rev-list", "-1",
1766                                        "$new_parents[$i]..$new_parents[$j]",
1767                                       );
1768                                if ( !$revs ) {
1769                                        undef($new_parents[$j]);
1770                                }
1771                        }
1772                }
1773        }
1774        push @$parents, grep { defined } @new_parents;
1775}
1776
1777sub make_log_entry {
1778        my ($self, $rev, $parents, $ed) = @_;
1779        my $untracked = $self->get_untracked($ed);
1780
1781        my @parents = @$parents;
1782        my $ps = $ed->{path_strip} || "";
1783        for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
1784                my $props = $ed->{dir_prop}{$path};
1785                if ( $props->{"svk:merge"} ) {
1786                        $self->find_extra_svk_parents
1787                                ($ed, $props->{"svk:merge"}, \@parents);
1788                }
1789                if ( $props->{"svn:mergeinfo"} ) {
1790                        $self->find_extra_svn_parents
1791                                ($ed,
1792                                 $props->{"svn:mergeinfo"},
1793                                 \@parents);
1794                }
1795        }
1796
1797        open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
1798        print $un "r$rev\n" or croak $!;
1799        print $un $_, "\n" foreach @$untracked;
1800        my %log_entry = ( parents => \@parents, revision => $rev,
1801                          log => '');
1802
1803        my $headrev;
1804        my $logged = delete $self->{logged_rev_props};
1805        if (!$logged || $self->{-want_revprops}) {
1806                my $rp = $self->ra->rev_proplist($rev);
1807                foreach (sort keys %$rp) {
1808                        my $v = $rp->{$_};
1809                        if (/^svn:(author|date|log)$/) {
1810                                $log_entry{$1} = $v;
1811                        } elsif ($_ eq 'svm:headrev') {
1812                                $headrev = $v;
1813                        } else {
1814                                print $un "  rev_prop: ", uri_encode($_), ' ',
1815                                          uri_encode($v), "\n";
1816                        }
1817                }
1818        } else {
1819                map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1820        }
1821        close $un or croak $!;
1822
1823        $log_entry{date} = parse_svn_date($log_entry{date});
1824        $log_entry{log} .= "\n";
1825        my $author = $log_entry{author} = check_author($log_entry{author});
1826        my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
1827                                                       : ($author, undef);
1828
1829        my ($commit_name, $commit_email) = ($name, $email);
1830        if ($_use_log_author) {
1831                my $name_field;
1832                if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
1833                        $name_field = $1;
1834                } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
1835                        $name_field = $1;
1836                }
1837                if (!defined $name_field) {
1838                        if (!defined $email) {
1839                                $email = $name;
1840                        }
1841                } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
1842                        ($name, $email) = ($1, $2);
1843                } elsif ($name_field =~ /(.*)@/) {
1844                        ($name, $email) = ($1, $name_field);
1845                } else {
1846                        ($name, $email) = ($name_field, $name_field);
1847                }
1848        }
1849        if (defined $headrev && $self->use_svm_props) {
1850                if ($self->rewrite_root) {
1851                        die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1852                            "options set!\n";
1853                }
1854                if ($self->rewrite_uuid) {
1855                        die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
1856                            "options set!\n";
1857                }
1858                my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
1859                # we don't want "SVM: initializing mirror for junk" ...
1860                return undef if $r == 0;
1861                my $svm = $self->svm;
1862                if ($uuid ne $svm->{uuid}) {
1863                        die "UUID mismatch on SVM path:\n",
1864                            "expected: $svm->{uuid}\n",
1865                            "     got: $uuid\n";
1866                }
1867                my $full_url = $self->full_url;
1868                $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1869                             die "Failed to replace '$svm->{replace}' with ",
1870                                 "'$svm->{source}' in $full_url\n";
1871                # throw away username for storing in records
1872                remove_username($full_url);
1873                $log_entry{metadata} = "$full_url\@$r $uuid";
1874                $log_entry{svm_revision} = $r;
1875                $email ||= "$author\@$uuid";
1876                $commit_email ||= "$author\@$uuid";
1877        } elsif ($self->use_svnsync_props) {
1878                my $full_url = $self->svnsync->{url};
1879                $full_url .= "/$self->{path}" if length $self->{path};
1880                remove_username($full_url);
1881                my $uuid = $self->svnsync->{uuid};
1882                $log_entry{metadata} = "$full_url\@$rev $uuid";
1883                $email ||= "$author\@$uuid";
1884                $commit_email ||= "$author\@$uuid";
1885        } else {
1886                my $url = $self->metadata_url;
1887                remove_username($url);
1888                my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
1889                $log_entry{metadata} = "$url\@$rev " . $uuid;
1890                $email ||= "$author\@" . $uuid;
1891                $commit_email ||= "$author\@" . $uuid;
1892        }
1893        $log_entry{name} = $name;
1894        $log_entry{email} = $email;
1895        $log_entry{commit_name} = $commit_name;
1896        $log_entry{commit_email} = $commit_email;
1897        \%log_entry;
1898}
1899
1900sub fetch {
1901        my ($self, $min_rev, $max_rev, @parents) = @_;
1902        my ($last_rev, $last_commit) = $self->last_rev_commit;
1903        my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1904        $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1905}
1906
1907sub set_tree_cb {
1908        my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1909        $self->{inject_parents} = { $rev => $tree };
1910        $self->fetch(undef, undef);
1911}
1912
1913sub set_tree {
1914        my ($self, $tree) = (shift, shift);
1915        my $log_entry = ::get_commit_entry($tree);
1916        unless ($self->{last_rev}) {
1917                fatal("Must have an existing revision to commit");
1918        }
1919        my %ed_opts = ( r => $self->{last_rev},
1920                        log => $log_entry->{log},
1921                        ra => $self->ra,
1922                        tree_a => $self->{last_commit},
1923                        tree_b => $tree,
1924                        editor_cb => sub {
1925                               $self->set_tree_cb($log_entry, $tree, @_) },
1926                        svn_path => $self->{path} );
1927        if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
1928                print "No changes\nr$self->{last_rev} = $tree\n";
1929        }
1930}
1931
1932sub rebuild_from_rev_db {
1933        my ($self, $path) = @_;
1934        my $r = -1;
1935        open my $fh, '<', $path or croak "open: $!";
1936        binmode $fh or croak "binmode: $!";
1937        while (<$fh>) {
1938                length($_) == 41 or croak "inconsistent size in ($_) != 41";
1939                chomp($_);
1940                ++$r;
1941                next if $_ eq ('0' x 40);
1942                $self->rev_map_set($r, $_);
1943                print "r$r = $_\n";
1944        }
1945        close $fh or croak "close: $!";
1946        unlink $path or croak "unlink: $!";
1947}
1948
1949sub rebuild {
1950        my ($self) = @_;
1951        my $map_path = $self->map_path;
1952        my $partial = (-e $map_path && ! -z $map_path);
1953        return unless ::verify_ref($self->refname.'^0');
1954        if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
1955                my $rev_db = $self->rev_db_path;
1956                $self->rebuild_from_rev_db($rev_db);
1957                if ($self->use_svm_props) {
1958                        my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
1959                        $self->rebuild_from_rev_db($svm_rev_db);
1960                }
1961                $self->unlink_rev_db_symlink;
1962                return;
1963        }
1964        print "Rebuilding $map_path ...\n" if (!$partial);
1965        my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
1966                (undef, undef));
1967        my ($log, $ctx) =
1968            command_output_pipe(qw/rev-list --pretty=raw --reverse/,
1969                                ($head ? "$head.." : "") . $self->refname,
1970                                '--');
1971        my $metadata_url = $self->metadata_url;
1972        remove_username($metadata_url);
1973        my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
1974        my $c;
1975        while (<$log>) {
1976                if ( m{^commit ($::sha1)$} ) {
1977                        $c = $1;
1978                        next;
1979                }
1980                next unless s{^\s*(git-svn-id:)}{$1};
1981                my ($url, $rev, $uuid) = ::extract_metadata($_);
1982                remove_username($url);
1983
1984                # ignore merges (from set-tree)
1985                next if (!defined $rev || !$uuid);
1986
1987                # if we merged or otherwise started elsewhere, this is
1988                # how we break out of it
1989                if (($uuid ne $svn_uuid) ||
1990                    ($metadata_url && $url && ($url ne $metadata_url))) {
1991                        next;
1992                }
1993                if ($partial && $head) {
1994                        print "Partial-rebuilding $map_path ...\n";
1995                        print "Currently at $base_rev = $head\n";
1996                        $head = undef;
1997                }
1998
1999                $self->rev_map_set($rev, $c);
2000                print "r$rev = $c\n";
2001        }
2002        command_close_pipe($log, $ctx);
2003        print "Done rebuilding $map_path\n" if (!$partial || !$head);
2004        my $rev_db_path = $self->rev_db_path;
2005        if (-f $self->rev_db_path) {
2006                unlink $self->rev_db_path or croak "unlink: $!";
2007        }
2008        $self->unlink_rev_db_symlink;
2009}
2010
2011# rev_map:
2012# Tie::File seems to be prone to offset errors if revisions get sparse,
2013# it's not that fast, either.  Tie::File is also not in Perl 5.6.  So
2014# one of my favorite modules is out :<  Next up would be one of the DBM
2015# modules, but I'm not sure which is most portable...
2016#
2017# This is the replacement for the rev_db format, which was too big
2018# and inefficient for large repositories with a lot of sparse history
2019# (mainly tags)
2020#
2021# The format is this:
2022#   - 24 bytes for every record,
2023#     * 4 bytes for the integer representing an SVN revision number
2024#     * 20 bytes representing the sha1 of a git commit
2025#   - No empty padding records like the old format
2026#     (except the last record, which can be overwritten)
2027#   - new records are written append-only since SVN revision numbers
2028#     increase monotonically
2029#   - lookups on SVN revision number are done via a binary search
2030#   - Piping the file to xxd -c24 is a good way of dumping it for
2031#     viewing or editing (piped back through xxd -r), should the need
2032#     ever arise.
2033#   - The last record can be padding revision with an all-zero sha1
2034#     This is used to optimize fetch performance when using multiple
2035#     "fetch" directives in .git/config
2036#
2037# These files are disposable unless noMetadata or useSvmProps is set
2038
2039sub _rev_map_set {
2040        my ($fh, $rev, $commit) = @_;
2041
2042        binmode $fh or croak "binmode: $!";
2043        my $size = (stat($fh))[7];
2044        ($size % 24) == 0 or croak "inconsistent size: $size";
2045
2046        my $wr_offset = 0;
2047        if ($size > 0) {
2048                sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2049                my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2050                $read == 24 or croak "read only $read bytes (!= 24)";
2051                my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
2052                if ($last_commit eq ('0' x40)) {
2053                        if ($size >= 48) {
2054                                sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2055                                $read = sysread($fh, $buf, 24) or
2056                                    croak "read: $!";
2057                                $read == 24 or
2058                                    croak "read only $read bytes (!= 24)";
2059                                ($last_rev, $last_commit) =
2060                                    unpack(rev_map_fmt, $buf);
2061                                if ($last_commit eq ('0' x40)) {
2062                                        croak "inconsistent .rev_map\n";
2063                                }
2064                        }
2065                        if ($last_rev >= $rev) {
2066                                croak "last_rev is higher!: $last_rev >= $rev";
2067                        }
2068                        $wr_offset = -24;
2069                }
2070        }
2071        sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
2072        syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2073          croak "write: $!";
2074}
2075
2076sub _rev_map_reset {
2077        my ($fh, $rev, $commit) = @_;
2078        my $c = _rev_map_get($fh, $rev);
2079        $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
2080        my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
2081        truncate $fh, $offset or croak "truncate: $!";
2082}
2083
2084sub mkfile {
2085        my ($path) = @_;
2086        unless (-e $path) {
2087                my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2088                mkpath([$dir]) unless -d $dir;
2089                open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2090                close $fh or die "Couldn't close (create) $path: $!\n";
2091        }
2092}
2093
2094sub rev_map_set {
2095        my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2096        defined $commit or die "missing arg3\n";
2097        length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2098        my $db = $self->map_path($uuid);
2099        my $db_lock = "$db.lock";
2100        my $sigmask;
2101        $update_ref ||= 0;
2102        if ($update_ref) {
2103                $sigmask = POSIX::SigSet->new();
2104                my $signew = POSIX::SigSet->new(SIGINT, SIGHUP, SIGTERM,
2105                        SIGALRM, SIGUSR1, SIGUSR2);
2106                sigprocmask(SIG_BLOCK, $signew, $sigmask) or
2107                        croak "Can't block signals: $!";
2108        }
2109        mkfile($db);
2110
2111        $LOCKFILES{$db_lock} = 1;
2112        my $sync;
2113        # both of these options make our .rev_db file very, very important
2114        # and we can't afford to lose it because rebuild() won't work
2115        if ($self->use_svm_props || $self->no_metadata) {
2116                $sync = 1;
2117                copy($db, $db_lock) or die "rev_map_set(@_): ",
2118                                           "Failed to copy: ",
2119                                           "$db => $db_lock ($!)\n";
2120        } else {
2121                rename $db, $db_lock or die "rev_map_set(@_): ",
2122                                            "Failed to rename: ",
2123                                            "$db => $db_lock ($!)\n";
2124        }
2125
2126        sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
2127             or croak "Couldn't open $db_lock: $!\n";
2128        if ($update_ref eq 'reset') {
2129                clear_memoized_mergeinfo_caches();
2130                _rev_map_reset($fh, $rev, $commit);
2131        } else {
2132                _rev_map_set($fh, $rev, $commit);
2133        }
2134
2135        if ($sync) {
2136                $fh->flush or die "Couldn't flush $db_lock: $!\n";
2137                $fh->sync or die "Couldn't sync $db_lock: $!\n";
2138        }
2139        close $fh or croak $!;
2140        if ($update_ref) {
2141                $_head = $self;
2142                my $note = "";
2143                $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
2144                command_noisy('update-ref', '-m', "r$rev$note",
2145                              $self->refname, $commit);
2146        }
2147        rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
2148                                    "$db_lock => $db ($!)\n";
2149        delete $LOCKFILES{$db_lock};
2150        if ($update_ref) {
2151                sigprocmask(SIG_SETMASK, $sigmask) or
2152                        croak "Can't restore signal mask: $!";
2153        }
2154}
2155
2156# If want_commit, this will return an array of (rev, commit) where
2157# commit _must_ be a valid commit in the archive.
2158# Otherwise, it'll return the max revision (whether or not the
2159# commit is valid or just a 0x40 placeholder).
2160sub rev_map_max {
2161        my ($self, $want_commit) = @_;
2162        $self->rebuild;
2163        my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2164        $want_commit ? ($r, $c) : $r;
2165}
2166
2167sub rev_map_max_norebuild {
2168        my ($self, $want_commit) = @_;
2169        my $map_path = $self->map_path;
2170        stat $map_path or return $want_commit ? (0, undef) : 0;
2171        sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
2172        binmode $fh or croak "binmode: $!";
2173        my $size = (stat($fh))[7];
2174        ($size % 24) == 0 or croak "inconsistent size: $size";
2175
2176        if ($size == 0) {
2177                close $fh or croak "close: $!";
2178                return $want_commit ? (0, undef) : 0;
2179        }
2180
2181        sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2182        sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2183        my ($r, $c) = unpack(rev_map_fmt, $buf);
2184        if ($want_commit && $c eq ('0' x40)) {
2185                if ($size < 48) {
2186                        return $want_commit ? (0, undef) : 0;
2187                }
2188                sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2189                sysread($fh, $buf, 24) == 24 or croak "read: $!";
2190                ($r, $c) = unpack(rev_map_fmt, $buf);
2191                if ($c eq ('0'x40)) {
2192                        croak "Penultimate record is all-zeroes in $map_path";
2193                }
2194        }
2195        close $fh or croak "close: $!";
2196        $want_commit ? ($r, $c) : $r;
2197}
2198
2199sub rev_map_get {
2200        my ($self, $rev, $uuid) = @_;
2201        my $map_path = $self->map_path($uuid);
2202        return undef unless -e $map_path;
2203
2204        sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
2205        my $c = _rev_map_get($fh, $rev);
2206        close($fh) or croak "close: $!";
2207        $c
2208}
2209
2210sub _rev_map_get {
2211        my ($fh, $rev) = @_;
2212
2213        binmode $fh or croak "binmode: $!";
2214        my $size = (stat($fh))[7];
2215        ($size % 24) == 0 or croak "inconsistent size: $size";
2216
2217        if ($size == 0) {
2218                return undef;
2219        }
2220
2221        my ($l, $u) = (0, $size - 24);
2222        my ($r, $c, $buf);
2223
2224        while ($l <= $u) {
2225                my $i = int(($l/24 + $u/24) / 2) * 24;
2226                sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
2227                sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2228                my ($r, $c) = unpack(rev_map_fmt, $buf);
2229
2230                if ($r < $rev) {
2231                        $l = $i + 24;
2232                } elsif ($r > $rev) {
2233                        $u = $i - 24;
2234                } else { # $r == $rev
2235                        return $c eq ('0' x 40) ? undef : $c;
2236                }
2237        }
2238        undef;
2239}
2240
2241# Finds the first svn revision that exists on (if $eq_ok is true) or
2242# before $rev for the current branch.  It will not search any lower
2243# than $min_rev.  Returns the git commit hash and svn revision number
2244# if found, else (undef, undef).
2245sub find_rev_before {
2246        my ($self, $rev, $eq_ok, $min_rev) = @_;
2247        --$rev unless $eq_ok;
2248        $min_rev ||= 1;
2249        my $max_rev = $self->rev_map_max;
2250        $rev = $max_rev if ($rev > $max_rev);
2251        while ($rev >= $min_rev) {
2252                if (my $c = $self->rev_map_get($rev)) {
2253                        return ($rev, $c);
2254                }
2255                --$rev;
2256        }
2257        return (undef, undef);
2258}
2259
2260# Finds the first svn revision that exists on (if $eq_ok is true) or
2261# after $rev for the current branch.  It will not search any higher
2262# than $max_rev.  Returns the git commit hash and svn revision number
2263# if found, else (undef, undef).
2264sub find_rev_after {
2265        my ($self, $rev, $eq_ok, $max_rev) = @_;
2266        ++$rev unless $eq_ok;
2267        $max_rev ||= $self->rev_map_max;
2268        while ($rev <= $max_rev) {
2269                if (my $c = $self->rev_map_get($rev)) {
2270                        return ($rev, $c);
2271                }
2272                ++$rev;
2273        }
2274        return (undef, undef);
2275}
2276
2277sub _new {
2278        my ($class, $repo_id, $ref_id, $path) = @_;
2279        unless (defined $repo_id && length $repo_id) {
2280                $repo_id = $default_repo_id;
2281        }
2282        unless (defined $ref_id && length $ref_id) {
2283                # Access the prefix option from the git-svn main program if it's loaded.
2284                my $prefix = defined &::opt_prefix ? ::opt_prefix() : "";
2285                $_[2] = $ref_id =
2286                             "refs/remotes/$prefix$default_ref_id";
2287        }
2288        $_[1] = $repo_id;
2289        my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2290
2291        # Older repos imported by us used $GIT_DIR/svn/foo instead of
2292        # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
2293        if ($ref_id =~ m{^refs/remotes/(.*)}) {
2294                my $old_dir = "$ENV{GIT_DIR}/svn/$1";
2295                if (-d $old_dir && ! -d $dir) {
2296                        $dir = $old_dir;
2297                }
2298        }
2299
2300        $_[3] = $path = '' unless (defined $path);
2301        mkpath([$dir]);
2302        bless {
2303                ref_id => $ref_id, dir => $dir, index => "$dir/index",
2304                path => $path, config => "$ENV{GIT_DIR}/svn/config",
2305                map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
2306}
2307
2308# for read-only access of old .rev_db formats
2309sub unlink_rev_db_symlink {
2310        my ($self) = @_;
2311        my $link = $self->rev_db_path;
2312        $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
2313        if (-l $link) {
2314                unlink $link or croak "unlink: $link failed!";
2315        }
2316}
2317
2318sub rev_db_path {
2319        my ($self, $uuid) = @_;
2320        my $db_path = $self->map_path($uuid);
2321        $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
2322            or croak "map_path: $db_path does not contain '/.rev_map.' !";
2323        $db_path;
2324}
2325
2326# the new replacement for .rev_db
2327sub map_path {
2328        my ($self, $uuid) = @_;
2329        $uuid ||= $self->ra_uuid;
2330        "$self->{map_root}.$uuid";
2331}
2332
2333sub uri_encode {
2334        my ($f) = @_;
2335        $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2336        $f
2337}
2338
2339sub uri_decode {
2340        my ($f) = @_;
2341        $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
2342        $f
2343}
2344
2345sub remove_username {
2346        $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2347}
2348
23491;