git-svn: error out from dcommit on a parent-less commit
[git/raj.git] / git-svn.perl
blobf0db4af58c69f6f542552651dcde02be02905f3b
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);
44 sub nag_lib {
45 print STDERR <<EOF;
46 ! Please consider installing the SVN Perl libraries (version 1.1.0 or
47 ! newer). You will generally get better performance and fewer bugs,
48 ! especially if you:
49 ! 1) have a case-insensitive filesystem
50 ! 2) replace symlinks with files (and vice-versa) in commits
52 EOF
55 $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB};
56 libsvn_load();
57 nag_lib() unless $_use_lib;
59 my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS};
60 my $sha1 = qr/[a-f\d]{40}/;
61 my $sha1_short = qr/[a-f\d]{4,40}/;
62 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
63 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
64 $_repack, $_repack_nr, $_repack_flags, $_q,
65 $_message, $_file, $_follow_parent, $_no_metadata,
66 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
67 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
68 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m,
69 $_merge, $_strategy, $_dry_run, $_ignore_nodate, $_non_recursive);
70 my (@_branch_from, %tree_map, %users, %rusers, %equiv);
71 my ($_svn_co_url_revs, $_svn_pg_peg_revs);
72 my @repo_path_split_cache;
74 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
75 'branch|b=s' => \@_branch_from,
76 'follow-parent|follow' => \$_follow_parent,
77 'branch-all-refs|B' => \$_branch_all_refs,
78 'authors-file|A=s' => \$_authors,
79 'repack:i' => \$_repack,
80 'no-metadata' => \$_no_metadata,
81 'quiet|q' => \$_q,
82 'ignore-nodate' => \$_ignore_nodate,
83 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
85 my ($_trunk, $_tags, $_branches);
86 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
87 'tags|t=s' => \$_tags,
88 'branches|b=s' => \$_branches );
89 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
90 my %cmt_opts = ( 'edit|e' => \$_edit,
91 'rmdir' => \$_rmdir,
92 'find-copies-harder' => \$_find_copies_harder,
93 'l=i' => \$_l,
94 'copy-similarity|C=i'=> \$_cp_similarity
97 my %cmd = (
98 fetch => [ \&fetch, "Download new revisions from SVN",
99 { 'revision|r=s' => \$_revision, %fc_opts } ],
100 init => [ \&init, "Initialize a repo for tracking" .
101 " (requires URL argument)",
102 \%init_opts ],
103 commit => [ \&commit, "Commit git revisions to SVN",
104 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
105 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
106 { 'revision|r=i' => \$_revision } ],
107 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
108 { 'no-ignore-externals' => \$_no_ignore_ext,
109 'copy-remote|remote=s' => \$_cp_remote,
110 'upgrade' => \$_upgrade } ],
111 'graft-branches' => [ \&graft_branches,
112 'Detect merges/branches from already imported history',
113 { 'merge-rx|m' => \@_opt_m,
114 'branch|b=s' => \@_branch_from,
115 'branch-all-refs|B' => \$_branch_all_refs,
116 'no-default-regex' => \$_no_default_regex,
117 'no-graft-copy' => \$_no_graft_copy } ],
118 'multi-init' => [ \&multi_init,
119 'Initialize multiple trees (like git-svnimport)',
120 { %multi_opts, %fc_opts } ],
121 'multi-fetch' => [ \&multi_fetch,
122 'Fetch multiple trees (like git-svnimport)',
123 \%fc_opts ],
124 'log' => [ \&show_log, 'Show commit logs',
125 { 'limit=i' => \$_limit,
126 'revision|r=s' => \$_revision,
127 'verbose|v' => \$_verbose,
128 'incremental' => \$_incremental,
129 'oneline' => \$_oneline,
130 'show-commit' => \$_show_commit,
131 'non-recursive' => \$_non_recursive,
132 'authors-file|A=s' => \$_authors,
133 } ],
134 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
135 { 'message|m=s' => \$_message,
136 'file|F=s' => \$_file,
137 'revision|r=s' => \$_revision,
138 %cmt_opts } ],
139 dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
140 { 'merge|m|M' => \$_merge,
141 'strategy|s=s' => \$_strategy,
142 'dry-run|n' => \$_dry_run,
143 %cmt_opts } ],
146 my $cmd;
147 for (my $i = 0; $i < @ARGV; $i++) {
148 if (defined $cmd{$ARGV[$i]}) {
149 $cmd = $ARGV[$i];
150 splice @ARGV, $i, 1;
151 last;
155 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
157 read_repo_config(\%opts);
158 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
159 'version|V' => \$_version,
160 'id|i=s' => \$GIT_SVN);
161 exit 1 if (!$rv && $cmd ne 'log');
163 set_default_vals();
164 usage(0) if $_help;
165 version() if $_version;
166 usage(1) unless defined $cmd;
167 init_vars();
168 load_authors() if $_authors;
169 load_all_refs() if $_branch_all_refs;
170 svn_compat_check() unless $_use_lib;
171 migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
172 $cmd{$cmd}->[0]->(@ARGV);
173 exit 0;
175 ####################### primary functions ######################
176 sub usage {
177 my $exit = shift || 0;
178 my $fd = $exit ? \*STDERR : \*STDOUT;
179 print $fd <<"";
180 git-svn - bidirectional operations between a single Subversion tree and git
181 Usage: $0 <command> [options] [arguments]\n
183 print $fd "Available commands:\n" unless $cmd;
185 foreach (sort keys %cmd) {
186 next if $cmd && $cmd ne $_;
187 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
188 foreach (keys %{$cmd{$_}->[2]}) {
189 # prints out arguments as they should be passed:
190 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
191 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
192 "--$_" : "-$_" }
193 split /\|/,$_)," $x\n";
196 print $fd <<"";
197 \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
198 arbitrary identifier if you're tracking multiple SVN branches/repositories in
199 one git repository and want to keep them separate. See git-svn(1) for more
200 information.
202 exit $exit;
205 sub version {
206 print "git-svn version $VERSION\n";
207 exit 0;
210 sub rebuild {
211 if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
212 copy_remote_ref();
214 $SVN_URL = shift or undef;
215 my $newest_rev = 0;
216 if ($_upgrade) {
217 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
218 } else {
219 check_upgrade_needed();
222 my $pid = open(my $rev_list,'-|');
223 defined $pid or croak $!;
224 if ($pid == 0) {
225 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
227 my $latest;
228 while (<$rev_list>) {
229 chomp;
230 my $c = $_;
231 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
232 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
233 next if (!@commit); # skip merges
234 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
235 if (!$rev || !$uuid) {
236 croak "Unable to extract revision or UUID from ",
237 "$c, $commit[$#commit]\n";
240 # if we merged or otherwise started elsewhere, this is
241 # how we break out of it
242 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
243 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
245 unless (defined $latest) {
246 if (!$SVN_URL && !$url) {
247 croak "SVN repository location required: $url\n";
249 $SVN_URL ||= $url;
250 $SVN_UUID ||= $uuid;
251 setup_git_svn();
252 $latest = $rev;
254 revdb_set($REVDB, $rev, $c);
255 print "r$rev = $c\n";
256 $newest_rev = $rev if ($rev > $newest_rev);
258 close $rev_list or croak $?;
260 goto out if $_use_lib;
261 if (!chdir $SVN_WC) {
262 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
263 chdir $SVN_WC or croak $!;
266 $pid = fork;
267 defined $pid or croak $!;
268 if ($pid == 0) {
269 my @svn_up = qw(svn up);
270 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
271 sys(@svn_up,"-r$newest_rev");
272 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
273 index_changes();
274 exec('git-write-tree') or croak $!;
276 waitpid $pid, 0;
277 croak $? if $?;
278 out:
279 if ($_upgrade) {
280 print STDERR <<"";
281 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
282 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
287 sub init {
288 my $url = shift or die "SVN repository location required " .
289 "as a command-line argument\n";
290 $url =~ s!/+$!!; # strip trailing slash
292 if (my $repo_path = shift) {
293 unless (-d $repo_path) {
294 mkpath([$repo_path]);
296 $GIT_DIR = $ENV{GIT_DIR} = $repo_path . "/.git";
297 init_vars();
300 $SVN_URL = $url;
301 unless (-d $GIT_DIR) {
302 my @init_db = ('git-init-db');
303 push @init_db, "--template=$_template" if defined $_template;
304 push @init_db, "--shared" if defined $_shared;
305 sys(@init_db);
307 setup_git_svn();
310 sub fetch {
311 check_upgrade_needed();
312 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
313 my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
314 if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
315 refs/heads/master^0))) {
316 sys(qw(git-update-ref refs/heads/master),$ret->{commit});
318 return $ret;
321 sub fetch_cmd {
322 my (@parents) = @_;
323 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
324 unless ($_revision) {
325 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
327 push @log_args, "-r$_revision";
328 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
330 my $svn_log = svn_log_raw(@log_args);
332 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
333 # don't need last_revision from grab_base_rev() because
334 # user could've specified a different revision to skip (they
335 # didn't want to import certain revisions into git for whatever
336 # reason, so trust $base->{revision} instead.
337 my (undef, $last_commit) = svn_grab_base_rev();
338 unless (-d $SVN_WC) {
339 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
340 chdir $SVN_WC or croak $!;
341 read_uuid();
342 $last_commit = git_commit($base, @parents);
343 assert_tree($last_commit);
344 } else {
345 chdir $SVN_WC or croak $!;
346 read_uuid();
347 # looks like a user manually cp'd and svn switch'ed
348 unless ($last_commit) {
349 sys(qw/svn revert -R ./);
350 assert_svn_wc_clean($base->{revision});
351 $last_commit = git_commit($base, @parents);
352 assert_tree($last_commit);
355 my @svn_up = qw(svn up);
356 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
357 my $last = $base;
358 while (my $log_msg = next_log_entry($svn_log)) {
359 if ($last->{revision} >= $log_msg->{revision}) {
360 croak "Out of order: last >= current: ",
361 "$last->{revision} >= $log_msg->{revision}\n";
363 # Revert is needed for cases like:
364 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
365 # I can't seem to reproduce something like that on a test...
366 sys(qw/svn revert -R ./);
367 assert_svn_wc_clean($last->{revision});
368 sys(@svn_up,"-r$log_msg->{revision}");
369 $last_commit = git_commit($log_msg, $last_commit, @parents);
370 $last = $log_msg;
372 close $svn_log->{fh};
373 $last->{commit} = $last_commit;
374 return $last;
377 sub fetch_lib {
378 my (@parents) = @_;
379 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
380 my $repo;
381 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
382 $SVN_LOG ||= libsvn_connect($repo);
383 $SVN ||= libsvn_connect($repo);
384 my ($last_rev, $last_commit) = svn_grab_base_rev();
385 my ($base, $head) = libsvn_parse_revision($last_rev);
386 if ($base > $head) {
387 return { revision => $last_rev, commit => $last_commit }
389 my $index = set_index($GIT_SVN_INDEX);
391 # limit ourselves and also fork() since get_log won't release memory
392 # after processing a revision and SVN stuff seems to leak
393 my $inc = 1000;
394 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
395 read_uuid();
396 if (defined $last_commit) {
397 unless (-e $GIT_SVN_INDEX) {
398 sys(qw/git-read-tree/, $last_commit);
400 chomp (my $x = `git-write-tree`);
401 my ($y) = (`git-cat-file commit $last_commit`
402 =~ /^tree ($sha1)/m);
403 if ($y ne $x) {
404 unlink $GIT_SVN_INDEX or croak $!;
405 sys(qw/git-read-tree/, $last_commit);
407 chomp ($x = `git-write-tree`);
408 if ($y ne $x) {
409 print STDERR "trees ($last_commit) $y != $x\n",
410 "Something is seriously wrong...\n";
413 while (1) {
414 # fork, because using SVN::Pool with get_log() still doesn't
415 # seem to help enough to keep memory usage down.
416 defined(my $pid = fork) or croak $!;
417 if (!$pid) {
418 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
420 # Yes I'm perfectly aware that the fourth argument
421 # below is the limit revisions number. Unfortunately
422 # performance sucks with it enabled, so it's much
423 # faster to fetch revision ranges instead of relying
424 # on the limiter.
425 libsvn_get_log($SVN_LOG, '/'.$SVN_PATH,
426 $min, $max, 0, 1, 1,
427 sub {
428 my $log_msg;
429 if ($last_commit) {
430 $log_msg = libsvn_fetch(
431 $last_commit, @_);
432 $last_commit = git_commit(
433 $log_msg,
434 $last_commit,
435 @parents);
436 } else {
437 $log_msg = libsvn_new_tree(@_);
438 $last_commit = git_commit(
439 $log_msg, @parents);
442 exit 0;
444 waitpid $pid, 0;
445 croak $? if $?;
446 ($last_rev, $last_commit) = svn_grab_base_rev();
447 last if ($max >= $head);
448 $min = $max + 1;
449 $max += $inc;
450 $max = $head if ($max > $head);
452 restore_index($index);
453 return { revision => $last_rev, commit => $last_commit };
456 sub commit {
457 my (@commits) = @_;
458 check_upgrade_needed();
459 if ($_stdin || !@commits) {
460 print "Reading from stdin...\n";
461 @commits = ();
462 while (<STDIN>) {
463 if (/\b($sha1_short)\b/o) {
464 unshift @commits, $1;
468 my @revs;
469 foreach my $c (@commits) {
470 chomp(my @tmp = safe_qx('git-rev-parse',$c));
471 if (scalar @tmp == 1) {
472 push @revs, $tmp[0];
473 } elsif (scalar @tmp > 1) {
474 push @revs, reverse (safe_qx('git-rev-list',@tmp));
475 } else {
476 die "Failed to rev-parse $c\n";
479 chomp @revs;
480 $_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
481 print "Done committing ",scalar @revs," revisions to SVN\n";
484 sub commit_cmd {
485 my (@revs) = @_;
487 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
488 my $info = svn_info('.');
489 my $fetched = fetch();
490 if ($info->{Revision} != $fetched->{revision}) {
491 print STDERR "There are new revisions that were fetched ",
492 "and need to be merged (or acknowledged) ",
493 "before committing.\n";
494 exit 1;
496 $info = svn_info('.');
497 read_uuid($info);
498 my $last = $fetched;
499 foreach my $c (@revs) {
500 my $mods = svn_checkout_tree($last, $c);
501 if (scalar @$mods == 0) {
502 print "Skipping, no changes detected\n";
503 next;
505 $last = svn_commit_tree($last, $c);
509 sub commit_lib {
510 my (@revs) = @_;
511 my ($r_last, $cmt_last) = svn_grab_base_rev();
512 defined $r_last or die "Must have an existing revision to commit\n";
513 my $fetched = fetch();
514 if ($r_last != $fetched->{revision}) {
515 print STDERR "There are new revisions that were fetched ",
516 "and need to be merged (or acknowledged) ",
517 "before committing.\n",
518 "last rev: $r_last\n",
519 " current: $fetched->{revision}\n";
520 exit 1;
522 read_uuid();
523 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
524 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
526 my $repo;
527 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
528 set_svn_commit_env();
529 foreach my $c (@revs) {
530 my $log_msg = get_commit_message($c, $commit_msg);
532 # fork for each commit because there's a memory leak I
533 # can't track down... (it's probably in the SVN code)
534 defined(my $pid = open my $fh, '-|') or croak $!;
535 if (!$pid) {
536 $SVN_LOG = libsvn_connect($repo);
537 $SVN = libsvn_connect($repo);
538 my $ed = SVN::Git::Editor->new(
539 { r => $r_last,
540 ra => $SVN_LOG,
541 c => $c,
542 svn_path => $SVN_PATH
544 $SVN->get_commit_editor(
545 $log_msg->{msg},
546 sub {
547 libsvn_commit_cb(
548 @_, $c,
549 $log_msg->{msg},
550 $r_last,
551 $cmt_last)
553 @lock)
555 my $mods = libsvn_checkout_tree($cmt_last, $c, $ed);
556 if (@$mods == 0) {
557 print "No changes\nr$r_last = $cmt_last\n";
558 $ed->abort_edit;
559 } else {
560 $ed->close_edit;
562 exit 0;
564 my ($r_new, $cmt_new, $no);
565 while (<$fh>) {
566 print $_;
567 chomp;
568 if (/^r(\d+) = ($sha1)$/o) {
569 ($r_new, $cmt_new) = ($1, $2);
570 } elsif ($_ eq 'No changes') {
571 $no = 1;
574 close $fh or croak $?;
575 if (! defined $r_new && ! defined $cmt_new) {
576 unless ($no) {
577 die "Failed to parse revision information\n";
579 } else {
580 ($r_last, $cmt_last) = ($r_new, $cmt_new);
583 $ENV{LC_ALL} = 'C';
584 unlink $commit_msg;
587 sub dcommit {
588 my $gs = "refs/remotes/$GIT_SVN";
589 chomp(my @refs = safe_qx(qw/git-rev-list --no-merges/, "$gs..HEAD"));
590 my $last_rev;
591 foreach my $d (reverse @refs) {
592 if (quiet_run('git-rev-parse','--verify',"$d~1") != 0) {
593 die "Commit $d\n",
594 "has no parent commit, and therefore ",
595 "nothing to diff against.\n",
596 "You should be working from a repository ",
597 "originally created by git-svn\n";
599 unless (defined $last_rev) {
600 (undef, $last_rev, undef) = cmt_metadata("$d~1");
601 unless (defined $last_rev) {
602 die "Unable to extract revision information ",
603 "from commit $d~1\n";
606 if ($_dry_run) {
607 print "diff-tree $d~1 $d\n";
608 } else {
609 if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
610 $last_rev = $r;
611 } # else: no changes, same $last_rev
614 return if $_dry_run;
615 fetch();
616 my @diff = safe_qx(qw/git-diff-tree HEAD/, $gs);
617 my @finish;
618 if (@diff) {
619 @finish = qw/rebase/;
620 push @finish, qw/--merge/ if $_merge;
621 push @finish, "--strategy=$_strategy" if $_strategy;
622 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
623 } else {
624 print "No changes between current HEAD and $gs\n",
625 "Hard resetting to the latest $gs\n";
626 @finish = qw/reset --hard/;
628 sys('git', @finish, $gs);
631 sub show_ignore {
632 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
633 $_use_lib ? show_ignore_lib() : show_ignore_cmd();
636 sub show_ignore_cmd {
637 require File::Find or die $!;
638 if (defined $_revision) {
639 die "-r/--revision option doesn't work unless the Perl SVN ",
640 "libraries are used\n";
642 chdir $SVN_WC or croak $!;
643 my %ign;
644 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
645 s#^\./##;
646 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
647 }}, no_chdir=>1},'.');
649 print "\n# /\n";
650 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
651 delete $ign{'.'};
652 foreach my $i (sort keys %ign) {
653 print "\n# ",$i,"\n";
654 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
658 sub show_ignore_lib {
659 my $repo;
660 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
661 $SVN ||= libsvn_connect($repo);
662 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
663 libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
666 sub graft_branches {
667 my $gr_file = "$GIT_DIR/info/grafts";
668 my ($grafts, $comments) = read_grafts($gr_file);
669 my $gr_sha1;
671 if (%$grafts) {
672 # temporarily disable our grafts file to make this idempotent
673 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
674 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
677 my $l_map = read_url_paths();
678 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
679 unless ($_no_default_regex) {
680 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
681 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
682 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
684 foreach my $u (keys %$l_map) {
685 if (@re) {
686 foreach my $p (keys %{$l_map->{$u}}) {
687 graft_merge_msg($grafts,$l_map,$u,$p,@re);
690 unless ($_no_graft_copy) {
691 if ($_use_lib) {
692 graft_file_copy_lib($grafts,$l_map,$u);
693 } else {
694 graft_file_copy_cmd($grafts,$l_map,$u);
698 graft_tree_joins($grafts);
700 write_grafts($grafts, $comments, $gr_file);
701 unlink "$gr_file~$gr_sha1" if $gr_sha1;
704 sub multi_init {
705 my $url = shift;
706 $_trunk ||= 'trunk';
707 $_trunk =~ s#/+$##;
708 $url =~ s#/+$## if $url;
709 if ($_trunk !~ m#^[a-z\+]+://#) {
710 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
711 unless ($url) {
712 print STDERR "E: '$_trunk' is not a complete URL ",
713 "and a separate URL is not specified\n";
714 exit 1;
716 $_trunk = $url . $_trunk;
718 my $ch_id;
719 if ($GIT_SVN eq 'git-svn') {
720 $ch_id = 1;
721 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
723 init_vars();
724 unless (-d $GIT_SVN_DIR) {
725 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
726 init($_trunk);
727 sys('git-repo-config', 'svn.trunk', $_trunk);
729 complete_url_ls_init($url, $_branches, '--branches/-b', '');
730 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
733 sub multi_fetch {
734 # try to do trunk first, since branches/tags
735 # may be descended from it.
736 if (-e "$GIT_DIR/svn/trunk/info/url") {
737 fetch_child_id('trunk', @_);
739 rec_fetch('', "$GIT_DIR/svn", @_);
742 sub show_log {
743 my (@args) = @_;
744 my ($r_min, $r_max);
745 my $r_last = -1; # prevent dupes
746 rload_authors() if $_authors;
747 if (defined $TZ) {
748 $ENV{TZ} = $TZ;
749 } else {
750 delete $ENV{TZ};
752 if (defined $_revision) {
753 if ($_revision =~ /^(\d+):(\d+)$/) {
754 ($r_min, $r_max) = ($1, $2);
755 } elsif ($_revision =~ /^\d+$/) {
756 $r_min = $r_max = $_revision;
757 } else {
758 print STDERR "-r$_revision is not supported, use ",
759 "standard \'git log\' arguments instead\n";
760 exit 1;
764 my $pid = open(my $log,'-|');
765 defined $pid or croak $!;
766 if (!$pid) {
767 exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
769 setup_pager();
770 my (@k, $c, $d);
772 while (<$log>) {
773 if (/^commit ($sha1_short)/o) {
774 my $cmt = $1;
775 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
776 $r_last = $c->{r};
777 process_commit($c, $r_min, $r_max, \@k) or
778 goto out;
780 $d = undef;
781 $c = { c => $cmt };
782 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
783 get_author_info($c, $1, $2, $3);
784 } elsif (/^(?:tree|parent|committer) /) {
785 # ignore
786 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
787 push @{$c->{raw}}, $_;
788 } elsif (/^[ACRMDT]\t/) {
789 # we could add $SVN_PATH here, but that requires
790 # remote access at the moment (repo_path_split)...
791 s#^([ACRMDT])\t# $1 #;
792 push @{$c->{changed}}, $_;
793 } elsif (/^diff /) {
794 $d = 1;
795 push @{$c->{diff}}, $_;
796 } elsif ($d) {
797 push @{$c->{diff}}, $_;
798 } elsif (/^ (git-svn-id:.+)$/) {
799 ($c->{url}, $c->{r}, undef) = extract_metadata($1);
800 } elsif (s/^ //) {
801 push @{$c->{l}}, $_;
804 if ($c && defined $c->{r} && $c->{r} != $r_last) {
805 $r_last = $c->{r};
806 process_commit($c, $r_min, $r_max, \@k);
808 if (@k) {
809 my $swap = $r_max;
810 $r_max = $r_min;
811 $r_min = $swap;
812 process_commit($_, $r_min, $r_max) foreach reverse @k;
814 out:
815 close $log;
816 print '-' x72,"\n" unless $_incremental || $_oneline;
819 sub commit_diff_usage {
820 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
821 exit 1
824 sub commit_diff {
825 if (!$_use_lib) {
826 print STDERR "commit-diff must be used with SVN libraries\n";
827 exit 1;
829 my $ta = shift or commit_diff_usage();
830 my $tb = shift or commit_diff_usage();
831 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
832 print STDERR "Needed URL or usable git-svn id command-line\n";
833 commit_diff_usage();
835 my $r = shift || $_revision;
836 die "-r|--revision is a required argument\n" unless (defined $r);
837 if (defined $_message && defined $_file) {
838 print STDERR "Both --message/-m and --file/-F specified ",
839 "for the commit message.\n",
840 "I have no idea what you mean\n";
841 exit 1;
843 if (defined $_file) {
844 $_message = file_to_s($_file);
845 } else {
846 $_message ||= get_commit_message($tb,
847 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
849 my $repo;
850 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
851 $SVN_LOG ||= libsvn_connect($repo);
852 $SVN ||= libsvn_connect($repo);
853 if ($r eq 'HEAD') {
854 $r = $SVN->get_latest_revnum;
855 } elsif ($r !~ /^\d+$/) {
856 die "revision argument: $r not understood by git-svn\n";
858 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
859 my $rev_committed;
860 my $ed = SVN::Git::Editor->new({ r => $r,
861 ra => $SVN_LOG, c => $tb,
862 svn_path => $SVN_PATH
864 $SVN->get_commit_editor($_message,
865 sub {
866 $rev_committed = $_[0];
867 print "Committed $_[0]\n";
868 }, @lock)
870 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
871 if (@$mods == 0) {
872 print "No changes\n$ta == $tb\n";
873 $ed->abort_edit;
874 } else {
875 $ed->close_edit;
877 $_message = $_file = undef;
878 return $rev_committed;
881 ########################### utility functions #########################
883 sub cmt_showable {
884 my ($c) = @_;
885 return 1 if defined $c->{r};
886 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
887 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
888 my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
889 shift @msg while ($msg[0] ne "\n");
890 shift @msg;
891 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
893 (undef, $c->{r}, undef) = extract_metadata(
894 (grep(/^git-svn-id: /, @msg))[-1]);
896 return defined $c->{r};
899 sub git_svn_log_cmd {
900 my ($r_min, $r_max) = @_;
901 my @cmd = (qw/git-log --abbrev-commit --pretty=raw
902 --default/, "refs/remotes/$GIT_SVN");
903 push @cmd, '-r' unless $_non_recursive;
904 push @cmd, qw/--raw --name-status/ if $_verbose;
905 return @cmd unless defined $r_max;
906 if ($r_max == $r_min) {
907 push @cmd, '--max-count=1';
908 if (my $c = revdb_get($REVDB, $r_max)) {
909 push @cmd, $c;
911 } else {
912 my ($c_min, $c_max);
913 $c_max = revdb_get($REVDB, $r_max);
914 $c_min = revdb_get($REVDB, $r_min);
915 if (defined $c_min && defined $c_max) {
916 if ($r_max > $r_max) {
917 push @cmd, "$c_min..$c_max";
918 } else {
919 push @cmd, "$c_max..$c_min";
921 } elsif ($r_max > $r_min) {
922 push @cmd, $c_max;
923 } else {
924 push @cmd, $c_min;
927 return @cmd;
930 sub fetch_child_id {
931 my $id = shift;
932 print "Fetching $id\n";
933 my $ref = "$GIT_DIR/refs/remotes/$id";
934 defined(my $pid = open my $fh, '-|') or croak $!;
935 if (!$pid) {
936 $_repack = undef;
937 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
938 init_vars();
939 fetch(@_);
940 exit 0;
942 while (<$fh>) {
943 print $_;
944 check_repack() if (/^r\d+ = $sha1/);
946 close $fh or croak $?;
949 sub rec_fetch {
950 my ($pfx, $p, @args) = @_;
951 my @dir;
952 foreach (sort <$p/*>) {
953 if (-r "$_/info/url") {
954 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
955 my $id = $pfx . basename $_;
956 next if $id eq 'trunk';
957 fetch_child_id($id, @args);
958 } elsif (-d $_) {
959 push @dir, $_;
962 foreach (@dir) {
963 my $x = $_;
964 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
965 rec_fetch($x, $_);
969 sub complete_url_ls_init {
970 my ($url, $var, $switch, $pfx) = @_;
971 unless ($var) {
972 print STDERR "W: $switch not specified\n";
973 return;
975 $var =~ s#/+$##;
976 if ($var !~ m#^[a-z\+]+://#) {
977 $var = '/' . $var if ($var !~ m#^/#);
978 unless ($url) {
979 print STDERR "E: '$var' is not a complete URL ",
980 "and a separate URL is not specified\n";
981 exit 1;
983 $var = $url . $var;
985 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
986 : safe_qx(qw/svn ls --non-interactive/, $var));
987 my $old = $GIT_SVN;
988 defined(my $pid = fork) or croak $!;
989 if (!$pid) {
990 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
991 $u =~ s#/+$##;
992 if ($u !~ m!\Q$var\E/(.+)$!) {
993 print STDERR "W: Unrecognized URL: $u\n";
994 die "This should never happen\n";
996 # don't try to init already existing refs
997 my $id = $pfx.$1;
998 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
999 init_vars();
1000 unless (-d $GIT_SVN_DIR) {
1001 print "init $u => $id\n";
1002 init($u);
1005 exit 0;
1007 waitpid $pid, 0;
1008 croak $? if $?;
1009 my ($n) = ($switch =~ /^--(\w+)/);
1010 sys('git-repo-config', "svn.$n", $var);
1013 sub common_prefix {
1014 my $paths = shift;
1015 my %common;
1016 foreach (@$paths) {
1017 my @tmp = split m#/#, $_;
1018 my $p = '';
1019 while (my $x = shift @tmp) {
1020 $p .= "/$x";
1021 $common{$p} ||= 0;
1022 $common{$p}++;
1025 foreach (sort {length $b <=> length $a} keys %common) {
1026 if ($common{$_} == @$paths) {
1027 return $_;
1030 return '';
1033 # grafts set here are 'stronger' in that they're based on actual tree
1034 # matches, and won't be deleted from merge-base checking in write_grafts()
1035 sub graft_tree_joins {
1036 my $grafts = shift;
1037 map_tree_joins() if (@_branch_from && !%tree_map);
1038 return unless %tree_map;
1040 git_svn_each(sub {
1041 my $i = shift;
1042 defined(my $pid = open my $fh, '-|') or croak $!;
1043 if (!$pid) {
1044 exec qw/git-rev-list --pretty=raw/,
1045 "refs/remotes/$i" or croak $!;
1047 while (<$fh>) {
1048 next unless /^commit ($sha1)$/o;
1049 my $c = $1;
1050 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1051 next unless $tree_map{$t};
1053 my $l;
1054 do {
1055 $l = readline $fh;
1056 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1058 my ($s, $tz) = ($1, $2);
1059 if ($tz =~ s/^\+//) {
1060 $s += tz_to_s_offset($tz);
1061 } elsif ($tz =~ s/^\-//) {
1062 $s -= tz_to_s_offset($tz);
1065 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
1067 foreach my $p (@{$tree_map{$t}}) {
1068 next if $p eq $c;
1069 my $mb = eval {
1070 safe_qx('git-merge-base', $c, $p)
1072 next unless ($@ || $?);
1073 if (defined $r_a) {
1074 # see if SVN says it's a relative
1075 my ($url_b, $r_b, $uuid_b) =
1076 cmt_metadata($p);
1077 next if (defined $url_b &&
1078 defined $url_a &&
1079 ($url_a eq $url_b) &&
1080 ($uuid_a eq $uuid_b));
1081 if ($uuid_a eq $uuid_b) {
1082 if ($r_b < $r_a) {
1083 $grafts->{$c}->{$p} = 2;
1084 next;
1085 } elsif ($r_b > $r_a) {
1086 $grafts->{$p}->{$c} = 2;
1087 next;
1091 my $ct = get_commit_time($p);
1092 if ($ct < $s) {
1093 $grafts->{$c}->{$p} = 2;
1094 } elsif ($ct > $s) {
1095 $grafts->{$p}->{$c} = 2;
1097 # what should we do when $ct == $s ?
1100 close $fh or croak $?;
1104 # this isn't funky-filename safe, but good enough for now...
1105 sub graft_file_copy_cmd {
1106 my ($grafts, $l_map, $u) = @_;
1107 my $paths = $l_map->{$u};
1108 my $pfx = common_prefix([keys %$paths]);
1109 $SVN_URL ||= $u.$pfx;
1110 my $pid = open my $fh, '-|';
1111 defined $pid or croak $!;
1112 unless ($pid) {
1113 my @exec = qw/svn log -v/;
1114 push @exec, "-r$_revision" if defined $_revision;
1115 exec @exec, $u.$pfx or croak $!;
1117 my ($r, $mp) = (undef, undef);
1118 while (<$fh>) {
1119 chomp;
1120 if (/^\-{72}$/) {
1121 $mp = $r = undef;
1122 } elsif (/^r(\d+) \| /) {
1123 $r = $1 unless defined $r;
1124 } elsif (/^Changed paths:/) {
1125 $mp = 1;
1126 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1127 my ($p1, $p0, $r0) = ($1, $2, $3);
1128 my $c = find_graft_path_commit($paths, $p1, $r);
1129 next unless $c;
1130 find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
1135 sub graft_file_copy_lib {
1136 my ($grafts, $l_map, $u) = @_;
1137 my $tree_paths = $l_map->{$u};
1138 my $pfx = common_prefix([keys %$tree_paths]);
1139 my ($repo, $path) = repo_path_split($u.$pfx);
1140 $SVN_LOG ||= libsvn_connect($repo);
1141 $SVN ||= libsvn_connect($repo);
1143 my ($base, $head) = libsvn_parse_revision();
1144 my $inc = 1000;
1145 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
1146 my $eh = $SVN::Error::handler;
1147 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
1148 while (1) {
1149 my $pool = SVN::Pool->new;
1150 libsvn_get_log($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1151 sub {
1152 libsvn_graft_file_copies($grafts, $tree_paths,
1153 $path, @_);
1154 }, $pool);
1155 $pool->clear;
1156 last if ($max >= $head);
1157 $min = $max + 1;
1158 $max += $inc;
1159 $max = $head if ($max > $head);
1161 $SVN::Error::handler = $eh;
1164 sub process_merge_msg_matches {
1165 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1166 my (@strong, @weak);
1167 foreach (@matches) {
1168 # merging with ourselves is not interesting
1169 next if $_ eq $p;
1170 if ($l_map->{$u}->{$_}) {
1171 push @strong, $_;
1172 } else {
1173 push @weak, $_;
1176 foreach my $w (@weak) {
1177 last if @strong;
1178 # no exact match, use branch name as regexp.
1179 my $re = qr/\Q$w\E/i;
1180 foreach (keys %{$l_map->{$u}}) {
1181 if (/$re/) {
1182 push @strong, $l_map->{$u}->{$_};
1183 last;
1186 last if @strong;
1187 $w = basename($w);
1188 $re = qr/\Q$w\E/i;
1189 foreach (keys %{$l_map->{$u}}) {
1190 if (/$re/) {
1191 push @strong, $l_map->{$u}->{$_};
1192 last;
1196 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1197 \s(?:[a-f\d\-]+)$/xsm);
1198 unless (defined $rev) {
1199 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1200 \@(?:[a-f\d\-]+)/xsm);
1201 return unless defined $rev;
1203 foreach my $m (@strong) {
1204 my ($r0, $s0) = find_rev_before($rev, $m, 1);
1205 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1209 sub graft_merge_msg {
1210 my ($grafts, $l_map, $u, $p, @re) = @_;
1212 my $x = $l_map->{$u}->{$p};
1213 my $rl = rev_list_raw($x);
1214 while (my $c = next_rev_list_entry($rl)) {
1215 foreach my $re (@re) {
1216 my (@br) = ($c->{m} =~ /$re/g);
1217 next unless @br;
1218 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1223 sub read_uuid {
1224 return if $SVN_UUID;
1225 if ($_use_lib) {
1226 my $pool = SVN::Pool->new;
1227 $SVN_UUID = $SVN->get_uuid($pool);
1228 $pool->clear;
1229 } else {
1230 my $info = shift || svn_info('.');
1231 $SVN_UUID = $info->{'Repository UUID'} or
1232 croak "Repository UUID unreadable\n";
1236 sub quiet_run {
1237 my $pid = fork;
1238 defined $pid or croak $!;
1239 if (!$pid) {
1240 open my $null, '>', '/dev/null' or croak $!;
1241 open STDERR, '>&', $null or croak $!;
1242 open STDOUT, '>&', $null or croak $!;
1243 exec @_ or croak $!;
1245 waitpid $pid, 0;
1246 return $?;
1249 sub repo_path_split {
1250 my $full_url = shift;
1251 $full_url =~ s#/+$##;
1253 foreach (@repo_path_split_cache) {
1254 if ($full_url =~ s#$_##) {
1255 my $u = $1;
1256 $full_url =~ s#^/+##;
1257 return ($u, $full_url);
1261 if ($_use_lib) {
1262 my $tmp = libsvn_connect($full_url);
1263 my $url = $tmp->get_repos_root;
1264 $full_url =~ s#^\Q$url\E/*##;
1265 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1266 return ($url, $full_url);
1267 } else {
1268 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1269 $path =~ s#^/+##;
1270 my @paths = split(m#/+#, $path);
1271 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
1272 my $n = shift @paths || last;
1273 $url .= "/$n";
1275 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1276 $path = join('/',@paths);
1277 return ($url, $path);
1281 sub setup_git_svn {
1282 defined $SVN_URL or croak "SVN repository location required\n";
1283 unless (-d $GIT_DIR) {
1284 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1286 mkpath([$GIT_SVN_DIR]);
1287 mkpath(["$GIT_SVN_DIR/info"]);
1288 open my $fh, '>>',$REVDB or croak $!;
1289 close $fh;
1290 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
1294 sub assert_svn_wc_clean {
1295 return if $_use_lib;
1296 my ($svn_rev) = @_;
1297 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1298 my $lcr = svn_info('.')->{'Last Changed Rev'};
1299 if ($svn_rev != $lcr) {
1300 print STDERR "Checking for copy-tree ... ";
1301 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
1302 "-r$lcr:$svn_rev")));
1303 if (@diff) {
1304 croak "Nope! Expected r$svn_rev, got r$lcr\n";
1305 } else {
1306 print STDERR "OK!\n";
1309 my @status = grep(!/^Performing status on external/,(`svn status`));
1310 @status = grep(!/^\s*$/,@status);
1311 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1312 if (scalar @status) {
1313 print STDERR "Tree ($SVN_WC) is not clean:\n";
1314 print STDERR $_ foreach @status;
1315 croak;
1319 sub get_tree_from_treeish {
1320 my ($treeish) = @_;
1321 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1322 chomp(my $type = `git-cat-file -t $treeish`);
1323 my $expected;
1324 while ($type eq 'tag') {
1325 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1327 if ($type eq 'commit') {
1328 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1329 ($expected) = ($expected =~ /^tree ($sha1)$/);
1330 die "Unable to get tree from $treeish\n" unless $expected;
1331 } elsif ($type eq 'tree') {
1332 $expected = $treeish;
1333 } else {
1334 die "$treeish is a $type, expected tree, tag or commit\n";
1336 return $expected;
1339 sub assert_tree {
1340 return if $_use_lib;
1341 my ($treeish) = @_;
1342 my $expected = get_tree_from_treeish($treeish);
1344 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1345 if (-e $tmpindex) {
1346 unlink $tmpindex or croak $!;
1348 my $old_index = set_index($tmpindex);
1349 index_changes(1);
1350 chomp(my $tree = `git-write-tree`);
1351 restore_index($old_index);
1352 if ($tree ne $expected) {
1353 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
1355 unlink $tmpindex;
1358 sub parse_diff_tree {
1359 my $diff_fh = shift;
1360 local $/ = "\0";
1361 my $state = 'meta';
1362 my @mods;
1363 while (<$diff_fh>) {
1364 chomp $_; # this gets rid of the trailing "\0"
1365 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1366 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1367 push @mods, { mode_a => $1, mode_b => $2,
1368 sha1_b => $3, chg => $4 };
1369 if ($4 =~ /^(?:C|R)$/) {
1370 $state = 'file_a';
1371 } else {
1372 $state = 'file_b';
1374 } elsif ($state eq 'file_a') {
1375 my $x = $mods[$#mods] or croak "Empty array\n";
1376 if ($x->{chg} !~ /^(?:C|R)$/) {
1377 croak "Error parsing $_, $x->{chg}\n";
1379 $x->{file_a} = $_;
1380 $state = 'file_b';
1381 } elsif ($state eq 'file_b') {
1382 my $x = $mods[$#mods] or croak "Empty array\n";
1383 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1384 croak "Error parsing $_, $x->{chg}\n";
1386 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1387 croak "Error parsing $_, $x->{chg}\n";
1389 $x->{file_b} = $_;
1390 $state = 'meta';
1391 } else {
1392 croak "Error parsing $_\n";
1395 close $diff_fh or croak $?;
1397 return \@mods;
1400 sub svn_check_prop_executable {
1401 my $m = shift;
1402 return if -l $m->{file_b};
1403 if ($m->{mode_b} =~ /755$/) {
1404 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
1405 if ($m->{mode_a} !~ /755$/) {
1406 sys(qw(svn propset svn:executable 1), $m->{file_b});
1408 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
1409 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
1410 sys(qw(svn propdel svn:executable), $m->{file_b});
1411 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
1412 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
1416 sub svn_ensure_parent_path {
1417 my $dir_b = dirname(shift);
1418 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
1419 mkpath([$dir_b]) unless (-d $dir_b);
1420 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
1423 sub precommit_check {
1424 my $mods = shift;
1425 my (%rm_file, %rmdir_check, %added_check);
1427 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
1428 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1429 if ($m->{chg} eq 'R') {
1430 if (-d $m->{file_b}) {
1431 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1433 # dir/$file => dir/file/$file
1434 my $dirname = dirname($m->{file_b});
1435 while ($dirname ne File::Spec->curdir) {
1436 if ($dirname ne $m->{file_a}) {
1437 $dirname = dirname($dirname);
1438 next;
1440 err_file_to_dir("$m->{file_a} => $m->{file_b}");
1442 # baz/zzz => baz (baz is a file)
1443 $dirname = dirname($m->{file_a});
1444 while ($dirname ne File::Spec->curdir) {
1445 if ($dirname ne $m->{file_b}) {
1446 $dirname = dirname($dirname);
1447 next;
1449 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1452 if ($m->{chg} =~ /^(D|R)$/) {
1453 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
1454 $rm_file{ $m->{$t} } = 1;
1455 my $dirname = dirname( $m->{$t} );
1456 my $basename = basename( $m->{$t} );
1457 $rmdir_check{$dirname}->{$basename} = 1;
1458 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
1459 if (-d $m->{file_b}) {
1460 err_dir_to_file($m->{file_b});
1462 my $dirname = dirname( $m->{file_b} );
1463 my $basename = basename( $m->{file_b} );
1464 $added_check{$dirname}->{$basename} = 1;
1465 while ($dirname ne File::Spec->curdir) {
1466 if ($rm_file{$dirname}) {
1467 err_file_to_dir($m->{file_b});
1469 $dirname = dirname $dirname;
1473 return (\%rmdir_check, \%added_check);
1475 sub err_dir_to_file {
1476 my $file = shift;
1477 print STDERR "Node change from directory to file ",
1478 "is not supported by Subversion: ",$file,"\n";
1479 exit 1;
1481 sub err_file_to_dir {
1482 my $file = shift;
1483 print STDERR "Node change from file to directory ",
1484 "is not supported by Subversion: ",$file,"\n";
1485 exit 1;
1490 sub get_diff {
1491 my ($from, $treeish) = @_;
1492 assert_tree($from);
1493 print "diff-tree $from $treeish\n";
1494 my $pid = open my $diff_fh, '-|';
1495 defined $pid or croak $!;
1496 if ($pid == 0) {
1497 my @diff_tree = qw(git-diff-tree -z -r);
1498 if ($_cp_similarity) {
1499 push @diff_tree, "-C$_cp_similarity";
1500 } else {
1501 push @diff_tree, '-C';
1503 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1504 push @diff_tree, "-l$_l" if defined $_l;
1505 exec(@diff_tree, $from, $treeish) or croak $!;
1507 return parse_diff_tree($diff_fh);
1510 sub svn_checkout_tree {
1511 my ($from, $treeish) = @_;
1512 my $mods = get_diff($from->{commit}, $treeish);
1513 return $mods unless (scalar @$mods);
1514 my ($rm, $add) = precommit_check($mods);
1516 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1517 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1518 if ($m->{chg} eq 'C') {
1519 svn_ensure_parent_path( $m->{file_b} );
1520 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
1521 apply_mod_line_blob($m);
1522 svn_check_prop_executable($m);
1523 } elsif ($m->{chg} eq 'D') {
1524 sys(qw(svn rm --force), $m->{file_b});
1525 } elsif ($m->{chg} eq 'R') {
1526 svn_ensure_parent_path( $m->{file_b} );
1527 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
1528 apply_mod_line_blob($m);
1529 svn_check_prop_executable($m);
1530 } elsif ($m->{chg} eq 'M') {
1531 apply_mod_line_blob($m);
1532 svn_check_prop_executable($m);
1533 } elsif ($m->{chg} eq 'T') {
1534 svn_check_prop_executable($m);
1535 apply_mod_line_blob($m);
1536 if ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
1537 sys(qw(svn propdel svn:special), $m->{file_b});
1538 } else {
1539 sys(qw(svn propset svn:special *),$m->{file_b});
1541 } elsif ($m->{chg} eq 'A') {
1542 svn_ensure_parent_path( $m->{file_b} );
1543 apply_mod_line_blob($m);
1544 sys(qw(svn add), $m->{file_b});
1545 svn_check_prop_executable($m);
1546 } else {
1547 croak "Invalid chg: $m->{chg}\n";
1551 assert_tree($treeish);
1552 if ($_rmdir) { # remove empty directories
1553 handle_rmdir($rm, $add);
1555 assert_tree($treeish);
1556 return $mods;
1559 sub libsvn_checkout_tree {
1560 my ($from, $treeish, $ed) = @_;
1561 my $mods = get_diff($from, $treeish);
1562 return $mods unless (scalar @$mods);
1563 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1564 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1565 my $f = $m->{chg};
1566 if (defined $o{$f}) {
1567 $ed->$f($m, $_q);
1568 } else {
1569 croak "Invalid change type: $f\n";
1572 $ed->rmdirs($_q) if $_rmdir;
1573 return $mods;
1576 # svn ls doesn't work with respect to the current working tree, but what's
1577 # in the repository. There's not even an option for it... *sigh*
1578 # (added files don't show up and removed files remain in the ls listing)
1579 sub svn_ls_current {
1580 my ($dir, $rm, $add) = @_;
1581 chomp(my @ls = safe_qx('svn','ls',$dir));
1582 my @ret = ();
1583 foreach (@ls) {
1584 s#/$##; # trailing slashes are evil
1585 push @ret, $_ unless $rm->{$dir}->{$_};
1587 if (exists $add->{$dir}) {
1588 push @ret, keys %{$add->{$dir}};
1590 return \@ret;
1593 sub handle_rmdir {
1594 my ($rm, $add) = @_;
1596 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1597 my $ls = svn_ls_current($dir, $rm, $add);
1598 next if (scalar @$ls);
1599 sys(qw(svn rm --force),$dir);
1601 my $dn = dirname $dir;
1602 $rm->{ $dn }->{ basename $dir } = 1;
1603 $ls = svn_ls_current($dn, $rm, $add);
1604 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1605 sys(qw(svn rm --force),$dn);
1606 $dir = basename $dn;
1607 $dn = dirname $dn;
1608 $rm->{ $dn }->{ $dir } = 1;
1609 $ls = svn_ls_current($dn, $rm, $add);
1614 sub get_commit_message {
1615 my ($commit, $commit_msg) = (@_);
1616 my %log_msg = ( msg => '' );
1617 open my $msg, '>', $commit_msg or croak $!;
1619 chomp(my $type = `git-cat-file -t $commit`);
1620 if ($type eq 'commit' || $type eq 'tag') {
1621 my $pid = open my $msg_fh, '-|';
1622 defined $pid or croak $!;
1624 if ($pid == 0) {
1625 exec('git-cat-file', $type, $commit) or croak $!;
1627 my $in_msg = 0;
1628 while (<$msg_fh>) {
1629 if (!$in_msg) {
1630 $in_msg = 1 if (/^\s*$/);
1631 } elsif (/^git-svn-id: /) {
1632 # skip this, we regenerate the correct one
1633 # on re-fetch anyways
1634 } else {
1635 print $msg $_ or croak $!;
1638 close $msg_fh or croak $?;
1640 close $msg or croak $!;
1642 if ($_edit || ($type eq 'tree')) {
1643 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1644 system($editor, $commit_msg);
1647 # file_to_s removes all trailing newlines, so just use chomp() here:
1648 open $msg, '<', $commit_msg or croak $!;
1649 { local $/; chomp($log_msg{msg} = <$msg>); }
1650 close $msg or croak $!;
1652 return \%log_msg;
1655 sub set_svn_commit_env {
1656 if (defined $LC_ALL) {
1657 $ENV{LC_ALL} = $LC_ALL;
1658 } else {
1659 delete $ENV{LC_ALL};
1663 sub svn_commit_tree {
1664 my ($last, $commit) = @_;
1665 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1666 my $log_msg = get_commit_message($commit, $commit_msg);
1667 my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1668 print "Committing $commit: $oneline\n";
1670 set_svn_commit_env();
1671 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1672 $ENV{LC_ALL} = 'C';
1673 unlink $commit_msg;
1674 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1675 if (!defined $committed) {
1676 my $out = join("\n",@ci_output);
1677 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1678 $out, "\n\nAssuming English locale...";
1679 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1680 defined $committed or die " FAILED!\n",
1681 "Commit output failed to parse committed revision!\n",
1682 print STDERR " OK\n";
1685 my @svn_up = qw(svn up);
1686 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1687 if ($_optimize_commits && ($committed == ($last->{revision} + 1))) {
1688 push @svn_up, "-r$committed";
1689 sys(@svn_up);
1690 my $info = svn_info('.');
1691 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1692 if ($info->{'Last Changed Rev'} != $committed) {
1693 croak "$info->{'Last Changed Rev'} != $committed\n"
1695 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1696 /(\d{4})\-(\d\d)\-(\d\d)\s
1697 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1698 or croak "Failed to parse date: $date\n";
1699 $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
1700 $log_msg->{author} = $info->{'Last Changed Author'};
1701 $log_msg->{revision} = $committed;
1702 $log_msg->{msg} .= "\n";
1703 $log_msg->{parents} = [ $last->{commit} ];
1704 $log_msg->{commit} = git_commit($log_msg, $commit);
1705 return $log_msg;
1707 # resync immediately
1708 push @svn_up, "-r$last->{revision}";
1709 sys(@svn_up);
1710 return fetch("$committed=$commit");
1713 sub rev_list_raw {
1714 my (@args) = @_;
1715 my $pid = open my $fh, '-|';
1716 defined $pid or croak $!;
1717 if (!$pid) {
1718 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1720 return { fh => $fh, t => { } };
1723 sub next_rev_list_entry {
1724 my $rl = shift;
1725 my $fh = $rl->{fh};
1726 my $x = $rl->{t};
1727 while (<$fh>) {
1728 if (/^commit ($sha1)$/o) {
1729 if ($x->{c}) {
1730 $rl->{t} = { c => $1 };
1731 return $x;
1732 } else {
1733 $x->{c} = $1;
1735 } elsif (/^parent ($sha1)$/o) {
1736 $x->{p}->{$1} = 1;
1737 } elsif (s/^ //) {
1738 $x->{m} ||= '';
1739 $x->{m} .= $_;
1742 return ($x != $rl->{t}) ? $x : undef;
1745 # read the entire log into a temporary file (which is removed ASAP)
1746 # and store the file handle + parser state
1747 sub svn_log_raw {
1748 my (@log_args) = @_;
1749 my $log_fh = IO::File->new_tmpfile or croak $!;
1750 my $pid = fork;
1751 defined $pid or croak $!;
1752 if (!$pid) {
1753 open STDOUT, '>&', $log_fh or croak $!;
1754 exec (qw(svn log), @log_args) or croak $!
1756 waitpid $pid, 0;
1757 croak $? if $?;
1758 seek $log_fh, 0, 0 or croak $!;
1759 return { state => 'sep', fh => $log_fh };
1762 sub next_log_entry {
1763 my $log = shift; # retval of svn_log_raw()
1764 my $ret = undef;
1765 my $fh = $log->{fh};
1767 while (<$fh>) {
1768 chomp;
1769 if (/^\-{72}$/) {
1770 if ($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},
1779 "\n";
1781 next;
1783 if ($log->{state} ne 'sep') {
1784 croak "Log parse error at: $_\n",
1785 "state: $log->{state}\n",
1786 $ret->{revision},
1787 "\n";
1789 $log->{state} = 'rev';
1791 # if we have an empty log message, put something there:
1792 if ($ret) {
1793 $ret->{msg} ||= "\n";
1794 delete $ret->{lines};
1795 return $ret;
1797 next;
1799 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1800 my $rev = $1;
1801 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1802 ($lines) = ($lines =~ /(\d+)/);
1803 $date = '1970-01-01 00:00:00 +0000'
1804 if ($_ignore_nodate && $date eq '(no date)');
1805 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1806 /(\d{4})\-(\d\d)\-(\d\d)\s
1807 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1808 or croak "Failed to parse date: $date\n";
1809 $ret = { revision => $rev,
1810 date => "$tz $Y-$m-$d $H:$M:$S",
1811 author => $author,
1812 lines => $lines,
1813 msg => '' };
1814 if (defined $_authors && ! defined $users{$author}) {
1815 die "Author: $author not defined in ",
1816 "$_authors file\n";
1818 $log->{state} = 'msg_start';
1819 next;
1821 # skip the first blank line of the message:
1822 if ($log->{state} eq 'msg_start' && /^$/) {
1823 $log->{state} = 'msg';
1824 } elsif ($log->{state} eq 'msg') {
1825 if ($ret->{lines}) {
1826 $ret->{msg} .= $_."\n";
1827 unless (--$ret->{lines}) {
1828 $log->{state} = 'sep';
1830 } else {
1831 croak "Log parse error at: $_\n",
1832 $ret->{revision},"\n";
1836 return $ret;
1839 sub svn_info {
1840 my $url = shift || $SVN_URL;
1842 my $pid = open my $info_fh, '-|';
1843 defined $pid or croak $!;
1845 if ($pid == 0) {
1846 exec(qw(svn info),$url) or croak $!;
1849 my $ret = {};
1850 # only single-lines seem to exist in svn info output
1851 while (<$info_fh>) {
1852 chomp $_;
1853 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
1854 $ret->{$1} = $2;
1855 push @{$ret->{-order}}, $1;
1858 close $info_fh or croak $?;
1859 return $ret;
1862 sub sys { system(@_) == 0 or croak $? }
1864 sub do_update_index {
1865 my ($z_cmd, $cmd, $no_text_base) = @_;
1867 my $z = open my $p, '-|';
1868 defined $z or croak $!;
1869 unless ($z) { exec @$z_cmd or croak $! }
1871 my $pid = open my $ui, '|-';
1872 defined $pid or croak $!;
1873 unless ($pid) {
1874 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1876 local $/ = "\0";
1877 while (my $x = <$p>) {
1878 chomp $x;
1879 if (!$no_text_base && lstat $x && ! -l _ &&
1880 svn_propget_base('svn:keywords', $x)) {
1881 my $mode = -x _ ? 0755 : 0644;
1882 my ($v,$d,$f) = File::Spec->splitpath($x);
1883 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1884 'text-base',"$f.svn-base");
1885 $tb =~ s#^/##;
1886 unless (-f $tb) {
1887 $tb = File::Spec->catfile($d, '.svn',
1888 'text-base',"$f.svn-base");
1889 $tb =~ s#^/##;
1891 my @s = stat($x);
1892 unlink $x or croak $!;
1893 copy($tb, $x);
1894 chmod(($mode &~ umask), $x) or croak $!;
1895 utime $s[8], $s[9], $x;
1897 print $ui $x,"\0";
1899 close $ui or croak $?;
1902 sub index_changes {
1903 return if $_use_lib;
1905 if (!-f "$GIT_SVN_DIR/info/exclude") {
1906 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
1907 print $fd '.svn',"\n";
1908 close $fd or croak $!;
1910 my $no_text_base = shift;
1911 do_update_index([qw/git-diff-files --name-only -z/],
1912 'remove',
1913 $no_text_base);
1914 do_update_index([qw/git-ls-files -z --others/,
1915 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1916 'add',
1917 $no_text_base);
1920 sub s_to_file {
1921 my ($str, $file, $mode) = @_;
1922 open my $fd,'>',$file or croak $!;
1923 print $fd $str,"\n" or croak $!;
1924 close $fd or croak $!;
1925 chmod ($mode &~ umask, $file) if (defined $mode);
1928 sub file_to_s {
1929 my $file = shift;
1930 open my $fd,'<',$file or croak "$!: file: $file\n";
1931 local $/;
1932 my $ret = <$fd>;
1933 close $fd or croak $!;
1934 $ret =~ s/\s*$//s;
1935 return $ret;
1938 sub assert_revision_unknown {
1939 my $r = shift;
1940 if (my $c = revdb_get($REVDB, $r)) {
1941 croak "$r = $c already exists! Why are we refetching it?";
1945 sub trees_eq {
1946 my ($x, $y) = @_;
1947 my @x = safe_qx('git-cat-file','commit',$x);
1948 my @y = safe_qx('git-cat-file','commit',$y);
1949 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1950 || $y[0] !~ /^tree $sha1\n$/) {
1951 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1952 return 0
1954 return 1;
1957 sub git_commit {
1958 my ($log_msg, @parents) = @_;
1959 assert_revision_unknown($log_msg->{revision});
1960 map_tree_joins() if (@_branch_from && !%tree_map);
1962 my (@tmp_parents, @exec_parents, %seen_parent);
1963 if (my $lparents = $log_msg->{parents}) {
1964 @tmp_parents = @$lparents
1966 # commit parents can be conditionally bound to a particular
1967 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1968 foreach my $p (@parents) {
1969 next unless defined $p;
1970 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1971 if ($1 == $log_msg->{revision}) {
1972 push @tmp_parents, $2;
1974 } else {
1975 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1978 my $tree = $log_msg->{tree};
1979 if (!defined $tree) {
1980 my $index = set_index($GIT_SVN_INDEX);
1981 index_changes();
1982 chomp($tree = `git-write-tree`);
1983 croak $? if $?;
1984 restore_index($index);
1987 # just in case we clobber the existing ref, we still want that ref
1988 # as our parent:
1989 if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1990 push @tmp_parents, $cur;
1993 if (exists $tree_map{$tree}) {
1994 foreach my $p (@{$tree_map{$tree}}) {
1995 my $skip;
1996 foreach (@tmp_parents) {
1997 # see if a common parent is found
1998 my $mb = eval {
1999 safe_qx('git-merge-base', $_, $p)
2001 next if ($@ || $?);
2002 $skip = 1;
2003 last;
2005 next if $skip;
2006 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
2007 next if (($SVN_UUID eq $uuid_p) &&
2008 ($log_msg->{revision} > $r_p));
2009 next if (defined $url_p && defined $SVN_URL &&
2010 ($SVN_UUID eq $uuid_p) &&
2011 ($url_p eq $SVN_URL));
2012 push @tmp_parents, $p;
2015 foreach (@tmp_parents) {
2016 next if $seen_parent{$_};
2017 $seen_parent{$_} = 1;
2018 push @exec_parents, $_;
2019 # MAXPARENT is defined to 16 in commit-tree.c:
2020 last if @exec_parents > 16;
2023 set_commit_env($log_msg);
2024 my @exec = ('git-commit-tree', $tree);
2025 push @exec, '-p', $_ foreach @exec_parents;
2026 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2027 or croak $!;
2028 print $msg_fh $log_msg->{msg} or croak $!;
2029 unless ($_no_metadata) {
2030 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
2031 " $SVN_UUID\n" or croak $!;
2033 $msg_fh->flush == 0 or croak $!;
2034 close $msg_fh or croak $!;
2035 chomp(my $commit = do { local $/; <$out_fh> });
2036 close $out_fh or croak $!;
2037 waitpid $pid, 0;
2038 croak $? if $?;
2039 if ($commit !~ /^$sha1$/o) {
2040 die "Failed to commit, invalid sha1: $commit\n";
2042 sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2043 revdb_set($REVDB, $log_msg->{revision}, $commit);
2045 # this output is read via pipe, do not change:
2046 print "r$log_msg->{revision} = $commit\n";
2047 check_repack();
2048 return $commit;
2051 sub check_repack {
2052 if ($_repack && (--$_repack_nr == 0)) {
2053 $_repack_nr = $_repack;
2054 sys("git repack $_repack_flags");
2058 sub set_commit_env {
2059 my ($log_msg) = @_;
2060 my $author = $log_msg->{author};
2061 if (!defined $author || length $author == 0) {
2062 $author = '(no author)';
2064 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
2065 : ($author,"$author\@$SVN_UUID");
2066 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2067 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2068 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2071 sub apply_mod_line_blob {
2072 my $m = shift;
2073 if ($m->{mode_b} =~ /^120/) {
2074 blob_to_symlink($m->{sha1_b}, $m->{file_b});
2075 } else {
2076 blob_to_file($m->{sha1_b}, $m->{file_b});
2080 sub blob_to_symlink {
2081 my ($blob, $link) = @_;
2082 defined $link or croak "\$link not defined!\n";
2083 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2084 if (-l $link || -f _) {
2085 unlink $link or croak $!;
2088 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2089 symlink $dest, $link or croak $!;
2092 sub blob_to_file {
2093 my ($blob, $file) = @_;
2094 defined $file or croak "\$file not defined!\n";
2095 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2096 if (-l $file || -f _) {
2097 unlink $file or croak $!;
2100 open my $blob_fh, '>', $file or croak "$!: $file\n";
2101 my $pid = fork;
2102 defined $pid or croak $!;
2104 if ($pid == 0) {
2105 open STDOUT, '>&', $blob_fh or croak $!;
2106 exec('git-cat-file','blob',$blob) or croak $!;
2108 waitpid $pid, 0;
2109 croak $? if $?;
2111 close $blob_fh or croak $!;
2114 sub safe_qx {
2115 my $pid = open my $child, '-|';
2116 defined $pid or croak $!;
2117 if ($pid == 0) {
2118 exec(@_) or croak $!;
2120 my @ret = (<$child>);
2121 close $child or croak $?;
2122 die $? if $?; # just in case close didn't error out
2123 return wantarray ? @ret : join('',@ret);
2126 sub svn_compat_check {
2127 if ($_follow_parent) {
2128 print STDERR 'E: --follow-parent functionality is only ',
2129 "available when SVN libraries are used\n";
2130 exit 1;
2132 my @co_help = safe_qx(qw(svn co -h));
2133 unless (grep /ignore-externals/,@co_help) {
2134 print STDERR "W: Installed svn version does not support ",
2135 "--ignore-externals\n";
2136 $_no_ignore_ext = 1;
2138 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2139 $_svn_co_url_revs = 1;
2141 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2142 $_svn_pg_peg_revs = 1;
2145 # I really, really hope nobody hits this...
2146 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
2147 print STDERR <<'';
2148 W: The installed svn version does not support the --stop-on-copy flag in
2149 the log command.
2150 Lets hope the directory you're tracking is not a branch or tag
2151 and was never moved within the repository...
2153 $_no_stop_copy = 1;
2157 # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
2158 # (and they won't honor URL@<rev> without -r<rev>, too!)
2159 sub svn_cmd_checkout {
2160 my ($url, $rev, $dir) = @_;
2161 my @cmd = ('svn','co', "-r$rev");
2162 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2163 $url .= "\@$rev" if $_svn_co_url_revs;
2164 sys(@cmd, $url, $dir);
2167 sub check_upgrade_needed {
2168 if (!-r $REVDB) {
2169 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2170 open my $fh, '>>',$REVDB or croak $!;
2171 close $fh;
2173 my $old = eval {
2174 my $pid = open my $child, '-|';
2175 defined $pid or croak $!;
2176 if ($pid == 0) {
2177 close STDERR;
2178 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2180 my @ret = (<$child>);
2181 close $child or croak $?;
2182 die $? if $?; # just in case close didn't error out
2183 return wantarray ? @ret : join('',@ret);
2185 return unless $old;
2186 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
2187 if ($@ || !$head) {
2188 print STDERR "Please run: $0 rebuild --upgrade\n";
2189 exit 1;
2193 # fills %tree_map with a reverse mapping of trees to commits. Useful
2194 # for finding parents to commit on.
2195 sub map_tree_joins {
2196 my %seen;
2197 foreach my $br (@_branch_from) {
2198 my $pid = open my $pipe, '-|';
2199 defined $pid or croak $!;
2200 if ($pid == 0) {
2201 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2202 or croak $!;
2204 while (<$pipe>) {
2205 if (/^commit ($sha1)$/o) {
2206 my $commit = $1;
2208 # if we've seen a commit,
2209 # we've seen its parents
2210 last if $seen{$commit};
2211 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2212 unless (defined $tree) {
2213 die "Failed to parse commit $commit\n";
2215 push @{$tree_map{$tree}}, $commit;
2216 $seen{$commit} = 1;
2219 close $pipe; # we could be breaking the pipe early
2223 sub load_all_refs {
2224 if (@_branch_from) {
2225 print STDERR '--branch|-b parameters are ignored when ',
2226 "--branch-all-refs|-B is passed\n";
2229 # don't worry about rev-list on non-commit objects/tags,
2230 # it shouldn't blow up if a ref is a blob or tree...
2231 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2234 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2235 sub load_authors {
2236 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2237 while (<$authors>) {
2238 chomp;
2239 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2240 my ($user, $name, $email) = ($1, $2, $3);
2241 $users{$user} = [$name, $email];
2243 close $authors or croak $!;
2246 sub rload_authors {
2247 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2248 while (<$authors>) {
2249 chomp;
2250 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2251 my ($user, $name, $email) = ($1, $2, $3);
2252 $rusers{"$name <$email>"} = $user;
2254 close $authors or croak $!;
2257 sub svn_propget_base {
2258 my ($p, $f) = @_;
2259 $f .= '@BASE' if $_svn_pg_peg_revs;
2260 return safe_qx(qw/svn propget/, $p, $f);
2263 sub git_svn_each {
2264 my $sub = shift;
2265 foreach (`git-rev-parse --symbolic --all`) {
2266 next unless s#^refs/remotes/##;
2267 chomp $_;
2268 next unless -f "$GIT_DIR/svn/$_/info/url";
2269 &$sub($_);
2273 sub migrate_revdb {
2274 git_svn_each(sub {
2275 my $id = shift;
2276 defined(my $pid = fork) or croak $!;
2277 if (!$pid) {
2278 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2279 init_vars();
2280 exit 0 if -r $REVDB;
2281 print "Upgrading svn => git mapping...\n";
2282 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2283 open my $fh, '>>',$REVDB or croak $!;
2284 close $fh;
2285 rebuild();
2286 print "Done upgrading. You may now delete the ",
2287 "deprecated $GIT_SVN_DIR/revs directory\n";
2288 exit 0;
2290 waitpid $pid, 0;
2291 croak $? if $?;
2295 sub migration_check {
2296 migrate_revdb() unless (-e $REVDB);
2297 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
2298 print "Upgrading repository...\n";
2299 unless (-d "$GIT_DIR/svn") {
2300 mkdir "$GIT_DIR/svn" or croak $!;
2302 print "Data from a previous version of git-svn exists, but\n\t",
2303 "$GIT_SVN_DIR\n\t(required for this version ",
2304 "($VERSION) of git-svn) does not.\n";
2306 foreach my $x (`git-rev-parse --symbolic --all`) {
2307 next unless $x =~ s#^refs/remotes/##;
2308 chomp $x;
2309 next unless -f "$GIT_DIR/$x/info/url";
2310 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
2311 next unless $u;
2312 my $dn = dirname("$GIT_DIR/svn/$x");
2313 mkpath([$dn]) unless -d $dn;
2314 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
2316 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
2317 print "Done upgrading.\n";
2320 sub find_rev_before {
2321 my ($r, $id, $eq_ok) = @_;
2322 my $f = "$GIT_DIR/svn/$id/.rev_db";
2323 return (undef,undef) unless -r $f;
2324 --$r unless $eq_ok;
2325 while ($r > 0) {
2326 if (my $c = revdb_get($f, $r)) {
2327 return ($r, $c);
2329 --$r;
2331 return (undef, undef);
2334 sub init_vars {
2335 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
2336 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2337 $REVDB = "$GIT_SVN_DIR/.rev_db";
2338 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2339 $SVN_URL = undef;
2340 $SVN_WC = "$GIT_SVN_DIR/tree";
2341 %tree_map = ();
2344 # convert GetOpt::Long specs for use by git-repo-config
2345 sub read_repo_config {
2346 return unless -d $GIT_DIR;
2347 my $opts = shift;
2348 foreach my $o (keys %$opts) {
2349 my $v = $opts->{$o};
2350 my ($key) = ($o =~ /^([a-z\-]+)/);
2351 $key =~ s/-//g;
2352 my $arg = 'git-repo-config';
2353 $arg .= ' --int' if ($o =~ /[:=]i$/);
2354 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2355 if (ref $v eq 'ARRAY') {
2356 chomp(my @tmp = `$arg --get-all svn.$key`);
2357 @$v = @tmp if @tmp;
2358 } else {
2359 chomp(my $tmp = `$arg --get svn.$key`);
2360 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2361 $$v = $tmp;
2367 sub set_default_vals {
2368 if (defined $_repack) {
2369 $_repack = 1000 if ($_repack <= 0);
2370 $_repack_nr = $_repack;
2371 $_repack_flags ||= '-d';
2375 sub read_grafts {
2376 my $gr_file = shift;
2377 my ($grafts, $comments) = ({}, {});
2378 if (open my $fh, '<', $gr_file) {
2379 my @tmp;
2380 while (<$fh>) {
2381 if (/^($sha1)\s+/) {
2382 my $c = $1;
2383 if (@tmp) {
2384 @{$comments->{$c}} = @tmp;
2385 @tmp = ();
2387 foreach my $p (split /\s+/, $_) {
2388 $grafts->{$c}->{$p} = 1;
2390 } else {
2391 push @tmp, $_;
2394 close $fh or croak $!;
2395 @{$comments->{'END'}} = @tmp if @tmp;
2397 return ($grafts, $comments);
2400 sub write_grafts {
2401 my ($grafts, $comments, $gr_file) = @_;
2403 open my $fh, '>', $gr_file or croak $!;
2404 foreach my $c (sort keys %$grafts) {
2405 if ($comments->{$c}) {
2406 print $fh $_ foreach @{$comments->{$c}};
2408 my $p = $grafts->{$c};
2409 my %x; # real parents
2410 delete $p->{$c}; # commits are not self-reproducing...
2411 my $pid = open my $ch, '-|';
2412 defined $pid or croak $!;
2413 if (!$pid) {
2414 exec(qw/git-cat-file commit/, $c) or croak $!;
2416 while (<$ch>) {
2417 if (/^parent ($sha1)/) {
2418 $x{$1} = $p->{$1} = 1;
2419 } else {
2420 last unless /^\S/;
2423 close $ch; # breaking the pipe
2425 # if real parents are the only ones in the grafts, drop it
2426 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2428 my (@ip, @jp, $mb);
2429 my %del = %x;
2430 @ip = @jp = keys %$p;
2431 foreach my $i (@ip) {
2432 next if $del{$i} || $p->{$i} == 2;
2433 foreach my $j (@jp) {
2434 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2435 $mb = eval { safe_qx('git-merge-base',$i,$j) };
2436 next unless $mb;
2437 chomp $mb;
2438 next if $x{$mb};
2439 if ($mb eq $j) {
2440 delete $p->{$i};
2441 $del{$i} = 1;
2442 } elsif ($mb eq $i) {
2443 delete $p->{$j};
2444 $del{$j} = 1;
2449 # if real parents are the only ones in the grafts, drop it
2450 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2452 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2454 if ($comments->{'END'}) {
2455 print $fh $_ foreach @{$comments->{'END'}};
2457 close $fh or croak $!;
2460 sub read_url_paths_all {
2461 my ($l_map, $pfx, $p) = @_;
2462 my @dir;
2463 foreach (<$p/*>) {
2464 if (-r "$_/info/url") {
2465 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2466 my $id = $pfx . basename $_;
2467 my $url = file_to_s("$_/info/url");
2468 my ($u, $p) = repo_path_split($url);
2469 $l_map->{$u}->{$p} = $id;
2470 } elsif (-d $_) {
2471 push @dir, $_;
2474 foreach (@dir) {
2475 my $x = $_;
2476 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2477 read_url_paths_all($l_map, $x, $_);
2481 # this one only gets ids that have been imported, not new ones
2482 sub read_url_paths {
2483 my $l_map = {};
2484 git_svn_each(sub { my $x = shift;
2485 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
2486 my ($u, $p) = repo_path_split($url);
2487 $l_map->{$u}->{$p} = $x;
2489 return $l_map;
2492 sub extract_metadata {
2493 my $id = shift or return (undef, undef, undef);
2494 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
2495 \s([a-f\d\-]+)$/x);
2496 if (!$rev || !$uuid || !$url) {
2497 # some of the original repositories I made had
2498 # identifiers like this:
2499 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2501 return ($url, $rev, $uuid);
2504 sub cmt_metadata {
2505 return extract_metadata((grep(/^git-svn-id: /,
2506 safe_qx(qw/git-cat-file commit/, shift)))[-1]);
2509 sub get_commit_time {
2510 my $cmt = shift;
2511 defined(my $pid = open my $fh, '-|') or croak $!;
2512 if (!$pid) {
2513 exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!;
2515 while (<$fh>) {
2516 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2517 my ($s, $tz) = ($1, $2);
2518 if ($tz =~ s/^\+//) {
2519 $s += tz_to_s_offset($tz);
2520 } elsif ($tz =~ s/^\-//) {
2521 $s -= tz_to_s_offset($tz);
2523 close $fh;
2524 return $s;
2526 die "Can't get commit time for commit: $cmt\n";
2529 sub tz_to_s_offset {
2530 my ($tz) = @_;
2531 $tz =~ s/(\d\d)$//;
2532 return ($1 * 60) + ($tz * 3600);
2535 sub setup_pager { # translated to Perl from pager.c
2536 return unless (-t *STDOUT);
2537 my $pager = $ENV{PAGER};
2538 if (!defined $pager) {
2539 $pager = 'less';
2540 } elsif (length $pager == 0 || $pager eq 'cat') {
2541 return;
2543 pipe my $rfd, my $wfd or return;
2544 defined(my $pid = fork) or croak $!;
2545 if (!$pid) {
2546 open STDOUT, '>&', $wfd or croak $!;
2547 return;
2549 open STDIN, '<&', $rfd or croak $!;
2550 $ENV{LESS} ||= '-S';
2551 exec $pager or croak "Can't run pager: $!\n";;
2554 sub get_author_info {
2555 my ($dest, $author, $t, $tz) = @_;
2556 $author =~ s/(?:^\s*|\s*$)//g;
2557 $dest->{a_raw} = $author;
2558 my $_a;
2559 if ($_authors) {
2560 $_a = $rusers{$author} || undef;
2562 if (!$_a) {
2563 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2565 $dest->{t} = $t;
2566 $dest->{tz} = $tz;
2567 $dest->{a} = $_a;
2568 # Date::Parse isn't in the standard Perl distro :(
2569 if ($tz =~ s/^\+//) {
2570 $t += tz_to_s_offset($tz);
2571 } elsif ($tz =~ s/^\-//) {
2572 $t -= tz_to_s_offset($tz);
2574 $dest->{t_utc} = $t;
2577 sub process_commit {
2578 my ($c, $r_min, $r_max, $defer) = @_;
2579 if (defined $r_min && defined $r_max) {
2580 if ($r_min == $c->{r} && $r_min == $r_max) {
2581 show_commit($c);
2582 return 0;
2584 return 1 if $r_min == $r_max;
2585 if ($r_min < $r_max) {
2586 # we need to reverse the print order
2587 return 0 if (defined $_limit && --$_limit < 0);
2588 push @$defer, $c;
2589 return 1;
2591 if ($r_min != $r_max) {
2592 return 1 if ($r_min < $c->{r});
2593 return 1 if ($r_max > $c->{r});
2596 return 0 if (defined $_limit && --$_limit < 0);
2597 show_commit($c);
2598 return 1;
2601 sub show_commit {
2602 my $c = shift;
2603 if ($_oneline) {
2604 my $x = "\n";
2605 if (my $l = $c->{l}) {
2606 while ($l->[0] =~ /^\s*$/) { shift @$l }
2607 $x = $l->[0];
2609 $_l_fmt ||= 'A' . length($c->{r});
2610 print 'r',pack($_l_fmt, $c->{r}),' | ';
2611 print "$c->{c} | " if $_show_commit;
2612 print $x;
2613 } else {
2614 show_commit_normal($c);
2618 sub show_commit_changed_paths {
2619 my ($c) = @_;
2620 return unless $c->{changed};
2621 print "Changed paths:\n", @{$c->{changed}};
2624 sub show_commit_normal {
2625 my ($c) = @_;
2626 print '-' x72, "\nr$c->{r} | ";
2627 print "$c->{c} | " if $_show_commit;
2628 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2629 localtime($c->{t_utc})), ' | ';
2630 my $nr_line = 0;
2632 if (my $l = $c->{l}) {
2633 while ($l->[$#$l] eq "\n" && $#$l > 0
2634 && $l->[($#$l - 1)] eq "\n") {
2635 pop @$l;
2637 $nr_line = scalar @$l;
2638 if (!$nr_line) {
2639 print "1 line\n\n\n";
2640 } else {
2641 if ($nr_line == 1) {
2642 $nr_line = '1 line';
2643 } else {
2644 $nr_line .= ' lines';
2646 print $nr_line, "\n";
2647 show_commit_changed_paths($c);
2648 print "\n";
2649 print $_ foreach @$l;
2651 } else {
2652 print "1 line\n";
2653 show_commit_changed_paths($c);
2654 print "\n";
2657 foreach my $x (qw/raw diff/) {
2658 if ($c->{$x}) {
2659 print "\n";
2660 print $_ foreach @{$c->{$x}}
2665 sub libsvn_load {
2666 return unless $_use_lib;
2667 $_use_lib = eval {
2668 require SVN::Core;
2669 if ($SVN::Core::VERSION lt '1.1.0') {
2670 die "Need SVN::Core 1.1.0 or better ",
2671 "(got $SVN::Core::VERSION) ",
2672 "Falling back to command-line svn\n";
2674 require SVN::Ra;
2675 require SVN::Delta;
2676 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2677 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2678 $SVN::Node::dir.$SVN::Node::unknown.
2679 $SVN::Node::none.$SVN::Node::file.
2680 $SVN::Node::dir.$SVN::Node::unknown;
2685 sub libsvn_connect {
2686 my ($url) = @_;
2687 my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
2688 SVN::Client::get_ssl_server_trust_file_provider(),
2689 SVN::Client::get_username_provider()]);
2690 my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
2691 return $s;
2694 sub libsvn_get_file {
2695 my ($gui, $f, $rev, $chg) = @_;
2696 my $p = $f;
2697 if (length $SVN_PATH > 0) {
2698 return unless ($p =~ s#^\Q$SVN_PATH\E/##);
2700 print "\t$chg\t$f\n" unless $_q;
2702 my ($hash, $pid, $in, $out);
2703 my $pool = SVN::Pool->new;
2704 defined($pid = open3($in, $out, '>&STDERR',
2705 qw/git-hash-object -w --stdin/)) or croak $!;
2706 # redirect STDOUT for SVN 1.1.x compatibility
2707 open my $stdout, '>&', \*STDOUT or croak $!;
2708 open STDOUT, '>&', $in or croak $!;
2709 my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool);
2710 $in->flush == 0 or croak $!;
2711 open STDOUT, '>&', $stdout or croak $!;
2712 close $in or croak $!;
2713 close $stdout or croak $!;
2714 $pool->clear;
2715 chomp($hash = do { local $/; <$out> });
2716 close $out or croak $!;
2717 waitpid $pid, 0;
2718 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2720 my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
2721 if (exists $props->{'svn:special'}) {
2722 $mode = '120000';
2723 my $link = `git-cat-file blob $hash`;
2724 $link =~ s/^link // or die "svn:special file with contents: <",
2725 $link, "> is not understood\n";
2726 defined($pid = open3($in, $out, '>&STDERR',
2727 qw/git-hash-object -w --stdin/)) or croak $!;
2728 print $in $link;
2729 $in->flush == 0 or croak $!;
2730 close $in or croak $!;
2731 chomp($hash = do { local $/; <$out> });
2732 close $out or croak $!;
2733 waitpid $pid, 0;
2734 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2736 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2739 sub libsvn_log_entry {
2740 my ($rev, $author, $date, $msg, $parents) = @_;
2741 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2742 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2743 or die "Unable to parse date: $date\n";
2744 if (defined $_authors && ! defined $users{$author}) {
2745 die "Author: $author not defined in $_authors file\n";
2747 $msg = '' if ($rev == 0 && !defined $msg);
2748 return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2749 author => $author, msg => $msg."\n", parents => $parents || [] }
2752 sub process_rm {
2753 my ($gui, $last_commit, $f) = @_;
2754 $f =~ s#^\Q$SVN_PATH\E/?## or return;
2755 # remove entire directories.
2756 if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2757 defined(my $pid = open my $ls, '-|') or croak $!;
2758 if (!$pid) {
2759 exec(qw/git-ls-tree -r --name-only -z/,
2760 $last_commit,'--',$f) or croak $!;
2762 local $/ = "\0";
2763 while (<$ls>) {
2764 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2766 close $ls or croak $?;
2767 } else {
2768 print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2772 sub libsvn_fetch {
2773 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2774 open my $gui, '| git-update-index -z --index-info' or croak $!;
2775 my @amr;
2776 foreach my $f (keys %$paths) {
2777 my $m = $paths->{$f}->action();
2778 $f =~ s#^/+##;
2779 if ($m =~ /^[DR]$/) {
2780 print "\t$m\t$f\n" unless $_q;
2781 process_rm($gui, $last_commit, $f);
2782 next if $m eq 'D';
2783 # 'R' can be file replacements, too, right?
2785 my $pool = SVN::Pool->new;
2786 my $t = $SVN->check_path($f, $rev, $pool);
2787 if ($t == $SVN::Node::file) {
2788 if ($m =~ /^[AMR]$/) {
2789 push @amr, [ $m, $f ];
2790 } else {
2791 die "Unrecognized action: $m, ($f r$rev)\n";
2793 } elsif ($t == $SVN::Node::dir && $m =~ /^[AR]$/) {
2794 my @traversed = ();
2795 libsvn_traverse($gui, '', $f, $rev, \@traversed);
2796 foreach (@traversed) {
2797 push @amr, [ $m, $_ ]
2800 $pool->clear;
2802 foreach (@amr) {
2803 libsvn_get_file($gui, $_->[1], $rev, $_->[0]);
2805 close $gui or croak $?;
2806 return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2809 sub svn_grab_base_rev {
2810 defined(my $pid = open my $fh, '-|') or croak $!;
2811 if (!$pid) {
2812 open my $null, '>', '/dev/null' or croak $!;
2813 open STDERR, '>&', $null or croak $!;
2814 exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2815 or croak $!;
2817 chomp(my $c = do { local $/; <$fh> });
2818 close $fh;
2819 if (defined $c && length $c) {
2820 my ($url, $rev, $uuid) = cmt_metadata($c);
2821 return ($rev, $c) if defined $rev;
2823 if ($_no_metadata) {
2824 my $offset = -41; # from tail
2825 my $rl;
2826 open my $fh, '<', $REVDB or
2827 die "--no-metadata specified and $REVDB not readable\n";
2828 seek $fh, $offset, 2;
2829 $rl = readline $fh;
2830 defined $rl or return (undef, undef);
2831 chomp $rl;
2832 while ($c ne $rl && tell $fh != 0) {
2833 $offset -= 41;
2834 seek $fh, $offset, 2;
2835 $rl = readline $fh;
2836 defined $rl or return (undef, undef);
2837 chomp $rl;
2839 my $rev = tell $fh;
2840 croak $! if ($rev < -1);
2841 $rev = ($rev - 41) / 41;
2842 close $fh or croak $!;
2843 return ($rev, $c);
2845 return (undef, undef);
2848 sub libsvn_parse_revision {
2849 my $base = shift;
2850 my $head = $SVN->get_latest_revnum();
2851 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2852 return ($base + 1, $head) if (defined $base);
2853 return (0, $head);
2855 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2856 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2857 if ($_revision =~ /^BASE:(\d+)$/) {
2858 return ($base + 1, $1) if (defined $base);
2859 return (0, $head);
2861 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2862 die "revision argument: $_revision not understood by git-svn\n",
2863 "Try using the command-line svn client instead\n";
2866 sub libsvn_traverse {
2867 my ($gui, $pfx, $path, $rev, $files) = @_;
2868 my $cwd = "$pfx/$path";
2869 my $pool = SVN::Pool->new;
2870 $cwd =~ s#^/+##g;
2871 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2872 foreach my $d (keys %$dirent) {
2873 my $t = $dirent->{$d}->kind;
2874 if ($t == $SVN::Node::dir) {
2875 libsvn_traverse($gui, $cwd, $d, $rev, $files);
2876 } elsif ($t == $SVN::Node::file) {
2877 my $file = "$cwd/$d";
2878 if (defined $files) {
2879 push @$files, $file;
2880 } else {
2881 libsvn_get_file($gui, $file, $rev, 'A');
2885 $pool->clear;
2888 sub libsvn_traverse_ignore {
2889 my ($fh, $path, $r) = @_;
2890 $path =~ s#^/+##g;
2891 my $pool = SVN::Pool->new;
2892 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2893 my $p = $path;
2894 $p =~ s#^\Q$SVN_PATH\E/?##;
2895 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2896 if (my $s = $props->{'svn:ignore'}) {
2897 $s =~ s/[\r\n]+/\n/g;
2898 chomp $s;
2899 if (length $p == 0) {
2900 $s =~ s#\n#\n/$p#g;
2901 print $fh "/$s\n";
2902 } else {
2903 $s =~ s#\n#\n/$p/#g;
2904 print $fh "/$p/$s\n";
2907 foreach (sort keys %$dirent) {
2908 next if $dirent->{$_}->kind != $SVN::Node::dir;
2909 libsvn_traverse_ignore($fh, "$path/$_", $r);
2911 $pool->clear;
2914 sub revisions_eq {
2915 my ($path, $r0, $r1) = @_;
2916 return 1 if $r0 == $r1;
2917 my $nr = 0;
2918 if ($_use_lib) {
2919 # should be OK to use Pool here (r1 - r0) should be small
2920 my $pool = SVN::Pool->new;
2921 libsvn_get_log($SVN, "/$path", $r0, $r1,
2922 0, 1, 1, sub {$nr++}, $pool);
2923 $pool->clear;
2924 } else {
2925 my ($url, undef) = repo_path_split($SVN_URL);
2926 my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1");
2927 while (next_log_entry($svn_log)) { $nr++ }
2928 close $svn_log->{fh};
2930 return 0 if ($nr > 1);
2931 return 1;
2934 sub libsvn_find_parent_branch {
2935 my ($paths, $rev, $author, $date, $msg) = @_;
2936 my $svn_path = '/'.$SVN_PATH;
2938 # look for a parent from another branch:
2939 my $i = $paths->{$svn_path} or return;
2940 my $branch_from = $i->copyfrom_path or return;
2941 my $r = $i->copyfrom_rev;
2942 print STDERR "Found possible branch point: ",
2943 "$branch_from => $svn_path, $r\n";
2944 $branch_from =~ s#^/##;
2945 my $l_map = {};
2946 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2947 my $url = $SVN->{url};
2948 defined $l_map->{$url} or return;
2949 my $id = $l_map->{$url}->{$branch_from};
2950 if (!defined $id && $_follow_parent) {
2951 print STDERR "Following parent: $branch_from\@$r\n";
2952 # auto create a new branch and follow it
2953 $id = basename($branch_from);
2954 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2955 while (-r "$GIT_DIR/svn/$id") {
2956 # just grow a tail if we're not unique enough :x
2957 $id .= '-';
2960 return unless defined $id;
2962 my ($r0, $parent) = find_rev_before($r,$id,1);
2963 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2964 defined(my $pid = fork) or croak $!;
2965 if (!$pid) {
2966 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2967 init_vars();
2968 $SVN_URL = "$url/$branch_from";
2969 $SVN_LOG = $SVN = undef;
2970 setup_git_svn();
2971 # we can't assume SVN_URL exists at r+1:
2972 $_revision = "0:$r";
2973 fetch_lib();
2974 exit 0;
2976 waitpid $pid, 0;
2977 croak $? if $?;
2978 ($r0, $parent) = find_rev_before($r,$id,1);
2980 return unless (defined $r0 && defined $parent);
2981 if (revisions_eq($branch_from, $r0, $r)) {
2982 unlink $GIT_SVN_INDEX;
2983 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2984 sys(qw/git-read-tree/, $parent);
2985 return libsvn_fetch($parent, $paths, $rev,
2986 $author, $date, $msg);
2988 print STDERR "Nope, branch point not imported or unknown\n";
2989 return undef;
2992 sub libsvn_get_log {
2993 my ($ra, @args) = @_;
2994 if ($SVN::Core::VERSION le '1.2.0') {
2995 splice(@args, 3, 1);
2997 $ra->get_log(@args);
3000 sub libsvn_new_tree {
3001 if (my $log_entry = libsvn_find_parent_branch(@_)) {
3002 return $log_entry;
3004 my ($paths, $rev, $author, $date, $msg) = @_;
3005 open my $gui, '| git-update-index -z --index-info' or croak $!;
3006 libsvn_traverse($gui, '', $SVN_PATH, $rev);
3007 close $gui or croak $?;
3008 return libsvn_log_entry($rev, $author, $date, $msg);
3011 sub find_graft_path_commit {
3012 my ($tree_paths, $p1, $r1) = @_;
3013 foreach my $x (keys %$tree_paths) {
3014 next unless ($p1 =~ /^\Q$x\E/);
3015 my $i = $tree_paths->{$x};
3016 my ($r0, $parent) = find_rev_before($r1,$i,1);
3017 return $parent if (defined $r0 && $r0 == $r1);
3018 print STDERR "r$r1 of $i not imported\n";
3019 next;
3021 return undef;
3024 sub find_graft_path_parents {
3025 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
3026 foreach my $x (keys %$tree_paths) {
3027 next unless ($p0 =~ /^\Q$x\E/);
3028 my $i = $tree_paths->{$x};
3029 my ($r, $parent) = find_rev_before($r0, $i, 1);
3030 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
3031 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
3032 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
3033 next if ($url_a && $url_b && $url_a eq $url_b &&
3034 $uuid_b eq $uuid_a);
3035 $grafts->{$c}->{$parent} = 1;
3040 sub libsvn_graft_file_copies {
3041 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
3042 foreach (keys %$paths) {
3043 my $i = $paths->{$_};
3044 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
3045 $i->copyfrom_rev);
3046 next unless (defined $p0 && defined $r0);
3048 my $p1 = $_;
3049 $p1 =~ s#^/##;
3050 $p0 =~ s#^/##;
3051 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
3052 next unless $c;
3053 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
3057 sub set_index {
3058 my $old = $ENV{GIT_INDEX_FILE};
3059 $ENV{GIT_INDEX_FILE} = shift;
3060 return $old;
3063 sub restore_index {
3064 my ($old) = @_;
3065 if (defined $old) {
3066 $ENV{GIT_INDEX_FILE} = $old;
3067 } else {
3068 delete $ENV{GIT_INDEX_FILE};
3072 sub libsvn_commit_cb {
3073 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3074 if ($_optimize_commits && $rev == ($r_last + 1)) {
3075 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
3076 $log->{tree} = get_tree_from_treeish($c);
3077 my $cmt = git_commit($log, $cmt_last, $c);
3078 my @diff = safe_qx('git-diff-tree', $cmt, $c);
3079 if (@diff) {
3080 print STDERR "Trees differ: $cmt $c\n",
3081 join('',@diff),"\n";
3082 exit 1;
3084 } else {
3085 fetch("$rev=$c");
3089 sub libsvn_ls_fullurl {
3090 my $fullurl = shift;
3091 my ($repo, $path) = repo_path_split($fullurl);
3092 $SVN ||= libsvn_connect($repo);
3093 my @ret;
3094 my $pool = SVN::Pool->new;
3095 my ($dirent, undef, undef) = $SVN->get_dir($path,
3096 $SVN->get_latest_revnum, $pool);
3097 foreach my $d (keys %$dirent) {
3098 if ($dirent->{$d}->kind == $SVN::Node::dir) {
3099 push @ret, "$d/"; # add '/' for compat with cli svn
3102 $pool->clear;
3103 return @ret;
3107 sub libsvn_skip_unknown_revs {
3108 my $err = shift;
3109 my $errno = $err->apr_err();
3110 # Maybe the branch we're tracking didn't
3111 # exist when the repo started, so it's
3112 # not an error if it doesn't, just continue
3114 # Wonderfully consistent library, eh?
3115 # 160013 - svn:// and file://
3116 # 175002 - http(s)://
3117 # More codes may be discovered later...
3118 if ($errno == 175002 || $errno == 160013) {
3119 return;
3121 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3124 # Tie::File seems to be prone to offset errors if revisions get sparse,
3125 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3126 # one of my favorite modules is out :< Next up would be one of the DBM
3127 # modules, but I'm not sure which is most portable... So I'll just
3128 # go with something that's plain-text, but still capable of
3129 # being randomly accessed. So here's my ultra-simple fixed-width
3130 # database. All records are 40 characters + "\n", so it's easy to seek
3131 # to a revision: (41 * rev) is the byte offset.
3132 # A record of 40 0s denotes an empty revision.
3133 # And yes, it's still pretty fast (faster than Tie::File).
3134 sub revdb_set {
3135 my ($file, $rev, $commit) = @_;
3136 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
3137 open my $fh, '+<', $file or croak $!;
3138 my $offset = $rev * 41;
3139 # assume that append is the common case:
3140 seek $fh, 0, 2 or croak $!;
3141 my $pos = tell $fh;
3142 if ($pos < $offset) {
3143 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
3145 seek $fh, $offset, 0 or croak $!;
3146 print $fh $commit,"\n";
3147 close $fh or croak $!;
3150 sub revdb_get {
3151 my ($file, $rev) = @_;
3152 my $ret;
3153 my $offset = $rev * 41;
3154 open my $fh, '<', $file or croak $!;
3155 seek $fh, $offset, 0;
3156 if (tell $fh == $offset) {
3157 $ret = readline $fh;
3158 if (defined $ret) {
3159 chomp $ret;
3160 $ret = undef if ($ret =~ /^0{40}$/);
3163 close $fh or croak $!;
3164 return $ret;
3167 sub copy_remote_ref {
3168 my $origin = $_cp_remote ? $_cp_remote : 'origin';
3169 my $ref = "refs/remotes/$GIT_SVN";
3170 if (safe_qx('git-ls-remote', $origin, $ref)) {
3171 sys(qw/git fetch/, $origin, "$ref:$ref");
3172 } elsif ($_cp_remote && !$_upgrade) {
3173 die "Unable to find remote reference: ",
3174 "refs/remotes/$GIT_SVN on $origin\n";
3178 package SVN::Git::Editor;
3179 use vars qw/@ISA/;
3180 use strict;
3181 use warnings;
3182 use Carp qw/croak/;
3183 use IO::File;
3185 sub new {
3186 my $class = shift;
3187 my $git_svn = shift;
3188 my $self = SVN::Delta::Editor->new(@_);
3189 bless $self, $class;
3190 foreach (qw/svn_path c r ra /) {
3191 die "$_ required!\n" unless (defined $git_svn->{$_});
3192 $self->{$_} = $git_svn->{$_};
3194 $self->{pool} = SVN::Pool->new;
3195 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3196 $self->{rm} = { };
3197 require Digest::MD5;
3198 return $self;
3201 sub split_path {
3202 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3205 sub repo_path {
3206 (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
3207 : $_[0]->{svn_path}
3210 sub url_path {
3211 my ($self, $path) = @_;
3212 $self->{ra}->{url} . '/' . $self->repo_path($path);
3215 sub rmdirs {
3216 my ($self, $q) = @_;
3217 my $rm = $self->{rm};
3218 delete $rm->{''}; # we never delete the url we're tracking
3219 return unless %$rm;
3221 foreach (keys %$rm) {
3222 my @d = split m#/#, $_;
3223 my $c = shift @d;
3224 $rm->{$c} = 1;
3225 while (@d) {
3226 $c .= '/' . shift @d;
3227 $rm->{$c} = 1;
3230 delete $rm->{$self->{svn_path}};
3231 delete $rm->{''}; # we never delete the url we're tracking
3232 return unless %$rm;
3234 defined(my $pid = open my $fh,'-|') or croak $!;
3235 if (!$pid) {
3236 exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
3238 local $/ = "\0";
3239 my @svn_path = split m#/#, $self->{svn_path};
3240 while (<$fh>) {
3241 chomp;
3242 my @dn = (@svn_path, (split m#/#, $_));
3243 while (pop @dn) {
3244 delete $rm->{join '/', @dn};
3246 unless (%$rm) {
3247 close $fh;
3248 return;
3251 close $fh;
3253 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3254 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3255 $self->close_directory($bat->{$d}, $p);
3256 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
3257 print "\tD+\t/$d/\n" unless $q;
3258 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3259 delete $bat->{$d};
3263 sub open_or_add_dir {
3264 my ($self, $full_path, $baton) = @_;
3265 my $p = SVN::Pool->new;
3266 my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
3267 $p->clear;
3268 if ($t == $SVN::Node::none) {
3269 return $self->add_directory($full_path, $baton,
3270 undef, -1, $self->{pool});
3271 } elsif ($t == $SVN::Node::dir) {
3272 return $self->open_directory($full_path, $baton,
3273 $self->{r}, $self->{pool});
3275 print STDERR "$full_path already exists in repository at ",
3276 "r$self->{r} and it is not a directory (",
3277 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3278 exit 1;
3281 sub ensure_path {
3282 my ($self, $path) = @_;
3283 my $bat = $self->{bat};
3284 $path = $self->repo_path($path);
3285 return $bat->{''} unless (length $path);
3286 my @p = split m#/+#, $path;
3287 my $c = shift @p;
3288 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3289 while (@p) {
3290 my $c0 = $c;
3291 $c .= '/' . shift @p;
3292 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3294 return $bat->{$c};
3297 sub A {
3298 my ($self, $m, $q) = @_;
3299 my ($dir, $file) = split_path($m->{file_b});
3300 my $pbat = $self->ensure_path($dir);
3301 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3302 undef, -1);
3303 print "\tA\t$m->{file_b}\n" unless $q;
3304 $self->chg_file($fbat, $m);
3305 $self->close_file($fbat,undef,$self->{pool});
3308 sub C {
3309 my ($self, $m, $q) = @_;
3310 my ($dir, $file) = split_path($m->{file_b});
3311 my $pbat = $self->ensure_path($dir);
3312 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3313 $self->url_path($m->{file_a}), $self->{r});
3314 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3315 $self->chg_file($fbat, $m);
3316 $self->close_file($fbat,undef,$self->{pool});
3319 sub delete_entry {
3320 my ($self, $path, $pbat) = @_;
3321 my $rpath = $self->repo_path($path);
3322 my ($dir, $file) = split_path($rpath);
3323 $self->{rm}->{$dir} = 1;
3324 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3327 sub R {
3328 my ($self, $m, $q) = @_;
3329 my ($dir, $file) = split_path($m->{file_b});
3330 my $pbat = $self->ensure_path($dir);
3331 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3332 $self->url_path($m->{file_a}), $self->{r});
3333 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3334 $self->chg_file($fbat, $m);
3335 $self->close_file($fbat,undef,$self->{pool});
3337 ($dir, $file) = split_path($m->{file_a});
3338 $pbat = $self->ensure_path($dir);
3339 $self->delete_entry($m->{file_a}, $pbat);
3342 sub M {
3343 my ($self, $m, $q) = @_;
3344 my ($dir, $file) = split_path($m->{file_b});
3345 my $pbat = $self->ensure_path($dir);
3346 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3347 $pbat,$self->{r},$self->{pool});
3348 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3349 $self->chg_file($fbat, $m);
3350 $self->close_file($fbat,undef,$self->{pool});
3353 sub T { shift->M(@_) }
3355 sub change_file_prop {
3356 my ($self, $fbat, $pname, $pval) = @_;
3357 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3360 sub chg_file {
3361 my ($self, $fbat, $m) = @_;
3362 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3363 $self->change_file_prop($fbat,'svn:executable','*');
3364 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3365 $self->change_file_prop($fbat,'svn:executable',undef);
3367 my $fh = IO::File->new_tmpfile or croak $!;
3368 if ($m->{mode_b} =~ /^120/) {
3369 print $fh 'link ' or croak $!;
3370 $self->change_file_prop($fbat,'svn:special','*');
3371 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3372 $self->change_file_prop($fbat,'svn:special',undef);
3374 defined(my $pid = fork) or croak $!;
3375 if (!$pid) {
3376 open STDOUT, '>&', $fh or croak $!;
3377 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3379 waitpid $pid, 0;
3380 croak $? if $?;
3381 $fh->flush == 0 or croak $!;
3382 seek $fh, 0, 0 or croak $!;
3384 my $md5 = Digest::MD5->new;
3385 $md5->addfile($fh) or croak $!;
3386 seek $fh, 0, 0 or croak $!;
3388 my $exp = $md5->hexdigest;
3389 my $pool = SVN::Pool->new;
3390 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3391 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
3392 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3393 $pool->clear;
3395 close $fh or croak $!;
3398 sub D {
3399 my ($self, $m, $q) = @_;
3400 my ($dir, $file) = split_path($m->{file_b});
3401 my $pbat = $self->ensure_path($dir);
3402 print "\tD\t$m->{file_b}\n" unless $q;
3403 $self->delete_entry($m->{file_b}, $pbat);
3406 sub close_edit {
3407 my ($self) = @_;
3408 my ($p,$bat) = ($self->{pool}, $self->{bat});
3409 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3410 $self->close_directory($bat->{$_}, $p);
3412 $self->SUPER::close_edit($p);
3413 $p->clear;
3416 sub abort_edit {
3417 my ($self) = @_;
3418 $self->SUPER::abort_edit($self->{pool});
3419 $self->{pool}->clear;
3422 __END__
3424 Data structures:
3426 $svn_log hashref (as returned by svn_log_raw)
3428 fh => file handle of the log file,
3429 state => state of the log file parser (sep/msg/rev/msg_start...)
3432 $log_msg hashref as returned by next_log_entry($svn_log)
3434 msg => 'whitespace-formatted log entry
3435 ', # trailing newline is preserved
3436 revision => '8', # integer
3437 date => '2004-02-24T17:01:44.108345Z', # commit date
3438 author => 'committer name'
3442 @mods = array of diff-index line hashes, each element represents one line
3443 of diff-index output
3445 diff-index line ($m hash)
3447 mode_a => first column of diff-index output, no leading ':',
3448 mode_b => second column of diff-index output,
3449 sha1_b => sha1sum of the final blob,
3450 chg => change type [MCRADT],
3451 file_a => original file name of a file (iff chg is 'C' or 'R')
3452 file_b => new/current file name of a file (any chg)
3456 # retval of read_url_paths{,_all}();
3457 $l_map = {
3458 # repository root url
3459 'https://svn.musicpd.org' => {
3460 # repository path # GIT_SVN_ID
3461 'mpd/trunk' => 'trunk',
3462 'mpd/tags/0.11.5' => 'tags/0.11.5',
3466 Notes:
3467 I don't trust the each() function on unless I created %hash myself
3468 because the internal iterator may not have started at base.