35
my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
36
$_find_copies_harder, $_l, $_cp_similarity,
37
$_repack, $_repack_nr, $_repack_flags,
38
- $_template, $_shared,
39
- $_version, $_upgrade, $_authors, $_branch_all_refs);
38
+ $_template, $_shared, $_no_default_regex, $_no_graft_copy,
39
+ $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
40
my (@_branch_from, %tree_map, %users);
41
my ($_svn_co_url_revs, $_svn_pg_peg_revs);
42
my @repo_path_split_cache;
48
'repack:i' => \$_repack,
49
'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
50
51
+my ($_trunk, $_tags, $_branches);
52
+my %multi_opts = ( 'trunk|T=s' => \$_trunk,
53
+ 'tags|t=s' => \$_tags,
54
+ 'branches|b=s' => \$_branches );
55
+my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
56
+
57
# yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
58
my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
59
62
{ 'revision|r=s' => \$_revision, %fc_opts } ],
63
init => [ \&init, "Initialize a repo for tracking" .
64
" (requires URL argument)",
59
- { 'template=s' => \$_template,
60
- 'shared' => \$_shared } ],
65
+ \%init_opts ],
66
commit => [ \&commit, "Commit git revisions to SVN",
67
{ 'stdin|' => \$_stdin,
68
'edit|e' => \$_edit,
76
rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
77
{ 'no-ignore-externals' => \$_no_ignore_ext,
78
'upgrade' => \$_upgrade } ],
79
+ 'graft-branches' => [ \&graft_branches,
80
+ 'Detect merges/branches from already imported history',
81
+ { 'merge-rx|m' => \@_opt_m,
82
+ 'no-default-regex' => \$_no_default_regex,
83
+ 'no-graft-copy' => \$_no_graft_copy } ],
84
+ 'multi-init' => [ \&multi_init,
85
+ 'Initialize multiple trees (like git-svnimport)',
86
+ { %multi_opts, %fc_opts } ],
87
+ 'multi-fetch' => [ \&multi_fetch,
88
+ 'Fetch multiple trees (like git-svnimport)',
89
+ \%fc_opts ],
90
);
91
+
92
my $cmd;
93
for (my $i = 0; $i < @ARGV; $i++) {
94
if (defined $cmd{$ARGV[$i]}) {
113
load_authors() if $_authors;
114
load_all_refs() if $_branch_all_refs;
115
svn_compat_check();
99
-migration_check() unless $cmd eq 'init';
116
+migration_check() unless $cmd =~ /^(?:init|multi-init)$/;
117
$cmd{$cmd}->[0]->(@ARGV);
118
exit 0;
119
236
sub init {
237
$SVN_URL = shift or die "SVN repository location required " .
238
"as a command-line argument\n";
239
+ $SVN_URL =~ s!/+$!!; # strip trailing slash
240
unless (-d $GIT_DIR) {
241
my @init_db = ('git-init-db');
242
push @init_db, "--template=$_template" if defined $_template;
376
}
377
}
378
379
+sub graft_branches {
380
+ my $gr_file = "$GIT_DIR/info/grafts";
381
+ my ($grafts, $comments) = read_grafts($gr_file);
382
+ my $gr_sha1;
383
+
384
+ if (%$grafts) {
385
+ # temporarily disable our grafts file to make this idempotent
386
+ chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
387
+ rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
388
+ }
389
+
390
+ my $l_map = read_url_paths();
391
+ my @re = map { qr/$_/is } @_opt_m if @_opt_m;
392
+ unless ($_no_default_regex) {
393
+ push @re, ( qr/\b(?:merge|merging|merged)\s+(\S.+)/is,
394
+ qr/\b(?:from|of)\s+(\S.+)/is );
395
+ }
396
+ foreach my $u (keys %$l_map) {
397
+ if (@re) {
398
+ foreach my $p (keys %{$l_map->{$u}}) {
399
+ graft_merge_msg($grafts,$l_map,$u,$p);
400
+ }
401
+ }
402
+ graft_file_copy($grafts,$l_map,$u) unless $_no_graft_copy;
403
+ }
404
+
405
+ write_grafts($grafts, $comments, $gr_file);
406
+ unlink "$gr_file~$gr_sha1" if $gr_sha1;
407
+}
408
+
409
+sub multi_init {
410
+ my $url = shift;
411
+ $_trunk ||= 'trunk';
412
+ $_trunk =~ s#/+$##;
413
+ $url =~ s#/+$## if $url;
414
+ if ($_trunk !~ m#^[a-z\+]+://#) {
415
+ $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
416
+ unless ($url) {
417
+ print STDERR "E: '$_trunk' is not a complete URL ",
418
+ "and a separate URL is not specified\n";
419
+ exit 1;
420
+ }
421
+ $_trunk = $url . $_trunk;
422
+ }
423
+ if ($GIT_SVN eq 'git-svn') {
424
+ print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
425
+ $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
426
+ }
427
+ init_vars();
428
+ init($_trunk);
429
+ complete_url_ls_init($url, $_branches, '--branches/-b', '');
430
+ complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
431
+}
432
+
433
+sub multi_fetch {
434
+ # try to do trunk first, since branches/tags
435
+ # may be descended from it.
436
+ if (-d "$GIT_DIR/svn/trunk") {
437
+ print "Fetching trunk\n";
438
+ defined(my $pid = fork) or croak $!;
439
+ if (!$pid) {
440
+ $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
441
+ init_vars();
442
+ fetch(@_);
443
+ exit 0;
444
+ }
445
+ waitpid $pid, 0;
446
+ croak $? if $?;
447
+ }
448
+ rec_fetch('', "$GIT_DIR/svn", @_);
449
+}
450
+
451
########################### utility functions #########################
452
453
+sub rec_fetch {
454
+ my ($pfx, $p, @args) = @_;
455
+ my @dir;
456
+ foreach (sort <$p/*>) {
457
+ if (-r "$_/info/url") {
458
+ $pfx .= '/' if $pfx && $pfx !~ m!/$!;
459
+ my $id = $pfx . basename $_;
460
+ next if $id eq 'trunk';
461
+ print "Fetching $id\n";
462
+ defined(my $pid = fork) or croak $!;
463
+ if (!$pid) {
464
+ $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
465
+ init_vars();
466
+ fetch(@args);
467
+ exit 0;
468
+ }
469
+ waitpid $pid, 0;
470
+ croak $? if $?;
471
+ } elsif (-d $_) {
472
+ push @dir, $_;
473
+ }
474
+ }
475
+ foreach (@dir) {
476
+ my $x = $_;
477
+ $x =~ s!^\Q$GIT_DIR\E/svn/!!;
478
+ rec_fetch($x, $_);
479
+ }
480
+}
481
+
482
+sub complete_url_ls_init {
483
+ my ($url, $var, $switch, $pfx) = @_;
484
+ unless ($var) {
485
+ print STDERR "W: $switch not specified\n";
486
+ return;
487
+ }
488
+ $var =~ s#/+$##;
489
+ if ($var !~ m#^[a-z\+]+://#) {
490
+ $var = '/' . $var if ($var !~ m#^/#);
491
+ unless ($url) {
492
+ print STDERR "E: '$var' is not a complete URL ",
493
+ "and a separate URL is not specified\n";
494
+ exit 1;
495
+ }
496
+ $var = $url . $var;
497
+ }
498
+ chomp(my @ls = safe_qx(qw/svn ls --non-interactive/, $var));
499
+ my $old = $GIT_SVN;
500
+ defined(my $pid = fork) or croak $!;
501
+ if (!$pid) {
502
+ foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
503
+ $u =~ s#/+$##;
504
+ if ($u !~ m!\Q$var\E/(.+)$!) {
505
+ print STDERR "W: Unrecognized URL: $u\n";
506
+ die "This should never happen\n";
507
+ }
508
+ my $id = $pfx.$1;
509
+ print "init $u => $id\n";
510
+ $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
511
+ init_vars();
512
+ init($u);
513
+ }
514
+ exit 0;
515
+ }
516
+ waitpid $pid, 0;
517
+ croak $? if $?;
518
+}
519
+
520
+sub common_prefix {
521
+ my $paths = shift;
522
+ my %common;
523
+ foreach (@$paths) {
524
+ my @tmp = split m#/#, $_;
525
+ my $p = '';
526
+ while (my $x = shift @tmp) {
527
+ $p .= "/$x";
528
+ $common{$p} ||= 0;
529
+ $common{$p}++;
530
+ }
531
+ }
532
+ foreach (sort {length $b <=> length $a} keys %common) {
533
+ if ($common{$_} == @$paths) {
534
+ return $_;
535
+ }
536
+ }
537
+ return '';
538
+}
539
+
540
+# this isn't funky-filename safe, but good enough for now...
541
+sub graft_file_copy {
542
+ my ($grafts, $l_map, $u) = @_;
543
+ my $paths = $l_map->{$u};
544
+ my $pfx = common_prefix([keys %$paths]);
545
+
546
+ my $pid = open my $fh, '-|';
547
+ defined $pid or croak $!;
548
+ unless ($pid) {
549
+ exec(qw/svn log -v/, $u.$pfx) or croak $!;
550
+ }
551
+ my ($r, $mp) = (undef, undef);
552
+ while (<$fh>) {
553
+ chomp;
554
+ if (/^\-{72}$/) {
555
+ $mp = $r = undef;
556
+ } elsif (/^r(\d+) \| /) {
557
+ $r = $1 unless defined $r;
558
+ } elsif (/^Changed paths:/) {
559
+ $mp = 1;
560
+ } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
561
+ my $dbg = "r$r | $_";
562
+ my ($p1, $p0, $r0) = ($1, $2, $3);
563
+ my $c;
564
+ foreach my $x (keys %$paths) {
565
+ next unless ($p1 =~ /^\Q$x\E/);
566
+ my $i = $paths->{$x};
567
+ my $f = "$GIT_DIR/svn/$i/revs/$r";
568
+ unless (-r $f) {
569
+ print STDERR "r$r of $i not imported,",
570
+ " $dbg\n";
571
+ next;
572
+ }
573
+ $c = file_to_s($f);
574
+ }
575
+ next unless $c;
576
+ foreach my $x (keys %$paths) {
577
+ next unless ($p0 =~ /^\Q$x\E/);
578
+ my $i = $paths->{$x};
579
+ my $f = "$GIT_DIR/svn/$i/revs/$r0";
580
+ while ($r0 && !-r $f) {
581
+ # could be an older revision, too...
582
+ $r0--;
583
+ $f = "$GIT_DIR/svn/$i/revs/$r0";
584
+ }
585
+ unless (-r $f) {
586
+ print STDERR "r$r0 of $i not imported,",
587
+ " $dbg\n";
588
+ next;
589
+ }
590
+ my $r1 = file_to_s($f);
591
+ $grafts->{$c}->{$r1} = 1;
592
+ }
593
+ }
594
+ }
595
+}
596
+
597
+sub process_merge_msg_matches {
598
+ my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
599
+ my (@strong, @weak);
600
+ foreach (@matches) {
601
+ # merging with ourselves is not interesting
602
+ next if $_ eq $p;
603
+ if ($l_map->{$u}->{$_}) {
604
+ push @strong, $_;
605
+ } else {
606
+ push @weak, $_;
607
+ }
608
+ }
609
+ foreach my $w (@weak) {
610
+ last if @strong;
611
+ # no exact match, use branch name as regexp.
612
+ my $re = qr/\Q$w\E/i;
613
+ foreach (keys %{$l_map->{$u}}) {
614
+ if (/$re/) {
615
+ push @strong, $_;
616
+ last;
617
+ }
618
+ }
619
+ last if @strong;
620
+ $w = basename($w);
621
+ $re = qr/\Q$w\E/i;
622
+ foreach (keys %{$l_map->{$u}}) {
623
+ if (/$re/) {
624
+ push @strong, $_;
625
+ last;
626
+ }
627
+ }
628
+ }
629
+ my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
630
+ \s(?:[a-f\d\-]+)$/xsm);
631
+ unless (defined $rev) {
632
+ ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
633
+ \@(?:[a-f\d\-]+)/xsm);
634
+ return unless defined $rev;
635
+ }
636
+ foreach my $m (@strong) {
637
+ my ($r0, $s0) = find_rev_before($rev, $m);
638
+ $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
639
+ }
640
+}
641
+
642
+sub graft_merge_msg {
643
+ my ($grafts, $l_map, $u, $p, @re) = @_;
644
+
645
+ my $x = $l_map->{$u}->{$p};
646
+ my $rl = rev_list_raw($x);
647
+ while (my $c = next_rev_list_entry($rl)) {
648
+ foreach my $re (@re) {
649
+ my (@br) = ($c->{m} =~ /$re/g);
650
+ next unless @br;
651
+ process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
652
+ }
653
+ }
654
+}
655
+
656
sub read_uuid {
657
return if $SVN_UUID;
658
my $info = shift || svn_info('.');
695
$url .= "/$n";
696
}
697
push @repo_path_split_cache, qr/^(\Q$url\E)/;
698
+ $path = join('/',@paths);
699
return ($url, $path);
700
}
701
1100
return fetch("$committed=$commit")->{revision};
1101
}
1102
1103
+sub rev_list_raw {
1104
+ my (@args) = @_;
1105
+ my $pid = open my $fh, '-|';
1106
+ defined $pid or croak $!;
1107
+ if (!$pid) {
1108
+ exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1109
+ }
1110
+ return { fh => $fh, t => { } };
1111
+}
1112
+
1113
+sub next_rev_list_entry {
1114
+ my $rl = shift;
1115
+ my $fh = $rl->{fh};
1116
+ my $x = $rl->{t};
1117
+ while (<$fh>) {
1118
+ if (/^commit ($sha1)$/o) {
1119
+ if ($x->{c}) {
1120
+ $rl->{t} = { c => $1 };
1121
+ return $x;
1122
+ } else {
1123
+ $x->{c} = $1;
1124
+ }
1125
+ } elsif (/^parent ($sha1)$/o) {
1126
+ $x->{p}->{$1} = 1;
1127
+ } elsif (s/^ //) {
1128
+ $x->{m} ||= '';
1129
+ $x->{m} .= $_;
1130
+ }
1131
+ }
1132
+ return ($x != $rl->{t}) ? $x : undef;
1133
+}
1134
+
1135
# read the entire log into a temporary file (which is removed ASAP)
1136
# and store the file handle + parser state
1137
sub svn_log_raw {
1644
return safe_qx(qw/svn propget/, $p, $f);
1645
}
1646
1647
+sub git_svn_each {
1648
+ my $sub = shift;
1649
+ foreach (`git-rev-parse --symbolic --all`) {
1650
+ next unless s#^refs/remotes/##;
1651
+ chomp $_;
1652
+ next unless -f "$GIT_DIR/svn/$_/info/url";
1653
+ &$sub($_);
1654
+ }
1655
+}
1656
+
1657
sub migration_check {
1658
return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
1659
print "Upgrading repository...\n";
1680
print "Done upgrading.\n";
1681
}
1682
1683
+sub find_rev_before {
1684
+ my ($r, $git_svn_id) = @_;
1685
+ my @revs = map { basename $_ } <$GIT_DIR/svn/$git_svn_id/revs/*>;
1686
+ foreach my $r0 (sort { $b <=> $a } @revs) {
1687
+ next if $r0 >= $r;
1688
+ return ($r0, file_to_s("$GIT_DIR/svn/$git_svn_id/revs/$r0"));
1689
+ }
1690
+ return (undef, undef);
1691
+}
1692
+
1693
sub init_vars {
1694
$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1695
$GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1730
}
1731
}
1732
1733
+sub read_grafts {
1734
+ my $gr_file = shift;
1735
+ my ($grafts, $comments) = ({}, {});
1736
+ if (open my $fh, '<', $gr_file) {
1737
+ my @tmp;
1738
+ while (<$fh>) {
1739
+ if (/^($sha1)\s+/) {
1740
+ my $c = $1;
1741
+ if (@tmp) {
1742
+ @{$comments->{$c}} = @tmp;
1743
+ @tmp = ();
1744
+ }
1745
+ foreach my $p (split /\s+/, $_) {
1746
+ $grafts->{$c}->{$p} = 1;
1747
+ }
1748
+ } else {
1749
+ push @tmp, $_;
1750
+ }
1751
+ }
1752
+ close $fh or croak $!;
1753
+ @{$comments->{'END'}} = @tmp if @tmp;
1754
+ }
1755
+ return ($grafts, $comments);
1756
+}
1757
+
1758
+sub write_grafts {
1759
+ my ($grafts, $comments, $gr_file) = @_;
1760
+
1761
+ open my $fh, '>', $gr_file or croak $!;
1762
+ foreach my $c (sort keys %$grafts) {
1763
+ if ($comments->{$c}) {
1764
+ print $fh $_ foreach @{$comments->{$c}};
1765
+ }
1766
+ my $p = $grafts->{$c};
1767
+ delete $p->{$c}; # commits are not self-reproducing...
1768
+ my $pid = open my $ch, '-|';
1769
+ defined $pid or croak $!;
1770
+ if (!$pid) {
1771
+ exec(qw/git-cat-file commit/, $c) or croak $!;
1772
+ }
1773
+ while (<$ch>) {
1774
+ if (/^parent ([a-f\d]{40})/) {
1775
+ $p->{$1} = 1;
1776
+ } else {
1777
+ last unless /^\S/i;
1778
+ }
1779
+ }
1780
+ close $ch; # breaking the pipe
1781
+ print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1782
+ }
1783
+ if ($comments->{'END'}) {
1784
+ print $fh $_ foreach @{$comments->{'END'}};
1785
+ }
1786
+ close $fh or croak $!;
1787
+}
1788
+
1789
+sub read_url_paths {
1790
+ my $l_map = {};
1791
+ git_svn_each(sub { my $x = shift;
1792
+ my $u = file_to_s("$GIT_DIR/svn/$x/info/repo_url");
1793
+ my $p = file_to_s("$GIT_DIR/svn/$x/info/repo_path");
1794
+ # we hate trailing slashes
1795
+ if ($u =~ s#(?:^\/+|\/+$)##g) {
1796
+ s_to_file($u,"$GIT_DIR/svn/$x/info/repo_url");
1797
+ }
1798
+ if ($p =~ s#(?:^\/+|\/+$)##g) {
1799
+ s_to_file($p,"$GIT_DIR/svn/$x/info/repo_path");
1800
+ }
1801
+ $l_map->{$u}->{$p} = $x;
1802
+ });
1803
+ return $l_map;
1804
+}
1805
+
1806
__END__
1807
1808
Data structures: