git-svn: allow metadata options to be specified with 'init' and 'clone'
[git/haiku.git] / git-svn.perl
blobdc78dcf8cf801f01b2cc191e3fb8053d148c9561
1 #!/usr/bin/env perl
2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
4 use warnings;
5 use strict;
6 use vars qw/ $AUTHOR $VERSION
7 $sha1 $sha1_short $_revision
8 $_q $_authors %users/;
9 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
10 $VERSION = '@@GIT_VERSION@@';
12 $ENV{GIT_DIR} ||= '.git';
13 $Git::SVN::default_repo_id = 'svn';
14 $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
15 $Git::SVN::Ra::_log_window_size = 100;
17 $Git::SVN::Log::TZ = $ENV{TZ};
18 $ENV{TZ} = 'UTC';
19 $| = 1; # unbuffer STDOUT
21 sub fatal (@) { print STDERR @_; exit 1 }
22 require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
23 require SVN::Ra;
24 require SVN::Delta;
25 if ($SVN::Core::VERSION lt '1.1.0') {
26 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
28 push @Git::SVN::Ra::ISA, 'SVN::Ra';
29 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
30 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
31 use Carp qw/croak/;
32 use IO::File qw//;
33 use File::Basename qw/dirname basename/;
34 use File::Path qw/mkpath/;
35 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
36 use IPC::Open3;
37 use Git;
39 BEGIN {
40 my $s;
41 foreach (qw/command command_oneline command_noisy command_output_pipe
42 command_input_pipe command_close_pipe/) {
43 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
44 "*Git::SVN::Migration::$_ = ".
45 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
47 eval $s;
50 my ($SVN);
52 $sha1 = qr/[a-f\d]{40}/;
53 $sha1_short = qr/[a-f\d]{4,40}/;
54 my ($_stdin, $_help, $_edit,
55 $_message, $_file,
56 $_template, $_shared,
57 $_version, $_fetch_all,
58 $_merge, $_strategy, $_dry_run,
59 $_prefix, $_no_checkout, $_verbose);
60 $Git::SVN::_follow_parent = 1;
61 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
62 'config-dir=s' => \$Git::SVN::Ra::config_dir,
63 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
64 my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
65 'authors-file|A=s' => \$_authors,
66 'repack:i' => \$Git::SVN::_repack,
67 'noMetadata' => \$Git::SVN::_no_metadata,
68 'useSvmProps' => \$Git::SVN::_use_svm_props,
69 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
70 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
71 'no-checkout' => \$_no_checkout,
72 'quiet|q' => \$_q,
73 'repack-flags|repack-args|repack-opts=s' =>
74 \$Git::SVN::_repack_flags,
75 %remote_opts );
77 my ($_trunk, $_tags, $_branches);
78 my %icv;
79 my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
80 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
81 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
82 'no-metadata' => sub { $icv{noMetadata} = 1 },
83 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
84 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
85 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
86 %remote_opts );
87 my %cmt_opts = ( 'edit|e' => \$_edit,
88 'rmdir' => \$SVN::Git::Editor::_rmdir,
89 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
90 'l=i' => \$SVN::Git::Editor::_rename_limit,
91 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
94 my %cmd = (
95 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
96 { 'revision|r=s' => \$_revision,
97 'fetch-all|all' => \$_fetch_all,
98 %fc_opts } ],
99 clone => [ \&cmd_clone, "Initialize and fetch revisions",
100 { 'revision|r=s' => \$_revision,
101 %fc_opts, %init_opts } ],
102 init => [ \&cmd_init, "Initialize a repo for tracking" .
103 " (requires URL argument)",
104 \%init_opts ],
105 'multi-init' => [ \&cmd_multi_init,
106 "Deprecated alias for ".
107 "'$0 init -T<trunk> -b<branches> -t<tags>'",
108 \%init_opts ],
109 dcommit => [ \&cmd_dcommit,
110 'Commit several diffs to merge with upstream',
111 { 'merge|m|M' => \$_merge,
112 'strategy|s=s' => \$_strategy,
113 'verbose|v' => \$_verbose,
114 'dry-run|n' => \$_dry_run,
115 'fetch-all|all' => \$_fetch_all,
116 %cmt_opts, %fc_opts } ],
117 'set-tree' => [ \&cmd_set_tree,
118 "Set an SVN repository to a git tree-ish",
119 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
120 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
121 { 'revision|r=i' => \$_revision } ],
122 'multi-fetch' => [ \&cmd_multi_fetch,
123 "Deprecated alias for $0 fetch --all",
124 { 'revision|r=s' => \$_revision, %fc_opts } ],
125 'migrate' => [ sub { },
126 # no-op, we automatically run this anyways,
127 'Migrate configuration/metadata/layout from
128 previous versions of git-svn',
129 { 'minimize' => \$Git::SVN::Migration::_minimize,
130 %remote_opts } ],
131 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
132 { 'limit=i' => \$Git::SVN::Log::limit,
133 'revision|r=s' => \$_revision,
134 'verbose|v' => \$Git::SVN::Log::verbose,
135 'incremental' => \$Git::SVN::Log::incremental,
136 'oneline' => \$Git::SVN::Log::oneline,
137 'show-commit' => \$Git::SVN::Log::show_commit,
138 'non-recursive' => \$Git::SVN::Log::non_recursive,
139 'authors-file|A=s' => \$_authors,
140 'color' => \$Git::SVN::Log::color,
141 'pager=s' => \$Git::SVN::Log::pager,
142 } ],
143 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
144 { 'merge|m|M' => \$_merge,
145 'verbose|v' => \$_verbose,
146 'strategy|s=s' => \$_strategy,
147 'fetch-all|all' => \$_fetch_all,
148 %fc_opts } ],
149 'commit-diff' => [ \&cmd_commit_diff,
150 'Commit a diff between two trees',
151 { 'message|m=s' => \$_message,
152 'file|F=s' => \$_file,
153 'revision|r=s' => \$_revision,
154 %cmt_opts } ],
157 my $cmd;
158 for (my $i = 0; $i < @ARGV; $i++) {
159 if (defined $cmd{$ARGV[$i]}) {
160 $cmd = $ARGV[$i];
161 splice @ARGV, $i, 1;
162 last;
166 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
168 read_repo_config(\%opts);
169 my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
170 'minimize-connections' => \$Git::SVN::Migration::_minimize,
171 'id|i=s' => \$Git::SVN::default_ref_id,
172 'svn-remote|remote|R=s' => sub {
173 $Git::SVN::no_reuse_existing = 1;
174 $Git::SVN::default_repo_id = $_[1] });
175 exit 1 if (!$rv && $cmd ne 'log');
177 usage(0) if $_help;
178 version() if $_version;
179 usage(1) unless defined $cmd;
180 load_authors() if $_authors;
181 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
182 Git::SVN::Migration::migration_check();
184 Git::SVN::init_vars();
185 eval {
186 Git::SVN::verify_remotes_sanity();
187 $cmd{$cmd}->[0]->(@ARGV);
189 fatal $@ if $@;
190 post_fetch_checkout();
191 exit 0;
193 ####################### primary functions ######################
194 sub usage {
195 my $exit = shift || 0;
196 my $fd = $exit ? \*STDERR : \*STDOUT;
197 print $fd <<"";
198 git-svn - bidirectional operations between a single Subversion tree and git
199 Usage: $0 <command> [options] [arguments]\n
201 print $fd "Available commands:\n" unless $cmd;
203 foreach (sort keys %cmd) {
204 next if $cmd && $cmd ne $_;
205 next if /^multi-/; # don't show deprecated commands
206 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
207 foreach (keys %{$cmd{$_}->[2]}) {
208 # prints out arguments as they should be passed:
209 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
210 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
211 "--$_" : "-$_" }
212 split /\|/,$_)," $x\n";
215 print $fd <<"";
216 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
217 arbitrary identifier if you're tracking multiple SVN branches/repositories in
218 one git repository and want to keep them separate. See git-svn(1) for more
219 information.
221 exit $exit;
224 sub version {
225 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
226 exit 0;
229 sub do_git_init_db {
230 unless (-d $ENV{GIT_DIR}) {
231 my @init_db = ('init');
232 push @init_db, "--template=$_template" if defined $_template;
233 if (defined $_shared) {
234 if ($_shared =~ /[a-z]/) {
235 push @init_db, "--shared=$_shared";
236 } else {
237 push @init_db, "--shared";
240 command_noisy(@init_db);
242 my $set;
243 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
244 foreach my $i (keys %icv) {
245 die "'$set' and '$i' cannot both be set\n" if $set;
246 next unless defined $icv{$i};
247 command_noisy('config', "$pfx.$i", $icv{$i});
248 $set = $i;
252 sub init_subdir {
253 my $repo_path = shift or return;
254 mkpath([$repo_path]) unless -d $repo_path;
255 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
256 $ENV{GIT_DIR} = $repo_path . "/.git";
259 sub cmd_clone {
260 my ($url, $path) = @_;
261 if (!defined $path &&
262 (defined $_trunk || defined $_branches || defined $_tags) &&
263 $url !~ m#^[a-z\+]+://#) {
264 $path = $url;
266 warn "--path: $path\n" if defined $path;
267 $path = basename($url) if !defined $path || !length $path;
268 warn "++path: $path\n" if defined $path;
269 mkpath([$path]);
270 chdir $path or die "Couldn't chdir to $path\n";
271 cmd_init(@_);
272 Git::SVN::fetch_all($Git::SVN::default_repo_id);
275 sub cmd_init {
276 if (defined $_trunk || defined $_branches || defined $_tags) {
277 return cmd_multi_init(@_);
279 my $url = shift or die "SVN repository location required ",
280 "as a command-line argument\n";
281 init_subdir(@_);
282 do_git_init_db();
284 Git::SVN->init($url);
287 sub cmd_fetch {
288 if (grep /^\d+=./, @_) {
289 die "'<rev>=<commit>' fetch arguments are ",
290 "no longer supported.\n";
292 my ($remote) = @_;
293 if (@_ > 1) {
294 die "Usage: $0 fetch [--all] [svn-remote]\n";
296 $remote ||= $Git::SVN::default_repo_id;
297 if ($_fetch_all) {
298 cmd_multi_fetch();
299 } else {
300 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
304 sub cmd_set_tree {
305 my (@commits) = @_;
306 if ($_stdin || !@commits) {
307 print "Reading from stdin...\n";
308 @commits = ();
309 while (<STDIN>) {
310 if (/\b($sha1_short)\b/o) {
311 unshift @commits, $1;
315 my @revs;
316 foreach my $c (@commits) {
317 my @tmp = command('rev-parse',$c);
318 if (scalar @tmp == 1) {
319 push @revs, $tmp[0];
320 } elsif (scalar @tmp > 1) {
321 push @revs, reverse(command('rev-list',@tmp));
322 } else {
323 fatal "Failed to rev-parse $c\n";
326 my $gs = Git::SVN->new;
327 my ($r_last, $cmt_last) = $gs->last_rev_commit;
328 $gs->fetch;
329 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
330 fatal "There are new revisions that were fetched ",
331 "and need to be merged (or acknowledged) ",
332 "before committing.\nlast rev: $r_last\n",
333 " current: $gs->{last_rev}\n";
335 $gs->set_tree($_) foreach @revs;
336 print "Done committing ",scalar @revs," revisions to SVN\n";
339 sub cmd_dcommit {
340 my $head = shift;
341 $head ||= 'HEAD';
342 my @refs;
343 my ($url, $rev, $uuid) = working_head_info($head, \@refs);
344 my $c = $refs[-1];
345 unless (defined $url && defined $rev && defined $uuid) {
346 die "Unable to determine upstream SVN information from ",
347 "$head history\n";
349 my $gs = Git::SVN->find_by_url($url);
350 my $last_rev;
351 foreach my $d (@refs) {
352 if (!verify_ref("$d~1")) {
353 fatal "Commit $d\n",
354 "has no parent commit, and therefore ",
355 "nothing to diff against.\n",
356 "You should be working from a repository ",
357 "originally created by git-svn\n";
359 unless (defined $last_rev) {
360 (undef, $last_rev, undef) = cmt_metadata("$d~1");
361 unless (defined $last_rev) {
362 fatal "Unable to extract revision information ",
363 "from commit $d~1\n";
366 if ($_dry_run) {
367 print "diff-tree $d~1 $d\n";
368 } else {
369 my %ed_opts = ( r => $last_rev,
370 log => get_commit_entry($d)->{log},
371 ra => Git::SVN::Ra->new($url),
372 tree_a => "$d~1",
373 tree_b => $d,
374 editor_cb => sub {
375 print "Committed r$_[0]\n";
376 $last_rev = $_[0]; },
377 svn_path => '');
378 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
379 print "No changes\n$d~1 == $d\n";
383 return if $_dry_run;
384 unless ($gs) {
385 warn "Could not determine fetch information for $url\n",
386 "Will not attempt to fetch and rebase commits.\n",
387 "This probably means you have useSvmProps and should\n",
388 "now resync your SVN::Mirror repository.\n";
389 return;
391 $_fetch_all ? $gs->fetch_all : $gs->fetch;
392 # we always want to rebase against the current HEAD, not any
393 # head that was passed to us
394 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
395 my @finish;
396 if (@diff) {
397 @finish = rebase_cmd();
398 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
399 "using @finish:\n", "@diff";
400 } else {
401 print "No changes between current HEAD and ",
402 $gs->refname, "\nResetting to the latest ",
403 $gs->refname, "\n";
404 @finish = qw/reset --mixed/;
406 command_noisy(@finish, $gs->refname);
409 sub cmd_rebase {
410 command_noisy(qw/update-index --refresh/);
411 my $url = (working_head_info('HEAD'))[0];
412 if (!defined $url) {
413 die "Unable to determine upstream SVN information from ",
414 "working tree history\n";
417 my $gs = Git::SVN->find_by_url($url);
418 if (command(qw/diff-index HEAD --/)) {
419 print STDERR "Cannot rebase with uncommited changes:\n";
420 command_noisy('status');
421 exit 1;
423 $_fetch_all ? $gs->fetch_all : $gs->fetch;
424 command_noisy(rebase_cmd(), $gs->refname);
427 sub cmd_show_ignore {
428 my $gs = Git::SVN->new;
429 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
430 $gs->traverse_ignore(\*STDOUT, '', $r);
433 sub cmd_multi_init {
434 my $url = shift;
435 unless (defined $_trunk || defined $_branches || defined $_tags) {
436 usage(1);
438 do_git_init_db();
439 $_prefix = '' unless defined $_prefix;
440 if (defined $url) {
441 $url =~ s#/+$##;
442 init_subdir(@_);
444 if (defined $_trunk) {
445 my $trunk_ref = $_prefix . 'trunk';
446 # try both old-style and new-style lookups:
447 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
448 unless ($gs_trunk) {
449 my ($trunk_url, $trunk_path) =
450 complete_svn_url($url, $_trunk);
451 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
452 undef, $trunk_ref);
455 return unless defined $_branches || defined $_tags;
456 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
457 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
458 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
461 sub cmd_multi_fetch {
462 my $remotes = Git::SVN::read_all_remotes();
463 foreach my $repo_id (sort keys %$remotes) {
464 if ($remotes->{$repo_id}->{url}) {
465 Git::SVN::fetch_all($repo_id, $remotes);
470 # this command is special because it requires no metadata
471 sub cmd_commit_diff {
472 my ($ta, $tb, $url) = @_;
473 my $usage = "Usage: $0 commit-diff -r<revision> ".
474 "<tree-ish> <tree-ish> [<URL>]\n";
475 fatal($usage) if (!defined $ta || !defined $tb);
476 my $svn_path;
477 if (!defined $url) {
478 my $gs = eval { Git::SVN->new };
479 if (!$gs) {
480 fatal("Needed URL or usable git-svn --id in ",
481 "the command-line\n", $usage);
483 $url = $gs->{url};
484 $svn_path = $gs->{path};
486 unless (defined $_revision) {
487 fatal("-r|--revision is a required argument\n", $usage);
489 if (defined $_message && defined $_file) {
490 fatal("Both --message/-m and --file/-F specified ",
491 "for the commit message.\n",
492 "I have no idea what you mean\n");
494 if (defined $_file) {
495 $_message = file_to_s($_file);
496 } else {
497 $_message ||= get_commit_entry($tb)->{log};
499 my $ra ||= Git::SVN::Ra->new($url);
500 $svn_path ||= $ra->{svn_path};
501 my $r = $_revision;
502 if ($r eq 'HEAD') {
503 $r = $ra->get_latest_revnum;
504 } elsif ($r !~ /^\d+$/) {
505 die "revision argument: $r not understood by git-svn\n";
507 my %ed_opts = ( r => $r,
508 log => $_message,
509 ra => $ra,
510 tree_a => $ta,
511 tree_b => $tb,
512 editor_cb => sub { print "Committed r$_[0]\n" },
513 svn_path => $svn_path );
514 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
515 print "No changes\n$ta == $tb\n";
519 ########################### utility functions #########################
521 sub rebase_cmd {
522 my @cmd = qw/rebase/;
523 push @cmd, '-v' if $_verbose;
524 push @cmd, qw/--merge/ if $_merge;
525 push @cmd, "--strategy=$_strategy" if $_strategy;
526 @cmd;
529 sub post_fetch_checkout {
530 return if $_no_checkout;
531 my $gs = $Git::SVN::_head or return;
532 return if verify_ref('refs/heads/master^0');
534 my $valid_head = verify_ref('HEAD^0');
535 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
536 return if ($valid_head || !verify_ref('HEAD^0'));
538 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
539 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
540 return if -f $index;
542 chomp(my $bare = `git config --bool --get core.bare`);
543 return if $bare eq 'true';
544 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
545 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
546 print STDERR "Checked out HEAD:\n ",
547 $gs->full_url, " r", $gs->last_rev, "\n";
550 sub complete_svn_url {
551 my ($url, $path) = @_;
552 $path =~ s#/+$##;
553 if ($path !~ m#^[a-z\+]+://#) {
554 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
555 fatal("E: '$path' is not a complete URL ",
556 "and a separate URL is not specified\n");
558 return ($url, $path);
560 return ($path, '');
563 sub complete_url_ls_init {
564 my ($ra, $repo_path, $switch, $pfx) = @_;
565 unless ($repo_path) {
566 print STDERR "W: $switch not specified\n";
567 return;
569 $repo_path =~ s#/+$##;
570 if ($repo_path =~ m#^[a-z\+]+://#) {
571 $ra = Git::SVN::Ra->new($repo_path);
572 $repo_path = '';
573 } else {
574 $repo_path =~ s#^/+##;
575 unless ($ra) {
576 fatal("E: '$repo_path' is not a complete URL ",
577 "and a separate URL is not specified\n");
580 my $url = $ra->{url};
581 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
582 my $k = "svn-remote.$gs->{repo_id}.url";
583 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
584 if ($orig_url && ($orig_url ne $gs->{url})) {
585 die "$k already set: $orig_url\n",
586 "wanted to set to: $gs->{url}\n";
588 command_oneline('config', $k, $gs->{url}) unless $orig_url;
589 my $remote_path = "$ra->{svn_path}/$repo_path/*";
590 $remote_path =~ s#/+#/#g;
591 $remote_path =~ s#^/##g;
592 my ($n) = ($switch =~ /^--(\w+)/);
593 if (length $pfx && $pfx !~ m#/$#) {
594 die "--prefix='$pfx' must have a trailing slash '/'\n";
596 command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
597 "$remote_path:refs/remotes/$pfx*");
600 sub verify_ref {
601 my ($ref) = @_;
602 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
603 { STDERR => 0 }); };
606 sub get_tree_from_treeish {
607 my ($treeish) = @_;
608 # $treeish can be a symbolic ref, too:
609 my $type = command_oneline(qw/cat-file -t/, $treeish);
610 my $expected;
611 while ($type eq 'tag') {
612 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
614 if ($type eq 'commit') {
615 $expected = (grep /^tree /, command(qw/cat-file commit/,
616 $treeish))[0];
617 ($expected) = ($expected =~ /^tree ($sha1)$/o);
618 die "Unable to get tree from $treeish\n" unless $expected;
619 } elsif ($type eq 'tree') {
620 $expected = $treeish;
621 } else {
622 die "$treeish is a $type, expected tree, tag or commit\n";
624 return $expected;
627 sub get_commit_entry {
628 my ($treeish) = shift;
629 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
630 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
631 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
632 open my $log_fh, '>', $commit_editmsg or croak $!;
634 my $type = command_oneline(qw/cat-file -t/, $treeish);
635 if ($type eq 'commit' || $type eq 'tag') {
636 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
637 $type, $treeish);
638 my $in_msg = 0;
639 while (<$msg_fh>) {
640 if (!$in_msg) {
641 $in_msg = 1 if (/^\s*$/);
642 } elsif (/^git-svn-id: /) {
643 # skip this for now, we regenerate the
644 # correct one on re-fetch anyways
645 # TODO: set *:merge properties or like...
646 } else {
647 print $log_fh $_ or croak $!;
650 command_close_pipe($msg_fh, $ctx);
652 close $log_fh or croak $!;
654 if ($_edit || ($type eq 'tree')) {
655 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
656 # TODO: strip out spaces, comments, like git-commit.sh
657 system($editor, $commit_editmsg);
659 rename $commit_editmsg, $commit_msg or croak $!;
660 open $log_fh, '<', $commit_msg or croak $!;
661 { local $/; chomp($log_entry{log} = <$log_fh>); }
662 close $log_fh or croak $!;
663 unlink $commit_msg;
664 \%log_entry;
667 sub s_to_file {
668 my ($str, $file, $mode) = @_;
669 open my $fd,'>',$file or croak $!;
670 print $fd $str,"\n" or croak $!;
671 close $fd or croak $!;
672 chmod ($mode &~ umask, $file) if (defined $mode);
675 sub file_to_s {
676 my $file = shift;
677 open my $fd,'<',$file or croak "$!: file: $file\n";
678 local $/;
679 my $ret = <$fd>;
680 close $fd or croak $!;
681 $ret =~ s/\s*$//s;
682 return $ret;
685 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
686 sub load_authors {
687 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
688 my $log = $cmd eq 'log';
689 while (<$authors>) {
690 chomp;
691 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
692 my ($user, $name, $email) = ($1, $2, $3);
693 if ($log) {
694 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
695 } else {
696 $users{$user} = [$name, $email];
699 close $authors or croak $!;
702 # convert GetOpt::Long specs for use by git-config
703 sub read_repo_config {
704 return unless -d $ENV{GIT_DIR};
705 my $opts = shift;
706 my @config_only;
707 foreach my $o (keys %$opts) {
708 # if we have mixedCase and a long option-only, then
709 # it's a config-only variable that we don't need for
710 # the command-line.
711 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
712 my $v = $opts->{$o};
713 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
714 $key =~ s/-//g;
715 my $arg = 'git-config';
716 $arg .= ' --int' if ($o =~ /[:=]i$/);
717 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
718 if (ref $v eq 'ARRAY') {
719 chomp(my @tmp = `$arg --get-all svn.$key`);
720 @$v = @tmp if @tmp;
721 } else {
722 chomp(my $tmp = `$arg --get svn.$key`);
723 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
724 $$v = $tmp;
728 delete @$opts{@config_only} if @config_only;
731 sub extract_metadata {
732 my $id = shift or return (undef, undef, undef);
733 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
734 \s([a-f\d\-]+)$/x);
735 if (!defined $rev || !$uuid || !$url) {
736 # some of the original repositories I made had
737 # identifiers like this:
738 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
740 return ($url, $rev, $uuid);
743 sub cmt_metadata {
744 return extract_metadata((grep(/^git-svn-id: /,
745 command(qw/cat-file commit/, shift)))[-1]);
748 sub working_head_info {
749 my ($head, $refs) = @_;
750 my ($url, $rev, $uuid);
751 my ($fh, $ctx) = command_output_pipe('rev-list', $head);
752 while (<$fh>) {
753 chomp;
754 ($url, $rev, $uuid) = cmt_metadata($_);
755 last if (defined $url && defined $rev && defined $uuid);
756 unshift @$refs, $_ if $refs;
758 close $fh; # break the pipe
759 ($url, $rev, $uuid);
762 package Git::SVN;
763 use strict;
764 use warnings;
765 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
766 $_repack $_repack_flags $_use_svm_props $_head
767 $_use_svnsync_props $no_reuse_existing/;
768 use Carp qw/croak/;
769 use File::Path qw/mkpath/;
770 use File::Copy qw/copy/;
771 use IPC::Open3;
773 my $_repack_nr;
774 # properties that we do not log:
775 my %SKIP_PROP;
776 BEGIN {
777 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
778 svn:special svn:executable
779 svn:entry:committed-rev
780 svn:entry:last-author
781 svn:entry:uuid
782 svn:entry:committed-date/;
784 # some options are read globally, but can be overridden locally
785 # per [svn-remote "..."] section. Command-line options will *NOT*
786 # override options set in an [svn-remote "..."] section
787 my $e;
788 foreach (qw/follow_parent no_metadata use_svm_props
789 use_svnsync_props/) {
790 my $key = $_;
791 $key =~ tr/_//d;
792 $e .= "sub $_ {
793 my (\$self) = \@_;
794 return \$self->{-$_} if exists \$self->{-$_};
795 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
796 eval { command_oneline(qw/config --get/, \$k) };
797 if (\$@) {
798 \$self->{-$_} = \$Git::SVN::_$_;
799 } else {
800 my \$v = command_oneline(qw/config --bool/,\$k);
801 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
803 return \$self->{-$_} }\n";
805 $e .= "1;\n";
806 eval $e or die $@;
809 my %LOCKFILES;
810 END { unlink keys %LOCKFILES if %LOCKFILES }
812 sub resolve_local_globs {
813 my ($url, $fetch, $glob_spec) = @_;
814 return unless defined $glob_spec;
815 my $ref = $glob_spec->{ref};
816 my $path = $glob_spec->{path};
817 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
818 next unless m#^refs/remotes/$ref->{regex}$#;
819 my $p = $1;
820 my $pathname = $path->full_path($p);
821 my $refname = $ref->full_path($p);
822 if (my $existing = $fetch->{$pathname}) {
823 if ($existing ne $refname) {
824 die "Refspec conflict:\n",
825 "existing: refs/remotes/$existing\n",
826 " globbed: refs/remotes/$refname\n";
828 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
829 $u =~ s!^\Q$url\E(/|$)!! or die
830 "refs/remotes/$refname: '$url' not found in '$u'\n";
831 if ($pathname ne $u) {
832 warn "W: Refspec glob conflict ",
833 "(ref: refs/remotes/$refname):\n",
834 "expected path: $pathname\n",
835 " real path: $u\n",
836 "Continuing ahead with $u\n";
837 next;
839 } else {
840 $fetch->{$pathname} = $refname;
845 sub parse_revision_argument {
846 my ($base, $head) = @_;
847 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
848 return ($base, $head);
850 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
851 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
852 return ($head, $head) if ($::_revision eq 'HEAD');
853 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
854 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
855 die "revision argument: $::_revision not understood by git-svn\n";
858 sub fetch_all {
859 my ($repo_id, $remotes) = @_;
860 if (ref $repo_id) {
861 my $gs = $repo_id;
862 $repo_id = undef;
863 $repo_id = $gs->{repo_id};
865 $remotes ||= read_all_remotes();
866 my $remote = $remotes->{$repo_id} or
867 die "[svn-remote \"$repo_id\"] unknown\n";
868 my $fetch = $remote->{fetch};
869 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
870 my (@gs, @globs);
871 my $ra = Git::SVN::Ra->new($url);
872 my $uuid = $ra->get_uuid;
873 my $head = $ra->get_latest_revnum;
874 my $base = defined $fetch ? $head : 0;
876 # read the max revs for wildcard expansion (branches/*, tags/*)
877 foreach my $t (qw/branches tags/) {
878 defined $remote->{$t} or next;
879 push @globs, $remote->{$t};
880 my $max_rev = eval { tmp_config(qw/--int --get/,
881 "svn-remote.$repo_id.${t}-maxRev") };
882 if (defined $max_rev && ($max_rev < $base)) {
883 $base = $max_rev;
884 } elsif (!defined $max_rev) {
885 $base = 0;
889 if ($fetch) {
890 foreach my $p (sort keys %$fetch) {
891 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
892 my $lr = $gs->rev_db_max;
893 if (defined $lr) {
894 $base = $lr if ($lr < $base);
896 push @gs, $gs;
900 ($base, $head) = parse_revision_argument($base, $head);
901 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
904 sub read_all_remotes {
905 my $r = {};
906 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
907 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
908 $r->{$1}->{fetch}->{$2} = $3;
909 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
910 $r->{$1}->{url} = $2;
911 } elsif (m!^(.+)\.(branches|tags)=
912 (.*):refs/remotes/(.+)\s*$/!x) {
913 my ($p, $g) = ($3, $4);
914 my $rs = $r->{$1}->{$2} = {
915 t => $2,
916 remote => $1,
917 path => Git::SVN::GlobSpec->new($p),
918 ref => Git::SVN::GlobSpec->new($g) };
919 if (length($rs->{ref}->{right}) != 0) {
920 die "The '*' glob character must be the last ",
921 "character of '$g'\n";
928 sub init_vars {
929 if (defined $_repack) {
930 $_repack = 1000 if ($_repack <= 0);
931 $_repack_nr = $_repack;
932 $_repack_flags ||= '-d';
936 sub verify_remotes_sanity {
937 return unless -d $ENV{GIT_DIR};
938 my %seen;
939 foreach (command(qw/config -l/)) {
940 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
941 if ($seen{$1}) {
942 die "Remote ref refs/remote/$1 is tracked by",
943 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
944 "Please resolve this ambiguity in ",
945 "your git configuration file before ",
946 "continuing\n";
948 $seen{$1} = $_;
953 # we allow more chars than remotes2config.sh...
954 sub sanitize_remote_name {
955 my ($name) = @_;
956 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
957 $name;
960 sub find_existing_remote {
961 my ($url, $remotes) = @_;
962 return undef if $no_reuse_existing;
963 my $existing;
964 foreach my $repo_id (keys %$remotes) {
965 my $u = $remotes->{$repo_id}->{url} or next;
966 next if $u ne $url;
967 $existing = $repo_id;
968 last;
970 $existing;
973 sub init_remote_config {
974 my ($self, $url, $no_write) = @_;
975 $url =~ s!/+$!!; # strip trailing slash
976 my $r = read_all_remotes();
977 my $existing = find_existing_remote($url, $r);
978 if ($existing) {
979 unless ($no_write) {
980 print STDERR "Using existing ",
981 "[svn-remote \"$existing\"]\n";
983 $self->{repo_id} = $existing;
984 } else {
985 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
986 $existing = find_existing_remote($min_url, $r);
987 if ($existing) {
988 unless ($no_write) {
989 print STDERR "Using existing ",
990 "[svn-remote \"$existing\"]\n";
992 $self->{repo_id} = $existing;
994 if ($min_url ne $url) {
995 unless ($no_write) {
996 print STDERR "Using higher level of URL: ",
997 "$url => $min_url\n";
999 my $old_path = $self->{path};
1000 $self->{path} = $url;
1001 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
1002 if (length $old_path) {
1003 $self->{path} .= "/$old_path";
1005 $url = $min_url;
1008 my $orig_url;
1009 if (!$existing) {
1010 # verify that we aren't overwriting anything:
1011 $orig_url = eval {
1012 command_oneline('config', '--get',
1013 "svn-remote.$self->{repo_id}.url")
1015 if ($orig_url && ($orig_url ne $url)) {
1016 die "svn-remote.$self->{repo_id}.url already set: ",
1017 "$orig_url\nwanted to set to: $url\n";
1020 my ($xrepo_id, $xpath) = find_ref($self->refname);
1021 if (defined $xpath) {
1022 die "svn-remote.$xrepo_id.fetch already set to track ",
1023 "$xpath:refs/remotes/", $self->refname, "\n";
1025 unless ($no_write) {
1026 command_noisy('config',
1027 "svn-remote.$self->{repo_id}.url", $url);
1028 command_noisy('config', '--add',
1029 "svn-remote.$self->{repo_id}.fetch",
1030 "$self->{path}:".$self->refname);
1032 $self->{url} = $url;
1035 sub find_by_url { # repos_root and, path are optional
1036 my ($class, $full_url, $repos_root, $path) = @_;
1037 my $remotes = read_all_remotes();
1038 if (defined $full_url && defined $repos_root && !defined $path) {
1039 $path = $full_url;
1040 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1042 foreach my $repo_id (keys %$remotes) {
1043 my $u = $remotes->{$repo_id}->{url} or next;
1044 next if defined $repos_root && $repos_root ne $u;
1046 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1047 foreach (qw/branches tags/) {
1048 resolve_local_globs($u, $fetch,
1049 $remotes->{$repo_id}->{$_});
1051 my $p = $path;
1052 unless (defined $p) {
1053 $p = $full_url;
1054 $p =~ s#^\Q$u\E(?:/|$)## or next;
1056 foreach my $f (keys %$fetch) {
1057 next if $f ne $p;
1058 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1061 undef;
1064 sub init {
1065 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
1066 my $self = _new($class, $repo_id, $ref_id, $path);
1067 if (defined $url) {
1068 $self->init_remote_config($url, $no_write);
1070 $self;
1073 sub find_ref {
1074 my ($ref_id) = @_;
1075 foreach (command(qw/config -l/)) {
1076 next unless m!^svn-remote\.(.+)\.fetch=
1077 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1078 my ($repo_id, $path, $ref) = ($1, $2, $3);
1079 if ($ref eq $ref_id) {
1080 $path = '' if ($path =~ m#^\./?#);
1081 return ($repo_id, $path);
1084 (undef, undef, undef);
1087 sub new {
1088 my ($class, $ref_id, $repo_id, $path) = @_;
1089 if (defined $ref_id && !defined $repo_id && !defined $path) {
1090 ($repo_id, $path) = find_ref($ref_id);
1091 if (!defined $repo_id) {
1092 die "Could not find a \"svn-remote.*.fetch\" key ",
1093 "in the repository configuration matching: ",
1094 "refs/remotes/$ref_id\n";
1097 my $self = _new($class, $repo_id, $ref_id, $path);
1098 if (!defined $self->{path} || !length $self->{path}) {
1099 my $fetch = command_oneline('config', '--get',
1100 "svn-remote.$repo_id.fetch",
1101 ":refs/remotes/$ref_id\$") or
1102 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1103 "\":refs/remotes/$ref_id\$\" in config\n";
1104 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1106 $self->{url} = command_oneline('config', '--get',
1107 "svn-remote.$repo_id.url") or
1108 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
1109 $self->rebuild;
1110 $self;
1113 sub refname { "refs/remotes/$_[0]->{ref_id}" }
1115 sub svm_uuid {
1116 my ($self) = @_;
1117 return $self->{svm}->{uuid} if $self->svm;
1118 $self->ra;
1119 unless ($self->{svm}) {
1120 die "SVM UUID not cached, and reading remotely failed\n";
1122 $self->{svm}->{uuid};
1125 sub svm {
1126 my ($self) = @_;
1127 return $self->{svm} if $self->{svm};
1128 my $svm;
1129 # see if we have it in our config, first:
1130 eval {
1131 my $section = "svn-remote.$self->{repo_id}";
1132 $svm = {
1133 source => tmp_config('--get', "$section.svm-source"),
1134 uuid => tmp_config('--get', "$section.svm-uuid"),
1135 replace => tmp_config('--get', "$section.svm-replace"),
1138 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1139 $self->{svm} = $svm;
1141 $self->{svm};
1144 sub _set_svm_vars {
1145 my ($self, $ra) = @_;
1146 return $ra if $self->svm;
1148 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
1149 "(svm:source, svm:uuid) ",
1150 "from the following URLs:\n" );
1151 sub read_svm_props {
1152 my ($self, $ra, $path, $r) = @_;
1153 my $props = ($ra->get_dir($path, $r))[2];
1154 my $src = $props->{'svm:source'};
1155 my $uuid = $props->{'svm:uuid'};
1156 return undef if (!$src || !$uuid);
1158 chomp($src, $uuid);
1160 $uuid =~ m{^[0-9a-f\-]{30,}$}
1161 or die "doesn't look right - svm:uuid is '$uuid'\n";
1163 # the '!' is used to mark the repos_root!/relative/path
1164 $src =~ s{/?!/?}{/};
1165 $src =~ s{/+$}{}; # no trailing slashes please
1166 # username is of no interest
1167 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
1169 my $replace = $ra->{url};
1170 $replace .= "/$path" if length $path;
1172 my $section = "svn-remote.$self->{repo_id}";
1173 tmp_config("$section.svm-source", $src);
1174 tmp_config("$section.svm-replace", $replace);
1175 tmp_config("$section.svm-uuid", $uuid);
1176 $self->{svm} = {
1177 source => $src,
1178 uuid => $uuid,
1179 replace => $replace
1183 my $r = $ra->get_latest_revnum;
1184 my $path = $self->{path};
1185 my %tried;
1186 while (length $path) {
1187 unless ($tried{"$self->{url}/$path"}) {
1188 return $ra if $self->read_svm_props($ra, $path, $r);
1189 $tried{"$self->{url}/$path"} = 1;
1191 $path =~ s#/?[^/]+$##;
1193 die "Path: '$path' should be ''\n" if $path ne '';
1194 return $ra if $self->read_svm_props($ra, $path, $r);
1195 $tried{"$self->{url}/$path"} = 1;
1197 if ($ra->{repos_root} eq $self->{url}) {
1198 die @err, (map { " $_\n" } keys %tried), "\n";
1201 # nope, make sure we're connected to the repository root:
1202 my $ok;
1203 my @tried_b;
1204 $path = $ra->{svn_path};
1205 $ra = Git::SVN::Ra->new($ra->{repos_root});
1206 while (length $path) {
1207 unless ($tried{"$ra->{url}/$path"}) {
1208 $ok = $self->read_svm_props($ra, $path, $r);
1209 last if $ok;
1210 $tried{"$ra->{url}/$path"} = 1;
1212 $path =~ s#/?[^/]+$##;
1214 die "Path: '$path' should be ''\n" if $path ne '';
1215 $ok ||= $self->read_svm_props($ra, $path, $r);
1216 $tried{"$ra->{url}/$path"} = 1;
1217 if (!$ok) {
1218 die @err, (map { " $_\n" } keys %tried), "\n";
1220 Git::SVN::Ra->new($self->{url});
1223 sub svnsync {
1224 my ($self) = @_;
1225 return $self->{svnsync} if $self->{svnsync};
1227 if ($self->no_metadata) {
1228 die "Can't have both 'noMetadata' and ",
1229 "'useSvnsyncProps' options set!\n";
1231 if ($self->rewrite_root) {
1232 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1233 "options set!\n";
1236 my $svnsync;
1237 # see if we have it in our config, first:
1238 eval {
1239 my $section = "svn-remote.$self->{repo_id}";
1240 $svnsync = {
1241 url => tmp_config('--get', "$section.svnsync-url"),
1242 uuid => tmp_config('--get', "$section.svnsync-uuid"),
1245 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1246 return $self->{svnsync} = $svnsync;
1249 my $err = "useSvnsyncProps set, but failed to read " .
1250 "svnsync property: svn:sync-from-";
1251 my $rp = $self->ra->rev_proplist(0);
1253 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1254 $url =~ m{^[a-z\+]+://} or
1255 die "doesn't look right - svn:sync-from-url is '$url'\n";
1257 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1258 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1259 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1261 my $section = "svn-remote.$self->{repo_id}";
1262 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1263 tmp_config('--add', "$section.svnsync-url", $url);
1264 return $self->{svnsync} = { url => $url, uuid => $uuid };
1267 # this allows us to memoize our SVN::Ra UUID locally and avoid a
1268 # remote lookup (useful for 'git svn log').
1269 sub ra_uuid {
1270 my ($self) = @_;
1271 unless ($self->{ra_uuid}) {
1272 my $key = "svn-remote.$self->{repo_id}.uuid";
1273 my $uuid = eval { tmp_config('--get', $key) };
1274 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1275 $self->{ra_uuid} = $uuid;
1276 } else {
1277 die "ra_uuid called without URL\n" unless $self->{url};
1278 $self->{ra_uuid} = $self->ra->get_uuid;
1279 tmp_config('--add', $key, $self->{ra_uuid});
1282 $self->{ra_uuid};
1285 sub ra {
1286 my ($self) = shift;
1287 my $ra = Git::SVN::Ra->new($self->{url});
1288 if ($self->use_svm_props && !$self->{svm}) {
1289 if ($self->no_metadata) {
1290 die "Can't have both 'noMetadata' and ",
1291 "'useSvmProps' options set!\n";
1292 } elsif ($self->use_svnsync_props) {
1293 die "Can't have both 'useSvnsyncProps' and ",
1294 "'useSvmProps' options set!\n";
1296 $ra = $self->_set_svm_vars($ra);
1297 $self->{-want_revprops} = 1;
1299 $ra;
1302 sub rel_path {
1303 my ($self) = @_;
1304 my $repos_root = $self->ra->{repos_root};
1305 return $self->{path} if ($self->{url} eq $repos_root);
1306 die "BUG: rel_path failed! repos_root: $repos_root, Ra URL: ",
1307 $self->ra->{url}, " path: $self->{path}, URL: $self->{url}\n";
1310 sub traverse_ignore {
1311 my ($self, $fh, $path, $r) = @_;
1312 $path =~ s#^/+##g;
1313 my $ra = $self->ra;
1314 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
1315 my $p = $path;
1316 $p =~ s#^\Q$ra->{svn_path}\E/##;
1317 print $fh length $p ? "\n# $p\n" : "\n# /\n";
1318 if (my $s = $props->{'svn:ignore'}) {
1319 $s =~ s/[\r\n]+/\n/g;
1320 chomp $s;
1321 if (length $p == 0) {
1322 $s =~ s#\n#\n/$p#g;
1323 print $fh "/$s\n";
1324 } else {
1325 $s =~ s#\n#\n/$p/#g;
1326 print $fh "/$p/$s\n";
1329 foreach (sort keys %$dirent) {
1330 next if $dirent->{$_}->kind != $SVN::Node::dir;
1331 $self->traverse_ignore($fh, "$path/$_", $r);
1335 sub last_rev { ($_[0]->last_rev_commit)[0] }
1336 sub last_commit { ($_[0]->last_rev_commit)[1] }
1338 # returns the newest SVN revision number and newest commit SHA1
1339 sub last_rev_commit {
1340 my ($self) = @_;
1341 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1342 return ($self->{last_rev}, $self->{last_commit});
1344 my $c = ::verify_ref($self->refname.'^0');
1345 if ($c && !$self->use_svm_props && !$self->no_metadata) {
1346 my $rev = (::cmt_metadata($c))[1];
1347 if (defined $rev) {
1348 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1349 return ($rev, $c);
1352 my $db_path = $self->db_path;
1353 unless (-e $db_path) {
1354 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
1355 return (undef, undef);
1357 my $offset = -41; # from tail
1358 my $rl;
1359 open my $fh, '<', $db_path or croak "$db_path not readable: $!\n";
1360 sysseek($fh, $offset, 2); # don't care for errors
1361 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1362 chomp $rl;
1363 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
1364 $offset -= 41;
1365 sysseek($fh, $offset, 2); # don't care for errors
1366 sysread($fh, $rl, 41) == 41 or return (undef, undef);
1367 chomp $rl;
1369 if ($c && $c ne $rl) {
1370 die "$db_path and ", $self->refname,
1371 " inconsistent!:\n$c != $rl\n";
1373 my $rev = sysseek($fh, 0, 1) or croak $!;
1374 $rev = ($rev - 41) / 41;
1375 close $fh or croak $!;
1376 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1377 return ($rev, $c);
1380 sub get_fetch_range {
1381 my ($self, $min, $max) = @_;
1382 $max ||= $self->ra->get_latest_revnum;
1383 $min ||= $self->rev_db_max;
1384 (++$min, $max);
1387 sub tmp_config {
1388 my (@args) = @_;
1389 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1390 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1391 if (-e $old_def_config && ! -e $config) {
1392 rename $old_def_config, $config or
1393 die "Failed rename $old_def_config => $config: $!\n";
1395 my $old_config = $ENV{GIT_CONFIG};
1396 $ENV{GIT_CONFIG} = $config;
1397 $@ = undef;
1398 my @ret = eval {
1399 unless (-f $config) {
1400 mkfile($config);
1401 open my $fh, '>', $config or
1402 die "Can't open $config: $!\n";
1403 print $fh "; This file is used internally by ",
1404 "git-svn\n" or die
1405 "Couldn't write to $config: $!\n";
1406 print $fh "; You should not have to edit it\n" or
1407 die "Couldn't write to $config: $!\n";
1408 close $fh or die "Couldn't close $config: $!\n";
1410 command('config', @args);
1412 my $err = $@;
1413 if (defined $old_config) {
1414 $ENV{GIT_CONFIG} = $old_config;
1415 } else {
1416 delete $ENV{GIT_CONFIG};
1418 die $err if $err;
1419 wantarray ? @ret : $ret[0];
1422 sub tmp_index_do {
1423 my ($self, $sub) = @_;
1424 my $old_index = $ENV{GIT_INDEX_FILE};
1425 $ENV{GIT_INDEX_FILE} = $self->{index};
1426 $@ = undef;
1427 my @ret = eval {
1428 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1429 mkpath([$dir]) unless -d $dir;
1430 &$sub;
1432 my $err = $@;
1433 if (defined $old_index) {
1434 $ENV{GIT_INDEX_FILE} = $old_index;
1435 } else {
1436 delete $ENV{GIT_INDEX_FILE};
1438 die $err if $err;
1439 wantarray ? @ret : $ret[0];
1442 sub assert_index_clean {
1443 my ($self, $treeish) = @_;
1445 $self->tmp_index_do(sub {
1446 command_noisy('read-tree', $treeish) unless -e $self->{index};
1447 my $x = command_oneline('write-tree');
1448 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1449 /^tree ($::sha1)/mo);
1450 return if $y eq $x;
1452 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1453 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1454 command_noisy('read-tree', $treeish);
1455 $x = command_oneline('write-tree');
1456 if ($y ne $x) {
1457 ::fatal "trees ($treeish) $y != $x\n",
1458 "Something is seriously wrong...\n";
1463 sub get_commit_parents {
1464 my ($self, $log_entry) = @_;
1465 my (%seen, @ret, @tmp);
1466 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1467 if (my $ip = $self->{inject_parents}) {
1468 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1469 push @tmp, $commit;
1472 if (my $cur = ::verify_ref($self->refname.'^0')) {
1473 push @tmp, $cur;
1475 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
1476 while (my $p = shift @tmp) {
1477 next if $seen{$p};
1478 $seen{$p} = 1;
1479 push @ret, $p;
1480 # MAXPARENT is defined to 16 in commit-tree.c:
1481 last if @ret >= 16;
1483 if (@tmp) {
1484 die "r$log_entry->{revision}: No room for parents:\n\t",
1485 join("\n\t", @tmp), "\n";
1487 @ret;
1490 sub rewrite_root {
1491 my ($self) = @_;
1492 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
1493 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1494 my $rwr = eval { command_oneline(qw/config --get/, $k) };
1495 if ($rwr) {
1496 $rwr =~ s#/+$##;
1497 if ($rwr !~ m#^[a-z\+]+://#) {
1498 die "$rwr is not a valid URL (key: $k)\n";
1501 $self->{-rewrite_root} = $rwr;
1504 sub metadata_url {
1505 my ($self) = @_;
1506 ($self->rewrite_root || $self->{url}) .
1507 (length $self->{path} ? '/' . $self->{path} : '');
1510 sub full_url {
1511 my ($self) = @_;
1512 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
1515 sub do_git_commit {
1516 my ($self, $log_entry) = @_;
1517 my $lr = $self->last_rev;
1518 if (defined $lr && $lr >= $log_entry->{revision}) {
1519 die "Last fetched revision of ", $self->refname,
1520 " was r$lr, but we are about to fetch: ",
1521 "r$log_entry->{revision}!\n";
1523 if (my $c = $self->rev_db_get($log_entry->{revision})) {
1524 croak "$log_entry->{revision} = $c already exists! ",
1525 "Why are we refetching it?\n";
1527 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
1528 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
1529 $log_entry->{email};
1530 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
1532 my $tree = $log_entry->{tree};
1533 if (!defined $tree) {
1534 $tree = $self->tmp_index_do(sub {
1535 command_oneline('write-tree') });
1537 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1539 my @exec = ('git-commit-tree', $tree);
1540 foreach ($self->get_commit_parents($log_entry)) {
1541 push @exec, '-p', $_;
1543 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1544 or croak $!;
1545 print $msg_fh $log_entry->{log} or croak $!;
1546 unless ($self->no_metadata) {
1547 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1548 or croak $!;
1550 $msg_fh->flush == 0 or croak $!;
1551 close $msg_fh or croak $!;
1552 chomp(my $commit = do { local $/; <$out_fh> });
1553 close $out_fh or croak $!;
1554 waitpid $pid, 0;
1555 croak $? if $?;
1556 if ($commit !~ /^$::sha1$/o) {
1557 die "Failed to commit, invalid sha1: $commit\n";
1560 $self->rev_db_set($log_entry->{revision}, $commit, 1);
1562 $self->{last_rev} = $log_entry->{revision};
1563 $self->{last_commit} = $commit;
1564 print "r$log_entry->{revision}";
1565 if (defined $log_entry->{svm_revision}) {
1566 print " (\@$log_entry->{svm_revision})";
1567 $self->rev_db_set($log_entry->{svm_revision}, $commit,
1568 0, $self->svm_uuid);
1570 print " = $commit ($self->{ref_id})\n";
1571 if (defined $_repack && (--$_repack_nr == 0)) {
1572 $_repack_nr = $_repack;
1573 # repack doesn't use any arguments with spaces in them, does it?
1574 print "Running git repack $_repack_flags ...\n";
1575 command_noisy('repack', split(/\s+/, $_repack_flags));
1576 print "Done repacking\n";
1578 return $commit;
1581 sub match_paths {
1582 my ($self, $paths, $r) = @_;
1583 return 1 if $self->{path} eq '';
1584 if (my $path = $paths->{"/$self->{path}"}) {
1585 return ($path->{action} eq 'D') ? 0 : 1;
1587 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
1588 if (grep /$self->{path_regex}/, keys %$paths) {
1589 return 1;
1591 my $c = '';
1592 foreach (split m#/#, $self->{path}) {
1593 $c .= "/$_";
1594 next unless ($paths->{$c} &&
1595 ($paths->{$c}->{action} =~ /^[AR]$/));
1596 if ($self->ra->check_path($self->{path}, $r) ==
1597 $SVN::Node::dir) {
1598 return 1;
1601 return 0;
1604 sub find_parent_branch {
1605 my ($self, $paths, $rev) = @_;
1606 return undef unless $self->follow_parent;
1607 unless (defined $paths) {
1608 my $err_handler = $SVN::Error::handler;
1609 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
1610 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
1611 $paths =
1612 Git::SVN::Ra::dup_changed_paths($_[0]) });
1613 $SVN::Error::handler = $err_handler;
1615 return undef unless defined $paths;
1617 # look for a parent from another branch:
1618 my @b_path_components = split m#/#, $self->rel_path;
1619 my @a_path_components;
1620 my $i;
1621 while (@b_path_components) {
1622 $i = $paths->{'/'.join('/', @b_path_components)};
1623 last if $i && defined $i->{copyfrom_path};
1624 unshift(@a_path_components, pop(@b_path_components));
1626 return undef unless defined $i && defined $i->{copyfrom_path};
1627 my $branch_from = $i->{copyfrom_path};
1628 if (@a_path_components) {
1629 print STDERR "branch_from: $branch_from => ";
1630 $branch_from .= '/'.join('/', @a_path_components);
1631 print STDERR $branch_from, "\n";
1633 my $r = $i->{copyfrom_rev};
1634 my $repos_root = $self->ra->{repos_root};
1635 my $url = $self->ra->{url};
1636 my $new_url = $repos_root . $branch_from;
1637 print STDERR "Found possible branch point: ",
1638 "$new_url => ", $self->full_url, ", $r\n";
1639 $branch_from =~ s#^/##;
1640 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
1641 unless ($gs) {
1642 my $ref_id = $self->{ref_id};
1643 $ref_id =~ s/\@\d+$//;
1644 $ref_id .= "\@$r";
1645 # just grow a tail if we're not unique enough :x
1646 $ref_id .= '-' while find_ref($ref_id);
1647 print STDERR "Initializing parent: $ref_id\n";
1648 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
1650 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1651 if (!defined $r0 || !defined $parent) {
1652 $gs->fetch(0, $r);
1653 ($r0, $parent) = $gs->last_rev_commit;
1655 if (defined $r0 && defined $parent) {
1656 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
1657 $self->assert_index_clean($parent);
1658 my $ed;
1659 if ($self->ra->can_do_switch) {
1660 print STDERR "Following parent with do_switch\n";
1661 # do_switch works with svn/trunk >= r22312, but that
1662 # is not included with SVN 1.4.3 (the latest version
1663 # at the moment), so we can't rely on it
1664 $self->{last_commit} = $parent;
1665 $ed = SVN::Git::Fetcher->new($self);
1666 $gs->ra->gs_do_switch($r0, $rev, $gs,
1667 $self->full_url, $ed)
1668 or die "SVN connection failed somewhere...\n";
1669 } else {
1670 print STDERR "Following parent with do_update\n";
1671 $ed = SVN::Git::Fetcher->new($self);
1672 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1673 or die "SVN connection failed somewhere...\n";
1675 print STDERR "Successfully followed parent\n";
1676 return $self->make_log_entry($rev, [$parent], $ed);
1678 return undef;
1681 sub do_fetch {
1682 my ($self, $paths, $rev) = @_;
1683 my $ed;
1684 my ($last_rev, @parents);
1685 if (my $lc = $self->last_commit) {
1686 # we can have a branch that was deleted, then re-added
1687 # under the same name but copied from another path, in
1688 # which case we'll have multiple parents (we don't
1689 # want to break the original ref, nor lose copypath info):
1690 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1691 push @{$log_entry->{parents}}, $lc;
1692 return $log_entry;
1694 $ed = SVN::Git::Fetcher->new($self);
1695 $last_rev = $self->{last_rev};
1696 $ed->{c} = $lc;
1697 @parents = ($lc);
1698 } else {
1699 $last_rev = $rev;
1700 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1701 return $log_entry;
1703 $ed = SVN::Git::Fetcher->new($self);
1705 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1706 die "SVN connection failed somewhere...\n";
1708 $self->make_log_entry($rev, \@parents, $ed);
1711 sub get_untracked {
1712 my ($self, $ed) = @_;
1713 my @out;
1714 my $h = $ed->{empty};
1715 foreach (sort keys %$h) {
1716 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
1717 push @out, " $act: " . uri_encode($_);
1718 warn "W: $act: $_\n";
1720 foreach my $t (qw/dir_prop file_prop/) {
1721 $h = $ed->{$t} or next;
1722 foreach my $path (sort keys %$h) {
1723 my $ppath = $path eq '' ? '.' : $path;
1724 foreach my $prop (sort keys %{$h->{$path}}) {
1725 next if $SKIP_PROP{$prop};
1726 my $v = $h->{$path}->{$prop};
1727 my $t_ppath_prop = "$t: " .
1728 uri_encode($ppath) . ' ' .
1729 uri_encode($prop);
1730 if (defined $v) {
1731 push @out, " +$t_ppath_prop " .
1732 uri_encode($v);
1733 } else {
1734 push @out, " -$t_ppath_prop";
1739 foreach my $t (qw/absent_file absent_directory/) {
1740 $h = $ed->{$t} or next;
1741 foreach my $parent (sort keys %$h) {
1742 foreach my $path (sort @{$h->{$parent}}) {
1743 push @out, " $t: " .
1744 uri_encode("$parent/$path");
1745 warn "W: $t: $parent/$path ",
1746 "Insufficient permissions?\n";
1750 \@out;
1753 sub parse_svn_date {
1754 my $date = shift || return '+0000 1970-01-01 00:00:00';
1755 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1756 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
1757 croak "Unable to parse date: $date\n";
1758 "+0000 $Y-$m-$d $H:$M:$S";
1761 sub check_author {
1762 my ($author) = @_;
1763 if (!defined $author || length $author == 0) {
1764 $author = '(no author)';
1766 if (defined $::_authors && ! defined $::users{$author}) {
1767 die "Author: $author not defined in $::_authors file\n";
1769 $author;
1772 sub make_log_entry {
1773 my ($self, $rev, $parents, $ed) = @_;
1774 my $untracked = $self->get_untracked($ed);
1776 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
1777 print $un "r$rev\n" or croak $!;
1778 print $un $_, "\n" foreach @$untracked;
1779 my %log_entry = ( parents => $parents || [], revision => $rev,
1780 log => '');
1782 my $headrev;
1783 my $logged = delete $self->{logged_rev_props};
1784 if (!$logged || $self->{-want_revprops}) {
1785 my $rp = $self->ra->rev_proplist($rev);
1786 foreach (sort keys %$rp) {
1787 my $v = $rp->{$_};
1788 if (/^svn:(author|date|log)$/) {
1789 $log_entry{$1} = $v;
1790 } elsif ($_ eq 'svm:headrev') {
1791 $headrev = $v;
1792 } else {
1793 print $un " rev_prop: ", uri_encode($_), ' ',
1794 uri_encode($v), "\n";
1797 } else {
1798 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1800 close $un or croak $!;
1802 $log_entry{date} = parse_svn_date($log_entry{date});
1803 $log_entry{log} .= "\n";
1804 my $author = $log_entry{author} = check_author($log_entry{author});
1805 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
1806 : ($author, undef);
1807 if (defined $headrev && $self->use_svm_props) {
1808 if ($self->rewrite_root) {
1809 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1810 "options set!\n";
1812 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
1813 # we don't want "SVM: initializing mirror for junk" ...
1814 return undef if $r == 0;
1815 my $svm = $self->svm;
1816 if ($uuid ne $svm->{uuid}) {
1817 die "UUID mismatch on SVM path:\n",
1818 "expected: $svm->{uuid}\n",
1819 " got: $uuid\n";
1821 my $full_url = $self->full_url;
1822 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1823 die "Failed to replace '$svm->{replace}' with ",
1824 "'$svm->{source}' in $full_url\n";
1825 $log_entry{metadata} = "$full_url\@$r $uuid";
1826 $log_entry{svm_revision} = $r;
1827 $email ||= "$author\@$uuid"
1828 } elsif ($self->use_svnsync_props) {
1829 my $full_url = $self->svnsync->{url};
1830 $full_url .= "/$self->{path}" if length $self->{path};
1831 my $uuid = $self->svnsync->{uuid};
1832 $log_entry{metadata} = "$full_url\@$rev $uuid";
1833 $email ||= "$author\@$uuid"
1834 } else {
1835 $log_entry{metadata} = $self->metadata_url. "\@$rev " .
1836 $self->ra->get_uuid;
1837 $email ||= "$author\@" . $self->ra->get_uuid;
1839 $log_entry{name} = $name;
1840 $log_entry{email} = $email;
1841 \%log_entry;
1844 sub fetch {
1845 my ($self, $min_rev, $max_rev, @parents) = @_;
1846 my ($last_rev, $last_commit) = $self->last_rev_commit;
1847 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1848 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1851 sub set_tree_cb {
1852 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1853 $self->{inject_parents} = { $rev => $tree };
1854 $self->fetch(undef, undef);
1857 sub set_tree {
1858 my ($self, $tree) = (shift, shift);
1859 my $log_entry = ::get_commit_entry($tree);
1860 unless ($self->{last_rev}) {
1861 fatal("Must have an existing revision to commit\n");
1863 my %ed_opts = ( r => $self->{last_rev},
1864 log => $log_entry->{log},
1865 ra => $self->ra,
1866 tree_a => $self->{last_commit},
1867 tree_b => $tree,
1868 editor_cb => sub {
1869 $self->set_tree_cb($log_entry, $tree, @_) },
1870 svn_path => $self->{path} );
1871 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
1872 print "No changes\nr$self->{last_rev} = $tree\n";
1876 sub rebuild {
1877 my ($self) = @_;
1878 my $db_path = $self->db_path;
1879 return if (-e $db_path && ! -z $db_path);
1880 return unless ::verify_ref($self->refname.'^0');
1881 if (-f $self->{db_root}) {
1882 rename $self->{db_root}, $db_path or die
1883 "rename $self->{db_root} => $db_path failed: $!\n";
1884 my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#);
1885 symlink $base, $self->{db_root} or die
1886 "symlink $base => $self->{db_root} failed: $!\n";
1887 return;
1889 print "Rebuilding $db_path ...\n";
1890 my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
1891 my $latest;
1892 my $full_url = $self->full_url;
1893 my $svn_uuid;
1894 while (<$rev_list>) {
1895 chomp;
1896 my $c = $_;
1897 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1898 my ($url, $rev, $uuid) = ::cmt_metadata($c);
1900 # ignore merges (from set-tree)
1901 next if (!defined $rev || !$uuid);
1903 # if we merged or otherwise started elsewhere, this is
1904 # how we break out of it
1905 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1906 ($full_url && $url && ($url ne $full_url))) {
1907 next;
1909 $latest ||= $rev;
1910 $svn_uuid ||= $uuid;
1912 $self->rev_db_set($rev, $c);
1913 print "r$rev = $c\n";
1915 command_close_pipe($rev_list, $ctx);
1916 print "Done rebuilding $db_path\n";
1919 # rev_db:
1920 # Tie::File seems to be prone to offset errors if revisions get sparse,
1921 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1922 # one of my favorite modules is out :< Next up would be one of the DBM
1923 # modules, but I'm not sure which is most portable... So I'll just
1924 # go with something that's plain-text, but still capable of
1925 # being randomly accessed. So here's my ultra-simple fixed-width
1926 # database. All records are 40 characters + "\n", so it's easy to seek
1927 # to a revision: (41 * rev) is the byte offset.
1928 # A record of 40 0s denotes an empty revision.
1929 # And yes, it's still pretty fast (faster than Tie::File).
1930 # These files are disposable unless noMetadata or useSvmProps is set
1932 sub _rev_db_set {
1933 my ($fh, $rev, $commit) = @_;
1934 my $offset = $rev * 41;
1935 # assume that append is the common case:
1936 seek $fh, 0, 2 or croak $!;
1937 my $pos = tell $fh;
1938 if ($pos < $offset) {
1939 for (1 .. (($offset - $pos) / 41)) {
1940 print $fh (('0' x 40),"\n") or croak $!;
1943 seek $fh, $offset, 0 or croak $!;
1944 print $fh $commit,"\n" or croak $!;
1947 sub mkfile {
1948 my ($path) = @_;
1949 unless (-e $path) {
1950 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
1951 mkpath([$dir]) unless -d $dir;
1952 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1953 close $fh or die "Couldn't close (create) $path: $!\n";
1957 sub rev_db_set {
1958 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1959 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1960 my $db = $self->db_path($uuid);
1961 my $db_lock = "$db.lock";
1962 my $sig;
1963 if ($update_ref) {
1964 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1965 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
1967 mkfile($db);
1969 $LOCKFILES{$db_lock} = 1;
1970 my $sync;
1971 # both of these options make our .rev_db file very, very important
1972 # and we can't afford to lose it because rebuild() won't work
1973 if ($self->use_svm_props || $self->no_metadata) {
1974 $sync = 1;
1975 copy($db, $db_lock) or die "rev_db_set(@_): ",
1976 "Failed to copy: ",
1977 "$db => $db_lock ($!)\n";
1978 } else {
1979 rename $db, $db_lock or die "rev_db_set(@_): ",
1980 "Failed to rename: ",
1981 "$db => $db_lock ($!)\n";
1983 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
1984 _rev_db_set($fh, $rev, $commit);
1985 if ($sync) {
1986 $fh->flush or die "Couldn't flush $db_lock: $!\n";
1987 $fh->sync or die "Couldn't sync $db_lock: $!\n";
1989 close $fh or croak $!;
1990 if ($update_ref) {
1991 $_head = $self;
1992 command_noisy('update-ref', '-m', "r$rev",
1993 $self->refname, $commit);
1995 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
1996 "$db_lock => $db ($!)\n";
1997 delete $LOCKFILES{$db_lock};
1998 if ($update_ref) {
1999 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2000 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2001 kill $sig, $$ if defined $sig;
2005 sub rev_db_max {
2006 my ($self) = @_;
2007 $self->rebuild;
2008 my $db_path = $self->db_path;
2009 my @stat = stat $db_path or return 0;
2010 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
2011 my $max = $stat[7] / 41;
2012 (($max > 0) ? $max - 1 : 0);
2015 sub rev_db_get {
2016 my ($self, $rev, $uuid) = @_;
2017 my $ret;
2018 my $offset = $rev * 41;
2019 my $db_path = $self->db_path($uuid);
2020 return undef unless -e $db_path;
2021 open my $fh, '<', $db_path or croak $!;
2022 if (sysseek($fh, $offset, 0) == $offset) {
2023 my $read = sysread($fh, $ret, 40);
2024 $ret = undef if ($read != 40 || $ret eq ('0'x40));
2026 close $fh or croak $!;
2027 $ret;
2030 sub find_rev_before {
2031 my ($self, $rev, $eq_ok) = @_;
2032 --$rev unless $eq_ok;
2033 while ($rev > 0) {
2034 if (my $c = $self->rev_db_get($rev)) {
2035 return ($rev, $c);
2037 --$rev;
2039 return (undef, undef);
2042 sub _new {
2043 my ($class, $repo_id, $ref_id, $path) = @_;
2044 unless (defined $repo_id && length $repo_id) {
2045 $repo_id = $Git::SVN::default_repo_id;
2047 unless (defined $ref_id && length $ref_id) {
2048 $_[2] = $ref_id = $Git::SVN::default_ref_id;
2050 $_[1] = $repo_id = sanitize_remote_name($repo_id);
2051 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2052 $_[3] = $path = '' unless (defined $path);
2053 mkpath(["$ENV{GIT_DIR}/svn"]);
2054 bless {
2055 ref_id => $ref_id, dir => $dir, index => "$dir/index",
2056 path => $path, config => "$ENV{GIT_DIR}/svn/config",
2057 db_root => "$dir/.rev_db", repo_id => $repo_id }, $class;
2060 sub db_path {
2061 my ($self, $uuid) = @_;
2062 $uuid ||= $self->ra_uuid;
2063 "$self->{db_root}.$uuid";
2066 sub uri_encode {
2067 my ($f) = @_;
2068 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2072 package Git::SVN::Prompt;
2073 use strict;
2074 use warnings;
2075 require SVN::Core;
2076 use vars qw/$_no_auth_cache $_username/;
2078 sub simple {
2079 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2080 $may_save = undef if $_no_auth_cache;
2081 $default_username = $_username if defined $_username;
2082 if (defined $default_username && length $default_username) {
2083 if (defined $realm && length $realm) {
2084 print STDERR "Authentication realm: $realm\n";
2085 STDERR->flush;
2087 $cred->username($default_username);
2088 } else {
2089 username($cred, $realm, $may_save, $pool);
2091 $cred->password(_read_password("Password for '" .
2092 $cred->username . "': ", $realm));
2093 $cred->may_save($may_save);
2094 $SVN::_Core::SVN_NO_ERROR;
2097 sub ssl_server_trust {
2098 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2099 $may_save = undef if $_no_auth_cache;
2100 print STDERR "Error validating server certificate for '$realm':\n";
2101 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
2102 print STDERR " - The certificate is not issued by a trusted ",
2103 "authority. Use the\n",
2104 " fingerprint to validate the certificate manually!\n";
2106 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
2107 print STDERR " - The certificate hostname does not match.\n";
2109 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
2110 print STDERR " - The certificate is not yet valid.\n";
2112 if ($failures & $SVN::Auth::SSL::EXPIRED) {
2113 print STDERR " - The certificate has expired.\n";
2115 if ($failures & $SVN::Auth::SSL::OTHER) {
2116 print STDERR " - The certificate has an unknown error.\n";
2118 printf STDERR
2119 "Certificate information:\n".
2120 " - Hostname: %s\n".
2121 " - Valid: from %s until %s\n".
2122 " - Issuer: %s\n".
2123 " - Fingerprint: %s\n",
2124 map $cert_info->$_, qw(hostname valid_from valid_until
2125 issuer_dname fingerprint);
2126 my $choice;
2127 prompt:
2128 print STDERR $may_save ?
2129 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2130 "(R)eject or accept (t)emporarily? ";
2131 STDERR->flush;
2132 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2133 if ($choice =~ /^t$/i) {
2134 $cred->may_save(undef);
2135 } elsif ($choice =~ /^r$/i) {
2136 return -1;
2137 } elsif ($may_save && $choice =~ /^p$/i) {
2138 $cred->may_save($may_save);
2139 } else {
2140 goto prompt;
2142 $cred->accepted_failures($failures);
2143 $SVN::_Core::SVN_NO_ERROR;
2146 sub ssl_client_cert {
2147 my ($cred, $realm, $may_save, $pool) = @_;
2148 $may_save = undef if $_no_auth_cache;
2149 print STDERR "Client certificate filename: ";
2150 STDERR->flush;
2151 chomp(my $filename = <STDIN>);
2152 $cred->cert_file($filename);
2153 $cred->may_save($may_save);
2154 $SVN::_Core::SVN_NO_ERROR;
2157 sub ssl_client_cert_pw {
2158 my ($cred, $realm, $may_save, $pool) = @_;
2159 $may_save = undef if $_no_auth_cache;
2160 $cred->password(_read_password("Password: ", $realm));
2161 $cred->may_save($may_save);
2162 $SVN::_Core::SVN_NO_ERROR;
2165 sub username {
2166 my ($cred, $realm, $may_save, $pool) = @_;
2167 $may_save = undef if $_no_auth_cache;
2168 if (defined $realm && length $realm) {
2169 print STDERR "Authentication realm: $realm\n";
2171 my $username;
2172 if (defined $_username) {
2173 $username = $_username;
2174 } else {
2175 print STDERR "Username: ";
2176 STDERR->flush;
2177 chomp($username = <STDIN>);
2179 $cred->username($username);
2180 $cred->may_save($may_save);
2181 $SVN::_Core::SVN_NO_ERROR;
2184 sub _read_password {
2185 my ($prompt, $realm) = @_;
2186 print STDERR $prompt;
2187 STDERR->flush;
2188 require Term::ReadKey;
2189 Term::ReadKey::ReadMode('noecho');
2190 my $password = '';
2191 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2192 last if $key =~ /[\012\015]/; # \n\r
2193 $password .= $key;
2195 Term::ReadKey::ReadMode('restore');
2196 print STDERR "\n";
2197 STDERR->flush;
2198 $password;
2201 package main;
2204 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2205 $SVN::Node::dir.$SVN::Node::unknown.
2206 $SVN::Node::none.$SVN::Node::file.
2207 $SVN::Node::dir.$SVN::Node::unknown.
2208 $SVN::Auth::SSL::CNMISMATCH.
2209 $SVN::Auth::SSL::NOTYETVALID.
2210 $SVN::Auth::SSL::EXPIRED.
2211 $SVN::Auth::SSL::UNKNOWNCA.
2212 $SVN::Auth::SSL::OTHER;
2215 package SVN::Git::Fetcher;
2216 use vars qw/@ISA/;
2217 use strict;
2218 use warnings;
2219 use Carp qw/croak/;
2220 use IO::File qw//;
2221 use Digest::MD5;
2223 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
2224 sub new {
2225 my ($class, $git_svn) = @_;
2226 my $self = SVN::Delta::Editor->new;
2227 bless $self, $class;
2228 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
2229 $self->{empty} = {};
2230 $self->{dir_prop} = {};
2231 $self->{file_prop} = {};
2232 $self->{absent_dir} = {};
2233 $self->{absent_file} = {};
2234 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
2235 $self;
2238 sub set_path_strip {
2239 my ($self, $path) = @_;
2240 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
2243 sub open_root {
2244 { path => '' };
2247 sub open_directory {
2248 my ($self, $path, $pb, $rev) = @_;
2249 { path => $path };
2252 sub git_path {
2253 my ($self, $path) = @_;
2254 if ($self->{path_strip}) {
2255 $path =~ s!$self->{path_strip}!! or
2256 die "Failed to strip path '$path' ($self->{path_strip})\n";
2258 $path;
2261 sub delete_entry {
2262 my ($self, $path, $rev, $pb) = @_;
2264 my $gpath = $self->git_path($path);
2265 return undef if ($gpath eq '');
2267 # remove entire directories.
2268 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
2269 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2270 -r --name-only -z/,
2271 $self->{c}, '--', $gpath);
2272 local $/ = "\0";
2273 while (<$ls>) {
2274 chomp;
2275 $self->{gii}->remove($_);
2276 print "\tD\t$_\n" unless $::_q;
2278 print "\tD\t$gpath/\n" unless $::_q;
2279 command_close_pipe($ls, $ctx);
2280 $self->{empty}->{$path} = 0
2281 } else {
2282 $self->{gii}->remove($gpath);
2283 print "\tD\t$gpath\n" unless $::_q;
2285 undef;
2288 sub open_file {
2289 my ($self, $path, $pb, $rev) = @_;
2290 my $gpath = $self->git_path($path);
2291 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
2292 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
2293 unless (defined $mode && defined $blob) {
2294 die "$path was not found in commit $self->{c} (r$rev)\n";
2296 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
2297 pool => SVN::Pool->new, action => 'M' };
2300 sub add_file {
2301 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
2302 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2303 delete $self->{empty}->{$dir};
2304 { path => $path, mode_a => 100644, mode_b => 100644,
2305 pool => SVN::Pool->new, action => 'A' };
2308 sub add_directory {
2309 my ($self, $path, $cp_path, $cp_rev) = @_;
2310 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2311 delete $self->{empty}->{$dir};
2312 $self->{empty}->{$path} = 1;
2313 { path => $path };
2316 sub change_dir_prop {
2317 my ($self, $db, $prop, $value) = @_;
2318 $self->{dir_prop}->{$db->{path}} ||= {};
2319 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2320 undef;
2323 sub absent_directory {
2324 my ($self, $path, $pb) = @_;
2325 $self->{absent_dir}->{$pb->{path}} ||= [];
2326 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2327 undef;
2330 sub absent_file {
2331 my ($self, $path, $pb) = @_;
2332 $self->{absent_file}->{$pb->{path}} ||= [];
2333 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2334 undef;
2337 sub change_file_prop {
2338 my ($self, $fb, $prop, $value) = @_;
2339 if ($prop eq 'svn:executable') {
2340 if ($fb->{mode_b} != 120000) {
2341 $fb->{mode_b} = defined $value ? 100755 : 100644;
2343 } elsif ($prop eq 'svn:special') {
2344 $fb->{mode_b} = defined $value ? 120000 : 100644;
2345 } else {
2346 $self->{file_prop}->{$fb->{path}} ||= {};
2347 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
2349 undef;
2352 sub apply_textdelta {
2353 my ($self, $fb, $exp) = @_;
2354 my $fh = IO::File->new_tmpfile;
2355 $fh->autoflush(1);
2356 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2357 # (but $base does not,) so dup() it for reading in close_file
2358 open my $dup, '<&', $fh or croak $!;
2359 my $base = IO::File->new_tmpfile;
2360 $base->autoflush(1);
2361 if ($fb->{blob}) {
2362 defined (my $pid = fork) or croak $!;
2363 if (!$pid) {
2364 open STDOUT, '>&', $base or croak $!;
2365 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2366 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2368 waitpid $pid, 0;
2369 croak $? if $?;
2371 if (defined $exp) {
2372 seek $base, 0, 0 or croak $!;
2373 my $md5 = Digest::MD5->new;
2374 $md5->addfile($base);
2375 my $got = $md5->hexdigest;
2376 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2377 "expected: $exp\n",
2378 " got: $got\n" if ($got ne $exp);
2381 seek $base, 0, 0 or croak $!;
2382 $fb->{fh} = $dup;
2383 $fb->{base} = $base;
2384 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2387 sub close_file {
2388 my ($self, $fb, $exp) = @_;
2389 my $hash;
2390 my $path = $self->git_path($fb->{path});
2391 if (my $fh = $fb->{fh}) {
2392 seek($fh, 0, 0) or croak $!;
2393 my $md5 = Digest::MD5->new;
2394 $md5->addfile($fh);
2395 my $got = $md5->hexdigest;
2396 die "Checksum mismatch: $path\n",
2397 "expected: $exp\n got: $got\n" if ($got ne $exp);
2398 seek($fh, 0, 0) or croak $!;
2399 if ($fb->{mode_b} == 120000) {
2400 read($fh, my $buf, 5) == 5 or croak $!;
2401 $buf eq 'link ' or die "$path has mode 120000",
2402 "but is not a link\n";
2404 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2405 if (!$pid) {
2406 open STDIN, '<&', $fh or croak $!;
2407 exec qw/git-hash-object -w --stdin/ or croak $!;
2409 chomp($hash = do { local $/; <$out> });
2410 close $out or croak $!;
2411 close $fh or croak $!;
2412 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2413 close $fb->{base} or croak $!;
2414 } else {
2415 $hash = $fb->{blob} or die "no blob information\n";
2417 $fb->{pool}->clear;
2418 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
2419 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
2420 undef;
2423 sub abort_edit {
2424 my $self = shift;
2425 $self->{nr} = $self->{gii}->{nr};
2426 delete $self->{gii};
2427 $self->SUPER::abort_edit(@_);
2430 sub close_edit {
2431 my $self = shift;
2432 $self->{git_commit_ok} = 1;
2433 $self->{nr} = $self->{gii}->{nr};
2434 delete $self->{gii};
2435 $self->SUPER::close_edit(@_);
2438 package SVN::Git::Editor;
2439 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
2440 use strict;
2441 use warnings;
2442 use Carp qw/croak/;
2443 use IO::File;
2444 use Digest::MD5;
2446 sub new {
2447 my ($class, $opts) = @_;
2448 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
2449 die "$_ required!\n" unless (defined $opts->{$_});
2452 my $pool = SVN::Pool->new;
2453 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
2454 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
2455 $opts->{r}, $mods);
2457 # $opts->{ra} functions should not be used after this:
2458 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
2459 $opts->{editor_cb}, $pool);
2460 my $self = SVN::Delta::Editor->new(@ce, $pool);
2461 bless $self, $class;
2462 foreach (qw/svn_path r tree_a tree_b/) {
2463 $self->{$_} = $opts->{$_};
2465 $self->{url} = $opts->{ra}->{url};
2466 $self->{mods} = $mods;
2467 $self->{types} = $types;
2468 $self->{pool} = $pool;
2469 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2470 $self->{rm} = { };
2471 $self->{path_prefix} = length $self->{svn_path} ?
2472 "$self->{svn_path}/" : '';
2473 return $self;
2476 sub generate_diff {
2477 my ($tree_a, $tree_b) = @_;
2478 my @diff_tree = qw(diff-tree -z -r);
2479 if ($_cp_similarity) {
2480 push @diff_tree, "-C$_cp_similarity";
2481 } else {
2482 push @diff_tree, '-C';
2484 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2485 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
2486 push @diff_tree, $tree_a, $tree_b;
2487 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
2488 local $/ = "\0";
2489 my $state = 'meta';
2490 my @mods;
2491 while (<$diff_fh>) {
2492 chomp $_; # this gets rid of the trailing "\0"
2493 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
2494 $::sha1\s($::sha1)\s
2495 ([MTCRAD])\d*$/xo) {
2496 push @mods, { mode_a => $1, mode_b => $2,
2497 sha1_b => $3, chg => $4 };
2498 if ($4 =~ /^(?:C|R)$/) {
2499 $state = 'file_a';
2500 } else {
2501 $state = 'file_b';
2503 } elsif ($state eq 'file_a') {
2504 my $x = $mods[$#mods] or croak "Empty array\n";
2505 if ($x->{chg} !~ /^(?:C|R)$/) {
2506 croak "Error parsing $_, $x->{chg}\n";
2508 $x->{file_a} = $_;
2509 $state = 'file_b';
2510 } elsif ($state eq 'file_b') {
2511 my $x = $mods[$#mods] or croak "Empty array\n";
2512 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2513 croak "Error parsing $_, $x->{chg}\n";
2515 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2516 croak "Error parsing $_, $x->{chg}\n";
2518 $x->{file_b} = $_;
2519 $state = 'meta';
2520 } else {
2521 croak "Error parsing $_\n";
2524 command_close_pipe($diff_fh, $ctx);
2525 \@mods;
2528 sub check_diff_paths {
2529 my ($ra, $pfx, $rev, $mods) = @_;
2530 my %types;
2531 $pfx .= '/' if length $pfx;
2533 sub type_diff_paths {
2534 my ($ra, $types, $path, $rev) = @_;
2535 my @p = split m#/+#, $path;
2536 my $c = shift @p;
2537 unless (defined $types->{$c}) {
2538 $types->{$c} = $ra->check_path($c, $rev);
2540 while (@p) {
2541 $c .= '/' . shift @p;
2542 next if defined $types->{$c};
2543 $types->{$c} = $ra->check_path($c, $rev);
2547 foreach my $m (@$mods) {
2548 foreach my $f (qw/file_a file_b/) {
2549 next unless defined $m->{$f};
2550 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
2551 if (length $pfx.$dir && ! defined $types{$dir}) {
2552 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
2556 \%types;
2559 sub split_path {
2560 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2563 sub repo_path {
2564 my ($self, $path) = @_;
2565 $self->{path_prefix}.(defined $path ? $path : '');
2568 sub url_path {
2569 my ($self, $path) = @_;
2570 $self->{url} . '/' . $self->repo_path($path);
2573 sub rmdirs {
2574 my ($self) = @_;
2575 my $rm = $self->{rm};
2576 delete $rm->{''}; # we never delete the url we're tracking
2577 return unless %$rm;
2579 foreach (keys %$rm) {
2580 my @d = split m#/#, $_;
2581 my $c = shift @d;
2582 $rm->{$c} = 1;
2583 while (@d) {
2584 $c .= '/' . shift @d;
2585 $rm->{$c} = 1;
2588 delete $rm->{$self->{svn_path}};
2589 delete $rm->{''}; # we never delete the url we're tracking
2590 return unless %$rm;
2592 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2593 $self->{tree_b});
2594 local $/ = "\0";
2595 while (<$fh>) {
2596 chomp;
2597 my @dn = split m#/#, $_;
2598 while (pop @dn) {
2599 delete $rm->{join '/', @dn};
2601 unless (%$rm) {
2602 close $fh;
2603 return;
2606 command_close_pipe($fh, $ctx);
2608 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2609 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2610 $self->close_directory($bat->{$d}, $p);
2611 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
2612 print "\tD+\t$d/\n" unless $::_q;
2613 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2614 delete $bat->{$d};
2618 sub open_or_add_dir {
2619 my ($self, $full_path, $baton) = @_;
2620 my $t = $self->{types}->{$full_path};
2621 if (!defined $t) {
2622 die "$full_path not known in r$self->{r} or we have a bug!\n";
2624 if ($t == $SVN::Node::none) {
2625 return $self->add_directory($full_path, $baton,
2626 undef, -1, $self->{pool});
2627 } elsif ($t == $SVN::Node::dir) {
2628 return $self->open_directory($full_path, $baton,
2629 $self->{r}, $self->{pool});
2631 print STDERR "$full_path already exists in repository at ",
2632 "r$self->{r} and it is not a directory (",
2633 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2634 exit 1;
2637 sub ensure_path {
2638 my ($self, $path) = @_;
2639 my $bat = $self->{bat};
2640 my $repo_path = $self->repo_path($path);
2641 return $bat->{''} unless (length $repo_path);
2642 my @p = split m#/+#, $repo_path;
2643 my $c = shift @p;
2644 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2645 while (@p) {
2646 my $c0 = $c;
2647 $c .= '/' . shift @p;
2648 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2650 return $bat->{$c};
2653 sub A {
2654 my ($self, $m) = @_;
2655 my ($dir, $file) = split_path($m->{file_b});
2656 my $pbat = $self->ensure_path($dir);
2657 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2658 undef, -1);
2659 print "\tA\t$m->{file_b}\n" unless $::_q;
2660 $self->chg_file($fbat, $m);
2661 $self->close_file($fbat,undef,$self->{pool});
2664 sub C {
2665 my ($self, $m) = @_;
2666 my ($dir, $file) = split_path($m->{file_b});
2667 my $pbat = $self->ensure_path($dir);
2668 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2669 $self->url_path($m->{file_a}), $self->{r});
2670 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
2671 $self->chg_file($fbat, $m);
2672 $self->close_file($fbat,undef,$self->{pool});
2675 sub delete_entry {
2676 my ($self, $path, $pbat) = @_;
2677 my $rpath = $self->repo_path($path);
2678 my ($dir, $file) = split_path($rpath);
2679 $self->{rm}->{$dir} = 1;
2680 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2683 sub R {
2684 my ($self, $m) = @_;
2685 my ($dir, $file) = split_path($m->{file_b});
2686 my $pbat = $self->ensure_path($dir);
2687 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2688 $self->url_path($m->{file_a}), $self->{r});
2689 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
2690 $self->chg_file($fbat, $m);
2691 $self->close_file($fbat,undef,$self->{pool});
2693 ($dir, $file) = split_path($m->{file_a});
2694 $pbat = $self->ensure_path($dir);
2695 $self->delete_entry($m->{file_a}, $pbat);
2698 sub M {
2699 my ($self, $m) = @_;
2700 my ($dir, $file) = split_path($m->{file_b});
2701 my $pbat = $self->ensure_path($dir);
2702 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2703 $pbat,$self->{r},$self->{pool});
2704 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
2705 $self->chg_file($fbat, $m);
2706 $self->close_file($fbat,undef,$self->{pool});
2709 sub T { shift->M(@_) }
2711 sub change_file_prop {
2712 my ($self, $fbat, $pname, $pval) = @_;
2713 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2716 sub chg_file {
2717 my ($self, $fbat, $m) = @_;
2718 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2719 $self->change_file_prop($fbat,'svn:executable','*');
2720 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2721 $self->change_file_prop($fbat,'svn:executable',undef);
2723 my $fh = IO::File->new_tmpfile or croak $!;
2724 if ($m->{mode_b} =~ /^120/) {
2725 print $fh 'link ' or croak $!;
2726 $self->change_file_prop($fbat,'svn:special','*');
2727 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2728 $self->change_file_prop($fbat,'svn:special',undef);
2730 defined(my $pid = fork) or croak $!;
2731 if (!$pid) {
2732 open STDOUT, '>&', $fh or croak $!;
2733 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2735 waitpid $pid, 0;
2736 croak $? if $?;
2737 $fh->flush == 0 or croak $!;
2738 seek $fh, 0, 0 or croak $!;
2740 my $md5 = Digest::MD5->new;
2741 $md5->addfile($fh) or croak $!;
2742 seek $fh, 0, 0 or croak $!;
2744 my $exp = $md5->hexdigest;
2745 my $pool = SVN::Pool->new;
2746 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2747 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
2748 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2749 $pool->clear;
2751 close $fh or croak $!;
2754 sub D {
2755 my ($self, $m) = @_;
2756 my ($dir, $file) = split_path($m->{file_b});
2757 my $pbat = $self->ensure_path($dir);
2758 print "\tD\t$m->{file_b}\n" unless $::_q;
2759 $self->delete_entry($m->{file_b}, $pbat);
2762 sub close_edit {
2763 my ($self) = @_;
2764 my ($p,$bat) = ($self->{pool}, $self->{bat});
2765 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2766 $self->close_directory($bat->{$_}, $p);
2768 $self->SUPER::close_edit($p);
2769 $p->clear;
2772 sub abort_edit {
2773 my ($self) = @_;
2774 $self->SUPER::abort_edit($self->{pool});
2777 sub DESTROY {
2778 my $self = shift;
2779 $self->SUPER::DESTROY(@_);
2780 $self->{pool}->clear;
2783 # this drives the editor
2784 sub apply_diff {
2785 my ($self) = @_;
2786 my $mods = $self->{mods};
2787 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
2788 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
2789 my $f = $m->{chg};
2790 if (defined $o{$f}) {
2791 $self->$f($m);
2792 } else {
2793 fatal("Invalid change type: $f\n");
2796 $self->rmdirs if $_rmdir;
2797 if (@$mods == 0) {
2798 $self->abort_edit;
2799 } else {
2800 $self->close_edit;
2802 return scalar @$mods;
2805 package Git::SVN::Ra;
2806 use vars qw/@ISA $config_dir $_log_window_size/;
2807 use strict;
2808 use warnings;
2809 my ($can_do_switch);
2810 my $RA;
2812 BEGIN {
2813 # enforce temporary pool usage for some simple functions
2814 my $e;
2815 foreach (qw/get_latest_revnum get_uuid get_repos_root/) {
2816 $e .= "sub $_ {
2817 my \$self = shift;
2818 my \$pool = SVN::Pool->new;
2819 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2820 \$pool->clear;
2821 wantarray ? \@ret : \$ret[0]; }\n";
2824 # get_dir needs $pool held in cache for dirents to work,
2825 # check_path is cacheable and rev_proplist is close enough
2826 # for our purposes.
2827 foreach (qw/check_path get_dir rev_proplist/) {
2828 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2829 my \$self = shift;
2830 my \$r = pop;
2831 my \$k = join(\"\\0\", \@_);
2832 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2833 return wantarray ? \@\$x : \$x->[0];
2835 my \$pool = SVN::Pool->new;
2836 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2837 if (\$r != \$${_}_rev) {
2838 \%${_}_cache = ( pool => [] );
2839 \$${_}_rev = \$r;
2841 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2842 push \@{\$${_}_cache{pool}}, \$pool;
2843 wantarray ? \@ret : \$ret[0]; }\n";
2845 $e .= "\n1;";
2846 eval $e or die $@;
2849 sub new {
2850 my ($class, $url) = @_;
2851 $url =~ s!/+$!!;
2852 return $RA if ($RA && $RA->{url} eq $url);
2854 SVN::_Core::svn_config_ensure($config_dir, undef);
2855 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
2856 SVN::Client::get_simple_provider(),
2857 SVN::Client::get_ssl_server_trust_file_provider(),
2858 SVN::Client::get_simple_prompt_provider(
2859 \&Git::SVN::Prompt::simple, 2),
2860 SVN::Client::get_ssl_client_cert_prompt_provider(
2861 \&Git::SVN::Prompt::ssl_client_cert, 2),
2862 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
2863 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
2864 SVN::Client::get_username_provider(),
2865 SVN::Client::get_ssl_server_trust_prompt_provider(
2866 \&Git::SVN::Prompt::ssl_server_trust),
2867 SVN::Client::get_username_prompt_provider(
2868 \&Git::SVN::Prompt::username, 2),
2870 my $config = SVN::Core::config_get_config($config_dir);
2871 my $self = SVN::Ra->new(url => $url, auth => $baton,
2872 config => $config,
2873 pool => SVN::Pool->new,
2874 auth_provider_callbacks => $callbacks);
2875 $self->{svn_path} = $url;
2876 $self->{repos_root} = $self->get_repos_root;
2877 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
2878 $RA = bless $self, $class;
2881 sub DESTROY {
2882 # do not call the real DESTROY since we store ourselves in $RA
2885 sub get_log {
2886 my ($self, @args) = @_;
2887 my $pool = SVN::Pool->new;
2888 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
2889 my $ret = $self->SUPER::get_log(@args, $pool);
2890 $pool->clear;
2891 $ret;
2894 sub get_commit_editor {
2895 my ($self, $log, $cb, $pool) = @_;
2896 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
2897 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
2900 sub gs_do_update {
2901 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2902 my $new = ($rev_a == $rev_b);
2903 my $path = $gs->{path};
2905 my $pool = SVN::Pool->new;
2906 $editor->set_path_strip($path);
2907 my (@pc) = split m#/#, $path;
2908 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
2909 1, $editor, $pool);
2910 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2912 # Since we can't rely on svn_ra_reparent being available, we'll
2913 # just have to do some magic with set_path to make it so
2914 # we only want a partial path.
2915 my $sp = '';
2916 my $final = join('/', @pc);
2917 while (@pc) {
2918 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2919 $sp .= '/' if length $sp;
2920 $sp .= shift @pc;
2922 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2924 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2926 $reporter->finish_report($pool);
2927 $pool->clear;
2928 $editor->{git_commit_ok};
2931 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2932 # svn_ra_reparent didn't work before 1.4)
2933 sub gs_do_switch {
2934 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2935 my $path = $gs->{path};
2936 my $pool = SVN::Pool->new;
2938 my $full_url = $self->{url};
2939 my $old_url = $full_url;
2940 $full_url .= "/$path" if length $path;
2941 my ($ra, $reparented);
2942 if ($old_url ne $full_url) {
2943 if ($old_url !~ m#^svn(\+ssh)?://#) {
2944 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
2945 $pool);
2946 $self->{url} = $full_url;
2947 $reparented = 1;
2948 } else {
2949 $ra = Git::SVN::Ra->new($full_url);
2952 $ra ||= $self;
2953 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2954 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2955 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2956 $reporter->finish_report($pool);
2958 if ($reparented) {
2959 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
2960 $self->{url} = $old_url;
2963 $pool->clear;
2964 $editor->{git_commit_ok};
2967 sub gs_fetch_loop_common {
2968 my ($self, $base, $head, $gsv, $globs) = @_;
2969 return if ($base > $head);
2970 my $inc = $_log_window_size;
2971 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
2972 my %common;
2973 my $common_max = scalar @$gsv;
2975 foreach my $gs (@$gsv) {
2976 my @tmp = split m#/#, $gs->{path};
2977 my $p = '';
2978 foreach (@tmp) {
2979 $p .= length($p) ? "/$_" : $_;
2980 $common{$p} ||= 0;
2981 $common{$p}++;
2984 $globs ||= [];
2985 $common_max += scalar @$globs;
2986 foreach my $glob (@$globs) {
2987 my @tmp = split m#/#, $glob->{path}->{left};
2988 my $p = '';
2989 foreach (@tmp) {
2990 $p .= length($p) ? "/$_" : $_;
2991 $common{$p} ||= 0;
2992 $common{$p}++;
2996 my $longest_path = '';
2997 foreach (sort {length $b <=> length $a} keys %common) {
2998 if ($common{$_} == $common_max) {
2999 $longest_path = $_;
3000 last;
3003 while (1) {
3004 my %revs;
3005 my $err;
3006 my $err_handler = $SVN::Error::handler;
3007 $SVN::Error::handler = sub {
3008 ($err) = @_;
3009 skip_unknown_revs($err);
3011 sub _cb {
3012 my ($paths, $r, $author, $date, $log) = @_;
3013 [ dup_changed_paths($paths),
3014 { author => $author, date => $date, log => $log } ];
3016 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3017 sub { $revs{$_[1]} = _cb(@_) });
3018 if ($err && $max >= $head) {
3019 print STDERR "Path '$longest_path' ",
3020 "was probably deleted:\n",
3021 $err->expanded_message,
3022 "\nWill attempt to follow ",
3023 "revisions r$min .. r$max ",
3024 "committed before the deletion\n";
3025 my $hi = $max;
3026 while (--$hi >= $min) {
3027 my $ok;
3028 $self->get_log([$longest_path], $min, $hi,
3029 0, 1, 1, sub {
3030 $ok ||= $_[1];
3031 $revs{$_[1]} = _cb(@_) });
3032 if ($ok) {
3033 print STDERR "r$min .. r$ok OK\n";
3034 last;
3038 $SVN::Error::handler = $err_handler;
3040 my %exists = map { $_->{path} => $_ } @$gsv;
3041 foreach my $r (sort {$a <=> $b} keys %revs) {
3042 my ($paths, $logged) = @{$revs{$r}};
3044 foreach my $gs ($self->match_globs(\%exists, $paths,
3045 $globs, $r)) {
3046 if ($gs->rev_db_max >= $r) {
3047 next;
3049 next unless $gs->match_paths($paths, $r);
3050 $gs->{logged_rev_props} = $logged;
3051 if (my $last_commit = $gs->last_commit) {
3052 $gs->assert_index_clean($last_commit);
3054 my $log_entry = $gs->do_fetch($paths, $r);
3055 if ($log_entry) {
3056 $gs->do_git_commit($log_entry);
3059 foreach my $g (@$globs) {
3060 my $k = "svn-remote.$g->{remote}." .
3061 "$g->{t}-maxRev";
3062 Git::SVN::tmp_config($k, $r);
3065 # pre-fill the .rev_db since it'll eventually get filled in
3066 # with '0' x40 if something new gets committed
3067 foreach my $gs (@$gsv) {
3068 next if defined $gs->rev_db_get($max);
3069 $gs->rev_db_set($max, 0 x40);
3071 foreach my $g (@$globs) {
3072 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3073 Git::SVN::tmp_config($k, $max);
3075 last if $max >= $head;
3076 $min = $max + 1;
3077 $max += $inc;
3078 $max = $head if ($max > $head);
3082 sub match_globs {
3083 my ($self, $exists, $paths, $globs, $r) = @_;
3085 sub get_dir_check {
3086 my ($self, $exists, $g, $r) = @_;
3087 my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
3088 return unless scalar @x == 3;
3089 my $dirents = $x[0];
3090 foreach my $de (keys %$dirents) {
3091 next if $dirents->{$de}->kind != $SVN::Node::dir;
3092 my $p = $g->{path}->full_path($de);
3093 next if $exists->{$p};
3094 next if (length $g->{path}->{right} &&
3095 ($self->check_path($p, $r) !=
3096 $SVN::Node::dir));
3097 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
3098 $g->{ref}->full_path($de), 1);
3101 foreach my $g (@$globs) {
3102 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3103 if ($path->{action} =~ /^[AR]$/) {
3104 get_dir_check($self, $exists, $g, $r);
3107 foreach (keys %$paths) {
3108 if (/$g->{path}->{left_regex}/ &&
3109 !/$g->{path}->{regex}/) {
3110 next if $paths->{$_}->{action} !~ /^[AR]$/;
3111 get_dir_check($self, $exists, $g, $r);
3113 next unless /$g->{path}->{regex}/;
3114 my $p = $1;
3115 my $pathname = $g->{path}->full_path($p);
3116 next if $exists->{$pathname};
3117 $exists->{$pathname} = Git::SVN->init(
3118 $self->{url}, $pathname, undef,
3119 $g->{ref}->full_path($p), 1);
3121 my $c = '';
3122 foreach (split m#/#, $g->{path}->{left}) {
3123 $c .= "/$_";
3124 next unless ($paths->{$c} &&
3125 ($paths->{$c}->{action} =~ /^[AR]$/));
3126 get_dir_check($self, $exists, $g, $r);
3129 values %$exists;
3132 sub minimize_url {
3133 my ($self) = @_;
3134 return $self->{url} if ($self->{url} eq $self->{repos_root});
3135 my $url = $self->{repos_root};
3136 my @components = split(m!/!, $self->{svn_path});
3137 my $c = '';
3138 do {
3139 $url .= "/$c" if length $c;
3140 eval { (ref $self)->new($url)->get_latest_revnum };
3141 } while ($@ && ($c = shift @components));
3142 $url;
3145 sub can_do_switch {
3146 my $self = shift;
3147 unless (defined $can_do_switch) {
3148 my $pool = SVN::Pool->new;
3149 my $rep = eval {
3150 $self->do_switch(1, '', 0, $self->{url},
3151 SVN::Delta::Editor->new, $pool);
3153 if ($@) {
3154 $can_do_switch = 0;
3155 } else {
3156 $rep->abort_report($pool);
3157 $can_do_switch = 1;
3159 $pool->clear;
3161 $can_do_switch;
3164 sub skip_unknown_revs {
3165 my ($err) = @_;
3166 my $errno = $err->apr_err();
3167 # Maybe the branch we're tracking didn't
3168 # exist when the repo started, so it's
3169 # not an error if it doesn't, just continue
3171 # Wonderfully consistent library, eh?
3172 # 160013 - svn:// and file://
3173 # 175002 - http(s)://
3174 # 175007 - http(s):// (this repo required authorization, too...)
3175 # More codes may be discovered later...
3176 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
3177 warn "W: Ignoring error from SVN, path probably ",
3178 "does not exist: ($errno): ",
3179 $err->expanded_message,"\n";
3180 return;
3182 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3185 # svn_log_changed_path_t objects passed to get_log are likely to be
3186 # overwritten even if only the refs are copied to an external variable,
3187 # so we should dup the structures in their entirety. Using an externally
3188 # passed pool (instead of our temporary and quickly cleared pool in
3189 # Git::SVN::Ra) does not help matters at all...
3190 sub dup_changed_paths {
3191 my ($paths) = @_;
3192 return undef unless $paths;
3193 my %ret;
3194 foreach my $p (keys %$paths) {
3195 my $i = $paths->{$p};
3196 my %s = map { $_ => $i->$_ }
3197 qw/copyfrom_path copyfrom_rev action/;
3198 $ret{$p} = \%s;
3200 \%ret;
3203 package Git::SVN::Log;
3204 use strict;
3205 use warnings;
3206 use POSIX qw/strftime/;
3207 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
3208 %rusers $show_commit $incremental/;
3209 my $l_fmt;
3211 sub cmt_showable {
3212 my ($c) = @_;
3213 return 1 if defined $c->{r};
3214 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
3215 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
3216 my @log = command(qw/cat-file commit/, $c->{c});
3217 shift @log while ($log[0] ne "\n");
3218 shift @log;
3219 @{$c->{l}} = grep !/^git-svn-id: /, @log;
3221 (undef, $c->{r}, undef) = ::extract_metadata(
3222 (grep(/^git-svn-id: /, @log))[-1]);
3224 return defined $c->{r};
3227 sub log_use_color {
3228 return 1 if $color;
3229 my ($dc, $dcvar);
3230 $dcvar = 'color.diff';
3231 $dc = `git-config --get $dcvar`;
3232 if ($dc eq '') {
3233 # nothing at all; fallback to "diff.color"
3234 $dcvar = 'diff.color';
3235 $dc = `git-config --get $dcvar`;
3237 chomp($dc);
3238 if ($dc eq 'auto') {
3239 my $pc;
3240 $pc = `git-config --get color.pager`;
3241 if ($pc eq '') {
3242 # does not have it -- fallback to pager.color
3243 $pc = `git-config --bool --get pager.color`;
3245 else {
3246 $pc = `git-config --bool --get color.pager`;
3247 if ($?) {
3248 $pc = 'false';
3251 chomp($pc);
3252 if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
3253 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
3255 return 0;
3257 return 0 if $dc eq 'never';
3258 return 1 if $dc eq 'always';
3259 chomp($dc = `git-config --bool --get $dcvar`);
3260 return ($dc eq 'true');
3263 sub git_svn_log_cmd {
3264 my ($r_min, $r_max, @args) = @_;
3265 my $head = 'HEAD';
3266 foreach my $x (@args) {
3267 last if $x eq '--';
3268 next unless ::verify_ref("$x^0");
3269 $head = $x;
3270 last;
3273 my $url = (::working_head_info($head))[0];
3274 my $gs = Git::SVN->find_by_url($url) || Git::SVN->_new;
3275 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
3276 $gs->refname);
3277 push @cmd, '-r' unless $non_recursive;
3278 push @cmd, qw/--raw --name-status/ if $verbose;
3279 push @cmd, '--color' if log_use_color();
3280 return @cmd unless defined $r_max;
3281 if ($r_max == $r_min) {
3282 push @cmd, '--max-count=1';
3283 if (my $c = $gs->rev_db_get($r_max)) {
3284 push @cmd, $c;
3286 } else {
3287 my ($c_min, $c_max);
3288 $c_max = $gs->rev_db_get($r_max);
3289 $c_min = $gs->rev_db_get($r_min);
3290 if (defined $c_min && defined $c_max) {
3291 if ($r_max > $r_max) {
3292 push @cmd, "$c_min..$c_max";
3293 } else {
3294 push @cmd, "$c_max..$c_min";
3296 } elsif ($r_max > $r_min) {
3297 push @cmd, $c_max;
3298 } else {
3299 push @cmd, $c_min;
3302 return @cmd;
3305 # adapted from pager.c
3306 sub config_pager {
3307 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
3308 if (!defined $pager) {
3309 $pager = 'less';
3310 } elsif (length $pager == 0 || $pager eq 'cat') {
3311 $pager = undef;
3315 sub run_pager {
3316 return unless -t *STDOUT;
3317 pipe my $rfd, my $wfd or return;
3318 defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
3319 if (!$pid) {
3320 open STDOUT, '>&', $wfd or
3321 ::fatal "Can't redirect to stdout: $!\n";
3322 return;
3324 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n";
3325 $ENV{LESS} ||= 'FRSX';
3326 exec $pager or ::fatal "Can't run pager: $! ($pager)\n";
3329 sub tz_to_s_offset {
3330 my ($tz) = @_;
3331 $tz =~ s/(\d\d)$//;
3332 return ($1 * 60) + ($tz * 3600);
3335 sub get_author_info {
3336 my ($dest, $author, $t, $tz) = @_;
3337 $author =~ s/(?:^\s*|\s*$)//g;
3338 $dest->{a_raw} = $author;
3339 my $au;
3340 if ($::_authors) {
3341 $au = $rusers{$author} || undef;
3343 if (!$au) {
3344 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3346 $dest->{t} = $t;
3347 $dest->{tz} = $tz;
3348 $dest->{a} = $au;
3349 # Date::Parse isn't in the standard Perl distro :(
3350 if ($tz =~ s/^\+//) {
3351 $t += tz_to_s_offset($tz);
3352 } elsif ($tz =~ s/^\-//) {
3353 $t -= tz_to_s_offset($tz);
3355 $dest->{t_utc} = $t;
3358 sub process_commit {
3359 my ($c, $r_min, $r_max, $defer) = @_;
3360 if (defined $r_min && defined $r_max) {
3361 if ($r_min == $c->{r} && $r_min == $r_max) {
3362 show_commit($c);
3363 return 0;
3365 return 1 if $r_min == $r_max;
3366 if ($r_min < $r_max) {
3367 # we need to reverse the print order
3368 return 0 if (defined $limit && --$limit < 0);
3369 push @$defer, $c;
3370 return 1;
3372 if ($r_min != $r_max) {
3373 return 1 if ($r_min < $c->{r});
3374 return 1 if ($r_max > $c->{r});
3377 return 0 if (defined $limit && --$limit < 0);
3378 show_commit($c);
3379 return 1;
3382 sub show_commit {
3383 my $c = shift;
3384 if ($oneline) {
3385 my $x = "\n";
3386 if (my $l = $c->{l}) {
3387 while ($l->[0] =~ /^\s*$/) { shift @$l }
3388 $x = $l->[0];
3390 $l_fmt ||= 'A' . length($c->{r});
3391 print 'r',pack($l_fmt, $c->{r}),' | ';
3392 print "$c->{c} | " if $show_commit;
3393 print $x;
3394 } else {
3395 show_commit_normal($c);
3399 sub show_commit_changed_paths {
3400 my ($c) = @_;
3401 return unless $c->{changed};
3402 print "Changed paths:\n", @{$c->{changed}};
3405 sub show_commit_normal {
3406 my ($c) = @_;
3407 print '-' x72, "\nr$c->{r} | ";
3408 print "$c->{c} | " if $show_commit;
3409 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3410 localtime($c->{t_utc})), ' | ';
3411 my $nr_line = 0;
3413 if (my $l = $c->{l}) {
3414 while ($l->[$#$l] eq "\n" && $#$l > 0
3415 && $l->[($#$l - 1)] eq "\n") {
3416 pop @$l;
3418 $nr_line = scalar @$l;
3419 if (!$nr_line) {
3420 print "1 line\n\n\n";
3421 } else {
3422 if ($nr_line == 1) {
3423 $nr_line = '1 line';
3424 } else {
3425 $nr_line .= ' lines';
3427 print $nr_line, "\n";
3428 show_commit_changed_paths($c);
3429 print "\n";
3430 print $_ foreach @$l;
3432 } else {
3433 print "1 line\n";
3434 show_commit_changed_paths($c);
3435 print "\n";
3438 foreach my $x (qw/raw stat diff/) {
3439 if ($c->{$x}) {
3440 print "\n";
3441 print $_ foreach @{$c->{$x}}
3446 sub cmd_show_log {
3447 my (@args) = @_;
3448 my ($r_min, $r_max);
3449 my $r_last = -1; # prevent dupes
3450 if (defined $TZ) {
3451 $ENV{TZ} = $TZ;
3452 } else {
3453 delete $ENV{TZ};
3455 if (defined $::_revision) {
3456 if ($::_revision =~ /^(\d+):(\d+)$/) {
3457 ($r_min, $r_max) = ($1, $2);
3458 } elsif ($::_revision =~ /^\d+$/) {
3459 $r_min = $r_max = $::_revision;
3460 } else {
3461 ::fatal "-r$::_revision is not supported, use ",
3462 "standard \'git log\' arguments instead\n";
3466 config_pager();
3467 @args = (git_svn_log_cmd($r_min, $r_max, @args), @args);
3468 my $log = command_output_pipe(@args);
3469 run_pager();
3470 my (@k, $c, $d, $stat);
3471 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3472 while (<$log>) {
3473 if (/^${esc_color}commit ($::sha1_short)/o) {
3474 my $cmt = $1;
3475 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
3476 $r_last = $c->{r};
3477 process_commit($c, $r_min, $r_max, \@k) or
3478 goto out;
3480 $d = undef;
3481 $c = { c => $cmt };
3482 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3483 get_author_info($c, $1, $2, $3);
3484 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3485 # ignore
3486 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3487 push @{$c->{raw}}, $_;
3488 } elsif (/^${esc_color}[ACRMDT]\t/) {
3489 # we could add $SVN->{svn_path} here, but that requires
3490 # remote access at the moment (repo_path_split)...
3491 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3492 push @{$c->{changed}}, $_;
3493 } elsif (/^${esc_color}diff /o) {
3494 $d = 1;
3495 push @{$c->{diff}}, $_;
3496 } elsif ($d) {
3497 push @{$c->{diff}}, $_;
3498 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
3499 $esc_color*[\+\-]*$esc_color$/x) {
3500 $stat = 1;
3501 push @{$c->{stat}}, $_;
3502 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3503 push @{$c->{stat}}, $_;
3504 $stat = undef;
3505 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3506 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
3507 } elsif (s/^${esc_color} //o) {
3508 push @{$c->{l}}, $_;
3511 if ($c && defined $c->{r} && $c->{r} != $r_last) {
3512 $r_last = $c->{r};
3513 process_commit($c, $r_min, $r_max, \@k);
3515 if (@k) {
3516 my $swap = $r_max;
3517 $r_max = $r_min;
3518 $r_min = $swap;
3519 process_commit($_, $r_min, $r_max) foreach reverse @k;
3521 out:
3522 close $log;
3523 print '-' x72,"\n" unless $incremental || $oneline;
3526 package Git::SVN::Migration;
3527 # these version numbers do NOT correspond to actual version numbers
3528 # of git nor git-svn. They are just relative.
3530 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3532 # v1 layout: .git/$id/info/url, refs/remotes/$id
3534 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3536 # v3 layout: .git/svn/$id, refs/remotes/$id
3537 # - info/url may remain for backwards compatibility
3538 # - this is what we migrate up to this layout automatically,
3539 # - this will be used by git svn init on single branches
3540 # v3.1 layout (auto migrated):
3541 # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3542 # for backwards compatibility
3544 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3545 # - this is only created for newly multi-init-ed
3546 # repositories. Similar in spirit to the
3547 # --use-separate-remotes option in git-clone (now default)
3548 # - we do not automatically migrate to this (following
3549 # the example set by core git)
3550 use strict;
3551 use warnings;
3552 use Carp qw/croak/;
3553 use File::Path qw/mkpath/;
3554 use File::Basename qw/dirname basename/;
3555 use vars qw/$_minimize/;
3557 sub migrate_from_v0 {
3558 my $git_dir = $ENV{GIT_DIR};
3559 return undef unless -d $git_dir;
3560 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3561 my $migrated = 0;
3562 while (<$fh>) {
3563 chomp;
3564 my ($id, $orig_ref) = ($_, $_);
3565 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
3566 next unless -f "$git_dir/$id/info/url";
3567 my $new_ref = "refs/remotes/$id";
3568 if (::verify_ref("$new_ref^0")) {
3569 print STDERR "W: $orig_ref is probably an old ",
3570 "branch used by an ancient version of ",
3571 "git-svn.\n",
3572 "However, $new_ref also exists.\n",
3573 "We will not be able ",
3574 "to use this branch until this ",
3575 "ambiguity is resolved.\n";
3576 next;
3578 print STDERR "Migrating from v0 layout...\n" if !$migrated;
3579 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
3580 command_noisy('update-ref', $new_ref, $orig_ref);
3581 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
3582 $migrated++;
3584 command_close_pipe($fh, $ctx);
3585 print STDERR "Done migrating from v0 layout...\n" if $migrated;
3586 $migrated;
3589 sub migrate_from_v1 {
3590 my $git_dir = $ENV{GIT_DIR};
3591 my $migrated = 0;
3592 return $migrated unless -d $git_dir;
3593 my $svn_dir = "$git_dir/svn";
3595 # just in case somebody used 'svn' as their $id at some point...
3596 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
3598 print STDERR "Migrating from a git-svn v1 layout...\n";
3599 mkpath([$svn_dir]);
3600 print STDERR "Data from a previous version of git-svn exists, but\n\t",
3601 "$svn_dir\n\t(required for this version ",
3602 "($::VERSION) of git-svn) does not. exist\n";
3603 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3604 while (<$fh>) {
3605 my $x = $_;
3606 next unless $x =~ s#^refs/remotes/##;
3607 chomp $x;
3608 next unless -f "$git_dir/$x/info/url";
3609 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
3610 next unless $u;
3611 my $dn = dirname("$git_dir/svn/$x");
3612 mkpath([$dn]) unless -d $dn;
3613 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3614 mkpath(["$git_dir/svn/svn"]);
3615 print STDERR " - $git_dir/$x/info => ",
3616 "$git_dir/svn/$x/info\n";
3617 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3618 croak "$!: $x";
3619 # don't worry too much about these, they probably
3620 # don't exist with repos this old (save for index,
3621 # and we can easily regenerate that)
3622 foreach my $f (qw/unhandled.log index .rev_db/) {
3623 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3625 } else {
3626 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
3627 rename "$git_dir/$x", "$git_dir/svn/$x" or
3628 croak "$!: $x";
3630 $migrated++;
3632 command_close_pipe($fh, $ctx);
3633 print STDERR "Done migrating from a git-svn v1 layout\n";
3634 $migrated;
3637 sub read_old_urls {
3638 my ($l_map, $pfx, $path) = @_;
3639 my @dir;
3640 foreach (<$path/*>) {
3641 if (-r "$_/info/url") {
3642 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
3643 my $ref_id = $pfx . basename $_;
3644 my $url = ::file_to_s("$_/info/url");
3645 $l_map->{$ref_id} = $url;
3646 } elsif (-d $_) {
3647 push @dir, $_;
3650 foreach (@dir) {
3651 my $x = $_;
3652 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3653 read_old_urls($l_map, $x, $_);
3657 sub migrate_from_v2 {
3658 my @cfg = command(qw/config -l/);
3659 return if grep /^svn-remote\..+\.url=/, @cfg;
3660 my %l_map;
3661 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
3662 my $migrated = 0;
3664 foreach my $ref_id (sort keys %l_map) {
3665 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
3666 if ($@) {
3667 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3669 $migrated++;
3671 $migrated;
3674 sub minimize_connections {
3675 my $r = Git::SVN::read_all_remotes();
3676 my $new_urls = {};
3677 my $root_repos = {};
3678 foreach my $repo_id (keys %$r) {
3679 my $url = $r->{$repo_id}->{url} or next;
3680 my $fetch = $r->{$repo_id}->{fetch} or next;
3681 my $ra = Git::SVN::Ra->new($url);
3683 # skip existing cases where we already connect to the root
3684 if (($ra->{url} eq $ra->{repos_root}) ||
3685 (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
3686 $repo_id)) {
3687 $root_repos->{$ra->{url}} = $repo_id;
3688 next;
3691 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
3692 my $root_path = $ra->{url};
3693 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
3694 foreach my $path (keys %$fetch) {
3695 my $ref_id = $fetch->{$path};
3696 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
3698 # make sure we can read when connecting to
3699 # a higher level of a repository
3700 my ($last_rev, undef) = $gs->last_rev_commit;
3701 if (!defined $last_rev) {
3702 $last_rev = eval {
3703 $root_ra->get_latest_revnum;
3705 next if $@;
3707 my $new = $root_path;
3708 $new .= length $path ? "/$path" : '';
3709 eval {
3710 $root_ra->get_log([$new], $last_rev, $last_rev,
3711 0, 0, 1, sub { });
3713 next if $@;
3714 $new_urls->{$ra->{repos_root}}->{$new} =
3715 { ref_id => $ref_id,
3716 old_repo_id => $repo_id,
3717 old_path => $path };
3721 my @emptied;
3722 foreach my $url (keys %$new_urls) {
3723 # see if we can re-use an existing [svn-remote "repo_id"]
3724 # instead of creating a(n ugly) new section:
3725 my $repo_id = $root_repos->{$url} ||
3726 Git::SVN::sanitize_remote_name($url);
3728 my $fetch = $new_urls->{$url};
3729 foreach my $path (keys %$fetch) {
3730 my $x = $fetch->{$path};
3731 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
3732 my $pfx = "svn-remote.$x->{old_repo_id}";
3734 my $old_fetch = quotemeta("$x->{old_path}:".
3735 "refs/remotes/$x->{ref_id}");
3736 command_noisy(qw/config --unset/,
3737 "$pfx.fetch", '^'. $old_fetch . '$');
3738 delete $r->{$x->{old_repo_id}}->
3739 {fetch}->{$x->{old_path}};
3740 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
3741 command_noisy(qw/config --unset/,
3742 "$pfx.url");
3743 push @emptied, $x->{old_repo_id}
3747 if (@emptied) {
3748 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
3749 "$ENV{GIT_DIR}/config";
3750 print STDERR <<EOF;
3751 The following [svn-remote] sections in your config file ($file) are empty
3752 and can be safely removed:
3754 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
3758 sub migration_check {
3759 migrate_from_v0();
3760 migrate_from_v1();
3761 migrate_from_v2();
3762 minimize_connections() if $_minimize;
3765 package Git::IndexInfo;
3766 use strict;
3767 use warnings;
3768 use Git qw/command_input_pipe command_close_pipe/;
3770 sub new {
3771 my ($class) = @_;
3772 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
3773 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
3776 sub remove {
3777 my ($self, $path) = @_;
3778 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
3779 return ++$self->{nr};
3781 undef;
3784 sub update {
3785 my ($self, $mode, $hash, $path) = @_;
3786 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
3787 return ++$self->{nr};
3789 undef;
3792 sub DESTROY {
3793 my ($self) = @_;
3794 command_close_pipe($self->{gui}, $self->{ctx});
3797 package Git::SVN::GlobSpec;
3798 use strict;
3799 use warnings;
3801 sub new {
3802 my ($class, $glob) = @_;
3803 my $re = $glob;
3804 $re =~ s!/+$!!g; # no need for trailing slashes
3805 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3806 my ($left, $right) = ($1, $2);
3807 if ($nr > 1) {
3808 die "Only one '*' wildcard expansion ",
3809 "is supported (got $nr): '$glob'\n";
3810 } elsif ($nr == 0) {
3811 die "One '*' is needed for glob: '$glob'\n";
3813 $re = quotemeta($left) . $re . quotemeta($right);
3814 if (length $left && !($left =~ s!/+$!!g)) {
3815 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3817 if (length $right && !($right =~ s!^/+!!g)) {
3818 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3820 my $left_re = qr/^\/\Q$left\E(\/|$)/;
3821 bless { left => $left, right => $right, left_regex => $left_re,
3822 regex => qr/$re/, glob => $glob }, $class;
3825 sub full_path {
3826 my ($self, $path) = @_;
3827 return (length $self->{left} ? "$self->{left}/" : '') .
3828 $path . (length $self->{right} ? "/$self->{right}" : '');
3831 __END__
3833 Data structures:
3836 $remotes = { # returned by read_all_remotes()
3837 'svn' => {
3838 # svn-remote.svn.url=https://svn.musicpd.org
3839 url => 'https://svn.musicpd.org',
3840 # svn-remote.svn.fetch=mpd/trunk:trunk
3841 fetch => {
3842 'mpd/trunk' => 'trunk',
3844 # svn-remote.svn.tags=mpd/tags/*:tags/*
3845 tags => {
3846 path => {
3847 left => 'mpd/tags',
3848 right => '',
3849 regex => qr!mpd/tags/([^/]+)$!,
3850 glob => 'tags/*',
3852 ref => {
3853 left => 'tags',
3854 right => '',
3855 regex => qr!tags/([^/]+)$!,
3856 glob => 'tags/*',
3862 $log_entry hashref as returned by libsvn_log_entry()
3864 log => 'whitespace-formatted log entry
3865 ', # trailing newline is preserved
3866 revision => '8', # integer
3867 date => '2004-02-24T17:01:44.108345Z', # commit date
3868 author => 'committer name'
3872 # this is generated by generate_diff();
3873 @mods = array of diff-index line hashes, each element represents one line
3874 of diff-index output
3876 diff-index line ($m hash)
3878 mode_a => first column of diff-index output, no leading ':',
3879 mode_b => second column of diff-index output,
3880 sha1_b => sha1sum of the final blob,
3881 chg => change type [MCRADT],
3882 file_a => original file name of a file (iff chg is 'C' or 'R')
3883 file_b => new/current file name of a file (any chg)
3887 # retval of read_url_paths{,_all}();
3888 $l_map = {
3889 # repository root url
3890 'https://svn.musicpd.org' => {
3891 # repository path # GIT_SVN_ID
3892 'mpd/trunk' => 'trunk',
3893 'mpd/tags/0.11.5' => 'tags/0.11.5',
3897 Notes:
3898 I don't trust the each() function on unless I created %hash myself
3899 because the internal iterator may not have started at base.