2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
14 $GIT_DIR = abs_path
($ENV{GIT_DIR
} || '.git');
15 $ENV{GIT_DIR
} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL
};
19 # make sure the svn binary gives consistent output between locales and TZs:
22 $| = 1; # unbuffer STDOUT
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
27 # We don't use any modules not in the standard Perl distribution:
30 use File
::Basename qw
/dirname basename/;
31 use File
::Path qw
/mkpath/;
32 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
34 use File
::Copy qw
/copy/;
35 use POSIX qw
/strftime/;
38 memoize
('revisions_eq');
39 memoize
('cmt_metadata');
40 memoize
('get_commit_time');
42 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
46 ! Please consider installing the SVN Perl libraries (version 1.1.0 or
47 ! newer). You will generally get better performance and fewer bugs,
49 ! 1) have a case-insensitive filesystem
50 ! 2) replace symlinks with files (and vice-versa) in commits
55 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB
};
57 nag_lib
() unless $_use_lib;
59 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
60 my $sha1 = qr/[a-f\d]{40}/;
61 my $sha1_short = qr/[a-f\d]{4,40}/;
62 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
63 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
64 $_repack, $_repack_nr, $_repack_flags, $_q,
65 $_message, $_file, $_follow_parent, $_no_metadata,
66 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
67 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
68 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
69 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive);
70 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
71 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
72 my @repo_path_split_cache;
74 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
75 'branch|b=s' => \
@_branch_from,
76 'follow-parent|follow' => \
$_follow_parent,
77 'branch-all-refs|B' => \
$_branch_all_refs,
78 'authors-file|A=s' => \
$_authors,
79 'repack:i' => \
$_repack,
80 'no-metadata' => \
$_no_metadata,
82 'ignore-nodate' => \
$_ignore_nodate,
83 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
85 my ($_trunk, $_tags, $_branches);
86 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
87 'tags|t=s' => \
$_tags,
88 'branches|b=s' => \
$_branches );
89 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
90 my %cmt_opts = ( 'edit|e' => \
$_edit,
92 'find-copies-harder' => \
$_find_copies_harder,
94 'copy-similarity|C=i'=> \
$_cp_similarity
98 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
99 { 'revision|r=s' => \
$_revision, %fc_opts } ],
100 init
=> [ \
&init
, "Initialize a repo for tracking" .
101 " (requires URL argument)",
103 commit
=> [ \
&commit
, "Commit git revisions to SVN",
104 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
105 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings",
106 { 'revision|r=i' => \
$_revision } ],
107 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
108 { 'no-ignore-externals' => \
$_no_ignore_ext,
109 'copy-remote|remote=s' => \
$_cp_remote,
110 'upgrade' => \
$_upgrade } ],
111 'graft-branches' => [ \
&graft_branches
,
112 'Detect merges/branches from already imported history',
113 { 'merge-rx|m' => \
@_opt_m,
114 'branch|b=s' => \
@_branch_from,
115 'branch-all-refs|B' => \
$_branch_all_refs,
116 'no-default-regex' => \
$_no_default_regex,
117 'no-graft-copy' => \
$_no_graft_copy } ],
118 'multi-init' => [ \
&multi_init
,
119 'Initialize multiple trees (like git-svnimport)',
120 { %multi_opts, %fc_opts } ],
121 'multi-fetch' => [ \
&multi_fetch
,
122 'Fetch multiple trees (like git-svnimport)',
124 'log' => [ \
&show_log
, 'Show commit logs',
125 { 'limit=i' => \
$_limit,
126 'revision|r=s' => \
$_revision,
127 'verbose|v' => \
$_verbose,
128 'incremental' => \
$_incremental,
129 'oneline' => \
$_oneline,
130 'show-commit' => \
$_show_commit,
131 'non-recursive' => \
$_non_recursive,
132 'authors-file|A=s' => \
$_authors,
134 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
135 { 'message|m=s' => \
$_message,
136 'file|F=s' => \
$_file,
137 'revision|r=s' => \
$_revision,
139 dcommit
=> [ \
&dcommit
, 'Commit several diffs to merge with upstream',
140 { 'merge|m|M' => \
$_merge,
141 'strategy|s=s' => \
$_strategy,
142 'dry-run|n' => \
$_dry_run,
147 for (my $i = 0; $i < @ARGV; $i++) {
148 if (defined $cmd{$ARGV[$i]}) {
155 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
157 read_repo_config
(\
%opts);
158 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
159 'version|V' => \
$_version,
160 'id|i=s' => \
$GIT_SVN);
161 exit 1 if (!$rv && $cmd ne 'log');
165 version
() if $_version;
166 usage
(1) unless defined $cmd;
168 load_authors
() if $_authors;
169 load_all_refs
() if $_branch_all_refs;
170 svn_compat_check
() unless $_use_lib;
171 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
172 $cmd{$cmd}->[0]->(@ARGV);
175 ####################### primary functions ######################
177 my $exit = shift || 0;
178 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
180 git
-svn
- bidirectional operations between a single Subversion tree
and git
181 Usage
: $0 <command
> [options
] [arguments
]\n
183 print $fd "Available commands:\n" unless $cmd;
185 foreach (sort keys %cmd) {
186 next if $cmd && $cmd ne $_;
187 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
188 foreach (keys %{$cmd{$_}->[2]}) {
189 # prints out arguments as they should be passed:
190 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
191 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
193 split /\|/,$_)," $x\n";
197 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
198 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
199 one git repository and want to keep them separate. See git-svn(1) for more
206 print "git-svn version $VERSION\n";
211 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
214 $SVN_URL = shift or undef;
217 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
219 check_upgrade_needed();
222 my $pid = open(my $rev_list,'-|');
223 defined $pid or croak $!;
225 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
228 while (<$rev_list>) {
231 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
232 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
233 next if (!@commit); # skip merges
234 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
235 if (!defined $rev || !$uuid) {
236 croak "Unable to extract revision or UUID from ",
237 "$c, $commit[$#commit]\n";
240 # if we merged or otherwise started elsewhere, this is
241 # how we break out of it
242 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
243 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
245 unless (defined $latest) {
246 if (!$SVN_URL && !$url) {
247 croak "SVN repository location required: $url\n";
254 revdb_set($REVDB, $rev, $c);
255 print "r$rev = $c\n";
256 $newest_rev = $rev if ($rev > $newest_rev);
258 close $rev_list or croak $?;
260 goto out if $_use_lib;
261 if (!chdir $SVN_WC) {
262 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
263 chdir $SVN_WC or croak $!;
267 defined $pid or croak $!;
269 my @svn_up = qw(svn up);
270 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
271 sys
(@svn_up,"-r$newest_rev");
272 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
274 exec('git-write-tree') or croak
$!;
281 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
282 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
288 my $url = shift or die "SVN repository location required " .
289 "as a command-line argument\n";
290 $url =~ s!/+$!!; # strip trailing slash
292 if (my $repo_path = shift) {
293 unless (-d
$repo_path) {
294 mkpath
([$repo_path]);
296 $GIT_DIR = $ENV{GIT_DIR
} = $repo_path . "/.git";
301 unless (-d
$GIT_DIR) {
302 my @init_db = ('git-init-db');
303 push @init_db, "--template=$_template" if defined $_template;
304 push @init_db, "--shared" if defined $_shared;
311 check_upgrade_needed
();
312 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
313 my $ret = $_use_lib ? fetch_lib
(@_) : fetch_cmd
(@_);
314 if ($ret->{commit
} && quiet_run
(qw(git-rev-parse --verify
315 refs/heads/master^0))) {
316 sys
(qw(git-update-ref refs/heads/master),$ret->{commit
});
323 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
324 unless ($_revision) {
325 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
327 push @log_args, "-r$_revision";
328 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
330 my $svn_log = svn_log_raw
(@log_args);
332 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
333 # don't need last_revision from grab_base_rev() because
334 # user could've specified a different revision to skip (they
335 # didn't want to import certain revisions into git for whatever
336 # reason, so trust $base->{revision} instead.
337 my (undef, $last_commit) = svn_grab_base_rev
();
338 unless (-d
$SVN_WC) {
339 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
340 chdir $SVN_WC or croak
$!;
342 $last_commit = git_commit
($base, @parents);
343 assert_tree
($last_commit);
345 chdir $SVN_WC or croak
$!;
347 # looks like a user manually cp'd and svn switch'ed
348 unless ($last_commit) {
349 sys
(qw
/svn revert -R ./);
350 assert_svn_wc_clean
($base->{revision
});
351 $last_commit = git_commit
($base, @parents);
352 assert_tree
($last_commit);
355 my @svn_up = qw(svn up);
356 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
358 while (my $log_msg = next_log_entry
($svn_log)) {
359 if ($last->{revision
} >= $log_msg->{revision
}) {
360 croak
"Out of order: last >= current: ",
361 "$last->{revision} >= $log_msg->{revision}\n";
363 # Revert is needed for cases like:
364 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
365 # I can't seem to reproduce something like that on a test...
366 sys
(qw
/svn revert -R ./);
367 assert_svn_wc_clean
($last->{revision
});
368 sys
(@svn_up,"-r$log_msg->{revision}");
369 $last_commit = git_commit
($log_msg, $last_commit, @parents);
372 close $svn_log->{fh
};
373 $last->{commit
} = $last_commit;
379 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
381 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
382 $SVN_LOG ||= libsvn_connect
($repo);
383 $SVN ||= libsvn_connect
($repo);
384 my ($last_rev, $last_commit) = svn_grab_base_rev
();
385 my ($base, $head) = libsvn_parse_revision
($last_rev);
387 return { revision
=> $last_rev, commit
=> $last_commit }
389 my $index = set_index
($GIT_SVN_INDEX);
391 # limit ourselves and also fork() since get_log won't release memory
392 # after processing a revision and SVN stuff seems to leak
394 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
396 if (defined $last_commit) {
397 unless (-e
$GIT_SVN_INDEX) {
398 sys
(qw
/git-read-tree/, $last_commit);
400 chomp (my $x = `git-write-tree`);
401 my ($y) = (`git-cat-file commit $last_commit`
402 =~ /^tree ($sha1)/m);
404 unlink $GIT_SVN_INDEX or croak
$!;
405 sys
(qw
/git-read-tree/, $last_commit);
407 chomp ($x = `git-write-tree`);
409 print STDERR
"trees ($last_commit) $y != $x\n",
410 "Something is seriously wrong...\n";
414 # fork, because using SVN::Pool with get_log() still doesn't
415 # seem to help enough to keep memory usage down.
416 defined(my $pid = fork) or croak
$!;
418 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
420 # Yes I'm perfectly aware that the fourth argument
421 # below is the limit revisions number. Unfortunately
422 # performance sucks with it enabled, so it's much
423 # faster to fetch revision ranges instead of relying
425 libsvn_get_log
($SVN_LOG, '/'.$SVN_PATH,
430 $log_msg = libsvn_fetch
(
432 $last_commit = git_commit
(
437 $log_msg = libsvn_new_tree
(@_);
438 $last_commit = git_commit
(
446 ($last_rev, $last_commit) = svn_grab_base_rev
();
447 last if ($max >= $head);
450 $max = $head if ($max > $head);
452 restore_index
($index);
453 return { revision
=> $last_rev, commit
=> $last_commit };
458 check_upgrade_needed
();
459 if ($_stdin || !@commits) {
460 print "Reading from stdin...\n";
463 if (/\b($sha1_short)\b/o) {
464 unshift @commits, $1;
469 foreach my $c (@commits) {
470 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
471 if (scalar @tmp == 1) {
473 } elsif (scalar @tmp > 1) {
474 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
476 die "Failed to rev-parse $c\n";
480 $_use_lib ? commit_lib
(@revs) : commit_cmd
(@revs);
481 print "Done committing ",scalar @revs," revisions to SVN\n";
487 chdir $SVN_WC or croak
"Unable to chdir $SVN_WC: $!\n";
488 my $info = svn_info
('.');
489 my $fetched = fetch
();
490 if ($info->{Revision
} != $fetched->{revision
}) {
491 print STDERR
"There are new revisions that were fetched ",
492 "and need to be merged (or acknowledged) ",
493 "before committing.\n";
496 $info = svn_info
('.');
499 foreach my $c (@revs) {
500 my $mods = svn_checkout_tree
($last, $c);
501 if (scalar @
$mods == 0) {
502 print "Skipping, no changes detected\n";
505 $last = svn_commit_tree
($last, $c);
511 my ($r_last, $cmt_last) = svn_grab_base_rev
();
512 defined $r_last or die "Must have an existing revision to commit\n";
513 my $fetched = fetch
();
514 if ($r_last != $fetched->{revision
}) {
515 print STDERR
"There are new revisions that were fetched ",
516 "and need to be merged (or acknowledged) ",
517 "before committing.\n",
518 "last rev: $r_last\n",
519 " current: $fetched->{revision}\n";
523 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
524 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
527 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
528 set_svn_commit_env
();
529 foreach my $c (@revs) {
530 my $log_msg = get_commit_message
($c, $commit_msg);
532 # fork for each commit because there's a memory leak I
533 # can't track down... (it's probably in the SVN code)
534 defined(my $pid = open my $fh, '-|') or croak
$!;
536 $SVN_LOG = libsvn_connect
($repo);
537 $SVN = libsvn_connect
($repo);
538 my $ed = SVN
::Git
::Editor
->new(
542 svn_path
=> $SVN_PATH
544 $SVN->get_commit_editor(
555 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
557 print "No changes\nr$r_last = $cmt_last\n";
564 my ($r_new, $cmt_new, $no);
568 if (/^r(\d+) = ($sha1)$/o) {
569 ($r_new, $cmt_new) = ($1, $2);
570 } elsif ($_ eq 'No changes') {
574 close $fh or croak
$?
;
575 if (! defined $r_new && ! defined $cmt_new) {
577 die "Failed to parse revision information\n";
580 ($r_last, $cmt_last) = ($r_new, $cmt_new);
588 my $gs = "refs/remotes/$GIT_SVN";
589 chomp(my @refs = safe_qx
(qw
/git-rev-list --no-merges/, "$gs..HEAD"));
591 foreach my $d (reverse @refs) {
592 if (quiet_run
('git-rev-parse','--verify',"$d~1") != 0) {
594 "has no parent commit, and therefore ",
595 "nothing to diff against.\n",
596 "You should be working from a repository ",
597 "originally created by git-svn\n";
599 unless (defined $last_rev) {
600 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
601 unless (defined $last_rev) {
602 die "Unable to extract revision information ",
603 "from commit $d~1\n";
607 print "diff-tree $d~1 $d\n";
609 if (my $r = commit_diff
("$d~1", $d, undef, $last_rev)) {
611 } # else: no changes, same $last_rev
616 my @diff = safe_qx
(qw
/git-diff-tree HEAD/, $gs);
619 @finish = qw
/rebase/;
620 push @finish, qw
/--merge/ if $_merge;
621 push @finish, "--strategy=$_strategy" if $_strategy;
622 print STDERR
"W: HEAD and $gs differ, using @finish:\n", @diff;
624 print "No changes between current HEAD and $gs\n",
625 "Hard resetting to the latest $gs\n";
626 @finish = qw
/reset --mixed/;
628 sys
('git', @finish, $gs);
632 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
633 $_use_lib ? show_ignore_lib
() : show_ignore_cmd
();
636 sub show_ignore_cmd
{
637 require File
::Find
or die $!;
638 if (defined $_revision) {
639 die "-r/--revision option doesn't work unless the Perl SVN ",
640 "libraries are used\n";
642 chdir $SVN_WC or croak
$!;
644 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
646 @
{$ign{$_}} = svn_propget_base
('svn:ignore', $_);
647 }}, no_chdir
=>1},'.');
650 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
652 foreach my $i (sort keys %ign) {
653 print "\n# ",$i,"\n";
654 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
658 sub show_ignore_lib
{
660 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
661 $SVN ||= libsvn_connect
($repo);
662 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
663 libsvn_traverse_ignore
(\
*STDOUT
, $SVN_PATH, $r);
667 my $gr_file = "$GIT_DIR/info/grafts";
668 my ($grafts, $comments) = read_grafts
($gr_file);
672 # temporarily disable our grafts file to make this idempotent
673 chomp($gr_sha1 = safe_qx
(qw
/git-hash-object -w/,$gr_file));
674 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
677 my $l_map = read_url_paths
();
678 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
679 unless ($_no_default_regex) {
680 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
681 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
682 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
684 foreach my $u (keys %$l_map) {
686 foreach my $p (keys %{$l_map->{$u}}) {
687 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
690 unless ($_no_graft_copy) {
692 graft_file_copy_lib
($grafts,$l_map,$u);
694 graft_file_copy_cmd
($grafts,$l_map,$u);
698 graft_tree_joins
($grafts);
700 write_grafts
($grafts, $comments, $gr_file);
701 unlink "$gr_file~$gr_sha1" if $gr_sha1;
708 $url =~ s
#/+$## if $url;
709 if ($_trunk !~ m
#^[a-z\+]+://#) {
710 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
712 print STDERR
"E: '$_trunk' is not a complete URL ",
713 "and a separate URL is not specified\n";
716 $_trunk = $url . $_trunk;
719 if ($GIT_SVN eq 'git-svn') {
721 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
724 unless (-d
$GIT_SVN_DIR) {
725 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
727 sys
('git-repo-config', 'svn.trunk', $_trunk);
729 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
730 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
734 # try to do trunk first, since branches/tags
735 # may be descended from it.
736 if (-e
"$GIT_DIR/svn/trunk/info/url") {
737 fetch_child_id
('trunk', @_);
739 rec_fetch
('', "$GIT_DIR/svn", @_);
745 my $r_last = -1; # prevent dupes
746 rload_authors
() if $_authors;
752 if (defined $_revision) {
753 if ($_revision =~ /^(\d+):(\d+)$/) {
754 ($r_min, $r_max) = ($1, $2);
755 } elsif ($_revision =~ /^\d+$/) {
756 $r_min = $r_max = $_revision;
758 print STDERR
"-r$_revision is not supported, use ",
759 "standard \'git log\' arguments instead\n";
764 my $pid = open(my $log,'-|');
765 defined $pid or croak
$!;
767 exec(git_svn_log_cmd
($r_min,$r_max), @args) or croak
$!;
773 if (/^commit ($sha1_short)/o) {
775 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
777 process_commit
($c, $r_min, $r_max, \
@k) or
782 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
783 get_author_info
($c, $1, $2, $3);
784 } elsif (/^(?:tree|parent|committer) /) {
786 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
787 push @
{$c->{raw
}}, $_;
788 } elsif (/^[ACRMDT]\t/) {
789 # we could add $SVN_PATH here, but that requires
790 # remote access at the moment (repo_path_split)...
791 s
#^([ACRMDT])\t# $1 #;
792 push @
{$c->{changed
}}, $_;
795 push @
{$c->{diff
}}, $_;
797 push @
{$c->{diff
}}, $_;
798 } elsif (/^ (git-svn-id:.+)$/) {
799 ($c->{url
}, $c->{r
}, undef) = extract_metadata
($1);
804 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
806 process_commit
($c, $r_min, $r_max, \
@k);
812 process_commit
($_, $r_min, $r_max) foreach reverse @k;
816 print '-' x72
,"\n" unless $_incremental || $_oneline;
819 sub commit_diff_usage
{
820 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
826 print STDERR
"commit-diff must be used with SVN libraries\n";
829 my $ta = shift or commit_diff_usage
();
830 my $tb = shift or commit_diff_usage
();
831 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
832 print STDERR
"Needed URL or usable git-svn id command-line\n";
836 unless (defined $r) {
837 if (defined $_revision) {
840 die "-r|--revision is a required argument\n";
843 if (defined $_message && defined $_file) {
844 print STDERR
"Both --message/-m and --file/-F specified ",
845 "for the commit message.\n",
846 "I have no idea what you mean\n";
849 if (defined $_file) {
850 $_message = file_to_s
($_file);
852 $_message ||= get_commit_message
($tb,
853 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
856 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
857 $SVN_LOG ||= libsvn_connect
($repo);
858 $SVN ||= libsvn_connect
($repo);
860 $r = $SVN->get_latest_revnum;
861 } elsif ($r !~ /^\d+$/) {
862 die "revision argument: $r not understood by git-svn\n";
864 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
866 my $ed = SVN
::Git
::Editor
->new({ r
=> $r,
867 ra
=> $SVN_LOG, c
=> $tb,
868 svn_path
=> $SVN_PATH
870 $SVN->get_commit_editor($_message,
872 $rev_committed = $_[0];
873 print "Committed $_[0]\n";
876 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
878 print "No changes\n$ta == $tb\n";
883 $_message = $_file = undef;
884 return $rev_committed;
887 ########################### utility functions #########################
891 return 1 if defined $c->{r
};
892 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
893 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
894 my @msg = safe_qx
(qw
/git-cat-file commit/, $c->{c
});
895 shift @msg while ($msg[0] ne "\n");
897 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
899 (undef, $c->{r
}, undef) = extract_metadata
(
900 (grep(/^git-svn-id: /, @msg))[-1]);
902 return defined $c->{r
};
905 sub git_svn_log_cmd
{
906 my ($r_min, $r_max) = @_;
907 my @cmd = (qw
/git
-log --abbrev
-commit
--pretty
=raw
908 --default/, "refs/remotes
/$GIT_SVN");
909 push @cmd, '-r' unless $_non_recursive;
910 push @cmd, qw/--raw --name-status/ if $_verbose;
911 return @cmd unless defined $r_max;
912 if ($r_max == $r_min) {
913 push @cmd, '--max-count=1';
914 if (my $c = revdb_get($REVDB, $r_max)) {
919 $c_max = revdb_get($REVDB, $r_max);
920 $c_min = revdb_get($REVDB, $r_min);
921 if (defined $c_min && defined $c_max) {
922 if ($r_max > $r_max) {
923 push @cmd, "$c_min..$c_max";
925 push @cmd, "$c_max..$c_min";
927 } elsif ($r_max > $r_min) {
938 print "Fetching
$id\n";
939 my $ref = "$GIT_DIR/refs/remotes
/$id";
940 defined(my $pid = open my $fh, '-|') or croak $!;
943 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
950 check_repack() if (/^r\d+ = $sha1/);
952 close $fh or croak $?;
956 my ($pfx, $p, @args) = @_;
958 foreach (sort <$p/*>) {
959 if (-r "$_/info/url
") {
960 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
961 my $id = $pfx . basename $_;
962 next if $id eq 'trunk';
963 fetch_child_id($id, @args);
970 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
975 sub complete_url_ls_init {
976 my ($url, $var, $switch, $pfx) = @_;
978 print STDERR "W
: $switch not specified
\n";
982 if ($var !~ m#^[a-z\+]+://#) {
983 $var = '/' . $var if ($var !~ m#^/#);
985 print STDERR "E
: '$var' is
not a complete URL
",
986 "and a separate URL is
not specified
\n";
991 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
992 : safe_qx(qw/svn ls --non-interactive/, $var));
994 defined(my $pid = fork) or croak $!;
996 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
998 if ($u !~ m!\Q$var\E/(.+)$!) {
999 print STDERR
"W: Unrecognized URL: $u\n";
1000 die "This should never happen\n";
1002 # don't try to init already existing refs
1004 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
1006 unless (-d
$GIT_SVN_DIR) {
1007 print "init $u => $id\n";
1015 my ($n) = ($switch =~ /^--(\w+)/);
1016 sys
('git-repo-config', "svn.$n", $var);
1023 my @tmp = split m
#/#, $_;
1025 while (my $x = shift @tmp) {
1031 foreach (sort {length $b <=> length $a} keys %common) {
1032 if ($common{$_} == @
$paths) {
1039 # grafts set here are 'stronger' in that they're based on actual tree
1040 # matches, and won't be deleted from merge-base checking in write_grafts()
1041 sub graft_tree_joins
{
1043 map_tree_joins
() if (@_branch_from && !%tree_map);
1044 return unless %tree_map;
1048 defined(my $pid = open my $fh, '-|') or croak
$!;
1050 exec qw
/git-rev-list --pretty=raw/,
1051 "refs/remotes/$i" or croak
$!;
1054 next unless /^commit ($sha1)$/o;
1056 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1057 next unless $tree_map{$t};
1062 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1064 my ($s, $tz) = ($1, $2);
1065 if ($tz =~ s/^\+//) {
1066 $s += tz_to_s_offset
($tz);
1067 } elsif ($tz =~ s/^\-//) {
1068 $s -= tz_to_s_offset
($tz);
1071 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
1073 foreach my $p (@
{$tree_map{$t}}) {
1076 safe_qx
('git-merge-base', $c, $p)
1078 next unless ($@
|| $?
);
1080 # see if SVN says it's a relative
1081 my ($url_b, $r_b, $uuid_b) =
1083 next if (defined $url_b &&
1085 ($url_a eq $url_b) &&
1086 ($uuid_a eq $uuid_b));
1087 if ($uuid_a eq $uuid_b) {
1089 $grafts->{$c}->{$p} = 2;
1091 } elsif ($r_b > $r_a) {
1092 $grafts->{$p}->{$c} = 2;
1097 my $ct = get_commit_time
($p);
1099 $grafts->{$c}->{$p} = 2;
1100 } elsif ($ct > $s) {
1101 $grafts->{$p}->{$c} = 2;
1103 # what should we do when $ct == $s ?
1106 close $fh or croak
$?
;
1110 # this isn't funky-filename safe, but good enough for now...
1111 sub graft_file_copy_cmd
{
1112 my ($grafts, $l_map, $u) = @_;
1113 my $paths = $l_map->{$u};
1114 my $pfx = common_prefix
([keys %$paths]);
1115 $SVN_URL ||= $u.$pfx;
1116 my $pid = open my $fh, '-|';
1117 defined $pid or croak
$!;
1119 my @exec = qw
/svn log -v/;
1120 push @exec, "-r$_revision" if defined $_revision;
1121 exec @exec, $u.$pfx or croak
$!;
1123 my ($r, $mp) = (undef, undef);
1128 } elsif (/^r(\d+) \| /) {
1129 $r = $1 unless defined $r;
1130 } elsif (/^Changed paths:/) {
1132 } elsif ($mp && m
#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1133 my ($p1, $p0, $r0) = ($1, $2, $3);
1134 my $c = find_graft_path_commit
($paths, $p1, $r);
1136 find_graft_path_parents
($grafts, $paths, $c, $p0, $r0);
1141 sub graft_file_copy_lib
{
1142 my ($grafts, $l_map, $u) = @_;
1143 my $tree_paths = $l_map->{$u};
1144 my $pfx = common_prefix
([keys %$tree_paths]);
1145 my ($repo, $path) = repo_path_split
($u.$pfx);
1146 $SVN_LOG ||= libsvn_connect
($repo);
1147 $SVN ||= libsvn_connect
($repo);
1149 my ($base, $head) = libsvn_parse_revision
();
1151 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1152 my $eh = $SVN::Error
::handler
;
1153 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1155 my $pool = SVN
::Pool
->new;
1156 libsvn_get_log
($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1158 libsvn_graft_file_copies
($grafts, $tree_paths,
1162 last if ($max >= $head);
1165 $max = $head if ($max > $head);
1167 $SVN::Error
::handler
= $eh;
1170 sub process_merge_msg_matches
{
1171 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1172 my (@strong, @weak);
1173 foreach (@matches) {
1174 # merging with ourselves is not interesting
1176 if ($l_map->{$u}->{$_}) {
1182 foreach my $w (@weak) {
1184 # no exact match, use branch name as regexp.
1185 my $re = qr/\Q$w\E/i;
1186 foreach (keys %{$l_map->{$u}}) {
1188 push @strong, $l_map->{$u}->{$_};
1195 foreach (keys %{$l_map->{$u}}) {
1197 push @strong, $l_map->{$u}->{$_};
1202 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1203 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1204 unless (defined $rev) {
1205 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1206 \@
(?
:[a
-f\d\
-]+)/xsm
);
1207 return unless defined $rev;
1209 foreach my $m (@strong) {
1210 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1211 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1215 sub graft_merge_msg
{
1216 my ($grafts, $l_map, $u, $p, @re) = @_;
1218 my $x = $l_map->{$u}->{$p};
1219 my $rl = rev_list_raw
($x);
1220 while (my $c = next_rev_list_entry
($rl)) {
1221 foreach my $re (@re) {
1222 my (@br) = ($c->{m
} =~ /$re/g);
1224 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1230 return if $SVN_UUID;
1232 my $pool = SVN
::Pool
->new;
1233 $SVN_UUID = $SVN->get_uuid($pool);
1236 my $info = shift || svn_info
('.');
1237 $SVN_UUID = $info->{'Repository UUID'} or
1238 croak
"Repository UUID unreadable\n";
1244 defined $pid or croak
$!;
1246 open my $null, '>', '/dev/null' or croak
$!;
1247 open STDERR
, '>&', $null or croak
$!;
1248 open STDOUT
, '>&', $null or croak
$!;
1249 exec @_ or croak
$!;
1255 sub repo_path_split
{
1256 my $full_url = shift;
1257 $full_url =~ s
#/+$##;
1259 foreach (@repo_path_split_cache) {
1260 if ($full_url =~ s
#$_##) {
1262 $full_url =~ s
#^/+##;
1263 return ($u, $full_url);
1268 my $tmp = libsvn_connect
($full_url);
1269 my $url = $tmp->get_repos_root;
1270 $full_url =~ s
#^\Q$url\E/*##;
1271 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1272 return ($url, $full_url);
1274 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1276 my @paths = split(m
#/+#, $path);
1277 while (quiet_run
(qw
/svn ls --non-interactive/, $url)) {
1278 my $n = shift @paths || last;
1281 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1282 $path = join('/',@paths);
1283 return ($url, $path);
1288 defined $SVN_URL or croak
"SVN repository location required\n";
1289 unless (-d
$GIT_DIR) {
1290 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1292 mkpath
([$GIT_SVN_DIR]);
1293 mkpath
(["$GIT_SVN_DIR/info"]);
1294 open my $fh, '>>',$REVDB or croak
$!;
1296 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1300 sub assert_svn_wc_clean
{
1301 return if $_use_lib;
1303 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1304 my $lcr = svn_info
('.')->{'Last Changed Rev'};
1305 if ($svn_rev != $lcr) {
1306 print STDERR
"Checking for copy-tree ... ";
1307 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
1308 "-r$lcr:$svn_rev")));
1310 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
1312 print STDERR
"OK!\n";
1315 my @status = grep(!/^Performing status on external/,(`svn status`));
1316 @status = grep(!/^\s*$/,@status);
1317 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1318 if (scalar @status) {
1319 print STDERR
"Tree ($SVN_WC) is not clean:\n";
1320 print STDERR
$_ foreach @status;
1325 sub get_tree_from_treeish
{
1327 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1328 chomp(my $type = `git-cat-file -t $treeish`);
1330 while ($type eq 'tag') {
1331 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1333 if ($type eq 'commit') {
1334 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1335 ($expected) = ($expected =~ /^tree ($sha1)$/);
1336 die "Unable to get tree from $treeish\n" unless $expected;
1337 } elsif ($type eq 'tree') {
1338 $expected = $treeish;
1340 die "$treeish is a $type, expected tree, tag or commit\n";
1346 return if $_use_lib;
1348 my $expected = get_tree_from_treeish
($treeish);
1350 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1352 unlink $tmpindex or croak
$!;
1354 my $old_index = set_index
($tmpindex);
1356 chomp(my $tree = `git-write-tree`);
1357 restore_index
($old_index);
1358 if ($tree ne $expected) {
1359 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
1364 sub parse_diff_tree
{
1365 my $diff_fh = shift;
1369 while (<$diff_fh>) {
1370 chomp $_; # this gets rid of the trailing "\0"
1371 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1372 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1373 push @mods, { mode_a
=> $1, mode_b
=> $2,
1374 sha1_b
=> $3, chg
=> $4 };
1375 if ($4 =~ /^(?:C|R)$/) {
1380 } elsif ($state eq 'file_a') {
1381 my $x = $mods[$#mods] or croak
"Empty array\n";
1382 if ($x->{chg
} !~ /^(?:C|R)$/) {
1383 croak
"Error parsing $_, $x->{chg}\n";
1387 } elsif ($state eq 'file_b') {
1388 my $x = $mods[$#mods] or croak
"Empty array\n";
1389 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1390 croak
"Error parsing $_, $x->{chg}\n";
1392 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1393 croak
"Error parsing $_, $x->{chg}\n";
1398 croak
"Error parsing $_\n";
1401 close $diff_fh or croak
$?
;
1406 sub svn_check_prop_executable
{
1408 return if -l
$m->{file_b
};
1409 if ($m->{mode_b
} =~ /755$/) {
1410 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
1411 if ($m->{mode_a
} !~ /755$/) {
1412 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
1414 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
1415 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1416 sys
(qw(svn propdel svn:executable), $m->{file_b
});
1417 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
1418 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
1422 sub svn_ensure_parent_path
{
1423 my $dir_b = dirname
(shift);
1424 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
1425 mkpath
([$dir_b]) unless (-d
$dir_b);
1426 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
1429 sub precommit_check
{
1431 my (%rm_file, %rmdir_check, %added_check);
1433 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
1434 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1435 if ($m->{chg
} eq 'R') {
1436 if (-d
$m->{file_b
}) {
1437 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1439 # dir/$file => dir/file/$file
1440 my $dirname = dirname
($m->{file_b
});
1441 while ($dirname ne File
::Spec
->curdir) {
1442 if ($dirname ne $m->{file_a
}) {
1443 $dirname = dirname
($dirname);
1446 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
1448 # baz/zzz => baz (baz is a file)
1449 $dirname = dirname
($m->{file_a
});
1450 while ($dirname ne File
::Spec
->curdir) {
1451 if ($dirname ne $m->{file_b
}) {
1452 $dirname = dirname
($dirname);
1455 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1458 if ($m->{chg
} =~ /^(D|R)$/) {
1459 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
1460 $rm_file{ $m->{$t} } = 1;
1461 my $dirname = dirname
( $m->{$t} );
1462 my $basename = basename
( $m->{$t} );
1463 $rmdir_check{$dirname}->{$basename} = 1;
1464 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
1465 if (-d
$m->{file_b
}) {
1466 err_dir_to_file
($m->{file_b
});
1468 my $dirname = dirname
( $m->{file_b
} );
1469 my $basename = basename
( $m->{file_b
} );
1470 $added_check{$dirname}->{$basename} = 1;
1471 while ($dirname ne File
::Spec
->curdir) {
1472 if ($rm_file{$dirname}) {
1473 err_file_to_dir
($m->{file_b
});
1475 $dirname = dirname
$dirname;
1479 return (\
%rmdir_check, \
%added_check);
1481 sub err_dir_to_file
{
1483 print STDERR
"Node change from directory to file ",
1484 "is not supported by Subversion: ",$file,"\n";
1487 sub err_file_to_dir
{
1489 print STDERR
"Node change from file to directory ",
1490 "is not supported by Subversion: ",$file,"\n";
1497 my ($from, $treeish) = @_;
1499 print "diff-tree $from $treeish\n";
1500 my $pid = open my $diff_fh, '-|';
1501 defined $pid or croak
$!;
1503 my @diff_tree = qw(git-diff-tree -z -r);
1504 if ($_cp_similarity) {
1505 push @diff_tree, "-C$_cp_similarity";
1507 push @diff_tree, '-C';
1509 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1510 push @diff_tree, "-l$_l" if defined $_l;
1511 exec(@diff_tree, $from, $treeish) or croak
$!;
1513 return parse_diff_tree
($diff_fh);
1516 sub svn_checkout_tree
{
1517 my ($from, $treeish) = @_;
1518 my $mods = get_diff
($from->{commit
}, $treeish);
1519 return $mods unless (scalar @
$mods);
1520 my ($rm, $add) = precommit_check
($mods);
1522 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1523 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1524 if ($m->{chg
} eq 'C') {
1525 svn_ensure_parent_path
( $m->{file_b
} );
1526 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
1527 apply_mod_line_blob
($m);
1528 svn_check_prop_executable
($m);
1529 } elsif ($m->{chg
} eq 'D') {
1530 sys
(qw(svn rm --force), $m->{file_b
});
1531 } elsif ($m->{chg
} eq 'R') {
1532 svn_ensure_parent_path
( $m->{file_b
} );
1533 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
1534 apply_mod_line_blob
($m);
1535 svn_check_prop_executable
($m);
1536 } elsif ($m->{chg
} eq 'M') {
1537 apply_mod_line_blob
($m);
1538 svn_check_prop_executable
($m);
1539 } elsif ($m->{chg
} eq 'T') {
1540 svn_check_prop_executable
($m);
1541 apply_mod_line_blob
($m);
1542 if ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
1543 sys
(qw(svn propdel svn:special), $m->{file_b
});
1545 sys
(qw(svn propset svn:special *),$m->{file_b
});
1547 } elsif ($m->{chg
} eq 'A') {
1548 svn_ensure_parent_path
( $m->{file_b
} );
1549 apply_mod_line_blob
($m);
1550 sys
(qw(svn add), $m->{file_b
});
1551 svn_check_prop_executable
($m);
1553 croak
"Invalid chg: $m->{chg}\n";
1557 assert_tree
($treeish);
1558 if ($_rmdir) { # remove empty directories
1559 handle_rmdir
($rm, $add);
1561 assert_tree
($treeish);
1565 sub libsvn_checkout_tree
{
1566 my ($from, $treeish, $ed) = @_;
1567 my $mods = get_diff
($from, $treeish);
1568 return $mods unless (scalar @
$mods);
1569 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1570 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1572 if (defined $o{$f}) {
1575 croak
"Invalid change type: $f\n";
1578 $ed->rmdirs($_q) if $_rmdir;
1582 # svn ls doesn't work with respect to the current working tree, but what's
1583 # in the repository. There's not even an option for it... *sigh*
1584 # (added files don't show up and removed files remain in the ls listing)
1585 sub svn_ls_current
{
1586 my ($dir, $rm, $add) = @_;
1587 chomp(my @ls = safe_qx
('svn','ls',$dir));
1590 s
#/$##; # trailing slashes are evil
1591 push @ret, $_ unless $rm->{$dir}->{$_};
1593 if (exists $add->{$dir}) {
1594 push @ret, keys %{$add->{$dir}};
1600 my ($rm, $add) = @_;
1602 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1603 my $ls = svn_ls_current
($dir, $rm, $add);
1604 next if (scalar @
$ls);
1605 sys
(qw(svn rm --force),$dir);
1607 my $dn = dirname
$dir;
1608 $rm->{ $dn }->{ basename
$dir } = 1;
1609 $ls = svn_ls_current
($dn, $rm, $add);
1610 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
1611 sys
(qw(svn rm --force),$dn);
1612 $dir = basename
$dn;
1614 $rm->{ $dn }->{ $dir } = 1;
1615 $ls = svn_ls_current
($dn, $rm, $add);
1620 sub get_commit_message
{
1621 my ($commit, $commit_msg) = (@_);
1622 my %log_msg = ( msg
=> '' );
1623 open my $msg, '>', $commit_msg or croak
$!;
1625 chomp(my $type = `git-cat-file -t $commit`);
1626 if ($type eq 'commit' || $type eq 'tag') {
1627 my $pid = open my $msg_fh, '-|';
1628 defined $pid or croak
$!;
1631 exec('git-cat-file', $type, $commit) or croak
$!;
1636 $in_msg = 1 if (/^\s*$/);
1637 } elsif (/^git-svn-id: /) {
1638 # skip this, we regenerate the correct one
1639 # on re-fetch anyways
1641 print $msg $_ or croak
$!;
1644 close $msg_fh or croak
$?
;
1646 close $msg or croak
$!;
1648 if ($_edit || ($type eq 'tree')) {
1649 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1650 system($editor, $commit_msg);
1653 # file_to_s removes all trailing newlines, so just use chomp() here:
1654 open $msg, '<', $commit_msg or croak
$!;
1655 { local $/; chomp($log_msg{msg
} = <$msg>); }
1656 close $msg or croak
$!;
1661 sub set_svn_commit_env
{
1662 if (defined $LC_ALL) {
1663 $ENV{LC_ALL
} = $LC_ALL;
1665 delete $ENV{LC_ALL
};
1669 sub svn_commit_tree
{
1670 my ($last, $commit) = @_;
1671 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1672 my $log_msg = get_commit_message
($commit, $commit_msg);
1673 my ($oneline) = ($log_msg->{msg
} =~ /([^\n\r]+)/);
1674 print "Committing $commit: $oneline\n";
1676 set_svn_commit_env
();
1677 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
1680 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1681 if (!defined $committed) {
1682 my $out = join("\n",@ci_output);
1683 print STDERR
"W: Trouble parsing \`svn commit' output:\n\n",
1684 $out, "\n\nAssuming English locale...";
1685 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1686 defined $committed or die " FAILED!\n",
1687 "Commit output failed to parse committed revision!\n",
1688 print STDERR
" OK\n";
1691 my @svn_up = qw(svn up);
1692 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1693 if ($_optimize_commits && ($committed == ($last->{revision
} + 1))) {
1694 push @svn_up, "-r$committed";
1696 my $info = svn_info
('.');
1697 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1698 if ($info->{'Last Changed Rev'} != $committed) {
1699 croak
"$info->{'Last Changed Rev'} != $committed\n"
1701 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1702 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1703 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1704 or croak
"Failed to parse date: $date\n";
1705 $log_msg->{date
} = "$tz $Y-$m-$d $H:$M:$S";
1706 $log_msg->{author
} = $info->{'Last Changed Author'};
1707 $log_msg->{revision
} = $committed;
1708 $log_msg->{msg
} .= "\n";
1709 $log_msg->{parents
} = [ $last->{commit
} ];
1710 $log_msg->{commit
} = git_commit
($log_msg, $commit);
1713 # resync immediately
1714 push @svn_up, "-r$last->{revision}";
1716 return fetch
("$committed=$commit");
1721 my $pid = open my $fh, '-|';
1722 defined $pid or croak
$!;
1724 exec(qw
/git-rev-list --pretty=raw/, @args) or croak
$!;
1726 return { fh
=> $fh, t
=> { } };
1729 sub next_rev_list_entry
{
1734 if (/^commit ($sha1)$/o) {
1736 $rl->{t
} = { c
=> $1 };
1741 } elsif (/^parent ($sha1)$/o) {
1748 return ($x != $rl->{t
}) ?
$x : undef;
1751 # read the entire log into a temporary file (which is removed ASAP)
1752 # and store the file handle + parser state
1754 my (@log_args) = @_;
1755 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
1757 defined $pid or croak
$!;
1759 open STDOUT
, '>&', $log_fh or croak
$!;
1760 exec (qw(svn log), @log_args) or croak
$!
1764 seek $log_fh, 0, 0 or croak
$!;
1765 return { state => 'sep', fh
=> $log_fh };
1768 sub next_log_entry
{
1769 my $log = shift; # retval of svn_log_raw()
1771 my $fh = $log->{fh
};
1776 if ($log->{state} eq 'msg') {
1777 if ($ret->{lines
}) {
1778 $ret->{msg
} .= $_."\n";
1779 unless(--$ret->{lines
}) {
1780 $log->{state} = 'sep';
1783 croak
"Log parse error at: $_\n",
1789 if ($log->{state} ne 'sep') {
1790 croak
"Log parse error at: $_\n",
1791 "state: $log->{state}\n",
1795 $log->{state} = 'rev';
1797 # if we have an empty log message, put something there:
1799 $ret->{msg
} ||= "\n";
1800 delete $ret->{lines
};
1805 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1807 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1808 ($lines) = ($lines =~ /(\d+)/);
1809 $date = '1970-01-01 00:00:00 +0000'
1810 if ($_ignore_nodate && $date eq '(no date)');
1811 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1812 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1813 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1814 or croak
"Failed to parse date: $date\n";
1815 $ret = { revision
=> $rev,
1816 date
=> "$tz $Y-$m-$d $H:$M:$S",
1820 if (defined $_authors && ! defined $users{$author}) {
1821 die "Author: $author not defined in ",
1824 $log->{state} = 'msg_start';
1827 # skip the first blank line of the message:
1828 if ($log->{state} eq 'msg_start' && /^$/) {
1829 $log->{state} = 'msg';
1830 } elsif ($log->{state} eq 'msg') {
1831 if ($ret->{lines
}) {
1832 $ret->{msg
} .= $_."\n";
1833 unless (--$ret->{lines
}) {
1834 $log->{state} = 'sep';
1837 croak
"Log parse error at: $_\n",
1838 $ret->{revision
},"\n";
1846 my $url = shift || $SVN_URL;
1848 my $pid = open my $info_fh, '-|';
1849 defined $pid or croak
$!;
1852 exec(qw(svn info),$url) or croak
$!;
1856 # only single-lines seem to exist in svn info output
1857 while (<$info_fh>) {
1859 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
1861 push @
{$ret->{-order
}}, $1;
1864 close $info_fh or croak
$?
;
1868 sub sys
{ system(@_) == 0 or croak
$?
}
1870 sub do_update_index
{
1871 my ($z_cmd, $cmd, $no_text_base) = @_;
1873 my $z = open my $p, '-|';
1874 defined $z or croak
$!;
1875 unless ($z) { exec @
$z_cmd or croak
$! }
1877 my $pid = open my $ui, '|-';
1878 defined $pid or croak
$!;
1880 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
1883 while (my $x = <$p>) {
1885 if (!$no_text_base && lstat $x && ! -l _
&&
1886 svn_propget_base
('svn:keywords', $x)) {
1887 my $mode = -x _ ?
0755 : 0644;
1888 my ($v,$d,$f) = File
::Spec
->splitpath($x);
1889 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
1890 'text-base',"$f.svn-base");
1893 $tb = File
::Spec
->catfile($d, '.svn',
1894 'text-base',"$f.svn-base");
1898 unlink $x or croak
$!;
1900 chmod(($mode &~ umask), $x) or croak
$!;
1901 utime $s[8], $s[9], $x;
1905 close $ui or croak
$?
;
1909 return if $_use_lib;
1911 if (!-f
"$GIT_SVN_DIR/info/exclude") {
1912 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak
$!;
1913 print $fd '.svn',"\n";
1914 close $fd or croak
$!;
1916 my $no_text_base = shift;
1917 do_update_index
([qw
/git-diff-files --name-only -z/],
1920 do_update_index
([qw
/git-ls-files -z --others/,
1921 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1927 my ($str, $file, $mode) = @_;
1928 open my $fd,'>',$file or croak
$!;
1929 print $fd $str,"\n" or croak
$!;
1930 close $fd or croak
$!;
1931 chmod ($mode &~ umask, $file) if (defined $mode);
1936 open my $fd,'<',$file or croak
"$!: file: $file\n";
1939 close $fd or croak
$!;
1944 sub assert_revision_unknown
{
1946 if (my $c = revdb_get
($REVDB, $r)) {
1947 croak
"$r = $c already exists! Why are we refetching it?";
1953 my @x = safe_qx
('git-cat-file','commit',$x);
1954 my @y = safe_qx
('git-cat-file','commit',$y);
1955 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1956 || $y[0] !~ /^tree $sha1\n$/) {
1957 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
1964 my ($log_msg, @parents) = @_;
1965 assert_revision_unknown
($log_msg->{revision
});
1966 map_tree_joins
() if (@_branch_from && !%tree_map);
1968 my (@tmp_parents, @exec_parents, %seen_parent);
1969 if (my $lparents = $log_msg->{parents
}) {
1970 @tmp_parents = @
$lparents
1972 # commit parents can be conditionally bound to a particular
1973 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1974 foreach my $p (@parents) {
1975 next unless defined $p;
1976 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1977 if ($1 == $log_msg->{revision
}) {
1978 push @tmp_parents, $2;
1981 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1984 my $tree = $log_msg->{tree
};
1985 if (!defined $tree) {
1986 my $index = set_index
($GIT_SVN_INDEX);
1988 chomp($tree = `git-write-tree`);
1990 restore_index
($index);
1993 # just in case we clobber the existing ref, we still want that ref
1995 if (my $cur = eval { file_to_s
("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1996 push @tmp_parents, $cur;
1999 if (exists $tree_map{$tree}) {
2000 foreach my $p (@
{$tree_map{$tree}}) {
2002 foreach (@tmp_parents) {
2003 # see if a common parent is found
2005 safe_qx
('git-merge-base', $_, $p)
2012 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
2013 next if (($SVN_UUID eq $uuid_p) &&
2014 ($log_msg->{revision
} > $r_p));
2015 next if (defined $url_p && defined $SVN_URL &&
2016 ($SVN_UUID eq $uuid_p) &&
2017 ($url_p eq $SVN_URL));
2018 push @tmp_parents, $p;
2021 foreach (@tmp_parents) {
2022 next if $seen_parent{$_};
2023 $seen_parent{$_} = 1;
2024 push @exec_parents, $_;
2025 # MAXPARENT is defined to 16 in commit-tree.c:
2026 last if @exec_parents > 16;
2029 set_commit_env
($log_msg);
2030 my @exec = ('git-commit-tree', $tree);
2031 push @exec, '-p', $_ foreach @exec_parents;
2032 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2034 print $msg_fh $log_msg->{msg
} or croak
$!;
2035 unless ($_no_metadata) {
2036 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
2037 " $SVN_UUID\n" or croak
$!;
2039 $msg_fh->flush == 0 or croak
$!;
2040 close $msg_fh or croak
$!;
2041 chomp(my $commit = do { local $/; <$out_fh> });
2042 close $out_fh or croak
$!;
2045 if ($commit !~ /^$sha1$/o) {
2046 die "Failed to commit, invalid sha1: $commit\n";
2048 sys
('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2049 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
2051 # this output is read via pipe, do not change:
2052 print "r$log_msg->{revision} = $commit\n";
2058 if ($_repack && (--$_repack_nr == 0)) {
2059 $_repack_nr = $_repack;
2060 sys
("git repack $_repack_flags");
2064 sub set_commit_env
{
2066 my $author = $log_msg->{author
};
2067 if (!defined $author || length $author == 0) {
2068 $author = '(no author)';
2070 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
2071 : ($author,"$author\@$SVN_UUID");
2072 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
2073 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
2074 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
2077 sub apply_mod_line_blob
{
2079 if ($m->{mode_b
} =~ /^120/) {
2080 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
2082 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
2086 sub blob_to_symlink
{
2087 my ($blob, $link) = @_;
2088 defined $link or croak
"\$link not defined!\n";
2089 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2090 if (-l
$link || -f _
) {
2091 unlink $link or croak
$!;
2094 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2095 symlink $dest, $link or croak
$!;
2099 my ($blob, $file) = @_;
2100 defined $file or croak
"\$file not defined!\n";
2101 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2102 if (-l
$file || -f _
) {
2103 unlink $file or croak
$!;
2106 open my $blob_fh, '>', $file or croak
"$!: $file\n";
2108 defined $pid or croak
$!;
2111 open STDOUT
, '>&', $blob_fh or croak
$!;
2112 exec('git-cat-file','blob',$blob) or croak
$!;
2117 close $blob_fh or croak
$!;
2121 my $pid = open my $child, '-|';
2122 defined $pid or croak
$!;
2124 exec(@_) 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 sub svn_compat_check
{
2133 if ($_follow_parent) {
2134 print STDERR
'E: --follow-parent functionality is only ',
2135 "available when SVN libraries are used\n";
2138 my @co_help = safe_qx
(qw(svn co -h));
2139 unless (grep /ignore-externals/,@co_help) {
2140 print STDERR
"W: Installed svn version does not support ",
2141 "--ignore-externals\n";
2142 $_no_ignore_ext = 1;
2144 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2145 $_svn_co_url_revs = 1;
2147 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2148 $_svn_pg_peg_revs = 1;
2151 # I really, really hope nobody hits this...
2152 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
2154 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
2156 Lets hope the directory you
're tracking is not a branch or tag
2157 and was never moved within the repository...
2163 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
2164 # (and they won't honor URL@<rev> without -r<rev>, too!)
2165 sub svn_cmd_checkout
{
2166 my ($url, $rev, $dir) = @_;
2167 my @cmd = ('svn','co', "-r$rev");
2168 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2169 $url .= "\@$rev" if $_svn_co_url_revs;
2170 sys
(@cmd, $url, $dir);
2173 sub check_upgrade_needed
{
2175 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2176 open my $fh, '>>',$REVDB or croak
$!;
2180 my $pid = open my $child, '-|';
2181 defined $pid or croak
$!;
2184 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$!;
2186 my @ret = (<$child>);
2187 close $child or croak
$?
;
2188 die $?
if $?
; # just in case close didn't error out
2189 return wantarray ?
@ret : join('',@ret);
2192 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
2194 print STDERR
"Please run: $0 rebuild --upgrade\n";
2199 # fills %tree_map with a reverse mapping of trees to commits. Useful
2200 # for finding parents to commit on.
2201 sub map_tree_joins
{
2203 foreach my $br (@_branch_from) {
2204 my $pid = open my $pipe, '-|';
2205 defined $pid or croak
$!;
2207 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2211 if (/^commit ($sha1)$/o) {
2214 # if we've seen a commit,
2215 # we've seen its parents
2216 last if $seen{$commit};
2217 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2218 unless (defined $tree) {
2219 die "Failed to parse commit $commit\n";
2221 push @
{$tree_map{$tree}}, $commit;
2225 close $pipe; # we could be breaking the pipe early
2230 if (@_branch_from) {
2231 print STDERR
'--branch|-b parameters are ignored when ',
2232 "--branch-all-refs|-B is passed\n";
2235 # don't worry about rev-list on non-commit objects/tags,
2236 # it shouldn't blow up if a ref is a blob or tree...
2237 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2240 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2242 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2243 while (<$authors>) {
2245 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2246 my ($user, $name, $email) = ($1, $2, $3);
2247 $users{$user} = [$name, $email];
2249 close $authors or croak
$!;
2253 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2254 while (<$authors>) {
2256 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2257 my ($user, $name, $email) = ($1, $2, $3);
2258 $rusers{"$name <$email>"} = $user;
2260 close $authors or croak
$!;
2263 sub svn_propget_base
{
2265 $f .= '@BASE' if $_svn_pg_peg_revs;
2266 return safe_qx
(qw
/svn propget/, $p, $f);
2271 foreach (`git-rev-parse --symbolic --all`) {
2272 next unless s
#^refs/remotes/##;
2274 next unless -f
"$GIT_DIR/svn/$_/info/url";
2282 defined(my $pid = fork) or croak
$!;
2284 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2286 exit 0 if -r
$REVDB;
2287 print "Upgrading svn => git mapping...\n";
2288 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2289 open my $fh, '>>',$REVDB or croak
$!;
2292 print "Done upgrading. You may now delete the ",
2293 "deprecated $GIT_SVN_DIR/revs directory\n";
2301 sub migration_check
{
2302 migrate_revdb
() unless (-e
$REVDB);
2303 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
2304 print "Upgrading repository...\n";
2305 unless (-d
"$GIT_DIR/svn") {
2306 mkdir "$GIT_DIR/svn" or croak
$!;
2308 print "Data from a previous version of git-svn exists, but\n\t",
2309 "$GIT_SVN_DIR\n\t(required for this version ",
2310 "($VERSION) of git-svn) does not.\n";
2312 foreach my $x (`git-rev-parse --symbolic --all`) {
2313 next unless $x =~ s
#^refs/remotes/##;
2315 next unless -f
"$GIT_DIR/$x/info/url";
2316 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
2318 my $dn = dirname
("$GIT_DIR/svn/$x");
2319 mkpath
([$dn]) unless -d
$dn;
2320 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
2322 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
2323 print "Done upgrading.\n";
2326 sub find_rev_before
{
2327 my ($r, $id, $eq_ok) = @_;
2328 my $f = "$GIT_DIR/svn/$id/.rev_db";
2329 return (undef,undef) unless -r
$f;
2332 if (my $c = revdb_get
($f, $r)) {
2337 return (undef, undef);
2341 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
2342 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2343 $REVDB = "$GIT_SVN_DIR/.rev_db";
2344 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2346 $SVN_WC = "$GIT_SVN_DIR/tree";
2350 # convert GetOpt::Long specs for use by git-repo-config
2351 sub read_repo_config
{
2352 return unless -d
$GIT_DIR;
2354 foreach my $o (keys %$opts) {
2355 my $v = $opts->{$o};
2356 my ($key) = ($o =~ /^([a-z\-]+)/);
2358 my $arg = 'git-repo-config';
2359 $arg .= ' --int' if ($o =~ /[:=]i$/);
2360 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2361 if (ref $v eq 'ARRAY') {
2362 chomp(my @tmp = `$arg --get-all svn.$key`);
2365 chomp(my $tmp = `$arg --get svn.$key`);
2366 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2373 sub set_default_vals
{
2374 if (defined $_repack) {
2375 $_repack = 1000 if ($_repack <= 0);
2376 $_repack_nr = $_repack;
2377 $_repack_flags ||= '-d';
2382 my $gr_file = shift;
2383 my ($grafts, $comments) = ({}, {});
2384 if (open my $fh, '<', $gr_file) {
2387 if (/^($sha1)\s+/) {
2390 @
{$comments->{$c}} = @tmp;
2393 foreach my $p (split /\s+/, $_) {
2394 $grafts->{$c}->{$p} = 1;
2400 close $fh or croak
$!;
2401 @
{$comments->{'END'}} = @tmp if @tmp;
2403 return ($grafts, $comments);
2407 my ($grafts, $comments, $gr_file) = @_;
2409 open my $fh, '>', $gr_file or croak
$!;
2410 foreach my $c (sort keys %$grafts) {
2411 if ($comments->{$c}) {
2412 print $fh $_ foreach @
{$comments->{$c}};
2414 my $p = $grafts->{$c};
2415 my %x; # real parents
2416 delete $p->{$c}; # commits are not self-reproducing...
2417 my $pid = open my $ch, '-|';
2418 defined $pid or croak
$!;
2420 exec(qw
/git-cat-file commit/, $c) or croak
$!;
2423 if (/^parent ($sha1)/) {
2424 $x{$1} = $p->{$1} = 1;
2429 close $ch; # breaking the pipe
2431 # if real parents are the only ones in the grafts, drop it
2432 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2436 @ip = @jp = keys %$p;
2437 foreach my $i (@ip) {
2438 next if $del{$i} || $p->{$i} == 2;
2439 foreach my $j (@jp) {
2440 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2441 $mb = eval { safe_qx
('git-merge-base',$i,$j) };
2448 } elsif ($mb eq $i) {
2455 # if real parents are the only ones in the grafts, drop it
2456 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2458 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2460 if ($comments->{'END'}) {
2461 print $fh $_ foreach @
{$comments->{'END'}};
2463 close $fh or croak
$!;
2466 sub read_url_paths_all
{
2467 my ($l_map, $pfx, $p) = @_;
2470 if (-r
"$_/info/url") {
2471 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2472 my $id = $pfx . basename
$_;
2473 my $url = file_to_s
("$_/info/url");
2474 my ($u, $p) = repo_path_split
($url);
2475 $l_map->{$u}->{$p} = $id;
2482 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2483 read_url_paths_all
($l_map, $x, $_);
2487 # this one only gets ids that have been imported, not new ones
2488 sub read_url_paths
{
2490 git_svn_each
(sub { my $x = shift;
2491 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
2492 my ($u, $p) = repo_path_split
($url);
2493 $l_map->{$u}->{$p} = $x;
2498 sub extract_metadata
{
2499 my $id = shift or return (undef, undef, undef);
2500 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
2502 if (!defined $rev || !$uuid || !$url) {
2503 # some of the original repositories I made had
2504 # identifiers like this:
2505 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2507 return ($url, $rev, $uuid);
2511 return extract_metadata
((grep(/^git-svn-id: /,
2512 safe_qx
(qw
/git-cat-file commit/, shift)))[-1]);
2515 sub get_commit_time
{
2517 defined(my $pid = open my $fh, '-|') or croak
$!;
2519 exec qw
/git-rev-list --pretty=raw -n1/, $cmt or croak
$!;
2522 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2523 my ($s, $tz) = ($1, $2);
2524 if ($tz =~ s/^\+//) {
2525 $s += tz_to_s_offset
($tz);
2526 } elsif ($tz =~ s/^\-//) {
2527 $s -= tz_to_s_offset
($tz);
2532 die "Can't get commit time for commit: $cmt\n";
2535 sub tz_to_s_offset
{
2538 return ($1 * 60) + ($tz * 3600);
2541 sub setup_pager
{ # translated to Perl from pager.c
2542 return unless (-t
*STDOUT
);
2543 my $pager = $ENV{PAGER
};
2544 if (!defined $pager) {
2546 } elsif (length $pager == 0 || $pager eq 'cat') {
2549 pipe my $rfd, my $wfd or return;
2550 defined(my $pid = fork) or croak
$!;
2552 open STDOUT
, '>&', $wfd or croak
$!;
2555 open STDIN
, '<&', $rfd or croak
$!;
2556 $ENV{LESS
} ||= '-S';
2557 exec $pager or croak
"Can't run pager: $!\n";;
2560 sub get_author_info
{
2561 my ($dest, $author, $t, $tz) = @_;
2562 $author =~ s/(?:^\s*|\s*$)//g;
2563 $dest->{a_raw
} = $author;
2566 $_a = $rusers{$author} || undef;
2569 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2574 # Date::Parse isn't in the standard Perl distro :(
2575 if ($tz =~ s/^\+//) {
2576 $t += tz_to_s_offset
($tz);
2577 } elsif ($tz =~ s/^\-//) {
2578 $t -= tz_to_s_offset
($tz);
2580 $dest->{t_utc
} = $t;
2583 sub process_commit
{
2584 my ($c, $r_min, $r_max, $defer) = @_;
2585 if (defined $r_min && defined $r_max) {
2586 if ($r_min == $c->{r
} && $r_min == $r_max) {
2590 return 1 if $r_min == $r_max;
2591 if ($r_min < $r_max) {
2592 # we need to reverse the print order
2593 return 0 if (defined $_limit && --$_limit < 0);
2597 if ($r_min != $r_max) {
2598 return 1 if ($r_min < $c->{r
});
2599 return 1 if ($r_max > $c->{r
});
2602 return 0 if (defined $_limit && --$_limit < 0);
2611 if (my $l = $c->{l
}) {
2612 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2615 $_l_fmt ||= 'A' . length($c->{r
});
2616 print 'r',pack($_l_fmt, $c->{r
}),' | ';
2617 print "$c->{c} | " if $_show_commit;
2620 show_commit_normal
($c);
2624 sub show_commit_changed_paths
{
2626 return unless $c->{changed
};
2627 print "Changed paths:\n", @
{$c->{changed
}};
2630 sub show_commit_normal
{
2632 print '-' x72
, "\nr$c->{r} | ";
2633 print "$c->{c} | " if $_show_commit;
2634 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2635 localtime($c->{t_utc
})), ' | ';
2638 if (my $l = $c->{l
}) {
2639 while ($l->[$#$l] eq "\n" && $#$l > 0
2640 && $l->[($#$l - 1)] eq "\n") {
2643 $nr_line = scalar @
$l;
2645 print "1 line\n\n\n";
2647 if ($nr_line == 1) {
2648 $nr_line = '1 line';
2650 $nr_line .= ' lines';
2652 print $nr_line, "\n";
2653 show_commit_changed_paths
($c);
2655 print $_ foreach @
$l;
2659 show_commit_changed_paths
($c);
2663 foreach my $x (qw
/raw diff/) {
2666 print $_ foreach @
{$c->{$x}}
2672 return unless $_use_lib;
2675 if ($SVN::Core
::VERSION
lt '1.1.0') {
2676 die "Need SVN::Core 1.1.0 or better ",
2677 "(got $SVN::Core::VERSION) ",
2678 "Falling back to command-line svn\n";
2682 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
2683 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2684 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2685 $SVN::Node
::none
.$SVN::Node
::file
.
2686 $SVN::Node
::dir
.$SVN::Node
::unknown
;
2691 sub libsvn_connect
{
2693 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
(),
2694 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2695 SVN
::Client
::get_username_provider
()]);
2696 my $s = eval { SVN
::Ra
->new(url
=> $url, auth
=> $auth) };
2700 sub libsvn_get_file
{
2701 my ($gui, $f, $rev, $chg) = @_;
2703 if (length $SVN_PATH > 0) {
2704 return unless ($p =~ s
#^\Q$SVN_PATH\E/##);
2706 print "\t$chg\t$f\n" unless $_q;
2708 my ($hash, $pid, $in, $out);
2709 my $pool = SVN
::Pool
->new;
2710 defined($pid = open3
($in, $out, '>&STDERR',
2711 qw
/git-hash-object -w --stdin/)) or croak
$!;
2712 # redirect STDOUT for SVN 1.1.x compatibility
2713 open my $stdout, '>&', \
*STDOUT
or croak
$!;
2714 open STDOUT
, '>&', $in or croak
$!;
2715 my ($r, $props) = $SVN->get_file($f, $rev, \
*STDOUT
, $pool);
2716 $in->flush == 0 or croak
$!;
2717 open STDOUT
, '>&', $stdout or croak
$!;
2718 close $in or croak
$!;
2719 close $stdout or croak
$!;
2721 chomp($hash = do { local $/; <$out> });
2722 close $out or croak
$!;
2724 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2726 my $mode = exists $props->{'svn:executable'} ?
'100755' : '100644';
2727 if (exists $props->{'svn:special'}) {
2729 my $link = `git-cat-file blob $hash`;
2730 $link =~ s/^link // or die "svn:special file with contents: <",
2731 $link, "> is not understood\n";
2732 defined($pid = open3
($in, $out, '>&STDERR',
2733 qw
/git-hash-object -w --stdin/)) or croak
$!;
2735 $in->flush == 0 or croak
$!;
2736 close $in or croak
$!;
2737 chomp($hash = do { local $/; <$out> });
2738 close $out or croak
$!;
2740 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2742 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak
$!;
2745 sub libsvn_log_entry
{
2746 my ($rev, $author, $date, $msg, $parents) = @_;
2747 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2748 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2749 or die "Unable to parse date: $date\n";
2750 if (defined $_authors && ! defined $users{$author}) {
2751 die "Author: $author not defined in $_authors file\n";
2753 $msg = '' if ($rev == 0 && !defined $msg);
2754 return { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2755 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [] }
2759 my ($gui, $last_commit, $f) = @_;
2760 $f =~ s
#^\Q$SVN_PATH\E/?## or return;
2761 # remove entire directories.
2762 if (safe_qx
('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2763 defined(my $pid = open my $ls, '-|') or croak
$!;
2765 exec(qw
/git-ls-tree -r --name-only -z/,
2766 $last_commit,'--',$f) or croak
$!;
2770 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2772 close $ls or croak
$?
;
2774 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2779 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2780 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2782 foreach my $f (keys %$paths) {
2783 my $m = $paths->{$f}->action();
2785 if ($m =~ /^[DR]$/) {
2786 print "\t$m\t$f\n" unless $_q;
2787 process_rm
($gui, $last_commit, $f);
2789 # 'R' can be file replacements, too, right?
2791 my $pool = SVN
::Pool
->new;
2792 my $t = $SVN->check_path($f, $rev, $pool);
2793 if ($t == $SVN::Node
::file
) {
2794 if ($m =~ /^[AMR]$/) {
2795 push @amr, [ $m, $f ];
2797 die "Unrecognized action: $m, ($f r$rev)\n";
2799 } elsif ($t == $SVN::Node
::dir
&& $m =~ /^[AR]$/) {
2801 libsvn_traverse
($gui, '', $f, $rev, \
@traversed);
2802 foreach (@traversed) {
2803 push @amr, [ $m, $_ ]
2809 libsvn_get_file
($gui, $_->[1], $rev, $_->[0]);
2811 close $gui or croak
$?
;
2812 return libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2815 sub svn_grab_base_rev
{
2816 defined(my $pid = open my $fh, '-|') or croak
$!;
2818 open my $null, '>', '/dev/null' or croak
$!;
2819 open STDERR
, '>&', $null or croak
$!;
2820 exec qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2823 chomp(my $c = do { local $/; <$fh> });
2825 if (defined $c && length $c) {
2826 my ($url, $rev, $uuid) = cmt_metadata
($c);
2827 return ($rev, $c) if defined $rev;
2829 if ($_no_metadata) {
2830 my $offset = -41; # from tail
2832 open my $fh, '<', $REVDB or
2833 die "--no-metadata specified and $REVDB not readable\n";
2834 seek $fh, $offset, 2;
2836 defined $rl or return (undef, undef);
2838 while ($c ne $rl && tell $fh != 0) {
2840 seek $fh, $offset, 2;
2842 defined $rl or return (undef, undef);
2846 croak
$! if ($rev < -1);
2847 $rev = ($rev - 41) / 41;
2848 close $fh or croak
$!;
2851 return (undef, undef);
2854 sub libsvn_parse_revision
{
2856 my $head = $SVN->get_latest_revnum();
2857 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2858 return ($base + 1, $head) if (defined $base);
2861 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2862 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2863 if ($_revision =~ /^BASE:(\d+)$/) {
2864 return ($base + 1, $1) if (defined $base);
2867 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2868 die "revision argument: $_revision not understood by git-svn\n",
2869 "Try using the command-line svn client instead\n";
2872 sub libsvn_traverse
{
2873 my ($gui, $pfx, $path, $rev, $files) = @_;
2874 my $cwd = "$pfx/$path";
2875 my $pool = SVN
::Pool
->new;
2877 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2878 foreach my $d (keys %$dirent) {
2879 my $t = $dirent->{$d}->kind;
2880 if ($t == $SVN::Node
::dir
) {
2881 libsvn_traverse
($gui, $cwd, $d, $rev, $files);
2882 } elsif ($t == $SVN::Node
::file
) {
2883 my $file = "$cwd/$d";
2884 if (defined $files) {
2885 push @
$files, $file;
2887 libsvn_get_file
($gui, $file, $rev, 'A');
2894 sub libsvn_traverse_ignore
{
2895 my ($fh, $path, $r) = @_;
2897 my $pool = SVN
::Pool
->new;
2898 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2900 $p =~ s
#^\Q$SVN_PATH\E/?##;
2901 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2902 if (my $s = $props->{'svn:ignore'}) {
2903 $s =~ s/[\r\n]+/\n/g;
2905 if (length $p == 0) {
2909 $s =~ s
#\n#\n/$p/#g;
2910 print $fh "/$p/$s\n";
2913 foreach (sort keys %$dirent) {
2914 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2915 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2921 my ($path, $r0, $r1) = @_;
2922 return 1 if $r0 == $r1;
2925 # should be OK to use Pool here (r1 - r0) should be small
2926 my $pool = SVN
::Pool
->new;
2927 libsvn_get_log
($SVN, "/$path", $r0, $r1,
2928 0, 1, 1, sub {$nr++}, $pool);
2931 my ($url, undef) = repo_path_split
($SVN_URL);
2932 my $svn_log = svn_log_raw
("$url/$path","-r$r0:$r1");
2933 while (next_log_entry
($svn_log)) { $nr++ }
2934 close $svn_log->{fh
};
2936 return 0 if ($nr > 1);
2940 sub libsvn_find_parent_branch
{
2941 my ($paths, $rev, $author, $date, $msg) = @_;
2942 my $svn_path = '/'.$SVN_PATH;
2944 # look for a parent from another branch:
2945 my $i = $paths->{$svn_path} or return;
2946 my $branch_from = $i->copyfrom_path or return;
2947 my $r = $i->copyfrom_rev;
2948 print STDERR
"Found possible branch point: ",
2949 "$branch_from => $svn_path, $r\n";
2950 $branch_from =~ s
#^/##;
2952 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
2953 my $url = $SVN->{url
};
2954 defined $l_map->{$url} or return;
2955 my $id = $l_map->{$url}->{$branch_from};
2956 if (!defined $id && $_follow_parent) {
2957 print STDERR
"Following parent: $branch_from\@$r\n";
2958 # auto create a new branch and follow it
2959 $id = basename
($branch_from);
2960 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
2961 while (-r
"$GIT_DIR/svn/$id") {
2962 # just grow a tail if we're not unique enough :x
2966 return unless defined $id;
2968 my ($r0, $parent) = find_rev_before
($r,$id,1);
2969 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2970 defined(my $pid = fork) or croak
$!;
2972 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2974 $SVN_URL = "$url/$branch_from";
2975 $SVN_LOG = $SVN = undef;
2977 # we can't assume SVN_URL exists at r+1:
2978 $_revision = "0:$r";
2984 ($r0, $parent) = find_rev_before
($r,$id,1);
2986 return unless (defined $r0 && defined $parent);
2987 if (revisions_eq
($branch_from, $r0, $r)) {
2988 unlink $GIT_SVN_INDEX;
2989 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
2990 sys
(qw
/git-read-tree/, $parent);
2991 return libsvn_fetch
($parent, $paths, $rev,
2992 $author, $date, $msg);
2994 print STDERR
"Nope, branch point not imported or unknown\n";
2998 sub libsvn_get_log
{
2999 my ($ra, @args) = @_;
3000 if ($SVN::Core
::VERSION
le '1.2.0') {
3001 splice(@args, 3, 1);
3003 $ra->get_log(@args);
3006 sub libsvn_new_tree
{
3007 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
3010 my ($paths, $rev, $author, $date, $msg) = @_;
3011 open my $gui, '| git-update-index -z --index-info' or croak
$!;
3012 libsvn_traverse
($gui, '', $SVN_PATH, $rev);
3013 close $gui or croak
$?
;
3014 return libsvn_log_entry
($rev, $author, $date, $msg);
3017 sub find_graft_path_commit
{
3018 my ($tree_paths, $p1, $r1) = @_;
3019 foreach my $x (keys %$tree_paths) {
3020 next unless ($p1 =~ /^\Q$x\E/);
3021 my $i = $tree_paths->{$x};
3022 my ($r0, $parent) = find_rev_before
($r1,$i,1);
3023 return $parent if (defined $r0 && $r0 == $r1);
3024 print STDERR
"r$r1 of $i not imported\n";
3030 sub find_graft_path_parents
{
3031 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
3032 foreach my $x (keys %$tree_paths) {
3033 next unless ($p0 =~ /^\Q$x\E/);
3034 my $i = $tree_paths->{$x};
3035 my ($r, $parent) = find_rev_before
($r0, $i, 1);
3036 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
3037 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
3038 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
3039 next if ($url_a && $url_b && $url_a eq $url_b &&
3040 $uuid_b eq $uuid_a);
3041 $grafts->{$c}->{$parent} = 1;
3046 sub libsvn_graft_file_copies
{
3047 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
3048 foreach (keys %$paths) {
3049 my $i = $paths->{$_};
3050 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
3052 next unless (defined $p0 && defined $r0);
3057 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
3059 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
3064 my $old = $ENV{GIT_INDEX_FILE
};
3065 $ENV{GIT_INDEX_FILE
} = shift;
3072 $ENV{GIT_INDEX_FILE
} = $old;
3074 delete $ENV{GIT_INDEX_FILE
};
3078 sub libsvn_commit_cb
{
3079 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3080 if ($_optimize_commits && $rev == ($r_last + 1)) {
3081 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
3082 $log->{tree
} = get_tree_from_treeish
($c);
3083 my $cmt = git_commit
($log, $cmt_last, $c);
3084 my @diff = safe_qx
('git-diff-tree', $cmt, $c);
3086 print STDERR
"Trees differ: $cmt $c\n",
3087 join('',@diff),"\n";
3095 sub libsvn_ls_fullurl
{
3096 my $fullurl = shift;
3097 my ($repo, $path) = repo_path_split
($fullurl);
3098 $SVN ||= libsvn_connect
($repo);
3100 my $pool = SVN
::Pool
->new;
3101 my ($dirent, undef, undef) = $SVN->get_dir($path,
3102 $SVN->get_latest_revnum, $pool);
3103 foreach my $d (keys %$dirent) {
3104 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
3105 push @ret, "$d/"; # add '/' for compat with cli svn
3113 sub libsvn_skip_unknown_revs
{
3115 my $errno = $err->apr_err();
3116 # Maybe the branch we're tracking didn't
3117 # exist when the repo started, so it's
3118 # not an error if it doesn't, just continue
3120 # Wonderfully consistent library, eh?
3121 # 160013 - svn:// and file://
3122 # 175002 - http(s)://
3123 # More codes may be discovered later...
3124 if ($errno == 175002 || $errno == 160013) {
3127 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
3130 # Tie::File seems to be prone to offset errors if revisions get sparse,
3131 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3132 # one of my favorite modules is out :< Next up would be one of the DBM
3133 # modules, but I'm not sure which is most portable... So I'll just
3134 # go with something that's plain-text, but still capable of
3135 # being randomly accessed. So here's my ultra-simple fixed-width
3136 # database. All records are 40 characters + "\n", so it's easy to seek
3137 # to a revision: (41 * rev) is the byte offset.
3138 # A record of 40 0s denotes an empty revision.
3139 # And yes, it's still pretty fast (faster than Tie::File).
3141 my ($file, $rev, $commit) = @_;
3142 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
3143 open my $fh, '+<', $file or croak
$!;
3144 my $offset = $rev * 41;
3145 # assume that append is the common case:
3146 seek $fh, 0, 2 or croak
$!;
3148 if ($pos < $offset) {
3149 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
3151 seek $fh, $offset, 0 or croak
$!;
3152 print $fh $commit,"\n";
3153 close $fh or croak
$!;
3157 my ($file, $rev) = @_;
3159 my $offset = $rev * 41;
3160 open my $fh, '<', $file or croak
$!;
3161 seek $fh, $offset, 0;
3162 if (tell $fh == $offset) {
3163 $ret = readline $fh;
3166 $ret = undef if ($ret =~ /^0{40}$/);
3169 close $fh or croak
$!;
3173 sub copy_remote_ref
{
3174 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
3175 my $ref = "refs/remotes/$GIT_SVN";
3176 if (safe_qx
('git-ls-remote', $origin, $ref)) {
3177 sys
(qw
/git fetch/, $origin, "$ref:$ref");
3178 } elsif ($_cp_remote && !$_upgrade) {
3179 die "Unable to find remote reference: ",
3180 "refs/remotes/$GIT_SVN on $origin\n";
3184 package SVN
::Git
::Editor
;
3193 my $git_svn = shift;
3194 my $self = SVN
::Delta
::Editor
->new(@_);
3195 bless $self, $class;
3196 foreach (qw
/svn_path c r ra /) {
3197 die "$_ required!\n" unless (defined $git_svn->{$_});
3198 $self->{$_} = $git_svn->{$_};
3200 $self->{pool
} = SVN
::Pool
->new;
3201 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3203 require Digest
::MD5
;
3208 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3212 (defined $_[1] && length $_[1]) ?
"$_[0]->{svn_path}/$_[1]"
3217 my ($self, $path) = @_;
3218 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
3222 my ($self, $q) = @_;
3223 my $rm = $self->{rm
};
3224 delete $rm->{''}; # we never delete the url we're tracking
3227 foreach (keys %$rm) {
3228 my @d = split m
#/#, $_;
3232 $c .= '/' . shift @d;
3236 delete $rm->{$self->{svn_path
}};
3237 delete $rm->{''}; # we never delete the url we're tracking
3240 defined(my $pid = open my $fh,'-|') or croak
$!;
3242 exec qw
/git-ls-tree --name-only -r -z/, $self->{c
} or croak
$!;
3245 my @svn_path = split m
#/#, $self->{svn_path};
3248 my @dn = (@svn_path, (split m
#/#, $_));
3250 delete $rm->{join '/', @dn};
3259 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3260 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3261 $self->close_directory($bat->{$d}, $p);
3262 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3263 print "\tD+\t/$d/\n" unless $q;
3264 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3269 sub open_or_add_dir
{
3270 my ($self, $full_path, $baton) = @_;
3271 my $p = SVN
::Pool
->new;
3272 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
3274 if ($t == $SVN::Node
::none
) {
3275 return $self->add_directory($full_path, $baton,
3276 undef, -1, $self->{pool
});
3277 } elsif ($t == $SVN::Node
::dir
) {
3278 return $self->open_directory($full_path, $baton,
3279 $self->{r
}, $self->{pool
});
3281 print STDERR
"$full_path already exists in repository at ",
3282 "r$self->{r} and it is not a directory (",
3283 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3288 my ($self, $path) = @_;
3289 my $bat = $self->{bat
};
3290 $path = $self->repo_path($path);
3291 return $bat->{''} unless (length $path);
3292 my @p = split m
#/+#, $path;
3294 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3297 $c .= '/' . shift @p;
3298 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3304 my ($self, $m, $q) = @_;
3305 my ($dir, $file) = split_path
($m->{file_b
});
3306 my $pbat = $self->ensure_path($dir);
3307 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3309 print "\tA\t$m->{file_b}\n" unless $q;
3310 $self->chg_file($fbat, $m);
3311 $self->close_file($fbat,undef,$self->{pool
});
3315 my ($self, $m, $q) = @_;
3316 my ($dir, $file) = split_path
($m->{file_b
});
3317 my $pbat = $self->ensure_path($dir);
3318 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3319 $self->url_path($m->{file_a
}), $self->{r
});
3320 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3321 $self->chg_file($fbat, $m);
3322 $self->close_file($fbat,undef,$self->{pool
});
3326 my ($self, $path, $pbat) = @_;
3327 my $rpath = $self->repo_path($path);
3328 my ($dir, $file) = split_path
($rpath);
3329 $self->{rm
}->{$dir} = 1;
3330 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3334 my ($self, $m, $q) = @_;
3335 my ($dir, $file) = split_path
($m->{file_b
});
3336 my $pbat = $self->ensure_path($dir);
3337 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3338 $self->url_path($m->{file_a
}), $self->{r
});
3339 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3340 $self->chg_file($fbat, $m);
3341 $self->close_file($fbat,undef,$self->{pool
});
3343 ($dir, $file) = split_path
($m->{file_a
});
3344 $pbat = $self->ensure_path($dir);
3345 $self->delete_entry($m->{file_a
}, $pbat);
3349 my ($self, $m, $q) = @_;
3350 my ($dir, $file) = split_path
($m->{file_b
});
3351 my $pbat = $self->ensure_path($dir);
3352 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3353 $pbat,$self->{r
},$self->{pool
});
3354 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3355 $self->chg_file($fbat, $m);
3356 $self->close_file($fbat,undef,$self->{pool
});
3359 sub T
{ shift->M(@_) }
3361 sub change_file_prop
{
3362 my ($self, $fbat, $pname, $pval) = @_;
3363 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3367 my ($self, $fbat, $m) = @_;
3368 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3369 $self->change_file_prop($fbat,'svn:executable','*');
3370 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3371 $self->change_file_prop($fbat,'svn:executable',undef);
3373 my $fh = IO
::File
->new_tmpfile or croak
$!;
3374 if ($m->{mode_b
} =~ /^120/) {
3375 print $fh 'link ' or croak
$!;
3376 $self->change_file_prop($fbat,'svn:special','*');
3377 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3378 $self->change_file_prop($fbat,'svn:special',undef);
3380 defined(my $pid = fork) or croak
$!;
3382 open STDOUT
, '>&', $fh or croak
$!;
3383 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3387 $fh->flush == 0 or croak
$!;
3388 seek $fh, 0, 0 or croak
$!;
3390 my $md5 = Digest
::MD5
->new;
3391 $md5->addfile($fh) or croak
$!;
3392 seek $fh, 0, 0 or croak
$!;
3394 my $exp = $md5->hexdigest;
3395 my $pool = SVN
::Pool
->new;
3396 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3397 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
3398 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3401 close $fh or croak
$!;
3405 my ($self, $m, $q) = @_;
3406 my ($dir, $file) = split_path
($m->{file_b
});
3407 my $pbat = $self->ensure_path($dir);
3408 print "\tD\t$m->{file_b}\n" unless $q;
3409 $self->delete_entry($m->{file_b
}, $pbat);
3414 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3415 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3416 $self->close_directory($bat->{$_}, $p);
3418 $self->SUPER::close_edit
($p);
3424 $self->SUPER::abort_edit
($self->{pool
});
3425 $self->{pool
}->clear;
3432 $svn_log hashref
(as returned by svn_log_raw
)
3434 fh
=> file handle of the
log file
,
3435 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
3438 $log_msg hashref as returned by next_log_entry
($svn_log)
3440 msg
=> 'whitespace-formatted log entry
3441 ', # trailing newline is preserved
3442 revision
=> '8', # integer
3443 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3444 author
=> 'committer name'
3448 @mods = array of diff
-index line hashes
, each element represents one line
3449 of diff
-index output
3451 diff
-index line
($m hash
)
3453 mode_a
=> first column of diff
-index output
, no leading
':',
3454 mode_b
=> second column of diff
-index output
,
3455 sha1_b
=> sha1sum of the final blob
,
3456 chg
=> change type
[MCRADT
],
3457 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3458 file_b
=> new
/current file name of a file
(any chg
)
3462 # retval of read_url_paths{,_all}();
3464 # repository root url
3465 'https://svn.musicpd.org' => {
3466 # repository path # GIT_SVN_ID
3467 'mpd/trunk' => 'trunk',
3468 'mpd/tags/0.11.5' => 'tags/0.11.5',
3473 I don
't trust the each() function on unless I created %hash myself
3474 because the internal iterator may not have started at base.