2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB
10 $_follow_parent $sha1 $sha1_short $_revision
11 $_cp_remote $_upgrade $_rmdir $_q $_cp_similarity
12 $_find_copies_harder $_l $_authors %users/;
13 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
14 $VERSION = '@@GIT_VERSION@@';
17 $GIT_DIR = abs_path
($ENV{GIT_DIR
} || '.git');
18 $ENV{GIT_DIR
} = $GIT_DIR;
20 my $LC_ALL = $ENV{LC_ALL
};
21 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
22 # make sure the svn binary gives consistent output between locales and TZs:
25 $| = 1; # unbuffer STDOUT
27 sub fatal
(@
) { print STDERR
@_; exit 1 }
28 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
31 if ($SVN::Core
::VERSION
lt '1.1.0') {
32 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
34 push @Git::SVN
::Ra
::ISA
, 'SVN::Ra';
35 push @SVN::Git
::Editor
::ISA
, 'SVN::Delta::Editor';
36 push @SVN::Git
::Fetcher
::ISA
, 'SVN::Delta::Editor';
39 use File
::Basename qw
/dirname basename/;
40 use File
::Path qw
/mkpath/;
41 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
47 foreach (qw
/command command_oneline command_noisy command_output_pipe
48 command_input_pipe command_close_pipe
/) {
49 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
50 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
57 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
58 $sha1 = qr/[a-f\d]{40}/;
59 $sha1_short = qr/[a-f\d]{4,40}/;
60 my ($_stdin, $_help, $_edit,
61 $_repack, $_repack_nr, $_repack_flags,
62 $_message, $_file, $_no_metadata,
65 $_merge, $_strategy, $_dry_run,
67 my @repo_path_split_cache;
69 my %fc_opts = ( 'follow-parent|follow' => \
$_follow_parent,
70 'authors-file|A=s' => \
$_authors,
71 'repack:i' => \
$_repack,
72 'no-metadata' => \
$_no_metadata,
74 'username=s' => \
$Git::SVN
::Prompt
::_username
,
75 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
76 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
77 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags);
79 my ($_trunk, $_tags, $_branches);
80 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
81 'tags|t=s' => \
$_tags,
82 'branches|b=s' => \
$_branches );
83 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
84 my %cmt_opts = ( 'edit|e' => \
$_edit,
86 'find-copies-harder' => \
$_find_copies_harder,
88 'copy-similarity|C=i'=> \
$_cp_similarity
92 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
93 { 'revision|r=s' => \
$_revision, %fc_opts } ],
94 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
95 " (requires URL argument)",
97 dcommit
=> [ \
&cmd_dcommit
,
98 'Commit several diffs to merge with upstream',
99 { 'merge|m|M' => \
$_merge,
100 'strategy|s=s' => \
$_strategy,
101 'dry-run|n' => \
$_dry_run,
102 %cmt_opts, %fc_opts } ],
103 'set-tree' => [ \
&cmd_set_tree
,
104 "Set an SVN repository to a git tree-ish",
105 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
106 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
107 { 'revision|r=i' => \
$_revision } ],
108 rebuild
=> [ \
&cmd_rebuild
, "Rebuild git-svn metadata (after git clone)",
109 { 'copy-remote|remote=s' => \
$_cp_remote,
110 'upgrade' => \
$_upgrade } ],
111 'multi-init' => [ \
&cmd_multi_init
,
112 'Initialize multiple trees (like git-svnimport)',
113 { %multi_opts, %init_opts,
114 'revision|r=i' => \
$_revision,
115 'username=s' => \
$Git::SVN
::Prompt
::_username
,
116 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
117 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
118 'prefix=s' => \
$_prefix,
120 'multi-fetch' => [ \
&cmd_multi_fetch
,
121 'Fetch multiple trees (like git-svnimport)',
123 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
124 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
125 'revision|r=s' => \
$_revision,
126 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
127 'incremental' => \
$Git::SVN
::Log
::incremental
,
128 'oneline' => \
$Git::SVN
::Log
::oneline
,
129 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
130 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
131 'authors-file|A=s' => \
$_authors,
132 'color' => \
$Git::SVN
::Log
::color
,
133 'pager=s' => \
$Git::SVN
::Log
::pager
,
135 'commit-diff' => [ \
&cmd_commit_diff
,
136 'Commit a diff between two trees',
137 { 'message|m=s' => \
$_message,
138 'file|F=s' => \
$_file,
139 'revision|r=s' => \
$_revision,
144 for (my $i = 0; $i < @ARGV; $i++) {
145 if (defined $cmd{$ARGV[$i]}) {
152 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
154 read_repo_config
(\
%opts);
155 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
156 'version|V' => \
$_version,
157 'id|i=s' => \
$GIT_SVN);
158 exit 1 if (!$rv && $cmd ne 'log');
161 version
() if $_version;
162 usage
(1) unless defined $cmd;
164 load_authors
() if $_authors;
165 migration_check
() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
166 $cmd{$cmd}->[0]->(@ARGV);
169 ####################### primary functions ######################
171 my $exit = shift || 0;
172 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
174 git
-svn
- bidirectional operations between a single Subversion tree
and git
175 Usage
: $0 <command
> [options
] [arguments
]\n
177 print $fd "Available commands:\n" unless $cmd;
179 foreach (sort keys %cmd) {
180 next if $cmd && $cmd ne $_;
181 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
182 foreach (keys %{$cmd{$_}->[2]}) {
183 # prints out arguments as they should be passed:
184 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
185 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
187 split /\|/,$_)," $x\n";
191 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
192 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
193 one git repository and want to keep them separate. See git-svn(1) for more
200 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
206 my $gs = $url ? Git::SVN->init(undef, $url)
207 : eval { Git::SVN->new };
208 $gs ||= Git::SVN->_new;
209 if (!verify_ref($gs->refname.'^0')) {
210 $gs->copy_remote_ref;
213 command_noisy('update
-ref',$gs->refname, $gs->{id}.'-HEAD
');
215 $gs->check_upgrade_needed;
218 my ($rev_list, $ctx) = command_output_pipe("rev-list", $gs->refname);
221 while (<$rev_list>) {
224 fatal "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
225 my ($url, $rev, $uuid) = cmt_metadata($c);
227 # ignore merges (from set-tree)
228 next if (!defined $rev || !$uuid);
230 # if we merged or otherwise started elsewhere, this is
231 # how we break out of it
232 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
233 ($gs->{url} && $url && ($url ne $gs->{url}))) {
237 unless (defined $latest) {
238 if (!$gs->{url} && !$url) {
239 fatal "SVN repository location required\n";
241 $gs = Git::SVN->init(undef, $url);
244 $gs->rev_db_set($rev, $c);
245 print "r$rev = $c\n";
247 command_close_pipe($rev_list, $ctx);
251 unless (-d $ENV{GIT_DIR}) {
252 my @init_db = ('init
');
253 push @init_db, "--template=$_template" if defined $_template;
254 push @init_db, "--shared" if defined $_shared;
255 command_noisy(@init_db);
260 my $url = shift or die "SVN repository location required " .
261 "as a command-line argument\n";
262 if (my $repo_path = shift) {
263 unless (-d $repo_path) {
264 mkpath([$repo_path]);
266 chdir $repo_path or croak $!;
267 $ENV{GIT_DIR} = $repo_path . "/.git";
271 Git::SVN->init(undef, $url);
275 my $gs = Git::SVN->new;
277 if ($gs->{last_commit} && !verify_ref('refs
/heads/master
^0')) {
278 command_noisy(qw(update-ref refs/heads/master),
285 if ($_stdin || !@commits) {
286 print "Reading from stdin...\n";
289 if (/\b($sha1_short)\b/o) {
290 unshift @commits, $1;
295 foreach my $c (@commits) {
296 my @tmp = command
('rev-parse',$c);
297 if (scalar @tmp == 1) {
299 } elsif (scalar @tmp > 1) {
300 push @revs, reverse(command
('rev-list',@tmp));
302 fatal
"Failed to rev-parse $c\n";
305 my $gs = Git
::SVN
->new;
306 my ($r_last, $cmt_last) = $gs->last_rev_commit;
308 if ($r_last != $gs->{last_rev
}) {
309 fatal
"There are new revisions that were fetched ",
310 "and need to be merged (or acknowledged) ",
311 "before committing.\nlast rev: $r_last\n",
312 " current: $gs->{last_rev}\n";
314 $gs->set_tree($_) foreach @revs;
315 print "Done committing ",scalar @revs," revisions to SVN\n";
320 my $gs = Git
::SVN
->new;
322 my @refs = command
(qw
/rev-list --no-merges/, $gs->refname."..$head");
324 foreach my $d (reverse @refs) {
325 if (!verify_ref
("$d~1")) {
327 "has no parent commit, and therefore ",
328 "nothing to diff against.\n",
329 "You should be working from a repository ",
330 "originally created by git-svn\n";
332 unless (defined $last_rev) {
333 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
334 unless (defined $last_rev) {
335 fatal
"Unable to extract revision information ",
336 "from commit $d~1\n";
340 print "diff-tree $d~1 $d\n";
343 my $pool = SVN
::Pool
->new;
344 my %ed_opts = ( r
=> $last_rev,
346 svn_path
=> $ra->{svn_path
} );
347 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
348 $ra->get_commit_editor($::_message
,
349 sub { print "Committed r$_[0]\n";
350 $last_rev = $_[0]; }),
352 my $mods = $ed->apply_diff("$d~1", $d);
354 print "No changes\n$d~1 == $d\n";
360 # we always want to rebase against the current HEAD, not any
361 # head that was passed to us
362 my @diff = command
('diff-tree', 'HEAD', $gs->refname, '--');
365 @finish = qw
/rebase/;
366 push @finish, qw
/--merge/ if $_merge;
367 push @finish, "--strategy=$_strategy" if $_strategy;
368 print STDERR
"W: HEAD and ", $gs->refname, " differ, ",
369 "using @finish:\n", "@diff";
371 print "No changes between current HEAD and ",
372 $gs->refname, "\nResetting to the latest ",
374 @finish = qw
/reset --mixed/;
376 command_noisy
(@finish, $gs->refname);
379 sub cmd_show_ignore
{
380 my $gs = Git
::SVN
->new;
381 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
382 $gs->traverse_ignore(\
*STDOUT
, '', $r);
387 unless (defined $_trunk || defined $_branches || defined $_tags) {
391 $_prefix = '' unless defined $_prefix;
392 if (defined $_trunk) {
393 my $gs_trunk = eval { Git
::SVN
->new($_prefix . 'trunk') };
395 my $trunk_url = complete_svn_url
($url, $_trunk);
396 $gs_trunk = Git
::SVN
->init($_prefix . 'trunk',
398 command_noisy
('config', 'svn.trunk', $trunk_url);
401 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
402 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
403 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
406 sub cmd_multi_fetch
{
407 # try to do trunk first, since branches/tags
408 # may be descended from it.
409 if (-e
"$ENV{GIT_DIR}/svn/trunk/info/url") {
410 my $gs = Git
::SVN
->new('trunk');
413 rec_fetch
('', "$ENV{GIT_DIR}/svn", @_);
416 # this command is special because it requires no metadata
417 sub cmd_commit_diff
{
418 my ($ta, $tb, $url) = @_;
419 my $usage = "Usage: $0 commit-diff -r<revision> ".
420 "<tree-ish> <tree-ish> [<URL>]\n";
421 fatal
($usage) if (!defined $ta || !defined $tb);
423 my $gs = eval { Git
::SVN
->new };
425 fatal
("Needed URL or usable git-svn --id in ",
426 "the command-line\n", $usage);
430 unless (defined $_revision) {
431 fatal
("-r|--revision is a required argument\n", $usage);
433 if (defined $_message && defined $_file) {
434 fatal
("Both --message/-m and --file/-F specified ",
435 "for the commit message.\n",
436 "I have no idea what you mean\n");
438 if (defined $_file) {
439 $_message = file_to_s
($_file);
441 $_message ||= get_commit_entry
($tb)->{log};
443 my $ra ||= Git
::SVN
::Ra
->new($url);
446 $r = $ra->get_latest_revnum;
447 } elsif ($r !~ /^\d+$/) {
448 die "revision argument: $r not understood by git-svn\n";
450 my $pool = SVN
::Pool
->new;
451 my %ed_opts = ( r
=> $r,
453 svn_path
=> $ra->{svn_path
} );
454 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
455 $ra->get_commit_editor($_message,
456 sub { print "Committed r$_[0]\n" }),
458 my $mods = $ed->apply_diff($ta, $tb);
460 print "No changes\n$ta == $tb\n";
465 ########################### utility functions #########################
468 my ($pfx, $p, @args) = @_;
470 foreach (sort <$p/*>) {
471 if (-r
"$_/info/url") {
472 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
473 my $id = $pfx . basename
$_;
474 next if $id eq 'trunk';
475 my $gs = Git
::SVN
->new($id);
483 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
484 rec_fetch
($x, $_, @args);
488 sub complete_svn_url
{
489 my ($url, $path) = @_;
491 $url =~ s
#/+$## if $url;
492 if ($path !~ m
#^[a-z\+]+://#) {
493 $path = '/' . $path if ($path !~ m
#^/#);
494 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
495 fatal
("E: '$path' is not a complete URL ",
496 "and a separate URL is not specified\n");
498 $path = $url . $path;
503 sub complete_url_ls_init
{
504 my ($ra, $path, $switch, $pfx) = @_;
506 print STDERR
"W: $switch not specified\n";
510 if ($path =~ m
#^[a-z\+]+://#) {
511 $ra = Git
::SVN
::Ra
->new($path);
516 fatal
("E: '$path' is not a complete URL ",
517 "and a separate URL is not specified\n");
520 my $r = defined $_revision ?
$_revision : $ra->get_latest_revnum;
521 my ($dirent, undef, undef) = $ra->get_dir($path, $r);
522 my $url = $ra->{url
} . (length $path ?
"/$path" : '');
523 foreach my $d (sort keys %$dirent) {
524 next if ($dirent->{$d}->kind != $SVN::Node
::dir
);
527 my $gs = eval { Git
::SVN
->new($id) };
528 # don't try to init already existing refs
530 print "init $u => $id\n";
531 Git
::SVN
->init($id, $u);
534 my ($n) = ($switch =~ /^--(\w+)/);
535 command_noisy
('config', "svn.$n", $url);
542 my @tmp = split m
#/#, $_;
544 while (my $x = shift @tmp) {
550 foreach (sort {length $b <=> length $a} keys %common) {
551 if ($common{$_} == @
$paths) {
560 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
564 sub repo_path_split
{
565 my $full_url = shift;
566 $full_url =~ s
#/+$##;
568 foreach (@repo_path_split_cache) {
569 if ($full_url =~ s
#$_##) {
571 $full_url =~ s
#^/+##;
572 return ($u, $full_url);
575 my $tmp = Git
::SVN
::Ra
->new($full_url);
576 return ($tmp->{repos_root
}, $tmp->{svn_path
});
580 defined $SVN_URL or croak
"SVN repository location required\n";
581 unless (-d
$GIT_DIR) {
582 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
584 mkpath
([$GIT_SVN_DIR]);
585 mkpath
(["$GIT_SVN_DIR/info"]);
586 open my $fh, '>>',$REVDB or croak
$!;
588 s_to_file
($SVN_URL,"$GIT_SVN_DIR/info/url");
592 sub get_tree_from_treeish
{
594 # $treeish can be a symbolic ref, too:
595 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
597 while ($type eq 'tag') {
598 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
600 if ($type eq 'commit') {
601 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
603 ($expected) = ($expected =~ /^tree ($sha1)$/o);
604 die "Unable to get tree from $treeish\n" unless $expected;
605 } elsif ($type eq 'tree') {
606 $expected = $treeish;
608 die "$treeish is a $type, expected tree, tag or commit\n";
613 sub get_commit_entry
{
614 my ($treeish) = shift;
615 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
616 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
617 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
618 open my $log_fh, '>', $commit_editmsg or croak
$!;
620 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
621 if ($type eq 'commit' || $type eq 'tag') {
622 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
627 $in_msg = 1 if (/^\s*$/);
628 } elsif (/^git-svn-id: /) {
629 # skip this for now, we regenerate the
630 # correct one on re-fetch anyways
631 # TODO: set *:merge properties or like...
633 print $log_fh $_ or croak
$!;
636 command_close_pipe
($msg_fh, $ctx);
638 close $log_fh or croak
$!;
640 if ($_edit || ($type eq 'tree')) {
641 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
642 # TODO: strip out spaces, comments, like git-commit.sh
643 system($editor, $commit_editmsg);
645 rename $commit_editmsg, $commit_msg or croak
$!;
646 open $log_fh, '<', $commit_msg or croak
$!;
647 { local $/; chomp($log_entry{log} = <$log_fh>); }
648 close $log_fh or croak
$!;
654 my ($str, $file, $mode) = @_;
655 open my $fd,'>',$file or croak
$!;
656 print $fd $str,"\n" or croak
$!;
657 close $fd or croak
$!;
658 chmod ($mode &~ umask, $file) if (defined $mode);
663 open my $fd,'<',$file or croak
"$!: file: $file\n";
666 close $fd or croak
$!;
671 sub check_upgrade_needed
{
673 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
674 open my $fh, '>>',$REVDB or croak
$!;
678 command
([qw
/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
681 my $head = eval { command
('rev-parse',"refs/remotes/$GIT_SVN") };
683 print STDERR
"Please run: $0 rebuild --upgrade\n";
688 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
690 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
691 my $log = $cmd eq 'log';
694 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
695 my ($user, $name, $email) = ($1, $2, $3);
697 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
699 $users{$user} = [$name, $email];
702 close $authors or croak
$!;
707 foreach (command
(qw
/rev-parse --symbolic --all/)) {
708 next unless s
#^refs/remotes/##;
710 next unless -f
"$GIT_DIR/svn/$_/info/url";
718 defined(my $pid = fork) or croak
$!;
720 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
723 print "Upgrading svn => git mapping...\n";
724 -d
$GIT_SVN_DIR or mkpath
([$GIT_SVN_DIR]);
725 open my $fh, '>>',$REVDB or croak
$!;
728 print "Done upgrading. You may now delete the ",
729 "deprecated $GIT_SVN_DIR/revs directory\n";
737 sub migration_check
{
738 migrate_revdb
() unless (-e
$REVDB);
739 return if (-d
"$GIT_DIR/svn" || !-d
$GIT_DIR);
740 print "Upgrading repository...\n";
741 unless (-d
"$GIT_DIR/svn") {
742 mkdir "$GIT_DIR/svn" or croak
$!;
744 print "Data from a previous version of git-svn exists, but\n\t",
745 "$GIT_SVN_DIR\n\t(required for this version ",
746 "($VERSION) of git-svn) does not.\n";
748 foreach my $x (command
(qw
/rev-parse --symbolic --all/)) {
749 next unless $x =~ s
#^refs/remotes/##;
751 next unless -f
"$GIT_DIR/$x/info/url";
752 my $u = eval { file_to_s
("$GIT_DIR/$x/info/url") };
754 my $dn = dirname
("$GIT_DIR/svn/$x");
755 mkpath
([$dn]) unless -d
$dn;
756 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak
"$!: $x";
758 migrate_revdb
() if (-d
$GIT_SVN_DIR && !-w
$REVDB);
759 print "Done upgrading.\n";
763 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
764 $Git::SVN
::default = $GIT_SVN;
765 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
766 $REVDB = "$GIT_SVN_DIR/.rev_db";
767 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
771 # convert GetOpt::Long specs for use by git-config
772 sub read_repo_config
{
773 return unless -d
$GIT_DIR;
775 foreach my $o (keys %$opts) {
777 my ($key) = ($o =~ /^([a-z\-]+)/);
779 my $arg = 'git-config';
780 $arg .= ' --int' if ($o =~ /[:=]i$/);
781 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
782 if (ref $v eq 'ARRAY') {
783 chomp(my @tmp = `$arg --get-all svn.$key`);
786 chomp(my $tmp = `$arg --get svn.$key`);
787 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
794 sub read_url_paths_all
{
795 my ($l_map, $pfx, $p) = @_;
798 if (-r
"$_/info/url") {
799 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
800 my $id = $pfx . basename
$_;
801 my $url = file_to_s
("$_/info/url");
802 my ($u, $p) = repo_path_split
($url);
803 $l_map->{$u}->{$p} = $id;
810 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
811 read_url_paths_all
($l_map, $x, $_);
815 # this one only gets ids that have been imported, not new ones
818 git_svn_each
(sub { my $x = shift;
819 my $url = file_to_s
("$GIT_DIR/svn/$x/info/url");
820 my ($u, $p) = repo_path_split
($url);
821 $l_map->{$u}->{$p} = $x;
826 sub extract_metadata
{
827 my $id = shift or return (undef, undef, undef);
828 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
830 if (!defined $rev || !$uuid || !$url) {
831 # some of the original repositories I made had
832 # identifiers like this:
833 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
835 return ($url, $rev, $uuid);
839 return extract_metadata
((grep(/^git-svn-id: /,
840 command
(qw
/cat-file commit/, shift)))[-1]);
843 sub get_commit_time
{
845 my $fh = command_output_pipe
(qw
/rev-list --pretty=raw -n1/, $cmt);
847 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
848 my ($s, $tz) = ($1, $2);
849 if ($tz =~ s/^\+//) {
850 $s += tz_to_s_offset
($tz);
851 } elsif ($tz =~ s/^\-//) {
852 $s -= tz_to_s_offset
($tz);
857 die "Can't get commit time for commit: $cmt\n";
863 return ($1 * 60) + ($tz * 3600);
869 use vars qw
/$default/;
871 use File
::Path qw
/mkpath/;
874 # properties that we do not log:
877 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
878 svn
:special svn
:executable
879 svn
:entry
:committed
-rev
880 svn
:entry
:last-author
882 svn
:entry
:committed
-date
/;
886 my ($class, $id, $url) = @_;
887 my $self = _new
($class, $id);
888 mkpath
(["$self->{dir}/info"]);
890 $url =~ s!/+$!!; # strip trailing slash
891 ::s_to_file
($url, "$self->{dir}/info/url");
894 open my $fh, '>>', $self->{db_path
} or croak
$!;
895 close $fh or croak
$!;
900 my ($class, $id) = @_;
901 my $self = _new
($class, $id);
902 $self->{url
} = ::file_to_s
("$self->{dir}/info/url");
906 sub refname
{ "refs/remotes/$_[0]->{id}" }
910 $self->{ra
} ||= Git
::SVN
::Ra
->new($self->{url
});
913 sub copy_remote_ref
{
915 my $origin = $::_cp_remote ?
$::_cp_remote
: 'origin';
916 my $ref = $self->refname;
917 if (command
('ls-remote', $origin, $ref)) {
918 command_noisy
('fetch', $origin, "$ref:$ref");
919 } elsif ($::_cp_remote
&& !$::_upgrade
) {
920 die "Unable to find remote reference: $ref on $origin\n";
924 sub traverse_ignore
{
925 my ($self, $fh, $path, $r) = @_;
927 my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
929 $p =~ s
#^\Q$self->{ra}->{svn_path}\E/##;
930 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
931 if (my $s = $props->{'svn:ignore'}) {
932 $s =~ s/[\r\n]+/\n/g;
934 if (length $p == 0) {
939 print $fh "/$p/$s\n";
942 foreach (sort keys %$dirent) {
943 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
944 $self->traverse_ignore($fh, "$path/$_", $r);
948 # returns the newest SVN revision number and newest commit SHA1
949 sub last_rev_commit
{
951 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
952 return ($self->{last_rev
}, $self->{last_commit
});
954 my $c = ::verify_ref
($self->refname.'^0');
955 if (defined $c && length $c) {
956 my $rev = (::cmt_metadata
($c))[1];
958 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
962 my $offset = -41; # from tail
964 open my $fh, '<', $self->{db_path
} or
965 croak
"$self->{db_path} not readable: $!\n";
966 seek $fh, $offset, 2;
968 defined $rl or return (undef, undef);
970 while ($c ne $rl && tell $fh != 0) {
972 seek $fh, $offset, 2;
974 defined $rl or return (undef, undef);
978 croak
$! if ($rev < 0);
979 $rev = ($rev - 41) / 41;
980 close $fh or croak
$!;
981 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
986 my ($self, $base) = @_;
987 my $head = $self->ra->get_latest_revnum;
988 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
989 return ($base + 1, $head) if (defined $base);
992 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
993 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
994 if ($::_revision
=~ /^BASE:(\d+)$/) {
995 return ($base + 1, $1) if (defined $base);
998 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
999 die "revision argument: $::_revision not understood by git-svn\n",
1000 "Try using the command-line svn client instead\n";
1004 my ($self, $sub) = @_;
1005 my $old_index = $ENV{GIT_INDEX_FILE
};
1006 $ENV{GIT_INDEX_FILE
} = $self->{index};
1009 $ENV{GIT_INDEX_FILE
} = $old_index;
1011 delete $ENV{GIT_INDEX_FILE
};
1013 wantarray ?
@ret : $ret[0];
1016 sub assert_index_clean
{
1017 my ($self, $treeish) = @_;
1019 $self->tmp_index_do(sub {
1020 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
1021 my $x = command_oneline
('write-tree');
1022 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
1023 /^tree ($::sha1)/mo);
1025 unlink $self->{index} or croak
$!;
1026 command_noisy
('read-tree', $treeish);
1028 $x = command_oneline
('write-tree');
1030 ::fatal
"trees ($treeish) $y != $x\n",
1031 "Something is seriously wrong...\n";
1036 sub get_commit_parents
{
1037 my ($self, $log_entry, @parents) = @_;
1038 my (%seen, @ret, @tmp);
1039 # commit parents can be conditionally bound to a particular
1040 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1041 foreach my $p (@parents) {
1042 next unless defined $p;
1043 if ($p =~ /^(\d+)=($::sha1_short)$/o) {
1044 push @tmp, $2 if $1 == $log_entry->{revision
};
1046 push @tmp, $p if $p =~ /^$::sha1_short$/o;
1049 if (my $cur = ::verify_ref
($self->refname.'^0')) {
1052 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
1053 while (my $p = shift @tmp) {
1057 # MAXPARENT is defined to 16 in commit-tree.c:
1061 die "r$log_entry->{revision}: No room for parents:\n\t",
1062 join("\n\t", @tmp), "\n";
1067 sub check_upgrade_needed
{
1069 if (!-r
$self->{db_path
}) {
1070 -d
$self->{dir
} or mkpath
([$self->{dir
}]);
1071 open my $fh, '>>', $self->{db_path
} or croak
$!;
1074 return unless ::verify_ref
($self->{id
}.'-HEAD^0');
1075 my $head = ::verify_ref
($self->refname.'^0');
1077 ::fatal
("Please run: $0 rebuild --upgrade\n");
1082 my ($self, $log_entry, @parents) = @_;
1083 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
1084 croak
"$log_entry->{revision} = $c already exists! ",
1085 "Why are we refetching it?\n";
1087 my $author = $log_entry->{author
};
1088 my ($name, $email) = (defined $::users
{$author} ? @
{$::users
{$author}}
1089 : ($author, "$author\@".$self->ra->uuid));
1090 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1091 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1092 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
1094 my $tree = $log_entry->{tree
};
1095 if (!defined $tree) {
1096 $tree = $self->tmp_index_do(sub {
1097 command_oneline
('write-tree') });
1099 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1101 my @exec = ('git-commit-tree', $tree);
1102 foreach ($self->get_commit_parents($log_entry, @parents)) {
1103 push @exec, '-p', $_;
1105 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1107 print $msg_fh $log_entry->{log} or croak
$!;
1108 print $msg_fh "\ngit-svn-id: ", $self->ra->{url
}, '@',
1109 $log_entry->{revision
}, ' ',
1110 $self->ra->uuid, "\n" or croak
$!;
1111 $msg_fh->flush == 0 or croak
$!;
1112 close $msg_fh or croak
$!;
1113 chomp(my $commit = do { local $/; <$out_fh> });
1114 close $out_fh or croak
$!;
1117 if ($commit !~ /^$::sha1$/o) {
1118 die "Failed to commit, invalid sha1: $commit\n";
1121 command_noisy
('update-ref',$self->refname, $commit);
1122 $self->rev_db_set($log_entry->{revision
}, $commit);
1124 $self->{last_rev
} = $log_entry->{revision
};
1125 $self->{last_commit
} = $commit;
1126 print "r$log_entry->{revision} = $commit\n";
1131 my ($self, $paths, $rev) = @_; #, $author, $date, $log) = @_;
1132 my $ed = SVN
::Git
::Fetcher
->new($self);
1133 my ($last_rev, @parents);
1134 if ($self->{last_commit
}) {
1135 $last_rev = $self->{last_rev
};
1136 $ed->{c
} = $self->{last_commit
};
1137 @parents = ($self->{last_commit
});
1141 unless ($self->ra->gs_do_update($last_rev, $rev, '', 1, $ed)) {
1142 die "SVN connection failed somewhere...\n";
1144 $self->make_log_entry($rev, \
@parents, $ed);
1147 sub write_untracked
{
1148 my ($self, $rev, $fh, $untracked) = @_;
1150 print $fh "r$rev\n" or croak
$!;
1151 $h = $untracked->{empty
};
1152 foreach (sort keys %$h) {
1153 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1154 print $fh " $act: ", uri_encode
($_), "\n" or croak
$!;
1155 warn "W: $act: $_\n";
1157 foreach my $t (qw
/dir_prop file_prop/) {
1158 $h = $untracked->{$t} or next;
1159 foreach my $path (sort keys %$h) {
1160 my $ppath = $path eq '' ?
'.' : $path;
1161 foreach my $prop (sort keys %{$h->{$path}}) {
1162 next if $SKIP_PROP{$prop};
1163 my $v = $h->{$path}->{$prop};
1166 uri_encode
($ppath), ' ',
1167 uri_encode
($prop), ' ',
1168 uri_encode
($v), "\n"
1172 uri_encode
($ppath), ' ',
1173 uri_encode
($prop), "\n"
1179 foreach my $t (qw
/absent_file absent_directory/) {
1180 $h = $untracked->{$t} or next;
1181 foreach my $parent (sort keys %$h) {
1182 foreach my $path (sort @
{$h->{$parent}}) {
1184 uri_encode
("$parent/$path"), "\n"
1186 warn "W: $t: $parent/$path ",
1187 "Insufficient permissions?\n";
1193 sub parse_svn_date
{
1194 my $date = shift || return '+0000 1970-01-01 00:00:00';
1195 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1196 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1197 croak
"Unable to parse date: $date\n";
1198 "+0000 $Y-$m-$d $H:$M:$S";
1203 if (!defined $author || length $author == 0) {
1204 $author = '(no author)';
1206 if (defined $::_authors
&& ! defined $::users
{$author}) {
1207 die "Author: $author not defined in $::_authors file\n";
1212 sub make_log_entry
{
1213 my ($self, $rev, $parents, $untracked) = @_;
1214 my $rp = $self->ra->rev_proplist($rev);
1215 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1216 revprops
=> $rp, log => '');
1217 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1218 $self->write_untracked($rev, $un, $untracked);
1219 foreach (sort keys %$rp) {
1221 if (/^svn:(author|date|log)$/) {
1222 $log_entry{$1} = $v;
1224 print $un " rev_prop: ", uri_encode
($_), ' ',
1225 uri_encode
($v), "\n";
1228 close $un or croak
$!;
1229 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1230 $log_entry{author
} = check_author
($log_entry{author
});
1231 $log_entry{log} .= "\n";
1236 my ($self, @parents) = @_;
1237 my ($last_rev, $last_commit) = $self->last_rev_commit;
1238 my ($base, $head) = $self->parse_revision($last_rev);
1239 return if ($base > $head);
1240 if (defined $last_commit) {
1241 $self->assert_index_clean($last_commit);
1244 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
1245 my $err_handler = $SVN::Error
::handler
;
1246 $SVN::Error
::handler
= \
&skip_unknown_revs
;
1249 $self->ra->get_log([''], $min, $max, 0, 1, 1, sub {
1250 my ($paths, $rev, $author, $date, $log) = @_;
1251 push @revs, $rev });
1253 my $log_entry = $self->do_fetch(undef, $_);
1254 $self->do_git_commit($log_entry, @parents);
1256 last if $max >= $head;
1259 $max = $head if ($max > $head);
1261 $SVN::Error
::handler
= $err_handler;
1265 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1266 # TODO: enable and test optimized commits:
1267 if (0 && $rev == ($self->{last_rev
} + 1)) {
1268 $log_entry->{revision
} = $rev;
1269 $log_entry->{author
} = $author;
1270 $self->do_git_commit($log_entry, "$rev=$tree");
1272 $self->fetch("$rev=$tree");
1277 my ($self, $tree) = (shift, shift);
1278 my $log_entry = ::get_commit_entry
($tree);
1279 unless ($self->{last_rev
}) {
1280 fatal
("Must have an existing revision to commit\n");
1282 my $pool = SVN
::Pool
->new;
1283 my $ed = SVN
::Git
::Editor
->new({ r
=> $self->{last_rev
},
1284 ra
=> $self->ra->dup,
1285 svn_path
=> $self->ra->{svn_path
}
1287 $self->ra->get_commit_editor(
1288 $log_entry->{log}, sub {
1289 $self->set_tree_cb($log_entry,
1293 my $mods = $ed->apply_diff($self->{last_commit
}, $tree);
1295 print "No changes\nr$self->{last_rev} = $tree\n";
1300 sub skip_unknown_revs
{
1302 my $errno = $err->apr_err();
1303 # Maybe the branch we're tracking didn't
1304 # exist when the repo started, so it's
1305 # not an error if it doesn't, just continue
1307 # Wonderfully consistent library, eh?
1308 # 160013 - svn:// and file://
1309 # 175002 - http(s)://
1310 # 175007 - http(s):// (this repo required authorization, too...)
1311 # More codes may be discovered later...
1312 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
1315 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
1319 # Tie::File seems to be prone to offset errors if revisions get sparse,
1320 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1321 # one of my favorite modules is out :< Next up would be one of the DBM
1322 # modules, but I'm not sure which is most portable... So I'll just
1323 # go with something that's plain-text, but still capable of
1324 # being randomly accessed. So here's my ultra-simple fixed-width
1325 # database. All records are 40 characters + "\n", so it's easy to seek
1326 # to a revision: (41 * rev) is the byte offset.
1327 # A record of 40 0s denotes an empty revision.
1328 # And yes, it's still pretty fast (faster than Tie::File).
1331 my ($self, $rev, $commit) = @_;
1332 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
1333 open my $fh, '+<', $self->{db_path
} or croak
$!;
1334 my $offset = $rev * 41;
1335 # assume that append is the common case:
1336 seek $fh, 0, 2 or croak
$!;
1338 if ($pos < $offset) {
1339 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41)
1342 seek $fh, $offset, 0 or croak
$!;
1343 print $fh $commit,"\n" or croak
$!;
1344 close $fh or croak
$!;
1348 my ($self, $rev) = @_;
1350 my $offset = $rev * 41;
1351 open my $fh, '<', $self->{db_path
} or croak
$!;
1352 if (seek $fh, $offset, 0) {
1353 $ret = readline $fh;
1356 $ret = undef if ($ret =~ /^0{40}$/);
1359 close $fh or croak
$!;
1364 my ($class, $id) = @_;
1365 $id ||= $Git::SVN
::default;
1366 my $dir = "$ENV{GIT_DIR}/svn/$id";
1367 bless { id
=> $id, dir
=> $dir, index => "$dir/index",
1368 db_path
=> "$dir/.rev_db" }, $class;
1373 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1377 package Git
::SVN
::Prompt
;
1381 use vars qw
/$_no_auth_cache $_username/;
1384 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1385 $may_save = undef if $_no_auth_cache;
1386 $default_username = $_username if defined $_username;
1387 if (defined $default_username && length $default_username) {
1388 if (defined $realm && length $realm) {
1389 print STDERR
"Authentication realm: $realm\n";
1392 $cred->username($default_username);
1394 username
($cred, $realm, $may_save, $pool);
1396 $cred->password(_read_password
("Password for '" .
1397 $cred->username . "': ", $realm));
1398 $cred->may_save($may_save);
1399 $SVN::_Core
::SVN_NO_ERROR
;
1402 sub ssl_server_trust
{
1403 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1404 $may_save = undef if $_no_auth_cache;
1405 print STDERR
"Error validating server certificate for '$realm':\n";
1406 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
1407 print STDERR
" - The certificate is not issued by a trusted ",
1408 "authority. Use the\n",
1409 " fingerprint to validate the certificate manually!\n";
1411 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
1412 print STDERR
" - The certificate hostname does not match.\n";
1414 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
1415 print STDERR
" - The certificate is not yet valid.\n";
1417 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
1418 print STDERR
" - The certificate has expired.\n";
1420 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
1421 print STDERR
" - The certificate has an unknown error.\n";
1424 "Certificate information:\n".
1425 " - Hostname: %s\n".
1426 " - Valid: from %s until %s\n".
1428 " - Fingerprint: %s\n",
1429 map $cert_info->$_, qw(hostname valid_from valid_until
1430 issuer_dname fingerprint);
1433 print STDERR
$may_save ?
1434 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1435 "(R)eject or accept (t)emporarily? ";
1437 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
1438 if ($choice =~ /^t$/i) {
1439 $cred->may_save(undef);
1440 } elsif ($choice =~ /^r$/i) {
1442 } elsif ($may_save && $choice =~ /^p$/i) {
1443 $cred->may_save($may_save);
1447 $cred->accepted_failures($failures);
1448 $SVN::_Core
::SVN_NO_ERROR
;
1451 sub ssl_client_cert
{
1452 my ($cred, $realm, $may_save, $pool) = @_;
1453 $may_save = undef if $_no_auth_cache;
1454 print STDERR
"Client certificate filename: ";
1456 chomp(my $filename = <STDIN
>);
1457 $cred->cert_file($filename);
1458 $cred->may_save($may_save);
1459 $SVN::_Core
::SVN_NO_ERROR
;
1462 sub ssl_client_cert_pw
{
1463 my ($cred, $realm, $may_save, $pool) = @_;
1464 $may_save = undef if $_no_auth_cache;
1465 $cred->password(_read_password
("Password: ", $realm));
1466 $cred->may_save($may_save);
1467 $SVN::_Core
::SVN_NO_ERROR
;
1471 my ($cred, $realm, $may_save, $pool) = @_;
1472 $may_save = undef if $_no_auth_cache;
1473 if (defined $realm && length $realm) {
1474 print STDERR
"Authentication realm: $realm\n";
1477 if (defined $_username) {
1478 $username = $_username;
1480 print STDERR
"Username: ";
1482 chomp($username = <STDIN
>);
1484 $cred->username($username);
1485 $cred->may_save($may_save);
1486 $SVN::_Core
::SVN_NO_ERROR
;
1489 sub _read_password
{
1490 my ($prompt, $realm) = @_;
1491 print STDERR
$prompt;
1493 require Term
::ReadKey
;
1494 Term
::ReadKey
::ReadMode
('noecho');
1496 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
1497 last if $key =~ /[\012\015]/; # \n\r
1500 Term
::ReadKey
::ReadMode
('restore');
1510 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1517 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
1522 my ($path, $r0, $r1) = @_;
1523 return 1 if $r0 == $r1;
1525 # should be OK to use Pool here (r1 - r0) should be small
1526 $SVN->get_log([$path], $r0, $r1, 0, 0, 1, sub {$nr++});
1527 return 0 if ($nr > 1);
1531 sub libsvn_find_parent_branch
{
1532 my ($paths, $rev, $author, $date, $log) = @_;
1533 my $svn_path = '/'.$SVN->{svn_path
};
1535 # look for a parent from another branch:
1536 my $i = $paths->{$svn_path} or return;
1537 my $branch_from = $i->copyfrom_path or return;
1538 my $r = $i->copyfrom_rev;
1539 print STDERR
"Found possible branch point: ",
1540 "$branch_from => $svn_path, $r\n";
1541 $branch_from =~ s
#^/##;
1543 read_url_paths_all
($l_map, '', "$GIT_DIR/svn");
1544 my $url = $SVN->{repos_root
};
1545 defined $l_map->{$url} or return;
1546 my $id = $l_map->{$url}->{$branch_from};
1547 if (!defined $id && $_follow_parent) {
1548 print STDERR
"Following parent: $branch_from\@$r\n";
1549 # auto create a new branch and follow it
1550 $id = basename
($branch_from);
1551 $id .= '@'.$r if -r
"$GIT_DIR/svn/$id";
1552 while (-r
"$GIT_DIR/svn/$id") {
1553 # just grow a tail if we're not unique enough :x
1557 return unless defined $id;
1559 my ($r0, $parent) = find_rev_before
($r,$id,1);
1560 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
1561 defined(my $pid = fork) or croak
$!;
1563 $GIT_SVN = $ENV{GIT_SVN_ID
} = $id;
1565 $SVN_URL = "$url/$branch_from";
1568 # we can't assume SVN_URL exists at r+1:
1569 $_revision = "0:$r";
1575 ($r0, $parent) = find_rev_before
($r,$id,1);
1577 return unless (defined $r0 && defined $parent);
1578 if (revisions_eq
($branch_from, $r0, $r)) {
1579 unlink $GIT_SVN_INDEX;
1580 print STDERR
"Found branch parent: ($GIT_SVN) $parent\n";
1581 command_noisy
('read-tree', $parent);
1582 unless ($SVN->can_do_switch) {
1583 return _libsvn_new_tree
($paths, $rev, $author, $date,
1586 # do_switch works with svn/trunk >= r22312, but that is not
1587 # included with SVN 1.4.2 (the latest version at the moment),
1588 # so we can't rely on it.
1589 my $ra = Git
::SVN
::Ra
->new("$url/$branch_from");
1590 my $ed = SVN
::Git
::Fetcher
->new({c
=> $parent, q
=> $_q });
1591 $ra->gs_do_switch($r0, $rev, '', 1, $SVN->{url
}, $ed) or
1592 die "SVN connection failed somewhere...\n";
1593 return libsvn_log_entry
($rev, $author, $date, $log, [$parent]);
1595 print STDERR
"Nope, branch point not imported or unknown\n";
1599 sub _libsvn_new_tree
{
1600 my ($paths, $rev, $author, $date, $log, $parents) = @_;
1601 my $ed = SVN
::Git
::Fetcher
->new({q
=> $_q});
1602 unless ($SVN->gs_do_update($rev, $rev, '', 1, $ed)) {
1603 die "SVN connection failed somewhere...\n";
1605 libsvn_log_entry
($rev, $author, $date, $log, $parents, $ed);
1609 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
1610 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1611 $SVN::Node
::none
.$SVN::Node
::file
.
1612 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1613 $SVN::Auth
::SSL
::CNMISMATCH
.
1614 $SVN::Auth
::SSL
::NOTYETVALID
.
1615 $SVN::Auth
::SSL
::EXPIRED
.
1616 $SVN::Auth
::SSL
::UNKNOWNCA
.
1617 $SVN::Auth
::SSL
::OTHER
;
1620 package SVN
::Git
::Fetcher
;
1627 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
1629 my ($class, $git_svn) = @_;
1630 my $self = SVN
::Delta
::Editor
->new;
1631 bless $self, $class;
1632 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
1633 $self->{empty
} = {};
1634 $self->{dir_prop
} = {};
1635 $self->{file_prop
} = {};
1636 $self->{absent_dir
} = {};
1637 $self->{absent_file
} = {};
1638 ($self->{gui
}, $self->{ctx
}) = $git_svn->tmp_index_do(
1639 sub { command_input_pipe
(qw
/update-index -z --index-info/) } );
1640 require Digest
::MD5
;
1648 sub open_directory
{
1649 my ($self, $path, $pb, $rev) = @_;
1654 my ($self, $path, $rev, $pb) = @_;
1655 my $gui = $self->{gui
};
1657 # remove entire directories.
1658 if (command
('ls-tree', $self->{c
}, '--', $path) =~ /^040000 tree/) {
1659 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
1661 $self->{c
}, '--', $path);
1664 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
1665 print "\tD\t$_\n" unless $self->{q
};
1667 print "\tD\t$path/\n" unless $self->{q
};
1668 command_close_pipe
($ls, $ctx);
1669 $self->{empty
}->{$path} = 0
1671 print $gui '0 ',0 x
40,"\t",$path,"\0" or croak
$!;
1672 print "\tD\t$path\n" unless $self->{q
};
1678 my ($self, $path, $pb, $rev) = @_;
1679 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--',$path)
1680 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
1681 unless (defined $mode && defined $blob) {
1682 die "$path was not found in commit $self->{c} (r$rev)\n";
1684 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
1685 pool
=> SVN
::Pool
->new, action
=> 'M' };
1689 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
1690 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1691 delete $self->{empty
}->{$dir};
1692 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
1693 pool
=> SVN
::Pool
->new, action
=> 'A' };
1697 my ($self, $path, $cp_path, $cp_rev) = @_;
1698 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1699 delete $self->{empty
}->{$dir};
1700 $self->{empty
}->{$path} = 1;
1704 sub change_dir_prop
{
1705 my ($self, $db, $prop, $value) = @_;
1706 $self->{dir_prop
}->{$db->{path
}} ||= {};
1707 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
1711 sub absent_directory
{
1712 my ($self, $path, $pb) = @_;
1713 $self->{absent_dir
}->{$pb->{path
}} ||= [];
1714 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
1719 my ($self, $path, $pb) = @_;
1720 $self->{absent_file
}->{$pb->{path
}} ||= [];
1721 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
1725 sub change_file_prop
{
1726 my ($self, $fb, $prop, $value) = @_;
1727 if ($prop eq 'svn:executable') {
1728 if ($fb->{mode_b
} != 120000) {
1729 $fb->{mode_b
} = defined $value ?
100755 : 100644;
1731 } elsif ($prop eq 'svn:special') {
1732 $fb->{mode_b
} = defined $value ?
120000 : 100644;
1734 $self->{file_prop
}->{$fb->{path
}} ||= {};
1735 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
1740 sub apply_textdelta
{
1741 my ($self, $fb, $exp) = @_;
1742 my $fh = IO
::File
->new_tmpfile;
1744 # $fh gets auto-closed() by SVN::TxDelta::apply(),
1745 # (but $base does not,) so dup() it for reading in close_file
1746 open my $dup, '<&', $fh or croak
$!;
1747 my $base = IO
::File
->new_tmpfile;
1748 $base->autoflush(1);
1750 defined (my $pid = fork) or croak
$!;
1752 open STDOUT
, '>&', $base or croak
$!;
1753 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
1754 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
1760 seek $base, 0, 0 or croak
$!;
1761 my $md5 = Digest
::MD5
->new;
1762 $md5->addfile($base);
1763 my $got = $md5->hexdigest;
1764 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
1766 " got: $got\n" if ($got ne $exp);
1769 seek $base, 0, 0 or croak
$!;
1771 $fb->{base
} = $base;
1772 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
1776 my ($self, $fb, $exp) = @_;
1778 my $path = $fb->{path
};
1779 if (my $fh = $fb->{fh
}) {
1780 seek($fh, 0, 0) or croak
$!;
1781 my $md5 = Digest
::MD5
->new;
1783 my $got = $md5->hexdigest;
1784 die "Checksum mismatch: $path\n",
1785 "expected: $exp\n got: $got\n" if ($got ne $exp);
1786 seek($fh, 0, 0) or croak
$!;
1787 if ($fb->{mode_b
} == 120000) {
1788 read($fh, my $buf, 5) == 5 or croak
$!;
1789 $buf eq 'link ' or die "$path has mode 120000",
1790 "but is not a link\n";
1792 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
1794 open STDIN
, '<&', $fh or croak
$!;
1795 exec qw
/git-hash-object -w --stdin/ or croak
$!;
1797 chomp($hash = do { local $/; <$out> });
1798 close $out or croak
$!;
1799 close $fh or croak
$!;
1800 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
1801 close $fb->{base
} or croak
$!;
1803 $hash = $fb->{blob
} or die "no blob information\n";
1806 my $gui = $self->{gui
};
1807 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
1808 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $self->{q
};
1814 eval { command_close_pipe
($self->{gui
}, $self->{ctx
}) };
1815 $self->SUPER::abort_edit
(@_);
1820 command_close_pipe
($self->{gui
}, $self->{ctx
});
1821 $self->{git_commit_ok
} = 1;
1822 $self->SUPER::close_edit
(@_);
1825 package SVN
::Git
::Editor
;
1834 my $git_svn = shift;
1835 my $self = SVN
::Delta
::Editor
->new(@_);
1836 bless $self, $class;
1837 foreach (qw
/svn_path r ra/) {
1838 die "$_ required!\n" unless (defined $git_svn->{$_});
1839 $self->{$_} = $git_svn->{$_};
1841 $self->{pool
} = SVN
::Pool
->new;
1842 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
1844 require Digest
::MD5
;
1849 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
1853 (defined $_[1] && length $_[1]) ?
$_[1] : ''
1857 my ($self, $path) = @_;
1858 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
1862 my ($self, $tree_b) = @_;
1863 my $rm = $self->{rm
};
1864 delete $rm->{''}; # we never delete the url we're tracking
1867 foreach (keys %$rm) {
1868 my @d = split m
#/#, $_;
1872 $c .= '/' . shift @d;
1876 delete $rm->{$self->{svn_path
}};
1877 delete $rm->{''}; # we never delete the url we're tracking
1880 my ($fh, $ctx) = command_output_pipe
(
1881 qw
/ls-tree --name-only -r -z/, $tree_b);
1885 my @dn = split m
#/#, $_;
1887 delete $rm->{join '/', @dn};
1894 command_close_pipe
($fh, $ctx);
1896 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
1897 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
1898 $self->close_directory($bat->{$d}, $p);
1899 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
1900 print "\tD+\t$d/\n" unless $::_q
;
1901 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
1906 sub open_or_add_dir
{
1907 my ($self, $full_path, $baton) = @_;
1908 my $t = $self->{ra
}->check_path($full_path, $self->{r
});
1909 if ($t == $SVN::Node
::none
) {
1910 return $self->add_directory($full_path, $baton,
1911 undef, -1, $self->{pool
});
1912 } elsif ($t == $SVN::Node
::dir
) {
1913 return $self->open_directory($full_path, $baton,
1914 $self->{r
}, $self->{pool
});
1916 print STDERR
"$full_path already exists in repository at ",
1917 "r$self->{r} and it is not a directory (",
1918 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
1923 my ($self, $path) = @_;
1924 my $bat = $self->{bat
};
1925 $path = $self->repo_path($path);
1926 return $bat->{''} unless (length $path);
1927 my @p = split m
#/+#, $path;
1929 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
1932 $c .= '/' . shift @p;
1933 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
1939 my ($self, $m) = @_;
1940 my ($dir, $file) = split_path
($m->{file_b
});
1941 my $pbat = $self->ensure_path($dir);
1942 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1944 print "\tA\t$m->{file_b}\n" unless $::_q
;
1945 $self->chg_file($fbat, $m);
1946 $self->close_file($fbat,undef,$self->{pool
});
1950 my ($self, $m) = @_;
1951 my ($dir, $file) = split_path
($m->{file_b
});
1952 my $pbat = $self->ensure_path($dir);
1953 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1954 $self->url_path($m->{file_a
}), $self->{r
});
1955 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1956 $self->chg_file($fbat, $m);
1957 $self->close_file($fbat,undef,$self->{pool
});
1961 my ($self, $path, $pbat) = @_;
1962 my $rpath = $self->repo_path($path);
1963 my ($dir, $file) = split_path
($rpath);
1964 $self->{rm
}->{$dir} = 1;
1965 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
1969 my ($self, $m) = @_;
1970 my ($dir, $file) = split_path
($m->{file_b
});
1971 my $pbat = $self->ensure_path($dir);
1972 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1973 $self->url_path($m->{file_a
}), $self->{r
});
1974 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1975 $self->chg_file($fbat, $m);
1976 $self->close_file($fbat,undef,$self->{pool
});
1978 ($dir, $file) = split_path
($m->{file_a
});
1979 $pbat = $self->ensure_path($dir);
1980 $self->delete_entry($m->{file_a
}, $pbat);
1984 my ($self, $m) = @_;
1985 my ($dir, $file) = split_path
($m->{file_b
});
1986 my $pbat = $self->ensure_path($dir);
1987 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
1988 $pbat,$self->{r
},$self->{pool
});
1989 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
1990 $self->chg_file($fbat, $m);
1991 $self->close_file($fbat,undef,$self->{pool
});
1994 sub T
{ shift->M(@_) }
1996 sub change_file_prop
{
1997 my ($self, $fbat, $pname, $pval) = @_;
1998 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
2002 my ($self, $fbat, $m) = @_;
2003 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
2004 $self->change_file_prop($fbat,'svn:executable','*');
2005 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
2006 $self->change_file_prop($fbat,'svn:executable',undef);
2008 my $fh = IO
::File
->new_tmpfile or croak
$!;
2009 if ($m->{mode_b
} =~ /^120/) {
2010 print $fh 'link ' or croak
$!;
2011 $self->change_file_prop($fbat,'svn:special','*');
2012 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
2013 $self->change_file_prop($fbat,'svn:special',undef);
2015 defined(my $pid = fork) or croak
$!;
2017 open STDOUT
, '>&', $fh or croak
$!;
2018 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
2022 $fh->flush == 0 or croak
$!;
2023 seek $fh, 0, 0 or croak
$!;
2025 my $md5 = Digest
::MD5
->new;
2026 $md5->addfile($fh) or croak
$!;
2027 seek $fh, 0, 0 or croak
$!;
2029 my $exp = $md5->hexdigest;
2030 my $pool = SVN
::Pool
->new;
2031 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2032 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
2033 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2036 close $fh or croak
$!;
2040 my ($self, $m) = @_;
2041 my ($dir, $file) = split_path
($m->{file_b
});
2042 my $pbat = $self->ensure_path($dir);
2043 print "\tD\t$m->{file_b}\n" unless $::_q
;
2044 $self->delete_entry($m->{file_b
}, $pbat);
2049 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
2050 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2051 $self->close_directory($bat->{$_}, $p);
2053 $self->SUPER::close_edit
($p);
2059 $self->SUPER::abort_edit
($self->{pool
});
2060 $self->{pool
}->clear;
2063 # this drives the editor
2065 my ($self, $tree_a, $tree_b) = @_;
2066 my @diff_tree = qw(diff-tree -z -r);
2067 if ($::_cp_similarity
) {
2068 push @diff_tree, "-C$::_cp_similarity";
2070 push @diff_tree, '-C';
2072 push @diff_tree, '--find-copies-harder' if $::_find_copies_harder
;
2073 push @diff_tree, "-l$::_l" if defined $::_l
;
2074 push @diff_tree, $tree_a, $tree_b;
2075 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2080 while (<$diff_fh>) {
2081 chomp $_; # this gets rid of the trailing "\0"
2082 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2083 $::sha1\s
($::sha1
)\s
2084 ([MTCRAD
])\d
*$/xo
) {
2085 push @mods, { mode_a
=> $1, mode_b
=> $2,
2086 sha1_b
=> $3, chg
=> $4 };
2087 if ($4 =~ /^(?:C|R)$/) {
2092 } elsif ($state eq 'file_a') {
2093 my $x = $mods[$#mods] or croak
"Empty array\n";
2094 if ($x->{chg
} !~ /^(?:C|R)$/) {
2095 croak
"Error parsing $_, $x->{chg}\n";
2099 } elsif ($state eq 'file_b') {
2100 my $x = $mods[$#mods] or croak
"Empty array\n";
2101 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2102 croak
"Error parsing $_, $x->{chg}\n";
2104 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2105 croak
"Error parsing $_, $x->{chg}\n";
2110 croak
"Error parsing $_\n";
2113 command_close_pipe
($diff_fh, $ctx);
2116 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2117 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @mods) {
2119 if (defined $o{$f}) {
2122 fatal
("Invalid change type: $f\n");
2125 $self->rmdirs($tree_b) if $::_rmdir
;
2134 package Git
::SVN
::Ra
;
2135 use vars qw
/@ISA $config_dir/;
2138 my ($can_do_switch);
2141 # enforce temporary pool usage for some simple functions
2143 foreach (qw
/get_latest_revnum rev_proplist get_file
2144 check_path get_dir get_uuid get_repos_root
/) {
2147 my \$pool = SVN::Pool->new;
2148 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2150 wantarray ? \@ret : \$ret[0]; }\n";
2156 my ($class, $url) = @_;
2157 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2158 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2159 SVN
::Client
::get_simple_provider
(),
2160 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2161 SVN
::Client
::get_simple_prompt_provider
(
2162 \
&Git
::SVN
::Prompt
::simple
, 2),
2163 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2164 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2165 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2166 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2167 SVN
::Client
::get_username_provider
(),
2168 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2169 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2170 SVN
::Client
::get_username_prompt_provider
(
2171 \
&Git
::SVN
::Prompt
::username
, 2),
2173 my $config = SVN
::Core
::config_get_config
($config_dir);
2174 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2176 pool
=> SVN
::Pool
->new,
2177 auth_provider_callbacks
=> $callbacks);
2178 $self->{svn_path
} = $url;
2179 $self->{repos_root
} = $self->get_repos_root;
2180 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E/*##;
2181 bless $self, $class;
2186 $self->{pool
}->clear if $self->{pool
};
2187 $self->SUPER::DESTROY
(@_);
2192 my $dup = SVN
::Ra
->new(pool
=> SVN
::Pool
->new,
2193 map { $_ => $self->{$_} } qw
/config url
2194 auth auth_provider_callbacks repos_root svn_path
/);
2195 bless $dup, ref $self;
2199 my ($self, @args) = @_;
2200 my $pool = SVN
::Pool
->new;
2201 $args[4]-- if $args[4] && ! $::_follow_parent
;
2202 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2203 my $ret = $self->SUPER::get_log
(@args, $pool);
2208 sub get_commit_editor
{
2209 my ($self, $log, $cb, $pool) = @_;
2210 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2211 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2216 $self->{uuid
} ||= $self->get_uuid;
2220 my ($self, $rev_a, $rev_b, $path, $recurse, $editor) = @_;
2221 my $pool = SVN
::Pool
->new;
2222 my $reporter = $self->do_update($rev_b, $path, $recurse,
2224 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2225 my $new = ($rev_a == $rev_b);
2226 $reporter->set_path($path, $rev_a, $new, @lock, $pool);
2227 $reporter->finish_report($pool);
2229 $editor->{git_commit_ok
};
2233 my ($self, $rev_a, $rev_b, $path, $recurse, $url_b, $editor) = @_;
2234 my $pool = SVN
::Pool
->new;
2235 my $reporter = $self->do_switch($rev_b, $path, $recurse,
2236 $url_b, $editor, $pool);
2237 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2238 $reporter->set_path($path, $rev_a, 0, @lock, $pool);
2239 $reporter->finish_report($pool);
2241 $editor->{git_commit_ok
};
2246 unless (defined $can_do_switch) {
2247 my $pool = SVN
::Pool
->new;
2249 $self->do_switch(1, '', 0, $self->{url
},
2250 SVN
::Delta
::Editor
->new, $pool);
2255 $rep->abort_report($pool);
2263 package Git
::SVN
::Log
;
2266 use POSIX qw
/strftime/;
2267 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
2268 %rusers $show_commit $incremental/;
2273 return 1 if defined $c->{r
};
2274 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
2275 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
2276 my @log = command
(qw
/cat-file commit/, $c->{c
});
2277 shift @log while ($log[0] ne "\n");
2279 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
2281 (undef, $c->{r
}, undef) = ::extract_metadata
(
2282 (grep(/^git-svn-id: /, @log))[-1]);
2284 return defined $c->{r
};
2290 $dcvar = 'color.diff';
2291 $dc = `git-config --get $dcvar`;
2293 # nothing at all; fallback to "diff.color"
2294 $dcvar = 'diff.color';
2295 $dc = `git-config --get $dcvar`;
2298 if ($dc eq 'auto') {
2300 $pc = `git-config --get color.pager`;
2302 # does not have it -- fallback to pager.color
2303 $pc = `git-config --bool --get pager.color`;
2306 $pc = `git-config --bool --get color.pager`;
2312 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
2313 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
2317 return 0 if $dc eq 'never';
2318 return 1 if $dc eq 'always';
2319 chomp($dc = `git-config --bool --get $dcvar`);
2320 return ($dc eq 'true');
2323 sub git_svn_log_cmd
{
2324 my ($r_min, $r_max) = @_;
2325 my $gs = Git
::SVN
->_new;
2326 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
2328 push @cmd, '-r' unless $non_recursive;
2329 push @cmd, qw
/--raw --name-status/ if $verbose;
2330 push @cmd, '--color' if log_use_color
();
2331 return @cmd unless defined $r_max;
2332 if ($r_max == $r_min) {
2333 push @cmd, '--max-count=1';
2334 if (my $c = $gs->rev_db_get($r_max)) {
2338 my ($c_min, $c_max);
2339 $c_max = $gs->rev_db_get($r_max);
2340 $c_min = $gs->rev_db_get($r_min);
2341 if (defined $c_min && defined $c_max) {
2342 if ($r_max > $r_max) {
2343 push @cmd, "$c_min..$c_max";
2345 push @cmd, "$c_max..$c_min";
2347 } elsif ($r_max > $r_min) {
2356 # adapted from pager.c
2358 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
2359 if (!defined $pager) {
2361 } elsif (length $pager == 0 || $pager eq 'cat') {
2367 return unless -t
*STDOUT
;
2368 pipe my $rfd, my $wfd or return;
2369 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
2371 open STDOUT
, '>&', $wfd or
2372 ::fatal
"Can't redirect to stdout: $!\n";
2375 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
2376 $ENV{LESS
} ||= 'FRSX';
2377 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
2380 sub get_author_info
{
2381 my ($dest, $author, $t, $tz) = @_;
2382 $author =~ s/(?:^\s*|\s*$)//g;
2383 $dest->{a_raw
} = $author;
2386 $au = $rusers{$author} || undef;
2389 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
2394 # Date::Parse isn't in the standard Perl distro :(
2395 if ($tz =~ s/^\+//) {
2396 $t += ::tz_to_s_offset
($tz);
2397 } elsif ($tz =~ s/^\-//) {
2398 $t -= ::tz_to_s_offset
($tz);
2400 $dest->{t_utc
} = $t;
2403 sub process_commit
{
2404 my ($c, $r_min, $r_max, $defer) = @_;
2405 if (defined $r_min && defined $r_max) {
2406 if ($r_min == $c->{r
} && $r_min == $r_max) {
2410 return 1 if $r_min == $r_max;
2411 if ($r_min < $r_max) {
2412 # we need to reverse the print order
2413 return 0 if (defined $limit && --$limit < 0);
2417 if ($r_min != $r_max) {
2418 return 1 if ($r_min < $c->{r
});
2419 return 1 if ($r_max > $c->{r
});
2422 return 0 if (defined $limit && --$limit < 0);
2431 if (my $l = $c->{l
}) {
2432 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2435 $l_fmt ||= 'A' . length($c->{r
});
2436 print 'r',pack($l_fmt, $c->{r
}),' | ';
2437 print "$c->{c} | " if $show_commit;
2440 show_commit_normal
($c);
2444 sub show_commit_changed_paths
{
2446 return unless $c->{changed
};
2447 print "Changed paths:\n", @
{$c->{changed
}};
2450 sub show_commit_normal
{
2452 print '-' x72
, "\nr$c->{r} | ";
2453 print "$c->{c} | " if $show_commit;
2454 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2455 localtime($c->{t_utc
})), ' | ';
2458 if (my $l = $c->{l
}) {
2459 while ($l->[$#$l] eq "\n" && $#$l > 0
2460 && $l->[($#$l - 1)] eq "\n") {
2463 $nr_line = scalar @
$l;
2465 print "1 line\n\n\n";
2467 if ($nr_line == 1) {
2468 $nr_line = '1 line';
2470 $nr_line .= ' lines';
2472 print $nr_line, "\n";
2473 show_commit_changed_paths
($c);
2475 print $_ foreach @
$l;
2479 show_commit_changed_paths
($c);
2483 foreach my $x (qw
/raw diff/) {
2486 print $_ foreach @
{$c->{$x}}
2493 my ($r_min, $r_max);
2494 my $r_last = -1; # prevent dupes
2500 if (defined $::_revision
) {
2501 if ($::_revision
=~ /^(\d+):(\d+)$/) {
2502 ($r_min, $r_max) = ($1, $2);
2503 } elsif ($::_revision
=~ /^\d+$/) {
2504 $r_min = $r_max = $::_revision
;
2506 ::fatal
"-r$::_revision is not supported, use ",
2507 "standard \'git log\' arguments instead\n";
2512 @args = (git_svn_log_cmd
($r_min, $r_max), @args);
2513 my $log = command_output_pipe
(@args);
2516 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
2518 if (/^${esc_color}commit ($::sha1_short)/o) {
2520 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
2522 process_commit
($c, $r_min, $r_max, \
@k) or
2527 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
2528 get_author_info
($c, $1, $2, $3);
2529 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
2531 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
2532 push @
{$c->{raw
}}, $_;
2533 } elsif (/^${esc_color}[ACRMDT]\t/) {
2534 # we could add $SVN->{svn_path} here, but that requires
2535 # remote access at the moment (repo_path_split)...
2536 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
2537 push @
{$c->{changed
}}, $_;
2538 } elsif (/^${esc_color}diff /o) {
2540 push @
{$c->{diff
}}, $_;
2542 push @
{$c->{diff
}}, $_;
2543 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
2544 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
2545 } elsif (s/^${esc_color} //o) {
2546 push @
{$c->{l
}}, $_;
2549 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
2551 process_commit
($c, $r_min, $r_max, \
@k);
2557 process_commit
($_, $r_min, $r_max) foreach reverse @k;
2561 print '-' x72
,"\n" unless $incremental || $oneline;
2568 $log_entry hashref as returned by libsvn_log_entry
()
2570 log => 'whitespace-formatted log entry
2571 ', # trailing newline is preserved
2572 revision
=> '8', # integer
2573 date
=> '2004-02-24T17:01:44.108345Z', # commit date
2574 author
=> 'committer name'
2577 @mods = array of diff
-index line hashes
, each element represents one line
2578 of diff
-index output
2580 diff
-index line
($m hash
)
2582 mode_a
=> first column of diff
-index output
, no leading
':',
2583 mode_b
=> second column of diff
-index output
,
2584 sha1_b
=> sha1sum of the final blob
,
2585 chg
=> change type
[MCRADT
],
2586 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
2587 file_b
=> new
/current file name of a file
(any chg
)
2591 # retval of read_url_paths{,_all}();
2593 # repository root url
2594 'https://svn.musicpd.org' => {
2595 # repository path # GIT_SVN_ID
2596 'mpd/trunk' => 'trunk',
2597 'mpd/tags/0.11.5' => 'tags/0.11.5',
2602 I don
't trust the each() function on unless I created %hash myself
2603 because the internal iterator may not have started at base.