2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
7 use vars qw
/ $AUTHOR $VERSION
8 $sha1 $sha1_short $_revision $_repository
9 $_q $_authors $_authors_prog %users/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
14 use Git
::SVN
::Utils
qw(fatal can_compress);
16 # From which subdir have we been invoked?
17 my $cmd_dir_prefix = eval {
18 command_oneline
([qw
/rev-parse --show-prefix/], STDERR
=> 0)
21 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR
};
22 $ENV{GIT_DIR
} ||= '.git';
23 $Git::SVN
::default_repo_id
= 'svn';
24 $Git::SVN
::default_ref_id
= $ENV{GIT_SVN_ID
} || 'git-svn';
25 $Git::SVN
::Ra
::_log_window_size
= 100;
26 $Git::SVN
::_minimize_url
= 'unset';
28 if (! exists $ENV{SVN_SSH
} && exists $ENV{GIT_SSH
}) {
29 $ENV{SVN_SSH
} = $ENV{GIT_SSH
};
32 if (exists $ENV{SVN_SSH
} && $^O
eq 'msys') {
33 $ENV{SVN_SSH
} =~ s/\\/\\\\/g;
34 $ENV{SVN_SSH
} =~ s/(.*)/"$1"/;
37 $Git::SVN
::Log
::TZ
= $ENV{TZ
};
39 $| = 1; # unbuffer STDOUT
41 # All SVN commands do it. Otherwise we may die on SIGPIPE when the remote
42 # repository decides to close the connection which we expect to be kept alive.
43 $SIG{PIPE
} = 'IGNORE';
45 # Given a dot separated version number, "subtract" it from
46 # the SVN::Core::VERSION; non-negaitive return means the SVN::Core
47 # is at least at the version the caller asked for.
48 sub compare_svn_version
{
49 my (@ours) = split(/\./, $SVN::Core
::VERSION
);
50 my (@theirs) = split(/\./, $_[0]);
53 for ($i = 0; $i < @ours && $i < @theirs; $i++) {
54 $diff = $ours[$i] - $theirs[$i];
55 return $diff if ($diff);
57 return 1 if ($i < @ours);
58 return -1 if ($i < @theirs);
63 require SVN
::Core
; # use()-ing this causes segfaults for me... *shrug*
66 if (::compare_svn_version
('1.1.0') < 0) {
67 fatal
"Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
74 use File
::Basename qw
/dirname basename/;
75 use File
::Path qw
/mkpath/;
78 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
81 use Git
::SVN
::Editor qw
//;
82 use Git
::SVN
::Fetcher qw
//;
83 use Git
::SVN
::Ra qw
//;
84 use Git
::SVN
::Prompt qw
//;
85 use Memoize
; # core since 5.8.0, Jul 2002
88 # import functions from Git into our packages, en masse
90 foreach (qw
/command command_oneline command_noisy command_output_pipe
91 command_input_pipe command_close_pipe
92 command_bidi_pipe command_close_bidi_pipe
/) {
93 for my $package ( qw(Git::SVN::Migration Git::SVN::Log),
95 *{"${package}::$_"} = \
&{"Git::$_"};
98 Memoize
::memoize
'Git::config';
99 Memoize
::memoize
'Git::config_bool';
104 $sha1 = qr/[a-f\d]{40}/;
105 $sha1_short = qr/[a-f\d]{4,40}/;
106 my ($_stdin, $_help, $_edit,
107 $_message, $_file, $_branch_dest,
108 $_template, $_shared,
109 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
110 $_merge, $_strategy, $_preserve_merges, $_dry_run, $_local,
111 $_prefix, $_no_checkout, $_url, $_verbose,
112 $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive);
114 # This is a refactoring artifact so Git::SVN can get at this git-svn switch.
115 sub opt_prefix
{ return $_prefix || '' }
117 $Git::SVN
::_follow_parent
= 1;
118 $Git::SVN
::Fetcher
::_placeholder_filename
= ".gitignore";
120 my %remote_opts = ( 'username=s' => \
$Git::SVN
::Prompt
::_username
,
121 'config-dir=s' => \
$Git::SVN
::Ra
::config_dir
,
122 'no-auth-cache' => \
$Git::SVN
::Prompt
::_no_auth_cache
,
123 'ignore-paths=s' => \
$Git::SVN
::Fetcher
::_ignore_regex
,
124 'ignore-refs=s' => \
$Git::SVN
::Ra
::_ignore_refs_regex
);
125 my %fc_opts = ( 'follow-parent|follow!' => \
$Git::SVN
::_follow_parent
,
126 'authors-file|A=s' => \
$_authors,
127 'authors-prog=s' => \
$_authors_prog,
128 'repack:i' => \
$Git::SVN
::_repack
,
129 'noMetadata' => \
$Git::SVN
::_no_metadata
,
130 'useSvmProps' => \
$Git::SVN
::_use_svm_props
,
131 'useSvnsyncProps' => \
$Git::SVN
::_use_svnsync_props
,
132 'log-window-size=i' => \
$Git::SVN
::Ra
::_log_window_size
,
133 'no-checkout' => \
$_no_checkout,
135 'repack-flags|repack-args|repack-opts=s' =>
136 \
$Git::SVN
::_repack_flags
,
137 'use-log-author' => \
$Git::SVN
::_use_log_author
,
138 'add-author-from' => \
$Git::SVN
::_add_author_from
,
139 'localtime' => \
$Git::SVN
::_localtime
,
142 my ($_trunk, @_tags, @_branches, $_stdlayout);
144 my %init_opts = ( 'template=s' => \
$_template, 'shared:s' => \
$_shared,
145 'trunk|T=s' => \
$_trunk, 'tags|t=s@' => \
@_tags,
146 'branches|b=s@' => \
@_branches, 'prefix=s' => \
$_prefix,
147 'stdlayout|s' => \
$_stdlayout,
148 'minimize-url|m!' => \
$Git::SVN
::_minimize_url
,
149 'no-metadata' => sub { $icv{noMetadata
} = 1 },
150 'use-svm-props' => sub { $icv{useSvmProps
} = 1 },
151 'use-svnsync-props' => sub { $icv{useSvnsyncProps
} = 1 },
152 'rewrite-root=s' => sub { $icv{rewriteRoot
} = $_[1] },
153 'rewrite-uuid=s' => sub { $icv{rewriteUUID
} = $_[1] },
155 my %cmt_opts = ( 'edit|e' => \
$_edit,
156 'rmdir' => \
$Git::SVN
::Editor
::_rmdir
,
157 'find-copies-harder' => \
$Git::SVN
::Editor
::_find_copies_harder
,
158 'l=i' => \
$Git::SVN
::Editor
::_rename_limit
,
159 'copy-similarity|C=i'=> \
$Git::SVN
::Editor
::_cp_similarity
163 fetch
=> [ \
&cmd_fetch
, "Download new revisions from SVN",
164 { 'revision|r=s' => \
$_revision,
165 'fetch-all|all' => \
$_fetch_all,
166 'parent|p' => \
$_fetch_parent,
168 clone
=> [ \
&cmd_clone
, "Initialize and fetch revisions",
169 { 'revision|r=s' => \
$_revision,
170 'preserve-empty-dirs' =>
171 \
$Git::SVN
::Fetcher
::_preserve_empty_dirs
,
172 'placeholder-filename=s' =>
173 \
$Git::SVN
::Fetcher
::_placeholder_filename
,
174 %fc_opts, %init_opts } ],
175 init
=> [ \
&cmd_init
, "Initialize a repo for tracking" .
176 " (requires URL argument)",
178 'multi-init' => [ \
&cmd_multi_init
,
179 "Deprecated alias for ".
180 "'$0 init -T<trunk> -b<branches> -t<tags>'",
182 dcommit
=> [ \
&cmd_dcommit
,
183 'Commit several diffs to merge with upstream',
184 { 'merge|m|M' => \
$_merge,
185 'strategy|s=s' => \
$_strategy,
186 'verbose|v' => \
$_verbose,
187 'dry-run|n' => \
$_dry_run,
188 'fetch-all|all' => \
$_fetch_all,
189 'commit-url=s' => \
$_commit_url,
190 'revision|r=i' => \
$_revision,
191 'no-rebase' => \
$_no_rebase,
192 'mergeinfo=s' => \
$_merge_info,
193 'interactive|i' => \
$_interactive,
194 %cmt_opts, %fc_opts } ],
195 branch
=> [ \
&cmd_branch
,
196 'Create a branch in the SVN repository',
197 { 'message|m=s' => \
$_message,
198 'destination|d=s' => \
$_branch_dest,
199 'dry-run|n' => \
$_dry_run,
201 'username=s' => \
$Git::SVN
::Prompt
::_username
,
202 'commit-url=s' => \
$_commit_url } ],
203 tag
=> [ sub { $_tag = 1; cmd_branch
(@_) },
204 'Create a tag in the SVN repository',
205 { 'message|m=s' => \
$_message,
206 'destination|d=s' => \
$_branch_dest,
207 'dry-run|n' => \
$_dry_run,
208 'username=s' => \
$Git::SVN
::Prompt
::_username
,
209 'commit-url=s' => \
$_commit_url } ],
210 'set-tree' => [ \
&cmd_set_tree
,
211 "Set an SVN repository to a git tree-ish",
212 { 'stdin' => \
$_stdin, %cmt_opts, %fc_opts, } ],
213 'create-ignore' => [ \
&cmd_create_ignore
,
214 'Create a .gitignore per svn:ignore',
215 { 'revision|r=i' => \
$_revision
217 'mkdirs' => [ \
&cmd_mkdirs
,
218 "recreate empty directories after a checkout",
219 { 'revision|r=i' => \
$_revision } ],
220 'propget' => [ \
&cmd_propget
,
221 'Print the value of a property on a file or directory',
222 { 'revision|r=i' => \
$_revision } ],
223 'proplist' => [ \
&cmd_proplist
,
224 'List all properties of a file or directory',
225 { 'revision|r=i' => \
$_revision } ],
226 'show-ignore' => [ \
&cmd_show_ignore
, "Show svn:ignore listings",
227 { 'revision|r=i' => \
$_revision
229 'show-externals' => [ \
&cmd_show_externals
, "Show svn:externals listings",
230 { 'revision|r=i' => \
$_revision
232 'multi-fetch' => [ \
&cmd_multi_fetch
,
233 "Deprecated alias for $0 fetch --all",
234 { 'revision|r=s' => \
$_revision, %fc_opts } ],
235 'migrate' => [ sub { },
236 # no-op, we automatically run this anyways,
237 'Migrate configuration/metadata/layout from
238 previous versions of git-svn',
239 { 'minimize' => \
$Git::SVN
::Migration
::_minimize
,
241 'log' => [ \
&Git
::SVN
::Log
::cmd_show_log
, 'Show commit logs',
242 { 'limit=i' => \
$Git::SVN
::Log
::limit
,
243 'revision|r=s' => \
$_revision,
244 'verbose|v' => \
$Git::SVN
::Log
::verbose
,
245 'incremental' => \
$Git::SVN
::Log
::incremental
,
246 'oneline' => \
$Git::SVN
::Log
::oneline
,
247 'show-commit' => \
$Git::SVN
::Log
::show_commit
,
248 'non-recursive' => \
$Git::SVN
::Log
::non_recursive
,
249 'authors-file|A=s' => \
$_authors,
250 'color' => \
$Git::SVN
::Log
::color
,
251 'pager=s' => \
$Git::SVN
::Log
::pager
253 'find-rev' => [ \
&cmd_find_rev
,
254 "Translate between SVN revision numbers and tree-ish",
256 'rebase' => [ \
&cmd_rebase
, "Fetch and rebase your working directory",
257 { 'merge|m|M' => \
$_merge,
258 'verbose|v' => \
$_verbose,
259 'strategy|s=s' => \
$_strategy,
260 'local|l' => \
$_local,
261 'fetch-all|all' => \
$_fetch_all,
262 'dry-run|n' => \
$_dry_run,
263 'preserve-merges|p' => \
$_preserve_merges,
265 'commit-diff' => [ \
&cmd_commit_diff
,
266 'Commit a diff between two trees',
267 { 'message|m=s' => \
$_message,
268 'file|F=s' => \
$_file,
269 'revision|r=s' => \
$_revision,
271 'info' => [ \
&cmd_info
,
272 "Show info about the latest SVN revision
273 on the current branch",
274 { 'url' => \
$_url, } ],
275 'blame' => [ \
&Git
::SVN
::Log
::cmd_blame
,
276 "Show what revision and author last modified each line of a file",
277 { 'git-format' => \
$_git_format } ],
278 'reset' => [ \
&cmd_reset
,
279 "Undo fetches back to the specified SVN revision",
280 { 'revision|r=s' => \
$_revision,
281 'parent|p' => \
$_fetch_parent } ],
283 "Compress unhandled.log files in .git/svn and remove " .
284 "index files in .git/svn",
291 my ($class, $reason) = @_;
292 return bless \
$reason, shift;
296 die "Cannot use readline on FakeTerm: $$self";
301 $ENV{"GIT_SVN_NOTTY"}
302 ? new Term
::ReadLine
'git-svn', \
*STDIN
, \
*STDOUT
303 : new Term
::ReadLine
'git-svn';
306 $term = new FakeTerm
"$@: going non-interactive";
310 for (my $i = 0; $i < @ARGV; $i++) {
311 if (defined $cmd{$ARGV[$i]}) {
315 } elsif ($ARGV[$i] eq 'help') {
321 # make sure we're always running at the top-level working directory
322 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
323 unless (-d
$ENV{GIT_DIR
}) {
324 if ($git_dir_user_set) {
325 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
326 "but it is not a directory\n";
328 my $git_dir = delete $ENV{GIT_DIR
};
331 $cdup = command_oneline
(qw
/rev-parse --show-cdup/);
332 $git_dir = '.' unless ($cdup);
333 chomp $cdup if ($cdup);
334 $cdup = "." unless ($cdup && length $cdup);
335 } "Already at toplevel, but $git_dir not found\n";
336 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
337 unless (-d
$git_dir) {
338 die "$git_dir still not found after going to ",
341 $ENV{GIT_DIR
} = $git_dir;
343 $_repository = Git
->repository(Repository
=> $ENV{GIT_DIR
});
346 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
348 read_git_config
(\
%opts);
349 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
350 Getopt
::Long
::Configure
('pass_through');
352 my $rv = GetOptions
(%opts, 'h|H' => \
$_help, 'version|V' => \
$_version,
353 'minimize-connections' => \
$Git::SVN
::Migration
::_minimize
,
354 'id|i=s' => \
$Git::SVN
::default_ref_id
,
355 'svn-remote|remote|R=s' => sub {
356 $Git::SVN
::no_reuse_existing
= 1;
357 $Git::SVN
::default_repo_id
= $_[1] });
358 exit 1 if (!$rv && $cmd && $cmd ne 'log');
361 version
() if $_version;
362 usage
(1) unless defined $cmd;
363 load_authors
() if $_authors;
364 if (defined $_authors_prog) {
365 $_authors_prog = "'" . File
::Spec
->rel2abs($_authors_prog) . "'";
368 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
369 Git
::SVN
::Migration
::migration_check
();
371 Git
::SVN
::init_vars
();
373 Git
::SVN
::verify_remotes_sanity
();
374 $cmd{$cmd}->[0]->(@ARGV);
375 post_fetch_checkout
();
380 ####################### primary functions ######################
382 my $exit = shift || 0;
383 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
385 git
-svn
- bidirectional operations between a single Subversion tree
and git
386 Usage
: git svn
<command
> [options
] [arguments
]\n
388 print $fd "Available commands:\n" unless $cmd;
390 foreach (sort keys %cmd) {
391 next if $cmd && $cmd ne $_;
392 next if /^multi-/; # don't show deprecated commands
393 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
394 foreach (sort keys %{$cmd{$_}->[2]}) {
395 # mixed-case options are for .git/config only
396 next if /[A-Z]/ && /^[a-z]+$/i;
397 # prints out arguments as they should be passed:
398 my $x = s
#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
399 print $fd ' ' x
21, join(', ', map { length $_ > 1 ?
401 split /\|/,$_)," $x\n";
405 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
406 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
407 one git repository and want to keep them separate. See git-svn(1) for more
415 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
420 my ($prompt, %arg) = @_;
421 my $valid_re = $arg{valid_re};
422 my $default = $arg{default};
426 if ( !( defined($term->IN)
427 && defined( fileno($term->IN) )
428 && defined( $term->OUT )
429 && defined( fileno($term->OUT) ) ) ){
430 return defined($default) ? $default : undef;
434 $resp = $term->readline($prompt);
435 if (!defined $resp) { # EOF
437 return defined $default ? $default : undef;
439 if ($resp eq '' and defined $default) {
442 if (!defined $valid_re or $resp =~ /$valid_re/) {
450 unless (-d $ENV{GIT_DIR}) {
451 my @init_db = ('init
');
452 push @init_db, "--template=$_template" if defined $_template;
453 if (defined $_shared) {
454 if ($_shared =~ /[a-z]/) {
455 push @init_db, "--shared=$_shared";
457 push @init_db, "--shared";
460 command_noisy(@init_db);
461 $_repository = Git->repository(Repository => ".git");
464 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
465 foreach my $i (keys %icv) {
466 die "'$set' and '$i' cannot both be set\n" if $set;
467 next unless defined $icv{$i};
468 command_noisy('config
', "$pfx.$i", $icv{$i});
471 my $ignore_paths_regex = \$Git::SVN::Fetcher::_ignore_regex;
472 command_noisy('config
', "$pfx.ignore-paths", $$ignore_paths_regex)
473 if defined $$ignore_paths_regex;
474 my $ignore_refs_regex = \$Git::SVN::Ra::_ignore_refs_regex;
475 command_noisy('config
', "$pfx.ignore-refs", $$ignore_refs_regex)
476 if defined $$ignore_refs_regex;
478 if (defined $Git::SVN::Fetcher::_preserve_empty_dirs) {
479 my $fname = \$Git::SVN::Fetcher::_placeholder_filename;
480 command_noisy('config
', "$pfx.preserve-empty-dirs", 'true
');
481 command_noisy('config
', "$pfx.placeholder-filename", $$fname);
486 my $repo_path = shift or return;
487 mkpath([$repo_path]) unless -d $repo_path;
488 chdir $repo_path or die "Couldn't
chdir to
$repo_path: $!\n";
489 $ENV{GIT_DIR} = '.git';
490 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
494 my ($url, $path) = @_;
495 if (!defined $path &&
496 (defined $_trunk || @_branches || @_tags ||
497 defined $_stdlayout) &&
498 $url !~ m#^[a-z\+]+://#) {
501 $path = basename($url) if !defined $path || !length $path;
502 my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
503 cmd_init($url, $path);
504 command_oneline('config', 'svn.authorsfile', $authors_absolute)
506 Git::SVN::fetch_all($Git::SVN::default_repo_id);
510 if (defined $_stdlayout) {
511 $_trunk = 'trunk' if (!defined $_trunk);
512 @_tags = 'tags' if (! @_tags);
513 @_branches = 'branches' if (! @_branches);
515 if (defined $_trunk || @_branches || @_tags) {
516 return cmd_multi_init(@_);
518 my $url = shift or die "SVN repository location required
",
519 "as a command
-line argument
\n";
520 $url = canonicalize_url($url);
524 if ($Git::SVN::_minimize_url eq 'unset') {
525 $Git::SVN::_minimize_url = 0;
528 Git::SVN->init($url);
532 if (grep /^\d+=./, @_) {
533 die "'<rev>=<commit>' fetch arguments are
",
534 "no longer supported
.\n";
538 die "Usage
: $0 fetch
[--all
] [--parent
] [svn
-remote
]\n";
540 $Git::SVN::no_reuse_existing = undef;
541 if ($_fetch_parent) {
542 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
544 die "Unable to determine upstream SVN information from
",
545 "working tree history
\n";
547 # just fetch, don't checkout.
548 $_no_checkout = 'true';
549 $_fetch_all ? $gs->fetch_all : $gs->fetch;
550 } elsif ($_fetch_all) {
553 $remote ||= $Git::SVN::default_repo_id;
554 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
560 if ($_stdin || !@commits) {
561 print "Reading from stdin
...\n";
564 if (/\b($sha1_short)\b/o) {
565 unshift @commits, $1;
570 foreach my $c (@commits) {
571 my @tmp = command('rev-parse',$c);
572 if (scalar @tmp == 1) {
574 } elsif (scalar @tmp > 1) {
575 push @revs, reverse(command('rev-list',@tmp));
577 fatal "Failed to rev
-parse
$c";
580 my $gs = Git::SVN->new;
581 my ($r_last, $cmt_last) = $gs->last_rev_commit;
583 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
584 fatal "There are new revisions that were fetched
",
585 "and need to be merged
(or acknowledged
) ",
586 "before committing
.\nlast rev
: $r_last\n",
587 " current
: $gs->{last_rev
}";
589 $gs->set_tree($_) foreach @revs;
590 print "Done committing
",scalar @revs," revisions to SVN
\n";
594 sub split_merge_info_range {
596 if ($range =~ /(\d+)-(\d+)/) {
597 return (int($1), int($2));
599 return (int($range), int($range));
607 my @arr = split(/,/, $in);
608 for my $element (@arr) {
609 my ($start, $end) = split_merge_info_range($element);
613 my @sorted = @arr [ sort {
614 $fnums[$a] <=> $fnums[$b]
620 for my $element (@sorted) {
621 my ($start, $end) = split_merge_info_range($element);
628 if ($start <= $last+1) {
634 if ($first == $last) {
635 push @return, "$first";
637 push @return, "$first-$last";
644 if ($first == $last) {
645 push @return, "$first";
647 push @return, "$first-$last";
651 return join(',', @return);
654 sub merge_revs_into_hash {
655 my ($hash, $minfo) = @_;
656 my @lines = split(' ', $minfo);
658 for my $line (@lines) {
659 my ($branchpath, $revs) = split(/:/, $line);
661 if (exists($hash->{$branchpath})) {
662 # Merge the two revision sets
663 my $combined = "$hash->{$branchpath},$revs";
664 $hash->{$branchpath} = combine_ranges($combined);
666 # Just do range combining for consolidation
667 $hash->{$branchpath} = combine_ranges($revs);
672 sub merge_merge_info {
673 my ($mergeinfo_one, $mergeinfo_two) = @_;
674 my %result_hash = ();
676 merge_revs_into_hash(\%result_hash, $mergeinfo_one);
677 merge_revs_into_hash(\%result_hash, $mergeinfo_two);
680 # Sort below is for consistency's sake
681 for my $branchname (sort keys(%result_hash)) {
682 my $revlist = $result_hash{$branchname};
683 $result .= "$branchname:$revlist\n"
688 sub populate_merge_info {
689 my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_;
692 read_commit_parents(\%parentshash, $d);
693 my @parents = @{$parentshash{$d}};
696 my $all_parents_ok = 1;
697 my $aggregate_mergeinfo = '';
698 my $rooturl = $gs->repos_root;
700 if (defined($rewritten_parent)) {
701 # Replace first parent with newly-rewritten version
703 unshift @parents, $rewritten_parent;
706 foreach my $parent (@parents) {
707 my ($branchurl, $svnrev, $paruuid) =
708 cmt_metadata($parent);
710 unless (defined($svnrev)) {
711 # Should have been caught be preflight check
712 fatal "merge commit
$d has ancestor
$parent, but that change
"
713 ."does
not have git
-svn metadata
!";
715 unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
716 fatal "commit
$parent git
-svn metadata changed mid
-run
!";
720 my $ra = Git::SVN::Ra->new($branchurl);
721 my (undef, undef, $props) =
722 $ra->get_dir(canonicalize_path("."), $svnrev);
723 my $par_mergeinfo = $props->{'svn:mergeinfo'};
724 unless (defined $par_mergeinfo) {
727 # Merge previous mergeinfo values
728 $aggregate_mergeinfo =
729 merge_merge_info($aggregate_mergeinfo,
732 next if $parent eq $parents[0]; # Skip first parent
733 # Add new changes being placed in tree by merge
734 my @cmd = (qw/rev-list --reverse/,
736 foreach my $par (@parents) {
737 unless ($par eq $parent) {
742 my ($revlist, $ctx) = command_output_pipe(@cmd);
746 my (undef, $csvnrev, undef) =
748 unless (defined $csvnrev) {
749 # A child is missing SVN annotations...
750 # this might be OK, or might not be.
751 warn "W
:child
$irev is merged into revision
"
752 ."$d but does
not have git
-svn metadata
. "
753 ."This means git
-svn cannot determine the
"
754 ."svn revision numbers to place into the
"
755 ."svn
:mergeinfo property
. You must ensure
"
756 ."a branch is entirely committed to
"
757 ."SVN before merging it
in order
for "
758 ."svn
:mergeinfo population to function
"
761 push @revsin, $csvnrev;
763 command_close_pipe($revlist, $ctx);
765 last unless $all_parents_ok;
767 # We now have a list of all SVN revnos which are
768 # merged by this particular parent. Integrate them.
769 next if $#revsin == -1;
770 my $newmergeinfo = "$branchpath:" . join(',', @revsin);
771 $aggregate_mergeinfo =
772 merge_merge_info($aggregate_mergeinfo,
775 if ($all_parents_ok and $aggregate_mergeinfo) {
776 return $aggregate_mergeinfo;
785 command_noisy(qw/update-index --refresh/);
786 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
787 'Cannot dcommit with a dirty index. Commit your changes first, '
788 . "or stash them with
`git stash'.\n";
792 if ($head ne 'HEAD') {
794 command_oneline([qw/symbolic-ref -q HEAD/])
797 $old_head =~ s{^refs/heads/}{};
799 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
801 command(['checkout', $head], STDERR => 0);
805 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
807 die "Unable to determine upstream SVN information from ",
808 "$head history.\nPerhaps the repository is empty.";
811 if (defined $_commit_url) {
814 $url = eval { command_oneline('config', '--get',
815 "svn-remote.$gs->{repo_id}.commiturl") };
817 $url = $gs->full_pushurl
821 my $last_rev = $_revision if defined $_revision;
823 print "Committing to $url ...\n";
825 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
826 if ($_no_rebase && scalar(@$linear_refs) > 1) {
827 warn "Attempting to commit more than one change while ",
828 "--no-rebase is enabled.\n",
829 "If these changes depend on each other, re-running ",
830 "without --no-rebase may be required."
833 if (defined $_interactive){
834 my $ask_default = "y";
835 foreach my $d (@$linear_refs){
836 my ($fh, $ctx) = command_output_pipe(qw(show --summary), "$d");
840 command_close_pipe
($fh, $ctx);
841 $_ = ask
("Commit this patch to SVN? ([y]es (default)|[n]o|[q]uit|[a]ll): ",
842 valid_re
=> qr/^(?:yes|y|no|n|quit|q|all|a)/i,
843 default => $ask_default);
844 die "Commit this patch reply required" unless defined $_;
853 my $expect_url = $url;
855 my $push_merge_info = eval {
856 command_oneline
(qw
/config --get svn.pushmergeinfo/)
858 if (not defined($push_merge_info)
859 or $push_merge_info eq "false"
860 or $push_merge_info eq "no"
861 or $push_merge_info eq "never") {
862 $push_merge_info = 0;
865 unless (defined($_merge_info) || ! $push_merge_info) {
866 # Preflight check of changes to ensure no issues with mergeinfo
867 # This includes check for uncommitted-to-SVN parents
868 # (other than the first parent, which we will handle),
869 # information from different SVN repos, and paths
870 # which are not underneath this repository root.
871 my $rooturl = $gs->repos_root;
872 foreach my $d (@
$linear_refs) {
874 read_commit_parents
(\
%parentshash, $d);
875 my @realparents = @
{$parentshash{$d}};
876 if ($#realparents > 0) {
878 shift @realparents; # Remove/ignore first parent
879 foreach my $parent (@realparents) {
880 my ($branchurl, $svnrev, $paruuid) = cmt_metadata
($parent);
881 unless (defined $paruuid) {
882 # A parent is missing SVN annotations...
883 # abort the whole operation.
884 fatal
"$parent is merged into revision $d, "
885 ."but does not have git-svn metadata. "
886 ."Either dcommit the branch or use a "
887 ."local cherry-pick, FF merge, or rebase "
888 ."instead of an explicit merge commit.";
891 unless ($paruuid eq $uuid) {
892 # Parent has SVN metadata from different repository
893 fatal
"merge parent $parent for change $d has "
894 ."git-svn uuid $paruuid, while current change "
898 unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
899 # This branch is very strange indeed.
900 fatal
"merge parent $parent for $d is on branch "
901 ."$branchurl, which is not under the "
902 ."git-svn root $rooturl!";
909 my $rewritten_parent;
910 Git
::SVN
::remove_username
($expect_url);
911 if (defined($_merge_info)) {
912 $_merge_info =~ tr{ }{\n};
915 my $d = shift @
$linear_refs or last;
916 unless (defined $last_rev) {
917 (undef, $last_rev, undef) = cmt_metadata
("$d~1");
918 unless (defined $last_rev) {
919 fatal
"Unable to extract revision information ",
924 print "diff-tree $d~1 $d\n";
928 unless (defined($_merge_info) || ! $push_merge_info) {
929 $_merge_info = populate_merge_info
($d, $gs,
935 my %ed_opts = ( r
=> $last_rev,
936 log => get_commit_entry
($d)->{log},
937 ra
=> Git
::SVN
::Ra
->new($url),
938 config
=> SVN
::Core
::config_get_config
(
939 $Git::SVN
::Ra
::config_dir
944 print "Committed r$_[0]\n";
947 mergeinfo
=> $_merge_info,
949 if (!Git
::SVN
::Editor
->new(\
%ed_opts)->apply_diff) {
950 print "No changes\n$d~1 == $d\n";
951 } elsif ($parents->{$d} && @
{$parents->{$d}}) {
952 $gs->{inject_parents_dcommit
}->{$cmt_rev} =
955 $_fetch_all ?
$gs->fetch_all : $gs->fetch;
956 $last_rev = $cmt_rev;
959 # we always want to rebase against the current HEAD,
960 # not any head that was passed to us
961 my @diff = command
('diff-tree', $d,
965 @finish = rebase_cmd
();
966 print STDERR
"W: $d and ", $gs->refname,
967 " differ, using @finish:\n",
968 join("\n", @diff), "\n";
970 print "No changes between current HEAD and ",
972 "\nResetting to the latest ",
974 @finish = qw
/reset --mixed/;
976 command_noisy
(@finish, $gs->refname);
978 $rewritten_parent = command_oneline
(qw
/rev-parse HEAD/);
982 my ($url_, $rev_, $uuid_, $gs_) =
983 working_head_info
('HEAD', \
@refs);
984 my ($linear_refs_, $parents_) =
985 linearize_history
($gs_, \
@refs);
986 if (scalar(@
$linear_refs) !=
987 scalar(@
$linear_refs_)) {
988 fatal
"# of revisions changed ",
990 join("\n", @
$linear_refs),
992 join("\n", @
$linear_refs_), "\n",
993 'If you are attempting to commit ',
994 "merges, try running:\n\t",
995 'git rebase --interactive',
996 '--preserve-merges ',
998 "\nBefore dcommitting";
1000 if ($url_ ne $expect_url) {
1001 if ($url_ eq $gs->metadata_url) {
1003 "Accepting rewritten URL:",
1007 "URL mismatch after rebase:",
1008 " $url_ != $expect_url";
1011 if ($uuid_ ne $uuid) {
1012 fatal
"uuid mismatch after rebase: ",
1017 for ($i = 0; $i < scalar @
$linear_refs; $i++) {
1018 my $new = $linear_refs_->[$i] or next;
1020 $parents->{$linear_refs->[$i]};
1030 my $new_head = command_oneline
(qw
/rev-parse HEAD/);
1031 my $new_is_symbolic = eval {
1032 command_oneline
(qw
/symbolic-ref -q HEAD/);
1034 if ($new_is_symbolic) {
1035 print "dcommitted the branch ", $head, "\n";
1037 print "dcommitted on a detached HEAD because you gave ",
1038 "a revision argument.\n",
1039 "The rewritten commit is: ", $new_head, "\n";
1041 command
(['checkout', $old_head], STDERR
=> 0);
1044 unlink $gs->{index};
1048 my ($branch_name, $head) = @_;
1050 unless (defined $branch_name && length $branch_name) {
1051 die(($_tag ?
"tag" : "branch") . " name required\n");
1055 my (undef, $rev, undef, $gs) = working_head_info
($head);
1056 my $src = $gs->full_pushurl;
1058 my $remote = Git
::SVN
::read_all_remotes
()->{$gs->{repo_id
}};
1059 my $allglobs = $remote->{ $_tag ?
'tags' : 'branches' };
1061 if ($#{$allglobs} == 0) {
1062 $glob = $allglobs->[0];
1064 unless(defined $_branch_dest) {
1066 $_tag ?
"tag" : "branch",
1067 " paths defined for Subversion repository.\n",
1068 "You must specify where you want to create the ",
1069 $_tag ?
"tag" : "branch",
1070 " with the --destination argument.\n";
1072 foreach my $g (@
{$allglobs}) {
1073 my $re = Git
::SVN
::Editor
::glob2pat
($g->{path
}->{left
});
1074 if ($_branch_dest =~ /$re/) {
1079 unless (defined $glob) {
1080 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
1081 foreach my $g (@
{$allglobs}) {
1082 $g->{path
}->{left
} =~ /$dest_re/ or next;
1083 if (defined $glob) {
1084 die "Ambiguous destination: ",
1085 $_branch_dest, "\nmatches both '",
1086 $glob->{path
}->{left
}, "' and '",
1087 $g->{path
}->{left
}, "'\n";
1091 unless (defined $glob) {
1093 $_tag ?
"tag" : "branch",
1094 " destination $_branch_dest\n";
1098 my ($lft, $rgt) = @
{ $glob->{path
} }{qw
/left right/};
1100 if (defined $_commit_url) {
1101 $url = $_commit_url;
1103 $url = eval { command_oneline
('config', '--get',
1104 "svn-remote.$gs->{repo_id}.commiturl") };
1106 $url = $remote->{pushurl
} || $remote->{url
};
1109 my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
1111 if ($dst =~ /^https:/ && $src =~ /^http:/) {
1112 $src=~s/^http:/https:/;
1117 my $ctx = SVN
::Client
->new(
1118 auth
=> Git
::SVN
::Ra
::_auth_providers
(),
1120 ${ $_[0] } = defined $_message
1122 : 'Create ' . ($_tag ?
'tag ' : 'branch ' )
1128 $ctx->ls($dst, 'HEAD', 0);
1129 } and die "branch ${branch_name} already exists\n";
1131 print "Copying ${src} at r${rev} to ${dst}...\n";
1132 $ctx->copy($src, $rev, $dst)
1139 my $revision_or_hash = shift or die "SVN or git revision required ",
1140 "as a command-line argument\n";
1142 if ($revision_or_hash =~ /^r\d+$/) {
1146 my (undef, undef, $uuid, $gs) = working_head_info
($head, \
@refs);
1148 die "Unable to determine upstream SVN information from ",
1151 my $desired_revision = substr($revision_or_hash, 1);
1152 $result = $gs->rev_map_get($desired_revision, $uuid);
1154 my (undef, $rev, undef) = cmt_metadata
($revision_or_hash);
1157 print "$result\n" if $result;
1160 sub auto_create_empty_directories
{
1162 my $var = eval { command_oneline
('config', '--get', '--bool',
1163 "svn-remote.$gs->{repo_id}.automkdirs") };
1164 # By default, create empty directories by consulting the unhandled log,
1165 # but allow setting it to 'false' to skip it.
1166 return !($var && $var eq 'false');
1170 command_noisy
(qw
/update-index --refresh/);
1171 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1173 die "Unable to determine upstream SVN information from ",
1174 "working tree history\n";
1177 print "Remote Branch: " . $gs->refname . "\n";
1178 print "SVN URL: " . $url . "\n";
1181 if (command
(qw
/diff-index HEAD --/)) {
1182 print STDERR
"Cannot rebase with uncommited changes:\n";
1183 command_noisy
('status');
1187 # rebase will checkout for us, so no need to do it explicitly
1188 $_no_checkout = 'true';
1189 $_fetch_all ?
$gs->fetch_all : $gs->fetch;
1191 command_noisy
(rebase_cmd
(), $gs->refname);
1192 if (auto_create_empty_directories
($gs)) {
1197 sub cmd_show_ignore
{
1198 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1199 $gs ||= Git
::SVN
->new;
1200 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
1201 $gs->prop_walk($gs->{path
}, $r, sub {
1202 my ($gs, $path, $props) = @_;
1203 print STDOUT
"\n# $path\n";
1204 my $s = $props->{'svn:ignore'} or return;
1205 $s =~ s/[\r\n]+/\n/g;
1209 print STDOUT
"$s\n";
1213 sub cmd_show_externals
{
1214 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1215 $gs ||= Git
::SVN
->new;
1216 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
1217 $gs->prop_walk($gs->{path
}, $r, sub {
1218 my ($gs, $path, $props) = @_;
1219 print STDOUT
"\n# $path\n";
1220 my $s = $props->{'svn:externals'} or return;
1221 $s =~ s/[\r\n]+/\n/g;
1224 print STDOUT
"$s\n";
1228 sub cmd_create_ignore
{
1229 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1230 $gs ||= Git
::SVN
->new;
1231 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
1232 $gs->prop_walk($gs->{path
}, $r, sub {
1233 my ($gs, $path, $props) = @_;
1234 # $path is of the form /path/to/dir/
1235 $path = '.' . $path;
1236 # SVN can have attributes on empty directories,
1237 # which git won't track
1238 mkpath
([$path]) unless -d
$path;
1239 my $ignore = $path . '.gitignore';
1240 my $s = $props->{'svn:ignore'} or return;
1241 open(GITIGNORE
, '>', $ignore)
1242 or fatal
("Failed to open `$ignore' for writing: $!");
1243 $s =~ s/[\r\n]+/\n/g;
1246 # Prefix all patterns so that the ignore doesn't apply
1247 # to sub-directories.
1249 print GITIGNORE
"$s\n";
1251 or fatal
("Failed to close `$ignore': $!");
1252 command_noisy
('add', '-f', $ignore);
1257 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1258 $gs ||= Git
::SVN
->new;
1259 $gs->mkemptydirs($_revision);
1262 sub canonicalize_path
{
1264 my $dot_slash_added = 0;
1265 if (substr($path, 0, 1) ne "/") {
1266 $path = "./" . $path;
1267 $dot_slash_added = 1;
1269 # File::Spec->canonpath doesn't collapse x/../y into y (for a
1270 # good reason), so let's do this manually.
1272 $path =~ s
#/\.(?:/|$)#/#g;
1273 $path =~ s
#/[^/]+/\.\.##g;
1275 $path =~ s
#^\./## if $dot_slash_added;
1281 sub canonicalize_url
{
1283 $url =~ s
#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
1287 # get_svnprops(PATH)
1288 # ------------------
1289 # Helper for cmd_propget and cmd_proplist below.
1292 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1293 $gs ||= Git
::SVN
->new;
1295 # prefix THE PATH by the sub-directory from which the user
1297 $path = $cmd_dir_prefix . $path;
1298 fatal
("No such file or directory: $path") unless -e
$path;
1299 my $is_dir = -d
$path ?
1 : 0;
1300 $path = $gs->{path
} . '/' . $path;
1302 # canonicalize the path (otherwise libsvn will abort or fail to
1304 $path = canonicalize_path
($path);
1306 my $r = (defined $_revision ?
$_revision : $gs->ra->get_latest_revnum);
1309 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
1312 (undef, $props) = $gs->ra->get_file($path, $r, undef);
1317 # cmd_propget (PROP, PATH)
1318 # ------------------------
1319 # Print the SVN property PROP for PATH.
1321 my ($prop, $path) = @_;
1322 $path = '.' if not defined $path;
1323 usage
(1) if not defined $prop;
1324 my $props = get_svnprops
($path);
1325 if (not defined $props->{$prop}) {
1326 fatal
("`$path' does not have a `$prop' SVN property.");
1328 print $props->{$prop} . "\n";
1331 # cmd_proplist (PATH)
1332 # -------------------
1333 # Print the list of SVN properties for PATH.
1336 $path = '.' if not defined $path;
1337 my $props = get_svnprops
($path);
1338 print "Properties on '$path':\n";
1339 foreach (sort keys %{$props}) {
1344 sub cmd_multi_init
{
1346 unless (defined $_trunk || @_branches || @_tags) {
1350 $_prefix = '' unless defined $_prefix;
1352 $url = canonicalize_url
($url);
1356 if (defined $_trunk) {
1358 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
1359 # try both old-style and new-style lookups:
1360 my $gs_trunk = eval { Git
::SVN
->new($trunk_ref) };
1361 unless ($gs_trunk) {
1362 my ($trunk_url, $trunk_path) =
1363 complete_svn_url
($url, $_trunk);
1364 $gs_trunk = Git
::SVN
->init($trunk_url, $trunk_path,
1368 return unless @_branches || @_tags;
1369 my $ra = $url ? Git
::SVN
::Ra
->new($url) : undef;
1370 foreach my $path (@_branches) {
1371 complete_url_ls_init
($ra, $path, '--branches/-b', $_prefix);
1373 foreach my $path (@_tags) {
1374 complete_url_ls_init
($ra, $path, '--tags/-t', $_prefix.'tags/');
1378 sub cmd_multi_fetch
{
1379 $Git::SVN
::no_reuse_existing
= undef;
1380 my $remotes = Git
::SVN
::read_all_remotes
();
1381 foreach my $repo_id (sort keys %$remotes) {
1382 if ($remotes->{$repo_id}->{url
}) {
1383 Git
::SVN
::fetch_all
($repo_id, $remotes);
1388 # this command is special because it requires no metadata
1389 sub cmd_commit_diff
{
1390 my ($ta, $tb, $url) = @_;
1391 my $usage = "Usage: $0 commit-diff -r<revision> ".
1392 "<tree-ish> <tree-ish> [<URL>]";
1393 fatal
($usage) if (!defined $ta || !defined $tb);
1395 if (!defined $url) {
1396 my $gs = eval { Git
::SVN
->new };
1398 fatal
("Needed URL or usable git-svn --id in ",
1399 "the command-line\n", $usage);
1402 $svn_path = $gs->{path
};
1404 unless (defined $_revision) {
1405 fatal
("-r|--revision is a required argument\n", $usage);
1407 if (defined $_message && defined $_file) {
1408 fatal
("Both --message/-m and --file/-F specified ",
1409 "for the commit message.\n",
1410 "I have no idea what you mean");
1412 if (defined $_file) {
1413 $_message = file_to_s
($_file);
1415 $_message ||= get_commit_entry
($tb)->{log};
1417 my $ra ||= Git
::SVN
::Ra
->new($url);
1420 $r = $ra->get_latest_revnum;
1421 } elsif ($r !~ /^\d+$/) {
1422 die "revision argument: $r not understood by git-svn\n";
1424 my %ed_opts = ( r
=> $r,
1429 editor_cb
=> sub { print "Committed r$_[0]\n" },
1430 svn_path
=> $svn_path );
1431 if (!Git
::SVN
::Editor
->new(\
%ed_opts)->apply_diff) {
1432 print "No changes\n$ta == $tb\n";
1436 sub escape_uri_only
{
1439 foreach (split m{/}, $uri) {
1440 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
1448 if ($url =~ m
#^([^:]+)://([^/]*)(.*)$#) {
1449 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
1450 $url = "$scheme://$domain$uri";
1456 my $path = canonicalize_path
(defined($_[0]) ?
$_[0] : ".");
1457 my $fullpath = canonicalize_path
($cmd_dir_prefix . $path);
1459 die "Too many arguments specified\n";
1462 my ($file_type, $diff_status) = find_file_type_and_diff_status
($path);
1464 if (!$file_type && !$diff_status) {
1465 print STDERR
"svn: '$path' is not under version control\n";
1469 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1471 die "Unable to determine upstream SVN information from ",
1472 "working tree history\n";
1475 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1476 $path = "." if $path eq "";
1478 my $full_url = $url . ($fullpath eq "" ?
"" : "/$fullpath");
1481 print escape_url
($full_url), "\n";
1485 my $result = "Path: $path\n";
1486 $result .= "Name: " . basename
($path) . "\n" if $file_type ne "dir";
1487 $result .= "URL: " . escape_url
($full_url) . "\n";
1490 my $repos_root = $gs->repos_root;
1491 Git
::SVN
::remove_username
($repos_root);
1492 $result .= "Repository Root: " . escape_url
($repos_root) . "\n";
1495 $result .= "Repository Root: (offline)\n";
1498 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1499 (::compare_svn_version
('1.5.4') <= 0 || $file_type ne "dir");
1500 $result .= "Revision: " . ($diff_status eq "A" ?
0 : $rev) . "\n";
1502 $result .= "Node Kind: " .
1503 ($file_type eq "dir" ?
"directory" : "file") . "\n";
1505 my $schedule = $diff_status eq "A"
1507 : ($diff_status eq "D" ?
"delete" : "normal");
1508 $result .= "Schedule: $schedule\n";
1510 if ($diff_status eq "A") {
1511 print $result, "\n";
1515 my ($lc_author, $lc_rev, $lc_date_utc);
1516 my @args = Git
::SVN
::Log
::git_svn_log_cmd
($rev, $rev, "--", $fullpath);
1517 my $log = command_output_pipe
(@args);
1518 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1520 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1522 $lc_date_utc = Git
::SVN
::Log
::parse_git_date
($2, $3);
1523 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1524 (undef, $lc_rev, undef) = ::extract_metadata
($1);
1529 Git
::SVN
::Log
::set_local_timezone
();
1531 $result .= "Last Changed Author: $lc_author\n";
1532 $result .= "Last Changed Rev: $lc_rev\n";
1533 $result .= "Last Changed Date: " .
1534 Git
::SVN
::Log
::format_svn_date
($lc_date_utc) . "\n";
1536 if ($file_type ne "dir") {
1537 my $text_last_updated_date =
1538 ($diff_status eq "D" ?
$lc_date_utc : (stat $path)[9]);
1540 "Text Last Updated: " .
1541 Git
::SVN
::Log
::format_svn_date
($text_last_updated_date) .
1544 if ($diff_status eq "D") {
1546 command_output_pipe
(qw(cat-file blob), "HEAD:$path");
1547 if ($file_type eq "link") {
1548 my $file_name = <$fh>;
1549 $checksum = md5sum
("link $file_name");
1551 $checksum = md5sum
($fh);
1553 command_close_pipe
($fh, $ctx);
1554 } elsif ($file_type eq "link") {
1556 command
(qw(cat-file blob), "HEAD:$path");
1558 md5sum
("link " . $file_name);
1560 open FILE
, "<", $path or die $!;
1561 $checksum = md5sum
(\
*FILE
);
1562 close FILE
or die $!;
1564 $result .= "Checksum: " . $checksum . "\n";
1567 print $result, "\n";
1571 my $target = shift || $_revision or die "SVN revision required\n";
1572 $target = $1 if $target =~ /^r(\d+)$/;
1573 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1574 my ($url, $rev, $uuid, $gs) = working_head_info
('HEAD');
1576 die "Unable to determine upstream SVN information from ".
1579 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1580 die "Cannot find SVN revision $target\n" unless defined($c);
1581 $gs->rev_map_set($r, $c, 'reset', $uuid);
1582 print "r$r = $c ($gs->{ref_id})\n";
1586 if (!can_compress
()) {
1587 warn "Compress::Zlib could not be found; unhandled.log " .
1588 "files will not be compressed.\n";
1590 find
({ wanted
=> \
&gc_directory
, no_chdir
=> 1}, "$ENV{GIT_DIR}/svn");
1593 ########################### utility functions #########################
1596 my @cmd = qw
/rebase/;
1597 push @cmd, '-v' if $_verbose;
1598 push @cmd, qw
/--merge/ if $_merge;
1599 push @cmd, "--strategy=$_strategy" if $_strategy;
1600 push @cmd, "--preserve-merges" if $_preserve_merges;
1604 sub post_fetch_checkout
{
1605 return if $_no_checkout;
1606 return if verify_ref
('HEAD^0');
1607 my $gs = $Git::SVN
::_head
or return;
1609 # look for "trunk" ref if it exists
1610 my $remote = Git
::SVN
::read_all_remotes
()->{$gs->{repo_id
}};
1611 my $fetch = $remote->{fetch
};
1613 foreach my $p (keys %$fetch) {
1614 basename
($fetch->{$p}) eq 'trunk' or next;
1615 $gs = Git
::SVN
->new($fetch->{$p}, $gs->{repo_id
}, $p);
1620 command_noisy
(qw(update-ref HEAD), $gs->refname);
1621 return unless verify_ref
('HEAD^0');
1623 return if $ENV{GIT_DIR
} !~ m
#^(?:.*/)?\.git$#;
1624 my $index = $ENV{GIT_INDEX_FILE
} || "$ENV{GIT_DIR}/index";
1625 return if -f
$index;
1627 return if command_oneline
(qw
/rev-parse --is-inside-work-tree/) eq 'false';
1628 return if command_oneline
(qw
/rev-parse --is-inside-git-dir/) eq 'true';
1629 command_noisy
(qw
/read-tree -m -u -v HEAD HEAD/);
1630 print STDERR
"Checked out HEAD:\n ",
1631 $gs->full_url, " r", $gs->last_rev, "\n";
1632 if (auto_create_empty_directories
($gs)) {
1633 $gs->mkemptydirs($gs->last_rev);
1637 sub complete_svn_url
{
1638 my ($url, $path) = @_;
1640 if ($path !~ m
#^[a-z\+]+://#) {
1641 if (!defined $url || $url !~ m
#^[a-z\+]+://#) {
1642 fatal
("E: '$path' is not a complete URL ",
1643 "and a separate URL is not specified");
1645 return ($url, $path);
1650 sub complete_url_ls_init
{
1651 my ($ra, $repo_path, $switch, $pfx) = @_;
1652 unless ($repo_path) {
1653 print STDERR
"W: $switch not specified\n";
1656 $repo_path =~ s
#/+$##;
1657 if ($repo_path =~ m
#^[a-z\+]+://#) {
1658 $ra = Git
::SVN
::Ra
->new($repo_path);
1661 $repo_path =~ s
#^/+##;
1663 fatal
("E: '$repo_path' is not a complete URL ",
1664 "and a separate URL is not specified");
1667 my $url = $ra->{url
};
1668 my $gs = Git
::SVN
->init($url, undef, undef, undef, 1);
1669 my $k = "svn-remote.$gs->{repo_id}.url";
1670 my $orig_url = eval { command_oneline
(qw
/config --get/, $k) };
1671 if ($orig_url && ($orig_url ne $gs->{url
})) {
1672 die "$k already set: $orig_url\n",
1673 "wanted to set to: $gs->{url}\n";
1675 command_oneline
('config', $k, $gs->{url
}) unless $orig_url;
1676 my $remote_path = "$gs->{path}/$repo_path";
1677 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
1678 $remote_path =~ s
#/+#/#g;
1679 $remote_path =~ s
#^/##g;
1680 $remote_path .= "/*" if $remote_path !~ /\
*/;
1681 my ($n) = ($switch =~ /^--(\w+)/);
1682 if (length $pfx && $pfx !~ m
#/$#) {
1683 die "--prefix='$pfx' must have a trailing slash '/'\n";
1685 command_noisy
('config',
1687 "svn-remote.$gs->{repo_id}.$n",
1688 "$remote_path:refs/remotes/$pfx*" .
1689 ('/*' x
(($remote_path =~ tr
/*/*/) - 1)) );
1694 eval { command_oneline
([ 'rev-parse', '--verify', $ref ],
1695 { STDERR
=> 0 }); };
1698 sub get_tree_from_treeish
{
1700 # $treeish can be a symbolic ref, too:
1701 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1703 while ($type eq 'tag') {
1704 ($treeish, $type) = command
(qw
/cat-file tag/, $treeish);
1706 if ($type eq 'commit') {
1707 $expected = (grep /^tree /, command
(qw
/cat-file commit/,
1709 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1710 die "Unable to get tree from $treeish\n" unless $expected;
1711 } elsif ($type eq 'tree') {
1712 $expected = $treeish;
1714 die "$treeish is a $type, expected tree, tag or commit\n";
1719 sub get_commit_entry
{
1720 my ($treeish) = shift;
1721 my %log_entry = ( log => '', tree
=> get_tree_from_treeish
($treeish) );
1722 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1723 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1724 open my $log_fh, '>', $commit_editmsg or croak
$!;
1726 my $type = command_oneline
(qw
/cat-file -t/, $treeish);
1727 if ($type eq 'commit' || $type eq 'tag') {
1728 my ($msg_fh, $ctx) = command_output_pipe
('cat-file',
1736 $in_msg = 1 if (/^\s*$/);
1737 $author = $1 if (/^author (.*>)/);
1738 } elsif (/^git-svn-id: /) {
1739 # skip this for now, we regenerate the
1740 # correct one on re-fetch anyways
1741 # TODO: set *:merge properties or like...
1743 if (/^From:/ || /^Signed-off-by:/) {
1749 $msgbuf =~ s/\s+$//s;
1750 if ($Git::SVN
::_add_author_from
&& defined($author)
1752 $msgbuf .= "\n\nFrom: $author";
1754 print $log_fh $msgbuf or croak
$!;
1755 command_close_pipe
($msg_fh, $ctx);
1757 close $log_fh or croak
$!;
1759 if ($_edit || ($type eq 'tree')) {
1760 chomp(my $editor = command_oneline
(qw(var GIT_EDITOR)));
1761 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
1763 rename $commit_editmsg, $commit_msg or croak
$!;
1766 # SVN requires messages to be UTF-8 when entering the repo
1768 open $log_fh, '<', $commit_msg or croak
$!;
1770 chomp($log_entry{log} = <$log_fh>);
1772 my $enc = Git
::config
('i18n.commitencoding') || 'UTF-8';
1773 my $msg = $log_entry{log};
1775 eval { $msg = Encode
::decode
($enc, $msg, 1) };
1777 die "Could not decode as $enc:\n", $msg,
1778 "\nPerhaps you need to set i18n.commitencoding\n";
1781 eval { $msg = Encode
::encode
('UTF-8', $msg, 1) };
1782 die "Could not encode as UTF-8:\n$msg\n" if $@
;
1784 $log_entry{log} = $msg;
1786 close $log_fh or croak
$!;
1793 my ($str, $file, $mode) = @_;
1794 open my $fd,'>',$file or croak
$!;
1795 print $fd $str,"\n" or croak
$!;
1796 close $fd or croak
$!;
1797 chmod ($mode &~ umask, $file) if (defined $mode);
1802 open my $fd,'<',$file or croak
"$!: file: $file\n";
1805 close $fd or croak
$!;
1810 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1812 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1813 my $log = $cmd eq 'log';
1814 while (<$authors>) {
1816 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1817 my ($user, $name, $email) = ($1, $2, $3);
1819 $Git::SVN
::Log
::rusers
{"$name <$email>"} = $user;
1821 $users{$user} = [$name, $email];
1824 close $authors or croak
$!;
1827 # convert GetOpt::Long specs for use by git-config
1828 sub read_git_config
{
1831 foreach my $o (keys %$opts) {
1832 # if we have mixedCase and a long option-only, then
1833 # it's a config-only variable that we don't need for
1835 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1836 my $v = $opts->{$o};
1837 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1839 my $arg = 'git config';
1840 $arg .= ' --int' if ($o =~ /[:=]i$/);
1841 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1842 if (ref $v eq 'ARRAY') {
1843 chomp(my @tmp = `$arg --get-all svn.$key`);
1846 chomp(my $tmp = `$arg --get svn.$key`);
1847 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1852 delete @
$opts{@config_only} if @config_only;
1855 sub extract_metadata
{
1856 my $id = shift or return (undef, undef, undef);
1857 my ($url, $rev, $uuid) = ($id =~ /^\s
*git
-svn
-id
:\s
+(.*)\@
(\d
+)
1858 \s
([a
-f\d\
-]+)$/ix
);
1859 if (!defined $rev || !$uuid || !$url) {
1860 # some of the original repositories I made had
1861 # identifiers like this:
1862 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
1864 return ($url, $rev, $uuid);
1868 return extract_metadata
((grep(/^git-svn-id: /,
1869 command
(qw
/cat-file commit/, shift)))[-1]);
1872 sub cmt_sha2rev_batch
{
1874 my ($pid, $in, $out, $ctx) = command_bidi_pipe
(qw
/cat-file --batch/);
1877 foreach my $sha (@
{$list}) {
1880 print $out $sha, "\n";
1882 while (my $line = <$in>) {
1883 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1886 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1890 } elsif ($line =~ /^(git-svn-id: )/) {
1891 my (undef, $rev, undef) =
1892 extract_metadata
($line);
1896 $size -= length($line);
1897 last if ($size == 0);
1901 command_close_bidi_pipe
($pid, $in, $out, $ctx);
1906 sub working_head_info
{
1907 my ($head, $refs) = @_;
1908 my @args = qw
/rev-list --first-parent --pretty=medium/;
1909 my ($fh, $ctx) = command_output_pipe
(@args, $head);
1913 if ( m{^commit ($::sha1)$} ) {
1914 unshift @
$refs, $hash if $hash and $refs;
1918 next unless s{^\s*(git-svn-id:)}{$1};
1919 my ($url, $rev, $uuid) = extract_metadata
($_);
1920 if (defined $url && defined $rev) {
1921 next if $max{$url} and $max{$url} < $rev;
1922 if (my $gs = Git
::SVN
->find_by_url($url)) {
1923 my $c = $gs->rev_map_get($rev, $uuid);
1924 if ($c && $c eq $hash) {
1925 close $fh; # break the pipe
1926 return ($url, $rev, $uuid, $gs);
1928 $max{$url} ||= $gs->rev_map_max;
1933 command_close_pipe
($fh, $ctx);
1934 (undef, undef, undef, undef);
1937 sub read_commit_parents
{
1938 my ($parents, $c) = @_;
1939 chomp(my $p = command_oneline
(qw
/rev-list --parents -1/, $c));
1940 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1941 @
{$parents->{$c}} = split(/ /, $p);
1944 sub linearize_history
{
1945 my ($gs, $refs) = @_;
1947 foreach my $c (@
$refs) {
1948 read_commit_parents
(\
%parents, $c);
1953 my $last_svn_commit = $gs->last_commit;
1954 foreach my $c (reverse @
$refs) {
1955 next if $c eq $last_svn_commit;
1958 unshift @linear_refs, $c;
1961 # we only want the first parent to diff against for linear
1962 # history, we save the rest to inject when we finalize the
1964 my $fp_a = verify_ref
("$c~1");
1965 my $fp_b = shift @
{$parents{$c}} if $parents{$c};
1966 if (!$fp_a || !$fp_b) {
1968 "has no parent commit, and therefore ",
1969 "nothing to diff against.\n",
1970 "You should be working from a repository ",
1971 "originally created by git-svn\n";
1973 if ($fp_a ne $fp_b) {
1974 die "$c~1 = $fp_a, however parsing commit $c ",
1975 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1978 foreach my $p (@
{$parents{$c}}) {
1982 (\
@linear_refs, \
%parents);
1985 sub find_file_type_and_diff_status
{
1987 return ('dir', '') if $path eq '';
1990 command_oneline
(qw(diff --cached --name-status --), $path) || "";
1991 my $diff_status = (split(' ', $diff_output))[0] || "";
1993 my $ls_tree = command_oneline
(qw(ls-tree HEAD), $path) || "";
1995 return (undef, undef) if !$diff_status && !$ls_tree;
1997 if ($diff_status eq "A") {
1998 return ("link", $diff_status) if -l
$path;
1999 return ("dir", $diff_status) if -d
$path;
2000 return ("file", $diff_status);
2003 my $mode = (split(' ', $ls_tree))[0] || "";
2005 return ("link", $diff_status) if $mode eq "120000";
2006 return ("dir", $diff_status) if $mode eq "040000";
2007 return ("file", $diff_status);
2013 my $md5 = Digest
::MD5
->new();
2014 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
2015 $md5->addfile($arg) or croak
$!;
2016 } elsif ($ref eq 'SCALAR') {
2017 $md5->add($$arg) or croak
$!;
2019 $md5->add($arg) or croak
$!;
2021 fatal
"Can't provide MD5 hash for unknown ref type: '", $ref, "'";
2023 return $md5->hexdigest();
2027 if (can_compress
() && -f
$_ && basename
($_) eq "unhandled.log") {
2028 my $out_filename = $_ . ".gz";
2029 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
2031 my $gz = Compress
::Zlib
::gzopen
($out_filename, "ab") or
2032 die "Unable to open $out_filename: $!\n";
2035 while ($res = sysread($in_fh, my $str, 1024)) {
2036 $gz->gzwrite($str) or
2037 die "Unable to write: ".$gz->gzerror()."!\n";
2039 unlink $_ or die "unlink $File::Find::name: $!\n";
2040 } elsif (-f
$_ && basename
($_) eq "index") {
2041 unlink $_ or die "unlink $_: $!\n";
2046 package Git
::SVN
::Log
;
2049 use Git
::SVN
::Utils
qw(fatal);
2050 use POSIX qw
/strftime/;
2051 use constant commit_log_separator
=> ('-' x
72) . "\n";
2052 use vars qw
/$TZ $limit $color $pager $non_recursive $verbose $oneline
2053 %rusers $show_commit $incremental/;
2058 return 1 if defined $c->{r
};
2060 # big commit message got truncated by the 16k pretty buffer in rev-list
2061 if ($c->{l
} && $c->{l
}->[-1] eq "...\n" &&
2062 $c->{a_raw
} =~ /\@([a-f\d\-]+)>$/) {
2064 my @log = command
(qw
/cat-file commit/, $c->{c
});
2066 # shift off the headers
2067 shift @log while ($log[0] ne '');
2070 # TODO: make $c->{l} not have a trailing newline in the future
2071 @
{$c->{l
}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
2073 (undef, $c->{r
}, undef) = ::extract_metadata
(
2074 (grep(/^git-svn-id: /, @log))[-1]);
2076 return defined $c->{r
};
2080 return $color || Git
->repository->get_colorbool('color.diff');
2083 sub git_svn_log_cmd
{
2084 my ($r_min, $r_max, @args) = @_;
2086 my (@files, @log_opts);
2087 foreach my $x (@args) {
2088 if ($x eq '--' || @files) {
2091 if (::verify_ref
("$x^0")) {
2099 my ($url, $rev, $uuid, $gs) = ::working_head_info
($head);
2100 $gs ||= Git
::SVN
->_new;
2101 my @cmd = (qw
/log --abbrev-commit --pretty=raw --default/,
2103 push @cmd, '-r' unless $non_recursive;
2104 push @cmd, qw
/--raw --name-status/ if $verbose;
2105 push @cmd, '--color' if log_use_color
();
2106 push @cmd, @log_opts;
2107 if (defined $r_max && $r_max == $r_min) {
2108 push @cmd, '--max-count=1';
2109 if (my $c = $gs->rev_map_get($r_max)) {
2112 } elsif (defined $r_max) {
2113 if ($r_max < $r_min) {
2114 ($r_min, $r_max) = ($r_max, $r_min);
2116 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
2117 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
2118 # If there are no commits in the range, both $c_max and $c_min
2119 # will be undefined. If there is at least 1 commit in the
2120 # range, both will be defined.
2121 return () if !defined $c_min || !defined $c_max;
2122 if ($c_min eq $c_max) {
2123 push @cmd, '--max-count=1', $c_min;
2125 push @cmd, '--boundary', "$c_min..$c_max";
2128 return (@cmd, @files);
2131 # adapted from pager.c
2134 $ENV{GIT_PAGER_IN_USE
} = 'false';
2138 chomp($pager = command_oneline
(qw(var GIT_PAGER)));
2139 if ($pager eq 'cat') {
2142 $ENV{GIT_PAGER_IN_USE
} = defined($pager);
2146 return unless defined $pager;
2147 pipe my ($rfd, $wfd) or return;
2148 defined(my $pid = fork) or fatal
"Can't fork: $!";
2150 open STDOUT
, '>&', $wfd or
2151 fatal
"Can't redirect to stdout: $!";
2154 open STDIN
, '<&', $rfd or fatal
"Can't redirect stdin: $!";
2155 $ENV{LESS
} ||= 'FRSX';
2156 exec $pager or fatal
"Can't run pager: $! ($pager)";
2159 sub format_svn_date
{
2160 my $t = shift || time;
2161 my $gmoff = Git
::SVN
::get_tz
($t);
2162 return strftime
("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
2165 sub parse_git_date
{
2167 # Date::Parse isn't in the standard Perl distro :(
2168 if ($tz =~ s/^\+//) {
2169 $t += tz_to_s_offset
($tz);
2170 } elsif ($tz =~ s/^\-//) {
2171 $t -= tz_to_s_offset
($tz);
2176 sub set_local_timezone
{
2184 sub tz_to_s_offset
{
2187 return ($1 * 60) + ($tz * 3600);
2190 sub get_author_info
{
2191 my ($dest, $author, $t, $tz) = @_;
2192 $author =~ s/(?:^\s*|\s*$)//g;
2193 $dest->{a_raw
} = $author;
2196 $au = $rusers{$author} || undef;
2199 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
2204 $dest->{t_utc
} = parse_git_date
($t, $tz);
2207 sub process_commit
{
2208 my ($c, $r_min, $r_max, $defer) = @_;
2209 if (defined $r_min && defined $r_max) {
2210 if ($r_min == $c->{r
} && $r_min == $r_max) {
2214 return 1 if $r_min == $r_max;
2215 if ($r_min < $r_max) {
2216 # we need to reverse the print order
2217 return 0 if (defined $limit && --$limit < 0);
2221 if ($r_min != $r_max) {
2222 return 1 if ($r_min < $c->{r
});
2223 return 1 if ($r_max > $c->{r
});
2226 return 0 if (defined $limit && --$limit < 0);
2235 if (my $l = $c->{l
}) {
2236 while ($l->[0] =~ /^\s*$/) { shift @
$l }
2239 $l_fmt ||= 'A' . length($c->{r
});
2240 print 'r',pack($l_fmt, $c->{r
}),' | ';
2241 print "$c->{c} | " if $show_commit;
2244 show_commit_normal
($c);
2248 sub show_commit_changed_paths
{
2250 return unless $c->{changed
};
2251 print "Changed paths:\n", @
{$c->{changed
}};
2254 sub show_commit_normal
{
2256 print commit_log_separator
, "r$c->{r} | ";
2257 print "$c->{c} | " if $show_commit;
2258 print "$c->{a} | ", format_svn_date
($c->{t_utc
}), ' | ';
2261 if (my $l = $c->{l
}) {
2262 while ($l->[$#$l] eq "\n" && $#$l > 0
2263 && $l->[($#$l - 1)] eq "\n") {
2266 $nr_line = scalar @
$l;
2268 print "1 line\n\n\n";
2270 if ($nr_line == 1) {
2271 $nr_line = '1 line';
2273 $nr_line .= ' lines';
2275 print $nr_line, "\n";
2276 show_commit_changed_paths
($c);
2278 print $_ foreach @
$l;
2282 show_commit_changed_paths
($c);
2286 foreach my $x (qw
/raw stat diff/) {
2289 print $_ foreach @
{$c->{$x}}
2296 my ($r_min, $r_max);
2297 my $r_last = -1; # prevent dupes
2298 set_local_timezone
();
2299 if (defined $::_revision
) {
2300 if ($::_revision
=~ /^(\d+):(\d+)$/) {
2301 ($r_min, $r_max) = ($1, $2);
2302 } elsif ($::_revision
=~ /^\d+$/) {
2303 $r_min = $r_max = $::_revision
;
2305 fatal
"-r$::_revision is not supported, use ",
2306 "standard 'git log' arguments instead";
2311 @args = git_svn_log_cmd
($r_min, $r_max, @args);
2313 print commit_log_separator
unless $incremental || $oneline;
2316 my $log = command_output_pipe
(@args);
2318 my (@k, $c, $d, $stat);
2319 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
2321 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
2323 if ($c && cmt_showable
($c) && $c->{r
} != $r_last) {
2325 process_commit
($c, $r_min, $r_max, \
@k) or
2330 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
2331 get_author_info
($c, $1, $2, $3);
2332 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
2334 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
2335 push @
{$c->{raw
}}, $_;
2336 } elsif (/^${esc_color}[ACRMDT]\t/) {
2337 # we could add $SVN->{svn_path} here, but that requires
2338 # remote access at the moment (repo_path_split)...
2339 s
#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
2340 push @
{$c->{changed
}}, $_;
2341 } elsif (/^${esc_color}diff /o) {
2343 push @
{$c->{diff
}}, $_;
2345 push @
{$c->{diff
}}, $_;
2346 } elsif (/^\
.+\ \
|\s
*\d
+\
$esc_color[\
+\
-]*
2347 $esc_color*[\
+\
-]*$esc_color$/x
) {
2349 push @
{$c->{stat}}, $_;
2350 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
2351 push @
{$c->{stat}}, $_;
2353 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
2354 ($c->{url
}, $c->{r
}, undef) = ::extract_metadata
($1);
2355 } elsif (s/^${esc_color} //o) {
2356 push @
{$c->{l
}}, $_;
2359 if ($c && defined $c->{r
} && $c->{r
} != $r_last) {
2361 process_commit
($c, $r_min, $r_max, \
@k);
2364 ($r_min, $r_max) = ($r_max, $r_min);
2365 process_commit
($_, $r_min, $r_max) foreach reverse @k;
2369 print commit_log_separator
unless $incremental || $oneline;
2378 my ($fh, $ctx, $rev);
2381 ($fh, $ctx) = command_output_pipe
('blame', @_, $path);
2382 while (my $line = <$fh>) {
2383 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
2384 # Uncommitted edits show up as a rev ID of
2385 # all zeros, which we can't look up with
2388 (undef, $rev, undef) =
2390 $rev = '0' if (!$rev);
2394 $rev = sprintf('%-10s', $rev);
2395 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
2400 ($fh, $ctx) = command_output_pipe
('blame', '-p', @_, 'HEAD',
2405 my %dsha; #distinct sha keys
2407 while (my $line = <$fh>) {
2408 push @buffer, $line;
2409 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
2414 my $s2r = ::cmt_sha2rev_batch
([keys %dsha]);
2416 foreach my $line (@buffer) {
2417 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
2419 $rev = '0' if (!$rev)
2421 elsif ($line =~ /^author (.*)/) {
2422 $authors{$rev} = $1;
2423 $authors{$rev} =~ s/\s/_/g;
2425 elsif ($line =~ /^\t(.*)$/) {
2426 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
2430 command_close_pipe
($fh, $ctx);
2433 package Git
::SVN
::Migration
;
2434 # these version numbers do NOT correspond to actual version numbers
2435 # of git nor git-svn. They are just relative.
2437 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
2439 # v1 layout: .git/$id/info/url, refs/remotes/$id
2441 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
2443 # v3 layout: .git/svn/$id, refs/remotes/$id
2444 # - info/url may remain for backwards compatibility
2445 # - this is what we migrate up to this layout automatically,
2446 # - this will be used by git svn init on single branches
2447 # v3.1 layout (auto migrated):
2448 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
2449 # for backwards compatibility
2451 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
2452 # - this is only created for newly multi-init-ed
2453 # repositories. Similar in spirit to the
2454 # --use-separate-remotes option in git-clone (now default)
2455 # - we do not automatically migrate to this (following
2456 # the example set by core git)
2458 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
2459 # - newer, more-efficient format that uses 24-bytes per record
2460 # with no filler space.
2461 # - use xxd -c24 < .rev_map.$UUID to view and debug
2462 # - This is a one-way migration, repositories updated to the
2463 # new format will not be able to use old git-svn without
2464 # rebuilding the .rev_db. Rebuilding the rev_db is not
2465 # possible if noMetadata or useSvmProps are set; but should
2466 # be no problem for users that use the (sensible) defaults.
2470 use File
::Path qw
/mkpath/;
2471 use File
::Basename qw
/dirname basename/;
2472 use vars qw
/$_minimize/;
2474 sub migrate_from_v0
{
2475 my $git_dir = $ENV{GIT_DIR
};
2476 return undef unless -d
$git_dir;
2477 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2481 my ($id, $orig_ref) = ($_, $_);
2482 next unless $id =~ s
#^refs/heads/(.+)-HEAD$#$1#;
2483 next unless -f
"$git_dir/$id/info/url";
2484 my $new_ref = "refs/remotes/$id";
2485 if (::verify_ref
("$new_ref^0")) {
2486 print STDERR
"W: $orig_ref is probably an old ",
2487 "branch used by an ancient version of ",
2489 "However, $new_ref also exists.\n",
2490 "We will not be able ",
2491 "to use this branch until this ",
2492 "ambiguity is resolved.\n";
2495 print STDERR
"Migrating from v0 layout...\n" if !$migrated;
2496 print STDERR
"Renaming ref: $orig_ref => $new_ref\n";
2497 command_noisy
('update-ref', $new_ref, $orig_ref);
2498 command_noisy
('update-ref', '-d', $orig_ref, $orig_ref);
2501 command_close_pipe
($fh, $ctx);
2502 print STDERR
"Done migrating from v0 layout...\n" if $migrated;
2506 sub migrate_from_v1
{
2507 my $git_dir = $ENV{GIT_DIR
};
2509 return $migrated unless -d
$git_dir;
2510 my $svn_dir = "$git_dir/svn";
2512 # just in case somebody used 'svn' as their $id at some point...
2513 return $migrated if -d
$svn_dir && ! -f
"$svn_dir/info/url";
2515 print STDERR
"Migrating from a git-svn v1 layout...\n";
2517 print STDERR
"Data from a previous version of git-svn exists, but\n\t",
2518 "$svn_dir\n\t(required for this version ",
2519 "($::VERSION) of git-svn) does not exist.\n";
2520 my ($fh, $ctx) = command_output_pipe
(qw
/rev-parse --symbolic --all/);
2523 next unless $x =~ s
#^refs/remotes/##;
2525 next unless -f
"$git_dir/$x/info/url";
2526 my $u = eval { ::file_to_s
("$git_dir/$x/info/url") };
2528 my $dn = dirname
("$git_dir/svn/$x");
2529 mkpath
([$dn]) unless -d
$dn;
2530 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
2531 mkpath
(["$git_dir/svn/svn"]);
2532 print STDERR
" - $git_dir/$x/info => ",
2533 "$git_dir/svn/$x/info\n";
2534 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
2536 # don't worry too much about these, they probably
2537 # don't exist with repos this old (save for index,
2538 # and we can easily regenerate that)
2539 foreach my $f (qw
/unhandled.log index .rev_db/) {
2540 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
2543 print STDERR
" - $git_dir/$x => $git_dir/svn/$x\n";
2544 rename "$git_dir/$x", "$git_dir/svn/$x" or
2549 command_close_pipe
($fh, $ctx);
2550 print STDERR
"Done migrating from a git-svn v1 layout\n";
2555 my ($l_map, $pfx, $path) = @_;
2557 foreach (<$path/*>) {
2558 if (-r
"$_/info/url") {
2559 $pfx .= '/' if $pfx && $pfx !~ m
!/$!;
2560 my $ref_id = $pfx . basename
$_;
2561 my $url = ::file_to_s
("$_/info/url");
2562 $l_map->{$ref_id} = $url;
2569 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
2570 read_old_urls
($l_map, $x, $_);
2574 sub migrate_from_v2
{
2575 my @cfg = command
(qw
/config -l/);
2576 return if grep /^svn-remote\..+\.url=/, @cfg;
2578 read_old_urls
(\
%l_map, '', "$ENV{GIT_DIR}/svn");
2581 foreach my $ref_id (sort keys %l_map) {
2582 eval { Git
::SVN
->init($l_map{$ref_id}, '', undef, $ref_id) };
2584 Git
::SVN
->init($l_map{$ref_id}, '', $ref_id, $ref_id);
2591 sub minimize_connections
{
2592 my $r = Git
::SVN
::read_all_remotes
();
2594 my $root_repos = {};
2595 foreach my $repo_id (keys %$r) {
2596 my $url = $r->{$repo_id}->{url
} or next;
2597 my $fetch = $r->{$repo_id}->{fetch
} or next;
2598 my $ra = Git
::SVN
::Ra
->new($url);
2600 # skip existing cases where we already connect to the root
2601 if (($ra->{url
} eq $ra->{repos_root
}) ||
2602 ($ra->{repos_root
} eq $repo_id)) {
2603 $root_repos->{$ra->{url
}} = $repo_id;
2607 my $root_ra = Git
::SVN
::Ra
->new($ra->{repos_root
});
2608 my $root_path = $ra->{url
};
2609 $root_path =~ s
#^\Q$ra->{repos_root}\E(/|$)##;
2610 foreach my $path (keys %$fetch) {
2611 my $ref_id = $fetch->{$path};
2612 my $gs = Git
::SVN
->new($ref_id, $repo_id, $path);
2614 # make sure we can read when connecting to
2615 # a higher level of a repository
2616 my ($last_rev, undef) = $gs->last_rev_commit;
2617 if (!defined $last_rev) {
2619 $root_ra->get_latest_revnum;
2623 my $new = $root_path;
2624 $new .= length $path ?
"/$path" : '';
2626 $root_ra->get_log([$new], $last_rev, $last_rev,
2630 $new_urls->{$ra->{repos_root
}}->{$new} =
2631 { ref_id
=> $ref_id,
2632 old_repo_id
=> $repo_id,
2633 old_path
=> $path };
2638 foreach my $url (keys %$new_urls) {
2639 # see if we can re-use an existing [svn-remote "repo_id"]
2640 # instead of creating a(n ugly) new section:
2641 my $repo_id = $root_repos->{$url} || $url;
2643 my $fetch = $new_urls->{$url};
2644 foreach my $path (keys %$fetch) {
2645 my $x = $fetch->{$path};
2646 Git
::SVN
->init($url, $path, $repo_id, $x->{ref_id
});
2647 my $pfx = "svn-remote.$x->{old_repo_id}";
2649 my $old_fetch = quotemeta("$x->{old_path}:".
2651 command_noisy
(qw
/config --unset/,
2652 "$pfx.fetch", '^'. $old_fetch . '$');
2653 delete $r->{$x->{old_repo_id
}}->
2654 {fetch
}->{$x->{old_path
}};
2655 if (!keys %{$r->{$x->{old_repo_id
}}->{fetch
}}) {
2656 command_noisy
(qw
/config --unset/,
2658 push @emptied, $x->{old_repo_id
}
2663 my $file = $ENV{GIT_CONFIG
} || "$ENV{GIT_DIR}/config";
2665 The following [svn-remote] sections in your config file ($file) are empty
2666 and can be safely removed:
2668 print STDERR
"[svn-remote \"$_\"]\n" foreach @emptied;
2672 sub migration_check
{
2676 minimize_connections
() if $_minimize;
2679 package Git
::IndexInfo
;
2682 use Git qw
/command_input_pipe command_close_pipe/;
2686 my ($gui, $ctx) = command_input_pipe
(qw
/update-index -z --index-info/);
2687 bless { gui
=> $gui, ctx
=> $ctx, nr
=> 0}, $class;
2691 my ($self, $path) = @_;
2692 if (print { $self->{gui
} } '0 ', 0 x
40, "\t", $path, "\0") {
2693 return ++$self->{nr
};
2699 my ($self, $mode, $hash, $path) = @_;
2700 if (print { $self->{gui
} } $mode, ' ', $hash, "\t", $path, "\0") {
2701 return ++$self->{nr
};
2708 command_close_pipe
($self->{gui
}, $self->{ctx
});
2711 package Git
::SVN
::GlobSpec
;
2716 my ($class, $glob, $pattern_ok) = @_;
2718 $re =~ s!/+$!!g; # no need for trailing slashes
2719 my (@left, @right, @patterns);
2721 my $die_msg = "Only one set of wildcard directories " .
2722 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
2723 for my $part (split(m
|/|, $glob)) {
2724 if ($part =~ /\*/ && $part ne "*") {
2725 die "Invalid pattern in '$glob': $part\n";
2726 } elsif ($pattern_ok && $part =~ /[{}]/ &&
2727 $part !~ /^\{[^{}]+\}/) {
2728 die "Invalid pattern in '$glob': $part\n";
2731 die $die_msg if $state eq "right";
2733 push(@patterns, "[^/]*");
2734 } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) {
2735 die $die_msg if $state eq "right";
2737 my $p = quotemeta($1);
2739 push(@patterns, "(?:$p)");
2741 if ($state eq "left") {
2744 push(@right, $part);
2749 my $depth = @patterns;
2751 die "One '*' is needed in glob: '$glob'\n";
2753 my $left = join('/', @left);
2754 my $right = join('/', @right);
2755 $re = join('/', @patterns);
2757 grep(length, quotemeta($left), "($re)", quotemeta($right)));
2758 my $left_re = qr/^\/\Q
$left\E
(\
/|$)/;
2759 bless { left
=> $left, right
=> $right, left_regex
=> $left_re,
2760 regex
=> qr/$re/, glob => $glob, depth
=> $depth }, $class;
2764 my ($self, $path) = @_;
2765 return (length $self->{left
} ?
"$self->{left}/" : '') .
2766 $path . (length $self->{right
} ?
"/$self->{right}" : '');
2774 $remotes = { # returned by read_all_remotes()
2776 # svn-remote.svn.url=https://svn.musicpd.org
2777 url
=> 'https://svn.musicpd.org',
2778 # svn-remote.svn.fetch=mpd/trunk:trunk
2780 'mpd/trunk' => 'trunk',
2782 # svn-remote.svn.tags=mpd/tags/*:tags/*
2787 regex
=> qr!mpd/tags/([^/]+)$!,
2793 regex
=> qr!tags/([^/]+)$!,
2800 $log_entry hashref as returned by libsvn_log_entry
()
2802 log => 'whitespace-formatted log entry
2803 ', # trailing newline is preserved
2804 revision
=> '8', # integer
2805 date
=> '2004-02-24T17:01:44.108345Z', # commit date
2806 author
=> 'committer name'
2810 # this is generated by generate_diff();
2811 @mods = array of diff
-index line hashes
, each element represents one line
2812 of diff
-index output
2814 diff
-index line
($m hash
)
2816 mode_a
=> first column of diff
-index output
, no leading
':',
2817 mode_b
=> second column of diff
-index output
,
2818 sha1_b
=> sha1sum of the final blob
,
2819 chg
=> change type
[MCRADT
],
2820 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
2821 file_b
=> new
/current file name of a file
(any chg
)
2825 # retval of read_url_paths{,_all}();
2827 # repository root url
2828 'https://svn.musicpd.org' => {
2829 # repository path # GIT_SVN_ID
2830 'mpd/trunk' => 'trunk',
2831 'mpd/tags/0.11.5' => 'tags/0.11.5',
2836 I don
't trust the each() function on unless I created %hash myself
2837 because the internal iterator may not have started at base.