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 sub fatal
(@
) { print STDERR
$@
; exit 1 }
25 # If SVN:: library support is added, please make the dependencies
26 # optional and preserve the capability to use the command-line client.
27 # use eval { require SVN::... } to make it lazy load
28 # We don't use any modules not in the standard Perl distribution:
31 use File
::Basename qw
/dirname basename/;
32 use File
::Path qw
/mkpath/;
33 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
35 use File
::Copy qw
/copy/;
36 use POSIX qw
/strftime/;
39 memoize
('revisions_eq');
40 memoize
('cmt_metadata');
41 memoize
('get_commit_time');
47 ! Please consider installing the SVN Perl libraries (version 1.1.0 or
48 ! newer). You will generally get better performance and fewer bugs,
50 ! 1) have a case-insensitive filesystem
51 ! 2) replace symlinks with files (and vice-versa) in commits
56 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB
};
58 nag_lib
() unless $_use_lib;
60 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
61 my $sha1 = qr/[a-f\d]{40}/;
62 my $sha1_short = qr/[a-f\d]{4,40}/;
63 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
64 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
65 $_repack, $_repack_nr, $_repack_flags, $_q,
66 $_message, $_file, $_follow_parent, $_no_metadata,
67 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
68 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
69 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
70 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive,
71 $_username, $_config_dir, $_no_auth_cache, $_xfer_delta);
72 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
73 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
74 my @repo_path_split_cache;
76 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
77 'branch|b=s' => \
@_branch_from,
78 'follow-parent|follow' => \
$_follow_parent,
79 'branch-all-refs|B' => \
$_branch_all_refs,
80 'authors-file|A=s' => \
$_authors,
81 'repack:i' => \
$_repack,
82 'no-metadata' => \
$_no_metadata,
84 'username=s' => \
$_username,
85 'config-dir=s' => \
$_config_dir,
86 'no-auth-cache' => \
$_no_auth_cache,
87 'ignore-nodate' => \
$_ignore_nodate,
88 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
90 my ($_trunk, $_tags, $_branches);
91 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
92 'tags|t=s' => \
$_tags,
93 'branches|b=s' => \
$_branches );
94 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
95 my %cmt_opts = ( 'edit|e' => \
$_edit,
97 'find-copies-harder' => \
$_find_copies_harder,
99 'copy-similarity|C=i'=> \
$_cp_similarity
103 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
104 { 'revision|r=s' => \
$_revision, %fc_opts } ],
105 init
=> [ \
&init
, "Initialize a repo for tracking" .
106 " (requires URL argument)",
108 commit
=> [ \
&commit
, "Commit git revisions to SVN",
109 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
110 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings",
111 { 'revision|r=i' => \
$_revision } ],
112 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
113 { 'no-ignore-externals' => \
$_no_ignore_ext,
114 'copy-remote|remote=s' => \
$_cp_remote,
115 'upgrade' => \
$_upgrade } ],
116 'graft-branches' => [ \
&graft_branches
,
117 'Detect merges/branches from already imported history',
118 { 'merge-rx|m' => \
@_opt_m,
119 'branch|b=s' => \
@_branch_from,
120 'branch-all-refs|B' => \
$_branch_all_refs,
121 'no-default-regex' => \
$_no_default_regex,
122 'no-graft-copy' => \
$_no_graft_copy } ],
123 'multi-init' => [ \
&multi_init
,
124 'Initialize multiple trees (like git-svnimport)',
125 { %multi_opts, %fc_opts } ],
126 'multi-fetch' => [ \
&multi_fetch
,
127 'Fetch multiple trees (like git-svnimport)',
129 'log' => [ \
&show_log
, 'Show commit logs',
130 { 'limit=i' => \
$_limit,
131 'revision|r=s' => \
$_revision,
132 'verbose|v' => \
$_verbose,
133 'incremental' => \
$_incremental,
134 'oneline' => \
$_oneline,
135 'show-commit' => \
$_show_commit,
136 'non-recursive' => \
$_non_recursive,
137 'authors-file|A=s' => \
$_authors,
139 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
140 { 'message|m=s' => \
$_message,
141 'file|F=s' => \
$_file,
142 'revision|r=s' => \
$_revision,
144 dcommit
=> [ \
&dcommit
, 'Commit several diffs to merge with upstream',
145 { 'merge|m|M' => \
$_merge,
146 'strategy|s=s' => \
$_strategy,
147 'dry-run|n' => \
$_dry_run,
152 for (my $i = 0; $i < @ARGV; $i++) {
153 if (defined $cmd{$ARGV[$i]}) {
160 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
162 read_repo_config
(\
%opts);
163 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
164 'version|V' => \
$_version,
165 'id|i=s' => \
$GIT_SVN);
166 exit 1 if (!$rv && $cmd ne 'log');
170 version
() if $_version;
171 usage
(1) unless defined $cmd;
173 load_authors
() if $_authors;
174 load_all_refs
() if $_branch_all_refs;
175 svn_compat_check
() unless $_use_lib;
176 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
177 $cmd{$cmd}->[0]->(@ARGV);
180 ####################### primary functions ######################
182 my $exit = shift || 0;
183 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
185 git
-svn
- bidirectional operations between a single Subversion tree
and git
186 Usage
: $0 <command
> [options
] [arguments
]\n
188 print $fd "Available commands:\n" unless $cmd;
190 foreach (sort keys %cmd) {
191 next if $cmd && $cmd ne $_;
192 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
193 foreach (keys %{$cmd{$_}->[2]}) {
194 # prints out arguments as they should be passed:
195 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
196 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
198 split /\|/,$_)," $x\n";
202 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
203 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
204 one git repository and want to keep them separate. See git-svn(1) for more
211 print "git-svn version $VERSION\n";
216 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
219 $SVN_URL = shift or undef;
222 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
224 check_upgrade_needed();
227 my $pid = open(my $rev_list,'-|');
228 defined $pid or croak $!;
230 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
233 while (<$rev_list>) {
236 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
237 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
238 next if (!@commit); # skip merges
239 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
240 if (!defined $rev || !$uuid) {
241 croak "Unable to extract revision or UUID from ",
242 "$c, $commit[$#commit]\n";
245 # if we merged or otherwise started elsewhere, this is
246 # how we break out of it
247 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
248 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
250 unless (defined $latest) {
251 if (!$SVN_URL && !$url) {
252 croak "SVN repository location required: $url\n";
259 revdb_set($REVDB, $rev, $c);
260 print "r$rev = $c\n";
261 $newest_rev = $rev if ($rev > $newest_rev);
263 close $rev_list or croak $?;
265 goto out if $_use_lib;
266 if (!chdir $SVN_WC) {
267 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
268 chdir $SVN_WC or croak $!;
272 defined $pid or croak $!;
274 my @svn_up = qw(svn up);
275 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
276 sys
(@svn_up,"-r$newest_rev");
277 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
279 exec('git-write-tree') or croak
$!;
286 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
287 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
293 my $url = shift or die "SVN repository location required " .
294 "as a command-line argument\n";
295 $url =~ s!/+$!!; # strip trailing slash
297 if (my $repo_path = shift) {
298 unless (-d
$repo_path) {
299 mkpath
([$repo_path]);
301 $GIT_DIR = $ENV{GIT_DIR
} = $repo_path . "/.git";
306 unless (-d
$GIT_DIR) {
307 my @init_db = ('git-init-db');
308 push @init_db, "--template=$_template" if defined $_template;
309 push @init_db, "--shared" if defined $_shared;
316 check_upgrade_needed
();
317 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
318 my $ret = $_use_lib ? fetch_lib
(@_) : fetch_cmd
(@_);
319 if ($ret->{commit
} && quiet_run
(qw(git-rev-parse --verify
320 refs/heads/master^0))) {
321 sys
(qw(git-update-ref refs/heads/master),$ret->{commit
});
328 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
329 unless ($_revision) {
330 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
332 push @log_args, "-r$_revision";
333 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
335 my $svn_log = svn_log_raw
(@log_args);
337 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
338 # don't need last_revision from grab_base_rev() because
339 # user could've specified a different revision to skip (they
340 # didn't want to import certain revisions into git for whatever
341 # reason, so trust $base->{revision} instead.
342 my (undef, $last_commit) = svn_grab_base_rev
();
343 unless (-d
$SVN_WC) {
344 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
345 chdir $SVN_WC or croak
$!;
347 $last_commit = git_commit
($base, @parents);
348 assert_tree
($last_commit);
350 chdir $SVN_WC or croak
$!;
352 # looks like a user manually cp'd and svn switch'ed
353 unless ($last_commit) {
354 sys
(qw
/svn revert -R ./);
355 assert_svn_wc_clean
($base->{revision
});
356 $last_commit = git_commit
($base, @parents);
357 assert_tree
($last_commit);
360 my @svn_up = qw(svn up);
361 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
363 while (my $log_msg = next_log_entry
($svn_log)) {
364 if ($last->{revision
} >= $log_msg->{revision
}) {
365 croak
"Out of order: last >= current: ",
366 "$last->{revision} >= $log_msg->{revision}\n";
368 # Revert is needed for cases like:
369 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
370 # I can't seem to reproduce something like that on a test...
371 sys
(qw
/svn revert -R ./);
372 assert_svn_wc_clean
($last->{revision
});
373 sys
(@svn_up,"-r$log_msg->{revision}");
374 $last_commit = git_commit
($log_msg, $last_commit, @parents);
377 close $svn_log->{fh
};
378 $last->{commit
} = $last_commit;
384 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
385 $SVN ||= libsvn_connect
($SVN_URL);
386 my ($last_rev, $last_commit) = svn_grab_base_rev
();
387 my ($base, $head) = libsvn_parse_revision
($last_rev);
389 return { revision
=> $last_rev, commit
=> $last_commit }
391 my $index = set_index
($GIT_SVN_INDEX);
393 # limit ourselves and also fork() since get_log won't release memory
394 # after processing a revision and SVN stuff seems to leak
396 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
398 if (defined $last_commit) {
399 unless (-e
$GIT_SVN_INDEX) {
400 sys
(qw
/git-read-tree/, $last_commit);
402 chomp (my $x = `git-write-tree`);
403 my ($y) = (`git-cat-file commit $last_commit`
404 =~ /^tree ($sha1)/m);
406 unlink $GIT_SVN_INDEX or croak
$!;
407 sys
(qw
/git-read-tree/, $last_commit);
409 chomp ($x = `git-write-tree`);
411 print STDERR
"trees ($last_commit) $y != $x\n",
412 "Something is seriously wrong...\n";
416 # fork, because using SVN::Pool with get_log() still doesn't
417 # seem to help enough to keep memory usage down.
418 defined(my $pid = fork) or croak
$!;
420 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
422 # Yes I'm perfectly aware that the fourth argument
423 # below is the limit revisions number. Unfortunately
424 # performance sucks with it enabled, so it's much
425 # faster to fetch revision ranges instead of relying
427 libsvn_get_log
(libsvn_dup_ra
($SVN), [''],
432 $log_msg = libsvn_fetch
(
434 $last_commit = git_commit
(
439 $log_msg = libsvn_new_tree
(@_);
440 $last_commit = git_commit
(
448 ($last_rev, $last_commit) = svn_grab_base_rev
();
449 last if ($max >= $head);
452 $max = $head if ($max > $head);
454 restore_index
($index);
455 return { revision
=> $last_rev, commit
=> $last_commit };
460 check_upgrade_needed
();
461 if ($_stdin || !@commits) {
462 print "Reading from stdin...\n";
465 if (/\b($sha1_short)\b/o) {
466 unshift @commits, $1;
471 foreach my $c (@commits) {
472 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
473 if (scalar @tmp == 1) {
475 } elsif (scalar @tmp > 1) {
476 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
478 die "Failed to rev-parse $c\n";
482 $_use_lib ? commit_lib
(@revs) : commit_cmd
(@revs);
483 print "Done committing ",scalar @revs," revisions to SVN\n";
489 chdir $SVN_WC or croak
"Unable to chdir $SVN_WC: $!\n";
490 my $info = svn_info
('.');
491 my $fetched = fetch
();
492 if ($info->{Revision
} != $fetched->{revision
}) {
493 print STDERR
"There are new revisions that were fetched ",
494 "and need to be merged (or acknowledged) ",
495 "before committing.\n";
498 $info = svn_info
('.');
501 foreach my $c (@revs) {
502 my $mods = svn_checkout_tree
($last, $c);
503 if (scalar @
$mods == 0) {
504 print "Skipping, no changes detected\n";
507 $last = svn_commit_tree
($last, $c);
513 my ($r_last, $cmt_last) = svn_grab_base_rev
();
514 defined $r_last or die "Must have an existing revision to commit\n";
515 my $fetched = fetch
();
516 if ($r_last != $fetched->{revision
}) {
517 print STDERR
"There are new revisions that were fetched ",
518 "and need to be merged (or acknowledged) ",
519 "before committing.\n",
520 "last rev: $r_last\n",
521 " current: $fetched->{revision}\n";
525 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
526 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
529 set_svn_commit_env
();
530 foreach my $c (@revs) {
531 my $log_msg = get_commit_message
($c, $commit_msg);
533 # fork for each commit because there's a memory leak I
534 # can't track down... (it's probably in the SVN code)
535 defined(my $pid = open my $fh, '-|') or croak
$!;
537 my $ed = SVN
::Git
::Editor
->new(
539 ra
=> libsvn_dup_ra
($SVN),
541 svn_path
=> $SVN->{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') {
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"));
590 foreach my $d (reverse @refs) {
591 if (quiet_run
('git-rev-parse','--verify',"$d~1") != 0) {
593 "has no parent commit, and therefore ",
594 "nothing to diff against.\n",
595 "You should be working from a repository ",
596 "originally created by git-svn\n";
598 unless (defined $last_rev) {
599 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
600 unless (defined $last_rev) {
601 die "Unable to extract revision information ",
602 "from commit $d~1\n";
606 print "diff-tree $d~1 $d\n";
608 if (my $r = commit_diff
("$d~1", $d, undef, $last_rev)) {
610 } # else: no changes, same $last_rev
615 my @diff = safe_qx
(qw
/git-diff-tree HEAD/, $gs);
618 @finish = qw
/rebase/;
619 push @finish, qw
/--merge/ if $_merge;
620 push @finish, "--strategy=$_strategy" if $_strategy;
621 print STDERR
"W: HEAD and $gs differ, using @finish:\n", @diff;
623 print "No changes between current HEAD and $gs\n",
624 "Hard resetting to the latest $gs\n";
625 @finish = qw
/reset --mixed/;
627 sys
('git', @finish, $gs);
631 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
632 $_use_lib ? show_ignore_lib
() : show_ignore_cmd
();
635 sub show_ignore_cmd
{
636 require File
::Find
or die $!;
637 if (defined $_revision) {
638 die "-r/--revision option doesn't work unless the Perl SVN ",
639 "libraries are used\n";
641 chdir $SVN_WC or croak
$!;
643 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
645 @
{$ign{$_}} = svn_propget_base
('svn:ignore', $_);
646 }}, no_chdir
=>1},'.');
649 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
651 foreach my $i (sort keys %ign) {
652 print "\n# ",$i,"\n";
653 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
657 sub show_ignore_lib
{
659 $SVN ||= libsvn_connect
($SVN_URL);
660 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
661 libsvn_traverse_ignore
(\
*STDOUT
, $SVN->{svn_path
}, $r);
665 my $gr_file = "$GIT_DIR/info/grafts";
666 my ($grafts, $comments) = read_grafts
($gr_file);
670 # temporarily disable our grafts file to make this idempotent
671 chomp($gr_sha1 = safe_qx
(qw
/git-hash-object -w/,$gr_file));
672 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
675 my $l_map = read_url_paths
();
676 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
677 unless ($_no_default_regex) {
678 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
679 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
680 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
682 foreach my $u (keys %$l_map) {
684 foreach my $p (keys %{$l_map->{$u}}) {
685 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
688 unless ($_no_graft_copy) {
690 graft_file_copy_lib
($grafts,$l_map,$u);
692 graft_file_copy_cmd
($grafts,$l_map,$u);
696 graft_tree_joins
($grafts);
698 write_grafts
($grafts, $comments, $gr_file);
699 unlink "$gr_file~$gr_sha1" if $gr_sha1;
706 $url =~ s
#/+$## if $url;
707 if ($_trunk !~ m
#^[a-z\+]+://#) {
708 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
710 print STDERR
"E: '$_trunk' is not a complete URL ",
711 "and a separate URL is not specified\n";
714 $_trunk = $url . $_trunk;
717 if ($GIT_SVN eq 'git-svn') {
719 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
722 unless (-d
$GIT_SVN_DIR) {
723 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
725 sys
('git-repo-config', 'svn.trunk', $_trunk);
727 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
728 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
732 # try to do trunk first, since branches/tags
733 # may be descended from it.
734 if (-e
"$GIT_DIR/svn/trunk/info/url") {
735 fetch_child_id
('trunk', @_);
737 rec_fetch
('', "$GIT_DIR/svn", @_);
743 my $r_last = -1; # prevent dupes
744 rload_authors
() if $_authors;
750 if (defined $_revision) {
751 if ($_revision =~ /^(\d+):(\d+)$/) {
752 ($r_min, $r_max) = ($1, $2);
753 } elsif ($_revision =~ /^\d+$/) {
754 $r_min = $r_max = $_revision;
756 print STDERR
"-r$_revision is not supported, use ",
757 "standard \'git log\' arguments instead\n";
762 my $pid = open(my $log,'-|');
763 defined $pid or croak
$!;
765 exec(git_svn_log_cmd
($r_min,$r_max), @args) or croak
$!;
771 if (/^commit ($sha1_short)/o) {
773 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
775 process_commit
($c, $r_min, $r_max, \
@k) or
780 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
781 get_author_info
($c, $1, $2, $3);
782 } elsif (/^(?:tree|parent|committer) /) {
784 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
785 push @
{$c->{raw
}}, $_;
786 } elsif (/^[ACRMDT]\t/) {
787 # we could add $SVN->{svn_path} here, but that requires
788 # remote access at the moment (repo_path_split)...
789 s
#^([ACRMDT])\t# $1 #;
790 push @
{$c->{changed
}}, $_;
793 push @
{$c->{diff
}}, $_;
795 push @
{$c->{diff
}}, $_;
796 } elsif (/^ (git-svn-id:.+)$/) {
797 ($c->{url
}, $c->{r
}, undef) = extract_metadata
($1);
802 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
804 process_commit
($c, $r_min, $r_max, \
@k);
810 process_commit
($_, $r_min, $r_max) foreach reverse @k;
814 print '-' x72
,"\n" unless $_incremental || $_oneline;
817 sub commit_diff_usage
{
818 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
824 print STDERR
"commit-diff must be used with SVN libraries\n";
827 my $ta = shift or commit_diff_usage
();
828 my $tb = shift or commit_diff_usage
();
829 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
830 print STDERR
"Needed URL or usable git-svn id command-line\n";
834 unless (defined $r) {
835 if (defined $_revision) {
838 die "-r|--revision is a required argument\n";
841 if (defined $_message && defined $_file) {
842 print STDERR
"Both --message/-m and --file/-F specified ",
843 "for the commit message.\n",
844 "I have no idea what you mean\n";
847 if (defined $_file) {
848 $_message = file_to_s
($_file);
850 $_message ||= get_commit_message
($tb,
851 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
853 $SVN ||= libsvn_connect
($SVN_URL);
855 $r = $SVN->get_latest_revnum;
856 } elsif ($r !~ /^\d+$/) {
857 die "revision argument: $r not understood by git-svn\n";
859 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
861 my $ed = SVN
::Git
::Editor
->new({ r
=> $r,
862 ra
=> libsvn_dup_ra
($SVN),
864 svn_path
=> $SVN->{svn_path
}
866 $SVN->get_commit_editor($_message,
868 $rev_committed = $_[0];
869 print "Committed $_[0]\n";
873 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
875 print "No changes\n$ta == $tb\n";
882 $_message = $_file = undef;
883 return $rev_committed;
886 ########################### utility functions #########################
890 return 1 if defined $c->{r
};
891 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
892 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
893 my @msg = safe_qx
(qw
/git-cat-file commit/, $c->{c
});
894 shift @msg while ($msg[0] ne "\n");
896 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
898 (undef, $c->{r
}, undef) = extract_metadata
(
899 (grep(/^git-svn-id: /, @msg))[-1]);
901 return defined $c->{r
};
904 sub git_svn_log_cmd
{
905 my ($r_min, $r_max) = @_;
906 my @cmd = (qw
/git
-log --abbrev
-commit
--pretty
=raw
907 --default/, "refs/remotes
/$GIT_SVN");
908 push @cmd, '-r' unless $_non_recursive;
909 push @cmd, qw/--raw --name-status/ if $_verbose;
910 return @cmd unless defined $r_max;
911 if ($r_max == $r_min) {
912 push @cmd, '--max-count=1';
913 if (my $c = revdb_get($REVDB, $r_max)) {
918 $c_max = revdb_get($REVDB, $r_max);
919 $c_min = revdb_get($REVDB, $r_min);
920 if (defined $c_min && defined $c_max) {
921 if ($r_max > $r_max) {
922 push @cmd, "$c_min..$c_max";
924 push @cmd, "$c_max..$c_min";
926 } elsif ($r_max > $r_min) {
937 print "Fetching
$id\n";
938 my $ref = "$GIT_DIR/refs/remotes
/$id";
939 defined(my $pid = open my $fh, '-|') or croak $!;
942 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
949 check_repack() if (/^r\d+ = $sha1/);
951 close $fh or croak $?;
955 my ($pfx, $p, @args) = @_;
957 foreach (sort <$p/*>) {
958 if (-r "$_/info/url
") {
959 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
960 my $id = $pfx . basename $_;
961 next if $id eq 'trunk';
962 fetch_child_id($id, @args);
969 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
974 sub complete_url_ls_init {
975 my ($url, $var, $switch, $pfx) = @_;
977 print STDERR "W
: $switch not specified
\n";
981 if ($var !~ m#^[a-z\+]+://#) {
982 $var = '/' . $var if ($var !~ m#^/#);
984 print STDERR "E
: '$var' is
not a complete URL
",
985 "and a separate URL is
not specified
\n";
990 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
991 : safe_qx(qw/svn ls --non-interactive/, $var));
993 defined(my $pid = fork) or croak $!;
995 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
997 if ($u !~ m!\Q$var\E/(.+)$!) {
998 print STDERR
"W: Unrecognized URL: $u\n";
999 die "This should never happen\n";
1001 # don't try to init already existing refs
1003 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
1005 unless (-d
$GIT_SVN_DIR) {
1006 print "init $u => $id\n";
1014 my ($n) = ($switch =~ /^--(\w+)/);
1015 sys
('git-repo-config', "svn.$n", $var);
1022 my @tmp = split m
#/#, $_;
1024 while (my $x = shift @tmp) {
1030 foreach (sort {length $b <=> length $a} keys %common) {
1031 if ($common{$_} == @
$paths) {
1038 # grafts set here are 'stronger' in that they're based on actual tree
1039 # matches, and won't be deleted from merge-base checking in write_grafts()
1040 sub graft_tree_joins
{
1042 map_tree_joins
() if (@_branch_from && !%tree_map);
1043 return unless %tree_map;
1047 defined(my $pid = open my $fh, '-|') or croak
$!;
1049 exec qw
/git-rev-list --pretty=raw/,
1050 "refs/remotes/$i" or croak
$!;
1053 next unless /^commit ($sha1)$/o;
1055 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1056 next unless $tree_map{$t};
1061 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1063 my ($s, $tz) = ($1, $2);
1064 if ($tz =~ s/^\+//) {
1065 $s += tz_to_s_offset
($tz);
1066 } elsif ($tz =~ s/^\-//) {
1067 $s -= tz_to_s_offset
($tz);
1070 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
1072 foreach my $p (@
{$tree_map{$t}}) {
1075 safe_qx
('git-merge-base', $c, $p)
1077 next unless ($@
|| $?
);
1079 # see if SVN says it's a relative
1080 my ($url_b, $r_b, $uuid_b) =
1082 next if (defined $url_b &&
1084 ($url_a eq $url_b) &&
1085 ($uuid_a eq $uuid_b));
1086 if ($uuid_a eq $uuid_b) {
1088 $grafts->{$c}->{$p} = 2;
1090 } elsif ($r_b > $r_a) {
1091 $grafts->{$p}->{$c} = 2;
1096 my $ct = get_commit_time
($p);
1098 $grafts->{$c}->{$p} = 2;
1099 } elsif ($ct > $s) {
1100 $grafts->{$p}->{$c} = 2;
1102 # what should we do when $ct == $s ?
1105 close $fh or croak
$?
;
1109 # this isn't funky-filename safe, but good enough for now...
1110 sub graft_file_copy_cmd
{
1111 my ($grafts, $l_map, $u) = @_;
1112 my $paths = $l_map->{$u};
1113 my $pfx = common_prefix
([keys %$paths]);
1114 $SVN_URL ||= $u.$pfx;
1115 my $pid = open my $fh, '-|';
1116 defined $pid or croak
$!;
1118 my @exec = qw
/svn log -v/;
1119 push @exec, "-r$_revision" if defined $_revision;
1120 exec @exec, $u.$pfx or croak
$!;
1122 my ($r, $mp) = (undef, undef);
1127 } elsif (/^r(\d+) \| /) {
1128 $r = $1 unless defined $r;
1129 } elsif (/^Changed paths:/) {
1131 } elsif ($mp && m
#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1132 my ($p1, $p0, $r0) = ($1, $2, $3);
1133 my $c = find_graft_path_commit
($paths, $p1, $r);
1135 find_graft_path_parents
($grafts, $paths, $c, $p0, $r0);
1140 sub graft_file_copy_lib
{
1141 my ($grafts, $l_map, $u) = @_;
1142 my $tree_paths = $l_map->{$u};
1143 my $pfx = common_prefix
([keys %$tree_paths]);
1144 my ($repo, $path) = repo_path_split
($u.$pfx);
1145 $SVN = libsvn_connect
($repo);
1147 my ($base, $head) = libsvn_parse_revision
();
1149 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1150 my $eh = $SVN::Error
::handler
;
1151 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1153 my $pool = SVN
::Pool
->new;
1154 libsvn_get_log
(libsvn_dup_ra
($SVN), [$path],
1155 $min, $max, 0, 1, 1,
1157 libsvn_graft_file_copies
($grafts, $tree_paths,
1161 last if ($max >= $head);
1164 $max = $head if ($max > $head);
1166 $SVN::Error
::handler
= $eh;
1169 sub process_merge_msg_matches
{
1170 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1171 my (@strong, @weak);
1172 foreach (@matches) {
1173 # merging with ourselves is not interesting
1175 if ($l_map->{$u}->{$_}) {
1181 foreach my $w (@weak) {
1183 # no exact match, use branch name as regexp.
1184 my $re = qr/\Q$w\E/i;
1185 foreach (keys %{$l_map->{$u}}) {
1187 push @strong, $l_map->{$u}->{$_};
1194 foreach (keys %{$l_map->{$u}}) {
1196 push @strong, $l_map->{$u}->{$_};
1201 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1202 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1203 unless (defined $rev) {
1204 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1205 \@
(?
:[a
-f\d\
-]+)/xsm
);
1206 return unless defined $rev;
1208 foreach my $m (@strong) {
1209 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1210 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1214 sub graft_merge_msg
{
1215 my ($grafts, $l_map, $u, $p, @re) = @_;
1217 my $x = $l_map->{$u}->{$p};
1218 my $rl = rev_list_raw
($x);
1219 while (my $c = next_rev_list_entry
($rl)) {
1220 foreach my $re (@re) {
1221 my (@br) = ($c->{m
} =~ /$re/g);
1223 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1229 return if $SVN_UUID;
1231 my $pool = SVN
::Pool
->new;
1232 $SVN_UUID = $SVN->get_uuid($pool);
1235 my $info = shift || svn_info
('.');
1236 $SVN_UUID = $info->{'Repository UUID'} or
1237 croak
"Repository UUID unreadable\n";
1243 defined $pid or croak
$!;
1245 open my $null, '>', '/dev/null' or croak
$!;
1246 open STDERR
, '>&', $null or croak
$!;
1247 open STDOUT
, '>&', $null or croak
$!;
1248 exec @_ or croak
$!;
1254 sub repo_path_split
{
1255 my $full_url = shift;
1256 $full_url =~ s
#/+$##;
1258 foreach (@repo_path_split_cache) {
1259 if ($full_url =~ s
#$_##) {
1261 $full_url =~ s
#^/+##;
1262 return ($u, $full_url);
1266 my $tmp = libsvn_connect
($full_url);
1267 return ($tmp->{repos_root
}, $tmp->{svn_path
});
1269 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1271 my @paths = split(m
#/+#, $path);
1272 while (quiet_run
(qw
/svn ls --non-interactive/, $url)) {
1273 my $n = shift @paths || last;
1276 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1277 $path = join('/',@paths);
1278 return ($url, $path);
1283 defined $SVN_URL or croak
"SVN repository location required\n";
1284 unless (-d
$GIT_DIR) {
1285 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1287 mkpath
([$GIT_SVN_DIR]);
1288 mkpath
(["$GIT_SVN_DIR/info"]);
1289 open my $fh, '>>',$REVDB or croak
$!;
1291 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1295 sub assert_svn_wc_clean
{
1296 return if $_use_lib;
1298 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1299 my $lcr = svn_info
('.')->{'Last Changed Rev'};
1300 if ($svn_rev != $lcr) {
1301 print STDERR
"Checking for copy-tree ... ";
1302 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
1303 "-r$lcr:$svn_rev")));
1305 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
1307 print STDERR
"OK!\n";
1310 my @status = grep(!/^Performing status on external/,(`svn status`));
1311 @status = grep(!/^\s*$/,@status);
1312 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1313 if (scalar @status) {
1314 print STDERR
"Tree ($SVN_WC) is not clean:\n";
1315 print STDERR
$_ foreach @status;
1320 sub get_tree_from_treeish
{
1322 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1323 chomp(my $type = `git-cat-file -t $treeish`);
1325 while ($type eq 'tag') {
1326 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1328 if ($type eq 'commit') {
1329 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1330 ($expected) = ($expected =~ /^tree ($sha1)$/);
1331 die "Unable to get tree from $treeish\n" unless $expected;
1332 } elsif ($type eq 'tree') {
1333 $expected = $treeish;
1335 die "$treeish is a $type, expected tree, tag or commit\n";
1341 return if $_use_lib;
1343 my $expected = get_tree_from_treeish
($treeish);
1345 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1347 unlink $tmpindex or croak
$!;
1349 my $old_index = set_index
($tmpindex);
1351 chomp(my $tree = `git-write-tree`);
1352 restore_index
($old_index);
1353 if ($tree ne $expected) {
1354 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
1359 sub parse_diff_tree
{
1360 my $diff_fh = shift;
1364 while (<$diff_fh>) {
1365 chomp $_; # this gets rid of the trailing "\0"
1366 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1367 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1368 push @mods, { mode_a
=> $1, mode_b
=> $2,
1369 sha1_b
=> $3, chg
=> $4 };
1370 if ($4 =~ /^(?:C|R)$/) {
1375 } elsif ($state eq 'file_a') {
1376 my $x = $mods[$#mods] or croak
"Empty array\n";
1377 if ($x->{chg
} !~ /^(?:C|R)$/) {
1378 croak
"Error parsing $_, $x->{chg}\n";
1382 } elsif ($state eq 'file_b') {
1383 my $x = $mods[$#mods] or croak
"Empty array\n";
1384 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1385 croak
"Error parsing $_, $x->{chg}\n";
1387 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1388 croak
"Error parsing $_, $x->{chg}\n";
1393 croak
"Error parsing $_\n";
1396 close $diff_fh or croak
$?
;
1401 sub svn_check_prop_executable
{
1403 return if -l
$m->{file_b
};
1404 if ($m->{mode_b
} =~ /755$/) {
1405 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
1406 if ($m->{mode_a
} !~ /755$/) {
1407 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
1409 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
1410 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1411 sys
(qw(svn propdel svn:executable), $m->{file_b
});
1412 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
1413 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
1417 sub svn_ensure_parent_path
{
1418 my $dir_b = dirname
(shift);
1419 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
1420 mkpath
([$dir_b]) unless (-d
$dir_b);
1421 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
1424 sub precommit_check
{
1426 my (%rm_file, %rmdir_check, %added_check);
1428 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
1429 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1430 if ($m->{chg
} eq 'R') {
1431 if (-d
$m->{file_b
}) {
1432 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1434 # dir/$file => dir/file/$file
1435 my $dirname = dirname
($m->{file_b
});
1436 while ($dirname ne File
::Spec
->curdir) {
1437 if ($dirname ne $m->{file_a
}) {
1438 $dirname = dirname
($dirname);
1441 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
1443 # baz/zzz => baz (baz is a file)
1444 $dirname = dirname
($m->{file_a
});
1445 while ($dirname ne File
::Spec
->curdir) {
1446 if ($dirname ne $m->{file_b
}) {
1447 $dirname = dirname
($dirname);
1450 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1453 if ($m->{chg
} =~ /^(D|R)$/) {
1454 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
1455 $rm_file{ $m->{$t} } = 1;
1456 my $dirname = dirname
( $m->{$t} );
1457 my $basename = basename
( $m->{$t} );
1458 $rmdir_check{$dirname}->{$basename} = 1;
1459 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
1460 if (-d
$m->{file_b
}) {
1461 err_dir_to_file
($m->{file_b
});
1463 my $dirname = dirname
( $m->{file_b
} );
1464 my $basename = basename
( $m->{file_b
} );
1465 $added_check{$dirname}->{$basename} = 1;
1466 while ($dirname ne File
::Spec
->curdir) {
1467 if ($rm_file{$dirname}) {
1468 err_file_to_dir
($m->{file_b
});
1470 $dirname = dirname
$dirname;
1474 return (\
%rmdir_check, \
%added_check);
1476 sub err_dir_to_file
{
1478 print STDERR
"Node change from directory to file ",
1479 "is not supported by Subversion: ",$file,"\n";
1482 sub err_file_to_dir
{
1484 print STDERR
"Node change from file to directory ",
1485 "is not supported by Subversion: ",$file,"\n";
1492 my ($from, $treeish) = @_;
1494 print "diff-tree $from $treeish\n";
1495 my $pid = open my $diff_fh, '-|';
1496 defined $pid or croak
$!;
1498 my @diff_tree = qw(git-diff-tree -z -r);
1499 if ($_cp_similarity) {
1500 push @diff_tree, "-C$_cp_similarity";
1502 push @diff_tree, '-C';
1504 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1505 push @diff_tree, "-l$_l" if defined $_l;
1506 exec(@diff_tree, $from, $treeish) or croak
$!;
1508 return parse_diff_tree
($diff_fh);
1511 sub svn_checkout_tree
{
1512 my ($from, $treeish) = @_;
1513 my $mods = get_diff
($from->{commit
}, $treeish);
1514 return $mods unless (scalar @
$mods);
1515 my ($rm, $add) = precommit_check
($mods);
1517 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1518 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1519 if ($m->{chg
} eq 'C') {
1520 svn_ensure_parent_path
( $m->{file_b
} );
1521 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
1522 apply_mod_line_blob
($m);
1523 svn_check_prop_executable
($m);
1524 } elsif ($m->{chg
} eq 'D') {
1525 sys
(qw(svn rm --force), $m->{file_b
});
1526 } elsif ($m->{chg
} eq 'R') {
1527 svn_ensure_parent_path
( $m->{file_b
} );
1528 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
1529 apply_mod_line_blob
($m);
1530 svn_check_prop_executable
($m);
1531 } elsif ($m->{chg
} eq 'M') {
1532 apply_mod_line_blob
($m);
1533 svn_check_prop_executable
($m);
1534 } elsif ($m->{chg
} eq 'T') {
1535 svn_check_prop_executable
($m);
1536 apply_mod_line_blob
($m);
1537 if ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
1538 sys
(qw(svn propdel svn:special), $m->{file_b
});
1540 sys
(qw(svn propset svn:special *),$m->{file_b
});
1542 } elsif ($m->{chg
} eq 'A') {
1543 svn_ensure_parent_path
( $m->{file_b
} );
1544 apply_mod_line_blob
($m);
1545 sys
(qw(svn add), $m->{file_b
});
1546 svn_check_prop_executable
($m);
1548 croak
"Invalid chg: $m->{chg}\n";
1552 assert_tree
($treeish);
1553 if ($_rmdir) { # remove empty directories
1554 handle_rmdir
($rm, $add);
1556 assert_tree
($treeish);
1560 sub libsvn_checkout_tree
{
1561 my ($from, $treeish, $ed) = @_;
1562 my $mods = get_diff
($from, $treeish);
1563 return $mods unless (scalar @
$mods);
1564 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1565 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1567 if (defined $o{$f}) {
1570 croak
"Invalid change type: $f\n";
1573 $ed->rmdirs($_q) if $_rmdir;
1577 # svn ls doesn't work with respect to the current working tree, but what's
1578 # in the repository. There's not even an option for it... *sigh*
1579 # (added files don't show up and removed files remain in the ls listing)
1580 sub svn_ls_current
{
1581 my ($dir, $rm, $add) = @_;
1582 chomp(my @ls = safe_qx
('svn','ls',$dir));
1585 s
#/$##; # trailing slashes are evil
1586 push @ret, $_ unless $rm->{$dir}->{$_};
1588 if (exists $add->{$dir}) {
1589 push @ret, keys %{$add->{$dir}};
1595 my ($rm, $add) = @_;
1597 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1598 my $ls = svn_ls_current
($dir, $rm, $add);
1599 next if (scalar @
$ls);
1600 sys
(qw(svn rm --force),$dir);
1602 my $dn = dirname
$dir;
1603 $rm->{ $dn }->{ basename
$dir } = 1;
1604 $ls = svn_ls_current
($dn, $rm, $add);
1605 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
1606 sys
(qw(svn rm --force),$dn);
1607 $dir = basename
$dn;
1609 $rm->{ $dn }->{ $dir } = 1;
1610 $ls = svn_ls_current
($dn, $rm, $add);
1615 sub get_commit_message
{
1616 my ($commit, $commit_msg) = (@_);
1617 my %log_msg = ( msg
=> '' );
1618 open my $msg, '>', $commit_msg or croak
$!;
1620 chomp(my $type = `git-cat-file -t $commit`);
1621 if ($type eq 'commit' || $type eq 'tag') {
1622 my $pid = open my $msg_fh, '-|';
1623 defined $pid or croak
$!;
1626 exec('git-cat-file', $type, $commit) or croak
$!;
1631 $in_msg = 1 if (/^\s*$/);
1632 } elsif (/^git-svn-id: /) {
1633 # skip this, we regenerate the correct one
1634 # on re-fetch anyways
1636 print $msg $_ or croak
$!;
1639 close $msg_fh or croak
$?
;
1641 close $msg or croak
$!;
1643 if ($_edit || ($type eq 'tree')) {
1644 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1645 system($editor, $commit_msg);
1648 # file_to_s removes all trailing newlines, so just use chomp() here:
1649 open $msg, '<', $commit_msg or croak
$!;
1650 { local $/; chomp($log_msg{msg
} = <$msg>); }
1651 close $msg or croak
$!;
1656 sub set_svn_commit_env
{
1657 if (defined $LC_ALL) {
1658 $ENV{LC_ALL
} = $LC_ALL;
1660 delete $ENV{LC_ALL
};
1664 sub svn_commit_tree
{
1665 my ($last, $commit) = @_;
1666 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1667 my $log_msg = get_commit_message
($commit, $commit_msg);
1668 my ($oneline) = ($log_msg->{msg
} =~ /([^\n\r]+)/);
1669 print "Committing $commit: $oneline\n";
1671 set_svn_commit_env
();
1672 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
1675 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1676 if (!defined $committed) {
1677 my $out = join("\n",@ci_output);
1678 print STDERR
"W: Trouble parsing \`svn commit' output:\n\n",
1679 $out, "\n\nAssuming English locale...";
1680 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1681 defined $committed or die " FAILED!\n",
1682 "Commit output failed to parse committed revision!\n",
1683 print STDERR
" OK\n";
1686 my @svn_up = qw(svn up);
1687 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1688 if ($_optimize_commits && ($committed == ($last->{revision
} + 1))) {
1689 push @svn_up, "-r$committed";
1691 my $info = svn_info
('.');
1692 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1693 if ($info->{'Last Changed Rev'} != $committed) {
1694 croak
"$info->{'Last Changed Rev'} != $committed\n"
1696 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1697 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1698 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1699 or croak
"Failed to parse date: $date\n";
1700 $log_msg->{date
} = "$tz $Y-$m-$d $H:$M:$S";
1701 $log_msg->{author
} = $info->{'Last Changed Author'};
1702 $log_msg->{revision
} = $committed;
1703 $log_msg->{msg
} .= "\n";
1704 $log_msg->{parents
} = [ $last->{commit
} ];
1705 $log_msg->{commit
} = git_commit
($log_msg, $commit);
1708 # resync immediately
1709 push @svn_up, "-r$last->{revision}";
1711 return fetch
("$committed=$commit");
1716 my $pid = open my $fh, '-|';
1717 defined $pid or croak
$!;
1719 exec(qw
/git-rev-list --pretty=raw/, @args) or croak
$!;
1721 return { fh
=> $fh, t
=> { } };
1724 sub next_rev_list_entry
{
1729 if (/^commit ($sha1)$/o) {
1731 $rl->{t
} = { c
=> $1 };
1736 } elsif (/^parent ($sha1)$/o) {
1743 return ($x != $rl->{t
}) ?
$x : undef;
1746 # read the entire log into a temporary file (which is removed ASAP)
1747 # and store the file handle + parser state
1749 my (@log_args) = @_;
1750 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
1752 defined $pid or croak
$!;
1754 open STDOUT
, '>&', $log_fh or croak
$!;
1755 exec (qw(svn log), @log_args) or croak
$!
1759 seek $log_fh, 0, 0 or croak
$!;
1760 return { state => 'sep', fh
=> $log_fh };
1763 sub next_log_entry
{
1764 my $log = shift; # retval of svn_log_raw()
1766 my $fh = $log->{fh
};
1771 if ($log->{state} eq 'msg') {
1772 if ($ret->{lines
}) {
1773 $ret->{msg
} .= $_."\n";
1774 unless(--$ret->{lines
}) {
1775 $log->{state} = 'sep';
1778 croak
"Log parse error at: $_\n",
1784 if ($log->{state} ne 'sep') {
1785 croak
"Log parse error at: $_\n",
1786 "state: $log->{state}\n",
1790 $log->{state} = 'rev';
1792 # if we have an empty log message, put something there:
1794 $ret->{msg
} ||= "\n";
1795 delete $ret->{lines
};
1800 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1802 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1803 ($lines) = ($lines =~ /(\d+)/);
1804 $date = '1970-01-01 00:00:00 +0000'
1805 if ($_ignore_nodate && $date eq '(no date)');
1806 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1807 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1808 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1809 or croak
"Failed to parse date: $date\n";
1810 $ret = { revision
=> $rev,
1811 date
=> "$tz $Y-$m-$d $H:$M:$S",
1815 if (defined $_authors && ! defined $users{$author}) {
1816 die "Author: $author not defined in ",
1819 $log->{state} = 'msg_start';
1822 # skip the first blank line of the message:
1823 if ($log->{state} eq 'msg_start' && /^$/) {
1824 $log->{state} = 'msg';
1825 } elsif ($log->{state} eq 'msg') {
1826 if ($ret->{lines
}) {
1827 $ret->{msg
} .= $_."\n";
1828 unless (--$ret->{lines
}) {
1829 $log->{state} = 'sep';
1832 croak
"Log parse error at: $_\n",
1833 $ret->{revision
},"\n";
1841 my $url = shift || $SVN_URL;
1843 my $pid = open my $info_fh, '-|';
1844 defined $pid or croak
$!;
1847 exec(qw(svn info),$url) or croak
$!;
1851 # only single-lines seem to exist in svn info output
1852 while (<$info_fh>) {
1854 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
1856 push @
{$ret->{-order
}}, $1;
1859 close $info_fh or croak
$?
;
1863 sub sys
{ system(@_) == 0 or croak
$?
}
1865 sub do_update_index
{
1866 my ($z_cmd, $cmd, $no_text_base) = @_;
1868 my $z = open my $p, '-|';
1869 defined $z or croak
$!;
1870 unless ($z) { exec @
$z_cmd or croak
$! }
1872 my $pid = open my $ui, '|-';
1873 defined $pid or croak
$!;
1875 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
1878 while (my $x = <$p>) {
1880 if (!$no_text_base && lstat $x && ! -l _
&&
1881 svn_propget_base
('svn:keywords', $x)) {
1882 my $mode = -x _ ?
0755 : 0644;
1883 my ($v,$d,$f) = File
::Spec
->splitpath($x);
1884 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
1885 'text-base',"$f.svn-base");
1888 $tb = File
::Spec
->catfile($d, '.svn',
1889 'text-base',"$f.svn-base");
1893 unlink $x or croak
$!;
1895 chmod(($mode &~ umask), $x) or croak
$!;
1896 utime $s[8], $s[9], $x;
1900 close $ui or croak
$?
;
1904 return if $_use_lib;
1906 if (!-f
"$GIT_SVN_DIR/info/exclude") {
1907 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak
$!;
1908 print $fd '.svn',"\n";
1909 close $fd or croak
$!;
1911 my $no_text_base = shift;
1912 do_update_index
([qw
/git-diff-files --name-only -z/],
1915 do_update_index
([qw
/git-ls-files -z --others/,
1916 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1922 my ($str, $file, $mode) = @_;
1923 open my $fd,'>',$file or croak
$!;
1924 print $fd $str,"\n" or croak
$!;
1925 close $fd or croak
$!;
1926 chmod ($mode &~ umask, $file) if (defined $mode);
1931 open my $fd,'<',$file or croak
"$!: file: $file\n";
1934 close $fd or croak
$!;
1939 sub assert_revision_unknown
{
1941 if (my $c = revdb_get
($REVDB, $r)) {
1942 croak
"$r = $c already exists! Why are we refetching it?";
1948 my @x = safe_qx
('git-cat-file','commit',$x);
1949 my @y = safe_qx
('git-cat-file','commit',$y);
1950 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1951 || $y[0] !~ /^tree $sha1\n$/) {
1952 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
1959 my ($log_msg, @parents) = @_;
1960 assert_revision_unknown
($log_msg->{revision
});
1961 map_tree_joins
() if (@_branch_from && !%tree_map);
1963 my (@tmp_parents, @exec_parents, %seen_parent);
1964 if (my $lparents = $log_msg->{parents
}) {
1965 @tmp_parents = @
$lparents
1967 # commit parents can be conditionally bound to a particular
1968 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1969 foreach my $p (@parents) {
1970 next unless defined $p;
1971 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1972 if ($1 == $log_msg->{revision
}) {
1973 push @tmp_parents, $2;
1976 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1979 my $tree = $log_msg->{tree
};
1980 if (!defined $tree) {
1981 my $index = set_index
($GIT_SVN_INDEX);
1983 chomp($tree = `git-write-tree`);
1985 restore_index
($index);
1988 # just in case we clobber the existing ref, we still want that ref
1990 if (my $cur = eval { file_to_s
("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1991 push @tmp_parents, $cur;
1994 if (exists $tree_map{$tree}) {
1995 foreach my $p (@
{$tree_map{$tree}}) {
1997 foreach (@tmp_parents) {
1998 # see if a common parent is found
2000 safe_qx
('git-merge-base', $_, $p)
2007 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
2008 next if (($SVN_UUID eq $uuid_p) &&
2009 ($log_msg->{revision
} > $r_p));
2010 next if (defined $url_p && defined $SVN_URL &&
2011 ($SVN_UUID eq $uuid_p) &&
2012 ($url_p eq $SVN_URL));
2013 push @tmp_parents, $p;
2016 foreach (@tmp_parents) {
2017 next if $seen_parent{$_};
2018 $seen_parent{$_} = 1;
2019 push @exec_parents, $_;
2020 # MAXPARENT is defined to 16 in commit-tree.c:
2021 last if @exec_parents > 16;
2024 set_commit_env
($log_msg);
2025 my @exec = ('git-commit-tree', $tree);
2026 push @exec, '-p', $_ foreach @exec_parents;
2027 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2029 print $msg_fh $log_msg->{msg
} or croak
$!;
2030 unless ($_no_metadata) {
2031 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
2032 " $SVN_UUID\n" or croak
$!;
2034 $msg_fh->flush == 0 or croak
$!;
2035 close $msg_fh or croak
$!;
2036 chomp(my $commit = do { local $/; <$out_fh> });
2037 close $out_fh or croak
$!;
2040 if ($commit !~ /^$sha1$/o) {
2041 die "Failed to commit, invalid sha1: $commit\n";
2043 sys
('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2044 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
2046 # this output is read via pipe, do not change:
2047 print "r$log_msg->{revision} = $commit\n";
2053 if ($_repack && (--$_repack_nr == 0)) {
2054 $_repack_nr = $_repack;
2055 sys
("git repack $_repack_flags");
2059 sub set_commit_env
{
2061 my $author = $log_msg->{author
};
2062 if (!defined $author || length $author == 0) {
2063 $author = '(no author)';
2065 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
2066 : ($author,"$author\@$SVN_UUID");
2067 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
2068 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
2069 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
2072 sub apply_mod_line_blob
{
2074 if ($m->{mode_b
} =~ /^120/) {
2075 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
2077 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
2081 sub blob_to_symlink
{
2082 my ($blob, $link) = @_;
2083 defined $link or croak
"\$link not defined!\n";
2084 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2085 if (-l
$link || -f _
) {
2086 unlink $link or croak
$!;
2089 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2090 symlink $dest, $link or croak
$!;
2094 my ($blob, $file) = @_;
2095 defined $file or croak
"\$file not defined!\n";
2096 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2097 if (-l
$file || -f _
) {
2098 unlink $file or croak
$!;
2101 open my $blob_fh, '>', $file or croak
"$!: $file\n";
2103 defined $pid or croak
$!;
2106 open STDOUT
, '>&', $blob_fh or croak
$!;
2107 exec('git-cat-file','blob',$blob) or croak
$!;
2112 close $blob_fh or croak
$!;
2116 my $pid = open my $child, '-|';
2117 defined $pid or croak
$!;
2119 exec(@_) or croak
$!;
2121 my @ret = (<$child>);
2122 close $child or croak
$?
;
2123 die $?
if $?
; # just in case close didn't error out
2124 return wantarray ?
@ret : join('',@ret);
2127 sub svn_compat_check
{
2128 if ($_follow_parent) {
2129 print STDERR
'E: --follow-parent functionality is only ',
2130 "available when SVN libraries are used\n";
2133 my @co_help = safe_qx
(qw(svn co -h));
2134 unless (grep /ignore-externals/,@co_help) {
2135 print STDERR
"W: Installed svn version does not support ",
2136 "--ignore-externals\n";
2137 $_no_ignore_ext = 1;
2139 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2140 $_svn_co_url_revs = 1;
2142 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2143 $_svn_pg_peg_revs = 1;
2146 # I really, really hope nobody hits this...
2147 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
2149 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
2151 Lets hope the directory you
're tracking is not a branch or tag
2152 and was never moved within the repository...
2158 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
2159 # (and they won't honor URL@<rev> without -r<rev>, too!)
2160 sub svn_cmd_checkout
{
2161 my ($url, $rev, $dir) = @_;
2162 my @cmd = ('svn','co', "-r$rev");
2163 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2164 $url .= "\@$rev" if $_svn_co_url_revs;
2165 sys
(@cmd, $url, $dir);
2168 sub check_upgrade_needed
{
2170 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2171 open my $fh, '>>',$REVDB or croak
$!;
2175 my $pid = open my $child, '-|';
2176 defined $pid or croak
$!;
2179 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$!;
2181 my @ret = (<$child>);
2182 close $child or croak
$?
;
2183 die $?
if $?
; # just in case close didn't error out
2184 return wantarray ?
@ret : join('',@ret);
2187 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
2189 print STDERR
"Please run: $0 rebuild --upgrade\n";
2194 # fills %tree_map with a reverse mapping of trees to commits. Useful
2195 # for finding parents to commit on.
2196 sub map_tree_joins
{
2198 foreach my $br (@_branch_from) {
2199 my $pid = open my $pipe, '-|';
2200 defined $pid or croak
$!;
2202 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2206 if (/^commit ($sha1)$/o) {
2209 # if we've seen a commit,
2210 # we've seen its parents
2211 last if $seen{$commit};
2212 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2213 unless (defined $tree) {
2214 die "Failed to parse commit $commit\n";
2216 push @
{$tree_map{$tree}}, $commit;
2220 close $pipe; # we could be breaking the pipe early
2225 if (@_branch_from) {
2226 print STDERR
'--branch|-b parameters are ignored when ',
2227 "--branch-all-refs|-B is passed\n";
2230 # don't worry about rev-list on non-commit objects/tags,
2231 # it shouldn't blow up if a ref is a blob or tree...
2232 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2235 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2237 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2238 while (<$authors>) {
2240 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2241 my ($user, $name, $email) = ($1, $2, $3);
2242 $users{$user} = [$name, $email];
2244 close $authors or croak
$!;
2248 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2249 while (<$authors>) {
2251 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2252 my ($user, $name, $email) = ($1, $2, $3);
2253 $rusers{"$name <$email>"} = $user;
2255 close $authors or croak
$!;
2258 sub svn_propget_base
{
2260 $f .= '@BASE' if $_svn_pg_peg_revs;
2261 return safe_qx
(qw
/svn propget/, $p, $f);
2266 foreach (`git-rev-parse --symbolic --all`) {
2267 next unless s
#^refs/remotes/##;
2269 next unless -f
"$GIT_DIR/svn/$_/info/url";
2277 defined(my $pid = fork) or croak
$!;
2279 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2281 exit 0 if -r
$REVDB;
2282 print "Upgrading svn => git mapping...\n";
2283 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2284 open my $fh, '>>',$REVDB or croak
$!;
2287 print "Done upgrading. You may now delete the ",
2288 "deprecated $GIT_SVN_DIR/revs directory\n";
2296 sub migration_check
{
2297 migrate_revdb
() unless (-e
$REVDB);
2298 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
2299 print "Upgrading repository...\n";
2300 unless (-d
"$GIT_DIR/svn") {
2301 mkdir "$GIT_DIR/svn" or croak
$!;
2303 print "Data from a previous version of git-svn exists, but\n\t",
2304 "$GIT_SVN_DIR\n\t(required for this version ",
2305 "($VERSION) of git-svn) does not.\n";
2307 foreach my $x (`git-rev-parse --symbolic --all`) {
2308 next unless $x =~ s
#^refs/remotes/##;
2310 next unless -f
"$GIT_DIR/$x/info/url";
2311 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
2313 my $dn = dirname
("$GIT_DIR/svn/$x");
2314 mkpath
([$dn]) unless -d
$dn;
2315 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
2317 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
2318 print "Done upgrading.\n";
2321 sub find_rev_before
{
2322 my ($r, $id, $eq_ok) = @_;
2323 my $f = "$GIT_DIR/svn/$id/.rev_db";
2324 return (undef,undef) unless -r
$f;
2327 if (my $c = revdb_get
($f, $r)) {
2332 return (undef, undef);
2336 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
2337 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2338 $REVDB = "$GIT_SVN_DIR/.rev_db";
2339 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2341 $SVN_WC = "$GIT_SVN_DIR/tree";
2345 # convert GetOpt::Long specs for use by git-repo-config
2346 sub read_repo_config
{
2347 return unless -d
$GIT_DIR;
2349 foreach my $o (keys %$opts) {
2350 my $v = $opts->{$o};
2351 my ($key) = ($o =~ /^([a-z\-]+)/);
2353 my $arg = 'git-repo-config';
2354 $arg .= ' --int' if ($o =~ /[:=]i$/);
2355 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2356 if (ref $v eq 'ARRAY') {
2357 chomp(my @tmp = `$arg --get-all svn.$key`);
2360 chomp(my $tmp = `$arg --get svn.$key`);
2361 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2368 sub set_default_vals
{
2369 if (defined $_repack) {
2370 $_repack = 1000 if ($_repack <= 0);
2371 $_repack_nr = $_repack;
2372 $_repack_flags ||= '-d';
2377 my $gr_file = shift;
2378 my ($grafts, $comments) = ({}, {});
2379 if (open my $fh, '<', $gr_file) {
2382 if (/^($sha1)\s+/) {
2385 @
{$comments->{$c}} = @tmp;
2388 foreach my $p (split /\s+/, $_) {
2389 $grafts->{$c}->{$p} = 1;
2395 close $fh or croak
$!;
2396 @
{$comments->{'END'}} = @tmp if @tmp;
2398 return ($grafts, $comments);
2402 my ($grafts, $comments, $gr_file) = @_;
2404 open my $fh, '>', $gr_file or croak
$!;
2405 foreach my $c (sort keys %$grafts) {
2406 if ($comments->{$c}) {
2407 print $fh $_ foreach @
{$comments->{$c}};
2409 my $p = $grafts->{$c};
2410 my %x; # real parents
2411 delete $p->{$c}; # commits are not self-reproducing...
2412 my $pid = open my $ch, '-|';
2413 defined $pid or croak
$!;
2415 exec(qw
/git-cat-file commit/, $c) or croak
$!;
2418 if (/^parent ($sha1)/) {
2419 $x{$1} = $p->{$1} = 1;
2424 close $ch; # breaking the pipe
2426 # if real parents are the only ones in the grafts, drop it
2427 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2431 @ip = @jp = keys %$p;
2432 foreach my $i (@ip) {
2433 next if $del{$i} || $p->{$i} == 2;
2434 foreach my $j (@jp) {
2435 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2436 $mb = eval { safe_qx
('git-merge-base',$i,$j) };
2443 } elsif ($mb eq $i) {
2450 # if real parents are the only ones in the grafts, drop it
2451 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2453 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2455 if ($comments->{'END'}) {
2456 print $fh $_ foreach @
{$comments->{'END'}};
2458 close $fh or croak
$!;
2461 sub read_url_paths_all
{
2462 my ($l_map, $pfx, $p) = @_;
2465 if (-r
"$_/info/url") {
2466 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2467 my $id = $pfx . basename
$_;
2468 my $url = file_to_s
("$_/info/url");
2469 my ($u, $p) = repo_path_split
($url);
2470 $l_map->{$u}->{$p} = $id;
2477 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2478 read_url_paths_all
($l_map, $x, $_);
2482 # this one only gets ids that have been imported, not new ones
2483 sub read_url_paths
{
2485 git_svn_each
(sub { my $x = shift;
2486 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
2487 my ($u, $p) = repo_path_split
($url);
2488 $l_map->{$u}->{$p} = $x;
2493 sub extract_metadata
{
2494 my $id = shift or return (undef, undef, undef);
2495 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
2497 if (!defined $rev || !$uuid || !$url) {
2498 # some of the original repositories I made had
2499 # identifiers like this:
2500 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2502 return ($url, $rev, $uuid);
2506 return extract_metadata
((grep(/^git-svn-id: /,
2507 safe_qx
(qw
/git-cat-file commit/, shift)))[-1]);
2510 sub get_commit_time
{
2512 defined(my $pid = open my $fh, '-|') or croak
$!;
2514 exec qw
/git-rev-list --pretty=raw -n1/, $cmt or croak
$!;
2517 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2518 my ($s, $tz) = ($1, $2);
2519 if ($tz =~ s/^\+//) {
2520 $s += tz_to_s_offset
($tz);
2521 } elsif ($tz =~ s/^\-//) {
2522 $s -= tz_to_s_offset
($tz);
2527 die "Can't get commit time for commit: $cmt\n";
2530 sub tz_to_s_offset
{
2533 return ($1 * 60) + ($tz * 3600);
2536 sub setup_pager
{ # translated to Perl from pager.c
2537 return unless (-t
*STDOUT
);
2538 my $pager = $ENV{PAGER
};
2539 if (!defined $pager) {
2541 } elsif (length $pager == 0 || $pager eq 'cat') {
2544 pipe my $rfd, my $wfd or return;
2545 defined(my $pid = fork) or croak
$!;
2547 open STDOUT
, '>&', $wfd or croak
$!;
2550 open STDIN
, '<&', $rfd or croak
$!;
2551 $ENV{LESS
} ||= '-S';
2552 exec $pager or croak
"Can't run pager: $!\n";;
2555 sub get_author_info
{
2556 my ($dest, $author, $t, $tz) = @_;
2557 $author =~ s/(?:^\s*|\s*$)//g;
2558 $dest->{a_raw
} = $author;
2561 $_a = $rusers{$author} || undef;
2564 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2569 # Date::Parse isn't in the standard Perl distro :(
2570 if ($tz =~ s/^\+//) {
2571 $t += tz_to_s_offset
($tz);
2572 } elsif ($tz =~ s/^\-//) {
2573 $t -= tz_to_s_offset
($tz);
2575 $dest->{t_utc
} = $t;
2578 sub process_commit
{
2579 my ($c, $r_min, $r_max, $defer) = @_;
2580 if (defined $r_min && defined $r_max) {
2581 if ($r_min == $c->{r
} && $r_min == $r_max) {
2585 return 1 if $r_min == $r_max;
2586 if ($r_min < $r_max) {
2587 # we need to reverse the print order
2588 return 0 if (defined $_limit && --$_limit < 0);
2592 if ($r_min != $r_max) {
2593 return 1 if ($r_min < $c->{r
});
2594 return 1 if ($r_max > $c->{r
});
2597 return 0 if (defined $_limit && --$_limit < 0);
2606 if (my $l = $c->{l
}) {
2607 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2610 $_l_fmt ||= 'A' . length($c->{r
});
2611 print 'r',pack($_l_fmt, $c->{r
}),' | ';
2612 print "$c->{c} | " if $_show_commit;
2615 show_commit_normal
($c);
2619 sub show_commit_changed_paths
{
2621 return unless $c->{changed
};
2622 print "Changed paths:\n", @
{$c->{changed
}};
2625 sub show_commit_normal
{
2627 print '-' x72
, "\nr$c->{r} | ";
2628 print "$c->{c} | " if $_show_commit;
2629 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2630 localtime($c->{t_utc
})), ' | ';
2633 if (my $l = $c->{l
}) {
2634 while ($l->[$#$l] eq "\n" && $#$l > 0
2635 && $l->[($#$l - 1)] eq "\n") {
2638 $nr_line = scalar @
$l;
2640 print "1 line\n\n\n";
2642 if ($nr_line == 1) {
2643 $nr_line = '1 line';
2645 $nr_line .= ' lines';
2647 print $nr_line, "\n";
2648 show_commit_changed_paths
($c);
2650 print $_ foreach @
$l;
2654 show_commit_changed_paths
($c);
2658 foreach my $x (qw
/raw diff/) {
2661 print $_ foreach @
{$c->{$x}}
2667 return unless $_use_lib;
2670 if ($SVN::Core
::VERSION
lt '1.1.0') {
2671 die "Need SVN::Core 1.1.0 or better ",
2672 "(got $SVN::Core::VERSION) ",
2673 "Falling back to command-line svn\n";
2677 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
2678 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
2679 *SVN
::Git
::Fetcher
::process_rm
= *process_rm
;
2680 *SVN
::Git
::Fetcher
::safe_qx
= *safe_qx
;
2681 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2682 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2683 $SVN::Node
::none
.$SVN::Node
::file
.
2684 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2685 $SVN::Auth
::SSL
::CNMISMATCH
.
2686 $SVN::Auth
::SSL
::NOTYETVALID
.
2687 $SVN::Auth
::SSL
::EXPIRED
.
2688 $SVN::Auth
::SSL
::UNKNOWNCA
.
2689 $SVN::Auth
::SSL
::OTHER
;
2694 sub _simple_prompt
{
2695 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2696 $may_save = undef if $_no_auth_cache;
2697 $default_username = $_username if defined $_username;
2698 if (defined $default_username && length $default_username) {
2699 if (defined $realm && length $realm) {
2700 print "Authentication realm: $realm\n";
2702 $cred->username($default_username);
2704 _username_prompt
($cred, $realm, $may_save, $pool);
2706 $cred->password(_read_password
("Password for '" .
2707 $cred->username . "': ", $realm));
2708 $cred->may_save($may_save);
2709 $SVN::_Core
::SVN_NO_ERROR
;
2712 sub _ssl_server_trust_prompt
{
2713 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2714 $may_save = undef if $_no_auth_cache;
2715 print "Error validating server certificate for '$realm':\n";
2716 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
2717 print " - The certificate is not issued by a trusted ",
2718 "authority. Use the\n",
2719 " fingerprint to validate the certificate manually!\n";
2721 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
2722 print " - The certificate hostname does not match.\n";
2724 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
2725 print " - The certificate is not yet valid.\n";
2727 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
2728 print " - The certificate has expired.\n";
2730 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
2731 print " - The certificate has an unknown error.\n";
2733 printf( "Certificate information:\n".
2734 " - Hostname: %s\n".
2735 " - Valid: from %s until %s\n".
2737 " - Fingerprint: %s\n",
2738 map $cert_info->$_, qw(hostname valid_from valid_until
2739 issuer_dname fingerprint) );
2743 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2744 "(R)eject or accept (t)emporarily? ";
2745 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
2746 if ($choice =~ /^t$/i) {
2747 $cred->may_save(undef);
2748 } elsif ($choice =~ /^r$/i) {
2750 } elsif ($may_save && $choice =~ /^p$/i) {
2751 $cred->may_save($may_save);
2755 $cred->accepted_failures($failures);
2756 $SVN::_Core
::SVN_NO_ERROR
;
2759 sub _ssl_client_cert_prompt
{
2760 my ($cred, $realm, $may_save, $pool) = @_;
2761 $may_save = undef if $_no_auth_cache;
2762 print "Client certificate filename: ";
2763 chomp(my $filename = <STDIN
>);
2764 $cred->cert_file($filename);
2765 $cred->may_save($may_save);
2766 $SVN::_Core
::SVN_NO_ERROR
;
2769 sub _ssl_client_cert_pw_prompt
{
2770 my ($cred, $realm, $may_save, $pool) = @_;
2771 $may_save = undef if $_no_auth_cache;
2772 $cred->password(_read_password
("Password: ", $realm));
2773 $cred->may_save($may_save);
2774 $SVN::_Core
::SVN_NO_ERROR
;
2777 sub _username_prompt
{
2778 my ($cred, $realm, $may_save, $pool) = @_;
2779 $may_save = undef if $_no_auth_cache;
2780 if (defined $realm && length $realm) {
2781 print "Authentication realm: $realm\n";
2784 if (defined $_username) {
2785 $username = $_username;
2788 chomp($username = <STDIN
>);
2790 $cred->username($username);
2791 $cred->may_save($may_save);
2792 $SVN::_Core
::SVN_NO_ERROR
;
2795 sub _read_password
{
2796 my ($prompt, $realm) = @_;
2798 require Term
::ReadKey
;
2799 Term
::ReadKey
::ReadMode
('noecho');
2801 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
2802 last if $key =~ /[\012\015]/; # \n\r
2805 Term
::ReadKey
::ReadMode
('restore');
2810 sub libsvn_connect
{
2812 SVN
::_Core
::svn_config_ensure
($_config_dir, undef);
2813 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2814 SVN
::Client
::get_simple_provider
(),
2815 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2816 SVN
::Client
::get_simple_prompt_provider
(
2817 \
&_simple_prompt
, 2),
2818 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2819 \
&_ssl_client_cert_prompt
, 2),
2820 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2821 \
&_ssl_client_cert_pw_prompt
, 2),
2822 SVN
::Client
::get_username_provider
(),
2823 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2824 \
&_ssl_server_trust_prompt
),
2825 SVN
::Client
::get_username_prompt_provider
(
2826 \
&_username_prompt
, 2),
2828 my $config = SVN
::Core
::config_get_config
($_config_dir);
2829 my $ra = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2831 pool
=> SVN
::Pool
->new,
2832 auth_provider_callbacks
=> $callbacks);
2834 my $df = $ENV{GIT_SVN_DELTA_FETCH
};
2838 $_xfer_delta = ($url =~ m
#^file://#) ? undef : 1;
2840 $ra->{svn_path
} = $url;
2841 $ra->{repos_root
} = $ra->get_repos_root;
2842 $ra->{svn_path
} =~ s
#^\Q$ra->{repos_root}\E/*##;
2843 push @repo_path_split_cache, qr/^(\Q$ra->{repos_root}\E)/;
2849 SVN
::Ra
->new(map { $_ => $ra->{$_} } qw
/config url
2850 auth auth_provider_callbacks repos_root svn_path
/);
2853 sub libsvn_get_file
{
2854 my ($gui, $f, $rev, $chg) = @_;
2856 print "\t$chg\t$f\n" unless $_q;
2858 my ($hash, $pid, $in, $out);
2859 my $pool = SVN
::Pool
->new;
2860 defined($pid = open3
($in, $out, '>&STDERR',
2861 qw
/git-hash-object -w --stdin/)) or croak
$!;
2862 # redirect STDOUT for SVN 1.1.x compatibility
2863 open my $stdout, '>&', \
*STDOUT
or croak
$!;
2864 open STDOUT
, '>&', $in or croak
$!;
2865 my ($r, $props) = $SVN->get_file($f, $rev, \
*STDOUT
, $pool);
2866 $in->flush == 0 or croak
$!;
2867 open STDOUT
, '>&', $stdout or croak
$!;
2868 close $in or croak
$!;
2869 close $stdout or croak
$!;
2871 chomp($hash = do { local $/; <$out> });
2872 close $out or croak
$!;
2874 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2876 my $mode = exists $props->{'svn:executable'} ?
'100755' : '100644';
2877 if (exists $props->{'svn:special'}) {
2879 my $link = `git-cat-file blob $hash`;
2880 $link =~ s/^link // or die "svn:special file with contents: <",
2881 $link, "> is not understood\n";
2882 defined($pid = open3
($in, $out, '>&STDERR',
2883 qw
/git-hash-object -w --stdin/)) or croak
$!;
2885 $in->flush == 0 or croak
$!;
2886 close $in or croak
$!;
2887 chomp($hash = do { local $/; <$out> });
2888 close $out or croak
$!;
2890 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2892 print $gui $mode,' ',$hash,"\t",$f,"\0" or croak
$!;
2895 sub libsvn_log_entry
{
2896 my ($rev, $author, $date, $msg, $parents) = @_;
2897 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2898 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2899 or die "Unable to parse date: $date\n";
2900 if (defined $_authors && ! defined $users{$author}) {
2901 die "Author: $author not defined in $_authors file\n";
2903 $msg = '' if ($rev == 0 && !defined $msg);
2904 return { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2905 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [] }
2909 my ($gui, $last_commit, $f) = @_;
2910 # remove entire directories.
2911 if (safe_qx
('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2912 defined(my $pid = open my $ls, '-|') or croak
$!;
2914 exec(qw
/git-ls-tree -r --name-only -z/,
2915 $last_commit,'--',$f) or croak
$!;
2919 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2921 close $ls or croak
$?
;
2923 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2928 $_xfer_delta ? libsvn_fetch_delta
(@_) : libsvn_fetch_full
(@_);
2931 sub libsvn_fetch_delta
{
2932 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2933 my $pool = SVN
::Pool
->new;
2934 my $ed = SVN
::Git
::Fetcher
->new({ c
=> $last_commit, ra
=> $SVN,
2936 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
2937 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2938 my (undef, $last_rev, undef) = cmt_metadata
($last_commit);
2939 $reporter->set_path('', $last_rev, 0, @lock, $pool);
2940 $reporter->finish_report($pool);
2942 libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2945 sub libsvn_fetch_full
{
2946 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2947 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2949 my $p = $SVN->{svn_path
};
2950 foreach my $f (keys %$paths) {
2951 my $m = $paths->{$f}->action();
2953 $f =~ s
#^/\Q$p\E/##;
2954 next if $f =~ m
#^/#;
2958 if ($m =~ /^[DR]$/) {
2959 print "\t$m\t$f\n" unless $_q;
2960 process_rm
($gui, $last_commit, $f);
2962 # 'R' can be file replacements, too, right?
2964 my $pool = SVN
::Pool
->new;
2965 my $t = $SVN->check_path($f, $rev, $pool);
2966 if ($t == $SVN::Node
::file
) {
2967 if ($m =~ /^[AMR]$/) {
2968 push @amr, [ $m, $f ];
2970 die "Unrecognized action: $m, ($f r$rev)\n";
2972 } elsif ($t == $SVN::Node
::dir
&& $m =~ /^[AR]$/) {
2974 libsvn_traverse
($gui, '', $f, $rev, \
@traversed);
2975 foreach (@traversed) {
2976 push @amr, [ $m, $_ ]
2982 libsvn_get_file
($gui, $_->[1], $rev, $_->[0]);
2984 close $gui or croak
$?
;
2985 return libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2988 sub svn_grab_base_rev
{
2989 defined(my $pid = open my $fh, '-|') or croak
$!;
2991 open my $null, '>', '/dev/null' or croak
$!;
2992 open STDERR
, '>&', $null or croak
$!;
2993 exec qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2996 chomp(my $c = do { local $/; <$fh> });
2998 if (defined $c && length $c) {
2999 my ($url, $rev, $uuid) = cmt_metadata
($c);
3000 return ($rev, $c) if defined $rev;
3002 if ($_no_metadata) {
3003 my $offset = -41; # from tail
3005 open my $fh, '<', $REVDB or
3006 die "--no-metadata specified and $REVDB not readable\n";
3007 seek $fh, $offset, 2;
3009 defined $rl or return (undef, undef);
3011 while ($c ne $rl && tell $fh != 0) {
3013 seek $fh, $offset, 2;
3015 defined $rl or return (undef, undef);
3019 croak
$! if ($rev < -1);
3020 $rev = ($rev - 41) / 41;
3021 close $fh or croak
$!;
3024 return (undef, undef);
3027 sub libsvn_parse_revision
{
3029 my $head = $SVN->get_latest_revnum();
3030 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
3031 return ($base + 1, $head) if (defined $base);
3034 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
3035 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
3036 if ($_revision =~ /^BASE:(\d+)$/) {
3037 return ($base + 1, $1) if (defined $base);
3040 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
3041 die "revision argument: $_revision not understood by git-svn\n",
3042 "Try using the command-line svn client instead\n";
3045 sub libsvn_traverse
{
3046 my ($gui, $pfx, $path, $rev, $files) = @_;
3047 my $cwd = length $pfx ?
"$pfx/$path" : $path;
3048 my $pool = SVN
::Pool
->new;
3049 $cwd =~ s
#^\Q$SVN->{svn_path}\E##;
3050 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
3051 foreach my $d (keys %$dirent) {
3052 my $t = $dirent->{$d}->kind;
3053 if ($t == $SVN::Node
::dir
) {
3054 libsvn_traverse
($gui, $cwd, $d, $rev, $files);
3055 } elsif ($t == $SVN::Node
::file
) {
3056 my $file = "$cwd/$d";
3057 if (defined $files) {
3058 push @
$files, $file;
3060 libsvn_get_file
($gui, $file, $rev, 'A');
3067 sub libsvn_traverse_ignore
{
3068 my ($fh, $path, $r) = @_;
3070 my $pool = SVN
::Pool
->new;
3071 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
3073 $p =~ s
#^\Q$SVN->{svn_path}\E/##;
3074 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
3075 if (my $s = $props->{'svn:ignore'}) {
3076 $s =~ s/[\r\n]+/\n/g;
3078 if (length $p == 0) {
3082 $s =~ s
#\n#\n/$p/#g;
3083 print $fh "/$p/$s\n";
3086 foreach (sort keys %$dirent) {
3087 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
3088 libsvn_traverse_ignore
($fh, "$path/$_", $r);
3094 my ($path, $r0, $r1) = @_;
3095 return 1 if $r0 == $r1;
3098 # should be OK to use Pool here (r1 - r0) should be small
3099 my $pool = SVN
::Pool
->new;
3100 libsvn_get_log
($SVN, [$path], $r0, $r1,
3101 0, 1, 1, sub {$nr++}, $pool);
3104 my ($url, undef) = repo_path_split
($SVN_URL);
3105 my $svn_log = svn_log_raw
("$url/$path","-r$r0:$r1");
3106 while (next_log_entry
($svn_log)) { $nr++ }
3107 close $svn_log->{fh
};
3109 return 0 if ($nr > 1);
3113 sub libsvn_find_parent_branch
{
3114 my ($paths, $rev, $author, $date, $msg) = @_;
3115 my $svn_path = '/'.$SVN->{svn_path
};
3117 # look for a parent from another branch:
3118 my $i = $paths->{$svn_path} or return;
3119 my $branch_from = $i->copyfrom_path or return;
3120 my $r = $i->copyfrom_rev;
3121 print STDERR
"Found possible branch point: ",
3122 "$branch_from => $svn_path, $r\n";
3123 $branch_from =~ s
#^/##;
3125 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
3126 my $url = $SVN->{repos_root
};
3127 defined $l_map->{$url} or return;
3128 my $id = $l_map->{$url}->{$branch_from};
3129 if (!defined $id && $_follow_parent) {
3130 print STDERR
"Following parent: $branch_from\@$r\n";
3131 # auto create a new branch and follow it
3132 $id = basename
($branch_from);
3133 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
3134 while (-r
"$GIT_DIR/svn/$id") {
3135 # just grow a tail if we're not unique enough :x
3139 return unless defined $id;
3141 my ($r0, $parent) = find_rev_before
($r,$id,1);
3142 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
3143 defined(my $pid = fork) or croak
$!;
3145 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
3147 $SVN_URL = "$url/$branch_from";
3150 # we can't assume SVN_URL exists at r+1:
3151 $_revision = "0:$r";
3157 ($r0, $parent) = find_rev_before
($r,$id,1);
3159 return unless (defined $r0 && defined $parent);
3160 if (revisions_eq
($branch_from, $r0, $r)) {
3161 unlink $GIT_SVN_INDEX;
3162 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
3163 sys
(qw
/git-read-tree/, $parent);
3164 # I can't seem to get do_switch() to work correctly with
3165 # the SWIG interface (TypeError when passing switch_url...),
3166 # so we'll unconditionally bypass the delta interface here
3168 return libsvn_fetch_full
($parent, $paths, $rev,
3169 $author, $date, $msg);
3171 print STDERR
"Nope, branch point not imported or unknown\n";
3175 sub libsvn_get_log
{
3176 my ($ra, @args) = @_;
3177 if ($SVN::Core
::VERSION
le '1.2.0') {
3178 splice(@args, 3, 1);
3180 $ra->get_log(@args);
3183 sub libsvn_new_tree
{
3184 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
3187 my ($paths, $rev, $author, $date, $msg) = @_;
3189 my $pool = SVN
::Pool
->new;
3190 my $ed = SVN
::Git
::Fetcher
->new({paths
=> $paths, ra
=> $SVN});
3191 my $reporter = $SVN->do_update($rev, '', 1, $ed, $pool);
3192 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
3193 $reporter->set_path('', $rev, 1, @lock, $pool);
3194 $reporter->finish_report($pool);
3197 open my $gui, '| git-update-index -z --index-info' or croak
$!;
3198 libsvn_traverse
($gui, '', $SVN->{svn_path
}, $rev);
3199 close $gui or croak
$?
;
3201 return libsvn_log_entry
($rev, $author, $date, $msg);
3204 sub find_graft_path_commit
{
3205 my ($tree_paths, $p1, $r1) = @_;
3206 foreach my $x (keys %$tree_paths) {
3207 next unless ($p1 =~ /^\Q$x\E/);
3208 my $i = $tree_paths->{$x};
3209 my ($r0, $parent) = find_rev_before
($r1,$i,1);
3210 return $parent if (defined $r0 && $r0 == $r1);
3211 print STDERR
"r$r1 of $i not imported\n";
3217 sub find_graft_path_parents
{
3218 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
3219 foreach my $x (keys %$tree_paths) {
3220 next unless ($p0 =~ /^\Q$x\E/);
3221 my $i = $tree_paths->{$x};
3222 my ($r, $parent) = find_rev_before
($r0, $i, 1);
3223 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
3224 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
3225 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
3226 next if ($url_a && $url_b && $url_a eq $url_b &&
3227 $uuid_b eq $uuid_a);
3228 $grafts->{$c}->{$parent} = 1;
3233 sub libsvn_graft_file_copies
{
3234 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
3235 foreach (keys %$paths) {
3236 my $i = $paths->{$_};
3237 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
3239 next unless (defined $p0 && defined $r0);
3244 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
3246 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
3251 my $old = $ENV{GIT_INDEX_FILE
};
3252 $ENV{GIT_INDEX_FILE
} = shift;
3259 $ENV{GIT_INDEX_FILE
} = $old;
3261 delete $ENV{GIT_INDEX_FILE
};
3265 sub libsvn_commit_cb
{
3266 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3267 if ($_optimize_commits && $rev == ($r_last + 1)) {
3268 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
3269 $log->{tree
} = get_tree_from_treeish
($c);
3270 my $cmt = git_commit
($log, $cmt_last, $c);
3271 my @diff = safe_qx
('git-diff-tree', $cmt, $c);
3273 print STDERR
"Trees differ: $cmt $c\n",
3274 join('',@diff),"\n";
3282 sub libsvn_ls_fullurl
{
3283 my $fullurl = shift;
3284 $SVN ||= libsvn_connect
($fullurl);
3286 my $pool = SVN
::Pool
->new;
3287 my ($dirent, undef, undef) = $SVN->get_dir($SVN->{svn_path
},
3288 $SVN->get_latest_revnum, $pool);
3289 foreach my $d (keys %$dirent) {
3290 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
3291 push @ret, "$d/"; # add '/' for compat with cli svn
3299 sub libsvn_skip_unknown_revs
{
3301 my $errno = $err->apr_err();
3302 # Maybe the branch we're tracking didn't
3303 # exist when the repo started, so it's
3304 # not an error if it doesn't, just continue
3306 # Wonderfully consistent library, eh?
3307 # 160013 - svn:// and file://
3308 # 175002 - http(s)://
3309 # 175007 - http(s):// (this repo required authorization, too...)
3310 # More codes may be discovered later...
3311 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3314 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
3317 # Tie::File seems to be prone to offset errors if revisions get sparse,
3318 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3319 # one of my favorite modules is out :< Next up would be one of the DBM
3320 # modules, but I'm not sure which is most portable... So I'll just
3321 # go with something that's plain-text, but still capable of
3322 # being randomly accessed. So here's my ultra-simple fixed-width
3323 # database. All records are 40 characters + "\n", so it's easy to seek
3324 # to a revision: (41 * rev) is the byte offset.
3325 # A record of 40 0s denotes an empty revision.
3326 # And yes, it's still pretty fast (faster than Tie::File).
3328 my ($file, $rev, $commit) = @_;
3329 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
3330 open my $fh, '+<', $file or croak
$!;
3331 my $offset = $rev * 41;
3332 # assume that append is the common case:
3333 seek $fh, 0, 2 or croak
$!;
3335 if ($pos < $offset) {
3336 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
3338 seek $fh, $offset, 0 or croak
$!;
3339 print $fh $commit,"\n";
3340 close $fh or croak
$!;
3344 my ($file, $rev) = @_;
3346 my $offset = $rev * 41;
3347 open my $fh, '<', $file or croak
$!;
3348 seek $fh, $offset, 0;
3349 if (tell $fh == $offset) {
3350 $ret = readline $fh;
3353 $ret = undef if ($ret =~ /^0{40}$/);
3356 close $fh or croak
$!;
3360 sub copy_remote_ref
{
3361 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
3362 my $ref = "refs/remotes/$GIT_SVN";
3363 if (safe_qx
('git-ls-remote', $origin, $ref)) {
3364 sys
(qw
/git fetch/, $origin, "$ref:$ref");
3365 } elsif ($_cp_remote && !$_upgrade) {
3366 die "Unable to find remote reference: ",
3367 "refs/remotes/$GIT_SVN on $origin\n";
3370 package SVN
::Git
::Fetcher
;
3377 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
3379 my ($class, $git_svn) = @_;
3380 my $self = SVN
::Delta
::Editor
->new;
3381 bless $self, $class;
3382 open my $gui, '| git-update-index -z --index-info' or croak
$!;
3383 $self->{gui
} = $gui;
3384 $self->{c
} = $git_svn->{c
} if exists $git_svn->{c
};
3385 if (my $p = $git_svn->{paths
} && $git_svn->{ra
}) {
3386 my $s = $git_svn->{ra
}->{svn_path
};
3387 $s = length $s ? qr
#^/\Q$s\E/# : qr#^/#;
3388 $self->{paths
} = { map { my $x = $_;
3390 $x => $p->{$_} } keys %$p };
3392 require Digest
::MD5
;
3397 my ($self, $path, $rev, $pb) = @_;
3398 process_rm
($self->{gui
}, $self->{c
}, $path);
3403 my ($self, $path, $pb, $rev) = @_;
3404 my ($mode, $blob) = (safe_qx
('git-ls-tree',$self->{c
},'--',$path)
3405 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
3406 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
3407 pool
=> SVN
::Pool
->new };
3411 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
3412 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
3413 pool
=> SVN
::Pool
->new };
3416 sub change_file_prop
{
3417 my ($self, $fb, $prop, $value) = @_;
3418 if ($prop eq 'svn:executable') {
3419 if ($fb->{mode_b
} != 120000) {
3420 $fb->{mode_b
} = defined $value ?
100755 : 100644;
3422 } elsif ($prop eq 'svn:special') {
3423 $fb->{mode_b
} = defined $value ?
120000 : 100644;
3428 sub apply_textdelta
{
3429 my ($self, $fb, $exp) = @_;
3430 my $fh = IO
::File
->new_tmpfile;
3432 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3433 # (but $base does not,) so dup() it for reading in close_file
3434 open my $dup, '<&', $fh or croak
$!;
3435 my $base = IO
::File
->new_tmpfile;
3436 $base->autoflush(1);
3438 defined (my $pid = fork) or croak
$!;
3440 open STDOUT
, '>&', $base or croak
$!;
3441 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
3442 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
3448 seek $base, 0, 0 or croak
$!;
3449 my $md5 = Digest
::MD5
->new;
3450 $md5->addfile($base);
3451 my $got = $md5->hexdigest;
3452 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
3454 " got: $got\n" if ($got ne $exp);
3457 seek $base, 0, 0 or croak
$!;
3459 $fb->{base
} = $base;
3460 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
3464 my ($self, $fb, $exp) = @_;
3466 my $path = $fb->{path
};
3467 if (my $fh = $fb->{fh
}) {
3468 seek($fh, 0, 0) or croak
$!;
3469 my $md5 = Digest
::MD5
->new;
3471 my $got = $md5->hexdigest;
3472 die "Checksum mismatch: $path\n",
3473 "expected: $exp\n got: $got\n" if ($got ne $exp);
3474 seek($fh, 0, 0) or croak
$!;
3475 if ($fb->{mode_b
} == 120000) {
3476 read($fh, my $buf, 5) == 5 or croak
$!;
3477 $buf eq 'link ' or die "$path has mode 120000",
3478 "but is not a link\n";
3480 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
3482 open STDIN
, '<&', $fh or croak
$!;
3483 exec qw
/git-hash-object -w --stdin/ or croak
$!;
3485 chomp($hash = do { local $/; <$out> });
3486 close $out or croak
$!;
3487 close $fh or croak
$!;
3488 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
3489 close $fb->{base
} or croak
$!;
3491 $hash = $fb->{blob
} or die "no blob information\n";
3494 my $gui = $self->{gui
};
3495 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
3496 print "\t", $self->{paths
}->{$path}->action,
3497 "\t$path\n" if defined $self->{paths
}->{$path};
3504 $self->SUPER::abort_edit
(@_);
3509 close $self->{gui
} or croak
;
3510 $self->SUPER::close_edit
(@_);
3513 package SVN
::Git
::Editor
;
3522 my $git_svn = shift;
3523 my $self = SVN
::Delta
::Editor
->new(@_);
3524 bless $self, $class;
3525 foreach (qw
/svn_path c r ra /) {
3526 die "$_ required!\n" unless (defined $git_svn->{$_});
3527 $self->{$_} = $git_svn->{$_};
3529 $self->{pool
} = SVN
::Pool
->new;
3530 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3532 require Digest
::MD5
;
3537 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3541 (defined $_[1] && length $_[1]) ?
$_[1] : ''
3545 my ($self, $path) = @_;
3546 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
3550 my ($self, $q) = @_;
3551 my $rm = $self->{rm
};
3552 delete $rm->{''}; # we never delete the url we're tracking
3555 foreach (keys %$rm) {
3556 my @d = split m
#/#, $_;
3560 $c .= '/' . shift @d;
3564 delete $rm->{$self->{svn_path
}};
3565 delete $rm->{''}; # we never delete the url we're tracking
3568 defined(my $pid = open my $fh,'-|') or croak
$!;
3570 exec qw
/git-ls-tree --name-only -r -z/, $self->{c
} or croak
$!;
3575 my @dn = split m
#/#, $_;
3577 delete $rm->{join '/', @dn};
3586 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3587 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3588 $self->close_directory($bat->{$d}, $p);
3589 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3590 print "\tD+\t/$d/\n" unless $q;
3591 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3596 sub open_or_add_dir
{
3597 my ($self, $full_path, $baton) = @_;
3598 my $p = SVN
::Pool
->new;
3599 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
3601 if ($t == $SVN::Node
::none
) {
3602 return $self->add_directory($full_path, $baton,
3603 undef, -1, $self->{pool
});
3604 } elsif ($t == $SVN::Node
::dir
) {
3605 return $self->open_directory($full_path, $baton,
3606 $self->{r
}, $self->{pool
});
3608 print STDERR
"$full_path already exists in repository at ",
3609 "r$self->{r} and it is not a directory (",
3610 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3615 my ($self, $path) = @_;
3616 my $bat = $self->{bat
};
3617 $path = $self->repo_path($path);
3618 return $bat->{''} unless (length $path);
3619 my @p = split m
#/+#, $path;
3621 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3624 $c .= '/' . shift @p;
3625 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3631 my ($self, $m, $q) = @_;
3632 my ($dir, $file) = split_path
($m->{file_b
});
3633 my $pbat = $self->ensure_path($dir);
3634 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3636 print "\tA\t$m->{file_b}\n" unless $q;
3637 $self->chg_file($fbat, $m);
3638 $self->close_file($fbat,undef,$self->{pool
});
3642 my ($self, $m, $q) = @_;
3643 my ($dir, $file) = split_path
($m->{file_b
});
3644 my $pbat = $self->ensure_path($dir);
3645 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3646 $self->url_path($m->{file_a
}), $self->{r
});
3647 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3648 $self->chg_file($fbat, $m);
3649 $self->close_file($fbat,undef,$self->{pool
});
3653 my ($self, $path, $pbat) = @_;
3654 my $rpath = $self->repo_path($path);
3655 my ($dir, $file) = split_path
($rpath);
3656 $self->{rm
}->{$dir} = 1;
3657 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3661 my ($self, $m, $q) = @_;
3662 my ($dir, $file) = split_path
($m->{file_b
});
3663 my $pbat = $self->ensure_path($dir);
3664 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3665 $self->url_path($m->{file_a
}), $self->{r
});
3666 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3667 $self->chg_file($fbat, $m);
3668 $self->close_file($fbat,undef,$self->{pool
});
3670 ($dir, $file) = split_path
($m->{file_a
});
3671 $pbat = $self->ensure_path($dir);
3672 $self->delete_entry($m->{file_a
}, $pbat);
3676 my ($self, $m, $q) = @_;
3677 my ($dir, $file) = split_path
($m->{file_b
});
3678 my $pbat = $self->ensure_path($dir);
3679 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3680 $pbat,$self->{r
},$self->{pool
});
3681 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3682 $self->chg_file($fbat, $m);
3683 $self->close_file($fbat,undef,$self->{pool
});
3686 sub T
{ shift->M(@_) }
3688 sub change_file_prop
{
3689 my ($self, $fbat, $pname, $pval) = @_;
3690 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3694 my ($self, $fbat, $m) = @_;
3695 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3696 $self->change_file_prop($fbat,'svn:executable','*');
3697 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3698 $self->change_file_prop($fbat,'svn:executable',undef);
3700 my $fh = IO
::File
->new_tmpfile or croak
$!;
3701 if ($m->{mode_b
} =~ /^120/) {
3702 print $fh 'link ' or croak
$!;
3703 $self->change_file_prop($fbat,'svn:special','*');
3704 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3705 $self->change_file_prop($fbat,'svn:special',undef);
3707 defined(my $pid = fork) or croak
$!;
3709 open STDOUT
, '>&', $fh or croak
$!;
3710 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3714 $fh->flush == 0 or croak
$!;
3715 seek $fh, 0, 0 or croak
$!;
3717 my $md5 = Digest
::MD5
->new;
3718 $md5->addfile($fh) or croak
$!;
3719 seek $fh, 0, 0 or croak
$!;
3721 my $exp = $md5->hexdigest;
3722 my $pool = SVN
::Pool
->new;
3723 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3724 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
3725 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3728 close $fh or croak
$!;
3732 my ($self, $m, $q) = @_;
3733 my ($dir, $file) = split_path
($m->{file_b
});
3734 my $pbat = $self->ensure_path($dir);
3735 print "\tD\t$m->{file_b}\n" unless $q;
3736 $self->delete_entry($m->{file_b
}, $pbat);
3741 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3742 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3743 $self->close_directory($bat->{$_}, $p);
3745 $self->SUPER::close_edit
($p);
3751 $self->SUPER::abort_edit
($self->{pool
});
3752 $self->{pool
}->clear;
3759 $svn_log hashref
(as returned by svn_log_raw
)
3761 fh
=> file handle of the
log file
,
3762 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
3765 $log_msg hashref as returned by next_log_entry
($svn_log)
3767 msg
=> 'whitespace-formatted log entry
3768 ', # trailing newline is preserved
3769 revision
=> '8', # integer
3770 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3771 author
=> 'committer name'
3775 @mods = array of diff
-index line hashes
, each element represents one line
3776 of diff
-index output
3778 diff
-index line
($m hash
)
3780 mode_a
=> first column of diff
-index output
, no leading
':',
3781 mode_b
=> second column of diff
-index output
,
3782 sha1_b
=> sha1sum of the final blob
,
3783 chg
=> change type
[MCRADT
],
3784 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3785 file_b
=> new
/current file name of a file
(any chg
)
3789 # retval of read_url_paths{,_all}();
3791 # repository root url
3792 'https://svn.musicpd.org' => {
3793 # repository path # GIT_SVN_ID
3794 'mpd/trunk' => 'trunk',
3795 'mpd/tags/0.11.5' => 'tags/0.11.5',
3800 I don
't trust the each() function on unless I created %hash myself
3801 because the internal iterator may not have started at base.