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 $GIT_SVN = $ENV{GIT_SVN_ID
} || 'git-svn';
14 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
15 $ENV{GIT_DIR
} ||= $GIT_DIR;
17 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
18 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
20 # make sure the svn binary gives consistent output between locales and TZs:
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:
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/;
34 use POSIX qw
/strftime/;
35 my $sha1 = qr/[a-f\d]{40}/;
36 my $sha1_short = qr/[a-f\d]{4,40}/;
37 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
38 $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
39 my (@_branch_from, %tree_map, %users);
41 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
42 'branch|b=s' => \
@_branch_from,
43 'authors-file|A=s' => \
$_authors );
45 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
46 { 'revision|r=s' => \
$_revision, %fc_opts } ],
47 init
=> [ \
&init
, "Initialize and fetch (import)", { } ],
48 commit
=> [ \
&commit
, "Commit git revisions to SVN",
49 { 'stdin|' => \
$_stdin,
52 'find-copies-harder' => \
$_find_copies_harder,
56 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings", { } ],
57 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
58 { 'no-ignore-externals' => \
$_no_ignore_ext,
59 'upgrade' => \
$_upgrade } ],
62 for (my $i = 0; $i < @ARGV; $i++) {
63 if (defined $cmd{$ARGV[$i]}) {
70 # we may be called as git-svn-(command), or git-svn(command).
72 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
79 %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
81 GetOptions
(%opts, 'help|H|h' => \
$_help, 'version|V' => \
$_version ) or exit 1;
83 version
() if $_version;
84 usage
(1) unless defined $cmd;
85 load_authors
() if $_authors;
86 svn_check_ignore_externals
();
87 $cmd{$cmd}->[0]->(@ARGV);
90 ####################### primary functions ######################
92 my $exit = shift || 0;
93 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
95 git
-svn
- bidirectional operations between a single Subversion tree
and git
96 Usage
: $0 <command
> [options
] [arguments
]\n
99 foreach (sort keys %cmd) {
100 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
103 \nGIT_SVN_ID may be set
in the environment to an arbitrary identifier
if
104 you
're tracking multiple SVN branches/repositories in one git repository
105 and want to keep them separate. See git-svn(1) for more information.
111 print "git-svn version $VERSION\n";
116 $SVN_URL = shift or undef;
119 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
121 check_upgrade_needed();
124 my $pid = open(my $rev_list,'-|');
125 defined $pid or croak $!;
127 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
130 while (<$rev_list>) {
133 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
134 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
135 next if (!@commit); # skip merges
136 my $id = $commit[$#commit];
137 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
139 if (!$rev || !$uuid || !$url) {
140 # some of the original repositories I made had
141 # indentifiers like this:
142 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
144 if (!$rev || !$uuid) {
145 croak "Unable to extract revision or UUID from ",
150 # if we merged or otherwise started elsewhere, this is
151 # how we break out of it
152 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
153 next if (defined $SVN_URL && ($url ne $SVN_URL));
155 print "r$rev = $c\n";
156 unless (defined $latest) {
157 if (!$SVN_URL && !$url) {
158 croak "SVN repository location required: $url\n";
161 $SVN_UUID ||= setup_git_svn();
164 assert_revision_eq_or_unknown($rev, $c);
165 sys('git
-update
-ref',"$GIT_SVN/revs/$rev",$c);
166 $newest_rev = $rev if ($rev > $newest_rev);
168 close $rev_list or croak $?;
169 if (!chdir $SVN_WC) {
170 my @svn_co = ('svn
','co
',"-r$latest");
171 push @svn_co, '--ignore
-externals
' unless $_no_ignore_ext;
172 sys(@svn_co, $SVN_URL, $SVN_WC);
173 chdir $SVN_WC or croak $!;
177 defined $pid or croak $!;
179 my @svn_up = qw(svn up);
180 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
181 sys
(@svn_up,"-r$newest_rev");
182 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
184 exec('git-write-tree');
190 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
191 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
197 $SVN_URL = shift or croak
"SVN repository location required\n";
198 unless (-d
$GIT_DIR) {
206 check_upgrade_needed
();
207 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
208 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
209 unless ($_revision) {
210 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
212 push @log_args, "-r$_revision";
213 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
215 my $svn_log = svn_log_raw
(@log_args);
216 @
$svn_log = sort { $a->{revision
} <=> $b->{revision
} } @
$svn_log;
218 my $base = shift @
$svn_log or croak
"No base revision!\n";
219 my $last_commit = undef;
220 unless (-d
$SVN_WC) {
221 my @svn_co = ('svn','co',"-r$base->{revision}");
222 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
223 sys
(@svn_co, $SVN_URL, $SVN_WC);
224 chdir $SVN_WC or croak
$!;
225 $last_commit = git_commit
($base, @parents);
226 assert_svn_wc_clean
($base->{revision
}, $last_commit);
228 chdir $SVN_WC or croak
$!;
229 $last_commit = file_to_s
("$REV_DIR/$base->{revision}");
231 my @svn_up = qw(svn up);
232 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
233 my $last_rev = $base->{revision
};
234 foreach my $log_msg (@
$svn_log) {
235 assert_svn_wc_clean
($last_rev, $last_commit);
236 $last_rev = $log_msg->{revision
};
237 sys
(@svn_up,"-r$last_rev");
238 $last_commit = git_commit
($log_msg, $last_commit, @parents);
240 assert_svn_wc_clean
($last_rev, $last_commit);
241 unless (-e
"$GIT_DIR/refs/heads/master") {
242 sys
(qw(git-update-ref refs/heads/master),$last_commit);
244 return pop @
$svn_log;
249 check_upgrade_needed
();
250 if ($_stdin || !@commits) {
251 print "Reading from stdin...\n";
254 if (/\b($sha1_short)\b/o) {
255 unshift @commits, $1;
260 foreach my $c (@commits) {
261 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
262 if (scalar @tmp == 1) {
264 } elsif (scalar @tmp > 1) {
265 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
267 die "Failed to rev-parse $c\n";
273 chdir $SVN_WC or croak
$!;
274 my $svn_current_rev = svn_info
('.')->{'Last Changed Rev'};
275 foreach my $c (@revs) {
276 my $mods = svn_checkout_tree
($svn_current_rev, $c);
277 if (scalar @
$mods == 0) {
278 print "Skipping, no changes detected\n";
281 $svn_current_rev = svn_commit_tree
($svn_current_rev, $c);
283 print "Done committing ",scalar @revs," revisions to SVN\n";
288 require File
::Find
or die $!;
289 my $exclude_file = "$GIT_DIR/info/exclude";
290 open my $fh, '<', $exclude_file or croak
$!;
291 chomp(my @excludes = (<$fh>));
292 close $fh or croak
$!;
294 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
295 chdir $SVN_WC or croak
$!;
297 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
299 @
{$ign{$_}} = safe_qx
(qw(svn propget svn:ignore),$_);
300 }}, no_chdir
=>1},'.');
303 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
305 foreach my $i (sort keys %ign) {
306 print "\n# ",$i,"\n";
307 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
311 ########################### utility functions #########################
314 defined $SVN_URL or croak
"SVN repository location required\n";
315 unless (-d
$GIT_DIR) {
316 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
318 mkpath
(["$GIT_DIR/$GIT_SVN"]);
319 mkpath
(["$GIT_DIR/$GIT_SVN/info"]);
321 s_to_file
($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
322 $SVN_UUID = svn_info
($SVN_URL)->{'Repository UUID'} or
323 croak
"Repository UUID unreadable\n";
324 s_to_file
($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
326 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak
$!;
327 print $fd '.svn',"\n";
328 close $fd or croak
$!;
332 sub assert_svn_wc_clean
{
333 my ($svn_rev, $treeish) = @_;
334 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
335 croak
"$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
336 my $svn_info = svn_info
('.');
337 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
338 croak
"Expected r$svn_rev, got r",
339 $svn_info->{'Last Changed Rev'},"\n";
341 my @status = grep(!/^Performing status on external/,(`svn status`));
342 @status = grep(!/^\s*$/,@status);
343 if (scalar @status) {
344 print STDERR
"Tree ($SVN_WC) is not clean:\n";
345 print STDERR
$_ foreach @status;
348 assert_tree
($treeish);
353 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
354 chomp(my $type = `git-cat-file -t $treeish`);
356 while ($type eq 'tag') {
357 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
359 if ($type eq 'commit') {
360 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
361 ($expected) = ($expected =~ /^tree ($sha1)$/);
362 die "Unable to get tree from $treeish\n" unless $expected;
363 } elsif ($type eq 'tree') {
364 $expected = $treeish;
366 die "$treeish is a $type, expected tree, tag or commit\n";
369 my $old_index = $ENV{GIT_INDEX_FILE
};
370 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
372 unlink $tmpindex or croak
$!;
374 $ENV{GIT_INDEX_FILE
} = $tmpindex;
376 chomp(my $tree = `git-write-tree`);
378 $ENV{GIT_INDEX_FILE
} = $old_index;
380 delete $ENV{GIT_INDEX_FILE
};
382 if ($tree ne $expected) {
383 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
387 sub parse_diff_tree
{
393 chomp $_; # this gets rid of the trailing "\0"
394 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
395 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
396 push @mods, { mode_a
=> $1, mode_b
=> $2,
397 sha1_b
=> $3, chg
=> $4 };
398 if ($4 =~ /^(?:C|R)$/) {
403 } elsif ($state eq 'file_a') {
404 my $x = $mods[$#mods] or croak
"Empty array\n";
405 if ($x->{chg
} !~ /^(?:C|R)$/) {
406 croak
"Error parsing $_, $x->{chg}\n";
410 } elsif ($state eq 'file_b') {
411 my $x = $mods[$#mods] or croak
"Empty array\n";
412 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
413 croak
"Error parsing $_, $x->{chg}\n";
415 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
416 croak
"Error parsing $_, $x->{chg}\n";
421 croak
"Error parsing $_\n";
424 close $diff_fh or croak
$!;
429 sub svn_check_prop_executable
{
431 return if -l
$m->{file_b
};
432 if ($m->{mode_b
} =~ /755$/) {
433 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
434 if ($m->{mode_a
} !~ /755$/) {
435 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
437 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
438 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
439 sys
(qw(svn propdel svn:executable), $m->{file_b
});
440 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
441 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
445 sub svn_ensure_parent_path
{
446 my $dir_b = dirname
(shift);
447 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
448 mkpath
([$dir_b]) unless (-d
$dir_b);
449 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
452 sub precommit_check
{
454 my (%rm_file, %rmdir_check, %added_check);
456 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
457 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
458 if ($m->{chg
} eq 'R') {
459 if (-d
$m->{file_b
}) {
460 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
462 # dir/$file => dir/file/$file
463 my $dirname = dirname
($m->{file_b
});
464 while ($dirname ne File
::Spec
->curdir) {
465 if ($dirname ne $m->{file_a
}) {
466 $dirname = dirname
($dirname);
469 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
471 # baz/zzz => baz (baz is a file)
472 $dirname = dirname
($m->{file_a
});
473 while ($dirname ne File
::Spec
->curdir) {
474 if ($dirname ne $m->{file_b
}) {
475 $dirname = dirname
($dirname);
478 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
481 if ($m->{chg
} =~ /^(D|R)$/) {
482 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
483 $rm_file{ $m->{$t} } = 1;
484 my $dirname = dirname
( $m->{$t} );
485 my $basename = basename
( $m->{$t} );
486 $rmdir_check{$dirname}->{$basename} = 1;
487 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
488 if (-d
$m->{file_b
}) {
489 err_dir_to_file
($m->{file_b
});
491 my $dirname = dirname
( $m->{file_b
} );
492 my $basename = basename
( $m->{file_b
} );
493 $added_check{$dirname}->{$basename} = 1;
494 while ($dirname ne File
::Spec
->curdir) {
495 if ($rm_file{$dirname}) {
496 err_file_to_dir
($m->{file_b
});
498 $dirname = dirname
$dirname;
502 return (\
%rmdir_check, \
%added_check);
504 sub err_dir_to_file
{
506 print STDERR
"Node change from directory to file ",
507 "is not supported by Subversion: ",$file,"\n";
510 sub err_file_to_dir
{
512 print STDERR
"Node change from file to directory ",
513 "is not supported by Subversion: ",$file,"\n";
518 sub svn_checkout_tree
{
519 my ($svn_rev, $treeish) = @_;
520 my $from = file_to_s
("$REV_DIR/$svn_rev");
521 assert_svn_wc_clean
($svn_rev,$from);
522 print "diff-tree $from $treeish\n";
523 my $pid = open my $diff_fh, '-|';
524 defined $pid or croak
$!;
526 my @diff_tree = qw(git-diff-tree -z -r -C);
527 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
528 push @diff_tree, "-l$_l" if defined $_l;
529 exec(@diff_tree, $from, $treeish) or croak
$!;
531 my $mods = parse_diff_tree
($diff_fh);
533 # git can do empty commits, but SVN doesn't allow it...
536 my ($rm, $add) = precommit_check
($mods);
538 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
539 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
540 if ($m->{chg
} eq 'C') {
541 svn_ensure_parent_path
( $m->{file_b
} );
542 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
543 apply_mod_line_blob
($m);
544 svn_check_prop_executable
($m);
545 } elsif ($m->{chg
} eq 'D') {
546 sys
(qw(svn rm --force), $m->{file_b
});
547 } elsif ($m->{chg
} eq 'R') {
548 svn_ensure_parent_path
( $m->{file_b
} );
549 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
550 apply_mod_line_blob
($m);
551 svn_check_prop_executable
($m);
552 } elsif ($m->{chg
} eq 'M') {
553 apply_mod_line_blob
($m);
554 svn_check_prop_executable
($m);
555 } elsif ($m->{chg
} eq 'T') {
556 sys
(qw(svn rm --force),$m->{file_b
});
557 apply_mod_line_blob
($m);
558 sys
(qw(svn add --force), $m->{file_b
});
559 svn_check_prop_executable
($m);
560 } elsif ($m->{chg
} eq 'A') {
561 svn_ensure_parent_path
( $m->{file_b
} );
562 apply_mod_line_blob
($m);
563 sys
(qw(svn add --force), $m->{file_b
});
564 svn_check_prop_executable
($m);
566 croak
"Invalid chg: $m->{chg}\n";
570 assert_tree
($treeish);
571 if ($_rmdir) { # remove empty directories
572 handle_rmdir
($rm, $add);
574 assert_tree
($treeish);
578 # svn ls doesn't work with respect to the current working tree, but what's
579 # in the repository. There's not even an option for it... *sigh*
580 # (added files don't show up and removed files remain in the ls listing)
582 my ($dir, $rm, $add) = @_;
583 chomp(my @ls = safe_qx
('svn','ls',$dir));
586 s
#/$##; # trailing slashes are evil
587 push @ret, $_ unless $rm->{$dir}->{$_};
589 if (exists $add->{$dir}) {
590 push @ret, keys %{$add->{$dir}};
598 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
599 my $ls = svn_ls_current
($dir, $rm, $add);
600 next if (scalar @
$ls);
601 sys
(qw(svn rm --force),$dir);
603 my $dn = dirname
$dir;
604 $rm->{ $dn }->{ basename
$dir } = 1;
605 $ls = svn_ls_current
($dn, $rm, $add);
606 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
607 sys
(qw(svn rm --force),$dn);
610 $rm->{ $dn }->{ $dir } = 1;
611 $ls = svn_ls_current
($dn, $rm, $add);
616 sub svn_commit_tree
{
617 my ($svn_rev, $commit) = @_;
618 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
619 my %log_msg = ( msg
=> '' );
620 open my $msg, '>', $commit_msg or croak
$!;
622 chomp(my $type = `git-cat-file -t $commit`);
623 if ($type eq 'commit') {
624 my $pid = open my $msg_fh, '-|';
625 defined $pid or croak
$!;
628 exec(qw(git-cat-file commit), $commit) or croak
$!;
633 $in_msg = 1 if (/^\s*$/);
634 } elsif (/^git-svn-id: /) {
635 # skip this, we regenerate the correct one
636 # on re-fetch anyways
638 print $msg $_ or croak
$!;
641 close $msg_fh or croak
$!;
643 close $msg or croak
$!;
645 if ($_edit || ($type eq 'tree')) {
646 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
647 system($editor, $commit_msg);
650 # file_to_s removes all trailing newlines, so just use chomp() here:
651 open $msg, '<', $commit_msg or croak
$!;
652 { local $/; chomp($log_msg{msg
} = <$msg>); }
653 close $msg or croak
$!;
655 my ($oneline) = ($log_msg{msg
} =~ /([^\n\r]+)/);
656 print "Committing $commit: $oneline\n";
658 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
659 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
661 defined $committed or croak
662 "Commit output failed to parse committed revision!\n",
663 join("\n",@ci_output),"\n";
664 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
666 my @svn_up = qw(svn up);
667 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
668 if ($rev_committed == ($svn_rev + 1)) {
669 push @svn_up, "-r$rev_committed";
671 my $info = svn_info
('.');
672 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
673 if ($info->{'Last Changed Rev'} != $rev_committed) {
674 croak
"$info->{'Last Changed Rev'} != $rev_committed\n"
676 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
677 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
678 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
679 or croak
"Failed to parse date: $date\n";
680 $log_msg{date
} = "$tz $Y-$m-$d $H:$M:$S";
681 $log_msg{author
} = $info->{'Last Changed Author'};
682 $log_msg{revision
} = $rev_committed;
683 $log_msg{msg
} .= "\n";
684 my $parent = file_to_s
("$REV_DIR/$svn_rev");
685 git_commit
(\
%log_msg, $parent, $commit);
686 return $rev_committed;
689 push @svn_up, "-r$svn_rev";
691 return fetch
("$rev_committed=$commit")->{revision
};
696 my $pid = open my $log_fh,'-|';
697 defined $pid or croak
$!;
700 exec (qw(svn log), @log_args) or croak
$!
708 if ($state eq 'msg') {
709 if ($svn_log[$#svn_log]->{lines
}) {
710 $svn_log[$#svn_log]->{msg
} .= $_."\n";
711 unless(--$svn_log[$#svn_log]->{lines
}) {
715 croak
"Log parse error at: $_\n",
716 $svn_log[$#svn_log]->{revision
},
721 if ($state ne 'sep') {
722 croak
"Log parse error at: $_\n",
724 $svn_log[$#svn_log]->{revision
},
729 # if we have an empty log message, put something there:
731 $svn_log[$#svn_log]->{msg
} ||= "\n";
732 delete $svn_log[$#svn_log]->{lines
};
736 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
738 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
739 ($lines) = ($lines =~ /(\d+)/);
740 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
741 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
742 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
743 or croak
"Failed to parse date: $date\n";
744 my %log_msg = ( revision
=> $rev,
745 date
=> "$tz $Y-$m-$d $H:$M:$S",
749 if (defined $_authors && ! defined $users{$author}) {
750 die "Author: $author not defined in ",
753 push @svn_log, \
%log_msg;
754 $state = 'msg_start';
757 # skip the first blank line of the message:
758 if ($state eq 'msg_start' && /^$/) {
760 } elsif ($state eq 'msg') {
761 if ($svn_log[$#svn_log]->{lines
}) {
762 $svn_log[$#svn_log]->{msg
} .= $_."\n";
763 unless (--$svn_log[$#svn_log]->{lines
}) {
767 croak
"Log parse error at: $_\n",
768 $svn_log[$#svn_log]->{revision
},"\n";
772 close $log_fh or croak
$?
;
777 my $url = shift || $SVN_URL;
779 my $pid = open my $info_fh, '-|';
780 defined $pid or croak
$!;
783 exec(qw(svn info),$url) or croak
$!;
787 # only single-lines seem to exist in svn info output
790 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
792 push @
{$ret->{-order
}}, $1;
795 close $info_fh or croak
$!;
799 sub sys
{ system(@_) == 0 or croak
$?
}
802 system( "git-diff-files --name-only -z ".
803 " | git-update-index --remove -z --stdin && ".
804 "git-ls-files -z --others ".
805 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
806 " | git-update-index --add -z --stdin"
811 my ($str, $file, $mode) = @_;
812 open my $fd,'>',$file or croak
$!;
813 print $fd $str,"\n" or croak
$!;
814 close $fd or croak
$!;
815 chmod ($mode &~ umask, $file) if (defined $mode);
820 open my $fd,'<',$file or croak
"$!: file: $file\n";
823 close $fd or croak
$!;
828 sub assert_revision_unknown
{
830 if (-f
"$REV_DIR/$revno") {
831 croak
"$REV_DIR/$revno already exists! ",
832 "Why are we refetching it?";
836 sub assert_revision_eq_or_unknown
{
837 my ($revno, $commit) = @_;
838 if (-f
"$REV_DIR/$revno") {
839 my $current = file_to_s
("$REV_DIR/$revno");
840 if ($commit ne $current) {
841 croak
"$REV_DIR/$revno already exists!\n",
842 "current: $current\nexpected: $commit\n";
849 my ($log_msg, @parents) = @_;
850 assert_revision_unknown
($log_msg->{revision
});
851 my $out_fh = IO
::File
->new_tmpfile or croak
$!;
852 $SVN_UUID ||= svn_info
('.')->{'Repository UUID'};
854 map_tree_joins
() if (@_branch_from && !%tree_map);
856 # commit parents can be conditionally bound to a particular
857 # svn revision via: "svn_revno=commit_sha1", filter them out here:
859 foreach my $p (@parents) {
860 next unless defined $p;
861 if ($p =~ /^(\d+)=($sha1_short)$/o) {
862 if ($1 == $log_msg->{revision
}) {
863 push @exec_parents, $2;
866 push @exec_parents, $p if $p =~ /$sha1_short/o;
871 defined $pid or croak
$!;
873 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
875 chomp(my $tree = `git-write-tree`);
877 if (exists $tree_map{$tree}) {
878 my %seen_parent = map { $_ => 1 } @exec_parents;
879 foreach (@
{$tree_map{$tree}}) {
880 # MAXPARENT is defined to 16 in commit-tree.c:
881 if ($seen_parent{$_} || @exec_parents > 16) {
884 push @exec_parents, $_;
885 $seen_parent{$_} = 1;
888 my $msg_fh = IO
::File
->new_tmpfile or croak
$!;
889 print $msg_fh $log_msg->{msg
}, "\ngit-svn-id: ",
890 "$SVN_URL\@$log_msg->{revision}",
891 " $SVN_UUID\n" or croak
$!;
892 $msg_fh->flush == 0 or croak
$!;
893 seek $msg_fh, 0, 0 or croak
$!;
895 set_commit_env
($log_msg);
897 my @exec = ('git-commit-tree',$tree);
898 push @exec, '-p', $_ foreach @exec_parents;
899 open STDIN
, '<&', $msg_fh or croak
$!;
900 open STDOUT
, '>&', $out_fh or croak
$!;
901 exec @exec or croak
$!;
906 $out_fh->flush == 0 or croak
$!;
907 seek $out_fh, 0, 0 or croak
$!;
908 chomp(my $commit = do { local $/; <$out_fh> });
909 if ($commit !~ /^$sha1$/o) {
910 croak
"Failed to commit, invalid sha1: $commit\n";
912 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
913 if (my $primary_parent = shift @exec_parents) {
914 push @update_ref, $primary_parent;
917 sys
('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
918 print "r$log_msg->{revision} = $commit\n";
924 my $author = $log_msg->{author
};
925 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
926 : ($author,"$author\@$SVN_UUID");
927 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
928 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
929 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
932 sub apply_mod_line_blob
{
934 if ($m->{mode_b
} =~ /^120/) {
935 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
937 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
941 sub blob_to_symlink
{
942 my ($blob, $link) = @_;
943 defined $link or croak
"\$link not defined!\n";
944 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
945 if (-l
$link || -f _
) {
946 unlink $link or croak
$!;
949 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
950 symlink $dest, $link or croak
$!;
954 my ($blob, $file) = @_;
955 defined $file or croak
"\$file not defined!\n";
956 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
957 if (-l
$file || -f _
) {
958 unlink $file or croak
$!;
961 open my $blob_fh, '>', $file or croak
"$!: $file\n";
963 defined $pid or croak
$!;
966 open STDOUT
, '>&', $blob_fh or croak
$!;
967 exec('git-cat-file','blob',$blob);
972 close $blob_fh or croak
$!;
976 my $pid = open my $child, '-|';
977 defined $pid or croak
$!;
979 exec(@_) or croak
$?
;
981 my @ret = (<$child>);
982 close $child or croak
$?
;
983 die $?
if $?
; # just in case close didn't error out
984 return wantarray ?
@ret : join('',@ret);
987 sub svn_check_ignore_externals
{
988 return if $_no_ignore_ext;
989 unless (grep /ignore-externals/,(safe_qx
(qw(svn co -h)))) {
990 print STDERR
"W: Installed svn version does not support ",
991 "--ignore-externals\n";
996 sub check_upgrade_needed
{
998 my $pid = open my $child, '-|';
999 defined $pid or croak
$!;
1002 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$?
;
1004 my @ret = (<$child>);
1005 close $child or croak
$?
;
1006 die $?
if $?
; # just in case close didn't error out
1007 return wantarray ?
@ret : join('',@ret);
1010 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
1012 print STDERR
"Please run: $0 rebuild --upgrade\n";
1017 # fills %tree_map with a reverse mapping of trees to commits. Useful
1018 # for finding parents to commit on.
1019 sub map_tree_joins
{
1020 foreach my $br (@_branch_from) {
1021 my $pid = open my $pipe, '-|';
1022 defined $pid or croak
$!;
1024 exec(qw(git-rev-list --pretty=raw), $br) or croak
$?
;
1027 if (/^commit ($sha1)$/o) {
1029 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1030 unless (defined $tree) {
1031 die "Failed to parse commit $commit\n";
1033 push @
{$tree_map{$tree}}, $commit;
1036 close $pipe or croak
$?
;
1040 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1042 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1043 while (<$authors>) {
1045 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1046 my ($user, $name, $email) = ($1, $2, $3);
1047 $users{$user} = [$name, $email];
1049 close $authors or croak
$!;
1056 @svn_log = array of log_msg hashes
1060 msg
=> 'whitespace-formatted log entry
1061 ', # trailing newline is preserved
1062 revision
=> '8', # integer
1063 date
=> '2004-02-24T17:01:44.108345Z', # commit date
1064 author
=> 'committer name'
1068 @mods = array of diff
-index line hashes
, each element represents one line
1069 of diff
-index output
1071 diff
-index line
($m hash
)
1073 mode_a
=> first column of diff
-index output
, no leading
':',
1074 mode_b
=> second column of diff
-index output
,
1075 sha1_b
=> sha1sum of the final blob
,
1076 chg
=> change type
[MCRADT
],
1077 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
1078 file_b
=> new
/current file name of a file
(any chg
)