git-svn: fix buggy regular expression usage in several places
[git/haiku.git] / git-svn.perl
blob7664b385f6debfd46e4046d9367f0484a4638827
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';
16 $Git::SVN::Log::TZ = $ENV{TZ};
17 $ENV{TZ} = 'UTC';
18 $| = 1; # unbuffer STDOUT
20 sub fatal (@) { print STDERR @_; exit 1 }
21 require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
22 require SVN::Ra;
23 require SVN::Delta;
24 if ($SVN::Core::VERSION lt '1.1.0') {
25 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
27 push @Git::SVN::Ra::ISA, 'SVN::Ra';
28 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
29 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
30 use Carp qw/croak/;
31 use IO::File qw//;
32 use File::Basename qw/dirname basename/;
33 use File::Path qw/mkpath/;
34 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
35 use IPC::Open3;
36 use Git;
38 BEGIN {
39 my $s;
40 foreach (qw/command command_oneline command_noisy command_output_pipe
41 command_input_pipe command_close_pipe/) {
42 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
43 "*Git::SVN::Migration::$_ = ".
44 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
46 eval $s;
49 my ($SVN);
51 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
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,
58 $_merge, $_strategy, $_dry_run,
59 $_prefix);
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 'no-metadata' => \$Git::SVN::_no_metadata,
68 'quiet|q' => \$_q,
69 'repack-flags|repack-args|repack-opts=s' =>
70 \$Git::SVN::_repack_flags,
71 %remote_opts );
73 my ($_trunk, $_tags, $_branches);
74 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
75 'tags|t=s' => \$_tags,
76 'branches|b=s' => \$_branches );
77 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
78 my %cmt_opts = ( 'edit|e' => \$_edit,
79 'rmdir' => \$SVN::Git::Editor::_rmdir,
80 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
81 'l=i' => \$SVN::Git::Editor::_rename_limit,
82 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
85 my %cmd = (
86 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
87 { 'revision|r=s' => \$_revision, %fc_opts } ],
88 init => [ \&cmd_init, "Initialize a repo for tracking" .
89 " (requires URL argument)",
90 \%init_opts ],
91 dcommit => [ \&cmd_dcommit,
92 'Commit several diffs to merge with upstream',
93 { 'merge|m|M' => \$_merge,
94 'strategy|s=s' => \$_strategy,
95 'dry-run|n' => \$_dry_run,
96 %cmt_opts, %fc_opts } ],
97 'set-tree' => [ \&cmd_set_tree,
98 "Set an SVN repository to a git tree-ish",
99 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
100 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
101 { 'revision|r=i' => \$_revision } ],
102 'multi-init' => [ \&cmd_multi_init,
103 'Initialize multiple trees (like git-svnimport)',
104 { %multi_opts, %init_opts, %remote_opts,
105 'revision|r=i' => \$_revision,
106 'prefix=s' => \$_prefix,
107 } ],
108 'multi-fetch' => [ \&cmd_multi_fetch,
109 'Fetch multiple trees (like git-svnimport)',
110 \%fc_opts ],
111 'migrate' => [ sub { },
112 # no-op, we automatically run this anyways,
113 'Migrate configuration/metadata/layout from
114 previous versions of git-svn',
115 \%remote_opts ],
116 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
117 { 'limit=i' => \$Git::SVN::Log::limit,
118 'revision|r=s' => \$_revision,
119 'verbose|v' => \$Git::SVN::Log::verbose,
120 'incremental' => \$Git::SVN::Log::incremental,
121 'oneline' => \$Git::SVN::Log::oneline,
122 'show-commit' => \$Git::SVN::Log::show_commit,
123 'non-recursive' => \$Git::SVN::Log::non_recursive,
124 'authors-file|A=s' => \$_authors,
125 'color' => \$Git::SVN::Log::color,
126 'pager=s' => \$Git::SVN::Log::pager,
127 } ],
128 'commit-diff' => [ \&cmd_commit_diff,
129 'Commit a diff between two trees',
130 { 'message|m=s' => \$_message,
131 'file|F=s' => \$_file,
132 'revision|r=s' => \$_revision,
133 %cmt_opts } ],
136 my $cmd;
137 for (my $i = 0; $i < @ARGV; $i++) {
138 if (defined $cmd{$ARGV[$i]}) {
139 $cmd = $ARGV[$i];
140 splice @ARGV, $i, 1;
141 last;
145 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
147 read_repo_config(\%opts);
148 my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
149 'minimize-connections' => \$Git::SVN::Migration::_minimize,
150 'id|i=s' => \$Git::SVN::default_ref_id,
151 'svn-remote|remote|R=s' => \$Git::SVN::default_repo_id);
152 exit 1 if (!$rv && $cmd ne 'log');
154 usage(0) if $_help;
155 version() if $_version;
156 usage(1) unless defined $cmd;
157 load_authors() if $_authors;
158 unless ($cmd =~ /^(?:init|multi-init|commit-diff)$/) {
159 Git::SVN::Migration::migration_check();
161 Git::SVN::init_vars();
162 eval {
163 Git::SVN::verify_remotes_sanity();
164 $cmd{$cmd}->[0]->(@ARGV);
166 fatal $@ if $@;
167 exit 0;
169 ####################### primary functions ######################
170 sub usage {
171 my $exit = shift || 0;
172 my $fd = $exit ? \*STDERR : \*STDOUT;
173 print $fd <<"";
174 git-svn - bidirectional operations between a single Subversion tree and git
175 Usage: $0 <command> [options] [arguments]\n
177 print $fd "Available commands:\n" unless $cmd;
179 foreach (sort keys %cmd) {
180 next if $cmd && $cmd ne $_;
181 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
182 foreach (keys %{$cmd{$_}->[2]}) {
183 # prints out arguments as they should be passed:
184 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
185 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
186 "--$_" : "-$_" }
187 split /\|/,$_)," $x\n";
190 print $fd <<"";
191 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
192 arbitrary identifier if you're tracking multiple SVN branches/repositories in
193 one git repository and want to keep them separate. See git-svn(1) for more
194 information.
196 exit $exit;
199 sub version {
200 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
201 exit 0;
204 sub do_git_init_db {
205 unless (-d $ENV{GIT_DIR}) {
206 my @init_db = ('init');
207 push @init_db, "--template=$_template" if defined $_template;
208 push @init_db, "--shared" if defined $_shared;
209 command_noisy(@init_db);
213 sub cmd_init {
214 my $url = shift or die "SVN repository location required " .
215 "as a command-line argument\n";
216 if (my $repo_path = shift) {
217 unless (-d $repo_path) {
218 mkpath([$repo_path]);
220 chdir $repo_path or croak $!;
221 $ENV{GIT_DIR} = $repo_path . "/.git";
223 do_git_init_db();
225 Git::SVN->init($url);
228 sub cmd_fetch {
229 if (@_) {
230 die "Additional fetch arguments are no longer supported.\n",
231 "Use --follow-parent if you have moved/copied directories
232 instead.\n";
234 my $gs = Git::SVN->new;
235 $gs->fetch(parse_revision_argument());
236 if ($gs->{last_commit} && !verify_ref('refs/heads/master^0')) {
237 command_noisy(qw(update-ref refs/heads/master),
238 $gs->{last_commit});
242 sub cmd_set_tree {
243 my (@commits) = @_;
244 if ($_stdin || !@commits) {
245 print "Reading from stdin...\n";
246 @commits = ();
247 while (<STDIN>) {
248 if (/\b($sha1_short)\b/o) {
249 unshift @commits, $1;
253 my @revs;
254 foreach my $c (@commits) {
255 my @tmp = command('rev-parse',$c);
256 if (scalar @tmp == 1) {
257 push @revs, $tmp[0];
258 } elsif (scalar @tmp > 1) {
259 push @revs, reverse(command('rev-list',@tmp));
260 } else {
261 fatal "Failed to rev-parse $c\n";
264 my $gs = Git::SVN->new;
265 my ($r_last, $cmt_last) = $gs->last_rev_commit;
266 $gs->fetch;
267 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
268 fatal "There are new revisions that were fetched ",
269 "and need to be merged (or acknowledged) ",
270 "before committing.\nlast rev: $r_last\n",
271 " current: $gs->{last_rev}\n";
273 $gs->set_tree($_) foreach @revs;
274 print "Done committing ",scalar @revs," revisions to SVN\n";
277 sub cmd_dcommit {
278 my $head = shift;
279 my $gs = Git::SVN->new;
280 $head ||= 'HEAD';
281 my @refs = command(qw/rev-list --no-merges/, $gs->refname."..$head");
282 my $last_rev;
283 foreach my $d (reverse @refs) {
284 if (!verify_ref("$d~1")) {
285 fatal "Commit $d\n",
286 "has no parent commit, and therefore ",
287 "nothing to diff against.\n",
288 "You should be working from a repository ",
289 "originally created by git-svn\n";
291 unless (defined $last_rev) {
292 (undef, $last_rev, undef) = cmt_metadata("$d~1");
293 unless (defined $last_rev) {
294 fatal "Unable to extract revision information ",
295 "from commit $d~1\n";
298 if ($_dry_run) {
299 print "diff-tree $d~1 $d\n";
300 } else {
301 my %ed_opts = ( r => $last_rev,
302 log => get_commit_entry($d)->{log},
303 ra => $gs->ra,
304 tree_a => "$d~1",
305 tree_b => $d,
306 editor_cb => sub {
307 print "Committed r$_[0]\n";
308 $last_rev = $_[0]; },
309 svn_path => $gs->{path} );
310 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
311 print "No changes\n$d~1 == $d\n";
315 return if $_dry_run;
316 $gs->fetch;
317 # we always want to rebase against the current HEAD, not any
318 # head that was passed to us
319 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
320 my @finish;
321 if (@diff) {
322 @finish = qw/rebase/;
323 push @finish, qw/--merge/ if $_merge;
324 push @finish, "--strategy=$_strategy" if $_strategy;
325 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
326 "using @finish:\n", "@diff";
327 } else {
328 print "No changes between current HEAD and ",
329 $gs->refname, "\nResetting to the latest ",
330 $gs->refname, "\n";
331 @finish = qw/reset --mixed/;
333 command_noisy(@finish, $gs->refname);
336 sub cmd_show_ignore {
337 my $gs = Git::SVN->new;
338 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
339 $gs->traverse_ignore(\*STDOUT, '', $r);
342 sub cmd_multi_init {
343 my $url = shift;
344 unless (defined $_trunk || defined $_branches || defined $_tags) {
345 usage(1);
347 do_git_init_db();
348 $_prefix = '' unless defined $_prefix;
349 $url =~ s#/+$## if defined $url;
350 if (defined $_trunk) {
351 my $trunk_ref = $_prefix . 'trunk';
352 # try both old-style and new-style lookups:
353 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
354 unless ($gs_trunk) {
355 my ($trunk_url, $trunk_path) =
356 complete_svn_url($url, $_trunk);
357 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
358 undef, $trunk_ref);
361 return unless defined $_branches || defined $_tags;
362 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
363 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
364 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
367 sub cmd_multi_fetch {
368 my $remotes = Git::SVN::read_all_remotes();
369 foreach my $repo_id (sort keys %$remotes) {
370 if ($remotes->{$repo_id}->{url} &&
371 $remotes->{$repo_id}->{fetch}) {
372 Git::SVN::fetch_all($repo_id, $remotes);
377 # this command is special because it requires no metadata
378 sub cmd_commit_diff {
379 my ($ta, $tb, $url) = @_;
380 my $usage = "Usage: $0 commit-diff -r<revision> ".
381 "<tree-ish> <tree-ish> [<URL>]\n";
382 fatal($usage) if (!defined $ta || !defined $tb);
383 my $svn_path;
384 if (!defined $url) {
385 my $gs = eval { Git::SVN->new };
386 if (!$gs) {
387 fatal("Needed URL or usable git-svn --id in ",
388 "the command-line\n", $usage);
390 $url = $gs->{url};
391 $svn_path = $gs->{path};
393 unless (defined $_revision) {
394 fatal("-r|--revision is a required argument\n", $usage);
396 if (defined $_message && defined $_file) {
397 fatal("Both --message/-m and --file/-F specified ",
398 "for the commit message.\n",
399 "I have no idea what you mean\n");
401 if (defined $_file) {
402 $_message = file_to_s($_file);
403 } else {
404 $_message ||= get_commit_entry($tb)->{log};
406 my $ra ||= Git::SVN::Ra->new($url);
407 $svn_path ||= $ra->{svn_path};
408 my $r = $_revision;
409 if ($r eq 'HEAD') {
410 $r = $ra->get_latest_revnum;
411 } elsif ($r !~ /^\d+$/) {
412 die "revision argument: $r not understood by git-svn\n";
414 my %ed_opts = ( r => $r,
415 log => $_message,
416 ra => $ra,
417 tree_a => $ta,
418 tree_b => $tb,
419 editor_cb => sub { print "Committed r$_[0]\n" },
420 svn_path => $svn_path );
421 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
422 print "No changes\n$ta == $tb\n";
426 ########################### utility functions #########################
428 sub parse_revision_argument {
429 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
430 return (undef, undef);
432 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
433 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
434 return (undef, $1) if ($_revision =~ /^BASE:(\d+)$/);
435 return ($1, undef) if ($_revision =~ /^(\d+):HEAD$/);
436 die "revision argument: $_revision not understood by git-svn\n",
437 "Try using the command-line svn client instead\n";
440 sub complete_svn_url {
441 my ($url, $path) = @_;
442 $path =~ s#/+$##;
443 if ($path !~ m#^[a-z\+]+://#) {
444 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
445 fatal("E: '$path' is not a complete URL ",
446 "and a separate URL is not specified\n");
448 return ($url, $path);
450 return ($path, '');
453 sub complete_url_ls_init {
454 my ($ra, $repo_path, $switch, $pfx) = @_;
455 unless ($repo_path) {
456 print STDERR "W: $switch not specified\n";
457 return;
459 $repo_path =~ s#/+$##;
460 if ($repo_path =~ m#^[a-z\+]+://#) {
461 $ra = Git::SVN::Ra->new($repo_path);
462 $repo_path = '';
463 } else {
464 $repo_path =~ s#^/+##;
465 unless ($ra) {
466 fatal("E: '$repo_path' is not a complete URL ",
467 "and a separate URL is not specified\n");
470 my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
471 my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r);
472 my $url = $ra->{url};
473 my $remote_id;
474 my $remote_path;
475 foreach my $d (sort keys %$dirent) {
476 next if ($dirent->{$d}->kind != $SVN::Node::dir);
477 my $path = "$repo_path/$d";
478 my $ref = "$pfx$d";
479 my $gs = eval { Git::SVN->new($ref) };
480 # don't try to init already existing refs
481 unless ($gs) {
482 print "init $url/$path => $ref\n";
483 $gs = Git::SVN->init($url, $path, undef, $ref, 1);
485 if ($gs) {
486 $remote_id = $gs->{repo_id};
487 last;
490 if (defined $remote_id) {
491 $remote_path = "$ra->{svn_path}/$repo_path/*";
492 $remote_path =~ s#/+#/#g;
493 $remote_path =~ s#^/##g;
494 my ($n) = ($switch =~ /^--(\w+)/);
495 if (length $pfx && $pfx !~ m#/$#) {
496 die "--prefix='$pfx' must have a trailing slash '/'\n";
498 command_noisy('config', "svn-remote.$remote_id.$n",
499 "$remote_path:refs/remotes/$pfx*");
503 sub verify_ref {
504 my ($ref) = @_;
505 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
506 { STDERR => 0 }); };
509 sub get_tree_from_treeish {
510 my ($treeish) = @_;
511 # $treeish can be a symbolic ref, too:
512 my $type = command_oneline(qw/cat-file -t/, $treeish);
513 my $expected;
514 while ($type eq 'tag') {
515 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
517 if ($type eq 'commit') {
518 $expected = (grep /^tree /, command(qw/cat-file commit/,
519 $treeish))[0];
520 ($expected) = ($expected =~ /^tree ($sha1)$/o);
521 die "Unable to get tree from $treeish\n" unless $expected;
522 } elsif ($type eq 'tree') {
523 $expected = $treeish;
524 } else {
525 die "$treeish is a $type, expected tree, tag or commit\n";
527 return $expected;
530 sub get_commit_entry {
531 my ($treeish) = shift;
532 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
533 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
534 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
535 open my $log_fh, '>', $commit_editmsg or croak $!;
537 my $type = command_oneline(qw/cat-file -t/, $treeish);
538 if ($type eq 'commit' || $type eq 'tag') {
539 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
540 $type, $treeish);
541 my $in_msg = 0;
542 while (<$msg_fh>) {
543 if (!$in_msg) {
544 $in_msg = 1 if (/^\s*$/);
545 } elsif (/^git-svn-id: /) {
546 # skip this for now, we regenerate the
547 # correct one on re-fetch anyways
548 # TODO: set *:merge properties or like...
549 } else {
550 print $log_fh $_ or croak $!;
553 command_close_pipe($msg_fh, $ctx);
555 close $log_fh or croak $!;
557 if ($_edit || ($type eq 'tree')) {
558 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
559 # TODO: strip out spaces, comments, like git-commit.sh
560 system($editor, $commit_editmsg);
562 rename $commit_editmsg, $commit_msg or croak $!;
563 open $log_fh, '<', $commit_msg or croak $!;
564 { local $/; chomp($log_entry{log} = <$log_fh>); }
565 close $log_fh or croak $!;
566 unlink $commit_msg;
567 \%log_entry;
570 sub s_to_file {
571 my ($str, $file, $mode) = @_;
572 open my $fd,'>',$file or croak $!;
573 print $fd $str,"\n" or croak $!;
574 close $fd or croak $!;
575 chmod ($mode &~ umask, $file) if (defined $mode);
578 sub file_to_s {
579 my $file = shift;
580 open my $fd,'<',$file or croak "$!: file: $file\n";
581 local $/;
582 my $ret = <$fd>;
583 close $fd or croak $!;
584 $ret =~ s/\s*$//s;
585 return $ret;
588 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
589 sub load_authors {
590 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
591 my $log = $cmd eq 'log';
592 while (<$authors>) {
593 chomp;
594 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
595 my ($user, $name, $email) = ($1, $2, $3);
596 if ($log) {
597 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
598 } else {
599 $users{$user} = [$name, $email];
602 close $authors or croak $!;
605 # convert GetOpt::Long specs for use by git-config
606 sub read_repo_config {
607 return unless -d $ENV{GIT_DIR};
608 my $opts = shift;
609 foreach my $o (keys %$opts) {
610 my $v = $opts->{$o};
611 my ($key) = ($o =~ /^([a-z\-]+)/);
612 $key =~ s/-//g;
613 my $arg = 'git-config';
614 $arg .= ' --int' if ($o =~ /[:=]i$/);
615 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
616 if (ref $v eq 'ARRAY') {
617 chomp(my @tmp = `$arg --get-all svn.$key`);
618 @$v = @tmp if @tmp;
619 } else {
620 chomp(my $tmp = `$arg --get svn.$key`);
621 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
622 $$v = $tmp;
628 sub extract_metadata {
629 my $id = shift or return (undef, undef, undef);
630 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
631 \s([a-f\d\-]+)$/x);
632 if (!defined $rev || !$uuid || !$url) {
633 # some of the original repositories I made had
634 # identifiers like this:
635 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
637 return ($url, $rev, $uuid);
640 sub cmt_metadata {
641 return extract_metadata((grep(/^git-svn-id: /,
642 command(qw/cat-file commit/, shift)))[-1]);
645 package Git::SVN;
646 use strict;
647 use warnings;
648 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
649 $_repack $_repack_flags/;
650 use Carp qw/croak/;
651 use File::Path qw/mkpath/;
652 use File::Copy qw/copy/;
653 use IPC::Open3;
655 my $_repack_nr;
656 # properties that we do not log:
657 my %SKIP_PROP;
658 BEGIN {
659 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
660 svn:special svn:executable
661 svn:entry:committed-rev
662 svn:entry:last-author
663 svn:entry:uuid
664 svn:entry:committed-date/;
667 my %LOCKFILES;
668 END { unlink keys %LOCKFILES if %LOCKFILES }
670 sub resolve_local_globs {
671 my ($url, $fetch, $glob_spec) = @_;
672 return unless defined $glob_spec;
673 my $ref = $glob_spec->{ref};
674 my $path = $glob_spec->{path};
675 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
676 next unless m#^refs/remotes/$ref->{regex}$#;
677 my $p = $1;
678 my $pathname = $path->full_path($p);
679 my $refname = $ref->full_path($p);
680 if (my $existing = $fetch->{$pathname}) {
681 if ($existing ne $refname) {
682 die "Refspec conflict:\n",
683 "existing: refs/remotes/$existing\n",
684 " globbed: refs/remotes/$refname\n";
686 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
687 $u =~ s!^\Q$url\E(/|$)!! or die
688 "refs/remotes/$refname: '$url' not found in '$u'\n";
689 if ($pathname ne $u) {
690 warn "W: Refspec glob conflict ",
691 "(ref: refs/remotes/$refname):\n",
692 "expected path: $pathname\n",
693 " real path: $u\n",
694 "Continuing ahead with $u\n";
695 next;
697 } else {
698 $fetch->{$pathname} = $refname;
703 sub fetch_all {
704 my ($repo_id, $remotes) = @_;
705 my $remote = $remotes->{$repo_id};
706 my $fetch = $remote->{fetch};
707 my $url = $remote->{url};
708 my (@gs, @globs);
709 my $ra = Git::SVN::Ra->new($url);
710 my $uuid = $ra->uuid;
711 my $head = $ra->get_latest_revnum;
712 my $base = $head;
714 # read the max revs for wildcard expansion (branches/*, tags/*)
715 foreach my $t (qw/branches tags/) {
716 defined $remote->{$t} or next;
717 push @globs, $remote->{$t};
718 my $f = "$ENV{GIT_DIR}/svn/.$uuid.$t";
719 if (open my $fh, '<', $f) {
720 chomp(my $max_rev = <$fh>);
721 close $fh or die "Error closing $f: $!\n";
723 if ($max_rev !~ /^\d+$/) {
724 die "$max_rev (in $f) is not an integer!\n";
726 $remote->{$t}->{max_rev} = $max_rev;
727 $base = $max_rev if ($max_rev < $base);
731 foreach my $p (sort keys %$fetch) {
732 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
733 my $lr = $gs->rev_db_max;
734 if (defined $lr) {
735 $base = $lr if ($lr < $base);
737 push @gs, $gs;
739 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
742 sub read_all_remotes {
743 my $r = {};
744 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
745 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
746 $r->{$1}->{fetch}->{$2} = $3;
747 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
748 $r->{$1}->{url} = $2;
749 } elsif (m!^(.+)\.(branches|tags)=
750 (.*):refs/remotes/(.+)\s*$/!x) {
751 my ($p, $g) = ($3, $4);
752 my $rs = $r->{$1}->{$2} = {
753 t => $2,
754 path => Git::SVN::GlobSpec->new($p),
755 ref => Git::SVN::GlobSpec->new($g) };
756 if (length($rs->{ref}->{right}) != 0) {
757 die "The '*' glob character must be the last ",
758 "character of '$g'\n";
765 sub init_vars {
766 if (defined $_repack) {
767 $_repack = 1000 if ($_repack <= 0);
768 $_repack_nr = $_repack;
769 $_repack_flags ||= '-d';
773 sub verify_remotes_sanity {
774 return unless -d $ENV{GIT_DIR};
775 my %seen;
776 foreach (command(qw/config -l/)) {
777 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
778 if ($seen{$1}) {
779 die "Remote ref refs/remote/$1 is tracked by",
780 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
781 "Please resolve this ambiguity in ",
782 "your git configuration file before ",
783 "continuing\n";
785 $seen{$1} = $_;
790 # we allow more chars than remotes2config.sh...
791 sub sanitize_remote_name {
792 my ($name) = @_;
793 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
794 $name;
797 sub find_existing_remote {
798 my ($url, $remotes) = @_;
799 my $existing;
800 foreach my $repo_id (keys %$remotes) {
801 my $u = $remotes->{$repo_id}->{url} or next;
802 next if $u ne $url;
803 $existing = $repo_id;
804 last;
806 $existing;
809 sub init_remote_config {
810 my ($self, $url, $no_write) = @_;
811 $url =~ s!/+$!!; # strip trailing slash
812 my $r = read_all_remotes();
813 my $existing = find_existing_remote($url, $r);
814 if ($existing) {
815 unless ($no_write) {
816 print STDERR "Using existing ",
817 "[svn-remote \"$existing\"]\n";
819 $self->{repo_id} = $existing;
820 } else {
821 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
822 $existing = find_existing_remote($min_url, $r);
823 if ($existing) {
824 unless ($no_write) {
825 print STDERR "Using existing ",
826 "[svn-remote \"$existing\"]\n";
828 $self->{repo_id} = $existing;
830 if ($min_url ne $url) {
831 unless ($no_write) {
832 print STDERR "Using higher level of URL: ",
833 "$url => $min_url\n";
835 my $old_path = $self->{path};
836 $self->{path} = $url;
837 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
838 if (length $old_path) {
839 $self->{path} .= "/$old_path";
841 $url = $min_url;
844 my $orig_url;
845 if (!$existing) {
846 # verify that we aren't overwriting anything:
847 $orig_url = eval {
848 command_oneline('config', '--get',
849 "svn-remote.$self->{repo_id}.url")
851 if ($orig_url && ($orig_url ne $url)) {
852 die "svn-remote.$self->{repo_id}.url already set: ",
853 "$orig_url\nwanted to set to: $url\n";
856 my ($xrepo_id, $xpath) = find_ref($self->refname);
857 if (defined $xpath) {
858 die "svn-remote.$xrepo_id.fetch already set to track ",
859 "$xpath:refs/remotes/", $self->refname, "\n";
861 unless ($no_write) {
862 command_noisy('config',
863 "svn-remote.$self->{repo_id}.url", $url);
864 command_noisy('config', '--add',
865 "svn-remote.$self->{repo_id}.fetch",
866 "$self->{path}:".$self->refname);
868 $self->{url} = $url;
871 sub init {
872 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
873 my $self = _new($class, $repo_id, $ref_id, $path);
874 if (defined $url) {
875 $self->init_remote_config($url, $no_write);
877 $self;
880 sub find_ref {
881 my ($ref_id) = @_;
882 foreach (command(qw/config -l/)) {
883 next unless m!^svn-remote\.(.+)\.fetch=
884 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
885 my ($repo_id, $path, $ref) = ($1, $2, $3);
886 if ($ref eq $ref_id) {
887 $path = '' if ($path =~ m#^\./?#);
888 return ($repo_id, $path);
891 (undef, undef, undef);
894 sub new {
895 my ($class, $ref_id, $repo_id, $path) = @_;
896 if (defined $ref_id && !defined $repo_id && !defined $path) {
897 ($repo_id, $path) = find_ref($ref_id);
898 if (!defined $repo_id) {
899 die "Could not find a \"svn-remote.*.fetch\" key ",
900 "in the repository configuration matching: ",
901 "refs/remotes/$ref_id\n";
904 my $self = _new($class, $repo_id, $ref_id, $path);
905 if (!defined $self->{path} || !length $self->{path}) {
906 my $fetch = command_oneline('config', '--get',
907 "svn-remote.$repo_id.fetch",
908 ":refs/remotes/$ref_id\$") or
909 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
910 "\":refs/remotes/$ref_id\$\" in config\n";
911 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
913 $self->{url} = command_oneline('config', '--get',
914 "svn-remote.$repo_id.url") or
915 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
916 if (-z $self->{db_path} && ::verify_ref($self->refname.'^0')) {
917 $self->rebuild;
919 $self;
922 sub refname { "refs/remotes/$_[0]->{ref_id}" }
924 sub ra {
925 my ($self) = shift;
926 Git::SVN::Ra->new($self->{url});
929 sub rel_path {
930 my ($self) = @_;
931 my $repos_root = $self->ra->{repos_root};
932 return $self->{path} if ($self->{url} eq $repos_root);
933 die "BUG: rel_path failed! repos_root: $repos_root, Ra URL: ",
934 $self->ra->{url}, " path: $self->{path}, URL: $self->{url}\n";
937 sub traverse_ignore {
938 my ($self, $fh, $path, $r) = @_;
939 $path =~ s#^/+##g;
940 my $ra = $self->ra;
941 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
942 my $p = $path;
943 $p =~ s#^\Q$ra->{svn_path}\E/##;
944 print $fh length $p ? "\n# $p\n" : "\n# /\n";
945 if (my $s = $props->{'svn:ignore'}) {
946 $s =~ s/[\r\n]+/\n/g;
947 chomp $s;
948 if (length $p == 0) {
949 $s =~ s#\n#\n/$p#g;
950 print $fh "/$s\n";
951 } else {
952 $s =~ s#\n#\n/$p/#g;
953 print $fh "/$p/$s\n";
956 foreach (sort keys %$dirent) {
957 next if $dirent->{$_}->kind != $SVN::Node::dir;
958 $self->traverse_ignore($fh, "$path/$_", $r);
962 sub last_rev { ($_[0]->last_rev_commit)[0] }
963 sub last_commit { ($_[0]->last_rev_commit)[1] }
965 # returns the newest SVN revision number and newest commit SHA1
966 sub last_rev_commit {
967 my ($self) = @_;
968 if (defined $self->{last_rev} && defined $self->{last_commit}) {
969 return ($self->{last_rev}, $self->{last_commit});
971 my $c = ::verify_ref($self->refname.'^0');
972 if ($c) {
973 my $rev = (::cmt_metadata($c))[1];
974 if (defined $rev) {
975 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
976 return ($rev, $c);
979 my $offset = -41; # from tail
980 my $rl;
981 open my $fh, '<', $self->{db_path} or
982 croak "$self->{db_path} not readable: $!\n";
983 sysseek($fh, $offset, 2); # don't care for errors
984 sysread($fh, $rl, 41) == 41 or return (undef, undef);
985 chomp $rl;
986 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
987 $offset -= 41;
988 sysseek($fh, $offset, 2); # don't care for errors
989 sysread($fh, $rl, 41) == 41 or return (undef, undef);
990 chomp $rl;
992 if ($c) {
993 die "$self->{db_path} and ", $self->refname,
994 " inconsistent!:\n$c != $rl\n";
996 my $rev = sysseek($fh, 0, 1) or croak $!;
997 $rev = ($rev - 41) / 41;
998 close $fh or croak $!;
999 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1000 return ($rev, $c);
1003 sub get_fetch_range {
1004 my ($self, $min, $max) = @_;
1005 $max ||= $self->ra->get_latest_revnum;
1006 $min ||= $self->rev_db_max;
1007 (++$min, $max);
1010 sub tmp_index_do {
1011 my ($self, $sub) = @_;
1012 my $old_index = $ENV{GIT_INDEX_FILE};
1013 $ENV{GIT_INDEX_FILE} = $self->{index};
1014 my @ret = &$sub;
1015 if ($old_index) {
1016 $ENV{GIT_INDEX_FILE} = $old_index;
1017 } else {
1018 delete $ENV{GIT_INDEX_FILE};
1020 wantarray ? @ret : $ret[0];
1023 sub assert_index_clean {
1024 my ($self, $treeish) = @_;
1026 $self->tmp_index_do(sub {
1027 command_noisy('read-tree', $treeish) unless -e $self->{index};
1028 my $x = command_oneline('write-tree');
1029 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1030 /^tree ($::sha1)/mo);
1031 if ($y ne $x) {
1032 unlink $self->{index} or croak $!;
1033 command_noisy('read-tree', $treeish);
1035 $x = command_oneline('write-tree');
1036 if ($y ne $x) {
1037 ::fatal "trees ($treeish) $y != $x\n",
1038 "Something is seriously wrong...\n";
1043 sub get_commit_parents {
1044 my ($self, $log_entry) = @_;
1045 my (%seen, @ret, @tmp);
1046 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1047 if (my $ip = $self->{inject_parents}) {
1048 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1049 push @tmp, $commit;
1052 if (my $cur = ::verify_ref($self->refname.'^0')) {
1053 push @tmp, $cur;
1055 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
1056 while (my $p = shift @tmp) {
1057 next if $seen{$p};
1058 $seen{$p} = 1;
1059 push @ret, $p;
1060 # MAXPARENT is defined to 16 in commit-tree.c:
1061 last if @ret >= 16;
1063 if (@tmp) {
1064 die "r$log_entry->{revision}: No room for parents:\n\t",
1065 join("\n\t", @tmp), "\n";
1067 @ret;
1070 sub full_url {
1071 my ($self) = @_;
1072 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
1075 sub do_git_commit {
1076 my ($self, $log_entry) = @_;
1077 my $lr = $self->last_rev;
1078 if (defined $lr && $lr >= $log_entry->{revision}) {
1079 die "Last fetched revision of ", $self->refname,
1080 " was r$lr, but we are about to fetch: ",
1081 "r$log_entry->{revision}!\n";
1083 if (my $c = $self->rev_db_get($log_entry->{revision})) {
1084 croak "$log_entry->{revision} = $c already exists! ",
1085 "Why are we refetching it?\n";
1087 my $author = $log_entry->{author};
1088 my ($name, $email) = (defined $::users{$author} ? @{$::users{$author}}
1089 : ($author, "$author\@".$self->ra->uuid));
1090 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1091 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1092 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
1094 my $tree = $log_entry->{tree};
1095 if (!defined $tree) {
1096 $tree = $self->tmp_index_do(sub {
1097 command_oneline('write-tree') });
1099 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1101 my @exec = ('git-commit-tree', $tree);
1102 foreach ($self->get_commit_parents($log_entry)) {
1103 push @exec, '-p', $_;
1105 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1106 or croak $!;
1107 print $msg_fh $log_entry->{log} or croak $!;
1108 unless ($_no_metadata) {
1109 print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
1110 $log_entry->{revision}, ' ',
1111 $self->ra->uuid, "\n" or croak $!;
1113 $msg_fh->flush == 0 or croak $!;
1114 close $msg_fh or croak $!;
1115 chomp(my $commit = do { local $/; <$out_fh> });
1116 close $out_fh or croak $!;
1117 waitpid $pid, 0;
1118 croak $? if $?;
1119 if ($commit !~ /^$::sha1$/o) {
1120 die "Failed to commit, invalid sha1: $commit\n";
1123 $self->rev_db_set($log_entry->{revision}, $commit, 1);
1125 $self->{last_rev} = $log_entry->{revision};
1126 $self->{last_commit} = $commit;
1127 print "r$log_entry->{revision} = $commit ($self->{ref_id})\n";
1128 if (defined $_repack && (--$_repack_nr == 0)) {
1129 $_repack_nr = $_repack;
1130 # repack doesn't use any arguments with spaces in them, does it?
1131 print "Running git repack $_repack_flags ...\n";
1132 command_noisy('repack', split(/\s+/, $_repack_flags));
1133 print "Done repacking\n";
1135 return $commit;
1138 sub match_paths {
1139 my ($self, $paths, $r) = @_;
1140 return 1 if $self->{path} eq '';
1141 if (my $path = $paths->{"/$self->{path}"}) {
1142 return ($path->{action} eq 'D') ? 0 : 1;
1144 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
1145 if (grep /$self->{path_regex}/, keys %$paths) {
1146 return 1;
1148 my $c = '';
1149 foreach (split m#/#, $self->{path}) {
1150 $c .= "/$_";
1151 next unless ($paths->{$c} && ($paths->{$c}->{action} eq 'A'));
1152 if ($self->ra->check_path($self->{path}, $r) ==
1153 $SVN::Node::dir) {
1154 return 1;
1157 return 0;
1160 sub find_parent_branch {
1161 my ($self, $paths, $rev) = @_;
1162 return undef unless $_follow_parent;
1163 unless (defined $paths) {
1164 my $err_handler = $SVN::Error::handler;
1165 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
1166 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
1167 $paths =
1168 Git::SVN::Ra::dup_changed_paths($_[0]) });
1169 $SVN::Error::handler = $err_handler;
1171 return undef unless defined $paths;
1173 # look for a parent from another branch:
1174 my @b_path_components = split m#/#, $self->rel_path;
1175 my @a_path_components;
1176 my $i;
1177 while (@b_path_components) {
1178 $i = $paths->{'/'.join('/', @b_path_components)};
1179 last if $i;
1180 unshift(@a_path_components, pop(@b_path_components));
1182 return undef unless defined $i;
1183 my $branch_from = $i->{copyfrom_path} or return undef;
1184 if (@a_path_components) {
1185 print STDERR "branch_from: $branch_from => ";
1186 $branch_from .= '/'.join('/', @a_path_components);
1187 print STDERR $branch_from, "\n";
1189 my $r = $i->{copyfrom_rev};
1190 my $repos_root = $self->ra->{repos_root};
1191 my $url = $self->ra->{url};
1192 my $new_url = $repos_root . $branch_from;
1193 print STDERR "Found possible branch point: ",
1194 "$new_url => ", $self->full_url, ", $r\n";
1195 $branch_from =~ s#^/##;
1196 my $remotes = read_all_remotes();
1197 my $gs;
1198 foreach my $repo_id (keys %$remotes) {
1199 my $u = $remotes->{$repo_id}->{url} or next;
1200 next if $url ne $u;
1201 my $fetch = $remotes->{$repo_id}->{fetch};
1202 foreach (qw/branches tags/) {
1203 resolve_local_globs($url, $fetch,
1204 $remotes->{$repo_id}->{$_});
1206 foreach my $f (keys %$fetch) {
1207 next if $f ne $branch_from;
1208 $gs = Git::SVN->new($fetch->{$f}, $repo_id, $f);
1209 last;
1211 last if $gs;
1213 unless ($gs) {
1214 my $ref_id = $self->{ref_id};
1215 $ref_id =~ s/\@\d+$//;
1216 $ref_id .= "\@$r";
1217 # just grow a tail if we're not unique enough :x
1218 $ref_id .= '-' while find_ref($ref_id);
1219 print STDERR "Initializing parent: $ref_id\n";
1220 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
1222 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1223 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
1224 $gs->fetch(0, $r);
1225 ($r0, $parent) = $gs->last_rev_commit;
1227 if (defined $r0 && defined $parent) {
1228 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
1229 $self->assert_index_clean($parent);
1230 my $ed;
1231 if ($self->ra->can_do_switch) {
1232 print STDERR "Following parent with do_switch\n";
1233 # do_switch works with svn/trunk >= r22312, but that
1234 # is not included with SVN 1.4.3 (the latest version
1235 # at the moment), so we can't rely on it
1236 $self->{last_commit} = $parent;
1237 $ed = SVN::Git::Fetcher->new($self);
1238 $gs->ra->gs_do_switch($r0, $rev, $gs,
1239 $self->full_url, $ed)
1240 or die "SVN connection failed somewhere...\n";
1241 } else {
1242 print STDERR "Following parent with do_update\n";
1243 $ed = SVN::Git::Fetcher->new($self);
1244 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1245 or die "SVN connection failed somewhere...\n";
1247 print STDERR "Successfully followed parent\n";
1248 return $self->make_log_entry($rev, [$parent], $ed);
1250 return undef;
1253 sub do_fetch {
1254 my ($self, $paths, $rev) = @_;
1255 my $ed;
1256 my ($last_rev, @parents);
1257 if (my $lc = $self->last_commit) {
1258 # we can have a branch that was deleted, then re-added
1259 # under the same name but copied from another path, in
1260 # which case we'll have multiple parents (we don't
1261 # want to break the original ref, nor lose copypath info):
1262 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1263 push @{$log_entry->{parents}}, $lc;
1264 return $log_entry;
1266 $ed = SVN::Git::Fetcher->new($self);
1267 $last_rev = $self->{last_rev};
1268 $ed->{c} = $lc;
1269 @parents = ($lc);
1270 } else {
1271 $last_rev = $rev;
1272 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1273 return $log_entry;
1275 $ed = SVN::Git::Fetcher->new($self);
1277 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1278 die "SVN connection failed somewhere...\n";
1280 $self->make_log_entry($rev, \@parents, $ed);
1283 sub get_untracked {
1284 my ($self, $ed) = @_;
1285 my @out;
1286 my $h = $ed->{empty};
1287 foreach (sort keys %$h) {
1288 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
1289 push @out, " $act: " . uri_encode($_);
1290 warn "W: $act: $_\n";
1292 foreach my $t (qw/dir_prop file_prop/) {
1293 $h = $ed->{$t} or next;
1294 foreach my $path (sort keys %$h) {
1295 my $ppath = $path eq '' ? '.' : $path;
1296 foreach my $prop (sort keys %{$h->{$path}}) {
1297 next if $SKIP_PROP{$prop};
1298 my $v = $h->{$path}->{$prop};
1299 my $t_ppath_prop = "$t: " .
1300 uri_encode($ppath) . ' ' .
1301 uri_encode($prop);
1302 if (defined $v) {
1303 push @out, " +$t_ppath_prop " .
1304 uri_encode($v);
1305 } else {
1306 push @out, " -$t_ppath_prop";
1311 foreach my $t (qw/absent_file absent_directory/) {
1312 $h = $ed->{$t} or next;
1313 foreach my $parent (sort keys %$h) {
1314 foreach my $path (sort @{$h->{$parent}}) {
1315 push @out, " $t: " .
1316 uri_encode("$parent/$path");
1317 warn "W: $t: $parent/$path ",
1318 "Insufficient permissions?\n";
1322 \@out;
1325 sub parse_svn_date {
1326 my $date = shift || return '+0000 1970-01-01 00:00:00';
1327 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1328 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
1329 croak "Unable to parse date: $date\n";
1330 "+0000 $Y-$m-$d $H:$M:$S";
1333 sub check_author {
1334 my ($author) = @_;
1335 if (!defined $author || length $author == 0) {
1336 $author = '(no author)';
1338 if (defined $::_authors && ! defined $::users{$author}) {
1339 die "Author: $author not defined in $::_authors file\n";
1341 $author;
1344 sub make_log_entry {
1345 my ($self, $rev, $parents, $ed) = @_;
1346 my $untracked = $self->get_untracked($ed);
1348 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
1349 print $un "r$rev\n" or croak $!;
1350 print $un $_, "\n" foreach @$untracked;
1351 my %log_entry = ( parents => $parents || [], revision => $rev,
1352 log => '');
1354 my $logged = delete $self->{logged_rev_props};
1355 if (!$logged || $self->{-want_extra_revprops}) {
1356 my $rp = $self->ra->rev_proplist($rev);
1357 foreach (sort keys %$rp) {
1358 my $v = $rp->{$_};
1359 if (/^svn:(author|date|log)$/) {
1360 $log_entry{$1} = $v;
1361 } else {
1362 print $un " rev_prop: ", uri_encode($_), ' ',
1363 uri_encode($v), "\n";
1366 } else {
1367 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1369 close $un or croak $!;
1371 $log_entry{date} = parse_svn_date($log_entry{date});
1372 $log_entry{author} = check_author($log_entry{author});
1373 $log_entry{log} .= "\n";
1374 \%log_entry;
1377 sub fetch {
1378 my ($self, $min_rev, $max_rev, @parents) = @_;
1379 my ($last_rev, $last_commit) = $self->last_rev_commit;
1380 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1381 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1384 sub set_tree_cb {
1385 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1386 # TODO: enable and test optimized commits:
1387 if (0 && $rev == ($self->{last_rev} + 1)) {
1388 $log_entry->{revision} = $rev;
1389 $log_entry->{author} = $author;
1390 $self->do_git_commit($log_entry, "$rev=$tree");
1391 } else {
1392 $self->{inject_parents} = { $rev => $tree };
1393 $self->fetch(undef, undef);
1397 sub set_tree {
1398 my ($self, $tree) = (shift, shift);
1399 my $log_entry = ::get_commit_entry($tree);
1400 unless ($self->{last_rev}) {
1401 fatal("Must have an existing revision to commit\n");
1403 my %ed_opts = ( r => $self->{last_rev},
1404 log => $log_entry->{log},
1405 ra => $self->ra,
1406 tree_a => $self->{last_commit},
1407 tree_b => $tree,
1408 editor_cb => sub {
1409 $self->set_tree_cb($log_entry, $tree, @_) },
1410 svn_path => $self->{path} );
1411 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
1412 print "No changes\nr$self->{last_rev} = $tree\n";
1416 sub rebuild {
1417 my ($self) = @_;
1418 print "Rebuilding $self->{db_path} ...\n";
1419 my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
1420 my $latest;
1421 my $full_url = $self->full_url;
1422 my $svn_uuid;
1423 while (<$rev_list>) {
1424 chomp;
1425 my $c = $_;
1426 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1427 my ($url, $rev, $uuid) = ::cmt_metadata($c);
1429 # ignore merges (from set-tree)
1430 next if (!defined $rev || !$uuid);
1432 # if we merged or otherwise started elsewhere, this is
1433 # how we break out of it
1434 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1435 ($full_url && $url && ($url ne $full_url))) {
1436 next;
1438 $latest ||= $rev;
1439 $svn_uuid ||= $uuid;
1441 $self->rev_db_set($rev, $c);
1442 print "r$rev = $c\n";
1444 command_close_pipe($rev_list, $ctx);
1445 print "Done rebuilding $self->{db_path}\n";
1448 # rev_db:
1449 # Tie::File seems to be prone to offset errors if revisions get sparse,
1450 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1451 # one of my favorite modules is out :< Next up would be one of the DBM
1452 # modules, but I'm not sure which is most portable... So I'll just
1453 # go with something that's plain-text, but still capable of
1454 # being randomly accessed. So here's my ultra-simple fixed-width
1455 # database. All records are 40 characters + "\n", so it's easy to seek
1456 # to a revision: (41 * rev) is the byte offset.
1457 # A record of 40 0s denotes an empty revision.
1458 # And yes, it's still pretty fast (faster than Tie::File).
1459 # These files are disposable unless --no-metadata is set
1461 sub rev_db_set {
1462 my ($self, $rev, $commit, $update_ref) = @_;
1463 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
1464 my ($db, $db_lock) = ($self->{db_path}, "$self->{db_path}.lock");
1465 my $sig;
1466 if ($update_ref) {
1467 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1468 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
1470 $LOCKFILES{$db_lock} = 1;
1471 if ($_no_metadata) {
1472 copy($db, $db_lock) or die "rev_db_set(@_): ",
1473 "Failed to copy: ",
1474 "$db => $db_lock ($!)\n";
1475 } else {
1476 rename $db, $db_lock or die "rev_db_set(@_): ",
1477 "Failed to rename: ",
1478 "$db => $db_lock ($!)\n";
1480 open my $fh, '+<', $db_lock or croak $!;
1481 my $offset = $rev * 41;
1482 # assume that append is the common case:
1483 seek $fh, 0, 2 or croak $!;
1484 my $pos = tell $fh;
1485 if ($pos < $offset) {
1486 for (1 .. (($offset - $pos) / 41)) {
1487 print $fh (('0' x 40),"\n") or croak $!;
1490 seek $fh, $offset, 0 or croak $!;
1491 print $fh $commit,"\n" or croak $!;
1492 close $fh or croak $!;
1493 if ($update_ref) {
1494 command_noisy('update-ref', '-m', "r$rev",
1495 $self->refname, $commit);
1497 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
1498 "$db_lock => $db ($!)\n";
1499 delete $LOCKFILES{$db_lock};
1500 if ($update_ref) {
1501 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1502 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
1503 kill $sig, $$ if defined $sig;
1507 sub rev_db_max {
1508 my ($self) = @_;
1509 my @stat = stat $self->{db_path} or
1510 die "Couldn't stat $self->{db_path}: $!\n";
1511 ($stat[7] % 41) == 0 or
1512 die "$self->{db_path} inconsistent size:$stat[7]\n";
1513 my $max = $stat[7] / 41;
1514 (($max > 0) ? $max - 1 : 0);
1517 sub rev_db_get {
1518 my ($self, $rev) = @_;
1519 my $ret;
1520 my $offset = $rev * 41;
1521 open my $fh, '<', $self->{db_path} or croak $!;
1522 if (sysseek($fh, $offset, 0) == $offset) {
1523 my $read = sysread($fh, $ret, 40);
1524 $ret = undef if ($read != 40 || $ret eq ('0'x40));
1526 close $fh or croak $!;
1527 $ret;
1530 sub find_rev_before {
1531 my ($self, $rev, $eq_ok) = @_;
1532 --$rev unless $eq_ok;
1533 while ($rev > 0) {
1534 if (my $c = $self->rev_db_get($rev)) {
1535 return ($rev, $c);
1537 --$rev;
1539 return (undef, undef);
1542 sub _new {
1543 my ($class, $repo_id, $ref_id, $path) = @_;
1544 unless (defined $repo_id && length $repo_id) {
1545 $repo_id = $Git::SVN::default_repo_id;
1547 unless (defined $ref_id && length $ref_id) {
1548 $_[2] = $ref_id = $Git::SVN::default_ref_id;
1550 $_[1] = $repo_id = sanitize_remote_name($repo_id);
1551 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
1552 $_[3] = $path = '' unless (defined $path);
1553 mkpath([$dir]);
1554 unless (-f "$dir/.rev_db") {
1555 open my $fh, '>>', "$dir/.rev_db" or croak $!;
1556 close $fh or croak $!;
1558 bless { ref_id => $ref_id, dir => $dir, index => "$dir/index",
1559 path => $path,
1560 db_path => "$dir/.rev_db", repo_id => $repo_id }, $class;
1563 sub uri_encode {
1564 my ($f) = @_;
1565 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
1569 package Git::SVN::Prompt;
1570 use strict;
1571 use warnings;
1572 require SVN::Core;
1573 use vars qw/$_no_auth_cache $_username/;
1575 sub simple {
1576 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
1577 $may_save = undef if $_no_auth_cache;
1578 $default_username = $_username if defined $_username;
1579 if (defined $default_username && length $default_username) {
1580 if (defined $realm && length $realm) {
1581 print STDERR "Authentication realm: $realm\n";
1582 STDERR->flush;
1584 $cred->username($default_username);
1585 } else {
1586 username($cred, $realm, $may_save, $pool);
1588 $cred->password(_read_password("Password for '" .
1589 $cred->username . "': ", $realm));
1590 $cred->may_save($may_save);
1591 $SVN::_Core::SVN_NO_ERROR;
1594 sub ssl_server_trust {
1595 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
1596 $may_save = undef if $_no_auth_cache;
1597 print STDERR "Error validating server certificate for '$realm':\n";
1598 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
1599 print STDERR " - The certificate is not issued by a trusted ",
1600 "authority. Use the\n",
1601 " fingerprint to validate the certificate manually!\n";
1603 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
1604 print STDERR " - The certificate hostname does not match.\n";
1606 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
1607 print STDERR " - The certificate is not yet valid.\n";
1609 if ($failures & $SVN::Auth::SSL::EXPIRED) {
1610 print STDERR " - The certificate has expired.\n";
1612 if ($failures & $SVN::Auth::SSL::OTHER) {
1613 print STDERR " - The certificate has an unknown error.\n";
1615 printf STDERR
1616 "Certificate information:\n".
1617 " - Hostname: %s\n".
1618 " - Valid: from %s until %s\n".
1619 " - Issuer: %s\n".
1620 " - Fingerprint: %s\n",
1621 map $cert_info->$_, qw(hostname valid_from valid_until
1622 issuer_dname fingerprint);
1623 my $choice;
1624 prompt:
1625 print STDERR $may_save ?
1626 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
1627 "(R)eject or accept (t)emporarily? ";
1628 STDERR->flush;
1629 $choice = lc(substr(<STDIN> || 'R', 0, 1));
1630 if ($choice =~ /^t$/i) {
1631 $cred->may_save(undef);
1632 } elsif ($choice =~ /^r$/i) {
1633 return -1;
1634 } elsif ($may_save && $choice =~ /^p$/i) {
1635 $cred->may_save($may_save);
1636 } else {
1637 goto prompt;
1639 $cred->accepted_failures($failures);
1640 $SVN::_Core::SVN_NO_ERROR;
1643 sub ssl_client_cert {
1644 my ($cred, $realm, $may_save, $pool) = @_;
1645 $may_save = undef if $_no_auth_cache;
1646 print STDERR "Client certificate filename: ";
1647 STDERR->flush;
1648 chomp(my $filename = <STDIN>);
1649 $cred->cert_file($filename);
1650 $cred->may_save($may_save);
1651 $SVN::_Core::SVN_NO_ERROR;
1654 sub ssl_client_cert_pw {
1655 my ($cred, $realm, $may_save, $pool) = @_;
1656 $may_save = undef if $_no_auth_cache;
1657 $cred->password(_read_password("Password: ", $realm));
1658 $cred->may_save($may_save);
1659 $SVN::_Core::SVN_NO_ERROR;
1662 sub username {
1663 my ($cred, $realm, $may_save, $pool) = @_;
1664 $may_save = undef if $_no_auth_cache;
1665 if (defined $realm && length $realm) {
1666 print STDERR "Authentication realm: $realm\n";
1668 my $username;
1669 if (defined $_username) {
1670 $username = $_username;
1671 } else {
1672 print STDERR "Username: ";
1673 STDERR->flush;
1674 chomp($username = <STDIN>);
1676 $cred->username($username);
1677 $cred->may_save($may_save);
1678 $SVN::_Core::SVN_NO_ERROR;
1681 sub _read_password {
1682 my ($prompt, $realm) = @_;
1683 print STDERR $prompt;
1684 STDERR->flush;
1685 require Term::ReadKey;
1686 Term::ReadKey::ReadMode('noecho');
1687 my $password = '';
1688 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
1689 last if $key =~ /[\012\015]/; # \n\r
1690 $password .= $key;
1692 Term::ReadKey::ReadMode('restore');
1693 print STDERR "\n";
1694 STDERR->flush;
1695 $password;
1698 package main;
1701 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
1702 $SVN::Node::dir.$SVN::Node::unknown.
1703 $SVN::Node::none.$SVN::Node::file.
1704 $SVN::Node::dir.$SVN::Node::unknown.
1705 $SVN::Auth::SSL::CNMISMATCH.
1706 $SVN::Auth::SSL::NOTYETVALID.
1707 $SVN::Auth::SSL::EXPIRED.
1708 $SVN::Auth::SSL::UNKNOWNCA.
1709 $SVN::Auth::SSL::OTHER;
1712 package SVN::Git::Fetcher;
1713 use vars qw/@ISA/;
1714 use strict;
1715 use warnings;
1716 use Carp qw/croak/;
1717 use IO::File qw//;
1718 use Digest::MD5;
1720 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
1721 sub new {
1722 my ($class, $git_svn) = @_;
1723 my $self = SVN::Delta::Editor->new;
1724 bless $self, $class;
1725 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
1726 $self->{empty} = {};
1727 $self->{dir_prop} = {};
1728 $self->{file_prop} = {};
1729 $self->{absent_dir} = {};
1730 $self->{absent_file} = {};
1731 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
1732 $self;
1735 sub set_path_strip {
1736 my ($self, $path) = @_;
1737 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
1740 sub open_root {
1741 { path => '' };
1744 sub open_directory {
1745 my ($self, $path, $pb, $rev) = @_;
1746 { path => $path };
1749 sub git_path {
1750 my ($self, $path) = @_;
1751 if ($self->{path_strip}) {
1752 $path =~ s!$self->{path_strip}!! or
1753 die "Failed to strip path '$path' ($self->{path_strip})\n";
1755 $path;
1758 sub delete_entry {
1759 my ($self, $path, $rev, $pb) = @_;
1761 my $gpath = $self->git_path($path);
1762 return undef if ($gpath eq '');
1764 # remove entire directories.
1765 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
1766 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
1767 -r --name-only -z/,
1768 $self->{c}, '--', $gpath);
1769 local $/ = "\0";
1770 while (<$ls>) {
1771 chomp;
1772 $self->{gii}->remove($_);
1773 print "\tD\t$_\n" unless $self->{q};
1775 print "\tD\t$gpath/\n" unless $self->{q};
1776 command_close_pipe($ls, $ctx);
1777 $self->{empty}->{$path} = 0
1778 } else {
1779 $self->{gii}->remove($gpath);
1780 print "\tD\t$gpath\n" unless $self->{q};
1782 undef;
1785 sub open_file {
1786 my ($self, $path, $pb, $rev) = @_;
1787 my $gpath = $self->git_path($path);
1788 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
1789 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
1790 unless (defined $mode && defined $blob) {
1791 die "$path was not found in commit $self->{c} (r$rev)\n";
1793 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
1794 pool => SVN::Pool->new, action => 'M' };
1797 sub add_file {
1798 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
1799 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
1800 delete $self->{empty}->{$dir};
1801 { path => $path, mode_a => 100644, mode_b => 100644,
1802 pool => SVN::Pool->new, action => 'A' };
1805 sub add_directory {
1806 my ($self, $path, $cp_path, $cp_rev) = @_;
1807 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
1808 delete $self->{empty}->{$dir};
1809 $self->{empty}->{$path} = 1;
1810 { path => $path };
1813 sub change_dir_prop {
1814 my ($self, $db, $prop, $value) = @_;
1815 $self->{dir_prop}->{$db->{path}} ||= {};
1816 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
1817 undef;
1820 sub absent_directory {
1821 my ($self, $path, $pb) = @_;
1822 $self->{absent_dir}->{$pb->{path}} ||= [];
1823 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
1824 undef;
1827 sub absent_file {
1828 my ($self, $path, $pb) = @_;
1829 $self->{absent_file}->{$pb->{path}} ||= [];
1830 push @{$self->{absent_file}->{$pb->{path}}}, $path;
1831 undef;
1834 sub change_file_prop {
1835 my ($self, $fb, $prop, $value) = @_;
1836 if ($prop eq 'svn:executable') {
1837 if ($fb->{mode_b} != 120000) {
1838 $fb->{mode_b} = defined $value ? 100755 : 100644;
1840 } elsif ($prop eq 'svn:special') {
1841 $fb->{mode_b} = defined $value ? 120000 : 100644;
1842 } else {
1843 $self->{file_prop}->{$fb->{path}} ||= {};
1844 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
1846 undef;
1849 sub apply_textdelta {
1850 my ($self, $fb, $exp) = @_;
1851 my $fh = IO::File->new_tmpfile;
1852 $fh->autoflush(1);
1853 # $fh gets auto-closed() by SVN::TxDelta::apply(),
1854 # (but $base does not,) so dup() it for reading in close_file
1855 open my $dup, '<&', $fh or croak $!;
1856 my $base = IO::File->new_tmpfile;
1857 $base->autoflush(1);
1858 if ($fb->{blob}) {
1859 defined (my $pid = fork) or croak $!;
1860 if (!$pid) {
1861 open STDOUT, '>&', $base or croak $!;
1862 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
1863 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
1865 waitpid $pid, 0;
1866 croak $? if $?;
1868 if (defined $exp) {
1869 seek $base, 0, 0 or croak $!;
1870 my $md5 = Digest::MD5->new;
1871 $md5->addfile($base);
1872 my $got = $md5->hexdigest;
1873 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
1874 "expected: $exp\n",
1875 " got: $got\n" if ($got ne $exp);
1878 seek $base, 0, 0 or croak $!;
1879 $fb->{fh} = $dup;
1880 $fb->{base} = $base;
1881 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
1884 sub close_file {
1885 my ($self, $fb, $exp) = @_;
1886 my $hash;
1887 my $path = $self->git_path($fb->{path});
1888 if (my $fh = $fb->{fh}) {
1889 seek($fh, 0, 0) or croak $!;
1890 my $md5 = Digest::MD5->new;
1891 $md5->addfile($fh);
1892 my $got = $md5->hexdigest;
1893 die "Checksum mismatch: $path\n",
1894 "expected: $exp\n got: $got\n" if ($got ne $exp);
1895 seek($fh, 0, 0) or croak $!;
1896 if ($fb->{mode_b} == 120000) {
1897 read($fh, my $buf, 5) == 5 or croak $!;
1898 $buf eq 'link ' or die "$path has mode 120000",
1899 "but is not a link\n";
1901 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
1902 if (!$pid) {
1903 open STDIN, '<&', $fh or croak $!;
1904 exec qw/git-hash-object -w --stdin/ or croak $!;
1906 chomp($hash = do { local $/; <$out> });
1907 close $out or croak $!;
1908 close $fh or croak $!;
1909 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
1910 close $fb->{base} or croak $!;
1911 } else {
1912 $hash = $fb->{blob} or die "no blob information\n";
1914 $fb->{pool}->clear;
1915 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
1916 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $self->{q};
1917 undef;
1920 sub abort_edit {
1921 my $self = shift;
1922 $self->{nr} = $self->{gii}->{nr};
1923 delete $self->{gii};
1924 $self->SUPER::abort_edit(@_);
1927 sub close_edit {
1928 my $self = shift;
1929 $self->{git_commit_ok} = 1;
1930 $self->{nr} = $self->{gii}->{nr};
1931 delete $self->{gii};
1932 $self->SUPER::close_edit(@_);
1935 package SVN::Git::Editor;
1936 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
1937 use strict;
1938 use warnings;
1939 use Carp qw/croak/;
1940 use IO::File;
1941 use Digest::MD5;
1943 sub new {
1944 my ($class, $opts) = @_;
1945 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
1946 die "$_ required!\n" unless (defined $opts->{$_});
1949 my $pool = SVN::Pool->new;
1950 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
1951 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
1952 $opts->{r}, $mods);
1954 # $opts->{ra} functions should not be used after this:
1955 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
1956 $opts->{editor_cb}, $pool);
1957 my $self = SVN::Delta::Editor->new(@ce, $pool);
1958 bless $self, $class;
1959 foreach (qw/svn_path r tree_a tree_b/) {
1960 $self->{$_} = $opts->{$_};
1962 $self->{url} = $opts->{ra}->{url};
1963 $self->{mods} = $mods;
1964 $self->{types} = $types;
1965 $self->{pool} = $pool;
1966 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
1967 $self->{rm} = { };
1968 $self->{path_prefix} = length $self->{svn_path} ?
1969 "$self->{svn_path}/" : '';
1970 return $self;
1973 sub generate_diff {
1974 my ($tree_a, $tree_b) = @_;
1975 my @diff_tree = qw(diff-tree -z -r);
1976 if ($_cp_similarity) {
1977 push @diff_tree, "-C$_cp_similarity";
1978 } else {
1979 push @diff_tree, '-C';
1981 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1982 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
1983 push @diff_tree, $tree_a, $tree_b;
1984 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
1985 local $/ = "\0";
1986 my $state = 'meta';
1987 my @mods;
1988 while (<$diff_fh>) {
1989 chomp $_; # this gets rid of the trailing "\0"
1990 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1991 $::sha1\s($::sha1)\s
1992 ([MTCRAD])\d*$/xo) {
1993 push @mods, { mode_a => $1, mode_b => $2,
1994 sha1_b => $3, chg => $4 };
1995 if ($4 =~ /^(?:C|R)$/) {
1996 $state = 'file_a';
1997 } else {
1998 $state = 'file_b';
2000 } elsif ($state eq 'file_a') {
2001 my $x = $mods[$#mods] or croak "Empty array\n";
2002 if ($x->{chg} !~ /^(?:C|R)$/) {
2003 croak "Error parsing $_, $x->{chg}\n";
2005 $x->{file_a} = $_;
2006 $state = 'file_b';
2007 } elsif ($state eq 'file_b') {
2008 my $x = $mods[$#mods] or croak "Empty array\n";
2009 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2010 croak "Error parsing $_, $x->{chg}\n";
2012 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2013 croak "Error parsing $_, $x->{chg}\n";
2015 $x->{file_b} = $_;
2016 $state = 'meta';
2017 } else {
2018 croak "Error parsing $_\n";
2021 command_close_pipe($diff_fh, $ctx);
2022 \@mods;
2025 sub check_diff_paths {
2026 my ($ra, $pfx, $rev, $mods) = @_;
2027 my %types;
2028 $pfx .= '/' if length $pfx;
2030 sub type_diff_paths {
2031 my ($ra, $types, $path, $rev) = @_;
2032 my @p = split m#/+#, $path;
2033 my $c = shift @p;
2034 unless (defined $types->{$c}) {
2035 $types->{$c} = $ra->check_path($c, $rev);
2037 while (@p) {
2038 $c .= '/' . shift @p;
2039 next if defined $types->{$c};
2040 $types->{$c} = $ra->check_path($c, $rev);
2044 foreach my $m (@$mods) {
2045 foreach my $f (qw/file_a file_b/) {
2046 next unless defined $m->{$f};
2047 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
2048 if (length $pfx.$dir && ! defined $types{$dir}) {
2049 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
2053 \%types;
2056 sub split_path {
2057 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2060 sub repo_path {
2061 my ($self, $path) = @_;
2062 $self->{path_prefix}.(defined $path ? $path : '');
2065 sub url_path {
2066 my ($self, $path) = @_;
2067 $self->{url} . '/' . $self->repo_path($path);
2070 sub rmdirs {
2071 my ($self) = @_;
2072 my $rm = $self->{rm};
2073 delete $rm->{''}; # we never delete the url we're tracking
2074 return unless %$rm;
2076 foreach (keys %$rm) {
2077 my @d = split m#/#, $_;
2078 my $c = shift @d;
2079 $rm->{$c} = 1;
2080 while (@d) {
2081 $c .= '/' . shift @d;
2082 $rm->{$c} = 1;
2085 delete $rm->{$self->{svn_path}};
2086 delete $rm->{''}; # we never delete the url we're tracking
2087 return unless %$rm;
2089 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2090 $self->{tree_b});
2091 local $/ = "\0";
2092 while (<$fh>) {
2093 chomp;
2094 my @dn = split m#/#, $_;
2095 while (pop @dn) {
2096 delete $rm->{join '/', @dn};
2098 unless (%$rm) {
2099 close $fh;
2100 return;
2103 command_close_pipe($fh, $ctx);
2105 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2106 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2107 $self->close_directory($bat->{$d}, $p);
2108 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
2109 print "\tD+\t$d/\n" unless $::_q;
2110 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2111 delete $bat->{$d};
2115 sub open_or_add_dir {
2116 my ($self, $full_path, $baton) = @_;
2117 my $t = $self->{types}->{$full_path};
2118 if (!defined $t) {
2119 die "$full_path not known in r$self->{r} or we have a bug!\n";
2121 if ($t == $SVN::Node::none) {
2122 return $self->add_directory($full_path, $baton,
2123 undef, -1, $self->{pool});
2124 } elsif ($t == $SVN::Node::dir) {
2125 return $self->open_directory($full_path, $baton,
2126 $self->{r}, $self->{pool});
2128 print STDERR "$full_path already exists in repository at ",
2129 "r$self->{r} and it is not a directory (",
2130 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2131 exit 1;
2134 sub ensure_path {
2135 my ($self, $path) = @_;
2136 my $bat = $self->{bat};
2137 my $repo_path = $self->repo_path($path);
2138 return $bat->{''} unless (length $repo_path);
2139 my @p = split m#/+#, $repo_path;
2140 my $c = shift @p;
2141 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2142 while (@p) {
2143 my $c0 = $c;
2144 $c .= '/' . shift @p;
2145 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2147 return $bat->{$c};
2150 sub A {
2151 my ($self, $m) = @_;
2152 my ($dir, $file) = split_path($m->{file_b});
2153 my $pbat = $self->ensure_path($dir);
2154 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2155 undef, -1);
2156 print "\tA\t$m->{file_b}\n" unless $::_q;
2157 $self->chg_file($fbat, $m);
2158 $self->close_file($fbat,undef,$self->{pool});
2161 sub C {
2162 my ($self, $m) = @_;
2163 my ($dir, $file) = split_path($m->{file_b});
2164 my $pbat = $self->ensure_path($dir);
2165 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2166 $self->url_path($m->{file_a}), $self->{r});
2167 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
2168 $self->chg_file($fbat, $m);
2169 $self->close_file($fbat,undef,$self->{pool});
2172 sub delete_entry {
2173 my ($self, $path, $pbat) = @_;
2174 my $rpath = $self->repo_path($path);
2175 my ($dir, $file) = split_path($rpath);
2176 $self->{rm}->{$dir} = 1;
2177 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2180 sub R {
2181 my ($self, $m) = @_;
2182 my ($dir, $file) = split_path($m->{file_b});
2183 my $pbat = $self->ensure_path($dir);
2184 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2185 $self->url_path($m->{file_a}), $self->{r});
2186 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
2187 $self->chg_file($fbat, $m);
2188 $self->close_file($fbat,undef,$self->{pool});
2190 ($dir, $file) = split_path($m->{file_a});
2191 $pbat = $self->ensure_path($dir);
2192 $self->delete_entry($m->{file_a}, $pbat);
2195 sub M {
2196 my ($self, $m) = @_;
2197 my ($dir, $file) = split_path($m->{file_b});
2198 my $pbat = $self->ensure_path($dir);
2199 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2200 $pbat,$self->{r},$self->{pool});
2201 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
2202 $self->chg_file($fbat, $m);
2203 $self->close_file($fbat,undef,$self->{pool});
2206 sub T { shift->M(@_) }
2208 sub change_file_prop {
2209 my ($self, $fbat, $pname, $pval) = @_;
2210 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2213 sub chg_file {
2214 my ($self, $fbat, $m) = @_;
2215 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2216 $self->change_file_prop($fbat,'svn:executable','*');
2217 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2218 $self->change_file_prop($fbat,'svn:executable',undef);
2220 my $fh = IO::File->new_tmpfile or croak $!;
2221 if ($m->{mode_b} =~ /^120/) {
2222 print $fh 'link ' or croak $!;
2223 $self->change_file_prop($fbat,'svn:special','*');
2224 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2225 $self->change_file_prop($fbat,'svn:special',undef);
2227 defined(my $pid = fork) or croak $!;
2228 if (!$pid) {
2229 open STDOUT, '>&', $fh or croak $!;
2230 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2232 waitpid $pid, 0;
2233 croak $? if $?;
2234 $fh->flush == 0 or croak $!;
2235 seek $fh, 0, 0 or croak $!;
2237 my $md5 = Digest::MD5->new;
2238 $md5->addfile($fh) or croak $!;
2239 seek $fh, 0, 0 or croak $!;
2241 my $exp = $md5->hexdigest;
2242 my $pool = SVN::Pool->new;
2243 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2244 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
2245 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2246 $pool->clear;
2248 close $fh or croak $!;
2251 sub D {
2252 my ($self, $m) = @_;
2253 my ($dir, $file) = split_path($m->{file_b});
2254 my $pbat = $self->ensure_path($dir);
2255 print "\tD\t$m->{file_b}\n" unless $::_q;
2256 $self->delete_entry($m->{file_b}, $pbat);
2259 sub close_edit {
2260 my ($self) = @_;
2261 my ($p,$bat) = ($self->{pool}, $self->{bat});
2262 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2263 $self->close_directory($bat->{$_}, $p);
2265 $self->SUPER::close_edit($p);
2266 $p->clear;
2269 sub abort_edit {
2270 my ($self) = @_;
2271 $self->SUPER::abort_edit($self->{pool});
2274 sub DESTROY {
2275 my $self = shift;
2276 $self->SUPER::DESTROY(@_);
2277 $self->{pool}->clear;
2280 # this drives the editor
2281 sub apply_diff {
2282 my ($self) = @_;
2283 my $mods = $self->{mods};
2284 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
2285 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
2286 my $f = $m->{chg};
2287 if (defined $o{$f}) {
2288 $self->$f($m);
2289 } else {
2290 fatal("Invalid change type: $f\n");
2293 $self->rmdirs if $_rmdir;
2294 if (@$mods == 0) {
2295 $self->abort_edit;
2296 } else {
2297 $self->close_edit;
2299 return scalar @$mods;
2302 package Git::SVN::Ra;
2303 use vars qw/@ISA $config_dir/;
2304 use strict;
2305 use warnings;
2306 my ($can_do_switch);
2307 my $RA;
2309 BEGIN {
2310 # enforce temporary pool usage for some simple functions
2311 my $e;
2312 foreach (qw/get_latest_revnum rev_proplist get_file
2313 check_path get_dir get_uuid get_repos_root/) {
2314 $e .= "sub $_ {
2315 my \$self = shift;
2316 my \$pool = SVN::Pool->new;
2317 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2318 \$pool->clear;
2319 wantarray ? \@ret : \$ret[0]; }\n";
2321 eval $e;
2324 sub new {
2325 my ($class, $url) = @_;
2326 $url =~ s!/+$!!;
2327 return $RA if ($RA && $RA->{url} eq $url);
2329 SVN::_Core::svn_config_ensure($config_dir, undef);
2330 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
2331 SVN::Client::get_simple_provider(),
2332 SVN::Client::get_ssl_server_trust_file_provider(),
2333 SVN::Client::get_simple_prompt_provider(
2334 \&Git::SVN::Prompt::simple, 2),
2335 SVN::Client::get_ssl_client_cert_prompt_provider(
2336 \&Git::SVN::Prompt::ssl_client_cert, 2),
2337 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
2338 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
2339 SVN::Client::get_username_provider(),
2340 SVN::Client::get_ssl_server_trust_prompt_provider(
2341 \&Git::SVN::Prompt::ssl_server_trust),
2342 SVN::Client::get_username_prompt_provider(
2343 \&Git::SVN::Prompt::username, 2),
2345 my $config = SVN::Core::config_get_config($config_dir);
2346 my $self = SVN::Ra->new(url => $url, auth => $baton,
2347 config => $config,
2348 pool => SVN::Pool->new,
2349 auth_provider_callbacks => $callbacks);
2350 $self->{svn_path} = $url;
2351 $self->{repos_root} = $self->get_repos_root;
2352 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
2353 $RA = bless $self, $class;
2356 sub DESTROY {
2357 # do not call the real DESTROY since we store ourselves in $RA
2360 sub get_log {
2361 my ($self, @args) = @_;
2362 my $pool = SVN::Pool->new;
2363 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
2364 my $ret = $self->SUPER::get_log(@args, $pool);
2365 $pool->clear;
2366 $ret;
2369 sub get_commit_editor {
2370 my ($self, $log, $cb, $pool) = @_;
2371 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
2372 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
2375 sub uuid {
2376 my ($self) = @_;
2377 $self->{uuid} ||= $self->get_uuid;
2380 sub gs_do_update {
2381 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2382 my $new = ($rev_a == $rev_b);
2383 my $path = $gs->{path};
2385 my $pool = SVN::Pool->new;
2386 $editor->set_path_strip($path);
2387 my (@pc) = split m#/#, $path;
2388 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
2389 1, $editor, $pool);
2390 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2392 # Since we can't rely on svn_ra_reparent being available, we'll
2393 # just have to do some magic with set_path to make it so
2394 # we only want a partial path.
2395 my $sp = '';
2396 my $final = join('/', @pc);
2397 while (@pc) {
2398 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2399 $sp .= '/' if length $sp;
2400 $sp .= shift @pc;
2402 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2404 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2406 $reporter->finish_report($pool);
2407 $pool->clear;
2408 $editor->{git_commit_ok};
2411 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2412 # svn_ra_reparent didn't work before 1.4)
2413 sub gs_do_switch {
2414 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2415 my $path = $gs->{path};
2416 my $pool = SVN::Pool->new;
2418 my $full_url = $self->{url};
2419 my $old_url = $full_url;
2420 $full_url .= "/$path" if length $path;
2421 my ($ra, $reparented);
2422 if ($old_url ne $full_url) {
2423 if ($old_url !~ m#^svn(\+ssh)?://#) {
2424 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
2425 $pool);
2426 $self->{url} = $full_url;
2427 $reparented = 1;
2428 } else {
2429 $ra = Git::SVN::Ra->new($full_url);
2432 $ra ||= $self;
2433 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
2434 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2435 $reporter->set_path('', $rev_a, 0, @lock, $pool);
2436 $reporter->finish_report($pool);
2438 if ($reparented) {
2439 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
2440 $self->{url} = $old_url;
2443 $pool->clear;
2444 $editor->{git_commit_ok};
2447 sub gs_fetch_loop_common {
2448 my ($self, $base, $head, $gsv, $globs) = @_;
2449 return if ($base > $head);
2450 my $inc = 1000;
2451 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
2452 my %common;
2453 my $common_max = scalar @$gsv;
2455 foreach my $gs (@$gsv) {
2456 if (my $last_commit = $gs->last_commit) {
2457 $gs->assert_index_clean($last_commit);
2459 my @tmp = split m#/#, $gs->{path};
2460 my $p = '';
2461 foreach (@tmp) {
2462 $p .= length($p) ? "/$_" : $_;
2463 $common{$p} ||= 0;
2464 $common{$p}++;
2467 $globs ||= [];
2468 $common_max += scalar @$globs;
2469 foreach my $glob (@$globs) {
2470 my @tmp = split m#/#, $glob->{path}->{left};
2471 my $p = '';
2472 foreach (@tmp) {
2473 $p .= length($p) ? "/$_" : $_;
2474 $common{$p} ||= 0;
2475 $common{$p}++;
2479 my $longest_path = '';
2480 foreach (sort {length $b <=> length $a} keys %common) {
2481 if ($common{$_} == $common_max) {
2482 $longest_path = $_;
2483 last;
2486 while (1) {
2487 my %revs;
2488 my $err;
2489 my $err_handler = $SVN::Error::handler;
2490 $SVN::Error::handler = sub {
2491 ($err) = @_;
2492 skip_unknown_revs($err);
2494 sub _cb {
2495 my ($paths, $r, $author, $date, $log) = @_;
2496 [ dup_changed_paths($paths),
2497 { author => $author, date => $date, log => $log } ];
2499 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
2500 sub { $revs{$_[1]} = _cb(@_) });
2501 if ($err && $max >= $head) {
2502 print STDERR "Path '$longest_path' ",
2503 "was probably deleted:\n",
2504 $err->expanded_message,
2505 "\nWill attempt to follow ",
2506 "revisions r$min .. r$max ",
2507 "committed before the deletion\n";
2508 my $hi = $max;
2509 while (--$hi >= $min) {
2510 my $ok;
2511 $self->get_log([$longest_path], $min, $hi,
2512 0, 1, 1, sub {
2513 $ok ||= $_[1];
2514 $revs{$_[1]} = _cb(@_) });
2515 if ($ok) {
2516 print STDERR "r$min .. r$ok OK\n";
2517 last;
2521 $SVN::Error::handler = $err_handler;
2523 my %exists = map { $_->{path} => $_ } @$gsv;
2524 foreach my $r (sort {$a <=> $b} keys %revs) {
2525 my ($paths, $logged) = @{$revs{$r}};
2527 foreach my $gs ($self->match_globs(\%exists, $paths,
2528 $globs, $r)) {
2529 if ($gs->rev_db_max >= $r) {
2530 next;
2532 next unless $gs->match_paths($paths, $r);
2533 $gs->{logged_rev_props} = $logged;
2534 my $log_entry = $gs->do_fetch($paths, $r);
2535 if ($log_entry) {
2536 $gs->do_git_commit($log_entry);
2539 foreach my $g (@$globs) {
2540 my $f = "$ENV{GIT_DIR}/svn/." .
2541 $self->uuid . ".$g->{t}";
2542 open my $fh, '>', "$f.tmp" or
2543 die "Can't open $f.tmp for writing: $!";
2544 print $fh "$r\n" or
2545 die "Couldn't write to $f: $!\n";
2546 close $fh or die "Error closing $f: $!\n";
2547 rename "$f.tmp", $f or
2548 die "Couldn't rename ",
2549 "$f.tmp => $f: $!\n";
2552 # pre-fill the .rev_db since it'll eventually get filled in
2553 # with '0' x40 if something new gets committed
2554 foreach my $gs (@$gsv) {
2555 next if defined $gs->rev_db_get($max);
2556 $gs->rev_db_set($max, 0 x40);
2558 last if $max >= $head;
2559 $min = $max + 1;
2560 $max += $inc;
2561 $max = $head if ($max > $head);
2565 sub match_globs {
2566 my ($self, $exists, $paths, $globs, $r) = @_;
2567 foreach my $g (@$globs) {
2568 foreach (keys %$paths) {
2569 next unless /$g->{path}->{regex}/;
2570 my $p = $1;
2571 my $pathname = $g->{path}->full_path($p);
2572 next if $exists->{$pathname};
2573 $exists->{$pathname} = Git::SVN->init(
2574 $self->{url}, $pathname, undef,
2575 $g->{ref}->full_path($p), 1);
2577 my $c = '';
2578 foreach (split m#/#, $g->{path}->{left}) {
2579 $c .= "/$_";
2580 next unless ($paths->{$c} &&
2581 ($paths->{$c}->{action} eq 'A'));
2582 my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
2583 next unless scalar @x == 3;
2584 my $dirents = $x[0];
2585 foreach my $de (keys %$dirents) {
2586 next if $dirents->{$de}->kind !=
2587 $SVN::Node::dir;
2588 my $p = $g->{path}->full_path($de);
2589 next if $exists->{$p};
2590 next if (length $g->{path}->{right} &&
2591 ($self->check_path($p, $r) !=
2592 $SVN::Node::dir));
2593 $exists->{$p} = Git::SVN->init($self->{url},
2594 $p, undef,
2595 $g->{ref}->full_path($de), 1);
2599 values %$exists;
2602 sub minimize_url {
2603 my ($self) = @_;
2604 return $self->{url} if ($self->{url} eq $self->{repos_root});
2605 my $url = $self->{repos_root};
2606 my @components = split(m!/!, $self->{svn_path});
2607 my $c = '';
2608 do {
2609 $url .= "/$c" if length $c;
2610 eval { (ref $self)->new($url)->get_latest_revnum };
2611 } while ($@ && ($c = shift @components));
2612 $url;
2615 sub can_do_switch {
2616 my $self = shift;
2617 unless (defined $can_do_switch) {
2618 my $pool = SVN::Pool->new;
2619 my $rep = eval {
2620 $self->do_switch(1, '', 0, $self->{url},
2621 SVN::Delta::Editor->new, $pool);
2623 if ($@) {
2624 $can_do_switch = 0;
2625 } else {
2626 $rep->abort_report($pool);
2627 $can_do_switch = 1;
2629 $pool->clear;
2631 $can_do_switch;
2634 sub skip_unknown_revs {
2635 my ($err) = @_;
2636 my $errno = $err->apr_err();
2637 # Maybe the branch we're tracking didn't
2638 # exist when the repo started, so it's
2639 # not an error if it doesn't, just continue
2641 # Wonderfully consistent library, eh?
2642 # 160013 - svn:// and file://
2643 # 175002 - http(s)://
2644 # 175007 - http(s):// (this repo required authorization, too...)
2645 # More codes may be discovered later...
2646 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2647 return;
2649 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2652 # svn_log_changed_path_t objects passed to get_log are likely to be
2653 # overwritten even if only the refs are copied to an external variable,
2654 # so we should dup the structures in their entirety. Using an externally
2655 # passed pool (instead of our temporary and quickly cleared pool in
2656 # Git::SVN::Ra) does not help matters at all...
2657 sub dup_changed_paths {
2658 my ($paths) = @_;
2659 return undef unless $paths;
2660 my %ret;
2661 foreach my $p (keys %$paths) {
2662 my $i = $paths->{$p};
2663 my %s = map { $_ => $i->$_ }
2664 qw/copyfrom_path copyfrom_rev action/;
2665 $ret{$p} = \%s;
2667 \%ret;
2670 package Git::SVN::Log;
2671 use strict;
2672 use warnings;
2673 use POSIX qw/strftime/;
2674 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
2675 %rusers $show_commit $incremental/;
2676 my $l_fmt;
2678 sub cmt_showable {
2679 my ($c) = @_;
2680 return 1 if defined $c->{r};
2681 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
2682 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
2683 my @log = command(qw/cat-file commit/, $c->{c});
2684 shift @log while ($log[0] ne "\n");
2685 shift @log;
2686 @{$c->{l}} = grep !/^git-svn-id: /, @log;
2688 (undef, $c->{r}, undef) = ::extract_metadata(
2689 (grep(/^git-svn-id: /, @log))[-1]);
2691 return defined $c->{r};
2694 sub log_use_color {
2695 return 1 if $color;
2696 my ($dc, $dcvar);
2697 $dcvar = 'color.diff';
2698 $dc = `git-config --get $dcvar`;
2699 if ($dc eq '') {
2700 # nothing at all; fallback to "diff.color"
2701 $dcvar = 'diff.color';
2702 $dc = `git-config --get $dcvar`;
2704 chomp($dc);
2705 if ($dc eq 'auto') {
2706 my $pc;
2707 $pc = `git-config --get color.pager`;
2708 if ($pc eq '') {
2709 # does not have it -- fallback to pager.color
2710 $pc = `git-config --bool --get pager.color`;
2712 else {
2713 $pc = `git-config --bool --get color.pager`;
2714 if ($?) {
2715 $pc = 'false';
2718 chomp($pc);
2719 if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
2720 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
2722 return 0;
2724 return 0 if $dc eq 'never';
2725 return 1 if $dc eq 'always';
2726 chomp($dc = `git-config --bool --get $dcvar`);
2727 return ($dc eq 'true');
2730 sub git_svn_log_cmd {
2731 my ($r_min, $r_max) = @_;
2732 my $gs = Git::SVN->_new;
2733 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
2734 $gs->refname);
2735 push @cmd, '-r' unless $non_recursive;
2736 push @cmd, qw/--raw --name-status/ if $verbose;
2737 push @cmd, '--color' if log_use_color();
2738 return @cmd unless defined $r_max;
2739 if ($r_max == $r_min) {
2740 push @cmd, '--max-count=1';
2741 if (my $c = $gs->rev_db_get($r_max)) {
2742 push @cmd, $c;
2744 } else {
2745 my ($c_min, $c_max);
2746 $c_max = $gs->rev_db_get($r_max);
2747 $c_min = $gs->rev_db_get($r_min);
2748 if (defined $c_min && defined $c_max) {
2749 if ($r_max > $r_max) {
2750 push @cmd, "$c_min..$c_max";
2751 } else {
2752 push @cmd, "$c_max..$c_min";
2754 } elsif ($r_max > $r_min) {
2755 push @cmd, $c_max;
2756 } else {
2757 push @cmd, $c_min;
2760 return @cmd;
2763 # adapted from pager.c
2764 sub config_pager {
2765 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
2766 if (!defined $pager) {
2767 $pager = 'less';
2768 } elsif (length $pager == 0 || $pager eq 'cat') {
2769 $pager = undef;
2773 sub run_pager {
2774 return unless -t *STDOUT;
2775 pipe my $rfd, my $wfd or return;
2776 defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
2777 if (!$pid) {
2778 open STDOUT, '>&', $wfd or
2779 ::fatal "Can't redirect to stdout: $!\n";
2780 return;
2782 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n";
2783 $ENV{LESS} ||= 'FRSX';
2784 exec $pager or ::fatal "Can't run pager: $! ($pager)\n";
2787 sub tz_to_s_offset {
2788 my ($tz) = @_;
2789 $tz =~ s/(\d\d)$//;
2790 return ($1 * 60) + ($tz * 3600);
2793 sub get_author_info {
2794 my ($dest, $author, $t, $tz) = @_;
2795 $author =~ s/(?:^\s*|\s*$)//g;
2796 $dest->{a_raw} = $author;
2797 my $au;
2798 if ($::_authors) {
2799 $au = $rusers{$author} || undef;
2801 if (!$au) {
2802 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
2804 $dest->{t} = $t;
2805 $dest->{tz} = $tz;
2806 $dest->{a} = $au;
2807 # Date::Parse isn't in the standard Perl distro :(
2808 if ($tz =~ s/^\+//) {
2809 $t += tz_to_s_offset($tz);
2810 } elsif ($tz =~ s/^\-//) {
2811 $t -= tz_to_s_offset($tz);
2813 $dest->{t_utc} = $t;
2816 sub process_commit {
2817 my ($c, $r_min, $r_max, $defer) = @_;
2818 if (defined $r_min && defined $r_max) {
2819 if ($r_min == $c->{r} && $r_min == $r_max) {
2820 show_commit($c);
2821 return 0;
2823 return 1 if $r_min == $r_max;
2824 if ($r_min < $r_max) {
2825 # we need to reverse the print order
2826 return 0 if (defined $limit && --$limit < 0);
2827 push @$defer, $c;
2828 return 1;
2830 if ($r_min != $r_max) {
2831 return 1 if ($r_min < $c->{r});
2832 return 1 if ($r_max > $c->{r});
2835 return 0 if (defined $limit && --$limit < 0);
2836 show_commit($c);
2837 return 1;
2840 sub show_commit {
2841 my $c = shift;
2842 if ($oneline) {
2843 my $x = "\n";
2844 if (my $l = $c->{l}) {
2845 while ($l->[0] =~ /^\s*$/) { shift @$l }
2846 $x = $l->[0];
2848 $l_fmt ||= 'A' . length($c->{r});
2849 print 'r',pack($l_fmt, $c->{r}),' | ';
2850 print "$c->{c} | " if $show_commit;
2851 print $x;
2852 } else {
2853 show_commit_normal($c);
2857 sub show_commit_changed_paths {
2858 my ($c) = @_;
2859 return unless $c->{changed};
2860 print "Changed paths:\n", @{$c->{changed}};
2863 sub show_commit_normal {
2864 my ($c) = @_;
2865 print '-' x72, "\nr$c->{r} | ";
2866 print "$c->{c} | " if $show_commit;
2867 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2868 localtime($c->{t_utc})), ' | ';
2869 my $nr_line = 0;
2871 if (my $l = $c->{l}) {
2872 while ($l->[$#$l] eq "\n" && $#$l > 0
2873 && $l->[($#$l - 1)] eq "\n") {
2874 pop @$l;
2876 $nr_line = scalar @$l;
2877 if (!$nr_line) {
2878 print "1 line\n\n\n";
2879 } else {
2880 if ($nr_line == 1) {
2881 $nr_line = '1 line';
2882 } else {
2883 $nr_line .= ' lines';
2885 print $nr_line, "\n";
2886 show_commit_changed_paths($c);
2887 print "\n";
2888 print $_ foreach @$l;
2890 } else {
2891 print "1 line\n";
2892 show_commit_changed_paths($c);
2893 print "\n";
2896 foreach my $x (qw/raw diff/) {
2897 if ($c->{$x}) {
2898 print "\n";
2899 print $_ foreach @{$c->{$x}}
2904 sub cmd_show_log {
2905 my (@args) = @_;
2906 my ($r_min, $r_max);
2907 my $r_last = -1; # prevent dupes
2908 if (defined $TZ) {
2909 $ENV{TZ} = $TZ;
2910 } else {
2911 delete $ENV{TZ};
2913 if (defined $::_revision) {
2914 if ($::_revision =~ /^(\d+):(\d+)$/) {
2915 ($r_min, $r_max) = ($1, $2);
2916 } elsif ($::_revision =~ /^\d+$/) {
2917 $r_min = $r_max = $::_revision;
2918 } else {
2919 ::fatal "-r$::_revision is not supported, use ",
2920 "standard \'git log\' arguments instead\n";
2924 config_pager();
2925 @args = (git_svn_log_cmd($r_min, $r_max), @args);
2926 my $log = command_output_pipe(@args);
2927 run_pager();
2928 my (@k, $c, $d);
2929 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
2930 while (<$log>) {
2931 if (/^${esc_color}commit ($::sha1_short)/o) {
2932 my $cmt = $1;
2933 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
2934 $r_last = $c->{r};
2935 process_commit($c, $r_min, $r_max, \@k) or
2936 goto out;
2938 $d = undef;
2939 $c = { c => $cmt };
2940 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
2941 get_author_info($c, $1, $2, $3);
2942 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
2943 # ignore
2944 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
2945 push @{$c->{raw}}, $_;
2946 } elsif (/^${esc_color}[ACRMDT]\t/) {
2947 # we could add $SVN->{svn_path} here, but that requires
2948 # remote access at the moment (repo_path_split)...
2949 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
2950 push @{$c->{changed}}, $_;
2951 } elsif (/^${esc_color}diff /o) {
2952 $d = 1;
2953 push @{$c->{diff}}, $_;
2954 } elsif ($d) {
2955 push @{$c->{diff}}, $_;
2956 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
2957 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
2958 } elsif (s/^${esc_color} //o) {
2959 push @{$c->{l}}, $_;
2962 if ($c && defined $c->{r} && $c->{r} != $r_last) {
2963 $r_last = $c->{r};
2964 process_commit($c, $r_min, $r_max, \@k);
2966 if (@k) {
2967 my $swap = $r_max;
2968 $r_max = $r_min;
2969 $r_min = $swap;
2970 process_commit($_, $r_min, $r_max) foreach reverse @k;
2972 out:
2973 close $log;
2974 print '-' x72,"\n" unless $incremental || $oneline;
2977 package Git::SVN::Migration;
2978 # these version numbers do NOT correspond to actual version numbers
2979 # of git nor git-svn. They are just relative.
2981 # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
2983 # v1 layout: .git/$id/info/url, refs/remotes/$id
2985 # v2 layout: .git/svn/$id/info/url, refs/remotes/$id
2987 # v3 layout: .git/svn/$id, refs/remotes/$id
2988 # - info/url may remain for backwards compatibility
2989 # - this is what we migrate up to this layout automatically,
2990 # - this will be used by git svn init on single branches
2992 # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
2993 # - this is only created for newly multi-init-ed
2994 # repositories. Similar in spirit to the
2995 # --use-separate-remotes option in git-clone (now default)
2996 # - we do not automatically migrate to this (following
2997 # the example set by core git)
2998 use strict;
2999 use warnings;
3000 use Carp qw/croak/;
3001 use File::Path qw/mkpath/;
3002 use File::Basename qw/dirname basename/;
3003 use vars qw/$_minimize/;
3005 sub migrate_from_v0 {
3006 my $git_dir = $ENV{GIT_DIR};
3007 return undef unless -d $git_dir;
3008 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3009 my $migrated = 0;
3010 while (<$fh>) {
3011 chomp;
3012 my ($id, $orig_ref) = ($_, $_);
3013 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
3014 next unless -f "$git_dir/$id/info/url";
3015 my $new_ref = "refs/remotes/$id";
3016 if (::verify_ref("$new_ref^0")) {
3017 print STDERR "W: $orig_ref is probably an old ",
3018 "branch used by an ancient version of ",
3019 "git-svn.\n",
3020 "However, $new_ref also exists.\n",
3021 "We will not be able ",
3022 "to use this branch until this ",
3023 "ambiguity is resolved.\n";
3024 next;
3026 print STDERR "Migrating from v0 layout...\n" if !$migrated;
3027 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
3028 command_noisy('update-ref', $new_ref, $orig_ref);
3029 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
3030 $migrated++;
3032 command_close_pipe($fh, $ctx);
3033 print STDERR "Done migrating from v0 layout...\n" if $migrated;
3034 $migrated;
3037 sub migrate_from_v1 {
3038 my $git_dir = $ENV{GIT_DIR};
3039 my $migrated = 0;
3040 return $migrated unless -d $git_dir;
3041 my $svn_dir = "$git_dir/svn";
3043 # just in case somebody used 'svn' as their $id at some point...
3044 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
3046 print STDERR "Migrating from a git-svn v1 layout...\n";
3047 mkpath([$svn_dir]);
3048 print STDERR "Data from a previous version of git-svn exists, but\n\t",
3049 "$svn_dir\n\t(required for this version ",
3050 "($::VERSION) of git-svn) does not. exist\n";
3051 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3052 while (<$fh>) {
3053 my $x = $_;
3054 next unless $x =~ s#^refs/remotes/##;
3055 chomp $x;
3056 next unless -f "$git_dir/$x/info/url";
3057 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
3058 next unless $u;
3059 my $dn = dirname("$git_dir/svn/$x");
3060 mkpath([$dn]) unless -d $dn;
3061 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3062 mkpath(["$git_dir/svn/svn"]);
3063 print STDERR " - $git_dir/$x/info => ",
3064 "$git_dir/svn/$x/info\n";
3065 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3066 croak "$!: $x";
3067 # don't worry too much about these, they probably
3068 # don't exist with repos this old (save for index,
3069 # and we can easily regenerate that)
3070 foreach my $f (qw/unhandled.log index .rev_db/) {
3071 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3073 } else {
3074 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
3075 rename "$git_dir/$x", "$git_dir/svn/$x" or
3076 croak "$!: $x";
3078 $migrated++;
3080 command_close_pipe($fh, $ctx);
3081 print STDERR "Done migrating from a git-svn v1 layout\n";
3082 $migrated;
3085 sub read_old_urls {
3086 my ($l_map, $pfx, $path) = @_;
3087 my @dir;
3088 foreach (<$path/*>) {
3089 if (-r "$_/info/url") {
3090 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
3091 my $ref_id = $pfx . basename $_;
3092 my $url = ::file_to_s("$_/info/url");
3093 $l_map->{$ref_id} = $url;
3094 } elsif (-d $_) {
3095 push @dir, $_;
3098 foreach (@dir) {
3099 my $x = $_;
3100 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3101 read_old_urls($l_map, $x, $_);
3105 sub migrate_from_v2 {
3106 my @cfg = command(qw/config -l/);
3107 return if grep /^svn-remote\..+\.url=/, @cfg;
3108 my %l_map;
3109 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
3110 my $migrated = 0;
3112 foreach my $ref_id (sort keys %l_map) {
3113 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
3114 if ($@) {
3115 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3117 $migrated++;
3119 $migrated;
3122 sub minimize_connections {
3123 my $r = Git::SVN::read_all_remotes();
3124 my $new_urls = {};
3125 my $root_repos = {};
3126 foreach my $repo_id (keys %$r) {
3127 my $url = $r->{$repo_id}->{url} or next;
3128 my $fetch = $r->{$repo_id}->{fetch} or next;
3129 my $ra = Git::SVN::Ra->new($url);
3131 # skip existing cases where we already connect to the root
3132 if (($ra->{url} eq $ra->{repos_root}) ||
3133 (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
3134 $repo_id)) {
3135 $root_repos->{$ra->{url}} = $repo_id;
3136 next;
3139 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
3140 my $root_path = $ra->{url};
3141 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
3142 foreach my $path (keys %$fetch) {
3143 my $ref_id = $fetch->{$path};
3144 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
3146 # make sure we can read when connecting to
3147 # a higher level of a repository
3148 my ($last_rev, undef) = $gs->last_rev_commit;
3149 if (!defined $last_rev) {
3150 $last_rev = eval {
3151 $root_ra->get_latest_revnum;
3153 next if $@;
3155 my $new = $root_path;
3156 $new .= length $path ? "/$path" : '';
3157 eval {
3158 $root_ra->get_log([$new], $last_rev, $last_rev,
3159 0, 0, 1, sub { });
3161 next if $@;
3162 $new_urls->{$ra->{repos_root}}->{$new} =
3163 { ref_id => $ref_id,
3164 old_repo_id => $repo_id,
3165 old_path => $path };
3169 my @emptied;
3170 foreach my $url (keys %$new_urls) {
3171 # see if we can re-use an existing [svn-remote "repo_id"]
3172 # instead of creating a(n ugly) new section:
3173 my $repo_id = $root_repos->{$url} ||
3174 Git::SVN::sanitize_remote_name($url);
3176 my $fetch = $new_urls->{$url};
3177 foreach my $path (keys %$fetch) {
3178 my $x = $fetch->{$path};
3179 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
3180 my $pfx = "svn-remote.$x->{old_repo_id}";
3182 my $old_fetch = quotemeta("$x->{old_path}:".
3183 "refs/remotes/$x->{ref_id}");
3184 command_noisy(qw/config --unset/,
3185 "$pfx.fetch", '^'. $old_fetch . '$');
3186 delete $r->{$x->{old_repo_id}}->
3187 {fetch}->{$x->{old_path}};
3188 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
3189 command_noisy(qw/config --unset/,
3190 "$pfx.url");
3191 push @emptied, $x->{old_repo_id}
3195 if (@emptied) {
3196 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
3197 "$ENV{GIT_DIR}/config";
3198 print STDERR <<EOF;
3199 The following [svn-remote] sections in your config file ($file) are empty
3200 and can be safely removed:
3202 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
3206 sub migration_check {
3207 migrate_from_v0();
3208 migrate_from_v1();
3209 migrate_from_v2();
3210 minimize_connections() if $_minimize;
3213 package Git::IndexInfo;
3214 use strict;
3215 use warnings;
3216 use Git qw/command_input_pipe command_close_pipe/;
3218 sub new {
3219 my ($class) = @_;
3220 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
3221 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
3224 sub remove {
3225 my ($self, $path) = @_;
3226 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
3227 return ++$self->{nr};
3229 undef;
3232 sub update {
3233 my ($self, $mode, $hash, $path) = @_;
3234 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
3235 return ++$self->{nr};
3237 undef;
3240 sub DESTROY {
3241 my ($self) = @_;
3242 command_close_pipe($self->{gui}, $self->{ctx});
3245 package Git::SVN::GlobSpec;
3246 use strict;
3247 use warnings;
3249 sub new {
3250 my ($class, $glob) = @_;
3251 my $re = $glob;
3252 $re =~ s!/+$!!g; # no need for trailing slashes
3253 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
3254 my ($left, $right) = ($1, $2);
3255 if ($nr > 1) {
3256 die "Only one '*' wildcard expansion ",
3257 "is supported (got $nr): '$glob'\n";
3258 } elsif ($nr == 0) {
3259 die "One '*' is needed for glob: '$glob'\n";
3261 $re = quotemeta($left) . $re . quotemeta($right);
3262 if (length $left && !($left =~ s!/+$!!g)) {
3263 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3265 if (length $right && !($right =~ s!^/+!!g)) {
3266 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3268 bless { left => $left, right => $right,
3269 regex => qr/$re/, glob => $glob }, $class;
3272 sub full_path {
3273 my ($self, $path) = @_;
3274 return (length $self->{left} ? "$self->{left}/" : '') .
3275 $path . (length $self->{right} ? "/$self->{right}" : '');
3278 __END__
3280 Data structures:
3283 $remotes = { # returned by read_all_remotes()
3284 'svn' => {
3285 # svn-remote.svn.url=https://svn.musicpd.org
3286 url => 'https://svn.musicpd.org',
3287 # svn-remote.svn.fetch=mpd/trunk:trunk
3288 fetch => {
3289 'mpd/trunk' => 'trunk',
3291 # svn-remote.svn.tags=mpd/tags/*:tags/*
3292 tags => {
3293 path => {
3294 left => 'mpd/tags',
3295 right => '',
3296 regex => qr!mpd/tags/([^/]+)$!,
3297 glob => 'tags/*',
3299 ref => {
3300 left => 'tags',
3301 right => '',
3302 regex => qr!tags/([^/]+)$!,
3303 glob => 'tags/*',
3309 $log_entry hashref as returned by libsvn_log_entry()
3311 log => 'whitespace-formatted log entry
3312 ', # trailing newline is preserved
3313 revision => '8', # integer
3314 date => '2004-02-24T17:01:44.108345Z', # commit date
3315 author => 'committer name'
3319 # this is generated by generate_diff();
3320 @mods = array of diff-index line hashes, each element represents one line
3321 of diff-index output
3323 diff-index line ($m hash)
3325 mode_a => first column of diff-index output, no leading ':',
3326 mode_b => second column of diff-index output,
3327 sha1_b => sha1sum of the final blob,
3328 chg => change type [MCRADT],
3329 file_a => original file name of a file (iff chg is 'C' or 'R')
3330 file_b => new/current file name of a file (any chg)
3334 # retval of read_url_paths{,_all}();
3335 $l_map = {
3336 # repository root url
3337 'https://svn.musicpd.org' => {
3338 # repository path # GIT_SVN_ID
3339 'mpd/trunk' => 'trunk',
3340 'mpd/tags/0.11.5' => 'tags/0.11.5',
3344 Notes:
3345 I don't trust the each() function on unless I created %hash myself
3346 because the internal iterator may not have started at base.