git svn dcommit: new option --interactive.
[git.git] / git-svn.perl
blobc14d813f40189b9e9cda402af5c3edc14cf86de1
1 #!/usr/bin/env perl
2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
4 use 5.008;
5 use warnings;
6 use strict;
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@@';
13 # From which subdir have we been invoked?
14 my $cmd_dir_prefix = eval {
15 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
16 } || '';
18 my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
19 $ENV{GIT_DIR} ||= '.git';
20 $Git::SVN::default_repo_id = 'svn';
21 $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
22 $Git::SVN::Ra::_log_window_size = 100;
23 $Git::SVN::_minimize_url = 'unset';
25 if (! exists $ENV{SVN_SSH}) {
26 if (exists $ENV{GIT_SSH}) {
27 $ENV{SVN_SSH} = $ENV{GIT_SSH};
28 if ($^O eq 'msys') {
29 $ENV{SVN_SSH} =~ s/\\/\\\\/g;
30 $ENV{SVN_SSH} =~ s/(.*)/"$1"/;
35 $Git::SVN::Log::TZ = $ENV{TZ};
36 $ENV{TZ} = 'UTC';
37 $| = 1; # unbuffer STDOUT
39 sub fatal (@) { print STDERR "@_\n"; exit 1 }
40 sub _req_svn {
41 require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
42 require SVN::Ra;
43 require SVN::Delta;
44 if ($SVN::Core::VERSION lt '1.1.0') {
45 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
48 my $can_compress = eval { require Compress::Zlib; 1};
49 push @Git::SVN::Ra::ISA, 'SVN::Ra';
50 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
51 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
52 use Carp qw/croak/;
53 use Digest::MD5;
54 use IO::File qw//;
55 use File::Basename qw/dirname basename/;
56 use File::Path qw/mkpath/;
57 use File::Spec;
58 use File::Find;
59 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
60 use IPC::Open3;
61 use Git;
62 use Memoize; # core since 5.8.0, Jul 2002
64 BEGIN {
65 # import functions from Git into our packages, en masse
66 no strict 'refs';
67 foreach (qw/command command_oneline command_noisy command_output_pipe
68 command_input_pipe command_close_pipe
69 command_bidi_pipe command_close_bidi_pipe/) {
70 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
71 Git::SVN::Migration Git::SVN::Log Git::SVN),
72 __PACKAGE__) {
73 *{"${package}::$_"} = \&{"Git::$_"};
76 Memoize::memoize 'Git::config';
77 Memoize::memoize 'Git::config_bool';
80 my ($SVN);
82 $sha1 = qr/[a-f\d]{40}/;
83 $sha1_short = qr/[a-f\d]{4,40}/;
84 my ($_stdin, $_help, $_edit,
85 $_message, $_file, $_branch_dest,
86 $_template, $_shared,
87 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
88 $_merge, $_strategy, $_dry_run, $_local,
89 $_prefix, $_no_checkout, $_url, $_verbose,
90 $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive);
91 $Git::SVN::_follow_parent = 1;
92 $SVN::Git::Fetcher::_placeholder_filename = ".gitignore";
93 $_q ||= 0;
94 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
95 'config-dir=s' => \$Git::SVN::Ra::config_dir,
96 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
97 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
98 my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
99 'authors-file|A=s' => \$_authors,
100 'authors-prog=s' => \$_authors_prog,
101 'repack:i' => \$Git::SVN::_repack,
102 'noMetadata' => \$Git::SVN::_no_metadata,
103 'useSvmProps' => \$Git::SVN::_use_svm_props,
104 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
105 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
106 'no-checkout' => \$_no_checkout,
107 'quiet|q+' => \$_q,
108 'repack-flags|repack-args|repack-opts=s' =>
109 \$Git::SVN::_repack_flags,
110 'use-log-author' => \$Git::SVN::_use_log_author,
111 'add-author-from' => \$Git::SVN::_add_author_from,
112 'localtime' => \$Git::SVN::_localtime,
113 %remote_opts );
115 my ($_trunk, @_tags, @_branches, $_stdlayout);
116 my %icv;
117 my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
118 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
119 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
120 'stdlayout|s' => \$_stdlayout,
121 'minimize-url|m!' => \$Git::SVN::_minimize_url,
122 'no-metadata' => sub { $icv{noMetadata} = 1 },
123 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
124 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
125 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
126 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] },
127 %remote_opts );
128 my %cmt_opts = ( 'edit|e' => \$_edit,
129 'rmdir' => \$SVN::Git::Editor::_rmdir,
130 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
131 'l=i' => \$SVN::Git::Editor::_rename_limit,
132 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
135 my %cmd = (
136 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
137 { 'revision|r=s' => \$_revision,
138 'fetch-all|all' => \$_fetch_all,
139 'parent|p' => \$_fetch_parent,
140 %fc_opts } ],
141 clone => [ \&cmd_clone, "Initialize and fetch revisions",
142 { 'revision|r=s' => \$_revision,
143 'preserve-empty-dirs' =>
144 \$SVN::Git::Fetcher::_preserve_empty_dirs,
145 'placeholder-filename=s' =>
146 \$SVN::Git::Fetcher::_placeholder_filename,
147 %fc_opts, %init_opts } ],
148 init => [ \&cmd_init, "Initialize a repo for tracking" .
149 " (requires URL argument)",
150 \%init_opts ],
151 'multi-init' => [ \&cmd_multi_init,
152 "Deprecated alias for ".
153 "'$0 init -T<trunk> -b<branches> -t<tags>'",
154 \%init_opts ],
155 dcommit => [ \&cmd_dcommit,
156 'Commit several diffs to merge with upstream',
157 { 'merge|m|M' => \$_merge,
158 'strategy|s=s' => \$_strategy,
159 'verbose|v' => \$_verbose,
160 'dry-run|n' => \$_dry_run,
161 'fetch-all|all' => \$_fetch_all,
162 'commit-url=s' => \$_commit_url,
163 'revision|r=i' => \$_revision,
164 'no-rebase' => \$_no_rebase,
165 'mergeinfo=s' => \$_merge_info,
166 'interactive|i' => \$_interactive,
167 %cmt_opts, %fc_opts } ],
168 branch => [ \&cmd_branch,
169 'Create a branch in the SVN repository',
170 { 'message|m=s' => \$_message,
171 'destination|d=s' => \$_branch_dest,
172 'dry-run|n' => \$_dry_run,
173 'tag|t' => \$_tag,
174 'username=s' => \$Git::SVN::Prompt::_username,
175 'commit-url=s' => \$_commit_url } ],
176 tag => [ sub { $_tag = 1; cmd_branch(@_) },
177 'Create a tag in the SVN repository',
178 { 'message|m=s' => \$_message,
179 'destination|d=s' => \$_branch_dest,
180 'dry-run|n' => \$_dry_run,
181 'username=s' => \$Git::SVN::Prompt::_username,
182 'commit-url=s' => \$_commit_url } ],
183 'set-tree' => [ \&cmd_set_tree,
184 "Set an SVN repository to a git tree-ish",
185 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
186 'create-ignore' => [ \&cmd_create_ignore,
187 'Create a .gitignore per svn:ignore',
188 { 'revision|r=i' => \$_revision
189 } ],
190 'mkdirs' => [ \&cmd_mkdirs ,
191 "recreate empty directories after a checkout",
192 { 'revision|r=i' => \$_revision } ],
193 'propget' => [ \&cmd_propget,
194 'Print the value of a property on a file or directory',
195 { 'revision|r=i' => \$_revision } ],
196 'proplist' => [ \&cmd_proplist,
197 'List all properties of a file or directory',
198 { 'revision|r=i' => \$_revision } ],
199 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
200 { 'revision|r=i' => \$_revision
201 } ],
202 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
203 { 'revision|r=i' => \$_revision
204 } ],
205 'multi-fetch' => [ \&cmd_multi_fetch,
206 "Deprecated alias for $0 fetch --all",
207 { 'revision|r=s' => \$_revision, %fc_opts } ],
208 'migrate' => [ sub { },
209 # no-op, we automatically run this anyways,
210 'Migrate configuration/metadata/layout from
211 previous versions of git-svn',
212 { 'minimize' => \$Git::SVN::Migration::_minimize,
213 %remote_opts } ],
214 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
215 { 'limit=i' => \$Git::SVN::Log::limit,
216 'revision|r=s' => \$_revision,
217 'verbose|v' => \$Git::SVN::Log::verbose,
218 'incremental' => \$Git::SVN::Log::incremental,
219 'oneline' => \$Git::SVN::Log::oneline,
220 'show-commit' => \$Git::SVN::Log::show_commit,
221 'non-recursive' => \$Git::SVN::Log::non_recursive,
222 'authors-file|A=s' => \$_authors,
223 'color' => \$Git::SVN::Log::color,
224 'pager=s' => \$Git::SVN::Log::pager
225 } ],
226 'find-rev' => [ \&cmd_find_rev,
227 "Translate between SVN revision numbers and tree-ish",
228 {} ],
229 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
230 { 'merge|m|M' => \$_merge,
231 'verbose|v' => \$_verbose,
232 'strategy|s=s' => \$_strategy,
233 'local|l' => \$_local,
234 'fetch-all|all' => \$_fetch_all,
235 'dry-run|n' => \$_dry_run,
236 %fc_opts } ],
237 'commit-diff' => [ \&cmd_commit_diff,
238 'Commit a diff between two trees',
239 { 'message|m=s' => \$_message,
240 'file|F=s' => \$_file,
241 'revision|r=s' => \$_revision,
242 %cmt_opts } ],
243 'info' => [ \&cmd_info,
244 "Show info about the latest SVN revision
245 on the current branch",
246 { 'url' => \$_url, } ],
247 'blame' => [ \&Git::SVN::Log::cmd_blame,
248 "Show what revision and author last modified each line of a file",
249 { 'git-format' => \$_git_format } ],
250 'reset' => [ \&cmd_reset,
251 "Undo fetches back to the specified SVN revision",
252 { 'revision|r=s' => \$_revision,
253 'parent|p' => \$_fetch_parent } ],
254 'gc' => [ \&cmd_gc,
255 "Compress unhandled.log files in .git/svn and remove " .
256 "index files in .git/svn",
257 {} ],
260 use Term::ReadLine;
261 package FakeTerm;
262 sub new {
263 my ($class, $reason) = @_;
264 return bless \$reason, shift;
266 sub readline {
267 my $self = shift;
268 die "Cannot use readline on FakeTerm: $$self";
270 package main;
272 my $term = eval {
273 $ENV{"GIT_SVN_NOTTY"}
274 ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT
275 : new Term::ReadLine 'git-svn';
277 if ($@) {
278 $term = new FakeTerm "$@: going non-interactive";
281 my $cmd;
282 for (my $i = 0; $i < @ARGV; $i++) {
283 if (defined $cmd{$ARGV[$i]}) {
284 $cmd = $ARGV[$i];
285 splice @ARGV, $i, 1;
286 last;
287 } elsif ($ARGV[$i] eq 'help') {
288 $cmd = $ARGV[$i+1];
289 usage(0);
293 # make sure we're always running at the top-level working directory
294 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
295 unless (-d $ENV{GIT_DIR}) {
296 if ($git_dir_user_set) {
297 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
298 "but it is not a directory\n";
300 my $git_dir = delete $ENV{GIT_DIR};
301 my $cdup = undef;
302 git_cmd_try {
303 $cdup = command_oneline(qw/rev-parse --show-cdup/);
304 $git_dir = '.' unless ($cdup);
305 chomp $cdup if ($cdup);
306 $cdup = "." unless ($cdup && length $cdup);
307 } "Already at toplevel, but $git_dir not found\n";
308 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
309 unless (-d $git_dir) {
310 die "$git_dir still not found after going to ",
311 "'$cdup'\n";
313 $ENV{GIT_DIR} = $git_dir;
315 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
318 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
320 read_git_config(\%opts);
321 if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
322 Getopt::Long::Configure('pass_through');
324 my $rv = GetOptions(%opts, 'h|H' => \$_help, 'version|V' => \$_version,
325 'minimize-connections' => \$Git::SVN::Migration::_minimize,
326 'id|i=s' => \$Git::SVN::default_ref_id,
327 'svn-remote|remote|R=s' => sub {
328 $Git::SVN::no_reuse_existing = 1;
329 $Git::SVN::default_repo_id = $_[1] });
330 exit 1 if (!$rv && $cmd && $cmd ne 'log');
332 usage(0) if $_help;
333 version() if $_version;
334 usage(1) unless defined $cmd;
335 load_authors() if $_authors;
336 if (defined $_authors_prog) {
337 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
340 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
341 Git::SVN::Migration::migration_check();
343 Git::SVN::init_vars();
344 eval {
345 Git::SVN::verify_remotes_sanity();
346 $cmd{$cmd}->[0]->(@ARGV);
348 fatal $@ if $@;
349 post_fetch_checkout();
350 exit 0;
352 ####################### primary functions ######################
353 sub usage {
354 my $exit = shift || 0;
355 my $fd = $exit ? \*STDERR : \*STDOUT;
356 print $fd <<"";
357 git-svn - bidirectional operations between a single Subversion tree and git
358 Usage: git svn <command> [options] [arguments]\n
360 print $fd "Available commands:\n" unless $cmd;
362 foreach (sort keys %cmd) {
363 next if $cmd && $cmd ne $_;
364 next if /^multi-/; # don't show deprecated commands
365 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
366 foreach (sort keys %{$cmd{$_}->[2]}) {
367 # mixed-case options are for .git/config only
368 next if /[A-Z]/ && /^[a-z]+$/i;
369 # prints out arguments as they should be passed:
370 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
371 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
372 "--$_" : "-$_" }
373 split /\|/,$_)," $x\n";
376 print $fd <<"";
377 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
378 arbitrary identifier if you're tracking multiple SVN branches/repositories in
379 one git repository and want to keep them separate. See git-svn(1) for more
380 information.
382 exit $exit;
385 sub version {
386 ::_req_svn();
387 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
388 exit 0;
391 sub ask {
392 my ($prompt, %arg) = @_;
393 my $valid_re = $arg{valid_re};
394 my $default = $arg{default};
395 my $resp;
396 my $i = 0;
398 if ( !( defined($term->IN)
399 && defined( fileno($term->IN) )
400 && defined( $term->OUT )
401 && defined( fileno($term->OUT) ) ) ){
402 return defined($default) ? $default : undef;
405 while ($i++ < 10) {
406 $resp = $term->readline($prompt);
407 if (!defined $resp) { # EOF
408 print "\n";
409 return defined $default ? $default : undef;
411 if ($resp eq '' and defined $default) {
412 return $default;
414 if (!defined $valid_re or $resp =~ /$valid_re/) {
415 return $resp;
418 return undef;
421 sub do_git_init_db {
422 unless (-d $ENV{GIT_DIR}) {
423 my @init_db = ('init');
424 push @init_db, "--template=$_template" if defined $_template;
425 if (defined $_shared) {
426 if ($_shared =~ /[a-z]/) {
427 push @init_db, "--shared=$_shared";
428 } else {
429 push @init_db, "--shared";
432 command_noisy(@init_db);
433 $_repository = Git->repository(Repository => ".git");
435 my $set;
436 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
437 foreach my $i (keys %icv) {
438 die "'$set' and '$i' cannot both be set\n" if $set;
439 next unless defined $icv{$i};
440 command_noisy('config', "$pfx.$i", $icv{$i});
441 $set = $i;
443 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
444 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
445 if defined $$ignore_regex;
447 if (defined $SVN::Git::Fetcher::_preserve_empty_dirs) {
448 my $fname = \$SVN::Git::Fetcher::_placeholder_filename;
449 command_noisy('config', "$pfx.preserve-empty-dirs", 'true');
450 command_noisy('config', "$pfx.placeholder-filename", $$fname);
454 sub init_subdir {
455 my $repo_path = shift or return;
456 mkpath([$repo_path]) unless -d $repo_path;
457 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
458 $ENV{GIT_DIR} = '.git';
459 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
462 sub cmd_clone {
463 my ($url, $path) = @_;
464 if (!defined $path &&
465 (defined $_trunk || @_branches || @_tags ||
466 defined $_stdlayout) &&
467 $url !~ m#^[a-z\+]+://#) {
468 $path = $url;
470 $path = basename($url) if !defined $path || !length $path;
471 my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
472 cmd_init($url, $path);
473 command_oneline('config', 'svn.authorsfile', $authors_absolute)
474 if $_authors;
475 Git::SVN::fetch_all($Git::SVN::default_repo_id);
478 sub cmd_init {
479 if (defined $_stdlayout) {
480 $_trunk = 'trunk' if (!defined $_trunk);
481 @_tags = 'tags' if (! @_tags);
482 @_branches = 'branches' if (! @_branches);
484 if (defined $_trunk || @_branches || @_tags) {
485 return cmd_multi_init(@_);
487 my $url = shift or die "SVN repository location required ",
488 "as a command-line argument\n";
489 $url = canonicalize_url($url);
490 init_subdir(@_);
491 do_git_init_db();
493 if ($Git::SVN::_minimize_url eq 'unset') {
494 $Git::SVN::_minimize_url = 0;
497 Git::SVN->init($url);
500 sub cmd_fetch {
501 if (grep /^\d+=./, @_) {
502 die "'<rev>=<commit>' fetch arguments are ",
503 "no longer supported.\n";
505 my ($remote) = @_;
506 if (@_ > 1) {
507 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
509 $Git::SVN::no_reuse_existing = undef;
510 if ($_fetch_parent) {
511 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
512 unless ($gs) {
513 die "Unable to determine upstream SVN information from ",
514 "working tree history\n";
516 # just fetch, don't checkout.
517 $_no_checkout = 'true';
518 $_fetch_all ? $gs->fetch_all : $gs->fetch;
519 } elsif ($_fetch_all) {
520 cmd_multi_fetch();
521 } else {
522 $remote ||= $Git::SVN::default_repo_id;
523 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
527 sub cmd_set_tree {
528 my (@commits) = @_;
529 if ($_stdin || !@commits) {
530 print "Reading from stdin...\n";
531 @commits = ();
532 while (<STDIN>) {
533 if (/\b($sha1_short)\b/o) {
534 unshift @commits, $1;
538 my @revs;
539 foreach my $c (@commits) {
540 my @tmp = command('rev-parse',$c);
541 if (scalar @tmp == 1) {
542 push @revs, $tmp[0];
543 } elsif (scalar @tmp > 1) {
544 push @revs, reverse(command('rev-list',@tmp));
545 } else {
546 fatal "Failed to rev-parse $c";
549 my $gs = Git::SVN->new;
550 my ($r_last, $cmt_last) = $gs->last_rev_commit;
551 $gs->fetch;
552 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
553 fatal "There are new revisions that were fetched ",
554 "and need to be merged (or acknowledged) ",
555 "before committing.\nlast rev: $r_last\n",
556 " current: $gs->{last_rev}";
558 $gs->set_tree($_) foreach @revs;
559 print "Done committing ",scalar @revs," revisions to SVN\n";
560 unlink $gs->{index};
563 sub split_merge_info_range {
564 my ($range) = @_;
565 if ($range =~ /(\d+)-(\d+)/) {
566 return (int($1), int($2));
567 } else {
568 return (int($range), int($range));
572 sub combine_ranges {
573 my ($in) = @_;
575 my @fnums = ();
576 my @arr = split(/,/, $in);
577 for my $element (@arr) {
578 my ($start, $end) = split_merge_info_range($element);
579 push @fnums, $start;
582 my @sorted = @arr [ sort {
583 $fnums[$a] <=> $fnums[$b]
584 } 0..$#arr ];
586 my @return = ();
587 my $last = -1;
588 my $first = -1;
589 for my $element (@sorted) {
590 my ($start, $end) = split_merge_info_range($element);
592 if ($last == -1) {
593 $first = $start;
594 $last = $end;
595 next;
597 if ($start <= $last+1) {
598 if ($end > $last) {
599 $last = $end;
601 next;
603 if ($first == $last) {
604 push @return, "$first";
605 } else {
606 push @return, "$first-$last";
608 $first = $start;
609 $last = $end;
612 if ($first != -1) {
613 if ($first == $last) {
614 push @return, "$first";
615 } else {
616 push @return, "$first-$last";
620 return join(',', @return);
623 sub merge_revs_into_hash {
624 my ($hash, $minfo) = @_;
625 my @lines = split(' ', $minfo);
627 for my $line (@lines) {
628 my ($branchpath, $revs) = split(/:/, $line);
630 if (exists($hash->{$branchpath})) {
631 # Merge the two revision sets
632 my $combined = "$hash->{$branchpath},$revs";
633 $hash->{$branchpath} = combine_ranges($combined);
634 } else {
635 # Just do range combining for consolidation
636 $hash->{$branchpath} = combine_ranges($revs);
641 sub merge_merge_info {
642 my ($mergeinfo_one, $mergeinfo_two) = @_;
643 my %result_hash = ();
645 merge_revs_into_hash(\%result_hash, $mergeinfo_one);
646 merge_revs_into_hash(\%result_hash, $mergeinfo_two);
648 my $result = '';
649 # Sort below is for consistency's sake
650 for my $branchname (sort keys(%result_hash)) {
651 my $revlist = $result_hash{$branchname};
652 $result .= "$branchname:$revlist\n"
654 return $result;
657 sub populate_merge_info {
658 my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_;
660 my %parentshash;
661 read_commit_parents(\%parentshash, $d);
662 my @parents = @{$parentshash{$d}};
663 if ($#parents > 0) {
664 # Merge commit
665 my $all_parents_ok = 1;
666 my $aggregate_mergeinfo = '';
667 my $rooturl = $gs->repos_root;
669 if (defined($rewritten_parent)) {
670 # Replace first parent with newly-rewritten version
671 shift @parents;
672 unshift @parents, $rewritten_parent;
675 foreach my $parent (@parents) {
676 my ($branchurl, $svnrev, $paruuid) =
677 cmt_metadata($parent);
679 unless (defined($svnrev)) {
680 # Should have been caught be preflight check
681 fatal "merge commit $d has ancestor $parent, but that change "
682 ."does not have git-svn metadata!";
684 unless ($branchurl =~ /^$rooturl(.*)/) {
685 fatal "commit $parent git-svn metadata changed mid-run!";
687 my $branchpath = $1;
689 my $ra = Git::SVN::Ra->new($branchurl);
690 my (undef, undef, $props) =
691 $ra->get_dir(canonicalize_path("."), $svnrev);
692 my $par_mergeinfo = $props->{'svn:mergeinfo'};
693 unless (defined $par_mergeinfo) {
694 $par_mergeinfo = '';
696 # Merge previous mergeinfo values
697 $aggregate_mergeinfo =
698 merge_merge_info($aggregate_mergeinfo,
699 $par_mergeinfo, 0);
701 next if $parent eq $parents[0]; # Skip first parent
702 # Add new changes being placed in tree by merge
703 my @cmd = (qw/rev-list --reverse/,
704 $parent, qw/--not/);
705 foreach my $par (@parents) {
706 unless ($par eq $parent) {
707 push @cmd, $par;
710 my @revsin = ();
711 my ($revlist, $ctx) = command_output_pipe(@cmd);
712 while (<$revlist>) {
713 my $irev = $_;
714 chomp $irev;
715 my (undef, $csvnrev, undef) =
716 cmt_metadata($irev);
717 unless (defined $csvnrev) {
718 # A child is missing SVN annotations...
719 # this might be OK, or might not be.
720 warn "W:child $irev is merged into revision "
721 ."$d but does not have git-svn metadata. "
722 ."This means git-svn cannot determine the "
723 ."svn revision numbers to place into the "
724 ."svn:mergeinfo property. You must ensure "
725 ."a branch is entirely committed to "
726 ."SVN before merging it in order for "
727 ."svn:mergeinfo population to function "
728 ."properly";
730 push @revsin, $csvnrev;
732 command_close_pipe($revlist, $ctx);
734 last unless $all_parents_ok;
736 # We now have a list of all SVN revnos which are
737 # merged by this particular parent. Integrate them.
738 next if $#revsin == -1;
739 my $newmergeinfo = "$branchpath:" . join(',', @revsin);
740 $aggregate_mergeinfo =
741 merge_merge_info($aggregate_mergeinfo,
742 $newmergeinfo, 1);
744 if ($all_parents_ok and $aggregate_mergeinfo) {
745 return $aggregate_mergeinfo;
749 return undef;
752 sub cmd_dcommit {
753 my $head = shift;
754 command_noisy(qw/update-index --refresh/);
755 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
756 'Cannot dcommit with a dirty index. Commit your changes first, '
757 . "or stash them with `git stash'.\n";
758 $head ||= 'HEAD';
760 my $old_head;
761 if ($head ne 'HEAD') {
762 $old_head = eval {
763 command_oneline([qw/symbolic-ref -q HEAD/])
765 if ($old_head) {
766 $old_head =~ s{^refs/heads/}{};
767 } else {
768 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
770 command(['checkout', $head], STDERR => 0);
773 my @refs;
774 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
775 unless ($gs) {
776 die "Unable to determine upstream SVN information from ",
777 "$head history.\nPerhaps the repository is empty.";
780 if (defined $_commit_url) {
781 $url = $_commit_url;
782 } else {
783 $url = eval { command_oneline('config', '--get',
784 "svn-remote.$gs->{repo_id}.commiturl") };
785 if (!$url) {
786 $url = $gs->full_pushurl
790 my $last_rev = $_revision if defined $_revision;
791 if ($url) {
792 print "Committing to $url ...\n";
794 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
795 if ($_no_rebase && scalar(@$linear_refs) > 1) {
796 warn "Attempting to commit more than one change while ",
797 "--no-rebase is enabled.\n",
798 "If these changes depend on each other, re-running ",
799 "without --no-rebase may be required."
802 if (defined $_interactive){
803 my $ask_default = "y";
804 foreach my $d (@$linear_refs){
805 my ($fh, $ctx) = command_output_pipe(qw(show --summary), "$d");
806 while (<$fh>){
807 print $_;
809 command_close_pipe($fh, $ctx);
810 $_ = ask("Commit this patch to SVN? ([y]es (default)|[n]o|[q]uit|[a]ll): ",
811 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
812 default => $ask_default);
813 die "Commit this patch reply required" unless defined $_;
814 if (/^[nq]/i) {
815 exit(0);
816 } elsif (/^a/i) {
817 last;
822 my $expect_url = $url;
824 my $push_merge_info = eval {
825 command_oneline(qw/config --get svn.pushmergeinfo/)
827 if (not defined($push_merge_info)
828 or $push_merge_info eq "false"
829 or $push_merge_info eq "no"
830 or $push_merge_info eq "never") {
831 $push_merge_info = 0;
834 unless (defined($_merge_info) || ! $push_merge_info) {
835 # Preflight check of changes to ensure no issues with mergeinfo
836 # This includes check for uncommitted-to-SVN parents
837 # (other than the first parent, which we will handle),
838 # information from different SVN repos, and paths
839 # which are not underneath this repository root.
840 my $rooturl = $gs->repos_root;
841 foreach my $d (@$linear_refs) {
842 my %parentshash;
843 read_commit_parents(\%parentshash, $d);
844 my @realparents = @{$parentshash{$d}};
845 if ($#realparents > 0) {
846 # Merge commit
847 shift @realparents; # Remove/ignore first parent
848 foreach my $parent (@realparents) {
849 my ($branchurl, $svnrev, $paruuid) = cmt_metadata($parent);
850 unless (defined $paruuid) {
851 # A parent is missing SVN annotations...
852 # abort the whole operation.
853 fatal "$parent is merged into revision $d, "
854 ."but does not have git-svn metadata. "
855 ."Either dcommit the branch or use a "
856 ."local cherry-pick, FF merge, or rebase "
857 ."instead of an explicit merge commit.";
860 unless ($paruuid eq $uuid) {
861 # Parent has SVN metadata from different repository
862 fatal "merge parent $parent for change $d has "
863 ."git-svn uuid $paruuid, while current change "
864 ."has uuid $uuid!";
867 unless ($branchurl =~ /^$rooturl(.*)/) {
868 # This branch is very strange indeed.
869 fatal "merge parent $parent for $d is on branch "
870 ."$branchurl, which is not under the "
871 ."git-svn root $rooturl!";
878 my $rewritten_parent;
879 Git::SVN::remove_username($expect_url);
880 if (defined($_merge_info)) {
881 $_merge_info =~ tr{ }{\n};
883 while (1) {
884 my $d = shift @$linear_refs or last;
885 unless (defined $last_rev) {
886 (undef, $last_rev, undef) = cmt_metadata("$d~1");
887 unless (defined $last_rev) {
888 fatal "Unable to extract revision information ",
889 "from commit $d~1";
892 if ($_dry_run) {
893 print "diff-tree $d~1 $d\n";
894 } else {
895 my $cmt_rev;
897 unless (defined($_merge_info) || ! $push_merge_info) {
898 $_merge_info = populate_merge_info($d, $gs,
899 $uuid,
900 $linear_refs,
901 $rewritten_parent);
904 my %ed_opts = ( r => $last_rev,
905 log => get_commit_entry($d)->{log},
906 ra => Git::SVN::Ra->new($url),
907 config => SVN::Core::config_get_config(
908 $Git::SVN::Ra::config_dir
910 tree_a => "$d~1",
911 tree_b => $d,
912 editor_cb => sub {
913 print "Committed r$_[0]\n";
914 $cmt_rev = $_[0];
916 mergeinfo => $_merge_info,
917 svn_path => '');
918 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
919 print "No changes\n$d~1 == $d\n";
920 } elsif ($parents->{$d} && @{$parents->{$d}}) {
921 $gs->{inject_parents_dcommit}->{$cmt_rev} =
922 $parents->{$d};
924 $_fetch_all ? $gs->fetch_all : $gs->fetch;
925 $last_rev = $cmt_rev;
926 next if $_no_rebase;
928 # we always want to rebase against the current HEAD,
929 # not any head that was passed to us
930 my @diff = command('diff-tree', $d,
931 $gs->refname, '--');
932 my @finish;
933 if (@diff) {
934 @finish = rebase_cmd();
935 print STDERR "W: $d and ", $gs->refname,
936 " differ, using @finish:\n",
937 join("\n", @diff), "\n";
938 } else {
939 print "No changes between current HEAD and ",
940 $gs->refname,
941 "\nResetting to the latest ",
942 $gs->refname, "\n";
943 @finish = qw/reset --mixed/;
945 command_noisy(@finish, $gs->refname);
947 $rewritten_parent = command_oneline(qw/rev-parse HEAD/);
949 if (@diff) {
950 @refs = ();
951 my ($url_, $rev_, $uuid_, $gs_) =
952 working_head_info('HEAD', \@refs);
953 my ($linear_refs_, $parents_) =
954 linearize_history($gs_, \@refs);
955 if (scalar(@$linear_refs) !=
956 scalar(@$linear_refs_)) {
957 fatal "# of revisions changed ",
958 "\nbefore:\n",
959 join("\n", @$linear_refs),
960 "\n\nafter:\n",
961 join("\n", @$linear_refs_), "\n",
962 'If you are attempting to commit ',
963 "merges, try running:\n\t",
964 'git rebase --interactive',
965 '--preserve-merges ',
966 $gs->refname,
967 "\nBefore dcommitting";
969 if ($url_ ne $expect_url) {
970 if ($url_ eq $gs->metadata_url) {
971 print
972 "Accepting rewritten URL:",
973 " $url_\n";
974 } else {
975 fatal
976 "URL mismatch after rebase:",
977 " $url_ != $expect_url";
980 if ($uuid_ ne $uuid) {
981 fatal "uuid mismatch after rebase: ",
982 "$uuid_ != $uuid";
984 # remap parents
985 my (%p, @l, $i);
986 for ($i = 0; $i < scalar @$linear_refs; $i++) {
987 my $new = $linear_refs_->[$i] or next;
988 $p{$new} =
989 $parents->{$linear_refs->[$i]};
990 push @l, $new;
992 $parents = \%p;
993 $linear_refs = \@l;
998 if ($old_head) {
999 my $new_head = command_oneline(qw/rev-parse HEAD/);
1000 my $new_is_symbolic = eval {
1001 command_oneline(qw/symbolic-ref -q HEAD/);
1003 if ($new_is_symbolic) {
1004 print "dcommitted the branch ", $head, "\n";
1005 } else {
1006 print "dcommitted on a detached HEAD because you gave ",
1007 "a revision argument.\n",
1008 "The rewritten commit is: ", $new_head, "\n";
1010 command(['checkout', $old_head], STDERR => 0);
1013 unlink $gs->{index};
1016 sub cmd_branch {
1017 my ($branch_name, $head) = @_;
1019 unless (defined $branch_name && length $branch_name) {
1020 die(($_tag ? "tag" : "branch") . " name required\n");
1022 $head ||= 'HEAD';
1024 my (undef, $rev, undef, $gs) = working_head_info($head);
1025 my $src = $gs->full_pushurl;
1027 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1028 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
1029 my $glob;
1030 if ($#{$allglobs} == 0) {
1031 $glob = $allglobs->[0];
1032 } else {
1033 unless(defined $_branch_dest) {
1034 die "Multiple ",
1035 $_tag ? "tag" : "branch",
1036 " paths defined for Subversion repository.\n",
1037 "You must specify where you want to create the ",
1038 $_tag ? "tag" : "branch",
1039 " with the --destination argument.\n";
1041 foreach my $g (@{$allglobs}) {
1042 # SVN::Git::Editor could probably be moved to Git.pm..
1043 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
1044 if ($_branch_dest =~ /$re/) {
1045 $glob = $g;
1046 last;
1049 unless (defined $glob) {
1050 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
1051 foreach my $g (@{$allglobs}) {
1052 $g->{path}->{left} =~ /$dest_re/ or next;
1053 if (defined $glob) {
1054 die "Ambiguous destination: ",
1055 $_branch_dest, "\nmatches both '",
1056 $glob->{path}->{left}, "' and '",
1057 $g->{path}->{left}, "'\n";
1059 $glob = $g;
1061 unless (defined $glob) {
1062 die "Unknown ",
1063 $_tag ? "tag" : "branch",
1064 " destination $_branch_dest\n";
1068 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
1069 my $url;
1070 if (defined $_commit_url) {
1071 $url = $_commit_url;
1072 } else {
1073 $url = eval { command_oneline('config', '--get',
1074 "svn-remote.$gs->{repo_id}.commiturl") };
1075 if (!$url) {
1076 $url = $remote->{pushurl} || $remote->{url};
1079 my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
1081 if ($dst =~ /^https:/ && $src =~ /^http:/) {
1082 $src=~s/^http:/https:/;
1085 ::_req_svn();
1087 my $ctx = SVN::Client->new(
1088 auth => Git::SVN::Ra::_auth_providers(),
1089 log_msg => sub {
1090 ${ $_[0] } = defined $_message
1091 ? $_message
1092 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
1093 . $branch_name;
1097 eval {
1098 $ctx->ls($dst, 'HEAD', 0);
1099 } and die "branch ${branch_name} already exists\n";
1101 print "Copying ${src} at r${rev} to ${dst}...\n";
1102 $ctx->copy($src, $rev, $dst)
1103 unless $_dry_run;
1105 $gs->fetch_all;
1108 sub cmd_find_rev {
1109 my $revision_or_hash = shift or die "SVN or git revision required ",
1110 "as a command-line argument\n";
1111 my $result;
1112 if ($revision_or_hash =~ /^r\d+$/) {
1113 my $head = shift;
1114 $head ||= 'HEAD';
1115 my @refs;
1116 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
1117 unless ($gs) {
1118 die "Unable to determine upstream SVN information from ",
1119 "$head history\n";
1121 my $desired_revision = substr($revision_or_hash, 1);
1122 $result = $gs->rev_map_get($desired_revision, $uuid);
1123 } else {
1124 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
1125 $result = $rev;
1127 print "$result\n" if $result;
1130 sub auto_create_empty_directories {
1131 my ($gs) = @_;
1132 my $var = eval { command_oneline('config', '--get', '--bool',
1133 "svn-remote.$gs->{repo_id}.automkdirs") };
1134 # By default, create empty directories by consulting the unhandled log,
1135 # but allow setting it to 'false' to skip it.
1136 return !($var && $var eq 'false');
1139 sub cmd_rebase {
1140 command_noisy(qw/update-index --refresh/);
1141 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1142 unless ($gs) {
1143 die "Unable to determine upstream SVN information from ",
1144 "working tree history\n";
1146 if ($_dry_run) {
1147 print "Remote Branch: " . $gs->refname . "\n";
1148 print "SVN URL: " . $url . "\n";
1149 return;
1151 if (command(qw/diff-index HEAD --/)) {
1152 print STDERR "Cannot rebase with uncommited changes:\n";
1153 command_noisy('status');
1154 exit 1;
1156 unless ($_local) {
1157 # rebase will checkout for us, so no need to do it explicitly
1158 $_no_checkout = 'true';
1159 $_fetch_all ? $gs->fetch_all : $gs->fetch;
1161 command_noisy(rebase_cmd(), $gs->refname);
1162 if (auto_create_empty_directories($gs)) {
1163 $gs->mkemptydirs;
1167 sub cmd_show_ignore {
1168 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1169 $gs ||= Git::SVN->new;
1170 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1171 $gs->prop_walk($gs->{path}, $r, sub {
1172 my ($gs, $path, $props) = @_;
1173 print STDOUT "\n# $path\n";
1174 my $s = $props->{'svn:ignore'} or return;
1175 $s =~ s/[\r\n]+/\n/g;
1176 $s =~ s/^\n+//;
1177 chomp $s;
1178 $s =~ s#^#$path#gm;
1179 print STDOUT "$s\n";
1183 sub cmd_show_externals {
1184 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1185 $gs ||= Git::SVN->new;
1186 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1187 $gs->prop_walk($gs->{path}, $r, sub {
1188 my ($gs, $path, $props) = @_;
1189 print STDOUT "\n# $path\n";
1190 my $s = $props->{'svn:externals'} or return;
1191 $s =~ s/[\r\n]+/\n/g;
1192 chomp $s;
1193 $s =~ s#^#$path#gm;
1194 print STDOUT "$s\n";
1198 sub cmd_create_ignore {
1199 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1200 $gs ||= Git::SVN->new;
1201 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1202 $gs->prop_walk($gs->{path}, $r, sub {
1203 my ($gs, $path, $props) = @_;
1204 # $path is of the form /path/to/dir/
1205 $path = '.' . $path;
1206 # SVN can have attributes on empty directories,
1207 # which git won't track
1208 mkpath([$path]) unless -d $path;
1209 my $ignore = $path . '.gitignore';
1210 my $s = $props->{'svn:ignore'} or return;
1211 open(GITIGNORE, '>', $ignore)
1212 or fatal("Failed to open `$ignore' for writing: $!");
1213 $s =~ s/[\r\n]+/\n/g;
1214 $s =~ s/^\n+//;
1215 chomp $s;
1216 # Prefix all patterns so that the ignore doesn't apply
1217 # to sub-directories.
1218 $s =~ s#^#/#gm;
1219 print GITIGNORE "$s\n";
1220 close(GITIGNORE)
1221 or fatal("Failed to close `$ignore': $!");
1222 command_noisy('add', '-f', $ignore);
1226 sub cmd_mkdirs {
1227 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1228 $gs ||= Git::SVN->new;
1229 $gs->mkemptydirs($_revision);
1232 sub canonicalize_path {
1233 my ($path) = @_;
1234 my $dot_slash_added = 0;
1235 if (substr($path, 0, 1) ne "/") {
1236 $path = "./" . $path;
1237 $dot_slash_added = 1;
1239 # File::Spec->canonpath doesn't collapse x/../y into y (for a
1240 # good reason), so let's do this manually.
1241 $path =~ s#/+#/#g;
1242 $path =~ s#/\.(?:/|$)#/#g;
1243 $path =~ s#/[^/]+/\.\.##g;
1244 $path =~ s#/$##g;
1245 $path =~ s#^\./## if $dot_slash_added;
1246 $path =~ s#^/##;
1247 $path =~ s#^\.$##;
1248 return $path;
1251 sub canonicalize_url {
1252 my ($url) = @_;
1253 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
1254 return $url;
1257 # get_svnprops(PATH)
1258 # ------------------
1259 # Helper for cmd_propget and cmd_proplist below.
1260 sub get_svnprops {
1261 my $path = shift;
1262 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1263 $gs ||= Git::SVN->new;
1265 # prefix THE PATH by the sub-directory from which the user
1266 # invoked us.
1267 $path = $cmd_dir_prefix . $path;
1268 fatal("No such file or directory: $path") unless -e $path;
1269 my $is_dir = -d $path ? 1 : 0;
1270 $path = $gs->{path} . '/' . $path;
1272 # canonicalize the path (otherwise libsvn will abort or fail to
1273 # find the file)
1274 $path = canonicalize_path($path);
1276 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
1277 my $props;
1278 if ($is_dir) {
1279 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
1281 else {
1282 (undef, $props) = $gs->ra->get_file($path, $r, undef);
1284 return $props;
1287 # cmd_propget (PROP, PATH)
1288 # ------------------------
1289 # Print the SVN property PROP for PATH.
1290 sub cmd_propget {
1291 my ($prop, $path) = @_;
1292 $path = '.' if not defined $path;
1293 usage(1) if not defined $prop;
1294 my $props = get_svnprops($path);
1295 if (not defined $props->{$prop}) {
1296 fatal("`$path' does not have a `$prop' SVN property.");
1298 print $props->{$prop} . "\n";
1301 # cmd_proplist (PATH)
1302 # -------------------
1303 # Print the list of SVN properties for PATH.
1304 sub cmd_proplist {
1305 my $path = shift;
1306 $path = '.' if not defined $path;
1307 my $props = get_svnprops($path);
1308 print "Properties on '$path':\n";
1309 foreach (sort keys %{$props}) {
1310 print " $_\n";
1314 sub cmd_multi_init {
1315 my $url = shift;
1316 unless (defined $_trunk || @_branches || @_tags) {
1317 usage(1);
1320 $_prefix = '' unless defined $_prefix;
1321 if (defined $url) {
1322 $url = canonicalize_url($url);
1323 init_subdir(@_);
1325 do_git_init_db();
1326 if (defined $_trunk) {
1327 $_trunk =~ s#^/+##;
1328 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
1329 # try both old-style and new-style lookups:
1330 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
1331 unless ($gs_trunk) {
1332 my ($trunk_url, $trunk_path) =
1333 complete_svn_url($url, $_trunk);
1334 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
1335 undef, $trunk_ref);
1338 return unless @_branches || @_tags;
1339 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
1340 foreach my $path (@_branches) {
1341 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
1343 foreach my $path (@_tags) {
1344 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
1348 sub cmd_multi_fetch {
1349 $Git::SVN::no_reuse_existing = undef;
1350 my $remotes = Git::SVN::read_all_remotes();
1351 foreach my $repo_id (sort keys %$remotes) {
1352 if ($remotes->{$repo_id}->{url}) {
1353 Git::SVN::fetch_all($repo_id, $remotes);
1358 # this command is special because it requires no metadata
1359 sub cmd_commit_diff {
1360 my ($ta, $tb, $url) = @_;
1361 my $usage = "Usage: $0 commit-diff -r<revision> ".
1362 "<tree-ish> <tree-ish> [<URL>]";
1363 fatal($usage) if (!defined $ta || !defined $tb);
1364 my $svn_path = '';
1365 if (!defined $url) {
1366 my $gs = eval { Git::SVN->new };
1367 if (!$gs) {
1368 fatal("Needed URL or usable git-svn --id in ",
1369 "the command-line\n", $usage);
1371 $url = $gs->{url};
1372 $svn_path = $gs->{path};
1374 unless (defined $_revision) {
1375 fatal("-r|--revision is a required argument\n", $usage);
1377 if (defined $_message && defined $_file) {
1378 fatal("Both --message/-m and --file/-F specified ",
1379 "for the commit message.\n",
1380 "I have no idea what you mean");
1382 if (defined $_file) {
1383 $_message = file_to_s($_file);
1384 } else {
1385 $_message ||= get_commit_entry($tb)->{log};
1387 my $ra ||= Git::SVN::Ra->new($url);
1388 my $r = $_revision;
1389 if ($r eq 'HEAD') {
1390 $r = $ra->get_latest_revnum;
1391 } elsif ($r !~ /^\d+$/) {
1392 die "revision argument: $r not understood by git-svn\n";
1394 my %ed_opts = ( r => $r,
1395 log => $_message,
1396 ra => $ra,
1397 tree_a => $ta,
1398 tree_b => $tb,
1399 editor_cb => sub { print "Committed r$_[0]\n" },
1400 svn_path => $svn_path );
1401 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
1402 print "No changes\n$ta == $tb\n";
1406 sub escape_uri_only {
1407 my ($uri) = @_;
1408 my @tmp;
1409 foreach (split m{/}, $uri) {
1410 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
1411 push @tmp, $_;
1413 join('/', @tmp);
1416 sub escape_url {
1417 my ($url) = @_;
1418 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1419 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1420 $url = "$scheme://$domain$uri";
1422 $url;
1425 sub cmd_info {
1426 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
1427 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
1428 if (exists $_[1]) {
1429 die "Too many arguments specified\n";
1432 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1434 if (!$file_type && !$diff_status) {
1435 print STDERR "svn: '$path' is not under version control\n";
1436 exit 1;
1439 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1440 unless ($gs) {
1441 die "Unable to determine upstream SVN information from ",
1442 "working tree history\n";
1445 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1446 $path = "." if $path eq "";
1448 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
1450 if ($_url) {
1451 print escape_url($full_url), "\n";
1452 return;
1455 my $result = "Path: $path\n";
1456 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
1457 $result .= "URL: " . escape_url($full_url) . "\n";
1459 eval {
1460 my $repos_root = $gs->repos_root;
1461 Git::SVN::remove_username($repos_root);
1462 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
1464 if ($@) {
1465 $result .= "Repository Root: (offline)\n";
1467 ::_req_svn();
1468 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1469 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
1470 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1472 $result .= "Node Kind: " .
1473 ($file_type eq "dir" ? "directory" : "file") . "\n";
1475 my $schedule = $diff_status eq "A"
1476 ? "add"
1477 : ($diff_status eq "D" ? "delete" : "normal");
1478 $result .= "Schedule: $schedule\n";
1480 if ($diff_status eq "A") {
1481 print $result, "\n";
1482 return;
1485 my ($lc_author, $lc_rev, $lc_date_utc);
1486 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
1487 my $log = command_output_pipe(@args);
1488 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1489 while (<$log>) {
1490 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1491 $lc_author = $1;
1492 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1493 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1494 (undef, $lc_rev, undef) = ::extract_metadata($1);
1497 close $log;
1499 Git::SVN::Log::set_local_timezone();
1501 $result .= "Last Changed Author: $lc_author\n";
1502 $result .= "Last Changed Rev: $lc_rev\n";
1503 $result .= "Last Changed Date: " .
1504 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1506 if ($file_type ne "dir") {
1507 my $text_last_updated_date =
1508 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1509 $result .=
1510 "Text Last Updated: " .
1511 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1512 "\n";
1513 my $checksum;
1514 if ($diff_status eq "D") {
1515 my ($fh, $ctx) =
1516 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1517 if ($file_type eq "link") {
1518 my $file_name = <$fh>;
1519 $checksum = md5sum("link $file_name");
1520 } else {
1521 $checksum = md5sum($fh);
1523 command_close_pipe($fh, $ctx);
1524 } elsif ($file_type eq "link") {
1525 my $file_name =
1526 command(qw(cat-file blob), "HEAD:$path");
1527 $checksum =
1528 md5sum("link " . $file_name);
1529 } else {
1530 open FILE, "<", $path or die $!;
1531 $checksum = md5sum(\*FILE);
1532 close FILE or die $!;
1534 $result .= "Checksum: " . $checksum . "\n";
1537 print $result, "\n";
1540 sub cmd_reset {
1541 my $target = shift || $_revision or die "SVN revision required\n";
1542 $target = $1 if $target =~ /^r(\d+)$/;
1543 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1544 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1545 unless ($gs) {
1546 die "Unable to determine upstream SVN information from ".
1547 "history\n";
1549 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1550 die "Cannot find SVN revision $target\n" unless defined($c);
1551 $gs->rev_map_set($r, $c, 'reset', $uuid);
1552 print "r$r = $c ($gs->{ref_id})\n";
1555 sub cmd_gc {
1556 if (!$can_compress) {
1557 warn "Compress::Zlib could not be found; unhandled.log " .
1558 "files will not be compressed.\n";
1560 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1563 ########################### utility functions #########################
1565 sub rebase_cmd {
1566 my @cmd = qw/rebase/;
1567 push @cmd, '-v' if $_verbose;
1568 push @cmd, qw/--merge/ if $_merge;
1569 push @cmd, "--strategy=$_strategy" if $_strategy;
1570 @cmd;
1573 sub post_fetch_checkout {
1574 return if $_no_checkout;
1575 my $gs = $Git::SVN::_head or return;
1576 return if verify_ref('refs/heads/master^0');
1578 # look for "trunk" ref if it exists
1579 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1580 my $fetch = $remote->{fetch};
1581 if ($fetch) {
1582 foreach my $p (keys %$fetch) {
1583 basename($fetch->{$p}) eq 'trunk' or next;
1584 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1585 last;
1589 my $valid_head = verify_ref('HEAD^0');
1590 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1591 return if ($valid_head || !verify_ref('HEAD^0'));
1593 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1594 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1595 return if -f $index;
1597 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
1598 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1599 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1600 print STDERR "Checked out HEAD:\n ",
1601 $gs->full_url, " r", $gs->last_rev, "\n";
1602 if (auto_create_empty_directories($gs)) {
1603 $gs->mkemptydirs($gs->last_rev);
1607 sub complete_svn_url {
1608 my ($url, $path) = @_;
1609 $path =~ s#/+$##;
1610 if ($path !~ m#^[a-z\+]+://#) {
1611 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1612 fatal("E: '$path' is not a complete URL ",
1613 "and a separate URL is not specified");
1615 return ($url, $path);
1617 return ($path, '');
1620 sub complete_url_ls_init {
1621 my ($ra, $repo_path, $switch, $pfx) = @_;
1622 unless ($repo_path) {
1623 print STDERR "W: $switch not specified\n";
1624 return;
1626 $repo_path =~ s#/+$##;
1627 if ($repo_path =~ m#^[a-z\+]+://#) {
1628 $ra = Git::SVN::Ra->new($repo_path);
1629 $repo_path = '';
1630 } else {
1631 $repo_path =~ s#^/+##;
1632 unless ($ra) {
1633 fatal("E: '$repo_path' is not a complete URL ",
1634 "and a separate URL is not specified");
1637 my $url = $ra->{url};
1638 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1639 my $k = "svn-remote.$gs->{repo_id}.url";
1640 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1641 if ($orig_url && ($orig_url ne $gs->{url})) {
1642 die "$k already set: $orig_url\n",
1643 "wanted to set to: $gs->{url}\n";
1645 command_oneline('config', $k, $gs->{url}) unless $orig_url;
1646 my $remote_path = "$gs->{path}/$repo_path";
1647 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
1648 $remote_path =~ s#/+#/#g;
1649 $remote_path =~ s#^/##g;
1650 $remote_path .= "/*" if $remote_path !~ /\*/;
1651 my ($n) = ($switch =~ /^--(\w+)/);
1652 if (length $pfx && $pfx !~ m#/$#) {
1653 die "--prefix='$pfx' must have a trailing slash '/'\n";
1655 command_noisy('config',
1656 '--add',
1657 "svn-remote.$gs->{repo_id}.$n",
1658 "$remote_path:refs/remotes/$pfx*" .
1659 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
1662 sub verify_ref {
1663 my ($ref) = @_;
1664 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1665 { STDERR => 0 }); };
1668 sub get_tree_from_treeish {
1669 my ($treeish) = @_;
1670 # $treeish can be a symbolic ref, too:
1671 my $type = command_oneline(qw/cat-file -t/, $treeish);
1672 my $expected;
1673 while ($type eq 'tag') {
1674 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
1676 if ($type eq 'commit') {
1677 $expected = (grep /^tree /, command(qw/cat-file commit/,
1678 $treeish))[0];
1679 ($expected) = ($expected =~ /^tree ($sha1)$/o);
1680 die "Unable to get tree from $treeish\n" unless $expected;
1681 } elsif ($type eq 'tree') {
1682 $expected = $treeish;
1683 } else {
1684 die "$treeish is a $type, expected tree, tag or commit\n";
1686 return $expected;
1689 sub get_commit_entry {
1690 my ($treeish) = shift;
1691 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1692 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1693 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1694 open my $log_fh, '>', $commit_editmsg or croak $!;
1696 my $type = command_oneline(qw/cat-file -t/, $treeish);
1697 if ($type eq 'commit' || $type eq 'tag') {
1698 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
1699 $type, $treeish);
1700 my $in_msg = 0;
1701 my $author;
1702 my $saw_from = 0;
1703 my $msgbuf = "";
1704 while (<$msg_fh>) {
1705 if (!$in_msg) {
1706 $in_msg = 1 if (/^\s*$/);
1707 $author = $1 if (/^author (.*>)/);
1708 } elsif (/^git-svn-id: /) {
1709 # skip this for now, we regenerate the
1710 # correct one on re-fetch anyways
1711 # TODO: set *:merge properties or like...
1712 } else {
1713 if (/^From:/ || /^Signed-off-by:/) {
1714 $saw_from = 1;
1716 $msgbuf .= $_;
1719 $msgbuf =~ s/\s+$//s;
1720 if ($Git::SVN::_add_author_from && defined($author)
1721 && !$saw_from) {
1722 $msgbuf .= "\n\nFrom: $author";
1724 print $log_fh $msgbuf or croak $!;
1725 command_close_pipe($msg_fh, $ctx);
1727 close $log_fh or croak $!;
1729 if ($_edit || ($type eq 'tree')) {
1730 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1731 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
1733 rename $commit_editmsg, $commit_msg or croak $!;
1735 require Encode;
1736 # SVN requires messages to be UTF-8 when entering the repo
1737 local $/;
1738 open $log_fh, '<', $commit_msg or croak $!;
1739 binmode $log_fh;
1740 chomp($log_entry{log} = <$log_fh>);
1742 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1743 my $msg = $log_entry{log};
1745 eval { $msg = Encode::decode($enc, $msg, 1) };
1746 if ($@) {
1747 die "Could not decode as $enc:\n", $msg,
1748 "\nPerhaps you need to set i18n.commitencoding\n";
1751 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1752 die "Could not encode as UTF-8:\n$msg\n" if $@;
1754 $log_entry{log} = $msg;
1756 close $log_fh or croak $!;
1758 unlink $commit_msg;
1759 \%log_entry;
1762 sub s_to_file {
1763 my ($str, $file, $mode) = @_;
1764 open my $fd,'>',$file or croak $!;
1765 print $fd $str,"\n" or croak $!;
1766 close $fd or croak $!;
1767 chmod ($mode &~ umask, $file) if (defined $mode);
1770 sub file_to_s {
1771 my $file = shift;
1772 open my $fd,'<',$file or croak "$!: file: $file\n";
1773 local $/;
1774 my $ret = <$fd>;
1775 close $fd or croak $!;
1776 $ret =~ s/\s*$//s;
1777 return $ret;
1780 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1781 sub load_authors {
1782 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1783 my $log = $cmd eq 'log';
1784 while (<$authors>) {
1785 chomp;
1786 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
1787 my ($user, $name, $email) = ($1, $2, $3);
1788 if ($log) {
1789 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1790 } else {
1791 $users{$user} = [$name, $email];
1794 close $authors or croak $!;
1797 # convert GetOpt::Long specs for use by git-config
1798 sub read_git_config {
1799 my $opts = shift;
1800 my @config_only;
1801 foreach my $o (keys %$opts) {
1802 # if we have mixedCase and a long option-only, then
1803 # it's a config-only variable that we don't need for
1804 # the command-line.
1805 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
1806 my $v = $opts->{$o};
1807 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
1808 $key =~ s/-//g;
1809 my $arg = 'git config';
1810 $arg .= ' --int' if ($o =~ /[:=]i$/);
1811 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1812 if (ref $v eq 'ARRAY') {
1813 chomp(my @tmp = `$arg --get-all svn.$key`);
1814 @$v = @tmp if @tmp;
1815 } else {
1816 chomp(my $tmp = `$arg --get svn.$key`);
1817 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
1818 $$v = $tmp;
1822 delete @$opts{@config_only} if @config_only;
1825 sub extract_metadata {
1826 my $id = shift or return (undef, undef, undef);
1827 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
1828 \s([a-f\d\-]+)$/ix);
1829 if (!defined $rev || !$uuid || !$url) {
1830 # some of the original repositories I made had
1831 # identifiers like this:
1832 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
1834 return ($url, $rev, $uuid);
1837 sub cmt_metadata {
1838 return extract_metadata((grep(/^git-svn-id: /,
1839 command(qw/cat-file commit/, shift)))[-1]);
1842 sub cmt_sha2rev_batch {
1843 my %s2r;
1844 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1845 my $list = shift;
1847 foreach my $sha (@{$list}) {
1848 my $first = 1;
1849 my $size = 0;
1850 print $out $sha, "\n";
1852 while (my $line = <$in>) {
1853 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1854 last;
1855 } elsif ($first &&
1856 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1857 $first = 0;
1858 $size = $1;
1859 next;
1860 } elsif ($line =~ /^(git-svn-id: )/) {
1861 my (undef, $rev, undef) =
1862 extract_metadata($line);
1863 $s2r{$sha} = $rev;
1866 $size -= length($line);
1867 last if ($size == 0);
1871 command_close_bidi_pipe($pid, $in, $out, $ctx);
1873 return \%s2r;
1876 sub working_head_info {
1877 my ($head, $refs) = @_;
1878 my @args = qw/log --no-color --no-decorate --first-parent
1879 --pretty=medium/;
1880 my ($fh, $ctx) = command_output_pipe(@args, $head);
1881 my $hash;
1882 my %max;
1883 while (<$fh>) {
1884 if ( m{^commit ($::sha1)$} ) {
1885 unshift @$refs, $hash if $hash and $refs;
1886 $hash = $1;
1887 next;
1889 next unless s{^\s*(git-svn-id:)}{$1};
1890 my ($url, $rev, $uuid) = extract_metadata($_);
1891 if (defined $url && defined $rev) {
1892 next if $max{$url} and $max{$url} < $rev;
1893 if (my $gs = Git::SVN->find_by_url($url)) {
1894 my $c = $gs->rev_map_get($rev, $uuid);
1895 if ($c && $c eq $hash) {
1896 close $fh; # break the pipe
1897 return ($url, $rev, $uuid, $gs);
1898 } else {
1899 $max{$url} ||= $gs->rev_map_max;
1904 command_close_pipe($fh, $ctx);
1905 (undef, undef, undef, undef);
1908 sub read_commit_parents {
1909 my ($parents, $c) = @_;
1910 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1911 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1912 @{$parents->{$c}} = split(/ /, $p);
1915 sub linearize_history {
1916 my ($gs, $refs) = @_;
1917 my %parents;
1918 foreach my $c (@$refs) {
1919 read_commit_parents(\%parents, $c);
1922 my @linear_refs;
1923 my %skip = ();
1924 my $last_svn_commit = $gs->last_commit;
1925 foreach my $c (reverse @$refs) {
1926 next if $c eq $last_svn_commit;
1927 last if $skip{$c};
1929 unshift @linear_refs, $c;
1930 $skip{$c} = 1;
1932 # we only want the first parent to diff against for linear
1933 # history, we save the rest to inject when we finalize the
1934 # svn commit
1935 my $fp_a = verify_ref("$c~1");
1936 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1937 if (!$fp_a || !$fp_b) {
1938 die "Commit $c\n",
1939 "has no parent commit, and therefore ",
1940 "nothing to diff against.\n",
1941 "You should be working from a repository ",
1942 "originally created by git-svn\n";
1944 if ($fp_a ne $fp_b) {
1945 die "$c~1 = $fp_a, however parsing commit $c ",
1946 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1949 foreach my $p (@{$parents{$c}}) {
1950 $skip{$p} = 1;
1953 (\@linear_refs, \%parents);
1956 sub find_file_type_and_diff_status {
1957 my ($path) = @_;
1958 return ('dir', '') if $path eq '';
1960 my $diff_output =
1961 command_oneline(qw(diff --cached --name-status --), $path) || "";
1962 my $diff_status = (split(' ', $diff_output))[0] || "";
1964 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1966 return (undef, undef) if !$diff_status && !$ls_tree;
1968 if ($diff_status eq "A") {
1969 return ("link", $diff_status) if -l $path;
1970 return ("dir", $diff_status) if -d $path;
1971 return ("file", $diff_status);
1974 my $mode = (split(' ', $ls_tree))[0] || "";
1976 return ("link", $diff_status) if $mode eq "120000";
1977 return ("dir", $diff_status) if $mode eq "040000";
1978 return ("file", $diff_status);
1981 sub md5sum {
1982 my $arg = shift;
1983 my $ref = ref $arg;
1984 my $md5 = Digest::MD5->new();
1985 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
1986 $md5->addfile($arg) or croak $!;
1987 } elsif ($ref eq 'SCALAR') {
1988 $md5->add($$arg) or croak $!;
1989 } elsif (!$ref) {
1990 $md5->add($arg) or croak $!;
1991 } else {
1992 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1994 return $md5->hexdigest();
1997 sub gc_directory {
1998 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1999 my $out_filename = $_ . ".gz";
2000 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
2001 binmode $in_fh;
2002 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
2003 die "Unable to open $out_filename: $!\n";
2005 my $res;
2006 while ($res = sysread($in_fh, my $str, 1024)) {
2007 $gz->gzwrite($str) or
2008 die "Unable to write: ".$gz->gzerror()."!\n";
2010 unlink $_ or die "unlink $File::Find::name: $!\n";
2011 } elsif (-f $_ && basename($_) eq "index") {
2012 unlink $_ or die "unlink $_: $!\n";
2016 package Git::SVN;
2017 use strict;
2018 use warnings;
2019 use Fcntl qw/:DEFAULT :seek/;
2020 use constant rev_map_fmt => 'NH40';
2021 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
2022 $_repack $_repack_flags $_use_svm_props $_head
2023 $_use_svnsync_props $no_reuse_existing $_minimize_url
2024 $_use_log_author $_add_author_from $_localtime/;
2025 use Carp qw/croak/;
2026 use File::Path qw/mkpath/;
2027 use File::Copy qw/copy/;
2028 use IPC::Open3;
2029 use Memoize; # core since 5.8.0, Jul 2002
2030 use Memoize::Storable;
2032 my ($_gc_nr, $_gc_period);
2034 # properties that we do not log:
2035 my %SKIP_PROP;
2036 BEGIN {
2037 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
2038 svn:special svn:executable
2039 svn:entry:committed-rev
2040 svn:entry:last-author
2041 svn:entry:uuid
2042 svn:entry:committed-date/;
2044 # some options are read globally, but can be overridden locally
2045 # per [svn-remote "..."] section. Command-line options will *NOT*
2046 # override options set in an [svn-remote "..."] section
2047 no strict 'refs';
2048 for my $option (qw/follow_parent no_metadata use_svm_props
2049 use_svnsync_props/) {
2050 my $key = $option;
2051 $key =~ tr/_//d;
2052 my $prop = "-$option";
2053 *$option = sub {
2054 my ($self) = @_;
2055 return $self->{$prop} if exists $self->{$prop};
2056 my $k = "svn-remote.$self->{repo_id}.$key";
2057 eval { command_oneline(qw/config --get/, $k) };
2058 if ($@) {
2059 $self->{$prop} = ${"Git::SVN::_$option"};
2060 } else {
2061 my $v = command_oneline(qw/config --bool/,$k);
2062 $self->{$prop} = $v eq 'false' ? 0 : 1;
2064 return $self->{$prop};
2070 my (%LOCKFILES, %INDEX_FILES);
2071 END {
2072 unlink keys %LOCKFILES if %LOCKFILES;
2073 unlink keys %INDEX_FILES if %INDEX_FILES;
2076 sub resolve_local_globs {
2077 my ($url, $fetch, $glob_spec) = @_;
2078 return unless defined $glob_spec;
2079 my $ref = $glob_spec->{ref};
2080 my $path = $glob_spec->{path};
2081 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
2082 next unless m#^$ref->{regex}$#;
2083 my $p = $1;
2084 my $pathname = desanitize_refname($path->full_path($p));
2085 my $refname = desanitize_refname($ref->full_path($p));
2086 if (my $existing = $fetch->{$pathname}) {
2087 if ($existing ne $refname) {
2088 die "Refspec conflict:\n",
2089 "existing: $existing\n",
2090 " globbed: $refname\n";
2092 my $u = (::cmt_metadata("$refname"))[0];
2093 $u =~ s!^\Q$url\E(/|$)!! or die
2094 "$refname: '$url' not found in '$u'\n";
2095 if ($pathname ne $u) {
2096 warn "W: Refspec glob conflict ",
2097 "(ref: $refname):\n",
2098 "expected path: $pathname\n",
2099 " real path: $u\n",
2100 "Continuing ahead with $u\n";
2101 next;
2103 } else {
2104 $fetch->{$pathname} = $refname;
2109 sub parse_revision_argument {
2110 my ($base, $head) = @_;
2111 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
2112 return ($base, $head);
2114 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
2115 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
2116 return ($head, $head) if ($::_revision eq 'HEAD');
2117 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
2118 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
2119 die "revision argument: $::_revision not understood by git-svn\n";
2122 sub fetch_all {
2123 my ($repo_id, $remotes) = @_;
2124 if (ref $repo_id) {
2125 my $gs = $repo_id;
2126 $repo_id = undef;
2127 $repo_id = $gs->{repo_id};
2129 $remotes ||= read_all_remotes();
2130 my $remote = $remotes->{$repo_id} or
2131 die "[svn-remote \"$repo_id\"] unknown\n";
2132 my $fetch = $remote->{fetch};
2133 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
2134 my (@gs, @globs);
2135 my $ra = Git::SVN::Ra->new($url);
2136 my $uuid = $ra->get_uuid;
2137 my $head = $ra->get_latest_revnum;
2139 # ignore errors, $head revision may not even exist anymore
2140 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
2141 warn "W: $@\n" if $@;
2143 my $base = defined $fetch ? $head : 0;
2145 # read the max revs for wildcard expansion (branches/*, tags/*)
2146 foreach my $t (qw/branches tags/) {
2147 defined $remote->{$t} or next;
2148 push @globs, @{$remote->{$t}};
2150 my $max_rev = eval { tmp_config(qw/--int --get/,
2151 "svn-remote.$repo_id.${t}-maxRev") };
2152 if (defined $max_rev && ($max_rev < $base)) {
2153 $base = $max_rev;
2154 } elsif (!defined $max_rev) {
2155 $base = 0;
2159 if ($fetch) {
2160 foreach my $p (sort keys %$fetch) {
2161 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
2162 my $lr = $gs->rev_map_max;
2163 if (defined $lr) {
2164 $base = $lr if ($lr < $base);
2166 push @gs, $gs;
2170 ($base, $head) = parse_revision_argument($base, $head);
2171 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
2174 sub read_all_remotes {
2175 my $r = {};
2176 my $use_svm_props = eval { command_oneline(qw/config --bool
2177 svn.useSvmProps/) };
2178 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
2179 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
2180 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
2181 if (m!^(.+)\.fetch=$svn_refspec$!) {
2182 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
2183 die("svn-remote.$remote: remote ref '$remote_ref' "
2184 . "must start with 'refs/'\n")
2185 unless $remote_ref =~ m{^refs/};
2186 $local_ref = uri_decode($local_ref);
2187 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
2188 $r->{$remote}->{svm} = {} if $use_svm_props;
2189 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
2190 $r->{$1}->{svm} = {};
2191 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
2192 $r->{$1}->{url} = $2;
2193 } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
2194 $r->{$1}->{pushurl} = $2;
2195 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
2196 my ($remote, $t, $local_ref, $remote_ref) =
2197 ($1, $2, $3, $4);
2198 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
2199 . "must start with 'refs/'\n")
2200 unless $remote_ref =~ m{^refs/};
2201 $local_ref = uri_decode($local_ref);
2202 my $rs = {
2203 t => $t,
2204 remote => $remote,
2205 path => Git::SVN::GlobSpec->new($local_ref, 1),
2206 ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
2207 if (length($rs->{ref}->{right}) != 0) {
2208 die "The '*' glob character must be the last ",
2209 "character of '$remote_ref'\n";
2211 push @{ $r->{$remote}->{$t} }, $rs;
2215 map {
2216 if (defined $r->{$_}->{svm}) {
2217 my $svm;
2218 eval {
2219 my $section = "svn-remote.$_";
2220 $svm = {
2221 source => tmp_config('--get',
2222 "$section.svm-source"),
2223 replace => tmp_config('--get',
2224 "$section.svm-replace"),
2227 $r->{$_}->{svm} = $svm;
2229 } keys %$r;
2234 sub init_vars {
2235 $_gc_nr = $_gc_period = 1000;
2236 if (defined $_repack || defined $_repack_flags) {
2237 warn "Repack options are obsolete; they have no effect.\n";
2241 sub verify_remotes_sanity {
2242 return unless -d $ENV{GIT_DIR};
2243 my %seen;
2244 foreach (command(qw/config -l/)) {
2245 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
2246 if ($seen{$1}) {
2247 die "Remote ref refs/remote/$1 is tracked by",
2248 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
2249 "Please resolve this ambiguity in ",
2250 "your git configuration file before ",
2251 "continuing\n";
2253 $seen{$1} = $_;
2258 sub find_existing_remote {
2259 my ($url, $remotes) = @_;
2260 return undef if $no_reuse_existing;
2261 my $existing;
2262 foreach my $repo_id (keys %$remotes) {
2263 my $u = $remotes->{$repo_id}->{url} or next;
2264 next if $u ne $url;
2265 $existing = $repo_id;
2266 last;
2268 $existing;
2271 sub init_remote_config {
2272 my ($self, $url, $no_write) = @_;
2273 $url =~ s!/+$!!; # strip trailing slash
2274 my $r = read_all_remotes();
2275 my $existing = find_existing_remote($url, $r);
2276 if ($existing) {
2277 unless ($no_write) {
2278 print STDERR "Using existing ",
2279 "[svn-remote \"$existing\"]\n";
2281 $self->{repo_id} = $existing;
2282 } elsif ($_minimize_url) {
2283 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
2284 $existing = find_existing_remote($min_url, $r);
2285 if ($existing) {
2286 unless ($no_write) {
2287 print STDERR "Using existing ",
2288 "[svn-remote \"$existing\"]\n";
2290 $self->{repo_id} = $existing;
2292 if ($min_url ne $url) {
2293 unless ($no_write) {
2294 print STDERR "Using higher level of URL: ",
2295 "$url => $min_url\n";
2297 my $old_path = $self->{path};
2298 $self->{path} = $url;
2299 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
2300 if (length $old_path) {
2301 $self->{path} .= "/$old_path";
2303 $url = $min_url;
2306 my $orig_url;
2307 if (!$existing) {
2308 # verify that we aren't overwriting anything:
2309 $orig_url = eval {
2310 command_oneline('config', '--get',
2311 "svn-remote.$self->{repo_id}.url")
2313 if ($orig_url && ($orig_url ne $url)) {
2314 die "svn-remote.$self->{repo_id}.url already set: ",
2315 "$orig_url\nwanted to set to: $url\n";
2318 my ($xrepo_id, $xpath) = find_ref($self->refname);
2319 if (!$no_write && defined $xpath) {
2320 die "svn-remote.$xrepo_id.fetch already set to track ",
2321 "$xpath:", $self->refname, "\n";
2323 unless ($no_write) {
2324 command_noisy('config',
2325 "svn-remote.$self->{repo_id}.url", $url);
2326 $self->{path} =~ s{^/}{};
2327 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
2328 command_noisy('config', '--add',
2329 "svn-remote.$self->{repo_id}.fetch",
2330 "$self->{path}:".$self->refname);
2332 $self->{url} = $url;
2335 sub find_by_url { # repos_root and, path are optional
2336 my ($class, $full_url, $repos_root, $path) = @_;
2338 return undef unless defined $full_url;
2339 remove_username($full_url);
2340 remove_username($repos_root) if defined $repos_root;
2341 my $remotes = read_all_remotes();
2342 if (defined $full_url && defined $repos_root && !defined $path) {
2343 $path = $full_url;
2344 $path =~ s#^\Q$repos_root\E(?:/|$)##;
2346 foreach my $repo_id (keys %$remotes) {
2347 my $u = $remotes->{$repo_id}->{url} or next;
2348 remove_username($u);
2349 next if defined $repos_root && $repos_root ne $u;
2351 my $fetch = $remotes->{$repo_id}->{fetch} || {};
2352 foreach my $t (qw/branches tags/) {
2353 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
2354 resolve_local_globs($u, $fetch, $globspec);
2357 my $p = $path;
2358 my $rwr = rewrite_root({repo_id => $repo_id});
2359 my $svm = $remotes->{$repo_id}->{svm}
2360 if defined $remotes->{$repo_id}->{svm};
2361 unless (defined $p) {
2362 $p = $full_url;
2363 my $z = $u;
2364 my $prefix = '';
2365 if ($rwr) {
2366 $z = $rwr;
2367 remove_username($z);
2368 } elsif (defined $svm) {
2369 $z = $svm->{source};
2370 $prefix = $svm->{replace};
2371 $prefix =~ s#^\Q$u\E(?:/|$)##;
2372 $prefix =~ s#/$##;
2374 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
2376 foreach my $f (keys %$fetch) {
2377 next if $f ne $p;
2378 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
2381 undef;
2384 sub init {
2385 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
2386 my $self = _new($class, $repo_id, $ref_id, $path);
2387 if (defined $url) {
2388 $self->init_remote_config($url, $no_write);
2390 $self;
2393 sub find_ref {
2394 my ($ref_id) = @_;
2395 foreach (command(qw/config -l/)) {
2396 next unless m!^svn-remote\.(.+)\.fetch=
2397 \s*(.*?)\s*:\s*(.+?)\s*$!x;
2398 my ($repo_id, $path, $ref) = ($1, $2, $3);
2399 if ($ref eq $ref_id) {
2400 $path = '' if ($path =~ m#^\./?#);
2401 return ($repo_id, $path);
2404 (undef, undef, undef);
2407 sub new {
2408 my ($class, $ref_id, $repo_id, $path) = @_;
2409 if (defined $ref_id && !defined $repo_id && !defined $path) {
2410 ($repo_id, $path) = find_ref($ref_id);
2411 if (!defined $repo_id) {
2412 die "Could not find a \"svn-remote.*.fetch\" key ",
2413 "in the repository configuration matching: ",
2414 "$ref_id\n";
2417 my $self = _new($class, $repo_id, $ref_id, $path);
2418 if (!defined $self->{path} || !length $self->{path}) {
2419 my $fetch = command_oneline('config', '--get',
2420 "svn-remote.$repo_id.fetch",
2421 ":$ref_id\$") or
2422 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
2423 "\":$ref_id\$\" in config\n";
2424 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2426 $self->{path} =~ s{/+}{/}g;
2427 $self->{path} =~ s{\A/}{};
2428 $self->{path} =~ s{/\z}{};
2429 $self->{url} = command_oneline('config', '--get',
2430 "svn-remote.$repo_id.url") or
2431 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
2432 $self->{pushurl} = eval { command_oneline('config', '--get',
2433 "svn-remote.$repo_id.pushurl") };
2434 $self->rebuild;
2435 $self;
2438 sub refname {
2439 my ($refname) = $_[0]->{ref_id} ;
2441 # It cannot end with a slash /, we'll throw up on this because
2442 # SVN can't have directories with a slash in their name, either:
2443 if ($refname =~ m{/$}) {
2444 die "ref: '$refname' ends with a trailing slash, this is ",
2445 "not permitted by git nor Subversion\n";
2448 # It cannot have ASCII control character space, tilde ~, caret ^,
2449 # colon :, question-mark ?, asterisk *, space, or open bracket [
2450 # anywhere.
2452 # Additionally, % must be escaped because it is used for escaping
2453 # and we want our escaped refname to be reversible
2454 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2456 # no slash-separated component can begin with a dot .
2457 # /.* becomes /%2E*
2458 $refname =~ s{/\.}{/%2E}g;
2460 # It cannot have two consecutive dots .. anywhere
2461 # .. becomes %2E%2E
2462 $refname =~ s{\.\.}{%2E%2E}g;
2464 # trailing dots and .lock are not allowed
2465 # .$ becomes %2E and .lock becomes %2Elock
2466 $refname =~ s{\.(?=$|lock$)}{%2E};
2468 # the sequence @{ is used to access the reflog
2469 # @{ becomes %40{
2470 $refname =~ s{\@\{}{%40\{}g;
2472 return $refname;
2475 sub desanitize_refname {
2476 my ($refname) = @_;
2477 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2478 return $refname;
2481 sub svm_uuid {
2482 my ($self) = @_;
2483 return $self->{svm}->{uuid} if $self->svm;
2484 $self->ra;
2485 unless ($self->{svm}) {
2486 die "SVM UUID not cached, and reading remotely failed\n";
2488 $self->{svm}->{uuid};
2491 sub svm {
2492 my ($self) = @_;
2493 return $self->{svm} if $self->{svm};
2494 my $svm;
2495 # see if we have it in our config, first:
2496 eval {
2497 my $section = "svn-remote.$self->{repo_id}";
2498 $svm = {
2499 source => tmp_config('--get', "$section.svm-source"),
2500 uuid => tmp_config('--get', "$section.svm-uuid"),
2501 replace => tmp_config('--get', "$section.svm-replace"),
2504 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2505 $self->{svm} = $svm;
2507 $self->{svm};
2510 sub _set_svm_vars {
2511 my ($self, $ra) = @_;
2512 return $ra if $self->svm;
2514 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
2515 "(svm:source, svm:uuid) ",
2516 "from the following URLs:\n" );
2517 sub read_svm_props {
2518 my ($self, $ra, $path, $r) = @_;
2519 my $props = ($ra->get_dir($path, $r))[2];
2520 my $src = $props->{'svm:source'};
2521 my $uuid = $props->{'svm:uuid'};
2522 return undef if (!$src || !$uuid);
2524 chomp($src, $uuid);
2526 $uuid =~ m{^[0-9a-f\-]{30,}$}i
2527 or die "doesn't look right - svm:uuid is '$uuid'\n";
2529 # the '!' is used to mark the repos_root!/relative/path
2530 $src =~ s{/?!/?}{/};
2531 $src =~ s{/+$}{}; # no trailing slashes please
2532 # username is of no interest
2533 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
2535 my $replace = $ra->{url};
2536 $replace .= "/$path" if length $path;
2538 my $section = "svn-remote.$self->{repo_id}";
2539 tmp_config("$section.svm-source", $src);
2540 tmp_config("$section.svm-replace", $replace);
2541 tmp_config("$section.svm-uuid", $uuid);
2542 $self->{svm} = {
2543 source => $src,
2544 uuid => $uuid,
2545 replace => $replace
2549 my $r = $ra->get_latest_revnum;
2550 my $path = $self->{path};
2551 my %tried;
2552 while (length $path) {
2553 unless ($tried{"$self->{url}/$path"}) {
2554 return $ra if $self->read_svm_props($ra, $path, $r);
2555 $tried{"$self->{url}/$path"} = 1;
2557 $path =~ s#/?[^/]+$##;
2559 die "Path: '$path' should be ''\n" if $path ne '';
2560 return $ra if $self->read_svm_props($ra, $path, $r);
2561 $tried{"$self->{url}/$path"} = 1;
2563 if ($ra->{repos_root} eq $self->{url}) {
2564 die @err, (map { " $_\n" } keys %tried), "\n";
2567 # nope, make sure we're connected to the repository root:
2568 my $ok;
2569 my @tried_b;
2570 $path = $ra->{svn_path};
2571 $ra = Git::SVN::Ra->new($ra->{repos_root});
2572 while (length $path) {
2573 unless ($tried{"$ra->{url}/$path"}) {
2574 $ok = $self->read_svm_props($ra, $path, $r);
2575 last if $ok;
2576 $tried{"$ra->{url}/$path"} = 1;
2578 $path =~ s#/?[^/]+$##;
2580 die "Path: '$path' should be ''\n" if $path ne '';
2581 $ok ||= $self->read_svm_props($ra, $path, $r);
2582 $tried{"$ra->{url}/$path"} = 1;
2583 if (!$ok) {
2584 die @err, (map { " $_\n" } keys %tried), "\n";
2586 Git::SVN::Ra->new($self->{url});
2589 sub svnsync {
2590 my ($self) = @_;
2591 return $self->{svnsync} if $self->{svnsync};
2593 if ($self->no_metadata) {
2594 die "Can't have both 'noMetadata' and ",
2595 "'useSvnsyncProps' options set!\n";
2597 if ($self->rewrite_root) {
2598 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2599 "options set!\n";
2601 if ($self->rewrite_uuid) {
2602 die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
2603 "options set!\n";
2606 my $svnsync;
2607 # see if we have it in our config, first:
2608 eval {
2609 my $section = "svn-remote.$self->{repo_id}";
2611 my $url = tmp_config('--get', "$section.svnsync-url");
2612 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2613 die "doesn't look right - svn:sync-from-url is '$url'\n";
2615 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
2616 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
2617 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2619 $svnsync = { url => $url, uuid => $uuid }
2621 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2622 return $self->{svnsync} = $svnsync;
2625 my $err = "useSvnsyncProps set, but failed to read " .
2626 "svnsync property: svn:sync-from-";
2627 my $rp = $self->ra->rev_proplist(0);
2629 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
2630 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2631 die "doesn't look right - svn:sync-from-url is '$url'\n";
2633 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
2634 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
2635 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2637 my $section = "svn-remote.$self->{repo_id}";
2638 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2639 tmp_config('--add', "$section.svnsync-url", $url);
2640 return $self->{svnsync} = { url => $url, uuid => $uuid };
2643 # this allows us to memoize our SVN::Ra UUID locally and avoid a
2644 # remote lookup (useful for 'git svn log').
2645 sub ra_uuid {
2646 my ($self) = @_;
2647 unless ($self->{ra_uuid}) {
2648 my $key = "svn-remote.$self->{repo_id}.uuid";
2649 my $uuid = eval { tmp_config('--get', $key) };
2650 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
2651 $self->{ra_uuid} = $uuid;
2652 } else {
2653 die "ra_uuid called without URL\n" unless $self->{url};
2654 $self->{ra_uuid} = $self->ra->get_uuid;
2655 tmp_config('--add', $key, $self->{ra_uuid});
2658 $self->{ra_uuid};
2661 sub _set_repos_root {
2662 my ($self, $repos_root) = @_;
2663 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2664 $repos_root ||= $self->ra->{repos_root};
2665 tmp_config($k, $repos_root);
2666 $repos_root;
2669 sub repos_root {
2670 my ($self) = @_;
2671 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2672 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2675 sub ra {
2676 my ($self) = shift;
2677 my $ra = Git::SVN::Ra->new($self->{url});
2678 $self->_set_repos_root($ra->{repos_root});
2679 if ($self->use_svm_props && !$self->{svm}) {
2680 if ($self->no_metadata) {
2681 die "Can't have both 'noMetadata' and ",
2682 "'useSvmProps' options set!\n";
2683 } elsif ($self->use_svnsync_props) {
2684 die "Can't have both 'useSvnsyncProps' and ",
2685 "'useSvmProps' options set!\n";
2687 $ra = $self->_set_svm_vars($ra);
2688 $self->{-want_revprops} = 1;
2690 $ra;
2693 # prop_walk(PATH, REV, SUB)
2694 # -------------------------
2695 # Recursively traverse PATH at revision REV and invoke SUB for each
2696 # directory that contains a SVN property. SUB will be invoked as
2697 # follows: &SUB(gs, path, props); where `gs' is this instance of
2698 # Git::SVN, `path' the path to the directory where the properties
2699 # `props' were found. The `path' will be relative to point of checkout,
2700 # that is, if url://repo/trunk is the current Git branch, and that
2701 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
2702 # as `path' (note the trailing `/').
2703 sub prop_walk {
2704 my ($self, $path, $rev, $sub) = @_;
2706 $path =~ s#^/##;
2707 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2708 $path =~ s#^/*#/#g;
2709 my $p = $path;
2710 # Strip the irrelevant part of the path.
2711 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2712 # Ensure the path is terminated by a `/'.
2713 $p =~ s#/*$#/#;
2715 # The properties contain all the internal SVN stuff nobody
2716 # (usually) cares about.
2717 my $interesting_props = 0;
2718 foreach (keys %{$props}) {
2719 # If it doesn't start with `svn:', it must be a
2720 # user-defined property.
2721 ++$interesting_props and next if $_ !~ /^svn:/;
2722 # FIXME: Fragile, if SVN adds new public properties,
2723 # this needs to be updated.
2724 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2725 |eol-style|mime-type
2726 |externals|needs-lock)$/x;
2728 &$sub($self, $p, $props) if $interesting_props;
2730 foreach (sort keys %$dirent) {
2731 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
2732 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
2736 sub last_rev { ($_[0]->last_rev_commit)[0] }
2737 sub last_commit { ($_[0]->last_rev_commit)[1] }
2739 # returns the newest SVN revision number and newest commit SHA1
2740 sub last_rev_commit {
2741 my ($self) = @_;
2742 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2743 return ($self->{last_rev}, $self->{last_commit});
2745 my $c = ::verify_ref($self->refname.'^0');
2746 if ($c && !$self->use_svm_props && !$self->no_metadata) {
2747 my $rev = (::cmt_metadata($c))[1];
2748 if (defined $rev) {
2749 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2750 return ($rev, $c);
2753 my $map_path = $self->map_path;
2754 unless (-e $map_path) {
2755 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2756 return (undef, undef);
2758 my ($rev, $commit) = $self->rev_map_max(1);
2759 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2760 return ($rev, $commit);
2763 sub get_fetch_range {
2764 my ($self, $min, $max) = @_;
2765 $max ||= $self->ra->get_latest_revnum;
2766 $min ||= $self->rev_map_max;
2767 (++$min, $max);
2770 sub tmp_config {
2771 my (@args) = @_;
2772 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2773 my $config = "$ENV{GIT_DIR}/svn/.metadata";
2774 if (! -f $config && -f $old_def_config) {
2775 rename $old_def_config, $config or
2776 die "Failed rename $old_def_config => $config: $!\n";
2778 my $old_config = $ENV{GIT_CONFIG};
2779 $ENV{GIT_CONFIG} = $config;
2780 $@ = undef;
2781 my @ret = eval {
2782 unless (-f $config) {
2783 mkfile($config);
2784 open my $fh, '>', $config or
2785 die "Can't open $config: $!\n";
2786 print $fh "; This file is used internally by ",
2787 "git-svn\n" or die
2788 "Couldn't write to $config: $!\n";
2789 print $fh "; You should not have to edit it\n" or
2790 die "Couldn't write to $config: $!\n";
2791 close $fh or die "Couldn't close $config: $!\n";
2793 command('config', @args);
2795 my $err = $@;
2796 if (defined $old_config) {
2797 $ENV{GIT_CONFIG} = $old_config;
2798 } else {
2799 delete $ENV{GIT_CONFIG};
2801 die $err if $err;
2802 wantarray ? @ret : $ret[0];
2805 sub tmp_index_do {
2806 my ($self, $sub) = @_;
2807 my $old_index = $ENV{GIT_INDEX_FILE};
2808 $ENV{GIT_INDEX_FILE} = $self->{index};
2809 $@ = undef;
2810 my @ret = eval {
2811 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2812 mkpath([$dir]) unless -d $dir;
2813 &$sub;
2815 my $err = $@;
2816 if (defined $old_index) {
2817 $ENV{GIT_INDEX_FILE} = $old_index;
2818 } else {
2819 delete $ENV{GIT_INDEX_FILE};
2821 die $err if $err;
2822 wantarray ? @ret : $ret[0];
2825 sub assert_index_clean {
2826 my ($self, $treeish) = @_;
2828 $self->tmp_index_do(sub {
2829 command_noisy('read-tree', $treeish) unless -e $self->{index};
2830 my $x = command_oneline('write-tree');
2831 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2832 /^tree ($::sha1)/mo);
2833 return if $y eq $x;
2835 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2836 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2837 command_noisy('read-tree', $treeish);
2838 $x = command_oneline('write-tree');
2839 if ($y ne $x) {
2840 ::fatal "trees ($treeish) $y != $x\n",
2841 "Something is seriously wrong...";
2846 sub get_commit_parents {
2847 my ($self, $log_entry) = @_;
2848 my (%seen, @ret, @tmp);
2849 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2850 if (my $ip = $self->{inject_parents}) {
2851 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2852 push @tmp, $commit;
2855 if (my $cur = ::verify_ref($self->refname.'^0')) {
2856 push @tmp, $cur;
2858 if (my $ipd = $self->{inject_parents_dcommit}) {
2859 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2860 push @tmp, @$commit;
2863 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
2864 while (my $p = shift @tmp) {
2865 next if $seen{$p};
2866 $seen{$p} = 1;
2867 push @ret, $p;
2869 @ret;
2872 sub rewrite_root {
2873 my ($self) = @_;
2874 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2875 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2876 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2877 if ($rwr) {
2878 $rwr =~ s#/+$##;
2879 if ($rwr !~ m#^[a-z\+]+://#) {
2880 die "$rwr is not a valid URL (key: $k)\n";
2883 $self->{-rewrite_root} = $rwr;
2886 sub rewrite_uuid {
2887 my ($self) = @_;
2888 return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
2889 my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
2890 my $rwid = eval { command_oneline(qw/config --get/, $k) };
2891 if ($rwid) {
2892 $rwid =~ s#/+$##;
2893 if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
2894 die "$rwid is not a valid UUID (key: $k)\n";
2897 $self->{-rewrite_uuid} = $rwid;
2900 sub metadata_url {
2901 my ($self) = @_;
2902 ($self->rewrite_root || $self->{url}) .
2903 (length $self->{path} ? '/' . $self->{path} : '');
2906 sub full_url {
2907 my ($self) = @_;
2908 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
2911 sub full_pushurl {
2912 my ($self) = @_;
2913 if ($self->{pushurl}) {
2914 return $self->{pushurl} . (length $self->{path} ? '/' .
2915 $self->{path} : '');
2916 } else {
2917 return $self->full_url;
2921 sub set_commit_header_env {
2922 my ($log_entry) = @_;
2923 my %env;
2924 foreach my $ned (qw/NAME EMAIL DATE/) {
2925 foreach my $ac (qw/AUTHOR COMMITTER/) {
2926 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2930 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2931 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2932 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2934 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2935 ? $log_entry->{commit_name}
2936 : $log_entry->{name};
2937 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2938 ? $log_entry->{commit_email}
2939 : $log_entry->{email};
2940 \%env;
2943 sub restore_commit_header_env {
2944 my ($env) = @_;
2945 foreach my $ned (qw/NAME EMAIL DATE/) {
2946 foreach my $ac (qw/AUTHOR COMMITTER/) {
2947 my $k = "GIT_${ac}_${ned}";
2948 if (defined $env->{$k}) {
2949 $ENV{$k} = $env->{$k};
2950 } else {
2951 delete $ENV{$k};
2957 sub gc {
2958 command_noisy('gc', '--auto');
2961 sub do_git_commit {
2962 my ($self, $log_entry) = @_;
2963 my $lr = $self->last_rev;
2964 if (defined $lr && $lr >= $log_entry->{revision}) {
2965 die "Last fetched revision of ", $self->refname,
2966 " was r$lr, but we are about to fetch: ",
2967 "r$log_entry->{revision}!\n";
2969 if (my $c = $self->rev_map_get($log_entry->{revision})) {
2970 croak "$log_entry->{revision} = $c already exists! ",
2971 "Why are we refetching it?\n";
2973 my $old_env = set_commit_header_env($log_entry);
2974 my $tree = $log_entry->{tree};
2975 if (!defined $tree) {
2976 $tree = $self->tmp_index_do(sub {
2977 command_oneline('write-tree') });
2979 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2981 my @exec = ('git', 'commit-tree', $tree);
2982 foreach ($self->get_commit_parents($log_entry)) {
2983 push @exec, '-p', $_;
2985 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2986 or croak $!;
2987 binmode $msg_fh;
2989 # we always get UTF-8 from SVN, but we may want our commits in
2990 # a different encoding.
2991 if (my $enc = Git::config('i18n.commitencoding')) {
2992 require Encode;
2993 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2995 print $msg_fh $log_entry->{log} or croak $!;
2996 restore_commit_header_env($old_env);
2997 unless ($self->no_metadata) {
2998 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2999 or croak $!;
3001 $msg_fh->flush == 0 or croak $!;
3002 close $msg_fh or croak $!;
3003 chomp(my $commit = do { local $/; <$out_fh> });
3004 close $out_fh or croak $!;
3005 waitpid $pid, 0;
3006 croak $? if $?;
3007 if ($commit !~ /^$::sha1$/o) {
3008 die "Failed to commit, invalid sha1: $commit\n";
3011 $self->rev_map_set($log_entry->{revision}, $commit, 1);
3013 $self->{last_rev} = $log_entry->{revision};
3014 $self->{last_commit} = $commit;
3015 print "r$log_entry->{revision}" unless $::_q > 1;
3016 if (defined $log_entry->{svm_revision}) {
3017 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
3018 $self->rev_map_set($log_entry->{svm_revision}, $commit,
3019 0, $self->svm_uuid);
3021 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
3022 if (--$_gc_nr == 0) {
3023 $_gc_nr = $_gc_period;
3024 gc();
3026 return $commit;
3029 sub match_paths {
3030 my ($self, $paths, $r) = @_;
3031 return 1 if $self->{path} eq '';
3032 if (my $path = $paths->{"/$self->{path}"}) {
3033 return ($path->{action} eq 'D') ? 0 : 1;
3035 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
3036 if (grep /$self->{path_regex}/, keys %$paths) {
3037 return 1;
3039 my $c = '';
3040 foreach (split m#/#, $self->{path}) {
3041 $c .= "/$_";
3042 next unless ($paths->{$c} &&
3043 ($paths->{$c}->{action} =~ /^[AR]$/));
3044 if ($self->ra->check_path($self->{path}, $r) ==
3045 $SVN::Node::dir) {
3046 return 1;
3049 return 0;
3052 sub find_parent_branch {
3053 my ($self, $paths, $rev) = @_;
3054 return undef unless $self->follow_parent;
3055 unless (defined $paths) {
3056 my $err_handler = $SVN::Error::handler;
3057 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
3058 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
3059 sub { $paths = $_[0] });
3060 $SVN::Error::handler = $err_handler;
3062 return undef unless defined $paths;
3064 # look for a parent from another branch:
3065 my @b_path_components = split m#/#, $self->{path};
3066 my @a_path_components;
3067 my $i;
3068 while (@b_path_components) {
3069 $i = $paths->{'/'.join('/', @b_path_components)};
3070 last if $i && defined $i->{copyfrom_path};
3071 unshift(@a_path_components, pop(@b_path_components));
3073 return undef unless defined $i && defined $i->{copyfrom_path};
3074 my $branch_from = $i->{copyfrom_path};
3075 if (@a_path_components) {
3076 print STDERR "branch_from: $branch_from => ";
3077 $branch_from .= '/'.join('/', @a_path_components);
3078 print STDERR $branch_from, "\n";
3080 my $r = $i->{copyfrom_rev};
3081 my $repos_root = $self->ra->{repos_root};
3082 my $url = $self->ra->{url};
3083 my $new_url = $url . $branch_from;
3084 print STDERR "Found possible branch point: ",
3085 "$new_url => ", $self->full_url, ", $r\n"
3086 unless $::_q > 1;
3087 $branch_from =~ s#^/##;
3088 my $gs = $self->other_gs($new_url, $url,
3089 $branch_from, $r, $self->{ref_id});
3090 my ($r0, $parent) = $gs->find_rev_before($r, 1);
3092 my ($base, $head);
3093 if (!defined $r0 || !defined $parent) {
3094 ($base, $head) = parse_revision_argument(0, $r);
3095 } else {
3096 if ($r0 < $r) {
3097 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
3098 0, 1, sub { $base = $_[1] - 1 });
3101 if (defined $base && $base <= $r) {
3102 $gs->fetch($base, $r);
3104 ($r0, $parent) = $gs->find_rev_before($r, 1);
3106 if (defined $r0 && defined $parent) {
3107 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
3108 unless $::_q > 1;
3109 my $ed;
3110 if ($self->ra->can_do_switch) {
3111 $self->assert_index_clean($parent);
3112 print STDERR "Following parent with do_switch\n"
3113 unless $::_q > 1;
3114 # do_switch works with svn/trunk >= r22312, but that
3115 # is not included with SVN 1.4.3 (the latest version
3116 # at the moment), so we can't rely on it
3117 $self->{last_rev} = $r0;
3118 $self->{last_commit} = $parent;
3119 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
3120 $gs->ra->gs_do_switch($r0, $rev, $gs,
3121 $self->full_url, $ed)
3122 or die "SVN connection failed somewhere...\n";
3123 } elsif ($self->ra->trees_match($new_url, $r0,
3124 $self->full_url, $rev)) {
3125 print STDERR "Trees match:\n",
3126 " $new_url\@$r0\n",
3127 " ${\$self->full_url}\@$rev\n",
3128 "Following parent with no changes\n"
3129 unless $::_q > 1;
3130 $self->tmp_index_do(sub {
3131 command_noisy('read-tree', $parent);
3133 $self->{last_commit} = $parent;
3134 } else {
3135 print STDERR "Following parent with do_update\n"
3136 unless $::_q > 1;
3137 $ed = SVN::Git::Fetcher->new($self);
3138 $self->ra->gs_do_update($rev, $rev, $self, $ed)
3139 or die "SVN connection failed somewhere...\n";
3141 print STDERR "Successfully followed parent\n" unless $::_q > 1;
3142 return $self->make_log_entry($rev, [$parent], $ed);
3144 return undef;
3147 sub do_fetch {
3148 my ($self, $paths, $rev) = @_;
3149 my $ed;
3150 my ($last_rev, @parents);
3151 if (my $lc = $self->last_commit) {
3152 # we can have a branch that was deleted, then re-added
3153 # under the same name but copied from another path, in
3154 # which case we'll have multiple parents (we don't
3155 # want to break the original ref, nor lose copypath info):
3156 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
3157 push @{$log_entry->{parents}}, $lc;
3158 return $log_entry;
3160 $ed = SVN::Git::Fetcher->new($self);
3161 $last_rev = $self->{last_rev};
3162 $ed->{c} = $lc;
3163 @parents = ($lc);
3164 } else {
3165 $last_rev = $rev;
3166 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
3167 return $log_entry;
3169 $ed = SVN::Git::Fetcher->new($self);
3171 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
3172 die "SVN connection failed somewhere...\n";
3174 $self->make_log_entry($rev, \@parents, $ed);
3177 sub mkemptydirs {
3178 my ($self, $r) = @_;
3180 sub scan {
3181 my ($r, $empty_dirs, $line) = @_;
3182 if (defined $r && $line =~ /^r(\d+)$/) {
3183 return 0 if $1 > $r;
3184 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
3185 $empty_dirs->{$1} = 1;
3186 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
3187 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
3188 delete @$empty_dirs{@d};
3190 1; # continue
3193 my %empty_dirs = ();
3194 my $gz_file = "$self->{dir}/unhandled.log.gz";
3195 if (-f $gz_file) {
3196 if (!$can_compress) {
3197 warn "Compress::Zlib could not be found; ",
3198 "empty directories in $gz_file will not be read\n";
3199 } else {
3200 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
3201 die "Unable to open $gz_file: $!\n";
3202 my $line;
3203 while ($gz->gzreadline($line) > 0) {
3204 scan($r, \%empty_dirs, $line) or last;
3206 $gz->gzclose;
3210 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
3211 binmode $fh or croak "binmode: $!";
3212 while (<$fh>) {
3213 scan($r, \%empty_dirs, $_) or last;
3215 close $fh;
3218 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
3219 foreach my $d (sort keys %empty_dirs) {
3220 $d = uri_decode($d);
3221 $d =~ s/$strip//;
3222 next unless length($d);
3223 next if -d $d;
3224 if (-e $d) {
3225 warn "$d exists but is not a directory\n";
3226 } else {
3227 print "creating empty directory: $d\n";
3228 mkpath([$d]);
3233 sub get_untracked {
3234 my ($self, $ed) = @_;
3235 my @out;
3236 my $h = $ed->{empty};
3237 foreach (sort keys %$h) {
3238 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
3239 push @out, " $act: " . uri_encode($_);
3240 warn "W: $act: $_\n";
3242 foreach my $t (qw/dir_prop file_prop/) {
3243 $h = $ed->{$t} or next;
3244 foreach my $path (sort keys %$h) {
3245 my $ppath = $path eq '' ? '.' : $path;
3246 foreach my $prop (sort keys %{$h->{$path}}) {
3247 next if $SKIP_PROP{$prop};
3248 my $v = $h->{$path}->{$prop};
3249 my $t_ppath_prop = "$t: " .
3250 uri_encode($ppath) . ' ' .
3251 uri_encode($prop);
3252 if (defined $v) {
3253 push @out, " +$t_ppath_prop " .
3254 uri_encode($v);
3255 } else {
3256 push @out, " -$t_ppath_prop";
3261 foreach my $t (qw/absent_file absent_directory/) {
3262 $h = $ed->{$t} or next;
3263 foreach my $parent (sort keys %$h) {
3264 foreach my $path (sort @{$h->{$parent}}) {
3265 push @out, " $t: " .
3266 uri_encode("$parent/$path");
3267 warn "W: $t: $parent/$path ",
3268 "Insufficient permissions?\n";
3272 \@out;
3275 # parse_svn_date(DATE)
3276 # --------------------
3277 # Given a date (in UTC) from Subversion, return a string in the format
3278 # "<TZ Offset> <local date/time>" that Git will use.
3280 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
3281 # is true we'll convert it to the local timezone instead.
3282 sub parse_svn_date {
3283 my $date = shift || return '+0000 1970-01-01 00:00:00';
3284 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
3285 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
3286 croak "Unable to parse date: $date\n";
3287 my $parsed_date; # Set next.
3289 if ($Git::SVN::_localtime) {
3290 # Translate the Subversion datetime to an epoch time.
3291 # Begin by switching ourselves to $date's timezone, UTC.
3292 my $old_env_TZ = $ENV{TZ};
3293 $ENV{TZ} = 'UTC';
3295 my $epoch_in_UTC =
3296 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
3298 # Determine our local timezone (including DST) at the
3299 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
3300 # value of TZ, if any, at the time we were run.
3301 if (defined $Git::SVN::Log::TZ) {
3302 $ENV{TZ} = $Git::SVN::Log::TZ;
3303 } else {
3304 delete $ENV{TZ};
3307 my $our_TZ =
3308 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
3310 # This converts $epoch_in_UTC into our local timezone.
3311 my ($sec, $min, $hour, $mday, $mon, $year,
3312 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
3314 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
3315 $our_TZ, $year + 1900, $mon + 1,
3316 $mday, $hour, $min, $sec);
3318 # Reset us to the timezone in effect when we entered
3319 # this routine.
3320 if (defined $old_env_TZ) {
3321 $ENV{TZ} = $old_env_TZ;
3322 } else {
3323 delete $ENV{TZ};
3325 } else {
3326 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
3329 return $parsed_date;
3332 sub other_gs {
3333 my ($self, $new_url, $url,
3334 $branch_from, $r, $old_ref_id) = @_;
3335 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
3336 unless ($gs) {
3337 my $ref_id = $old_ref_id;
3338 $ref_id =~ s/\@\d+-*$//;
3339 $ref_id .= "\@$r";
3340 # just grow a tail if we're not unique enough :x
3341 $ref_id .= '-' while find_ref($ref_id);
3342 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
3343 if ($u =~ s#^\Q$url\E(/|$)##) {
3344 $p = $u;
3345 $u = $url;
3346 $repo_id = $self->{repo_id};
3348 while (1) {
3349 # It is possible to tag two different subdirectories at
3350 # the same revision. If the url for an existing ref
3351 # does not match, we must either find a ref with a
3352 # matching url or create a new ref by growing a tail.
3353 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
3354 my (undef, $max_commit) = $gs->rev_map_max(1);
3355 last if (!$max_commit);
3356 my ($url) = ::cmt_metadata($max_commit);
3357 last if ($url eq $gs->metadata_url);
3358 $ref_id .= '-';
3360 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
3365 sub call_authors_prog {
3366 my ($orig_author) = @_;
3367 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
3368 my $author = `$::_authors_prog $orig_author`;
3369 if ($? != 0) {
3370 die "$::_authors_prog failed with exit code $?\n"
3372 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
3373 my ($name, $email) = ($1, $2);
3374 $email = undef if length $2 == 0;
3375 return [$name, $email];
3376 } else {
3377 die "Author: $orig_author: $::_authors_prog returned "
3378 . "invalid author format: $author\n";
3382 sub check_author {
3383 my ($author) = @_;
3384 if (!defined $author || length $author == 0) {
3385 $author = '(no author)';
3387 if (!defined $::users{$author}) {
3388 if (defined $::_authors_prog) {
3389 $::users{$author} = call_authors_prog($author);
3390 } elsif (defined $::_authors) {
3391 die "Author: $author not defined in $::_authors file\n";
3394 $author;
3397 sub find_extra_svk_parents {
3398 my ($self, $ed, $tickets, $parents) = @_;
3399 # aha! svk:merge property changed...
3400 my @tickets = split "\n", $tickets;
3401 my @known_parents;
3402 for my $ticket ( @tickets ) {
3403 my ($uuid, $path, $rev) = split /:/, $ticket;
3404 if ( $uuid eq $self->ra_uuid ) {
3405 my $url = $self->{url};
3406 my $repos_root = $url;
3407 my $branch_from = $path;
3408 $branch_from =~ s{^/}{};
3409 my $gs = $self->other_gs($repos_root."/".$branch_from,
3410 $url,
3411 $branch_from,
3412 $rev,
3413 $self->{ref_id});
3414 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
3415 # wahey! we found it, but it might be
3416 # an old one (!)
3417 push @known_parents, [ $rev, $commit ];
3421 # Ordering matters; highest-numbered commit merge tickets
3422 # first, as they may account for later merge ticket additions
3423 # or changes.
3424 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
3425 for my $parent ( @known_parents ) {
3426 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
3427 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3428 my $new;
3429 while ( <$msg_fh> ) {
3430 $new=1;last;
3432 command_close_pipe($msg_fh, $ctx);
3433 if ( $new ) {
3434 print STDERR
3435 "Found merge parent (svk:merge ticket): $parent\n";
3436 push @$parents, $parent;
3441 sub lookup_svn_merge {
3442 my $uuid = shift;
3443 my $url = shift;
3444 my $merge = shift;
3446 my ($source, $revs) = split ":", $merge;
3447 my $path = $source;
3448 $path =~ s{^/}{};
3449 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3450 if ( !$gs ) {
3451 warn "Couldn't find revmap for $url$source\n";
3452 return;
3454 my @ranges = split ",", $revs;
3455 my ($tip, $tip_commit);
3456 my @merged_commit_ranges;
3457 # find the tip
3458 for my $range ( @ranges ) {
3459 my ($bottom, $top) = split "-", $range;
3460 $top ||= $bottom;
3461 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3462 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
3464 unless ($top_commit and $bottom_commit) {
3465 warn "W:unknown path/rev in svn:mergeinfo "
3466 ."dirprop: $source:$range\n";
3467 next;
3470 if (scalar(command('rev-parse', "$bottom_commit^@"))) {
3471 push @merged_commit_ranges,
3472 "$bottom_commit^..$top_commit";
3473 } else {
3474 push @merged_commit_ranges, "$top_commit";
3477 if ( !defined $tip or $top > $tip ) {
3478 $tip = $top;
3479 $tip_commit = $top_commit;
3482 return ($tip_commit, @merged_commit_ranges);
3485 sub _rev_list {
3486 my ($msg_fh, $ctx) = command_output_pipe(
3487 "rev-list", @_,
3489 my @rv;
3490 while ( <$msg_fh> ) {
3491 chomp;
3492 push @rv, $_;
3494 command_close_pipe($msg_fh, $ctx);
3495 @rv;
3498 sub check_cherry_pick {
3499 my $base = shift;
3500 my $tip = shift;
3501 my $parents = shift;
3502 my @ranges = @_;
3503 my %commits = map { $_ => 1 }
3504 _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--");
3505 for my $range ( @ranges ) {
3506 delete @commits{_rev_list($range, "--")};
3508 for my $commit (keys %commits) {
3509 if (has_no_changes($commit)) {
3510 delete $commits{$commit};
3513 return (keys %commits);
3516 sub has_no_changes {
3517 my $commit = shift;
3519 my @revs = split / /, command_oneline(
3520 qw(rev-list --parents -1 -m), $commit);
3522 # Commits with no parents, e.g. the start of a partial branch,
3523 # have changes by definition.
3524 return 1 if (@revs < 2);
3526 # Commits with multiple parents, e.g a merge, have no changes
3527 # by definition.
3528 return 0 if (@revs > 2);
3530 return (command_oneline("rev-parse", "$commit^{tree}") eq
3531 command_oneline("rev-parse", "$commit~1^{tree}"));
3534 # The GIT_DIR environment variable is not always set until after the command
3535 # line arguments are processed, so we can't memoize in a BEGIN block.
3537 my $memoized = 0;
3539 sub memoize_svn_mergeinfo_functions {
3540 return if $memoized;
3541 $memoized = 1;
3543 my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
3544 mkpath([$cache_path]) unless -d $cache_path;
3546 tie my %lookup_svn_merge_cache => 'Memoize::Storable',
3547 "$cache_path/lookup_svn_merge.db", 'nstore';
3548 memoize 'lookup_svn_merge',
3549 SCALAR_CACHE => 'FAULT',
3550 LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
3553 tie my %check_cherry_pick_cache => 'Memoize::Storable',
3554 "$cache_path/check_cherry_pick.db", 'nstore';
3555 memoize 'check_cherry_pick',
3556 SCALAR_CACHE => 'FAULT',
3557 LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
3560 tie my %has_no_changes_cache => 'Memoize::Storable',
3561 "$cache_path/has_no_changes.db", 'nstore';
3562 memoize 'has_no_changes',
3563 SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
3564 LIST_CACHE => 'FAULT',
3568 sub unmemoize_svn_mergeinfo_functions {
3569 return if not $memoized;
3570 $memoized = 0;
3572 Memoize::unmemoize 'lookup_svn_merge';
3573 Memoize::unmemoize 'check_cherry_pick';
3574 Memoize::unmemoize 'has_no_changes';
3577 Memoize::memoize 'Git::SVN::repos_root';
3580 END {
3581 # Force cache writeout explicitly instead of waiting for
3582 # global destruction to avoid segfault in Storable:
3583 # http://rt.cpan.org/Public/Bug/Display.html?id=36087
3584 unmemoize_svn_mergeinfo_functions();
3587 sub parents_exclude {
3588 my $parents = shift;
3589 my @commits = @_;
3590 return unless @commits;
3592 my @excluded;
3593 my $excluded;
3594 do {
3595 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
3596 $excluded = command_oneline(@cmd);
3597 if ( $excluded ) {
3598 my @new;
3599 my $found;
3600 for my $commit ( @commits ) {
3601 if ( $commit eq $excluded ) {
3602 push @excluded, $commit;
3603 $found++;
3604 last;
3606 else {
3607 push @new, $commit;
3610 die "saw commit '$excluded' in rev-list output, "
3611 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
3612 unless $found;
3613 @commits = @new;
3616 while ($excluded and @commits);
3618 return @excluded;
3622 # note: this function should only be called if the various dirprops
3623 # have actually changed
3624 sub find_extra_svn_parents {
3625 my ($self, $ed, $mergeinfo, $parents) = @_;
3626 # aha! svk:merge property changed...
3628 memoize_svn_mergeinfo_functions();
3630 # We first search for merged tips which are not in our
3631 # history. Then, we figure out which git revisions are in
3632 # that tip, but not this revision. If all of those revisions
3633 # are now marked as merge, we can add the tip as a parent.
3634 my @merges = split "\n", $mergeinfo;
3635 my @merge_tips;
3636 my $url = $self->{url};
3637 my $uuid = $self->ra_uuid;
3638 my %ranges;
3639 for my $merge ( @merges ) {
3640 my ($tip_commit, @ranges) =
3641 lookup_svn_merge( $uuid, $url, $merge );
3642 unless (!$tip_commit or
3643 grep { $_ eq $tip_commit } @$parents ) {
3644 push @merge_tips, $tip_commit;
3645 $ranges{$tip_commit} = \@ranges;
3646 } else {
3647 push @merge_tips, undef;
3651 my %excluded = map { $_ => 1 }
3652 parents_exclude($parents, grep { defined } @merge_tips);
3654 # check merge tips for new parents
3655 my @new_parents;
3656 for my $merge_tip ( @merge_tips ) {
3657 my $spec = shift @merges;
3658 next unless $merge_tip and $excluded{$merge_tip};
3660 my $ranges = $ranges{$merge_tip};
3662 # check out 'new' tips
3663 my $merge_base;
3664 eval {
3665 $merge_base = command_oneline(
3666 "merge-base",
3667 @$parents, $merge_tip,
3670 if ($@) {
3671 die "An error occurred during merge-base"
3672 unless $@->isa("Git::Error::Command");
3674 warn "W: Cannot find common ancestor between ".
3675 "@$parents and $merge_tip. Ignoring merge info.\n";
3676 next;
3679 # double check that there are no missing non-merge commits
3680 my (@incomplete) = check_cherry_pick(
3681 $merge_base, $merge_tip,
3682 $parents,
3683 @$ranges,
3686 if ( @incomplete ) {
3687 warn "W:svn cherry-pick ignored ($spec) - missing "
3688 .@incomplete." commit(s) (eg $incomplete[0])\n";
3689 } else {
3690 warn
3691 "Found merge parent (svn:mergeinfo prop): ",
3692 $merge_tip, "\n";
3693 push @new_parents, $merge_tip;
3697 # cater for merges which merge commits from multiple branches
3698 if ( @new_parents > 1 ) {
3699 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
3700 for ( my $j = 0; $j <= $#new_parents; $j++ ) {
3701 next if $i == $j;
3702 next unless $new_parents[$i];
3703 next unless $new_parents[$j];
3704 my $revs = command_oneline(
3705 "rev-list", "-1",
3706 "$new_parents[$i]..$new_parents[$j]",
3708 if ( !$revs ) {
3709 undef($new_parents[$j]);
3714 push @$parents, grep { defined } @new_parents;
3717 sub make_log_entry {
3718 my ($self, $rev, $parents, $ed) = @_;
3719 my $untracked = $self->get_untracked($ed);
3721 my @parents = @$parents;
3722 my $ps = $ed->{path_strip} || "";
3723 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3724 my $props = $ed->{dir_prop}{$path};
3725 if ( $props->{"svk:merge"} ) {
3726 $self->find_extra_svk_parents
3727 ($ed, $props->{"svk:merge"}, \@parents);
3729 if ( $props->{"svn:mergeinfo"} ) {
3730 $self->find_extra_svn_parents
3731 ($ed,
3732 $props->{"svn:mergeinfo"},
3733 \@parents);
3737 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
3738 print $un "r$rev\n" or croak $!;
3739 print $un $_, "\n" foreach @$untracked;
3740 my %log_entry = ( parents => \@parents, revision => $rev,
3741 log => '');
3743 my $headrev;
3744 my $logged = delete $self->{logged_rev_props};
3745 if (!$logged || $self->{-want_revprops}) {
3746 my $rp = $self->ra->rev_proplist($rev);
3747 foreach (sort keys %$rp) {
3748 my $v = $rp->{$_};
3749 if (/^svn:(author|date|log)$/) {
3750 $log_entry{$1} = $v;
3751 } elsif ($_ eq 'svm:headrev') {
3752 $headrev = $v;
3753 } else {
3754 print $un " rev_prop: ", uri_encode($_), ' ',
3755 uri_encode($v), "\n";
3758 } else {
3759 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
3761 close $un or croak $!;
3763 $log_entry{date} = parse_svn_date($log_entry{date});
3764 $log_entry{log} .= "\n";
3765 my $author = $log_entry{author} = check_author($log_entry{author});
3766 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
3767 : ($author, undef);
3769 my ($commit_name, $commit_email) = ($name, $email);
3770 if ($_use_log_author) {
3771 my $name_field;
3772 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3773 $name_field = $1;
3774 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3775 $name_field = $1;
3777 if (!defined $name_field) {
3778 if (!defined $email) {
3779 $email = $name;
3781 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
3782 ($name, $email) = ($1, $2);
3783 } elsif ($name_field =~ /(.*)@/) {
3784 ($name, $email) = ($1, $name_field);
3785 } else {
3786 ($name, $email) = ($name_field, $name_field);
3789 if (defined $headrev && $self->use_svm_props) {
3790 if ($self->rewrite_root) {
3791 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3792 "options set!\n";
3794 if ($self->rewrite_uuid) {
3795 die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
3796 "options set!\n";
3798 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
3799 # we don't want "SVM: initializing mirror for junk" ...
3800 return undef if $r == 0;
3801 my $svm = $self->svm;
3802 if ($uuid ne $svm->{uuid}) {
3803 die "UUID mismatch on SVM path:\n",
3804 "expected: $svm->{uuid}\n",
3805 " got: $uuid\n";
3807 my $full_url = $self->full_url;
3808 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3809 die "Failed to replace '$svm->{replace}' with ",
3810 "'$svm->{source}' in $full_url\n";
3811 # throw away username for storing in records
3812 remove_username($full_url);
3813 $log_entry{metadata} = "$full_url\@$r $uuid";
3814 $log_entry{svm_revision} = $r;
3815 $email ||= "$author\@$uuid";
3816 $commit_email ||= "$author\@$uuid";
3817 } elsif ($self->use_svnsync_props) {
3818 my $full_url = $self->svnsync->{url};
3819 $full_url .= "/$self->{path}" if length $self->{path};
3820 remove_username($full_url);
3821 my $uuid = $self->svnsync->{uuid};
3822 $log_entry{metadata} = "$full_url\@$rev $uuid";
3823 $email ||= "$author\@$uuid";
3824 $commit_email ||= "$author\@$uuid";
3825 } else {
3826 my $url = $self->metadata_url;
3827 remove_username($url);
3828 my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
3829 $log_entry{metadata} = "$url\@$rev " . $uuid;
3830 $email ||= "$author\@" . $uuid;
3831 $commit_email ||= "$author\@" . $uuid;
3833 $log_entry{name} = $name;
3834 $log_entry{email} = $email;
3835 $log_entry{commit_name} = $commit_name;
3836 $log_entry{commit_email} = $commit_email;
3837 \%log_entry;
3840 sub fetch {
3841 my ($self, $min_rev, $max_rev, @parents) = @_;
3842 my ($last_rev, $last_commit) = $self->last_rev_commit;
3843 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
3844 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
3847 sub set_tree_cb {
3848 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
3849 $self->{inject_parents} = { $rev => $tree };
3850 $self->fetch(undef, undef);
3853 sub set_tree {
3854 my ($self, $tree) = (shift, shift);
3855 my $log_entry = ::get_commit_entry($tree);
3856 unless ($self->{last_rev}) {
3857 ::fatal("Must have an existing revision to commit");
3859 my %ed_opts = ( r => $self->{last_rev},
3860 log => $log_entry->{log},
3861 ra => $self->ra,
3862 tree_a => $self->{last_commit},
3863 tree_b => $tree,
3864 editor_cb => sub {
3865 $self->set_tree_cb($log_entry, $tree, @_) },
3866 svn_path => $self->{path} );
3867 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
3868 print "No changes\nr$self->{last_rev} = $tree\n";
3872 sub rebuild_from_rev_db {
3873 my ($self, $path) = @_;
3874 my $r = -1;
3875 open my $fh, '<', $path or croak "open: $!";
3876 binmode $fh or croak "binmode: $!";
3877 while (<$fh>) {
3878 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3879 chomp($_);
3880 ++$r;
3881 next if $_ eq ('0' x 40);
3882 $self->rev_map_set($r, $_);
3883 print "r$r = $_\n";
3885 close $fh or croak "close: $!";
3886 unlink $path or croak "unlink: $!";
3889 sub rebuild {
3890 my ($self) = @_;
3891 my $map_path = $self->map_path;
3892 my $partial = (-e $map_path && ! -z $map_path);
3893 return unless ::verify_ref($self->refname.'^0');
3894 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
3895 my $rev_db = $self->rev_db_path;
3896 $self->rebuild_from_rev_db($rev_db);
3897 if ($self->use_svm_props) {
3898 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3899 $self->rebuild_from_rev_db($svm_rev_db);
3901 $self->unlink_rev_db_symlink;
3902 return;
3904 print "Rebuilding $map_path ...\n" if (!$partial);
3905 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3906 (undef, undef));
3907 my ($log, $ctx) =
3908 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
3909 ($head ? "$head.." : "") . $self->refname,
3910 '--');
3911 my $metadata_url = $self->metadata_url;
3912 remove_username($metadata_url);
3913 my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
3914 my $c;
3915 while (<$log>) {
3916 if ( m{^commit ($::sha1)$} ) {
3917 $c = $1;
3918 next;
3920 next unless s{^\s*(git-svn-id:)}{$1};
3921 my ($url, $rev, $uuid) = ::extract_metadata($_);
3922 remove_username($url);
3924 # ignore merges (from set-tree)
3925 next if (!defined $rev || !$uuid);
3927 # if we merged or otherwise started elsewhere, this is
3928 # how we break out of it
3929 if (($uuid ne $svn_uuid) ||
3930 ($metadata_url && $url && ($url ne $metadata_url))) {
3931 next;
3933 if ($partial && $head) {
3934 print "Partial-rebuilding $map_path ...\n";
3935 print "Currently at $base_rev = $head\n";
3936 $head = undef;
3939 $self->rev_map_set($rev, $c);
3940 print "r$rev = $c\n";
3942 command_close_pipe($log, $ctx);
3943 print "Done rebuilding $map_path\n" if (!$partial || !$head);
3944 my $rev_db_path = $self->rev_db_path;
3945 if (-f $self->rev_db_path) {
3946 unlink $self->rev_db_path or croak "unlink: $!";
3948 $self->unlink_rev_db_symlink;
3951 # rev_map:
3952 # Tie::File seems to be prone to offset errors if revisions get sparse,
3953 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3954 # one of my favorite modules is out :< Next up would be one of the DBM
3955 # modules, but I'm not sure which is most portable...
3957 # This is the replacement for the rev_db format, which was too big
3958 # and inefficient for large repositories with a lot of sparse history
3959 # (mainly tags)
3961 # The format is this:
3962 # - 24 bytes for every record,
3963 # * 4 bytes for the integer representing an SVN revision number
3964 # * 20 bytes representing the sha1 of a git commit
3965 # - No empty padding records like the old format
3966 # (except the last record, which can be overwritten)
3967 # - new records are written append-only since SVN revision numbers
3968 # increase monotonically
3969 # - lookups on SVN revision number are done via a binary search
3970 # - Piping the file to xxd -c24 is a good way of dumping it for
3971 # viewing or editing (piped back through xxd -r), should the need
3972 # ever arise.
3973 # - The last record can be padding revision with an all-zero sha1
3974 # This is used to optimize fetch performance when using multiple
3975 # "fetch" directives in .git/config
3977 # These files are disposable unless noMetadata or useSvmProps is set
3979 sub _rev_map_set {
3980 my ($fh, $rev, $commit) = @_;
3982 binmode $fh or croak "binmode: $!";
3983 my $size = (stat($fh))[7];
3984 ($size % 24) == 0 or croak "inconsistent size: $size";
3986 my $wr_offset = 0;
3987 if ($size > 0) {
3988 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3989 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3990 $read == 24 or croak "read only $read bytes (!= 24)";
3991 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
3992 if ($last_commit eq ('0' x40)) {
3993 if ($size >= 48) {
3994 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3995 $read = sysread($fh, $buf, 24) or
3996 croak "read: $!";
3997 $read == 24 or
3998 croak "read only $read bytes (!= 24)";
3999 ($last_rev, $last_commit) =
4000 unpack(rev_map_fmt, $buf);
4001 if ($last_commit eq ('0' x40)) {
4002 croak "inconsistent .rev_map\n";
4005 if ($last_rev >= $rev) {
4006 croak "last_rev is higher!: $last_rev >= $rev";
4008 $wr_offset = -24;
4011 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
4012 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
4013 croak "write: $!";
4016 sub _rev_map_reset {
4017 my ($fh, $rev, $commit) = @_;
4018 my $c = _rev_map_get($fh, $rev);
4019 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
4020 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
4021 truncate $fh, $offset or croak "truncate: $!";
4024 sub mkfile {
4025 my ($path) = @_;
4026 unless (-e $path) {
4027 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
4028 mkpath([$dir]) unless -d $dir;
4029 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
4030 close $fh or die "Couldn't close (create) $path: $!\n";
4034 sub rev_map_set {
4035 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
4036 defined $commit or die "missing arg3\n";
4037 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
4038 my $db = $self->map_path($uuid);
4039 my $db_lock = "$db.lock";
4040 my $sig;
4041 $update_ref ||= 0;
4042 if ($update_ref) {
4043 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
4044 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
4046 mkfile($db);
4048 $LOCKFILES{$db_lock} = 1;
4049 my $sync;
4050 # both of these options make our .rev_db file very, very important
4051 # and we can't afford to lose it because rebuild() won't work
4052 if ($self->use_svm_props || $self->no_metadata) {
4053 $sync = 1;
4054 copy($db, $db_lock) or die "rev_map_set(@_): ",
4055 "Failed to copy: ",
4056 "$db => $db_lock ($!)\n";
4057 } else {
4058 rename $db, $db_lock or die "rev_map_set(@_): ",
4059 "Failed to rename: ",
4060 "$db => $db_lock ($!)\n";
4063 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
4064 or croak "Couldn't open $db_lock: $!\n";
4065 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
4066 _rev_map_set($fh, $rev, $commit);
4067 if ($sync) {
4068 $fh->flush or die "Couldn't flush $db_lock: $!\n";
4069 $fh->sync or die "Couldn't sync $db_lock: $!\n";
4071 close $fh or croak $!;
4072 if ($update_ref) {
4073 $_head = $self;
4074 my $note = "";
4075 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
4076 command_noisy('update-ref', '-m', "r$rev$note",
4077 $self->refname, $commit);
4079 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
4080 "$db_lock => $db ($!)\n";
4081 delete $LOCKFILES{$db_lock};
4082 if ($update_ref) {
4083 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
4084 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
4085 kill $sig, $$ if defined $sig;
4089 # If want_commit, this will return an array of (rev, commit) where
4090 # commit _must_ be a valid commit in the archive.
4091 # Otherwise, it'll return the max revision (whether or not the
4092 # commit is valid or just a 0x40 placeholder).
4093 sub rev_map_max {
4094 my ($self, $want_commit) = @_;
4095 $self->rebuild;
4096 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
4097 $want_commit ? ($r, $c) : $r;
4100 sub rev_map_max_norebuild {
4101 my ($self, $want_commit) = @_;
4102 my $map_path = $self->map_path;
4103 stat $map_path or return $want_commit ? (0, undef) : 0;
4104 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
4105 binmode $fh or croak "binmode: $!";
4106 my $size = (stat($fh))[7];
4107 ($size % 24) == 0 or croak "inconsistent size: $size";
4109 if ($size == 0) {
4110 close $fh or croak "close: $!";
4111 return $want_commit ? (0, undef) : 0;
4114 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
4115 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
4116 my ($r, $c) = unpack(rev_map_fmt, $buf);
4117 if ($want_commit && $c eq ('0' x40)) {
4118 if ($size < 48) {
4119 return $want_commit ? (0, undef) : 0;
4121 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
4122 sysread($fh, $buf, 24) == 24 or croak "read: $!";
4123 ($r, $c) = unpack(rev_map_fmt, $buf);
4124 if ($c eq ('0'x40)) {
4125 croak "Penultimate record is all-zeroes in $map_path";
4128 close $fh or croak "close: $!";
4129 $want_commit ? ($r, $c) : $r;
4132 sub rev_map_get {
4133 my ($self, $rev, $uuid) = @_;
4134 my $map_path = $self->map_path($uuid);
4135 return undef unless -e $map_path;
4137 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
4138 my $c = _rev_map_get($fh, $rev);
4139 close($fh) or croak "close: $!";
4143 sub _rev_map_get {
4144 my ($fh, $rev) = @_;
4146 binmode $fh or croak "binmode: $!";
4147 my $size = (stat($fh))[7];
4148 ($size % 24) == 0 or croak "inconsistent size: $size";
4150 if ($size == 0) {
4151 return undef;
4154 my ($l, $u) = (0, $size - 24);
4155 my ($r, $c, $buf);
4157 while ($l <= $u) {
4158 my $i = int(($l/24 + $u/24) / 2) * 24;
4159 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
4160 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
4161 my ($r, $c) = unpack(rev_map_fmt, $buf);
4163 if ($r < $rev) {
4164 $l = $i + 24;
4165 } elsif ($r > $rev) {
4166 $u = $i - 24;
4167 } else { # $r == $rev
4168 return $c eq ('0' x 40) ? undef : $c;
4171 undef;
4174 # Finds the first svn revision that exists on (if $eq_ok is true) or
4175 # before $rev for the current branch. It will not search any lower
4176 # than $min_rev. Returns the git commit hash and svn revision number
4177 # if found, else (undef, undef).
4178 sub find_rev_before {
4179 my ($self, $rev, $eq_ok, $min_rev) = @_;
4180 --$rev unless $eq_ok;
4181 $min_rev ||= 1;
4182 my $max_rev = $self->rev_map_max;
4183 $rev = $max_rev if ($rev > $max_rev);
4184 while ($rev >= $min_rev) {
4185 if (my $c = $self->rev_map_get($rev)) {
4186 return ($rev, $c);
4188 --$rev;
4190 return (undef, undef);
4193 # Finds the first svn revision that exists on (if $eq_ok is true) or
4194 # after $rev for the current branch. It will not search any higher
4195 # than $max_rev. Returns the git commit hash and svn revision number
4196 # if found, else (undef, undef).
4197 sub find_rev_after {
4198 my ($self, $rev, $eq_ok, $max_rev) = @_;
4199 ++$rev unless $eq_ok;
4200 $max_rev ||= $self->rev_map_max;
4201 while ($rev <= $max_rev) {
4202 if (my $c = $self->rev_map_get($rev)) {
4203 return ($rev, $c);
4205 ++$rev;
4207 return (undef, undef);
4210 sub _new {
4211 my ($class, $repo_id, $ref_id, $path) = @_;
4212 unless (defined $repo_id && length $repo_id) {
4213 $repo_id = $Git::SVN::default_repo_id;
4215 unless (defined $ref_id && length $ref_id) {
4216 $_prefix = '' unless defined($_prefix);
4217 $_[2] = $ref_id =
4218 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
4220 $_[1] = $repo_id;
4221 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
4223 # Older repos imported by us used $GIT_DIR/svn/foo instead of
4224 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
4225 if ($ref_id =~ m{^refs/remotes/(.*)}) {
4226 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
4227 if (-d $old_dir && ! -d $dir) {
4228 $dir = $old_dir;
4232 $_[3] = $path = '' unless (defined $path);
4233 mkpath([$dir]);
4234 bless {
4235 ref_id => $ref_id, dir => $dir, index => "$dir/index",
4236 path => $path, config => "$ENV{GIT_DIR}/svn/config",
4237 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
4240 # for read-only access of old .rev_db formats
4241 sub unlink_rev_db_symlink {
4242 my ($self) = @_;
4243 my $link = $self->rev_db_path;
4244 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
4245 if (-l $link) {
4246 unlink $link or croak "unlink: $link failed!";
4250 sub rev_db_path {
4251 my ($self, $uuid) = @_;
4252 my $db_path = $self->map_path($uuid);
4253 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
4254 or croak "map_path: $db_path does not contain '/.rev_map.' !";
4255 $db_path;
4258 # the new replacement for .rev_db
4259 sub map_path {
4260 my ($self, $uuid) = @_;
4261 $uuid ||= $self->ra_uuid;
4262 "$self->{map_root}.$uuid";
4265 sub uri_encode {
4266 my ($f) = @_;
4267 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
4271 sub uri_decode {
4272 my ($f) = @_;
4273 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
4277 sub remove_username {
4278 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
4281 package Git::SVN::Prompt;
4282 use strict;
4283 use warnings;
4284 require SVN::Core;
4285 use vars qw/$_no_auth_cache $_username/;
4287 sub simple {
4288 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
4289 $may_save = undef if $_no_auth_cache;
4290 $default_username = $_username if defined $_username;
4291 if (defined $default_username && length $default_username) {
4292 if (defined $realm && length $realm) {
4293 print STDERR "Authentication realm: $realm\n";
4294 STDERR->flush;
4296 $cred->username($default_username);
4297 } else {
4298 username($cred, $realm, $may_save, $pool);
4300 $cred->password(_read_password("Password for '" .
4301 $cred->username . "': ", $realm));
4302 $cred->may_save($may_save);
4303 $SVN::_Core::SVN_NO_ERROR;
4306 sub ssl_server_trust {
4307 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
4308 $may_save = undef if $_no_auth_cache;
4309 print STDERR "Error validating server certificate for '$realm':\n";
4311 no warnings 'once';
4312 # All variables SVN::Auth::SSL::* are used only once,
4313 # so we're shutting up Perl warnings about this.
4314 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
4315 print STDERR " - The certificate is not issued ",
4316 "by a trusted authority. Use the\n",
4317 " fingerprint to validate ",
4318 "the certificate manually!\n";
4320 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
4321 print STDERR " - The certificate hostname ",
4322 "does not match.\n";
4324 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
4325 print STDERR " - The certificate is not yet valid.\n";
4327 if ($failures & $SVN::Auth::SSL::EXPIRED) {
4328 print STDERR " - The certificate has expired.\n";
4330 if ($failures & $SVN::Auth::SSL::OTHER) {
4331 print STDERR " - The certificate has ",
4332 "an unknown error.\n";
4334 } # no warnings 'once'
4335 printf STDERR
4336 "Certificate information:\n".
4337 " - Hostname: %s\n".
4338 " - Valid: from %s until %s\n".
4339 " - Issuer: %s\n".
4340 " - Fingerprint: %s\n",
4341 map $cert_info->$_, qw(hostname valid_from valid_until
4342 issuer_dname fingerprint);
4343 my $choice;
4344 prompt:
4345 print STDERR $may_save ?
4346 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
4347 "(R)eject or accept (t)emporarily? ";
4348 STDERR->flush;
4349 $choice = lc(substr(<STDIN> || 'R', 0, 1));
4350 if ($choice =~ /^t$/i) {
4351 $cred->may_save(undef);
4352 } elsif ($choice =~ /^r$/i) {
4353 return -1;
4354 } elsif ($may_save && $choice =~ /^p$/i) {
4355 $cred->may_save($may_save);
4356 } else {
4357 goto prompt;
4359 $cred->accepted_failures($failures);
4360 $SVN::_Core::SVN_NO_ERROR;
4363 sub ssl_client_cert {
4364 my ($cred, $realm, $may_save, $pool) = @_;
4365 $may_save = undef if $_no_auth_cache;
4366 print STDERR "Client certificate filename: ";
4367 STDERR->flush;
4368 chomp(my $filename = <STDIN>);
4369 $cred->cert_file($filename);
4370 $cred->may_save($may_save);
4371 $SVN::_Core::SVN_NO_ERROR;
4374 sub ssl_client_cert_pw {
4375 my ($cred, $realm, $may_save, $pool) = @_;
4376 $may_save = undef if $_no_auth_cache;
4377 $cred->password(_read_password("Password: ", $realm));
4378 $cred->may_save($may_save);
4379 $SVN::_Core::SVN_NO_ERROR;
4382 sub username {
4383 my ($cred, $realm, $may_save, $pool) = @_;
4384 $may_save = undef if $_no_auth_cache;
4385 if (defined $realm && length $realm) {
4386 print STDERR "Authentication realm: $realm\n";
4388 my $username;
4389 if (defined $_username) {
4390 $username = $_username;
4391 } else {
4392 print STDERR "Username: ";
4393 STDERR->flush;
4394 chomp($username = <STDIN>);
4396 $cred->username($username);
4397 $cred->may_save($may_save);
4398 $SVN::_Core::SVN_NO_ERROR;
4401 sub _read_password {
4402 my ($prompt, $realm) = @_;
4403 my $password = '';
4404 if (exists $ENV{GIT_ASKPASS}) {
4405 open(PH, "-|", $ENV{GIT_ASKPASS}, $prompt);
4406 $password = <PH>;
4407 $password =~ s/[\012\015]//; # \n\r
4408 close(PH);
4409 } else {
4410 print STDERR $prompt;
4411 STDERR->flush;
4412 require Term::ReadKey;
4413 Term::ReadKey::ReadMode('noecho');
4414 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
4415 last if $key =~ /[\012\015]/; # \n\r
4416 $password .= $key;
4418 Term::ReadKey::ReadMode('restore');
4419 print STDERR "\n";
4420 STDERR->flush;
4422 $password;
4425 package SVN::Git::Fetcher;
4426 use vars qw/@ISA $_ignore_regex $_preserve_empty_dirs $_placeholder_filename
4427 @deleted_gpath %added_placeholder $repo_id/;
4428 use strict;
4429 use warnings;
4430 use Carp qw/croak/;
4431 use File::Basename qw/dirname/;
4432 use IO::File qw//;
4434 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
4435 sub new {
4436 my ($class, $git_svn, $switch_path) = @_;
4437 my $self = SVN::Delta::Editor->new;
4438 bless $self, $class;
4439 if (exists $git_svn->{last_commit}) {
4440 $self->{c} = $git_svn->{last_commit};
4441 $self->{empty_symlinks} =
4442 _mark_empty_symlinks($git_svn, $switch_path);
4445 # some options are read globally, but can be overridden locally
4446 # per [svn-remote "..."] section. Command-line options will *NOT*
4447 # override options set in an [svn-remote "..."] section
4448 $repo_id = $git_svn->{repo_id};
4449 my $k = "svn-remote.$repo_id.ignore-paths";
4450 my $v = eval { command_oneline('config', '--get', $k) };
4451 $self->{ignore_regex} = $v;
4453 $k = "svn-remote.$repo_id.preserve-empty-dirs";
4454 $v = eval { command_oneline('config', '--get', '--bool', $k) };
4455 if ($v && $v eq 'true') {
4456 $_preserve_empty_dirs = 1;
4457 $k = "svn-remote.$repo_id.placeholder-filename";
4458 $v = eval { command_oneline('config', '--get', $k) };
4459 $_placeholder_filename = $v;
4462 # Load the list of placeholder files added during previous invocations.
4463 $k = "svn-remote.$repo_id.added-placeholder";
4464 $v = eval { command_oneline('config', '--get-all', $k) };
4465 if ($_preserve_empty_dirs && $v) {
4466 # command() prints errors to stderr, so we only call it if
4467 # command_oneline() succeeded.
4468 my @v = command('config', '--get-all', $k);
4469 $added_placeholder{ dirname($_) } = $_ foreach @v;
4472 $self->{empty} = {};
4473 $self->{dir_prop} = {};
4474 $self->{file_prop} = {};
4475 $self->{absent_dir} = {};
4476 $self->{absent_file} = {};
4477 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
4478 $self->{pathnameencoding} = Git::config('svn.pathnameencoding');
4479 $self;
4482 # this uses the Ra object, so it must be called before do_{switch,update},
4483 # not inside them (when the Git::SVN::Fetcher object is passed) to
4484 # do_{switch,update}
4485 sub _mark_empty_symlinks {
4486 my ($git_svn, $switch_path) = @_;
4487 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
4488 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
4490 my %ret;
4491 my ($rev, $cmt) = $git_svn->last_rev_commit;
4492 return {} unless ($rev && $cmt);
4494 # allow the warning to be printed for each revision we fetch to
4495 # ensure the user sees it. The user can also disable the workaround
4496 # on the repository even while git svn is running and the next
4497 # revision fetched will skip this expensive function.
4498 my $printed_warning;
4499 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
4500 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
4501 local $/ = "\0";
4502 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
4503 $pfx .= '/' if length($pfx);
4504 while (<$ls>) {
4505 chomp;
4506 s/\A100644 blob $empty_blob\t//o or next;
4507 unless ($printed_warning) {
4508 print STDERR "Scanning for empty symlinks, ",
4509 "this may take a while if you have ",
4510 "many empty files\n",
4511 "You may disable this with `",
4512 "git config svn.brokenSymlinkWorkaround ",
4513 "false'.\n",
4514 "This may be done in a different ",
4515 "terminal without restarting ",
4516 "git svn\n";
4517 $printed_warning = 1;
4519 my $path = $_;
4520 my (undef, $props) =
4521 $git_svn->ra->get_file($pfx.$path, $rev, undef);
4522 if ($props->{'svn:special'}) {
4523 $ret{$path} = 1;
4526 command_close_pipe($ls, $ctx);
4527 \%ret;
4530 # returns true if a given path is inside a ".git" directory
4531 sub in_dot_git {
4532 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
4535 # return value: 0 -- don't ignore, 1 -- ignore
4536 sub is_path_ignored {
4537 my ($self, $path) = @_;
4538 return 1 if in_dot_git($path);
4539 return 1 if defined($self->{ignore_regex}) &&
4540 $path =~ m!$self->{ignore_regex}!;
4541 return 0 unless defined($_ignore_regex);
4542 return 1 if $path =~ m!$_ignore_regex!o;
4543 return 0;
4546 sub set_path_strip {
4547 my ($self, $path) = @_;
4548 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
4551 sub open_root {
4552 { path => '' };
4555 sub open_directory {
4556 my ($self, $path, $pb, $rev) = @_;
4557 { path => $path };
4560 sub git_path {
4561 my ($self, $path) = @_;
4562 if (my $enc = $self->{pathnameencoding}) {
4563 require Encode;
4564 Encode::from_to($path, 'UTF-8', $enc);
4566 if ($self->{path_strip}) {
4567 $path =~ s!$self->{path_strip}!! or
4568 die "Failed to strip path '$path' ($self->{path_strip})\n";
4570 $path;
4573 sub delete_entry {
4574 my ($self, $path, $rev, $pb) = @_;
4575 return undef if $self->is_path_ignored($path);
4577 my $gpath = $self->git_path($path);
4578 return undef if ($gpath eq '');
4580 # remove entire directories.
4581 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4582 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
4583 if ($tree) {
4584 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4585 -r --name-only -z/,
4586 $tree);
4587 local $/ = "\0";
4588 while (<$ls>) {
4589 chomp;
4590 my $rmpath = "$gpath/$_";
4591 $self->{gii}->remove($rmpath);
4592 print "\tD\t$rmpath\n" unless $::_q;
4594 print "\tD\t$gpath/\n" unless $::_q;
4595 command_close_pipe($ls, $ctx);
4596 } else {
4597 $self->{gii}->remove($gpath);
4598 print "\tD\t$gpath\n" unless $::_q;
4600 # Don't add to @deleted_gpath if we're deleting a placeholder file.
4601 push @deleted_gpath, $gpath unless $added_placeholder{dirname($path)};
4602 $self->{empty}->{$path} = 0;
4603 undef;
4606 sub open_file {
4607 my ($self, $path, $pb, $rev) = @_;
4608 my ($mode, $blob);
4610 goto out if $self->is_path_ignored($path);
4612 my $gpath = $self->git_path($path);
4613 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4614 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
4615 unless (defined $mode && defined $blob) {
4616 die "$path was not found in commit $self->{c} (r$rev)\n";
4618 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
4619 $mode = '120000';
4621 out:
4622 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
4623 pool => SVN::Pool->new, action => 'M' };
4626 sub add_file {
4627 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
4628 my $mode;
4630 if (!$self->is_path_ignored($path)) {
4631 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4632 delete $self->{empty}->{$dir};
4633 $mode = '100644';
4635 if ($added_placeholder{$dir}) {
4636 # Remove our placeholder file, if we created one.
4637 delete_entry($self, $added_placeholder{$dir})
4638 unless $path eq $added_placeholder{$dir};
4639 delete $added_placeholder{$dir}
4643 { path => $path, mode_a => $mode, mode_b => $mode,
4644 pool => SVN::Pool->new, action => 'A' };
4647 sub add_directory {
4648 my ($self, $path, $cp_path, $cp_rev) = @_;
4649 goto out if $self->is_path_ignored($path);
4650 my $gpath = $self->git_path($path);
4651 if ($gpath eq '') {
4652 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4653 -r --name-only -z/,
4654 $self->{c});
4655 local $/ = "\0";
4656 while (<$ls>) {
4657 chomp;
4658 $self->{gii}->remove($_);
4659 print "\tD\t$_\n" unless $::_q;
4660 push @deleted_gpath, $gpath;
4662 command_close_pipe($ls, $ctx);
4663 $self->{empty}->{$path} = 0;
4665 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4666 delete $self->{empty}->{$dir};
4667 $self->{empty}->{$path} = 1;
4669 if ($added_placeholder{$dir}) {
4670 # Remove our placeholder file, if we created one.
4671 delete_entry($self, $added_placeholder{$dir});
4672 delete $added_placeholder{$dir}
4675 out:
4676 { path => $path };
4679 sub change_dir_prop {
4680 my ($self, $db, $prop, $value) = @_;
4681 return undef if $self->is_path_ignored($db->{path});
4682 $self->{dir_prop}->{$db->{path}} ||= {};
4683 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
4684 undef;
4687 sub absent_directory {
4688 my ($self, $path, $pb) = @_;
4689 return undef if $self->is_path_ignored($path);
4690 $self->{absent_dir}->{$pb->{path}} ||= [];
4691 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
4692 undef;
4695 sub absent_file {
4696 my ($self, $path, $pb) = @_;
4697 return undef if $self->is_path_ignored($path);
4698 $self->{absent_file}->{$pb->{path}} ||= [];
4699 push @{$self->{absent_file}->{$pb->{path}}}, $path;
4700 undef;
4703 sub change_file_prop {
4704 my ($self, $fb, $prop, $value) = @_;
4705 return undef if $self->is_path_ignored($fb->{path});
4706 if ($prop eq 'svn:executable') {
4707 if ($fb->{mode_b} != 120000) {
4708 $fb->{mode_b} = defined $value ? 100755 : 100644;
4710 } elsif ($prop eq 'svn:special') {
4711 $fb->{mode_b} = defined $value ? 120000 : 100644;
4712 } else {
4713 $self->{file_prop}->{$fb->{path}} ||= {};
4714 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
4716 undef;
4719 sub apply_textdelta {
4720 my ($self, $fb, $exp) = @_;
4721 return undef if $self->is_path_ignored($fb->{path});
4722 my $fh = $::_repository->temp_acquire('svn_delta');
4723 # $fh gets auto-closed() by SVN::TxDelta::apply(),
4724 # (but $base does not,) so dup() it for reading in close_file
4725 open my $dup, '<&', $fh or croak $!;
4726 my $base = $::_repository->temp_acquire('git_blob');
4728 if ($fb->{blob}) {
4729 my ($base_is_link, $size);
4731 if ($fb->{mode_a} eq '120000' &&
4732 ! $self->{empty_symlinks}->{$fb->{path}}) {
4733 print $base 'link ' or die "print $!\n";
4734 $base_is_link = 1;
4736 retry:
4737 $size = $::_repository->cat_blob($fb->{blob}, $base);
4738 die "Failed to read object $fb->{blob}" if ($size < 0);
4740 if (defined $exp) {
4741 seek $base, 0, 0 or croak $!;
4742 my $got = ::md5sum($base);
4743 if ($got ne $exp) {
4744 my $err = "Checksum mismatch: ".
4745 "$fb->{path} $fb->{blob}\n" .
4746 "expected: $exp\n" .
4747 " got: $got\n";
4748 if ($base_is_link) {
4749 warn $err,
4750 "Retrying... (possibly ",
4751 "a bad symlink from SVN)\n";
4752 $::_repository->temp_reset($base);
4753 $base_is_link = 0;
4754 goto retry;
4756 die $err;
4760 seek $base, 0, 0 or croak $!;
4761 $fb->{fh} = $fh;
4762 $fb->{base} = $base;
4763 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
4766 sub close_file {
4767 my ($self, $fb, $exp) = @_;
4768 return undef if $self->is_path_ignored($fb->{path});
4770 my $hash;
4771 my $path = $self->git_path($fb->{path});
4772 if (my $fh = $fb->{fh}) {
4773 if (defined $exp) {
4774 seek($fh, 0, 0) or croak $!;
4775 my $got = ::md5sum($fh);
4776 if ($got ne $exp) {
4777 die "Checksum mismatch: $path\n",
4778 "expected: $exp\n got: $got\n";
4781 if ($fb->{mode_b} == 120000) {
4782 sysseek($fh, 0, 0) or croak $!;
4783 my $rd = sysread($fh, my $buf, 5);
4785 if (!defined $rd) {
4786 croak "sysread: $!\n";
4787 } elsif ($rd == 0) {
4788 warn "$path has mode 120000",
4789 " but it points to nothing\n",
4790 "converting to an empty file with mode",
4791 " 100644\n";
4792 $fb->{mode_b} = '100644';
4793 } elsif ($buf ne 'link ') {
4794 warn "$path has mode 120000",
4795 " but is not a link\n";
4796 } else {
4797 my $tmp_fh = $::_repository->temp_acquire(
4798 'svn_hash');
4799 my $res;
4800 while ($res = sysread($fh, my $str, 1024)) {
4801 my $out = syswrite($tmp_fh, $str, $res);
4802 defined($out) && $out == $res
4803 or croak("write ",
4804 Git::temp_path($tmp_fh),
4805 ": $!\n");
4807 defined $res or croak $!;
4809 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4810 Git::temp_release($tmp_fh, 1);
4814 $hash = $::_repository->hash_and_insert_object(
4815 Git::temp_path($fh));
4816 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
4818 Git::temp_release($fb->{base}, 1);
4819 Git::temp_release($fh, 1);
4820 } else {
4821 $hash = $fb->{blob} or die "no blob information\n";
4823 $fb->{pool}->clear;
4824 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
4825 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
4826 undef;
4829 sub abort_edit {
4830 my $self = shift;
4831 $self->{nr} = $self->{gii}->{nr};
4832 delete $self->{gii};
4833 $self->SUPER::abort_edit(@_);
4836 sub close_edit {
4837 my $self = shift;
4839 if ($_preserve_empty_dirs) {
4840 my @empty_dirs;
4842 # Any entry flagged as empty that also has an associated
4843 # dir_prop represents a newly created empty directory.
4844 foreach my $i (keys %{$self->{empty}}) {
4845 push @empty_dirs, $i if exists $self->{dir_prop}->{$i};
4848 # Search for directories that have become empty due subsequent
4849 # file deletes.
4850 push @empty_dirs, $self->find_empty_directories();
4852 # Finally, add a placeholder file to each empty directory.
4853 $self->add_placeholder_file($_) foreach (@empty_dirs);
4855 $self->stash_placeholder_list();
4858 $self->{git_commit_ok} = 1;
4859 $self->{nr} = $self->{gii}->{nr};
4860 delete $self->{gii};
4861 $self->SUPER::close_edit(@_);
4864 sub find_empty_directories {
4865 my ($self) = @_;
4866 my @empty_dirs;
4867 my %dirs = map { dirname($_) => 1 } @deleted_gpath;
4869 foreach my $dir (sort keys %dirs) {
4870 next if $dir eq ".";
4872 # If there have been any additions to this directory, there is
4873 # no reason to check if it is empty.
4874 my $skip_added = 0;
4875 foreach my $t (qw/dir_prop file_prop/) {
4876 foreach my $path (keys %{ $self->{$t} }) {
4877 if (exists $self->{$t}->{dirname($path)}) {
4878 $skip_added = 1;
4879 last;
4882 last if $skip_added;
4884 next if $skip_added;
4886 # Use `git ls-tree` to get the filenames of this directory
4887 # that existed prior to this particular commit.
4888 my $ls = command('ls-tree', '-z', '--name-only',
4889 $self->{c}, "$dir/");
4890 my %files = map { $_ => 1 } split(/\0/, $ls);
4892 # Remove the filenames that were deleted during this commit.
4893 delete $files{$_} foreach (@deleted_gpath);
4895 # Report the directory if there are no filenames left.
4896 push @empty_dirs, $dir unless (scalar %files);
4898 @empty_dirs;
4901 sub add_placeholder_file {
4902 my ($self, $dir) = @_;
4903 my $path = "$dir/$_placeholder_filename";
4904 my $gpath = $self->git_path($path);
4906 my $fh = $::_repository->temp_acquire($gpath);
4907 my $hash = $::_repository->hash_and_insert_object(Git::temp_path($fh));
4908 Git::temp_release($fh, 1);
4909 $self->{gii}->update('100644', $hash, $gpath) or croak $!;
4911 # The directory should no longer be considered empty.
4912 delete $self->{empty}->{$dir} if exists $self->{empty}->{$dir};
4914 # Keep track of any placeholder files we create.
4915 $added_placeholder{$dir} = $path;
4918 sub stash_placeholder_list {
4919 my ($self) = @_;
4920 my $k = "svn-remote.$repo_id.added-placeholder";
4921 my $v = eval { command_oneline('config', '--get-all', $k) };
4922 command_noisy('config', '--unset-all', $k) if $v;
4923 foreach (values %added_placeholder) {
4924 command_noisy('config', '--add', $k, $_);
4928 package SVN::Git::Editor;
4929 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
4930 use strict;
4931 use warnings;
4932 use Carp qw/croak/;
4933 use IO::File;
4935 sub new {
4936 my ($class, $opts) = @_;
4937 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
4938 die "$_ required!\n" unless (defined $opts->{$_});
4941 my $pool = SVN::Pool->new;
4942 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
4943 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
4944 $opts->{r}, $mods);
4946 # $opts->{ra} functions should not be used after this:
4947 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
4948 $opts->{editor_cb}, $pool);
4949 my $self = SVN::Delta::Editor->new(@ce, $pool);
4950 bless $self, $class;
4951 foreach (qw/svn_path r tree_a tree_b/) {
4952 $self->{$_} = $opts->{$_};
4954 $self->{url} = $opts->{ra}->{url};
4955 $self->{mods} = $mods;
4956 $self->{types} = $types;
4957 $self->{pool} = $pool;
4958 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
4959 $self->{rm} = { };
4960 $self->{path_prefix} = length $self->{svn_path} ?
4961 "$self->{svn_path}/" : '';
4962 $self->{config} = $opts->{config};
4963 $self->{mergeinfo} = $opts->{mergeinfo};
4964 return $self;
4967 sub generate_diff {
4968 my ($tree_a, $tree_b) = @_;
4969 my @diff_tree = qw(diff-tree -z -r);
4970 if ($_cp_similarity) {
4971 push @diff_tree, "-C$_cp_similarity";
4972 } else {
4973 push @diff_tree, '-C';
4975 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4976 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
4977 push @diff_tree, $tree_a, $tree_b;
4978 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
4979 local $/ = "\0";
4980 my $state = 'meta';
4981 my @mods;
4982 while (<$diff_fh>) {
4983 chomp $_; # this gets rid of the trailing "\0"
4984 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
4985 ($::sha1)\s($::sha1)\s
4986 ([MTCRAD])\d*$/xo) {
4987 push @mods, { mode_a => $1, mode_b => $2,
4988 sha1_a => $3, sha1_b => $4,
4989 chg => $5 };
4990 if ($5 =~ /^(?:C|R)$/) {
4991 $state = 'file_a';
4992 } else {
4993 $state = 'file_b';
4995 } elsif ($state eq 'file_a') {
4996 my $x = $mods[$#mods] or croak "Empty array\n";
4997 if ($x->{chg} !~ /^(?:C|R)$/) {
4998 croak "Error parsing $_, $x->{chg}\n";
5000 $x->{file_a} = $_;
5001 $state = 'file_b';
5002 } elsif ($state eq 'file_b') {
5003 my $x = $mods[$#mods] or croak "Empty array\n";
5004 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
5005 croak "Error parsing $_, $x->{chg}\n";
5007 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
5008 croak "Error parsing $_, $x->{chg}\n";
5010 $x->{file_b} = $_;
5011 $state = 'meta';
5012 } else {
5013 croak "Error parsing $_\n";
5016 command_close_pipe($diff_fh, $ctx);
5017 \@mods;
5020 sub check_diff_paths {
5021 my ($ra, $pfx, $rev, $mods) = @_;
5022 my %types;
5023 $pfx .= '/' if length $pfx;
5025 sub type_diff_paths {
5026 my ($ra, $types, $path, $rev) = @_;
5027 my @p = split m#/+#, $path;
5028 my $c = shift @p;
5029 unless (defined $types->{$c}) {
5030 $types->{$c} = $ra->check_path($c, $rev);
5032 while (@p) {
5033 $c .= '/' . shift @p;
5034 next if defined $types->{$c};
5035 $types->{$c} = $ra->check_path($c, $rev);
5039 foreach my $m (@$mods) {
5040 foreach my $f (qw/file_a file_b/) {
5041 next unless defined $m->{$f};
5042 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
5043 if (length $pfx.$dir && ! defined $types{$dir}) {
5044 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
5048 \%types;
5051 sub split_path {
5052 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
5055 sub repo_path {
5056 my ($self, $path) = @_;
5057 if (my $enc = $self->{pathnameencoding}) {
5058 require Encode;
5059 Encode::from_to($path, $enc, 'UTF-8');
5061 $self->{path_prefix}.(defined $path ? $path : '');
5064 sub url_path {
5065 my ($self, $path) = @_;
5066 if ($self->{url} =~ m#^https?://#) {
5067 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
5069 $self->{url} . '/' . $self->repo_path($path);
5072 sub rmdirs {
5073 my ($self) = @_;
5074 my $rm = $self->{rm};
5075 delete $rm->{''}; # we never delete the url we're tracking
5076 return unless %$rm;
5078 foreach (keys %$rm) {
5079 my @d = split m#/#, $_;
5080 my $c = shift @d;
5081 $rm->{$c} = 1;
5082 while (@d) {
5083 $c .= '/' . shift @d;
5084 $rm->{$c} = 1;
5087 delete $rm->{$self->{svn_path}};
5088 delete $rm->{''}; # we never delete the url we're tracking
5089 return unless %$rm;
5091 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
5092 $self->{tree_b});
5093 local $/ = "\0";
5094 while (<$fh>) {
5095 chomp;
5096 my @dn = split m#/#, $_;
5097 while (pop @dn) {
5098 delete $rm->{join '/', @dn};
5100 unless (%$rm) {
5101 close $fh;
5102 return;
5105 command_close_pipe($fh, $ctx);
5107 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
5108 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
5109 $self->close_directory($bat->{$d}, $p);
5110 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
5111 print "\tD+\t$d/\n" unless $::_q;
5112 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
5113 delete $bat->{$d};
5117 sub open_or_add_dir {
5118 my ($self, $full_path, $baton) = @_;
5119 my $t = $self->{types}->{$full_path};
5120 if (!defined $t) {
5121 die "$full_path not known in r$self->{r} or we have a bug!\n";
5124 no warnings 'once';
5125 # SVN::Node::none and SVN::Node::file are used only once,
5126 # so we're shutting up Perl's warnings about them.
5127 if ($t == $SVN::Node::none) {
5128 return $self->add_directory($full_path, $baton,
5129 undef, -1, $self->{pool});
5130 } elsif ($t == $SVN::Node::dir) {
5131 return $self->open_directory($full_path, $baton,
5132 $self->{r}, $self->{pool});
5133 } # no warnings 'once'
5134 print STDERR "$full_path already exists in repository at ",
5135 "r$self->{r} and it is not a directory (",
5136 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
5137 } # no warnings 'once'
5138 exit 1;
5141 sub ensure_path {
5142 my ($self, $path) = @_;
5143 my $bat = $self->{bat};
5144 my $repo_path = $self->repo_path($path);
5145 return $bat->{''} unless (length $repo_path);
5146 my @p = split m#/+#, $repo_path;
5147 my $c = shift @p;
5148 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
5149 while (@p) {
5150 my $c0 = $c;
5151 $c .= '/' . shift @p;
5152 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
5154 return $bat->{$c};
5157 # Subroutine to convert a globbing pattern to a regular expression.
5158 # From perl cookbook.
5159 sub glob2pat {
5160 my $globstr = shift;
5161 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
5162 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
5163 return '^' . $globstr . '$';
5166 sub check_autoprop {
5167 my ($self, $pattern, $properties, $file, $fbat) = @_;
5168 # Convert the globbing pattern to a regular expression.
5169 my $regex = glob2pat($pattern);
5170 # Check if the pattern matches the file name.
5171 if($file =~ m/($regex)/) {
5172 # Parse the list of properties to set.
5173 my @props = split(/;/, $properties);
5174 foreach my $prop (@props) {
5175 # Parse 'name=value' syntax and set the property.
5176 if ($prop =~ /([^=]+)=(.*)/) {
5177 my ($n,$v) = ($1,$2);
5178 for ($n, $v) {
5179 s/^\s+//; s/\s+$//;
5181 $self->change_file_prop($fbat, $n, $v);
5187 sub apply_autoprops {
5188 my ($self, $file, $fbat) = @_;
5189 my $conf_t = ${$self->{config}}{'config'};
5190 no warnings 'once';
5191 # Check [miscellany]/enable-auto-props in svn configuration.
5192 if (SVN::_Core::svn_config_get_bool(
5193 $conf_t,
5194 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
5195 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
5196 0)) {
5197 # Auto-props are enabled. Enumerate them to look for matches.
5198 my $callback = sub {
5199 $self->check_autoprop($_[0], $_[1], $file, $fbat);
5201 SVN::_Core::svn_config_enumerate(
5202 $conf_t,
5203 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
5204 $callback);
5208 sub A {
5209 my ($self, $m) = @_;
5210 my ($dir, $file) = split_path($m->{file_b});
5211 my $pbat = $self->ensure_path($dir);
5212 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
5213 undef, -1);
5214 print "\tA\t$m->{file_b}\n" unless $::_q;
5215 $self->apply_autoprops($file, $fbat);
5216 $self->chg_file($fbat, $m);
5217 $self->close_file($fbat,undef,$self->{pool});
5220 sub C {
5221 my ($self, $m) = @_;
5222 my ($dir, $file) = split_path($m->{file_b});
5223 my $pbat = $self->ensure_path($dir);
5224 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
5225 $self->url_path($m->{file_a}), $self->{r});
5226 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
5227 $self->chg_file($fbat, $m);
5228 $self->close_file($fbat,undef,$self->{pool});
5231 sub delete_entry {
5232 my ($self, $path, $pbat) = @_;
5233 my $rpath = $self->repo_path($path);
5234 my ($dir, $file) = split_path($rpath);
5235 $self->{rm}->{$dir} = 1;
5236 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
5239 sub R {
5240 my ($self, $m) = @_;
5241 my ($dir, $file) = split_path($m->{file_b});
5242 my $pbat = $self->ensure_path($dir);
5243 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
5244 $self->url_path($m->{file_a}), $self->{r});
5245 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
5246 $self->apply_autoprops($file, $fbat);
5247 $self->chg_file($fbat, $m);
5248 $self->close_file($fbat,undef,$self->{pool});
5250 ($dir, $file) = split_path($m->{file_a});
5251 $pbat = $self->ensure_path($dir);
5252 $self->delete_entry($m->{file_a}, $pbat);
5255 sub M {
5256 my ($self, $m) = @_;
5257 my ($dir, $file) = split_path($m->{file_b});
5258 my $pbat = $self->ensure_path($dir);
5259 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
5260 $pbat,$self->{r},$self->{pool});
5261 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
5262 $self->chg_file($fbat, $m);
5263 $self->close_file($fbat,undef,$self->{pool});
5266 sub T { shift->M(@_) }
5268 sub change_file_prop {
5269 my ($self, $fbat, $pname, $pval) = @_;
5270 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
5273 sub change_dir_prop {
5274 my ($self, $pbat, $pname, $pval) = @_;
5275 $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool});
5278 sub _chg_file_get_blob ($$$$) {
5279 my ($self, $fbat, $m, $which) = @_;
5280 my $fh = $::_repository->temp_acquire("git_blob_$which");
5281 if ($m->{"mode_$which"} =~ /^120/) {
5282 print $fh 'link ' or croak $!;
5283 $self->change_file_prop($fbat,'svn:special','*');
5284 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
5285 $self->change_file_prop($fbat,'svn:special',undef);
5287 my $blob = $m->{"sha1_$which"};
5288 return ($fh,) if ($blob =~ /^0{40}$/);
5289 my $size = $::_repository->cat_blob($blob, $fh);
5290 croak "Failed to read object $blob" if ($size < 0);
5291 $fh->flush == 0 or croak $!;
5292 seek $fh, 0, 0 or croak $!;
5294 my $exp = ::md5sum($fh);
5295 seek $fh, 0, 0 or croak $!;
5296 return ($fh, $exp);
5299 sub chg_file {
5300 my ($self, $fbat, $m) = @_;
5301 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
5302 $self->change_file_prop($fbat,'svn:executable','*');
5303 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
5304 $self->change_file_prop($fbat,'svn:executable',undef);
5306 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
5307 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
5308 my $pool = SVN::Pool->new;
5309 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
5310 if (-s $fh_a) {
5311 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
5312 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
5313 if (defined $res) {
5314 die "Unexpected result from send_txstream: $res\n",
5315 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
5317 } else {
5318 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
5319 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
5320 if ($got ne $exp_b);
5322 Git::temp_release($fh_b, 1);
5323 Git::temp_release($fh_a, 1);
5324 $pool->clear;
5327 sub D {
5328 my ($self, $m) = @_;
5329 my ($dir, $file) = split_path($m->{file_b});
5330 my $pbat = $self->ensure_path($dir);
5331 print "\tD\t$m->{file_b}\n" unless $::_q;
5332 $self->delete_entry($m->{file_b}, $pbat);
5335 sub close_edit {
5336 my ($self) = @_;
5337 my ($p,$bat) = ($self->{pool}, $self->{bat});
5338 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
5339 next if $_ eq '';
5340 $self->close_directory($bat->{$_}, $p);
5342 $self->close_directory($bat->{''}, $p);
5343 $self->SUPER::close_edit($p);
5344 $p->clear;
5347 sub abort_edit {
5348 my ($self) = @_;
5349 $self->SUPER::abort_edit($self->{pool});
5352 sub DESTROY {
5353 my $self = shift;
5354 $self->SUPER::DESTROY(@_);
5355 $self->{pool}->clear;
5358 # this drives the editor
5359 sub apply_diff {
5360 my ($self) = @_;
5361 my $mods = $self->{mods};
5362 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
5363 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
5364 my $f = $m->{chg};
5365 if (defined $o{$f}) {
5366 $self->$f($m);
5367 } else {
5368 fatal("Invalid change type: $f");
5372 if (defined($self->{mergeinfo})) {
5373 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
5374 $self->{mergeinfo});
5376 $self->rmdirs if $_rmdir;
5377 if (@$mods == 0) {
5378 $self->abort_edit;
5379 } else {
5380 $self->close_edit;
5382 return scalar @$mods;
5385 package Git::SVN::Ra;
5386 use vars qw/@ISA $config_dir $_log_window_size/;
5387 use strict;
5388 use warnings;
5389 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
5391 BEGIN {
5392 # enforce temporary pool usage for some simple functions
5393 no strict 'refs';
5394 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
5395 get_file/) {
5396 my $SUPER = "SUPER::$f";
5397 *$f = sub {
5398 my $self = shift;
5399 my $pool = SVN::Pool->new;
5400 my @ret = $self->$SUPER(@_,$pool);
5401 $pool->clear;
5402 wantarray ? @ret : $ret[0];
5407 sub _auth_providers () {
5409 SVN::Client::get_simple_provider(),
5410 SVN::Client::get_ssl_server_trust_file_provider(),
5411 SVN::Client::get_simple_prompt_provider(
5412 \&Git::SVN::Prompt::simple, 2),
5413 SVN::Client::get_ssl_client_cert_file_provider(),
5414 SVN::Client::get_ssl_client_cert_prompt_provider(
5415 \&Git::SVN::Prompt::ssl_client_cert, 2),
5416 SVN::Client::get_ssl_client_cert_pw_file_provider(),
5417 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
5418 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
5419 SVN::Client::get_username_provider(),
5420 SVN::Client::get_ssl_server_trust_prompt_provider(
5421 \&Git::SVN::Prompt::ssl_server_trust),
5422 SVN::Client::get_username_prompt_provider(
5423 \&Git::SVN::Prompt::username, 2)
5427 sub escape_uri_only {
5428 my ($uri) = @_;
5429 my @tmp;
5430 foreach (split m{/}, $uri) {
5431 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
5432 push @tmp, $_;
5434 join('/', @tmp);
5437 sub escape_url {
5438 my ($url) = @_;
5439 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
5440 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
5441 $url = "$scheme://$domain$uri";
5443 $url;
5446 sub new {
5447 my ($class, $url) = @_;
5448 $url =~ s!/+$!!;
5449 return $RA if ($RA && $RA->{url} eq $url);
5451 ::_req_svn();
5453 SVN::_Core::svn_config_ensure($config_dir, undef);
5454 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
5455 my $config = SVN::Core::config_get_config($config_dir);
5456 $RA = undef;
5457 my $dont_store_passwords = 1;
5458 my $conf_t = ${$config}{'config'};
5460 no warnings 'once';
5461 # The usage of $SVN::_Core::SVN_CONFIG_* variables
5462 # produces warnings that variables are used only once.
5463 # I had not found the better way to shut them up, so
5464 # the warnings of type 'once' are disabled in this block.
5465 if (SVN::_Core::svn_config_get_bool($conf_t,
5466 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
5467 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
5468 1) == 0) {
5469 SVN::_Core::svn_auth_set_parameter($baton,
5470 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
5471 bless (\$dont_store_passwords, "_p_void"));
5473 if (SVN::_Core::svn_config_get_bool($conf_t,
5474 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
5475 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
5476 1) == 0) {
5477 $Git::SVN::Prompt::_no_auth_cache = 1;
5479 } # no warnings 'once'
5480 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
5481 config => $config,
5482 pool => SVN::Pool->new,
5483 auth_provider_callbacks => $callbacks);
5484 $self->{url} = $url;
5485 $self->{svn_path} = $url;
5486 $self->{repos_root} = $self->get_repos_root;
5487 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
5488 $self->{cache} = { check_path => { r => 0, data => {} },
5489 get_dir => { r => 0, data => {} } };
5490 $RA = bless $self, $class;
5493 sub check_path {
5494 my ($self, $path, $r) = @_;
5495 my $cache = $self->{cache}->{check_path};
5496 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
5497 return $cache->{data}->{$path};
5499 my $pool = SVN::Pool->new;
5500 my $t = $self->SUPER::check_path($path, $r, $pool);
5501 $pool->clear;
5502 if ($r != $cache->{r}) {
5503 %{$cache->{data}} = ();
5504 $cache->{r} = $r;
5506 $cache->{data}->{$path} = $t;
5509 sub get_dir {
5510 my ($self, $dir, $r) = @_;
5511 my $cache = $self->{cache}->{get_dir};
5512 if ($r == $cache->{r}) {
5513 if (my $x = $cache->{data}->{$dir}) {
5514 return wantarray ? @$x : $x->[0];
5517 my $pool = SVN::Pool->new;
5518 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
5519 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
5520 $pool->clear;
5521 if ($r != $cache->{r}) {
5522 %{$cache->{data}} = ();
5523 $cache->{r} = $r;
5525 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
5526 wantarray ? (\%dirents, $r, $props) : \%dirents;
5529 sub DESTROY {
5530 # do not call the real DESTROY since we store ourselves in $RA
5533 # get_log(paths, start, end, limit,
5534 # discover_changed_paths, strict_node_history, receiver)
5535 sub get_log {
5536 my ($self, @args) = @_;
5537 my $pool = SVN::Pool->new;
5539 # svn_log_changed_path_t objects passed to get_log are likely to be
5540 # overwritten even if only the refs are copied to an external variable,
5541 # so we should dup the structures in their entirety. Using an
5542 # externally passed pool (instead of our temporary and quickly cleared
5543 # pool in Git::SVN::Ra) does not help matters at all...
5544 my $receiver = pop @args;
5545 my $prefix = "/".$self->{svn_path};
5546 $prefix =~ s#/+($)##;
5547 my $prefix_regex = qr#^\Q$prefix\E#;
5548 push(@args, sub {
5549 my ($paths) = $_[0];
5550 return &$receiver(@_) unless $paths;
5551 $_[0] = ();
5552 foreach my $p (keys %$paths) {
5553 my $i = $paths->{$p};
5554 # Make path relative to our url, not repos_root
5555 $p =~ s/$prefix_regex//;
5556 my %s = map { $_ => $i->$_; }
5557 qw/copyfrom_path copyfrom_rev action/;
5558 if ($s{'copyfrom_path'}) {
5559 $s{'copyfrom_path'} =~ s/$prefix_regex//;
5561 $_[0]{$p} = \%s;
5563 &$receiver(@_);
5567 # the limit parameter was not supported in SVN 1.1.x, so we
5568 # drop it. Therefore, the receiver callback passed to it
5569 # is made aware of this limitation by being wrapped if
5570 # the limit passed to is being wrapped.
5571 if ($SVN::Core::VERSION le '1.2.0') {
5572 my $limit = splice(@args, 3, 1);
5573 if ($limit > 0) {
5574 my $receiver = pop @args;
5575 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
5578 my $ret = $self->SUPER::get_log(@args, $pool);
5579 $pool->clear;
5580 $ret;
5583 sub trees_match {
5584 my ($self, $url1, $rev1, $url2, $rev2) = @_;
5585 my $ctx = SVN::Client->new(auth => _auth_providers);
5586 my $out = IO::File->new_tmpfile;
5588 # older SVN (1.1.x) doesn't take $pool as the last parameter for
5589 # $ctx->diff(), so we'll create a default one
5590 my $pool = SVN::Pool->new_default_sub;
5592 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
5593 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
5594 $out->flush;
5595 my $ret = (($out->stat)[7] == 0);
5596 close $out or croak $!;
5598 $ret;
5601 sub get_commit_editor {
5602 my ($self, $log, $cb, $pool) = @_;
5603 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
5604 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
5607 sub gs_do_update {
5608 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
5609 my $new = ($rev_a == $rev_b);
5610 my $path = $gs->{path};
5612 if ($new && -e $gs->{index}) {
5613 unlink $gs->{index} or die
5614 "Couldn't unlink index: $gs->{index}: $!\n";
5616 my $pool = SVN::Pool->new;
5617 $editor->set_path_strip($path);
5618 my (@pc) = split m#/#, $path;
5619 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
5620 1, $editor, $pool);
5621 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
5623 # Since we can't rely on svn_ra_reparent being available, we'll
5624 # just have to do some magic with set_path to make it so
5625 # we only want a partial path.
5626 my $sp = '';
5627 my $final = join('/', @pc);
5628 while (@pc) {
5629 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
5630 $sp .= '/' if length $sp;
5631 $sp .= shift @pc;
5633 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
5635 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
5637 $reporter->finish_report($pool);
5638 $pool->clear;
5639 $editor->{git_commit_ok};
5642 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
5643 # svn_ra_reparent didn't work before 1.4)
5644 sub gs_do_switch {
5645 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
5646 my $path = $gs->{path};
5647 my $pool = SVN::Pool->new;
5649 my $full_url = $self->{url};
5650 my $old_url = $full_url;
5651 $full_url .= '/' . $path if length $path;
5652 my ($ra, $reparented);
5654 if ($old_url =~ m#^svn(\+ssh)?://# ||
5655 ($full_url =~ m#^https?://# &&
5656 escape_url($full_url) ne $full_url)) {
5657 $_[0] = undef;
5658 $self = undef;
5659 $RA = undef;
5660 $ra = Git::SVN::Ra->new($full_url);
5661 $ra_invalid = 1;
5662 } elsif ($old_url ne $full_url) {
5663 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
5664 $self->{url} = $full_url;
5665 $reparented = 1;
5668 $ra ||= $self;
5669 $url_b = escape_url($url_b);
5670 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
5671 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
5672 $reporter->set_path('', $rev_a, 0, @lock, $pool);
5673 $reporter->finish_report($pool);
5675 if ($reparented) {
5676 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
5677 $self->{url} = $old_url;
5680 $pool->clear;
5681 $editor->{git_commit_ok};
5684 sub longest_common_path {
5685 my ($gsv, $globs) = @_;
5686 my %common;
5687 my $common_max = scalar @$gsv;
5689 foreach my $gs (@$gsv) {
5690 my @tmp = split m#/#, $gs->{path};
5691 my $p = '';
5692 foreach (@tmp) {
5693 $p .= length($p) ? "/$_" : $_;
5694 $common{$p} ||= 0;
5695 $common{$p}++;
5698 $globs ||= [];
5699 $common_max += scalar @$globs;
5700 foreach my $glob (@$globs) {
5701 my @tmp = split m#/#, $glob->{path}->{left};
5702 my $p = '';
5703 foreach (@tmp) {
5704 $p .= length($p) ? "/$_" : $_;
5705 $common{$p} ||= 0;
5706 $common{$p}++;
5710 my $longest_path = '';
5711 foreach (sort {length $b <=> length $a} keys %common) {
5712 if ($common{$_} == $common_max) {
5713 $longest_path = $_;
5714 last;
5717 $longest_path;
5720 sub gs_fetch_loop_common {
5721 my ($self, $base, $head, $gsv, $globs) = @_;
5722 return if ($base > $head);
5723 my $inc = $_log_window_size;
5724 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
5725 my $longest_path = longest_common_path($gsv, $globs);
5726 my $ra_url = $self->{url};
5727 my $find_trailing_edge;
5728 while (1) {
5729 my %revs;
5730 my $err;
5731 my $err_handler = $SVN::Error::handler;
5732 $SVN::Error::handler = sub {
5733 ($err) = @_;
5734 skip_unknown_revs($err);
5736 sub _cb {
5737 my ($paths, $r, $author, $date, $log) = @_;
5738 [ $paths,
5739 { author => $author, date => $date, log => $log } ];
5741 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
5742 sub { $revs{$_[1]} = _cb(@_) });
5743 if ($err) {
5744 print "Checked through r$max\r";
5745 } else {
5746 $find_trailing_edge = 1;
5748 if ($err and $find_trailing_edge) {
5749 print STDERR "Path '$longest_path' ",
5750 "was probably deleted:\n",
5751 $err->expanded_message,
5752 "\nWill attempt to follow ",
5753 "revisions r$min .. r$max ",
5754 "committed before the deletion\n";
5755 my $hi = $max;
5756 while (--$hi >= $min) {
5757 my $ok;
5758 $self->get_log([$longest_path], $min, $hi,
5759 0, 1, 1, sub {
5760 $ok = $_[1];
5761 $revs{$_[1]} = _cb(@_) });
5762 if ($ok) {
5763 print STDERR "r$min .. r$ok OK\n";
5764 last;
5767 $find_trailing_edge = 0;
5769 $SVN::Error::handler = $err_handler;
5771 my %exists = map { $_->{path} => $_ } @$gsv;
5772 foreach my $r (sort {$a <=> $b} keys %revs) {
5773 my ($paths, $logged) = @{$revs{$r}};
5775 foreach my $gs ($self->match_globs(\%exists, $paths,
5776 $globs, $r)) {
5777 if ($gs->rev_map_max >= $r) {
5778 next;
5780 next unless $gs->match_paths($paths, $r);
5781 $gs->{logged_rev_props} = $logged;
5782 if (my $last_commit = $gs->last_commit) {
5783 $gs->assert_index_clean($last_commit);
5785 my $log_entry = $gs->do_fetch($paths, $r);
5786 if ($log_entry) {
5787 $gs->do_git_commit($log_entry);
5789 $INDEX_FILES{$gs->{index}} = 1;
5791 foreach my $g (@$globs) {
5792 my $k = "svn-remote.$g->{remote}." .
5793 "$g->{t}-maxRev";
5794 Git::SVN::tmp_config($k, $r);
5796 if ($ra_invalid) {
5797 $_[0] = undef;
5798 $self = undef;
5799 $RA = undef;
5800 $self = Git::SVN::Ra->new($ra_url);
5801 $ra_invalid = undef;
5804 # pre-fill the .rev_db since it'll eventually get filled in
5805 # with '0' x40 if something new gets committed
5806 foreach my $gs (@$gsv) {
5807 next if $gs->rev_map_max >= $max;
5808 next if defined $gs->rev_map_get($max);
5809 $gs->rev_map_set($max, 0 x40);
5811 foreach my $g (@$globs) {
5812 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
5813 Git::SVN::tmp_config($k, $max);
5815 last if $max >= $head;
5816 $min = $max + 1;
5817 $max += $inc;
5818 $max = $head if ($max > $head);
5820 Git::SVN::gc();
5823 sub get_dir_globbed {
5824 my ($self, $left, $depth, $r) = @_;
5826 my @x = eval { $self->get_dir($left, $r) };
5827 return unless scalar @x == 3;
5828 my $dirents = $x[0];
5829 my @finalents;
5830 foreach my $de (keys %$dirents) {
5831 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
5832 if ($depth > 1) {
5833 my @args = ("$left/$de", $depth - 1, $r);
5834 foreach my $dir ($self->get_dir_globbed(@args)) {
5835 push @finalents, "$de/$dir";
5837 } else {
5838 push @finalents, $de;
5841 @finalents;
5844 sub match_globs {
5845 my ($self, $exists, $paths, $globs, $r) = @_;
5847 sub get_dir_check {
5848 my ($self, $exists, $g, $r) = @_;
5850 my @dirs = $self->get_dir_globbed($g->{path}->{left},
5851 $g->{path}->{depth},
5852 $r);
5854 foreach my $de (@dirs) {
5855 my $p = $g->{path}->full_path($de);
5856 next if $exists->{$p};
5857 next if (length $g->{path}->{right} &&
5858 ($self->check_path($p, $r) !=
5859 $SVN::Node::dir));
5860 next unless $p =~ /$g->{path}->{regex}/;
5861 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
5862 $g->{ref}->full_path($de), 1);
5865 foreach my $g (@$globs) {
5866 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5867 if ($path->{action} =~ /^[AR]$/) {
5868 get_dir_check($self, $exists, $g, $r);
5871 foreach (keys %$paths) {
5872 if (/$g->{path}->{left_regex}/ &&
5873 !/$g->{path}->{regex}/) {
5874 next if $paths->{$_}->{action} !~ /^[AR]$/;
5875 get_dir_check($self, $exists, $g, $r);
5877 next unless /$g->{path}->{regex}/;
5878 my $p = $1;
5879 my $pathname = $g->{path}->full_path($p);
5880 next if $exists->{$pathname};
5881 next if ($self->check_path($pathname, $r) !=
5882 $SVN::Node::dir);
5883 $exists->{$pathname} = Git::SVN->init(
5884 $self->{url}, $pathname, undef,
5885 $g->{ref}->full_path($p), 1);
5887 my $c = '';
5888 foreach (split m#/#, $g->{path}->{left}) {
5889 $c .= "/$_";
5890 next unless ($paths->{$c} &&
5891 ($paths->{$c}->{action} =~ /^[AR]$/));
5892 get_dir_check($self, $exists, $g, $r);
5895 values %$exists;
5898 sub minimize_url {
5899 my ($self) = @_;
5900 return $self->{url} if ($self->{url} eq $self->{repos_root});
5901 my $url = $self->{repos_root};
5902 my @components = split(m!/!, $self->{svn_path});
5903 my $c = '';
5904 do {
5905 $url .= "/$c" if length $c;
5906 eval {
5907 my $ra = (ref $self)->new($url);
5908 my $latest = $ra->get_latest_revnum;
5909 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5911 } while ($@ && ($c = shift @components));
5912 $url;
5915 sub can_do_switch {
5916 my $self = shift;
5917 unless (defined $can_do_switch) {
5918 my $pool = SVN::Pool->new;
5919 my $rep = eval {
5920 $self->do_switch(1, '', 0, $self->{url},
5921 SVN::Delta::Editor->new, $pool);
5923 if ($@) {
5924 $can_do_switch = 0;
5925 } else {
5926 $rep->abort_report($pool);
5927 $can_do_switch = 1;
5929 $pool->clear;
5931 $can_do_switch;
5934 sub skip_unknown_revs {
5935 my ($err) = @_;
5936 my $errno = $err->apr_err();
5937 # Maybe the branch we're tracking didn't
5938 # exist when the repo started, so it's
5939 # not an error if it doesn't, just continue
5941 # Wonderfully consistent library, eh?
5942 # 160013 - svn:// and file://
5943 # 175002 - http(s)://
5944 # 175007 - http(s):// (this repo required authorization, too...)
5945 # More codes may be discovered later...
5946 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
5947 my $err_key = $err->expanded_message;
5948 # revision numbers change every time, filter them out
5949 $err_key =~ s/\d+/\0/g;
5950 $err_key = "$errno\0$err_key";
5951 unless ($ignored_err{$err_key}) {
5952 warn "W: Ignoring error from SVN, path probably ",
5953 "does not exist: ($errno): ",
5954 $err->expanded_message,"\n";
5955 warn "W: Do not be alarmed at the above message ",
5956 "git-svn is just searching aggressively for ",
5957 "old history.\n",
5958 "This may take a while on large repositories\n";
5959 $ignored_err{$err_key} = 1;
5961 return;
5963 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5966 package Git::SVN::Log;
5967 use strict;
5968 use warnings;
5969 use POSIX qw/strftime/;
5970 use Time::Local;
5971 use constant commit_log_separator => ('-' x 72) . "\n";
5972 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
5973 %rusers $show_commit $incremental/;
5974 my $l_fmt;
5976 sub cmt_showable {
5977 my ($c) = @_;
5978 return 1 if defined $c->{r};
5980 # big commit message got truncated by the 16k pretty buffer in rev-list
5981 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
5982 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
5983 @{$c->{l}} = ();
5984 my @log = command(qw/cat-file commit/, $c->{c});
5986 # shift off the headers
5987 shift @log while ($log[0] ne '');
5988 shift @log;
5990 # TODO: make $c->{l} not have a trailing newline in the future
5991 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
5993 (undef, $c->{r}, undef) = ::extract_metadata(
5994 (grep(/^git-svn-id: /, @log))[-1]);
5996 return defined $c->{r};
5999 sub log_use_color {
6000 return $color || Git->repository->get_colorbool('color.diff');
6003 sub git_svn_log_cmd {
6004 my ($r_min, $r_max, @args) = @_;
6005 my $head = 'HEAD';
6006 my (@files, @log_opts);
6007 foreach my $x (@args) {
6008 if ($x eq '--' || @files) {
6009 push @files, $x;
6010 } else {
6011 if (::verify_ref("$x^0")) {
6012 $head = $x;
6013 } else {
6014 push @log_opts, $x;
6019 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
6020 $gs ||= Git::SVN->_new;
6021 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
6022 $gs->refname);
6023 push @cmd, '-r' unless $non_recursive;
6024 push @cmd, qw/--raw --name-status/ if $verbose;
6025 push @cmd, '--color' if log_use_color();
6026 push @cmd, @log_opts;
6027 if (defined $r_max && $r_max == $r_min) {
6028 push @cmd, '--max-count=1';
6029 if (my $c = $gs->rev_map_get($r_max)) {
6030 push @cmd, $c;
6032 } elsif (defined $r_max) {
6033 if ($r_max < $r_min) {
6034 ($r_min, $r_max) = ($r_max, $r_min);
6036 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
6037 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
6038 # If there are no commits in the range, both $c_max and $c_min
6039 # will be undefined. If there is at least 1 commit in the
6040 # range, both will be defined.
6041 return () if !defined $c_min || !defined $c_max;
6042 if ($c_min eq $c_max) {
6043 push @cmd, '--max-count=1', $c_min;
6044 } else {
6045 push @cmd, '--boundary', "$c_min..$c_max";
6048 return (@cmd, @files);
6051 # adapted from pager.c
6052 sub config_pager {
6053 if (! -t *STDOUT) {
6054 $ENV{GIT_PAGER_IN_USE} = 'false';
6055 $pager = undef;
6056 return;
6058 chomp($pager = command_oneline(qw(var GIT_PAGER)));
6059 if ($pager eq 'cat') {
6060 $pager = undef;
6062 $ENV{GIT_PAGER_IN_USE} = defined($pager);
6065 sub run_pager {
6066 return unless defined $pager;
6067 pipe my ($rfd, $wfd) or return;
6068 defined(my $pid = fork) or ::fatal "Can't fork: $!";
6069 if (!$pid) {
6070 open STDOUT, '>&', $wfd or
6071 ::fatal "Can't redirect to stdout: $!";
6072 return;
6074 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
6075 $ENV{LESS} ||= 'FRSX';
6076 exec $pager or ::fatal "Can't run pager: $! ($pager)";
6079 sub format_svn_date {
6080 # some systmes don't handle or mishandle %z, so be creative.
6081 my $t = shift || time;
6082 my $gm = timelocal(gmtime($t));
6083 my $sign = qw( + + - )[ $t <=> $gm ];
6084 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
6085 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
6088 sub parse_git_date {
6089 my ($t, $tz) = @_;
6090 # Date::Parse isn't in the standard Perl distro :(
6091 if ($tz =~ s/^\+//) {
6092 $t += tz_to_s_offset($tz);
6093 } elsif ($tz =~ s/^\-//) {
6094 $t -= tz_to_s_offset($tz);
6096 return $t;
6099 sub set_local_timezone {
6100 if (defined $TZ) {
6101 $ENV{TZ} = $TZ;
6102 } else {
6103 delete $ENV{TZ};
6107 sub tz_to_s_offset {
6108 my ($tz) = @_;
6109 $tz =~ s/(\d\d)$//;
6110 return ($1 * 60) + ($tz * 3600);
6113 sub get_author_info {
6114 my ($dest, $author, $t, $tz) = @_;
6115 $author =~ s/(?:^\s*|\s*$)//g;
6116 $dest->{a_raw} = $author;
6117 my $au;
6118 if ($::_authors) {
6119 $au = $rusers{$author} || undef;
6121 if (!$au) {
6122 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
6124 $dest->{t} = $t;
6125 $dest->{tz} = $tz;
6126 $dest->{a} = $au;
6127 $dest->{t_utc} = parse_git_date($t, $tz);
6130 sub process_commit {
6131 my ($c, $r_min, $r_max, $defer) = @_;
6132 if (defined $r_min && defined $r_max) {
6133 if ($r_min == $c->{r} && $r_min == $r_max) {
6134 show_commit($c);
6135 return 0;
6137 return 1 if $r_min == $r_max;
6138 if ($r_min < $r_max) {
6139 # we need to reverse the print order
6140 return 0 if (defined $limit && --$limit < 0);
6141 push @$defer, $c;
6142 return 1;
6144 if ($r_min != $r_max) {
6145 return 1 if ($r_min < $c->{r});
6146 return 1 if ($r_max > $c->{r});
6149 return 0 if (defined $limit && --$limit < 0);
6150 show_commit($c);
6151 return 1;
6154 sub show_commit {
6155 my $c = shift;
6156 if ($oneline) {
6157 my $x = "\n";
6158 if (my $l = $c->{l}) {
6159 while ($l->[0] =~ /^\s*$/) { shift @$l }
6160 $x = $l->[0];
6162 $l_fmt ||= 'A' . length($c->{r});
6163 print 'r',pack($l_fmt, $c->{r}),' | ';
6164 print "$c->{c} | " if $show_commit;
6165 print $x;
6166 } else {
6167 show_commit_normal($c);
6171 sub show_commit_changed_paths {
6172 my ($c) = @_;
6173 return unless $c->{changed};
6174 print "Changed paths:\n", @{$c->{changed}};
6177 sub show_commit_normal {
6178 my ($c) = @_;
6179 print commit_log_separator, "r$c->{r} | ";
6180 print "$c->{c} | " if $show_commit;
6181 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
6182 my $nr_line = 0;
6184 if (my $l = $c->{l}) {
6185 while ($l->[$#$l] eq "\n" && $#$l > 0
6186 && $l->[($#$l - 1)] eq "\n") {
6187 pop @$l;
6189 $nr_line = scalar @$l;
6190 if (!$nr_line) {
6191 print "1 line\n\n\n";
6192 } else {
6193 if ($nr_line == 1) {
6194 $nr_line = '1 line';
6195 } else {
6196 $nr_line .= ' lines';
6198 print $nr_line, "\n";
6199 show_commit_changed_paths($c);
6200 print "\n";
6201 print $_ foreach @$l;
6203 } else {
6204 print "1 line\n";
6205 show_commit_changed_paths($c);
6206 print "\n";
6209 foreach my $x (qw/raw stat diff/) {
6210 if ($c->{$x}) {
6211 print "\n";
6212 print $_ foreach @{$c->{$x}}
6217 sub cmd_show_log {
6218 my (@args) = @_;
6219 my ($r_min, $r_max);
6220 my $r_last = -1; # prevent dupes
6221 set_local_timezone();
6222 if (defined $::_revision) {
6223 if ($::_revision =~ /^(\d+):(\d+)$/) {
6224 ($r_min, $r_max) = ($1, $2);
6225 } elsif ($::_revision =~ /^\d+$/) {
6226 $r_min = $r_max = $::_revision;
6227 } else {
6228 ::fatal "-r$::_revision is not supported, use ",
6229 "standard 'git log' arguments instead";
6233 config_pager();
6234 @args = git_svn_log_cmd($r_min, $r_max, @args);
6235 if (!@args) {
6236 print commit_log_separator unless $incremental || $oneline;
6237 return;
6239 my $log = command_output_pipe(@args);
6240 run_pager();
6241 my (@k, $c, $d, $stat);
6242 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
6243 while (<$log>) {
6244 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
6245 my $cmt = $1;
6246 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
6247 $r_last = $c->{r};
6248 process_commit($c, $r_min, $r_max, \@k) or
6249 goto out;
6251 $d = undef;
6252 $c = { c => $cmt };
6253 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
6254 get_author_info($c, $1, $2, $3);
6255 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
6256 # ignore
6257 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
6258 push @{$c->{raw}}, $_;
6259 } elsif (/^${esc_color}[ACRMDT]\t/) {
6260 # we could add $SVN->{svn_path} here, but that requires
6261 # remote access at the moment (repo_path_split)...
6262 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
6263 push @{$c->{changed}}, $_;
6264 } elsif (/^${esc_color}diff /o) {
6265 $d = 1;
6266 push @{$c->{diff}}, $_;
6267 } elsif ($d) {
6268 push @{$c->{diff}}, $_;
6269 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
6270 $esc_color*[\+\-]*$esc_color$/x) {
6271 $stat = 1;
6272 push @{$c->{stat}}, $_;
6273 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
6274 push @{$c->{stat}}, $_;
6275 $stat = undef;
6276 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
6277 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
6278 } elsif (s/^${esc_color} //o) {
6279 push @{$c->{l}}, $_;
6282 if ($c && defined $c->{r} && $c->{r} != $r_last) {
6283 $r_last = $c->{r};
6284 process_commit($c, $r_min, $r_max, \@k);
6286 if (@k) {
6287 ($r_min, $r_max) = ($r_max, $r_min);
6288 process_commit($_, $r_min, $r_max) foreach reverse @k;
6290 out:
6291 close $log;
6292 print commit_log_separator unless $incremental || $oneline;
6295 sub cmd_blame {
6296 my $path = pop;
6298 config_pager();
6299 run_pager();
6301 my ($fh, $ctx, $rev);
6303 if ($_git_format) {
6304 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
6305 while (my $line = <$fh>) {
6306 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
6307 # Uncommitted edits show up as a rev ID of
6308 # all zeros, which we can't look up with
6309 # cmt_metadata
6310 if ($1 !~ /^0+$/) {
6311 (undef, $rev, undef) =
6312 ::cmt_metadata($1);
6313 $rev = '0' if (!$rev);
6314 } else {
6315 $rev = '0';
6317 $rev = sprintf('%-10s', $rev);
6318 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
6320 print $line;
6322 } else {
6323 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
6324 '--', $path);
6325 my ($sha1);
6326 my %authors;
6327 my @buffer;
6328 my %dsha; #distinct sha keys
6330 while (my $line = <$fh>) {
6331 push @buffer, $line;
6332 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
6333 $dsha{$1} = 1;
6337 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
6339 foreach my $line (@buffer) {
6340 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
6341 $rev = $s2r->{$1};
6342 $rev = '0' if (!$rev)
6344 elsif ($line =~ /^author (.*)/) {
6345 $authors{$rev} = $1;
6346 $authors{$rev} =~ s/\s/_/g;
6348 elsif ($line =~ /^\t(.*)$/) {
6349 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
6353 command_close_pipe($fh, $ctx);
6356 package Git::SVN::Migration;
6357 # these version numbers do NOT correspond to actual version numbers
6358 # of git nor git-svn. They are just relative.
6360 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
6362 # v1 layout: .git/$id/info/url, refs/remotes/$id
6364 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
6366 # v3 layout: .git/svn/$id, refs/remotes/$id
6367 # - info/url may remain for backwards compatibility
6368 # - this is what we migrate up to this layout automatically,
6369 # - this will be used by git svn init on single branches
6370 # v3.1 layout (auto migrated):
6371 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
6372 # for backwards compatibility
6374 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
6375 # - this is only created for newly multi-init-ed
6376 # repositories. Similar in spirit to the
6377 # --use-separate-remotes option in git-clone (now default)
6378 # - we do not automatically migrate to this (following
6379 # the example set by core git)
6381 # v5 layout: .rev_db.$UUID => .rev_map.$UUID
6382 # - newer, more-efficient format that uses 24-bytes per record
6383 # with no filler space.
6384 # - use xxd -c24 < .rev_map.$UUID to view and debug
6385 # - This is a one-way migration, repositories updated to the
6386 # new format will not be able to use old git-svn without
6387 # rebuilding the .rev_db. Rebuilding the rev_db is not
6388 # possible if noMetadata or useSvmProps are set; but should
6389 # be no problem for users that use the (sensible) defaults.
6390 use strict;
6391 use warnings;
6392 use Carp qw/croak/;
6393 use File::Path qw/mkpath/;
6394 use File::Basename qw/dirname basename/;
6395 use vars qw/$_minimize/;
6397 sub migrate_from_v0 {
6398 my $git_dir = $ENV{GIT_DIR};
6399 return undef unless -d $git_dir;
6400 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
6401 my $migrated = 0;
6402 while (<$fh>) {
6403 chomp;
6404 my ($id, $orig_ref) = ($_, $_);
6405 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
6406 next unless -f "$git_dir/$id/info/url";
6407 my $new_ref = "refs/remotes/$id";
6408 if (::verify_ref("$new_ref^0")) {
6409 print STDERR "W: $orig_ref is probably an old ",
6410 "branch used by an ancient version of ",
6411 "git-svn.\n",
6412 "However, $new_ref also exists.\n",
6413 "We will not be able ",
6414 "to use this branch until this ",
6415 "ambiguity is resolved.\n";
6416 next;
6418 print STDERR "Migrating from v0 layout...\n" if !$migrated;
6419 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
6420 command_noisy('update-ref', $new_ref, $orig_ref);
6421 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
6422 $migrated++;
6424 command_close_pipe($fh, $ctx);
6425 print STDERR "Done migrating from v0 layout...\n" if $migrated;
6426 $migrated;
6429 sub migrate_from_v1 {
6430 my $git_dir = $ENV{GIT_DIR};
6431 my $migrated = 0;
6432 return $migrated unless -d $git_dir;
6433 my $svn_dir = "$git_dir/svn";
6435 # just in case somebody used 'svn' as their $id at some point...
6436 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
6438 print STDERR "Migrating from a git-svn v1 layout...\n";
6439 mkpath([$svn_dir]);
6440 print STDERR "Data from a previous version of git-svn exists, but\n\t",
6441 "$svn_dir\n\t(required for this version ",
6442 "($::VERSION) of git-svn) does not exist.\n";
6443 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
6444 while (<$fh>) {
6445 my $x = $_;
6446 next unless $x =~ s#^refs/remotes/##;
6447 chomp $x;
6448 next unless -f "$git_dir/$x/info/url";
6449 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
6450 next unless $u;
6451 my $dn = dirname("$git_dir/svn/$x");
6452 mkpath([$dn]) unless -d $dn;
6453 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
6454 mkpath(["$git_dir/svn/svn"]);
6455 print STDERR " - $git_dir/$x/info => ",
6456 "$git_dir/svn/$x/info\n";
6457 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
6458 croak "$!: $x";
6459 # don't worry too much about these, they probably
6460 # don't exist with repos this old (save for index,
6461 # and we can easily regenerate that)
6462 foreach my $f (qw/unhandled.log index .rev_db/) {
6463 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
6465 } else {
6466 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
6467 rename "$git_dir/$x", "$git_dir/svn/$x" or
6468 croak "$!: $x";
6470 $migrated++;
6472 command_close_pipe($fh, $ctx);
6473 print STDERR "Done migrating from a git-svn v1 layout\n";
6474 $migrated;
6477 sub read_old_urls {
6478 my ($l_map, $pfx, $path) = @_;
6479 my @dir;
6480 foreach (<$path/*>) {
6481 if (-r "$_/info/url") {
6482 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
6483 my $ref_id = $pfx . basename $_;
6484 my $url = ::file_to_s("$_/info/url");
6485 $l_map->{$ref_id} = $url;
6486 } elsif (-d $_) {
6487 push @dir, $_;
6490 foreach (@dir) {
6491 my $x = $_;
6492 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
6493 read_old_urls($l_map, $x, $_);
6497 sub migrate_from_v2 {
6498 my @cfg = command(qw/config -l/);
6499 return if grep /^svn-remote\..+\.url=/, @cfg;
6500 my %l_map;
6501 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
6502 my $migrated = 0;
6504 foreach my $ref_id (sort keys %l_map) {
6505 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
6506 if ($@) {
6507 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
6509 $migrated++;
6511 $migrated;
6514 sub minimize_connections {
6515 my $r = Git::SVN::read_all_remotes();
6516 my $new_urls = {};
6517 my $root_repos = {};
6518 foreach my $repo_id (keys %$r) {
6519 my $url = $r->{$repo_id}->{url} or next;
6520 my $fetch = $r->{$repo_id}->{fetch} or next;
6521 my $ra = Git::SVN::Ra->new($url);
6523 # skip existing cases where we already connect to the root
6524 if (($ra->{url} eq $ra->{repos_root}) ||
6525 ($ra->{repos_root} eq $repo_id)) {
6526 $root_repos->{$ra->{url}} = $repo_id;
6527 next;
6530 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
6531 my $root_path = $ra->{url};
6532 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
6533 foreach my $path (keys %$fetch) {
6534 my $ref_id = $fetch->{$path};
6535 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
6537 # make sure we can read when connecting to
6538 # a higher level of a repository
6539 my ($last_rev, undef) = $gs->last_rev_commit;
6540 if (!defined $last_rev) {
6541 $last_rev = eval {
6542 $root_ra->get_latest_revnum;
6544 next if $@;
6546 my $new = $root_path;
6547 $new .= length $path ? "/$path" : '';
6548 eval {
6549 $root_ra->get_log([$new], $last_rev, $last_rev,
6550 0, 0, 1, sub { });
6552 next if $@;
6553 $new_urls->{$ra->{repos_root}}->{$new} =
6554 { ref_id => $ref_id,
6555 old_repo_id => $repo_id,
6556 old_path => $path };
6560 my @emptied;
6561 foreach my $url (keys %$new_urls) {
6562 # see if we can re-use an existing [svn-remote "repo_id"]
6563 # instead of creating a(n ugly) new section:
6564 my $repo_id = $root_repos->{$url} || $url;
6566 my $fetch = $new_urls->{$url};
6567 foreach my $path (keys %$fetch) {
6568 my $x = $fetch->{$path};
6569 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
6570 my $pfx = "svn-remote.$x->{old_repo_id}";
6572 my $old_fetch = quotemeta("$x->{old_path}:".
6573 "$x->{ref_id}");
6574 command_noisy(qw/config --unset/,
6575 "$pfx.fetch", '^'. $old_fetch . '$');
6576 delete $r->{$x->{old_repo_id}}->
6577 {fetch}->{$x->{old_path}};
6578 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
6579 command_noisy(qw/config --unset/,
6580 "$pfx.url");
6581 push @emptied, $x->{old_repo_id}
6585 if (@emptied) {
6586 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
6587 print STDERR <<EOF;
6588 The following [svn-remote] sections in your config file ($file) are empty
6589 and can be safely removed:
6591 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
6595 sub migration_check {
6596 migrate_from_v0();
6597 migrate_from_v1();
6598 migrate_from_v2();
6599 minimize_connections() if $_minimize;
6602 package Git::IndexInfo;
6603 use strict;
6604 use warnings;
6605 use Git qw/command_input_pipe command_close_pipe/;
6607 sub new {
6608 my ($class) = @_;
6609 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
6610 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
6613 sub remove {
6614 my ($self, $path) = @_;
6615 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
6616 return ++$self->{nr};
6618 undef;
6621 sub update {
6622 my ($self, $mode, $hash, $path) = @_;
6623 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
6624 return ++$self->{nr};
6626 undef;
6629 sub DESTROY {
6630 my ($self) = @_;
6631 command_close_pipe($self->{gui}, $self->{ctx});
6634 package Git::SVN::GlobSpec;
6635 use strict;
6636 use warnings;
6638 sub new {
6639 my ($class, $glob, $pattern_ok) = @_;
6640 my $re = $glob;
6641 $re =~ s!/+$!!g; # no need for trailing slashes
6642 my (@left, @right, @patterns);
6643 my $state = "left";
6644 my $die_msg = "Only one set of wildcard directories " .
6645 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
6646 for my $part (split(m|/|, $glob)) {
6647 if ($part =~ /\*/ && $part ne "*") {
6648 die "Invalid pattern in '$glob': $part\n";
6649 } elsif ($pattern_ok && $part =~ /[{}]/ &&
6650 $part !~ /^\{[^{}]+\}/) {
6651 die "Invalid pattern in '$glob': $part\n";
6653 if ($part eq "*") {
6654 die $die_msg if $state eq "right";
6655 $state = "pattern";
6656 push(@patterns, "[^/]*");
6657 } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) {
6658 die $die_msg if $state eq "right";
6659 $state = "pattern";
6660 my $p = quotemeta($1);
6661 $p =~ s/\\,/|/g;
6662 push(@patterns, "(?:$p)");
6663 } else {
6664 if ($state eq "left") {
6665 push(@left, $part);
6666 } else {
6667 push(@right, $part);
6668 $state = "right";
6672 my $depth = @patterns;
6673 if ($depth == 0) {
6674 die "One '*' is needed in glob: '$glob'\n";
6676 my $left = join('/', @left);
6677 my $right = join('/', @right);
6678 $re = join('/', @patterns);
6679 $re = join('\/',
6680 grep(length, quotemeta($left), "($re)", quotemeta($right)));
6681 my $left_re = qr/^\/\Q$left\E(\/|$)/;
6682 bless { left => $left, right => $right, left_regex => $left_re,
6683 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
6686 sub full_path {
6687 my ($self, $path) = @_;
6688 return (length $self->{left} ? "$self->{left}/" : '') .
6689 $path . (length $self->{right} ? "/$self->{right}" : '');
6692 __END__
6694 Data structures:
6697 $remotes = { # returned by read_all_remotes()
6698 'svn' => {
6699 # svn-remote.svn.url=https://svn.musicpd.org
6700 url => 'https://svn.musicpd.org',
6701 # svn-remote.svn.fetch=mpd/trunk:trunk
6702 fetch => {
6703 'mpd/trunk' => 'trunk',
6705 # svn-remote.svn.tags=mpd/tags/*:tags/*
6706 tags => {
6707 path => {
6708 left => 'mpd/tags',
6709 right => '',
6710 regex => qr!mpd/tags/([^/]+)$!,
6711 glob => 'tags/*',
6713 ref => {
6714 left => 'tags',
6715 right => '',
6716 regex => qr!tags/([^/]+)$!,
6717 glob => 'tags/*',
6723 $log_entry hashref as returned by libsvn_log_entry()
6725 log => 'whitespace-formatted log entry
6726 ', # trailing newline is preserved
6727 revision => '8', # integer
6728 date => '2004-02-24T17:01:44.108345Z', # commit date
6729 author => 'committer name'
6733 # this is generated by generate_diff();
6734 @mods = array of diff-index line hashes, each element represents one line
6735 of diff-index output
6737 diff-index line ($m hash)
6739 mode_a => first column of diff-index output, no leading ':',
6740 mode_b => second column of diff-index output,
6741 sha1_b => sha1sum of the final blob,
6742 chg => change type [MCRADT],
6743 file_a => original file name of a file (iff chg is 'C' or 'R')
6744 file_b => new/current file name of a file (any chg)
6748 # retval of read_url_paths{,_all}();
6749 $l_map = {
6750 # repository root url
6751 'https://svn.musicpd.org' => {
6752 # repository path # GIT_SVN_ID
6753 'mpd/trunk' => 'trunk',
6754 'mpd/tags/0.11.5' => 'tags/0.11.5',
6758 Notes:
6759 I don't trust the each() function on unless I created %hash myself
6760 because the internal iterator may not have started at base.