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 $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
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:
22 $| = 1; # unbuffer STDOUT
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
27 # We don't use any modules not in the standard Perl distribution:
30 use File::Basename qw/dirname basename/;
31 use File::Path qw/mkpath/;
32 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
34 use File::Copy qw/copy/;
35 use POSIX qw/strftime/;
38 memoize('revisions_eq');
39 memoize('cmt_metadata');
40 memoize('get_commit_time');
42 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
46 ! Please consider installing the SVN Perl libraries (version 1.1.0 or
47 ! newer). You will generally get better performance and fewer bugs,
49 ! 1) have a case-insensitive filesystem
50 ! 2) replace symlinks with files (and vice-versa) in commits
55 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB};
57 nag_lib() unless $_use_lib;
59 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
60 my $sha1 = qr/[a-f\d]{40}/;
61 my $sha1_short = qr/[a-f\d]{4,40}/;
62 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
63 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
64 $_repack, $_repack_nr, $_repack_flags, $_q,
65 $_message, $_file, $_follow_parent, $_no_metadata,
66 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
67 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
68 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
69 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive);
70 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
71 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
72 my @repo_path_split_cache;
74 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
75 'branch|b=s' => \@_branch_from,
76 'follow-parent|follow' => \$_follow_parent,
77 'branch-all-refs|B' => \$_branch_all_refs,
78 'authors-file|A=s' => \$_authors,
79 'repack:i' => \$_repack,
80 'no-metadata' => \$_no_metadata,
82 'ignore-nodate' => \$_ignore_nodate,
83 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
85 my ($_trunk, $_tags, $_branches);
86 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
87 'tags|t=s' => \$_tags,
88 'branches|b=s' => \$_branches );
89 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
90 my %cmt_opts = ( 'edit|e' => \$_edit,
92 'find-copies-harder' => \$_find_copies_harder,
94 'copy-similarity|C=i'=> \$_cp_similarity
98 fetch => [ \&fetch, "Download new revisions from SVN",
99 { 'revision|r=s' => \$_revision, %fc_opts } ],
100 init => [ \&init, "Initialize a repo for tracking" .
101 " (requires URL argument)",
103 commit => [ \&commit, "Commit git revisions to SVN",
104 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
105 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
106 { 'revision|r=i' => \$_revision } ],
107 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
108 { 'no-ignore-externals' => \$_no_ignore_ext,
109 'copy-remote|remote=s' => \$_cp_remote,
110 'upgrade' => \$_upgrade } ],
111 'graft-branches' => [ \&graft_branches,
112 'Detect merges/branches from already imported history',
113 { 'merge-rx|m' => \@_opt_m,
114 'branch|b=s' => \@_branch_from,
115 'branch-all-refs|B' => \$_branch_all_refs,
116 'no-default-regex' => \$_no_default_regex,
117 'no-graft-copy' => \$_no_graft_copy } ],
118 'multi-init' => [ \&multi_init,
119 'Initialize multiple trees (like git-svnimport)',
120 { %multi_opts, %fc_opts } ],
121 'multi-fetch' => [ \&multi_fetch,
122 'Fetch multiple trees (like git-svnimport)',
124 'log' => [ \&show_log, 'Show commit logs',
125 { 'limit=i' => \$_limit,
126 'revision|r=s' => \$_revision,
127 'verbose|v' => \$_verbose,
128 'incremental' => \$_incremental,
129 'oneline' => \$_oneline,
130 'show-commit' => \$_show_commit,
131 'non-recursive' => \$_non_recursive,
132 'authors-file|A=s' => \$_authors,
134 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
135 { 'message|m=s' => \$_message,
136 'file|F=s' => \$_file,
138 dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
139 { 'merge|m|M' => \$_merge,
140 'strategy|s=s' => \$_strategy,
141 'dry-run|n' => \$_dry_run,
146 for (my $i = 0; $i < @ARGV; $i++) {
147 if (defined $cmd{$ARGV[$i]}) {
154 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
156 read_repo_config(\%opts);
157 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
158 'version|V' => \$_version,
159 'id|i=s' => \$GIT_SVN);
160 exit 1 if (!$rv && $cmd ne 'log');
164 version() if $_version;
165 usage(1) unless defined $cmd;
167 load_authors() if $_authors;
168 load_all_refs() if $_branch_all_refs;
169 svn_compat_check() unless $_use_lib;
170 migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
171 $cmd{$cmd}->[0]->(@ARGV);
174 ####################### primary functions ######################
176 my $exit = shift || 0;
177 my $fd = $exit ? \*STDERR : \*STDOUT;
179 git-svn - bidirectional operations between a single Subversion tree and git
180 Usage: $0 <command> [options] [arguments]\n
182 print $fd "Available commands:\n" unless $cmd;
184 foreach (sort keys %cmd) {
185 next if $cmd && $cmd ne $_;
186 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
187 foreach (keys %{$cmd{$_}->[2]}) {
188 # prints out arguments as they should be passed:
189 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
190 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
192 split /\|/,$_)," $x\n";
196 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
197 arbitrary identifier if you're tracking multiple SVN branches/repositories in
198 one git repository and want to keep them separate. See git-svn(1) for more
205 print "git-svn version $VERSION\n";
210 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
213 $SVN_URL = shift or undef;
216 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
218 check_upgrade_needed();
221 my $pid = open(my $rev_list,'-|');
222 defined $pid or croak $!;
224 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
227 while (<$rev_list>) {
230 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
231 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
232 next if (!@commit); # skip merges
233 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
234 if (!$rev || !$uuid) {
235 croak "Unable to extract revision or UUID from ",
236 "$c, $commit[$#commit]\n";
239 # if we merged or otherwise started elsewhere, this is
240 # how we break out of it
241 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
242 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
244 unless (defined $latest) {
245 if (!$SVN_URL && !$url) {
246 croak "SVN repository location required: $url\n";
253 revdb_set($REVDB, $rev, $c);
254 print "r$rev = $c\n";
255 $newest_rev = $rev if ($rev > $newest_rev);
257 close $rev_list or croak $?;
259 goto out if $_use_lib;
260 if (!chdir $SVN_WC) {
261 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
262 chdir $SVN_WC or croak $!;
266 defined $pid or croak $!;
268 my @svn_up = qw(svn up);
269 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
270 sys(@svn_up,"-r$newest_rev");
271 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
273 exec('git-write-tree') or croak $!;
280 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
281 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
287 my $url = shift or die "SVN repository location required " .
288 "as a command-line argument\n";
289 $url =~ s!/+$!!; # strip trailing slash
291 if (my $repo_path = shift) {
292 unless (-d $repo_path) {
293 mkpath([$repo_path]);
295 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
300 unless (-d $GIT_DIR) {
301 my @init_db = ('git-init-db');
302 push @init_db, "--template=$_template" if defined $_template;
303 push @init_db, "--shared" if defined $_shared;
310 check_upgrade_needed();
311 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
312 my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
313 if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
314 refs/heads/master^0))) {
315 sys(qw(git-update-ref refs/heads/master),$ret->{commit});
322 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
323 unless ($_revision) {
324 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
326 push @log_args, "-r$_revision";
327 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
329 my $svn_log = svn_log_raw(@log_args);
331 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
332 # don't need last_revision from grab_base_rev() because
333 # user could've specified a different revision to skip (they
334 # didn't want to import certain revisions into git for whatever
335 # reason, so trust $base->{revision} instead.
336 my (undef, $last_commit) = svn_grab_base_rev();
337 unless (-d $SVN_WC) {
338 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
339 chdir $SVN_WC or croak $!;
341 $last_commit = git_commit($base, @parents);
342 assert_tree($last_commit);
344 chdir $SVN_WC or croak $!;
346 # looks like a user manually cp'd and svn switch'ed
347 unless ($last_commit) {
348 sys(qw/svn revert -R ./);
349 assert_svn_wc_clean($base->{revision});
350 $last_commit = git_commit($base, @parents);
351 assert_tree($last_commit);
354 my @svn_up = qw(svn up);
355 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
357 while (my $log_msg = next_log_entry($svn_log)) {
358 if ($last->{revision} >= $log_msg->{revision}) {
359 croak "Out of order: last >= current: ",
360 "$last->{revision} >= $log_msg->{revision}\n";
362 # Revert is needed for cases like:
363 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
364 # I can't seem to reproduce something like that on a test...
365 sys(qw/svn revert -R ./);
366 assert_svn_wc_clean($last->{revision});
367 sys(@svn_up,"-r$log_msg->{revision}");
368 $last_commit = git_commit($log_msg, $last_commit, @parents);
371 close $svn_log->{fh};
372 $last->{commit} = $last_commit;
378 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
380 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
381 $SVN_LOG ||= libsvn_connect($repo);
382 $SVN ||= libsvn_connect($repo);
383 my ($last_rev, $last_commit) = svn_grab_base_rev();
384 my ($base, $head) = libsvn_parse_revision($last_rev);
386 return { revision => $last_rev, commit => $last_commit }
388 my $index = set_index($GIT_SVN_INDEX);
390 # limit ourselves and also fork() since get_log won't release memory
391 # after processing a revision and SVN stuff seems to leak
393 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
395 if (defined $last_commit) {
396 unless (-e $GIT_SVN_INDEX) {
397 sys(qw/git-read-tree/, $last_commit);
399 chomp (my $x = `git-write-tree`);
400 my ($y) = (`git-cat-file commit $last_commit`
401 =~ /^tree ($sha1)/m);
403 unlink $GIT_SVN_INDEX or croak $!;
404 sys(qw/git-read-tree/, $last_commit);
406 chomp ($x = `git-write-tree`);
408 print STDERR "trees ($last_commit) $y != $x\n",
409 "Something is seriously wrong...\n";
413 # fork, because using SVN::Pool with get_log() still doesn't
414 # seem to help enough to keep memory usage down.
415 defined(my $pid = fork) or croak $!;
417 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
419 # Yes I'm perfectly aware that the fourth argument
420 # below is the limit revisions number. Unfortunately
421 # performance sucks with it enabled, so it's much
422 # faster to fetch revision ranges instead of relying
424 libsvn_get_log($SVN_LOG, '/'.$SVN_PATH,
429 $log_msg = libsvn_fetch(
431 $last_commit = git_commit(
436 $log_msg = libsvn_new_tree(@_);
437 $last_commit = git_commit(
445 ($last_rev, $last_commit) = svn_grab_base_rev();
446 last if ($max >= $head);
449 $max = $head if ($max > $head);
451 restore_index($index);
452 return { revision => $last_rev, commit => $last_commit };
457 check_upgrade_needed();
458 if ($_stdin || !@commits) {
459 print "Reading from stdin...\n";
462 if (/\b($sha1_short)\b/o) {
463 unshift @commits, $1;
468 foreach my $c (@commits) {
469 chomp(my @tmp = safe_qx('git-rev-parse',$c));
470 if (scalar @tmp == 1) {
472 } elsif (scalar @tmp > 1) {
473 push @revs, reverse (safe_qx('git-rev-list',@tmp));
475 die "Failed to rev-parse $c\n";
479 $_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
480 print "Done committing ",scalar @revs," revisions to SVN\n";
486 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
487 my $info = svn_info('.');
488 my $fetched = fetch();
489 if ($info->{Revision} != $fetched->{revision}) {
490 print STDERR "There are new revisions that were fetched ",
491 "and need to be merged (or acknowledged) ",
492 "before committing.\n";
495 $info = svn_info('.');
498 foreach my $c (@revs) {
499 my $mods = svn_checkout_tree($last, $c);
500 if (scalar @$mods == 0) {
501 print "Skipping, no changes detected\n";
504 $last = svn_commit_tree($last, $c);
510 my ($r_last, $cmt_last) = svn_grab_base_rev();
511 defined $r_last or die "Must have an existing revision to commit\n";
512 my $fetched = fetch();
513 if ($r_last != $fetched->{revision}) {
514 print STDERR "There are new revisions that were fetched ",
515 "and need to be merged (or acknowledged) ",
516 "before committing.\n",
517 "last rev: $r_last\n",
518 " current: $fetched->{revision}\n";
522 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
523 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
526 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
527 set_svn_commit_env();
528 foreach my $c (@revs) {
529 my $log_msg = get_commit_message($c, $commit_msg);
531 # fork for each commit because there's a memory leak I
532 # can't track down... (it's probably in the SVN code)
533 defined(my $pid = open my $fh, '-|') or croak $!;
535 $SVN_LOG = libsvn_connect($repo);
536 $SVN = libsvn_connect($repo);
537 my $ed = SVN::Git::Editor->new(
541 svn_path => $SVN_PATH
543 $SVN->get_commit_editor(
554 my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
556 print "No changes\nr$r_last = $cmt_last\n";
563 my ($r_new, $cmt_new, $no);
567 if (/^r(\d+) = ($sha1)$/o) {
568 ($r_new, $cmt_new) = ($1, $2);
569 } elsif ($_ eq 'No changes') {
573 close $fh or croak $?;
574 if (! defined $r_new && ! defined $cmt_new) {
576 die "Failed to parse revision information\n";
579 ($r_last, $cmt_last) = ($r_new, $cmt_new);
587 my $gs = "refs/remotes/$GIT_SVN";
588 chomp(my @refs = safe_qx(qw/git-rev-list --no-merges/, "$gs..HEAD"));
589 foreach my $d (reverse @refs) {
591 print "diff-tree $d~1 $d\n";
593 commit_diff("$d~1", $d);
598 my @diff = safe_qx(qw/git-diff-tree HEAD/, $gs);
601 @finish = qw/rebase/;
602 push @finish, qw/--merge/ if $_merge;
603 push @finish, "--strategy=$_strategy" if $_strategy;
604 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
606 print "No changes between current HEAD and $gs\n",
607 "Hard resetting to the latest $gs\n";
608 @finish = qw/reset --hard/;
610 sys('git', @finish, $gs);
614 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
615 $_use_lib ? show_ignore_lib() : show_ignore_cmd();
618 sub show_ignore_cmd {
619 require File::Find or die $!;
620 if (defined $_revision) {
621 die "-r/--revision option doesn't work unless the Perl SVN ",
622 "libraries are used\n";
624 chdir $SVN_WC or croak $!;
626 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
628 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
629 }}, no_chdir=>1},'.');
632 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
634 foreach my $i (sort keys %ign) {
635 print "\n# ",$i,"\n";
636 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
640 sub show_ignore_lib {
642 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
643 $SVN ||= libsvn_connect($repo);
644 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
645 libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
649 my $gr_file = "$GIT_DIR/info/grafts";
650 my ($grafts, $comments) = read_grafts($gr_file);
654 # temporarily disable our grafts file to make this idempotent
655 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
656 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
659 my $l_map = read_url_paths();
660 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
661 unless ($_no_default_regex) {
662 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
663 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
664 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
666 foreach my $u (keys %$l_map) {
668 foreach my $p (keys %{$l_map->{$u}}) {
669 graft_merge_msg($grafts,$l_map,$u,$p,@re);
672 unless ($_no_graft_copy) {
674 graft_file_copy_lib($grafts,$l_map,$u);
676 graft_file_copy_cmd($grafts,$l_map,$u);
680 graft_tree_joins($grafts);
682 write_grafts($grafts, $comments, $gr_file);
683 unlink "$gr_file~$gr_sha1" if $gr_sha1;
690 $url =~ s#/+$## if $url;
691 if ($_trunk !~ m#^[a-z\+]+://#) {
692 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
694 print STDERR "E: '$_trunk' is not a complete URL ",
695 "and a separate URL is not specified\n";
698 $_trunk = $url . $_trunk;
701 if ($GIT_SVN eq 'git-svn') {
703 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
706 unless (-d $GIT_SVN_DIR) {
707 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
709 sys('git-repo-config', 'svn.trunk', $_trunk);
711 complete_url_ls_init($url, $_branches, '--branches/-b', '');
712 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
716 # try to do trunk first, since branches/tags
717 # may be descended from it.
718 if (-e "$GIT_DIR/svn/trunk/info/url") {
719 fetch_child_id('trunk', @_);
721 rec_fetch('', "$GIT_DIR/svn", @_);
727 my $r_last = -1; # prevent dupes
728 rload_authors() if $_authors;
734 if (defined $_revision) {
735 if ($_revision =~ /^(\d+):(\d+)$/) {
736 ($r_min, $r_max) = ($1, $2);
737 } elsif ($_revision =~ /^\d+$/) {
738 $r_min = $r_max = $_revision;
740 print STDERR "-r$_revision is not supported, use ",
741 "standard \'git log\' arguments instead\n";
746 my $pid = open(my $log,'-|');
747 defined $pid or croak $!;
749 exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
755 if (/^commit ($sha1_short)/o) {
757 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
759 process_commit($c, $r_min, $r_max, \@k) or
764 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
765 get_author_info($c, $1, $2, $3);
766 } elsif (/^(?:tree|parent|committer) /) {
768 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
769 push @{$c->{raw}}, $_;
770 } elsif (/^[ACRMDT]\t/) {
771 # we could add $SVN_PATH here, but that requires
772 # remote access at the moment (repo_path_split)...
773 s#^([ACRMDT])\t# $1 #;
774 push @{$c->{changed}}, $_;
777 push @{$c->{diff}}, $_;
779 push @{$c->{diff}}, $_;
780 } elsif (/^ (git-svn-id:.+)$/) {
781 ($c->{url}, $c->{r}, undef) = extract_metadata($1);
786 if ($c && defined $c->{r} && $c->{r} != $r_last) {
788 process_commit($c, $r_min, $r_max, \@k);
794 process_commit($_, $r_min, $r_max) foreach reverse @k;
798 print '-' x72,"\n" unless $_incremental || $_oneline;
801 sub commit_diff_usage {
802 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
808 print STDERR "commit-diff must be used with SVN libraries\n";
811 my $ta = shift or commit_diff_usage();
812 my $tb = shift or commit_diff_usage();
813 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
814 print STDERR "Needed URL or usable git-svn id command-line\n";
817 if (defined $_message && defined $_file) {
818 print STDERR "Both --message/-m and --file/-F specified ",
819 "for the commit message.\n",
820 "I have no idea what you mean\n";
823 if (defined $_file) {
824 $_message = file_to_s($_file);
826 $_message ||= get_commit_message($tb,
827 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
830 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
831 $SVN_LOG ||= libsvn_connect($repo);
832 $SVN ||= libsvn_connect($repo);
833 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
834 my $ed = SVN::Git::Editor->new({ r => $SVN->get_latest_revnum,
835 ra => $SVN, c => $tb,
836 svn_path => $SVN_PATH
838 $SVN->get_commit_editor($_message,
839 sub {print "Committed $_[0]\n"},@lock)
841 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
843 print "No changes\n$ta == $tb\n";
848 $_message = $_file = undef;
851 ########################### utility functions #########################
855 return 1 if defined $c->{r};
856 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
857 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
858 my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
859 shift @msg while ($msg[0] ne "\n");
861 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
863 (undef, $c->{r}, undef) = extract_metadata(
864 (grep(/^git-svn-id: /, @msg))[-1]);
866 return defined $c->{r};
869 sub git_svn_log_cmd {
870 my ($r_min, $r_max) = @_;
871 my @cmd = (qw/git-log --abbrev-commit --pretty=raw
872 --default/, "refs/remotes/$GIT_SVN");
873 push @cmd, '-r' unless $_non_recursive;
874 push @cmd, qw/--raw --name-status/ if $_verbose;
875 return @cmd unless defined $r_max;
876 if ($r_max == $r_min) {
877 push @cmd, '--max-count=1';
878 if (my $c = revdb_get($REVDB, $r_max)) {
883 $c_max = revdb_get($REVDB, $r_max);
884 $c_min = revdb_get($REVDB, $r_min);
885 if (defined $c_min && defined $c_max) {
886 if ($r_max > $r_max) {
887 push @cmd, "$c_min..$c_max";
889 push @cmd, "$c_max..$c_min";
891 } elsif ($r_max > $r_min) {
902 print "Fetching $id\n";
903 my $ref = "$GIT_DIR/refs/remotes/$id";
904 defined(my $pid = open my $fh, '-|') or croak $!;
907 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
914 check_repack() if (/^r\d+ = $sha1/);
916 close $fh or croak $?;
920 my ($pfx, $p, @args) = @_;
922 foreach (sort <$p/*>) {
923 if (-r "$_/info/url") {
924 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
925 my $id = $pfx . basename $_;
926 next if $id eq 'trunk';
927 fetch_child_id($id, @args);
934 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
939 sub complete_url_ls_init {
940 my ($url, $var, $switch, $pfx) = @_;
942 print STDERR "W: $switch not specified\n";
946 if ($var !~ m#^[a-z\+]+://#) {
947 $var = '/' . $var if ($var !~ m#^/#);
949 print STDERR "E: '$var' is not a complete URL ",
950 "and a separate URL is not specified\n";
955 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
956 : safe_qx(qw/svn ls --non-interactive/, $var));
958 defined(my $pid = fork) or croak $!;
960 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
962 if ($u !~ m!\Q$var\E/(.+)$!) {
963 print STDERR "W: Unrecognized URL: $u\n";
964 die "This should never happen\n";
966 # don't try to init already existing refs
968 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
970 unless (-d $GIT_SVN_DIR) {
971 print "init $u => $id\n";
979 my ($n) = ($switch =~ /^--(\w+)/);
980 sys('git-repo-config', "svn.$n", $var);
987 my @tmp = split m#/#, $_;
989 while (my $x = shift @tmp) {
995 foreach (sort {length $b <=> length $a} keys %common) {
996 if ($common{$_} == @$paths) {
1003 # grafts set here are 'stronger' in that they're based on actual tree
1004 # matches, and won't be deleted from merge-base checking in write_grafts()
1005 sub graft_tree_joins {
1007 map_tree_joins() if (@_branch_from && !%tree_map);
1008 return unless %tree_map;
1012 defined(my $pid = open my $fh, '-|') or croak $!;
1014 exec qw/git-rev-list --pretty=raw/,
1015 "refs/remotes/$i" or croak $!;
1018 next unless /^commit ($sha1)$/o;
1020 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1021 next unless $tree_map{$t};
1026 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1028 my ($s, $tz) = ($1, $2);
1029 if ($tz =~ s/^\+//) {
1030 $s += tz_to_s_offset($tz);
1031 } elsif ($tz =~ s/^\-//) {
1032 $s -= tz_to_s_offset($tz);
1035 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
1037 foreach my $p (@{$tree_map{$t}}) {
1040 safe_qx('git-merge-base', $c, $p)
1042 next unless ($@ || $?);
1044 # see if SVN says it's a relative
1045 my ($url_b, $r_b, $uuid_b) =
1047 next if (defined $url_b &&
1049 ($url_a eq $url_b) &&
1050 ($uuid_a eq $uuid_b));
1051 if ($uuid_a eq $uuid_b) {
1053 $grafts->{$c}->{$p} = 2;
1055 } elsif ($r_b > $r_a) {
1056 $grafts->{$p}->{$c} = 2;
1061 my $ct = get_commit_time($p);
1063 $grafts->{$c}->{$p} = 2;
1064 } elsif ($ct > $s) {
1065 $grafts->{$p}->{$c} = 2;
1067 # what should we do when $ct == $s ?
1070 close $fh or croak $?;
1074 # this isn't funky-filename safe, but good enough for now...
1075 sub graft_file_copy_cmd {
1076 my ($grafts, $l_map, $u) = @_;
1077 my $paths = $l_map->{$u};
1078 my $pfx = common_prefix([keys %$paths]);
1079 $SVN_URL ||= $u.$pfx;
1080 my $pid = open my $fh, '-|';
1081 defined $pid or croak $!;
1083 my @exec = qw/svn log -v/;
1084 push @exec, "-r$_revision" if defined $_revision;
1085 exec @exec, $u.$pfx or croak $!;
1087 my ($r, $mp) = (undef, undef);
1092 } elsif (/^r(\d+) \| /) {
1093 $r = $1 unless defined $r;
1094 } elsif (/^Changed paths:/) {
1096 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1097 my ($p1, $p0, $r0) = ($1, $2, $3);
1098 my $c = find_graft_path_commit($paths, $p1, $r);
1100 find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
1105 sub graft_file_copy_lib {
1106 my ($grafts, $l_map, $u) = @_;
1107 my $tree_paths = $l_map->{$u};
1108 my $pfx = common_prefix([keys %$tree_paths]);
1109 my ($repo, $path) = repo_path_split($u.$pfx);
1110 $SVN_LOG ||= libsvn_connect($repo);
1111 $SVN ||= libsvn_connect($repo);
1113 my ($base, $head) = libsvn_parse_revision();
1115 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
1116 my $eh = $SVN::Error::handler;
1117 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
1119 my $pool = SVN::Pool->new;
1120 libsvn_get_log($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1122 libsvn_graft_file_copies($grafts, $tree_paths,
1126 last if ($max >= $head);
1129 $max = $head if ($max > $head);
1131 $SVN::Error::handler = $eh;
1134 sub process_merge_msg_matches {
1135 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1136 my (@strong, @weak);
1137 foreach (@matches) {
1138 # merging with ourselves is not interesting
1140 if ($l_map->{$u}->{$_}) {
1146 foreach my $w (@weak) {
1148 # no exact match, use branch name as regexp.
1149 my $re = qr/\Q$w\E/i;
1150 foreach (keys %{$l_map->{$u}}) {
1152 push @strong, $l_map->{$u}->{$_};
1159 foreach (keys %{$l_map->{$u}}) {
1161 push @strong, $l_map->{$u}->{$_};
1166 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1167 \s(?:[a-f\d\-]+)$/xsm);
1168 unless (defined $rev) {
1169 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1170 \@(?:[a-f\d\-]+)/xsm);
1171 return unless defined $rev;
1173 foreach my $m (@strong) {
1174 my ($r0, $s0) = find_rev_before($rev, $m, 1);
1175 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1179 sub graft_merge_msg {
1180 my ($grafts, $l_map, $u, $p, @re) = @_;
1182 my $x = $l_map->{$u}->{$p};
1183 my $rl = rev_list_raw($x);
1184 while (my $c = next_rev_list_entry($rl)) {
1185 foreach my $re (@re) {
1186 my (@br) = ($c->{m} =~ /$re/g);
1188 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1194 return if $SVN_UUID;
1196 my $pool = SVN::Pool->new;
1197 $SVN_UUID = $SVN->get_uuid($pool);
1200 my $info = shift || svn_info('.');
1201 $SVN_UUID = $info->{'Repository UUID'} or
1202 croak "Repository UUID unreadable\n";
1208 defined $pid or croak $!;
1210 open my $null, '>', '/dev/null' or croak $!;
1211 open STDERR, '>&', $null or croak $!;
1212 open STDOUT, '>&', $null or croak $!;
1213 exec @_ or croak $!;
1219 sub repo_path_split {
1220 my $full_url = shift;
1221 $full_url =~ s#/+$##;
1223 foreach (@repo_path_split_cache) {
1224 if ($full_url =~ s#$_##) {
1226 $full_url =~ s#^/+##;
1227 return ($u, $full_url);
1232 my $tmp = libsvn_connect($full_url);
1233 my $url = $tmp->get_repos_root;
1234 $full_url =~ s#^\Q$url\E/*##;
1235 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1236 return ($url, $full_url);
1238 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1240 my @paths = split(m#/+#, $path);
1241 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
1242 my $n = shift @paths || last;
1245 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1246 $path = join('/',@paths);
1247 return ($url, $path);
1252 defined $SVN_URL or croak "SVN repository location required\n";
1253 unless (-d $GIT_DIR) {
1254 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1256 mkpath([$GIT_SVN_DIR]);
1257 mkpath(["$GIT_SVN_DIR/info"]);
1258 open my $fh, '>>',$REVDB or croak $!;
1260 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
1264 sub assert_svn_wc_clean {
1265 return if $_use_lib;
1267 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1268 my $lcr = svn_info('.')->{'Last Changed Rev'};
1269 if ($svn_rev != $lcr) {
1270 print STDERR "Checking for copy-tree ... ";
1271 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
1272 "-r$lcr:$svn_rev")));
1274 croak "Nope! Expected r$svn_rev, got r$lcr\n";
1276 print STDERR "OK!\n";
1279 my @status = grep(!/^Performing status on external/,(`svn status`));
1280 @status = grep(!/^\s*$/,@status);
1281 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1282 if (scalar @status) {
1283 print STDERR "Tree ($SVN_WC) is not clean:\n";
1284 print STDERR $_ foreach @status;
1289 sub get_tree_from_treeish {
1291 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1292 chomp(my $type = `git-cat-file -t $treeish`);
1294 while ($type eq 'tag') {
1295 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1297 if ($type eq 'commit') {
1298 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1299 ($expected) = ($expected =~ /^tree ($sha1)$/);
1300 die "Unable to get tree from $treeish\n" unless $expected;
1301 } elsif ($type eq 'tree') {
1302 $expected = $treeish;
1304 die "$treeish is a $type, expected tree, tag or commit\n";
1310 return if $_use_lib;
1312 my $expected = get_tree_from_treeish($treeish);
1314 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1316 unlink $tmpindex or croak $!;
1318 my $old_index = set_index($tmpindex);
1320 chomp(my $tree = `git-write-tree`);
1321 restore_index($old_index);
1322 if ($tree ne $expected) {
1323 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
1328 sub parse_diff_tree {
1329 my $diff_fh = shift;
1333 while (<$diff_fh>) {
1334 chomp $_; # this gets rid of the trailing "\0"
1335 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1336 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1337 push @mods, { mode_a => $1, mode_b => $2,
1338 sha1_b => $3, chg => $4 };
1339 if ($4 =~ /^(?:C|R)$/) {
1344 } elsif ($state eq 'file_a') {
1345 my $x = $mods[$#mods] or croak "Empty array\n";
1346 if ($x->{chg} !~ /^(?:C|R)$/) {
1347 croak "Error parsing $_, $x->{chg}\n";
1351 } elsif ($state eq 'file_b') {
1352 my $x = $mods[$#mods] or croak "Empty array\n";
1353 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1354 croak "Error parsing $_, $x->{chg}\n";
1356 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1357 croak "Error parsing $_, $x->{chg}\n";
1362 croak "Error parsing $_\n";
1365 close $diff_fh or croak $?;
1370 sub svn_check_prop_executable {
1372 return if -l $m->{file_b};
1373 if ($m->{mode_b} =~ /755$/) {
1374 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
1375 if ($m->{mode_a} !~ /755$/) {
1376 sys(qw(svn propset svn:executable 1), $m->{file_b});
1378 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
1379 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
1380 sys(qw(svn propdel svn:executable), $m->{file_b});
1381 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
1382 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
1386 sub svn_ensure_parent_path {
1387 my $dir_b = dirname(shift);
1388 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
1389 mkpath([$dir_b]) unless (-d $dir_b);
1390 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
1393 sub precommit_check {
1395 my (%rm_file, %rmdir_check, %added_check);
1397 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
1398 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1399 if ($m->{chg} eq 'R') {
1400 if (-d $m->{file_b}) {
1401 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1403 # dir/$file => dir/file/$file
1404 my $dirname = dirname($m->{file_b});
1405 while ($dirname ne File::Spec->curdir) {
1406 if ($dirname ne $m->{file_a}) {
1407 $dirname = dirname($dirname);
1410 err_file_to_dir("$m->{file_a} => $m->{file_b}");
1412 # baz/zzz => baz (baz is a file)
1413 $dirname = dirname($m->{file_a});
1414 while ($dirname ne File::Spec->curdir) {
1415 if ($dirname ne $m->{file_b}) {
1416 $dirname = dirname($dirname);
1419 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1422 if ($m->{chg} =~ /^(D|R)$/) {
1423 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
1424 $rm_file{ $m->{$t} } = 1;
1425 my $dirname = dirname( $m->{$t} );
1426 my $basename = basename( $m->{$t} );
1427 $rmdir_check{$dirname}->{$basename} = 1;
1428 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
1429 if (-d $m->{file_b}) {
1430 err_dir_to_file($m->{file_b});
1432 my $dirname = dirname( $m->{file_b} );
1433 my $basename = basename( $m->{file_b} );
1434 $added_check{$dirname}->{$basename} = 1;
1435 while ($dirname ne File::Spec->curdir) {
1436 if ($rm_file{$dirname}) {
1437 err_file_to_dir($m->{file_b});
1439 $dirname = dirname $dirname;
1443 return (\%rmdir_check, \%added_check);
1445 sub err_dir_to_file {
1447 print STDERR "Node change from directory to file ",
1448 "is not supported by Subversion: ",$file,"\n";
1451 sub err_file_to_dir {
1453 print STDERR "Node change from file to directory ",
1454 "is not supported by Subversion: ",$file,"\n";
1461 my ($from, $treeish) = @_;
1463 print "diff-tree $from $treeish\n";
1464 my $pid = open my $diff_fh, '-|';
1465 defined $pid or croak $!;
1467 my @diff_tree = qw(git-diff-tree -z -r);
1468 if ($_cp_similarity) {
1469 push @diff_tree, "-C$_cp_similarity";
1471 push @diff_tree, '-C';
1473 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1474 push @diff_tree, "-l$_l" if defined $_l;
1475 exec(@diff_tree, $from, $treeish) or croak $!;
1477 return parse_diff_tree($diff_fh);
1480 sub svn_checkout_tree {
1481 my ($from, $treeish) = @_;
1482 my $mods = get_diff($from->{commit}, $treeish);
1483 return $mods unless (scalar @$mods);
1484 my ($rm, $add) = precommit_check($mods);
1486 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1487 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1488 if ($m->{chg} eq 'C') {
1489 svn_ensure_parent_path( $m->{file_b} );
1490 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
1491 apply_mod_line_blob($m);
1492 svn_check_prop_executable($m);
1493 } elsif ($m->{chg} eq 'D') {
1494 sys(qw(svn rm --force), $m->{file_b});
1495 } elsif ($m->{chg} eq 'R') {
1496 svn_ensure_parent_path( $m->{file_b} );
1497 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
1498 apply_mod_line_blob($m);
1499 svn_check_prop_executable($m);
1500 } elsif ($m->{chg} eq 'M') {
1501 apply_mod_line_blob($m);
1502 svn_check_prop_executable($m);
1503 } elsif ($m->{chg} eq 'T') {
1504 sys(qw(svn rm --force),$m->{file_b});
1505 apply_mod_line_blob($m);
1506 sys(qw(svn add), $m->{file_b});
1507 svn_check_prop_executable($m);
1508 } elsif ($m->{chg} eq 'A') {
1509 svn_ensure_parent_path( $m->{file_b} );
1510 apply_mod_line_blob($m);
1511 sys(qw(svn add), $m->{file_b});
1512 svn_check_prop_executable($m);
1514 croak "Invalid chg: $m->{chg}\n";
1518 assert_tree($treeish);
1519 if ($_rmdir) { # remove empty directories
1520 handle_rmdir($rm, $add);
1522 assert_tree($treeish);
1526 sub libsvn_checkout_tree {
1527 my ($from, $treeish, $ed) = @_;
1528 my $mods = get_diff($from, $treeish);
1529 return $mods unless (scalar @$mods);
1530 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1531 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1533 if (defined $o{$f}) {
1536 croak "Invalid change type: $f\n";
1539 $ed->rmdirs($_q) if $_rmdir;
1543 # svn ls doesn't work with respect to the current working tree, but what's
1544 # in the repository. There's not even an option for it... *sigh*
1545 # (added files don't show up and removed files remain in the ls listing)
1546 sub svn_ls_current {
1547 my ($dir, $rm, $add) = @_;
1548 chomp(my @ls = safe_qx('svn','ls',$dir));
1551 s#/$##; # trailing slashes are evil
1552 push @ret, $_ unless $rm->{$dir}->{$_};
1554 if (exists $add->{$dir}) {
1555 push @ret, keys %{$add->{$dir}};
1561 my ($rm, $add) = @_;
1563 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1564 my $ls = svn_ls_current($dir, $rm, $add);
1565 next if (scalar @$ls);
1566 sys(qw(svn rm --force),$dir);
1568 my $dn = dirname $dir;
1569 $rm->{ $dn }->{ basename $dir } = 1;
1570 $ls = svn_ls_current($dn, $rm, $add);
1571 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1572 sys(qw(svn rm --force),$dn);
1573 $dir = basename $dn;
1575 $rm->{ $dn }->{ $dir } = 1;
1576 $ls = svn_ls_current($dn, $rm, $add);
1581 sub get_commit_message {
1582 my ($commit, $commit_msg) = (@_);
1583 my %log_msg = ( msg => '' );
1584 open my $msg, '>', $commit_msg or croak $!;
1586 chomp(my $type = `git-cat-file -t $commit`);
1587 if ($type eq 'commit' || $type eq 'tag') {
1588 my $pid = open my $msg_fh, '-|';
1589 defined $pid or croak $!;
1592 exec('git-cat-file', $type, $commit) or croak $!;
1597 $in_msg = 1 if (/^\s*$/);
1598 } elsif (/^git-svn-id: /) {
1599 # skip this, we regenerate the correct one
1600 # on re-fetch anyways
1602 print $msg $_ or croak $!;
1605 close $msg_fh or croak $?;
1607 close $msg or croak $!;
1609 if ($_edit || ($type eq 'tree')) {
1610 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1611 system($editor, $commit_msg);
1614 # file_to_s removes all trailing newlines, so just use chomp() here:
1615 open $msg, '<', $commit_msg or croak $!;
1616 { local $/; chomp($log_msg{msg} = <$msg>); }
1617 close $msg or croak $!;
1622 sub set_svn_commit_env {
1623 if (defined $LC_ALL) {
1624 $ENV{LC_ALL} = $LC_ALL;
1626 delete $ENV{LC_ALL};
1630 sub svn_commit_tree {
1631 my ($last, $commit) = @_;
1632 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1633 my $log_msg = get_commit_message($commit, $commit_msg);
1634 my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1635 print "Committing $commit: $oneline\n";
1637 set_svn_commit_env();
1638 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1641 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1642 if (!defined $committed) {
1643 my $out = join("\n",@ci_output);
1644 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1645 $out, "\n\nAssuming English locale...";
1646 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1647 defined $committed or die " FAILED!\n",
1648 "Commit output failed to parse committed revision!\n",
1649 print STDERR " OK\n";
1652 my @svn_up = qw(svn up);
1653 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1654 if ($_optimize_commits && ($committed == ($last->{revision} + 1))) {
1655 push @svn_up, "-r$committed";
1657 my $info = svn_info('.');
1658 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1659 if ($info->{'Last Changed Rev'} != $committed) {
1660 croak "$info->{'Last Changed Rev'} != $committed\n"
1662 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1663 /(\d{4})\-(\d\d)\-(\d\d)\s
1664 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1665 or croak "Failed to parse date: $date\n";
1666 $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
1667 $log_msg->{author} = $info->{'Last Changed Author'};
1668 $log_msg->{revision} = $committed;
1669 $log_msg->{msg} .= "\n";
1670 $log_msg->{parents} = [ $last->{commit} ];
1671 $log_msg->{commit} = git_commit($log_msg, $commit);
1674 # resync immediately
1675 push @svn_up, "-r$last->{revision}";
1677 return fetch("$committed=$commit");
1682 my $pid = open my $fh, '-|';
1683 defined $pid or croak $!;
1685 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1687 return { fh => $fh, t => { } };
1690 sub next_rev_list_entry {
1695 if (/^commit ($sha1)$/o) {
1697 $rl->{t} = { c => $1 };
1702 } elsif (/^parent ($sha1)$/o) {
1709 return ($x != $rl->{t}) ? $x : undef;
1712 # read the entire log into a temporary file (which is removed ASAP)
1713 # and store the file handle + parser state
1715 my (@log_args) = @_;
1716 my $log_fh = IO::File->new_tmpfile or croak $!;
1718 defined $pid or croak $!;
1720 open STDOUT, '>&', $log_fh or croak $!;
1721 exec (qw(svn log), @log_args) or croak $!
1725 seek $log_fh, 0, 0 or croak $!;
1726 return { state => 'sep', fh => $log_fh };
1729 sub next_log_entry {
1730 my $log = shift; # retval of svn_log_raw()
1732 my $fh = $log->{fh};
1737 if ($log->{state} eq 'msg') {
1738 if ($ret->{lines}) {
1739 $ret->{msg} .= $_."\n";
1740 unless(--$ret->{lines}) {
1741 $log->{state} = 'sep';
1744 croak "Log parse error at: $_\n",
1750 if ($log->{state} ne 'sep') {
1751 croak "Log parse error at: $_\n",
1752 "state: $log->{state}\n",
1756 $log->{state} = 'rev';
1758 # if we have an empty log message, put something there:
1760 $ret->{msg} ||= "\n";
1761 delete $ret->{lines};
1766 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1768 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1769 ($lines) = ($lines =~ /(\d+)/);
1770 $date = '1970-01-01 00:00:00 +0000'
1771 if ($_ignore_nodate && $date eq '(no date)');
1772 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1773 /(\d{4})\-(\d\d)\-(\d\d)\s
1774 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1775 or croak "Failed to parse date: $date\n";
1776 $ret = { revision => $rev,
1777 date => "$tz $Y-$m-$d $H:$M:$S",
1781 if (defined $_authors && ! defined $users{$author}) {
1782 die "Author: $author not defined in ",
1785 $log->{state} = 'msg_start';
1788 # skip the first blank line of the message:
1789 if ($log->{state} eq 'msg_start' && /^$/) {
1790 $log->{state} = 'msg';
1791 } elsif ($log->{state} eq 'msg') {
1792 if ($ret->{lines}) {
1793 $ret->{msg} .= $_."\n";
1794 unless (--$ret->{lines}) {
1795 $log->{state} = 'sep';
1798 croak "Log parse error at: $_\n",
1799 $ret->{revision},"\n";
1807 my $url = shift || $SVN_URL;
1809 my $pid = open my $info_fh, '-|';
1810 defined $pid or croak $!;
1813 exec(qw(svn info),$url) or croak $!;
1817 # only single-lines seem to exist in svn info output
1818 while (<$info_fh>) {
1820 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
1822 push @{$ret->{-order}}, $1;
1825 close $info_fh or croak $?;
1829 sub sys { system(@_) == 0 or croak $? }
1831 sub do_update_index {
1832 my ($z_cmd, $cmd, $no_text_base) = @_;
1834 my $z = open my $p, '-|';
1835 defined $z or croak $!;
1836 unless ($z) { exec @$z_cmd or croak $! }
1838 my $pid = open my $ui, '|-';
1839 defined $pid or croak $!;
1841 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1844 while (my $x = <$p>) {
1846 if (!$no_text_base && lstat $x && ! -l _ &&
1847 svn_propget_base('svn:keywords', $x)) {
1848 my $mode = -x _ ? 0755 : 0644;
1849 my ($v,$d,$f) = File::Spec->splitpath($x);
1850 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1851 'text-base',"$f.svn-base");
1854 $tb = File::Spec->catfile($d, '.svn',
1855 'text-base',"$f.svn-base");
1859 unlink $x or croak $!;
1861 chmod(($mode &~ umask), $x) or croak $!;
1862 utime $s[8], $s[9], $x;
1866 close $ui or croak $?;
1870 return if $_use_lib;
1872 if (!-f "$GIT_SVN_DIR/info/exclude") {
1873 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
1874 print $fd '.svn',"\n";
1875 close $fd or croak $!;
1877 my $no_text_base = shift;
1878 do_update_index([qw/git-diff-files --name-only -z/],
1881 do_update_index([qw/git-ls-files -z --others/,
1882 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1888 my ($str, $file, $mode) = @_;
1889 open my $fd,'>',$file or croak $!;
1890 print $fd $str,"\n" or croak $!;
1891 close $fd or croak $!;
1892 chmod ($mode &~ umask, $file) if (defined $mode);
1897 open my $fd,'<',$file or croak "$!: file: $file\n";
1900 close $fd or croak $!;
1905 sub assert_revision_unknown {
1907 if (my $c = revdb_get($REVDB, $r)) {
1908 croak "$r = $c already exists! Why are we refetching it?";
1914 my @x = safe_qx('git-cat-file','commit',$x);
1915 my @y = safe_qx('git-cat-file','commit',$y);
1916 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1917 || $y[0] !~ /^tree $sha1\n$/) {
1918 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1925 my ($log_msg, @parents) = @_;
1926 assert_revision_unknown($log_msg->{revision});
1927 map_tree_joins() if (@_branch_from && !%tree_map);
1929 my (@tmp_parents, @exec_parents, %seen_parent);
1930 if (my $lparents = $log_msg->{parents}) {
1931 @tmp_parents = @$lparents
1933 # commit parents can be conditionally bound to a particular
1934 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1935 foreach my $p (@parents) {
1936 next unless defined $p;
1937 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1938 if ($1 == $log_msg->{revision}) {
1939 push @tmp_parents, $2;
1942 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1945 my $tree = $log_msg->{tree};
1946 if (!defined $tree) {
1947 my $index = set_index($GIT_SVN_INDEX);
1949 chomp($tree = `git-write-tree`);
1951 restore_index($index);
1954 # just in case we clobber the existing ref, we still want that ref
1956 if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1957 push @tmp_parents, $cur;
1960 if (exists $tree_map{$tree}) {
1961 foreach my $p (@{$tree_map{$tree}}) {
1963 foreach (@tmp_parents) {
1964 # see if a common parent is found
1966 safe_qx('git-merge-base', $_, $p)
1973 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
1974 next if (($SVN_UUID eq $uuid_p) &&
1975 ($log_msg->{revision} > $r_p));
1976 next if (defined $url_p && defined $SVN_URL &&
1977 ($SVN_UUID eq $uuid_p) &&
1978 ($url_p eq $SVN_URL));
1979 push @tmp_parents, $p;
1982 foreach (@tmp_parents) {
1983 next if $seen_parent{$_};
1984 $seen_parent{$_} = 1;
1985 push @exec_parents, $_;
1986 # MAXPARENT is defined to 16 in commit-tree.c:
1987 last if @exec_parents > 16;
1990 set_commit_env($log_msg);
1991 my @exec = ('git-commit-tree', $tree);
1992 push @exec, '-p', $_ foreach @exec_parents;
1993 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1995 print $msg_fh $log_msg->{msg} or croak $!;
1996 unless ($_no_metadata) {
1997 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1998 " $SVN_UUID\n" or croak $!;
2000 $msg_fh->flush == 0 or croak $!;
2001 close $msg_fh or croak $!;
2002 chomp(my $commit = do { local $/; <$out_fh> });
2003 close $out_fh or croak $!;
2006 if ($commit !~ /^$sha1$/o) {
2007 die "Failed to commit, invalid sha1: $commit\n";
2009 sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2010 revdb_set($REVDB, $log_msg->{revision}, $commit);
2012 # this output is read via pipe, do not change:
2013 print "r$log_msg->{revision} = $commit\n";
2019 if ($_repack && (--$_repack_nr == 0)) {
2020 $_repack_nr = $_repack;
2021 sys("git repack $_repack_flags");
2025 sub set_commit_env {
2027 my $author = $log_msg->{author};
2028 if (!defined $author || length $author == 0) {
2029 $author = '(no author)';
2031 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
2032 : ($author,"$author\@$SVN_UUID");
2033 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2034 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2035 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2038 sub apply_mod_line_blob {
2040 if ($m->{mode_b} =~ /^120/) {
2041 blob_to_symlink($m->{sha1_b}, $m->{file_b});
2043 blob_to_file($m->{sha1_b}, $m->{file_b});
2047 sub blob_to_symlink {
2048 my ($blob, $link) = @_;
2049 defined $link or croak "\$link not defined!\n";
2050 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2051 if (-l $link || -f _) {
2052 unlink $link or croak $!;
2055 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2056 symlink $dest, $link or croak $!;
2060 my ($blob, $file) = @_;
2061 defined $file or croak "\$file not defined!\n";
2062 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2063 if (-l $file || -f _) {
2064 unlink $file or croak $!;
2067 open my $blob_fh, '>', $file or croak "$!: $file\n";
2069 defined $pid or croak $!;
2072 open STDOUT, '>&', $blob_fh or croak $!;
2073 exec('git-cat-file','blob',$blob) or croak $!;
2078 close $blob_fh or croak $!;
2082 my $pid = open my $child, '-|';
2083 defined $pid or croak $!;
2085 exec(@_) or croak $!;
2087 my @ret = (<$child>);
2088 close $child or croak $?;
2089 die $? if $?; # just in case close didn't error out
2090 return wantarray ? @ret : join('',@ret);
2093 sub svn_compat_check {
2094 if ($_follow_parent) {
2095 print STDERR 'E: --follow-parent functionality is only ',
2096 "available when SVN libraries are used\n";
2099 my @co_help = safe_qx(qw(svn co -h));
2100 unless (grep /ignore-externals/,@co_help) {
2101 print STDERR "W: Installed svn version does not support ",
2102 "--ignore-externals\n";
2103 $_no_ignore_ext = 1;
2105 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2106 $_svn_co_url_revs = 1;
2108 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2109 $_svn_pg_peg_revs = 1;
2112 # I really, really hope nobody hits this...
2113 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
2115 W: The installed svn version does not support the --stop-on-copy flag in
2117 Lets hope the directory you're tracking is not a branch or tag
2118 and was never moved within the repository...
2124 # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
2125 # (and they won't honor URL@<rev> without -r<rev>, too!)
2126 sub svn_cmd_checkout {
2127 my ($url, $rev, $dir) = @_;
2128 my @cmd = ('svn','co', "-r$rev");
2129 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2130 $url .= "\@$rev" if $_svn_co_url_revs;
2131 sys(@cmd, $url, $dir);
2134 sub check_upgrade_needed {
2136 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2137 open my $fh, '>>',$REVDB or croak $!;
2141 my $pid = open my $child, '-|';
2142 defined $pid or croak $!;
2145 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2147 my @ret = (<$child>);
2148 close $child or croak $?;
2149 die $? if $?; # just in case close didn't error out
2150 return wantarray ? @ret : join('',@ret);
2153 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
2155 print STDERR "Please run: $0 rebuild --upgrade\n";
2160 # fills %tree_map with a reverse mapping of trees to commits. Useful
2161 # for finding parents to commit on.
2162 sub map_tree_joins {
2164 foreach my $br (@_branch_from) {
2165 my $pid = open my $pipe, '-|';
2166 defined $pid or croak $!;
2168 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2172 if (/^commit ($sha1)$/o) {
2175 # if we've seen a commit,
2176 # we've seen its parents
2177 last if $seen{$commit};
2178 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2179 unless (defined $tree) {
2180 die "Failed to parse commit $commit\n";
2182 push @{$tree_map{$tree}}, $commit;
2186 close $pipe; # we could be breaking the pipe early
2191 if (@_branch_from) {
2192 print STDERR '--branch|-b parameters are ignored when ',
2193 "--branch-all-refs|-B is passed\n";
2196 # don't worry about rev-list on non-commit objects/tags,
2197 # it shouldn't blow up if a ref is a blob or tree...
2198 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2201 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2203 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2204 while (<$authors>) {
2206 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2207 my ($user, $name, $email) = ($1, $2, $3);
2208 $users{$user} = [$name, $email];
2210 close $authors or croak $!;
2214 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2215 while (<$authors>) {
2217 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2218 my ($user, $name, $email) = ($1, $2, $3);
2219 $rusers{"$name <$email>"} = $user;
2221 close $authors or croak $!;
2224 sub svn_propget_base {
2226 $f .= '@BASE' if $_svn_pg_peg_revs;
2227 return safe_qx(qw/svn propget/, $p, $f);
2232 foreach (`git-rev-parse --symbolic --all`) {
2233 next unless s#^refs/remotes/##;
2235 next unless -f "$GIT_DIR/svn/$_/info/url";
2243 defined(my $pid = fork) or croak $!;
2245 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2247 exit 0 if -r $REVDB;
2248 print "Upgrading svn => git mapping...\n";
2249 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2250 open my $fh, '>>',$REVDB or croak $!;
2253 print "Done upgrading. You may now delete the ",
2254 "deprecated $GIT_SVN_DIR/revs directory\n";
2262 sub migration_check {
2263 migrate_revdb() unless (-e $REVDB);
2264 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
2265 print "Upgrading repository...\n";
2266 unless (-d "$GIT_DIR/svn") {
2267 mkdir "$GIT_DIR/svn" or croak $!;
2269 print "Data from a previous version of git-svn exists, but\n\t",
2270 "$GIT_SVN_DIR\n\t(required for this version ",
2271 "($VERSION) of git-svn) does not.\n";
2273 foreach my $x (`git-rev-parse --symbolic --all`) {
2274 next unless $x =~ s#^refs/remotes/##;
2276 next unless -f "$GIT_DIR/$x/info/url";
2277 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
2279 my $dn = dirname("$GIT_DIR/svn/$x");
2280 mkpath([$dn]) unless -d $dn;
2281 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
2283 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
2284 print "Done upgrading.\n";
2287 sub find_rev_before {
2288 my ($r, $id, $eq_ok) = @_;
2289 my $f = "$GIT_DIR/svn/$id/.rev_db";
2290 return (undef,undef) unless -r $f;
2293 if (my $c = revdb_get($f, $r)) {
2298 return (undef, undef);
2302 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
2303 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2304 $REVDB = "$GIT_SVN_DIR/.rev_db";
2305 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2307 $SVN_WC = "$GIT_SVN_DIR/tree";
2311 # convert GetOpt::Long specs for use by git-repo-config
2312 sub read_repo_config {
2313 return unless -d $GIT_DIR;
2315 foreach my $o (keys %$opts) {
2316 my $v = $opts->{$o};
2317 my ($key) = ($o =~ /^([a-z\-]+)/);
2319 my $arg = 'git-repo-config';
2320 $arg .= ' --int' if ($o =~ /[:=]i$/);
2321 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2322 if (ref $v eq 'ARRAY') {
2323 chomp(my @tmp = `$arg --get-all svn.$key`);
2326 chomp(my $tmp = `$arg --get svn.$key`);
2327 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2334 sub set_default_vals {
2335 if (defined $_repack) {
2336 $_repack = 1000 if ($_repack <= 0);
2337 $_repack_nr = $_repack;
2338 $_repack_flags ||= '-d';
2343 my $gr_file = shift;
2344 my ($grafts, $comments) = ({}, {});
2345 if (open my $fh, '<', $gr_file) {
2348 if (/^($sha1)\s+/) {
2351 @{$comments->{$c}} = @tmp;
2354 foreach my $p (split /\s+/, $_) {
2355 $grafts->{$c}->{$p} = 1;
2361 close $fh or croak $!;
2362 @{$comments->{'END'}} = @tmp if @tmp;
2364 return ($grafts, $comments);
2368 my ($grafts, $comments, $gr_file) = @_;
2370 open my $fh, '>', $gr_file or croak $!;
2371 foreach my $c (sort keys %$grafts) {
2372 if ($comments->{$c}) {
2373 print $fh $_ foreach @{$comments->{$c}};
2375 my $p = $grafts->{$c};
2376 my %x; # real parents
2377 delete $p->{$c}; # commits are not self-reproducing...
2378 my $pid = open my $ch, '-|';
2379 defined $pid or croak $!;
2381 exec(qw/git-cat-file commit/, $c) or croak $!;
2384 if (/^parent ($sha1)/) {
2385 $x{$1} = $p->{$1} = 1;
2390 close $ch; # breaking the pipe
2392 # if real parents are the only ones in the grafts, drop it
2393 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2397 @ip = @jp = keys %$p;
2398 foreach my $i (@ip) {
2399 next if $del{$i} || $p->{$i} == 2;
2400 foreach my $j (@jp) {
2401 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2402 $mb = eval { safe_qx('git-merge-base',$i,$j) };
2409 } elsif ($mb eq $i) {
2416 # if real parents are the only ones in the grafts, drop it
2417 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2419 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2421 if ($comments->{'END'}) {
2422 print $fh $_ foreach @{$comments->{'END'}};
2424 close $fh or croak $!;
2427 sub read_url_paths_all {
2428 my ($l_map, $pfx, $p) = @_;
2431 if (-r "$_/info/url") {
2432 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2433 my $id = $pfx . basename $_;
2434 my $url = file_to_s("$_/info/url");
2435 my ($u, $p) = repo_path_split($url);
2436 $l_map->{$u}->{$p} = $id;
2443 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2444 read_url_paths_all($l_map, $x, $_);
2448 # this one only gets ids that have been imported, not new ones
2449 sub read_url_paths {
2451 git_svn_each(sub { my $x = shift;
2452 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
2453 my ($u, $p) = repo_path_split($url);
2454 $l_map->{$u}->{$p} = $x;
2459 sub extract_metadata {
2460 my $id = shift or return (undef, undef, undef);
2461 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
2463 if (!$rev || !$uuid || !$url) {
2464 # some of the original repositories I made had
2465 # identifiers like this:
2466 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2468 return ($url, $rev, $uuid);
2472 return extract_metadata((grep(/^git-svn-id: /,
2473 safe_qx(qw/git-cat-file commit/, shift)))[-1]);
2476 sub get_commit_time {
2478 defined(my $pid = open my $fh, '-|') or croak $!;
2480 exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!;
2483 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2484 my ($s, $tz) = ($1, $2);
2485 if ($tz =~ s/^\+//) {
2486 $s += tz_to_s_offset($tz);
2487 } elsif ($tz =~ s/^\-//) {
2488 $s -= tz_to_s_offset($tz);
2493 die "Can't get commit time for commit: $cmt\n";
2496 sub tz_to_s_offset {
2499 return ($1 * 60) + ($tz * 3600);
2502 sub setup_pager { # translated to Perl from pager.c
2503 return unless (-t *STDOUT);
2504 my $pager = $ENV{PAGER};
2505 if (!defined $pager) {
2507 } elsif (length $pager == 0 || $pager eq 'cat') {
2510 pipe my $rfd, my $wfd or return;
2511 defined(my $pid = fork) or croak $!;
2513 open STDOUT, '>&', $wfd or croak $!;
2516 open STDIN, '<&', $rfd or croak $!;
2517 $ENV{LESS} ||= '-S';
2518 exec $pager or croak "Can't run pager: $!\n";;
2521 sub get_author_info {
2522 my ($dest, $author, $t, $tz) = @_;
2523 $author =~ s/(?:^\s*|\s*$)//g;
2524 $dest->{a_raw} = $author;
2527 $_a = $rusers{$author} || undef;
2530 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2535 # Date::Parse isn't in the standard Perl distro :(
2536 if ($tz =~ s/^\+//) {
2537 $t += tz_to_s_offset($tz);
2538 } elsif ($tz =~ s/^\-//) {
2539 $t -= tz_to_s_offset($tz);
2541 $dest->{t_utc} = $t;
2544 sub process_commit {
2545 my ($c, $r_min, $r_max, $defer) = @_;
2546 if (defined $r_min && defined $r_max) {
2547 if ($r_min == $c->{r} && $r_min == $r_max) {
2551 return 1 if $r_min == $r_max;
2552 if ($r_min < $r_max) {
2553 # we need to reverse the print order
2554 return 0 if (defined $_limit && --$_limit < 0);
2558 if ($r_min != $r_max) {
2559 return 1 if ($r_min < $c->{r});
2560 return 1 if ($r_max > $c->{r});
2563 return 0 if (defined $_limit && --$_limit < 0);
2572 if (my $l = $c->{l}) {
2573 while ($l->[0] =~ /^\s*$/) { shift @$l }
2576 $_l_fmt ||= 'A' . length($c->{r});
2577 print 'r',pack($_l_fmt, $c->{r}),' | ';
2578 print "$c->{c} | " if $_show_commit;
2581 show_commit_normal($c);
2585 sub show_commit_changed_paths {
2587 return unless $c->{changed};
2588 print "Changed paths:\n", @{$c->{changed}};
2591 sub show_commit_normal {
2593 print '-' x72, "\nr$c->{r} | ";
2594 print "$c->{c} | " if $_show_commit;
2595 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2596 localtime($c->{t_utc})), ' | ';
2599 if (my $l = $c->{l}) {
2600 while ($l->[$#$l] eq "\n" && $#$l > 0
2601 && $l->[($#$l - 1)] eq "\n") {
2604 $nr_line = scalar @$l;
2606 print "1 line\n\n\n";
2608 if ($nr_line == 1) {
2609 $nr_line = '1 line';
2611 $nr_line .= ' lines';
2613 print $nr_line, "\n";
2614 show_commit_changed_paths($c);
2616 print $_ foreach @$l;
2620 show_commit_changed_paths($c);
2624 foreach my $x (qw/raw diff/) {
2627 print $_ foreach @{$c->{$x}}
2633 return unless $_use_lib;
2636 if ($SVN::Core::VERSION lt '1.1.0') {
2637 die "Need SVN::Core 1.1.0 or better ",
2638 "(got $SVN::Core::VERSION) ",
2639 "Falling back to command-line svn\n";
2643 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2644 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2645 $SVN::Node::dir.$SVN::Node::unknown.
2646 $SVN::Node::none.$SVN::Node::file.
2647 $SVN::Node::dir.$SVN::Node::unknown;
2652 sub libsvn_connect {
2654 my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
2655 SVN::Client::get_ssl_server_trust_file_provider(),
2656 SVN::Client::get_username_provider()]);
2657 my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
2661 sub libsvn_get_file {
2662 my ($gui, $f, $rev) = @_;
2664 if (length $SVN_PATH > 0) {
2665 return unless ($p =~ s#^\Q$SVN_PATH\E/##);
2668 my ($hash, $pid, $in, $out);
2669 my $pool = SVN::Pool->new;
2670 defined($pid = open3($in, $out, '>&STDERR',
2671 qw/git-hash-object -w --stdin/)) or croak $!;
2672 # redirect STDOUT for SVN 1.1.x compatibility
2673 open my $stdout, '>&', \*STDOUT or croak $!;
2674 open STDOUT, '>&', $in or croak $!;
2675 my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool);
2676 $in->flush == 0 or croak $!;
2677 open STDOUT, '>&', $stdout or croak $!;
2678 close $in or croak $!;
2679 close $stdout or croak $!;
2681 chomp($hash = do { local $/; <$out> });
2682 close $out or croak $!;
2684 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2686 my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
2687 if (exists $props->{'svn:special'}) {
2689 my $link = `git-cat-file blob $hash`;
2690 $link =~ s/^link // or die "svn:special file with contents: <",
2691 $link, "> is not understood\n";
2692 defined($pid = open3($in, $out, '>&STDERR',
2693 qw/git-hash-object -w --stdin/)) or croak $!;
2695 $in->flush == 0 or croak $!;
2696 close $in or croak $!;
2697 chomp($hash = do { local $/; <$out> });
2698 close $out or croak $!;
2700 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2702 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2705 sub libsvn_log_entry {
2706 my ($rev, $author, $date, $msg, $parents) = @_;
2707 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2708 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2709 or die "Unable to parse date: $date\n";
2710 if (defined $_authors && ! defined $users{$author}) {
2711 die "Author: $author not defined in $_authors file\n";
2713 $msg = '' if ($rev == 0 && !defined $msg);
2714 return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2715 author => $author, msg => $msg."\n", parents => $parents || [] }
2719 my ($gui, $last_commit, $f) = @_;
2720 $f =~ s#^\Q$SVN_PATH\E/?## or return;
2721 # remove entire directories.
2722 if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2723 defined(my $pid = open my $ls, '-|') or croak $!;
2725 exec(qw/git-ls-tree -r --name-only -z/,
2726 $last_commit,'--',$f) or croak $!;
2730 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2732 close $ls or croak $?;
2734 print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2739 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2740 open my $gui, '| git-update-index -z --index-info' or croak $!;
2742 foreach my $f (keys %$paths) {
2743 my $m = $paths->{$f}->action();
2745 if ($m =~ /^[DR]$/) {
2746 print "\t$m\t$f\n" unless $_q;
2747 process_rm($gui, $last_commit, $f);
2749 # 'R' can be file replacements, too, right?
2751 my $pool = SVN::Pool->new;
2752 my $t = $SVN->check_path($f, $rev, $pool);
2753 if ($t == $SVN::Node::file) {
2754 if ($m =~ /^[AMR]$/) {
2755 push @amr, [ $m, $f ];
2757 die "Unrecognized action: $m, ($f r$rev)\n";
2759 } elsif ($t == $SVN::Node::dir && $m =~ /^[AR]$/) {
2761 libsvn_traverse($gui, '', $f, $rev, \@traversed);
2762 foreach (@traversed) {
2763 push @amr, [ $m, $_ ]
2769 print "\t$_->[0]\t$_->[1]\n" unless $_q;
2770 libsvn_get_file($gui, $_->[1], $rev)
2772 close $gui or croak $?;
2773 return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2776 sub svn_grab_base_rev {
2777 defined(my $pid = open my $fh, '-|') or croak $!;
2779 open my $null, '>', '/dev/null' or croak $!;
2780 open STDERR, '>&', $null or croak $!;
2781 exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2784 chomp(my $c = do { local $/; <$fh> });
2786 if (defined $c && length $c) {
2787 my ($url, $rev, $uuid) = cmt_metadata($c);
2788 return ($rev, $c) if defined $rev;
2790 if ($_no_metadata) {
2791 my $offset = -41; # from tail
2793 open my $fh, '<', $REVDB or
2794 die "--no-metadata specified and $REVDB not readable\n";
2795 seek $fh, $offset, 2;
2797 defined $rl or return (undef, undef);
2799 while ($c ne $rl && tell $fh != 0) {
2801 seek $fh, $offset, 2;
2803 defined $rl or return (undef, undef);
2807 croak $! if ($rev < -1);
2808 $rev = ($rev - 41) / 41;
2809 close $fh or croak $!;
2812 return (undef, undef);
2815 sub libsvn_parse_revision {
2817 my $head = $SVN->get_latest_revnum();
2818 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2819 return ($base + 1, $head) if (defined $base);
2822 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2823 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2824 if ($_revision =~ /^BASE:(\d+)$/) {
2825 return ($base + 1, $1) if (defined $base);
2828 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2829 die "revision argument: $_revision not understood by git-svn\n",
2830 "Try using the command-line svn client instead\n";
2833 sub libsvn_traverse {
2834 my ($gui, $pfx, $path, $rev, $files) = @_;
2835 my $cwd = "$pfx/$path";
2836 my $pool = SVN::Pool->new;
2838 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2839 foreach my $d (keys %$dirent) {
2840 my $t = $dirent->{$d}->kind;
2841 if ($t == $SVN::Node::dir) {
2842 libsvn_traverse($gui, $cwd, $d, $rev, $files);
2843 } elsif ($t == $SVN::Node::file) {
2844 my $file = "$cwd/$d";
2845 if (defined $files) {
2846 push @$files, $file;
2848 print "\tA\t$file\n" unless $_q;
2849 libsvn_get_file($gui, $file, $rev);
2856 sub libsvn_traverse_ignore {
2857 my ($fh, $path, $r) = @_;
2859 my $pool = SVN::Pool->new;
2860 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2862 $p =~ s#^\Q$SVN_PATH\E/?##;
2863 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2864 if (my $s = $props->{'svn:ignore'}) {
2865 $s =~ s/[\r\n]+/\n/g;
2867 if (length $p == 0) {
2871 $s =~ s#\n#\n/$p/#g;
2872 print $fh "/$p/$s\n";
2875 foreach (sort keys %$dirent) {
2876 next if $dirent->{$_}->kind != $SVN::Node::dir;
2877 libsvn_traverse_ignore($fh, "$path/$_", $r);
2883 my ($path, $r0, $r1) = @_;
2884 return 1 if $r0 == $r1;
2887 # should be OK to use Pool here (r1 - r0) should be small
2888 my $pool = SVN::Pool->new;
2889 libsvn_get_log($SVN, "/$path", $r0, $r1,
2890 0, 1, 1, sub {$nr++}, $pool);
2893 my ($url, undef) = repo_path_split($SVN_URL);
2894 my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1");
2895 while (next_log_entry($svn_log)) { $nr++ }
2896 close $svn_log->{fh};
2898 return 0 if ($nr > 1);
2902 sub libsvn_find_parent_branch {
2903 my ($paths, $rev, $author, $date, $msg) = @_;
2904 my $svn_path = '/'.$SVN_PATH;
2906 # look for a parent from another branch:
2907 my $i = $paths->{$svn_path} or return;
2908 my $branch_from = $i->copyfrom_path or return;
2909 my $r = $i->copyfrom_rev;
2910 print STDERR "Found possible branch point: ",
2911 "$branch_from => $svn_path, $r\n";
2912 $branch_from =~ s#^/##;
2914 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2915 my $url = $SVN->{url};
2916 defined $l_map->{$url} or return;
2917 my $id = $l_map->{$url}->{$branch_from};
2918 if (!defined $id && $_follow_parent) {
2919 print STDERR "Following parent: $branch_from\@$r\n";
2920 # auto create a new branch and follow it
2921 $id = basename($branch_from);
2922 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2923 while (-r "$GIT_DIR/svn/$id") {
2924 # just grow a tail if we're not unique enough :x
2928 return unless defined $id;
2930 my ($r0, $parent) = find_rev_before($r,$id,1);
2931 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2932 defined(my $pid = fork) or croak $!;
2934 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2936 $SVN_URL = "$url/$branch_from";
2937 $SVN_LOG = $SVN = undef;
2939 # we can't assume SVN_URL exists at r+1:
2940 $_revision = "0:$r";
2946 ($r0, $parent) = find_rev_before($r,$id,1);
2948 return unless (defined $r0 && defined $parent);
2949 if (revisions_eq($branch_from, $r0, $r)) {
2950 unlink $GIT_SVN_INDEX;
2951 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2952 sys(qw/git-read-tree/, $parent);
2953 return libsvn_fetch($parent, $paths, $rev,
2954 $author, $date, $msg);
2956 print STDERR "Nope, branch point not imported or unknown\n";
2960 sub libsvn_get_log {
2961 my ($ra, @args) = @_;
2962 if ($SVN::Core::VERSION le '1.2.0') {
2963 splice(@args, 3, 1);
2965 $ra->get_log(@args);
2968 sub libsvn_new_tree {
2969 if (my $log_entry = libsvn_find_parent_branch(@_)) {
2972 my ($paths, $rev, $author, $date, $msg) = @_;
2973 open my $gui, '| git-update-index -z --index-info' or croak $!;
2974 libsvn_traverse($gui, '', $SVN_PATH, $rev);
2975 close $gui or croak $?;
2976 return libsvn_log_entry($rev, $author, $date, $msg);
2979 sub find_graft_path_commit {
2980 my ($tree_paths, $p1, $r1) = @_;
2981 foreach my $x (keys %$tree_paths) {
2982 next unless ($p1 =~ /^\Q$x\E/);
2983 my $i = $tree_paths->{$x};
2984 my ($r0, $parent) = find_rev_before($r1,$i,1);
2985 return $parent if (defined $r0 && $r0 == $r1);
2986 print STDERR "r$r1 of $i not imported\n";
2992 sub find_graft_path_parents {
2993 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2994 foreach my $x (keys %$tree_paths) {
2995 next unless ($p0 =~ /^\Q$x\E/);
2996 my $i = $tree_paths->{$x};
2997 my ($r, $parent) = find_rev_before($r0, $i, 1);
2998 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
2999 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
3000 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
3001 next if ($url_a && $url_b && $url_a eq $url_b &&
3002 $uuid_b eq $uuid_a);
3003 $grafts->{$c}->{$parent} = 1;
3008 sub libsvn_graft_file_copies {
3009 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
3010 foreach (keys %$paths) {
3011 my $i = $paths->{$_};
3012 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
3014 next unless (defined $p0 && defined $r0);
3019 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
3021 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
3026 my $old = $ENV{GIT_INDEX_FILE};
3027 $ENV{GIT_INDEX_FILE} = shift;
3034 $ENV{GIT_INDEX_FILE} = $old;
3036 delete $ENV{GIT_INDEX_FILE};
3040 sub libsvn_commit_cb {
3041 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3042 if ($_optimize_commits && $rev == ($r_last + 1)) {
3043 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
3044 $log->{tree} = get_tree_from_treeish($c);
3045 my $cmt = git_commit($log, $cmt_last, $c);
3046 my @diff = safe_qx('git-diff-tree', $cmt, $c);
3048 print STDERR "Trees differ: $cmt $c\n",
3049 join('',@diff),"\n";
3057 sub libsvn_ls_fullurl {
3058 my $fullurl = shift;
3059 my ($repo, $path) = repo_path_split($fullurl);
3060 $SVN ||= libsvn_connect($repo);
3062 my $pool = SVN::Pool->new;
3063 my ($dirent, undef, undef) = $SVN->get_dir($path,
3064 $SVN->get_latest_revnum, $pool);
3065 foreach my $d (keys %$dirent) {
3066 if ($dirent->{$d}->kind == $SVN::Node::dir) {
3067 push @ret, "$d/"; # add '/' for compat with cli svn
3075 sub libsvn_skip_unknown_revs {
3077 my $errno = $err->apr_err();
3078 # Maybe the branch we're tracking didn't
3079 # exist when the repo started, so it's
3080 # not an error if it doesn't, just continue
3082 # Wonderfully consistent library, eh?
3083 # 160013 - svn:// and file://
3084 # 175002 - http(s)://
3085 # More codes may be discovered later...
3086 if ($errno == 175002 || $errno == 160013) {
3089 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3092 # Tie::File seems to be prone to offset errors if revisions get sparse,
3093 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3094 # one of my favorite modules is out :< Next up would be one of the DBM
3095 # modules, but I'm not sure which is most portable... So I'll just
3096 # go with something that's plain-text, but still capable of
3097 # being randomly accessed. So here's my ultra-simple fixed-width
3098 # database. All records are 40 characters + "\n", so it's easy to seek
3099 # to a revision: (41 * rev) is the byte offset.
3100 # A record of 40 0s denotes an empty revision.
3101 # And yes, it's still pretty fast (faster than Tie::File).
3103 my ($file, $rev, $commit) = @_;
3104 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
3105 open my $fh, '+<', $file or croak $!;
3106 my $offset = $rev * 41;
3107 # assume that append is the common case:
3108 seek $fh, 0, 2 or croak $!;
3110 if ($pos < $offset) {
3111 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
3113 seek $fh, $offset, 0 or croak $!;
3114 print $fh $commit,"\n";
3115 close $fh or croak $!;
3119 my ($file, $rev) = @_;
3121 my $offset = $rev * 41;
3122 open my $fh, '<', $file or croak $!;
3123 seek $fh, $offset, 0;
3124 if (tell $fh == $offset) {
3125 $ret = readline $fh;
3128 $ret = undef if ($ret =~ /^0{40}$/);
3131 close $fh or croak $!;
3135 sub copy_remote_ref {
3136 my $origin = $_cp_remote ? $_cp_remote : 'origin';
3137 my $ref = "refs/remotes/$GIT_SVN";
3138 if (safe_qx('git-ls-remote', $origin, $ref)) {
3139 sys(qw/git fetch/, $origin, "$ref:$ref");
3141 die "Unable to find remote reference: ",
3142 "refs/remotes/$GIT_SVN on $origin\n";
3146 package SVN::Git::Editor;
3155 my $git_svn = shift;
3156 my $self = SVN::Delta::Editor->new(@_);
3157 bless $self, $class;
3158 foreach (qw/svn_path c r ra /) {
3159 die "$_ required!\n" unless (defined $git_svn->{$_});
3160 $self->{$_} = $git_svn->{$_};
3162 $self->{pool} = SVN::Pool->new;
3163 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3165 require Digest::MD5;
3170 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3174 (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
3179 my ($self, $path) = @_;
3180 $self->{ra}->{url} . '/' . $self->repo_path($path);
3184 my ($self, $q) = @_;
3185 my $rm = $self->{rm};
3186 delete $rm->{''}; # we never delete the url we're tracking
3189 foreach (keys %$rm) {
3190 my @d = split m#/#, $_;
3194 $c .= '/' . shift @d;
3198 delete $rm->{$self->{svn_path}};
3199 delete $rm->{''}; # we never delete the url we're tracking
3202 defined(my $pid = open my $fh,'-|') or croak $!;
3204 exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
3207 my @svn_path = split m#/#, $self->{svn_path};
3210 my @dn = (@svn_path, (split m#/#, $_));
3212 delete $rm->{join '/', @dn};
3221 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3222 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3223 $self->close_directory($bat->{$d}, $p);
3224 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
3225 print "\tD+\t/$d/\n" unless $q;
3226 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3231 sub open_or_add_dir {
3232 my ($self, $full_path, $baton) = @_;
3233 my $p = SVN::Pool->new;
3234 my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
3236 if ($t == $SVN::Node::none) {
3237 return $self->add_directory($full_path, $baton,
3238 undef, -1, $self->{pool});
3239 } elsif ($t == $SVN::Node::dir) {
3240 return $self->open_directory($full_path, $baton,
3241 $self->{r}, $self->{pool});
3243 print STDERR "$full_path already exists in repository at ",
3244 "r$self->{r} and it is not a directory (",
3245 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3250 my ($self, $path) = @_;
3251 my $bat = $self->{bat};
3252 $path = $self->repo_path($path);
3253 return $bat->{''} unless (length $path);
3254 my @p = split m#/+#, $path;
3256 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3259 $c .= '/' . shift @p;
3260 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3266 my ($self, $m, $q) = @_;
3267 my ($dir, $file) = split_path($m->{file_b});
3268 my $pbat = $self->ensure_path($dir);
3269 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3271 print "\tA\t$m->{file_b}\n" unless $q;
3272 $self->chg_file($fbat, $m);
3273 $self->close_file($fbat,undef,$self->{pool});
3277 my ($self, $m, $q) = @_;
3278 my ($dir, $file) = split_path($m->{file_b});
3279 my $pbat = $self->ensure_path($dir);
3280 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3281 $self->url_path($m->{file_a}), $self->{r});
3282 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3283 $self->chg_file($fbat, $m);
3284 $self->close_file($fbat,undef,$self->{pool});
3288 my ($self, $path, $pbat) = @_;
3289 my $rpath = $self->repo_path($path);
3290 my ($dir, $file) = split_path($rpath);
3291 $self->{rm}->{$dir} = 1;
3292 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3296 my ($self, $m, $q) = @_;
3297 my ($dir, $file) = split_path($m->{file_b});
3298 my $pbat = $self->ensure_path($dir);
3299 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3300 $self->url_path($m->{file_a}), $self->{r});
3301 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3302 $self->chg_file($fbat, $m);
3303 $self->close_file($fbat,undef,$self->{pool});
3305 ($dir, $file) = split_path($m->{file_a});
3306 $pbat = $self->ensure_path($dir);
3307 $self->delete_entry($m->{file_a}, $pbat);
3311 my ($self, $m, $q) = @_;
3312 my ($dir, $file) = split_path($m->{file_b});
3313 my $pbat = $self->ensure_path($dir);
3314 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3315 $pbat,$self->{r},$self->{pool});
3316 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3317 $self->chg_file($fbat, $m);
3318 $self->close_file($fbat,undef,$self->{pool});
3321 sub T { shift->M(@_) }
3323 sub change_file_prop {
3324 my ($self, $fbat, $pname, $pval) = @_;
3325 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3329 my ($self, $fbat, $m) = @_;
3330 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3331 $self->change_file_prop($fbat,'svn:executable','*');
3332 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3333 $self->change_file_prop($fbat,'svn:executable',undef);
3335 my $fh = IO::File->new_tmpfile or croak $!;
3336 if ($m->{mode_b} =~ /^120/) {
3337 print $fh 'link ' or croak $!;
3338 $self->change_file_prop($fbat,'svn:special','*');
3339 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3340 $self->change_file_prop($fbat,'svn:special',undef);
3342 defined(my $pid = fork) or croak $!;
3344 open STDOUT, '>&', $fh or croak $!;
3345 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3349 $fh->flush == 0 or croak $!;
3350 seek $fh, 0, 0 or croak $!;
3352 my $md5 = Digest::MD5->new;
3353 $md5->addfile($fh) or croak $!;
3354 seek $fh, 0, 0 or croak $!;
3356 my $exp = $md5->hexdigest;
3357 my $atd = $self->apply_textdelta($fbat, undef, $self->{pool});
3358 my $got = SVN::TxDelta::send_stream($fh, @$atd, $self->{pool});
3359 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3361 close $fh or croak $!;
3365 my ($self, $m, $q) = @_;
3366 my ($dir, $file) = split_path($m->{file_b});
3367 my $pbat = $self->ensure_path($dir);
3368 print "\tD\t$m->{file_b}\n" unless $q;
3369 $self->delete_entry($m->{file_b}, $pbat);
3374 my ($p,$bat) = ($self->{pool}, $self->{bat});
3375 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3376 $self->close_directory($bat->{$_}, $p);
3378 $self->SUPER::close_edit($p);
3384 $self->SUPER::abort_edit($self->{pool});
3385 $self->{pool}->clear;
3392 $svn_log hashref (as returned by svn_log_raw)
3394 fh => file handle of the log file,
3395 state => state of the log file parser (sep/msg/rev/msg_start...)
3398 $log_msg hashref as returned by next_log_entry($svn_log)
3400 msg => 'whitespace-formatted log entry
3401 ', # trailing newline is preserved
3402 revision => '8', # integer
3403 date => '2004-02-24T17:01:44.108345Z', # commit date
3404 author => 'committer name'
3408 @mods = array of diff-index line hashes, each element represents one line
3409 of diff-index output
3411 diff-index line ($m hash)
3413 mode_a => first column of diff-index output, no leading ':',
3414 mode_b => second column of diff-index output,
3415 sha1_b => sha1sum of the final blob,
3416 chg => change type [MCRADT],
3417 file_a => original file name of a file (iff chg is 'C' or 'R')
3418 file_b => new/current file name of a file (any chg)
3422 # retval of read_url_paths{,_all}();
3424 # repository root url
3425 'https://svn.musicpd.org' => {
3426 # repository path # GIT_SVN_ID
3427 'mpd/trunk' => 'trunk',
3428 'mpd/tags/0.11.5' => 'tags/0.11.5',
3433 I don't trust the each() function on unless I created %hash myself
3434 because the internal iterator may not have started at base.