2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $REV_DIR $GIT_SVN_DIR/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '1.1.0-pre';
14 $GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15 $ENV{GIT_DIR} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL};
19 # make sure the svn binary gives consistent output between locales and TZs:
23 # If SVN:: library support is added, please make the dependencies
24 # optional and preserve the capability to use the command-line client.
25 # use eval { require SVN::... } to make it lazy load
26 # We don't use any modules not in the standard Perl distribution:
29 use File::Basename qw/dirname basename/;
30 use File::Path qw/mkpath/;
31 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
33 use POSIX qw/strftime/;
34 my $sha1 = qr/[a-f\d]{40}/;
35 my $sha1_short = qr/[a-f\d]{4,40}/;
36 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
37 $_find_copies_harder, $_l, $_cp_similarity,
38 $_repack, $_repack_nr, $_repack_flags,
39 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
40 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
41 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
42 my (@_branch_from, %tree_map, %users, %rusers);
43 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
44 my @repo_path_split_cache;
46 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
47 'branch|b=s' => \@_branch_from,
48 'branch-all-refs|B' => \$_branch_all_refs,
49 'authors-file|A=s' => \$_authors,
50 'repack:i' => \$_repack,
51 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
53 my ($_trunk, $_tags, $_branches);
54 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
55 'tags|t=s' => \$_tags,
56 'branches|b=s' => \$_branches );
57 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
59 # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
60 my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
63 fetch => [ \&fetch, "Download new revisions from SVN",
64 { 'revision|r=s' => \$_revision, %fc_opts } ],
65 init => [ \&init, "Initialize a repo for tracking" .
66 " (requires URL argument)",
68 commit => [ \&commit, "Commit git revisions to SVN",
69 { 'stdin|' => \$_stdin,
72 'find-copies-harder' => \$_find_copies_harder,
74 'copy-similarity|C=i'=> \$_cp_similarity,
77 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
78 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
79 { 'no-ignore-externals' => \$_no_ignore_ext,
80 'upgrade' => \$_upgrade } ],
81 'graft-branches' => [ \&graft_branches,
82 'Detect merges/branches from already imported history',
83 { 'merge-rx|m' => \@_opt_m,
84 'no-default-regex' => \$_no_default_regex,
85 'no-graft-copy' => \$_no_graft_copy } ],
86 'multi-init' => [ \&multi_init,
87 'Initialize multiple trees (like git-svnimport)',
88 { %multi_opts, %fc_opts } ],
89 'multi-fetch' => [ \&multi_fetch,
90 'Fetch multiple trees (like git-svnimport)',
92 'log' => [ \&show_log, 'Show commit logs',
93 { 'limit=i' => \$_limit,
94 'revision|r=s' => \$_revision,
95 'verbose|v' => \$_verbose,
96 'incremental' => \$_incremental,
97 'oneline' => \$_oneline,
98 'show-commit' => \$_show_commit,
99 'authors-file|A=s' => \$_authors,
104 for (my $i = 0; $i < @ARGV; $i++) {
105 if (defined $cmd{$ARGV[$i]}) {
112 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
114 read_repo_config(\%opts);
115 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
116 'version|V' => \$_version,
117 'id|i=s' => \$GIT_SVN);
118 exit 1 if (!$rv && $cmd ne 'log');
122 version() if $_version;
123 usage(1) unless defined $cmd;
125 load_authors() if $_authors;
126 load_all_refs() if $_branch_all_refs;
128 migration_check() unless $cmd =~ /^(?:init|multi-init)$/;
129 $cmd{$cmd}->[0]->(@ARGV);
132 ####################### primary functions ######################
134 my $exit = shift || 0;
135 my $fd = $exit ? \*STDERR : \*STDOUT;
137 git-svn - bidirectional operations between a single Subversion tree and git
138 Usage: $0 <command> [options] [arguments]\n
140 print $fd "Available commands:\n" unless $cmd;
142 foreach (sort keys %cmd) {
143 next if $cmd && $cmd ne $_;
144 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
145 foreach (keys %{$cmd{$_}->[2]}) {
146 # prints out arguments as they should be passed:
147 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
148 print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
150 split /\|/,$_)," $x\n";
154 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
155 arbitrary identifier if you're tracking multiple SVN branches/repositories in
156 one git repository and want to keep them separate. See git-svn(1) for more
163 print "git-svn version $VERSION\n";
168 $SVN_URL = shift or undef;
171 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
173 check_upgrade_needed();
176 my $pid = open(my $rev_list,'-|');
177 defined $pid or croak $!;
179 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
182 while (<$rev_list>) {
185 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
186 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
187 next if (!@commit); # skip merges
188 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
189 if (!$rev || !$uuid) {
190 croak "Unable to extract revision or UUID from ",
191 "$c, $commit[$#commit]\n";
194 # if we merged or otherwise started elsewhere, this is
195 # how we break out of it
196 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
197 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
199 print "r$rev = $c\n";
200 unless (defined $latest) {
201 if (!$SVN_URL && !$url) {
202 croak "SVN repository location required: $url\n";
209 assert_revision_eq_or_unknown($rev, $c);
210 sys('git-update-ref',"svn/$GIT_SVN/revs/$rev",$c);
211 $newest_rev = $rev if ($rev > $newest_rev);
213 close $rev_list or croak $?;
214 if (!chdir $SVN_WC) {
215 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
216 chdir $SVN_WC or croak $!;
220 defined $pid or croak $!;
222 my @svn_up = qw(svn up);
223 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
224 sys(@svn_up,"-r$newest_rev");
225 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
227 exec('git-write-tree') or croak $!;
234 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
235 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
241 $SVN_URL = shift or die "SVN repository location required " .
242 "as a command-line argument\n";
243 $SVN_URL =~ s!/+$!!; # strip trailing slash
244 unless (-d $GIT_DIR) {
245 my @init_db = ('git-init-db');
246 push @init_db, "--template=$_template" if defined $_template;
247 push @init_db, "--shared" if defined $_shared;
255 check_upgrade_needed();
256 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
257 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
258 unless ($_revision) {
259 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
261 push @log_args, "-r$_revision";
262 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
264 my $svn_log = svn_log_raw(@log_args);
266 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
267 my $last_commit = undef;
268 unless (-d $SVN_WC) {
269 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
270 chdir $SVN_WC or croak $!;
272 $last_commit = git_commit($base, @parents);
273 assert_tree($last_commit);
275 chdir $SVN_WC or croak $!;
277 eval { $last_commit = file_to_s("$REV_DIR/$base->{revision}") };
278 # looks like a user manually cp'd and svn switch'ed
279 unless ($last_commit) {
280 sys(qw/svn revert -R ./);
281 assert_svn_wc_clean($base->{revision});
282 $last_commit = git_commit($base, @parents);
283 assert_tree($last_commit);
286 my @svn_up = qw(svn up);
287 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
289 while (my $log_msg = next_log_entry($svn_log)) {
290 assert_tree($last_commit);
291 if ($last->{revision} >= $log_msg->{revision}) {
292 croak "Out of order: last >= current: ",
293 "$last->{revision} >= $log_msg->{revision}\n";
295 # Revert is needed for cases like:
296 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
297 # I can't seem to reproduce something like that on a test...
298 sys(qw/svn revert -R ./);
299 assert_svn_wc_clean($last->{revision});
300 sys(@svn_up,"-r$log_msg->{revision}");
301 $last_commit = git_commit($log_msg, $last_commit, @parents);
304 unless (-e "$GIT_DIR/refs/heads/master") {
305 sys(qw(git-update-ref refs/heads/master),$last_commit);
307 close $svn_log->{fh};
313 check_upgrade_needed();
314 if ($_stdin || !@commits) {
315 print "Reading from stdin...\n";
318 if (/\b($sha1_short)\b/o) {
319 unshift @commits, $1;
324 foreach my $c (@commits) {
325 chomp(my @tmp = safe_qx('git-rev-parse',$c));
326 if (scalar @tmp == 1) {
328 } elsif (scalar @tmp > 1) {
329 push @revs, reverse (safe_qx('git-rev-list',@tmp));
331 die "Failed to rev-parse $c\n";
336 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
337 my $info = svn_info('.');
338 my $fetched = fetch();
339 if ($info->{Revision} != $fetched->{revision}) {
340 print STDERR "There are new revisions that were fetched ",
341 "and need to be merged (or acknowledged) ",
342 "before committing.\n";
345 $info = svn_info('.');
347 my $svn_current_rev = $info->{'Last Changed Rev'};
348 foreach my $c (@revs) {
349 my $mods = svn_checkout_tree($svn_current_rev, $c);
350 if (scalar @$mods == 0) {
351 print "Skipping, no changes detected\n";
354 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
356 print "Done committing ",scalar @revs," revisions to SVN\n";
360 require File::Find or die $!;
361 my $exclude_file = "$GIT_DIR/info/exclude";
362 open my $fh, '<', $exclude_file or croak $!;
363 chomp(my @excludes = (<$fh>));
364 close $fh or croak $!;
366 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
367 chdir $SVN_WC or croak $!;
369 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
371 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
372 }}, no_chdir=>1},'.');
375 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
377 foreach my $i (sort keys %ign) {
378 print "\n# ",$i,"\n";
379 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
384 my $gr_file = "$GIT_DIR/info/grafts";
385 my ($grafts, $comments) = read_grafts($gr_file);
389 # temporarily disable our grafts file to make this idempotent
390 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
391 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
394 my $l_map = read_url_paths();
395 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
396 unless ($_no_default_regex) {
397 push @re, ( qr/\b(?:merge|merging|merged)\s+(\S.+)/is,
398 qr/\b(?:from|of)\s+(\S.+)/is );
400 foreach my $u (keys %$l_map) {
402 foreach my $p (keys %{$l_map->{$u}}) {
403 graft_merge_msg($grafts,$l_map,$u,$p);
406 graft_file_copy($grafts,$l_map,$u) unless $_no_graft_copy;
409 write_grafts($grafts, $comments, $gr_file);
410 unlink "$gr_file~$gr_sha1" if $gr_sha1;
417 $url =~ s#/+$## if $url;
418 if ($_trunk !~ m#^[a-z\+]+://#) {
419 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
421 print STDERR "E: '$_trunk' is not a complete URL ",
422 "and a separate URL is not specified\n";
425 $_trunk = $url . $_trunk;
427 if ($GIT_SVN eq 'git-svn') {
428 print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
429 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
433 complete_url_ls_init($url, $_branches, '--branches/-b', '');
434 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
438 # try to do trunk first, since branches/tags
439 # may be descended from it.
440 if (-d "$GIT_DIR/svn/trunk") {
441 print "Fetching trunk\n";
442 defined(my $pid = fork) or croak $!;
444 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
452 rec_fetch('', "$GIT_DIR/svn", @_);
458 my $r_last = -1; # prevent dupes
459 rload_authors() if $_authors;
465 if (defined $_revision) {
466 if ($_revision =~ /^(\d+):(\d+)$/) {
467 ($r_min, $r_max) = ($1, $2);
468 } elsif ($_revision =~ /^\d+$/) {
469 $r_min = $r_max = $_revision;
471 print STDERR "-r$_revision is not supported, use ",
472 "standard \'git log\' arguments instead\n";
477 my $pid = open(my $log,'-|');
478 defined $pid or croak $!;
480 my @rl = (qw/git-log --abbrev-commit --pretty=raw
481 --default/, "remotes/$GIT_SVN");
482 push @rl, '--raw' if $_verbose;
483 exec(@rl, @args) or croak $!;
488 if (/^commit ($sha1_short)/o) {
490 if ($c && defined $c->{r} && $c->{r} != $r_last) {
492 process_commit($c, $r_min, $r_max, \@k) or
497 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
498 get_author_info($c, $1, $2, $3);
499 } elsif (/^(?:tree|parent|committer) /) {
501 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
502 push @{$c->{raw}}, $_;
505 push @{$c->{diff}}, $_;
507 push @{$c->{diff}}, $_;
508 } elsif (/^ (git-svn-id:.+)$/) {
509 my ($url, $rev, $uuid) = extract_metadata($1);
515 if ($c && defined $c->{r} && $c->{r} != $r_last) {
517 process_commit($c, $r_min, $r_max, \@k);
523 process_commit($_, $r_min, $r_max) foreach reverse @k;
527 print '-' x72,"\n" unless $_incremental || $_oneline;
530 ########################### utility functions #########################
533 my ($pfx, $p, @args) = @_;
535 foreach (sort <$p/*>) {
536 if (-r "$_/info/url") {
537 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
538 my $id = $pfx . basename $_;
539 next if $id eq 'trunk';
540 print "Fetching $id\n";
541 defined(my $pid = fork) or croak $!;
543 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
556 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
561 sub complete_url_ls_init {
562 my ($url, $var, $switch, $pfx) = @_;
564 print STDERR "W: $switch not specified\n";
568 if ($var !~ m#^[a-z\+]+://#) {
569 $var = '/' . $var if ($var !~ m#^/#);
571 print STDERR "E: '$var' is not a complete URL ",
572 "and a separate URL is not specified\n";
577 chomp(my @ls = safe_qx(qw/svn ls --non-interactive/, $var));
579 defined(my $pid = fork) or croak $!;
581 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
583 if ($u !~ m!\Q$var\E/(.+)$!) {
584 print STDERR "W: Unrecognized URL: $u\n";
585 die "This should never happen\n";
588 print "init $u => $id\n";
589 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
603 my @tmp = split m#/#, $_;
605 while (my $x = shift @tmp) {
611 foreach (sort {length $b <=> length $a} keys %common) {
612 if ($common{$_} == @$paths) {
619 # this isn't funky-filename safe, but good enough for now...
620 sub graft_file_copy {
621 my ($grafts, $l_map, $u) = @_;
622 my $paths = $l_map->{$u};
623 my $pfx = common_prefix([keys %$paths]);
625 my $pid = open my $fh, '-|';
626 defined $pid or croak $!;
628 exec(qw/svn log -v/, $u.$pfx) or croak $!;
630 my ($r, $mp) = (undef, undef);
635 } elsif (/^r(\d+) \| /) {
636 $r = $1 unless defined $r;
637 } elsif (/^Changed paths:/) {
639 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
640 my $dbg = "r$r | $_";
641 my ($p1, $p0, $r0) = ($1, $2, $3);
643 foreach my $x (keys %$paths) {
644 next unless ($p1 =~ /^\Q$x\E/);
645 my $i = $paths->{$x};
646 my $f = "$GIT_DIR/svn/$i/revs/$r";
648 print STDERR "r$r of $i not imported,",
655 foreach my $x (keys %$paths) {
656 next unless ($p0 =~ /^\Q$x\E/);
657 my $i = $paths->{$x};
658 my $f = "$GIT_DIR/svn/$i/revs/$r0";
659 while ($r0 && !-r $f) {
660 # could be an older revision, too...
662 $f = "$GIT_DIR/svn/$i/revs/$r0";
665 print STDERR "r$r0 of $i not imported,",
669 my $r1 = file_to_s($f);
670 $grafts->{$c}->{$r1} = 1;
676 sub process_merge_msg_matches {
677 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
680 # merging with ourselves is not interesting
682 if ($l_map->{$u}->{$_}) {
688 foreach my $w (@weak) {
690 # no exact match, use branch name as regexp.
691 my $re = qr/\Q$w\E/i;
692 foreach (keys %{$l_map->{$u}}) {
701 foreach (keys %{$l_map->{$u}}) {
708 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
709 \s(?:[a-f\d\-]+)$/xsm);
710 unless (defined $rev) {
711 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
712 \@(?:[a-f\d\-]+)/xsm);
713 return unless defined $rev;
715 foreach my $m (@strong) {
716 my ($r0, $s0) = find_rev_before($rev, $m);
717 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
721 sub graft_merge_msg {
722 my ($grafts, $l_map, $u, $p, @re) = @_;
724 my $x = $l_map->{$u}->{$p};
725 my $rl = rev_list_raw($x);
726 while (my $c = next_rev_list_entry($rl)) {
727 foreach my $re (@re) {
728 my (@br) = ($c->{m} =~ /$re/g);
730 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
737 my $info = shift || svn_info('.');
738 $SVN_UUID = $info->{'Repository UUID'} or
739 croak "Repository UUID unreadable\n";
740 s_to_file($SVN_UUID,"$GIT_SVN_DIR/info/uuid");
745 defined $pid or croak $!;
747 open my $null, '>', '/dev/null' or croak $!;
748 open STDERR, '>&', $null or croak $!;
749 open STDOUT, '>&', $null or croak $!;
756 sub repo_path_split {
757 my $full_url = shift;
758 $full_url =~ s#/+$##;
760 foreach (@repo_path_split_cache) {
761 if ($full_url =~ s#$_##) {
763 $full_url =~ s#^/+##;
764 return ($u, $full_url);
768 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
770 my @paths = split(m#/+#, $path);
772 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
773 my $n = shift @paths || last;
776 push @repo_path_split_cache, qr/^(\Q$url\E)/;
777 $path = join('/',@paths);
778 return ($url, $path);
782 defined $SVN_URL or croak "SVN repository location required\n";
783 unless (-d $GIT_DIR) {
784 croak "GIT_DIR=$GIT_DIR does not exist!\n";
786 mkpath([$GIT_SVN_DIR]);
787 mkpath(["$GIT_SVN_DIR/info"]);
789 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
791 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
792 print $fd '.svn',"\n";
793 close $fd or croak $!;
794 my ($url, $path) = repo_path_split($SVN_URL);
795 s_to_file($url, "$GIT_SVN_DIR/info/repo_url");
796 s_to_file($path, "$GIT_SVN_DIR/info/repo_path");
799 sub assert_svn_wc_clean {
801 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
802 my $lcr = svn_info('.')->{'Last Changed Rev'};
803 if ($svn_rev != $lcr) {
804 print STDERR "Checking for copy-tree ... ";
805 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
806 "-r$lcr:$svn_rev")));
808 croak "Nope! Expected r$svn_rev, got r$lcr\n";
810 print STDERR "OK!\n";
813 my @status = grep(!/^Performing status on external/,(`svn status`));
814 @status = grep(!/^\s*$/,@status);
815 if (scalar @status) {
816 print STDERR "Tree ($SVN_WC) is not clean:\n";
817 print STDERR $_ foreach @status;
824 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
825 chomp(my $type = `git-cat-file -t $treeish`);
827 while ($type eq 'tag') {
828 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
830 if ($type eq 'commit') {
831 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
832 ($expected) = ($expected =~ /^tree ($sha1)$/);
833 die "Unable to get tree from $treeish\n" unless $expected;
834 } elsif ($type eq 'tree') {
835 $expected = $treeish;
837 die "$treeish is a $type, expected tree, tag or commit\n";
840 my $old_index = $ENV{GIT_INDEX_FILE};
841 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
843 unlink $tmpindex or croak $!;
845 $ENV{GIT_INDEX_FILE} = $tmpindex;
847 chomp(my $tree = `git-write-tree`);
849 $ENV{GIT_INDEX_FILE} = $old_index;
851 delete $ENV{GIT_INDEX_FILE};
853 if ($tree ne $expected) {
854 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
859 sub parse_diff_tree {
865 chomp $_; # this gets rid of the trailing "\0"
866 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
867 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
868 push @mods, { mode_a => $1, mode_b => $2,
869 sha1_b => $3, chg => $4 };
870 if ($4 =~ /^(?:C|R)$/) {
875 } elsif ($state eq 'file_a') {
876 my $x = $mods[$#mods] or croak "Empty array\n";
877 if ($x->{chg} !~ /^(?:C|R)$/) {
878 croak "Error parsing $_, $x->{chg}\n";
882 } elsif ($state eq 'file_b') {
883 my $x = $mods[$#mods] or croak "Empty array\n";
884 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
885 croak "Error parsing $_, $x->{chg}\n";
887 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
888 croak "Error parsing $_, $x->{chg}\n";
893 croak "Error parsing $_\n";
896 close $diff_fh or croak $!;
901 sub svn_check_prop_executable {
903 return if -l $m->{file_b};
904 if ($m->{mode_b} =~ /755$/) {
905 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
906 if ($m->{mode_a} !~ /755$/) {
907 sys(qw(svn propset svn:executable 1), $m->{file_b});
909 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
910 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
911 sys(qw(svn propdel svn:executable), $m->{file_b});
912 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
913 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
917 sub svn_ensure_parent_path {
918 my $dir_b = dirname(shift);
919 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
920 mkpath([$dir_b]) unless (-d $dir_b);
921 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
924 sub precommit_check {
926 my (%rm_file, %rmdir_check, %added_check);
928 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
929 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
930 if ($m->{chg} eq 'R') {
931 if (-d $m->{file_b}) {
932 err_dir_to_file("$m->{file_a} => $m->{file_b}");
934 # dir/$file => dir/file/$file
935 my $dirname = dirname($m->{file_b});
936 while ($dirname ne File::Spec->curdir) {
937 if ($dirname ne $m->{file_a}) {
938 $dirname = dirname($dirname);
941 err_file_to_dir("$m->{file_a} => $m->{file_b}");
943 # baz/zzz => baz (baz is a file)
944 $dirname = dirname($m->{file_a});
945 while ($dirname ne File::Spec->curdir) {
946 if ($dirname ne $m->{file_b}) {
947 $dirname = dirname($dirname);
950 err_dir_to_file("$m->{file_a} => $m->{file_b}");
953 if ($m->{chg} =~ /^(D|R)$/) {
954 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
955 $rm_file{ $m->{$t} } = 1;
956 my $dirname = dirname( $m->{$t} );
957 my $basename = basename( $m->{$t} );
958 $rmdir_check{$dirname}->{$basename} = 1;
959 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
960 if (-d $m->{file_b}) {
961 err_dir_to_file($m->{file_b});
963 my $dirname = dirname( $m->{file_b} );
964 my $basename = basename( $m->{file_b} );
965 $added_check{$dirname}->{$basename} = 1;
966 while ($dirname ne File::Spec->curdir) {
967 if ($rm_file{$dirname}) {
968 err_file_to_dir($m->{file_b});
970 $dirname = dirname $dirname;
974 return (\%rmdir_check, \%added_check);
976 sub err_dir_to_file {
978 print STDERR "Node change from directory to file ",
979 "is not supported by Subversion: ",$file,"\n";
982 sub err_file_to_dir {
984 print STDERR "Node change from file to directory ",
985 "is not supported by Subversion: ",$file,"\n";
990 sub svn_checkout_tree {
991 my ($svn_rev, $treeish) = @_;
992 my $from = file_to_s("$REV_DIR/$svn_rev");
994 print "diff-tree $from $treeish\n";
995 my $pid = open my $diff_fh, '-|';
996 defined $pid or croak $!;
998 my @diff_tree = qw(git-diff-tree -z -r);
999 if ($_cp_similarity) {
1000 push @diff_tree, "-C$_cp_similarity";
1002 push @diff_tree, '-C';
1004 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1005 push @diff_tree, "-l$_l" if defined $_l;
1006 exec(@diff_tree, $from, $treeish) or croak $!;
1008 my $mods = parse_diff_tree($diff_fh);
1010 # git can do empty commits, but SVN doesn't allow it...
1013 my ($rm, $add) = precommit_check($mods);
1015 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1016 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1017 if ($m->{chg} eq 'C') {
1018 svn_ensure_parent_path( $m->{file_b} );
1019 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
1020 apply_mod_line_blob($m);
1021 svn_check_prop_executable($m);
1022 } elsif ($m->{chg} eq 'D') {
1023 sys(qw(svn rm --force), $m->{file_b});
1024 } elsif ($m->{chg} eq 'R') {
1025 svn_ensure_parent_path( $m->{file_b} );
1026 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
1027 apply_mod_line_blob($m);
1028 svn_check_prop_executable($m);
1029 } elsif ($m->{chg} eq 'M') {
1030 apply_mod_line_blob($m);
1031 svn_check_prop_executable($m);
1032 } elsif ($m->{chg} eq 'T') {
1033 sys(qw(svn rm --force),$m->{file_b});
1034 apply_mod_line_blob($m);
1035 sys(qw(svn add --force), $m->{file_b});
1036 svn_check_prop_executable($m);
1037 } elsif ($m->{chg} eq 'A') {
1038 svn_ensure_parent_path( $m->{file_b} );
1039 apply_mod_line_blob($m);
1040 sys(qw(svn add --force), $m->{file_b});
1041 svn_check_prop_executable($m);
1043 croak "Invalid chg: $m->{chg}\n";
1047 assert_tree($treeish);
1048 if ($_rmdir) { # remove empty directories
1049 handle_rmdir($rm, $add);
1051 assert_tree($treeish);
1055 # svn ls doesn't work with respect to the current working tree, but what's
1056 # in the repository. There's not even an option for it... *sigh*
1057 # (added files don't show up and removed files remain in the ls listing)
1058 sub svn_ls_current {
1059 my ($dir, $rm, $add) = @_;
1060 chomp(my @ls = safe_qx('svn','ls',$dir));
1063 s#/$##; # trailing slashes are evil
1064 push @ret, $_ unless $rm->{$dir}->{$_};
1066 if (exists $add->{$dir}) {
1067 push @ret, keys %{$add->{$dir}};
1073 my ($rm, $add) = @_;
1075 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1076 my $ls = svn_ls_current($dir, $rm, $add);
1077 next if (scalar @$ls);
1078 sys(qw(svn rm --force),$dir);
1080 my $dn = dirname $dir;
1081 $rm->{ $dn }->{ basename $dir } = 1;
1082 $ls = svn_ls_current($dn, $rm, $add);
1083 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1084 sys(qw(svn rm --force),$dn);
1085 $dir = basename $dn;
1087 $rm->{ $dn }->{ $dir } = 1;
1088 $ls = svn_ls_current($dn, $rm, $add);
1093 sub svn_commit_tree {
1094 my ($svn_rev, $commit) = @_;
1095 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1096 my %log_msg = ( msg => '' );
1097 open my $msg, '>', $commit_msg or croak $!;
1099 chomp(my $type = `git-cat-file -t $commit`);
1100 if ($type eq 'commit') {
1101 my $pid = open my $msg_fh, '-|';
1102 defined $pid or croak $!;
1105 exec(qw(git-cat-file commit), $commit) or croak $!;
1110 $in_msg = 1 if (/^\s*$/);
1111 } elsif (/^git-svn-id: /) {
1112 # skip this, we regenerate the correct one
1113 # on re-fetch anyways
1115 print $msg $_ or croak $!;
1118 close $msg_fh or croak $!;
1120 close $msg or croak $!;
1122 if ($_edit || ($type eq 'tree')) {
1123 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1124 system($editor, $commit_msg);
1127 # file_to_s removes all trailing newlines, so just use chomp() here:
1128 open $msg, '<', $commit_msg or croak $!;
1129 { local $/; chomp($log_msg{msg} = <$msg>); }
1130 close $msg or croak $!;
1132 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
1133 print "Committing $commit: $oneline\n";
1135 if (defined $LC_ALL) {
1136 $ENV{LC_ALL} = $LC_ALL;
1138 delete $ENV{LC_ALL};
1140 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1143 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1144 if (!defined $committed) {
1145 my $out = join("\n",@ci_output);
1146 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1147 $out, "\n\nAssuming English locale...";
1148 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1149 defined $committed or die " FAILED!\n",
1150 "Commit output failed to parse committed revision!\n",
1151 print STDERR " OK\n";
1154 my @svn_up = qw(svn up);
1155 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1156 if ($committed == ($svn_rev + 1)) {
1157 push @svn_up, "-r$committed";
1159 my $info = svn_info('.');
1160 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1161 if ($info->{'Last Changed Rev'} != $committed) {
1162 croak "$info->{'Last Changed Rev'} != $committed\n"
1164 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1165 /(\d{4})\-(\d\d)\-(\d\d)\s
1166 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1167 or croak "Failed to parse date: $date\n";
1168 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
1169 $log_msg{author} = $info->{'Last Changed Author'};
1170 $log_msg{revision} = $committed;
1171 $log_msg{msg} .= "\n";
1172 my $parent = file_to_s("$REV_DIR/$svn_rev");
1173 git_commit(\%log_msg, $parent, $commit);
1176 # resync immediately
1177 push @svn_up, "-r$svn_rev";
1179 return fetch("$committed=$commit")->{revision};
1184 my $pid = open my $fh, '-|';
1185 defined $pid or croak $!;
1187 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1189 return { fh => $fh, t => { } };
1192 sub next_rev_list_entry {
1197 if (/^commit ($sha1)$/o) {
1199 $rl->{t} = { c => $1 };
1204 } elsif (/^parent ($sha1)$/o) {
1211 return ($x != $rl->{t}) ? $x : undef;
1214 # read the entire log into a temporary file (which is removed ASAP)
1215 # and store the file handle + parser state
1217 my (@log_args) = @_;
1218 my $log_fh = IO::File->new_tmpfile or croak $!;
1220 defined $pid or croak $!;
1222 open STDOUT, '>&', $log_fh or croak $!;
1223 exec (qw(svn log), @log_args) or croak $!
1227 seek $log_fh, 0, 0 or croak $!;
1228 return { state => 'sep', fh => $log_fh };
1231 sub next_log_entry {
1232 my $log = shift; # retval of svn_log_raw()
1234 my $fh = $log->{fh};
1239 if ($log->{state} eq 'msg') {
1240 if ($ret->{lines}) {
1241 $ret->{msg} .= $_."\n";
1242 unless(--$ret->{lines}) {
1243 $log->{state} = 'sep';
1246 croak "Log parse error at: $_\n",
1252 if ($log->{state} ne 'sep') {
1253 croak "Log parse error at: $_\n",
1254 "state: $log->{state}\n",
1258 $log->{state} = 'rev';
1260 # if we have an empty log message, put something there:
1262 $ret->{msg} ||= "\n";
1263 delete $ret->{lines};
1268 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1270 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1271 ($lines) = ($lines =~ /(\d+)/);
1272 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1273 /(\d{4})\-(\d\d)\-(\d\d)\s
1274 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1275 or croak "Failed to parse date: $date\n";
1276 $ret = { revision => $rev,
1277 date => "$tz $Y-$m-$d $H:$M:$S",
1281 if (defined $_authors && ! defined $users{$author}) {
1282 die "Author: $author not defined in ",
1285 $log->{state} = 'msg_start';
1288 # skip the first blank line of the message:
1289 if ($log->{state} eq 'msg_start' && /^$/) {
1290 $log->{state} = 'msg';
1291 } elsif ($log->{state} eq 'msg') {
1292 if ($ret->{lines}) {
1293 $ret->{msg} .= $_."\n";
1294 unless (--$ret->{lines}) {
1295 $log->{state} = 'sep';
1298 croak "Log parse error at: $_\n",
1299 $ret->{revision},"\n";
1307 my $url = shift || $SVN_URL;
1309 my $pid = open my $info_fh, '-|';
1310 defined $pid or croak $!;
1313 exec(qw(svn info),$url) or croak $!;
1317 # only single-lines seem to exist in svn info output
1318 while (<$info_fh>) {
1320 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
1322 push @{$ret->{-order}}, $1;
1325 close $info_fh or croak $!;
1329 sub sys { system(@_) == 0 or croak $? }
1332 my ($from, $to) = @_;
1333 my $es = svn_propget_base('svn:eol-style', $to);
1334 open my $rfd, '<', $from or croak $!;
1335 binmode $rfd or croak $!;
1336 open my $wfd, '>', $to or croak $!;
1337 binmode $wfd or croak $!;
1339 my $eol = $EOL{$es} or undef;
1344 defined($r = sysread($rfd, $buf, 4096)) or croak $!;
1347 if ($buf =~ /\015$/) {
1349 defined($r = sysread($rfd,$c,1)) or croak $!;
1350 $buf .= $c if $r > 0;
1352 $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
1355 for ($w = 0; $w < $r; $w += $t) {
1356 $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!;
1362 sub do_update_index {
1363 my ($z_cmd, $cmd, $no_text_base) = @_;
1365 my $z = open my $p, '-|';
1366 defined $z or croak $!;
1367 unless ($z) { exec @$z_cmd or croak $! }
1369 my $pid = open my $ui, '|-';
1370 defined $pid or croak $!;
1372 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1375 while (my $x = <$p>) {
1377 if (!$no_text_base && lstat $x && ! -l _ &&
1378 svn_propget_base('svn:keywords', $x)) {
1379 my $mode = -x _ ? 0755 : 0644;
1380 my ($v,$d,$f) = File::Spec->splitpath($x);
1381 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1382 'text-base',"$f.svn-base");
1385 $tb = File::Spec->catfile($d, '.svn',
1386 'text-base',"$f.svn-base");
1389 unlink $x or croak $!;
1391 chmod(($mode &~ umask), $x) or croak $!;
1395 close $ui or croak $!;
1399 my $no_text_base = shift;
1400 do_update_index([qw/git-diff-files --name-only -z/],
1403 do_update_index([qw/git-ls-files -z --others/,
1404 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1410 my ($str, $file, $mode) = @_;
1411 open my $fd,'>',$file or croak $!;
1412 print $fd $str,"\n" or croak $!;
1413 close $fd or croak $!;
1414 chmod ($mode &~ umask, $file) if (defined $mode);
1419 open my $fd,'<',$file or croak "$!: file: $file\n";
1422 close $fd or croak $!;
1427 sub assert_revision_unknown {
1429 if (-f "$REV_DIR/$revno") {
1430 croak "$REV_DIR/$revno already exists! ",
1431 "Why are we refetching it?";
1437 my @x = safe_qx('git-cat-file','commit',$x);
1438 my @y = safe_qx('git-cat-file','commit',$y);
1439 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1440 || $y[0] !~ /^tree $sha1\n$/) {
1441 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1447 sub assert_revision_eq_or_unknown {
1448 my ($revno, $commit) = @_;
1449 if (-f "$REV_DIR/$revno") {
1450 my $current = file_to_s("$REV_DIR/$revno");
1451 if (($commit ne $current) && !trees_eq($commit, $current)) {
1452 croak "$REV_DIR/$revno already exists!\n",
1453 "current: $current\nexpected: $commit\n";
1460 my ($log_msg, @parents) = @_;
1461 assert_revision_unknown($log_msg->{revision});
1462 my $out_fh = IO::File->new_tmpfile or croak $!;
1464 map_tree_joins() if (@_branch_from && !%tree_map);
1466 # commit parents can be conditionally bound to a particular
1467 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1469 foreach my $p (@parents) {
1470 next unless defined $p;
1471 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1472 if ($1 == $log_msg->{revision}) {
1473 push @exec_parents, $2;
1476 push @exec_parents, $p if $p =~ /$sha1_short/o;
1481 defined $pid or croak $!;
1483 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
1485 chomp(my $tree = `git-write-tree`);
1487 if (exists $tree_map{$tree}) {
1488 my %seen_parent = map { $_ => 1 } @exec_parents;
1489 foreach (@{$tree_map{$tree}}) {
1490 # MAXPARENT is defined to 16 in commit-tree.c:
1491 if ($seen_parent{$_} || @exec_parents > 16) {
1494 push @exec_parents, $_;
1495 $seen_parent{$_} = 1;
1498 my $msg_fh = IO::File->new_tmpfile or croak $!;
1499 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1500 "$SVN_URL\@$log_msg->{revision}",
1501 " $SVN_UUID\n" or croak $!;
1502 $msg_fh->flush == 0 or croak $!;
1503 seek $msg_fh, 0, 0 or croak $!;
1505 set_commit_env($log_msg);
1507 my @exec = ('git-commit-tree',$tree);
1508 push @exec, '-p', $_ foreach @exec_parents;
1509 open STDIN, '<&', $msg_fh or croak $!;
1510 open STDOUT, '>&', $out_fh or croak $!;
1511 exec @exec or croak $!;
1516 $out_fh->flush == 0 or croak $!;
1517 seek $out_fh, 0, 0 or croak $!;
1518 chomp(my $commit = do { local $/; <$out_fh> });
1519 if ($commit !~ /^$sha1$/o) {
1520 croak "Failed to commit, invalid sha1: $commit\n";
1522 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1523 if (my $primary_parent = shift @exec_parents) {
1525 defined $pid or croak $!;
1529 exec 'git-rev-parse','--verify',
1530 "refs/remotes/$GIT_SVN^0" or croak $!;
1533 push @update_ref, $primary_parent unless $?;
1536 sys('git-update-ref',"svn/$GIT_SVN/revs/$log_msg->{revision}",$commit);
1537 print "r$log_msg->{revision} = $commit\n";
1538 if ($_repack && (--$_repack_nr == 0)) {
1539 $_repack_nr = $_repack;
1540 sys("git repack $_repack_flags");
1545 sub set_commit_env {
1547 my $author = $log_msg->{author};
1548 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1549 : ($author,"$author\@$SVN_UUID");
1550 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1551 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1552 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1555 sub apply_mod_line_blob {
1557 if ($m->{mode_b} =~ /^120/) {
1558 blob_to_symlink($m->{sha1_b}, $m->{file_b});
1560 blob_to_file($m->{sha1_b}, $m->{file_b});
1564 sub blob_to_symlink {
1565 my ($blob, $link) = @_;
1566 defined $link or croak "\$link not defined!\n";
1567 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
1568 if (-l $link || -f _) {
1569 unlink $link or croak $!;
1572 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1573 symlink $dest, $link or croak $!;
1577 my ($blob, $file) = @_;
1578 defined $file or croak "\$file not defined!\n";
1579 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
1580 if (-l $file || -f _) {
1581 unlink $file or croak $!;
1584 open my $blob_fh, '>', $file or croak "$!: $file\n";
1586 defined $pid or croak $!;
1589 open STDOUT, '>&', $blob_fh or croak $!;
1590 exec('git-cat-file','blob',$blob) or croak $!;
1595 close $blob_fh or croak $!;
1599 my $pid = open my $child, '-|';
1600 defined $pid or croak $!;
1602 exec(@_) or croak $!;
1604 my @ret = (<$child>);
1605 close $child or croak $?;
1606 die $? if $?; # just in case close didn't error out
1607 return wantarray ? @ret : join('',@ret);
1610 sub svn_compat_check {
1611 my @co_help = safe_qx(qw(svn co -h));
1612 unless (grep /ignore-externals/,@co_help) {
1613 print STDERR "W: Installed svn version does not support ",
1614 "--ignore-externals\n";
1615 $_no_ignore_ext = 1;
1617 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1618 $_svn_co_url_revs = 1;
1620 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
1621 $_svn_pg_peg_revs = 1;
1624 # I really, really hope nobody hits this...
1625 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
1627 W: The installed svn version does not support the --stop-on-copy flag in
1629 Lets hope the directory you're tracking is not a branch or tag
1630 and was never moved within the repository...
1636 # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
1637 # (and they won't honor URL@<rev> without -r<rev>, too!)
1638 sub svn_cmd_checkout {
1639 my ($url, $rev, $dir) = @_;
1640 my @cmd = ('svn','co', "-r$rev");
1641 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1642 $url .= "\@$rev" if $_svn_co_url_revs;
1643 sys(@cmd, $url, $dir);
1646 sub check_upgrade_needed {
1648 my $pid = open my $child, '-|';
1649 defined $pid or croak $!;
1652 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
1654 my @ret = (<$child>);
1655 close $child or croak $?;
1656 die $? if $?; # just in case close didn't error out
1657 return wantarray ? @ret : join('',@ret);
1660 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
1662 print STDERR "Please run: $0 rebuild --upgrade\n";
1667 # fills %tree_map with a reverse mapping of trees to commits. Useful
1668 # for finding parents to commit on.
1669 sub map_tree_joins {
1671 foreach my $br (@_branch_from) {
1672 my $pid = open my $pipe, '-|';
1673 defined $pid or croak $!;
1675 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
1679 if (/^commit ($sha1)$/o) {
1682 # if we've seen a commit,
1683 # we've seen its parents
1684 last if $seen{$commit};
1685 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1686 unless (defined $tree) {
1687 die "Failed to parse commit $commit\n";
1689 push @{$tree_map{$tree}}, $commit;
1693 close $pipe; # we could be breaking the pipe early
1698 if (@_branch_from) {
1699 print STDERR '--branch|-b parameters are ignored when ',
1700 "--branch-all-refs|-B is passed\n";
1703 # don't worry about rev-list on non-commit objects/tags,
1704 # it shouldn't blow up if a ref is a blob or tree...
1705 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
1708 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1710 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1711 while (<$authors>) {
1713 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1714 my ($user, $name, $email) = ($1, $2, $3);
1715 $users{$user} = [$name, $email];
1717 close $authors or croak $!;
1721 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1722 while (<$authors>) {
1724 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1725 my ($user, $name, $email) = ($1, $2, $3);
1726 $rusers{"$name <$email>"} = $user;
1728 close $authors or croak $!;
1731 sub svn_propget_base {
1733 $f .= '@BASE' if $_svn_pg_peg_revs;
1734 return safe_qx(qw/svn propget/, $p, $f);
1739 foreach (`git-rev-parse --symbolic --all`) {
1740 next unless s#^refs/remotes/##;
1742 next unless -f "$GIT_DIR/svn/$_/info/url";
1747 sub migration_check {
1748 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
1749 print "Upgrading repository...\n";
1750 unless (-d "$GIT_DIR/svn") {
1751 mkdir "$GIT_DIR/svn" or croak $!;
1753 print "Data from a previous version of git-svn exists, but\n\t",
1754 "$GIT_SVN_DIR\n\t(required for this version ",
1755 "($VERSION) of git-svn) does not.\n";
1757 foreach my $x (`git-rev-parse --symbolic --all`) {
1758 next unless $x =~ s#^refs/remotes/##;
1760 next unless -f "$GIT_DIR/$x/info/url";
1761 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
1763 my $dn = dirname("$GIT_DIR/svn/$x");
1764 mkpath([$dn]) unless -d $dn;
1765 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
1766 my ($url, $path) = repo_path_split($u);
1767 s_to_file($url, "$GIT_DIR/svn/$x/info/repo_url");
1768 s_to_file($path, "$GIT_DIR/svn/$x/info/repo_path");
1770 print "Done upgrading.\n";
1773 sub find_rev_before {
1774 my ($r, $git_svn_id) = @_;
1775 my @revs = map { basename $_ } <$GIT_DIR/svn/$git_svn_id/revs/*>;
1776 foreach my $r0 (sort { $b <=> $a } @revs) {
1778 return ($r0, file_to_s("$GIT_DIR/svn/$git_svn_id/revs/$r0"));
1780 return (undef, undef);
1784 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1785 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1786 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1788 $REV_DIR = "$GIT_SVN_DIR/revs";
1789 $SVN_WC = "$GIT_SVN_DIR/tree";
1792 # convert GetOpt::Long specs for use by git-repo-config
1793 sub read_repo_config {
1794 return unless -d $GIT_DIR;
1796 foreach my $o (keys %$opts) {
1797 my $v = $opts->{$o};
1798 my ($key) = ($o =~ /^([a-z\-]+)/);
1800 my $arg = 'git-repo-config';
1801 $arg .= ' --int' if ($o =~ /[:=]i$/);
1802 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1803 if (ref $v eq 'ARRAY') {
1804 chomp(my @tmp = `$arg --get-all svn.$key`);
1807 chomp(my $tmp = `$arg --get svn.$key`);
1808 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
1815 sub set_default_vals {
1816 if (defined $_repack) {
1817 $_repack = 1000 if ($_repack <= 0);
1818 $_repack_nr = $_repack;
1819 $_repack_flags ||= '';
1824 my $gr_file = shift;
1825 my ($grafts, $comments) = ({}, {});
1826 if (open my $fh, '<', $gr_file) {
1829 if (/^($sha1)\s+/) {
1832 @{$comments->{$c}} = @tmp;
1835 foreach my $p (split /\s+/, $_) {
1836 $grafts->{$c}->{$p} = 1;
1842 close $fh or croak $!;
1843 @{$comments->{'END'}} = @tmp if @tmp;
1845 return ($grafts, $comments);
1849 my ($grafts, $comments, $gr_file) = @_;
1851 open my $fh, '>', $gr_file or croak $!;
1852 foreach my $c (sort keys %$grafts) {
1853 if ($comments->{$c}) {
1854 print $fh $_ foreach @{$comments->{$c}};
1856 my $p = $grafts->{$c};
1857 delete $p->{$c}; # commits are not self-reproducing...
1858 my $pid = open my $ch, '-|';
1859 defined $pid or croak $!;
1861 exec(qw/git-cat-file commit/, $c) or croak $!;
1864 if (/^parent ([a-f\d]{40})/) {
1870 close $ch; # breaking the pipe
1871 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1873 if ($comments->{'END'}) {
1874 print $fh $_ foreach @{$comments->{'END'}};
1876 close $fh or croak $!;
1879 sub read_url_paths {
1881 git_svn_each(sub { my $x = shift;
1882 my $u = file_to_s("$GIT_DIR/svn/$x/info/repo_url");
1883 my $p = file_to_s("$GIT_DIR/svn/$x/info/repo_path");
1884 # we hate trailing slashes
1885 if ($u =~ s#(?:^\/+|\/+$)##g) {
1886 s_to_file($u,"$GIT_DIR/svn/$x/info/repo_url");
1888 if ($p =~ s#(?:^\/+|\/+$)##g) {
1889 s_to_file($p,"$GIT_DIR/svn/$x/info/repo_path");
1891 $l_map->{$u}->{$p} = $x;
1896 sub extract_metadata {
1898 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
1900 if (!$rev || !$uuid || !$url) {
1901 # some of the original repositories I made had
1902 # indentifiers like this:
1903 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1905 return ($url, $rev, $uuid);
1908 sub tz_to_s_offset {
1911 return ($1 * 60) + ($tz * 3600);
1914 sub setup_pager { # translated to Perl from pager.c
1915 return unless (-t *STDOUT);
1916 my $pager = $ENV{PAGER};
1917 if (!defined $pager) {
1919 } elsif (length $pager == 0 || $pager eq 'cat') {
1922 pipe my $rfd, my $wfd or return;
1923 defined(my $pid = fork) or croak $!;
1925 open STDOUT, '>&', $wfd or croak $!;
1928 open STDIN, '<&', $rfd or croak $!;
1929 $ENV{LESS} ||= '-S';
1930 exec $pager or croak "Can't run pager: $!\n";;
1933 sub get_author_info {
1934 my ($dest, $author, $t, $tz) = @_;
1935 $author =~ s/(?:^\s*|\s*$)//g;
1938 $_a = $rusers{$author} || undef;
1941 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1946 # Date::Parse isn't in the standard Perl distro :(
1947 if ($tz =~ s/^\+//) {
1948 $t += tz_to_s_offset($tz);
1949 } elsif ($tz =~ s/^\-//) {
1950 $t -= tz_to_s_offset($tz);
1952 $dest->{t_utc} = $t;
1955 sub process_commit {
1956 my ($c, $r_min, $r_max, $defer) = @_;
1957 if (defined $r_min && defined $r_max) {
1958 if ($r_min == $c->{r} && $r_min == $r_max) {
1962 return 1 if $r_min == $r_max;
1963 if ($r_min < $r_max) {
1964 # we need to reverse the print order
1965 return 0 if (defined $_limit && --$_limit < 0);
1969 if ($r_min != $r_max) {
1970 return 1 if ($r_min < $c->{r});
1971 return 1 if ($r_max > $c->{r});
1974 return 0 if (defined $_limit && --$_limit < 0);
1983 if (my $l = $c->{l}) {
1984 while ($l->[0] =~ /^\s*$/) { shift @$l }
1987 $_l_fmt ||= 'A' . length($c->{r});
1988 print 'r',pack($_l_fmt, $c->{r}),' | ';
1989 print "$c->{c} | " if $_show_commit;
1992 show_commit_normal($c);
1996 sub show_commit_normal {
1998 print '-' x72, "\nr$c->{r} | ";
1999 print "$c->{c} | " if $_show_commit;
2000 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2001 localtime($c->{t_utc})), ' | ';
2004 if (my $l = $c->{l}) {
2005 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2008 $nr_line = scalar @$l;
2010 print "1 line\n\n\n";
2012 if ($nr_line == 1) {
2013 $nr_line = '1 line';
2015 $nr_line .= ' lines';
2017 print $nr_line, "\n\n";
2018 print $_ foreach @$l;
2024 foreach my $x (qw/raw diff/) {
2027 print $_ foreach @{$c->{$x}}
2036 $svn_log hashref (as returned by svn_log_raw)
2038 fh => file handle of the log file,
2039 state => state of the log file parser (sep/msg/rev/msg_start...)
2042 $log_msg hashref as returned by next_log_entry($svn_log)
2044 msg => 'whitespace-formatted log entry
2045 ', # trailing newline is preserved
2046 revision => '8', # integer
2047 date => '2004-02-24T17:01:44.108345Z', # commit date
2048 author => 'committer name'
2052 @mods = array of diff-index line hashes, each element represents one line
2053 of diff-index output
2055 diff-index line ($m hash)
2057 mode_a => first column of diff-index output, no leading ':',
2058 mode_b => second column of diff-index output,
2059 sha1_b => sha1sum of the final blob,
2060 chg => change type [MCRADT],
2061 file_a => original file name of a file (iff chg is 'C' or 'R')
2062 file_b => new/current file name of a file (any chg)