2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
6 use vars qw
/ $AUTHOR $VERSION
7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
8 $GIT_SVN_INDEX $GIT_SVN
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
12 $GIT_DIR = $ENV{GIT_DIR
} || "$ENV{PWD}/.git";
13 # make sure the svn binary gives consistent output between locales and TZs:
17 # If SVN:: library support is added, please make the dependencies
18 # optional and preserve the capability to use the command-line client.
19 # use eval { require SVN::... } to make it lazy load
20 # We don't use any modules not in the standard Perl distribution:
23 use File
::Basename qw
/dirname basename/;
24 use File
::Path qw
/mkpath/;
25 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
27 use POSIX qw
/strftime/;
28 my $sha1 = qr/[a-f\d]{40}/;
29 my $sha1_short = qr/[a-f\d]{4,40}/;
30 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
31 $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
32 my (@_branch_from, %tree_map, %users);
35 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
36 'branch|b=s' => \
@_branch_from,
37 'authors-file|A=s' => \
$_authors );
39 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
40 { 'revision|r=s' => \
$_revision, %fc_opts } ],
41 init
=> [ \
&init
, "Initialize and fetch (import)", { } ],
42 commit
=> [ \
&commit
, "Commit git revisions to SVN",
43 { 'stdin|' => \
$_stdin,
46 'find-copies-harder' => \
$_find_copies_harder,
50 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings", { } ],
51 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
52 { 'no-ignore-externals' => \
$_no_ignore_ext,
53 'upgrade' => \
$_upgrade } ],
56 for (my $i = 0; $i < @ARGV; $i++) {
57 if (defined $cmd{$ARGV[$i]}) {
64 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
66 GetOptions
(%opts, 'help|H|h' => \
$_help,
67 'version|V' => \
$_version,
68 'id|i=s' => \
$GIT_SVN) or exit 1;
70 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
71 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
72 $ENV{GIT_DIR
} ||= $GIT_DIR;
74 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
75 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
78 version
() if $_version;
79 usage
(1) unless defined $cmd;
80 load_authors
() if $_authors;
82 $cmd{$cmd}->[0]->(@ARGV);
85 ####################### primary functions ######################
87 my $exit = shift || 0;
88 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
90 git
-svn
- bidirectional operations between a single Subversion tree
and git
91 Usage
: $0 <command
> [options
] [arguments
]\n
93 print $fd "Available commands:\n" unless $cmd;
95 foreach (sort keys %cmd) {
96 next if $cmd && $cmd ne $_;
97 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
98 foreach (keys %{$cmd{$_}->[2]}) {
99 # prints out arguments as they should be passed:
100 my $x = s
#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
101 print $fd ' ' x
17, join(', ', map { length $_ > 1 ?
103 split /\|/,$_)," $x\n";
107 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
108 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
109 one git repository and want to keep them separate. See git-svn(1) for more
116 print "git-svn version $VERSION\n";
121 $SVN_URL = shift or undef;
124 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
126 check_upgrade_needed();
129 my $pid = open(my $rev_list,'-|');
130 defined $pid or croak $!;
132 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
135 while (<$rev_list>) {
138 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
139 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
140 next if (!@commit); # skip merges
141 my $id = $commit[$#commit];
142 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
144 if (!$rev || !$uuid || !$url) {
145 # some of the original repositories I made had
146 # indentifiers like this:
147 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
149 if (!$rev || !$uuid) {
150 croak "Unable to extract revision or UUID from ",
155 # if we merged or otherwise started elsewhere, this is
156 # how we break out of it
157 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
158 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
160 print "r$rev = $c\n";
161 unless (defined $latest) {
162 if (!$SVN_URL && !$url) {
163 croak "SVN repository location required: $url\n";
170 assert_revision_eq_or_unknown($rev, $c);
171 sys('git
-update
-ref',"$GIT_SVN/revs/$rev",$c);
172 $newest_rev = $rev if ($rev > $newest_rev);
174 close $rev_list or croak $?;
175 if (!chdir $SVN_WC) {
176 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
177 chdir $SVN_WC or croak $!;
181 defined $pid or croak $!;
183 my @svn_up = qw(svn up);
184 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
185 sys
(@svn_up,"-r$newest_rev");
186 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
188 exec('git-write-tree');
194 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
195 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
201 $SVN_URL = shift or croak
"SVN repository location required\n";
202 unless (-d
$GIT_DIR) {
210 check_upgrade_needed
();
211 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
212 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
213 unless ($_revision) {
214 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
216 push @log_args, "-r$_revision";
217 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
219 my $svn_log = svn_log_raw
(@log_args);
221 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
222 my $last_commit = undef;
223 unless (-d
$SVN_WC) {
224 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
225 chdir $SVN_WC or croak
$!;
227 $last_commit = git_commit
($base, @parents);
228 assert_svn_wc_clean
($base->{revision
}, $last_commit);
230 chdir $SVN_WC or croak
$!;
232 $last_commit = file_to_s
("$REV_DIR/$base->{revision}");
234 my @svn_up = qw(svn up);
235 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
237 while (my $log_msg = next_log_entry
($svn_log)) {
238 assert_svn_wc_clean
($last->{revision
}, $last_commit);
239 if ($last->{revision
} >= $log_msg->{revision
}) {
240 croak
"Out of order: last >= current: ",
241 "$last->{revision} >= $log_msg->{revision}\n";
243 sys
(@svn_up,"-r$log_msg->{revision}");
244 $last_commit = git_commit
($log_msg, $last_commit, @parents);
247 assert_svn_wc_clean
($last->{revision
}, $last_commit);
248 unless (-e
"$GIT_DIR/refs/heads/master") {
249 sys
(qw(git-update-ref refs/heads/master),$last_commit);
256 check_upgrade_needed
();
257 if ($_stdin || !@commits) {
258 print "Reading from stdin...\n";
261 if (/\b($sha1_short)\b/o) {
262 unshift @commits, $1;
267 foreach my $c (@commits) {
268 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
269 if (scalar @tmp == 1) {
271 } elsif (scalar @tmp > 1) {
272 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
274 die "Failed to rev-parse $c\n";
280 chdir $SVN_WC or croak
$!;
281 my $info = svn_info
('.');
283 my $svn_current_rev = $info->{'Last Changed Rev'};
284 foreach my $c (@revs) {
285 my $mods = svn_checkout_tree
($svn_current_rev, $c);
286 if (scalar @
$mods == 0) {
287 print "Skipping, no changes detected\n";
290 $svn_current_rev = svn_commit_tree
($svn_current_rev, $c);
292 print "Done committing ",scalar @revs," revisions to SVN\n";
297 require File
::Find
or die $!;
298 my $exclude_file = "$GIT_DIR/info/exclude";
299 open my $fh, '<', $exclude_file or croak
$!;
300 chomp(my @excludes = (<$fh>));
301 close $fh or croak
$!;
303 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
304 chdir $SVN_WC or croak
$!;
306 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
308 @
{$ign{$_}} = safe_qx
(qw(svn propget svn:ignore),$_);
309 }}, no_chdir
=>1},'.');
312 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
314 foreach my $i (sort keys %ign) {
315 print "\n# ",$i,"\n";
316 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
320 ########################### utility functions #########################
324 my $info = shift || svn_info
('.');
325 $SVN_UUID = $info->{'Repository UUID'} or
326 croak
"Repository UUID unreadable\n";
327 s_to_file
($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
331 defined $SVN_URL or croak
"SVN repository location required\n";
332 unless (-d
$GIT_DIR) {
333 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
335 mkpath
(["$GIT_DIR/$GIT_SVN"]);
336 mkpath
(["$GIT_DIR/$GIT_SVN/info"]);
338 s_to_file
($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
340 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak
$!;
341 print $fd '.svn',"\n";
342 close $fd or croak
$!;
345 sub assert_svn_wc_clean
{
346 my ($svn_rev, $treeish) = @_;
347 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
348 croak
"$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
349 my $lcr = svn_info
('.')->{'Last Changed Rev'};
350 if ($svn_rev != $lcr) {
351 print STDERR
"Checking for copy-tree ... ";
353 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
354 "-r$lcr:$svn_rev")));
356 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
358 print STDERR
"OK!\n";
361 my @status = grep(!/^Performing status on external/,(`svn status`));
362 @status = grep(!/^\s*$/,@status);
363 if (scalar @status) {
364 print STDERR
"Tree ($SVN_WC) is not clean:\n";
365 print STDERR
$_ foreach @status;
368 assert_tree
($treeish);
373 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
374 chomp(my $type = `git-cat-file -t $treeish`);
376 while ($type eq 'tag') {
377 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
379 if ($type eq 'commit') {
380 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
381 ($expected) = ($expected =~ /^tree ($sha1)$/);
382 die "Unable to get tree from $treeish\n" unless $expected;
383 } elsif ($type eq 'tree') {
384 $expected = $treeish;
386 die "$treeish is a $type, expected tree, tag or commit\n";
389 my $old_index = $ENV{GIT_INDEX_FILE
};
390 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
392 unlink $tmpindex or croak
$!;
394 $ENV{GIT_INDEX_FILE
} = $tmpindex;
396 chomp(my $tree = `git-write-tree`);
398 $ENV{GIT_INDEX_FILE
} = $old_index;
400 delete $ENV{GIT_INDEX_FILE
};
402 if ($tree ne $expected) {
403 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
407 sub parse_diff_tree
{
413 chomp $_; # this gets rid of the trailing "\0"
414 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
415 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
416 push @mods, { mode_a
=> $1, mode_b
=> $2,
417 sha1_b
=> $3, chg
=> $4 };
418 if ($4 =~ /^(?:C|R)$/) {
423 } elsif ($state eq 'file_a') {
424 my $x = $mods[$#mods] or croak
"Empty array\n";
425 if ($x->{chg
} !~ /^(?:C|R)$/) {
426 croak
"Error parsing $_, $x->{chg}\n";
430 } elsif ($state eq 'file_b') {
431 my $x = $mods[$#mods] or croak
"Empty array\n";
432 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
433 croak
"Error parsing $_, $x->{chg}\n";
435 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
436 croak
"Error parsing $_, $x->{chg}\n";
441 croak
"Error parsing $_\n";
444 close $diff_fh or croak
$!;
449 sub svn_check_prop_executable
{
451 return if -l
$m->{file_b
};
452 if ($m->{mode_b
} =~ /755$/) {
453 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
454 if ($m->{mode_a
} !~ /755$/) {
455 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
457 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
458 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
459 sys
(qw(svn propdel svn:executable), $m->{file_b
});
460 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
461 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
465 sub svn_ensure_parent_path
{
466 my $dir_b = dirname
(shift);
467 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
468 mkpath
([$dir_b]) unless (-d
$dir_b);
469 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
472 sub precommit_check
{
474 my (%rm_file, %rmdir_check, %added_check);
476 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
477 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
478 if ($m->{chg
} eq 'R') {
479 if (-d
$m->{file_b
}) {
480 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
482 # dir/$file => dir/file/$file
483 my $dirname = dirname
($m->{file_b
});
484 while ($dirname ne File
::Spec
->curdir) {
485 if ($dirname ne $m->{file_a
}) {
486 $dirname = dirname
($dirname);
489 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
491 # baz/zzz => baz (baz is a file)
492 $dirname = dirname
($m->{file_a
});
493 while ($dirname ne File
::Spec
->curdir) {
494 if ($dirname ne $m->{file_b
}) {
495 $dirname = dirname
($dirname);
498 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
501 if ($m->{chg
} =~ /^(D|R)$/) {
502 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
503 $rm_file{ $m->{$t} } = 1;
504 my $dirname = dirname
( $m->{$t} );
505 my $basename = basename
( $m->{$t} );
506 $rmdir_check{$dirname}->{$basename} = 1;
507 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
508 if (-d
$m->{file_b
}) {
509 err_dir_to_file
($m->{file_b
});
511 my $dirname = dirname
( $m->{file_b
} );
512 my $basename = basename
( $m->{file_b
} );
513 $added_check{$dirname}->{$basename} = 1;
514 while ($dirname ne File
::Spec
->curdir) {
515 if ($rm_file{$dirname}) {
516 err_file_to_dir
($m->{file_b
});
518 $dirname = dirname
$dirname;
522 return (\
%rmdir_check, \
%added_check);
524 sub err_dir_to_file
{
526 print STDERR
"Node change from directory to file ",
527 "is not supported by Subversion: ",$file,"\n";
530 sub err_file_to_dir
{
532 print STDERR
"Node change from file to directory ",
533 "is not supported by Subversion: ",$file,"\n";
538 sub svn_checkout_tree
{
539 my ($svn_rev, $treeish) = @_;
540 my $from = file_to_s
("$REV_DIR/$svn_rev");
541 assert_svn_wc_clean
($svn_rev,$from);
542 print "diff-tree $from $treeish\n";
543 my $pid = open my $diff_fh, '-|';
544 defined $pid or croak
$!;
546 my @diff_tree = qw(git-diff-tree -z -r -C);
547 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
548 push @diff_tree, "-l$_l" if defined $_l;
549 exec(@diff_tree, $from, $treeish) or croak
$!;
551 my $mods = parse_diff_tree
($diff_fh);
553 # git can do empty commits, but SVN doesn't allow it...
556 my ($rm, $add) = precommit_check
($mods);
558 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
559 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
560 if ($m->{chg
} eq 'C') {
561 svn_ensure_parent_path
( $m->{file_b
} );
562 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
563 apply_mod_line_blob
($m);
564 svn_check_prop_executable
($m);
565 } elsif ($m->{chg
} eq 'D') {
566 sys
(qw(svn rm --force), $m->{file_b
});
567 } elsif ($m->{chg
} eq 'R') {
568 svn_ensure_parent_path
( $m->{file_b
} );
569 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
570 apply_mod_line_blob
($m);
571 svn_check_prop_executable
($m);
572 } elsif ($m->{chg
} eq 'M') {
573 apply_mod_line_blob
($m);
574 svn_check_prop_executable
($m);
575 } elsif ($m->{chg
} eq 'T') {
576 sys
(qw(svn rm --force),$m->{file_b
});
577 apply_mod_line_blob
($m);
578 sys
(qw(svn add --force), $m->{file_b
});
579 svn_check_prop_executable
($m);
580 } elsif ($m->{chg
} eq 'A') {
581 svn_ensure_parent_path
( $m->{file_b
} );
582 apply_mod_line_blob
($m);
583 sys
(qw(svn add --force), $m->{file_b
});
584 svn_check_prop_executable
($m);
586 croak
"Invalid chg: $m->{chg}\n";
590 assert_tree
($treeish);
591 if ($_rmdir) { # remove empty directories
592 handle_rmdir
($rm, $add);
594 assert_tree
($treeish);
598 # svn ls doesn't work with respect to the current working tree, but what's
599 # in the repository. There's not even an option for it... *sigh*
600 # (added files don't show up and removed files remain in the ls listing)
602 my ($dir, $rm, $add) = @_;
603 chomp(my @ls = safe_qx
('svn','ls',$dir));
606 s
#/$##; # trailing slashes are evil
607 push @ret, $_ unless $rm->{$dir}->{$_};
609 if (exists $add->{$dir}) {
610 push @ret, keys %{$add->{$dir}};
618 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
619 my $ls = svn_ls_current
($dir, $rm, $add);
620 next if (scalar @
$ls);
621 sys
(qw(svn rm --force),$dir);
623 my $dn = dirname
$dir;
624 $rm->{ $dn }->{ basename
$dir } = 1;
625 $ls = svn_ls_current
($dn, $rm, $add);
626 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
627 sys
(qw(svn rm --force),$dn);
630 $rm->{ $dn }->{ $dir } = 1;
631 $ls = svn_ls_current
($dn, $rm, $add);
636 sub svn_commit_tree
{
637 my ($svn_rev, $commit) = @_;
638 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
639 my %log_msg = ( msg
=> '' );
640 open my $msg, '>', $commit_msg or croak
$!;
642 chomp(my $type = `git-cat-file -t $commit`);
643 if ($type eq 'commit') {
644 my $pid = open my $msg_fh, '-|';
645 defined $pid or croak
$!;
648 exec(qw(git-cat-file commit), $commit) or croak
$!;
653 $in_msg = 1 if (/^\s*$/);
654 } elsif (/^git-svn-id: /) {
655 # skip this, we regenerate the correct one
656 # on re-fetch anyways
658 print $msg $_ or croak
$!;
661 close $msg_fh or croak
$!;
663 close $msg or croak
$!;
665 if ($_edit || ($type eq 'tree')) {
666 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
667 system($editor, $commit_msg);
670 # file_to_s removes all trailing newlines, so just use chomp() here:
671 open $msg, '<', $commit_msg or croak
$!;
672 { local $/; chomp($log_msg{msg
} = <$msg>); }
673 close $msg or croak
$!;
675 my ($oneline) = ($log_msg{msg
} =~ /([^\n\r]+)/);
676 print "Committing $commit: $oneline\n";
678 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
679 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
681 defined $committed or croak
682 "Commit output failed to parse committed revision!\n",
683 join("\n",@ci_output),"\n";
684 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
686 my @svn_up = qw(svn up);
687 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
688 if ($rev_committed == ($svn_rev + 1)) {
689 push @svn_up, "-r$rev_committed";
691 my $info = svn_info
('.');
692 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
693 if ($info->{'Last Changed Rev'} != $rev_committed) {
694 croak
"$info->{'Last Changed Rev'} != $rev_committed\n"
696 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
697 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
698 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
699 or croak
"Failed to parse date: $date\n";
700 $log_msg{date
} = "$tz $Y-$m-$d $H:$M:$S";
701 $log_msg{author
} = $info->{'Last Changed Author'};
702 $log_msg{revision
} = $rev_committed;
703 $log_msg{msg
} .= "\n";
704 my $parent = file_to_s
("$REV_DIR/$svn_rev");
705 git_commit
(\
%log_msg, $parent, $commit);
706 return $rev_committed;
709 push @svn_up, "-r$svn_rev";
711 return fetch
("$rev_committed=$commit")->{revision
};
714 # read the entire log into a temporary file (which is removed ASAP)
715 # and store the file handle + parser state
718 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
720 defined $pid or croak
$!;
722 open STDOUT
, '>&', $log_fh or croak
$!;
723 exec (qw(svn log), @log_args) or croak
$!
727 seek $log_fh, 0, 0 or croak
$!;
728 return { state => 'sep', fh
=> $log_fh };
732 my $log = shift; # retval of svn_log_raw()
739 if ($log->{state} eq 'msg') {
741 $ret->{msg
} .= $_."\n";
742 unless(--$ret->{lines
}) {
743 $log->{state} = 'sep';
746 croak
"Log parse error at: $_\n",
752 if ($log->{state} ne 'sep') {
753 croak
"Log parse error at: $_\n",
754 "state: $log->{state}\n",
758 $log->{state} = 'rev';
760 # if we have an empty log message, put something there:
762 $ret->{msg
} ||= "\n";
763 delete $ret->{lines
};
768 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
770 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
771 ($lines) = ($lines =~ /(\d+)/);
772 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
773 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
774 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
775 or croak
"Failed to parse date: $date\n";
776 $ret = { revision
=> $rev,
777 date
=> "$tz $Y-$m-$d $H:$M:$S",
781 if (defined $_authors && ! defined $users{$author}) {
782 die "Author: $author not defined in ",
785 $log->{state} = 'msg_start';
788 # skip the first blank line of the message:
789 if ($log->{state} eq 'msg_start' && /^$/) {
790 $log->{state} = 'msg';
791 } elsif ($log->{state} eq 'msg') {
793 $ret->{msg
} .= $_."\n";
794 unless (--$ret->{lines
}) {
795 $log->{state} = 'sep';
798 croak
"Log parse error at: $_\n",
799 $ret->{revision
},"\n";
807 my $url = shift || $SVN_URL;
809 my $pid = open my $info_fh, '-|';
810 defined $pid or croak
$!;
813 exec(qw(svn info),$url) or croak
$!;
817 # only single-lines seem to exist in svn info output
820 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
822 push @
{$ret->{-order
}}, $1;
825 close $info_fh or croak
$!;
829 sub sys
{ system(@_) == 0 or croak
$?
}
832 system( "git-diff-files --name-only -z ".
833 " | git-update-index --remove -z --stdin && ".
834 "git-ls-files -z --others ".
835 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
836 " | git-update-index --add -z --stdin"
841 my ($str, $file, $mode) = @_;
842 open my $fd,'>',$file or croak
$!;
843 print $fd $str,"\n" or croak
$!;
844 close $fd or croak
$!;
845 chmod ($mode &~ umask, $file) if (defined $mode);
850 open my $fd,'<',$file or croak
"$!: file: $file\n";
853 close $fd or croak
$!;
858 sub assert_revision_unknown
{
860 if (-f
"$REV_DIR/$revno") {
861 croak
"$REV_DIR/$revno already exists! ",
862 "Why are we refetching it?";
868 my @x = safe_qx
('git-cat-file','commit',$x);
869 my @y = safe_qx
('git-cat-file','commit',$y);
870 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
871 || $y[0] !~ /^tree $sha1\n$/) {
872 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
878 sub assert_revision_eq_or_unknown
{
879 my ($revno, $commit) = @_;
880 if (-f
"$REV_DIR/$revno") {
881 my $current = file_to_s
("$REV_DIR/$revno");
882 if (($commit ne $current) && !trees_eq
($commit, $current)) {
883 croak
"$REV_DIR/$revno already exists!\n",
884 "current: $current\nexpected: $commit\n";
891 my ($log_msg, @parents) = @_;
892 assert_revision_unknown
($log_msg->{revision
});
893 my $out_fh = IO
::File
->new_tmpfile or croak
$!;
895 map_tree_joins
() if (@_branch_from && !%tree_map);
897 # commit parents can be conditionally bound to a particular
898 # svn revision via: "svn_revno=commit_sha1", filter them out here:
900 foreach my $p (@parents) {
901 next unless defined $p;
902 if ($p =~ /^(\d+)=($sha1_short)$/o) {
903 if ($1 == $log_msg->{revision
}) {
904 push @exec_parents, $2;
907 push @exec_parents, $p if $p =~ /$sha1_short/o;
912 defined $pid or croak
$!;
914 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
916 chomp(my $tree = `git-write-tree`);
918 if (exists $tree_map{$tree}) {
919 my %seen_parent = map { $_ => 1 } @exec_parents;
920 foreach (@
{$tree_map{$tree}}) {
921 # MAXPARENT is defined to 16 in commit-tree.c:
922 if ($seen_parent{$_} || @exec_parents > 16) {
925 push @exec_parents, $_;
926 $seen_parent{$_} = 1;
929 my $msg_fh = IO
::File
->new_tmpfile or croak
$!;
930 print $msg_fh $log_msg->{msg
}, "\ngit-svn-id: ",
931 "$SVN_URL\@$log_msg->{revision}",
932 " $SVN_UUID\n" or croak
$!;
933 $msg_fh->flush == 0 or croak
$!;
934 seek $msg_fh, 0, 0 or croak
$!;
936 set_commit_env
($log_msg);
938 my @exec = ('git-commit-tree',$tree);
939 push @exec, '-p', $_ foreach @exec_parents;
940 open STDIN
, '<&', $msg_fh or croak
$!;
941 open STDOUT
, '>&', $out_fh or croak
$!;
942 exec @exec or croak
$!;
947 $out_fh->flush == 0 or croak
$!;
948 seek $out_fh, 0, 0 or croak
$!;
949 chomp(my $commit = do { local $/; <$out_fh> });
950 if ($commit !~ /^$sha1$/o) {
951 croak
"Failed to commit, invalid sha1: $commit\n";
953 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
954 if (my $primary_parent = shift @exec_parents) {
956 defined $pid or croak
$!;
960 exec 'git-rev-parse','--verify',
961 "refs/remotes/$GIT_SVN^0";
964 push @update_ref, $primary_parent unless $?
;
967 sys
('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
968 print "r$log_msg->{revision} = $commit\n";
974 my $author = $log_msg->{author
};
975 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
976 : ($author,"$author\@$SVN_UUID");
977 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
978 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
979 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
982 sub apply_mod_line_blob
{
984 if ($m->{mode_b
} =~ /^120/) {
985 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
987 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
991 sub blob_to_symlink
{
992 my ($blob, $link) = @_;
993 defined $link or croak
"\$link not defined!\n";
994 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
995 if (-l
$link || -f _
) {
996 unlink $link or croak
$!;
999 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1000 symlink $dest, $link or croak
$!;
1004 my ($blob, $file) = @_;
1005 defined $file or croak
"\$file not defined!\n";
1006 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
1007 if (-l
$file || -f _
) {
1008 unlink $file or croak
$!;
1011 open my $blob_fh, '>', $file or croak
"$!: $file\n";
1013 defined $pid or croak
$!;
1016 open STDOUT
, '>&', $blob_fh or croak
$!;
1017 exec('git-cat-file','blob',$blob);
1022 close $blob_fh or croak
$!;
1026 my $pid = open my $child, '-|';
1027 defined $pid or croak
$!;
1029 exec(@_) or croak
$?
;
1031 my @ret = (<$child>);
1032 close $child or croak
$?
;
1033 die $?
if $?
; # just in case close didn't error out
1034 return wantarray ?
@ret : join('',@ret);
1037 sub svn_compat_check
{
1038 my @co_help = safe_qx
(qw(svn co -h));
1039 unless (grep /ignore-externals/,@co_help) {
1040 print STDERR
"W: Installed svn version does not support ",
1041 "--ignore-externals\n";
1042 $_no_ignore_ext = 1;
1044 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1045 $_svn_co_url_revs = 1;
1048 # I really, really hope nobody hits this...
1049 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
1051 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
1053 Lets hope the directory you
're tracking is not a branch or tag
1054 and was never moved within the repository...
1060 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
1061 # (and they won't honor URL@<rev> without -r<rev>, too!)
1062 sub svn_cmd_checkout
{
1063 my ($url, $rev, $dir) = @_;
1064 my @cmd = ('svn','co', "-r$rev");
1065 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1066 $url .= "\@$rev" if $_svn_co_url_revs;
1067 sys
(@cmd, $url, $dir);
1070 sub check_upgrade_needed
{
1072 my $pid = open my $child, '-|';
1073 defined $pid or croak
$!;
1076 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$?
;
1078 my @ret = (<$child>);
1079 close $child or croak
$?
;
1080 die $?
if $?
; # just in case close didn't error out
1081 return wantarray ?
@ret : join('',@ret);
1084 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
1086 print STDERR
"Please run: $0 rebuild --upgrade\n";
1091 # fills %tree_map with a reverse mapping of trees to commits. Useful
1092 # for finding parents to commit on.
1093 sub map_tree_joins
{
1094 foreach my $br (@_branch_from) {
1095 my $pid = open my $pipe, '-|';
1096 defined $pid or croak
$!;
1098 exec(qw(git-rev-list --pretty=raw), $br) or croak
$?
;
1101 if (/^commit ($sha1)$/o) {
1103 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1104 unless (defined $tree) {
1105 die "Failed to parse commit $commit\n";
1107 push @
{$tree_map{$tree}}, $commit;
1110 close $pipe or croak
$?
;
1114 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1116 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1117 while (<$authors>) {
1119 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1120 my ($user, $name, $email) = ($1, $2, $3);
1121 $users{$user} = [$name, $email];
1123 close $authors or croak
$!;
1130 $svn_log hashref
(as returned by svn_log_raw
)
1132 fh
=> file handle of the
log file
,
1133 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
1136 $log_msg hashref as returned by next_log_entry
($svn_log)
1138 msg
=> 'whitespace-formatted log entry
1139 ', # trailing newline is preserved
1140 revision
=> '8', # integer
1141 date
=> '2004-02-24T17:01:44.108345Z', # commit date
1142 author
=> 'committer name'
1146 @mods = array of diff
-index line hashes
, each element represents one line
1147 of diff
-index output
1149 diff
-index line
($m hash
)
1151 mode_a
=> first column of diff
-index output
, no leading
':',
1152 mode_b
=> second column of diff
-index output
,
1153 sha1_b
=> sha1sum of the final blob
,
1154 chg
=> change type
[MCRADT
],
1155 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
1156 file_b
=> new
/current file name of a file
(any chg
)