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
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
29 use File
::Basename qw
/dirname basename/;
30 use File
::Path qw
/mkpath/;
31 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
33 my $sha1 = qr/[a-f\d]{40}/;
34 my $sha1_short = qr/[a-f\d]{6,40}/;
35 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
36 $_find_copies_harder, $_l, $_version);
38 GetOptions
( 'revision|r=s' => \
$_revision,
39 'no-ignore-externals' => \
$_no_ignore_ext,
43 'help|H|h' => \
$_help,
44 'find-copies-harder' => \
$_find_copies_harder,
46 'version|V' => \
$_version,
47 'no-stop-on-copy' => \
$_no_stop_copy );
49 fetch
=> [ \
&fetch
, "Download new revisions from SVN" ],
50 init
=> [ \
&init
, "Initialize and fetch (import)"],
51 commit
=> [ \
&commit
, "Commit git revisions to SVN" ],
52 'show-ignore' => [ \
&show_ignore
, "Show svn:ignore listings" ],
53 rebuild
=> [ \
&rebuild
, "Rebuild git-svn metadata (after git clone)" ],
54 help
=> [ \
&usage
, "Show help" ],
57 for (my $i = 0; $i < @ARGV; $i++) {
58 if (defined $cmd{$ARGV[$i]}) {
65 # we may be called as git-svn-(command), or git-svn(command).
67 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
73 version
() if $_version;
74 usage
(1) unless (defined $cmd);
75 svn_check_ignore_externals
();
76 $cmd{$cmd}->[0]->(@ARGV);
79 ####################### primary functions ######################
81 my $exit = shift || 0;
82 my $fd = $exit ? \
*STDERR
: \
*STDOUT
;
84 git
-svn
- bidirectional operations between a single Subversion tree
and git
85 Usage
: $0 <command
> [options
] [arguments
]\n
88 foreach (sort keys %cmd) {
89 print $fd ' ',pack('A10',$_),$cmd{$_}->[1],"\n";
92 \nGIT_SVN_ID may be set
in the environment to an arbitrary identifier
if
93 you
're tracking multiple SVN branches/repositories in one git repository
94 and want to keep them separate.
100 print "git-svn version $VERSION\n";
105 $SVN_URL = shift or undef;
109 my $pid = open(my $rev_list,'-|');
110 defined $pid or croak $!;
112 exec("git-rev-list","$GIT_SVN-HEAD") or croak $!;
115 while (<$rev_list>) {
118 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
119 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
120 next if (!@commit); # skip merges
121 my $id = $commit[$#commit];
122 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
124 if (!$rev || !$uuid || !$url) {
125 # some of the original repositories I made had
126 # indentifiers like this:
127 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
129 if (!$rev || !$uuid) {
130 croak "Unable to extract revision or UUID from ",
134 print "r$rev = $c\n";
135 unless (defined $first) {
136 if (!$SVN_URL && !$url) {
137 croak "SVN repository location required: $url\n";
140 $repo_uuid = setup_git_svn();
143 if ($uuid ne $repo_uuid) {
144 croak "Repository UUIDs do not match!\ngot: $uuid\n",
145 "expected: $repo_uuid\n";
147 assert_revision_eq_or_unknown($rev, $c);
148 sys('git
-update
-ref',"$GIT_SVN/revs/$rev",$c);
149 $newest_rev = $rev if ($rev > $newest_rev);
151 close $rev_list or croak $?;
152 if (!chdir $SVN_WC) {
153 my @svn_co = ('svn
','co
',"-r$first");
154 push @svn_co, '--ignore
-externals
' unless $_no_ignore_ext;
155 sys(@svn_co, $SVN_URL, $SVN_WC);
156 chdir $SVN_WC or croak $!;
160 defined $pid or croak $!;
162 my @svn_up = qw(svn up);
163 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
164 sys
(@svn_up,"-r$newest_rev");
165 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
167 exec('git-write-tree');
173 $SVN_URL = shift or croak
"SVN repository location required\n";
174 unless (-d
$GIT_DIR) {
182 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
183 my @log_args = -d
$SVN_WC ?
($SVN_WC) : ($SVN_URL);
184 unless ($_revision) {
185 $_revision = -d
$SVN_WC ?
'BASE:HEAD' : '0:HEAD';
187 push @log_args, "-r$_revision";
188 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
190 my $svn_log = svn_log_raw
(@log_args);
191 @
$svn_log = sort { $a->{revision
} <=> $b->{revision
} } @
$svn_log;
193 my $base = shift @
$svn_log or croak
"No base revision!\n";
194 my $last_commit = undef;
195 unless (-d
$SVN_WC) {
196 my @svn_co = ('svn','co',"-r$base->{revision}");
197 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
198 sys
(@svn_co, $SVN_URL, $SVN_WC);
199 chdir $SVN_WC or croak
$!;
200 $last_commit = git_commit
($base, @parents);
201 unless (-f
"$GIT_DIR/refs/heads/master") {
202 sys
(qw(git-update-ref refs/heads/master),$last_commit);
204 assert_svn_wc_clean
($base->{revision
}, $last_commit);
206 chdir $SVN_WC or croak
$!;
207 $last_commit = file_to_s
("$REV_DIR/$base->{revision}");
209 my @svn_up = qw(svn up);
210 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
211 my $last_rev = $base->{revision
};
212 foreach my $log_msg (@
$svn_log) {
213 assert_svn_wc_clean
($last_rev, $last_commit);
214 $last_rev = $log_msg->{revision
};
215 sys
(@svn_up,"-r$last_rev");
216 $last_commit = git_commit
($log_msg, $last_commit, @parents);
218 assert_svn_wc_clean
($last_rev, $last_commit);
219 return pop @
$svn_log;
224 if ($_stdin || !@commits) {
225 print "Reading from stdin...\n";
228 if (/\b([a-f\d]{6,40})\b/) {
229 unshift @commits, $1;
234 foreach my $c (@commits) {
235 chomp(my @tmp = safe_qx
('git-rev-parse',$c));
236 if (scalar @tmp == 1) {
238 } elsif (scalar @tmp > 1) {
239 push @revs, reverse (safe_qx
('git-rev-list',@tmp));
241 die "Failed to rev-parse $c\n";
247 chdir $SVN_WC or croak
$!;
248 my $svn_current_rev = svn_info
('.')->{'Last Changed Rev'};
249 foreach my $c (@revs) {
250 print "Committing $c\n";
251 my $mods = svn_checkout_tree
($svn_current_rev, $c);
252 if (scalar @
$mods == 0) {
253 print "Skipping, no changes detected\n";
256 $svn_current_rev = svn_commit_tree
($svn_current_rev, $c);
258 print "Done committing ",scalar @revs," revisions to SVN\n";
263 require File
::Find
or die $!;
264 my $exclude_file = "$GIT_DIR/info/exclude";
265 open my $fh, '<', $exclude_file or croak
$!;
266 chomp(my @excludes = (<$fh>));
267 close $fh or croak
$!;
269 $SVN_URL ||= file_to_s
("$GIT_DIR/$GIT_SVN/info/url");
270 chdir $SVN_WC or croak
$!;
272 File
::Find
::find
({wanted
=>sub{if(lstat $_ && -d _
&& -d
"$_/.svn"){
274 @
{$ign{$_}} = safe_qx
(qw(svn propget svn:ignore),$_);
275 }}, no_chdir
=>1},'.');
278 foreach (@
{$ign{'.'}}) { print '/',$_ if /\S
/ }
280 foreach my $i (sort keys %ign) {
281 print "\n# ",$i,"\n";
282 foreach (@
{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
286 ########################### utility functions #########################
289 defined $SVN_URL or croak
"SVN repository location required\n";
290 unless (-d
$GIT_DIR) {
291 croak
"GIT_DIR=$GIT_DIR does not exist!\n";
293 mkpath
(["$GIT_DIR/$GIT_SVN"]);
294 mkpath
(["$GIT_DIR/$GIT_SVN/info"]);
296 s_to_file
($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
297 my $uuid = svn_info
($SVN_URL)->{'Repository UUID'} or
298 croak
"Repository UUID unreadable\n";
299 s_to_file
($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
301 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak
$!;
302 print $fd '.svn',"\n";
303 close $fd or croak
$!;
307 sub assert_svn_wc_clean
{
308 my ($svn_rev, $treeish) = @_;
309 croak
"$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
310 croak
"$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
311 my $svn_info = svn_info
('.');
312 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
313 croak
"Expected r$svn_rev, got r",
314 $svn_info->{'Last Changed Rev'},"\n";
316 my @status = grep(!/^Performing status on external/,(`svn status`));
317 @status = grep(!/^\s*$/,@status);
318 if (scalar @status) {
319 print STDERR
"Tree ($SVN_WC) is not clean:\n";
320 print STDERR
$_ foreach @status;
323 assert_tree
($treeish);
328 croak
"Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
329 chomp(my $type = `git-cat-file -t $treeish`);
331 while ($type eq 'tag') {
332 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
334 if ($type eq 'commit') {
335 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
336 ($expected) = ($expected =~ /^tree ($sha1)$/);
337 die "Unable to get tree from $treeish\n" unless $expected;
338 } elsif ($type eq 'tree') {
339 $expected = $treeish;
341 die "$treeish is a $type, expected tree, tag or commit\n";
344 my $old_index = $ENV{GIT_INDEX_FILE
};
345 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
347 unlink $tmpindex or croak
$!;
349 $ENV{GIT_INDEX_FILE
} = $tmpindex;
351 chomp(my $tree = `git-write-tree`);
353 $ENV{GIT_INDEX_FILE
} = $old_index;
355 delete $ENV{GIT_INDEX_FILE
};
357 if ($tree ne $expected) {
358 croak
"Tree mismatch, Got: $tree, Expected: $expected\n";
362 sub parse_diff_tree
{
368 chomp $_; # this gets rid of the trailing "\0"
369 if ($state eq 'meta' && /^:(\d
{6})\s
(\d
{6})\s
370 $sha1\s
($sha1)\s
([MTCRAD
])\d
*$/xo
) {
371 push @mods, { mode_a
=> $1, mode_b
=> $2,
372 sha1_b
=> $3, chg
=> $4 };
373 if ($4 =~ /^(?:C|R)$/) {
378 } elsif ($state eq 'file_a') {
379 my $x = $mods[$#mods] or croak
"Empty array\n";
380 if ($x->{chg
} !~ /^(?:C|R)$/) {
381 croak
"Error parsing $_, $x->{chg}\n";
385 } elsif ($state eq 'file_b') {
386 my $x = $mods[$#mods] or croak
"Empty array\n";
387 if (exists $x->{file_a
} && $x->{chg
} !~ /^(?:C|R)$/) {
388 croak
"Error parsing $_, $x->{chg}\n";
390 if (!exists $x->{file_a
} && $x->{chg
} =~ /^(?:C|R)$/) {
391 croak
"Error parsing $_, $x->{chg}\n";
396 croak
"Error parsing $_\n";
399 close $diff_fh or croak
$!;
404 sub svn_check_prop_executable
{
406 return if -l
$m->{file_b
};
407 if ($m->{mode_b
} =~ /755$/) {
408 chmod((0755 &~ umask),$m->{file_b
}) or croak
$!;
409 if ($m->{mode_a
} !~ /755$/) {
410 sys
(qw(svn propset svn:executable 1), $m->{file_b
});
412 -x
$m->{file_b
} or croak
"$m->{file_b} is not executable!\n";
413 } elsif ($m->{mode_b
} !~ /755$/ && $m->{mode_a
} =~ /755$/) {
414 sys
(qw(svn propdel svn:executable), $m->{file_b
});
415 chmod((0644 &~ umask),$m->{file_b
}) or croak
$!;
416 -x
$m->{file_b
} and croak
"$m->{file_b} is executable!\n";
420 sub svn_ensure_parent_path
{
421 my $dir_b = dirname
(shift);
422 svn_ensure_parent_path
($dir_b) if ($dir_b ne File
::Spec
->curdir);
423 mkpath
([$dir_b]) unless (-d
$dir_b);
424 sys
(qw(svn add -N), $dir_b) unless (-d
"$dir_b/.svn");
427 sub precommit_check
{
429 my (%rm_file, %rmdir_check, %added_check);
431 my %o = ( D
=> 0, R
=> 1, C
=> 2, A
=> 3, M
=> 3, T
=> 3 );
432 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
433 if ($m->{chg
} eq 'R') {
434 if (-d
$m->{file_b
}) {
435 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
437 # dir/$file => dir/file/$file
438 my $dirname = dirname
($m->{file_b
});
439 while ($dirname ne File
::Spec
->curdir) {
440 if ($dirname ne $m->{file_a
}) {
441 $dirname = dirname
($dirname);
444 err_file_to_dir
("$m->{file_a} => $m->{file_b}");
446 # baz/zzz => baz (baz is a file)
447 $dirname = dirname
($m->{file_a
});
448 while ($dirname ne File
::Spec
->curdir) {
449 if ($dirname ne $m->{file_b
}) {
450 $dirname = dirname
($dirname);
453 err_dir_to_file
("$m->{file_a} => $m->{file_b}");
456 if ($m->{chg
} =~ /^(D|R)$/) {
457 my $t = $1 eq 'D' ?
'file_b' : 'file_a';
458 $rm_file{ $m->{$t} } = 1;
459 my $dirname = dirname
( $m->{$t} );
460 my $basename = basename
( $m->{$t} );
461 $rmdir_check{$dirname}->{$basename} = 1;
462 } elsif ($m->{chg
} =~ /^(?:A|C)$/) {
463 if (-d
$m->{file_b
}) {
464 err_dir_to_file
($m->{file_b
});
466 my $dirname = dirname
( $m->{file_b
} );
467 my $basename = basename
( $m->{file_b
} );
468 $added_check{$dirname}->{$basename} = 1;
469 while ($dirname ne File
::Spec
->curdir) {
470 if ($rm_file{$dirname}) {
471 err_file_to_dir
($m->{file_b
});
473 $dirname = dirname
$dirname;
477 return (\
%rmdir_check, \
%added_check);
479 sub err_dir_to_file
{
481 print STDERR
"Node change from directory to file ",
482 "is not supported by Subversion: ",$file,"\n";
485 sub err_file_to_dir
{
487 print STDERR
"Node change from file to directory ",
488 "is not supported by Subversion: ",$file,"\n";
493 sub svn_checkout_tree
{
494 my ($svn_rev, $treeish) = @_;
495 my $from = file_to_s
("$REV_DIR/$svn_rev");
496 assert_svn_wc_clean
($svn_rev,$from);
497 print "diff-tree '$from' '$treeish'\n";
498 my $pid = open my $diff_fh, '-|';
499 defined $pid or croak
$!;
501 my @diff_tree = qw(git-diff-tree -z -r -C);
502 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
503 push @diff_tree, "-l$_l" if defined $_l;
504 exec(@diff_tree, $from, $treeish) or croak
$!;
506 my $mods = parse_diff_tree
($diff_fh);
508 # git can do empty commits, SVN doesn't allow it...
511 my ($rm, $add) = precommit_check
($mods);
513 my %o = ( D
=> 1, R
=> 0, C
=> -1, A
=> 3, M
=> 3, T
=> 3 );
514 foreach my $m (sort { $o{$a->{chg
}} <=> $o{$b->{chg
}} } @
$mods) {
515 if ($m->{chg
} eq 'C') {
516 svn_ensure_parent_path
( $m->{file_b
} );
517 sys
(qw(svn cp), $m->{file_a
}, $m->{file_b
});
518 apply_mod_line_blob
($m);
519 svn_check_prop_executable
($m);
520 } elsif ($m->{chg
} eq 'D') {
521 sys
(qw(svn rm --force), $m->{file_b
});
522 } elsif ($m->{chg
} eq 'R') {
523 svn_ensure_parent_path
( $m->{file_b
} );
524 sys
(qw(svn mv --force), $m->{file_a
}, $m->{file_b
});
525 apply_mod_line_blob
($m);
526 svn_check_prop_executable
($m);
527 } elsif ($m->{chg
} eq 'M') {
528 apply_mod_line_blob
($m);
529 svn_check_prop_executable
($m);
530 } elsif ($m->{chg
} eq 'T') {
531 sys
(qw(svn rm --force),$m->{file_b
});
532 apply_mod_line_blob
($m);
533 sys
(qw(svn add --force), $m->{file_b
});
534 svn_check_prop_executable
($m);
535 } elsif ($m->{chg
} eq 'A') {
536 svn_ensure_parent_path
( $m->{file_b
} );
537 apply_mod_line_blob
($m);
538 sys
(qw(svn add --force), $m->{file_b
});
539 svn_check_prop_executable
($m);
541 croak
"Invalid chg: $m->{chg}\n";
545 assert_tree
($treeish);
546 if ($_rmdir) { # remove empty directories
547 handle_rmdir
($rm, $add);
549 assert_tree
($treeish);
553 # svn ls doesn't work with respect to the current working tree, but what's
554 # in the repository. There's not even an option for it... *sigh*
555 # (added files don't show up and removed files remain in the ls listing)
557 my ($dir, $rm, $add) = @_;
558 chomp(my @ls = safe_qx
('svn','ls',$dir));
561 s
#/$##; # trailing slashes are evil
562 push @ret, $_ unless $rm->{$dir}->{$_};
564 if (exists $add->{$dir}) {
565 push @ret, keys %{$add->{$dir}};
573 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
574 my $ls = svn_ls_current
($dir, $rm, $add);
575 next if (scalar @
$ls);
576 sys
(qw(svn rm --force),$dir);
578 my $dn = dirname
$dir;
579 $rm->{ $dn }->{ basename
$dir } = 1;
580 $ls = svn_ls_current
($dn, $rm, $add);
581 while (scalar @
$ls == 0 && $dn ne File
::Spec
->curdir) {
582 sys
(qw(svn rm --force),$dn);
585 $rm->{ $dn }->{ $dir } = 1;
586 $ls = svn_ls_current
($dn, $rm, $add);
591 sub svn_commit_tree
{
592 my ($svn_rev, $commit) = @_;
593 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
594 open my $msg, '>', $commit_msg or croak
$!;
596 chomp(my $type = `git-cat-file -t $commit`);
597 if ($type eq 'commit') {
598 my $pid = open my $msg_fh, '-|';
599 defined $pid or croak
$!;
602 exec(qw(git-cat-file commit), $commit) or croak
$!;
607 $in_msg = 1 if (/^\s*$/);
609 print $msg $_ or croak
$!;
612 close $msg_fh or croak
$!;
614 close $msg or croak
$!;
616 if ($_edit || ($type eq 'tree')) {
617 my $editor = $ENV{VISUAL
} || $ENV{EDITOR
} || 'vi';
618 system($editor, $commit_msg);
620 my @ci_output = safe_qx
(qw(svn commit -F),$commit_msg);
621 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
623 defined $committed or croak
624 "Commit output failed to parse committed revision!\n",
625 join("\n",@ci_output),"\n";
626 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
629 my @svn_up = (qw(svn up), "-r$svn_rev");
630 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
632 return fetch
("$rev_committed=$commit")->{revision
};
637 my $pid = open my $log_fh,'-|';
638 defined $pid or croak
$!;
641 exec (qw(svn log), @log_args) or croak
$!
649 if ($state eq 'msg') {
650 if ($svn_log[$#svn_log]->{lines
}) {
651 $svn_log[$#svn_log]->{msg
} .= $_."\n";
652 unless(--$svn_log[$#svn_log]->{lines
}) {
656 croak
"Log parse error at: $_\n",
657 $svn_log[$#svn_log]->{revision
},
662 if ($state ne 'sep') {
663 croak
"Log parse error at: $_\n",
665 $svn_log[$#svn_log]->{revision
},
670 # if we have an empty log message, put something there:
672 $svn_log[$#svn_log]->{msg
} ||= "\n";
673 delete $svn_log[$#svn_log]->{lines
};
677 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
679 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
680 ($lines) = ($lines =~ /(\d+)/);
681 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
682 /(\d
{4})\
-(\d\d
)\
-(\d\d
)\s
683 (\d\d
)\
:(\d\d
)\
:(\d\d
)\s
([\
-\
+]\d
+)/x
)
684 or croak
"Failed to parse date: $date\n";
685 my %log_msg = ( revision
=> $rev,
686 date
=> "$tz $Y-$m-$d $H:$M:$S",
690 push @svn_log, \
%log_msg;
691 $state = 'msg_start';
694 # skip the first blank line of the message:
695 if ($state eq 'msg_start' && /^$/) {
697 } elsif ($state eq 'msg') {
698 if ($svn_log[$#svn_log]->{lines
}) {
699 $svn_log[$#svn_log]->{msg
} .= $_."\n";
700 unless (--$svn_log[$#svn_log]->{lines
}) {
704 croak
"Log parse error at: $_\n",
705 $svn_log[$#svn_log]->{revision
},"\n";
709 close $log_fh or croak
$?
;
714 my $url = shift || $SVN_URL;
716 my $pid = open my $info_fh, '-|';
717 defined $pid or croak
$!;
720 exec(qw(svn info),$url) or croak
$!;
724 # only single-lines seem to exist in svn info output
727 if (m
#^([^:]+)\s*:\s*(\S*)$#) {
729 push @
{$ret->{-order
}}, $1;
732 close $info_fh or croak
$!;
736 sub sys
{ system(@_) == 0 or croak
$?
}
739 system( "git-diff-files --name-only -z ".
740 " | git-update-index --remove -z --stdin && ".
741 "git-ls-files -z --others ".
742 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
743 " | git-update-index --add -z --stdin"
748 my ($str, $file, $mode) = @_;
749 open my $fd,'>',$file or croak
$!;
750 print $fd $str,"\n" or croak
$!;
751 close $fd or croak
$!;
752 chmod ($mode &~ umask, $file) if (defined $mode);
757 open my $fd,'<',$file or croak
"$!: file: $file\n";
760 close $fd or croak
$!;
765 sub assert_revision_unknown
{
767 if (-f
"$REV_DIR/$revno") {
768 croak
"$REV_DIR/$revno already exists! ",
769 "Why are we refetching it?";
773 sub assert_revision_eq_or_unknown
{
774 my ($revno, $commit) = @_;
775 if (-f
"$REV_DIR/$revno") {
776 my $current = file_to_s
("$REV_DIR/$revno");
777 if ($commit ne $current) {
778 croak
"$REV_DIR/$revno already exists!\n",
779 "current: $current\nexpected: $commit\n";
786 my ($log_msg, @parents) = @_;
787 assert_revision_unknown
($log_msg->{revision
});
788 my $out_fh = IO
::File
->new_tmpfile or croak
$!;
789 my $info = svn_info
('.');
790 my $uuid = $info->{'Repository UUID'};
791 defined $uuid or croak
"Unable to get Repository UUID\n";
793 # commit parents can be conditionally bound to a particular
794 # svn revision via: "svn_revno=commit_sha1", filter them out here:
796 foreach my $p (@parents) {
797 next unless defined $p;
798 if ($p =~ /^(\d+)=($sha1_short)$/o) {
799 if ($1 == $log_msg->{revision
}) {
800 push @exec_parents, $2;
803 push @exec_parents, $p if $p =~ /$sha1_short/o;
808 defined $pid or croak
$!;
810 $ENV{GIT_INDEX_FILE
} = $GIT_SVN_INDEX;
812 chomp(my $tree = `git-write-tree`);
814 my $msg_fh = IO
::File
->new_tmpfile or croak
$!;
815 print $msg_fh $log_msg->{msg
}, "\ngit-svn-id: ",
816 "$SVN_URL\@$log_msg->{revision}",
817 " $uuid\n" or croak
$!;
818 $msg_fh->flush == 0 or croak
$!;
819 seek $msg_fh, 0, 0 or croak
$!;
821 $ENV{GIT_AUTHOR_NAME
} = $ENV{GIT_COMMITTER_NAME
} =
823 $ENV{GIT_AUTHOR_EMAIL
} = $ENV{GIT_COMMITTER_EMAIL
} =
824 $log_msg->{author
}."\@$uuid";
825 $ENV{GIT_AUTHOR_DATE
} = $ENV{GIT_COMMITTER_DATE
} =
827 my @exec = ('git-commit-tree',$tree);
828 push @exec, '-p', $_ foreach @exec_parents;
829 open STDIN
, '<&', $msg_fh or croak
$!;
830 open STDOUT
, '>&', $out_fh or croak
$!;
831 exec @exec or croak
$!;
836 $out_fh->flush == 0 or croak
$!;
837 seek $out_fh, 0, 0 or croak
$!;
838 chomp(my $commit = do { local $/; <$out_fh> });
839 if ($commit !~ /^$sha1$/o) {
840 croak
"Failed to commit, invalid sha1: $commit\n";
842 my @update_ref = ('git-update-ref',"refs/heads/$GIT_SVN-HEAD",$commit);
843 if (my $primary_parent = shift @exec_parents) {
844 push @update_ref, $primary_parent;
847 sys
('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
848 print "r$log_msg->{revision} = $commit\n";
852 sub apply_mod_line_blob
{
854 if ($m->{mode_b
} =~ /^120/) {
855 blob_to_symlink
($m->{sha1_b
}, $m->{file_b
});
857 blob_to_file
($m->{sha1_b
}, $m->{file_b
});
861 sub blob_to_symlink
{
862 my ($blob, $link) = @_;
863 defined $link or croak
"\$link not defined!\n";
864 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
865 if (-l
$link || -f _
) {
866 unlink $link or croak
$!;
869 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
870 symlink $dest, $link or croak
$!;
874 my ($blob, $file) = @_;
875 defined $file or croak
"\$file not defined!\n";
876 croak
"Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
877 if (-l
$file || -f _
) {
878 unlink $file or croak
$!;
881 open my $blob_fh, '>', $file or croak
"$!: $file\n";
883 defined $pid or croak
$!;
886 open STDOUT
, '>&', $blob_fh or croak
$!;
887 exec('git-cat-file','blob',$blob);
892 close $blob_fh or croak
$!;
896 my $pid = open my $child, '-|';
897 defined $pid or croak
$!;
899 exec(@_) or croak
$?
;
901 my @ret = (<$child>);
902 close $child or croak
$?
;
903 die $?
if $?
; # just in case close didn't error out
904 return wantarray ?
@ret : join('',@ret);
907 sub svn_check_ignore_externals
{
908 return if $_no_ignore_ext;
909 unless (grep /ignore-externals/,(safe_qx
(qw(svn co -h)))) {
910 print STDERR
"W: Installed svn version does not support ",
911 "--ignore-externals\n";
919 @svn_log = array of log_msg hashes
923 msg
=> 'whitespace-formatted log entry
924 ', # trailing newline is preserved
925 revision
=> '8', # integer
926 date
=> '2004-02-24T17:01:44.108345Z', # commit date
927 author
=> 'committer name'
931 @mods = array of diff
-index line hashes
, each element represents one line
934 diff
-index line
($m hash
)
936 mode_a
=> first column of diff
-index output
, no leading
':',
937 mode_b
=> second column of diff
-index output
,
938 sha1_b
=> sha1sum of the final blob
,
939 chg
=> change type
[MCRAD
],
940 file_a
=> original file name of a file
(iff chg is
'C' or 'R')
941 file_b
=> new
/current file name of a file
(any chg
)