contrib/git-svn: add -b/--branch switch for branch detection
I've said I don't like branches in Subversion, and I still don't. This is a bit more flexible, though, as the argument for -b is any arbitrary git head/tag reference. This makes some things easier: * Importing git history into a brand new SVN branch. * Tracking multiple SVN branches via GIT_SVN_ID, even from multiple repositories. * Adding tags from SVN (still need to use GIT_SVN_ID, though). * Even merge tracking is supported, if and only the heads end up with 100% equivalent tree objects. This is more stricter but more robust and foolproof than parsing commit messages, imho. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Mar 3, 2006 at 01:20 UTC
69f0d91e49dbf3c3942bae125c07ad17f6027f83
2 files changed
+54
-1
contrib/git-svn/git-svn.perl
+38
@@ -35,6 +35,7 @@ my $sha1 = qr/[a-f\d]{40}/;
35
my $sha1_short = qr/[a-f\d]{6,40}/;
36
my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
37
$_find_copies_harder, $_l, $_version, $_upgrade);
38
+my (@_branch_from, %tree_map);
39
40
GetOptions( 'revision|r=s' => \$_revision,
41
'no-ignore-externals' => \$_no_ignore_ext,
@@ -43,6 +44,7 @@ GetOptions( 'revision|r=s' => \$_revision,
44
'rmdir' => \$_rmdir,
45
'upgrade' => \$_upgrade,
46
'help|H|h' => \$_help,
47
+ 'branch|b=s' => \@_branch_from,
48
'find-copies-harder' => \$_find_copies_harder,
49
'l=i' => \$_l,
50
'version|V' => \$_version,
@@ -831,6 +833,8 @@ sub git_commit {
833
my $uuid = $info->{'Repository UUID'};
834
defined $uuid or croak "Unable to get Repository UUID\n";
835
836
+ map_tree_joins() if (@_branch_from && !%tree_map);
837
+
838
# commit parents can be conditionally bound to a particular
839
# svn revision via: "svn_revno=commit_sha1", filter them out here:
840
my @exec_parents;
@@ -852,6 +856,17 @@ sub git_commit {
856
git_addremove();
857
chomp(my $tree = `git-write-tree`);
858
croak if $?;
859
+ if (exists $tree_map{$tree}) {
860
+ my %seen_parent = map { $_ => 1 } @exec_parents;
861
+ foreach (@{$tree_map{$tree}}) {
862
+ # MAXPARENT is defined to 16 in commit-tree.c:
863
+ if ($seen_parent{$_} || @exec_parents > 16) {
864
+ next;
865
+ }
866
+ push @exec_parents, $_;
867
+ $seen_parent{$_} = 1;
868
+ }
869
+ }
870
my $msg_fh = IO::File->new_tmpfile or croak $!;
871
print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
872
"$SVN_URL\@$log_msg->{revision}",
@@ -975,6 +990,29 @@ sub check_upgrade_needed {
990
}
991
}
992
993
+# fills %tree_map with a reverse mapping of trees to commits. Useful
994
+# for finding parents to commit on.
995
+sub map_tree_joins {
996
+ foreach my $br (@_branch_from) {
997
+ my $pid = open my $pipe, '-|';
998
+ defined $pid or croak $!;
999
+ if ($pid == 0) {
1000
+ exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
1001
+ }
1002
+ while (<$pipe>) {
1003
+ if (/^commit ($sha1)$/o) {
1004
+ my $commit = $1;
1005
+ my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1006
+ unless (defined $tree) {
1007
+ die "Failed to parse commit $commit\n";
1008
+ }
1009
+ push @{$tree_map{$tree}}, $commit;
1010
+ }
1011
+ }
1012
+ close $pipe or croak $?;
1013
+ }
1014
+}
1015
+
1016
__END__
1017
1018
Data structures:
contrib/git-svn/git-svn.txt
+16
-1
@@ -27,7 +27,7 @@ For importing svn, git-svnimport is potentially more powerful when
27
operating on repositories organized under the recommended
28
trunk/branch/tags structure, and should be faster, too.
29
30
-git-svn completely ignores the very limited view of branching that
30
+git-svn mostly ignores the very limited view of branching that
31
Subversion has. This allows git-svn to be much easier to use,
32
especially on repositories that are not organized in a manner that
33
git-svnimport is designed for.
@@ -116,6 +116,21 @@ OPTIONS
116
They are both passed directly to git-diff-tree see
117
git-diff-tree(1) for more information.
118
119
+-b<refname>::
120
+--branch <refname>::
121
+ Used with 'fetch' or 'commit'.
122
+
123
+ This can be used to join arbitrary git branches to remotes/git-svn
124
+ on new commits where the tree object is equivalent.
125
+
126
+ When used with different GIT_SVN_ID values, tags and branches in
127
+ SVN can be tracked this way, as can some merges where the heads
128
+ end up having completely equivalent content. This can even be
129
+ used to track branches across multiple SVN _repositories_.
130
+
131
+ This option may be specified multiple times, once for each
132
+ branch.
133
+
134
COMPATIBILITY OPTIONS
135
---------------------
136
--no-ignore-externals::