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>';
11 $VERSION = '1.1.0-pre';
14 $GIT_DIR = abs_path
($ENV{GIT_DIR
} || '.git');
15 $ENV{GIT_DIR
} = $GIT_DIR;
17 # make sure the svn binary gives consistent output between locales and TZs:
21 # If SVN:: library support is added, please make the dependencies
22 # optional and preserve the capability to use the command-line client.
23 # use eval { require SVN::... } to make it lazy load
24 # We don't use any modules not in the standard Perl distribution:
27 use File
::Basename qw
/dirname basename/;
28 use File
::Path qw
/mkpath/;
29 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
31 use POSIX qw
/strftime/;
32 my $sha1 = qr/[a-f\d]{40}/;
33 my $sha1_short = qr/[a-f\d]{4,40}/;
34 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
35 $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
36 my (@_branch_from, %tree_map, %users);
39 my %fc_opts = ( 'no-ignore-externals' => \
$_no_ignore_ext,
40 'branch|b=s' => \
@_branch_from,
41 'authors-file|A=s' => \
$_authors );
43 # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
44 my %EOL = ( CR
=> "\015", LF
=> "\012", CRLF
=> "\015\012", native
=> "\012" );
47 fetch
=> [ \
&fetch
, "Download new revisions from SVN",
48 { 'revision|r=s' => \
$_revision, %fc_opts } ],
49 init
=> [ \
&init
, "Initialize a repo for tracking" .
50 " (requires URL argument)", { } ],
51 commit
=> [ \
&commit
, "Commit git revisions to SVN",
52 { 'stdin|' => \
$_stdin,
55 'find-copies-harder' => \
$_find_copies_harder,
59 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings", { } ],
60 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)",
61 { 'no-ignore-externals' => \
$_no_ignore_ext,
62 'upgrade' => \
$_upgrade } ],
65 for (my $i = 0; $i < @ARGV; $i++) {
66 if (defined $cmd{$ARGV[$i]}) {
73 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
75 # convert GetOpt::Long specs for use by git-repo-config
76 foreach my $o (keys %opts) {
78 my ($key) = ($o =~ /^([a-z\-]+)/);
80 my $arg = 'git-repo-config';
81 $arg .= ' --int' if ($o =~ /=i$/);
82 $arg .= ' --bool' if ($o !~ /=[sfi]$/);
83 if (ref $v eq 'ARRAY') {
84 chomp(my @tmp = `$arg --get-all svn.$key`);
87 chomp(my $tmp = `$arg --get svn.$key`);
88 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
94 GetOptions
(%opts, 'help|H|h' => \
$_help,
95 'version|V' => \
$_version,
96 'id|i=s' => \
$GIT_SVN) or exit 1;
98 $GIT_SVN ||= $ENV{GIT_SVN_ID
} || 'git-svn';
99 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
101 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
102 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
105 version
() if $_version;
106 usage
(1) unless defined $cmd;
107 load_authors
() if $_authors;
109 $cmd{$cmd}->[0]->(@ARGV);
112 ####################### primary functions ######################
114 my $exit = shift || 0;
115 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
117 git
-svn
- bidirectional operations between a single Subversion tree
and git
118 Usage
: $0 <command
> [options
] [arguments
]\n
120 print $fd "Available commands:\n" unless $cmd;
122 foreach (sort keys %cmd) {
123 next if $cmd && $cmd ne $_;
124 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
125 foreach (keys %{$cmd{$_}->[2]}) {
126 # prints out arguments as they should be passed:
127 my $x = s
#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
128 print $fd ' ' x
17, join(', ', map { length $_ > 1 ?
130 split /\|/,$_)," $x\n";
134 \nGIT_SVN_ID may be set
in the environment
or via the
--id
/-i switch to an
135 arbitrary identifier
if you
're tracking multiple SVN branches/repositories in
136 one git repository and want to keep them separate. See git-svn(1) for more
143 print "git-svn version $VERSION\n";
148 $SVN_URL = shift or undef;
151 sys('git
-update
-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
153 check_upgrade_needed();
156 my $pid = open(my $rev_list,'-|');
157 defined $pid or croak $!;
159 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
162 while (<$rev_list>) {
165 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
166 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
167 next if (!@commit); # skip merges
168 my $id = $commit[$#commit];
169 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
171 if (!$rev || !$uuid || !$url) {
172 # some of the original repositories I made had
173 # indentifiers like this:
174 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
176 if (!$rev || !$uuid) {
177 croak "Unable to extract revision or UUID from ",
182 # if we merged or otherwise started elsewhere, this is
183 # how we break out of it
184 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
185 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
187 print "r$rev = $c\n";
188 unless (defined $latest) {
189 if (!$SVN_URL && !$url) {
190 croak "SVN repository location required: $url\n";
197 assert_revision_eq_or_unknown($rev, $c);
198 sys('git
-update
-ref',"$GIT_SVN/revs/$rev",$c);
199 $newest_rev = $rev if ($rev > $newest_rev);
201 close $rev_list or croak $?;
202 if (!chdir $SVN_WC) {
203 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
204 chdir $SVN_WC or croak $!;
208 defined $pid or croak $!;
210 my @svn_up = qw(svn up);
211 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
212 sys
(@svn_up,"-r$newest_rev");
213 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
215 exec('git-write-tree');
221 Keeping deprecated refs
/head/$GIT_SVN-HEAD
for now
. Please remove it
222 when you have upgraded your tools
and habits to
use refs
/remotes/$GIT_SVN
228 $SVN_URL = shift or die "SVN repository location required " .
229 "as a command-line argument\n";
230 unless (-d
$GIT_DIR) {
238 check_upgrade_needed
();
239 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
240 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
241 unless ($_revision) {
242 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
244 push @log_args, "-r$_revision";
245 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
247 my $svn_log = svn_log_raw
(@log_args);
249 my $base = next_log_entry
($svn_log) or croak
"No base revision!\n";
250 my $last_commit = undef;
251 unless (-d
$SVN_WC) {
252 svn_cmd_checkout
($SVN_URL,$base->{revision
},$SVN_WC);
253 chdir $SVN_WC or croak
$!;
255 $last_commit = git_commit
($base, @parents);
256 assert_tree
($last_commit);
258 chdir $SVN_WC or croak
$!;
260 $last_commit = file_to_s
("$REV_DIR/$base->{revision}");
262 my @svn_up = qw(svn up);
263 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
265 while (my $log_msg = next_log_entry
($svn_log)) {
266 assert_tree
($last_commit);
267 if ($last->{revision
} >= $log_msg->{revision
}) {
268 croak
"Out of order: last >= current: ",
269 "$last->{revision} >= $log_msg->{revision}\n";
271 # Revert is needed for cases like:
272 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
273 # I can't seem to reproduce something like that on a test...
274 sys
(qw
/svn revert -R ./);
275 assert_svn_wc_clean
($last->{revision
});
276 sys
(@svn_up,"-r$log_msg->{revision}");
277 $last_commit = git_commit
($log_msg, $last_commit, @parents);
280 unless (-e
"$GIT_DIR/refs/heads/master") {
281 sys
(qw(git-update-ref refs/heads/master),$last_commit);
288 check_upgrade_needed
();
289 if ($_stdin || !@commits) {
290 print "Reading from stdin...\n";
293 if (/\b($sha1_short)\b/o) {
294 unshift @commits, $1;
299 foreach my $c (@commits) {
300 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
301 if (scalar @tmp == 1) {
303 } elsif (scalar @tmp > 1) {
304 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
306 die "Failed to rev-parse $c\n";
312 chdir $SVN_WC or croak
$!;
313 my $info = svn_info
('.');
315 my $svn_current_rev = $info->{'Last Changed Rev'};
316 foreach my $c (@revs) {
317 my $mods = svn_checkout_tree
($svn_current_rev, $c);
318 if (scalar @
$mods == 0) {
319 print "Skipping, no changes detected\n";
322 $svn_current_rev = svn_commit_tree
($svn_current_rev, $c);
324 print "Done committing ",scalar @revs," revisions to SVN\n";
328 require File
::Find
or die $!;
329 my $exclude_file = "$GIT_DIR/info/exclude";
330 open my $fh, '<', $exclude_file or croak
$!;
331 chomp(my @excludes = (<$fh>));
332 close $fh or croak
$!;
334 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
335 chdir $SVN_WC or croak
$!;
337 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
339 @
{$ign{$_}} = safe_qx
(qw(svn propget svn:ignore),$_);
340 }}, no_chdir
=>1},'.');
343 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
345 foreach my $i (sort keys %ign) {
346 print "\n# ",$i,"\n";
347 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
351 ########################### utility functions #########################
355 my $info = shift || svn_info
('.');
356 $SVN_UUID = $info->{'Repository UUID'} or
357 croak
"Repository UUID unreadable\n";
358 s_to_file
($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
362 defined $SVN_URL or croak
"SVN repository location required\n";
363 unless (-d
$GIT_DIR) {
364 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
366 mkpath
(["$GIT_DIR/$GIT_SVN"]);
367 mkpath
(["$GIT_DIR/$GIT_SVN/info"]);
369 s_to_file
($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
371 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak
$!;
372 print $fd '.svn',"\n";
373 close $fd or croak
$!;
376 sub assert_svn_wc_clean
{
378 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
379 my $lcr = svn_info
('.')->{'Last Changed Rev'};
380 if ($svn_rev != $lcr) {
381 print STDERR
"Checking for copy-tree ... ";
382 my @diff = grep(/^Index: /,(safe_qx
(qw(svn diff),
383 "-r$lcr:$svn_rev")));
385 croak
"Nope! Expected r$svn_rev, got r$lcr\n";
387 print STDERR
"OK!\n";
390 my @status = grep(!/^Performing status on external/,(`svn status`));
391 @status = grep(!/^\s*$/,@status);
392 if (scalar @status) {
393 print STDERR
"Tree ($SVN_WC) is not clean:\n";
394 print STDERR
$_ foreach @status;
401 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
402 chomp(my $type = `git-cat-file -t $treeish`);
404 while ($type eq 'tag') {
405 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
407 if ($type eq 'commit') {
408 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
409 ($expected) = ($expected =~ /^tree ($sha1)$/);
410 die "Unable to get tree from $treeish\n" unless $expected;
411 } elsif ($type eq 'tree') {
412 $expected = $treeish;
414 die "$treeish is a $type, expected tree, tag or commit\n";
417 my $old_index = $ENV{GIT_INDEX_FILE
};
418 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
420 unlink $tmpindex or croak
$!;
422 $ENV{GIT_INDEX_FILE
} = $tmpindex;
424 chomp(my $tree = `git-write-tree`);
426 $ENV{GIT_INDEX_FILE
} = $old_index;
428 delete $ENV{GIT_INDEX_FILE
};
430 if ($tree ne $expected) {
431 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
436 sub parse_diff_tree
{
442 chomp $_; # this gets rid of the trailing "\0"
443 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
444 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
445 push @mods, { mode_a
=> $1, mode_b
=> $2,
446 sha1_b
=> $3, chg
=> $4 };
447 if ($4 =~ /^(?:C|R)$/) {
452 } elsif ($state eq 'file_a') {
453 my $x = $mods[$#mods] or croak
"Empty array\n";
454 if ($x->{chg
} !~ /^(?:C|R)$/) {
455 croak
"Error parsing $_, $x->{chg}\n";
459 } elsif ($state eq 'file_b') {
460 my $x = $mods[$#mods] or croak
"Empty array\n";
461 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
462 croak
"Error parsing $_, $x->{chg}\n";
464 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
465 croak
"Error parsing $_, $x->{chg}\n";
470 croak
"Error parsing $_\n";
473 close $diff_fh or croak
$!;
478 sub svn_check_prop_executable
{
480 return if -l
$m->{file_b
};
481 if ($m->{mode_b
} =~ /755$/) {
482 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
483 if ($m->{mode_a
} !~ /755$/) {
484 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
486 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
487 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
488 sys
(qw(svn propdel svn:executable), $m->{file_b
});
489 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
490 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
494 sub svn_ensure_parent_path
{
495 my $dir_b = dirname
(shift);
496 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
497 mkpath
([$dir_b]) unless (-d
$dir_b);
498 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
501 sub precommit_check
{
503 my (%rm_file, %rmdir_check, %added_check);
505 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
506 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
507 if ($m->{chg
} eq 'R') {
508 if (-d
$m->{file_b
}) {
509 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
511 # dir/$file => dir/file/$file
512 my $dirname = dirname
($m->{file_b
});
513 while ($dirname ne File
::Spec
->curdir) {
514 if ($dirname ne $m->{file_a
}) {
515 $dirname = dirname
($dirname);
518 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
520 # baz/zzz => baz (baz is a file)
521 $dirname = dirname
($m->{file_a
});
522 while ($dirname ne File
::Spec
->curdir) {
523 if ($dirname ne $m->{file_b
}) {
524 $dirname = dirname
($dirname);
527 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
530 if ($m->{chg
} =~ /^(D|R)$/) {
531 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
532 $rm_file{ $m->{$t} } = 1;
533 my $dirname = dirname
( $m->{$t} );
534 my $basename = basename
( $m->{$t} );
535 $rmdir_check{$dirname}->{$basename} = 1;
536 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
537 if (-d
$m->{file_b
}) {
538 err_dir_to_file
($m->{file_b
});
540 my $dirname = dirname
( $m->{file_b
} );
541 my $basename = basename
( $m->{file_b
} );
542 $added_check{$dirname}->{$basename} = 1;
543 while ($dirname ne File
::Spec
->curdir) {
544 if ($rm_file{$dirname}) {
545 err_file_to_dir
($m->{file_b
});
547 $dirname = dirname
$dirname;
551 return (\
%rmdir_check, \
%added_check);
553 sub err_dir_to_file
{
555 print STDERR
"Node change from directory to file ",
556 "is not supported by Subversion: ",$file,"\n";
559 sub err_file_to_dir
{
561 print STDERR
"Node change from file to directory ",
562 "is not supported by Subversion: ",$file,"\n";
567 sub svn_checkout_tree
{
568 my ($svn_rev, $treeish) = @_;
569 my $from = file_to_s
("$REV_DIR/$svn_rev");
570 assert_svn_wc_clean
($svn_rev);
572 print "diff-tree $from $treeish\n";
573 my $pid = open my $diff_fh, '-|';
574 defined $pid or croak
$!;
576 my @diff_tree = qw(git-diff-tree -z -r -C);
577 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
578 push @diff_tree, "-l$_l" if defined $_l;
579 exec(@diff_tree, $from, $treeish) or croak
$!;
581 my $mods = parse_diff_tree
($diff_fh);
583 # git can do empty commits, but SVN doesn't allow it...
586 my ($rm, $add) = precommit_check
($mods);
588 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
589 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
590 if ($m->{chg
} eq 'C') {
591 svn_ensure_parent_path
( $m->{file_b
} );
592 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
593 apply_mod_line_blob
($m);
594 svn_check_prop_executable
($m);
595 } elsif ($m->{chg
} eq 'D') {
596 sys
(qw(svn rm --force), $m->{file_b
});
597 } elsif ($m->{chg
} eq 'R') {
598 svn_ensure_parent_path
( $m->{file_b
} );
599 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
600 apply_mod_line_blob
($m);
601 svn_check_prop_executable
($m);
602 } elsif ($m->{chg
} eq 'M') {
603 apply_mod_line_blob
($m);
604 svn_check_prop_executable
($m);
605 } elsif ($m->{chg
} eq 'T') {
606 sys
(qw(svn rm --force),$m->{file_b
});
607 apply_mod_line_blob
($m);
608 sys
(qw(svn add --force), $m->{file_b
});
609 svn_check_prop_executable
($m);
610 } elsif ($m->{chg
} eq 'A') {
611 svn_ensure_parent_path
( $m->{file_b
} );
612 apply_mod_line_blob
($m);
613 sys
(qw(svn add --force), $m->{file_b
});
614 svn_check_prop_executable
($m);
616 croak
"Invalid chg: $m->{chg}\n";
620 assert_tree
($treeish);
621 if ($_rmdir) { # remove empty directories
622 handle_rmdir
($rm, $add);
624 assert_tree
($treeish);
628 # svn ls doesn't work with respect to the current working tree, but what's
629 # in the repository. There's not even an option for it... *sigh*
630 # (added files don't show up and removed files remain in the ls listing)
632 my ($dir, $rm, $add) = @_;
633 chomp(my @ls = safe_qx
('svn','ls',$dir));
636 s
#/$##; # trailing slashes are evil
637 push @ret, $_ unless $rm->{$dir}->{$_};
639 if (exists $add->{$dir}) {
640 push @ret, keys %{$add->{$dir}};
648 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
649 my $ls = svn_ls_current
($dir, $rm, $add);
650 next if (scalar @
$ls);
651 sys
(qw(svn rm --force),$dir);
653 my $dn = dirname
$dir;
654 $rm->{ $dn }->{ basename
$dir } = 1;
655 $ls = svn_ls_current
($dn, $rm, $add);
656 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
657 sys
(qw(svn rm --force),$dn);
660 $rm->{ $dn }->{ $dir } = 1;
661 $ls = svn_ls_current
($dn, $rm, $add);
666 sub svn_commit_tree
{
667 my ($svn_rev, $commit) = @_;
668 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
669 my %log_msg = ( msg
=> '' );
670 open my $msg, '>', $commit_msg or croak
$!;
672 chomp(my $type = `git-cat-file -t $commit`);
673 if ($type eq 'commit') {
674 my $pid = open my $msg_fh, '-|';
675 defined $pid or croak
$!;
678 exec(qw(git-cat-file commit), $commit) or croak
$!;
683 $in_msg = 1 if (/^\s*$/);
684 } elsif (/^git-svn-id: /) {
685 # skip this, we regenerate the correct one
686 # on re-fetch anyways
688 print $msg $_ or croak
$!;
691 close $msg_fh or croak
$!;
693 close $msg or croak
$!;
695 if ($_edit || ($type eq 'tree')) {
696 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
697 system($editor, $commit_msg);
700 # file_to_s removes all trailing newlines, so just use chomp() here:
701 open $msg, '<', $commit_msg or croak
$!;
702 { local $/; chomp($log_msg{msg
} = <$msg>); }
703 close $msg or croak
$!;
705 my ($oneline) = ($log_msg{msg
} =~ /([^\n\r]+)/);
706 print "Committing $commit: $oneline\n";
708 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
709 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
711 defined $committed or croak
712 "Commit output failed to parse committed revision!\n",
713 join("\n",@ci_output),"\n";
714 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
716 my @svn_up = qw(svn up);
717 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
718 if ($rev_committed == ($svn_rev + 1)) {
719 push @svn_up, "-r$rev_committed";
721 my $info = svn_info
('.');
722 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
723 if ($info->{'Last Changed Rev'} != $rev_committed) {
724 croak
"$info->{'Last Changed Rev'} != $rev_committed\n"
726 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
727 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
728 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
729 or croak
"Failed to parse date: $date\n";
730 $log_msg{date
} = "$tz $Y-$m-$d $H:$M:$S";
731 $log_msg{author
} = $info->{'Last Changed Author'};
732 $log_msg{revision
} = $rev_committed;
733 $log_msg{msg
} .= "\n";
734 my $parent = file_to_s
("$REV_DIR/$svn_rev");
735 git_commit
(\
%log_msg, $parent, $commit);
736 return $rev_committed;
739 push @svn_up, "-r$svn_rev";
741 return fetch
("$rev_committed=$commit")->{revision
};
744 # read the entire log into a temporary file (which is removed ASAP)
745 # and store the file handle + parser state
748 my $log_fh = IO
::File
->new_tmpfile or croak
$!;
750 defined $pid or croak
$!;
752 open STDOUT
, '>&', $log_fh or croak
$!;
753 exec (qw(svn log), @log_args) or croak
$!
757 seek $log_fh, 0, 0 or croak
$!;
758 return { state => 'sep', fh
=> $log_fh };
762 my $log = shift; # retval of svn_log_raw()
769 if ($log->{state} eq 'msg') {
771 $ret->{msg
} .= $_."\n";
772 unless(--$ret->{lines
}) {
773 $log->{state} = 'sep';
776 croak
"Log parse error at: $_\n",
782 if ($log->{state} ne 'sep') {
783 croak
"Log parse error at: $_\n",
784 "state: $log->{state}\n",
788 $log->{state} = 'rev';
790 # if we have an empty log message, put something there:
792 $ret->{msg
} ||= "\n";
793 delete $ret->{lines
};
798 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
800 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
801 ($lines) = ($lines =~ /(\d+)/);
802 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
803 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
804 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
805 or croak
"Failed to parse date: $date\n";
806 $ret = { revision
=> $rev,
807 date
=> "$tz $Y-$m-$d $H:$M:$S",
811 if (defined $_authors && ! defined $users{$author}) {
812 die "Author: $author not defined in ",
815 $log->{state} = 'msg_start';
818 # skip the first blank line of the message:
819 if ($log->{state} eq 'msg_start' && /^$/) {
820 $log->{state} = 'msg';
821 } elsif ($log->{state} eq 'msg') {
823 $ret->{msg
} .= $_."\n";
824 unless (--$ret->{lines
}) {
825 $log->{state} = 'sep';
828 croak
"Log parse error at: $_\n",
829 $ret->{revision
},"\n";
837 my $url = shift || $SVN_URL;
839 my $pid = open my $info_fh, '-|';
840 defined $pid or croak
$!;
843 exec(qw(svn info),$url) or croak
$!;
847 # only single-lines seem to exist in svn info output
850 if (m
#^([^:]+)\s*:\s*(\S.*)$#) {
852 push @
{$ret->{-order
}}, $1;
855 close $info_fh or croak
$!;
859 sub sys
{ system(@_) == 0 or croak
$?
}
862 my ($from, $to) = @_;
863 my $es = safe_qx
(qw
/svn propget svn:eol-style/, $to);
864 open my $rfd, '<', $from or croak
$!;
865 binmode $rfd or croak
$!;
866 open my $wfd, '>', $to or croak
$!;
867 binmode $wfd or croak
$!;
869 my $eol = $EOL{$es} or undef;
871 print "$eol: $from => $to\n";
876 defined($r = sysread($rfd, $buf, 4096)) or croak
$!;
878 $buf =~ s/(?:\015|\012|\015\012)/$eol/gs if $eol;
879 for ($w = 0; $w < $r; $w += $t) {
880 $t = syswrite($wfd, $buf, $r - $w, $w) or croak
$!;
885 sub do_update_index
{
886 my ($z_cmd, $cmd, $no_text_base) = @_;
888 my $z = open my $p, '-|';
889 defined $z or croak
$!;
890 unless ($z) { exec @
$z_cmd or croak
$! }
892 my $pid = open my $ui, '|-';
893 defined $pid or croak
$!;
895 exec('git-update-index',"--$cmd",'-z','--stdin') or croak
$!;
898 while (my $x = <$p>) {
900 if (!$no_text_base && lstat $x && ! -l _
&&
901 safe_qx
(qw
/svn propget svn:keywords/,$x)) {
902 my $mode = -x _ ?
0755 : 0644;
903 my ($v,$d,$f) = File
::Spec
->splitpath($x);
904 my $tb = File
::Spec
->catfile($d, '.svn', 'tmp',
905 'text-base',"$f.svn-base");
908 $tb = File
::Spec
->catfile($d, '.svn',
909 'text-base',"$f.svn-base");
912 unlink $x or croak
$!;
914 chmod(($mode &~ umask), $x) or croak
$!;
918 close $ui or croak
$!;
922 my $no_text_base = shift;
923 do_update_index
([qw
/git-diff-files --name-only -z/],
926 do_update_index
([qw
/git-ls-files -z --others/,
927 "--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude"],
933 my ($str, $file, $mode) = @_;
934 open my $fd,'>',$file or croak
$!;
935 print $fd $str,"\n" or croak
$!;
936 close $fd or croak
$!;
937 chmod ($mode &~ umask, $file) if (defined $mode);
942 open my $fd,'<',$file or croak
"$!: file: $file\n";
945 close $fd or croak
$!;
950 sub assert_revision_unknown
{
952 if (-f
"$REV_DIR/$revno") {
953 croak
"$REV_DIR/$revno already exists! ",
954 "Why are we refetching it?";
960 my @x = safe_qx
('git-cat-file','commit',$x);
961 my @y = safe_qx
('git-cat-file','commit',$y);
962 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
963 || $y[0] !~ /^tree $sha1\n$/) {
964 print STDERR
"Trees not equal: $y[0] != $x[0]\n";
970 sub assert_revision_eq_or_unknown
{
971 my ($revno, $commit) = @_;
972 if (-f
"$REV_DIR/$revno") {
973 my $current = file_to_s
("$REV_DIR/$revno");
974 if (($commit ne $current) && !trees_eq
($commit, $current)) {
975 croak
"$REV_DIR/$revno already exists!\n",
976 "current: $current\nexpected: $commit\n";
983 my ($log_msg, @parents) = @_;
984 assert_revision_unknown
($log_msg->{revision
});
985 my $out_fh = IO
::File
->new_tmpfile or croak
$!;
987 map_tree_joins
() if (@_branch_from && !%tree_map);
989 # commit parents can be conditionally bound to a particular
990 # svn revision via: "svn_revno=commit_sha1", filter them out here:
992 foreach my $p (@parents) {
993 next unless defined $p;
994 if ($p =~ /^(\d+)=($sha1_short)$/o) {
995 if ($1 == $log_msg->{revision
}) {
996 push @exec_parents, $2;
999 push @exec_parents, $p if $p =~ /$sha1_short/o;
1004 defined $pid or croak
$!;
1006 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
1008 chomp(my $tree = `git-write-tree`);
1010 if (exists $tree_map{$tree}) {
1011 my %seen_parent = map { $_ => 1 } @exec_parents;
1012 foreach (@
{$tree_map{$tree}}) {
1013 # MAXPARENT is defined to 16 in commit-tree.c:
1014 if ($seen_parent{$_} || @exec_parents > 16) {
1017 push @exec_parents, $_;
1018 $seen_parent{$_} = 1;
1021 my $msg_fh = IO
::File
->new_tmpfile or croak
$!;
1022 print $msg_fh $log_msg->{msg
}, "\ngit-svn-id: ",
1023 "$SVN_URL\@$log_msg->{revision}",
1024 " $SVN_UUID\n" or croak
$!;
1025 $msg_fh->flush == 0 or croak
$!;
1026 seek $msg_fh, 0, 0 or croak
$!;
1028 set_commit_env
($log_msg);
1030 my @exec = ('git-commit-tree',$tree);
1031 push @exec, '-p', $_ foreach @exec_parents;
1032 open STDIN
, '<&', $msg_fh or croak
$!;
1033 open STDOUT
, '>&', $out_fh or croak
$!;
1034 exec @exec or croak
$!;
1039 $out_fh->flush == 0 or croak
$!;
1040 seek $out_fh, 0, 0 or croak
$!;
1041 chomp(my $commit = do { local $/; <$out_fh> });
1042 if ($commit !~ /^$sha1$/o) {
1043 croak
"Failed to commit, invalid sha1: $commit\n";
1045 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1046 if (my $primary_parent = shift @exec_parents) {
1048 defined $pid or croak
$!;
1052 exec 'git-rev-parse','--verify',
1053 "refs/remotes/$GIT_SVN^0";
1056 push @update_ref, $primary_parent unless $?
;
1059 sys
('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
1060 print "r$log_msg->{revision} = $commit\n";
1064 sub set_commit_env
{
1066 my $author = $log_msg->{author
};
1067 my ($name,$email) = defined $users{$author} ? @
{$users{$author}}
1068 : ($author,"$author\@$SVN_UUID");
1069 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} = $name;
1070 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} = $email;
1071 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} = $log_msg->{date
};
1074 sub apply_mod_line_blob
{
1076 if ($m->{mode_b
} =~ /^120/) {
1077 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
1079 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
1083 sub blob_to_symlink
{
1084 my ($blob, $link) = @_;
1085 defined $link or croak
"\$link not defined!\n";
1086 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
1087 if (-l
$link || -f _
) {
1088 unlink $link or croak
$!;
1091 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1092 symlink $dest, $link or croak
$!;
1096 my ($blob, $file) = @_;
1097 defined $file or croak
"\$file not defined!\n";
1098 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
1099 if (-l
$file || -f _
) {
1100 unlink $file or croak
$!;
1103 open my $blob_fh, '>', $file or croak
"$!: $file\n";
1105 defined $pid or croak
$!;
1108 open STDOUT
, '>&', $blob_fh or croak
$!;
1109 exec('git-cat-file','blob',$blob);
1114 close $blob_fh or croak
$!;
1118 my $pid = open my $child, '-|';
1119 defined $pid or croak
$!;
1121 exec(@_) or croak
$?
;
1123 my @ret = (<$child>);
1124 close $child or croak
$?
;
1125 die $?
if $?
; # just in case close didn't error out
1126 return wantarray ?
@ret : join('',@ret);
1129 sub svn_compat_check
{
1130 my @co_help = safe_qx
(qw(svn co -h));
1131 unless (grep /ignore-externals/,@co_help) {
1132 print STDERR
"W: Installed svn version does not support ",
1133 "--ignore-externals\n";
1134 $_no_ignore_ext = 1;
1136 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1137 $_svn_co_url_revs = 1;
1140 # I really, really hope nobody hits this...
1141 unless (grep /stop-on-copy/, (safe_qx
(qw(svn log -h)))) {
1143 W
: The installed svn version does
not support the
--stop
-on
-copy flag
in
1145 Lets hope the directory you
're tracking is not a branch or tag
1146 and was never moved within the repository...
1152 # *sigh*, new versions of svn won't honor
-r
<rev
> without URL@
<rev
>,
1153 # (and they won't honor URL@<rev> without -r<rev>, too!)
1154 sub svn_cmd_checkout
{
1155 my ($url, $rev, $dir) = @_;
1156 my @cmd = ('svn','co', "-r$rev");
1157 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1158 $url .= "\@$rev" if $_svn_co_url_revs;
1159 sys
(@cmd, $url, $dir);
1162 sub check_upgrade_needed
{
1164 my $pid = open my $child, '-|';
1165 defined $pid or croak
$!;
1168 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak
$?
;
1170 my @ret = (<$child>);
1171 close $child or croak
$?
;
1172 die $?
if $?
; # just in case close didn't error out
1173 return wantarray ?
@ret : join('',@ret);
1176 my $head = eval { safe_qx
('git-rev-parse',"refs/remotes/$GIT_SVN") };
1178 print STDERR
"Please run: $0 rebuild --upgrade\n";
1183 # fills %tree_map with a reverse mapping of trees to commits. Useful
1184 # for finding parents to commit on.
1185 sub map_tree_joins
{
1186 foreach my $br (@_branch_from) {
1187 my $pid = open my $pipe, '-|';
1188 defined $pid or croak
$!;
1190 exec(qw(git-rev-list --pretty=raw), $br) or croak
$?
;
1193 if (/^commit ($sha1)$/o) {
1195 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1196 unless (defined $tree) {
1197 die "Failed to parse commit $commit\n";
1199 push @
{$tree_map{$tree}}, $commit;
1202 close $pipe or croak
$?
;
1206 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
1208 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1209 while (<$authors>) {
1211 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1212 my ($user, $name, $email) = ($1, $2, $3);
1213 $users{$user} = [$name, $email];
1215 close $authors or croak
$!;
1222 $svn_log hashref
(as returned by svn_log_raw
)
1224 fh
=> file handle of the
log file
,
1225 state => state of the
log file parser
(sep
/msg/rev
/msg_start
...)
1228 $log_msg hashref as returned by next_log_entry
($svn_log)
1230 msg
=> 'whitespace-formatted log entry
1231 ', # trailing newline is preserved
1232 revision
=> '8', # integer
1233 date
=> '2004-02-24T17:01:44.108345Z', # commit date
1234 author
=> 'committer name'
1238 @mods = array of diff
-index line hashes
, each element represents one line
1239 of diff
-index output
1241 diff
-index line
($m hash
)
1243 mode_a
=> first column of diff
-index output
, no leading
':',
1244 mode_b
=> second column of diff
-index output
,
1245 sha1_b
=> sha1sum of the final blob
,
1246 chg
=> change type
[MCRADT
],
1247 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
1248 file_b
=> new
/current file name of a file
(any chg
)