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 = '1.1.1-broken';
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 POSIX qw
/strftime/;
37 memoize
('revisions_eq');
38 memoize
('cmt_metadata');
39 memoize
('get_commit_time');
41 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
42 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB
};
44 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
45 my $sha1 = qr/[a-f\d]{40}/;
46 my $sha1_short = qr/[a-f\d]{4,40}/;
47 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
48 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
49 $_repack, $_repack_nr, $_repack_flags, $_q,
50 $_message, $_file, $_follow_parent, $_no_metadata,
51 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
52 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
53 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
54 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
55 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
56 my @repo_path_split_cache;
58 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
59 'branch|b=s' => \
@_branch_from,
60 'follow-parent|follow' => \
$_follow_parent,
61 'branch-all-refs|B' => \
$_branch_all_refs,
62 'authors-file|A=s' => \
$_authors,
63 'repack:i' => \
$_repack,
64 'no-metadata' => \
$_no_metadata,
66 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
68 my ($_trunk, $_tags, $_branches);
69 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
70 'tags|t=s' => \
$_tags,
71 'branches|b=s' => \
$_branches );
72 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
73 my %cmt_opts = ( 'edit|e' => \
$_edit,
75 'find-copies-harder' => \
$_find_copies_harder,
77 'copy-similarity|C=i'=> \
$_cp_similarity
80 # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
81 my %EOL = ( CR
=> "\015", LF
=> "\012", CRLF
=> "\015\012", native
=> "\012" );
84 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
85 { 'revision|r=s' => \
$_revision, %fc_opts } ],
86 init
=> [ \
&init
, "Initialize a repo for tracking" .
87 " (requires URL argument)",
89 commit
=> [ \
&commit
, "Commit git revisions to SVN",
90 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
91 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings",
92 { 'revision|r=i' => \
$_revision } ],
93 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
94 { 'no-ignore-externals' => \
$_no_ignore_ext,
95 'copy-remote|remote=s' => \
$_cp_remote,
96 'upgrade' => \
$_upgrade } ],
97 'graft-branches' => [ \
&graft_branches
,
98 'Detect merges/branches from already imported history',
99 { 'merge-rx|m' => \
@_opt_m,
100 'branch|b=s' => \
@_branch_from,
101 'branch-all-refs|B' => \
$_branch_all_refs,
102 'no-default-regex' => \
$_no_default_regex,
103 'no-graft-copy' => \
$_no_graft_copy } ],
104 'multi-init' => [ \
&multi_init
,
105 'Initialize multiple trees (like git-svnimport)',
106 { %multi_opts, %fc_opts } ],
107 'multi-fetch' => [ \
&multi_fetch
,
108 'Fetch multiple trees (like git-svnimport)',
110 'log' => [ \
&show_log
, 'Show commit logs',
111 { 'limit=i' => \
$_limit,
112 'revision|r=s' => \
$_revision,
113 'verbose|v' => \
$_verbose,
114 'incremental' => \
$_incremental,
115 'oneline' => \
$_oneline,
116 'show-commit' => \
$_show_commit,
117 'authors-file|A=s' => \
$_authors,
119 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
120 { 'message|m=s' => \
$_message,
121 'file|F=s' => \
$_file,
126 for (my $i = 0; $i < @ARGV; $i++) {
127 if (defined $cmd{$ARGV[$i]}) {
134 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
136 read_repo_config
(\
%opts);
137 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
138 'version|V' => \
$_version,
139 'id|i=s' => \
$GIT_SVN);
140 exit 1 if (!$rv && $cmd ne 'log');
144 version
() if $_version;
145 usage
(1) unless defined $cmd;
147 load_authors
() if $_authors;
148 load_all_refs
() if $_branch_all_refs;
149 svn_compat_check
() unless $_use_lib;
150 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init)$/;
151 $cmd{$cmd}->[0]->(@ARGV);
154 ####################### primary functions ######################
156 my $exit = shift || 0;
157 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
159 git
-svn
- bidirectional operations between a single Subversion tree
and git
160 Usage
: $0 <command
> [options
] [arguments
]\n
162 print $fd "Available commands:\n" unless $cmd;
164 foreach (sort keys %cmd) {
165 next if $cmd && $cmd ne $_;
166 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
167 foreach (keys %{$cmd{$_}->[2]}) {
168 # prints out arguments as they should be passed:
169 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
170 print $fd ' ' x
17, join(', ', map { length $_ > 1 ?
172 split /\|/,$_)," $x\n";
176 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
177 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
178 one git repository and want to keep them separate. See git-svn(1) for more
185 print "git-svn version $VERSION\n";
190 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
193 $SVN_URL = shift or undef;
196 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
198 check_upgrade_needed();
201 my $pid = open(my $rev_list,'-|');
202 defined $pid or croak $!;
204 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
207 while (<$rev_list>) {
210 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
211 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
212 next if (!@commit); # skip merges
213 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
214 if (!$rev || !$uuid) {
215 croak "Unable to extract revision or UUID from ",
216 "$c, $commit[$#commit]\n";
219 # if we merged or otherwise started elsewhere, this is
220 # how we break out of it
221 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
222 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
224 unless (defined $latest) {
225 if (!$SVN_URL && !$url) {
226 croak "SVN repository location required: $url\n";
233 revdb_set($REVDB, $rev, $c);
234 print "r$rev = $c\n";
235 $newest_rev = $rev if ($rev > $newest_rev);
237 close $rev_list or croak $?;
239 goto out if $_use_lib;
240 if (!chdir $SVN_WC) {
241 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
242 chdir $SVN_WC or croak $!;
246 defined $pid or croak $!;
248 my @svn_up = qw(svn up);
249 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
250 sys
(@svn_up,"-r$newest_rev");
251 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
253 exec('git-write-tree') or croak
$!;
260 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
261 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
267 $SVN_URL = shift or die "SVN repository location required " .
268 "as a command-line argument\n";
269 $SVN_URL =~ s!/+$!!; # strip trailing slash
270 unless (-d
$GIT_DIR) {
271 my @init_db = ('git-init-db');
272 push @init_db, "--template=$_template" if defined $_template;
273 push @init_db, "--shared" if defined $_shared;
280 check_upgrade_needed
();
281 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
282 my $ret = $_use_lib ? fetch_lib
(@_) : fetch_cmd
(@_);
283 if ($ret->{commit
} && quiet_run
(qw(git-rev-parse --verify
284 refs/heads/master^0))) {
285 sys
(qw(git-update-ref refs/heads/master),$ret->{commit
});
292 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
293 unless ($_revision) {
294 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
296 push @log_args, "-r$_revision";
297 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
299 my $svn_log = svn_log_raw
(@log_args);
301 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
302 # don't need last_revision from grab_base_rev() because
303 # user could've specified a different revision to skip (they
304 # didn't want to import certain revisions into git for whatever
305 # reason, so trust $base->{revision} instead.
306 my (undef, $last_commit) = svn_grab_base_rev
();
307 unless (-d
$SVN_WC) {
308 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
309 chdir $SVN_WC or croak
$!;
311 $last_commit = git_commit
($base, @parents);
312 assert_tree
($last_commit);
314 chdir $SVN_WC or croak
$!;
316 # looks like a user manually cp'd and svn switch'ed
317 unless ($last_commit) {
318 sys
(qw
/svn revert -R ./);
319 assert_svn_wc_clean
($base->{revision
});
320 $last_commit = git_commit
($base, @parents);
321 assert_tree
($last_commit);
324 my @svn_up = qw(svn up);
325 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
327 while (my $log_msg = next_log_entry
($svn_log)) {
328 if ($last->{revision
} >= $log_msg->{revision
}) {
329 croak
"Out of order: last >= current: ",
330 "$last->{revision} >= $log_msg->{revision}\n";
332 # Revert is needed for cases like:
333 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
334 # I can't seem to reproduce something like that on a test...
335 sys
(qw
/svn revert -R ./);
336 assert_svn_wc_clean
($last->{revision
});
337 sys
(@svn_up,"-r$log_msg->{revision}");
338 $last_commit = git_commit
($log_msg, $last_commit, @parents);
341 close $svn_log->{fh
};
342 $last->{commit
} = $last_commit;
348 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
350 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
351 $SVN_LOG ||= libsvn_connect
($repo);
352 $SVN ||= libsvn_connect
($repo);
353 my ($last_rev, $last_commit) = svn_grab_base_rev
();
354 my ($base, $head) = libsvn_parse_revision
($last_rev);
356 return { revision
=> $last_rev, commit
=> $last_commit }
358 my $index = set_index
($GIT_SVN_INDEX);
360 # limit ourselves and also fork() since get_log won't release memory
361 # after processing a revision and SVN stuff seems to leak
363 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
365 if (defined $last_commit) {
366 unless (-e
$GIT_SVN_INDEX) {
367 sys
(qw
/git-read-tree/, $last_commit);
369 chomp (my $x = `git-write-tree`);
370 my ($y) = (`git-cat-file commit $last_commit`
371 =~ /^tree ($sha1)/m);
373 unlink $GIT_SVN_INDEX or croak
$!;
374 sys
(qw
/git-read-tree/, $last_commit);
376 chomp ($x = `git-write-tree`);
378 print STDERR
"trees ($last_commit) $y != $x\n",
379 "Something is seriously wrong...\n";
383 # fork, because using SVN::Pool with get_log() still doesn't
384 # seem to help enough to keep memory usage down.
385 defined(my $pid = fork) or croak
$!;
387 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
389 # Yes I'm perfectly aware that the fourth argument
390 # below is the limit revisions number. Unfortunately
391 # performance sucks with it enabled, so it's much
392 # faster to fetch revision ranges instead of relying
394 libsvn_get_log
($SVN_LOG, '/'.$SVN_PATH,
399 $log_msg = libsvn_fetch
(
401 $last_commit = git_commit
(
406 $log_msg = libsvn_new_tree
(@_);
407 $last_commit = git_commit
(
415 ($last_rev, $last_commit) = svn_grab_base_rev
();
416 last if ($max >= $head);
419 $max = $head if ($max > $head);
421 restore_index
($index);
422 return { revision
=> $last_rev, commit
=> $last_commit };
427 check_upgrade_needed
();
428 if ($_stdin || !@commits) {
429 print "Reading from stdin...\n";
432 if (/\b($sha1_short)\b/o) {
433 unshift @commits, $1;
438 foreach my $c (@commits) {
439 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
440 if (scalar @tmp == 1) {
442 } elsif (scalar @tmp > 1) {
443 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
445 die "Failed to rev-parse $c\n";
449 $_use_lib ? commit_lib
(@revs) : commit_cmd
(@revs);
450 print "Done committing ",scalar @revs," revisions to SVN\n";
456 chdir $SVN_WC or croak
"Unable to chdir $SVN_WC: $!\n";
457 my $info = svn_info
('.');
458 my $fetched = fetch
();
459 if ($info->{Revision
} != $fetched->{revision
}) {
460 print STDERR
"There are new revisions that were fetched ",
461 "and need to be merged (or acknowledged) ",
462 "before committing.\n";
465 $info = svn_info
('.');
468 foreach my $c (@revs) {
469 my $mods = svn_checkout_tree
($last, $c);
470 if (scalar @
$mods == 0) {
471 print "Skipping, no changes detected\n";
474 $last = svn_commit_tree
($last, $c);
480 my ($r_last, $cmt_last) = svn_grab_base_rev
();
481 defined $r_last or die "Must have an existing revision to commit\n";
482 my $fetched = fetch
();
483 if ($r_last != $fetched->{revision
}) {
484 print STDERR
"There are new revisions that were fetched ",
485 "and need to be merged (or acknowledged) ",
486 "before committing.\n",
487 "last rev: $r_last\n",
488 " current: $fetched->{revision}\n";
492 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
493 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
495 set_svn_commit_env
();
496 foreach my $c (@revs) {
497 my $log_msg = get_commit_message
($c, $commit_msg);
499 # fork for each commit because there's a memory leak I
500 # can't track down... (it's probably in the SVN code)
501 defined(my $pid = open my $fh, '-|') or croak
$!;
503 my $ed = SVN
::Git
::Editor
->new(
507 svn_path
=> $SVN_PATH
509 $SVN->get_commit_editor(
520 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
522 print "No changes\nr$r_last = $cmt_last\n";
529 my ($r_new, $cmt_new, $no);
533 if (/^r(\d+) = ($sha1)$/o) {
534 ($r_new, $cmt_new) = ($1, $2);
535 } elsif ($_ eq 'No changes') {
539 close $fh or croak
$?
;
540 if (! defined $r_new && ! defined $cmt_new) {
542 die "Failed to parse revision information\n";
545 ($r_last, $cmt_last) = ($r_new, $cmt_new);
553 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
554 $_use_lib ? show_ignore_lib
() : show_ignore_cmd
();
557 sub show_ignore_cmd
{
558 require File
::Find
or die $!;
559 if (defined $_revision) {
560 die "-r/--revision option doesn't work unless the Perl SVN ",
561 "libraries are used\n";
563 chdir $SVN_WC or croak
$!;
565 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
567 @
{$ign{$_}} = svn_propget_base
('svn:ignore', $_);
568 }}, no_chdir
=>1},'.');
571 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
573 foreach my $i (sort keys %ign) {
574 print "\n# ",$i,"\n";
575 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
579 sub show_ignore_lib
{
581 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
582 $SVN ||= libsvn_connect
($repo);
583 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
584 libsvn_traverse_ignore
(\
*STDOUT
, $SVN_PATH, $r);
588 my $gr_file = "$GIT_DIR/info/grafts";
589 my ($grafts, $comments) = read_grafts
($gr_file);
593 # temporarily disable our grafts file to make this idempotent
594 chomp($gr_sha1 = safe_qx
(qw
/git-hash-object -w/,$gr_file));
595 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
598 my $l_map = read_url_paths
();
599 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
600 unless ($_no_default_regex) {
601 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
602 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
603 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
605 foreach my $u (keys %$l_map) {
607 foreach my $p (keys %{$l_map->{$u}}) {
608 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
611 unless ($_no_graft_copy) {
613 graft_file_copy_lib
($grafts,$l_map,$u);
615 graft_file_copy_cmd
($grafts,$l_map,$u);
619 graft_tree_joins
($grafts);
621 write_grafts
($grafts, $comments, $gr_file);
622 unlink "$gr_file~$gr_sha1" if $gr_sha1;
629 $url =~ s
#/+$## if $url;
630 if ($_trunk !~ m
#^[a-z\+]+://#) {
631 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
633 print STDERR
"E: '$_trunk' is not a complete URL ",
634 "and a separate URL is not specified\n";
637 $_trunk = $url . $_trunk;
639 if ($GIT_SVN eq 'git-svn') {
640 print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
641 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
645 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
646 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
650 # try to do trunk first, since branches/tags
651 # may be descended from it.
652 if (-e
"$GIT_DIR/svn/trunk/info/url") {
653 fetch_child_id
('trunk', @_);
655 rec_fetch
('', "$GIT_DIR/svn", @_);
661 my $r_last = -1; # prevent dupes
662 rload_authors
() if $_authors;
668 if (defined $_revision) {
669 if ($_revision =~ /^(\d+):(\d+)$/) {
670 ($r_min, $r_max) = ($1, $2);
671 } elsif ($_revision =~ /^\d+$/) {
672 $r_min = $r_max = $_revision;
674 print STDERR
"-r$_revision is not supported, use ",
675 "standard \'git log\' arguments instead\n";
680 my $pid = open(my $log,'-|');
681 defined $pid or croak
$!;
683 exec(git_svn_log_cmd
($r_min,$r_max), @args) or croak
$!;
689 if (/^commit ($sha1_short)/o) {
691 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
693 process_commit
($c, $r_min, $r_max, \
@k) or
698 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
699 get_author_info
($c, $1, $2, $3);
700 } elsif (/^(?:tree|parent|committer) /) {
702 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
703 push @
{$c->{raw
}}, $_;
706 push @
{$c->{diff
}}, $_;
708 push @
{$c->{diff
}}, $_;
709 } elsif (/^ (git-svn-id:.+)$/) {
710 (undef, $c->{r
}, undef) = extract_metadata
($1);
715 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
717 process_commit
($c, $r_min, $r_max, \
@k);
723 process_commit
($_, $r_min, $r_max) foreach reverse @k;
727 print '-' x72
,"\n" unless $_incremental || $_oneline;
730 sub commit_diff_usage
{
731 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
737 print STDERR
"commit-diff must be used with SVN libraries\n";
740 my $ta = shift or commit_diff_usage
();
741 my $tb = shift or commit_diff_usage
();
742 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
743 print STDERR
"Needed URL or usable git-svn id command-line\n";
746 if (defined $_message && defined $_file) {
747 print STDERR
"Both --message/-m and --file/-F specified ",
748 "for the commit message.\n",
749 "I have no idea what you mean\n";
752 if (defined $_file) {
753 $_message = file_to_s
($_message);
755 $_message ||= get_commit_message
($tb,
756 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
759 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
760 $SVN_LOG ||= libsvn_connect
($repo);
761 $SVN ||= libsvn_connect
($repo);
762 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
763 my $ed = SVN
::Git
::Editor
->new({ r
=> $SVN->get_latest_revnum,
764 ra
=> $SVN, c
=> $tb,
765 svn_path
=> $SVN_PATH
767 $SVN->get_commit_editor($_message,
768 sub {print "Committed $_[0]\n"},@lock)
770 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
772 print "No changes\n$ta == $tb\n";
779 ########################### utility functions #########################
783 return 1 if defined $c->{r
};
784 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
785 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
786 my @msg = safe_qx
(qw
/git-cat-file commit/, $c->{c
});
787 shift @msg while ($msg[0] ne "\n");
789 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
791 (undef, $c->{r
}, undef) = extract_metadata
(
792 (grep(/^git-svn-id: /, @msg))[-1]);
794 return defined $c->{r
};
797 sub git_svn_log_cmd
{
798 my ($r_min, $r_max) = @_;
799 my @cmd = (qw
/git
-log --abbrev
-commit
--pretty
=raw
800 --default/, "refs/remotes
/$GIT_SVN");
801 push @cmd, '--summary' if $_verbose;
802 return @cmd unless defined $r_max;
803 if ($r_max == $r_min) {
804 push @cmd, '--max-count=1';
805 if (my $c = revdb_get($REVDB, $r_max)) {
810 $c_max = revdb_get($REVDB, $r_max);
811 $c_min = revdb_get($REVDB, $r_min);
812 if ($c_min && $c_max) {
813 if ($r_max > $r_max) {
814 push @cmd, "$c_min..$c_max";
816 push @cmd, "$c_max..$c_min";
818 } elsif ($r_max > $r_min) {
829 print "Fetching
$id\n";
830 my $ref = "$GIT_DIR/refs/remotes
/$id";
831 defined(my $pid = open my $fh, '-|') or croak $!;
834 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
841 check_repack() if (/^r\d+ = $sha1/);
843 close $fh or croak $?;
847 my ($pfx, $p, @args) = @_;
849 foreach (sort <$p/*>) {
850 if (-r "$_/info/url
") {
851 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
852 my $id = $pfx . basename $_;
853 next if $id eq 'trunk';
854 fetch_child_id($id, @args);
861 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
866 sub complete_url_ls_init {
867 my ($url, $var, $switch, $pfx) = @_;
869 print STDERR "W
: $switch not specified
\n";
873 if ($var !~ m#^[a-z\+]+://#) {
874 $var = '/' . $var if ($var !~ m#^/#);
876 print STDERR "E
: '$var' is
not a complete URL
",
877 "and a separate URL is
not specified
\n";
882 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
883 : safe_qx(qw/svn ls --non-interactive/, $var));
885 defined(my $pid = fork) or croak $!;
887 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
889 if ($u !~ m!\Q$var\E/(.+)$!) {
890 print STDERR
"W: Unrecognized URL: $u\n";
891 die "This should never happen\n";
894 print "init $u => $id\n";
895 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
909 my @tmp = split m
#/#, $_;
911 while (my $x = shift @tmp) {
917 foreach (sort {length $b <=> length $a} keys %common) {
918 if ($common{$_} == @
$paths) {
925 # grafts set here are 'stronger' in that they're based on actual tree
926 # matches, and won't be deleted from merge-base checking in write_grafts()
927 sub graft_tree_joins
{
929 map_tree_joins
() if (@_branch_from && !%tree_map);
930 return unless %tree_map;
934 defined(my $pid = open my $fh, '-|') or croak
$!;
936 exec qw
/git-rev-list --pretty=raw/,
937 "refs/remotes/$i" or croak
$!;
940 next unless /^commit ($sha1)$/o;
942 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
943 next unless $tree_map{$t};
948 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
950 my ($s, $tz) = ($1, $2);
951 if ($tz =~ s/^\+//) {
952 $s += tz_to_s_offset
($tz);
953 } elsif ($tz =~ s/^\-//) {
954 $s -= tz_to_s_offset
($tz);
957 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
959 foreach my $p (@
{$tree_map{$t}}) {
962 safe_qx
('git-merge-base', $c, $p)
964 next unless ($@
|| $?
);
966 # see if SVN says it's a relative
967 my ($url_b, $r_b, $uuid_b) =
969 next if (defined $url_b &&
971 ($url_a eq $url_b) &&
972 ($uuid_a eq $uuid_b));
973 if ($uuid_a eq $uuid_b) {
975 $grafts->{$c}->{$p} = 2;
977 } elsif ($r_b > $r_a) {
978 $grafts->{$p}->{$c} = 2;
983 my $ct = get_commit_time
($p);
985 $grafts->{$c}->{$p} = 2;
987 $grafts->{$p}->{$c} = 2;
989 # what should we do when $ct == $s ?
992 close $fh or croak
$?
;
996 # this isn't funky-filename safe, but good enough for now...
997 sub graft_file_copy_cmd
{
998 my ($grafts, $l_map, $u) = @_;
999 my $paths = $l_map->{$u};
1000 my $pfx = common_prefix
([keys %$paths]);
1001 $SVN_URL ||= $u.$pfx;
1002 my $pid = open my $fh, '-|';
1003 defined $pid or croak
$!;
1005 my @exec = qw
/svn log -v/;
1006 push @exec, "-r$_revision" if defined $_revision;
1007 exec @exec, $u.$pfx or croak
$!;
1009 my ($r, $mp) = (undef, undef);
1014 } elsif (/^r(\d+) \| /) {
1015 $r = $1 unless defined $r;
1016 } elsif (/^Changed paths:/) {
1018 } elsif ($mp && m
#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1019 my ($p1, $p0, $r0) = ($1, $2, $3);
1020 my $c = find_graft_path_commit
($paths, $p1, $r);
1022 find_graft_path_parents
($grafts, $paths, $c, $p0, $r0);
1027 sub graft_file_copy_lib
{
1028 my ($grafts, $l_map, $u) = @_;
1029 my $tree_paths = $l_map->{$u};
1030 my $pfx = common_prefix
([keys %$tree_paths]);
1031 my ($repo, $path) = repo_path_split
($u.$pfx);
1032 $SVN_LOG ||= libsvn_connect
($repo);
1033 $SVN ||= libsvn_connect
($repo);
1035 my ($base, $head) = libsvn_parse_revision
();
1037 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1038 my $eh = $SVN::Error
::handler
;
1039 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1041 my $pool = SVN
::Pool
->new;
1042 libsvn_get_log
($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1044 libsvn_graft_file_copies
($grafts, $tree_paths,
1048 last if ($max >= $head);
1051 $max = $head if ($max > $head);
1053 $SVN::Error
::handler
= $eh;
1056 sub process_merge_msg_matches
{
1057 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1058 my (@strong, @weak);
1059 foreach (@matches) {
1060 # merging with ourselves is not interesting
1062 if ($l_map->{$u}->{$_}) {
1068 foreach my $w (@weak) {
1070 # no exact match, use branch name as regexp.
1071 my $re = qr/\Q$w\E/i;
1072 foreach (keys %{$l_map->{$u}}) {
1074 push @strong, $l_map->{$u}->{$_};
1081 foreach (keys %{$l_map->{$u}}) {
1083 push @strong, $l_map->{$u}->{$_};
1088 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1089 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1090 unless (defined $rev) {
1091 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1092 \@
(?
:[a
-f\d\
-]+)/xsm
);
1093 return unless defined $rev;
1095 foreach my $m (@strong) {
1096 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1097 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1101 sub graft_merge_msg
{
1102 my ($grafts, $l_map, $u, $p, @re) = @_;
1104 my $x = $l_map->{$u}->{$p};
1105 my $rl = rev_list_raw
($x);
1106 while (my $c = next_rev_list_entry
($rl)) {
1107 foreach my $re (@re) {
1108 my (@br) = ($c->{m
} =~ /$re/g);
1110 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1116 return if $SVN_UUID;
1118 my $pool = SVN
::Pool
->new;
1119 $SVN_UUID = $SVN->get_uuid($pool);
1122 my $info = shift || svn_info
('.');
1123 $SVN_UUID = $info->{'Repository UUID'} or
1124 croak
"Repository UUID unreadable\n";
1130 defined $pid or croak
$!;
1132 open my $null, '>', '/dev/null' or croak
$!;
1133 open STDERR
, '>&', $null or croak
$!;
1134 open STDOUT
, '>&', $null or croak
$!;
1135 exec @_ or croak
$!;
1141 sub repo_path_split
{
1142 my $full_url = shift;
1143 $full_url =~ s
#/+$##;
1145 foreach (@repo_path_split_cache) {
1146 if ($full_url =~ s
#$_##) {
1148 $full_url =~ s
#^/+##;
1149 return ($u, $full_url);
1153 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1155 my @paths = split(m
#/+#, $path);
1159 $SVN = libsvn_connect
($url);
1160 last if (defined $SVN &&
1161 defined eval { $SVN->get_latest_revnum });
1162 my $n = shift @paths || last;
1166 while (quiet_run
(qw
/svn ls --non-interactive/, $url)) {
1167 my $n = shift @paths || last;
1171 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1172 $path = join('/',@paths);
1173 return ($url, $path);
1177 defined $SVN_URL or croak
"SVN repository location required\n";
1178 unless (-d
$GIT_DIR) {
1179 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1181 mkpath
([$GIT_SVN_DIR]);
1182 mkpath
(["$GIT_SVN_DIR/info"]);
1183 open my $fh, '>>',$REVDB or croak
$!;
1185 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1189 sub assert_svn_wc_clean
{
1190 return if $_use_lib;
1192 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1193 my $lcr = svn_info
('.')->{'Last Changed Rev'};
1194 if ($svn_rev != $lcr) {
1195 print STDERR
"Checking for copy-tree ... ";
1196 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
1197 "-r$lcr:$svn_rev")));
1199 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
1201 print STDERR
"OK!\n";
1204 my @status = grep(!/^Performing status on external/,(`svn status`));
1205 @status = grep(!/^\s*$/,@status);
1206 if (scalar @status) {
1207 print STDERR
"Tree ($SVN_WC) is not clean:\n";
1208 print STDERR
$_ foreach @status;
1213 sub get_tree_from_treeish
{
1215 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1216 chomp(my $type = `git-cat-file -t $treeish`);
1218 while ($type eq 'tag') {
1219 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1221 if ($type eq 'commit') {
1222 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1223 ($expected) = ($expected =~ /^tree ($sha1)$/);
1224 die "Unable to get tree from $treeish\n" unless $expected;
1225 } elsif ($type eq 'tree') {
1226 $expected = $treeish;
1228 die "$treeish is a $type, expected tree, tag or commit\n";
1234 return if $_use_lib;
1236 my $expected = get_tree_from_treeish
($treeish);
1238 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1240 unlink $tmpindex or croak
$!;
1242 my $old_index = set_index
($tmpindex);
1244 chomp(my $tree = `git-write-tree`);
1245 restore_index
($old_index);
1246 if ($tree ne $expected) {
1247 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
1252 sub parse_diff_tree
{
1253 my $diff_fh = shift;
1257 while (<$diff_fh>) {
1258 chomp $_; # this gets rid of the trailing "\0"
1259 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1260 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1261 push @mods, { mode_a
=> $1, mode_b
=> $2,
1262 sha1_b
=> $3, chg
=> $4 };
1263 if ($4 =~ /^(?:C|R)$/) {
1268 } elsif ($state eq 'file_a') {
1269 my $x = $mods[$#mods] or croak
"Empty array\n";
1270 if ($x->{chg
} !~ /^(?:C|R)$/) {
1271 croak
"Error parsing $_, $x->{chg}\n";
1275 } elsif ($state eq 'file_b') {
1276 my $x = $mods[$#mods] or croak
"Empty array\n";
1277 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1278 croak
"Error parsing $_, $x->{chg}\n";
1280 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1281 croak
"Error parsing $_, $x->{chg}\n";
1286 croak
"Error parsing $_\n";
1289 close $diff_fh or croak
$?
;
1294 sub svn_check_prop_executable
{
1296 return if -l
$m->{file_b
};
1297 if ($m->{mode_b
} =~ /755$/) {
1298 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
1299 if ($m->{mode_a
} !~ /755$/) {
1300 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
1302 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
1303 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1304 sys
(qw(svn propdel svn:executable), $m->{file_b
});
1305 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
1306 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
1310 sub svn_ensure_parent_path
{
1311 my $dir_b = dirname
(shift);
1312 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
1313 mkpath
([$dir_b]) unless (-d
$dir_b);
1314 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
1317 sub precommit_check
{
1319 my (%rm_file, %rmdir_check, %added_check);
1321 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
1322 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1323 if ($m->{chg
} eq 'R') {
1324 if (-d
$m->{file_b
}) {
1325 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1327 # dir/$file => dir/file/$file
1328 my $dirname = dirname
($m->{file_b
});
1329 while ($dirname ne File
::Spec
->curdir) {
1330 if ($dirname ne $m->{file_a
}) {
1331 $dirname = dirname
($dirname);
1334 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
1336 # baz/zzz => baz (baz is a file)
1337 $dirname = dirname
($m->{file_a
});
1338 while ($dirname ne File
::Spec
->curdir) {
1339 if ($dirname ne $m->{file_b
}) {
1340 $dirname = dirname
($dirname);
1343 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1346 if ($m->{chg
} =~ /^(D|R)$/) {
1347 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
1348 $rm_file{ $m->{$t} } = 1;
1349 my $dirname = dirname
( $m->{$t} );
1350 my $basename = basename
( $m->{$t} );
1351 $rmdir_check{$dirname}->{$basename} = 1;
1352 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
1353 if (-d
$m->{file_b
}) {
1354 err_dir_to_file
($m->{file_b
});
1356 my $dirname = dirname
( $m->{file_b
} );
1357 my $basename = basename
( $m->{file_b
} );
1358 $added_check{$dirname}->{$basename} = 1;
1359 while ($dirname ne File
::Spec
->curdir) {
1360 if ($rm_file{$dirname}) {
1361 err_file_to_dir
($m->{file_b
});
1363 $dirname = dirname
$dirname;
1367 return (\
%rmdir_check, \
%added_check);
1369 sub err_dir_to_file
{
1371 print STDERR
"Node change from directory to file ",
1372 "is not supported by Subversion: ",$file,"\n";
1375 sub err_file_to_dir
{
1377 print STDERR
"Node change from file to directory ",
1378 "is not supported by Subversion: ",$file,"\n";
1385 my ($from, $treeish) = @_;
1387 print "diff-tree $from $treeish\n";
1388 my $pid = open my $diff_fh, '-|';
1389 defined $pid or croak
$!;
1391 my @diff_tree = qw(git-diff-tree -z -r);
1392 if ($_cp_similarity) {
1393 push @diff_tree, "-C$_cp_similarity";
1395 push @diff_tree, '-C';
1397 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1398 push @diff_tree, "-l$_l" if defined $_l;
1399 exec(@diff_tree, $from, $treeish) or croak
$!;
1401 return parse_diff_tree
($diff_fh);
1404 sub svn_checkout_tree
{
1405 my ($from, $treeish) = @_;
1406 my $mods = get_diff
($from->{commit
}, $treeish);
1407 return $mods unless (scalar @
$mods);
1408 my ($rm, $add) = precommit_check
($mods);
1410 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1411 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1412 if ($m->{chg
} eq 'C') {
1413 svn_ensure_parent_path
( $m->{file_b
} );
1414 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
1415 apply_mod_line_blob
($m);
1416 svn_check_prop_executable
($m);
1417 } elsif ($m->{chg
} eq 'D') {
1418 sys
(qw(svn rm --force), $m->{file_b
});
1419 } elsif ($m->{chg
} eq 'R') {
1420 svn_ensure_parent_path
( $m->{file_b
} );
1421 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
1422 apply_mod_line_blob
($m);
1423 svn_check_prop_executable
($m);
1424 } elsif ($m->{chg
} eq 'M') {
1425 apply_mod_line_blob
($m);
1426 svn_check_prop_executable
($m);
1427 } elsif ($m->{chg
} eq 'T') {
1428 sys
(qw(svn rm --force),$m->{file_b
});
1429 apply_mod_line_blob
($m);
1430 sys
(qw(svn add), $m->{file_b
});
1431 svn_check_prop_executable
($m);
1432 } elsif ($m->{chg
} eq 'A') {
1433 svn_ensure_parent_path
( $m->{file_b
} );
1434 apply_mod_line_blob
($m);
1435 sys
(qw(svn add), $m->{file_b
});
1436 svn_check_prop_executable
($m);
1438 croak
"Invalid chg: $m->{chg}\n";
1442 assert_tree
($treeish);
1443 if ($_rmdir) { # remove empty directories
1444 handle_rmdir
($rm, $add);
1446 assert_tree
($treeish);
1450 sub libsvn_checkout_tree
{
1451 my ($from, $treeish, $ed) = @_;
1452 my $mods = get_diff
($from, $treeish);
1453 return $mods unless (scalar @
$mods);
1454 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1455 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1457 if (defined $o{$f}) {
1460 croak
"Invalid change type: $f\n";
1463 $ed->rmdirs($_q) if $_rmdir;
1467 # svn ls doesn't work with respect to the current working tree, but what's
1468 # in the repository. There's not even an option for it... *sigh*
1469 # (added files don't show up and removed files remain in the ls listing)
1470 sub svn_ls_current
{
1471 my ($dir, $rm, $add) = @_;
1472 chomp(my @ls = safe_qx
('svn','ls',$dir));
1475 s
#/$##; # trailing slashes are evil
1476 push @ret, $_ unless $rm->{$dir}->{$_};
1478 if (exists $add->{$dir}) {
1479 push @ret, keys %{$add->{$dir}};
1485 my ($rm, $add) = @_;
1487 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1488 my $ls = svn_ls_current
($dir, $rm, $add);
1489 next if (scalar @
$ls);
1490 sys
(qw(svn rm --force),$dir);
1492 my $dn = dirname
$dir;
1493 $rm->{ $dn }->{ basename
$dir } = 1;
1494 $ls = svn_ls_current
($dn, $rm, $add);
1495 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
1496 sys
(qw(svn rm --force),$dn);
1497 $dir = basename
$dn;
1499 $rm->{ $dn }->{ $dir } = 1;
1500 $ls = svn_ls_current
($dn, $rm, $add);
1505 sub get_commit_message
{
1506 my ($commit, $commit_msg) = (@_);
1507 my %log_msg = ( msg
=> '' );
1508 open my $msg, '>', $commit_msg or croak
$!;
1510 chomp(my $type = `git-cat-file -t $commit`);
1511 if ($type eq 'commit') {
1512 my $pid = open my $msg_fh, '-|';
1513 defined $pid or croak
$!;
1516 exec(qw(git-cat-file commit), $commit) or croak
$!;
1521 $in_msg = 1 if (/^\s*$/);
1522 } elsif (/^git-svn-id: /) {
1523 # skip this, we regenerate the correct one
1524 # on re-fetch anyways
1526 print $msg $_ or croak
$!;
1529 close $msg_fh or croak
$?
;
1531 close $msg or croak
$!;
1533 if ($_edit || ($type eq 'tree')) {
1534 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1535 system($editor, $commit_msg);
1538 # file_to_s removes all trailing newlines, so just use chomp() here:
1539 open $msg, '<', $commit_msg or croak
$!;
1540 { local $/; chomp($log_msg{msg
} = <$msg>); }
1541 close $msg or croak
$!;
1546 sub set_svn_commit_env
{
1547 if (defined $LC_ALL) {
1548 $ENV{LC_ALL
} = $LC_ALL;
1550 delete $ENV{LC_ALL
};
1554 sub svn_commit_tree
{
1555 my ($last, $commit) = @_;
1556 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1557 my $log_msg = get_commit_message
($commit, $commit_msg);
1558 my ($oneline) = ($log_msg->{msg
} =~ /([^\n\r]+)/);
1559 print "Committing $commit: $oneline\n";
1561 set_svn_commit_env
();
1562 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
1565 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1566 if (!defined $committed) {
1567 my $out = join("\n",@ci_output);
1568 print STDERR
"W: Trouble parsing \`svn commit' output:\n\n",
1569 $out, "\n\nAssuming English locale...";
1570 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1571 defined $committed or die " FAILED!\n",
1572 "Commit output failed to parse committed revision!\n",
1573 print STDERR
" OK\n";
1576 my @svn_up = qw(svn up);
1577 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1578 if ($_optimize_commits && ($committed == ($last->{revision
} + 1))) {
1579 push @svn_up, "-r$committed";
1581 my $info = svn_info
('.');
1582 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1583 if ($info->{'Last Changed Rev'} != $committed) {
1584 croak
"$info->{'Last Changed Rev'} != $committed\n"
1586 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1587 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1588 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1589 or croak
"Failed to parse date: $date\n";
1590 $log_msg->{date
} = "$tz $Y-$m-$d $H:$M:$S";
1591 $log_msg->{author
} = $info->{'Last Changed Author'};
1592 $log_msg->{revision
} = $committed;
1593 $log_msg->{msg
} .= "\n";
1594 $log_msg->{parents
} = [ $last->{commit
} ];
1595 $log_msg->{commit
} = git_commit
($log_msg, $commit);
1598 # resync immediately
1599 push @svn_up, "-r$last->{revision}";
1601 return fetch
("$committed=$commit");
1606 my $pid = open my $fh, '-|';
1607 defined $pid or croak
$!;
1609 exec(qw
/git-rev-list --pretty=raw/, @args) or croak
$!;
1611 return { fh
=> $fh, t
=> { } };
1614 sub next_rev_list_entry
{
1619 if (/^commit ($sha1)$/o) {
1621 $rl->{t
} = { c
=> $1 };
1626 } elsif (/^parent ($sha1)$/o) {
1633 return ($x != $rl->{t
}) ?
$x : undef;
1636 # read the entire log into a temporary file (which is removed ASAP)
1637 # and store the file handle + parser state
1639 my (@log_args) = @_;
1640 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
1642 defined $pid or croak
$!;
1644 open STDOUT
, '>&', $log_fh or croak
$!;
1645 exec (qw(svn log), @log_args) or croak
$!
1649 seek $log_fh, 0, 0 or croak
$!;
1650 return { state => 'sep', fh
=> $log_fh };
1653 sub next_log_entry
{
1654 my $log = shift; # retval of svn_log_raw()
1656 my $fh = $log->{fh
};
1661 if ($log->{state} eq 'msg') {
1662 if ($ret->{lines
}) {
1663 $ret->{msg
} .= $_."\n";
1664 unless(--$ret->{lines
}) {
1665 $log->{state} = 'sep';
1668 croak
"Log parse error at: $_\n",
1674 if ($log->{state} ne 'sep') {
1675 croak
"Log parse error at: $_\n",
1676 "state: $log->{state}\n",
1680 $log->{state} = 'rev';
1682 # if we have an empty log message, put something there:
1684 $ret->{msg
} ||= "\n";
1685 delete $ret->{lines
};
1690 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1692 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1693 ($lines) = ($lines =~ /(\d+)/);
1694 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1695 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1696 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1697 or croak
"Failed to parse date: $date\n";
1698 $ret = { revision
=> $rev,
1699 date
=> "$tz $Y-$m-$d $H:$M:$S",
1703 if (defined $_authors && ! defined $users{$author}) {
1704 die "Author: $author not defined in ",
1707 $log->{state} = 'msg_start';
1710 # skip the first blank line of the message:
1711 if ($log->{state} eq 'msg_start' && /^$/) {
1712 $log->{state} = 'msg';
1713 } elsif ($log->{state} eq 'msg') {
1714 if ($ret->{lines
}) {
1715 $ret->{msg
} .= $_."\n";
1716 unless (--$ret->{lines
}) {
1717 $log->{state} = 'sep';
1720 croak
"Log parse error at: $_\n",
1721 $ret->{revision
},"\n";
1729 my $url = shift || $SVN_URL;
1731 my $pid = open my $info_fh, '-|';
1732 defined $pid or croak
$!;
1735 exec(qw(svn info),$url) or croak
$!;
1739 # only single-lines seem to exist in svn info output
1740 while (<$info_fh>) {
1742 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
1744 push @
{$ret->{-order
}}, $1;
1747 close $info_fh or croak
$?
;
1751 sub sys
{ system(@_) == 0 or croak
$?
}
1754 my ($from, $to) = @_;
1755 my $es = svn_propget_base
('svn:eol-style', $to);
1756 open my $rfd, '<', $from or croak
$!;
1757 binmode $rfd or croak
$!;
1758 open my $wfd, '>', $to or croak
$!;
1759 binmode $wfd or croak
$!;
1760 eol_cp_fd
($rfd, $wfd, $es);
1761 close $rfd or croak
$!;
1762 close $wfd or croak
$!;
1766 my ($rfd, $wfd, $es) = @_;
1767 my $eol = defined $es ?
$EOL{$es} : undef;
1772 defined($r = sysread($rfd, $buf, 4096)) or croak
$!;
1775 if ($buf =~ /\015$/) {
1777 defined($r = sysread($rfd,$c,1)) or croak
$!;
1778 $buf .= $c if $r > 0;
1780 $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
1783 for ($w = 0; $w < $r; $w += $t) {
1784 $t = syswrite($wfd, $buf, $r - $w, $w) or croak
$!;
1790 sub do_update_index
{
1791 my ($z_cmd, $cmd, $no_text_base) = @_;
1793 my $z = open my $p, '-|';
1794 defined $z or croak
$!;
1795 unless ($z) { exec @
$z_cmd or croak
$! }
1797 my $pid = open my $ui, '|-';
1798 defined $pid or croak
$!;
1800 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
1803 while (my $x = <$p>) {
1805 if (!$no_text_base && lstat $x && ! -l _
&&
1806 svn_propget_base
('svn:keywords', $x)) {
1807 my $mode = -x _ ?
0755 : 0644;
1808 my ($v,$d,$f) = File
::Spec
->splitpath($x);
1809 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
1810 'text-base',"$f.svn-base");
1813 $tb = File
::Spec
->catfile($d, '.svn',
1814 'text-base',"$f.svn-base");
1817 unlink $x or croak
$!;
1819 chmod(($mode &~ umask), $x) or croak
$!;
1823 close $ui or croak
$?
;
1827 return if $_use_lib;
1829 if (!-f
"$GIT_SVN_DIR/info/exclude") {
1830 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak
$!;
1831 print $fd '.svn',"\n";
1832 close $fd or croak
$!;
1834 my $no_text_base = shift;
1835 do_update_index
([qw
/git-diff-files --name-only -z/],
1838 do_update_index
([qw
/git-ls-files -z --others/,
1839 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1845 my ($str, $file, $mode) = @_;
1846 open my $fd,'>',$file or croak
$!;
1847 print $fd $str,"\n" or croak
$!;
1848 close $fd or croak
$!;
1849 chmod ($mode &~ umask, $file) if (defined $mode);
1854 open my $fd,'<',$file or croak
"$!: file: $file\n";
1857 close $fd or croak
$!;
1862 sub assert_revision_unknown
{
1864 if (my $c = revdb_get
($REVDB, $r)) {
1865 croak
"$r = $c already exists! Why are we refetching it?";
1871 my @x = safe_qx
('git-cat-file','commit',$x);
1872 my @y = safe_qx
('git-cat-file','commit',$y);
1873 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1874 || $y[0] !~ /^tree $sha1\n$/) {
1875 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
1882 my ($log_msg, @parents) = @_;
1883 assert_revision_unknown
($log_msg->{revision
});
1884 map_tree_joins
() if (@_branch_from && !%tree_map);
1886 my (@tmp_parents, @exec_parents, %seen_parent);
1887 if (my $lparents = $log_msg->{parents
}) {
1888 @tmp_parents = @
$lparents
1890 # commit parents can be conditionally bound to a particular
1891 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1892 foreach my $p (@parents) {
1893 next unless defined $p;
1894 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1895 if ($1 == $log_msg->{revision
}) {
1896 push @tmp_parents, $2;
1899 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1902 my $tree = $log_msg->{tree
};
1903 if (!defined $tree) {
1904 my $index = set_index
($GIT_SVN_INDEX);
1906 chomp($tree = `git-write-tree`);
1908 restore_index
($index);
1911 # just in case we clobber the existing ref, we still want that ref
1913 if (my $cur = eval { file_to_s
("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1914 push @tmp_parents, $cur;
1917 if (exists $tree_map{$tree}) {
1918 foreach my $p (@
{$tree_map{$tree}}) {
1920 foreach (@tmp_parents) {
1921 # see if a common parent is found
1923 safe_qx
('git-merge-base', $_, $p)
1930 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
1931 next if (($SVN_UUID eq $uuid_p) &&
1932 ($log_msg->{revision
} > $r_p));
1933 next if (defined $url_p && defined $SVN_URL &&
1934 ($SVN_UUID eq $uuid_p) &&
1935 ($url_p eq $SVN_URL));
1936 push @tmp_parents, $p;
1939 foreach (@tmp_parents) {
1940 next if $seen_parent{$_};
1941 $seen_parent{$_} = 1;
1942 push @exec_parents, $_;
1943 # MAXPARENT is defined to 16 in commit-tree.c:
1944 last if @exec_parents > 16;
1947 set_commit_env
($log_msg);
1948 my @exec = ('git-commit-tree', $tree);
1949 push @exec, '-p', $_ foreach @exec_parents;
1950 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1952 print $msg_fh $log_msg->{msg
} or croak
$!;
1953 unless ($_no_metadata) {
1954 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1955 " $SVN_UUID\n" or croak
$!;
1957 $msg_fh->flush == 0 or croak
$!;
1958 close $msg_fh or croak
$!;
1959 chomp(my $commit = do { local $/; <$out_fh> });
1960 close $out_fh or croak
$!;
1963 if ($commit !~ /^$sha1$/o) {
1964 die "Failed to commit, invalid sha1: $commit\n";
1966 sys
('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1967 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
1969 # this output is read via pipe, do not change:
1970 print "r$log_msg->{revision} = $commit\n";
1976 if ($_repack && (--$_repack_nr == 0)) {
1977 $_repack_nr = $_repack;
1978 sys
("git repack $_repack_flags");
1982 sub set_commit_env
{
1984 my $author = $log_msg->{author
};
1985 if (!defined $author || length $author == 0) {
1986 $author = '(no author)';
1988 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
1989 : ($author,"$author\@$SVN_UUID");
1990 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1991 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1992 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
1995 sub apply_mod_line_blob
{
1997 if ($m->{mode_b
} =~ /^120/) {
1998 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
2000 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
2004 sub blob_to_symlink
{
2005 my ($blob, $link) = @_;
2006 defined $link or croak
"\$link not defined!\n";
2007 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2008 if (-l
$link || -f _
) {
2009 unlink $link or croak
$!;
2012 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2013 symlink $dest, $link or croak
$!;
2017 my ($blob, $file) = @_;
2018 defined $file or croak
"\$file not defined!\n";
2019 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2020 if (-l
$file || -f _
) {
2021 unlink $file or croak
$!;
2024 open my $blob_fh, '>', $file or croak
"$!: $file\n";
2026 defined $pid or croak
$!;
2029 open STDOUT
, '>&', $blob_fh or croak
$!;
2030 exec('git-cat-file','blob',$blob) or croak
$!;
2035 close $blob_fh or croak
$!;
2039 my $pid = open my $child, '-|';
2040 defined $pid or croak
$!;
2042 exec(@_) or croak
$!;
2044 my @ret = (<$child>);
2045 close $child or croak
$?
;
2046 die $?
if $?
; # just in case close didn't error out
2047 return wantarray ?
@ret : join('',@ret);
2050 sub svn_compat_check
{
2051 if ($_follow_parent) {
2052 print STDERR
'E: --follow-parent functionality is only ',
2053 "available when SVN libraries are used\n";
2056 my @co_help = safe_qx
(qw(svn co -h));
2057 unless (grep /ignore-externals/,@co_help) {
2058 print STDERR
"W: Installed svn version does not support ",
2059 "--ignore-externals\n";
2060 $_no_ignore_ext = 1;
2062 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2063 $_svn_co_url_revs = 1;
2065 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2066 $_svn_pg_peg_revs = 1;
2069 # I really, really hope nobody hits this...
2070 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
2072 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
2074 Lets hope the directory you
're tracking is not a branch or tag
2075 and was never moved within the repository...
2081 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
2082 # (and they won't honor URL@<rev> without -r<rev>, too!)
2083 sub svn_cmd_checkout
{
2084 my ($url, $rev, $dir) = @_;
2085 my @cmd = ('svn','co', "-r$rev");
2086 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2087 $url .= "\@$rev" if $_svn_co_url_revs;
2088 sys
(@cmd, $url, $dir);
2091 sub check_upgrade_needed
{
2093 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2094 open my $fh, '>>',$REVDB or croak
$!;
2098 my $pid = open my $child, '-|';
2099 defined $pid or croak
$!;
2102 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$!;
2104 my @ret = (<$child>);
2105 close $child or croak
$?
;
2106 die $?
if $?
; # just in case close didn't error out
2107 return wantarray ?
@ret : join('',@ret);
2110 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
2112 print STDERR
"Please run: $0 rebuild --upgrade\n";
2117 # fills %tree_map with a reverse mapping of trees to commits. Useful
2118 # for finding parents to commit on.
2119 sub map_tree_joins
{
2121 foreach my $br (@_branch_from) {
2122 my $pid = open my $pipe, '-|';
2123 defined $pid or croak
$!;
2125 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2129 if (/^commit ($sha1)$/o) {
2132 # if we've seen a commit,
2133 # we've seen its parents
2134 last if $seen{$commit};
2135 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2136 unless (defined $tree) {
2137 die "Failed to parse commit $commit\n";
2139 push @
{$tree_map{$tree}}, $commit;
2143 close $pipe; # we could be breaking the pipe early
2148 if (@_branch_from) {
2149 print STDERR
'--branch|-b parameters are ignored when ',
2150 "--branch-all-refs|-B is passed\n";
2153 # don't worry about rev-list on non-commit objects/tags,
2154 # it shouldn't blow up if a ref is a blob or tree...
2155 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2158 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2160 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2161 while (<$authors>) {
2163 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2164 my ($user, $name, $email) = ($1, $2, $3);
2165 $users{$user} = [$name, $email];
2167 close $authors or croak
$!;
2171 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2172 while (<$authors>) {
2174 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2175 my ($user, $name, $email) = ($1, $2, $3);
2176 $rusers{"$name <$email>"} = $user;
2178 close $authors or croak
$!;
2181 sub svn_propget_base
{
2183 $f .= '@BASE' if $_svn_pg_peg_revs;
2184 return safe_qx
(qw
/svn propget/, $p, $f);
2189 foreach (`git-rev-parse --symbolic --all`) {
2190 next unless s
#^refs/remotes/##;
2192 next unless -f
"$GIT_DIR/svn/$_/info/url";
2200 defined(my $pid = fork) or croak
$!;
2202 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2204 exit 0 if -r
$REVDB;
2205 print "Upgrading svn => git mapping...\n";
2206 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2207 open my $fh, '>>',$REVDB or croak
$!;
2210 print "Done upgrading. You may now delete the ",
2211 "deprecated $GIT_SVN_DIR/revs directory\n";
2219 sub migration_check
{
2220 migrate_revdb
() unless (-e
$REVDB);
2221 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
2222 print "Upgrading repository...\n";
2223 unless (-d
"$GIT_DIR/svn") {
2224 mkdir "$GIT_DIR/svn" or croak
$!;
2226 print "Data from a previous version of git-svn exists, but\n\t",
2227 "$GIT_SVN_DIR\n\t(required for this version ",
2228 "($VERSION) of git-svn) does not.\n";
2230 foreach my $x (`git-rev-parse --symbolic --all`) {
2231 next unless $x =~ s
#^refs/remotes/##;
2233 next unless -f
"$GIT_DIR/$x/info/url";
2234 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
2236 my $dn = dirname
("$GIT_DIR/svn/$x");
2237 mkpath
([$dn]) unless -d
$dn;
2238 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
2240 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
2241 print "Done upgrading.\n";
2244 sub find_rev_before
{
2245 my ($r, $id, $eq_ok) = @_;
2246 my $f = "$GIT_DIR/svn/$id/.rev_db";
2247 return (undef,undef) unless -r
$f;
2250 if (my $c = revdb_get
($f, $r)) {
2255 return (undef, undef);
2259 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
2260 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2261 $REVDB = "$GIT_SVN_DIR/.rev_db";
2262 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2264 $SVN_WC = "$GIT_SVN_DIR/tree";
2268 # convert GetOpt::Long specs for use by git-repo-config
2269 sub read_repo_config
{
2270 return unless -d
$GIT_DIR;
2272 foreach my $o (keys %$opts) {
2273 my $v = $opts->{$o};
2274 my ($key) = ($o =~ /^([a-z\-]+)/);
2276 my $arg = 'git-repo-config';
2277 $arg .= ' --int' if ($o =~ /[:=]i$/);
2278 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2279 if (ref $v eq 'ARRAY') {
2280 chomp(my @tmp = `$arg --get-all svn.$key`);
2283 chomp(my $tmp = `$arg --get svn.$key`);
2284 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2291 sub set_default_vals
{
2292 if (defined $_repack) {
2293 $_repack = 1000 if ($_repack <= 0);
2294 $_repack_nr = $_repack;
2295 $_repack_flags ||= '-d';
2300 my $gr_file = shift;
2301 my ($grafts, $comments) = ({}, {});
2302 if (open my $fh, '<', $gr_file) {
2305 if (/^($sha1)\s+/) {
2308 @
{$comments->{$c}} = @tmp;
2311 foreach my $p (split /\s+/, $_) {
2312 $grafts->{$c}->{$p} = 1;
2318 close $fh or croak
$!;
2319 @
{$comments->{'END'}} = @tmp if @tmp;
2321 return ($grafts, $comments);
2325 my ($grafts, $comments, $gr_file) = @_;
2327 open my $fh, '>', $gr_file or croak
$!;
2328 foreach my $c (sort keys %$grafts) {
2329 if ($comments->{$c}) {
2330 print $fh $_ foreach @
{$comments->{$c}};
2332 my $p = $grafts->{$c};
2333 my %x; # real parents
2334 delete $p->{$c}; # commits are not self-reproducing...
2335 my $pid = open my $ch, '-|';
2336 defined $pid or croak
$!;
2338 exec(qw
/git-cat-file commit/, $c) or croak
$!;
2341 if (/^parent ($sha1)/) {
2342 $x{$1} = $p->{$1} = 1;
2347 close $ch; # breaking the pipe
2349 # if real parents are the only ones in the grafts, drop it
2350 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2354 @ip = @jp = keys %$p;
2355 foreach my $i (@ip) {
2356 next if $del{$i} || $p->{$i} == 2;
2357 foreach my $j (@jp) {
2358 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2359 $mb = eval { safe_qx
('git-merge-base',$i,$j) };
2366 } elsif ($mb eq $i) {
2373 # if real parents are the only ones in the grafts, drop it
2374 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2376 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2378 if ($comments->{'END'}) {
2379 print $fh $_ foreach @
{$comments->{'END'}};
2381 close $fh or croak
$!;
2384 sub read_url_paths_all
{
2385 my ($l_map, $pfx, $p) = @_;
2388 if (-r
"$_/info/url") {
2389 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2390 my $id = $pfx . basename
$_;
2391 my $url = file_to_s
("$_/info/url");
2392 my ($u, $p) = repo_path_split
($url);
2393 $l_map->{$u}->{$p} = $id;
2400 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2401 read_url_paths_all
($l_map, $x, $_);
2405 # this one only gets ids that have been imported, not new ones
2406 sub read_url_paths
{
2408 git_svn_each
(sub { my $x = shift;
2409 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
2410 my ($u, $p) = repo_path_split
($url);
2411 $l_map->{$u}->{$p} = $x;
2416 sub extract_metadata
{
2417 my $id = shift or return (undef, undef, undef);
2418 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
2420 if (!$rev || !$uuid || !$url) {
2421 # some of the original repositories I made had
2422 # indentifiers like this:
2423 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2425 return ($url, $rev, $uuid);
2429 return extract_metadata
((grep(/^git-svn-id: /,
2430 safe_qx
(qw
/git-cat-file commit/, shift)))[-1]);
2433 sub get_commit_time
{
2435 defined(my $pid = open my $fh, '-|') or croak
$!;
2437 exec qw
/git-rev-list --pretty=raw -n1/, $cmt or croak
$!;
2440 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2441 my ($s, $tz) = ($1, $2);
2442 if ($tz =~ s/^\+//) {
2443 $s += tz_to_s_offset
($tz);
2444 } elsif ($tz =~ s/^\-//) {
2445 $s -= tz_to_s_offset
($tz);
2450 die "Can't get commit time for commit: $cmt\n";
2453 sub tz_to_s_offset
{
2456 return ($1 * 60) + ($tz * 3600);
2459 sub setup_pager
{ # translated to Perl from pager.c
2460 return unless (-t
*STDOUT
);
2461 my $pager = $ENV{PAGER
};
2462 if (!defined $pager) {
2464 } elsif (length $pager == 0 || $pager eq 'cat') {
2467 pipe my $rfd, my $wfd or return;
2468 defined(my $pid = fork) or croak
$!;
2470 open STDOUT
, '>&', $wfd or croak
$!;
2473 open STDIN
, '<&', $rfd or croak
$!;
2474 $ENV{LESS
} ||= '-S';
2475 exec $pager or croak
"Can't run pager: $!\n";;
2478 sub get_author_info
{
2479 my ($dest, $author, $t, $tz) = @_;
2480 $author =~ s/(?:^\s*|\s*$)//g;
2481 $dest->{a_raw
} = $author;
2484 $_a = $rusers{$author} || undef;
2487 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2492 # Date::Parse isn't in the standard Perl distro :(
2493 if ($tz =~ s/^\+//) {
2494 $t += tz_to_s_offset
($tz);
2495 } elsif ($tz =~ s/^\-//) {
2496 $t -= tz_to_s_offset
($tz);
2498 $dest->{t_utc
} = $t;
2501 sub process_commit
{
2502 my ($c, $r_min, $r_max, $defer) = @_;
2503 if (defined $r_min && defined $r_max) {
2504 if ($r_min == $c->{r
} && $r_min == $r_max) {
2508 return 1 if $r_min == $r_max;
2509 if ($r_min < $r_max) {
2510 # we need to reverse the print order
2511 return 0 if (defined $_limit && --$_limit < 0);
2515 if ($r_min != $r_max) {
2516 return 1 if ($r_min < $c->{r
});
2517 return 1 if ($r_max > $c->{r
});
2520 return 0 if (defined $_limit && --$_limit < 0);
2529 if (my $l = $c->{l
}) {
2530 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2533 $_l_fmt ||= 'A' . length($c->{r
});
2534 print 'r',pack($_l_fmt, $c->{r
}),' | ';
2535 print "$c->{c} | " if $_show_commit;
2538 show_commit_normal
($c);
2542 sub show_commit_normal
{
2544 print '-' x72
, "\nr$c->{r} | ";
2545 print "$c->{c} | " if $_show_commit;
2546 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2547 localtime($c->{t_utc
})), ' | ';
2550 if (my $l = $c->{l
}) {
2551 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2554 $nr_line = scalar @
$l;
2556 print "1 line\n\n\n";
2558 if ($nr_line == 1) {
2559 $nr_line = '1 line';
2561 $nr_line .= ' lines';
2563 print $nr_line, "\n\n";
2564 print $_ foreach @
$l;
2570 foreach my $x (qw
/raw diff/) {
2573 print $_ foreach @
{$c->{$x}}
2579 return unless $_use_lib;
2582 if ($SVN::Core
::VERSION
lt '1.1.0') {
2583 die "Need SVN::Core 1.1.0 or better ",
2584 "(got $SVN::Core::VERSION) ",
2585 "Falling back to command-line svn\n";
2589 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
2590 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2591 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2592 $SVN::Node
::none
.$SVN::Node
::file
.
2593 $SVN::Node
::dir
.$SVN::Node
::unknown
;
2598 sub libsvn_connect
{
2600 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
(),
2601 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2602 SVN
::Client
::get_username_provider
()]);
2603 my $s = eval { SVN
::Ra
->new(url
=> $url, auth
=> $auth) };
2607 sub libsvn_get_file
{
2608 my ($gui, $f, $rev) = @_;
2610 return unless ($p =~ s
#^\Q$SVN_PATH\E/?##);
2612 my ($hash, $pid, $in, $out);
2613 my $pool = SVN
::Pool
->new;
2614 defined($pid = open3
($in, $out, '>&STDERR',
2615 qw
/git-hash-object -w --stdin/)) or croak
$!;
2616 # redirect STDOUT for SVN 1.1.x compatibility
2617 open my $stdout, '>&', \
*STDOUT
or croak
$!;
2618 open STDOUT
, '>&', $in or croak
$!;
2619 my ($r, $props) = $SVN->get_file($f, $rev, \
*STDOUT
, $pool);
2620 $in->flush == 0 or croak
$!;
2621 open STDOUT
, '>&', $stdout or croak
$!;
2622 close $in or croak
$!;
2623 close $stdout or croak
$!;
2625 chomp($hash = do { local $/; <$out> });
2626 close $out or croak
$!;
2628 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2630 my $mode = exists $props->{'svn:executable'} ?
'100755' : '100644';
2631 if (exists $props->{'svn:special'}) {
2633 my $link = `git-cat-file blob $hash`;
2634 $link =~ s/^link // or die "svn:special file with contents: <",
2635 $link, "> is not understood\n";
2636 defined($pid = open3
($in, $out, '>&STDERR',
2637 qw
/git-hash-object -w --stdin/)) or croak
$!;
2639 $in->flush == 0 or croak
$!;
2640 close $in or croak
$!;
2641 chomp($hash = do { local $/; <$out> });
2642 close $out or croak
$!;
2644 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2646 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak
$!;
2649 sub libsvn_log_entry
{
2650 my ($rev, $author, $date, $msg, $parents) = @_;
2651 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2652 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2653 or die "Unable to parse date: $date\n";
2654 if (defined $_authors && ! defined $users{$author}) {
2655 die "Author: $author not defined in $_authors file\n";
2657 return { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2658 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [] }
2662 my ($gui, $last_commit, $f) = @_;
2663 $f =~ s
#^\Q$SVN_PATH\E/?## or return;
2664 # remove entire directories.
2665 if (safe_qx
('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2666 defined(my $pid = open my $ls, '-|') or croak
$!;
2668 exec(qw
/git-ls-tree -r --name-only -z/,
2669 $last_commit,'--',$f) or croak
$!;
2673 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2675 close $ls or croak
$?
;
2677 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2682 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2683 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2685 foreach my $f (keys %$paths) {
2686 my $m = $paths->{$f}->action();
2688 if ($m =~ /^[DR]$/) {
2689 print "\t$m\t$f\n" unless $_q;
2690 process_rm
($gui, $last_commit, $f);
2692 # 'R' can be file replacements, too, right?
2694 my $pool = SVN
::Pool
->new;
2695 my $t = $SVN->check_path($f, $rev, $pool);
2696 if ($t == $SVN::Node
::file
) {
2697 if ($m =~ /^[AMR]$/) {
2698 push @amr, [ $m, $f ];
2700 die "Unrecognized action: $m, ($f r$rev)\n";
2706 print "\t$_->[0]\t$_->[1]\n" unless $_q;
2707 libsvn_get_file
($gui, $_->[1], $rev)
2709 close $gui or croak
$?
;
2710 return libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2713 sub svn_grab_base_rev
{
2714 defined(my $pid = open my $fh, '-|') or croak
$!;
2716 open my $null, '>', '/dev/null' or croak
$!;
2717 open STDERR
, '>&', $null or croak
$!;
2718 exec qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2721 chomp(my $c = do { local $/; <$fh> });
2723 if (defined $c && length $c) {
2724 my ($url, $rev, $uuid) = cmt_metadata
($c);
2725 return ($rev, $c) if defined $rev;
2727 if ($_no_metadata) {
2728 my $offset = -41; # from tail
2730 open my $fh, '<', $REVDB or
2731 die "--no-metadata specified and $REVDB not readable\n";
2732 seek $fh, $offset, 2;
2734 defined $rl or return (undef, undef);
2736 while ($c ne $rl && tell $fh != 0) {
2738 seek $fh, $offset, 2;
2740 defined $rl or return (undef, undef);
2744 croak
$! if ($rev < -1);
2745 $rev = ($rev - 41) / 41;
2746 close $fh or croak
$!;
2749 return (undef, undef);
2752 sub libsvn_parse_revision
{
2754 my $head = $SVN->get_latest_revnum();
2755 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2756 return ($base + 1, $head) if (defined $base);
2759 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2760 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2761 if ($_revision =~ /^BASE:(\d+)$/) {
2762 return ($base + 1, $1) if (defined $base);
2765 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2766 die "revision argument: $_revision not understood by git-svn\n",
2767 "Try using the command-line svn client instead\n";
2770 sub libsvn_traverse
{
2771 my ($gui, $pfx, $path, $rev) = @_;
2772 my $cwd = "$pfx/$path";
2773 my $pool = SVN
::Pool
->new;
2775 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2776 foreach my $d (keys %$dirent) {
2777 my $t = $dirent->{$d}->kind;
2778 if ($t == $SVN::Node
::dir
) {
2779 libsvn_traverse
($gui, $cwd, $d, $rev);
2780 } elsif ($t == $SVN::Node
::file
) {
2781 print "\tA\t$cwd/$d\n" unless $_q;
2782 libsvn_get_file
($gui, "$cwd/$d", $rev);
2788 sub libsvn_traverse_ignore
{
2789 my ($fh, $path, $r) = @_;
2791 my $pool = SVN
::Pool
->new;
2792 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2794 $p =~ s
#^\Q$SVN_PATH\E/?##;
2795 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2796 if (my $s = $props->{'svn:ignore'}) {
2797 $s =~ s/[\r\n]+/\n/g;
2799 if (length $p == 0) {
2803 $s =~ s
#\n#\n/$p/#g;
2804 print $fh "/$p/$s\n";
2807 foreach (sort keys %$dirent) {
2808 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2809 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2815 my ($path, $r0, $r1) = @_;
2816 return 1 if $r0 == $r1;
2819 # should be OK to use Pool here (r1 - r0) should be small
2820 my $pool = SVN
::Pool
->new;
2821 libsvn_get_log
($SVN, "/$path", $r0, $r1,
2822 0, 1, 1, sub {$nr++}, $pool);
2825 my ($url, undef) = repo_path_split
($SVN_URL);
2826 my $svn_log = svn_log_raw
("$url/$path","-r$r0:$r1");
2827 while (next_log_entry
($svn_log)) { $nr++ }
2828 close $svn_log->{fh
};
2830 return 0 if ($nr > 1);
2834 sub libsvn_find_parent_branch
{
2835 my ($paths, $rev, $author, $date, $msg) = @_;
2836 my $svn_path = '/'.$SVN_PATH;
2838 # look for a parent from another branch:
2839 my $i = $paths->{$svn_path} or return;
2840 my $branch_from = $i->copyfrom_path or return;
2841 my $r = $i->copyfrom_rev;
2842 print STDERR
"Found possible branch point: ",
2843 "$branch_from => $svn_path, $r\n";
2844 $branch_from =~ s
#^/##;
2846 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
2847 my $url = $SVN->{url
};
2848 defined $l_map->{$url} or return;
2849 my $id = $l_map->{$url}->{$branch_from};
2850 if (!defined $id && $_follow_parent) {
2851 print STDERR
"Following parent: $branch_from\@$r\n";
2852 # auto create a new branch and follow it
2853 $id = basename
($branch_from);
2854 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
2855 while (-r
"$GIT_DIR/svn/$id") {
2856 # just grow a tail if we're not unique enough :x
2860 return unless defined $id;
2862 my ($r0, $parent) = find_rev_before
($r,$id,1);
2863 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2864 defined(my $pid = fork) or croak
$!;
2866 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2868 $SVN_URL = "$url/$branch_from";
2869 $SVN_LOG = $SVN = undef;
2871 # we can't assume SVN_URL exists at r+1:
2872 $_revision = "0:$r";
2878 ($r0, $parent) = find_rev_before
($r,$id,1);
2880 return unless (defined $r0 && defined $parent);
2881 if (revisions_eq
($branch_from, $r0, $r)) {
2882 unlink $GIT_SVN_INDEX;
2883 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
2884 sys
(qw
/git-read-tree/, $parent);
2885 return libsvn_fetch
($parent, $paths, $rev,
2886 $author, $date, $msg);
2888 print STDERR
"Nope, branch point not imported or unknown\n";
2892 sub libsvn_get_log
{
2893 my ($ra, @args) = @_;
2894 if ($SVN::Core
::VERSION
le '1.2.0') {
2895 splice(@args, 3, 1);
2897 $ra->get_log(@args);
2900 sub libsvn_new_tree
{
2901 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
2904 my ($paths, $rev, $author, $date, $msg) = @_;
2905 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2906 my $pool = SVN
::Pool
->new;
2907 libsvn_traverse
($gui, '', $SVN_PATH, $rev, $pool);
2909 close $gui or croak
$?
;
2910 return libsvn_log_entry
($rev, $author, $date, $msg);
2913 sub find_graft_path_commit
{
2914 my ($tree_paths, $p1, $r1) = @_;
2915 foreach my $x (keys %$tree_paths) {
2916 next unless ($p1 =~ /^\Q$x\E/);
2917 my $i = $tree_paths->{$x};
2918 my ($r0, $parent) = find_rev_before
($r1,$i,1);
2919 return $parent if (defined $r0 && $r0 == $r1);
2920 print STDERR
"r$r1 of $i not imported\n";
2926 sub find_graft_path_parents
{
2927 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2928 foreach my $x (keys %$tree_paths) {
2929 next unless ($p0 =~ /^\Q$x\E/);
2930 my $i = $tree_paths->{$x};
2931 my ($r, $parent) = find_rev_before
($r0, $i, 1);
2932 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
2933 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
2934 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
2935 next if ($url_a && $url_b && $url_a eq $url_b &&
2936 $uuid_b eq $uuid_a);
2937 $grafts->{$c}->{$parent} = 1;
2942 sub libsvn_graft_file_copies
{
2943 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2944 foreach (keys %$paths) {
2945 my $i = $paths->{$_};
2946 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2948 next unless (defined $p0 && defined $r0);
2953 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
2955 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
2960 my $old = $ENV{GIT_INDEX_FILE
};
2961 $ENV{GIT_INDEX_FILE
} = shift;
2968 $ENV{GIT_INDEX_FILE
} = $old;
2970 delete $ENV{GIT_INDEX_FILE
};
2974 sub libsvn_commit_cb
{
2975 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2976 if ($_optimize_commits && $rev == ($r_last + 1)) {
2977 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
2978 $log->{tree
} = get_tree_from_treeish
($c);
2979 my $cmt = git_commit
($log, $cmt_last, $c);
2980 my @diff = safe_qx
('git-diff-tree', $cmt, $c);
2982 print STDERR
"Trees differ: $cmt $c\n",
2983 join('',@diff),"\n";
2991 sub libsvn_ls_fullurl
{
2992 my $fullurl = shift;
2993 my ($repo, $path) = repo_path_split
($fullurl);
2994 $SVN ||= libsvn_connect
($repo);
2996 my $pool = SVN
::Pool
->new;
2997 my ($dirent, undef, undef) = $SVN->get_dir($path,
2998 $SVN->get_latest_revnum, $pool);
2999 foreach my $d (keys %$dirent) {
3000 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
3001 push @ret, "$d/"; # add '/' for compat with cli svn
3009 sub libsvn_skip_unknown_revs
{
3011 my $errno = $err->apr_err();
3012 # Maybe the branch we're tracking didn't
3013 # exist when the repo started, so it's
3014 # not an error if it doesn't, just continue
3016 # Wonderfully consistent library, eh?
3017 # 160013 - svn:// and file://
3018 # 175002 - http(s)://
3019 # More codes may be discovered later...
3020 if ($errno == 175002 || $errno == 160013) {
3023 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
3026 # Tie::File seems to be prone to offset errors if revisions get sparse,
3027 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3028 # one of my favorite modules is out :< Next up would be one of the DBM
3029 # modules, but I'm not sure which is most portable... So I'll just
3030 # go with something that's plain-text, but still capable of
3031 # being randomly accessed. So here's my ultra-simple fixed-width
3032 # database. All records are 40 characters + "\n", so it's easy to seek
3033 # to a revision: (41 * rev) is the byte offset.
3034 # A record of 40 0s denotes an empty revision.
3035 # And yes, it's still pretty fast (faster than Tie::File).
3037 my ($file, $rev, $commit) = @_;
3038 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
3039 open my $fh, '+<', $file or croak
$!;
3040 my $offset = $rev * 41;
3041 # assume that append is the common case:
3042 seek $fh, 0, 2 or croak
$!;
3044 if ($pos < $offset) {
3045 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
3047 seek $fh, $offset, 0 or croak
$!;
3048 print $fh $commit,"\n";
3049 close $fh or croak
$!;
3053 my ($file, $rev) = @_;
3055 my $offset = $rev * 41;
3056 open my $fh, '<', $file or croak
$!;
3057 seek $fh, $offset, 0;
3058 if (tell $fh == $offset) {
3059 $ret = readline $fh;
3062 $ret = undef if ($ret =~ /^0{40}$/);
3065 close $fh or croak
$!;
3069 sub copy_remote_ref
{
3070 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
3071 my $ref = "refs/remotes/$GIT_SVN";
3072 if (safe_qx
('git-ls-remote', $origin, $ref)) {
3073 sys
(qw
/git fetch/, $origin, "$ref:$ref");
3075 die "Unable to find remote reference: ",
3076 "refs/remotes/$GIT_SVN on $origin\n";
3080 package SVN
::Git
::Editor
;
3089 my $git_svn = shift;
3090 my $self = SVN
::Delta
::Editor
->new(@_);
3091 bless $self, $class;
3092 foreach (qw
/svn_path c r ra /) {
3093 die "$_ required!\n" unless (defined $git_svn->{$_});
3094 $self->{$_} = $git_svn->{$_};
3096 $self->{pool
} = SVN
::Pool
->new;
3097 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3099 require Digest
::MD5
;
3104 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3108 (defined $_[1] && length $_[1]) ?
"$_[0]->{svn_path}/$_[1]"
3113 my ($self, $path) = @_;
3114 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
3118 my ($self, $q) = @_;
3119 my $rm = $self->{rm
};
3120 delete $rm->{''}; # we never delete the url we're tracking
3123 foreach (keys %$rm) {
3124 my @d = split m
#/#, $_;
3128 $c .= '/' . shift @d;
3132 delete $rm->{$self->{svn_path
}};
3133 delete $rm->{''}; # we never delete the url we're tracking
3136 defined(my $pid = open my $fh,'-|') or croak
$!;
3138 exec qw
/git-ls-tree --name-only -r -z/, $self->{c
} or croak
$!;
3141 my @svn_path = split m
#/#, $self->{svn_path};
3144 my @dn = (@svn_path, (split m
#/#, $_));
3146 delete $rm->{join '/', @dn};
3155 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3156 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3157 $self->close_directory($bat->{$d}, $p);
3158 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3159 print "\tD+\t/$d/\n" unless $q;
3160 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3165 sub open_or_add_dir
{
3166 my ($self, $full_path, $baton) = @_;
3167 my $p = SVN
::Pool
->new;
3168 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
3170 if ($t == $SVN::Node
::none
) {
3171 return $self->add_directory($full_path, $baton,
3172 undef, -1, $self->{pool
});
3173 } elsif ($t == $SVN::Node
::dir
) {
3174 return $self->open_directory($full_path, $baton,
3175 $self->{r
}, $self->{pool
});
3177 print STDERR
"$full_path already exists in repository at ",
3178 "r$self->{r} and it is not a directory (",
3179 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3184 my ($self, $path) = @_;
3185 my $bat = $self->{bat
};
3186 $path = $self->repo_path($path);
3187 return $bat->{''} unless (length $path);
3188 my @p = split m
#/+#, $path;
3190 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3193 $c .= '/' . shift @p;
3194 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3200 my ($self, $m, $q) = @_;
3201 my ($dir, $file) = split_path
($m->{file_b
});
3202 my $pbat = $self->ensure_path($dir);
3203 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3205 print "\tA\t$m->{file_b}\n" unless $q;
3206 $self->chg_file($fbat, $m);
3207 $self->close_file($fbat,undef,$self->{pool
});
3211 my ($self, $m, $q) = @_;
3212 my ($dir, $file) = split_path
($m->{file_b
});
3213 my $pbat = $self->ensure_path($dir);
3214 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3215 $self->url_path($m->{file_a
}), $self->{r
});
3216 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3217 $self->chg_file($fbat, $m);
3218 $self->close_file($fbat,undef,$self->{pool
});
3222 my ($self, $path, $pbat) = @_;
3223 my $rpath = $self->repo_path($path);
3224 my ($dir, $file) = split_path
($rpath);
3225 $self->{rm
}->{$dir} = 1;
3226 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3230 my ($self, $m, $q) = @_;
3231 my ($dir, $file) = split_path
($m->{file_b
});
3232 my $pbat = $self->ensure_path($dir);
3233 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3234 $self->url_path($m->{file_a
}), $self->{r
});
3235 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3236 $self->chg_file($fbat, $m);
3237 $self->close_file($fbat,undef,$self->{pool
});
3239 ($dir, $file) = split_path
($m->{file_a
});
3240 $pbat = $self->ensure_path($dir);
3241 $self->delete_entry($m->{file_a
}, $pbat);
3245 my ($self, $m, $q) = @_;
3246 my ($dir, $file) = split_path
($m->{file_b
});
3247 my $pbat = $self->ensure_path($dir);
3248 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3249 $pbat,$self->{r
},$self->{pool
});
3250 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3251 $self->chg_file($fbat, $m);
3252 $self->close_file($fbat,undef,$self->{pool
});
3255 sub T
{ shift->M(@_) }
3257 sub change_file_prop
{
3258 my ($self, $fbat, $pname, $pval) = @_;
3259 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3263 my ($self, $fbat, $m) = @_;
3264 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3265 $self->change_file_prop($fbat,'svn:executable','*');
3266 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3267 $self->change_file_prop($fbat,'svn:executable',undef);
3269 my $fh = IO
::File
->new_tmpfile or croak
$!;
3270 if ($m->{mode_b
} =~ /^120/) {
3271 print $fh 'link ' or croak
$!;
3272 $self->change_file_prop($fbat,'svn:special','*');
3273 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3274 $self->change_file_prop($fbat,'svn:special',undef);
3276 defined(my $pid = fork) or croak
$!;
3278 open STDOUT
, '>&', $fh or croak
$!;
3279 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3283 $fh->flush == 0 or croak
$!;
3284 seek $fh, 0, 0 or croak
$!;
3286 my $md5 = Digest
::MD5
->new;
3287 $md5->addfile($fh) or croak
$!;
3288 seek $fh, 0, 0 or croak
$!;
3290 my $exp = $md5->hexdigest;
3291 my $atd = $self->apply_textdelta($fbat, undef, $self->{pool
});
3292 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $self->{pool
});
3293 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3295 close $fh or croak
$!;
3299 my ($self, $m, $q) = @_;
3300 my ($dir, $file) = split_path
($m->{file_b
});
3301 my $pbat = $self->ensure_path($dir);
3302 print "\tD\t$m->{file_b}\n" unless $q;
3303 $self->delete_entry($m->{file_b
}, $pbat);
3308 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3309 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3310 $self->close_directory($bat->{$_}, $p);
3312 $self->SUPER::close_edit
($p);
3318 $self->SUPER::abort_edit
($self->{pool
});
3319 $self->{pool
}->clear;
3326 $svn_log hashref
(as returned by svn_log_raw
)
3328 fh
=> file handle of the
log file
,
3329 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
3332 $log_msg hashref as returned by next_log_entry
($svn_log)
3334 msg
=> 'whitespace-formatted log entry
3335 ', # trailing newline is preserved
3336 revision
=> '8', # integer
3337 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3338 author
=> 'committer name'
3342 @mods = array of diff
-index line hashes
, each element represents one line
3343 of diff
-index output
3345 diff
-index line
($m hash
)
3347 mode_a
=> first column of diff
-index output
, no leading
':',
3348 mode_b
=> second column of diff
-index output
,
3349 sha1_b
=> sha1sum of the final blob
,
3350 chg
=> change type
[MCRADT
],
3351 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3352 file_b
=> new
/current file name of a file
(any chg
)
3356 # retval of read_url_paths{,_all}();
3358 # repository root url
3359 'https://svn.musicpd.org' => {
3360 # repository path # GIT_SVN_ID
3361 'mpd/trunk' => 'trunk',
3362 'mpd/tags/0.11.5' => 'tags/0.11.5',
3367 I don
't trust the each() function on unless I created %hash myself
3368 because the internal iterator may not have started at base.