2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '1.1.1-broken';
14 $GIT_DIR = abs_path
($ENV{GIT_DIR
} || '.git');
15 $ENV{GIT_DIR
} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL
};
19 # make sure the svn binary gives consistent output between locales and TZs:
23 # If SVN:: library support is added, please make the dependencies
24 # optional and preserve the capability to use the command-line client.
25 # use eval { require SVN::... } to make it lazy load
26 # We don't use any modules not in the standard Perl distribution:
29 use File
::Basename qw
/dirname basename/;
30 use File
::Path qw
/mkpath/;
31 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
33 use POSIX qw
/strftime/;
36 memoize
('revisions_eq');
37 memoize
('cmt_metadata');
38 memoize
('get_commit_time');
40 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
41 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB
};
43 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
44 my $sha1 = qr/[a-f\d]{40}/;
45 my $sha1_short = qr/[a-f\d]{4,40}/;
46 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
47 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
48 $_repack, $_repack_nr, $_repack_flags,
50 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
51 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
52 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
53 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
54 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
55 my @repo_path_split_cache;
57 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
58 'branch|b=s' => \
@_branch_from,
59 'branch-all-refs|B' => \
$_branch_all_refs,
60 'authors-file|A=s' => \
$_authors,
61 'repack:i' => \
$_repack,
62 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
64 my ($_trunk, $_tags, $_branches);
65 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
66 'tags|t=s' => \
$_tags,
67 'branches|b=s' => \
$_branches );
68 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
69 my %cmt_opts = ( 'edit|e' => \
$_edit,
71 'find-copies-harder' => \
$_find_copies_harder,
73 'copy-similarity|C=i'=> \
$_cp_similarity
76 # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
77 my %EOL = ( CR
=> "\015", LF
=> "\012", CRLF
=> "\015\012", native
=> "\012" );
80 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
81 { 'revision|r=s' => \
$_revision, %fc_opts } ],
82 init
=> [ \
&init
, "Initialize a repo for tracking" .
83 " (requires URL argument)",
85 commit
=> [ \
&commit
, "Commit git revisions to SVN",
86 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
87 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings",
88 { 'revision|r=i' => \
$_revision } ],
89 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
90 { 'no-ignore-externals' => \
$_no_ignore_ext,
91 'copy-remote|remote=s' => \
$_cp_remote,
92 'upgrade' => \
$_upgrade } ],
93 'graft-branches' => [ \
&graft_branches
,
94 'Detect merges/branches from already imported history',
95 { 'merge-rx|m' => \
@_opt_m,
96 'branch|b=s' => \
@_branch_from,
97 'branch-all-refs|B' => \
$_branch_all_refs,
98 'no-default-regex' => \
$_no_default_regex,
99 'no-graft-copy' => \
$_no_graft_copy } ],
100 'multi-init' => [ \
&multi_init
,
101 'Initialize multiple trees (like git-svnimport)',
102 { %multi_opts, %fc_opts } ],
103 'multi-fetch' => [ \
&multi_fetch
,
104 'Fetch multiple trees (like git-svnimport)',
106 'log' => [ \
&show_log
, 'Show commit logs',
107 { 'limit=i' => \
$_limit,
108 'revision|r=s' => \
$_revision,
109 'verbose|v' => \
$_verbose,
110 'incremental' => \
$_incremental,
111 'oneline' => \
$_oneline,
112 'show-commit' => \
$_show_commit,
113 'authors-file|A=s' => \
$_authors,
115 'commit-diff' => [ \
&commit_diff
, 'Commit a diff between two trees',
116 { 'message|m=s' => \
$_message,
117 'file|F=s' => \
$_file,
122 for (my $i = 0; $i < @ARGV; $i++) {
123 if (defined $cmd{$ARGV[$i]}) {
130 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
132 read_repo_config
(\
%opts);
133 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
134 'version|V' => \
$_version,
135 'id|i=s' => \
$GIT_SVN);
136 exit 1 if (!$rv && $cmd ne 'log');
140 version
() if $_version;
141 usage
(1) unless defined $cmd;
143 load_authors
() if $_authors;
144 load_all_refs
() if $_branch_all_refs;
145 svn_compat_check
() unless $_use_lib;
146 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init)$/;
147 $cmd{$cmd}->[0]->(@ARGV);
150 ####################### primary functions ######################
152 my $exit = shift || 0;
153 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
155 git
-svn
- bidirectional operations between a single Subversion tree
and git
156 Usage
: $0 <command
> [options
] [arguments
]\n
158 print $fd "Available commands:\n" unless $cmd;
160 foreach (sort keys %cmd) {
161 next if $cmd && $cmd ne $_;
162 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
163 foreach (keys %{$cmd{$_}->[2]}) {
164 # prints out arguments as they should be passed:
165 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
166 print $fd ' ' x
17, join(', ', map { length $_ > 1 ?
168 split /\|/,$_)," $x\n";
172 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
173 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
174 one git repository and want to keep them separate. See git-svn(1) for more
181 print "git-svn version $VERSION\n";
186 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
189 $SVN_URL = shift or undef;
192 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
194 check_upgrade_needed();
197 my $pid = open(my $rev_list,'-|');
198 defined $pid or croak $!;
200 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
203 while (<$rev_list>) {
206 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
207 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
208 next if (!@commit); # skip merges
209 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
210 if (!$rev || !$uuid) {
211 croak "Unable to extract revision or UUID from ",
212 "$c, $commit[$#commit]\n";
215 # if we merged or otherwise started elsewhere, this is
216 # how we break out of it
217 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
218 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
220 unless (defined $latest) {
221 if (!$SVN_URL && !$url) {
222 croak "SVN repository location required: $url\n";
229 revdb_set($REVDB, $rev, $c);
230 print "r$rev = $c\n";
231 $newest_rev = $rev if ($rev > $newest_rev);
233 close $rev_list or croak $?;
235 goto out if $_use_lib;
236 if (!chdir $SVN_WC) {
237 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
238 chdir $SVN_WC or croak $!;
242 defined $pid or croak $!;
244 my @svn_up = qw(svn up);
245 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
246 sys
(@svn_up,"-r$newest_rev");
247 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
249 exec('git-write-tree') or croak
$!;
256 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
257 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
263 $SVN_URL = shift or die "SVN repository location required " .
264 "as a command-line argument\n";
265 $SVN_URL =~ s!/+$!!; # strip trailing slash
266 unless (-d
$GIT_DIR) {
267 my @init_db = ('git-init-db');
268 push @init_db, "--template=$_template" if defined $_template;
269 push @init_db, "--shared" if defined $_shared;
276 check_upgrade_needed
();
277 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
278 my $ret = $_use_lib ? fetch_lib
(@_) : fetch_cmd
(@_);
279 if ($ret->{commit
} && quiet_run
(qw(git-rev-parse --verify
280 refs/heads/master^0))) {
281 sys
(qw(git-update-ref refs/heads/master),$ret->{commit
});
288 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
289 unless ($_revision) {
290 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
292 push @log_args, "-r$_revision";
293 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
295 my $svn_log = svn_log_raw
(@log_args);
297 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
298 # don't need last_revision from grab_base_rev() because
299 # user could've specified a different revision to skip (they
300 # didn't want to import certain revisions into git for whatever
301 # reason, so trust $base->{revision} instead.
302 my (undef, $last_commit) = svn_grab_base_rev
();
303 unless (-d
$SVN_WC) {
304 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
305 chdir $SVN_WC or croak
$!;
307 $last_commit = git_commit
($base, @parents);
308 assert_tree
($last_commit);
310 chdir $SVN_WC or croak
$!;
312 # looks like a user manually cp'd and svn switch'ed
313 unless ($last_commit) {
314 sys
(qw
/svn revert -R ./);
315 assert_svn_wc_clean
($base->{revision
});
316 $last_commit = git_commit
($base, @parents);
317 assert_tree
($last_commit);
320 my @svn_up = qw(svn up);
321 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
323 while (my $log_msg = next_log_entry
($svn_log)) {
324 if ($last->{revision
} >= $log_msg->{revision
}) {
325 croak
"Out of order: last >= current: ",
326 "$last->{revision} >= $log_msg->{revision}\n";
328 # Revert is needed for cases like:
329 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
330 # I can't seem to reproduce something like that on a test...
331 sys
(qw
/svn revert -R ./);
332 assert_svn_wc_clean
($last->{revision
});
333 sys
(@svn_up,"-r$log_msg->{revision}");
334 $last_commit = git_commit
($log_msg, $last_commit, @parents);
337 close $svn_log->{fh
};
338 $last->{commit
} = $last_commit;
344 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
346 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
347 $SVN_LOG ||= libsvn_connect
($repo);
348 $SVN ||= libsvn_connect
($repo);
349 my ($last_rev, $last_commit) = svn_grab_base_rev
();
350 my ($base, $head) = libsvn_parse_revision
($last_rev);
352 return { revision
=> $last_rev, commit
=> $last_commit }
354 my $index = set_index
($GIT_SVN_INDEX);
356 # limit ourselves and also fork() since get_log won't release memory
357 # after processing a revision and SVN stuff seems to leak
359 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
361 if (defined $last_commit) {
362 unless (-e
$GIT_SVN_INDEX) {
363 sys
(qw
/git-read-tree/, $last_commit);
365 chomp (my $x = `git-write-tree`);
366 my ($y) = (`git-cat-file commit $last_commit`
367 =~ /^tree ($sha1)/m);
369 unlink $GIT_SVN_INDEX or croak
$!;
370 sys
(qw
/git-read-tree/, $last_commit);
372 chomp ($x = `git-write-tree`);
374 print STDERR
"trees ($last_commit) $y != $x\n",
375 "Something is seriously wrong...\n";
379 # fork, because using SVN::Pool with get_log() still doesn't
380 # seem to help enough to keep memory usage down.
381 defined(my $pid = fork) or croak
$!;
383 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
385 # Yes I'm perfectly aware that the fourth argument
386 # below is the limit revisions number. Unfortunately
387 # performance sucks with it enabled, so it's much
388 # faster to fetch revision ranges instead of relying
390 libsvn_get_log
($SVN_LOG, '/'.$SVN_PATH,
395 $log_msg = libsvn_fetch
(
397 $last_commit = git_commit
(
402 $log_msg = libsvn_new_tree
(@_);
403 $last_commit = git_commit
(
411 ($last_rev, $last_commit) = svn_grab_base_rev
();
412 last if ($max >= $head);
415 $max = $head if ($max > $head);
417 restore_index
($index);
418 return { revision
=> $last_rev, commit
=> $last_commit };
423 check_upgrade_needed
();
424 if ($_stdin || !@commits) {
425 print "Reading from stdin...\n";
428 if (/\b($sha1_short)\b/o) {
429 unshift @commits, $1;
434 foreach my $c (@commits) {
435 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
436 if (scalar @tmp == 1) {
438 } elsif (scalar @tmp > 1) {
439 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
441 die "Failed to rev-parse $c\n";
445 $_use_lib ? commit_lib
(@revs) : commit_cmd
(@revs);
446 print "Done committing ",scalar @revs," revisions to SVN\n";
452 chdir $SVN_WC or croak
"Unable to chdir $SVN_WC: $!\n";
453 my $info = svn_info
('.');
454 my $fetched = fetch
();
455 if ($info->{Revision
} != $fetched->{revision
}) {
456 print STDERR
"There are new revisions that were fetched ",
457 "and need to be merged (or acknowledged) ",
458 "before committing.\n";
461 $info = svn_info
('.');
464 foreach my $c (@revs) {
465 my $mods = svn_checkout_tree
($last, $c);
466 if (scalar @
$mods == 0) {
467 print "Skipping, no changes detected\n";
470 $last = svn_commit_tree
($last, $c);
476 my ($r_last, $cmt_last) = svn_grab_base_rev
();
477 defined $r_last or die "Must have an existing revision to commit\n";
478 my $fetched = fetch
();
479 if ($r_last != $fetched->{revision
}) {
480 print STDERR
"There are new revisions that were fetched ",
481 "and need to be merged (or acknowledged) ",
482 "before committing.\n",
483 "last rev: $r_last\n",
484 " current: $fetched->{revision}\n";
488 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
489 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
491 set_svn_commit_env
();
492 foreach my $c (@revs) {
493 my $log_msg = get_commit_message
($c, $commit_msg);
495 # fork for each commit because there's a memory leak I
496 # can't track down... (it's probably in the SVN code)
497 defined(my $pid = open my $fh, '-|') or croak
$!;
499 my $ed = SVN
::Git
::Editor
->new(
503 svn_path
=> $SVN_PATH
505 $SVN->get_commit_editor(
516 my $mods = libsvn_checkout_tree
($cmt_last, $c, $ed);
518 print "No changes\nr$r_last = $cmt_last\n";
525 my ($r_new, $cmt_new, $no);
529 if (/^r(\d+) = ($sha1)$/o) {
530 ($r_new, $cmt_new) = ($1, $2);
531 } elsif ($_ eq 'No changes') {
535 close $fh or croak
$?
;
536 if (! defined $r_new && ! defined $cmt_new) {
538 die "Failed to parse revision information\n";
541 ($r_last, $cmt_last) = ($r_new, $cmt_new);
549 $SVN_URL ||= file_to_s
("$GIT_SVN_DIR/info/url");
550 $_use_lib ? show_ignore_lib
() : show_ignore_cmd
();
553 sub show_ignore_cmd
{
554 require File
::Find
or die $!;
555 if (defined $_revision) {
556 die "-r/--revision option doesn't work unless the Perl SVN ",
557 "libraries are used\n";
559 chdir $SVN_WC or croak
$!;
561 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
563 @
{$ign{$_}} = svn_propget_base
('svn:ignore', $_);
564 }}, no_chdir
=>1},'.');
567 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
569 foreach my $i (sort keys %ign) {
570 print "\n# ",$i,"\n";
571 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
575 sub show_ignore_lib
{
577 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
578 $SVN ||= libsvn_connect
($repo);
579 my $r = defined $_revision ?
$_revision : $SVN->get_latest_revnum;
580 libsvn_traverse_ignore
(\
*STDOUT
, $SVN_PATH, $r);
584 my $gr_file = "$GIT_DIR/info/grafts";
585 my ($grafts, $comments) = read_grafts
($gr_file);
589 # temporarily disable our grafts file to make this idempotent
590 chomp($gr_sha1 = safe_qx
(qw
/git-hash-object -w/,$gr_file));
591 rename $gr_file, "$gr_file~$gr_sha1" or croak
$!;
594 my $l_map = read_url_paths
();
595 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
596 unless ($_no_default_regex) {
597 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
598 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
599 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
601 foreach my $u (keys %$l_map) {
603 foreach my $p (keys %{$l_map->{$u}}) {
604 graft_merge_msg
($grafts,$l_map,$u,$p,@re);
607 unless ($_no_graft_copy) {
609 graft_file_copy_lib
($grafts,$l_map,$u);
611 graft_file_copy_cmd
($grafts,$l_map,$u);
615 graft_tree_joins
($grafts);
617 write_grafts
($grafts, $comments, $gr_file);
618 unlink "$gr_file~$gr_sha1" if $gr_sha1;
625 $url =~ s
#/+$## if $url;
626 if ($_trunk !~ m
#^[a-z\+]+://#) {
627 $_trunk = '/' . $_trunk if ($_trunk !~ m
#^/#);
629 print STDERR
"E: '$_trunk' is not a complete URL ",
630 "and a separate URL is not specified\n";
633 $_trunk = $url . $_trunk;
635 if ($GIT_SVN eq 'git-svn') {
636 print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
637 $GIT_SVN = $ENV{GIT_SVN_ID
} = 'trunk';
641 complete_url_ls_init
($url, $_branches, '--branches/-b', '');
642 complete_url_ls_init
($url, $_tags, '--tags/-t', 'tags/');
646 # try to do trunk first, since branches/tags
647 # may be descended from it.
648 if (-e
"$GIT_DIR/svn/trunk/info/url") {
649 fetch_child_id
('trunk', @_);
651 rec_fetch
('', "$GIT_DIR/svn", @_);
657 my $r_last = -1; # prevent dupes
658 rload_authors
() if $_authors;
664 if (defined $_revision) {
665 if ($_revision =~ /^(\d+):(\d+)$/) {
666 ($r_min, $r_max) = ($1, $2);
667 } elsif ($_revision =~ /^\d+$/) {
668 $r_min = $r_max = $_revision;
670 print STDERR
"-r$_revision is not supported, use ",
671 "standard \'git log\' arguments instead\n";
676 my $pid = open(my $log,'-|');
677 defined $pid or croak
$!;
679 exec(git_svn_log_cmd
($r_min,$r_max), @args) or croak
$!;
685 if (/^commit ($sha1_short)/o) {
687 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
689 process_commit
($c, $r_min, $r_max, \
@k) or
694 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
695 get_author_info
($c, $1, $2, $3);
696 } elsif (/^(?:tree|parent|committer) /) {
698 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
699 push @
{$c->{raw
}}, $_;
702 push @
{$c->{diff
}}, $_;
704 push @
{$c->{diff
}}, $_;
705 } elsif (/^ (git-svn-id:.+)$/) {
706 (undef, $c->{r
}, undef) = extract_metadata
($1);
711 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
713 process_commit
($c, $r_min, $r_max, \
@k);
719 process_commit
($_, $r_min, $r_max) foreach reverse @k;
723 print '-' x72
,"\n" unless $_incremental || $_oneline;
726 sub commit_diff_usage
{
727 print STDERR
"Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
733 print STDERR
"commit-diff must be used with SVN libraries\n";
736 my $ta = shift or commit_diff_usage
();
737 my $tb = shift or commit_diff_usage
();
738 if (!eval { $SVN_URL = shift || file_to_s
("$GIT_SVN_DIR/info/url") }) {
739 print STDERR
"Needed URL or usable git-svn id command-line\n";
742 if (defined $_message && defined $_file) {
743 print STDERR
"Both --message/-m and --file/-F specified ",
744 "for the commit message.\n",
745 "I have no idea what you mean\n";
748 if (defined $_file) {
749 $_message = file_to_s
($_message);
751 $_message ||= get_commit_message
($tb,
752 "$GIT_DIR/.svn-commit.tmp.$$")->{msg
};
755 ($repo, $SVN_PATH) = repo_path_split
($SVN_URL);
756 $SVN_LOG ||= libsvn_connect
($repo);
757 $SVN ||= libsvn_connect
($repo);
758 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
759 my $ed = SVN
::Git
::Editor
->new({ r
=> $SVN->get_latest_revnum,
760 ra
=> $SVN, c
=> $tb,
761 svn_path
=> $SVN_PATH
763 $SVN->get_commit_editor($_message,
764 sub {print "Committed $_[0]\n"},@lock)
766 my $mods = libsvn_checkout_tree
($ta, $tb, $ed);
768 print "No changes\n$ta == $tb\n";
775 ########################### utility functions #########################
779 return 1 if defined $c->{r
};
780 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
781 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
782 my @msg = safe_qx
(qw
/git-cat-file commit/, $c->{c
});
783 shift @msg while ($msg[0] ne "\n");
785 @
{$c->{l
}} = grep !/^git-svn-id: /, @msg;
787 (undef, $c->{r
}, undef) = extract_metadata
(
788 (grep(/^git-svn-id: /, @msg))[-1]);
790 return defined $c->{r
};
793 sub git_svn_log_cmd
{
794 my ($r_min, $r_max) = @_;
795 my @cmd = (qw
/git
-log --abbrev
-commit
--pretty
=raw
796 --default/, "refs/remotes
/$GIT_SVN");
797 push @cmd, '--summary' if $_verbose;
798 return @cmd unless defined $r_max;
799 if ($r_max == $r_min) {
800 push @cmd, '--max-count=1';
801 if (my $c = revdb_get($REVDB, $r_max)) {
806 $c_max = revdb_get($REVDB, $r_max);
807 $c_min = revdb_get($REVDB, $r_min);
808 if ($c_min && $c_max) {
809 if ($r_max > $r_max) {
810 push @cmd, "$c_min..$c_max";
812 push @cmd, "$c_max..$c_min";
814 } elsif ($r_max > $r_min) {
825 print "Fetching
$id\n";
826 my $ref = "$GIT_DIR/refs/remotes
/$id";
827 my $ca = file_to_s($ref) if (-r $ref);
828 defined(my $pid = fork) or croak $!;
830 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
837 return unless $_repack || -r $ref;
839 my $cb = file_to_s($ref);
841 defined($pid = open my $fh, '-|') or croak $!;
842 my $url = file_to_s("$GIT_DIR/svn/$id/info/url
");
845 exec qw/git-rev-list --pretty=raw/,
846 $ca ? "$ca..$cb" : $cb or croak $!;
849 if (/^ git-svn-id: $url\@\d+ [a-f0-9\-]+$/) {
851 } elsif (/^ git-svn-id: \S+\@\d+ [a-f0-9\-]+$/) {
859 my ($pfx, $p, @args) = @_;
861 foreach (sort <$p/*>) {
862 if (-r "$_/info/url
") {
863 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
864 my $id = $pfx . basename $_;
865 next if $id eq 'trunk';
866 fetch_child_id($id, @args);
873 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
878 sub complete_url_ls_init {
879 my ($url, $var, $switch, $pfx) = @_;
881 print STDERR "W
: $switch not specified
\n";
885 if ($var !~ m#^[a-z\+]+://#) {
886 $var = '/' . $var if ($var !~ m#^/#);
888 print STDERR "E
: '$var' is
not a complete URL
",
889 "and a separate URL is
not specified
\n";
894 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
895 : safe_qx(qw/svn ls --non-interactive/, $var));
897 defined(my $pid = fork) or croak $!;
899 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
901 if ($u !~ m!\Q$var\E/(.+)$!) {
902 print STDERR
"W: Unrecognized URL: $u\n";
903 die "This should never happen\n";
906 print "init $u => $id\n";
907 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
921 my @tmp = split m
#/#, $_;
923 while (my $x = shift @tmp) {
929 foreach (sort {length $b <=> length $a} keys %common) {
930 if ($common{$_} == @
$paths) {
937 # grafts set here are 'stronger' in that they're based on actual tree
938 # matches, and won't be deleted from merge-base checking in write_grafts()
939 sub graft_tree_joins
{
941 map_tree_joins
() if (@_branch_from && !%tree_map);
942 return unless %tree_map;
946 defined(my $pid = open my $fh, '-|') or croak
$!;
948 exec qw
/git-rev-list --pretty=raw/,
949 "refs/remotes/$i" or croak
$!;
952 next unless /^commit ($sha1)$/o;
954 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
955 next unless $tree_map{$t};
960 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
962 my ($s, $tz) = ($1, $2);
963 if ($tz =~ s/^\+//) {
964 $s += tz_to_s_offset
($tz);
965 } elsif ($tz =~ s/^\-//) {
966 $s -= tz_to_s_offset
($tz);
969 my ($url_a, $r_a, $uuid_a) = cmt_metadata
($c);
971 foreach my $p (@
{$tree_map{$t}}) {
974 safe_qx
('git-merge-base', $c, $p)
976 next unless ($@
|| $?
);
978 # see if SVN says it's a relative
979 my ($url_b, $r_b, $uuid_b) =
981 next if (defined $url_b &&
983 ($url_a eq $url_b) &&
984 ($uuid_a eq $uuid_b));
985 if ($uuid_a eq $uuid_b) {
987 $grafts->{$c}->{$p} = 2;
989 } elsif ($r_b > $r_a) {
990 $grafts->{$p}->{$c} = 2;
995 my $ct = get_commit_time
($p);
997 $grafts->{$c}->{$p} = 2;
999 $grafts->{$p}->{$c} = 2;
1001 # what should we do when $ct == $s ?
1004 close $fh or croak
$?
;
1008 # this isn't funky-filename safe, but good enough for now...
1009 sub graft_file_copy_cmd
{
1010 my ($grafts, $l_map, $u) = @_;
1011 my $paths = $l_map->{$u};
1012 my $pfx = common_prefix
([keys %$paths]);
1013 $SVN_URL ||= $u.$pfx;
1014 my $pid = open my $fh, '-|';
1015 defined $pid or croak
$!;
1017 my @exec = qw
/svn log -v/;
1018 push @exec, "-r$_revision" if defined $_revision;
1019 exec @exec, $u.$pfx or croak
$!;
1021 my ($r, $mp) = (undef, undef);
1026 } elsif (/^r(\d+) \| /) {
1027 $r = $1 unless defined $r;
1028 } elsif (/^Changed paths:/) {
1030 } elsif ($mp && m
#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1031 my ($p1, $p0, $r0) = ($1, $2, $3);
1032 my $c = find_graft_path_commit
($paths, $p1, $r);
1034 find_graft_path_parents
($grafts, $paths, $c, $p0, $r0);
1039 sub graft_file_copy_lib
{
1040 my ($grafts, $l_map, $u) = @_;
1041 my $tree_paths = $l_map->{$u};
1042 my $pfx = common_prefix
([keys %$tree_paths]);
1043 my ($repo, $path) = repo_path_split
($u.$pfx);
1044 $SVN_LOG ||= libsvn_connect
($repo);
1045 $SVN ||= libsvn_connect
($repo);
1047 my ($base, $head) = libsvn_parse_revision
();
1049 my ($min, $max) = ($base, $head < $base+$inc ?
$head : $base+$inc);
1050 my $eh = $SVN::Error
::handler
;
1051 $SVN::Error
::handler
= \
&libsvn_skip_unknown_revs
;
1053 my $pool = SVN
::Pool
->new;
1054 libsvn_get_log
($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1056 libsvn_graft_file_copies
($grafts, $tree_paths,
1060 last if ($max >= $head);
1063 $max = $head if ($max > $head);
1065 $SVN::Error
::handler
= $eh;
1068 sub process_merge_msg_matches
{
1069 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1070 my (@strong, @weak);
1071 foreach (@matches) {
1072 # merging with ourselves is not interesting
1074 if ($l_map->{$u}->{$_}) {
1080 foreach my $w (@weak) {
1082 # no exact match, use branch name as regexp.
1083 my $re = qr/\Q$w\E/i;
1084 foreach (keys %{$l_map->{$u}}) {
1086 push @strong, $l_map->{$u}->{$_};
1093 foreach (keys %{$l_map->{$u}}) {
1095 push @strong, $l_map->{$u}->{$_};
1100 my ($rev) = ($c->{m
} =~ /^git
-svn
-id
:\s
(?
:\S
+?
)\@
(\d
+)
1101 \s
(?
:[a
-f\d\
-]+)$/xsm
);
1102 unless (defined $rev) {
1103 ($rev) = ($c->{m
} =~/^git
-svn
-id
:\s
(\d
+)
1104 \@
(?
:[a
-f\d\
-]+)/xsm
);
1105 return unless defined $rev;
1107 foreach my $m (@strong) {
1108 my ($r0, $s0) = find_rev_before
($rev, $m, 1);
1109 $grafts->{$c->{c
}}->{$s0} = 1 if defined $s0;
1113 sub graft_merge_msg
{
1114 my ($grafts, $l_map, $u, $p, @re) = @_;
1116 my $x = $l_map->{$u}->{$p};
1117 my $rl = rev_list_raw
($x);
1118 while (my $c = next_rev_list_entry
($rl)) {
1119 foreach my $re (@re) {
1120 my (@br) = ($c->{m
} =~ /$re/g);
1122 process_merge_msg_matches
($grafts,$l_map,$u,$p,$c,@br);
1128 return if $SVN_UUID;
1130 my $pool = SVN
::Pool
->new;
1131 $SVN_UUID = $SVN->get_uuid($pool);
1134 my $info = shift || svn_info
('.');
1135 $SVN_UUID = $info->{'Repository UUID'} or
1136 croak
"Repository UUID unreadable\n";
1142 defined $pid or croak
$!;
1144 open my $null, '>', '/dev/null' or croak
$!;
1145 open STDERR
, '>&', $null or croak
$!;
1146 open STDOUT
, '>&', $null or croak
$!;
1147 exec @_ or croak
$!;
1153 sub repo_path_split
{
1154 my $full_url = shift;
1155 $full_url =~ s
#/+$##;
1157 foreach (@repo_path_split_cache) {
1158 if ($full_url =~ s
#$_##) {
1160 $full_url =~ s
#^/+##;
1161 return ($u, $full_url);
1165 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1167 my @paths = split(m
#/+#, $path);
1171 $SVN = libsvn_connect
($url);
1172 last if (defined $SVN &&
1173 defined eval { $SVN->get_latest_revnum });
1174 my $n = shift @paths || last;
1178 while (quiet_run
(qw
/svn ls --non-interactive/, $url)) {
1179 my $n = shift @paths || last;
1183 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1184 $path = join('/',@paths);
1185 return ($url, $path);
1189 defined $SVN_URL or croak
"SVN repository location required\n";
1190 unless (-d
$GIT_DIR) {
1191 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
1193 mkpath
([$GIT_SVN_DIR]);
1194 mkpath
(["$GIT_SVN_DIR/info"]);
1195 open my $fh, '>>',$REVDB or croak
$!;
1197 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
1201 sub assert_svn_wc_clean
{
1202 return if $_use_lib;
1204 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1205 my $lcr = svn_info
('.')->{'Last Changed Rev'};
1206 if ($svn_rev != $lcr) {
1207 print STDERR
"Checking for copy-tree ... ";
1208 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
1209 "-r$lcr:$svn_rev")));
1211 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
1213 print STDERR
"OK!\n";
1216 my @status = grep(!/^Performing status on external/,(`svn status`));
1217 @status = grep(!/^\s*$/,@status);
1218 if (scalar @status) {
1219 print STDERR
"Tree ($SVN_WC) is not clean:\n";
1220 print STDERR
$_ foreach @status;
1225 sub get_tree_from_treeish
{
1227 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1228 chomp(my $type = `git-cat-file -t $treeish`);
1230 while ($type eq 'tag') {
1231 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1233 if ($type eq 'commit') {
1234 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1235 ($expected) = ($expected =~ /^tree ($sha1)$/);
1236 die "Unable to get tree from $treeish\n" unless $expected;
1237 } elsif ($type eq 'tree') {
1238 $expected = $treeish;
1240 die "$treeish is a $type, expected tree, tag or commit\n";
1246 return if $_use_lib;
1248 my $expected = get_tree_from_treeish
($treeish);
1250 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1252 unlink $tmpindex or croak
$!;
1254 my $old_index = set_index
($tmpindex);
1256 chomp(my $tree = `git-write-tree`);
1257 restore_index
($old_index);
1258 if ($tree ne $expected) {
1259 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
1264 sub parse_diff_tree
{
1265 my $diff_fh = shift;
1269 while (<$diff_fh>) {
1270 chomp $_; # this gets rid of the trailing "\0"
1271 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
1272 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
1273 push @mods, { mode_a
=> $1, mode_b
=> $2,
1274 sha1_b
=> $3, chg
=> $4 };
1275 if ($4 =~ /^(?:C|R)$/) {
1280 } elsif ($state eq 'file_a') {
1281 my $x = $mods[$#mods] or croak
"Empty array\n";
1282 if ($x->{chg
} !~ /^(?:C|R)$/) {
1283 croak
"Error parsing $_, $x->{chg}\n";
1287 } elsif ($state eq 'file_b') {
1288 my $x = $mods[$#mods] or croak
"Empty array\n";
1289 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
1290 croak
"Error parsing $_, $x->{chg}\n";
1292 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
1293 croak
"Error parsing $_, $x->{chg}\n";
1298 croak
"Error parsing $_\n";
1301 close $diff_fh or croak
$?
;
1306 sub svn_check_prop_executable
{
1308 return if -l
$m->{file_b
};
1309 if ($m->{mode_b
} =~ /755$/) {
1310 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
1311 if ($m->{mode_a
} !~ /755$/) {
1312 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
1314 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
1315 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1316 sys
(qw(svn propdel svn:executable), $m->{file_b
});
1317 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
1318 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
1322 sub svn_ensure_parent_path
{
1323 my $dir_b = dirname
(shift);
1324 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
1325 mkpath
([$dir_b]) unless (-d
$dir_b);
1326 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
1329 sub precommit_check
{
1331 my (%rm_file, %rmdir_check, %added_check);
1333 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
1334 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1335 if ($m->{chg
} eq 'R') {
1336 if (-d
$m->{file_b
}) {
1337 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1339 # dir/$file => dir/file/$file
1340 my $dirname = dirname
($m->{file_b
});
1341 while ($dirname ne File
::Spec
->curdir) {
1342 if ($dirname ne $m->{file_a
}) {
1343 $dirname = dirname
($dirname);
1346 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
1348 # baz/zzz => baz (baz is a file)
1349 $dirname = dirname
($m->{file_a
});
1350 while ($dirname ne File
::Spec
->curdir) {
1351 if ($dirname ne $m->{file_b
}) {
1352 $dirname = dirname
($dirname);
1355 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
1358 if ($m->{chg
} =~ /^(D|R)$/) {
1359 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
1360 $rm_file{ $m->{$t} } = 1;
1361 my $dirname = dirname
( $m->{$t} );
1362 my $basename = basename
( $m->{$t} );
1363 $rmdir_check{$dirname}->{$basename} = 1;
1364 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
1365 if (-d
$m->{file_b
}) {
1366 err_dir_to_file
($m->{file_b
});
1368 my $dirname = dirname
( $m->{file_b
} );
1369 my $basename = basename
( $m->{file_b
} );
1370 $added_check{$dirname}->{$basename} = 1;
1371 while ($dirname ne File
::Spec
->curdir) {
1372 if ($rm_file{$dirname}) {
1373 err_file_to_dir
($m->{file_b
});
1375 $dirname = dirname
$dirname;
1379 return (\
%rmdir_check, \
%added_check);
1381 sub err_dir_to_file
{
1383 print STDERR
"Node change from directory to file ",
1384 "is not supported by Subversion: ",$file,"\n";
1387 sub err_file_to_dir
{
1389 print STDERR
"Node change from file to directory ",
1390 "is not supported by Subversion: ",$file,"\n";
1397 my ($from, $treeish) = @_;
1399 print "diff-tree $from $treeish\n";
1400 my $pid = open my $diff_fh, '-|';
1401 defined $pid or croak
$!;
1403 my @diff_tree = qw(git-diff-tree -z -r);
1404 if ($_cp_similarity) {
1405 push @diff_tree, "-C$_cp_similarity";
1407 push @diff_tree, '-C';
1409 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1410 push @diff_tree, "-l$_l" if defined $_l;
1411 exec(@diff_tree, $from, $treeish) or croak
$!;
1413 return parse_diff_tree
($diff_fh);
1416 sub svn_checkout_tree
{
1417 my ($from, $treeish) = @_;
1418 my $mods = get_diff
($from->{commit
}, $treeish);
1419 return $mods unless (scalar @
$mods);
1420 my ($rm, $add) = precommit_check
($mods);
1422 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1423 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1424 if ($m->{chg
} eq 'C') {
1425 svn_ensure_parent_path
( $m->{file_b
} );
1426 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
1427 apply_mod_line_blob
($m);
1428 svn_check_prop_executable
($m);
1429 } elsif ($m->{chg
} eq 'D') {
1430 sys
(qw(svn rm --force), $m->{file_b
});
1431 } elsif ($m->{chg
} eq 'R') {
1432 svn_ensure_parent_path
( $m->{file_b
} );
1433 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
1434 apply_mod_line_blob
($m);
1435 svn_check_prop_executable
($m);
1436 } elsif ($m->{chg
} eq 'M') {
1437 apply_mod_line_blob
($m);
1438 svn_check_prop_executable
($m);
1439 } elsif ($m->{chg
} eq 'T') {
1440 sys
(qw(svn rm --force),$m->{file_b
});
1441 apply_mod_line_blob
($m);
1442 sys
(qw(svn add), $m->{file_b
});
1443 svn_check_prop_executable
($m);
1444 } elsif ($m->{chg
} eq 'A') {
1445 svn_ensure_parent_path
( $m->{file_b
} );
1446 apply_mod_line_blob
($m);
1447 sys
(qw(svn add), $m->{file_b
});
1448 svn_check_prop_executable
($m);
1450 croak
"Invalid chg: $m->{chg}\n";
1454 assert_tree
($treeish);
1455 if ($_rmdir) { # remove empty directories
1456 handle_rmdir
($rm, $add);
1458 assert_tree
($treeish);
1462 sub libsvn_checkout_tree
{
1463 my ($from, $treeish, $ed) = @_;
1464 my $mods = get_diff
($from, $treeish);
1465 return $mods unless (scalar @
$mods);
1466 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
1467 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
1469 if (defined $o{$f}) {
1472 croak
"Invalid change type: $f\n";
1475 $ed->rmdirs if $_rmdir;
1479 # svn ls doesn't work with respect to the current working tree, but what's
1480 # in the repository. There's not even an option for it... *sigh*
1481 # (added files don't show up and removed files remain in the ls listing)
1482 sub svn_ls_current
{
1483 my ($dir, $rm, $add) = @_;
1484 chomp(my @ls = safe_qx
('svn','ls',$dir));
1487 s
#/$##; # trailing slashes are evil
1488 push @ret, $_ unless $rm->{$dir}->{$_};
1490 if (exists $add->{$dir}) {
1491 push @ret, keys %{$add->{$dir}};
1497 my ($rm, $add) = @_;
1499 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1500 my $ls = svn_ls_current
($dir, $rm, $add);
1501 next if (scalar @
$ls);
1502 sys
(qw(svn rm --force),$dir);
1504 my $dn = dirname
$dir;
1505 $rm->{ $dn }->{ basename
$dir } = 1;
1506 $ls = svn_ls_current
($dn, $rm, $add);
1507 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
1508 sys
(qw(svn rm --force),$dn);
1509 $dir = basename
$dn;
1511 $rm->{ $dn }->{ $dir } = 1;
1512 $ls = svn_ls_current
($dn, $rm, $add);
1517 sub get_commit_message
{
1518 my ($commit, $commit_msg) = (@_);
1519 my %log_msg = ( msg
=> '' );
1520 open my $msg, '>', $commit_msg or croak
$!;
1522 chomp(my $type = `git-cat-file -t $commit`);
1523 if ($type eq 'commit') {
1524 my $pid = open my $msg_fh, '-|';
1525 defined $pid or croak
$!;
1528 exec(qw(git-cat-file commit), $commit) or croak
$!;
1533 $in_msg = 1 if (/^\s*$/);
1534 } elsif (/^git-svn-id: /) {
1535 # skip this, we regenerate the correct one
1536 # on re-fetch anyways
1538 print $msg $_ or croak
$!;
1541 close $msg_fh or croak
$?
;
1543 close $msg or croak
$!;
1545 if ($_edit || ($type eq 'tree')) {
1546 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
1547 system($editor, $commit_msg);
1550 # file_to_s removes all trailing newlines, so just use chomp() here:
1551 open $msg, '<', $commit_msg or croak
$!;
1552 { local $/; chomp($log_msg{msg
} = <$msg>); }
1553 close $msg or croak
$!;
1558 sub set_svn_commit_env
{
1559 if (defined $LC_ALL) {
1560 $ENV{LC_ALL
} = $LC_ALL;
1562 delete $ENV{LC_ALL
};
1566 sub svn_commit_tree
{
1567 my ($last, $commit) = @_;
1568 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1569 my $log_msg = get_commit_message
($commit, $commit_msg);
1570 my ($oneline) = ($log_msg->{msg
} =~ /([^\n\r]+)/);
1571 print "Committing $commit: $oneline\n";
1573 set_svn_commit_env
();
1574 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
1577 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1578 if (!defined $committed) {
1579 my $out = join("\n",@ci_output);
1580 print STDERR
"W: Trouble parsing \`svn commit' output:\n\n",
1581 $out, "\n\nAssuming English locale...";
1582 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1583 defined $committed or die " FAILED!\n",
1584 "Commit output failed to parse committed revision!\n",
1585 print STDERR
" OK\n";
1588 my @svn_up = qw(svn up);
1589 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1590 if ($_optimize_commits && ($committed == ($last->{revision
} + 1))) {
1591 push @svn_up, "-r$committed";
1593 my $info = svn_info
('.');
1594 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1595 if ($info->{'Last Changed Rev'} != $committed) {
1596 croak
"$info->{'Last Changed Rev'} != $committed\n"
1598 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1599 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1600 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1601 or croak
"Failed to parse date: $date\n";
1602 $log_msg->{date
} = "$tz $Y-$m-$d $H:$M:$S";
1603 $log_msg->{author
} = $info->{'Last Changed Author'};
1604 $log_msg->{revision
} = $committed;
1605 $log_msg->{msg
} .= "\n";
1606 $log_msg->{parents
} = [ $last->{commit
} ];
1607 $log_msg->{commit
} = git_commit
($log_msg, $commit);
1610 # resync immediately
1611 push @svn_up, "-r$last->{revision}";
1613 return fetch
("$committed=$commit");
1618 my $pid = open my $fh, '-|';
1619 defined $pid or croak
$!;
1621 exec(qw
/git-rev-list --pretty=raw/, @args) or croak
$!;
1623 return { fh
=> $fh, t
=> { } };
1626 sub next_rev_list_entry
{
1631 if (/^commit ($sha1)$/o) {
1633 $rl->{t
} = { c
=> $1 };
1638 } elsif (/^parent ($sha1)$/o) {
1645 return ($x != $rl->{t
}) ?
$x : undef;
1648 # read the entire log into a temporary file (which is removed ASAP)
1649 # and store the file handle + parser state
1651 my (@log_args) = @_;
1652 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
1654 defined $pid or croak
$!;
1656 open STDOUT
, '>&', $log_fh or croak
$!;
1657 exec (qw(svn log), @log_args) or croak
$!
1661 seek $log_fh, 0, 0 or croak
$!;
1662 return { state => 'sep', fh
=> $log_fh };
1665 sub next_log_entry
{
1666 my $log = shift; # retval of svn_log_raw()
1668 my $fh = $log->{fh
};
1673 if ($log->{state} eq 'msg') {
1674 if ($ret->{lines
}) {
1675 $ret->{msg
} .= $_."\n";
1676 unless(--$ret->{lines
}) {
1677 $log->{state} = 'sep';
1680 croak
"Log parse error at: $_\n",
1686 if ($log->{state} ne 'sep') {
1687 croak
"Log parse error at: $_\n",
1688 "state: $log->{state}\n",
1692 $log->{state} = 'rev';
1694 # if we have an empty log message, put something there:
1696 $ret->{msg
} ||= "\n";
1697 delete $ret->{lines
};
1702 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1704 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1705 ($lines) = ($lines =~ /(\d+)/);
1706 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1707 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
1708 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
1709 or croak
"Failed to parse date: $date\n";
1710 $ret = { revision
=> $rev,
1711 date
=> "$tz $Y-$m-$d $H:$M:$S",
1715 if (defined $_authors && ! defined $users{$author}) {
1716 die "Author: $author not defined in ",
1719 $log->{state} = 'msg_start';
1722 # skip the first blank line of the message:
1723 if ($log->{state} eq 'msg_start' && /^$/) {
1724 $log->{state} = 'msg';
1725 } elsif ($log->{state} eq 'msg') {
1726 if ($ret->{lines
}) {
1727 $ret->{msg
} .= $_."\n";
1728 unless (--$ret->{lines
}) {
1729 $log->{state} = 'sep';
1732 croak
"Log parse error at: $_\n",
1733 $ret->{revision
},"\n";
1741 my $url = shift || $SVN_URL;
1743 my $pid = open my $info_fh, '-|';
1744 defined $pid or croak
$!;
1747 exec(qw(svn info),$url) or croak
$!;
1751 # only single-lines seem to exist in svn info output
1752 while (<$info_fh>) {
1754 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
1756 push @
{$ret->{-order
}}, $1;
1759 close $info_fh or croak
$?
;
1763 sub sys
{ system(@_) == 0 or croak
$?
}
1766 my ($from, $to) = @_;
1767 my $es = svn_propget_base
('svn:eol-style', $to);
1768 open my $rfd, '<', $from or croak
$!;
1769 binmode $rfd or croak
$!;
1770 open my $wfd, '>', $to or croak
$!;
1771 binmode $wfd or croak
$!;
1772 eol_cp_fd
($rfd, $wfd, $es);
1773 close $rfd or croak
$!;
1774 close $wfd or croak
$!;
1778 my ($rfd, $wfd, $es) = @_;
1779 my $eol = defined $es ?
$EOL{$es} : undef;
1784 defined($r = sysread($rfd, $buf, 4096)) or croak
$!;
1787 if ($buf =~ /\015$/) {
1789 defined($r = sysread($rfd,$c,1)) or croak
$!;
1790 $buf .= $c if $r > 0;
1792 $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
1795 for ($w = 0; $w < $r; $w += $t) {
1796 $t = syswrite($wfd, $buf, $r - $w, $w) or croak
$!;
1802 sub do_update_index
{
1803 my ($z_cmd, $cmd, $no_text_base) = @_;
1805 my $z = open my $p, '-|';
1806 defined $z or croak
$!;
1807 unless ($z) { exec @
$z_cmd or croak
$! }
1809 my $pid = open my $ui, '|-';
1810 defined $pid or croak
$!;
1812 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
1815 while (my $x = <$p>) {
1817 if (!$no_text_base && lstat $x && ! -l _
&&
1818 svn_propget_base
('svn:keywords', $x)) {
1819 my $mode = -x _ ?
0755 : 0644;
1820 my ($v,$d,$f) = File
::Spec
->splitpath($x);
1821 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
1822 'text-base',"$f.svn-base");
1825 $tb = File
::Spec
->catfile($d, '.svn',
1826 'text-base',"$f.svn-base");
1829 unlink $x or croak
$!;
1831 chmod(($mode &~ umask), $x) or croak
$!;
1835 close $ui or croak
$?
;
1839 return if $_use_lib;
1841 if (!-f
"$GIT_SVN_DIR/info/exclude") {
1842 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak
$!;
1843 print $fd '.svn',"\n";
1844 close $fd or croak
$!;
1846 my $no_text_base = shift;
1847 do_update_index
([qw
/git-diff-files --name-only -z/],
1850 do_update_index
([qw
/git-ls-files -z --others/,
1851 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1857 my ($str, $file, $mode) = @_;
1858 open my $fd,'>',$file or croak
$!;
1859 print $fd $str,"\n" or croak
$!;
1860 close $fd or croak
$!;
1861 chmod ($mode &~ umask, $file) if (defined $mode);
1866 open my $fd,'<',$file or croak
"$!: file: $file\n";
1869 close $fd or croak
$!;
1874 sub assert_revision_unknown
{
1876 if (my $c = revdb_get
($REVDB, $r)) {
1877 croak
"$r = $c already exists! Why are we refetching it?";
1883 my @x = safe_qx
('git-cat-file','commit',$x);
1884 my @y = safe_qx
('git-cat-file','commit',$y);
1885 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1886 || $y[0] !~ /^tree $sha1\n$/) {
1887 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
1894 my ($log_msg, @parents) = @_;
1895 assert_revision_unknown
($log_msg->{revision
});
1896 map_tree_joins
() if (@_branch_from && !%tree_map);
1898 my (@tmp_parents, @exec_parents, %seen_parent);
1899 if (my $lparents = $log_msg->{parents
}) {
1900 @tmp_parents = @
$lparents
1902 # commit parents can be conditionally bound to a particular
1903 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1904 foreach my $p (@parents) {
1905 next unless defined $p;
1906 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1907 if ($1 == $log_msg->{revision
}) {
1908 push @tmp_parents, $2;
1911 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1914 my $tree = $log_msg->{tree
};
1915 if (!defined $tree) {
1916 my $index = set_index
($GIT_SVN_INDEX);
1918 chomp($tree = `git-write-tree`);
1920 restore_index
($index);
1922 if (exists $tree_map{$tree}) {
1923 foreach my $p (@
{$tree_map{$tree}}) {
1925 foreach (@tmp_parents) {
1926 # see if a common parent is found
1928 safe_qx
('git-merge-base', $_, $p)
1935 my ($url_p, $r_p, $uuid_p) = cmt_metadata
($p);
1936 next if (($SVN_UUID eq $uuid_p) &&
1937 ($log_msg->{revision
} > $r_p));
1938 next if (defined $url_p && defined $SVN_URL &&
1939 ($SVN_UUID eq $uuid_p) &&
1940 ($url_p eq $SVN_URL));
1941 push @tmp_parents, $p;
1944 foreach (@tmp_parents) {
1945 next if $seen_parent{$_};
1946 $seen_parent{$_} = 1;
1947 push @exec_parents, $_;
1948 # MAXPARENT is defined to 16 in commit-tree.c:
1949 last if @exec_parents > 16;
1952 defined(my $pid = open my $out_fh, '-|') or croak
$!;
1954 my $msg_fh = IO
::File
->new_tmpfile or croak
$!;
1955 print $msg_fh $log_msg->{msg
}, "\ngit-svn-id: ",
1956 "$SVN_URL\@$log_msg->{revision}",
1957 " $SVN_UUID\n" or croak
$!;
1958 $msg_fh->flush == 0 or croak
$!;
1959 seek $msg_fh, 0, 0 or croak
$!;
1960 set_commit_env
($log_msg);
1961 my @exec = ('git-commit-tree',$tree);
1962 push @exec, '-p', $_ foreach @exec_parents;
1963 open STDIN
, '<&', $msg_fh or croak
$!;
1964 exec @exec or croak
$!;
1966 chomp(my $commit = do { local $/; <$out_fh> });
1967 close $out_fh or croak
$?
;
1968 if ($commit !~ /^$sha1$/o) {
1969 croak
"Failed to commit, invalid sha1: $commit\n";
1971 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1972 if (my $primary_parent = shift @exec_parents) {
1973 quiet_run
(qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0");
1974 push @update_ref, $primary_parent unless $?
;
1977 revdb_set
($REVDB, $log_msg->{revision
}, $commit);
1979 # this output is read via pipe, do not change:
1980 print "r$log_msg->{revision} = $commit\n";
1986 if ($_repack && (--$_repack_nr == 0)) {
1987 $_repack_nr = $_repack;
1988 sys
("git repack $_repack_flags");
1992 sub set_commit_env
{
1994 my $author = $log_msg->{author
};
1995 if (!defined $author || length $author == 0) {
1996 $author = '(no author)';
1998 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
1999 : ($author,"$author\@$SVN_UUID");
2000 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
2001 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
2002 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
2005 sub apply_mod_line_blob
{
2007 if ($m->{mode_b
} =~ /^120/) {
2008 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
2010 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
2014 sub blob_to_symlink
{
2015 my ($blob, $link) = @_;
2016 defined $link or croak
"\$link not defined!\n";
2017 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2018 if (-l
$link || -f _
) {
2019 unlink $link or croak
$!;
2022 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2023 symlink $dest, $link or croak
$!;
2027 my ($blob, $file) = @_;
2028 defined $file or croak
"\$file not defined!\n";
2029 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2030 if (-l
$file || -f _
) {
2031 unlink $file or croak
$!;
2034 open my $blob_fh, '>', $file or croak
"$!: $file\n";
2036 defined $pid or croak
$!;
2039 open STDOUT
, '>&', $blob_fh or croak
$!;
2040 exec('git-cat-file','blob',$blob) or croak
$!;
2045 close $blob_fh or croak
$!;
2049 my $pid = open my $child, '-|';
2050 defined $pid or croak
$!;
2052 exec(@_) or croak
$!;
2054 my @ret = (<$child>);
2055 close $child or croak
$?
;
2056 die $?
if $?
; # just in case close didn't error out
2057 return wantarray ?
@ret : join('',@ret);
2060 sub svn_compat_check
{
2061 my @co_help = safe_qx
(qw(svn co -h));
2062 unless (grep /ignore-externals/,@co_help) {
2063 print STDERR
"W: Installed svn version does not support ",
2064 "--ignore-externals\n";
2065 $_no_ignore_ext = 1;
2067 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2068 $_svn_co_url_revs = 1;
2070 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2071 $_svn_pg_peg_revs = 1;
2074 # I really, really hope nobody hits this...
2075 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
2077 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
2079 Lets hope the directory you
're tracking is not a branch or tag
2080 and was never moved within the repository...
2086 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
2087 # (and they won't honor URL@<rev> without -r<rev>, too!)
2088 sub svn_cmd_checkout
{
2089 my ($url, $rev, $dir) = @_;
2090 my @cmd = ('svn','co', "-r$rev");
2091 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2092 $url .= "\@$rev" if $_svn_co_url_revs;
2093 sys
(@cmd, $url, $dir);
2096 sub check_upgrade_needed
{
2098 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2099 open my $fh, '>>',$REVDB or croak
$!;
2103 my $pid = open my $child, '-|';
2104 defined $pid or croak
$!;
2107 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$!;
2109 my @ret = (<$child>);
2110 close $child or croak
$?
;
2111 die $?
if $?
; # just in case close didn't error out
2112 return wantarray ?
@ret : join('',@ret);
2115 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
2117 print STDERR
"Please run: $0 rebuild --upgrade\n";
2122 # fills %tree_map with a reverse mapping of trees to commits. Useful
2123 # for finding parents to commit on.
2124 sub map_tree_joins
{
2126 foreach my $br (@_branch_from) {
2127 my $pid = open my $pipe, '-|';
2128 defined $pid or croak
$!;
2130 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2134 if (/^commit ($sha1)$/o) {
2137 # if we've seen a commit,
2138 # we've seen its parents
2139 last if $seen{$commit};
2140 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2141 unless (defined $tree) {
2142 die "Failed to parse commit $commit\n";
2144 push @
{$tree_map{$tree}}, $commit;
2148 close $pipe; # we could be breaking the pipe early
2153 if (@_branch_from) {
2154 print STDERR
'--branch|-b parameters are ignored when ',
2155 "--branch-all-refs|-B is passed\n";
2158 # don't worry about rev-list on non-commit objects/tags,
2159 # it shouldn't blow up if a ref is a blob or tree...
2160 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2163 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2165 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2166 while (<$authors>) {
2168 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2169 my ($user, $name, $email) = ($1, $2, $3);
2170 $users{$user} = [$name, $email];
2172 close $authors or croak
$!;
2176 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2177 while (<$authors>) {
2179 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2180 my ($user, $name, $email) = ($1, $2, $3);
2181 $rusers{"$name <$email>"} = $user;
2183 close $authors or croak
$!;
2186 sub svn_propget_base
{
2188 $f .= '@BASE' if $_svn_pg_peg_revs;
2189 return safe_qx
(qw
/svn propget/, $p, $f);
2194 foreach (`git-rev-parse --symbolic --all`) {
2195 next unless s
#^refs/remotes/##;
2197 next unless -f
"$GIT_DIR/svn/$_/info/url";
2205 defined(my $pid = fork) or croak
$!;
2207 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
2209 exit 0 if -r
$REVDB;
2210 print "Upgrading svn => git mapping...\n";
2211 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
2212 open my $fh, '>>',$REVDB or croak
$!;
2215 print "Done upgrading. You may now delete the ",
2216 "deprecated $GIT_SVN_DIR/revs directory\n";
2224 sub migration_check
{
2225 migrate_revdb
() unless (-e
$REVDB);
2226 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
2227 print "Upgrading repository...\n";
2228 unless (-d
"$GIT_DIR/svn") {
2229 mkdir "$GIT_DIR/svn" or croak
$!;
2231 print "Data from a previous version of git-svn exists, but\n\t",
2232 "$GIT_SVN_DIR\n\t(required for this version ",
2233 "($VERSION) of git-svn) does not.\n";
2235 foreach my $x (`git-rev-parse --symbolic --all`) {
2236 next unless $x =~ s
#^refs/remotes/##;
2238 next unless -f
"$GIT_DIR/$x/info/url";
2239 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
2241 my $dn = dirname
("$GIT_DIR/svn/$x");
2242 mkpath
([$dn]) unless -d
$dn;
2243 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
2245 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
2246 print "Done upgrading.\n";
2249 sub find_rev_before
{
2250 my ($r, $id, $eq_ok) = @_;
2251 my $f = "$GIT_DIR/svn/$id/.rev_db";
2252 return (undef,undef) unless -r
$f;
2255 if (my $c = revdb_get
($f, $r)) {
2260 return (undef, undef);
2264 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
2265 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2266 $REVDB = "$GIT_SVN_DIR/.rev_db";
2267 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2269 $SVN_WC = "$GIT_SVN_DIR/tree";
2273 # convert GetOpt::Long specs for use by git-repo-config
2274 sub read_repo_config
{
2275 return unless -d
$GIT_DIR;
2277 foreach my $o (keys %$opts) {
2278 my $v = $opts->{$o};
2279 my ($key) = ($o =~ /^([a-z\-]+)/);
2281 my $arg = 'git-repo-config';
2282 $arg .= ' --int' if ($o =~ /[:=]i$/);
2283 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2284 if (ref $v eq 'ARRAY') {
2285 chomp(my @tmp = `$arg --get-all svn.$key`);
2288 chomp(my $tmp = `$arg --get svn.$key`);
2289 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2296 sub set_default_vals
{
2297 if (defined $_repack) {
2298 $_repack = 1000 if ($_repack <= 0);
2299 $_repack_nr = $_repack;
2300 $_repack_flags ||= '-d';
2305 my $gr_file = shift;
2306 my ($grafts, $comments) = ({}, {});
2307 if (open my $fh, '<', $gr_file) {
2310 if (/^($sha1)\s+/) {
2313 @
{$comments->{$c}} = @tmp;
2316 foreach my $p (split /\s+/, $_) {
2317 $grafts->{$c}->{$p} = 1;
2323 close $fh or croak
$!;
2324 @
{$comments->{'END'}} = @tmp if @tmp;
2326 return ($grafts, $comments);
2330 my ($grafts, $comments, $gr_file) = @_;
2332 open my $fh, '>', $gr_file or croak
$!;
2333 foreach my $c (sort keys %$grafts) {
2334 if ($comments->{$c}) {
2335 print $fh $_ foreach @
{$comments->{$c}};
2337 my $p = $grafts->{$c};
2338 my %x; # real parents
2339 delete $p->{$c}; # commits are not self-reproducing...
2340 my $pid = open my $ch, '-|';
2341 defined $pid or croak
$!;
2343 exec(qw
/git-cat-file commit/, $c) or croak
$!;
2346 if (/^parent ($sha1)/) {
2347 $x{$1} = $p->{$1} = 1;
2352 close $ch; # breaking the pipe
2354 # if real parents are the only ones in the grafts, drop it
2355 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2359 @ip = @jp = keys %$p;
2360 foreach my $i (@ip) {
2361 next if $del{$i} || $p->{$i} == 2;
2362 foreach my $j (@jp) {
2363 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2364 $mb = eval { safe_qx
('git-merge-base',$i,$j) };
2371 } elsif ($mb eq $i) {
2378 # if real parents are the only ones in the grafts, drop it
2379 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2381 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2383 if ($comments->{'END'}) {
2384 print $fh $_ foreach @
{$comments->{'END'}};
2386 close $fh or croak
$!;
2389 sub read_url_paths
{
2391 git_svn_each
(sub { my $x = shift;
2392 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
2393 my ($u, $p) = repo_path_split
($url);
2394 $l_map->{$u}->{$p} = $x;
2399 sub extract_metadata
{
2400 my $id = shift or return (undef, undef, undef);
2401 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
2403 if (!$rev || !$uuid || !$url) {
2404 # some of the original repositories I made had
2405 # indentifiers like this:
2406 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2408 return ($url, $rev, $uuid);
2412 return extract_metadata
((grep(/^git-svn-id: /,
2413 safe_qx
(qw
/git-cat-file commit/, shift)))[-1]);
2416 sub get_commit_time
{
2418 defined(my $pid = open my $fh, '-|') or croak
$!;
2420 exec qw
/git-rev-list --pretty=raw -n1/, $cmt or croak
$!;
2423 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2424 my ($s, $tz) = ($1, $2);
2425 if ($tz =~ s/^\+//) {
2426 $s += tz_to_s_offset
($tz);
2427 } elsif ($tz =~ s/^\-//) {
2428 $s -= tz_to_s_offset
($tz);
2433 die "Can't get commit time for commit: $cmt\n";
2436 sub tz_to_s_offset
{
2439 return ($1 * 60) + ($tz * 3600);
2442 sub setup_pager
{ # translated to Perl from pager.c
2443 return unless (-t
*STDOUT
);
2444 my $pager = $ENV{PAGER
};
2445 if (!defined $pager) {
2447 } elsif (length $pager == 0 || $pager eq 'cat') {
2450 pipe my $rfd, my $wfd or return;
2451 defined(my $pid = fork) or croak
$!;
2453 open STDOUT
, '>&', $wfd or croak
$!;
2456 open STDIN
, '<&', $rfd or croak
$!;
2457 $ENV{LESS
} ||= '-S';
2458 exec $pager or croak
"Can't run pager: $!\n";;
2461 sub get_author_info
{
2462 my ($dest, $author, $t, $tz) = @_;
2463 $author =~ s/(?:^\s*|\s*$)//g;
2464 $dest->{a_raw
} = $author;
2467 $_a = $rusers{$author} || undef;
2470 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2475 # Date::Parse isn't in the standard Perl distro :(
2476 if ($tz =~ s/^\+//) {
2477 $t += tz_to_s_offset
($tz);
2478 } elsif ($tz =~ s/^\-//) {
2479 $t -= tz_to_s_offset
($tz);
2481 $dest->{t_utc
} = $t;
2484 sub process_commit
{
2485 my ($c, $r_min, $r_max, $defer) = @_;
2486 if (defined $r_min && defined $r_max) {
2487 if ($r_min == $c->{r
} && $r_min == $r_max) {
2491 return 1 if $r_min == $r_max;
2492 if ($r_min < $r_max) {
2493 # we need to reverse the print order
2494 return 0 if (defined $_limit && --$_limit < 0);
2498 if ($r_min != $r_max) {
2499 return 1 if ($r_min < $c->{r
});
2500 return 1 if ($r_max > $c->{r
});
2503 return 0 if (defined $_limit && --$_limit < 0);
2512 if (my $l = $c->{l
}) {
2513 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2516 $_l_fmt ||= 'A' . length($c->{r
});
2517 print 'r',pack($_l_fmt, $c->{r
}),' | ';
2518 print "$c->{c} | " if $_show_commit;
2521 show_commit_normal
($c);
2525 sub show_commit_normal
{
2527 print '-' x72
, "\nr$c->{r} | ";
2528 print "$c->{c} | " if $_show_commit;
2529 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2530 localtime($c->{t_utc
})), ' | ';
2533 if (my $l = $c->{l
}) {
2534 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2537 $nr_line = scalar @
$l;
2539 print "1 line\n\n\n";
2541 if ($nr_line == 1) {
2542 $nr_line = '1 line';
2544 $nr_line .= ' lines';
2546 print $nr_line, "\n\n";
2547 print $_ foreach @
$l;
2553 foreach my $x (qw
/raw diff/) {
2556 print $_ foreach @
{$c->{$x}}
2562 return unless $_use_lib;
2565 if ($SVN::Core
::VERSION
lt '1.1.0') {
2566 die "Need SVN::Core 1.1.0 or better ",
2567 "(got $SVN::Core::VERSION) ",
2568 "Falling back to command-line svn\n";
2572 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
2573 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
2574 $SVN::Node
::dir
.$SVN::Node
::unknown
.
2575 $SVN::Node
::none
.$SVN::Node
::file
.
2576 $SVN::Node
::dir
.$SVN::Node
::unknown
;
2581 sub libsvn_connect
{
2583 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
(),
2584 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2585 SVN
::Client
::get_username_provider
()]);
2586 my $s = eval { SVN
::Ra
->new(url
=> $url, auth
=> $auth) };
2590 sub libsvn_get_file
{
2591 my ($gui, $f, $rev) = @_;
2593 return unless ($p =~ s
#^\Q$SVN_PATH\E/?##);
2595 my ($hash, $pid, $in, $out);
2596 my $pool = SVN
::Pool
->new;
2597 defined($pid = open3
($in, $out, '>&STDERR',
2598 qw
/git-hash-object -w --stdin/)) or croak
$!;
2599 # redirect STDOUT for SVN 1.1.x compatibility
2600 open my $stdout, '>&', \
*STDOUT
or croak
$!;
2601 open STDOUT
, '>&', $in or croak
$!;
2602 $| = 1; # not sure if this is necessary, better safe than sorry...
2603 my ($r, $props) = $SVN->get_file($f, $rev, \
*STDOUT
, $pool);
2604 $in->flush == 0 or croak
$!;
2605 open STDOUT
, '>&', $stdout or croak
$!;
2606 close $in or croak
$!;
2607 close $stdout or croak
$!;
2609 chomp($hash = do { local $/; <$out> });
2610 close $out or croak
$!;
2612 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2614 my $mode = exists $props->{'svn:executable'} ?
'100755' : '100644';
2615 if (exists $props->{'svn:special'}) {
2617 my $link = `git-cat-file blob $hash`;
2618 $link =~ s/^link // or die "svn:special file with contents: <",
2619 $link, "> is not understood\n";
2620 defined($pid = open3
($in, $out, '>&STDERR',
2621 qw
/git-hash-object -w --stdin/)) or croak
$!;
2623 $in->flush == 0 or croak
$!;
2624 close $in or croak
$!;
2625 chomp($hash = do { local $/; <$out> });
2626 close $out or croak
$!;
2628 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2630 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak
$!;
2633 sub libsvn_log_entry
{
2634 my ($rev, $author, $date, $msg, $parents) = @_;
2635 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
2636 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
)
2637 or die "Unable to parse date: $date\n";
2638 if (defined $_authors && ! defined $users{$author}) {
2639 die "Author: $author not defined in $_authors file\n";
2641 return { revision
=> $rev, date
=> "+0000 $Y-$m-$d $H:$M:$S",
2642 author
=> $author, msg
=> $msg."\n", parents
=> $parents || [] }
2646 my ($gui, $last_commit, $f) = @_;
2647 $f =~ s
#^\Q$SVN_PATH\E/?## or return;
2648 # remove entire directories.
2649 if (safe_qx
('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2650 defined(my $pid = open my $ls, '-|') or croak
$!;
2652 exec(qw
/git-ls-tree -r --name-only -z/,
2653 $last_commit,'--',$f) or croak
$!;
2657 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
2659 close $ls or croak
$?
;
2661 print $gui '0 ',0 x
40,"\t",$f,"\0" or croak
$!;
2666 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2667 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2669 foreach my $f (keys %$paths) {
2670 my $m = $paths->{$f}->action();
2672 if ($m =~ /^[DR]$/) {
2673 process_rm
($gui, $last_commit, $f);
2675 # 'R' can be file replacements, too, right?
2677 my $pool = SVN
::Pool
->new;
2678 my $t = $SVN->check_path($f, $rev, $pool);
2679 if ($t == $SVN::Node
::file
) {
2680 if ($m =~ /^[AMR]$/) {
2683 die "Unrecognized action: $m, ($f r$rev)\n";
2688 libsvn_get_file
($gui, $_, $rev) foreach (@amr);
2689 close $gui or croak
$?
;
2690 return libsvn_log_entry
($rev, $author, $date, $msg, [$last_commit]);
2693 sub svn_grab_base_rev
{
2694 defined(my $pid = open my $fh, '-|') or croak
$!;
2696 open my $null, '>', '/dev/null' or croak
$!;
2697 open STDERR
, '>&', $null or croak
$!;
2698 exec qw
/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2701 chomp(my $c = do { local $/; <$fh> });
2703 if (defined $c && length $c) {
2704 my ($url, $rev, $uuid) = cmt_metadata
($c);
2707 return (undef, undef);
2710 sub libsvn_parse_revision
{
2712 my $head = $SVN->get_latest_revnum();
2713 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2714 return ($base + 1, $head) if (defined $base);
2717 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2718 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2719 if ($_revision =~ /^BASE:(\d+)$/) {
2720 return ($base + 1, $1) if (defined $base);
2723 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2724 die "revision argument: $_revision not understood by git-svn\n",
2725 "Try using the command-line svn client instead\n";
2728 sub libsvn_traverse
{
2729 my ($gui, $pfx, $path, $rev) = @_;
2730 my $cwd = "$pfx/$path";
2731 my $pool = SVN
::Pool
->new;
2733 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2734 foreach my $d (keys %$dirent) {
2735 my $t = $dirent->{$d}->kind;
2736 if ($t == $SVN::Node
::dir
) {
2737 libsvn_traverse
($gui, $cwd, $d, $rev);
2738 } elsif ($t == $SVN::Node
::file
) {
2739 libsvn_get_file
($gui, "$cwd/$d", $rev);
2745 sub libsvn_traverse_ignore
{
2746 my ($fh, $path, $r) = @_;
2748 my $pool = SVN
::Pool
->new;
2749 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2751 $p =~ s
#^\Q$SVN_PATH\E/?##;
2752 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
2753 if (my $s = $props->{'svn:ignore'}) {
2754 $s =~ s/[\r\n]+/\n/g;
2756 if (length $p == 0) {
2760 $s =~ s
#\n#\n/$p/#g;
2761 print $fh "/$p/$s\n";
2764 foreach (sort keys %$dirent) {
2765 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
2766 libsvn_traverse_ignore
($fh, "$path/$_", $r);
2772 my ($path, $r0, $r1) = @_;
2773 return 1 if $r0 == $r1;
2776 # should be OK to use Pool here (r1 - r0) should be small
2777 my $pool = SVN
::Pool
->new;
2778 libsvn_get_log
($SVN, "/$path", $r0, $r1,
2779 0, 1, 1, sub {$nr++}, $pool);
2782 my ($url, undef) = repo_path_split
($SVN_URL);
2783 my $svn_log = svn_log_raw
("$url/$path","-r$r0:$r1");
2784 while (next_log_entry
($svn_log)) { $nr++ }
2785 close $svn_log->{fh
};
2787 return 0 if ($nr > 1);
2791 sub libsvn_find_parent_branch
{
2792 my ($paths, $rev, $author, $date, $msg) = @_;
2793 my $svn_path = '/'.$SVN_PATH;
2795 # look for a parent from another branch:
2796 my $i = $paths->{$svn_path} or return;
2797 my $branch_from = $i->copyfrom_path or return;
2798 my $r = $i->copyfrom_rev;
2799 print STDERR
"Found possible branch point: ",
2800 "$branch_from => $svn_path, $r\n";
2801 $branch_from =~ s
#^/##;
2802 my $l_map = read_url_paths
();
2803 my $url = $SVN->{url
};
2804 defined $l_map->{$url} or return;
2805 my $id = $l_map->{$url}->{$branch_from} or return;
2806 my ($r0, $parent) = find_rev_before
($r,$id,1);
2807 return unless (defined $r0 && defined $parent);
2808 if (revisions_eq
($branch_from, $r0, $r)) {
2809 unlink $GIT_SVN_INDEX;
2810 print STDERR
"Found branch parent: $parent\n";
2811 sys
(qw
/git-read-tree/, $parent);
2812 return libsvn_fetch
($parent, $paths, $rev,
2813 $author, $date, $msg);
2815 print STDERR
"Nope, branch point not imported or unknown\n";
2819 sub libsvn_get_log
{
2820 my ($ra, @args) = @_;
2821 if ($SVN::Core
::VERSION
le '1.2.0') {
2822 splice(@args, 3, 1);
2824 $ra->get_log(@args);
2827 sub libsvn_new_tree
{
2828 if (my $log_entry = libsvn_find_parent_branch
(@_)) {
2831 my ($paths, $rev, $author, $date, $msg) = @_;
2832 open my $gui, '| git-update-index -z --index-info' or croak
$!;
2833 my $pool = SVN
::Pool
->new;
2834 libsvn_traverse
($gui, '', $SVN_PATH, $rev, $pool);
2836 close $gui or croak
$?
;
2837 return libsvn_log_entry
($rev, $author, $date, $msg);
2840 sub find_graft_path_commit
{
2841 my ($tree_paths, $p1, $r1) = @_;
2842 foreach my $x (keys %$tree_paths) {
2843 next unless ($p1 =~ /^\Q$x\E/);
2844 my $i = $tree_paths->{$x};
2845 my ($r0, $parent) = find_rev_before
($r1,$i,1);
2846 return $parent if (defined $r0 && $r0 == $r1);
2847 print STDERR
"r$r1 of $i not imported\n";
2853 sub find_graft_path_parents
{
2854 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2855 foreach my $x (keys %$tree_paths) {
2856 next unless ($p0 =~ /^\Q$x\E/);
2857 my $i = $tree_paths->{$x};
2858 my ($r, $parent) = find_rev_before
($r0, $i, 1);
2859 if (defined $r && defined $parent && revisions_eq
($x,$r,$r0)) {
2860 my ($url_b, undef, $uuid_b) = cmt_metadata
($c);
2861 my ($url_a, undef, $uuid_a) = cmt_metadata
($parent);
2862 next if ($url_a && $url_b && $url_a eq $url_b &&
2863 $uuid_b eq $uuid_a);
2864 $grafts->{$c}->{$parent} = 1;
2869 sub libsvn_graft_file_copies
{
2870 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2871 foreach (keys %$paths) {
2872 my $i = $paths->{$_};
2873 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2875 next unless (defined $p0 && defined $r0);
2880 my $c = find_graft_path_commit
($tree_paths, $p1, $rev);
2882 find_graft_path_parents
($grafts, $tree_paths, $c, $p0, $r0);
2887 my $old = $ENV{GIT_INDEX_FILE
};
2888 $ENV{GIT_INDEX_FILE
} = shift;
2895 $ENV{GIT_INDEX_FILE
} = $old;
2897 delete $ENV{GIT_INDEX_FILE
};
2901 sub libsvn_commit_cb
{
2902 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2903 if ($_optimize_commits && $rev == ($r_last + 1)) {
2904 my $log = libsvn_log_entry
($rev,$committer,$date,$msg);
2905 $log->{tree
} = get_tree_from_treeish
($c);
2906 my $cmt = git_commit
($log, $cmt_last, $c);
2907 my @diff = safe_qx
('git-diff-tree', $cmt, $c);
2909 print STDERR
"Trees differ: $cmt $c\n",
2910 join('',@diff),"\n";
2918 sub libsvn_ls_fullurl
{
2919 my $fullurl = shift;
2920 my ($repo, $path) = repo_path_split
($fullurl);
2921 $SVN ||= libsvn_connect
($repo);
2923 my $pool = SVN
::Pool
->new;
2924 my ($dirent, undef, undef) = $SVN->get_dir($path,
2925 $SVN->get_latest_revnum, $pool);
2926 foreach my $d (keys %$dirent) {
2927 if ($dirent->{$d}->kind == $SVN::Node
::dir
) {
2928 push @ret, "$d/"; # add '/' for compat with cli svn
2936 sub libsvn_skip_unknown_revs
{
2938 my $errno = $err->apr_err();
2939 # Maybe the branch we're tracking didn't
2940 # exist when the repo started, so it's
2941 # not an error if it doesn't, just continue
2943 # Wonderfully consistent library, eh?
2944 # 160013 - svn:// and file://
2945 # 175002 - http(s)://
2946 # More codes may be discovered later...
2947 if ($errno == 175002 || $errno == 160013) {
2950 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
2953 # Tie::File seems to be prone to offset errors if revisions get sparse,
2954 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
2955 # one of my favorite modules is out :< Next up would be one of the DBM
2956 # modules, but I'm not sure which is most portable... So I'll just
2957 # go with something that's plain-text, but still capable of
2958 # being randomly accessed. So here's my ultra-simple fixed-width
2959 # database. All records are 40 characters + "\n", so it's easy to seek
2960 # to a revision: (41 * rev) is the byte offset.
2961 # A record of 40 0s denotes an empty revision.
2962 # And yes, it's still pretty fast (faster than Tie::File).
2964 my ($file, $rev, $commit) = @_;
2965 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
2966 open my $fh, '+<', $file or croak
$!;
2967 my $offset = $rev * 41;
2968 # assume that append is the common case:
2969 seek $fh, 0, 2 or croak
$!;
2971 if ($pos < $offset) {
2972 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41);
2974 seek $fh, $offset, 0 or croak
$!;
2975 print $fh $commit,"\n";
2976 close $fh or croak
$!;
2980 my ($file, $rev) = @_;
2982 my $offset = $rev * 41;
2983 open my $fh, '<', $file or croak
$!;
2984 seek $fh, $offset, 0;
2985 if (tell $fh == $offset) {
2986 $ret = readline $fh;
2989 $ret = undef if ($ret =~ /^0{40}$/);
2992 close $fh or croak
$!;
2996 sub copy_remote_ref
{
2997 my $origin = $_cp_remote ?
$_cp_remote : 'origin';
2998 my $ref = "refs/remotes/$GIT_SVN";
2999 if (safe_qx
('git-ls-remote', $origin, $ref)) {
3000 sys
(qw
/git fetch/, $origin, "$ref:$ref");
3002 die "Unable to find remote reference: ",
3003 "refs/remotes/$GIT_SVN on $origin\n";
3007 package SVN
::Git
::Editor
;
3016 my $git_svn = shift;
3017 my $self = SVN
::Delta
::Editor
->new(@_);
3018 bless $self, $class;
3019 foreach (qw
/svn_path c r ra /) {
3020 die "$_ required!\n" unless (defined $git_svn->{$_});
3021 $self->{$_} = $git_svn->{$_};
3023 $self->{pool
} = SVN
::Pool
->new;
3024 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
3026 require Digest
::MD5
;
3031 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
3035 (defined $_[1] && length $_[1]) ?
"$_[0]->{svn_path}/$_[1]"
3040 my ($self, $path) = @_;
3041 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
3046 my $rm = $self->{rm
};
3047 delete $rm->{''}; # we never delete the url we're tracking
3050 foreach (keys %$rm) {
3051 my @d = split m
#/#, $_;
3055 $c .= '/' . shift @d;
3059 delete $rm->{$self->{svn_path
}};
3060 delete $rm->{''}; # we never delete the url we're tracking
3063 defined(my $pid = open my $fh,'-|') or croak
$!;
3065 exec qw
/git-ls-tree --name-only -r -z/, $self->{c
} or croak
$!;
3068 my @svn_path = split m
#/#, $self->{svn_path};
3071 my @dn = (@svn_path, (split m
#/#, $_));
3073 delete $rm->{join '/', @dn};
3082 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
3083 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3084 $self->close_directory($bat->{$d}, $p);
3085 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
3086 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
3091 sub open_or_add_dir
{
3092 my ($self, $full_path, $baton) = @_;
3093 my $p = SVN
::Pool
->new;
3094 my $t = $self->{ra
}->check_path($full_path, $self->{r
}, $p);
3096 if ($t == $SVN::Node
::none
) {
3097 return $self->add_directory($full_path, $baton,
3098 undef, -1, $self->{pool
});
3099 } elsif ($t == $SVN::Node
::dir
) {
3100 return $self->open_directory($full_path, $baton,
3101 $self->{r
}, $self->{pool
});
3103 print STDERR
"$full_path already exists in repository at ",
3104 "r$self->{r} and it is not a directory (",
3105 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
3110 my ($self, $path) = @_;
3111 my $bat = $self->{bat
};
3112 $path = $self->repo_path($path);
3113 return $bat->{''} unless (length $path);
3114 my @p = split m
#/+#, $path;
3116 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3119 $c .= '/' . shift @p;
3120 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3126 my ($self, $m) = @_;
3127 my ($dir, $file) = split_path
($m->{file_b
});
3128 my $pbat = $self->ensure_path($dir);
3129 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3131 $self->chg_file($fbat, $m);
3132 $self->close_file($fbat,undef,$self->{pool
});
3136 my ($self, $m) = @_;
3137 my ($dir, $file) = split_path
($m->{file_b
});
3138 my $pbat = $self->ensure_path($dir);
3139 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3140 $self->url_path($m->{file_a
}), $self->{r
});
3141 $self->chg_file($fbat, $m);
3142 $self->close_file($fbat,undef,$self->{pool
});
3146 my ($self, $path, $pbat) = @_;
3147 my $rpath = $self->repo_path($path);
3148 my ($dir, $file) = split_path
($rpath);
3149 $self->{rm
}->{$dir} = 1;
3150 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
3154 my ($self, $m) = @_;
3155 my ($dir, $file) = split_path
($m->{file_b
});
3156 my $pbat = $self->ensure_path($dir);
3157 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
3158 $self->url_path($m->{file_a
}), $self->{r
});
3159 $self->chg_file($fbat, $m);
3160 $self->close_file($fbat,undef,$self->{pool
});
3162 ($dir, $file) = split_path
($m->{file_a
});
3163 $pbat = $self->ensure_path($dir);
3164 $self->delete_entry($m->{file_a
}, $pbat);
3168 my ($self, $m) = @_;
3169 my ($dir, $file) = split_path
($m->{file_b
});
3170 my $pbat = $self->ensure_path($dir);
3171 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
3172 $pbat,$self->{r
},$self->{pool
});
3173 $self->chg_file($fbat, $m);
3174 $self->close_file($fbat,undef,$self->{pool
});
3177 sub T
{ shift->M(@_) }
3179 sub change_file_prop
{
3180 my ($self, $fbat, $pname, $pval) = @_;
3181 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
3185 my ($self, $fbat, $m) = @_;
3186 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
3187 $self->change_file_prop($fbat,'svn:executable','*');
3188 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
3189 $self->change_file_prop($fbat,'svn:executable',undef);
3191 my $fh = IO
::File
->new_tmpfile or croak
$!;
3192 if ($m->{mode_b
} =~ /^120/) {
3193 print $fh 'link ' or croak
$!;
3194 $self->change_file_prop($fbat,'svn:special','*');
3195 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
3196 $self->change_file_prop($fbat,'svn:special',undef);
3198 defined(my $pid = fork) or croak
$!;
3200 open STDOUT
, '>&', $fh or croak
$!;
3201 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
3205 $fh->flush == 0 or croak
$!;
3206 seek $fh, 0, 0 or croak
$!;
3208 my $md5 = Digest
::MD5
->new;
3209 $md5->addfile($fh) or croak
$!;
3210 seek $fh, 0, 0 or croak
$!;
3212 my $exp = $md5->hexdigest;
3213 my $atd = $self->apply_textdelta($fbat, undef, $self->{pool
});
3214 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $self->{pool
});
3215 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3217 close $fh or croak
$!;
3221 my ($self, $m) = @_;
3222 my ($dir, $file) = split_path
($m->{file_b
});
3223 my $pbat = $self->ensure_path($dir);
3224 $self->delete_entry($m->{file_b
}, $pbat);
3229 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
3230 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3231 $self->close_directory($bat->{$_}, $p);
3233 $self->SUPER::close_edit
($p);
3239 $self->SUPER::abort_edit
($self->{pool
});
3240 $self->{pool
}->clear;
3247 $svn_log hashref
(as returned by svn_log_raw
)
3249 fh
=> file handle of the
log file
,
3250 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
3253 $log_msg hashref as returned by next_log_entry
($svn_log)
3255 msg
=> 'whitespace-formatted log entry
3256 ', # trailing newline is preserved
3257 revision
=> '8', # integer
3258 date
=> '2004-02-24T17:01:44.108345Z', # commit date
3259 author
=> 'committer name'
3263 @mods = array of diff
-index line hashes
, each element represents one line
3264 of diff
-index output
3266 diff
-index line
($m hash
)
3268 mode_a
=> first column of diff
-index output
, no leading
':',
3269 mode_b
=> second column of diff
-index output
,
3270 sha1_b
=> sha1sum of the final blob
,
3271 chg
=> change type
[MCRADT
],
3272 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
3273 file_b
=> new
/current file name of a file
(any chg
)
3278 I don
't trust the each() function on unless I created %hash myself
3279 because the internal iterator may not have started at base.