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@@';
16 $ENV{GIT_DIR
} ||= '.git';
17 $Git::SVN
::default_repo_id
= 'git-svn';
18 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
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::Migration::$_ = ".
51 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
58 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS
};
59 $sha1 = qr/[a-f\d]{40}/;
60 $sha1_short = qr/[a-f\d]{4,40}/;
61 my ($_stdin, $_help, $_edit,
62 $_repack, $_repack_nr, $_repack_flags,
63 $_message, $_file, $_no_metadata,
66 $_merge, $_strategy, $_dry_run,
69 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
70 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
71 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
);
72 my %fc_opts = ( 'follow-parent|follow' => \
$_follow_parent,
73 'authors-file|A=s' => \
$_authors,
74 'repack:i' => \
$_repack,
75 'no-metadata' => \
$_no_metadata,
77 'repack-flags|repack-args|repack-opts=s' => \
$_repack_flags,
80 my ($_trunk, $_tags, $_branches);
81 my %multi_opts = ( 'trunk|T=s' => \
$_trunk,
82 'tags|t=s' => \
$_tags,
83 'branches|b=s' => \
$_branches );
84 my %init_opts = ( 'template=s' => \
$_template, 'shared' => \
$_shared );
85 my %cmt_opts = ( 'edit|e' => \
$_edit,
87 'find-copies-harder' => \
$_find_copies_harder,
89 'copy-similarity|C=i'=> \
$_cp_similarity
93 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
94 { 'revision|r=s' => \
$_revision, %fc_opts } ],
95 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
96 " (requires URL argument)",
98 dcommit
=> [ \
&cmd_dcommit
,
99 'Commit several diffs to merge with upstream',
100 { 'merge|m|M' => \
$_merge,
101 'strategy|s=s' => \
$_strategy,
102 'dry-run|n' => \
$_dry_run,
103 %cmt_opts, %fc_opts } ],
104 'set-tree' => [ \
&cmd_set_tree
,
105 "Set an SVN repository to a git tree-ish",
106 { 'stdin|' => \
$_stdin, %cmt_opts, %fc_opts, } ],
107 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
108 { 'revision|r=i' => \
$_revision } ],
109 rebuild
=> [ \
&cmd_rebuild
, "Rebuild git-svn metadata (after git clone)",
110 { 'copy-remote|remote=s' => \
$_cp_remote,
111 'upgrade' => \
$_upgrade } ],
112 'multi-init' => [ \
&cmd_multi_init
,
113 'Initialize multiple trees (like git-svnimport)',
114 { %multi_opts, %init_opts, %remote_opts,
115 'revision|r=i' => \
$_revision,
116 'prefix=s' => \
$_prefix,
118 'multi-fetch' => [ \
&cmd_multi_fetch
,
119 'Fetch multiple trees (like git-svnimport)',
121 'migrate' => [ sub { },
122 # no-op, we automatically run this anyways,
123 'Migrate configuration/metadata/layout from
124 previous versions of git-svn',
126 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
127 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
128 'revision|r=s' => \
$_revision,
129 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
130 'incremental' => \
$Git::SVN
::Log
::incremental
,
131 'oneline' => \
$Git::SVN
::Log
::oneline
,
132 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
133 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
134 'authors-file|A=s' => \
$_authors,
135 'color' => \
$Git::SVN
::Log
::color
,
136 'pager=s' => \
$Git::SVN
::Log
::pager
,
138 'commit-diff' => [ \
&cmd_commit_diff
,
139 'Commit a diff between two trees',
140 { 'message|m=s' => \
$_message,
141 'file|F=s' => \
$_file,
142 'revision|r=s' => \
$_revision,
147 for (my $i = 0; $i < @ARGV; $i++) {
148 if (defined $cmd{$ARGV[$i]}) {
155 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
157 read_repo_config
(\
%opts);
158 my $rv = GetOptions
(%opts, 'help|H|h' => \
$_help,
159 'version|V' => \
$_version,
160 'minimize-connections' =>
161 \
$Git::SVN
::Migration
::_minimize
,
162 'id|i=s' => \
$Git::SVN
::default_ref_id
);
163 exit 1 if (!$rv && $cmd ne 'log');
166 version
() if $_version;
167 usage
(1) unless defined $cmd;
168 load_authors
() if $_authors;
169 unless ($cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/) {
170 Git
::SVN
::Migration
::migration_check
();
172 $cmd{$cmd}->[0]->(@ARGV);
175 ####################### primary functions ######################
177 my $exit = shift || 0;
178 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
180 git
-svn
- bidirectional operations between a single Subversion tree
and git
181 Usage
: $0 <command
> [options
] [arguments
]\n
183 print $fd "Available commands:\n" unless $cmd;
185 foreach (sort keys %cmd) {
186 next if $cmd && $cmd ne $_;
187 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
188 foreach (keys %{$cmd{$_}->[2]}) {
189 # prints out arguments as they should be passed:
190 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
191 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
193 split /\|/,$_)," $x\n";
197 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
198 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
199 one git repository and want to keep them separate. See git-svn(1) for more
206 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
212 my $gs = $url ? Git::SVN->init($url)
213 : eval { Git::SVN->new };
214 $gs ||= Git::SVN->_new;
215 if (!verify_ref($gs->refname.'^0')) {
216 $gs->copy_remote_ref;
219 my ($rev_list, $ctx) = command_output_pipe("rev-list", $gs->refname);
222 while (<$rev_list>) {
225 fatal "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
226 my ($url, $rev, $uuid) = cmt_metadata($c);
228 # ignore merges (from set-tree)
229 next if (!defined $rev || !$uuid);
231 # if we merged or otherwise started elsewhere, this is
232 # how we break out of it
233 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
234 ($gs->{url} && $url && ($url ne $gs->{url}))) {
238 unless (defined $latest) {
239 if (!$gs->{url} && !$url) {
240 fatal "SVN repository location required\n";
242 $gs = Git::SVN->init($url);
245 $gs->rev_db_set($rev, $c);
246 print "r$rev = $c\n";
248 command_close_pipe($rev_list, $ctx);
252 unless (-d $ENV{GIT_DIR}) {
253 my @init_db = ('init
');
254 push @init_db, "--template=$_template" if defined $_template;
255 push @init_db, "--shared" if defined $_shared;
256 command_noisy(@init_db);
261 my $url = shift or die "SVN repository location required " .
262 "as a command-line argument\n";
263 if (my $repo_path = shift) {
264 unless (-d $repo_path) {
265 mkpath([$repo_path]);
267 chdir $repo_path or croak $!;
268 $ENV{GIT_DIR} = $repo_path . "/.git";
272 Git::SVN->init($url);
276 my $gs = Git::SVN->new;
278 if ($gs->{last_commit} && !verify_ref('refs
/heads/master
^0')) {
279 command_noisy(qw(update-ref refs/heads/master),
286 if ($_stdin || !@commits) {
287 print "Reading from stdin...\n";
290 if (/\b($sha1_short)\b/o) {
291 unshift @commits, $1;
296 foreach my $c (@commits) {
297 my @tmp = command
('rev-parse',$c);
298 if (scalar @tmp == 1) {
300 } elsif (scalar @tmp > 1) {
301 push @revs, reverse(command
('rev-list',@tmp));
303 fatal
"Failed to rev-parse $c\n";
306 my $gs = Git
::SVN
->new;
307 my ($r_last, $cmt_last) = $gs->last_rev_commit;
309 if ($r_last != $gs->{last_rev
}) {
310 fatal
"There are new revisions that were fetched ",
311 "and need to be merged (or acknowledged) ",
312 "before committing.\nlast rev: $r_last\n",
313 " current: $gs->{last_rev}\n";
315 $gs->set_tree($_) foreach @revs;
316 print "Done committing ",scalar @revs," revisions to SVN\n";
321 my $gs = Git
::SVN
->new;
323 my @refs = command
(qw
/rev-list --no-merges/, $gs->refname."..$head");
325 foreach my $d (reverse @refs) {
326 if (!verify_ref
("$d~1")) {
328 "has no parent commit, and therefore ",
329 "nothing to diff against.\n",
330 "You should be working from a repository ",
331 "originally created by git-svn\n";
333 unless (defined $last_rev) {
334 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
335 unless (defined $last_rev) {
336 fatal
"Unable to extract revision information ",
337 "from commit $d~1\n";
341 print "diff-tree $d~1 $d\n";
343 my $log = get_commit_entry
($d)->{log};
345 my $pool = SVN
::Pool
->new;
346 my %ed_opts = ( r
=> $last_rev,
348 svn_path
=> $ra->{svn_path
} );
349 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
350 $ra->get_commit_editor($log,
351 sub { print "Committed r$_[0]\n";
352 $last_rev = $_[0]; }),
354 my $mods = $ed->apply_diff("$d~1", $d);
356 print "No changes\n$d~1 == $d\n";
362 # we always want to rebase against the current HEAD, not any
363 # head that was passed to us
364 my @diff = command
('diff-tree', 'HEAD', $gs->refname, '--');
367 @finish = qw
/rebase/;
368 push @finish, qw
/--merge/ if $_merge;
369 push @finish, "--strategy=$_strategy" if $_strategy;
370 print STDERR
"W: HEAD and ", $gs->refname, " differ, ",
371 "using @finish:\n", "@diff";
373 print "No changes between current HEAD and ",
374 $gs->refname, "\nResetting to the latest ",
376 @finish = qw
/reset --mixed/;
378 command_noisy
(@finish, $gs->refname);
381 sub cmd_show_ignore
{
382 my $gs = Git
::SVN
->new;
383 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
384 $gs->traverse_ignore(\
*STDOUT
, '', $r);
389 unless (defined $_trunk || defined $_branches || defined $_tags) {
393 $_prefix = '' unless defined $_prefix;
394 $url =~ s
#/+$## if defined $url;
395 if (defined $_trunk) {
396 my $trunk_ref = $_prefix . 'trunk';
397 # try both old-style and new-style lookups:
398 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
400 my ($trunk_url, $trunk_path) =
401 complete_svn_url
($url, $_trunk);
402 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
406 return unless defined $_branches || defined $_tags;
407 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
408 complete_url_ls_init
($ra, $_branches, '--branches/-b', $_prefix);
409 complete_url_ls_init
($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
412 sub cmd_multi_fetch
{
414 foreach (command
(qw
/config -l/)) {
415 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
416 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
417 my ($repo_id, $path, $ref_id) = ($1, $2, $3);
418 push @gs, Git
::SVN
->new($ref_id, $repo_id, $path);
425 # this command is special because it requires no metadata
426 sub cmd_commit_diff
{
427 my ($ta, $tb, $url) = @_;
428 my $usage = "Usage: $0 commit-diff -r<revision> ".
429 "<tree-ish> <tree-ish> [<URL>]\n";
430 fatal
($usage) if (!defined $ta || !defined $tb);
432 my $gs = eval { Git
::SVN
->new };
434 fatal
("Needed URL or usable git-svn --id in ",
435 "the command-line\n", $usage);
439 unless (defined $_revision) {
440 fatal
("-r|--revision is a required argument\n", $usage);
442 if (defined $_message && defined $_file) {
443 fatal
("Both --message/-m and --file/-F specified ",
444 "for the commit message.\n",
445 "I have no idea what you mean\n");
447 if (defined $_file) {
448 $_message = file_to_s
($_file);
450 $_message ||= get_commit_entry
($tb)->{log};
452 my $ra ||= Git
::SVN
::Ra
->new($url);
455 $r = $ra->get_latest_revnum;
456 } elsif ($r !~ /^\d+$/) {
457 die "revision argument: $r not understood by git-svn\n";
459 my $pool = SVN
::Pool
->new;
460 my %ed_opts = ( r
=> $r,
462 svn_path
=> $ra->{svn_path
} );
463 my $ed = SVN
::Git
::Editor
->new(\
%ed_opts,
464 $ra->get_commit_editor($_message,
465 sub { print "Committed r$_[0]\n" }),
467 my $mods = $ed->apply_diff($ta, $tb);
469 print "No changes\n$ta == $tb\n";
474 ########################### utility functions #########################
476 sub complete_svn_url
{
477 my ($url, $path) = @_;
479 if ($path !~ m
#^[a-z\+]+://#) {
480 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
481 fatal
("E: '$path' is not a complete URL ",
482 "and a separate URL is not specified\n");
484 return ($url, $path);
489 sub complete_url_ls_init
{
490 my ($ra, $repo_path, $switch, $pfx) = @_;
491 unless ($repo_path) {
492 print STDERR
"W: $switch not specified\n";
495 $repo_path =~ s
#/+$##;
496 if ($repo_path =~ m
#^[a-z\+]+://#) {
497 $ra = Git
::SVN
::Ra
->new($repo_path);
500 $repo_path =~ s
#^/+##;
502 fatal
("E: '$repo_path' is not a complete URL ",
503 "and a separate URL is not specified\n");
506 my $r = defined $_revision ?
$_revision : $ra->get_latest_revnum;
507 my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r);
508 my $url = $ra->{url
};
509 foreach my $d (sort keys %$dirent) {
510 next if ($dirent->{$d}->kind != $SVN::Node
::dir
);
511 my $path = "$repo_path/$d";
513 my $gs = eval { Git
::SVN
->new($ref) };
514 # don't try to init already existing refs
516 print "init $url/$path => $ref\n";
517 Git
::SVN
->init($url, $path, undef, $ref);
524 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
528 sub get_tree_from_treeish
{
530 # $treeish can be a symbolic ref, too:
531 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
533 while ($type eq 'tag') {
534 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
536 if ($type eq 'commit') {
537 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
539 ($expected) = ($expected =~ /^tree ($sha1)$/o);
540 die "Unable to get tree from $treeish\n" unless $expected;
541 } elsif ($type eq 'tree') {
542 $expected = $treeish;
544 die "$treeish is a $type, expected tree, tag or commit\n";
549 sub get_commit_entry
{
550 my ($treeish) = shift;
551 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
552 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
553 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
554 open my $log_fh, '>', $commit_editmsg or croak
$!;
556 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
557 if ($type eq 'commit' || $type eq 'tag') {
558 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
563 $in_msg = 1 if (/^\s*$/);
564 } elsif (/^git-svn-id: /) {
565 # skip this for now, we regenerate the
566 # correct one on re-fetch anyways
567 # TODO: set *:merge properties or like...
569 print $log_fh $_ or croak
$!;
572 command_close_pipe
($msg_fh, $ctx);
574 close $log_fh or croak
$!;
576 if ($_edit || ($type eq 'tree')) {
577 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
578 # TODO: strip out spaces, comments, like git-commit.sh
579 system($editor, $commit_editmsg);
581 rename $commit_editmsg, $commit_msg or croak
$!;
582 open $log_fh, '<', $commit_msg or croak
$!;
583 { local $/; chomp($log_entry{log} = <$log_fh>); }
584 close $log_fh or croak
$!;
590 my ($str, $file, $mode) = @_;
591 open my $fd,'>',$file or croak
$!;
592 print $fd $str,"\n" or croak
$!;
593 close $fd or croak
$!;
594 chmod ($mode &~ umask, $file) if (defined $mode);
599 open my $fd,'<',$file or croak
"$!: file: $file\n";
602 close $fd or croak
$!;
607 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
609 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
610 my $log = $cmd eq 'log';
613 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
614 my ($user, $name, $email) = ($1, $2, $3);
616 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
618 $users{$user} = [$name, $email];
621 close $authors or croak
$!;
624 # convert GetOpt::Long specs for use by git-config
625 sub read_repo_config
{
626 return unless -d
$ENV{GIT_DIR
};
628 foreach my $o (keys %$opts) {
630 my ($key) = ($o =~ /^([a-z\-]+)/);
632 my $arg = 'git-config';
633 $arg .= ' --int' if ($o =~ /[:=]i$/);
634 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
635 if (ref $v eq 'ARRAY') {
636 chomp(my @tmp = `$arg --get-all svn.$key`);
639 chomp(my $tmp = `$arg --get svn.$key`);
640 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
647 sub extract_metadata
{
648 my $id = shift or return (undef, undef, undef);
649 my ($url, $rev, $uuid) = ($id =~ /^git
-svn
-id
:\s
(\S
+?
)\@
(\d
+)
651 if (!defined $rev || !$uuid || !$url) {
652 # some of the original repositories I made had
653 # identifiers like this:
654 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
656 return ($url, $rev, $uuid);
660 return extract_metadata
((grep(/^git-svn-id: /,
661 command
(qw
/cat-file commit/, shift)))[-1]);
664 sub get_commit_time
{
666 my $fh = command_output_pipe
(qw
/rev-list --pretty=raw -n1/, $cmt);
668 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
669 my ($s, $tz) = ($1, $2);
670 if ($tz =~ s/^\+//) {
671 $s += tz_to_s_offset
($tz);
672 } elsif ($tz =~ s/^\-//) {
673 $s -= tz_to_s_offset
($tz);
678 die "Can't get commit time for commit: $cmt\n";
684 return ($1 * 60) + ($tz * 3600);
690 use vars qw
/$default_repo_id $default_ref_id/;
692 use File
::Path qw
/mkpath/;
695 # properties that we do not log:
698 %SKIP_PROP = map { $_ => 1 } qw
/svn
:wc
:ra_dav
:version
-url
699 svn
:special svn
:executable
700 svn
:entry
:committed
-rev
701 svn
:entry
:last-author
703 svn
:entry
:committed
-date
/;
706 sub read_all_remotes
{
708 foreach (grep { s/^svn-remote\.// } command
(qw
/config -l/)) {
709 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
710 $r->{$1}->{fetch
}->{$2} = $3;
711 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
712 $r->{$1}->{url
} = $2;
718 # we allow more chars than remotes2config.sh...
719 sub sanitize_remote_name
{
721 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
726 my ($class, $url, $path, $repo_id, $ref_id) = @_;
727 my $self = _new
($class, $repo_id, $ref_id, $path);
729 $url =~ s!/+$!!; # strip trailing slash
730 my $orig_url = eval {
731 command_oneline
('config', '--get',
732 "svn-remote.$repo_id.url")
735 if ($orig_url ne $url) {
736 die "svn-remote.$repo_id.url already set: ",
737 "$orig_url\nwanted to set to: $url\n";
740 command_noisy
('config',
741 "svn-remote.$repo_id.url", $url);
743 command_noisy
('config', '--add',
744 "svn-remote.$repo_id.fetch",
745 "$path:".$self->refname);
753 foreach (command
(qw
/config -l/)) {
754 next unless m
!^svn
-remote\
.(.+)\
.fetch
=
755 \s
*(.*)\s
*:\s
*refs
/remotes/(.+)\s
*$!x
;
756 my ($repo_id, $path, $ref) = ($1, $2, $3);
757 if ($ref eq $ref_id) {
758 $path = '' if ($path =~ m
#^\./?#);
759 return ($repo_id, $path);
762 (undef, undef, undef);
766 my ($class, $ref_id, $repo_id, $path) = @_;
767 if (defined $ref_id && !defined $repo_id && !defined $path) {
768 ($repo_id, $path) = find_ref
($ref_id);
769 if (!defined $repo_id) {
770 die "Could not find a \"svn-remote.*.fetch\" key ",
771 "in the repository configuration matching: ",
772 "refs/remotes/$ref_id\n";
775 my $self = _new
($class, $repo_id, $ref_id, $path);
776 if (!defined $self->{path
} || !length $self->{path
}) {
777 my $fetch = command_oneline
('config', '--get',
778 "svn-remote.$repo_id.fetch",
779 ":refs/remotes/$ref_id\$") or
780 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
781 "\":refs/remotes/$ref_id\$\" in config\n";
782 ($self->{path
}, undef) = split(/\s*:\s*/, $fetch);
784 $self->{url
} = command_oneline
('config', '--get',
785 "svn-remote.$repo_id.url") or
786 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
790 sub refname
{ "refs/remotes/$_[0]->{ref_id}" }
794 $self->{ra
} ||= Git
::SVN
::Ra
->new($self->{url
});
799 my $repos_root = $self->ra->{repos_root
};
800 return $self->{path
} if ($self->{url
} eq $repos_root);
801 my $url = $self->{url
} .
802 (length $self->{path
} ?
"/$self->{path}" : $self->{path
});
803 $url =~ s!^\Q$repos_root\E/*!!g;
807 sub copy_remote_ref
{
809 my $origin = $::_cp_remote ?
$::_cp_remote
: 'origin';
810 my $ref = $self->refname;
811 if (command
('ls-remote', $origin, $ref)) {
812 command_noisy
('fetch', $origin, "$ref:$ref");
813 } elsif ($::_cp_remote
&& !$::_upgrade
) {
814 die "Unable to find remote reference: $ref on $origin\n";
818 sub traverse_ignore
{
819 my ($self, $fh, $path, $r) = @_;
821 my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
823 $p =~ s
#^\Q$self->{ra}->{svn_path}\E/##;
824 print $fh length $p ?
"\n# $p\n" : "\n# /\n";
825 if (my $s = $props->{'svn:ignore'}) {
826 $s =~ s/[\r\n]+/\n/g;
828 if (length $p == 0) {
833 print $fh "/$p/$s\n";
836 foreach (sort keys %$dirent) {
837 next if $dirent->{$_}->kind != $SVN::Node
::dir
;
838 $self->traverse_ignore($fh, "$path/$_", $r);
842 # returns the newest SVN revision number and newest commit SHA1
843 sub last_rev_commit
{
845 if (defined $self->{last_rev
} && defined $self->{last_commit
}) {
846 return ($self->{last_rev
}, $self->{last_commit
});
848 my $c = ::verify_ref
($self->refname.'^0');
850 my $rev = (::cmt_metadata
($c))[1];
852 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
856 my $offset = -41; # from tail
858 open my $fh, '<', $self->{db_path
} or
859 croak
"$self->{db_path} not readable: $!\n";
860 seek $fh, $offset, 2;
862 defined $rl or return (undef, undef);
864 while ($c ne $rl && tell $fh != 0) {
866 seek $fh, $offset, 2;
868 defined $rl or return (undef, undef);
872 croak
$! if ($rev < 0);
873 $rev = ($rev - 41) / 41;
874 close $fh or croak
$!;
875 ($self->{last_rev
}, $self->{last_commit
}) = ($rev, $c);
880 my ($self, $base) = @_;
881 my $head = $self->ra->get_latest_revnum;
882 if (!defined $::_revision
|| $::_revision
eq 'BASE:HEAD') {
883 return ($base + 1, $head) if (defined $base);
886 return ($1, $2) if ($::_revision
=~ /^(\d+):(\d+)$/);
887 return ($::_revision
, $::_revision
) if ($::_revision
=~ /^\d+$/);
888 if ($::_revision
=~ /^BASE:(\d+)$/) {
889 return ($base + 1, $1) if (defined $base);
892 return ($1, $head) if ($::_revision
=~ /^(\d+):HEAD$/);
893 die "revision argument: $::_revision not understood by git-svn\n",
894 "Try using the command-line svn client instead\n";
898 my ($self, $sub) = @_;
899 my $old_index = $ENV{GIT_INDEX_FILE
};
900 $ENV{GIT_INDEX_FILE
} = $self->{index};
903 $ENV{GIT_INDEX_FILE
} = $old_index;
905 delete $ENV{GIT_INDEX_FILE
};
907 wantarray ?
@ret : $ret[0];
910 sub assert_index_clean
{
911 my ($self, $treeish) = @_;
913 $self->tmp_index_do(sub {
914 command_noisy
('read-tree', $treeish) unless -e
$self->{index};
915 my $x = command_oneline
('write-tree');
916 my ($y) = (command
(qw
/cat-file commit/, $treeish) =~
917 /^tree ($::sha1)/mo);
919 unlink $self->{index} or croak
$!;
920 command_noisy
('read-tree', $treeish);
922 $x = command_oneline
('write-tree');
924 ::fatal
"trees ($treeish) $y != $x\n",
925 "Something is seriously wrong...\n";
930 sub get_commit_parents
{
931 my ($self, $log_entry, @parents) = @_;
932 my (%seen, @ret, @tmp);
933 # commit parents can be conditionally bound to a particular
934 # svn revision via: "svn_revno=commit_sha1", filter them out here:
935 foreach my $p (@parents) {
936 next unless defined $p;
937 if ($p =~ /^(\d+)=($::sha1_short)$/o) {
938 push @tmp, $2 if $1 == $log_entry->{revision
};
940 push @tmp, $p if $p =~ /^$::sha1_short$/o;
943 if (my $cur = ::verify_ref
($self->refname.'^0')) {
946 push @tmp, $_ foreach (@
{$log_entry->{parents
}}, @tmp);
947 while (my $p = shift @tmp) {
951 # MAXPARENT is defined to 16 in commit-tree.c:
955 die "r$log_entry->{revision}: No room for parents:\n\t",
956 join("\n\t", @tmp), "\n";
963 $self->ra->{url
} . (length $self->{path
} ?
'/' . $self->{path
} : '');
967 my ($self, $log_entry, @parents) = @_;
968 if (my $c = $self->rev_db_get($log_entry->{revision
})) {
969 croak
"$log_entry->{revision} = $c already exists! ",
970 "Why are we refetching it?\n";
972 my $author = $log_entry->{author
};
973 my ($name, $email) = (defined $::users
{$author} ? @
{$::users
{$author}}
974 : ($author, "$author\@".$self->ra->uuid));
975 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
976 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
977 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_entry->{date
};
979 my $tree = $log_entry->{tree
};
980 if (!defined $tree) {
981 $tree = $self->tmp_index_do(sub {
982 command_oneline
('write-tree') });
984 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
986 my @exec = ('git-commit-tree', $tree);
987 foreach ($self->get_commit_parents($log_entry, @parents)) {
988 push @exec, '-p', $_;
990 defined(my $pid = open3
(my $msg_fh, my $out_fh, '>&STDERR', @exec))
992 print $msg_fh $log_entry->{log} or croak
$!;
993 print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
994 $log_entry->{revision
}, ' ',
995 $self->ra->uuid, "\n" or croak
$!;
996 $msg_fh->flush == 0 or croak
$!;
997 close $msg_fh or croak
$!;
998 chomp(my $commit = do { local $/; <$out_fh> });
999 close $out_fh or croak
$!;
1002 if ($commit !~ /^$::sha1$/o) {
1003 die "Failed to commit, invalid sha1: $commit\n";
1006 command_noisy
('update-ref',$self->refname, $commit);
1007 $self->rev_db_set($log_entry->{revision
}, $commit);
1009 $self->{last_rev
} = $log_entry->{revision
};
1010 $self->{last_commit
} = $commit;
1011 print "r$log_entry->{revision} = $commit\n";
1016 my ($self, $r0, $r1) = @_;
1017 return 1 if $r0 == $r1;
1019 $self->ra->get_log([$self->{path
}], $r0, $r1,
1020 0, 0, 1, sub { $nr++ });
1021 return 0 if ($nr > 1);
1025 sub find_parent_branch
{
1026 my ($self, $paths, $rev) = @_;
1028 # look for a parent from another branch:
1029 my $i = $paths->{'/'.$self->rel_path} or return;
1030 my $branch_from = $i->copyfrom_path or return;
1031 my $r = $i->copyfrom_rev;
1032 my $repos_root = $self->ra->{repos_root
};
1033 my $url = $self->ra->{url
};
1034 my $new_url = $repos_root . $branch_from;
1035 print STDERR
"Found possible branch point: ",
1036 "$new_url => ", $self->full_url, ", $r\n";
1037 $branch_from =~ s
#^/##;
1038 my $remotes = read_all_remotes
();
1040 foreach my $repo_id (keys %$remotes) {
1041 my $u = $remotes->{$repo_id}->{url
} or next;
1043 my $fetch = $remotes->{$repo_id}->{fetch
};
1044 foreach my $f (keys %$fetch) {
1045 next if $f ne $branch_from;
1046 $gs = Git
::SVN
->new($fetch->{$f}, $repo_id, $f);
1052 my $ref_id = $branch_from;
1053 $ref_id .= "\@$r" if find_ref
($ref_id);
1054 # just grow a tail if we're not unique enough :x
1055 $ref_id .= '-' while find_ref
($ref_id);
1056 $gs = Git
::SVN
->init($new_url, '', $ref_id, $ref_id);
1058 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1059 if ($::_follow_parent
&& (!defined $r0 || !defined $parent)) {
1061 my $log_entry = eval { $gs->do_fetch(undef, $_) };
1062 $gs->do_git_commit($log_entry) if $log_entry;
1064 ($r0, $parent) = $gs->last_rev_commit;
1066 if (defined $r0 && defined $parent && $gs->revisions_eq($r0, $r)) {
1067 print STDERR
"Found branch parent: ($self->{ref_id}) $parent\n";
1068 command_noisy
('read-tree', $parent);
1070 if ($self->ra->can_do_switch) {
1071 print STDERR
"Following parent with do_switch\n";
1072 # do_switch works with svn/trunk >= r22312, but that
1073 # is not included with SVN 1.4.2 (the latest version
1074 # at the moment), so we can't rely on it
1075 $self->{last_commit
} = $parent;
1076 $ed = SVN
::Git
::Fetcher
->new($self);
1077 $gs->ra->gs_do_switch($r0, $rev, $gs->{path
}, 1,
1078 $self->full_url, $ed)
1079 or die "SVN connection failed somewhere...\n";
1081 print STDERR
"Following parent with do_update\n";
1082 $ed = SVN
::Git
::Fetcher
->new($self);
1083 $self->ra->gs_do_update($rev, $rev, $self->{path
},
1085 or die "SVN connection failed somewhere...\n";
1087 return $self->make_log_entry($rev, [$parent], $ed);
1089 print STDERR
"Branch parent not found...\n";
1094 my ($self, $paths, $rev) = @_;
1096 my ($last_rev, @parents);
1097 if ($self->{last_commit
}) {
1098 $ed = SVN
::Git
::Fetcher
->new($self);
1099 $last_rev = $self->{last_rev
};
1100 $ed->{c
} = $self->{last_commit
};
1101 @parents = ($self->{last_commit
});
1104 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1107 $ed = SVN
::Git
::Fetcher
->new($self);
1109 unless ($self->ra->gs_do_update($last_rev, $rev,
1110 $self->{path
}, 1, $ed)) {
1111 die "SVN connection failed somewhere...\n";
1113 $self->make_log_entry($rev, \
@parents, $ed);
1116 sub write_untracked
{
1117 my ($self, $rev, $fh, $untracked) = @_;
1119 print $fh "r$rev\n" or croak
$!;
1120 $h = $untracked->{empty
};
1121 foreach (sort keys %$h) {
1122 my $act = $h->{$_} ?
'+empty_dir' : '-empty_dir';
1123 print $fh " $act: ", uri_encode
($_), "\n" or croak
$!;
1124 warn "W: $act: $_\n";
1126 foreach my $t (qw
/dir_prop file_prop/) {
1127 $h = $untracked->{$t} or next;
1128 foreach my $path (sort keys %$h) {
1129 my $ppath = $path eq '' ?
'.' : $path;
1130 foreach my $prop (sort keys %{$h->{$path}}) {
1131 next if $SKIP_PROP{$prop};
1132 my $v = $h->{$path}->{$prop};
1135 uri_encode
($ppath), ' ',
1136 uri_encode
($prop), ' ',
1137 uri_encode
($v), "\n"
1141 uri_encode
($ppath), ' ',
1142 uri_encode
($prop), "\n"
1148 foreach my $t (qw
/absent_file absent_directory/) {
1149 $h = $untracked->{$t} or next;
1150 foreach my $parent (sort keys %$h) {
1151 foreach my $path (sort @
{$h->{$parent}}) {
1153 uri_encode
("$parent/$path"), "\n"
1155 warn "W: $t: $parent/$path ",
1156 "Insufficient permissions?\n";
1162 sub parse_svn_date
{
1163 my $date = shift || return '+0000 1970-01-01 00:00:00';
1164 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d
{4})\
-(\d\d
)\
-(\d\d
)T
1165 (\d\d
)\
:(\d\d
)\
:(\d\d
).\d
+Z
$/x
) or
1166 croak
"Unable to parse date: $date\n";
1167 "+0000 $Y-$m-$d $H:$M:$S";
1172 if (!defined $author || length $author == 0) {
1173 $author = '(no author)';
1175 if (defined $::_authors
&& ! defined $::users
{$author}) {
1176 die "Author: $author not defined in $::_authors file\n";
1181 sub make_log_entry
{
1182 my ($self, $rev, $parents, $untracked) = @_;
1183 my $rp = $self->ra->rev_proplist($rev);
1184 my %log_entry = ( parents
=> $parents || [], revision
=> $rev,
1185 revprops
=> $rp, log => '');
1186 open my $un, '>>', "$self->{dir}/unhandled.log" or croak
$!;
1187 $self->write_untracked($rev, $un, $untracked);
1188 foreach (sort keys %$rp) {
1190 if (/^svn:(author|date|log)$/) {
1191 $log_entry{$1} = $v;
1193 print $un " rev_prop: ", uri_encode
($_), ' ',
1194 uri_encode
($v), "\n";
1197 close $un or croak
$!;
1198 $log_entry{date
} = parse_svn_date
($log_entry{date
});
1199 $log_entry{author
} = check_author
($log_entry{author
});
1200 $log_entry{log} .= "\n";
1205 my ($self, @parents) = @_;
1206 my ($last_rev, $last_commit) = $self->last_rev_commit;
1207 my ($base, $head) = $self->parse_revision($last_rev);
1208 return if ($base > $head);
1209 if (defined $last_commit) {
1210 $self->assert_index_clean($last_commit);
1213 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
1214 my $err_handler = $SVN::Error
::handler
;
1215 $SVN::Error
::handler
= \
&skip_unknown_revs
;
1218 $self->ra->get_log([$self->{path
}], $min, $max, 0, 1, 1, sub {
1219 my ($paths, $rev, $author, $date, $log) = @_;
1220 push @revs, [ $paths, $rev ] });
1222 my $log_entry = $self->do_fetch(@
$_);
1223 $self->do_git_commit($log_entry, @parents);
1225 last if $max >= $head;
1228 $max = $head if ($max > $head);
1230 $SVN::Error
::handler
= $err_handler;
1234 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1235 # TODO: enable and test optimized commits:
1236 if (0 && $rev == ($self->{last_rev
} + 1)) {
1237 $log_entry->{revision
} = $rev;
1238 $log_entry->{author
} = $author;
1239 $self->do_git_commit($log_entry, "$rev=$tree");
1241 $self->fetch("$rev=$tree");
1246 my ($self, $tree) = (shift, shift);
1247 my $log_entry = ::get_commit_entry
($tree);
1248 unless ($self->{last_rev
}) {
1249 fatal
("Must have an existing revision to commit\n");
1251 my $pool = SVN
::Pool
->new;
1252 my $ed = SVN
::Git
::Editor
->new({ r
=> $self->{last_rev
},
1253 ra
=> $self->ra->dup,
1254 svn_path
=> $self->ra->{svn_path
}
1256 $self->ra->get_commit_editor(
1257 $log_entry->{log}, sub {
1258 $self->set_tree_cb($log_entry,
1262 my $mods = $ed->apply_diff($self->{last_commit
}, $tree);
1264 print "No changes\nr$self->{last_rev} = $tree\n";
1269 sub skip_unknown_revs
{
1271 my $errno = $err->apr_err();
1272 # Maybe the branch we're tracking didn't
1273 # exist when the repo started, so it's
1274 # not an error if it doesn't, just continue
1276 # Wonderfully consistent library, eh?
1277 # 160013 - svn:// and file://
1278 # 175002 - http(s)://
1279 # 175007 - http(s):// (this repo required authorization, too...)
1280 # More codes may be discovered later...
1281 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
1284 croak
"Error from SVN, ($errno): ", $err->expanded_message,"\n";
1288 # Tie::File seems to be prone to offset errors if revisions get sparse,
1289 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1290 # one of my favorite modules is out :< Next up would be one of the DBM
1291 # modules, but I'm not sure which is most portable... So I'll just
1292 # go with something that's plain-text, but still capable of
1293 # being randomly accessed. So here's my ultra-simple fixed-width
1294 # database. All records are 40 characters + "\n", so it's easy to seek
1295 # to a revision: (41 * rev) is the byte offset.
1296 # A record of 40 0s denotes an empty revision.
1297 # And yes, it's still pretty fast (faster than Tie::File).
1300 my ($self, $rev, $commit) = @_;
1301 length $commit == 40 or croak
"arg3 must be a full SHA1 hexsum\n";
1302 open my $fh, '+<', $self->{db_path
} or croak
$!;
1303 my $offset = $rev * 41;
1304 # assume that append is the common case:
1305 seek $fh, 0, 2 or croak
$!;
1307 if ($pos < $offset) {
1308 print $fh (('0' x
40),"\n") x
(($offset - $pos) / 41)
1311 seek $fh, $offset, 0 or croak
$!;
1312 print $fh $commit,"\n" or croak
$!;
1313 close $fh or croak
$!;
1317 my ($self, $rev) = @_;
1319 my $offset = $rev * 41;
1320 open my $fh, '<', $self->{db_path
} or croak
$!;
1321 if (seek $fh, $offset, 0) {
1322 $ret = readline $fh;
1325 $ret = undef if ($ret =~ /^0{40}$/);
1328 close $fh or croak
$!;
1332 sub find_rev_before
{
1333 my ($self, $rev, $eq_ok) = @_;
1334 --$rev unless $eq_ok;
1336 if (my $c = $self->rev_db_get($rev)) {
1341 return (undef, undef);
1345 my ($class, $repo_id, $ref_id, $path) = @_;
1346 unless (defined $repo_id && length $repo_id) {
1347 $repo_id = $Git::SVN
::default_repo_id
;
1349 unless (defined $ref_id && length $ref_id) {
1350 $_[2] = $ref_id = $Git::SVN
::default_ref_id
;
1352 $_[1] = $repo_id = sanitize_remote_name
($repo_id);
1353 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
1354 $_[3] = $path = '' unless (defined $path);
1356 unless (-f
"$dir/.rev_db") {
1357 open my $fh, '>>', "$dir/.rev_db" or croak
$!;
1358 close $fh or croak
$!;
1360 bless { ref_id
=> $ref_id, dir
=> $dir, index => "$dir/index",
1362 db_path
=> "$dir/.rev_db", repo_id
=> $repo_id }, $class;
1367 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1371 package Git
::SVN
::Prompt
;
1375 use vars qw
/$_no_auth_cache $_username/;
1378 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1379 $may_save = undef if $_no_auth_cache;
1380 $default_username = $_username if defined $_username;
1381 if (defined $default_username && length $default_username) {
1382 if (defined $realm && length $realm) {
1383 print STDERR
"Authentication realm: $realm\n";
1386 $cred->username($default_username);
1388 username
($cred, $realm, $may_save, $pool);
1390 $cred->password(_read_password
("Password for '" .
1391 $cred->username . "': ", $realm));
1392 $cred->may_save($may_save);
1393 $SVN::_Core
::SVN_NO_ERROR
;
1396 sub ssl_server_trust
{
1397 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1398 $may_save = undef if $_no_auth_cache;
1399 print STDERR
"Error validating server certificate for '$realm':\n";
1400 if ($failures & $SVN::Auth
::SSL
::UNKNOWNCA
) {
1401 print STDERR
" - The certificate is not issued by a trusted ",
1402 "authority. Use the\n",
1403 " fingerprint to validate the certificate manually!\n";
1405 if ($failures & $SVN::Auth
::SSL
::CNMISMATCH
) {
1406 print STDERR
" - The certificate hostname does not match.\n";
1408 if ($failures & $SVN::Auth
::SSL
::NOTYETVALID
) {
1409 print STDERR
" - The certificate is not yet valid.\n";
1411 if ($failures & $SVN::Auth
::SSL
::EXPIRED
) {
1412 print STDERR
" - The certificate has expired.\n";
1414 if ($failures & $SVN::Auth
::SSL
::OTHER
) {
1415 print STDERR
" - The certificate has an unknown error.\n";
1418 "Certificate information:\n".
1419 " - Hostname: %s\n".
1420 " - Valid: from %s until %s\n".
1422 " - Fingerprint: %s\n",
1423 map $cert_info->$_, qw(hostname valid_from valid_until
1424 issuer_dname fingerprint);
1427 print STDERR
$may_save ?
1428 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1429 "(R)eject or accept (t)emporarily? ";
1431 $choice = lc(substr(<STDIN
> || 'R', 0, 1));
1432 if ($choice =~ /^t$/i) {
1433 $cred->may_save(undef);
1434 } elsif ($choice =~ /^r$/i) {
1436 } elsif ($may_save && $choice =~ /^p$/i) {
1437 $cred->may_save($may_save);
1441 $cred->accepted_failures($failures);
1442 $SVN::_Core
::SVN_NO_ERROR
;
1445 sub ssl_client_cert
{
1446 my ($cred, $realm, $may_save, $pool) = @_;
1447 $may_save = undef if $_no_auth_cache;
1448 print STDERR
"Client certificate filename: ";
1450 chomp(my $filename = <STDIN
>);
1451 $cred->cert_file($filename);
1452 $cred->may_save($may_save);
1453 $SVN::_Core
::SVN_NO_ERROR
;
1456 sub ssl_client_cert_pw
{
1457 my ($cred, $realm, $may_save, $pool) = @_;
1458 $may_save = undef if $_no_auth_cache;
1459 $cred->password(_read_password
("Password: ", $realm));
1460 $cred->may_save($may_save);
1461 $SVN::_Core
::SVN_NO_ERROR
;
1465 my ($cred, $realm, $may_save, $pool) = @_;
1466 $may_save = undef if $_no_auth_cache;
1467 if (defined $realm && length $realm) {
1468 print STDERR
"Authentication realm: $realm\n";
1471 if (defined $_username) {
1472 $username = $_username;
1474 print STDERR
"Username: ";
1476 chomp($username = <STDIN
>);
1478 $cred->username($username);
1479 $cred->may_save($may_save);
1480 $SVN::_Core
::SVN_NO_ERROR
;
1483 sub _read_password
{
1484 my ($prompt, $realm) = @_;
1485 print STDERR
$prompt;
1487 require Term
::ReadKey
;
1488 Term
::ReadKey
::ReadMode
('noecho');
1490 while (defined(my $key = Term
::ReadKey
::ReadKey
(0))) {
1491 last if $key =~ /[\012\015]/; # \n\r
1494 Term
::ReadKey
::ReadMode
('restore');
1504 $f =~ s
#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1511 $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
1516 my $kill_stupid_warnings = $SVN::Node
::none
.$SVN::Node
::file
.
1517 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1518 $SVN::Node
::none
.$SVN::Node
::file
.
1519 $SVN::Node
::dir
.$SVN::Node
::unknown
.
1520 $SVN::Auth
::SSL
::CNMISMATCH
.
1521 $SVN::Auth
::SSL
::NOTYETVALID
.
1522 $SVN::Auth
::SSL
::EXPIRED
.
1523 $SVN::Auth
::SSL
::UNKNOWNCA
.
1524 $SVN::Auth
::SSL
::OTHER
;
1527 package SVN
::Git
::Fetcher
;
1534 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
1536 my ($class, $git_svn) = @_;
1537 my $self = SVN
::Delta
::Editor
->new;
1538 bless $self, $class;
1539 $self->{c
} = $git_svn->{last_commit
} if exists $git_svn->{last_commit
};
1540 $self->{empty
} = {};
1541 $self->{dir_prop
} = {};
1542 $self->{file_prop
} = {};
1543 $self->{absent_dir
} = {};
1544 $self->{absent_file
} = {};
1545 ($self->{gui
}, $self->{ctx
}) = $git_svn->tmp_index_do(
1546 sub { command_input_pipe
(qw
/update-index -z --index-info/) } );
1547 require Digest
::MD5
;
1551 sub set_path_strip
{
1552 my ($self, $path) = @_;
1553 $self->{path_strip
} = qr/^\Q$path\E\/?
/;
1560 sub open_directory
{
1561 my ($self, $path, $pb, $rev) = @_;
1566 my ($self, $path) = @_;
1567 $path =~ s!$self->{path_strip}!! if $self->{path_strip
};
1572 my ($self, $path, $rev, $pb) = @_;
1573 my $gui = $self->{gui
};
1575 my $gpath = $self->git_path($path);
1576 # remove entire directories.
1577 if (command
('ls-tree', $self->{c
}, '--', $gpath) =~ /^040000 tree/) {
1578 my ($ls, $ctx) = command_output_pipe
(qw
/ls
-tree
1580 $self->{c
}, '--', $gpath);
1583 print $gui '0 ',0 x
40,"\t",$_ or croak
$!;
1584 print "\tD\t$_\n" unless $self->{q
};
1586 print "\tD\t$gpath/\n" unless $self->{q
};
1587 command_close_pipe
($ls, $ctx);
1588 $self->{empty
}->{$path} = 0
1590 print $gui '0 ',0 x
40,"\t",$gpath,"\0" or croak
$!;
1591 print "\tD\t$gpath\n" unless $self->{q
};
1597 my ($self, $path, $pb, $rev) = @_;
1598 my $gpath = $self->git_path($path);
1599 my ($mode, $blob) = (command
('ls-tree', $self->{c
}, '--', $gpath)
1600 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
1601 unless (defined $mode && defined $blob) {
1602 die "$path was not found in commit $self->{c} (r$rev)\n";
1604 { path
=> $path, mode_a
=> $mode, mode_b
=> $mode, blob
=> $blob,
1605 pool
=> SVN
::Pool
->new, action
=> 'M' };
1609 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
1610 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1611 delete $self->{empty
}->{$dir};
1612 { path
=> $path, mode_a
=> 100644, mode_b
=> 100644,
1613 pool
=> SVN
::Pool
->new, action
=> 'A' };
1617 my ($self, $path, $cp_path, $cp_rev) = @_;
1618 my ($dir, $file) = ($path =~ m
#^(.*?)/?([^/]+)$#);
1619 delete $self->{empty
}->{$dir};
1620 $self->{empty
}->{$path} = 1;
1624 sub change_dir_prop
{
1625 my ($self, $db, $prop, $value) = @_;
1626 $self->{dir_prop
}->{$db->{path
}} ||= {};
1627 $self->{dir_prop
}->{$db->{path
}}->{$prop} = $value;
1631 sub absent_directory
{
1632 my ($self, $path, $pb) = @_;
1633 $self->{absent_dir
}->{$pb->{path
}} ||= [];
1634 push @
{$self->{absent_dir
}->{$pb->{path
}}}, $path;
1639 my ($self, $path, $pb) = @_;
1640 $self->{absent_file
}->{$pb->{path
}} ||= [];
1641 push @
{$self->{absent_file
}->{$pb->{path
}}}, $path;
1645 sub change_file_prop
{
1646 my ($self, $fb, $prop, $value) = @_;
1647 if ($prop eq 'svn:executable') {
1648 if ($fb->{mode_b
} != 120000) {
1649 $fb->{mode_b
} = defined $value ?
100755 : 100644;
1651 } elsif ($prop eq 'svn:special') {
1652 $fb->{mode_b
} = defined $value ?
120000 : 100644;
1654 $self->{file_prop
}->{$fb->{path
}} ||= {};
1655 $self->{file_prop
}->{$fb->{path
}}->{$prop} = $value;
1660 sub apply_textdelta
{
1661 my ($self, $fb, $exp) = @_;
1662 my $fh = IO
::File
->new_tmpfile;
1664 # $fh gets auto-closed() by SVN::TxDelta::apply(),
1665 # (but $base does not,) so dup() it for reading in close_file
1666 open my $dup, '<&', $fh or croak
$!;
1667 my $base = IO
::File
->new_tmpfile;
1668 $base->autoflush(1);
1670 defined (my $pid = fork) or croak
$!;
1672 open STDOUT
, '>&', $base or croak
$!;
1673 print STDOUT
'link ' if ($fb->{mode_a
} == 120000);
1674 exec qw
/git-cat-file blob/, $fb->{blob
} or croak
$!;
1680 seek $base, 0, 0 or croak
$!;
1681 my $md5 = Digest
::MD5
->new;
1682 $md5->addfile($base);
1683 my $got = $md5->hexdigest;
1684 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
1686 " got: $got\n" if ($got ne $exp);
1689 seek $base, 0, 0 or croak
$!;
1691 $fb->{base
} = $base;
1692 [ SVN
::TxDelta
::apply
($base, $fh, undef, $fb->{path
}, $fb->{pool
}) ];
1696 my ($self, $fb, $exp) = @_;
1698 my $path = $self->git_path($fb->{path
});
1699 if (my $fh = $fb->{fh
}) {
1700 seek($fh, 0, 0) or croak
$!;
1701 my $md5 = Digest
::MD5
->new;
1703 my $got = $md5->hexdigest;
1704 die "Checksum mismatch: $path\n",
1705 "expected: $exp\n got: $got\n" if ($got ne $exp);
1706 seek($fh, 0, 0) or croak
$!;
1707 if ($fb->{mode_b
} == 120000) {
1708 read($fh, my $buf, 5) == 5 or croak
$!;
1709 $buf eq 'link ' or die "$path has mode 120000",
1710 "but is not a link\n";
1712 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
1714 open STDIN
, '<&', $fh or croak
$!;
1715 exec qw
/git-hash-object -w --stdin/ or croak
$!;
1717 chomp($hash = do { local $/; <$out> });
1718 close $out or croak
$!;
1719 close $fh or croak
$!;
1720 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
1721 close $fb->{base
} or croak
$!;
1723 $hash = $fb->{blob
} or die "no blob information\n";
1726 my $gui = $self->{gui
};
1727 print $gui "$fb->{mode_b} $hash\t$path\0" or croak
$!;
1728 print "\t$fb->{action}\t$path\n" if $fb->{action
} && ! $self->{q
};
1734 eval { command_close_pipe
($self->{gui
}, $self->{ctx
}) };
1735 $self->SUPER::abort_edit
(@_);
1740 command_close_pipe
($self->{gui
}, $self->{ctx
});
1741 $self->{git_commit_ok
} = 1;
1742 $self->SUPER::close_edit
(@_);
1745 package SVN
::Git
::Editor
;
1754 my $git_svn = shift;
1755 my $self = SVN
::Delta
::Editor
->new(@_);
1756 bless $self, $class;
1757 foreach (qw
/svn_path r ra/) {
1758 die "$_ required!\n" unless (defined $git_svn->{$_});
1759 $self->{$_} = $git_svn->{$_};
1761 $self->{pool
} = SVN
::Pool
->new;
1762 $self->{bat
} = { '' => $self->open_root($self->{r
}, $self->{pool
}) };
1764 require Digest
::MD5
;
1769 return ($_[0] =~ m
#^(.*?)/?([^/]+)$#);
1773 (defined $_[1] && length $_[1]) ?
$_[1] : ''
1777 my ($self, $path) = @_;
1778 $self->{ra
}->{url
} . '/' . $self->repo_path($path);
1782 my ($self, $tree_b) = @_;
1783 my $rm = $self->{rm
};
1784 delete $rm->{''}; # we never delete the url we're tracking
1787 foreach (keys %$rm) {
1788 my @d = split m
#/#, $_;
1792 $c .= '/' . shift @d;
1796 delete $rm->{$self->{svn_path
}};
1797 delete $rm->{''}; # we never delete the url we're tracking
1800 my ($fh, $ctx) = command_output_pipe
(
1801 qw
/ls-tree --name-only -r -z/, $tree_b);
1805 my @dn = split m
#/#, $_;
1807 delete $rm->{join '/', @dn};
1814 command_close_pipe
($fh, $ctx);
1816 my ($r, $p, $bat) = ($self->{r
}, $self->{pool
}, $self->{bat
});
1817 foreach my $d (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
1818 $self->close_directory($bat->{$d}, $p);
1819 my ($dn) = ($d =~ m
#^(.*?)/?(?:[^/]+)$#);
1820 print "\tD+\t$d/\n" unless $::_q
;
1821 $self->SUPER::delete_entry
($d, $r, $bat->{$dn}, $p);
1826 sub open_or_add_dir
{
1827 my ($self, $full_path, $baton) = @_;
1828 my $t = $self->{ra
}->check_path($full_path, $self->{r
});
1829 if ($t == $SVN::Node
::none
) {
1830 return $self->add_directory($full_path, $baton,
1831 undef, -1, $self->{pool
});
1832 } elsif ($t == $SVN::Node
::dir
) {
1833 return $self->open_directory($full_path, $baton,
1834 $self->{r
}, $self->{pool
});
1836 print STDERR
"$full_path already exists in repository at ",
1837 "r$self->{r} and it is not a directory (",
1838 ($t == $SVN::Node
::file ?
'file' : 'unknown'),"/$t)\n";
1843 my ($self, $path) = @_;
1844 my $bat = $self->{bat
};
1845 $path = $self->repo_path($path);
1846 return $bat->{''} unless (length $path);
1847 my @p = split m
#/+#, $path;
1849 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
1852 $c .= '/' . shift @p;
1853 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
1859 my ($self, $m) = @_;
1860 my ($dir, $file) = split_path
($m->{file_b
});
1861 my $pbat = $self->ensure_path($dir);
1862 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1864 print "\tA\t$m->{file_b}\n" unless $::_q
;
1865 $self->chg_file($fbat, $m);
1866 $self->close_file($fbat,undef,$self->{pool
});
1870 my ($self, $m) = @_;
1871 my ($dir, $file) = split_path
($m->{file_b
});
1872 my $pbat = $self->ensure_path($dir);
1873 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1874 $self->url_path($m->{file_a
}), $self->{r
});
1875 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1876 $self->chg_file($fbat, $m);
1877 $self->close_file($fbat,undef,$self->{pool
});
1881 my ($self, $path, $pbat) = @_;
1882 my $rpath = $self->repo_path($path);
1883 my ($dir, $file) = split_path
($rpath);
1884 $self->{rm
}->{$dir} = 1;
1885 $self->SUPER::delete_entry
($rpath, $self->{r
}, $pbat, $self->{pool
});
1889 my ($self, $m) = @_;
1890 my ($dir, $file) = split_path
($m->{file_b
});
1891 my $pbat = $self->ensure_path($dir);
1892 my $fbat = $self->add_file($self->repo_path($m->{file_b
}), $pbat,
1893 $self->url_path($m->{file_a
}), $self->{r
});
1894 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q
;
1895 $self->chg_file($fbat, $m);
1896 $self->close_file($fbat,undef,$self->{pool
});
1898 ($dir, $file) = split_path
($m->{file_a
});
1899 $pbat = $self->ensure_path($dir);
1900 $self->delete_entry($m->{file_a
}, $pbat);
1904 my ($self, $m) = @_;
1905 my ($dir, $file) = split_path
($m->{file_b
});
1906 my $pbat = $self->ensure_path($dir);
1907 my $fbat = $self->open_file($self->repo_path($m->{file_b
}),
1908 $pbat,$self->{r
},$self->{pool
});
1909 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q
;
1910 $self->chg_file($fbat, $m);
1911 $self->close_file($fbat,undef,$self->{pool
});
1914 sub T
{ shift->M(@_) }
1916 sub change_file_prop
{
1917 my ($self, $fbat, $pname, $pval) = @_;
1918 $self->SUPER::change_file_prop
($fbat, $pname, $pval, $self->{pool
});
1922 my ($self, $fbat, $m) = @_;
1923 if ($m->{mode_b
} =~ /755$/ && $m->{mode_a
} !~ /755$/) {
1924 $self->change_file_prop($fbat,'svn:executable','*');
1925 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
1926 $self->change_file_prop($fbat,'svn:executable',undef);
1928 my $fh = IO
::File
->new_tmpfile or croak
$!;
1929 if ($m->{mode_b
} =~ /^120/) {
1930 print $fh 'link ' or croak
$!;
1931 $self->change_file_prop($fbat,'svn:special','*');
1932 } elsif ($m->{mode_a
} =~ /^120/ && $m->{mode_b
} !~ /^120/) {
1933 $self->change_file_prop($fbat,'svn:special',undef);
1935 defined(my $pid = fork) or croak
$!;
1937 open STDOUT
, '>&', $fh or croak
$!;
1938 exec qw
/git-cat-file blob/, $m->{sha1_b
} or croak
$!;
1942 $fh->flush == 0 or croak
$!;
1943 seek $fh, 0, 0 or croak
$!;
1945 my $md5 = Digest
::MD5
->new;
1946 $md5->addfile($fh) or croak
$!;
1947 seek $fh, 0, 0 or croak
$!;
1949 my $exp = $md5->hexdigest;
1950 my $pool = SVN
::Pool
->new;
1951 my $atd = $self->apply_textdelta($fbat, undef, $pool);
1952 my $got = SVN
::TxDelta
::send_stream
($fh, @
$atd, $pool);
1953 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
1956 close $fh or croak
$!;
1960 my ($self, $m) = @_;
1961 my ($dir, $file) = split_path
($m->{file_b
});
1962 my $pbat = $self->ensure_path($dir);
1963 print "\tD\t$m->{file_b}\n" unless $::_q
;
1964 $self->delete_entry($m->{file_b
}, $pbat);
1969 my ($p,$bat) = ($self->{pool
}, $self->{bat
});
1970 foreach (sort { $b =~ tr
#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
1971 $self->close_directory($bat->{$_}, $p);
1973 $self->SUPER::close_edit
($p);
1979 $self->SUPER::abort_edit
($self->{pool
});
1980 $self->{pool
}->clear;
1983 # this drives the editor
1985 my ($self, $tree_a, $tree_b) = @_;
1986 my @diff_tree = qw(diff-tree -z -r);
1987 if ($::_cp_similarity
) {
1988 push @diff_tree, "-C$::_cp_similarity";
1990 push @diff_tree, '-C';
1992 push @diff_tree, '--find-copies-harder' if $::_find_copies_harder
;
1993 push @diff_tree, "-l$::_l" if defined $::_l
;
1994 push @diff_tree, $tree_a, $tree_b;
1995 my ($diff_fh, $ctx) = command_output_pipe
(@diff_tree);
2000 while (<$diff_fh>) {
2001 chomp $_; # this gets rid of the trailing "\0"
2002 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
2003 $::sha1\s
($::sha1
)\s
2004 ([MTCRAD
])\d
*$/xo
) {
2005 push @mods, { mode_a
=> $1, mode_b
=> $2,
2006 sha1_b
=> $3, chg
=> $4 };
2007 if ($4 =~ /^(?:C|R)$/) {
2012 } elsif ($state eq 'file_a') {
2013 my $x = $mods[$#mods] or croak
"Empty array\n";
2014 if ($x->{chg
} !~ /^(?:C|R)$/) {
2015 croak
"Error parsing $_, $x->{chg}\n";
2019 } elsif ($state eq 'file_b') {
2020 my $x = $mods[$#mods] or croak
"Empty array\n";
2021 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
2022 croak
"Error parsing $_, $x->{chg}\n";
2024 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
2025 croak
"Error parsing $_, $x->{chg}\n";
2030 croak
"Error parsing $_\n";
2033 command_close_pipe
($diff_fh, $ctx);
2036 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
2037 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @mods) {
2039 if (defined $o{$f}) {
2042 fatal
("Invalid change type: $f\n");
2045 $self->rmdirs($tree_b) if $::_rmdir
;
2054 package Git
::SVN
::Ra
;
2055 use vars qw
/@ISA $config_dir/;
2058 my ($can_do_switch);
2062 # enforce temporary pool usage for some simple functions
2064 foreach (qw
/get_latest_revnum rev_proplist get_file
2065 check_path get_dir get_uuid get_repos_root
/) {
2068 my \$pool = SVN::Pool->new;
2069 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2071 wantarray ? \@ret : \$ret[0]; }\n";
2077 my ($class, $url) = @_;
2079 return $RA{$url} if $RA{$url};
2081 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
2082 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
([
2083 SVN
::Client
::get_simple_provider
(),
2084 SVN
::Client
::get_ssl_server_trust_file_provider
(),
2085 SVN
::Client
::get_simple_prompt_provider
(
2086 \
&Git
::SVN
::Prompt
::simple
, 2),
2087 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
2088 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
2089 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
2090 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
2091 SVN
::Client
::get_username_provider
(),
2092 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
2093 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
2094 SVN
::Client
::get_username_prompt_provider
(
2095 \
&Git
::SVN
::Prompt
::username
, 2),
2097 my $config = SVN
::Core
::config_get_config
($config_dir);
2098 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
2100 pool
=> SVN
::Pool
->new,
2101 auth_provider_callbacks
=> $callbacks);
2102 $self->{svn_path
} = $url;
2103 $self->{repos_root
} = $self->get_repos_root;
2104 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E/*##;
2105 $RA{$url} = bless $self, $class;
2109 # do not call the real DESTROY since we store ourselves in %RA
2114 my $dup = SVN
::Ra
->new(pool
=> SVN
::Pool
->new,
2115 map { $_ => $self->{$_} } qw
/config url
2116 auth auth_provider_callbacks repos_root svn_path
/);
2117 bless $dup, ref $self;
2121 my ($self, @args) = @_;
2122 my $pool = SVN
::Pool
->new;
2123 $args[4]-- if $args[4] && ! $::_follow_parent
;
2124 splice(@args, 3, 1) if ($SVN::Core
::VERSION
le '1.2.0');
2125 my $ret = $self->SUPER::get_log
(@args, $pool);
2130 sub get_commit_editor
{
2131 my ($self, $log, $cb, $pool) = @_;
2132 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef, 0) : ();
2133 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
2138 $self->{uuid
} ||= $self->get_uuid;
2142 my ($self, $rev_a, $rev_b, $path, $recurse, $editor) = @_;
2143 my $pool = SVN
::Pool
->new;
2144 $editor->set_path_strip($path);
2145 my $reporter = $self->do_update($rev_b, $path, $recurse,
2147 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2148 my $new = ($rev_a == $rev_b);
2149 $reporter->set_path('', $rev_a, $new, @lock, $pool);
2150 $reporter->finish_report($pool);
2152 $editor->{git_commit_ok
};
2156 my ($self, $rev_a, $rev_b, $path, $recurse, $url_b, $editor) = @_;
2157 my $pool = SVN
::Pool
->new;
2158 $editor->set_path_strip($path);
2159 my $reporter = $self->do_switch($rev_b, $path, $recurse,
2160 $url_b, $editor, $pool);
2161 my @lock = $SVN::Core
::VERSION
ge '1.2.0' ?
(undef) : ();
2162 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2163 $reporter->finish_report($pool);
2165 $editor->{git_commit_ok
};
2170 unless (defined $can_do_switch) {
2171 my $pool = SVN
::Pool
->new;
2173 $self->do_switch(1, '', 0, $self->{url
},
2174 SVN
::Delta
::Editor
->new, $pool);
2179 $rep->abort_report($pool);
2187 package Git
::SVN
::Log
;
2190 use POSIX qw
/strftime/;
2191 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
2192 %rusers $show_commit $incremental/;
2197 return 1 if defined $c->{r
};
2198 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
2199 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
2200 my @log = command
(qw
/cat-file commit/, $c->{c
});
2201 shift @log while ($log[0] ne "\n");
2203 @
{$c->{l
}} = grep !/^git-svn-id: /, @log;
2205 (undef, $c->{r
}, undef) = ::extract_metadata
(
2206 (grep(/^git-svn-id: /, @log))[-1]);
2208 return defined $c->{r
};
2214 $dcvar = 'color.diff';
2215 $dc = `git-config --get $dcvar`;
2217 # nothing at all; fallback to "diff.color"
2218 $dcvar = 'diff.color';
2219 $dc = `git-config --get $dcvar`;
2222 if ($dc eq 'auto') {
2224 $pc = `git-config --get color.pager`;
2226 # does not have it -- fallback to pager.color
2227 $pc = `git-config --bool --get pager.color`;
2230 $pc = `git-config --bool --get color.pager`;
2236 if (-t
*STDOUT
|| (defined $pager && $pc eq 'true')) {
2237 return ($ENV{TERM
} && $ENV{TERM
} ne 'dumb');
2241 return 0 if $dc eq 'never';
2242 return 1 if $dc eq 'always';
2243 chomp($dc = `git-config --bool --get $dcvar`);
2244 return ($dc eq 'true');
2247 sub git_svn_log_cmd
{
2248 my ($r_min, $r_max) = @_;
2249 my $gs = Git
::SVN
->_new;
2250 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
2252 push @cmd, '-r' unless $non_recursive;
2253 push @cmd, qw
/--raw --name-status/ if $verbose;
2254 push @cmd, '--color' if log_use_color
();
2255 return @cmd unless defined $r_max;
2256 if ($r_max == $r_min) {
2257 push @cmd, '--max-count=1';
2258 if (my $c = $gs->rev_db_get($r_max)) {
2262 my ($c_min, $c_max);
2263 $c_max = $gs->rev_db_get($r_max);
2264 $c_min = $gs->rev_db_get($r_min);
2265 if (defined $c_min && defined $c_max) {
2266 if ($r_max > $r_max) {
2267 push @cmd, "$c_min..$c_max";
2269 push @cmd, "$c_max..$c_min";
2271 } elsif ($r_max > $r_min) {
2280 # adapted from pager.c
2282 $pager ||= $ENV{GIT_PAGER
} || $ENV{PAGER
};
2283 if (!defined $pager) {
2285 } elsif (length $pager == 0 || $pager eq 'cat') {
2291 return unless -t
*STDOUT
;
2292 pipe my $rfd, my $wfd or return;
2293 defined(my $pid = fork) or ::fatal
"Can't fork: $!\n";
2295 open STDOUT
, '>&', $wfd or
2296 ::fatal
"Can't redirect to stdout: $!\n";
2299 open STDIN
, '<&', $rfd or ::fatal
"Can't redirect stdin: $!\n";
2300 $ENV{LESS
} ||= 'FRSX';
2301 exec $pager or ::fatal
"Can't run pager: $! ($pager)\n";
2304 sub get_author_info
{
2305 my ($dest, $author, $t, $tz) = @_;
2306 $author =~ s/(?:^\s*|\s*$)//g;
2307 $dest->{a_raw
} = $author;
2310 $au = $rusers{$author} || undef;
2313 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
2318 # Date::Parse isn't in the standard Perl distro :(
2319 if ($tz =~ s/^\+//) {
2320 $t += ::tz_to_s_offset
($tz);
2321 } elsif ($tz =~ s/^\-//) {
2322 $t -= ::tz_to_s_offset
($tz);
2324 $dest->{t_utc
} = $t;
2327 sub process_commit
{
2328 my ($c, $r_min, $r_max, $defer) = @_;
2329 if (defined $r_min && defined $r_max) {
2330 if ($r_min == $c->{r
} && $r_min == $r_max) {
2334 return 1 if $r_min == $r_max;
2335 if ($r_min < $r_max) {
2336 # we need to reverse the print order
2337 return 0 if (defined $limit && --$limit < 0);
2341 if ($r_min != $r_max) {
2342 return 1 if ($r_min < $c->{r
});
2343 return 1 if ($r_max > $c->{r
});
2346 return 0 if (defined $limit && --$limit < 0);
2355 if (my $l = $c->{l
}) {
2356 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2359 $l_fmt ||= 'A' . length($c->{r
});
2360 print 'r',pack($l_fmt, $c->{r
}),' | ';
2361 print "$c->{c} | " if $show_commit;
2364 show_commit_normal
($c);
2368 sub show_commit_changed_paths
{
2370 return unless $c->{changed
};
2371 print "Changed paths:\n", @
{$c->{changed
}};
2374 sub show_commit_normal
{
2376 print '-' x72
, "\nr$c->{r} | ";
2377 print "$c->{c} | " if $show_commit;
2378 print "$c->{a} | ", strftime
("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2379 localtime($c->{t_utc
})), ' | ';
2382 if (my $l = $c->{l
}) {
2383 while ($l->[$#$l] eq "\n" && $#$l > 0
2384 && $l->[($#$l - 1)] eq "\n") {
2387 $nr_line = scalar @
$l;
2389 print "1 line\n\n\n";
2391 if ($nr_line == 1) {
2392 $nr_line = '1 line';
2394 $nr_line .= ' lines';
2396 print $nr_line, "\n";
2397 show_commit_changed_paths
($c);
2399 print $_ foreach @
$l;
2403 show_commit_changed_paths
($c);
2407 foreach my $x (qw
/raw diff/) {
2410 print $_ foreach @
{$c->{$x}}
2417 my ($r_min, $r_max);
2418 my $r_last = -1; # prevent dupes
2424 if (defined $::_revision
) {
2425 if ($::_revision
=~ /^(\d+):(\d+)$/) {
2426 ($r_min, $r_max) = ($1, $2);
2427 } elsif ($::_revision
=~ /^\d+$/) {
2428 $r_min = $r_max = $::_revision
;
2430 ::fatal
"-r$::_revision is not supported, use ",
2431 "standard \'git log\' arguments instead\n";
2436 @args = (git_svn_log_cmd
($r_min, $r_max), @args);
2437 my $log = command_output_pipe
(@args);
2440 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
2442 if (/^${esc_color}commit ($::sha1_short)/o) {
2444 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
2446 process_commit
($c, $r_min, $r_max, \
@k) or
2451 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
2452 get_author_info
($c, $1, $2, $3);
2453 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
2455 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
2456 push @
{$c->{raw
}}, $_;
2457 } elsif (/^${esc_color}[ACRMDT]\t/) {
2458 # we could add $SVN->{svn_path} here, but that requires
2459 # remote access at the moment (repo_path_split)...
2460 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
2461 push @
{$c->{changed
}}, $_;
2462 } elsif (/^${esc_color}diff /o) {
2464 push @
{$c->{diff
}}, $_;
2466 push @
{$c->{diff
}}, $_;
2467 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
2468 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
2469 } elsif (s/^${esc_color} //o) {
2470 push @
{$c->{l
}}, $_;
2473 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
2475 process_commit
($c, $r_min, $r_max, \
@k);
2481 process_commit
($_, $r_min, $r_max) foreach reverse @k;
2485 print '-' x72
,"\n" unless $incremental || $oneline;
2488 package Git
::SVN
::Migration
;
2489 # these version numbers do NOT correspond to actual version numbers
2490 # of git nor git-svn. They are just relative.
2492 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
2494 # v1 layout: .git/$id/info/url, refs/remotes/$id
2496 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
2498 # v3 layout: .git/svn/$id, refs/remotes/$id
2499 # - info/url may remain for backwards compatibility
2500 # - this is what we migrate up to this layout automatically,
2501 # - this will be used by git svn init on single branches
2503 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
2504 # - this is only created for newly multi-init-ed
2505 # repositories. Similar in spirit to the
2506 # --use-separate-remotes option in git-clone (now default)
2507 # - we do not automatically migrate to this (following
2508 # the example set by core git)
2512 use File
::Path qw
/mkpath/;
2513 use File
::Basename qw
/dirname basename/;
2514 use vars qw
/$_minimize/;
2516 sub migrate_from_v0
{
2517 my $git_dir = $ENV{GIT_DIR
};
2518 return undef unless -d
$git_dir;
2519 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2523 my ($id, $orig_ref) = ($_, $_);
2524 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
2525 next unless -f
"$git_dir/$id/info/url";
2526 my $new_ref = "refs/remotes/$id";
2527 if (::verify_ref
("$new_ref^0")) {
2528 print STDERR
"W: $orig_ref is probably an old ",
2529 "branch used by an ancient version of ",
2531 "However, $new_ref also exists.\n",
2532 "We will not be able ",
2533 "to use this branch until this ",
2534 "ambiguity is resolved.\n";
2537 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
2538 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
2539 command_noisy
('update-ref', $new_ref, $orig_ref);
2540 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
2543 command_close_pipe
($fh, $ctx);
2544 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
2548 sub migrate_from_v1
{
2549 my $git_dir = $ENV{GIT_DIR
};
2551 return $migrated unless -d
$git_dir;
2552 my $svn_dir = "$git_dir/svn";
2554 # just in case somebody used 'svn' as their $id at some point...
2555 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
2557 print STDERR
"Migrating from a git-svn v1 layout...\n";
2559 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
2560 "$svn_dir\n\t(required for this version ",
2561 "($::VERSION) of git-svn) does not. exist\n";
2562 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2565 next unless $x =~ s
#^refs/remotes/##;
2567 next unless -f
"$git_dir/$x/info/url";
2568 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
2570 my $dn = dirname
("$git_dir/svn/$x");
2571 mkpath
([$dn]) unless -d
$dn;
2572 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
2573 mkpath
(["$git_dir/svn/svn"]);
2574 print STDERR
" - $git_dir/$x/info => ",
2575 "$git_dir/svn/$x/info\n";
2576 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
2578 # don't worry too much about these, they probably
2579 # don't exist with repos this old (save for index,
2580 # and we can easily regenerate that)
2581 foreach my $f (qw
/unhandled.log index .rev_db/) {
2582 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
2585 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
2586 rename "$git_dir/$x", "$git_dir/svn/$x" or
2591 command_close_pipe
($fh, $ctx);
2592 print STDERR
"Done migrating from a git-svn v1 layout\n";
2597 my ($l_map, $pfx, $path) = @_;
2599 foreach (<$path/*>) {
2600 if (-r
"$_/info/url") {
2601 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2602 my $ref_id = $pfx . basename
$_;
2603 my $url = ::file_to_s
("$_/info/url");
2604 $l_map->{$ref_id} = $url;
2611 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
2612 read_old_urls
($l_map, $x, $_);
2616 sub migrate_from_v2
{
2617 my @cfg = command
(qw
/config -l/);
2618 return if grep /^svn-remote\..+\.url=/, @cfg;
2620 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
2623 foreach my $ref_id (sort keys %l_map) {
2624 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
2630 sub minimize_connections
{
2631 my $r = Git
::SVN
::read_all_remotes
();
2633 my $root_repos = {};
2634 foreach my $repo_id (keys %$r) {
2635 my $url = $r->{$repo_id}->{url
} or next;
2636 my $fetch = $r->{$repo_id}->{fetch
} or next;
2637 my $ra = Git
::SVN
::Ra
->new($url);
2639 # skip existing cases where we already connect to the root
2640 if (($ra->{url
} eq $ra->{repos_root
}) ||
2641 (Git
::SVN
::sanitize_remote_name
($ra->{repos_root
}) eq
2643 $root_repos->{$ra->{url
}} = $repo_id;
2647 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
2648 my $root_path = $ra->{url
};
2649 $root_path =~ s
#^\Q$ra->{repos_root}\E/*##;
2650 foreach my $path (keys %$fetch) {
2651 my $ref_id = $fetch->{$path};
2652 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
2654 # make sure we can read when connecting to
2655 # a higher level of a repository
2656 my ($last_rev, undef) = $gs->last_rev_commit;
2657 if (!defined $last_rev) {
2659 $root_ra->get_latest_revnum;
2663 my $new = $root_path;
2664 $new .= length $path ?
"/$path" : '';
2666 $root_ra->get_log([$new], $last_rev, $last_rev,
2670 $new_urls->{$ra->{repos_root
}}->{$new} =
2671 { ref_id
=> $ref_id,
2672 old_repo_id
=> $repo_id,
2673 old_path
=> $path };
2678 foreach my $url (keys %$new_urls) {
2679 # see if we can re-use an existing [svn-remote "repo_id"]
2680 # instead of creating a(n ugly) new section:
2681 my $repo_id = $root_repos->{$url} ||
2682 Git
::SVN
::sanitize_remote_name
($url);
2684 my $fetch = $new_urls->{$url};
2685 foreach my $path (keys %$fetch) {
2686 my $x = $fetch->{$path};
2687 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
2688 my $pfx = "svn-remote.$x->{old_repo_id}";
2690 my $old_fetch = quotemeta("$x->{old_path}:".
2691 "refs/remotes/$x->{ref_id}");
2692 command_noisy
(qw
/config --unset/,
2693 "$pfx.fetch", '^'. $old_fetch . '$');
2694 delete $r->{$x->{old_repo_id
}}->
2695 {fetch
}->{$x->{old_path
}};
2696 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
2697 command_noisy
(qw
/config --unset/,
2699 push @emptied, $x->{old_repo_id
}
2704 my $file = $ENV{GIT_CONFIG
} || $ENV{GIT_CONFIG_LOCAL
} ||
2705 "$ENV{GIT_DIR}/config";
2707 The following [svn-remote] sections in your config file ($file) are empty
2708 and can be safely removed:
2710 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
2714 sub migration_check
{
2718 minimize_connections
() if $_minimize;
2725 $log_entry hashref as returned by libsvn_log_entry
()
2727 log => 'whitespace-formatted log entry
2728 ', # trailing newline is preserved
2729 revision
=> '8', # integer
2730 date
=> '2004-02-24T17:01:44.108345Z', # commit date
2731 author
=> 'committer name'
2734 @mods = array of diff
-index line hashes
, each element represents one line
2735 of diff
-index output
2737 diff
-index line
($m hash
)
2739 mode_a
=> first column of diff
-index output
, no leading
':',
2740 mode_b
=> second column of diff
-index output
,
2741 sha1_b
=> sha1sum of the final blob
,
2742 chg
=> change type
[MCRADT
],
2743 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
2744 file_b
=> new
/current file name of a file
(any chg
)
2748 # retval of read_url_paths{,_all}();
2750 # repository root url
2751 'https://svn.musicpd.org' => {
2752 # repository path # GIT_SVN_ID
2753 'mpd/trunk' => 'trunk',
2754 'mpd/tags/0.11.5' => 'tags/0.11.5',
2759 I don
't trust the each() function on unless I created %hash myself
2760 because the internal iterator may not have started at base.