git-svn: multi-init saves and reuses --tags and --branches arguments
[git/gitweb.git] / git-svn.perl
blob5a6c87e612ca3c0d2ca5ee54e60bf2547b6b397b
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 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $GIT_SVN_DIR $REVDB/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '@@GIT_VERSION@@';
13 use Cwd qw/abs_path/;
14 $GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15 $ENV{GIT_DIR} = $GIT_DIR;
17 my $LC_ALL = $ENV{LC_ALL};
18 my $TZ = $ENV{TZ};
19 # make sure the svn binary gives consistent output between locales and TZs:
20 $ENV{TZ} = 'UTC';
21 $ENV{LC_ALL} = 'C';
22 $| = 1; # unbuffer STDOUT
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
27 # We don't use any modules not in the standard Perl distribution:
28 use Carp qw/croak/;
29 use IO::File qw//;
30 use File::Basename qw/dirname basename/;
31 use File::Path qw/mkpath/;
32 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
33 use File::Spec qw//;
34 use File::Copy qw/copy/;
35 use POSIX qw/strftime/;
36 use IPC::Open3;
37 use Memoize;
38 memoize('revisions_eq');
39 memoize('cmt_metadata');
40 memoize('get_commit_time');
42 my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
43 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB};
44 libsvn_load();
45 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
46 my $sha1 = qr/[a-f\d]{40}/;
47 my $sha1_short = qr/[a-f\d]{4,40}/;
48 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
49 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
50 $_repack, $_repack_nr, $_repack_flags, $_q,
51 $_message, $_file, $_follow_parent, $_no_metadata,
52 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
53 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
54 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
55 $_merge, $_strategy, $_dry_run, $_ignore_nodate);
56 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
57 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
58 my @repo_path_split_cache;
60 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
61 'branch|b=s' => \@_branch_from,
62 'follow-parent|follow' => \$_follow_parent,
63 'branch-all-refs|B' => \$_branch_all_refs,
64 'authors-file|A=s' => \$_authors,
65 'repack:i' => \$_repack,
66 'no-metadata' => \$_no_metadata,
67 'quiet|q' => \$_q,
68 'ignore-nodate' => \$_ignore_nodate,
69 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
71 my ($_trunk, $_tags, $_branches);
72 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
73 'tags|t=s' => \$_tags,
74 'branches|b=s' => \$_branches );
75 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
76 my %cmt_opts = ( 'edit|e' => \$_edit,
77 'rmdir' => \$_rmdir,
78 'find-copies-harder' => \$_find_copies_harder,
79 'l=i' => \$_l,
80 'copy-similarity|C=i'=> \$_cp_similarity
83 my %cmd = (
84 fetch => [ \&fetch, "Download new revisions from SVN",
85 { 'revision|r=s' => \$_revision, %fc_opts } ],
86 init => [ \&init, "Initialize a repo for tracking" .
87 " (requires URL argument)",
88 \%init_opts ],
89 commit => [ \&commit, "Commit git revisions to SVN",
90 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
91 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
92 { 'revision|r=i' => \$_revision } ],
93 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
94 { 'no-ignore-externals' => \$_no_ignore_ext,
95 'copy-remote|remote=s' => \$_cp_remote,
96 'upgrade' => \$_upgrade } ],
97 'graft-branches' => [ \&graft_branches,
98 'Detect merges/branches from already imported history',
99 { 'merge-rx|m' => \@_opt_m,
100 'branch|b=s' => \@_branch_from,
101 'branch-all-refs|B' => \$_branch_all_refs,
102 'no-default-regex' => \$_no_default_regex,
103 'no-graft-copy' => \$_no_graft_copy } ],
104 'multi-init' => [ \&multi_init,
105 'Initialize multiple trees (like git-svnimport)',
106 { %multi_opts, %fc_opts } ],
107 'multi-fetch' => [ \&multi_fetch,
108 'Fetch multiple trees (like git-svnimport)',
109 \%fc_opts ],
110 'log' => [ \&show_log, 'Show commit logs',
111 { 'limit=i' => \$_limit,
112 'revision|r=s' => \$_revision,
113 'verbose|v' => \$_verbose,
114 'incremental' => \$_incremental,
115 'oneline' => \$_oneline,
116 'show-commit' => \$_show_commit,
117 'authors-file|A=s' => \$_authors,
118 } ],
119 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
120 { 'message|m=s' => \$_message,
121 'file|F=s' => \$_file,
122 %cmt_opts } ],
123 dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
124 { 'merge|m|M' => \$_merge,
125 'strategy|s=s' => \$_strategy,
126 'dry-run|n' => \$_dry_run,
127 %cmt_opts } ],
130 my $cmd;
131 for (my $i = 0; $i < @ARGV; $i++) {
132 if (defined $cmd{$ARGV[$i]}) {
133 $cmd = $ARGV[$i];
134 splice @ARGV, $i, 1;
135 last;
139 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
141 read_repo_config(\%opts);
142 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
143 'version|V' => \$_version,
144 'id|i=s' => \$GIT_SVN);
145 exit 1 if (!$rv && $cmd ne 'log');
147 set_default_vals();
148 usage(0) if $_help;
149 version() if $_version;
150 usage(1) unless defined $cmd;
151 init_vars();
152 load_authors() if $_authors;
153 load_all_refs() if $_branch_all_refs;
154 svn_compat_check() unless $_use_lib;
155 migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
156 $cmd{$cmd}->[0]->(@ARGV);
157 exit 0;
159 ####################### primary functions ######################
160 sub usage {
161 my $exit = shift || 0;
162 my $fd = $exit ? \*STDERR : \*STDOUT;
163 print $fd <<"";
164 git-svn - bidirectional operations between a single Subversion tree and git
165 Usage: $0 <command> [options] [arguments]\n
167 print $fd "Available commands:\n" unless $cmd;
169 foreach (sort keys %cmd) {
170 next if $cmd && $cmd ne $_;
171 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
172 foreach (keys %{$cmd{$_}->[2]}) {
173 # prints out arguments as they should be passed:
174 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
175 print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
176 "--$_" : "-$_" }
177 split /\|/,$_)," $x\n";
180 print $fd <<"";
181 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
182 arbitrary identifier if you're tracking multiple SVN branches/repositories in
183 one git repository and want to keep them separate. See git-svn(1) for more
184 information.
186 exit $exit;
189 sub version {
190 print "git-svn version $VERSION\n";
191 exit 0;
194 sub rebuild {
195 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
196 copy_remote_ref();
198 $SVN_URL = shift or undef;
199 my $newest_rev = 0;
200 if ($_upgrade) {
201 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
202 } else {
203 check_upgrade_needed();
206 my $pid = open(my $rev_list,'-|');
207 defined $pid or croak $!;
208 if ($pid == 0) {
209 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
211 my $latest;
212 while (<$rev_list>) {
213 chomp;
214 my $c = $_;
215 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
216 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
217 next if (!@commit); # skip merges
218 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
219 if (!$rev || !$uuid) {
220 croak "Unable to extract revision or UUID from ",
221 "$c, $commit[$#commit]\n";
224 # if we merged or otherwise started elsewhere, this is
225 # how we break out of it
226 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
227 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
229 unless (defined $latest) {
230 if (!$SVN_URL && !$url) {
231 croak "SVN repository location required: $url\n";
233 $SVN_URL ||= $url;
234 $SVN_UUID ||= $uuid;
235 setup_git_svn();
236 $latest = $rev;
238 revdb_set($REVDB, $rev, $c);
239 print "r$rev = $c\n";
240 $newest_rev = $rev if ($rev > $newest_rev);
242 close $rev_list or croak $?;
244 goto out if $_use_lib;
245 if (!chdir $SVN_WC) {
246 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
247 chdir $SVN_WC or croak $!;
250 $pid = fork;
251 defined $pid or croak $!;
252 if ($pid == 0) {
253 my @svn_up = qw(svn up);
254 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
255 sys(@svn_up,"-r$newest_rev");
256 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
257 index_changes();
258 exec('git-write-tree') or croak $!;
260 waitpid $pid, 0;
261 croak $? if $?;
262 out:
263 if ($_upgrade) {
264 print STDERR <<"";
265 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
266 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
271 sub init {
272 my $url = shift or die "SVN repository location required " .
273 "as a command-line argument\n";
274 $url =~ s!/+$!!; # strip trailing slash
276 if (my $repo_path = shift) {
277 unless (-d $repo_path) {
278 mkpath([$repo_path]);
280 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
281 init_vars();
284 $SVN_URL = $url;
285 unless (-d $GIT_DIR) {
286 my @init_db = ('git-init-db');
287 push @init_db, "--template=$_template" if defined $_template;
288 push @init_db, "--shared" if defined $_shared;
289 sys(@init_db);
291 setup_git_svn();
294 sub fetch {
295 check_upgrade_needed();
296 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
297 my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
298 if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
299 refs/heads/master^0))) {
300 sys(qw(git-update-ref refs/heads/master),$ret->{commit});
302 return $ret;
305 sub fetch_cmd {
306 my (@parents) = @_;
307 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
308 unless ($_revision) {
309 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
311 push @log_args, "-r$_revision";
312 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
314 my $svn_log = svn_log_raw(@log_args);
316 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
317 # don't need last_revision from grab_base_rev() because
318 # user could've specified a different revision to skip (they
319 # didn't want to import certain revisions into git for whatever
320 # reason, so trust $base->{revision} instead.
321 my (undef, $last_commit) = svn_grab_base_rev();
322 unless (-d $SVN_WC) {
323 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
324 chdir $SVN_WC or croak $!;
325 read_uuid();
326 $last_commit = git_commit($base, @parents);
327 assert_tree($last_commit);
328 } else {
329 chdir $SVN_WC or croak $!;
330 read_uuid();
331 # looks like a user manually cp'd and svn switch'ed
332 unless ($last_commit) {
333 sys(qw/svn revert -R ./);
334 assert_svn_wc_clean($base->{revision});
335 $last_commit = git_commit($base, @parents);
336 assert_tree($last_commit);
339 my @svn_up = qw(svn up);
340 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
341 my $last = $base;
342 while (my $log_msg = next_log_entry($svn_log)) {
343 if ($last->{revision} >= $log_msg->{revision}) {
344 croak "Out of order: last >= current: ",
345 "$last->{revision} >= $log_msg->{revision}\n";
347 # Revert is needed for cases like:
348 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
349 # I can't seem to reproduce something like that on a test...
350 sys(qw/svn revert -R ./);
351 assert_svn_wc_clean($last->{revision});
352 sys(@svn_up,"-r$log_msg->{revision}");
353 $last_commit = git_commit($log_msg, $last_commit, @parents);
354 $last = $log_msg;
356 close $svn_log->{fh};
357 $last->{commit} = $last_commit;
358 return $last;
361 sub fetch_lib {
362 my (@parents) = @_;
363 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
364 my $repo;
365 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
366 $SVN_LOG ||= libsvn_connect($repo);
367 $SVN ||= libsvn_connect($repo);
368 my ($last_rev, $last_commit) = svn_grab_base_rev();
369 my ($base, $head) = libsvn_parse_revision($last_rev);
370 if ($base > $head) {
371 return { revision => $last_rev, commit => $last_commit }
373 my $index = set_index($GIT_SVN_INDEX);
375 # limit ourselves and also fork() since get_log won't release memory
376 # after processing a revision and SVN stuff seems to leak
377 my $inc = 1000;
378 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
379 read_uuid();
380 if (defined $last_commit) {
381 unless (-e $GIT_SVN_INDEX) {
382 sys(qw/git-read-tree/, $last_commit);
384 chomp (my $x = `git-write-tree`);
385 my ($y) = (`git-cat-file commit $last_commit`
386 =~ /^tree ($sha1)/m);
387 if ($y ne $x) {
388 unlink $GIT_SVN_INDEX or croak $!;
389 sys(qw/git-read-tree/, $last_commit);
391 chomp ($x = `git-write-tree`);
392 if ($y ne $x) {
393 print STDERR "trees ($last_commit) $y != $x\n",
394 "Something is seriously wrong...\n";
397 while (1) {
398 # fork, because using SVN::Pool with get_log() still doesn't
399 # seem to help enough to keep memory usage down.
400 defined(my $pid = fork) or croak $!;
401 if (!$pid) {
402 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
404 # Yes I'm perfectly aware that the fourth argument
405 # below is the limit revisions number. Unfortunately
406 # performance sucks with it enabled, so it's much
407 # faster to fetch revision ranges instead of relying
408 # on the limiter.
409 libsvn_get_log($SVN_LOG, '/'.$SVN_PATH,
410 $min, $max, 0, 1, 1,
411 sub {
412 my $log_msg;
413 if ($last_commit) {
414 $log_msg = libsvn_fetch(
415 $last_commit, @_);
416 $last_commit = git_commit(
417 $log_msg,
418 $last_commit,
419 @parents);
420 } else {
421 $log_msg = libsvn_new_tree(@_);
422 $last_commit = git_commit(
423 $log_msg, @parents);
426 exit 0;
428 waitpid $pid, 0;
429 croak $? if $?;
430 ($last_rev, $last_commit) = svn_grab_base_rev();
431 last if ($max >= $head);
432 $min = $max + 1;
433 $max += $inc;
434 $max = $head if ($max > $head);
436 restore_index($index);
437 return { revision => $last_rev, commit => $last_commit };
440 sub commit {
441 my (@commits) = @_;
442 check_upgrade_needed();
443 if ($_stdin || !@commits) {
444 print "Reading from stdin...\n";
445 @commits = ();
446 while (<STDIN>) {
447 if (/\b($sha1_short)\b/o) {
448 unshift @commits, $1;
452 my @revs;
453 foreach my $c (@commits) {
454 chomp(my @tmp = safe_qx('git-rev-parse',$c));
455 if (scalar @tmp == 1) {
456 push @revs, $tmp[0];
457 } elsif (scalar @tmp > 1) {
458 push @revs, reverse (safe_qx('git-rev-list',@tmp));
459 } else {
460 die "Failed to rev-parse $c\n";
463 chomp @revs;
464 $_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
465 print "Done committing ",scalar @revs," revisions to SVN\n";
468 sub commit_cmd {
469 my (@revs) = @_;
471 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
472 my $info = svn_info('.');
473 my $fetched = fetch();
474 if ($info->{Revision} != $fetched->{revision}) {
475 print STDERR "There are new revisions that were fetched ",
476 "and need to be merged (or acknowledged) ",
477 "before committing.\n";
478 exit 1;
480 $info = svn_info('.');
481 read_uuid($info);
482 my $last = $fetched;
483 foreach my $c (@revs) {
484 my $mods = svn_checkout_tree($last, $c);
485 if (scalar @$mods == 0) {
486 print "Skipping, no changes detected\n";
487 next;
489 $last = svn_commit_tree($last, $c);
493 sub commit_lib {
494 my (@revs) = @_;
495 my ($r_last, $cmt_last) = svn_grab_base_rev();
496 defined $r_last or die "Must have an existing revision to commit\n";
497 my $fetched = fetch();
498 if ($r_last != $fetched->{revision}) {
499 print STDERR "There are new revisions that were fetched ",
500 "and need to be merged (or acknowledged) ",
501 "before committing.\n",
502 "last rev: $r_last\n",
503 " current: $fetched->{revision}\n";
504 exit 1;
506 read_uuid();
507 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
508 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
510 my $repo;
511 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
512 set_svn_commit_env();
513 foreach my $c (@revs) {
514 my $log_msg = get_commit_message($c, $commit_msg);
516 # fork for each commit because there's a memory leak I
517 # can't track down... (it's probably in the SVN code)
518 defined(my $pid = open my $fh, '-|') or croak $!;
519 if (!$pid) {
520 $SVN_LOG = libsvn_connect($repo);
521 $SVN = libsvn_connect($repo);
522 my $ed = SVN::Git::Editor->new(
523 { r => $r_last,
524 ra => $SVN,
525 c => $c,
526 svn_path => $SVN_PATH
528 $SVN->get_commit_editor(
529 $log_msg->{msg},
530 sub {
531 libsvn_commit_cb(
532 @_, $c,
533 $log_msg->{msg},
534 $r_last,
535 $cmt_last)
537 @lock)
539 my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
540 if (@$mods == 0) {
541 print "No changes\nr$r_last = $cmt_last\n";
542 $ed->abort_edit;
543 } else {
544 $ed->close_edit;
546 exit 0;
548 my ($r_new, $cmt_new, $no);
549 while (<$fh>) {
550 print $_;
551 chomp;
552 if (/^r(\d+) = ($sha1)$/o) {
553 ($r_new, $cmt_new) = ($1, $2);
554 } elsif ($_ eq 'No changes') {
555 $no = 1;
558 close $fh or croak $?;
559 if (! defined $r_new && ! defined $cmt_new) {
560 unless ($no) {
561 die "Failed to parse revision information\n";
563 } else {
564 ($r_last, $cmt_last) = ($r_new, $cmt_new);
567 $ENV{LC_ALL} = 'C';
568 unlink $commit_msg;
571 sub dcommit {
572 my $gs = "refs/remotes/$GIT_SVN";
573 chomp(my @refs = safe_qx(qw/git-rev-list --no-merges/, "$gs..HEAD"));
574 foreach my $d (reverse @refs) {
575 if ($_dry_run) {
576 print "diff-tree $d~1 $d\n";
577 } else {
578 commit_diff("$d~1", $d);
581 return if $_dry_run;
582 fetch();
583 my @diff = safe_qx(qw/git-diff-tree HEAD/, $gs);
584 my @finish;
585 if (@diff) {
586 @finish = qw/rebase/;
587 push @finish, qw/--merge/ if $_merge;
588 push @finish, "--strategy=$_strategy" if $_strategy;
589 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
590 } else {
591 print "No changes between current HEAD and $gs\n",
592 "Hard resetting to the latest $gs\n";
593 @finish = qw/reset --hard/;
595 sys('git', @finish, $gs);
598 sub show_ignore {
599 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
600 $_use_lib ? show_ignore_lib() : show_ignore_cmd();
603 sub show_ignore_cmd {
604 require File::Find or die $!;
605 if (defined $_revision) {
606 die "-r/--revision option doesn't work unless the Perl SVN ",
607 "libraries are used\n";
609 chdir $SVN_WC or croak $!;
610 my %ign;
611 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
612 s#^\./##;
613 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
614 }}, no_chdir=>1},'.');
616 print "\n# /\n";
617 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
618 delete $ign{'.'};
619 foreach my $i (sort keys %ign) {
620 print "\n# ",$i,"\n";
621 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
625 sub show_ignore_lib {
626 my $repo;
627 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
628 $SVN ||= libsvn_connect($repo);
629 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
630 libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
633 sub graft_branches {
634 my $gr_file = "$GIT_DIR/info/grafts";
635 my ($grafts, $comments) = read_grafts($gr_file);
636 my $gr_sha1;
638 if (%$grafts) {
639 # temporarily disable our grafts file to make this idempotent
640 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
641 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
644 my $l_map = read_url_paths();
645 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
646 unless ($_no_default_regex) {
647 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
648 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
649 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
651 foreach my $u (keys %$l_map) {
652 if (@re) {
653 foreach my $p (keys %{$l_map->{$u}}) {
654 graft_merge_msg($grafts,$l_map,$u,$p,@re);
657 unless ($_no_graft_copy) {
658 if ($_use_lib) {
659 graft_file_copy_lib($grafts,$l_map,$u);
660 } else {
661 graft_file_copy_cmd($grafts,$l_map,$u);
665 graft_tree_joins($grafts);
667 write_grafts($grafts, $comments, $gr_file);
668 unlink "$gr_file~$gr_sha1" if $gr_sha1;
671 sub multi_init {
672 my $url = shift;
673 $_trunk ||= 'trunk';
674 $_trunk =~ s#/+$##;
675 $url =~ s#/+$## if $url;
676 if ($_trunk !~ m#^[a-z\+]+://#) {
677 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
678 unless ($url) {
679 print STDERR "E: '$_trunk' is not a complete URL ",
680 "and a separate URL is not specified\n";
681 exit 1;
683 $_trunk = $url . $_trunk;
685 my $ch_id;
686 if ($GIT_SVN eq 'git-svn') {
687 $ch_id = 1;
688 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
690 init_vars();
691 unless (-d $GIT_SVN_DIR) {
692 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
693 init($_trunk);
694 sys('git-repo-config', 'svn.trunk', $_trunk);
696 complete_url_ls_init($url, $_branches, '--branches/-b', '');
697 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
700 sub multi_fetch {
701 # try to do trunk first, since branches/tags
702 # may be descended from it.
703 if (-e "$GIT_DIR/svn/trunk/info/url") {
704 fetch_child_id('trunk', @_);
706 rec_fetch('', "$GIT_DIR/svn", @_);
709 sub show_log {
710 my (@args) = @_;
711 my ($r_min, $r_max);
712 my $r_last = -1; # prevent dupes
713 rload_authors() if $_authors;
714 if (defined $TZ) {
715 $ENV{TZ} = $TZ;
716 } else {
717 delete $ENV{TZ};
719 if (defined $_revision) {
720 if ($_revision =~ /^(\d+):(\d+)$/) {
721 ($r_min, $r_max) = ($1, $2);
722 } elsif ($_revision =~ /^\d+$/) {
723 $r_min = $r_max = $_revision;
724 } else {
725 print STDERR "-r$_revision is not supported, use ",
726 "standard \'git log\' arguments instead\n";
727 exit 1;
731 my $pid = open(my $log,'-|');
732 defined $pid or croak $!;
733 if (!$pid) {
734 exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
736 setup_pager();
737 my (@k, $c, $d);
739 while (<$log>) {
740 if (/^commit ($sha1_short)/o) {
741 my $cmt = $1;
742 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
743 $r_last = $c->{r};
744 process_commit($c, $r_min, $r_max, \@k) or
745 goto out;
747 $d = undef;
748 $c = { c => $cmt };
749 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
750 get_author_info($c, $1, $2, $3);
751 } elsif (/^(?:tree|parent|committer) /) {
752 # ignore
753 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
754 push @{$c->{raw}}, $_;
755 } elsif (/^diff /) {
756 $d = 1;
757 push @{$c->{diff}}, $_;
758 } elsif ($d) {
759 push @{$c->{diff}}, $_;
760 } elsif (/^ (git-svn-id:.+)$/) {
761 (undef, $c->{r}, undef) = extract_metadata($1);
762 } elsif (s/^ //) {
763 push @{$c->{l}}, $_;
766 if ($c && defined $c->{r} && $c->{r} != $r_last) {
767 $r_last = $c->{r};
768 process_commit($c, $r_min, $r_max, \@k);
770 if (@k) {
771 my $swap = $r_max;
772 $r_max = $r_min;
773 $r_min = $swap;
774 process_commit($_, $r_min, $r_max) foreach reverse @k;
776 out:
777 close $log;
778 print '-' x72,"\n" unless $_incremental || $_oneline;
781 sub commit_diff_usage {
782 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
783 exit 1
786 sub commit_diff {
787 if (!$_use_lib) {
788 print STDERR "commit-diff must be used with SVN libraries\n";
789 exit 1;
791 my $ta = shift or commit_diff_usage();
792 my $tb = shift or commit_diff_usage();
793 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
794 print STDERR "Needed URL or usable git-svn id command-line\n";
795 commit_diff_usage();
797 if (defined $_message && defined $_file) {
798 print STDERR "Both --message/-m and --file/-F specified ",
799 "for the commit message.\n",
800 "I have no idea what you mean\n";
801 exit 1;
803 if (defined $_file) {
804 $_message = file_to_s($_file);
805 } else {
806 $_message ||= get_commit_message($tb,
807 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
809 my $repo;
810 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
811 $SVN_LOG ||= libsvn_connect($repo);
812 $SVN ||= libsvn_connect($repo);
813 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
814 my $ed = SVN::Git::Editor->new({ r => $SVN->get_latest_revnum,
815 ra => $SVN, c => $tb,
816 svn_path => $SVN_PATH
818 $SVN->get_commit_editor($_message,
819 sub {print "Committed $_[0]\n"},@lock)
821 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
822 if (@$mods == 0) {
823 print "No changes\n$ta == $tb\n";
824 $ed->abort_edit;
825 } else {
826 $ed->close_edit;
828 $_message = $_file = undef;
831 ########################### utility functions #########################
833 sub cmt_showable {
834 my ($c) = @_;
835 return 1 if defined $c->{r};
836 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
837 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
838 my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
839 shift @msg while ($msg[0] ne "\n");
840 shift @msg;
841 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
843 (undef, $c->{r}, undef) = extract_metadata(
844 (grep(/^git-svn-id: /, @msg))[-1]);
846 return defined $c->{r};
849 sub git_svn_log_cmd {
850 my ($r_min, $r_max) = @_;
851 my @cmd = (qw/git-log --abbrev-commit --pretty=raw
852 --default/, "refs/remotes/$GIT_SVN");
853 push @cmd, '--summary' if $_verbose;
854 return @cmd unless defined $r_max;
855 if ($r_max == $r_min) {
856 push @cmd, '--max-count=1';
857 if (my $c = revdb_get($REVDB, $r_max)) {
858 push @cmd, $c;
860 } else {
861 my ($c_min, $c_max);
862 $c_max = revdb_get($REVDB, $r_max);
863 $c_min = revdb_get($REVDB, $r_min);
864 if ($c_min && $c_max) {
865 if ($r_max > $r_max) {
866 push @cmd, "$c_min..$c_max";
867 } else {
868 push @cmd, "$c_max..$c_min";
870 } elsif ($r_max > $r_min) {
871 push @cmd, $c_max;
872 } else {
873 push @cmd, $c_min;
876 return @cmd;
879 sub fetch_child_id {
880 my $id = shift;
881 print "Fetching $id\n";
882 my $ref = "$GIT_DIR/refs/remotes/$id";
883 defined(my $pid = open my $fh, '-|') or croak $!;
884 if (!$pid) {
885 $_repack = undef;
886 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
887 init_vars();
888 fetch(@_);
889 exit 0;
891 while (<$fh>) {
892 print $_;
893 check_repack() if (/^r\d+ = $sha1/);
895 close $fh or croak $?;
898 sub rec_fetch {
899 my ($pfx, $p, @args) = @_;
900 my @dir;
901 foreach (sort <$p/*>) {
902 if (-r "$_/info/url") {
903 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
904 my $id = $pfx . basename $_;
905 next if $id eq 'trunk';
906 fetch_child_id($id, @args);
907 } elsif (-d $_) {
908 push @dir, $_;
911 foreach (@dir) {
912 my $x = $_;
913 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
914 rec_fetch($x, $_);
918 sub complete_url_ls_init {
919 my ($url, $var, $switch, $pfx) = @_;
920 unless ($var) {
921 print STDERR "W: $switch not specified\n";
922 return;
924 $var =~ s#/+$##;
925 if ($var !~ m#^[a-z\+]+://#) {
926 $var = '/' . $var if ($var !~ m#^/#);
927 unless ($url) {
928 print STDERR "E: '$var' is not a complete URL ",
929 "and a separate URL is not specified\n";
930 exit 1;
932 $var = $url . $var;
934 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
935 : safe_qx(qw/svn ls --non-interactive/, $var));
936 my $old = $GIT_SVN;
937 defined(my $pid = fork) or croak $!;
938 if (!$pid) {
939 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
940 $u =~ s#/+$##;
941 if ($u !~ m!\Q$var\E/(.+)$!) {
942 print STDERR "W: Unrecognized URL: $u\n";
943 die "This should never happen\n";
945 # don't try to init already existing refs
946 my $id = $pfx.$1;
947 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
948 init_vars();
949 unless (-d $GIT_SVN_DIR) {
950 print "init $u => $id\n";
951 init($u);
954 exit 0;
956 waitpid $pid, 0;
957 croak $? if $?;
958 my ($n) = ($switch =~ /^--(\w+)/);
959 sys('git-repo-config', "svn.$n", $var);
962 sub common_prefix {
963 my $paths = shift;
964 my %common;
965 foreach (@$paths) {
966 my @tmp = split m#/#, $_;
967 my $p = '';
968 while (my $x = shift @tmp) {
969 $p .= "/$x";
970 $common{$p} ||= 0;
971 $common{$p}++;
974 foreach (sort {length $b <=> length $a} keys %common) {
975 if ($common{$_} == @$paths) {
976 return $_;
979 return '';
982 # grafts set here are 'stronger' in that they're based on actual tree
983 # matches, and won't be deleted from merge-base checking in write_grafts()
984 sub graft_tree_joins {
985 my $grafts = shift;
986 map_tree_joins() if (@_branch_from && !%tree_map);
987 return unless %tree_map;
989 git_svn_each(sub {
990 my $i = shift;
991 defined(my $pid = open my $fh, '-|') or croak $!;
992 if (!$pid) {
993 exec qw/git-rev-list --pretty=raw/,
994 "refs/remotes/$i" or croak $!;
996 while (<$fh>) {
997 next unless /^commit ($sha1)$/o;
998 my $c = $1;
999 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1000 next unless $tree_map{$t};
1002 my $l;
1003 do {
1004 $l = readline $fh;
1005 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1007 my ($s, $tz) = ($1, $2);
1008 if ($tz =~ s/^\+//) {
1009 $s += tz_to_s_offset($tz);
1010 } elsif ($tz =~ s/^\-//) {
1011 $s -= tz_to_s_offset($tz);
1014 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
1016 foreach my $p (@{$tree_map{$t}}) {
1017 next if $p eq $c;
1018 my $mb = eval {
1019 safe_qx('git-merge-base', $c, $p)
1021 next unless ($@ || $?);
1022 if (defined $r_a) {
1023 # see if SVN says it's a relative
1024 my ($url_b, $r_b, $uuid_b) =
1025 cmt_metadata($p);
1026 next if (defined $url_b &&
1027 defined $url_a &&
1028 ($url_a eq $url_b) &&
1029 ($uuid_a eq $uuid_b));
1030 if ($uuid_a eq $uuid_b) {
1031 if ($r_b < $r_a) {
1032 $grafts->{$c}->{$p} = 2;
1033 next;
1034 } elsif ($r_b > $r_a) {
1035 $grafts->{$p}->{$c} = 2;
1036 next;
1040 my $ct = get_commit_time($p);
1041 if ($ct < $s) {
1042 $grafts->{$c}->{$p} = 2;
1043 } elsif ($ct > $s) {
1044 $grafts->{$p}->{$c} = 2;
1046 # what should we do when $ct == $s ?
1049 close $fh or croak $?;
1053 # this isn't funky-filename safe, but good enough for now...
1054 sub graft_file_copy_cmd {
1055 my ($grafts, $l_map, $u) = @_;
1056 my $paths = $l_map->{$u};
1057 my $pfx = common_prefix([keys %$paths]);
1058 $SVN_URL ||= $u.$pfx;
1059 my $pid = open my $fh, '-|';
1060 defined $pid or croak $!;
1061 unless ($pid) {
1062 my @exec = qw/svn log -v/;
1063 push @exec, "-r$_revision" if defined $_revision;
1064 exec @exec, $u.$pfx or croak $!;
1066 my ($r, $mp) = (undef, undef);
1067 while (<$fh>) {
1068 chomp;
1069 if (/^\-{72}$/) {
1070 $mp = $r = undef;
1071 } elsif (/^r(\d+) \| /) {
1072 $r = $1 unless defined $r;
1073 } elsif (/^Changed paths:/) {
1074 $mp = 1;
1075 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1076 my ($p1, $p0, $r0) = ($1, $2, $3);
1077 my $c = find_graft_path_commit($paths, $p1, $r);
1078 next unless $c;
1079 find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
1084 sub graft_file_copy_lib {
1085 my ($grafts, $l_map, $u) = @_;
1086 my $tree_paths = $l_map->{$u};
1087 my $pfx = common_prefix([keys %$tree_paths]);
1088 my ($repo, $path) = repo_path_split($u.$pfx);
1089 $SVN_LOG ||= libsvn_connect($repo);
1090 $SVN ||= libsvn_connect($repo);
1092 my ($base, $head) = libsvn_parse_revision();
1093 my $inc = 1000;
1094 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
1095 my $eh = $SVN::Error::handler;
1096 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
1097 while (1) {
1098 my $pool = SVN::Pool->new;
1099 libsvn_get_log($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1100 sub {
1101 libsvn_graft_file_copies($grafts, $tree_paths,
1102 $path, @_);
1103 }, $pool);
1104 $pool->clear;
1105 last if ($max >= $head);
1106 $min = $max + 1;
1107 $max += $inc;
1108 $max = $head if ($max > $head);
1110 $SVN::Error::handler = $eh;
1113 sub process_merge_msg_matches {
1114 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1115 my (@strong, @weak);
1116 foreach (@matches) {
1117 # merging with ourselves is not interesting
1118 next if $_ eq $p;
1119 if ($l_map->{$u}->{$_}) {
1120 push @strong, $_;
1121 } else {
1122 push @weak, $_;
1125 foreach my $w (@weak) {
1126 last if @strong;
1127 # no exact match, use branch name as regexp.
1128 my $re = qr/\Q$w\E/i;
1129 foreach (keys %{$l_map->{$u}}) {
1130 if (/$re/) {
1131 push @strong, $l_map->{$u}->{$_};
1132 last;
1135 last if @strong;
1136 $w = basename($w);
1137 $re = qr/\Q$w\E/i;
1138 foreach (keys %{$l_map->{$u}}) {
1139 if (/$re/) {
1140 push @strong, $l_map->{$u}->{$_};
1141 last;
1145 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1146 \s(?:[a-f\d\-]+)$/xsm);
1147 unless (defined $rev) {
1148 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1149 \@(?:[a-f\d\-]+)/xsm);
1150 return unless defined $rev;
1152 foreach my $m (@strong) {
1153 my ($r0, $s0) = find_rev_before($rev, $m, 1);
1154 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1158 sub graft_merge_msg {
1159 my ($grafts, $l_map, $u, $p, @re) = @_;
1161 my $x = $l_map->{$u}->{$p};
1162 my $rl = rev_list_raw($x);
1163 while (my $c = next_rev_list_entry($rl)) {
1164 foreach my $re (@re) {
1165 my (@br) = ($c->{m} =~ /$re/g);
1166 next unless @br;
1167 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1172 sub read_uuid {
1173 return if $SVN_UUID;
1174 if ($_use_lib) {
1175 my $pool = SVN::Pool->new;
1176 $SVN_UUID = $SVN->get_uuid($pool);
1177 $pool->clear;
1178 } else {
1179 my $info = shift || svn_info('.');
1180 $SVN_UUID = $info->{'Repository UUID'} or
1181 croak "Repository UUID unreadable\n";
1185 sub quiet_run {
1186 my $pid = fork;
1187 defined $pid or croak $!;
1188 if (!$pid) {
1189 open my $null, '>', '/dev/null' or croak $!;
1190 open STDERR, '>&', $null or croak $!;
1191 open STDOUT, '>&', $null or croak $!;
1192 exec @_ or croak $!;
1194 waitpid $pid, 0;
1195 return $?;
1198 sub repo_path_split {
1199 my $full_url = shift;
1200 $full_url =~ s#/+$##;
1202 foreach (@repo_path_split_cache) {
1203 if ($full_url =~ s#$_##) {
1204 my $u = $1;
1205 $full_url =~ s#^/+##;
1206 return ($u, $full_url);
1210 if ($_use_lib) {
1211 my $tmp = libsvn_connect($full_url);
1212 my $url = $tmp->get_repos_root;
1213 $full_url =~ s#^\Q$url\E/*##;
1214 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1215 return ($url, $full_url);
1216 } else {
1217 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1218 $path =~ s#^/+##;
1219 my @paths = split(m#/+#, $path);
1220 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
1221 my $n = shift @paths || last;
1222 $url .= "/$n";
1224 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1225 $path = join('/',@paths);
1226 return ($url, $path);
1230 sub setup_git_svn {
1231 defined $SVN_URL or croak "SVN repository location required\n";
1232 unless (-d $GIT_DIR) {
1233 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1235 mkpath([$GIT_SVN_DIR]);
1236 mkpath(["$GIT_SVN_DIR/info"]);
1237 open my $fh, '>>',$REVDB or croak $!;
1238 close $fh;
1239 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
1243 sub assert_svn_wc_clean {
1244 return if $_use_lib;
1245 my ($svn_rev) = @_;
1246 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1247 my $lcr = svn_info('.')->{'Last Changed Rev'};
1248 if ($svn_rev != $lcr) {
1249 print STDERR "Checking for copy-tree ... ";
1250 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
1251 "-r$lcr:$svn_rev")));
1252 if (@diff) {
1253 croak "Nope! Expected r$svn_rev, got r$lcr\n";
1254 } else {
1255 print STDERR "OK!\n";
1258 my @status = grep(!/^Performing status on external/,(`svn status`));
1259 @status = grep(!/^\s*$/,@status);
1260 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1261 if (scalar @status) {
1262 print STDERR "Tree ($SVN_WC) is not clean:\n";
1263 print STDERR $_ foreach @status;
1264 croak;
1268 sub get_tree_from_treeish {
1269 my ($treeish) = @_;
1270 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1271 chomp(my $type = `git-cat-file -t $treeish`);
1272 my $expected;
1273 while ($type eq 'tag') {
1274 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1276 if ($type eq 'commit') {
1277 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1278 ($expected) = ($expected =~ /^tree ($sha1)$/);
1279 die "Unable to get tree from $treeish\n" unless $expected;
1280 } elsif ($type eq 'tree') {
1281 $expected = $treeish;
1282 } else {
1283 die "$treeish is a $type, expected tree, tag or commit\n";
1285 return $expected;
1288 sub assert_tree {
1289 return if $_use_lib;
1290 my ($treeish) = @_;
1291 my $expected = get_tree_from_treeish($treeish);
1293 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1294 if (-e $tmpindex) {
1295 unlink $tmpindex or croak $!;
1297 my $old_index = set_index($tmpindex);
1298 index_changes(1);
1299 chomp(my $tree = `git-write-tree`);
1300 restore_index($old_index);
1301 if ($tree ne $expected) {
1302 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
1304 unlink $tmpindex;
1307 sub parse_diff_tree {
1308 my $diff_fh = shift;
1309 local $/ = "\0";
1310 my $state = 'meta';
1311 my @mods;
1312 while (<$diff_fh>) {
1313 chomp $_; # this gets rid of the trailing "\0"
1314 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1315 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1316 push @mods, { mode_a => $1, mode_b => $2,
1317 sha1_b => $3, chg => $4 };
1318 if ($4 =~ /^(?:C|R)$/) {
1319 $state = 'file_a';
1320 } else {
1321 $state = 'file_b';
1323 } elsif ($state eq 'file_a') {
1324 my $x = $mods[$#mods] or croak "Empty array\n";
1325 if ($x->{chg} !~ /^(?:C|R)$/) {
1326 croak "Error parsing $_, $x->{chg}\n";
1328 $x->{file_a} = $_;
1329 $state = 'file_b';
1330 } elsif ($state eq 'file_b') {
1331 my $x = $mods[$#mods] or croak "Empty array\n";
1332 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1333 croak "Error parsing $_, $x->{chg}\n";
1335 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1336 croak "Error parsing $_, $x->{chg}\n";
1338 $x->{file_b} = $_;
1339 $state = 'meta';
1340 } else {
1341 croak "Error parsing $_\n";
1344 close $diff_fh or croak $?;
1346 return \@mods;
1349 sub svn_check_prop_executable {
1350 my $m = shift;
1351 return if -l $m->{file_b};
1352 if ($m->{mode_b} =~ /755$/) {
1353 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
1354 if ($m->{mode_a} !~ /755$/) {
1355 sys(qw(svn propset svn:executable 1), $m->{file_b});
1357 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
1358 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
1359 sys(qw(svn propdel svn:executable), $m->{file_b});
1360 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
1361 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
1365 sub svn_ensure_parent_path {
1366 my $dir_b = dirname(shift);
1367 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
1368 mkpath([$dir_b]) unless (-d $dir_b);
1369 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
1372 sub precommit_check {
1373 my $mods = shift;
1374 my (%rm_file, %rmdir_check, %added_check);
1376 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
1377 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1378 if ($m->{chg} eq 'R') {
1379 if (-d $m->{file_b}) {
1380 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1382 # dir/$file => dir/file/$file
1383 my $dirname = dirname($m->{file_b});
1384 while ($dirname ne File::Spec->curdir) {
1385 if ($dirname ne $m->{file_a}) {
1386 $dirname = dirname($dirname);
1387 next;
1389 err_file_to_dir("$m->{file_a} => $m->{file_b}");
1391 # baz/zzz => baz (baz is a file)
1392 $dirname = dirname($m->{file_a});
1393 while ($dirname ne File::Spec->curdir) {
1394 if ($dirname ne $m->{file_b}) {
1395 $dirname = dirname($dirname);
1396 next;
1398 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1401 if ($m->{chg} =~ /^(D|R)$/) {
1402 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
1403 $rm_file{ $m->{$t} } = 1;
1404 my $dirname = dirname( $m->{$t} );
1405 my $basename = basename( $m->{$t} );
1406 $rmdir_check{$dirname}->{$basename} = 1;
1407 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
1408 if (-d $m->{file_b}) {
1409 err_dir_to_file($m->{file_b});
1411 my $dirname = dirname( $m->{file_b} );
1412 my $basename = basename( $m->{file_b} );
1413 $added_check{$dirname}->{$basename} = 1;
1414 while ($dirname ne File::Spec->curdir) {
1415 if ($rm_file{$dirname}) {
1416 err_file_to_dir($m->{file_b});
1418 $dirname = dirname $dirname;
1422 return (\%rmdir_check, \%added_check);
1424 sub err_dir_to_file {
1425 my $file = shift;
1426 print STDERR "Node change from directory to file ",
1427 "is not supported by Subversion: ",$file,"\n";
1428 exit 1;
1430 sub err_file_to_dir {
1431 my $file = shift;
1432 print STDERR "Node change from file to directory ",
1433 "is not supported by Subversion: ",$file,"\n";
1434 exit 1;
1439 sub get_diff {
1440 my ($from, $treeish) = @_;
1441 assert_tree($from);
1442 print "diff-tree $from $treeish\n";
1443 my $pid = open my $diff_fh, '-|';
1444 defined $pid or croak $!;
1445 if ($pid == 0) {
1446 my @diff_tree = qw(git-diff-tree -z -r);
1447 if ($_cp_similarity) {
1448 push @diff_tree, "-C$_cp_similarity";
1449 } else {
1450 push @diff_tree, '-C';
1452 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1453 push @diff_tree, "-l$_l" if defined $_l;
1454 exec(@diff_tree, $from, $treeish) or croak $!;
1456 return parse_diff_tree($diff_fh);
1459 sub svn_checkout_tree {
1460 my ($from, $treeish) = @_;
1461 my $mods = get_diff($from->{commit}, $treeish);
1462 return $mods unless (scalar @$mods);
1463 my ($rm, $add) = precommit_check($mods);
1465 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1466 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1467 if ($m->{chg} eq 'C') {
1468 svn_ensure_parent_path( $m->{file_b} );
1469 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
1470 apply_mod_line_blob($m);
1471 svn_check_prop_executable($m);
1472 } elsif ($m->{chg} eq 'D') {
1473 sys(qw(svn rm --force), $m->{file_b});
1474 } elsif ($m->{chg} eq 'R') {
1475 svn_ensure_parent_path( $m->{file_b} );
1476 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
1477 apply_mod_line_blob($m);
1478 svn_check_prop_executable($m);
1479 } elsif ($m->{chg} eq 'M') {
1480 apply_mod_line_blob($m);
1481 svn_check_prop_executable($m);
1482 } elsif ($m->{chg} eq 'T') {
1483 sys(qw(svn rm --force),$m->{file_b});
1484 apply_mod_line_blob($m);
1485 sys(qw(svn add), $m->{file_b});
1486 svn_check_prop_executable($m);
1487 } elsif ($m->{chg} eq 'A') {
1488 svn_ensure_parent_path( $m->{file_b} );
1489 apply_mod_line_blob($m);
1490 sys(qw(svn add), $m->{file_b});
1491 svn_check_prop_executable($m);
1492 } else {
1493 croak "Invalid chg: $m->{chg}\n";
1497 assert_tree($treeish);
1498 if ($_rmdir) { # remove empty directories
1499 handle_rmdir($rm, $add);
1501 assert_tree($treeish);
1502 return $mods;
1505 sub libsvn_checkout_tree {
1506 my ($from, $treeish, $ed) = @_;
1507 my $mods = get_diff($from, $treeish);
1508 return $mods unless (scalar @$mods);
1509 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1510 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1511 my $f = $m->{chg};
1512 if (defined $o{$f}) {
1513 $ed->$f($m, $_q);
1514 } else {
1515 croak "Invalid change type: $f\n";
1518 $ed->rmdirs($_q) if $_rmdir;
1519 return $mods;
1522 # svn ls doesn't work with respect to the current working tree, but what's
1523 # in the repository. There's not even an option for it... *sigh*
1524 # (added files don't show up and removed files remain in the ls listing)
1525 sub svn_ls_current {
1526 my ($dir, $rm, $add) = @_;
1527 chomp(my @ls = safe_qx('svn','ls',$dir));
1528 my @ret = ();
1529 foreach (@ls) {
1530 s#/$##; # trailing slashes are evil
1531 push @ret, $_ unless $rm->{$dir}->{$_};
1533 if (exists $add->{$dir}) {
1534 push @ret, keys %{$add->{$dir}};
1536 return \@ret;
1539 sub handle_rmdir {
1540 my ($rm, $add) = @_;
1542 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1543 my $ls = svn_ls_current($dir, $rm, $add);
1544 next if (scalar @$ls);
1545 sys(qw(svn rm --force),$dir);
1547 my $dn = dirname $dir;
1548 $rm->{ $dn }->{ basename $dir } = 1;
1549 $ls = svn_ls_current($dn, $rm, $add);
1550 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1551 sys(qw(svn rm --force),$dn);
1552 $dir = basename $dn;
1553 $dn = dirname $dn;
1554 $rm->{ $dn }->{ $dir } = 1;
1555 $ls = svn_ls_current($dn, $rm, $add);
1560 sub get_commit_message {
1561 my ($commit, $commit_msg) = (@_);
1562 my %log_msg = ( msg => '' );
1563 open my $msg, '>', $commit_msg or croak $!;
1565 chomp(my $type = `git-cat-file -t $commit`);
1566 if ($type eq 'commit' || $type eq 'tag') {
1567 my $pid = open my $msg_fh, '-|';
1568 defined $pid or croak $!;
1570 if ($pid == 0) {
1571 exec('git-cat-file', $type, $commit) or croak $!;
1573 my $in_msg = 0;
1574 while (<$msg_fh>) {
1575 if (!$in_msg) {
1576 $in_msg = 1 if (/^\s*$/);
1577 } elsif (/^git-svn-id: /) {
1578 # skip this, we regenerate the correct one
1579 # on re-fetch anyways
1580 } else {
1581 print $msg $_ or croak $!;
1584 close $msg_fh or croak $?;
1586 close $msg or croak $!;
1588 if ($_edit || ($type eq 'tree')) {
1589 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1590 system($editor, $commit_msg);
1593 # file_to_s removes all trailing newlines, so just use chomp() here:
1594 open $msg, '<', $commit_msg or croak $!;
1595 { local $/; chomp($log_msg{msg} = <$msg>); }
1596 close $msg or croak $!;
1598 return \%log_msg;
1601 sub set_svn_commit_env {
1602 if (defined $LC_ALL) {
1603 $ENV{LC_ALL} = $LC_ALL;
1604 } else {
1605 delete $ENV{LC_ALL};
1609 sub svn_commit_tree {
1610 my ($last, $commit) = @_;
1611 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1612 my $log_msg = get_commit_message($commit, $commit_msg);
1613 my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1614 print "Committing $commit: $oneline\n";
1616 set_svn_commit_env();
1617 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1618 $ENV{LC_ALL} = 'C';
1619 unlink $commit_msg;
1620 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1621 if (!defined $committed) {
1622 my $out = join("\n",@ci_output);
1623 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1624 $out, "\n\nAssuming English locale...";
1625 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1626 defined $committed or die " FAILED!\n",
1627 "Commit output failed to parse committed revision!\n",
1628 print STDERR " OK\n";
1631 my @svn_up = qw(svn up);
1632 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1633 if ($_optimize_commits && ($committed == ($last->{revision} + 1))) {
1634 push @svn_up, "-r$committed";
1635 sys(@svn_up);
1636 my $info = svn_info('.');
1637 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1638 if ($info->{'Last Changed Rev'} != $committed) {
1639 croak "$info->{'Last Changed Rev'} != $committed\n"
1641 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1642 /(\d{4})\-(\d\d)\-(\d\d)\s
1643 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1644 or croak "Failed to parse date: $date\n";
1645 $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
1646 $log_msg->{author} = $info->{'Last Changed Author'};
1647 $log_msg->{revision} = $committed;
1648 $log_msg->{msg} .= "\n";
1649 $log_msg->{parents} = [ $last->{commit} ];
1650 $log_msg->{commit} = git_commit($log_msg, $commit);
1651 return $log_msg;
1653 # resync immediately
1654 push @svn_up, "-r$last->{revision}";
1655 sys(@svn_up);
1656 return fetch("$committed=$commit");
1659 sub rev_list_raw {
1660 my (@args) = @_;
1661 my $pid = open my $fh, '-|';
1662 defined $pid or croak $!;
1663 if (!$pid) {
1664 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1666 return { fh => $fh, t => { } };
1669 sub next_rev_list_entry {
1670 my $rl = shift;
1671 my $fh = $rl->{fh};
1672 my $x = $rl->{t};
1673 while (<$fh>) {
1674 if (/^commit ($sha1)$/o) {
1675 if ($x->{c}) {
1676 $rl->{t} = { c => $1 };
1677 return $x;
1678 } else {
1679 $x->{c} = $1;
1681 } elsif (/^parent ($sha1)$/o) {
1682 $x->{p}->{$1} = 1;
1683 } elsif (s/^ //) {
1684 $x->{m} ||= '';
1685 $x->{m} .= $_;
1688 return ($x != $rl->{t}) ? $x : undef;
1691 # read the entire log into a temporary file (which is removed ASAP)
1692 # and store the file handle + parser state
1693 sub svn_log_raw {
1694 my (@log_args) = @_;
1695 my $log_fh = IO::File->new_tmpfile or croak $!;
1696 my $pid = fork;
1697 defined $pid or croak $!;
1698 if (!$pid) {
1699 open STDOUT, '>&', $log_fh or croak $!;
1700 exec (qw(svn log), @log_args) or croak $!
1702 waitpid $pid, 0;
1703 croak $? if $?;
1704 seek $log_fh, 0, 0 or croak $!;
1705 return { state => 'sep', fh => $log_fh };
1708 sub next_log_entry {
1709 my $log = shift; # retval of svn_log_raw()
1710 my $ret = undef;
1711 my $fh = $log->{fh};
1713 while (<$fh>) {
1714 chomp;
1715 if (/^\-{72}$/) {
1716 if ($log->{state} eq 'msg') {
1717 if ($ret->{lines}) {
1718 $ret->{msg} .= $_."\n";
1719 unless(--$ret->{lines}) {
1720 $log->{state} = 'sep';
1722 } else {
1723 croak "Log parse error at: $_\n",
1724 $ret->{revision},
1725 "\n";
1727 next;
1729 if ($log->{state} ne 'sep') {
1730 croak "Log parse error at: $_\n",
1731 "state: $log->{state}\n",
1732 $ret->{revision},
1733 "\n";
1735 $log->{state} = 'rev';
1737 # if we have an empty log message, put something there:
1738 if ($ret) {
1739 $ret->{msg} ||= "\n";
1740 delete $ret->{lines};
1741 return $ret;
1743 next;
1745 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1746 my $rev = $1;
1747 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1748 ($lines) = ($lines =~ /(\d+)/);
1749 $date = '1970-01-01 00:00:00 +0000'
1750 if ($_ignore_nodate && $date eq '(no date)');
1751 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1752 /(\d{4})\-(\d\d)\-(\d\d)\s
1753 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1754 or croak "Failed to parse date: $date\n";
1755 $ret = { revision => $rev,
1756 date => "$tz $Y-$m-$d $H:$M:$S",
1757 author => $author,
1758 lines => $lines,
1759 msg => '' };
1760 if (defined $_authors && ! defined $users{$author}) {
1761 die "Author: $author not defined in ",
1762 "$_authors file\n";
1764 $log->{state} = 'msg_start';
1765 next;
1767 # skip the first blank line of the message:
1768 if ($log->{state} eq 'msg_start' && /^$/) {
1769 $log->{state} = 'msg';
1770 } elsif ($log->{state} eq 'msg') {
1771 if ($ret->{lines}) {
1772 $ret->{msg} .= $_."\n";
1773 unless (--$ret->{lines}) {
1774 $log->{state} = 'sep';
1776 } else {
1777 croak "Log parse error at: $_\n",
1778 $ret->{revision},"\n";
1782 return $ret;
1785 sub svn_info {
1786 my $url = shift || $SVN_URL;
1788 my $pid = open my $info_fh, '-|';
1789 defined $pid or croak $!;
1791 if ($pid == 0) {
1792 exec(qw(svn info),$url) or croak $!;
1795 my $ret = {};
1796 # only single-lines seem to exist in svn info output
1797 while (<$info_fh>) {
1798 chomp $_;
1799 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
1800 $ret->{$1} = $2;
1801 push @{$ret->{-order}}, $1;
1804 close $info_fh or croak $?;
1805 return $ret;
1808 sub sys { system(@_) == 0 or croak $? }
1810 sub do_update_index {
1811 my ($z_cmd, $cmd, $no_text_base) = @_;
1813 my $z = open my $p, '-|';
1814 defined $z or croak $!;
1815 unless ($z) { exec @$z_cmd or croak $! }
1817 my $pid = open my $ui, '|-';
1818 defined $pid or croak $!;
1819 unless ($pid) {
1820 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1822 local $/ = "\0";
1823 while (my $x = <$p>) {
1824 chomp $x;
1825 if (!$no_text_base && lstat $x && ! -l _ &&
1826 svn_propget_base('svn:keywords', $x)) {
1827 my $mode = -x _ ? 0755 : 0644;
1828 my ($v,$d,$f) = File::Spec->splitpath($x);
1829 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1830 'text-base',"$f.svn-base");
1831 $tb =~ s#^/##;
1832 unless (-f $tb) {
1833 $tb = File::Spec->catfile($d, '.svn',
1834 'text-base',"$f.svn-base");
1835 $tb =~ s#^/##;
1837 my @s = stat($x);
1838 unlink $x or croak $!;
1839 copy($tb, $x);
1840 chmod(($mode &~ umask), $x) or croak $!;
1841 utime $s[8], $s[9], $x;
1843 print $ui $x,"\0";
1845 close $ui or croak $?;
1848 sub index_changes {
1849 return if $_use_lib;
1851 if (!-f "$GIT_SVN_DIR/info/exclude") {
1852 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
1853 print $fd '.svn',"\n";
1854 close $fd or croak $!;
1856 my $no_text_base = shift;
1857 do_update_index([qw/git-diff-files --name-only -z/],
1858 'remove',
1859 $no_text_base);
1860 do_update_index([qw/git-ls-files -z --others/,
1861 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1862 'add',
1863 $no_text_base);
1866 sub s_to_file {
1867 my ($str, $file, $mode) = @_;
1868 open my $fd,'>',$file or croak $!;
1869 print $fd $str,"\n" or croak $!;
1870 close $fd or croak $!;
1871 chmod ($mode &~ umask, $file) if (defined $mode);
1874 sub file_to_s {
1875 my $file = shift;
1876 open my $fd,'<',$file or croak "$!: file: $file\n";
1877 local $/;
1878 my $ret = <$fd>;
1879 close $fd or croak $!;
1880 $ret =~ s/\s*$//s;
1881 return $ret;
1884 sub assert_revision_unknown {
1885 my $r = shift;
1886 if (my $c = revdb_get($REVDB, $r)) {
1887 croak "$r = $c already exists! Why are we refetching it?";
1891 sub trees_eq {
1892 my ($x, $y) = @_;
1893 my @x = safe_qx('git-cat-file','commit',$x);
1894 my @y = safe_qx('git-cat-file','commit',$y);
1895 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1896 || $y[0] !~ /^tree $sha1\n$/) {
1897 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1898 return 0
1900 return 1;
1903 sub git_commit {
1904 my ($log_msg, @parents) = @_;
1905 assert_revision_unknown($log_msg->{revision});
1906 map_tree_joins() if (@_branch_from && !%tree_map);
1908 my (@tmp_parents, @exec_parents, %seen_parent);
1909 if (my $lparents = $log_msg->{parents}) {
1910 @tmp_parents = @$lparents
1912 # commit parents can be conditionally bound to a particular
1913 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1914 foreach my $p (@parents) {
1915 next unless defined $p;
1916 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1917 if ($1 == $log_msg->{revision}) {
1918 push @tmp_parents, $2;
1920 } else {
1921 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1924 my $tree = $log_msg->{tree};
1925 if (!defined $tree) {
1926 my $index = set_index($GIT_SVN_INDEX);
1927 index_changes();
1928 chomp($tree = `git-write-tree`);
1929 croak $? if $?;
1930 restore_index($index);
1933 # just in case we clobber the existing ref, we still want that ref
1934 # as our parent:
1935 if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1936 push @tmp_parents, $cur;
1939 if (exists $tree_map{$tree}) {
1940 foreach my $p (@{$tree_map{$tree}}) {
1941 my $skip;
1942 foreach (@tmp_parents) {
1943 # see if a common parent is found
1944 my $mb = eval {
1945 safe_qx('git-merge-base', $_, $p)
1947 next if ($@ || $?);
1948 $skip = 1;
1949 last;
1951 next if $skip;
1952 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
1953 next if (($SVN_UUID eq $uuid_p) &&
1954 ($log_msg->{revision} > $r_p));
1955 next if (defined $url_p && defined $SVN_URL &&
1956 ($SVN_UUID eq $uuid_p) &&
1957 ($url_p eq $SVN_URL));
1958 push @tmp_parents, $p;
1961 foreach (@tmp_parents) {
1962 next if $seen_parent{$_};
1963 $seen_parent{$_} = 1;
1964 push @exec_parents, $_;
1965 # MAXPARENT is defined to 16 in commit-tree.c:
1966 last if @exec_parents > 16;
1969 set_commit_env($log_msg);
1970 my @exec = ('git-commit-tree', $tree);
1971 push @exec, '-p', $_ foreach @exec_parents;
1972 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1973 or croak $!;
1974 print $msg_fh $log_msg->{msg} or croak $!;
1975 unless ($_no_metadata) {
1976 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1977 " $SVN_UUID\n" or croak $!;
1979 $msg_fh->flush == 0 or croak $!;
1980 close $msg_fh or croak $!;
1981 chomp(my $commit = do { local $/; <$out_fh> });
1982 close $out_fh or croak $!;
1983 waitpid $pid, 0;
1984 croak $? if $?;
1985 if ($commit !~ /^$sha1$/o) {
1986 die "Failed to commit, invalid sha1: $commit\n";
1988 sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1989 revdb_set($REVDB, $log_msg->{revision}, $commit);
1991 # this output is read via pipe, do not change:
1992 print "r$log_msg->{revision} = $commit\n";
1993 check_repack();
1994 return $commit;
1997 sub check_repack {
1998 if ($_repack && (--$_repack_nr == 0)) {
1999 $_repack_nr = $_repack;
2000 sys("git repack $_repack_flags");
2004 sub set_commit_env {
2005 my ($log_msg) = @_;
2006 my $author = $log_msg->{author};
2007 if (!defined $author || length $author == 0) {
2008 $author = '(no author)';
2010 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
2011 : ($author,"$author\@$SVN_UUID");
2012 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2013 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2014 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2017 sub apply_mod_line_blob {
2018 my $m = shift;
2019 if ($m->{mode_b} =~ /^120/) {
2020 blob_to_symlink($m->{sha1_b}, $m->{file_b});
2021 } else {
2022 blob_to_file($m->{sha1_b}, $m->{file_b});
2026 sub blob_to_symlink {
2027 my ($blob, $link) = @_;
2028 defined $link or croak "\$link not defined!\n";
2029 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2030 if (-l $link || -f _) {
2031 unlink $link or croak $!;
2034 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2035 symlink $dest, $link or croak $!;
2038 sub blob_to_file {
2039 my ($blob, $file) = @_;
2040 defined $file or croak "\$file not defined!\n";
2041 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2042 if (-l $file || -f _) {
2043 unlink $file or croak $!;
2046 open my $blob_fh, '>', $file or croak "$!: $file\n";
2047 my $pid = fork;
2048 defined $pid or croak $!;
2050 if ($pid == 0) {
2051 open STDOUT, '>&', $blob_fh or croak $!;
2052 exec('git-cat-file','blob',$blob) or croak $!;
2054 waitpid $pid, 0;
2055 croak $? if $?;
2057 close $blob_fh or croak $!;
2060 sub safe_qx {
2061 my $pid = open my $child, '-|';
2062 defined $pid or croak $!;
2063 if ($pid == 0) {
2064 exec(@_) or croak $!;
2066 my @ret = (<$child>);
2067 close $child or croak $?;
2068 die $? if $?; # just in case close didn't error out
2069 return wantarray ? @ret : join('',@ret);
2072 sub svn_compat_check {
2073 if ($_follow_parent) {
2074 print STDERR 'E: --follow-parent functionality is only ',
2075 "available when SVN libraries are used\n";
2076 exit 1;
2078 my @co_help = safe_qx(qw(svn co -h));
2079 unless (grep /ignore-externals/,@co_help) {
2080 print STDERR "W: Installed svn version does not support ",
2081 "--ignore-externals\n";
2082 $_no_ignore_ext = 1;
2084 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2085 $_svn_co_url_revs = 1;
2087 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2088 $_svn_pg_peg_revs = 1;
2091 # I really, really hope nobody hits this...
2092 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
2093 print STDERR <<'';
2094 W: The installed svn version does not support the --stop-on-copy flag in
2095 the log command.
2096 Lets hope the directory you're tracking is not a branch or tag
2097 and was never moved within the repository...
2099 $_no_stop_copy = 1;
2103 # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
2104 # (and they won't honor URL@<rev> without -r<rev>, too!)
2105 sub svn_cmd_checkout {
2106 my ($url, $rev, $dir) = @_;
2107 my @cmd = ('svn','co', "-r$rev");
2108 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2109 $url .= "\@$rev" if $_svn_co_url_revs;
2110 sys(@cmd, $url, $dir);
2113 sub check_upgrade_needed {
2114 if (!-r $REVDB) {
2115 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2116 open my $fh, '>>',$REVDB or croak $!;
2117 close $fh;
2119 my $old = eval {
2120 my $pid = open my $child, '-|';
2121 defined $pid or croak $!;
2122 if ($pid == 0) {
2123 close STDERR;
2124 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2126 my @ret = (<$child>);
2127 close $child or croak $?;
2128 die $? if $?; # just in case close didn't error out
2129 return wantarray ? @ret : join('',@ret);
2131 return unless $old;
2132 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
2133 if ($@ || !$head) {
2134 print STDERR "Please run: $0 rebuild --upgrade\n";
2135 exit 1;
2139 # fills %tree_map with a reverse mapping of trees to commits. Useful
2140 # for finding parents to commit on.
2141 sub map_tree_joins {
2142 my %seen;
2143 foreach my $br (@_branch_from) {
2144 my $pid = open my $pipe, '-|';
2145 defined $pid or croak $!;
2146 if ($pid == 0) {
2147 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2148 or croak $!;
2150 while (<$pipe>) {
2151 if (/^commit ($sha1)$/o) {
2152 my $commit = $1;
2154 # if we've seen a commit,
2155 # we've seen its parents
2156 last if $seen{$commit};
2157 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2158 unless (defined $tree) {
2159 die "Failed to parse commit $commit\n";
2161 push @{$tree_map{$tree}}, $commit;
2162 $seen{$commit} = 1;
2165 close $pipe; # we could be breaking the pipe early
2169 sub load_all_refs {
2170 if (@_branch_from) {
2171 print STDERR '--branch|-b parameters are ignored when ',
2172 "--branch-all-refs|-B is passed\n";
2175 # don't worry about rev-list on non-commit objects/tags,
2176 # it shouldn't blow up if a ref is a blob or tree...
2177 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2180 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2181 sub load_authors {
2182 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2183 while (<$authors>) {
2184 chomp;
2185 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2186 my ($user, $name, $email) = ($1, $2, $3);
2187 $users{$user} = [$name, $email];
2189 close $authors or croak $!;
2192 sub rload_authors {
2193 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2194 while (<$authors>) {
2195 chomp;
2196 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2197 my ($user, $name, $email) = ($1, $2, $3);
2198 $rusers{"$name <$email>"} = $user;
2200 close $authors or croak $!;
2203 sub svn_propget_base {
2204 my ($p, $f) = @_;
2205 $f .= '@BASE' if $_svn_pg_peg_revs;
2206 return safe_qx(qw/svn propget/, $p, $f);
2209 sub git_svn_each {
2210 my $sub = shift;
2211 foreach (`git-rev-parse --symbolic --all`) {
2212 next unless s#^refs/remotes/##;
2213 chomp $_;
2214 next unless -f "$GIT_DIR/svn/$_/info/url";
2215 &$sub($_);
2219 sub migrate_revdb {
2220 git_svn_each(sub {
2221 my $id = shift;
2222 defined(my $pid = fork) or croak $!;
2223 if (!$pid) {
2224 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2225 init_vars();
2226 exit 0 if -r $REVDB;
2227 print "Upgrading svn => git mapping...\n";
2228 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2229 open my $fh, '>>',$REVDB or croak $!;
2230 close $fh;
2231 rebuild();
2232 print "Done upgrading. You may now delete the ",
2233 "deprecated $GIT_SVN_DIR/revs directory\n";
2234 exit 0;
2236 waitpid $pid, 0;
2237 croak $? if $?;
2241 sub migration_check {
2242 migrate_revdb() unless (-e $REVDB);
2243 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
2244 print "Upgrading repository...\n";
2245 unless (-d "$GIT_DIR/svn") {
2246 mkdir "$GIT_DIR/svn" or croak $!;
2248 print "Data from a previous version of git-svn exists, but\n\t",
2249 "$GIT_SVN_DIR\n\t(required for this version ",
2250 "($VERSION) of git-svn) does not.\n";
2252 foreach my $x (`git-rev-parse --symbolic --all`) {
2253 next unless $x =~ s#^refs/remotes/##;
2254 chomp $x;
2255 next unless -f "$GIT_DIR/$x/info/url";
2256 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
2257 next unless $u;
2258 my $dn = dirname("$GIT_DIR/svn/$x");
2259 mkpath([$dn]) unless -d $dn;
2260 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
2262 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
2263 print "Done upgrading.\n";
2266 sub find_rev_before {
2267 my ($r, $id, $eq_ok) = @_;
2268 my $f = "$GIT_DIR/svn/$id/.rev_db";
2269 return (undef,undef) unless -r $f;
2270 --$r unless $eq_ok;
2271 while ($r > 0) {
2272 if (my $c = revdb_get($f, $r)) {
2273 return ($r, $c);
2275 --$r;
2277 return (undef, undef);
2280 sub init_vars {
2281 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
2282 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2283 $REVDB = "$GIT_SVN_DIR/.rev_db";
2284 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2285 $SVN_URL = undef;
2286 $SVN_WC = "$GIT_SVN_DIR/tree";
2287 %tree_map = ();
2290 # convert GetOpt::Long specs for use by git-repo-config
2291 sub read_repo_config {
2292 return unless -d $GIT_DIR;
2293 my $opts = shift;
2294 foreach my $o (keys %$opts) {
2295 my $v = $opts->{$o};
2296 my ($key) = ($o =~ /^([a-z\-]+)/);
2297 $key =~ s/-//g;
2298 my $arg = 'git-repo-config';
2299 $arg .= ' --int' if ($o =~ /[:=]i$/);
2300 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2301 if (ref $v eq 'ARRAY') {
2302 chomp(my @tmp = `$arg --get-all svn.$key`);
2303 @$v = @tmp if @tmp;
2304 } else {
2305 chomp(my $tmp = `$arg --get svn.$key`);
2306 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2307 $$v = $tmp;
2313 sub set_default_vals {
2314 if (defined $_repack) {
2315 $_repack = 1000 if ($_repack <= 0);
2316 $_repack_nr = $_repack;
2317 $_repack_flags ||= '-d';
2321 sub read_grafts {
2322 my $gr_file = shift;
2323 my ($grafts, $comments) = ({}, {});
2324 if (open my $fh, '<', $gr_file) {
2325 my @tmp;
2326 while (<$fh>) {
2327 if (/^($sha1)\s+/) {
2328 my $c = $1;
2329 if (@tmp) {
2330 @{$comments->{$c}} = @tmp;
2331 @tmp = ();
2333 foreach my $p (split /\s+/, $_) {
2334 $grafts->{$c}->{$p} = 1;
2336 } else {
2337 push @tmp, $_;
2340 close $fh or croak $!;
2341 @{$comments->{'END'}} = @tmp if @tmp;
2343 return ($grafts, $comments);
2346 sub write_grafts {
2347 my ($grafts, $comments, $gr_file) = @_;
2349 open my $fh, '>', $gr_file or croak $!;
2350 foreach my $c (sort keys %$grafts) {
2351 if ($comments->{$c}) {
2352 print $fh $_ foreach @{$comments->{$c}};
2354 my $p = $grafts->{$c};
2355 my %x; # real parents
2356 delete $p->{$c}; # commits are not self-reproducing...
2357 my $pid = open my $ch, '-|';
2358 defined $pid or croak $!;
2359 if (!$pid) {
2360 exec(qw/git-cat-file commit/, $c) or croak $!;
2362 while (<$ch>) {
2363 if (/^parent ($sha1)/) {
2364 $x{$1} = $p->{$1} = 1;
2365 } else {
2366 last unless /^\S/;
2369 close $ch; # breaking the pipe
2371 # if real parents are the only ones in the grafts, drop it
2372 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2374 my (@ip, @jp, $mb);
2375 my %del = %x;
2376 @ip = @jp = keys %$p;
2377 foreach my $i (@ip) {
2378 next if $del{$i} || $p->{$i} == 2;
2379 foreach my $j (@jp) {
2380 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2381 $mb = eval { safe_qx('git-merge-base',$i,$j) };
2382 next unless $mb;
2383 chomp $mb;
2384 next if $x{$mb};
2385 if ($mb eq $j) {
2386 delete $p->{$i};
2387 $del{$i} = 1;
2388 } elsif ($mb eq $i) {
2389 delete $p->{$j};
2390 $del{$j} = 1;
2395 # if real parents are the only ones in the grafts, drop it
2396 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2398 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2400 if ($comments->{'END'}) {
2401 print $fh $_ foreach @{$comments->{'END'}};
2403 close $fh or croak $!;
2406 sub read_url_paths_all {
2407 my ($l_map, $pfx, $p) = @_;
2408 my @dir;
2409 foreach (<$p/*>) {
2410 if (-r "$_/info/url") {
2411 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2412 my $id = $pfx . basename $_;
2413 my $url = file_to_s("$_/info/url");
2414 my ($u, $p) = repo_path_split($url);
2415 $l_map->{$u}->{$p} = $id;
2416 } elsif (-d $_) {
2417 push @dir, $_;
2420 foreach (@dir) {
2421 my $x = $_;
2422 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2423 read_url_paths_all($l_map, $x, $_);
2427 # this one only gets ids that have been imported, not new ones
2428 sub read_url_paths {
2429 my $l_map = {};
2430 git_svn_each(sub { my $x = shift;
2431 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
2432 my ($u, $p) = repo_path_split($url);
2433 $l_map->{$u}->{$p} = $x;
2435 return $l_map;
2438 sub extract_metadata {
2439 my $id = shift or return (undef, undef, undef);
2440 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
2441 \s([a-f\d\-]+)$/x);
2442 if (!$rev || !$uuid || !$url) {
2443 # some of the original repositories I made had
2444 # identifiers like this:
2445 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2447 return ($url, $rev, $uuid);
2450 sub cmt_metadata {
2451 return extract_metadata((grep(/^git-svn-id: /,
2452 safe_qx(qw/git-cat-file commit/, shift)))[-1]);
2455 sub get_commit_time {
2456 my $cmt = shift;
2457 defined(my $pid = open my $fh, '-|') or croak $!;
2458 if (!$pid) {
2459 exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!;
2461 while (<$fh>) {
2462 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2463 my ($s, $tz) = ($1, $2);
2464 if ($tz =~ s/^\+//) {
2465 $s += tz_to_s_offset($tz);
2466 } elsif ($tz =~ s/^\-//) {
2467 $s -= tz_to_s_offset($tz);
2469 close $fh;
2470 return $s;
2472 die "Can't get commit time for commit: $cmt\n";
2475 sub tz_to_s_offset {
2476 my ($tz) = @_;
2477 $tz =~ s/(\d\d)$//;
2478 return ($1 * 60) + ($tz * 3600);
2481 sub setup_pager { # translated to Perl from pager.c
2482 return unless (-t *STDOUT);
2483 my $pager = $ENV{PAGER};
2484 if (!defined $pager) {
2485 $pager = 'less';
2486 } elsif (length $pager == 0 || $pager eq 'cat') {
2487 return;
2489 pipe my $rfd, my $wfd or return;
2490 defined(my $pid = fork) or croak $!;
2491 if (!$pid) {
2492 open STDOUT, '>&', $wfd or croak $!;
2493 return;
2495 open STDIN, '<&', $rfd or croak $!;
2496 $ENV{LESS} ||= '-S';
2497 exec $pager or croak "Can't run pager: $!\n";;
2500 sub get_author_info {
2501 my ($dest, $author, $t, $tz) = @_;
2502 $author =~ s/(?:^\s*|\s*$)//g;
2503 $dest->{a_raw} = $author;
2504 my $_a;
2505 if ($_authors) {
2506 $_a = $rusers{$author} || undef;
2508 if (!$_a) {
2509 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2511 $dest->{t} = $t;
2512 $dest->{tz} = $tz;
2513 $dest->{a} = $_a;
2514 # Date::Parse isn't in the standard Perl distro :(
2515 if ($tz =~ s/^\+//) {
2516 $t += tz_to_s_offset($tz);
2517 } elsif ($tz =~ s/^\-//) {
2518 $t -= tz_to_s_offset($tz);
2520 $dest->{t_utc} = $t;
2523 sub process_commit {
2524 my ($c, $r_min, $r_max, $defer) = @_;
2525 if (defined $r_min && defined $r_max) {
2526 if ($r_min == $c->{r} && $r_min == $r_max) {
2527 show_commit($c);
2528 return 0;
2530 return 1 if $r_min == $r_max;
2531 if ($r_min < $r_max) {
2532 # we need to reverse the print order
2533 return 0 if (defined $_limit && --$_limit < 0);
2534 push @$defer, $c;
2535 return 1;
2537 if ($r_min != $r_max) {
2538 return 1 if ($r_min < $c->{r});
2539 return 1 if ($r_max > $c->{r});
2542 return 0 if (defined $_limit && --$_limit < 0);
2543 show_commit($c);
2544 return 1;
2547 sub show_commit {
2548 my $c = shift;
2549 if ($_oneline) {
2550 my $x = "\n";
2551 if (my $l = $c->{l}) {
2552 while ($l->[0] =~ /^\s*$/) { shift @$l }
2553 $x = $l->[0];
2555 $_l_fmt ||= 'A' . length($c->{r});
2556 print 'r',pack($_l_fmt, $c->{r}),' | ';
2557 print "$c->{c} | " if $_show_commit;
2558 print $x;
2559 } else {
2560 show_commit_normal($c);
2564 sub show_commit_normal {
2565 my ($c) = @_;
2566 print '-' x72, "\nr$c->{r} | ";
2567 print "$c->{c} | " if $_show_commit;
2568 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2569 localtime($c->{t_utc})), ' | ';
2570 my $nr_line = 0;
2572 if (my $l = $c->{l}) {
2573 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2574 pop @$l;
2576 $nr_line = scalar @$l;
2577 if (!$nr_line) {
2578 print "1 line\n\n\n";
2579 } else {
2580 if ($nr_line == 1) {
2581 $nr_line = '1 line';
2582 } else {
2583 $nr_line .= ' lines';
2585 print $nr_line, "\n\n";
2586 print $_ foreach @$l;
2588 } else {
2589 print "1 line\n\n";
2592 foreach my $x (qw/raw diff/) {
2593 if ($c->{$x}) {
2594 print "\n";
2595 print $_ foreach @{$c->{$x}}
2600 sub libsvn_load {
2601 return unless $_use_lib;
2602 $_use_lib = eval {
2603 require SVN::Core;
2604 if ($SVN::Core::VERSION lt '1.1.0') {
2605 die "Need SVN::Core 1.1.0 or better ",
2606 "(got $SVN::Core::VERSION) ",
2607 "Falling back to command-line svn\n";
2609 require SVN::Ra;
2610 require SVN::Delta;
2611 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2612 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2613 $SVN::Node::dir.$SVN::Node::unknown.
2614 $SVN::Node::none.$SVN::Node::file.
2615 $SVN::Node::dir.$SVN::Node::unknown;
2620 sub libsvn_connect {
2621 my ($url) = @_;
2622 my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
2623 SVN::Client::get_ssl_server_trust_file_provider(),
2624 SVN::Client::get_username_provider()]);
2625 my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
2626 return $s;
2629 sub libsvn_get_file {
2630 my ($gui, $f, $rev) = @_;
2631 my $p = $f;
2632 if (length $SVN_PATH > 0) {
2633 return unless ($p =~ s#^\Q$SVN_PATH\E/##);
2636 my ($hash, $pid, $in, $out);
2637 my $pool = SVN::Pool->new;
2638 defined($pid = open3($in, $out, '>&STDERR',
2639 qw/git-hash-object -w --stdin/)) or croak $!;
2640 # redirect STDOUT for SVN 1.1.x compatibility
2641 open my $stdout, '>&', \*STDOUT or croak $!;
2642 open STDOUT, '>&', $in or croak $!;
2643 my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool);
2644 $in->flush == 0 or croak $!;
2645 open STDOUT, '>&', $stdout or croak $!;
2646 close $in or croak $!;
2647 close $stdout or croak $!;
2648 $pool->clear;
2649 chomp($hash = do { local $/; <$out> });
2650 close $out or croak $!;
2651 waitpid $pid, 0;
2652 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2654 my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
2655 if (exists $props->{'svn:special'}) {
2656 $mode = '120000';
2657 my $link = `git-cat-file blob $hash`;
2658 $link =~ s/^link // or die "svn:special file with contents: <",
2659 $link, "> is not understood\n";
2660 defined($pid = open3($in, $out, '>&STDERR',
2661 qw/git-hash-object -w --stdin/)) or croak $!;
2662 print $in $link;
2663 $in->flush == 0 or croak $!;
2664 close $in or croak $!;
2665 chomp($hash = do { local $/; <$out> });
2666 close $out or croak $!;
2667 waitpid $pid, 0;
2668 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2670 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2673 sub libsvn_log_entry {
2674 my ($rev, $author, $date, $msg, $parents) = @_;
2675 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2676 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2677 or die "Unable to parse date: $date\n";
2678 if (defined $_authors && ! defined $users{$author}) {
2679 die "Author: $author not defined in $_authors file\n";
2681 $msg = '' if ($rev == 0 && !defined $msg);
2682 return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2683 author => $author, msg => $msg."\n", parents => $parents || [] }
2686 sub process_rm {
2687 my ($gui, $last_commit, $f) = @_;
2688 $f =~ s#^\Q$SVN_PATH\E/?## or return;
2689 # remove entire directories.
2690 if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2691 defined(my $pid = open my $ls, '-|') or croak $!;
2692 if (!$pid) {
2693 exec(qw/git-ls-tree -r --name-only -z/,
2694 $last_commit,'--',$f) or croak $!;
2696 local $/ = "\0";
2697 while (<$ls>) {
2698 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2700 close $ls or croak $?;
2701 } else {
2702 print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2706 sub libsvn_fetch {
2707 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2708 open my $gui, '| git-update-index -z --index-info' or croak $!;
2709 my @amr;
2710 foreach my $f (keys %$paths) {
2711 my $m = $paths->{$f}->action();
2712 $f =~ s#^/+##;
2713 if ($m =~ /^[DR]$/) {
2714 print "\t$m\t$f\n" unless $_q;
2715 process_rm($gui, $last_commit, $f);
2716 next if $m eq 'D';
2717 # 'R' can be file replacements, too, right?
2719 my $pool = SVN::Pool->new;
2720 my $t = $SVN->check_path($f, $rev, $pool);
2721 if ($t == $SVN::Node::file) {
2722 if ($m =~ /^[AMR]$/) {
2723 push @amr, [ $m, $f ];
2724 } else {
2725 die "Unrecognized action: $m, ($f r$rev)\n";
2727 } elsif ($t == $SVN::Node::dir && $m =~ /^[AR]$/) {
2728 my @traversed = ();
2729 libsvn_traverse($gui, '', $f, $rev, \@traversed);
2730 foreach (@traversed) {
2731 push @amr, [ $m, $_ ]
2734 $pool->clear;
2736 foreach (@amr) {
2737 print "\t$_->[0]\t$_->[1]\n" unless $_q;
2738 libsvn_get_file($gui, $_->[1], $rev)
2740 close $gui or croak $?;
2741 return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2744 sub svn_grab_base_rev {
2745 defined(my $pid = open my $fh, '-|') or croak $!;
2746 if (!$pid) {
2747 open my $null, '>', '/dev/null' or croak $!;
2748 open STDERR, '>&', $null or croak $!;
2749 exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2750 or croak $!;
2752 chomp(my $c = do { local $/; <$fh> });
2753 close $fh;
2754 if (defined $c && length $c) {
2755 my ($url, $rev, $uuid) = cmt_metadata($c);
2756 return ($rev, $c) if defined $rev;
2758 if ($_no_metadata) {
2759 my $offset = -41; # from tail
2760 my $rl;
2761 open my $fh, '<', $REVDB or
2762 die "--no-metadata specified and $REVDB not readable\n";
2763 seek $fh, $offset, 2;
2764 $rl = readline $fh;
2765 defined $rl or return (undef, undef);
2766 chomp $rl;
2767 while ($c ne $rl && tell $fh != 0) {
2768 $offset -= 41;
2769 seek $fh, $offset, 2;
2770 $rl = readline $fh;
2771 defined $rl or return (undef, undef);
2772 chomp $rl;
2774 my $rev = tell $fh;
2775 croak $! if ($rev < -1);
2776 $rev = ($rev - 41) / 41;
2777 close $fh or croak $!;
2778 return ($rev, $c);
2780 return (undef, undef);
2783 sub libsvn_parse_revision {
2784 my $base = shift;
2785 my $head = $SVN->get_latest_revnum();
2786 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2787 return ($base + 1, $head) if (defined $base);
2788 return (0, $head);
2790 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2791 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2792 if ($_revision =~ /^BASE:(\d+)$/) {
2793 return ($base + 1, $1) if (defined $base);
2794 return (0, $head);
2796 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2797 die "revision argument: $_revision not understood by git-svn\n",
2798 "Try using the command-line svn client instead\n";
2801 sub libsvn_traverse {
2802 my ($gui, $pfx, $path, $rev, $files) = @_;
2803 my $cwd = "$pfx/$path";
2804 my $pool = SVN::Pool->new;
2805 $cwd =~ s#^/+##g;
2806 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2807 foreach my $d (keys %$dirent) {
2808 my $t = $dirent->{$d}->kind;
2809 if ($t == $SVN::Node::dir) {
2810 libsvn_traverse($gui, $cwd, $d, $rev, $files);
2811 } elsif ($t == $SVN::Node::file) {
2812 my $file = "$cwd/$d";
2813 if (defined $files) {
2814 push @$files, $file;
2815 } else {
2816 print "\tA\t$file\n" unless $_q;
2817 libsvn_get_file($gui, $file, $rev);
2821 $pool->clear;
2824 sub libsvn_traverse_ignore {
2825 my ($fh, $path, $r) = @_;
2826 $path =~ s#^/+##g;
2827 my $pool = SVN::Pool->new;
2828 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2829 my $p = $path;
2830 $p =~ s#^\Q$SVN_PATH\E/?##;
2831 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2832 if (my $s = $props->{'svn:ignore'}) {
2833 $s =~ s/[\r\n]+/\n/g;
2834 chomp $s;
2835 if (length $p == 0) {
2836 $s =~ s#\n#\n/$p#g;
2837 print $fh "/$s\n";
2838 } else {
2839 $s =~ s#\n#\n/$p/#g;
2840 print $fh "/$p/$s\n";
2843 foreach (sort keys %$dirent) {
2844 next if $dirent->{$_}->kind != $SVN::Node::dir;
2845 libsvn_traverse_ignore($fh, "$path/$_", $r);
2847 $pool->clear;
2850 sub revisions_eq {
2851 my ($path, $r0, $r1) = @_;
2852 return 1 if $r0 == $r1;
2853 my $nr = 0;
2854 if ($_use_lib) {
2855 # should be OK to use Pool here (r1 - r0) should be small
2856 my $pool = SVN::Pool->new;
2857 libsvn_get_log($SVN, "/$path", $r0, $r1,
2858 0, 1, 1, sub {$nr++}, $pool);
2859 $pool->clear;
2860 } else {
2861 my ($url, undef) = repo_path_split($SVN_URL);
2862 my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1");
2863 while (next_log_entry($svn_log)) { $nr++ }
2864 close $svn_log->{fh};
2866 return 0 if ($nr > 1);
2867 return 1;
2870 sub libsvn_find_parent_branch {
2871 my ($paths, $rev, $author, $date, $msg) = @_;
2872 my $svn_path = '/'.$SVN_PATH;
2874 # look for a parent from another branch:
2875 my $i = $paths->{$svn_path} or return;
2876 my $branch_from = $i->copyfrom_path or return;
2877 my $r = $i->copyfrom_rev;
2878 print STDERR "Found possible branch point: ",
2879 "$branch_from => $svn_path, $r\n";
2880 $branch_from =~ s#^/##;
2881 my $l_map = {};
2882 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2883 my $url = $SVN->{url};
2884 defined $l_map->{$url} or return;
2885 my $id = $l_map->{$url}->{$branch_from};
2886 if (!defined $id && $_follow_parent) {
2887 print STDERR "Following parent: $branch_from\@$r\n";
2888 # auto create a new branch and follow it
2889 $id = basename($branch_from);
2890 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2891 while (-r "$GIT_DIR/svn/$id") {
2892 # just grow a tail if we're not unique enough :x
2893 $id .= '-';
2896 return unless defined $id;
2898 my ($r0, $parent) = find_rev_before($r,$id,1);
2899 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2900 defined(my $pid = fork) or croak $!;
2901 if (!$pid) {
2902 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2903 init_vars();
2904 $SVN_URL = "$url/$branch_from";
2905 $SVN_LOG = $SVN = undef;
2906 setup_git_svn();
2907 # we can't assume SVN_URL exists at r+1:
2908 $_revision = "0:$r";
2909 fetch_lib();
2910 exit 0;
2912 waitpid $pid, 0;
2913 croak $? if $?;
2914 ($r0, $parent) = find_rev_before($r,$id,1);
2916 return unless (defined $r0 && defined $parent);
2917 if (revisions_eq($branch_from, $r0, $r)) {
2918 unlink $GIT_SVN_INDEX;
2919 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2920 sys(qw/git-read-tree/, $parent);
2921 return libsvn_fetch($parent, $paths, $rev,
2922 $author, $date, $msg);
2924 print STDERR "Nope, branch point not imported or unknown\n";
2925 return undef;
2928 sub libsvn_get_log {
2929 my ($ra, @args) = @_;
2930 if ($SVN::Core::VERSION le '1.2.0') {
2931 splice(@args, 3, 1);
2933 $ra->get_log(@args);
2936 sub libsvn_new_tree {
2937 if (my $log_entry = libsvn_find_parent_branch(@_)) {
2938 return $log_entry;
2940 my ($paths, $rev, $author, $date, $msg) = @_;
2941 open my $gui, '| git-update-index -z --index-info' or croak $!;
2942 libsvn_traverse($gui, '', $SVN_PATH, $rev);
2943 close $gui or croak $?;
2944 return libsvn_log_entry($rev, $author, $date, $msg);
2947 sub find_graft_path_commit {
2948 my ($tree_paths, $p1, $r1) = @_;
2949 foreach my $x (keys %$tree_paths) {
2950 next unless ($p1 =~ /^\Q$x\E/);
2951 my $i = $tree_paths->{$x};
2952 my ($r0, $parent) = find_rev_before($r1,$i,1);
2953 return $parent if (defined $r0 && $r0 == $r1);
2954 print STDERR "r$r1 of $i not imported\n";
2955 next;
2957 return undef;
2960 sub find_graft_path_parents {
2961 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2962 foreach my $x (keys %$tree_paths) {
2963 next unless ($p0 =~ /^\Q$x\E/);
2964 my $i = $tree_paths->{$x};
2965 my ($r, $parent) = find_rev_before($r0, $i, 1);
2966 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
2967 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
2968 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
2969 next if ($url_a && $url_b && $url_a eq $url_b &&
2970 $uuid_b eq $uuid_a);
2971 $grafts->{$c}->{$parent} = 1;
2976 sub libsvn_graft_file_copies {
2977 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2978 foreach (keys %$paths) {
2979 my $i = $paths->{$_};
2980 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2981 $i->copyfrom_rev);
2982 next unless (defined $p0 && defined $r0);
2984 my $p1 = $_;
2985 $p1 =~ s#^/##;
2986 $p0 =~ s#^/##;
2987 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
2988 next unless $c;
2989 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
2993 sub set_index {
2994 my $old = $ENV{GIT_INDEX_FILE};
2995 $ENV{GIT_INDEX_FILE} = shift;
2996 return $old;
2999 sub restore_index {
3000 my ($old) = @_;
3001 if (defined $old) {
3002 $ENV{GIT_INDEX_FILE} = $old;
3003 } else {
3004 delete $ENV{GIT_INDEX_FILE};
3008 sub libsvn_commit_cb {
3009 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3010 if ($_optimize_commits && $rev == ($r_last + 1)) {
3011 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
3012 $log->{tree} = get_tree_from_treeish($c);
3013 my $cmt = git_commit($log, $cmt_last, $c);
3014 my @diff = safe_qx('git-diff-tree', $cmt, $c);
3015 if (@diff) {
3016 print STDERR "Trees differ: $cmt $c\n",
3017 join('',@diff),"\n";
3018 exit 1;
3020 } else {
3021 fetch("$rev=$c");
3025 sub libsvn_ls_fullurl {
3026 my $fullurl = shift;
3027 my ($repo, $path) = repo_path_split($fullurl);
3028 $SVN ||= libsvn_connect($repo);
3029 my @ret;
3030 my $pool = SVN::Pool->new;
3031 my ($dirent, undef, undef) = $SVN->get_dir($path,
3032 $SVN->get_latest_revnum, $pool);
3033 foreach my $d (keys %$dirent) {
3034 if ($dirent->{$d}->kind == $SVN::Node::dir) {
3035 push @ret, "$d/"; # add '/' for compat with cli svn
3038 $pool->clear;
3039 return @ret;
3043 sub libsvn_skip_unknown_revs {
3044 my $err = shift;
3045 my $errno = $err->apr_err();
3046 # Maybe the branch we're tracking didn't
3047 # exist when the repo started, so it's
3048 # not an error if it doesn't, just continue
3050 # Wonderfully consistent library, eh?
3051 # 160013 - svn:// and file://
3052 # 175002 - http(s)://
3053 # More codes may be discovered later...
3054 if ($errno == 175002 || $errno == 160013) {
3055 return;
3057 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3060 # Tie::File seems to be prone to offset errors if revisions get sparse,
3061 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3062 # one of my favorite modules is out :< Next up would be one of the DBM
3063 # modules, but I'm not sure which is most portable... So I'll just
3064 # go with something that's plain-text, but still capable of
3065 # being randomly accessed. So here's my ultra-simple fixed-width
3066 # database. All records are 40 characters + "\n", so it's easy to seek
3067 # to a revision: (41 * rev) is the byte offset.
3068 # A record of 40 0s denotes an empty revision.
3069 # And yes, it's still pretty fast (faster than Tie::File).
3070 sub revdb_set {
3071 my ($file, $rev, $commit) = @_;
3072 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
3073 open my $fh, '+<', $file or croak $!;
3074 my $offset = $rev * 41;
3075 # assume that append is the common case:
3076 seek $fh, 0, 2 or croak $!;
3077 my $pos = tell $fh;
3078 if ($pos < $offset) {
3079 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
3081 seek $fh, $offset, 0 or croak $!;
3082 print $fh $commit,"\n";
3083 close $fh or croak $!;
3086 sub revdb_get {
3087 my ($file, $rev) = @_;
3088 my $ret;
3089 my $offset = $rev * 41;
3090 open my $fh, '<', $file or croak $!;
3091 seek $fh, $offset, 0;
3092 if (tell $fh == $offset) {
3093 $ret = readline $fh;
3094 if (defined $ret) {
3095 chomp $ret;
3096 $ret = undef if ($ret =~ /^0{40}$/);
3099 close $fh or croak $!;
3100 return $ret;
3103 sub copy_remote_ref {
3104 my $origin = $_cp_remote ? $_cp_remote : 'origin';
3105 my $ref = "refs/remotes/$GIT_SVN";
3106 if (safe_qx('git-ls-remote', $origin, $ref)) {
3107 sys(qw/git fetch/, $origin, "$ref:$ref");
3108 } else {
3109 die "Unable to find remote reference: ",
3110 "refs/remotes/$GIT_SVN on $origin\n";
3114 package SVN::Git::Editor;
3115 use vars qw/@ISA/;
3116 use strict;
3117 use warnings;
3118 use Carp qw/croak/;
3119 use IO::File;
3121 sub new {
3122 my $class = shift;
3123 my $git_svn = shift;
3124 my $self = SVN::Delta::Editor->new(@_);
3125 bless $self, $class;
3126 foreach (qw/svn_path c r ra /) {
3127 die "$_ required!\n" unless (defined $git_svn->{$_});
3128 $self->{$_} = $git_svn->{$_};
3130 $self->{pool} = SVN::Pool->new;
3131 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3132 $self->{rm} = { };
3133 require Digest::MD5;
3134 return $self;
3137 sub split_path {
3138 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3141 sub repo_path {
3142 (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
3143 : $_[0]->{svn_path}
3146 sub url_path {
3147 my ($self, $path) = @_;
3148 $self->{ra}->{url} . '/' . $self->repo_path($path);
3151 sub rmdirs {
3152 my ($self, $q) = @_;
3153 my $rm = $self->{rm};
3154 delete $rm->{''}; # we never delete the url we're tracking
3155 return unless %$rm;
3157 foreach (keys %$rm) {
3158 my @d = split m#/#, $_;
3159 my $c = shift @d;
3160 $rm->{$c} = 1;
3161 while (@d) {
3162 $c .= '/' . shift @d;
3163 $rm->{$c} = 1;
3166 delete $rm->{$self->{svn_path}};
3167 delete $rm->{''}; # we never delete the url we're tracking
3168 return unless %$rm;
3170 defined(my $pid = open my $fh,'-|') or croak $!;
3171 if (!$pid) {
3172 exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
3174 local $/ = "\0";
3175 my @svn_path = split m#/#, $self->{svn_path};
3176 while (<$fh>) {
3177 chomp;
3178 my @dn = (@svn_path, (split m#/#, $_));
3179 while (pop @dn) {
3180 delete $rm->{join '/', @dn};
3182 unless (%$rm) {
3183 close $fh;
3184 return;
3187 close $fh;
3189 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3190 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3191 $self->close_directory($bat->{$d}, $p);
3192 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
3193 print "\tD+\t/$d/\n" unless $q;
3194 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3195 delete $bat->{$d};
3199 sub open_or_add_dir {
3200 my ($self, $full_path, $baton) = @_;
3201 my $p = SVN::Pool->new;
3202 my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
3203 $p->clear;
3204 if ($t == $SVN::Node::none) {
3205 return $self->add_directory($full_path, $baton,
3206 undef, -1, $self->{pool});
3207 } elsif ($t == $SVN::Node::dir) {
3208 return $self->open_directory($full_path, $baton,
3209 $self->{r}, $self->{pool});
3211 print STDERR "$full_path already exists in repository at ",
3212 "r$self->{r} and it is not a directory (",
3213 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3214 exit 1;
3217 sub ensure_path {
3218 my ($self, $path) = @_;
3219 my $bat = $self->{bat};
3220 $path = $self->repo_path($path);
3221 return $bat->{''} unless (length $path);
3222 my @p = split m#/+#, $path;
3223 my $c = shift @p;
3224 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3225 while (@p) {
3226 my $c0 = $c;
3227 $c .= '/' . shift @p;
3228 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3230 return $bat->{$c};
3233 sub A {
3234 my ($self, $m, $q) = @_;
3235 my ($dir, $file) = split_path($m->{file_b});
3236 my $pbat = $self->ensure_path($dir);
3237 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3238 undef, -1);
3239 print "\tA\t$m->{file_b}\n" unless $q;
3240 $self->chg_file($fbat, $m);
3241 $self->close_file($fbat,undef,$self->{pool});
3244 sub C {
3245 my ($self, $m, $q) = @_;
3246 my ($dir, $file) = split_path($m->{file_b});
3247 my $pbat = $self->ensure_path($dir);
3248 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3249 $self->url_path($m->{file_a}), $self->{r});
3250 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3251 $self->chg_file($fbat, $m);
3252 $self->close_file($fbat,undef,$self->{pool});
3255 sub delete_entry {
3256 my ($self, $path, $pbat) = @_;
3257 my $rpath = $self->repo_path($path);
3258 my ($dir, $file) = split_path($rpath);
3259 $self->{rm}->{$dir} = 1;
3260 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3263 sub R {
3264 my ($self, $m, $q) = @_;
3265 my ($dir, $file) = split_path($m->{file_b});
3266 my $pbat = $self->ensure_path($dir);
3267 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3268 $self->url_path($m->{file_a}), $self->{r});
3269 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3270 $self->chg_file($fbat, $m);
3271 $self->close_file($fbat,undef,$self->{pool});
3273 ($dir, $file) = split_path($m->{file_a});
3274 $pbat = $self->ensure_path($dir);
3275 $self->delete_entry($m->{file_a}, $pbat);
3278 sub M {
3279 my ($self, $m, $q) = @_;
3280 my ($dir, $file) = split_path($m->{file_b});
3281 my $pbat = $self->ensure_path($dir);
3282 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3283 $pbat,$self->{r},$self->{pool});
3284 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3285 $self->chg_file($fbat, $m);
3286 $self->close_file($fbat,undef,$self->{pool});
3289 sub T { shift->M(@_) }
3291 sub change_file_prop {
3292 my ($self, $fbat, $pname, $pval) = @_;
3293 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3296 sub chg_file {
3297 my ($self, $fbat, $m) = @_;
3298 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3299 $self->change_file_prop($fbat,'svn:executable','*');
3300 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3301 $self->change_file_prop($fbat,'svn:executable',undef);
3303 my $fh = IO::File->new_tmpfile or croak $!;
3304 if ($m->{mode_b} =~ /^120/) {
3305 print $fh 'link ' or croak $!;
3306 $self->change_file_prop($fbat,'svn:special','*');
3307 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3308 $self->change_file_prop($fbat,'svn:special',undef);
3310 defined(my $pid = fork) or croak $!;
3311 if (!$pid) {
3312 open STDOUT, '>&', $fh or croak $!;
3313 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3315 waitpid $pid, 0;
3316 croak $? if $?;
3317 $fh->flush == 0 or croak $!;
3318 seek $fh, 0, 0 or croak $!;
3320 my $md5 = Digest::MD5->new;
3321 $md5->addfile($fh) or croak $!;
3322 seek $fh, 0, 0 or croak $!;
3324 my $exp = $md5->hexdigest;
3325 my $atd = $self->apply_textdelta($fbat, undef, $self->{pool});
3326 my $got = SVN::TxDelta::send_stream($fh, @$atd, $self->{pool});
3327 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3329 close $fh or croak $!;
3332 sub D {
3333 my ($self, $m, $q) = @_;
3334 my ($dir, $file) = split_path($m->{file_b});
3335 my $pbat = $self->ensure_path($dir);
3336 print "\tD\t$m->{file_b}\n" unless $q;
3337 $self->delete_entry($m->{file_b}, $pbat);
3340 sub close_edit {
3341 my ($self) = @_;
3342 my ($p,$bat) = ($self->{pool}, $self->{bat});
3343 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3344 $self->close_directory($bat->{$_}, $p);
3346 $self->SUPER::close_edit($p);
3347 $p->clear;
3350 sub abort_edit {
3351 my ($self) = @_;
3352 $self->SUPER::abort_edit($self->{pool});
3353 $self->{pool}->clear;
3356 __END__
3358 Data structures:
3360 $svn_log hashref (as returned by svn_log_raw)
3362 fh => file handle of the log file,
3363 state => state of the log file parser (sep/msg/rev/msg_start...)
3366 $log_msg hashref as returned by next_log_entry($svn_log)
3368 msg => 'whitespace-formatted log entry
3369 ', # trailing newline is preserved
3370 revision => '8', # integer
3371 date => '2004-02-24T17:01:44.108345Z', # commit date
3372 author => 'committer name'
3376 @mods = array of diff-index line hashes, each element represents one line
3377 of diff-index output
3379 diff-index line ($m hash)
3381 mode_a => first column of diff-index output, no leading ':',
3382 mode_b => second column of diff-index output,
3383 sha1_b => sha1sum of the final blob,
3384 chg => change type [MCRADT],
3385 file_a => original file name of a file (iff chg is 'C' or 'R')
3386 file_b => new/current file name of a file (any chg)
3390 # retval of read_url_paths{,_all}();
3391 $l_map = {
3392 # repository root url
3393 'https://svn.musicpd.org' => {
3394 # repository path # GIT_SVN_ID
3395 'mpd/trunk' => 'trunk',
3396 'mpd/tags/0.11.5' => 'tags/0.11.5',
3400 Notes:
3401 I don't trust the each() function on unless I created %hash myself
3402 because the internal iterator may not have started at base.