2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
14 $GIT_DIR = abs_path
($ENV{GIT_DIR
} || '.git');
15 $ENV{GIT_DIR
} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL
};
19 # make sure the svn binary gives consistent output between locales and TZs:
22 $| = 1; # unbuffer STDOUT
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
27 # We don't use any modules not in the standard Perl distribution:
30 use File
::Basename qw
/dirname basename/;
31 use File
::Path qw
/mkpath/;
32 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
34 use File
::Copy qw
/copy/;
35 use POSIX qw
/strftime/;
38 memoize
('revisions_eq');
39 memoize
('cmt_metadata');
40 memoize
('get_commit_time');
42 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
43 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB
};
45 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
46 my $sha1 = qr/[a-f\d]{40}/;
47 my $sha1_short = qr/[a-f\d]{4,40}/;
48 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
49 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
50 $_repack, $_repack_nr, $_repack_flags, $_q,
51 $_message, $_file, $_follow_parent, $_no_metadata,
52 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
53 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
54 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
55 $_merge, $_strategy, $_dry_run, $_ignore_nodate);
56 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
57 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
58 my @repo_path_split_cache;
60 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
61 'branch|b=s' => \
@_branch_from,
62 'follow-parent|follow' => \
$_follow_parent,
63 'branch-all-refs|B' => \
$_branch_all_refs,
64 'authors-file|A=s' => \
$_authors,
65 'repack:i' => \
$_repack,
66 'no-metadata' => \
$_no_metadata,
68 'ignore-nodate' => \
$_ignore_nodate,
69 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
71 my ($_trunk, $_tags, $_branches);
72 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
73 'tags|t=s' => \
$_tags,
74 'branches|b=s' => \
$_branches );
75 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
76 my %cmt_opts = ( 'edit|e' => \
$_edit,
78 'find-copies-harder' => \
$_find_copies_harder,
80 'copy-similarity|C=i'=> \
$_cp_similarity
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,
123 dcommit
=> [ \
&dcommit
, 'Commit several diffs to merge with upstream',
124 { 'merge|m|M' => \
$_merge,
125 'strategy|s=s' => \
$_strategy,
126 'dry-run|n' => \
$_dry_run,
131 for (my $i = 0; $i < @ARGV; $i++) {
132 if (defined $cmd{$ARGV[$i]}) {
139 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
141 read_repo_config
(\
%opts);
142 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
143 'version|V' => \
$_version,
144 'id|i=s' => \
$GIT_SVN);
145 exit 1 if (!$rv && $cmd ne 'log');
149 version
() if $_version;
150 usage
(1) unless defined $cmd;
152 load_authors
() if $_authors;
153 load_all_refs
() if $_branch_all_refs;
154 svn_compat_check
() unless $_use_lib;
155 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
156 $cmd{$cmd}->[0]->(@ARGV);
159 ####################### primary functions ######################
161 my $exit = shift || 0;
162 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
164 git
-svn
- bidirectional operations between a single Subversion tree
and git
165 Usage
: $0 <command
> [options
] [arguments
]\n
167 print $fd "Available commands:\n" unless $cmd;
169 foreach (sort keys %cmd) {
170 next if $cmd && $cmd ne $_;
171 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
172 foreach (keys %{$cmd{$_}->[2]}) {
173 # prints out arguments as they should be passed:
174 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
175 print $fd ' ' x
17, join(', ', map { length $_ > 1 ?
177 split /\|/,$_)," $x\n";
181 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
182 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
183 one git repository and want to keep them separate. See git-svn(1) for more
190 print "git-svn version $VERSION\n";
195 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
198 $SVN_URL = shift or undef;
201 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
203 check_upgrade_needed();
206 my $pid = open(my $rev_list,'-|');
207 defined $pid or croak $!;
209 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
212 while (<$rev_list>) {
215 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
216 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
217 next if (!@commit); # skip merges
218 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
219 if (!$rev || !$uuid) {
220 croak "Unable to extract revision or UUID from ",
221 "$c, $commit[$#commit]\n";
224 # if we merged or otherwise started elsewhere, this is
225 # how we break out of it
226 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
227 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
229 unless (defined $latest) {
230 if (!$SVN_URL && !$url) {
231 croak "SVN repository location required: $url\n";
238 revdb_set($REVDB, $rev, $c);
239 print "r$rev = $c\n";
240 $newest_rev = $rev if ($rev > $newest_rev);
242 close $rev_list or croak $?;
244 goto out if $_use_lib;
245 if (!chdir $SVN_WC) {
246 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
247 chdir $SVN_WC or croak $!;
251 defined $pid or croak $!;
253 my @svn_up = qw(svn up);
254 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
255 sys
(@svn_up,"-r$newest_rev");
256 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
258 exec('git-write-tree') or croak
$!;
265 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
266 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
272 my $url = shift or die "SVN repository location required " .
273 "as a command-line argument\n";
274 $url =~ s!/+$!!; # strip trailing slash
276 if (my $repo_path = shift) {
277 unless (-d
$repo_path) {
278 mkpath
([$repo_path]);
280 $GIT_DIR = $ENV{GIT_DIR
} = $repo_path . "/.git";
285 unless (-d
$GIT_DIR) {
286 my @init_db = ('git-init-db');
287 push @init_db, "--template=$_template" if defined $_template;
288 push @init_db, "--shared" if defined $_shared;
295 check_upgrade_needed
();
296 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
297 my $ret = $_use_lib ? fetch_lib
(@_) : fetch_cmd
(@_);
298 if ($ret->{commit
} && quiet_run
(qw(git-rev-parse --verify
299 refs/heads/master^0))) {
300 sys
(qw(git-update-ref refs/heads/master),$ret->{commit
});
307 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
308 unless ($_revision) {
309 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
311 push @log_args, "-r$_revision";
312 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
314 my $svn_log = svn_log_raw
(@log_args);
316 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
317 # don't need last_revision from grab_base_rev() because
318 # user could've specified a different revision to skip (they
319 # didn't want to import certain revisions into git for whatever
320 # reason, so trust $base->{revision} instead.
321 my (undef, $last_commit) = svn_grab_base_rev
();
322 unless (-d
$SVN_WC) {
323 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
324 chdir $SVN_WC or croak
$!;
326 $last_commit = git_commit
($base, @parents);
327 assert_tree
($last_commit);
329 chdir $SVN_WC or croak
$!;
331 # looks like a user manually cp'd and svn switch'ed
332 unless ($last_commit) {
333 sys
(qw
/svn revert -R ./);
334 assert_svn_wc_clean
($base->{revision
});
335 $last_commit = git_commit
($base, @parents);
336 assert_tree
($last_commit);
339 my @svn_up = qw(svn up);
340 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
342 while (my $log_msg = next_log_entry
($svn_log)) {
343 if ($last->{revision
} >= $log_msg->{revision
}) {
344 croak
"Out of order: last >= current: ",
345 "$last->{revision} >= $log_msg->{revision}\n";
347 # Revert is needed for cases like:
348 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
349 # I can't seem to reproduce something like that on a test...
350 sys
(qw
/svn revert -R ./);
351 assert_svn_wc_clean
($last->{revision
});
352 sys
(@svn_up,"-r$log_msg->{revision}");
353 $last_commit = git_commit
($log_msg, $last_commit, @parents);
356 close $svn_log->{fh
};
357 $last->{commit
} = $last_commit;
363 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
365 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
366 $SVN_LOG ||= libsvn_connect
($repo);
367 $SVN ||= libsvn_connect
($repo);
368 my ($last_rev, $last_commit) = svn_grab_base_rev
();
369 my ($base, $head) = libsvn_parse_revision
($last_rev);
371 return { revision
=> $last_rev, commit
=> $last_commit }
373 my $index = set_index
($GIT_SVN_INDEX);
375 # limit ourselves and also fork() since get_log won't release memory
376 # after processing a revision and SVN stuff seems to leak
378 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
380 if (defined $last_commit) {
381 unless (-e
$GIT_SVN_INDEX) {
382 sys
(qw
/git-read-tree/, $last_commit);
384 chomp (my $x = `git-write-tree`);
385 my ($y) = (`git-cat-file commit $last_commit`
386 =~ /^tree ($sha1)/m);
388 unlink $GIT_SVN_INDEX or croak
$!;
389 sys
(qw
/git-read-tree/, $last_commit);
391 chomp ($x = `git-write-tree`);
393 print STDERR
"trees ($last_commit) $y != $x\n",
394 "Something is seriously wrong...\n";
398 # fork, because using SVN::Pool with get_log() still doesn't
399 # seem to help enough to keep memory usage down.
400 defined(my $pid = fork) or croak
$!;
402 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
404 # Yes I'm perfectly aware that the fourth argument
405 # below is the limit revisions number. Unfortunately
406 # performance sucks with it enabled, so it's much
407 # faster to fetch revision ranges instead of relying
409 libsvn_get_log
($SVN_LOG, '/'.$SVN_PATH,
414 $log_msg = libsvn_fetch
(
416 $last_commit = git_commit
(
421 $log_msg = libsvn_new_tree
(@_);
422 $last_commit = git_commit
(
430 ($last_rev, $last_commit) = svn_grab_base_rev
();
431 last if ($max >= $head);
434 $max = $head if ($max > $head);
436 restore_index
($index);
437 return { revision
=> $last_rev, commit
=> $last_commit };
442 check_upgrade_needed
();
443 if ($_stdin || !@commits) {
444 print "Reading from stdin...\n";
447 if (/\b($sha1_short)\b/o) {
448 unshift @commits, $1;
453 foreach my $c (@commits) {
454 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
455 if (scalar @tmp == 1) {
457 } elsif (scalar @tmp > 1) {
458 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
460 die "Failed to rev-parse $c\n";
464 $_use_lib ? commit_lib
(@revs) : commit_cmd
(@revs);
465 print "Done committing ",scalar @revs," revisions to SVN\n";
471 chdir $SVN_WC or croak
"Unable to chdir $SVN_WC: $!\n";
472 my $info = svn_info
('.');
473 my $fetched = fetch
();
474 if ($info->{Revision
} != $fetched->{revision
}) {
475 print STDERR
"There are new revisions that were fetched ",
476 "and need to be merged (or acknowledged) ",
477 "before committing.\n";
480 $info = svn_info
('.');
483 foreach my $c (@revs) {
484 my $mods = svn_checkout_tree
($last, $c);
485 if (scalar @
$mods == 0) {
486 print "Skipping, no changes detected\n";
489 $last = svn_commit_tree
($last, $c);
495 my ($r_last, $cmt_last) = svn_grab_base_rev
();
496 defined $r_last or die "Must have an existing revision to commit\n";
497 my $fetched = fetch
();
498 if ($r_last != $fetched->{revision
}) {
499 print STDERR
"There are new revisions that were fetched ",
500 "and need to be merged (or acknowledged) ",
501 "before committing.\n",
502 "last rev: $r_last\n",
503 " current: $fetched->{revision}\n";
507 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
508 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
511 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
512 set_svn_commit_env
();
513 foreach my $c (@revs) {
514 my $log_msg = get_commit_message
($c, $commit_msg);
516 # fork for each commit because there's a memory leak I
517 # can't track down... (it's probably in the SVN code)
518 defined(my $pid = open my $fh, '-|') or croak
$!;
520 $SVN_LOG = libsvn_connect
($repo);
521 $SVN = libsvn_connect
($repo);
522 my $ed = SVN
::Git
::Editor
->new(
526 svn_path
=> $SVN_PATH
528 $SVN->get_commit_editor(
539 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
541 print "No changes\nr$r_last = $cmt_last\n";
548 my ($r_new, $cmt_new, $no);
552 if (/^r(\d+) = ($sha1)$/o) {
553 ($r_new, $cmt_new) = ($1, $2);
554 } elsif ($_ eq 'No changes') {
558 close $fh or croak
$?
;
559 if (! defined $r_new && ! defined $cmt_new) {
561 die "Failed to parse revision information\n";
564 ($r_last, $cmt_last) = ($r_new, $cmt_new);
572 my $gs = "refs/remotes/$GIT_SVN";
573 chomp(my @refs = safe_qx
(qw
/git-rev-list --no-merges/, "$gs..HEAD"));
574 foreach my $d (reverse @refs) {
576 print "diff-tree $d~1 $d\n";
578 commit_diff
("$d~1", $d);
583 my @diff = safe_qx
(qw
/git-diff-tree HEAD/, $gs);
586 @finish = qw
/rebase/;
587 push @finish, qw
/--merge/ if $_merge;
588 push @finish, "--strategy=$_strategy" if $_strategy;
589 print STDERR
"W: HEAD and $gs differ, using @finish:\n", @diff;
591 print "No changes between current HEAD and $gs\n",
592 "Hard resetting to the latest $gs\n";
593 @finish = qw
/reset --hard/;
595 sys
('git', @finish, $gs);
599 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
600 $_use_lib ? show_ignore_lib
() : show_ignore_cmd
();
603 sub show_ignore_cmd
{
604 require File
::Find
or die $!;
605 if (defined $_revision) {
606 die "-r/--revision option doesn't work unless the Perl SVN ",
607 "libraries are used\n";
609 chdir $SVN_WC or croak
$!;
611 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
613 @
{$ign{$_}} = svn_propget_base
('svn:ignore', $_);
614 }}, no_chdir
=>1},'.');
617 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
619 foreach my $i (sort keys %ign) {
620 print "\n# ",$i,"\n";
621 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
625 sub show_ignore_lib
{
627 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
628 $SVN ||= libsvn_connect
($repo);
629 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
630 libsvn_traverse_ignore
(\
*STDOUT
, $SVN_PATH, $r);
634 my $gr_file = "$GIT_DIR/info/grafts";
635 my ($grafts, $comments) = read_grafts
($gr_file);
639 # temporarily disable our grafts file to make this idempotent
640 chomp($gr_sha1 = safe_qx
(qw
/git-hash-object -w/,$gr_file));
641 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
644 my $l_map = read_url_paths
();
645 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
646 unless ($_no_default_regex) {
647 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
648 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
649 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
651 foreach my $u (keys %$l_map) {
653 foreach my $p (keys %{$l_map->{$u}}) {
654 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
657 unless ($_no_graft_copy) {
659 graft_file_copy_lib
($grafts,$l_map,$u);
661 graft_file_copy_cmd
($grafts,$l_map,$u);
665 graft_tree_joins
($grafts);
667 write_grafts
($grafts, $comments, $gr_file);
668 unlink "$gr_file~$gr_sha1" if $gr_sha1;
675 $url =~ s
#/+$## if $url;
676 if ($_trunk !~ m
#^[a-z\+]+://#) {
677 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
679 print STDERR
"E: '$_trunk' is not a complete URL ",
680 "and a separate URL is not specified\n";
683 $_trunk = $url . $_trunk;
686 if ($GIT_SVN eq 'git-svn') {
688 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
691 unless (-d
$GIT_SVN_DIR) {
692 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
694 sys
('git-repo-config', 'svn.trunk', $_trunk);
696 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
697 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
701 # try to do trunk first, since branches/tags
702 # may be descended from it.
703 if (-e
"$GIT_DIR/svn/trunk/info/url") {
704 fetch_child_id
('trunk', @_);
706 rec_fetch
('', "$GIT_DIR/svn", @_);
712 my $r_last = -1; # prevent dupes
713 rload_authors
() if $_authors;
719 if (defined $_revision) {
720 if ($_revision =~ /^(\d+):(\d+)$/) {
721 ($r_min, $r_max) = ($1, $2);
722 } elsif ($_revision =~ /^\d+$/) {
723 $r_min = $r_max = $_revision;
725 print STDERR
"-r$_revision is not supported, use ",
726 "standard \'git log\' arguments instead\n";
731 my $pid = open(my $log,'-|');
732 defined $pid or croak
$!;
734 exec(git_svn_log_cmd
($r_min,$r_max), @args) or croak
$!;
740 if (/^commit ($sha1_short)/o) {
742 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
744 process_commit
($c, $r_min, $r_max, \
@k) or
749 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
750 get_author_info
($c, $1, $2, $3);
751 } elsif (/^(?:tree|parent|committer) /) {
753 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
754 push @
{$c->{raw
}}, $_;
757 push @
{$c->{diff
}}, $_;
759 push @
{$c->{diff
}}, $_;
760 } elsif (/^ (git-svn-id:.+)$/) {
761 (undef, $c->{r
}, undef) = extract_metadata
($1);
766 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
768 process_commit
($c, $r_min, $r_max, \
@k);
774 process_commit
($_, $r_min, $r_max) foreach reverse @k;
778 print '-' x72
,"\n" unless $_incremental || $_oneline;
781 sub commit_diff_usage
{
782 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
788 print STDERR
"commit-diff must be used with SVN libraries\n";
791 my $ta = shift or commit_diff_usage
();
792 my $tb = shift or commit_diff_usage
();
793 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
794 print STDERR
"Needed URL or usable git-svn id command-line\n";
797 if (defined $_message && defined $_file) {
798 print STDERR
"Both --message/-m and --file/-F specified ",
799 "for the commit message.\n",
800 "I have no idea what you mean\n";
803 if (defined $_file) {
804 $_message = file_to_s
($_file);
806 $_message ||= get_commit_message
($tb,
807 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
810 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
811 $SVN_LOG ||= libsvn_connect
($repo);
812 $SVN ||= libsvn_connect
($repo);
813 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
814 my $ed = SVN
::Git
::Editor
->new({ r
=> $SVN->get_latest_revnum,
815 ra
=> $SVN, c
=> $tb,
816 svn_path
=> $SVN_PATH
818 $SVN->get_commit_editor($_message,
819 sub {print "Committed $_[0]\n"},@lock)
821 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
823 print "No changes\n$ta == $tb\n";
828 $_message = $_file = undef;
831 ########################### utility functions #########################
835 return 1 if defined $c->{r
};
836 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
837 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
838 my @msg = safe_qx
(qw
/git-cat-file commit/, $c->{c
});
839 shift @msg while ($msg[0] ne "\n");
841 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
843 (undef, $c->{r
}, undef) = extract_metadata
(
844 (grep(/^git-svn-id: /, @msg))[-1]);
846 return defined $c->{r
};
849 sub git_svn_log_cmd
{
850 my ($r_min, $r_max) = @_;
851 my @cmd = (qw
/git
-log --abbrev
-commit
--pretty
=raw
852 --default/, "refs/remotes
/$GIT_SVN");
853 push @cmd, '--summary' if $_verbose;
854 return @cmd unless defined $r_max;
855 if ($r_max == $r_min) {
856 push @cmd, '--max-count=1';
857 if (my $c = revdb_get($REVDB, $r_max)) {
862 $c_max = revdb_get($REVDB, $r_max);
863 $c_min = revdb_get($REVDB, $r_min);
864 if ($c_min && $c_max) {
865 if ($r_max > $r_max) {
866 push @cmd, "$c_min..$c_max";
868 push @cmd, "$c_max..$c_min";
870 } elsif ($r_max > $r_min) {
881 print "Fetching
$id\n";
882 my $ref = "$GIT_DIR/refs/remotes
/$id";
883 defined(my $pid = open my $fh, '-|') or croak $!;
886 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
893 check_repack() if (/^r\d+ = $sha1/);
895 close $fh or croak $?;
899 my ($pfx, $p, @args) = @_;
901 foreach (sort <$p/*>) {
902 if (-r "$_/info/url
") {
903 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
904 my $id = $pfx . basename $_;
905 next if $id eq 'trunk';
906 fetch_child_id($id, @args);
913 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
918 sub complete_url_ls_init {
919 my ($url, $var, $switch, $pfx) = @_;
921 print STDERR "W
: $switch not specified
\n";
925 if ($var !~ m#^[a-z\+]+://#) {
926 $var = '/' . $var if ($var !~ m#^/#);
928 print STDERR "E
: '$var' is
not a complete URL
",
929 "and a separate URL is
not specified
\n";
934 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
935 : safe_qx(qw/svn ls --non-interactive/, $var));
937 defined(my $pid = fork) or croak $!;
939 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
941 if ($u !~ m!\Q$var\E/(.+)$!) {
942 print STDERR
"W: Unrecognized URL: $u\n";
943 die "This should never happen\n";
945 # don't try to init already existing refs
947 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
949 unless (-d
$GIT_SVN_DIR) {
950 print "init $u => $id\n";
958 my ($n) = ($switch =~ /^--(\w+)/);
959 sys
('git-repo-config', "svn.$n", $var);
966 my @tmp = split m
#/#, $_;
968 while (my $x = shift @tmp) {
974 foreach (sort {length $b <=> length $a} keys %common) {
975 if ($common{$_} == @
$paths) {
982 # grafts set here are 'stronger' in that they're based on actual tree
983 # matches, and won't be deleted from merge-base checking in write_grafts()
984 sub graft_tree_joins
{
986 map_tree_joins
() if (@_branch_from && !%tree_map);
987 return unless %tree_map;
991 defined(my $pid = open my $fh, '-|') or croak
$!;
993 exec qw
/git-rev-list --pretty=raw/,
994 "refs/remotes/$i" or croak
$!;
997 next unless /^commit ($sha1)$/o;
999 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1000 next unless $tree_map{$t};
1005 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1007 my ($s, $tz) = ($1, $2);
1008 if ($tz =~ s/^\+//) {
1009 $s += tz_to_s_offset
($tz);
1010 } elsif ($tz =~ s/^\-//) {
1011 $s -= tz_to_s_offset
($tz);
1014 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
1016 foreach my $p (@
{$tree_map{$t}}) {
1019 safe_qx
('git-merge-base', $c, $p)
1021 next unless ($@
|| $?
);
1023 # see if SVN says it's a relative
1024 my ($url_b, $r_b, $uuid_b) =
1026 next if (defined $url_b &&
1028 ($url_a eq $url_b) &&
1029 ($uuid_a eq $uuid_b));
1030 if ($uuid_a eq $uuid_b) {
1032 $grafts->{$c}->{$p} = 2;
1034 } elsif ($r_b > $r_a) {
1035 $grafts->{$p}->{$c} = 2;
1040 my $ct = get_commit_time
($p);
1042 $grafts->{$c}->{$p} = 2;
1043 } elsif ($ct > $s) {
1044 $grafts->{$p}->{$c} = 2;
1046 # what should we do when $ct == $s ?
1049 close $fh or croak
$?
;
1053 # this isn't funky-filename safe, but good enough for now...
1054 sub graft_file_copy_cmd
{
1055 my ($grafts, $l_map, $u) = @_;
1056 my $paths = $l_map->{$u};
1057 my $pfx = common_prefix
([keys %$paths]);
1058 $SVN_URL ||= $u.$pfx;
1059 my $pid = open my $fh, '-|';
1060 defined $pid or croak
$!;
1062 my @exec = qw
/svn log -v/;
1063 push @exec, "-r$_revision" if defined $_revision;
1064 exec @exec, $u.$pfx or croak
$!;
1066 my ($r, $mp) = (undef, undef);
1071 } elsif (/^r(\d+) \| /) {
1072 $r = $1 unless defined $r;
1073 } elsif (/^Changed paths:/) {
1075 } elsif ($mp && m
#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1076 my ($p1, $p0, $r0) = ($1, $2, $3);
1077 my $c = find_graft_path_commit
($paths, $p1, $r);
1079 find_graft_path_parents
($grafts, $paths, $c, $p0, $r0);
1084 sub graft_file_copy_lib
{
1085 my ($grafts, $l_map, $u) = @_;
1086 my $tree_paths = $l_map->{$u};
1087 my $pfx = common_prefix
([keys %$tree_paths]);
1088 my ($repo, $path) = repo_path_split
($u.$pfx);
1089 $SVN_LOG ||= libsvn_connect
($repo);
1090 $SVN ||= libsvn_connect
($repo);
1092 my ($base, $head) = libsvn_parse_revision
();
1094 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1095 my $eh = $SVN::Error
::handler
;
1096 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1098 my $pool = SVN
::Pool
->new;
1099 libsvn_get_log
($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1101 libsvn_graft_file_copies
($grafts, $tree_paths,
1105 last if ($max >= $head);
1108 $max = $head if ($max > $head);
1110 $SVN::Error
::handler
= $eh;
1113 sub process_merge_msg_matches
{
1114 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1115 my (@strong, @weak);
1116 foreach (@matches) {
1117 # merging with ourselves is not interesting
1119 if ($l_map->{$u}->{$_}) {
1125 foreach my $w (@weak) {
1127 # no exact match, use branch name as regexp.
1128 my $re = qr/\Q$w\E/i;
1129 foreach (keys %{$l_map->{$u}}) {
1131 push @strong, $l_map->{$u}->{$_};
1138 foreach (keys %{$l_map->{$u}}) {
1140 push @strong, $l_map->{$u}->{$_};
1145 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1146 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1147 unless (defined $rev) {
1148 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1149 \@
(?
:[a
-f\d\
-]+)/xsm
);
1150 return unless defined $rev;
1152 foreach my $m (@strong) {
1153 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1154 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1158 sub graft_merge_msg
{
1159 my ($grafts, $l_map, $u, $p, @re) = @_;
1161 my $x = $l_map->{$u}->{$p};
1162 my $rl = rev_list_raw
($x);
1163 while (my $c = next_rev_list_entry
($rl)) {
1164 foreach my $re (@re) {
1165 my (@br) = ($c->{m
} =~ /$re/g);
1167 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1173 return if $SVN_UUID;
1175 my $pool = SVN
::Pool
->new;
1176 $SVN_UUID = $SVN->get_uuid($pool);
1179 my $info = shift || svn_info
('.');
1180 $SVN_UUID = $info->{'Repository UUID'} or
1181 croak
"Repository UUID unreadable\n";
1187 defined $pid or croak
$!;
1189 open my $null, '>', '/dev/null' or croak
$!;
1190 open STDERR
, '>&', $null or croak
$!;
1191 open STDOUT
, '>&', $null or croak
$!;
1192 exec @_ or croak
$!;
1198 sub repo_path_split
{
1199 my $full_url = shift;
1200 $full_url =~ s
#/+$##;
1202 foreach (@repo_path_split_cache) {
1203 if ($full_url =~ s
#$_##) {
1205 $full_url =~ s
#^/+##;
1206 return ($u, $full_url);
1211 my $tmp = libsvn_connect
($full_url);
1212 my $url = $tmp->get_repos_root;
1213 $full_url =~ s
#^\Q$url\E/*##;
1214 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1215 return ($url, $full_url);
1217 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1219 my @paths = split(m
#/+#, $path);
1220 while (quiet_run
(qw
/svn ls --non-interactive/, $url)) {
1221 my $n = shift @paths || last;
1224 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1225 $path = join('/',@paths);
1226 return ($url, $path);
1231 defined $SVN_URL or croak
"SVN repository location required\n";
1232 unless (-d
$GIT_DIR) {
1233 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1235 mkpath
([$GIT_SVN_DIR]);
1236 mkpath
(["$GIT_SVN_DIR/info"]);
1237 open my $fh, '>>',$REVDB or croak
$!;
1239 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1243 sub assert_svn_wc_clean
{
1244 return if $_use_lib;
1246 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1247 my $lcr = svn_info
('.')->{'Last Changed Rev'};
1248 if ($svn_rev != $lcr) {
1249 print STDERR
"Checking for copy-tree ... ";
1250 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
1251 "-r$lcr:$svn_rev")));
1253 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
1255 print STDERR
"OK!\n";
1258 my @status = grep(!/^Performing status on external/,(`svn status`));
1259 @status = grep(!/^\s*$/,@status);
1260 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1261 if (scalar @status) {
1262 print STDERR
"Tree ($SVN_WC) is not clean:\n";
1263 print STDERR
$_ foreach @status;
1268 sub get_tree_from_treeish
{
1270 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1271 chomp(my $type = `git-cat-file -t $treeish`);
1273 while ($type eq 'tag') {
1274 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1276 if ($type eq 'commit') {
1277 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1278 ($expected) = ($expected =~ /^tree ($sha1)$/);
1279 die "Unable to get tree from $treeish\n" unless $expected;
1280 } elsif ($type eq 'tree') {
1281 $expected = $treeish;
1283 die "$treeish is a $type, expected tree, tag or commit\n";
1289 return if $_use_lib;
1291 my $expected = get_tree_from_treeish
($treeish);
1293 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1295 unlink $tmpindex or croak
$!;
1297 my $old_index = set_index
($tmpindex);
1299 chomp(my $tree = `git-write-tree`);
1300 restore_index
($old_index);
1301 if ($tree ne $expected) {
1302 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
1307 sub parse_diff_tree
{
1308 my $diff_fh = shift;
1312 while (<$diff_fh>) {
1313 chomp $_; # this gets rid of the trailing "\0"
1314 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1315 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1316 push @mods, { mode_a
=> $1, mode_b
=> $2,
1317 sha1_b
=> $3, chg
=> $4 };
1318 if ($4 =~ /^(?:C|R)$/) {
1323 } elsif ($state eq 'file_a') {
1324 my $x = $mods[$#mods] or croak
"Empty array\n";
1325 if ($x->{chg
} !~ /^(?:C|R)$/) {
1326 croak
"Error parsing $_, $x->{chg}\n";
1330 } elsif ($state eq 'file_b') {
1331 my $x = $mods[$#mods] or croak
"Empty array\n";
1332 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1333 croak
"Error parsing $_, $x->{chg}\n";
1335 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1336 croak
"Error parsing $_, $x->{chg}\n";
1341 croak
"Error parsing $_\n";
1344 close $diff_fh or croak
$?
;
1349 sub svn_check_prop_executable
{
1351 return if -l
$m->{file_b
};
1352 if ($m->{mode_b
} =~ /755$/) {
1353 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
1354 if ($m->{mode_a
} !~ /755$/) {
1355 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
1357 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
1358 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1359 sys
(qw(svn propdel svn:executable), $m->{file_b
});
1360 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
1361 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
1365 sub svn_ensure_parent_path
{
1366 my $dir_b = dirname
(shift);
1367 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
1368 mkpath
([$dir_b]) unless (-d
$dir_b);
1369 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
1372 sub precommit_check
{
1374 my (%rm_file, %rmdir_check, %added_check);
1376 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
1377 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1378 if ($m->{chg
} eq 'R') {
1379 if (-d
$m->{file_b
}) {
1380 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1382 # dir/$file => dir/file/$file
1383 my $dirname = dirname
($m->{file_b
});
1384 while ($dirname ne File
::Spec
->curdir) {
1385 if ($dirname ne $m->{file_a
}) {
1386 $dirname = dirname
($dirname);
1389 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
1391 # baz/zzz => baz (baz is a file)
1392 $dirname = dirname
($m->{file_a
});
1393 while ($dirname ne File
::Spec
->curdir) {
1394 if ($dirname ne $m->{file_b
}) {
1395 $dirname = dirname
($dirname);
1398 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1401 if ($m->{chg
} =~ /^(D|R)$/) {
1402 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
1403 $rm_file{ $m->{$t} } = 1;
1404 my $dirname = dirname
( $m->{$t} );
1405 my $basename = basename
( $m->{$t} );
1406 $rmdir_check{$dirname}->{$basename} = 1;
1407 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
1408 if (-d
$m->{file_b
}) {
1409 err_dir_to_file
($m->{file_b
});
1411 my $dirname = dirname
( $m->{file_b
} );
1412 my $basename = basename
( $m->{file_b
} );
1413 $added_check{$dirname}->{$basename} = 1;
1414 while ($dirname ne File
::Spec
->curdir) {
1415 if ($rm_file{$dirname}) {
1416 err_file_to_dir
($m->{file_b
});
1418 $dirname = dirname
$dirname;
1422 return (\
%rmdir_check, \
%added_check);
1424 sub err_dir_to_file
{
1426 print STDERR
"Node change from directory to file ",
1427 "is not supported by Subversion: ",$file,"\n";
1430 sub err_file_to_dir
{
1432 print STDERR
"Node change from file to directory ",
1433 "is not supported by Subversion: ",$file,"\n";
1440 my ($from, $treeish) = @_;
1442 print "diff-tree $from $treeish\n";
1443 my $pid = open my $diff_fh, '-|';
1444 defined $pid or croak
$!;
1446 my @diff_tree = qw(git-diff-tree -z -r);
1447 if ($_cp_similarity) {
1448 push @diff_tree, "-C$_cp_similarity";
1450 push @diff_tree, '-C';
1452 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1453 push @diff_tree, "-l$_l" if defined $_l;
1454 exec(@diff_tree, $from, $treeish) or croak
$!;
1456 return parse_diff_tree
($diff_fh);
1459 sub svn_checkout_tree
{
1460 my ($from, $treeish) = @_;
1461 my $mods = get_diff
($from->{commit
}, $treeish);
1462 return $mods unless (scalar @
$mods);
1463 my ($rm, $add) = precommit_check
($mods);
1465 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1466 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1467 if ($m->{chg
} eq 'C') {
1468 svn_ensure_parent_path
( $m->{file_b
} );
1469 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
1470 apply_mod_line_blob
($m);
1471 svn_check_prop_executable
($m);
1472 } elsif ($m->{chg
} eq 'D') {
1473 sys
(qw(svn rm --force), $m->{file_b
});
1474 } elsif ($m->{chg
} eq 'R') {
1475 svn_ensure_parent_path
( $m->{file_b
} );
1476 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
1477 apply_mod_line_blob
($m);
1478 svn_check_prop_executable
($m);
1479 } elsif ($m->{chg
} eq 'M') {
1480 apply_mod_line_blob
($m);
1481 svn_check_prop_executable
($m);
1482 } elsif ($m->{chg
} eq 'T') {
1483 sys
(qw(svn rm --force),$m->{file_b
});
1484 apply_mod_line_blob
($m);
1485 sys
(qw(svn add), $m->{file_b
});
1486 svn_check_prop_executable
($m);
1487 } elsif ($m->{chg
} eq 'A') {
1488 svn_ensure_parent_path
( $m->{file_b
} );
1489 apply_mod_line_blob
($m);
1490 sys
(qw(svn add), $m->{file_b
});
1491 svn_check_prop_executable
($m);
1493 croak
"Invalid chg: $m->{chg}\n";
1497 assert_tree
($treeish);
1498 if ($_rmdir) { # remove empty directories
1499 handle_rmdir
($rm, $add);
1501 assert_tree
($treeish);
1505 sub libsvn_checkout_tree
{
1506 my ($from, $treeish, $ed) = @_;
1507 my $mods = get_diff
($from, $treeish);
1508 return $mods unless (scalar @
$mods);
1509 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1510 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1512 if (defined $o{$f}) {
1515 croak
"Invalid change type: $f\n";
1518 $ed->rmdirs($_q) if $_rmdir;
1522 # svn ls doesn't work with respect to the current working tree, but what's
1523 # in the repository. There's not even an option for it... *sigh*
1524 # (added files don't show up and removed files remain in the ls listing)
1525 sub svn_ls_current
{
1526 my ($dir, $rm, $add) = @_;
1527 chomp(my @ls = safe_qx
('svn','ls',$dir));
1530 s
#/$##; # trailing slashes are evil
1531 push @ret, $_ unless $rm->{$dir}->{$_};
1533 if (exists $add->{$dir}) {
1534 push @ret, keys %{$add->{$dir}};
1540 my ($rm, $add) = @_;
1542 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1543 my $ls = svn_ls_current
($dir, $rm, $add);
1544 next if (scalar @
$ls);
1545 sys
(qw(svn rm --force),$dir);
1547 my $dn = dirname
$dir;
1548 $rm->{ $dn }->{ basename
$dir } = 1;
1549 $ls = svn_ls_current
($dn, $rm, $add);
1550 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
1551 sys
(qw(svn rm --force),$dn);
1552 $dir = basename
$dn;
1554 $rm->{ $dn }->{ $dir } = 1;
1555 $ls = svn_ls_current
($dn, $rm, $add);
1560 sub get_commit_message
{
1561 my ($commit, $commit_msg) = (@_);
1562 my %log_msg = ( msg
=> '' );
1563 open my $msg, '>', $commit_msg or croak
$!;
1565 chomp(my $type = `git-cat-file -t $commit`);
1566 if ($type eq 'commit' || $type eq 'tag') {
1567 my $pid = open my $msg_fh, '-|';
1568 defined $pid or croak
$!;
1571 exec('git-cat-file', $type, $commit) or croak
$!;
1576 $in_msg = 1 if (/^\s*$/);
1577 } elsif (/^git-svn-id: /) {
1578 # skip this, we regenerate the correct one
1579 # on re-fetch anyways
1581 print $msg $_ or croak
$!;
1584 close $msg_fh or croak
$?
;
1586 close $msg or croak
$!;
1588 if ($_edit || ($type eq 'tree')) {
1589 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1590 system($editor, $commit_msg);
1593 # file_to_s removes all trailing newlines, so just use chomp() here:
1594 open $msg, '<', $commit_msg or croak
$!;
1595 { local $/; chomp($log_msg{msg
} = <$msg>); }
1596 close $msg or croak
$!;
1601 sub set_svn_commit_env
{
1602 if (defined $LC_ALL) {
1603 $ENV{LC_ALL
} = $LC_ALL;
1605 delete $ENV{LC_ALL
};
1609 sub svn_commit_tree
{
1610 my ($last, $commit) = @_;
1611 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1612 my $log_msg = get_commit_message
($commit, $commit_msg);
1613 my ($oneline) = ($log_msg->{msg
} =~ /([^\n\r]+)/);
1614 print "Committing $commit: $oneline\n";
1616 set_svn_commit_env
();
1617 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
1620 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1621 if (!defined $committed) {
1622 my $out = join("\n",@ci_output);
1623 print STDERR
"W: Trouble parsing \`svn commit' output:\n\n",
1624 $out, "\n\nAssuming English locale...";
1625 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1626 defined $committed or die " FAILED!\n",
1627 "Commit output failed to parse committed revision!\n",
1628 print STDERR
" OK\n";
1631 my @svn_up = qw(svn up);
1632 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1633 if ($_optimize_commits && ($committed == ($last->{revision
} + 1))) {
1634 push @svn_up, "-r$committed";
1636 my $info = svn_info
('.');
1637 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1638 if ($info->{'Last Changed Rev'} != $committed) {
1639 croak
"$info->{'Last Changed Rev'} != $committed\n"
1641 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1642 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1643 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1644 or croak
"Failed to parse date: $date\n";
1645 $log_msg->{date
} = "$tz $Y-$m-$d $H:$M:$S";
1646 $log_msg->{author
} = $info->{'Last Changed Author'};
1647 $log_msg->{revision
} = $committed;
1648 $log_msg->{msg
} .= "\n";
1649 $log_msg->{parents
} = [ $last->{commit
} ];
1650 $log_msg->{commit
} = git_commit
($log_msg, $commit);
1653 # resync immediately
1654 push @svn_up, "-r$last->{revision}";
1656 return fetch
("$committed=$commit");
1661 my $pid = open my $fh, '-|';
1662 defined $pid or croak
$!;
1664 exec(qw
/git-rev-list --pretty=raw/, @args) or croak
$!;
1666 return { fh
=> $fh, t
=> { } };
1669 sub next_rev_list_entry
{
1674 if (/^commit ($sha1)$/o) {
1676 $rl->{t
} = { c
=> $1 };
1681 } elsif (/^parent ($sha1)$/o) {
1688 return ($x != $rl->{t
}) ?
$x : undef;
1691 # read the entire log into a temporary file (which is removed ASAP)
1692 # and store the file handle + parser state
1694 my (@log_args) = @_;
1695 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
1697 defined $pid or croak
$!;
1699 open STDOUT
, '>&', $log_fh or croak
$!;
1700 exec (qw(svn log), @log_args) or croak
$!
1704 seek $log_fh, 0, 0 or croak
$!;
1705 return { state => 'sep', fh
=> $log_fh };
1708 sub next_log_entry
{
1709 my $log = shift; # retval of svn_log_raw()
1711 my $fh = $log->{fh
};
1716 if ($log->{state} eq 'msg') {
1717 if ($ret->{lines
}) {
1718 $ret->{msg
} .= $_."\n";
1719 unless(--$ret->{lines
}) {
1720 $log->{state} = 'sep';
1723 croak
"Log parse error at: $_\n",
1729 if ($log->{state} ne 'sep') {
1730 croak
"Log parse error at: $_\n",
1731 "state: $log->{state}\n",
1735 $log->{state} = 'rev';
1737 # if we have an empty log message, put something there:
1739 $ret->{msg
} ||= "\n";
1740 delete $ret->{lines
};
1745 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1747 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1748 ($lines) = ($lines =~ /(\d+)/);
1749 $date = '1970-01-01 00:00:00 +0000'
1750 if ($_ignore_nodate && $date eq '(no date)');
1751 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1752 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1753 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1754 or croak
"Failed to parse date: $date\n";
1755 $ret = { revision
=> $rev,
1756 date
=> "$tz $Y-$m-$d $H:$M:$S",
1760 if (defined $_authors && ! defined $users{$author}) {
1761 die "Author: $author not defined in ",
1764 $log->{state} = 'msg_start';
1767 # skip the first blank line of the message:
1768 if ($log->{state} eq 'msg_start' && /^$/) {
1769 $log->{state} = 'msg';
1770 } elsif ($log->{state} eq 'msg') {
1771 if ($ret->{lines
}) {
1772 $ret->{msg
} .= $_."\n";
1773 unless (--$ret->{lines
}) {
1774 $log->{state} = 'sep';
1777 croak
"Log parse error at: $_\n",
1778 $ret->{revision
},"\n";
1786 my $url = shift || $SVN_URL;
1788 my $pid = open my $info_fh, '-|';
1789 defined $pid or croak
$!;
1792 exec(qw(svn info),$url) or croak
$!;
1796 # only single-lines seem to exist in svn info output
1797 while (<$info_fh>) {
1799 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
1801 push @
{$ret->{-order
}}, $1;
1804 close $info_fh or croak
$?
;
1808 sub sys
{ system(@_) == 0 or croak
$?
}
1810 sub do_update_index
{
1811 my ($z_cmd, $cmd, $no_text_base) = @_;
1813 my $z = open my $p, '-|';
1814 defined $z or croak
$!;
1815 unless ($z) { exec @
$z_cmd or croak
$! }
1817 my $pid = open my $ui, '|-';
1818 defined $pid or croak
$!;
1820 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
1823 while (my $x = <$p>) {
1825 if (!$no_text_base && lstat $x && ! -l _
&&
1826 svn_propget_base
('svn:keywords', $x)) {
1827 my $mode = -x _ ?
0755 : 0644;
1828 my ($v,$d,$f) = File
::Spec
->splitpath($x);
1829 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
1830 'text-base',"$f.svn-base");
1833 $tb = File
::Spec
->catfile($d, '.svn',
1834 'text-base',"$f.svn-base");
1838 unlink $x or croak
$!;
1840 chmod(($mode &~ umask), $x) or croak
$!;
1841 utime $s[8], $s[9], $x;
1845 close $ui or croak
$?
;
1849 return if $_use_lib;
1851 if (!-f
"$GIT_SVN_DIR/info/exclude") {
1852 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak
$!;
1853 print $fd '.svn',"\n";
1854 close $fd or croak
$!;
1856 my $no_text_base = shift;
1857 do_update_index
([qw
/git-diff-files --name-only -z/],
1860 do_update_index
([qw
/git-ls-files -z --others/,
1861 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1867 my ($str, $file, $mode) = @_;
1868 open my $fd,'>',$file or croak
$!;
1869 print $fd $str,"\n" or croak
$!;
1870 close $fd or croak
$!;
1871 chmod ($mode &~ umask, $file) if (defined $mode);
1876 open my $fd,'<',$file or croak
"$!: file: $file\n";
1879 close $fd or croak
$!;
1884 sub assert_revision_unknown
{
1886 if (my $c = revdb_get
($REVDB, $r)) {
1887 croak
"$r = $c already exists! Why are we refetching it?";
1893 my @x = safe_qx
('git-cat-file','commit',$x);
1894 my @y = safe_qx
('git-cat-file','commit',$y);
1895 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1896 || $y[0] !~ /^tree $sha1\n$/) {
1897 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
1904 my ($log_msg, @parents) = @_;
1905 assert_revision_unknown
($log_msg->{revision
});
1906 map_tree_joins
() if (@_branch_from && !%tree_map);
1908 my (@tmp_parents, @exec_parents, %seen_parent);
1909 if (my $lparents = $log_msg->{parents
}) {
1910 @tmp_parents = @
$lparents
1912 # commit parents can be conditionally bound to a particular
1913 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1914 foreach my $p (@parents) {
1915 next unless defined $p;
1916 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1917 if ($1 == $log_msg->{revision
}) {
1918 push @tmp_parents, $2;
1921 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1924 my $tree = $log_msg->{tree
};
1925 if (!defined $tree) {
1926 my $index = set_index
($GIT_SVN_INDEX);
1928 chomp($tree = `git-write-tree`);
1930 restore_index
($index);
1933 # just in case we clobber the existing ref, we still want that ref
1935 if (my $cur = eval { file_to_s
("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1936 push @tmp_parents, $cur;
1939 if (exists $tree_map{$tree}) {
1940 foreach my $p (@
{$tree_map{$tree}}) {
1942 foreach (@tmp_parents) {
1943 # see if a common parent is found
1945 safe_qx
('git-merge-base', $_, $p)
1952 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
1953 next if (($SVN_UUID eq $uuid_p) &&
1954 ($log_msg->{revision
} > $r_p));
1955 next if (defined $url_p && defined $SVN_URL &&
1956 ($SVN_UUID eq $uuid_p) &&
1957 ($url_p eq $SVN_URL));
1958 push @tmp_parents, $p;
1961 foreach (@tmp_parents) {
1962 next if $seen_parent{$_};
1963 $seen_parent{$_} = 1;
1964 push @exec_parents, $_;
1965 # MAXPARENT is defined to 16 in commit-tree.c:
1966 last if @exec_parents > 16;
1969 set_commit_env
($log_msg);
1970 my @exec = ('git-commit-tree', $tree);
1971 push @exec, '-p', $_ foreach @exec_parents;
1972 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1974 print $msg_fh $log_msg->{msg
} or croak
$!;
1975 unless ($_no_metadata) {
1976 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1977 " $SVN_UUID\n" or croak
$!;
1979 $msg_fh->flush == 0 or croak
$!;
1980 close $msg_fh or croak
$!;
1981 chomp(my $commit = do { local $/; <$out_fh> });
1982 close $out_fh or croak
$!;
1985 if ($commit !~ /^$sha1$/o) {
1986 die "Failed to commit, invalid sha1: $commit\n";
1988 sys
('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1989 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
1991 # this output is read via pipe, do not change:
1992 print "r$log_msg->{revision} = $commit\n";
1998 if ($_repack && (--$_repack_nr == 0)) {
1999 $_repack_nr = $_repack;
2000 sys
("git repack $_repack_flags");
2004 sub set_commit_env
{
2006 my $author = $log_msg->{author
};
2007 if (!defined $author || length $author == 0) {
2008 $author = '(no author)';
2010 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
2011 : ($author,"$author\@$SVN_UUID");
2012 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
2013 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
2014 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
2017 sub apply_mod_line_blob
{
2019 if ($m->{mode_b
} =~ /^120/) {
2020 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
2022 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
2026 sub blob_to_symlink
{
2027 my ($blob, $link) = @_;
2028 defined $link or croak
"\$link not defined!\n";
2029 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2030 if (-l
$link || -f _
) {
2031 unlink $link or croak
$!;
2034 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2035 symlink $dest, $link or croak
$!;
2039 my ($blob, $file) = @_;
2040 defined $file or croak
"\$file not defined!\n";
2041 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2042 if (-l
$file || -f _
) {
2043 unlink $file or croak
$!;
2046 open my $blob_fh, '>', $file or croak
"$!: $file\n";
2048 defined $pid or croak
$!;
2051 open STDOUT
, '>&', $blob_fh or croak
$!;
2052 exec('git-cat-file','blob',$blob) or croak
$!;
2057 close $blob_fh or croak
$!;
2061 my $pid = open my $child, '-|';
2062 defined $pid or croak
$!;
2064 exec(@_) or croak
$!;
2066 my @ret = (<$child>);
2067 close $child or croak
$?
;
2068 die $?
if $?
; # just in case close didn't error out
2069 return wantarray ?
@ret : join('',@ret);
2072 sub svn_compat_check
{
2073 if ($_follow_parent) {
2074 print STDERR
'E: --follow-parent functionality is only ',
2075 "available when SVN libraries are used\n";
2078 my @co_help = safe_qx
(qw(svn co -h));
2079 unless (grep /ignore-externals/,@co_help) {
2080 print STDERR
"W: Installed svn version does not support ",
2081 "--ignore-externals\n";
2082 $_no_ignore_ext = 1;
2084 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2085 $_svn_co_url_revs = 1;
2087 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2088 $_svn_pg_peg_revs = 1;
2091 # I really, really hope nobody hits this...
2092 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
2094 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
2096 Lets hope the directory you
're tracking is not a branch or tag
2097 and was never moved within the repository...
2103 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
2104 # (and they won't honor URL@<rev> without -r<rev>, too!)
2105 sub svn_cmd_checkout
{
2106 my ($url, $rev, $dir) = @_;
2107 my @cmd = ('svn','co', "-r$rev");
2108 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2109 $url .= "\@$rev" if $_svn_co_url_revs;
2110 sys
(@cmd, $url, $dir);
2113 sub check_upgrade_needed
{
2115 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2116 open my $fh, '>>',$REVDB or croak
$!;
2120 my $pid = open my $child, '-|';
2121 defined $pid or croak
$!;
2124 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$!;
2126 my @ret = (<$child>);
2127 close $child or croak
$?
;
2128 die $?
if $?
; # just in case close didn't error out
2129 return wantarray ?
@ret : join('',@ret);
2132 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
2134 print STDERR
"Please run: $0 rebuild --upgrade\n";
2139 # fills %tree_map with a reverse mapping of trees to commits. Useful
2140 # for finding parents to commit on.
2141 sub map_tree_joins
{
2143 foreach my $br (@_branch_from) {
2144 my $pid = open my $pipe, '-|';
2145 defined $pid or croak
$!;
2147 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2151 if (/^commit ($sha1)$/o) {
2154 # if we've seen a commit,
2155 # we've seen its parents
2156 last if $seen{$commit};
2157 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2158 unless (defined $tree) {
2159 die "Failed to parse commit $commit\n";
2161 push @
{$tree_map{$tree}}, $commit;
2165 close $pipe; # we could be breaking the pipe early
2170 if (@_branch_from) {
2171 print STDERR
'--branch|-b parameters are ignored when ',
2172 "--branch-all-refs|-B is passed\n";
2175 # don't worry about rev-list on non-commit objects/tags,
2176 # it shouldn't blow up if a ref is a blob or tree...
2177 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2180 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2182 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2183 while (<$authors>) {
2185 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2186 my ($user, $name, $email) = ($1, $2, $3);
2187 $users{$user} = [$name, $email];
2189 close $authors or croak
$!;
2193 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2194 while (<$authors>) {
2196 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2197 my ($user, $name, $email) = ($1, $2, $3);
2198 $rusers{"$name <$email>"} = $user;
2200 close $authors or croak
$!;
2203 sub svn_propget_base
{
2205 $f .= '@BASE' if $_svn_pg_peg_revs;
2206 return safe_qx
(qw
/svn propget/, $p, $f);
2211 foreach (`git-rev-parse --symbolic --all`) {
2212 next unless s
#^refs/remotes/##;
2214 next unless -f
"$GIT_DIR/svn/$_/info/url";
2222 defined(my $pid = fork) or croak
$!;
2224 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2226 exit 0 if -r
$REVDB;
2227 print "Upgrading svn => git mapping...\n";
2228 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2229 open my $fh, '>>',$REVDB or croak
$!;
2232 print "Done upgrading. You may now delete the ",
2233 "deprecated $GIT_SVN_DIR/revs directory\n";
2241 sub migration_check
{
2242 migrate_revdb
() unless (-e
$REVDB);
2243 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
2244 print "Upgrading repository...\n";
2245 unless (-d
"$GIT_DIR/svn") {
2246 mkdir "$GIT_DIR/svn" or croak
$!;
2248 print "Data from a previous version of git-svn exists, but\n\t",
2249 "$GIT_SVN_DIR\n\t(required for this version ",
2250 "($VERSION) of git-svn) does not.\n";
2252 foreach my $x (`git-rev-parse --symbolic --all`) {
2253 next unless $x =~ s
#^refs/remotes/##;
2255 next unless -f
"$GIT_DIR/$x/info/url";
2256 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
2258 my $dn = dirname
("$GIT_DIR/svn/$x");
2259 mkpath
([$dn]) unless -d
$dn;
2260 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
2262 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
2263 print "Done upgrading.\n";
2266 sub find_rev_before
{
2267 my ($r, $id, $eq_ok) = @_;
2268 my $f = "$GIT_DIR/svn/$id/.rev_db";
2269 return (undef,undef) unless -r
$f;
2272 if (my $c = revdb_get
($f, $r)) {
2277 return (undef, undef);
2281 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
2282 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2283 $REVDB = "$GIT_SVN_DIR/.rev_db";
2284 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2286 $SVN_WC = "$GIT_SVN_DIR/tree";
2290 # convert GetOpt::Long specs for use by git-repo-config
2291 sub read_repo_config
{
2292 return unless -d
$GIT_DIR;
2294 foreach my $o (keys %$opts) {
2295 my $v = $opts->{$o};
2296 my ($key) = ($o =~ /^([a-z\-]+)/);
2298 my $arg = 'git-repo-config';
2299 $arg .= ' --int' if ($o =~ /[:=]i$/);
2300 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2301 if (ref $v eq 'ARRAY') {
2302 chomp(my @tmp = `$arg --get-all svn.$key`);
2305 chomp(my $tmp = `$arg --get svn.$key`);
2306 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2313 sub set_default_vals
{
2314 if (defined $_repack) {
2315 $_repack = 1000 if ($_repack <= 0);
2316 $_repack_nr = $_repack;
2317 $_repack_flags ||= '-d';
2322 my $gr_file = shift;
2323 my ($grafts, $comments) = ({}, {});
2324 if (open my $fh, '<', $gr_file) {
2327 if (/^($sha1)\s+/) {
2330 @
{$comments->{$c}} = @tmp;
2333 foreach my $p (split /\s+/, $_) {
2334 $grafts->{$c}->{$p} = 1;
2340 close $fh or croak
$!;
2341 @
{$comments->{'END'}} = @tmp if @tmp;
2343 return ($grafts, $comments);
2347 my ($grafts, $comments, $gr_file) = @_;
2349 open my $fh, '>', $gr_file or croak
$!;
2350 foreach my $c (sort keys %$grafts) {
2351 if ($comments->{$c}) {
2352 print $fh $_ foreach @
{$comments->{$c}};
2354 my $p = $grafts->{$c};
2355 my %x; # real parents
2356 delete $p->{$c}; # commits are not self-reproducing...
2357 my $pid = open my $ch, '-|';
2358 defined $pid or croak
$!;
2360 exec(qw
/git-cat-file commit/, $c) or croak
$!;
2363 if (/^parent ($sha1)/) {
2364 $x{$1} = $p->{$1} = 1;
2369 close $ch; # breaking the pipe
2371 # if real parents are the only ones in the grafts, drop it
2372 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2376 @ip = @jp = keys %$p;
2377 foreach my $i (@ip) {
2378 next if $del{$i} || $p->{$i} == 2;
2379 foreach my $j (@jp) {
2380 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2381 $mb = eval { safe_qx
('git-merge-base',$i,$j) };
2388 } elsif ($mb eq $i) {
2395 # if real parents are the only ones in the grafts, drop it
2396 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2398 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2400 if ($comments->{'END'}) {
2401 print $fh $_ foreach @
{$comments->{'END'}};
2403 close $fh or croak
$!;
2406 sub read_url_paths_all
{
2407 my ($l_map, $pfx, $p) = @_;
2410 if (-r
"$_/info/url") {
2411 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2412 my $id = $pfx . basename
$_;
2413 my $url = file_to_s
("$_/info/url");
2414 my ($u, $p) = repo_path_split
($url);
2415 $l_map->{$u}->{$p} = $id;
2422 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2423 read_url_paths_all
($l_map, $x, $_);
2427 # this one only gets ids that have been imported, not new ones
2428 sub read_url_paths
{
2430 git_svn_each
(sub { my $x = shift;
2431 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
2432 my ($u, $p) = repo_path_split
($url);
2433 $l_map->{$u}->{$p} = $x;
2438 sub extract_metadata
{
2439 my $id = shift or return (undef, undef, undef);
2440 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
2442 if (!$rev || !$uuid || !$url) {
2443 # some of the original repositories I made had
2444 # identifiers like this:
2445 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2447 return ($url, $rev, $uuid);
2451 return extract_metadata
((grep(/^git-svn-id: /,
2452 safe_qx
(qw
/git-cat-file commit/, shift)))[-1]);
2455 sub get_commit_time
{
2457 defined(my $pid = open my $fh, '-|') or croak
$!;
2459 exec qw
/git-rev-list --pretty=raw -n1/, $cmt or croak
$!;
2462 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2463 my ($s, $tz) = ($1, $2);
2464 if ($tz =~ s/^\+//) {
2465 $s += tz_to_s_offset
($tz);
2466 } elsif ($tz =~ s/^\-//) {
2467 $s -= tz_to_s_offset
($tz);
2472 die "Can't get commit time for commit: $cmt\n";
2475 sub tz_to_s_offset
{
2478 return ($1 * 60) + ($tz * 3600);
2481 sub setup_pager
{ # translated to Perl from pager.c
2482 return unless (-t
*STDOUT
);
2483 my $pager = $ENV{PAGER
};
2484 if (!defined $pager) {
2486 } elsif (length $pager == 0 || $pager eq 'cat') {
2489 pipe my $rfd, my $wfd or return;
2490 defined(my $pid = fork) or croak
$!;
2492 open STDOUT
, '>&', $wfd or croak
$!;
2495 open STDIN
, '<&', $rfd or croak
$!;
2496 $ENV{LESS
} ||= '-S';
2497 exec $pager or croak
"Can't run pager: $!\n";;
2500 sub get_author_info
{
2501 my ($dest, $author, $t, $tz) = @_;
2502 $author =~ s/(?:^\s*|\s*$)//g;
2503 $dest->{a_raw
} = $author;
2506 $_a = $rusers{$author} || undef;
2509 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2514 # Date::Parse isn't in the standard Perl distro :(
2515 if ($tz =~ s/^\+//) {
2516 $t += tz_to_s_offset
($tz);
2517 } elsif ($tz =~ s/^\-//) {
2518 $t -= tz_to_s_offset
($tz);
2520 $dest->{t_utc
} = $t;
2523 sub process_commit
{
2524 my ($c, $r_min, $r_max, $defer) = @_;
2525 if (defined $r_min && defined $r_max) {
2526 if ($r_min == $c->{r
} && $r_min == $r_max) {
2530 return 1 if $r_min == $r_max;
2531 if ($r_min < $r_max) {
2532 # we need to reverse the print order
2533 return 0 if (defined $_limit && --$_limit < 0);
2537 if ($r_min != $r_max) {
2538 return 1 if ($r_min < $c->{r
});
2539 return 1 if ($r_max > $c->{r
});
2542 return 0 if (defined $_limit && --$_limit < 0);
2551 if (my $l = $c->{l
}) {
2552 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2555 $_l_fmt ||= 'A' . length($c->{r
});
2556 print 'r',pack($_l_fmt, $c->{r
}),' | ';
2557 print "$c->{c} | " if $_show_commit;
2560 show_commit_normal
($c);
2564 sub show_commit_normal
{
2566 print '-' x72
, "\nr$c->{r} | ";
2567 print "$c->{c} | " if $_show_commit;
2568 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2569 localtime($c->{t_utc
})), ' | ';
2572 if (my $l = $c->{l
}) {
2573 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2576 $nr_line = scalar @
$l;
2578 print "1 line\n\n\n";
2580 if ($nr_line == 1) {
2581 $nr_line = '1 line';
2583 $nr_line .= ' lines';
2585 print $nr_line, "\n\n";
2586 print $_ foreach @
$l;
2592 foreach my $x (qw
/raw diff/) {
2595 print $_ foreach @
{$c->{$x}}
2601 return unless $_use_lib;
2604 if ($SVN::Core
::VERSION
lt '1.1.0') {
2605 die "Need SVN::Core 1.1.0 or better ",
2606 "(got $SVN::Core::VERSION) ",
2607 "Falling back to command-line svn\n";
2611 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
2612 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2613 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2614 $SVN::Node
::none
.$SVN::Node
::file
.
2615 $SVN::Node
::dir
.$SVN::Node
::unknown
;
2620 sub libsvn_connect
{
2622 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
(),
2623 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2624 SVN
::Client
::get_username_provider
()]);
2625 my $s = eval { SVN
::Ra
->new(url
=> $url, auth
=> $auth) };
2629 sub libsvn_get_file
{
2630 my ($gui, $f, $rev) = @_;
2632 if (length $SVN_PATH > 0) {
2633 return unless ($p =~ s
#^\Q$SVN_PATH\E/##);
2636 my ($hash, $pid, $in, $out);
2637 my $pool = SVN
::Pool
->new;
2638 defined($pid = open3
($in, $out, '>&STDERR',
2639 qw
/git-hash-object -w --stdin/)) or croak
$!;
2640 # redirect STDOUT for SVN 1.1.x compatibility
2641 open my $stdout, '>&', \
*STDOUT
or croak
$!;
2642 open STDOUT
, '>&', $in or croak
$!;
2643 my ($r, $props) = $SVN->get_file($f, $rev, \
*STDOUT
, $pool);
2644 $in->flush == 0 or croak
$!;
2645 open STDOUT
, '>&', $stdout or croak
$!;
2646 close $in or croak
$!;
2647 close $stdout or croak
$!;
2649 chomp($hash = do { local $/; <$out> });
2650 close $out or croak
$!;
2652 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2654 my $mode = exists $props->{'svn:executable'} ?
'100755' : '100644';
2655 if (exists $props->{'svn:special'}) {
2657 my $link = `git-cat-file blob $hash`;
2658 $link =~ s/^link // or die "svn:special file with contents: <",
2659 $link, "> is not understood\n";
2660 defined($pid = open3
($in, $out, '>&STDERR',
2661 qw
/git-hash-object -w --stdin/)) or croak
$!;
2663 $in->flush == 0 or croak
$!;
2664 close $in or croak
$!;
2665 chomp($hash = do { local $/; <$out> });
2666 close $out or croak
$!;
2668 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2670 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak
$!;
2673 sub libsvn_log_entry
{
2674 my ($rev, $author, $date, $msg, $parents) = @_;
2675 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2676 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2677 or die "Unable to parse date: $date\n";
2678 if (defined $_authors && ! defined $users{$author}) {
2679 die "Author: $author not defined in $_authors file\n";
2681 $msg = '' if ($rev == 0 && !defined $msg);
2682 return { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2683 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [] }
2687 my ($gui, $last_commit, $f) = @_;
2688 $f =~ s
#^\Q$SVN_PATH\E/?## or return;
2689 # remove entire directories.
2690 if (safe_qx
('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2691 defined(my $pid = open my $ls, '-|') or croak
$!;
2693 exec(qw
/git-ls-tree -r --name-only -z/,
2694 $last_commit,'--',$f) or croak
$!;
2698 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2700 close $ls or croak
$?
;
2702 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2707 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2708 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2710 foreach my $f (keys %$paths) {
2711 my $m = $paths->{$f}->action();
2713 if ($m =~ /^[DR]$/) {
2714 print "\t$m\t$f\n" unless $_q;
2715 process_rm
($gui, $last_commit, $f);
2717 # 'R' can be file replacements, too, right?
2719 my $pool = SVN
::Pool
->new;
2720 my $t = $SVN->check_path($f, $rev, $pool);
2721 if ($t == $SVN::Node
::file
) {
2722 if ($m =~ /^[AMR]$/) {
2723 push @amr, [ $m, $f ];
2725 die "Unrecognized action: $m, ($f r$rev)\n";
2727 } elsif ($t == $SVN::Node
::dir
&& $m =~ /^[AR]$/) {
2729 libsvn_traverse
($gui, '', $f, $rev, \
@traversed);
2730 foreach (@traversed) {
2731 push @amr, [ $m, $_ ]
2737 print "\t$_->[0]\t$_->[1]\n" unless $_q;
2738 libsvn_get_file
($gui, $_->[1], $rev)
2740 close $gui or croak
$?
;
2741 return libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2744 sub svn_grab_base_rev
{
2745 defined(my $pid = open my $fh, '-|') or croak
$!;
2747 open my $null, '>', '/dev/null' or croak
$!;
2748 open STDERR
, '>&', $null or croak
$!;
2749 exec qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2752 chomp(my $c = do { local $/; <$fh> });
2754 if (defined $c && length $c) {
2755 my ($url, $rev, $uuid) = cmt_metadata
($c);
2756 return ($rev, $c) if defined $rev;
2758 if ($_no_metadata) {
2759 my $offset = -41; # from tail
2761 open my $fh, '<', $REVDB or
2762 die "--no-metadata specified and $REVDB not readable\n";
2763 seek $fh, $offset, 2;
2765 defined $rl or return (undef, undef);
2767 while ($c ne $rl && tell $fh != 0) {
2769 seek $fh, $offset, 2;
2771 defined $rl or return (undef, undef);
2775 croak
$! if ($rev < -1);
2776 $rev = ($rev - 41) / 41;
2777 close $fh or croak
$!;
2780 return (undef, undef);
2783 sub libsvn_parse_revision
{
2785 my $head = $SVN->get_latest_revnum();
2786 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2787 return ($base + 1, $head) if (defined $base);
2790 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2791 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2792 if ($_revision =~ /^BASE:(\d+)$/) {
2793 return ($base + 1, $1) if (defined $base);
2796 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2797 die "revision argument: $_revision not understood by git-svn\n",
2798 "Try using the command-line svn client instead\n";
2801 sub libsvn_traverse
{
2802 my ($gui, $pfx, $path, $rev, $files) = @_;
2803 my $cwd = "$pfx/$path";
2804 my $pool = SVN
::Pool
->new;
2806 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2807 foreach my $d (keys %$dirent) {
2808 my $t = $dirent->{$d}->kind;
2809 if ($t == $SVN::Node
::dir
) {
2810 libsvn_traverse
($gui, $cwd, $d, $rev, $files);
2811 } elsif ($t == $SVN::Node
::file
) {
2812 my $file = "$cwd/$d";
2813 if (defined $files) {
2814 push @
$files, $file;
2816 print "\tA\t$file\n" unless $_q;
2817 libsvn_get_file
($gui, $file, $rev);
2824 sub libsvn_traverse_ignore
{
2825 my ($fh, $path, $r) = @_;
2827 my $pool = SVN
::Pool
->new;
2828 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2830 $p =~ s
#^\Q$SVN_PATH\E/?##;
2831 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2832 if (my $s = $props->{'svn:ignore'}) {
2833 $s =~ s/[\r\n]+/\n/g;
2835 if (length $p == 0) {
2839 $s =~ s
#\n#\n/$p/#g;
2840 print $fh "/$p/$s\n";
2843 foreach (sort keys %$dirent) {
2844 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2845 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2851 my ($path, $r0, $r1) = @_;
2852 return 1 if $r0 == $r1;
2855 # should be OK to use Pool here (r1 - r0) should be small
2856 my $pool = SVN
::Pool
->new;
2857 libsvn_get_log
($SVN, "/$path", $r0, $r1,
2858 0, 1, 1, sub {$nr++}, $pool);
2861 my ($url, undef) = repo_path_split
($SVN_URL);
2862 my $svn_log = svn_log_raw
("$url/$path","-r$r0:$r1");
2863 while (next_log_entry
($svn_log)) { $nr++ }
2864 close $svn_log->{fh
};
2866 return 0 if ($nr > 1);
2870 sub libsvn_find_parent_branch
{
2871 my ($paths, $rev, $author, $date, $msg) = @_;
2872 my $svn_path = '/'.$SVN_PATH;
2874 # look for a parent from another branch:
2875 my $i = $paths->{$svn_path} or return;
2876 my $branch_from = $i->copyfrom_path or return;
2877 my $r = $i->copyfrom_rev;
2878 print STDERR
"Found possible branch point: ",
2879 "$branch_from => $svn_path, $r\n";
2880 $branch_from =~ s
#^/##;
2882 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
2883 my $url = $SVN->{url
};
2884 defined $l_map->{$url} or return;
2885 my $id = $l_map->{$url}->{$branch_from};
2886 if (!defined $id && $_follow_parent) {
2887 print STDERR
"Following parent: $branch_from\@$r\n";
2888 # auto create a new branch and follow it
2889 $id = basename
($branch_from);
2890 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
2891 while (-r
"$GIT_DIR/svn/$id") {
2892 # just grow a tail if we're not unique enough :x
2896 return unless defined $id;
2898 my ($r0, $parent) = find_rev_before
($r,$id,1);
2899 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2900 defined(my $pid = fork) or croak
$!;
2902 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2904 $SVN_URL = "$url/$branch_from";
2905 $SVN_LOG = $SVN = undef;
2907 # we can't assume SVN_URL exists at r+1:
2908 $_revision = "0:$r";
2914 ($r0, $parent) = find_rev_before
($r,$id,1);
2916 return unless (defined $r0 && defined $parent);
2917 if (revisions_eq
($branch_from, $r0, $r)) {
2918 unlink $GIT_SVN_INDEX;
2919 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
2920 sys
(qw
/git-read-tree/, $parent);
2921 return libsvn_fetch
($parent, $paths, $rev,
2922 $author, $date, $msg);
2924 print STDERR
"Nope, branch point not imported or unknown\n";
2928 sub libsvn_get_log
{
2929 my ($ra, @args) = @_;
2930 if ($SVN::Core
::VERSION
le '1.2.0') {
2931 splice(@args, 3, 1);
2933 $ra->get_log(@args);
2936 sub libsvn_new_tree
{
2937 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
2940 my ($paths, $rev, $author, $date, $msg) = @_;
2941 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2942 libsvn_traverse
($gui, '', $SVN_PATH, $rev);
2943 close $gui or croak
$?
;
2944 return libsvn_log_entry
($rev, $author, $date, $msg);
2947 sub find_graft_path_commit
{
2948 my ($tree_paths, $p1, $r1) = @_;
2949 foreach my $x (keys %$tree_paths) {
2950 next unless ($p1 =~ /^\Q$x\E/);
2951 my $i = $tree_paths->{$x};
2952 my ($r0, $parent) = find_rev_before
($r1,$i,1);
2953 return $parent if (defined $r0 && $r0 == $r1);
2954 print STDERR
"r$r1 of $i not imported\n";
2960 sub find_graft_path_parents
{
2961 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2962 foreach my $x (keys %$tree_paths) {
2963 next unless ($p0 =~ /^\Q$x\E/);
2964 my $i = $tree_paths->{$x};
2965 my ($r, $parent) = find_rev_before
($r0, $i, 1);
2966 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
2967 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
2968 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
2969 next if ($url_a && $url_b && $url_a eq $url_b &&
2970 $uuid_b eq $uuid_a);
2971 $grafts->{$c}->{$parent} = 1;
2976 sub libsvn_graft_file_copies
{
2977 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2978 foreach (keys %$paths) {
2979 my $i = $paths->{$_};
2980 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2982 next unless (defined $p0 && defined $r0);
2987 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
2989 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
2994 my $old = $ENV{GIT_INDEX_FILE
};
2995 $ENV{GIT_INDEX_FILE
} = shift;
3002 $ENV{GIT_INDEX_FILE
} = $old;
3004 delete $ENV{GIT_INDEX_FILE
};
3008 sub libsvn_commit_cb
{
3009 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3010 if ($_optimize_commits && $rev == ($r_last + 1)) {
3011 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
3012 $log->{tree
} = get_tree_from_treeish
($c);
3013 my $cmt = git_commit
($log, $cmt_last, $c);
3014 my @diff = safe_qx
('git-diff-tree', $cmt, $c);
3016 print STDERR
"Trees differ: $cmt $c\n",
3017 join('',@diff),"\n";
3025 sub libsvn_ls_fullurl
{
3026 my $fullurl = shift;
3027 my ($repo, $path) = repo_path_split
($fullurl);
3028 $SVN ||= libsvn_connect
($repo);
3030 my $pool = SVN
::Pool
->new;
3031 my ($dirent, undef, undef) = $SVN->get_dir($path,
3032 $SVN->get_latest_revnum, $pool);
3033 foreach my $d (keys %$dirent) {
3034 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
3035 push @ret, "$d/"; # add '/' for compat with cli svn
3043 sub libsvn_skip_unknown_revs
{
3045 my $errno = $err->apr_err();
3046 # Maybe the branch we're tracking didn't
3047 # exist when the repo started, so it's
3048 # not an error if it doesn't, just continue
3050 # Wonderfully consistent library, eh?
3051 # 160013 - svn:// and file://
3052 # 175002 - http(s)://
3053 # More codes may be discovered later...
3054 if ($errno == 175002 || $errno == 160013) {
3057 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
3060 # Tie::File seems to be prone to offset errors if revisions get sparse,
3061 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3062 # one of my favorite modules is out :< Next up would be one of the DBM
3063 # modules, but I'm not sure which is most portable... So I'll just
3064 # go with something that's plain-text, but still capable of
3065 # being randomly accessed. So here's my ultra-simple fixed-width
3066 # database. All records are 40 characters + "\n", so it's easy to seek
3067 # to a revision: (41 * rev) is the byte offset.
3068 # A record of 40 0s denotes an empty revision.
3069 # And yes, it's still pretty fast (faster than Tie::File).
3071 my ($file, $rev, $commit) = @_;
3072 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
3073 open my $fh, '+<', $file or croak
$!;
3074 my $offset = $rev * 41;
3075 # assume that append is the common case:
3076 seek $fh, 0, 2 or croak
$!;
3078 if ($pos < $offset) {
3079 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
3081 seek $fh, $offset, 0 or croak
$!;
3082 print $fh $commit,"\n";
3083 close $fh or croak
$!;
3087 my ($file, $rev) = @_;
3089 my $offset = $rev * 41;
3090 open my $fh, '<', $file or croak
$!;
3091 seek $fh, $offset, 0;
3092 if (tell $fh == $offset) {
3093 $ret = readline $fh;
3096 $ret = undef if ($ret =~ /^0{40}$/);
3099 close $fh or croak
$!;
3103 sub copy_remote_ref
{
3104 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
3105 my $ref = "refs/remotes/$GIT_SVN";
3106 if (safe_qx
('git-ls-remote', $origin, $ref)) {
3107 sys
(qw
/git fetch/, $origin, "$ref:$ref");
3109 die "Unable to find remote reference: ",
3110 "refs/remotes/$GIT_SVN on $origin\n";
3114 package SVN
::Git
::Editor
;
3123 my $git_svn = shift;
3124 my $self = SVN
::Delta
::Editor
->new(@_);
3125 bless $self, $class;
3126 foreach (qw
/svn_path c r ra /) {
3127 die "$_ required!\n" unless (defined $git_svn->{$_});
3128 $self->{$_} = $git_svn->{$_};
3130 $self->{pool
} = SVN
::Pool
->new;
3131 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3133 require Digest
::MD5
;
3138 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3142 (defined $_[1] && length $_[1]) ?
"$_[0]->{svn_path}/$_[1]"
3147 my ($self, $path) = @_;
3148 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
3152 my ($self, $q) = @_;
3153 my $rm = $self->{rm
};
3154 delete $rm->{''}; # we never delete the url we're tracking
3157 foreach (keys %$rm) {
3158 my @d = split m
#/#, $_;
3162 $c .= '/' . shift @d;
3166 delete $rm->{$self->{svn_path
}};
3167 delete $rm->{''}; # we never delete the url we're tracking
3170 defined(my $pid = open my $fh,'-|') or croak
$!;
3172 exec qw
/git-ls-tree --name-only -r -z/, $self->{c
} or croak
$!;
3175 my @svn_path = split m
#/#, $self->{svn_path};
3178 my @dn = (@svn_path, (split m
#/#, $_));
3180 delete $rm->{join '/', @dn};
3189 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3190 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3191 $self->close_directory($bat->{$d}, $p);
3192 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3193 print "\tD+\t/$d/\n" unless $q;
3194 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3199 sub open_or_add_dir
{
3200 my ($self, $full_path, $baton) = @_;
3201 my $p = SVN
::Pool
->new;
3202 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
3204 if ($t == $SVN::Node
::none
) {
3205 return $self->add_directory($full_path, $baton,
3206 undef, -1, $self->{pool
});
3207 } elsif ($t == $SVN::Node
::dir
) {
3208 return $self->open_directory($full_path, $baton,
3209 $self->{r
}, $self->{pool
});
3211 print STDERR
"$full_path already exists in repository at ",
3212 "r$self->{r} and it is not a directory (",
3213 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3218 my ($self, $path) = @_;
3219 my $bat = $self->{bat
};
3220 $path = $self->repo_path($path);
3221 return $bat->{''} unless (length $path);
3222 my @p = split m
#/+#, $path;
3224 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3227 $c .= '/' . shift @p;
3228 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3234 my ($self, $m, $q) = @_;
3235 my ($dir, $file) = split_path
($m->{file_b
});
3236 my $pbat = $self->ensure_path($dir);
3237 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3239 print "\tA\t$m->{file_b}\n" unless $q;
3240 $self->chg_file($fbat, $m);
3241 $self->close_file($fbat,undef,$self->{pool
});
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->add_file($self->repo_path($m->{file_b
}), $pbat,
3249 $self->url_path($m->{file_a
}), $self->{r
});
3250 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3251 $self->chg_file($fbat, $m);
3252 $self->close_file($fbat,undef,$self->{pool
});
3256 my ($self, $path, $pbat) = @_;
3257 my $rpath = $self->repo_path($path);
3258 my ($dir, $file) = split_path
($rpath);
3259 $self->{rm
}->{$dir} = 1;
3260 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3264 my ($self, $m, $q) = @_;
3265 my ($dir, $file) = split_path
($m->{file_b
});
3266 my $pbat = $self->ensure_path($dir);
3267 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3268 $self->url_path($m->{file_a
}), $self->{r
});
3269 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3270 $self->chg_file($fbat, $m);
3271 $self->close_file($fbat,undef,$self->{pool
});
3273 ($dir, $file) = split_path
($m->{file_a
});
3274 $pbat = $self->ensure_path($dir);
3275 $self->delete_entry($m->{file_a
}, $pbat);
3279 my ($self, $m, $q) = @_;
3280 my ($dir, $file) = split_path
($m->{file_b
});
3281 my $pbat = $self->ensure_path($dir);
3282 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3283 $pbat,$self->{r
},$self->{pool
});
3284 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3285 $self->chg_file($fbat, $m);
3286 $self->close_file($fbat,undef,$self->{pool
});
3289 sub T
{ shift->M(@_) }
3291 sub change_file_prop
{
3292 my ($self, $fbat, $pname, $pval) = @_;
3293 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3297 my ($self, $fbat, $m) = @_;
3298 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3299 $self->change_file_prop($fbat,'svn:executable','*');
3300 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3301 $self->change_file_prop($fbat,'svn:executable',undef);
3303 my $fh = IO
::File
->new_tmpfile or croak
$!;
3304 if ($m->{mode_b
} =~ /^120/) {
3305 print $fh 'link ' or croak
$!;
3306 $self->change_file_prop($fbat,'svn:special','*');
3307 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3308 $self->change_file_prop($fbat,'svn:special',undef);
3310 defined(my $pid = fork) or croak
$!;
3312 open STDOUT
, '>&', $fh or croak
$!;
3313 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3317 $fh->flush == 0 or croak
$!;
3318 seek $fh, 0, 0 or croak
$!;
3320 my $md5 = Digest
::MD5
->new;
3321 $md5->addfile($fh) or croak
$!;
3322 seek $fh, 0, 0 or croak
$!;
3324 my $exp = $md5->hexdigest;
3325 my $atd = $self->apply_textdelta($fbat, undef, $self->{pool
});
3326 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $self->{pool
});
3327 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3329 close $fh or croak
$!;
3333 my ($self, $m, $q) = @_;
3334 my ($dir, $file) = split_path
($m->{file_b
});
3335 my $pbat = $self->ensure_path($dir);
3336 print "\tD\t$m->{file_b}\n" unless $q;
3337 $self->delete_entry($m->{file_b
}, $pbat);
3342 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3343 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3344 $self->close_directory($bat->{$_}, $p);
3346 $self->SUPER::close_edit
($p);
3352 $self->SUPER::abort_edit
($self->{pool
});
3353 $self->{pool
}->clear;
3360 $svn_log hashref
(as returned by svn_log_raw
)
3362 fh
=> file handle of the
log file
,
3363 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
3366 $log_msg hashref as returned by next_log_entry
($svn_log)
3368 msg
=> 'whitespace-formatted log entry
3369 ', # trailing newline is preserved
3370 revision
=> '8', # integer
3371 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3372 author
=> 'committer name'
3376 @mods = array of diff
-index line hashes
, each element represents one line
3377 of diff
-index output
3379 diff
-index line
($m hash
)
3381 mode_a
=> first column of diff
-index output
, no leading
':',
3382 mode_b
=> second column of diff
-index output
,
3383 sha1_b
=> sha1sum of the final blob
,
3384 chg
=> change type
[MCRADT
],
3385 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3386 file_b
=> new
/current file name of a file
(any chg
)
3390 # retval of read_url_paths{,_all}();
3392 # repository root url
3393 'https://svn.musicpd.org' => {
3394 # repository path # GIT_SVN_ID
3395 'mpd/trunk' => 'trunk',
3396 'mpd/tags/0.11.5' => 'tags/0.11.5',
3401 I don
't trust the each() function on unless I created %hash myself
3402 because the internal iterator may not have started at base.