git-apply: slightly clean up bitfield usage
[git/dkf.git] / git-svn.perl
blob80b7b87f0f4f1933e551439ca4fb2cd68981a029
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 unless (defined $last_rev) {
593 (undef, $last_rev, undef) = cmt_metadata("$d~1");
594 unless (defined $last_rev) {
595 die "Unable to extract revision information ",
596 "from commit $d~1\n";
599 if ($_dry_run) {
600 print "diff-tree $d~1 $d\n";
601 } else {
602 if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
603 $last_rev = $r;
604 } # else: no changes, same $last_rev
607 return if $_dry_run;
608 fetch();
609 my @diff = safe_qx(qw/git-diff-tree HEAD/, $gs);
610 my @finish;
611 if (@diff) {
612 @finish = qw/rebase/;
613 push @finish, qw/--merge/ if $_merge;
614 push @finish, "--strategy=$_strategy" if $_strategy;
615 print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
616 } else {
617 print "No changes between current HEAD and $gs\n",
618 "Hard resetting to the latest $gs\n";
619 @finish = qw/reset --hard/;
621 sys('git', @finish, $gs);
624 sub show_ignore {
625 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
626 $_use_lib ? show_ignore_lib() : show_ignore_cmd();
629 sub show_ignore_cmd {
630 require File::Find or die $!;
631 if (defined $_revision) {
632 die "-r/--revision option doesn't work unless the Perl SVN ",
633 "libraries are used\n";
635 chdir $SVN_WC or croak $!;
636 my %ign;
637 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
638 s#^\./##;
639 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
640 }}, no_chdir=>1},'.');
642 print "\n# /\n";
643 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
644 delete $ign{'.'};
645 foreach my $i (sort keys %ign) {
646 print "\n# ",$i,"\n";
647 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
651 sub show_ignore_lib {
652 my $repo;
653 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
654 $SVN ||= libsvn_connect($repo);
655 my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
656 libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
659 sub graft_branches {
660 my $gr_file = "$GIT_DIR/info/grafts";
661 my ($grafts, $comments) = read_grafts($gr_file);
662 my $gr_sha1;
664 if (%$grafts) {
665 # temporarily disable our grafts file to make this idempotent
666 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
667 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
670 my $l_map = read_url_paths();
671 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
672 unless ($_no_default_regex) {
673 push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i,
674 qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i,
675 qr/\b(?:from|of)\s+([\w\.\-]+)/i );
677 foreach my $u (keys %$l_map) {
678 if (@re) {
679 foreach my $p (keys %{$l_map->{$u}}) {
680 graft_merge_msg($grafts,$l_map,$u,$p,@re);
683 unless ($_no_graft_copy) {
684 if ($_use_lib) {
685 graft_file_copy_lib($grafts,$l_map,$u);
686 } else {
687 graft_file_copy_cmd($grafts,$l_map,$u);
691 graft_tree_joins($grafts);
693 write_grafts($grafts, $comments, $gr_file);
694 unlink "$gr_file~$gr_sha1" if $gr_sha1;
697 sub multi_init {
698 my $url = shift;
699 $_trunk ||= 'trunk';
700 $_trunk =~ s#/+$##;
701 $url =~ s#/+$## if $url;
702 if ($_trunk !~ m#^[a-z\+]+://#) {
703 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
704 unless ($url) {
705 print STDERR "E: '$_trunk' is not a complete URL ",
706 "and a separate URL is not specified\n";
707 exit 1;
709 $_trunk = $url . $_trunk;
711 my $ch_id;
712 if ($GIT_SVN eq 'git-svn') {
713 $ch_id = 1;
714 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
716 init_vars();
717 unless (-d $GIT_SVN_DIR) {
718 print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
719 init($_trunk);
720 sys('git-repo-config', 'svn.trunk', $_trunk);
722 complete_url_ls_init($url, $_branches, '--branches/-b', '');
723 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
726 sub multi_fetch {
727 # try to do trunk first, since branches/tags
728 # may be descended from it.
729 if (-e "$GIT_DIR/svn/trunk/info/url") {
730 fetch_child_id('trunk', @_);
732 rec_fetch('', "$GIT_DIR/svn", @_);
735 sub show_log {
736 my (@args) = @_;
737 my ($r_min, $r_max);
738 my $r_last = -1; # prevent dupes
739 rload_authors() if $_authors;
740 if (defined $TZ) {
741 $ENV{TZ} = $TZ;
742 } else {
743 delete $ENV{TZ};
745 if (defined $_revision) {
746 if ($_revision =~ /^(\d+):(\d+)$/) {
747 ($r_min, $r_max) = ($1, $2);
748 } elsif ($_revision =~ /^\d+$/) {
749 $r_min = $r_max = $_revision;
750 } else {
751 print STDERR "-r$_revision is not supported, use ",
752 "standard \'git log\' arguments instead\n";
753 exit 1;
757 my $pid = open(my $log,'-|');
758 defined $pid or croak $!;
759 if (!$pid) {
760 exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
762 setup_pager();
763 my (@k, $c, $d);
765 while (<$log>) {
766 if (/^commit ($sha1_short)/o) {
767 my $cmt = $1;
768 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
769 $r_last = $c->{r};
770 process_commit($c, $r_min, $r_max, \@k) or
771 goto out;
773 $d = undef;
774 $c = { c => $cmt };
775 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
776 get_author_info($c, $1, $2, $3);
777 } elsif (/^(?:tree|parent|committer) /) {
778 # ignore
779 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
780 push @{$c->{raw}}, $_;
781 } elsif (/^[ACRMDT]\t/) {
782 # we could add $SVN_PATH here, but that requires
783 # remote access at the moment (repo_path_split)...
784 s#^([ACRMDT])\t# $1 #;
785 push @{$c->{changed}}, $_;
786 } elsif (/^diff /) {
787 $d = 1;
788 push @{$c->{diff}}, $_;
789 } elsif ($d) {
790 push @{$c->{diff}}, $_;
791 } elsif (/^ (git-svn-id:.+)$/) {
792 ($c->{url}, $c->{r}, undef) = extract_metadata($1);
793 } elsif (s/^ //) {
794 push @{$c->{l}}, $_;
797 if ($c && defined $c->{r} && $c->{r} != $r_last) {
798 $r_last = $c->{r};
799 process_commit($c, $r_min, $r_max, \@k);
801 if (@k) {
802 my $swap = $r_max;
803 $r_max = $r_min;
804 $r_min = $swap;
805 process_commit($_, $r_min, $r_max) foreach reverse @k;
807 out:
808 close $log;
809 print '-' x72,"\n" unless $_incremental || $_oneline;
812 sub commit_diff_usage {
813 print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
814 exit 1
817 sub commit_diff {
818 if (!$_use_lib) {
819 print STDERR "commit-diff must be used with SVN libraries\n";
820 exit 1;
822 my $ta = shift or commit_diff_usage();
823 my $tb = shift or commit_diff_usage();
824 if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
825 print STDERR "Needed URL or usable git-svn id command-line\n";
826 commit_diff_usage();
828 my $r = shift || $_revision;
829 die "-r|--revision is a required argument\n" unless (defined $r);
830 if (defined $_message && defined $_file) {
831 print STDERR "Both --message/-m and --file/-F specified ",
832 "for the commit message.\n",
833 "I have no idea what you mean\n";
834 exit 1;
836 if (defined $_file) {
837 $_message = file_to_s($_file);
838 } else {
839 $_message ||= get_commit_message($tb,
840 "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
842 my $repo;
843 ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
844 $SVN_LOG ||= libsvn_connect($repo);
845 $SVN ||= libsvn_connect($repo);
846 if ($r eq 'HEAD') {
847 $r = $SVN->get_latest_revnum;
848 } elsif ($r !~ /^\d+$/) {
849 die "revision argument: $r not understood by git-svn\n";
851 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
852 my $rev_committed;
853 my $ed = SVN::Git::Editor->new({ r => $r,
854 ra => $SVN_LOG, c => $tb,
855 svn_path => $SVN_PATH
857 $SVN->get_commit_editor($_message,
858 sub {
859 $rev_committed = $_[0];
860 print "Committed $_[0]\n";
861 }, @lock)
863 my $mods = libsvn_checkout_tree($ta, $tb, $ed);
864 if (@$mods == 0) {
865 print "No changes\n$ta == $tb\n";
866 $ed->abort_edit;
867 } else {
868 $ed->close_edit;
870 $_message = $_file = undef;
871 return $rev_committed;
874 ########################### utility functions #########################
876 sub cmt_showable {
877 my ($c) = @_;
878 return 1 if defined $c->{r};
879 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
880 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
881 my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
882 shift @msg while ($msg[0] ne "\n");
883 shift @msg;
884 @{$c->{l}} = grep !/^git-svn-id: /, @msg;
886 (undef, $c->{r}, undef) = extract_metadata(
887 (grep(/^git-svn-id: /, @msg))[-1]);
889 return defined $c->{r};
892 sub git_svn_log_cmd {
893 my ($r_min, $r_max) = @_;
894 my @cmd = (qw/git-log --abbrev-commit --pretty=raw
895 --default/, "refs/remotes/$GIT_SVN");
896 push @cmd, '-r' unless $_non_recursive;
897 push @cmd, qw/--raw --name-status/ if $_verbose;
898 return @cmd unless defined $r_max;
899 if ($r_max == $r_min) {
900 push @cmd, '--max-count=1';
901 if (my $c = revdb_get($REVDB, $r_max)) {
902 push @cmd, $c;
904 } else {
905 my ($c_min, $c_max);
906 $c_max = revdb_get($REVDB, $r_max);
907 $c_min = revdb_get($REVDB, $r_min);
908 if (defined $c_min && defined $c_max) {
909 if ($r_max > $r_max) {
910 push @cmd, "$c_min..$c_max";
911 } else {
912 push @cmd, "$c_max..$c_min";
914 } elsif ($r_max > $r_min) {
915 push @cmd, $c_max;
916 } else {
917 push @cmd, $c_min;
920 return @cmd;
923 sub fetch_child_id {
924 my $id = shift;
925 print "Fetching $id\n";
926 my $ref = "$GIT_DIR/refs/remotes/$id";
927 defined(my $pid = open my $fh, '-|') or croak $!;
928 if (!$pid) {
929 $_repack = undef;
930 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
931 init_vars();
932 fetch(@_);
933 exit 0;
935 while (<$fh>) {
936 print $_;
937 check_repack() if (/^r\d+ = $sha1/);
939 close $fh or croak $?;
942 sub rec_fetch {
943 my ($pfx, $p, @args) = @_;
944 my @dir;
945 foreach (sort <$p/*>) {
946 if (-r "$_/info/url") {
947 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
948 my $id = $pfx . basename $_;
949 next if $id eq 'trunk';
950 fetch_child_id($id, @args);
951 } elsif (-d $_) {
952 push @dir, $_;
955 foreach (@dir) {
956 my $x = $_;
957 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
958 rec_fetch($x, $_);
962 sub complete_url_ls_init {
963 my ($url, $var, $switch, $pfx) = @_;
964 unless ($var) {
965 print STDERR "W: $switch not specified\n";
966 return;
968 $var =~ s#/+$##;
969 if ($var !~ m#^[a-z\+]+://#) {
970 $var = '/' . $var if ($var !~ m#^/#);
971 unless ($url) {
972 print STDERR "E: '$var' is not a complete URL ",
973 "and a separate URL is not specified\n";
974 exit 1;
976 $var = $url . $var;
978 chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
979 : safe_qx(qw/svn ls --non-interactive/, $var));
980 my $old = $GIT_SVN;
981 defined(my $pid = fork) or croak $!;
982 if (!$pid) {
983 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
984 $u =~ s#/+$##;
985 if ($u !~ m!\Q$var\E/(.+)$!) {
986 print STDERR "W: Unrecognized URL: $u\n";
987 die "This should never happen\n";
989 # don't try to init already existing refs
990 my $id = $pfx.$1;
991 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
992 init_vars();
993 unless (-d $GIT_SVN_DIR) {
994 print "init $u => $id\n";
995 init($u);
998 exit 0;
1000 waitpid $pid, 0;
1001 croak $? if $?;
1002 my ($n) = ($switch =~ /^--(\w+)/);
1003 sys('git-repo-config', "svn.$n", $var);
1006 sub common_prefix {
1007 my $paths = shift;
1008 my %common;
1009 foreach (@$paths) {
1010 my @tmp = split m#/#, $_;
1011 my $p = '';
1012 while (my $x = shift @tmp) {
1013 $p .= "/$x";
1014 $common{$p} ||= 0;
1015 $common{$p}++;
1018 foreach (sort {length $b <=> length $a} keys %common) {
1019 if ($common{$_} == @$paths) {
1020 return $_;
1023 return '';
1026 # grafts set here are 'stronger' in that they're based on actual tree
1027 # matches, and won't be deleted from merge-base checking in write_grafts()
1028 sub graft_tree_joins {
1029 my $grafts = shift;
1030 map_tree_joins() if (@_branch_from && !%tree_map);
1031 return unless %tree_map;
1033 git_svn_each(sub {
1034 my $i = shift;
1035 defined(my $pid = open my $fh, '-|') or croak $!;
1036 if (!$pid) {
1037 exec qw/git-rev-list --pretty=raw/,
1038 "refs/remotes/$i" or croak $!;
1040 while (<$fh>) {
1041 next unless /^commit ($sha1)$/o;
1042 my $c = $1;
1043 my ($t) = (<$fh> =~ /^tree ($sha1)$/o);
1044 next unless $tree_map{$t};
1046 my $l;
1047 do {
1048 $l = readline $fh;
1049 } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/);
1051 my ($s, $tz) = ($1, $2);
1052 if ($tz =~ s/^\+//) {
1053 $s += tz_to_s_offset($tz);
1054 } elsif ($tz =~ s/^\-//) {
1055 $s -= tz_to_s_offset($tz);
1058 my ($url_a, $r_a, $uuid_a) = cmt_metadata($c);
1060 foreach my $p (@{$tree_map{$t}}) {
1061 next if $p eq $c;
1062 my $mb = eval {
1063 safe_qx('git-merge-base', $c, $p)
1065 next unless ($@ || $?);
1066 if (defined $r_a) {
1067 # see if SVN says it's a relative
1068 my ($url_b, $r_b, $uuid_b) =
1069 cmt_metadata($p);
1070 next if (defined $url_b &&
1071 defined $url_a &&
1072 ($url_a eq $url_b) &&
1073 ($uuid_a eq $uuid_b));
1074 if ($uuid_a eq $uuid_b) {
1075 if ($r_b < $r_a) {
1076 $grafts->{$c}->{$p} = 2;
1077 next;
1078 } elsif ($r_b > $r_a) {
1079 $grafts->{$p}->{$c} = 2;
1080 next;
1084 my $ct = get_commit_time($p);
1085 if ($ct < $s) {
1086 $grafts->{$c}->{$p} = 2;
1087 } elsif ($ct > $s) {
1088 $grafts->{$p}->{$c} = 2;
1090 # what should we do when $ct == $s ?
1093 close $fh or croak $?;
1097 # this isn't funky-filename safe, but good enough for now...
1098 sub graft_file_copy_cmd {
1099 my ($grafts, $l_map, $u) = @_;
1100 my $paths = $l_map->{$u};
1101 my $pfx = common_prefix([keys %$paths]);
1102 $SVN_URL ||= $u.$pfx;
1103 my $pid = open my $fh, '-|';
1104 defined $pid or croak $!;
1105 unless ($pid) {
1106 my @exec = qw/svn log -v/;
1107 push @exec, "-r$_revision" if defined $_revision;
1108 exec @exec, $u.$pfx or croak $!;
1110 my ($r, $mp) = (undef, undef);
1111 while (<$fh>) {
1112 chomp;
1113 if (/^\-{72}$/) {
1114 $mp = $r = undef;
1115 } elsif (/^r(\d+) \| /) {
1116 $r = $1 unless defined $r;
1117 } elsif (/^Changed paths:/) {
1118 $mp = 1;
1119 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
1120 my ($p1, $p0, $r0) = ($1, $2, $3);
1121 my $c = find_graft_path_commit($paths, $p1, $r);
1122 next unless $c;
1123 find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
1128 sub graft_file_copy_lib {
1129 my ($grafts, $l_map, $u) = @_;
1130 my $tree_paths = $l_map->{$u};
1131 my $pfx = common_prefix([keys %$tree_paths]);
1132 my ($repo, $path) = repo_path_split($u.$pfx);
1133 $SVN_LOG ||= libsvn_connect($repo);
1134 $SVN ||= libsvn_connect($repo);
1136 my ($base, $head) = libsvn_parse_revision();
1137 my $inc = 1000;
1138 my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
1139 my $eh = $SVN::Error::handler;
1140 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
1141 while (1) {
1142 my $pool = SVN::Pool->new;
1143 libsvn_get_log($SVN_LOG, "/$path", $min, $max, 0, 1, 1,
1144 sub {
1145 libsvn_graft_file_copies($grafts, $tree_paths,
1146 $path, @_);
1147 }, $pool);
1148 $pool->clear;
1149 last if ($max >= $head);
1150 $min = $max + 1;
1151 $max += $inc;
1152 $max = $head if ($max > $head);
1154 $SVN::Error::handler = $eh;
1157 sub process_merge_msg_matches {
1158 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
1159 my (@strong, @weak);
1160 foreach (@matches) {
1161 # merging with ourselves is not interesting
1162 next if $_ eq $p;
1163 if ($l_map->{$u}->{$_}) {
1164 push @strong, $_;
1165 } else {
1166 push @weak, $_;
1169 foreach my $w (@weak) {
1170 last if @strong;
1171 # no exact match, use branch name as regexp.
1172 my $re = qr/\Q$w\E/i;
1173 foreach (keys %{$l_map->{$u}}) {
1174 if (/$re/) {
1175 push @strong, $l_map->{$u}->{$_};
1176 last;
1179 last if @strong;
1180 $w = basename($w);
1181 $re = qr/\Q$w\E/i;
1182 foreach (keys %{$l_map->{$u}}) {
1183 if (/$re/) {
1184 push @strong, $l_map->{$u}->{$_};
1185 last;
1189 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
1190 \s(?:[a-f\d\-]+)$/xsm);
1191 unless (defined $rev) {
1192 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
1193 \@(?:[a-f\d\-]+)/xsm);
1194 return unless defined $rev;
1196 foreach my $m (@strong) {
1197 my ($r0, $s0) = find_rev_before($rev, $m, 1);
1198 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
1202 sub graft_merge_msg {
1203 my ($grafts, $l_map, $u, $p, @re) = @_;
1205 my $x = $l_map->{$u}->{$p};
1206 my $rl = rev_list_raw($x);
1207 while (my $c = next_rev_list_entry($rl)) {
1208 foreach my $re (@re) {
1209 my (@br) = ($c->{m} =~ /$re/g);
1210 next unless @br;
1211 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
1216 sub read_uuid {
1217 return if $SVN_UUID;
1218 if ($_use_lib) {
1219 my $pool = SVN::Pool->new;
1220 $SVN_UUID = $SVN->get_uuid($pool);
1221 $pool->clear;
1222 } else {
1223 my $info = shift || svn_info('.');
1224 $SVN_UUID = $info->{'Repository UUID'} or
1225 croak "Repository UUID unreadable\n";
1229 sub quiet_run {
1230 my $pid = fork;
1231 defined $pid or croak $!;
1232 if (!$pid) {
1233 open my $null, '>', '/dev/null' or croak $!;
1234 open STDERR, '>&', $null or croak $!;
1235 open STDOUT, '>&', $null or croak $!;
1236 exec @_ or croak $!;
1238 waitpid $pid, 0;
1239 return $?;
1242 sub repo_path_split {
1243 my $full_url = shift;
1244 $full_url =~ s#/+$##;
1246 foreach (@repo_path_split_cache) {
1247 if ($full_url =~ s#$_##) {
1248 my $u = $1;
1249 $full_url =~ s#^/+##;
1250 return ($u, $full_url);
1254 if ($_use_lib) {
1255 my $tmp = libsvn_connect($full_url);
1256 my $url = $tmp->get_repos_root;
1257 $full_url =~ s#^\Q$url\E/*##;
1258 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1259 return ($url, $full_url);
1260 } else {
1261 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
1262 $path =~ s#^/+##;
1263 my @paths = split(m#/+#, $path);
1264 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
1265 my $n = shift @paths || last;
1266 $url .= "/$n";
1268 push @repo_path_split_cache, qr/^(\Q$url\E)/;
1269 $path = join('/',@paths);
1270 return ($url, $path);
1274 sub setup_git_svn {
1275 defined $SVN_URL or croak "SVN repository location required\n";
1276 unless (-d $GIT_DIR) {
1277 croak "GIT_DIR=$GIT_DIR does not exist!\n";
1279 mkpath([$GIT_SVN_DIR]);
1280 mkpath(["$GIT_SVN_DIR/info"]);
1281 open my $fh, '>>',$REVDB or croak $!;
1282 close $fh;
1283 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
1287 sub assert_svn_wc_clean {
1288 return if $_use_lib;
1289 my ($svn_rev) = @_;
1290 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1291 my $lcr = svn_info('.')->{'Last Changed Rev'};
1292 if ($svn_rev != $lcr) {
1293 print STDERR "Checking for copy-tree ... ";
1294 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
1295 "-r$lcr:$svn_rev")));
1296 if (@diff) {
1297 croak "Nope! Expected r$svn_rev, got r$lcr\n";
1298 } else {
1299 print STDERR "OK!\n";
1302 my @status = grep(!/^Performing status on external/,(`svn status`));
1303 @status = grep(!/^\s*$/,@status);
1304 @status = grep(!/^X/,@status) if $_no_ignore_ext;
1305 if (scalar @status) {
1306 print STDERR "Tree ($SVN_WC) is not clean:\n";
1307 print STDERR $_ foreach @status;
1308 croak;
1312 sub get_tree_from_treeish {
1313 my ($treeish) = @_;
1314 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1315 chomp(my $type = `git-cat-file -t $treeish`);
1316 my $expected;
1317 while ($type eq 'tag') {
1318 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1320 if ($type eq 'commit') {
1321 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1322 ($expected) = ($expected =~ /^tree ($sha1)$/);
1323 die "Unable to get tree from $treeish\n" unless $expected;
1324 } elsif ($type eq 'tree') {
1325 $expected = $treeish;
1326 } else {
1327 die "$treeish is a $type, expected tree, tag or commit\n";
1329 return $expected;
1332 sub assert_tree {
1333 return if $_use_lib;
1334 my ($treeish) = @_;
1335 my $expected = get_tree_from_treeish($treeish);
1337 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1338 if (-e $tmpindex) {
1339 unlink $tmpindex or croak $!;
1341 my $old_index = set_index($tmpindex);
1342 index_changes(1);
1343 chomp(my $tree = `git-write-tree`);
1344 restore_index($old_index);
1345 if ($tree ne $expected) {
1346 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
1348 unlink $tmpindex;
1351 sub parse_diff_tree {
1352 my $diff_fh = shift;
1353 local $/ = "\0";
1354 my $state = 'meta';
1355 my @mods;
1356 while (<$diff_fh>) {
1357 chomp $_; # this gets rid of the trailing "\0"
1358 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1359 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
1360 push @mods, { mode_a => $1, mode_b => $2,
1361 sha1_b => $3, chg => $4 };
1362 if ($4 =~ /^(?:C|R)$/) {
1363 $state = 'file_a';
1364 } else {
1365 $state = 'file_b';
1367 } elsif ($state eq 'file_a') {
1368 my $x = $mods[$#mods] or croak "Empty array\n";
1369 if ($x->{chg} !~ /^(?:C|R)$/) {
1370 croak "Error parsing $_, $x->{chg}\n";
1372 $x->{file_a} = $_;
1373 $state = 'file_b';
1374 } elsif ($state eq 'file_b') {
1375 my $x = $mods[$#mods] or croak "Empty array\n";
1376 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1377 croak "Error parsing $_, $x->{chg}\n";
1379 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1380 croak "Error parsing $_, $x->{chg}\n";
1382 $x->{file_b} = $_;
1383 $state = 'meta';
1384 } else {
1385 croak "Error parsing $_\n";
1388 close $diff_fh or croak $?;
1390 return \@mods;
1393 sub svn_check_prop_executable {
1394 my $m = shift;
1395 return if -l $m->{file_b};
1396 if ($m->{mode_b} =~ /755$/) {
1397 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
1398 if ($m->{mode_a} !~ /755$/) {
1399 sys(qw(svn propset svn:executable 1), $m->{file_b});
1401 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
1402 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
1403 sys(qw(svn propdel svn:executable), $m->{file_b});
1404 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
1405 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
1409 sub svn_ensure_parent_path {
1410 my $dir_b = dirname(shift);
1411 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
1412 mkpath([$dir_b]) unless (-d $dir_b);
1413 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
1416 sub precommit_check {
1417 my $mods = shift;
1418 my (%rm_file, %rmdir_check, %added_check);
1420 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
1421 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1422 if ($m->{chg} eq 'R') {
1423 if (-d $m->{file_b}) {
1424 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1426 # dir/$file => dir/file/$file
1427 my $dirname = dirname($m->{file_b});
1428 while ($dirname ne File::Spec->curdir) {
1429 if ($dirname ne $m->{file_a}) {
1430 $dirname = dirname($dirname);
1431 next;
1433 err_file_to_dir("$m->{file_a} => $m->{file_b}");
1435 # baz/zzz => baz (baz is a file)
1436 $dirname = dirname($m->{file_a});
1437 while ($dirname ne File::Spec->curdir) {
1438 if ($dirname ne $m->{file_b}) {
1439 $dirname = dirname($dirname);
1440 next;
1442 err_dir_to_file("$m->{file_a} => $m->{file_b}");
1445 if ($m->{chg} =~ /^(D|R)$/) {
1446 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
1447 $rm_file{ $m->{$t} } = 1;
1448 my $dirname = dirname( $m->{$t} );
1449 my $basename = basename( $m->{$t} );
1450 $rmdir_check{$dirname}->{$basename} = 1;
1451 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
1452 if (-d $m->{file_b}) {
1453 err_dir_to_file($m->{file_b});
1455 my $dirname = dirname( $m->{file_b} );
1456 my $basename = basename( $m->{file_b} );
1457 $added_check{$dirname}->{$basename} = 1;
1458 while ($dirname ne File::Spec->curdir) {
1459 if ($rm_file{$dirname}) {
1460 err_file_to_dir($m->{file_b});
1462 $dirname = dirname $dirname;
1466 return (\%rmdir_check, \%added_check);
1468 sub err_dir_to_file {
1469 my $file = shift;
1470 print STDERR "Node change from directory to file ",
1471 "is not supported by Subversion: ",$file,"\n";
1472 exit 1;
1474 sub err_file_to_dir {
1475 my $file = shift;
1476 print STDERR "Node change from file to directory ",
1477 "is not supported by Subversion: ",$file,"\n";
1478 exit 1;
1483 sub get_diff {
1484 my ($from, $treeish) = @_;
1485 assert_tree($from);
1486 print "diff-tree $from $treeish\n";
1487 my $pid = open my $diff_fh, '-|';
1488 defined $pid or croak $!;
1489 if ($pid == 0) {
1490 my @diff_tree = qw(git-diff-tree -z -r);
1491 if ($_cp_similarity) {
1492 push @diff_tree, "-C$_cp_similarity";
1493 } else {
1494 push @diff_tree, '-C';
1496 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1497 push @diff_tree, "-l$_l" if defined $_l;
1498 exec(@diff_tree, $from, $treeish) or croak $!;
1500 return parse_diff_tree($diff_fh);
1503 sub svn_checkout_tree {
1504 my ($from, $treeish) = @_;
1505 my $mods = get_diff($from->{commit}, $treeish);
1506 return $mods unless (scalar @$mods);
1507 my ($rm, $add) = precommit_check($mods);
1509 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1510 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1511 if ($m->{chg} eq 'C') {
1512 svn_ensure_parent_path( $m->{file_b} );
1513 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
1514 apply_mod_line_blob($m);
1515 svn_check_prop_executable($m);
1516 } elsif ($m->{chg} eq 'D') {
1517 sys(qw(svn rm --force), $m->{file_b});
1518 } elsif ($m->{chg} eq 'R') {
1519 svn_ensure_parent_path( $m->{file_b} );
1520 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
1521 apply_mod_line_blob($m);
1522 svn_check_prop_executable($m);
1523 } elsif ($m->{chg} eq 'M') {
1524 apply_mod_line_blob($m);
1525 svn_check_prop_executable($m);
1526 } elsif ($m->{chg} eq 'T') {
1527 svn_check_prop_executable($m);
1528 apply_mod_line_blob($m);
1529 if ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
1530 sys(qw(svn propdel svn:special), $m->{file_b});
1531 } else {
1532 sys(qw(svn propset svn:special *),$m->{file_b});
1534 } elsif ($m->{chg} eq 'A') {
1535 svn_ensure_parent_path( $m->{file_b} );
1536 apply_mod_line_blob($m);
1537 sys(qw(svn add), $m->{file_b});
1538 svn_check_prop_executable($m);
1539 } else {
1540 croak "Invalid chg: $m->{chg}\n";
1544 assert_tree($treeish);
1545 if ($_rmdir) { # remove empty directories
1546 handle_rmdir($rm, $add);
1548 assert_tree($treeish);
1549 return $mods;
1552 sub libsvn_checkout_tree {
1553 my ($from, $treeish, $ed) = @_;
1554 my $mods = get_diff($from, $treeish);
1555 return $mods unless (scalar @$mods);
1556 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1557 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1558 my $f = $m->{chg};
1559 if (defined $o{$f}) {
1560 $ed->$f($m, $_q);
1561 } else {
1562 croak "Invalid change type: $f\n";
1565 $ed->rmdirs($_q) if $_rmdir;
1566 return $mods;
1569 # svn ls doesn't work with respect to the current working tree, but what's
1570 # in the repository. There's not even an option for it... *sigh*
1571 # (added files don't show up and removed files remain in the ls listing)
1572 sub svn_ls_current {
1573 my ($dir, $rm, $add) = @_;
1574 chomp(my @ls = safe_qx('svn','ls',$dir));
1575 my @ret = ();
1576 foreach (@ls) {
1577 s#/$##; # trailing slashes are evil
1578 push @ret, $_ unless $rm->{$dir}->{$_};
1580 if (exists $add->{$dir}) {
1581 push @ret, keys %{$add->{$dir}};
1583 return \@ret;
1586 sub handle_rmdir {
1587 my ($rm, $add) = @_;
1589 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1590 my $ls = svn_ls_current($dir, $rm, $add);
1591 next if (scalar @$ls);
1592 sys(qw(svn rm --force),$dir);
1594 my $dn = dirname $dir;
1595 $rm->{ $dn }->{ basename $dir } = 1;
1596 $ls = svn_ls_current($dn, $rm, $add);
1597 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1598 sys(qw(svn rm --force),$dn);
1599 $dir = basename $dn;
1600 $dn = dirname $dn;
1601 $rm->{ $dn }->{ $dir } = 1;
1602 $ls = svn_ls_current($dn, $rm, $add);
1607 sub get_commit_message {
1608 my ($commit, $commit_msg) = (@_);
1609 my %log_msg = ( msg => '' );
1610 open my $msg, '>', $commit_msg or croak $!;
1612 chomp(my $type = `git-cat-file -t $commit`);
1613 if ($type eq 'commit' || $type eq 'tag') {
1614 my $pid = open my $msg_fh, '-|';
1615 defined $pid or croak $!;
1617 if ($pid == 0) {
1618 exec('git-cat-file', $type, $commit) or croak $!;
1620 my $in_msg = 0;
1621 while (<$msg_fh>) {
1622 if (!$in_msg) {
1623 $in_msg = 1 if (/^\s*$/);
1624 } elsif (/^git-svn-id: /) {
1625 # skip this, we regenerate the correct one
1626 # on re-fetch anyways
1627 } else {
1628 print $msg $_ or croak $!;
1631 close $msg_fh or croak $?;
1633 close $msg or croak $!;
1635 if ($_edit || ($type eq 'tree')) {
1636 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1637 system($editor, $commit_msg);
1640 # file_to_s removes all trailing newlines, so just use chomp() here:
1641 open $msg, '<', $commit_msg or croak $!;
1642 { local $/; chomp($log_msg{msg} = <$msg>); }
1643 close $msg or croak $!;
1645 return \%log_msg;
1648 sub set_svn_commit_env {
1649 if (defined $LC_ALL) {
1650 $ENV{LC_ALL} = $LC_ALL;
1651 } else {
1652 delete $ENV{LC_ALL};
1656 sub svn_commit_tree {
1657 my ($last, $commit) = @_;
1658 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1659 my $log_msg = get_commit_message($commit, $commit_msg);
1660 my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1661 print "Committing $commit: $oneline\n";
1663 set_svn_commit_env();
1664 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1665 $ENV{LC_ALL} = 'C';
1666 unlink $commit_msg;
1667 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1668 if (!defined $committed) {
1669 my $out = join("\n",@ci_output);
1670 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1671 $out, "\n\nAssuming English locale...";
1672 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1673 defined $committed or die " FAILED!\n",
1674 "Commit output failed to parse committed revision!\n",
1675 print STDERR " OK\n";
1678 my @svn_up = qw(svn up);
1679 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
1680 if ($_optimize_commits && ($committed == ($last->{revision} + 1))) {
1681 push @svn_up, "-r$committed";
1682 sys(@svn_up);
1683 my $info = svn_info('.');
1684 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
1685 if ($info->{'Last Changed Rev'} != $committed) {
1686 croak "$info->{'Last Changed Rev'} != $committed\n"
1688 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1689 /(\d{4})\-(\d\d)\-(\d\d)\s
1690 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1691 or croak "Failed to parse date: $date\n";
1692 $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
1693 $log_msg->{author} = $info->{'Last Changed Author'};
1694 $log_msg->{revision} = $committed;
1695 $log_msg->{msg} .= "\n";
1696 $log_msg->{parents} = [ $last->{commit} ];
1697 $log_msg->{commit} = git_commit($log_msg, $commit);
1698 return $log_msg;
1700 # resync immediately
1701 push @svn_up, "-r$last->{revision}";
1702 sys(@svn_up);
1703 return fetch("$committed=$commit");
1706 sub rev_list_raw {
1707 my (@args) = @_;
1708 my $pid = open my $fh, '-|';
1709 defined $pid or croak $!;
1710 if (!$pid) {
1711 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1713 return { fh => $fh, t => { } };
1716 sub next_rev_list_entry {
1717 my $rl = shift;
1718 my $fh = $rl->{fh};
1719 my $x = $rl->{t};
1720 while (<$fh>) {
1721 if (/^commit ($sha1)$/o) {
1722 if ($x->{c}) {
1723 $rl->{t} = { c => $1 };
1724 return $x;
1725 } else {
1726 $x->{c} = $1;
1728 } elsif (/^parent ($sha1)$/o) {
1729 $x->{p}->{$1} = 1;
1730 } elsif (s/^ //) {
1731 $x->{m} ||= '';
1732 $x->{m} .= $_;
1735 return ($x != $rl->{t}) ? $x : undef;
1738 # read the entire log into a temporary file (which is removed ASAP)
1739 # and store the file handle + parser state
1740 sub svn_log_raw {
1741 my (@log_args) = @_;
1742 my $log_fh = IO::File->new_tmpfile or croak $!;
1743 my $pid = fork;
1744 defined $pid or croak $!;
1745 if (!$pid) {
1746 open STDOUT, '>&', $log_fh or croak $!;
1747 exec (qw(svn log), @log_args) or croak $!
1749 waitpid $pid, 0;
1750 croak $? if $?;
1751 seek $log_fh, 0, 0 or croak $!;
1752 return { state => 'sep', fh => $log_fh };
1755 sub next_log_entry {
1756 my $log = shift; # retval of svn_log_raw()
1757 my $ret = undef;
1758 my $fh = $log->{fh};
1760 while (<$fh>) {
1761 chomp;
1762 if (/^\-{72}$/) {
1763 if ($log->{state} eq 'msg') {
1764 if ($ret->{lines}) {
1765 $ret->{msg} .= $_."\n";
1766 unless(--$ret->{lines}) {
1767 $log->{state} = 'sep';
1769 } else {
1770 croak "Log parse error at: $_\n",
1771 $ret->{revision},
1772 "\n";
1774 next;
1776 if ($log->{state} ne 'sep') {
1777 croak "Log parse error at: $_\n",
1778 "state: $log->{state}\n",
1779 $ret->{revision},
1780 "\n";
1782 $log->{state} = 'rev';
1784 # if we have an empty log message, put something there:
1785 if ($ret) {
1786 $ret->{msg} ||= "\n";
1787 delete $ret->{lines};
1788 return $ret;
1790 next;
1792 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
1793 my $rev = $1;
1794 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1795 ($lines) = ($lines =~ /(\d+)/);
1796 $date = '1970-01-01 00:00:00 +0000'
1797 if ($_ignore_nodate && $date eq '(no date)');
1798 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1799 /(\d{4})\-(\d\d)\-(\d\d)\s
1800 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1801 or croak "Failed to parse date: $date\n";
1802 $ret = { revision => $rev,
1803 date => "$tz $Y-$m-$d $H:$M:$S",
1804 author => $author,
1805 lines => $lines,
1806 msg => '' };
1807 if (defined $_authors && ! defined $users{$author}) {
1808 die "Author: $author not defined in ",
1809 "$_authors file\n";
1811 $log->{state} = 'msg_start';
1812 next;
1814 # skip the first blank line of the message:
1815 if ($log->{state} eq 'msg_start' && /^$/) {
1816 $log->{state} = 'msg';
1817 } elsif ($log->{state} eq 'msg') {
1818 if ($ret->{lines}) {
1819 $ret->{msg} .= $_."\n";
1820 unless (--$ret->{lines}) {
1821 $log->{state} = 'sep';
1823 } else {
1824 croak "Log parse error at: $_\n",
1825 $ret->{revision},"\n";
1829 return $ret;
1832 sub svn_info {
1833 my $url = shift || $SVN_URL;
1835 my $pid = open my $info_fh, '-|';
1836 defined $pid or croak $!;
1838 if ($pid == 0) {
1839 exec(qw(svn info),$url) or croak $!;
1842 my $ret = {};
1843 # only single-lines seem to exist in svn info output
1844 while (<$info_fh>) {
1845 chomp $_;
1846 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
1847 $ret->{$1} = $2;
1848 push @{$ret->{-order}}, $1;
1851 close $info_fh or croak $?;
1852 return $ret;
1855 sub sys { system(@_) == 0 or croak $? }
1857 sub do_update_index {
1858 my ($z_cmd, $cmd, $no_text_base) = @_;
1860 my $z = open my $p, '-|';
1861 defined $z or croak $!;
1862 unless ($z) { exec @$z_cmd or croak $! }
1864 my $pid = open my $ui, '|-';
1865 defined $pid or croak $!;
1866 unless ($pid) {
1867 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1869 local $/ = "\0";
1870 while (my $x = <$p>) {
1871 chomp $x;
1872 if (!$no_text_base && lstat $x && ! -l _ &&
1873 svn_propget_base('svn:keywords', $x)) {
1874 my $mode = -x _ ? 0755 : 0644;
1875 my ($v,$d,$f) = File::Spec->splitpath($x);
1876 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1877 'text-base',"$f.svn-base");
1878 $tb =~ s#^/##;
1879 unless (-f $tb) {
1880 $tb = File::Spec->catfile($d, '.svn',
1881 'text-base',"$f.svn-base");
1882 $tb =~ s#^/##;
1884 my @s = stat($x);
1885 unlink $x or croak $!;
1886 copy($tb, $x);
1887 chmod(($mode &~ umask), $x) or croak $!;
1888 utime $s[8], $s[9], $x;
1890 print $ui $x,"\0";
1892 close $ui or croak $?;
1895 sub index_changes {
1896 return if $_use_lib;
1898 if (!-f "$GIT_SVN_DIR/info/exclude") {
1899 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
1900 print $fd '.svn',"\n";
1901 close $fd or croak $!;
1903 my $no_text_base = shift;
1904 do_update_index([qw/git-diff-files --name-only -z/],
1905 'remove',
1906 $no_text_base);
1907 do_update_index([qw/git-ls-files -z --others/,
1908 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
1909 'add',
1910 $no_text_base);
1913 sub s_to_file {
1914 my ($str, $file, $mode) = @_;
1915 open my $fd,'>',$file or croak $!;
1916 print $fd $str,"\n" or croak $!;
1917 close $fd or croak $!;
1918 chmod ($mode &~ umask, $file) if (defined $mode);
1921 sub file_to_s {
1922 my $file = shift;
1923 open my $fd,'<',$file or croak "$!: file: $file\n";
1924 local $/;
1925 my $ret = <$fd>;
1926 close $fd or croak $!;
1927 $ret =~ s/\s*$//s;
1928 return $ret;
1931 sub assert_revision_unknown {
1932 my $r = shift;
1933 if (my $c = revdb_get($REVDB, $r)) {
1934 croak "$r = $c already exists! Why are we refetching it?";
1938 sub trees_eq {
1939 my ($x, $y) = @_;
1940 my @x = safe_qx('git-cat-file','commit',$x);
1941 my @y = safe_qx('git-cat-file','commit',$y);
1942 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1943 || $y[0] !~ /^tree $sha1\n$/) {
1944 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1945 return 0
1947 return 1;
1950 sub git_commit {
1951 my ($log_msg, @parents) = @_;
1952 assert_revision_unknown($log_msg->{revision});
1953 map_tree_joins() if (@_branch_from && !%tree_map);
1955 my (@tmp_parents, @exec_parents, %seen_parent);
1956 if (my $lparents = $log_msg->{parents}) {
1957 @tmp_parents = @$lparents
1959 # commit parents can be conditionally bound to a particular
1960 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1961 foreach my $p (@parents) {
1962 next unless defined $p;
1963 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1964 if ($1 == $log_msg->{revision}) {
1965 push @tmp_parents, $2;
1967 } else {
1968 push @tmp_parents, $p if $p =~ /$sha1_short/o;
1971 my $tree = $log_msg->{tree};
1972 if (!defined $tree) {
1973 my $index = set_index($GIT_SVN_INDEX);
1974 index_changes();
1975 chomp($tree = `git-write-tree`);
1976 croak $? if $?;
1977 restore_index($index);
1980 # just in case we clobber the existing ref, we still want that ref
1981 # as our parent:
1982 if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1983 push @tmp_parents, $cur;
1986 if (exists $tree_map{$tree}) {
1987 foreach my $p (@{$tree_map{$tree}}) {
1988 my $skip;
1989 foreach (@tmp_parents) {
1990 # see if a common parent is found
1991 my $mb = eval {
1992 safe_qx('git-merge-base', $_, $p)
1994 next if ($@ || $?);
1995 $skip = 1;
1996 last;
1998 next if $skip;
1999 my ($url_p, $r_p, $uuid_p) = cmt_metadata($p);
2000 next if (($SVN_UUID eq $uuid_p) &&
2001 ($log_msg->{revision} > $r_p));
2002 next if (defined $url_p && defined $SVN_URL &&
2003 ($SVN_UUID eq $uuid_p) &&
2004 ($url_p eq $SVN_URL));
2005 push @tmp_parents, $p;
2008 foreach (@tmp_parents) {
2009 next if $seen_parent{$_};
2010 $seen_parent{$_} = 1;
2011 push @exec_parents, $_;
2012 # MAXPARENT is defined to 16 in commit-tree.c:
2013 last if @exec_parents > 16;
2016 set_commit_env($log_msg);
2017 my @exec = ('git-commit-tree', $tree);
2018 push @exec, '-p', $_ foreach @exec_parents;
2019 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2020 or croak $!;
2021 print $msg_fh $log_msg->{msg} or croak $!;
2022 unless ($_no_metadata) {
2023 print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
2024 " $SVN_UUID\n" or croak $!;
2026 $msg_fh->flush == 0 or croak $!;
2027 close $msg_fh or croak $!;
2028 chomp(my $commit = do { local $/; <$out_fh> });
2029 close $out_fh or croak $!;
2030 waitpid $pid, 0;
2031 croak $? if $?;
2032 if ($commit !~ /^$sha1$/o) {
2033 die "Failed to commit, invalid sha1: $commit\n";
2035 sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2036 revdb_set($REVDB, $log_msg->{revision}, $commit);
2038 # this output is read via pipe, do not change:
2039 print "r$log_msg->{revision} = $commit\n";
2040 check_repack();
2041 return $commit;
2044 sub check_repack {
2045 if ($_repack && (--$_repack_nr == 0)) {
2046 $_repack_nr = $_repack;
2047 sys("git repack $_repack_flags");
2051 sub set_commit_env {
2052 my ($log_msg) = @_;
2053 my $author = $log_msg->{author};
2054 if (!defined $author || length $author == 0) {
2055 $author = '(no author)';
2057 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
2058 : ($author,"$author\@$SVN_UUID");
2059 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2060 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2061 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2064 sub apply_mod_line_blob {
2065 my $m = shift;
2066 if ($m->{mode_b} =~ /^120/) {
2067 blob_to_symlink($m->{sha1_b}, $m->{file_b});
2068 } else {
2069 blob_to_file($m->{sha1_b}, $m->{file_b});
2073 sub blob_to_symlink {
2074 my ($blob, $link) = @_;
2075 defined $link or croak "\$link not defined!\n";
2076 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2077 if (-l $link || -f _) {
2078 unlink $link or croak $!;
2081 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
2082 symlink $dest, $link or croak $!;
2085 sub blob_to_file {
2086 my ($blob, $file) = @_;
2087 defined $file or croak "\$file not defined!\n";
2088 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
2089 if (-l $file || -f _) {
2090 unlink $file or croak $!;
2093 open my $blob_fh, '>', $file or croak "$!: $file\n";
2094 my $pid = fork;
2095 defined $pid or croak $!;
2097 if ($pid == 0) {
2098 open STDOUT, '>&', $blob_fh or croak $!;
2099 exec('git-cat-file','blob',$blob) or croak $!;
2101 waitpid $pid, 0;
2102 croak $? if $?;
2104 close $blob_fh or croak $!;
2107 sub safe_qx {
2108 my $pid = open my $child, '-|';
2109 defined $pid or croak $!;
2110 if ($pid == 0) {
2111 exec(@_) or croak $!;
2113 my @ret = (<$child>);
2114 close $child or croak $?;
2115 die $? if $?; # just in case close didn't error out
2116 return wantarray ? @ret : join('',@ret);
2119 sub svn_compat_check {
2120 if ($_follow_parent) {
2121 print STDERR 'E: --follow-parent functionality is only ',
2122 "available when SVN libraries are used\n";
2123 exit 1;
2125 my @co_help = safe_qx(qw(svn co -h));
2126 unless (grep /ignore-externals/,@co_help) {
2127 print STDERR "W: Installed svn version does not support ",
2128 "--ignore-externals\n";
2129 $_no_ignore_ext = 1;
2131 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
2132 $_svn_co_url_revs = 1;
2134 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
2135 $_svn_pg_peg_revs = 1;
2138 # I really, really hope nobody hits this...
2139 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
2140 print STDERR <<'';
2141 W: The installed svn version does not support the --stop-on-copy flag in
2142 the log command.
2143 Lets hope the directory you're tracking is not a branch or tag
2144 and was never moved within the repository...
2146 $_no_stop_copy = 1;
2150 # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
2151 # (and they won't honor URL@<rev> without -r<rev>, too!)
2152 sub svn_cmd_checkout {
2153 my ($url, $rev, $dir) = @_;
2154 my @cmd = ('svn','co', "-r$rev");
2155 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
2156 $url .= "\@$rev" if $_svn_co_url_revs;
2157 sys(@cmd, $url, $dir);
2160 sub check_upgrade_needed {
2161 if (!-r $REVDB) {
2162 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2163 open my $fh, '>>',$REVDB or croak $!;
2164 close $fh;
2166 my $old = eval {
2167 my $pid = open my $child, '-|';
2168 defined $pid or croak $!;
2169 if ($pid == 0) {
2170 close STDERR;
2171 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2173 my @ret = (<$child>);
2174 close $child or croak $?;
2175 die $? if $?; # just in case close didn't error out
2176 return wantarray ? @ret : join('',@ret);
2178 return unless $old;
2179 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
2180 if ($@ || !$head) {
2181 print STDERR "Please run: $0 rebuild --upgrade\n";
2182 exit 1;
2186 # fills %tree_map with a reverse mapping of trees to commits. Useful
2187 # for finding parents to commit on.
2188 sub map_tree_joins {
2189 my %seen;
2190 foreach my $br (@_branch_from) {
2191 my $pid = open my $pipe, '-|';
2192 defined $pid or croak $!;
2193 if ($pid == 0) {
2194 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2195 or croak $!;
2197 while (<$pipe>) {
2198 if (/^commit ($sha1)$/o) {
2199 my $commit = $1;
2201 # if we've seen a commit,
2202 # we've seen its parents
2203 last if $seen{$commit};
2204 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
2205 unless (defined $tree) {
2206 die "Failed to parse commit $commit\n";
2208 push @{$tree_map{$tree}}, $commit;
2209 $seen{$commit} = 1;
2212 close $pipe; # we could be breaking the pipe early
2216 sub load_all_refs {
2217 if (@_branch_from) {
2218 print STDERR '--branch|-b parameters are ignored when ',
2219 "--branch-all-refs|-B is passed\n";
2222 # don't worry about rev-list on non-commit objects/tags,
2223 # it shouldn't blow up if a ref is a blob or tree...
2224 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2227 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
2228 sub load_authors {
2229 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2230 while (<$authors>) {
2231 chomp;
2232 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
2233 my ($user, $name, $email) = ($1, $2, $3);
2234 $users{$user} = [$name, $email];
2236 close $authors or croak $!;
2239 sub rload_authors {
2240 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
2241 while (<$authors>) {
2242 chomp;
2243 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
2244 my ($user, $name, $email) = ($1, $2, $3);
2245 $rusers{"$name <$email>"} = $user;
2247 close $authors or croak $!;
2250 sub svn_propget_base {
2251 my ($p, $f) = @_;
2252 $f .= '@BASE' if $_svn_pg_peg_revs;
2253 return safe_qx(qw/svn propget/, $p, $f);
2256 sub git_svn_each {
2257 my $sub = shift;
2258 foreach (`git-rev-parse --symbolic --all`) {
2259 next unless s#^refs/remotes/##;
2260 chomp $_;
2261 next unless -f "$GIT_DIR/svn/$_/info/url";
2262 &$sub($_);
2266 sub migrate_revdb {
2267 git_svn_each(sub {
2268 my $id = shift;
2269 defined(my $pid = fork) or croak $!;
2270 if (!$pid) {
2271 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2272 init_vars();
2273 exit 0 if -r $REVDB;
2274 print "Upgrading svn => git mapping...\n";
2275 -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
2276 open my $fh, '>>',$REVDB or croak $!;
2277 close $fh;
2278 rebuild();
2279 print "Done upgrading. You may now delete the ",
2280 "deprecated $GIT_SVN_DIR/revs directory\n";
2281 exit 0;
2283 waitpid $pid, 0;
2284 croak $? if $?;
2288 sub migration_check {
2289 migrate_revdb() unless (-e $REVDB);
2290 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
2291 print "Upgrading repository...\n";
2292 unless (-d "$GIT_DIR/svn") {
2293 mkdir "$GIT_DIR/svn" or croak $!;
2295 print "Data from a previous version of git-svn exists, but\n\t",
2296 "$GIT_SVN_DIR\n\t(required for this version ",
2297 "($VERSION) of git-svn) does not.\n";
2299 foreach my $x (`git-rev-parse --symbolic --all`) {
2300 next unless $x =~ s#^refs/remotes/##;
2301 chomp $x;
2302 next unless -f "$GIT_DIR/$x/info/url";
2303 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
2304 next unless $u;
2305 my $dn = dirname("$GIT_DIR/svn/$x");
2306 mkpath([$dn]) unless -d $dn;
2307 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
2309 migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
2310 print "Done upgrading.\n";
2313 sub find_rev_before {
2314 my ($r, $id, $eq_ok) = @_;
2315 my $f = "$GIT_DIR/svn/$id/.rev_db";
2316 return (undef,undef) unless -r $f;
2317 --$r unless $eq_ok;
2318 while ($r > 0) {
2319 if (my $c = revdb_get($f, $r)) {
2320 return ($r, $c);
2322 --$r;
2324 return (undef, undef);
2327 sub init_vars {
2328 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
2329 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
2330 $REVDB = "$GIT_SVN_DIR/.rev_db";
2331 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
2332 $SVN_URL = undef;
2333 $SVN_WC = "$GIT_SVN_DIR/tree";
2334 %tree_map = ();
2337 # convert GetOpt::Long specs for use by git-repo-config
2338 sub read_repo_config {
2339 return unless -d $GIT_DIR;
2340 my $opts = shift;
2341 foreach my $o (keys %$opts) {
2342 my $v = $opts->{$o};
2343 my ($key) = ($o =~ /^([a-z\-]+)/);
2344 $key =~ s/-//g;
2345 my $arg = 'git-repo-config';
2346 $arg .= ' --int' if ($o =~ /[:=]i$/);
2347 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
2348 if (ref $v eq 'ARRAY') {
2349 chomp(my @tmp = `$arg --get-all svn.$key`);
2350 @$v = @tmp if @tmp;
2351 } else {
2352 chomp(my $tmp = `$arg --get svn.$key`);
2353 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
2354 $$v = $tmp;
2360 sub set_default_vals {
2361 if (defined $_repack) {
2362 $_repack = 1000 if ($_repack <= 0);
2363 $_repack_nr = $_repack;
2364 $_repack_flags ||= '-d';
2368 sub read_grafts {
2369 my $gr_file = shift;
2370 my ($grafts, $comments) = ({}, {});
2371 if (open my $fh, '<', $gr_file) {
2372 my @tmp;
2373 while (<$fh>) {
2374 if (/^($sha1)\s+/) {
2375 my $c = $1;
2376 if (@tmp) {
2377 @{$comments->{$c}} = @tmp;
2378 @tmp = ();
2380 foreach my $p (split /\s+/, $_) {
2381 $grafts->{$c}->{$p} = 1;
2383 } else {
2384 push @tmp, $_;
2387 close $fh or croak $!;
2388 @{$comments->{'END'}} = @tmp if @tmp;
2390 return ($grafts, $comments);
2393 sub write_grafts {
2394 my ($grafts, $comments, $gr_file) = @_;
2396 open my $fh, '>', $gr_file or croak $!;
2397 foreach my $c (sort keys %$grafts) {
2398 if ($comments->{$c}) {
2399 print $fh $_ foreach @{$comments->{$c}};
2401 my $p = $grafts->{$c};
2402 my %x; # real parents
2403 delete $p->{$c}; # commits are not self-reproducing...
2404 my $pid = open my $ch, '-|';
2405 defined $pid or croak $!;
2406 if (!$pid) {
2407 exec(qw/git-cat-file commit/, $c) or croak $!;
2409 while (<$ch>) {
2410 if (/^parent ($sha1)/) {
2411 $x{$1} = $p->{$1} = 1;
2412 } else {
2413 last unless /^\S/;
2416 close $ch; # breaking the pipe
2418 # if real parents are the only ones in the grafts, drop it
2419 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2421 my (@ip, @jp, $mb);
2422 my %del = %x;
2423 @ip = @jp = keys %$p;
2424 foreach my $i (@ip) {
2425 next if $del{$i} || $p->{$i} == 2;
2426 foreach my $j (@jp) {
2427 next if $i eq $j || $del{$j} || $p->{$j} == 2;
2428 $mb = eval { safe_qx('git-merge-base',$i,$j) };
2429 next unless $mb;
2430 chomp $mb;
2431 next if $x{$mb};
2432 if ($mb eq $j) {
2433 delete $p->{$i};
2434 $del{$i} = 1;
2435 } elsif ($mb eq $i) {
2436 delete $p->{$j};
2437 $del{$j} = 1;
2442 # if real parents are the only ones in the grafts, drop it
2443 next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
2445 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
2447 if ($comments->{'END'}) {
2448 print $fh $_ foreach @{$comments->{'END'}};
2450 close $fh or croak $!;
2453 sub read_url_paths_all {
2454 my ($l_map, $pfx, $p) = @_;
2455 my @dir;
2456 foreach (<$p/*>) {
2457 if (-r "$_/info/url") {
2458 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2459 my $id = $pfx . basename $_;
2460 my $url = file_to_s("$_/info/url");
2461 my ($u, $p) = repo_path_split($url);
2462 $l_map->{$u}->{$p} = $id;
2463 } elsif (-d $_) {
2464 push @dir, $_;
2467 foreach (@dir) {
2468 my $x = $_;
2469 $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2470 read_url_paths_all($l_map, $x, $_);
2474 # this one only gets ids that have been imported, not new ones
2475 sub read_url_paths {
2476 my $l_map = {};
2477 git_svn_each(sub { my $x = shift;
2478 my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
2479 my ($u, $p) = repo_path_split($url);
2480 $l_map->{$u}->{$p} = $x;
2482 return $l_map;
2485 sub extract_metadata {
2486 my $id = shift or return (undef, undef, undef);
2487 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
2488 \s([a-f\d\-]+)$/x);
2489 if (!$rev || !$uuid || !$url) {
2490 # some of the original repositories I made had
2491 # identifiers like this:
2492 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
2494 return ($url, $rev, $uuid);
2497 sub cmt_metadata {
2498 return extract_metadata((grep(/^git-svn-id: /,
2499 safe_qx(qw/git-cat-file commit/, shift)))[-1]);
2502 sub get_commit_time {
2503 my $cmt = shift;
2504 defined(my $pid = open my $fh, '-|') or croak $!;
2505 if (!$pid) {
2506 exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!;
2508 while (<$fh>) {
2509 /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2510 my ($s, $tz) = ($1, $2);
2511 if ($tz =~ s/^\+//) {
2512 $s += tz_to_s_offset($tz);
2513 } elsif ($tz =~ s/^\-//) {
2514 $s -= tz_to_s_offset($tz);
2516 close $fh;
2517 return $s;
2519 die "Can't get commit time for commit: $cmt\n";
2522 sub tz_to_s_offset {
2523 my ($tz) = @_;
2524 $tz =~ s/(\d\d)$//;
2525 return ($1 * 60) + ($tz * 3600);
2528 sub setup_pager { # translated to Perl from pager.c
2529 return unless (-t *STDOUT);
2530 my $pager = $ENV{PAGER};
2531 if (!defined $pager) {
2532 $pager = 'less';
2533 } elsif (length $pager == 0 || $pager eq 'cat') {
2534 return;
2536 pipe my $rfd, my $wfd or return;
2537 defined(my $pid = fork) or croak $!;
2538 if (!$pid) {
2539 open STDOUT, '>&', $wfd or croak $!;
2540 return;
2542 open STDIN, '<&', $rfd or croak $!;
2543 $ENV{LESS} ||= '-S';
2544 exec $pager or croak "Can't run pager: $!\n";;
2547 sub get_author_info {
2548 my ($dest, $author, $t, $tz) = @_;
2549 $author =~ s/(?:^\s*|\s*$)//g;
2550 $dest->{a_raw} = $author;
2551 my $_a;
2552 if ($_authors) {
2553 $_a = $rusers{$author} || undef;
2555 if (!$_a) {
2556 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
2558 $dest->{t} = $t;
2559 $dest->{tz} = $tz;
2560 $dest->{a} = $_a;
2561 # Date::Parse isn't in the standard Perl distro :(
2562 if ($tz =~ s/^\+//) {
2563 $t += tz_to_s_offset($tz);
2564 } elsif ($tz =~ s/^\-//) {
2565 $t -= tz_to_s_offset($tz);
2567 $dest->{t_utc} = $t;
2570 sub process_commit {
2571 my ($c, $r_min, $r_max, $defer) = @_;
2572 if (defined $r_min && defined $r_max) {
2573 if ($r_min == $c->{r} && $r_min == $r_max) {
2574 show_commit($c);
2575 return 0;
2577 return 1 if $r_min == $r_max;
2578 if ($r_min < $r_max) {
2579 # we need to reverse the print order
2580 return 0 if (defined $_limit && --$_limit < 0);
2581 push @$defer, $c;
2582 return 1;
2584 if ($r_min != $r_max) {
2585 return 1 if ($r_min < $c->{r});
2586 return 1 if ($r_max > $c->{r});
2589 return 0 if (defined $_limit && --$_limit < 0);
2590 show_commit($c);
2591 return 1;
2594 sub show_commit {
2595 my $c = shift;
2596 if ($_oneline) {
2597 my $x = "\n";
2598 if (my $l = $c->{l}) {
2599 while ($l->[0] =~ /^\s*$/) { shift @$l }
2600 $x = $l->[0];
2602 $_l_fmt ||= 'A' . length($c->{r});
2603 print 'r',pack($_l_fmt, $c->{r}),' | ';
2604 print "$c->{c} | " if $_show_commit;
2605 print $x;
2606 } else {
2607 show_commit_normal($c);
2611 sub show_commit_changed_paths {
2612 my ($c) = @_;
2613 return unless $c->{changed};
2614 print "Changed paths:\n", @{$c->{changed}};
2617 sub show_commit_normal {
2618 my ($c) = @_;
2619 print '-' x72, "\nr$c->{r} | ";
2620 print "$c->{c} | " if $_show_commit;
2621 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2622 localtime($c->{t_utc})), ' | ';
2623 my $nr_line = 0;
2625 if (my $l = $c->{l}) {
2626 while ($l->[$#$l] eq "\n" && $#$l > 0
2627 && $l->[($#$l - 1)] eq "\n") {
2628 pop @$l;
2630 $nr_line = scalar @$l;
2631 if (!$nr_line) {
2632 print "1 line\n\n\n";
2633 } else {
2634 if ($nr_line == 1) {
2635 $nr_line = '1 line';
2636 } else {
2637 $nr_line .= ' lines';
2639 print $nr_line, "\n";
2640 show_commit_changed_paths($c);
2641 print "\n";
2642 print $_ foreach @$l;
2644 } else {
2645 print "1 line\n";
2646 show_commit_changed_paths($c);
2647 print "\n";
2650 foreach my $x (qw/raw diff/) {
2651 if ($c->{$x}) {
2652 print "\n";
2653 print $_ foreach @{$c->{$x}}
2658 sub libsvn_load {
2659 return unless $_use_lib;
2660 $_use_lib = eval {
2661 require SVN::Core;
2662 if ($SVN::Core::VERSION lt '1.1.0') {
2663 die "Need SVN::Core 1.1.0 or better ",
2664 "(got $SVN::Core::VERSION) ",
2665 "Falling back to command-line svn\n";
2667 require SVN::Ra;
2668 require SVN::Delta;
2669 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2670 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2671 $SVN::Node::dir.$SVN::Node::unknown.
2672 $SVN::Node::none.$SVN::Node::file.
2673 $SVN::Node::dir.$SVN::Node::unknown;
2678 sub libsvn_connect {
2679 my ($url) = @_;
2680 my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
2681 SVN::Client::get_ssl_server_trust_file_provider(),
2682 SVN::Client::get_username_provider()]);
2683 my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
2684 return $s;
2687 sub libsvn_get_file {
2688 my ($gui, $f, $rev, $chg) = @_;
2689 my $p = $f;
2690 if (length $SVN_PATH > 0) {
2691 return unless ($p =~ s#^\Q$SVN_PATH\E/##);
2693 print "\t$chg\t$f\n" unless $_q;
2695 my ($hash, $pid, $in, $out);
2696 my $pool = SVN::Pool->new;
2697 defined($pid = open3($in, $out, '>&STDERR',
2698 qw/git-hash-object -w --stdin/)) or croak $!;
2699 # redirect STDOUT for SVN 1.1.x compatibility
2700 open my $stdout, '>&', \*STDOUT or croak $!;
2701 open STDOUT, '>&', $in or croak $!;
2702 my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool);
2703 $in->flush == 0 or croak $!;
2704 open STDOUT, '>&', $stdout or croak $!;
2705 close $in or croak $!;
2706 close $stdout or croak $!;
2707 $pool->clear;
2708 chomp($hash = do { local $/; <$out> });
2709 close $out or croak $!;
2710 waitpid $pid, 0;
2711 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2713 my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
2714 if (exists $props->{'svn:special'}) {
2715 $mode = '120000';
2716 my $link = `git-cat-file blob $hash`;
2717 $link =~ s/^link // or die "svn:special file with contents: <",
2718 $link, "> is not understood\n";
2719 defined($pid = open3($in, $out, '>&STDERR',
2720 qw/git-hash-object -w --stdin/)) or croak $!;
2721 print $in $link;
2722 $in->flush == 0 or croak $!;
2723 close $in or croak $!;
2724 chomp($hash = do { local $/; <$out> });
2725 close $out or croak $!;
2726 waitpid $pid, 0;
2727 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2729 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2732 sub libsvn_log_entry {
2733 my ($rev, $author, $date, $msg, $parents) = @_;
2734 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2735 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2736 or die "Unable to parse date: $date\n";
2737 if (defined $_authors && ! defined $users{$author}) {
2738 die "Author: $author not defined in $_authors file\n";
2740 $msg = '' if ($rev == 0 && !defined $msg);
2741 return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2742 author => $author, msg => $msg."\n", parents => $parents || [] }
2745 sub process_rm {
2746 my ($gui, $last_commit, $f) = @_;
2747 $f =~ s#^\Q$SVN_PATH\E/?## or return;
2748 # remove entire directories.
2749 if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2750 defined(my $pid = open my $ls, '-|') or croak $!;
2751 if (!$pid) {
2752 exec(qw/git-ls-tree -r --name-only -z/,
2753 $last_commit,'--',$f) or croak $!;
2755 local $/ = "\0";
2756 while (<$ls>) {
2757 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2759 close $ls or croak $?;
2760 } else {
2761 print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2765 sub libsvn_fetch {
2766 my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2767 open my $gui, '| git-update-index -z --index-info' or croak $!;
2768 my @amr;
2769 foreach my $f (keys %$paths) {
2770 my $m = $paths->{$f}->action();
2771 $f =~ s#^/+##;
2772 if ($m =~ /^[DR]$/) {
2773 print "\t$m\t$f\n" unless $_q;
2774 process_rm($gui, $last_commit, $f);
2775 next if $m eq 'D';
2776 # 'R' can be file replacements, too, right?
2778 my $pool = SVN::Pool->new;
2779 my $t = $SVN->check_path($f, $rev, $pool);
2780 if ($t == $SVN::Node::file) {
2781 if ($m =~ /^[AMR]$/) {
2782 push @amr, [ $m, $f ];
2783 } else {
2784 die "Unrecognized action: $m, ($f r$rev)\n";
2786 } elsif ($t == $SVN::Node::dir && $m =~ /^[AR]$/) {
2787 my @traversed = ();
2788 libsvn_traverse($gui, '', $f, $rev, \@traversed);
2789 foreach (@traversed) {
2790 push @amr, [ $m, $_ ]
2793 $pool->clear;
2795 foreach (@amr) {
2796 libsvn_get_file($gui, $_->[1], $rev, $_->[0]);
2798 close $gui or croak $?;
2799 return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2802 sub svn_grab_base_rev {
2803 defined(my $pid = open my $fh, '-|') or croak $!;
2804 if (!$pid) {
2805 open my $null, '>', '/dev/null' or croak $!;
2806 open STDERR, '>&', $null or croak $!;
2807 exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2808 or croak $!;
2810 chomp(my $c = do { local $/; <$fh> });
2811 close $fh;
2812 if (defined $c && length $c) {
2813 my ($url, $rev, $uuid) = cmt_metadata($c);
2814 return ($rev, $c) if defined $rev;
2816 if ($_no_metadata) {
2817 my $offset = -41; # from tail
2818 my $rl;
2819 open my $fh, '<', $REVDB or
2820 die "--no-metadata specified and $REVDB not readable\n";
2821 seek $fh, $offset, 2;
2822 $rl = readline $fh;
2823 defined $rl or return (undef, undef);
2824 chomp $rl;
2825 while ($c ne $rl && tell $fh != 0) {
2826 $offset -= 41;
2827 seek $fh, $offset, 2;
2828 $rl = readline $fh;
2829 defined $rl or return (undef, undef);
2830 chomp $rl;
2832 my $rev = tell $fh;
2833 croak $! if ($rev < -1);
2834 $rev = ($rev - 41) / 41;
2835 close $fh or croak $!;
2836 return ($rev, $c);
2838 return (undef, undef);
2841 sub libsvn_parse_revision {
2842 my $base = shift;
2843 my $head = $SVN->get_latest_revnum();
2844 if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2845 return ($base + 1, $head) if (defined $base);
2846 return (0, $head);
2848 return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2849 return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2850 if ($_revision =~ /^BASE:(\d+)$/) {
2851 return ($base + 1, $1) if (defined $base);
2852 return (0, $head);
2854 return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2855 die "revision argument: $_revision not understood by git-svn\n",
2856 "Try using the command-line svn client instead\n";
2859 sub libsvn_traverse {
2860 my ($gui, $pfx, $path, $rev, $files) = @_;
2861 my $cwd = "$pfx/$path";
2862 my $pool = SVN::Pool->new;
2863 $cwd =~ s#^/+##g;
2864 my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2865 foreach my $d (keys %$dirent) {
2866 my $t = $dirent->{$d}->kind;
2867 if ($t == $SVN::Node::dir) {
2868 libsvn_traverse($gui, $cwd, $d, $rev, $files);
2869 } elsif ($t == $SVN::Node::file) {
2870 my $file = "$cwd/$d";
2871 if (defined $files) {
2872 push @$files, $file;
2873 } else {
2874 libsvn_get_file($gui, $file, $rev, 'A');
2878 $pool->clear;
2881 sub libsvn_traverse_ignore {
2882 my ($fh, $path, $r) = @_;
2883 $path =~ s#^/+##g;
2884 my $pool = SVN::Pool->new;
2885 my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2886 my $p = $path;
2887 $p =~ s#^\Q$SVN_PATH\E/?##;
2888 print $fh length $p ? "\n# $p\n" : "\n# /\n";
2889 if (my $s = $props->{'svn:ignore'}) {
2890 $s =~ s/[\r\n]+/\n/g;
2891 chomp $s;
2892 if (length $p == 0) {
2893 $s =~ s#\n#\n/$p#g;
2894 print $fh "/$s\n";
2895 } else {
2896 $s =~ s#\n#\n/$p/#g;
2897 print $fh "/$p/$s\n";
2900 foreach (sort keys %$dirent) {
2901 next if $dirent->{$_}->kind != $SVN::Node::dir;
2902 libsvn_traverse_ignore($fh, "$path/$_", $r);
2904 $pool->clear;
2907 sub revisions_eq {
2908 my ($path, $r0, $r1) = @_;
2909 return 1 if $r0 == $r1;
2910 my $nr = 0;
2911 if ($_use_lib) {
2912 # should be OK to use Pool here (r1 - r0) should be small
2913 my $pool = SVN::Pool->new;
2914 libsvn_get_log($SVN, "/$path", $r0, $r1,
2915 0, 1, 1, sub {$nr++}, $pool);
2916 $pool->clear;
2917 } else {
2918 my ($url, undef) = repo_path_split($SVN_URL);
2919 my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1");
2920 while (next_log_entry($svn_log)) { $nr++ }
2921 close $svn_log->{fh};
2923 return 0 if ($nr > 1);
2924 return 1;
2927 sub libsvn_find_parent_branch {
2928 my ($paths, $rev, $author, $date, $msg) = @_;
2929 my $svn_path = '/'.$SVN_PATH;
2931 # look for a parent from another branch:
2932 my $i = $paths->{$svn_path} or return;
2933 my $branch_from = $i->copyfrom_path or return;
2934 my $r = $i->copyfrom_rev;
2935 print STDERR "Found possible branch point: ",
2936 "$branch_from => $svn_path, $r\n";
2937 $branch_from =~ s#^/##;
2938 my $l_map = {};
2939 read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2940 my $url = $SVN->{url};
2941 defined $l_map->{$url} or return;
2942 my $id = $l_map->{$url}->{$branch_from};
2943 if (!defined $id && $_follow_parent) {
2944 print STDERR "Following parent: $branch_from\@$r\n";
2945 # auto create a new branch and follow it
2946 $id = basename($branch_from);
2947 $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2948 while (-r "$GIT_DIR/svn/$id") {
2949 # just grow a tail if we're not unique enough :x
2950 $id .= '-';
2953 return unless defined $id;
2955 my ($r0, $parent) = find_rev_before($r,$id,1);
2956 if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2957 defined(my $pid = fork) or croak $!;
2958 if (!$pid) {
2959 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2960 init_vars();
2961 $SVN_URL = "$url/$branch_from";
2962 $SVN_LOG = $SVN = undef;
2963 setup_git_svn();
2964 # we can't assume SVN_URL exists at r+1:
2965 $_revision = "0:$r";
2966 fetch_lib();
2967 exit 0;
2969 waitpid $pid, 0;
2970 croak $? if $?;
2971 ($r0, $parent) = find_rev_before($r,$id,1);
2973 return unless (defined $r0 && defined $parent);
2974 if (revisions_eq($branch_from, $r0, $r)) {
2975 unlink $GIT_SVN_INDEX;
2976 print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2977 sys(qw/git-read-tree/, $parent);
2978 return libsvn_fetch($parent, $paths, $rev,
2979 $author, $date, $msg);
2981 print STDERR "Nope, branch point not imported or unknown\n";
2982 return undef;
2985 sub libsvn_get_log {
2986 my ($ra, @args) = @_;
2987 if ($SVN::Core::VERSION le '1.2.0') {
2988 splice(@args, 3, 1);
2990 $ra->get_log(@args);
2993 sub libsvn_new_tree {
2994 if (my $log_entry = libsvn_find_parent_branch(@_)) {
2995 return $log_entry;
2997 my ($paths, $rev, $author, $date, $msg) = @_;
2998 open my $gui, '| git-update-index -z --index-info' or croak $!;
2999 libsvn_traverse($gui, '', $SVN_PATH, $rev);
3000 close $gui or croak $?;
3001 return libsvn_log_entry($rev, $author, $date, $msg);
3004 sub find_graft_path_commit {
3005 my ($tree_paths, $p1, $r1) = @_;
3006 foreach my $x (keys %$tree_paths) {
3007 next unless ($p1 =~ /^\Q$x\E/);
3008 my $i = $tree_paths->{$x};
3009 my ($r0, $parent) = find_rev_before($r1,$i,1);
3010 return $parent if (defined $r0 && $r0 == $r1);
3011 print STDERR "r$r1 of $i not imported\n";
3012 next;
3014 return undef;
3017 sub find_graft_path_parents {
3018 my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
3019 foreach my $x (keys %$tree_paths) {
3020 next unless ($p0 =~ /^\Q$x\E/);
3021 my $i = $tree_paths->{$x};
3022 my ($r, $parent) = find_rev_before($r0, $i, 1);
3023 if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) {
3024 my ($url_b, undef, $uuid_b) = cmt_metadata($c);
3025 my ($url_a, undef, $uuid_a) = cmt_metadata($parent);
3026 next if ($url_a && $url_b && $url_a eq $url_b &&
3027 $uuid_b eq $uuid_a);
3028 $grafts->{$c}->{$parent} = 1;
3033 sub libsvn_graft_file_copies {
3034 my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
3035 foreach (keys %$paths) {
3036 my $i = $paths->{$_};
3037 my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
3038 $i->copyfrom_rev);
3039 next unless (defined $p0 && defined $r0);
3041 my $p1 = $_;
3042 $p1 =~ s#^/##;
3043 $p0 =~ s#^/##;
3044 my $c = find_graft_path_commit($tree_paths, $p1, $rev);
3045 next unless $c;
3046 find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
3050 sub set_index {
3051 my $old = $ENV{GIT_INDEX_FILE};
3052 $ENV{GIT_INDEX_FILE} = shift;
3053 return $old;
3056 sub restore_index {
3057 my ($old) = @_;
3058 if (defined $old) {
3059 $ENV{GIT_INDEX_FILE} = $old;
3060 } else {
3061 delete $ENV{GIT_INDEX_FILE};
3065 sub libsvn_commit_cb {
3066 my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
3067 if ($_optimize_commits && $rev == ($r_last + 1)) {
3068 my $log = libsvn_log_entry($rev,$committer,$date,$msg);
3069 $log->{tree} = get_tree_from_treeish($c);
3070 my $cmt = git_commit($log, $cmt_last, $c);
3071 my @diff = safe_qx('git-diff-tree', $cmt, $c);
3072 if (@diff) {
3073 print STDERR "Trees differ: $cmt $c\n",
3074 join('',@diff),"\n";
3075 exit 1;
3077 } else {
3078 fetch("$rev=$c");
3082 sub libsvn_ls_fullurl {
3083 my $fullurl = shift;
3084 my ($repo, $path) = repo_path_split($fullurl);
3085 $SVN ||= libsvn_connect($repo);
3086 my @ret;
3087 my $pool = SVN::Pool->new;
3088 my ($dirent, undef, undef) = $SVN->get_dir($path,
3089 $SVN->get_latest_revnum, $pool);
3090 foreach my $d (keys %$dirent) {
3091 if ($dirent->{$d}->kind == $SVN::Node::dir) {
3092 push @ret, "$d/"; # add '/' for compat with cli svn
3095 $pool->clear;
3096 return @ret;
3100 sub libsvn_skip_unknown_revs {
3101 my $err = shift;
3102 my $errno = $err->apr_err();
3103 # Maybe the branch we're tracking didn't
3104 # exist when the repo started, so it's
3105 # not an error if it doesn't, just continue
3107 # Wonderfully consistent library, eh?
3108 # 160013 - svn:// and file://
3109 # 175002 - http(s)://
3110 # More codes may be discovered later...
3111 if ($errno == 175002 || $errno == 160013) {
3112 return;
3114 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3117 # Tie::File seems to be prone to offset errors if revisions get sparse,
3118 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
3119 # one of my favorite modules is out :< Next up would be one of the DBM
3120 # modules, but I'm not sure which is most portable... So I'll just
3121 # go with something that's plain-text, but still capable of
3122 # being randomly accessed. So here's my ultra-simple fixed-width
3123 # database. All records are 40 characters + "\n", so it's easy to seek
3124 # to a revision: (41 * rev) is the byte offset.
3125 # A record of 40 0s denotes an empty revision.
3126 # And yes, it's still pretty fast (faster than Tie::File).
3127 sub revdb_set {
3128 my ($file, $rev, $commit) = @_;
3129 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
3130 open my $fh, '+<', $file or croak $!;
3131 my $offset = $rev * 41;
3132 # assume that append is the common case:
3133 seek $fh, 0, 2 or croak $!;
3134 my $pos = tell $fh;
3135 if ($pos < $offset) {
3136 print $fh (('0' x 40),"\n") x (($offset - $pos) / 41);
3138 seek $fh, $offset, 0 or croak $!;
3139 print $fh $commit,"\n";
3140 close $fh or croak $!;
3143 sub revdb_get {
3144 my ($file, $rev) = @_;
3145 my $ret;
3146 my $offset = $rev * 41;
3147 open my $fh, '<', $file or croak $!;
3148 seek $fh, $offset, 0;
3149 if (tell $fh == $offset) {
3150 $ret = readline $fh;
3151 if (defined $ret) {
3152 chomp $ret;
3153 $ret = undef if ($ret =~ /^0{40}$/);
3156 close $fh or croak $!;
3157 return $ret;
3160 sub copy_remote_ref {
3161 my $origin = $_cp_remote ? $_cp_remote : 'origin';
3162 my $ref = "refs/remotes/$GIT_SVN";
3163 if (safe_qx('git-ls-remote', $origin, $ref)) {
3164 sys(qw/git fetch/, $origin, "$ref:$ref");
3165 } elsif ($_cp_remote && !$_upgrade) {
3166 die "Unable to find remote reference: ",
3167 "refs/remotes/$GIT_SVN on $origin\n";
3171 package SVN::Git::Editor;
3172 use vars qw/@ISA/;
3173 use strict;
3174 use warnings;
3175 use Carp qw/croak/;
3176 use IO::File;
3178 sub new {
3179 my $class = shift;
3180 my $git_svn = shift;
3181 my $self = SVN::Delta::Editor->new(@_);
3182 bless $self, $class;
3183 foreach (qw/svn_path c r ra /) {
3184 die "$_ required!\n" unless (defined $git_svn->{$_});
3185 $self->{$_} = $git_svn->{$_};
3187 $self->{pool} = SVN::Pool->new;
3188 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3189 $self->{rm} = { };
3190 require Digest::MD5;
3191 return $self;
3194 sub split_path {
3195 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3198 sub repo_path {
3199 (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
3200 : $_[0]->{svn_path}
3203 sub url_path {
3204 my ($self, $path) = @_;
3205 $self->{ra}->{url} . '/' . $self->repo_path($path);
3208 sub rmdirs {
3209 my ($self, $q) = @_;
3210 my $rm = $self->{rm};
3211 delete $rm->{''}; # we never delete the url we're tracking
3212 return unless %$rm;
3214 foreach (keys %$rm) {
3215 my @d = split m#/#, $_;
3216 my $c = shift @d;
3217 $rm->{$c} = 1;
3218 while (@d) {
3219 $c .= '/' . shift @d;
3220 $rm->{$c} = 1;
3223 delete $rm->{$self->{svn_path}};
3224 delete $rm->{''}; # we never delete the url we're tracking
3225 return unless %$rm;
3227 defined(my $pid = open my $fh,'-|') or croak $!;
3228 if (!$pid) {
3229 exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
3231 local $/ = "\0";
3232 my @svn_path = split m#/#, $self->{svn_path};
3233 while (<$fh>) {
3234 chomp;
3235 my @dn = (@svn_path, (split m#/#, $_));
3236 while (pop @dn) {
3237 delete $rm->{join '/', @dn};
3239 unless (%$rm) {
3240 close $fh;
3241 return;
3244 close $fh;
3246 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3247 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3248 $self->close_directory($bat->{$d}, $p);
3249 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
3250 print "\tD+\t/$d/\n" unless $q;
3251 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3252 delete $bat->{$d};
3256 sub open_or_add_dir {
3257 my ($self, $full_path, $baton) = @_;
3258 my $p = SVN::Pool->new;
3259 my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
3260 $p->clear;
3261 if ($t == $SVN::Node::none) {
3262 return $self->add_directory($full_path, $baton,
3263 undef, -1, $self->{pool});
3264 } elsif ($t == $SVN::Node::dir) {
3265 return $self->open_directory($full_path, $baton,
3266 $self->{r}, $self->{pool});
3268 print STDERR "$full_path already exists in repository at ",
3269 "r$self->{r} and it is not a directory (",
3270 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3271 exit 1;
3274 sub ensure_path {
3275 my ($self, $path) = @_;
3276 my $bat = $self->{bat};
3277 $path = $self->repo_path($path);
3278 return $bat->{''} unless (length $path);
3279 my @p = split m#/+#, $path;
3280 my $c = shift @p;
3281 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3282 while (@p) {
3283 my $c0 = $c;
3284 $c .= '/' . shift @p;
3285 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3287 return $bat->{$c};
3290 sub A {
3291 my ($self, $m, $q) = @_;
3292 my ($dir, $file) = split_path($m->{file_b});
3293 my $pbat = $self->ensure_path($dir);
3294 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3295 undef, -1);
3296 print "\tA\t$m->{file_b}\n" unless $q;
3297 $self->chg_file($fbat, $m);
3298 $self->close_file($fbat,undef,$self->{pool});
3301 sub C {
3302 my ($self, $m, $q) = @_;
3303 my ($dir, $file) = split_path($m->{file_b});
3304 my $pbat = $self->ensure_path($dir);
3305 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3306 $self->url_path($m->{file_a}), $self->{r});
3307 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q;
3308 $self->chg_file($fbat, $m);
3309 $self->close_file($fbat,undef,$self->{pool});
3312 sub delete_entry {
3313 my ($self, $path, $pbat) = @_;
3314 my $rpath = $self->repo_path($path);
3315 my ($dir, $file) = split_path($rpath);
3316 $self->{rm}->{$dir} = 1;
3317 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3320 sub R {
3321 my ($self, $m, $q) = @_;
3322 my ($dir, $file) = split_path($m->{file_b});
3323 my $pbat = $self->ensure_path($dir);
3324 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3325 $self->url_path($m->{file_a}), $self->{r});
3326 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q;
3327 $self->chg_file($fbat, $m);
3328 $self->close_file($fbat,undef,$self->{pool});
3330 ($dir, $file) = split_path($m->{file_a});
3331 $pbat = $self->ensure_path($dir);
3332 $self->delete_entry($m->{file_a}, $pbat);
3335 sub M {
3336 my ($self, $m, $q) = @_;
3337 my ($dir, $file) = split_path($m->{file_b});
3338 my $pbat = $self->ensure_path($dir);
3339 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3340 $pbat,$self->{r},$self->{pool});
3341 print "\t$m->{chg}\t$m->{file_b}\n" unless $q;
3342 $self->chg_file($fbat, $m);
3343 $self->close_file($fbat,undef,$self->{pool});
3346 sub T { shift->M(@_) }
3348 sub change_file_prop {
3349 my ($self, $fbat, $pname, $pval) = @_;
3350 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3353 sub chg_file {
3354 my ($self, $fbat, $m) = @_;
3355 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3356 $self->change_file_prop($fbat,'svn:executable','*');
3357 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3358 $self->change_file_prop($fbat,'svn:executable',undef);
3360 my $fh = IO::File->new_tmpfile or croak $!;
3361 if ($m->{mode_b} =~ /^120/) {
3362 print $fh 'link ' or croak $!;
3363 $self->change_file_prop($fbat,'svn:special','*');
3364 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3365 $self->change_file_prop($fbat,'svn:special',undef);
3367 defined(my $pid = fork) or croak $!;
3368 if (!$pid) {
3369 open STDOUT, '>&', $fh or croak $!;
3370 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3372 waitpid $pid, 0;
3373 croak $? if $?;
3374 $fh->flush == 0 or croak $!;
3375 seek $fh, 0, 0 or croak $!;
3377 my $md5 = Digest::MD5->new;
3378 $md5->addfile($fh) or croak $!;
3379 seek $fh, 0, 0 or croak $!;
3381 my $exp = $md5->hexdigest;
3382 my $pool = SVN::Pool->new;
3383 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3384 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
3385 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
3386 $pool->clear;
3388 close $fh or croak $!;
3391 sub D {
3392 my ($self, $m, $q) = @_;
3393 my ($dir, $file) = split_path($m->{file_b});
3394 my $pbat = $self->ensure_path($dir);
3395 print "\tD\t$m->{file_b}\n" unless $q;
3396 $self->delete_entry($m->{file_b}, $pbat);
3399 sub close_edit {
3400 my ($self) = @_;
3401 my ($p,$bat) = ($self->{pool}, $self->{bat});
3402 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
3403 $self->close_directory($bat->{$_}, $p);
3405 $self->SUPER::close_edit($p);
3406 $p->clear;
3409 sub abort_edit {
3410 my ($self) = @_;
3411 $self->SUPER::abort_edit($self->{pool});
3412 $self->{pool}->clear;
3415 __END__
3417 Data structures:
3419 $svn_log hashref (as returned by svn_log_raw)
3421 fh => file handle of the log file,
3422 state => state of the log file parser (sep/msg/rev/msg_start...)
3425 $log_msg hashref as returned by next_log_entry($svn_log)
3427 msg => 'whitespace-formatted log entry
3428 ', # trailing newline is preserved
3429 revision => '8', # integer
3430 date => '2004-02-24T17:01:44.108345Z', # commit date
3431 author => 'committer name'
3435 @mods = array of diff-index line hashes, each element represents one line
3436 of diff-index output
3438 diff-index line ($m hash)
3440 mode_a => first column of diff-index output, no leading ':',
3441 mode_b => second column of diff-index output,
3442 sha1_b => sha1sum of the final blob,
3443 chg => change type [MCRADT],
3444 file_a => original file name of a file (iff chg is 'C' or 'R')
3445 file_b => new/current file name of a file (any chg)
3449 # retval of read_url_paths{,_all}();
3450 $l_map = {
3451 # repository root url
3452 'https://svn.musicpd.org' => {
3453 # repository path # GIT_SVN_ID
3454 'mpd/trunk' => 'trunk',
3455 'mpd/tags/0.11.5' => 'tags/0.11.5',
3459 Notes:
3460 I don't trust the each() function on unless I created %hash myself
3461 because the internal iterator may not have started at base.