@matheusbsbs / matheus-bagliano / commits / 2beb3cdd18
Fork 0

contrib/git-svn: use refs/remotes/git-svn instead of git-svn-HEAD

After reading a lengthy discussion on the list, I've come to the conclusion that creating a 'remotes' directory in refs isn't such a bad idea. You can still branch from it by specifying remotes/git-svn (not needing the leading 'refs/'), and the documentation has been updated to reflect that. The 'git-svn' part of the ref can of course be set to whatever you want by using the GIT_SVN_ID environment variable, as before. I'm using refs/remotes/git-svn, and not going with something like refs/remotes/git-svn/HEAD as it's redundant for Subversion where there's zero distinction between branches and directories. Run git-svn rebuild --upgrade to upgrade your repository to use the new head. git-svn-HEAD must be manually deleted for safety reasons. Side note: if you ever (and I hope you never) want to run git-update-refs on a 'remotes/' ref, make sure you have the 'refs/' prefix as you don't want to be clobbering your 'remotes/' in $GIT_DIR (where remote URLs are stored). Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed Mar 1, 2006 at 21:58 UTC 2beb3cdd18225be657849790ed845c4d367b760a
3 files changed +78 -39
contrib/git-svn/git-svn.perl
+51 -12
@@ -34,13 +34,14 @@ use POSIX qw/strftime/;
34 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);
37 + $_find_copies_harder, $_l, $_version, $_upgrade);
38
39 GetOptions( 'revision|r=s' => \$_revision,
40 'no-ignore-externals' => \$_no_ignore_ext,
41 'stdin|' => \$_stdin,
42 'edit|e' => \$_edit,
43 'rmdir' => \$_rmdir,
44 + 'upgrade' => \$_upgrade,
45 'help|H|h' => \$_help,
46 'find-copies-harder' => \$_find_copies_harder,
47 'l=i' => \$_l,
@@ -106,13 +107,18 @@ sub rebuild {
107 $SVN_URL = shift or undef;
108 my $repo_uuid;
109 my $newest_rev = 0;
110 + if ($_upgrade) {
111 + sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
112 + } else {
113 + check_upgrade_needed();
114 + }
115
116 my $pid = open(my $rev_list,'-|');
117 defined $pid or croak $!;
118 if ($pid == 0) {
113 - exec("git-rev-list","$GIT_SVN-HEAD") or croak $!;
119 + exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
120 }
115 - my $first;
121 + my $latest;
122 while (<$rev_list>) {
123 chomp;
124 my $c = $_;
@@ -132,18 +138,20 @@ sub rebuild {
138 "$c, $id\n";
139 }
140 }
141 +
142 + # if we merged or otherwise started elsewhere, this is
143 + # how we break out of it
144 + next if (defined $repo_uuid && ($uuid ne $repo_uuid));
145 + next if (defined $SVN_URL && ($url ne $SVN_URL));
146 +
147 print "r$rev = $c\n";
136 - unless (defined $first) {
148 + unless (defined $latest) {
149 if (!$SVN_URL && !$url) {
150 croak "SVN repository location required: $url\n";
151 }
152 $SVN_URL ||= $url;
141 - $repo_uuid = setup_git_svn();
142 - $first = $rev;
143 - }
144 - if ($uuid ne $repo_uuid) {
145 - croak "Repository UUIDs do not match!\ngot: $uuid\n",
146 - "expected: $repo_uuid\n";
153 + $repo_uuid ||= setup_git_svn();
154 + $latest = $rev;
155 }
156 assert_revision_eq_or_unknown($rev, $c);
157 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
@@ -151,7 +159,7 @@ sub rebuild {
159 }
160 close $rev_list or croak $?;
161 if (!chdir $SVN_WC) {
154 - my @svn_co = ('svn','co',"-r$first");
162 + my @svn_co = ('svn','co',"-r$latest");
163 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
164 sys(@svn_co, $SVN_URL, $SVN_WC);
165 chdir $SVN_WC or croak $!;
@@ -168,6 +176,13 @@ sub rebuild {
176 exec('git-write-tree');
177 }
178 waitpid $pid, 0;
179 +
180 + if ($_upgrade) {
181 + print STDERR <<"";
182 +Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
183 +when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
184 +
185 + }
186 }
187
188 sub init {
@@ -180,6 +195,7 @@ sub init {
195
196 sub fetch {
197 my (@parents) = @_;
198 + check_upgrade_needed();
199 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
200 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
201 unless ($_revision) {
@@ -222,6 +238,7 @@ sub fetch {
238
239 sub commit {
240 my (@commits) = @_;
241 + check_upgrade_needed();
242 if ($_stdin || !@commits) {
243 print "Reading from stdin...\n";
244 @commits = ();
@@ -863,7 +880,7 @@ sub git_commit {
880 if ($commit !~ /^$sha1$/o) {
881 croak "Failed to commit, invalid sha1: $commit\n";
882 }
866 - my @update_ref = ('git-update-ref',"refs/heads/$GIT_SVN-HEAD",$commit);
883 + my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
884 if (my $primary_parent = shift @exec_parents) {
885 push @update_ref, $primary_parent;
886 }
@@ -936,6 +953,28 @@ sub svn_check_ignore_externals {
953 $_no_ignore_ext = 1;
954 }
955 }
956 +
957 +sub check_upgrade_needed {
958 + my $old = eval {
959 + my $pid = open my $child, '-|';
960 + defined $pid or croak $!;
961 + if ($pid == 0) {
962 + close STDERR;
963 + exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
964 + }
965 + my @ret = (<$child>);
966 + close $child or croak $?;
967 + die $? if $?; # just in case close didn't error out
968 + return wantarray ? @ret : join('',@ret);
969 + };
970 + return unless $old;
971 + my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
972 + if ($@ || !$head) {
973 + print STDERR "Please run: $0 rebuild --upgrade\n";
974 + exit 1;
975 + }
976 +}
977 +
978 __END__
979
980 Data structures:
contrib/git-svn/git-svn.txt
+9 -9
@@ -41,12 +41,12 @@ init::
41
42 fetch::
43 Fetch unfetched revisions from the SVN_URL we are tracking.
44 - refs/heads/git-svn-HEAD will be updated to the latest revision.
44 + refs/heads/remotes/git-svn will be updated to the latest revision.
45
46 - Note: You should never attempt to modify the git-svn-HEAD branch
47 - outside of git-svn. Instead, create a branch from git-svn-HEAD
46 + Note: You should never attempt to modify the remotes/git-svn branch
47 + outside of git-svn. Instead, create a branch from remotes/git-svn
48 and work on that branch. Use the 'commit' command (see below)
49 - to write git commits back to git-svn-HEAD.
49 + to write git commits back to remotes/git-svn.
50
51 commit::
52 Commit specified commit or tree objects to SVN. This relies on
@@ -155,13 +155,13 @@ Tracking and contributing to an Subversion managed-project:
155 # Fetch remote revisions::
156 git-svn fetch
157 # Create your own branch to hack on::
158 - git checkout -b my-branch git-svn-HEAD
158 + git checkout -b my-branch remotes/git-svn
159 # Commit only the git commits you want to SVN::
160 git-svn commit <tree-ish> [<tree-ish_2> ...]
161 # Commit all the git commits from my-branch that don't exist in SVN::
162 - git-svn commit git-svn-HEAD..my-branch
162 + git-svn commit remotes/git-svn..my-branch
163 # Something is committed to SVN, pull the latest into your branch::
164 - git-svn fetch && git pull . git-svn-HEAD
164 + git-svn fetch && git pull . remotes/git-svn
165 # Append svn:ignore settings to the default git exclude file:
166 git-svn show-ignore >> .git/info/exclude
167
@@ -184,8 +184,8 @@ SVN repositories via one git repository. Simply set the GIT_SVN_ID
184 environment variable to a name other other than "git-svn" (the default)
185 and git-svn will ignore the contents of the $GIT_DIR/git-svn directory
186 and instead do all of its work in $GIT_DIR/$GIT_SVN_ID for that
187 -invocation. The interface branch will be $GIT_SVN_ID-HEAD, instead of
188 -git-svn-HEAD. Any $GIT_SVN_ID-HEAD branch should never be modified
187 +invocation. The interface branch will be remotes/$GIT_SVN_ID, instead of
188 +remotes/git-svn. Any remotes/$GIT_SVN_ID branch should never be modified
189 by the user outside of git-svn commands.
190
191 ADDITIONAL FETCH ARGUMENTS
contrib/git-svn/t/t0000-contrib-git-svn.sh
+18 -18
@@ -71,14 +71,14 @@ test_expect_success \
71
72
73 name='try a deep --rmdir with a commit'
74 -git checkout -b mybranch git-svn-HEAD
74 +git checkout -b mybranch remotes/git-svn
75 mv dir/a/b/c/d/e/file dir/file
76 cp dir/file file
77 git update-index --add --remove dir/a/b/c/d/e/file dir/file file
78 git commit -m "$name"
79
80 test_expect_success "$name" \
81 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch &&
81 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch &&
82 test -d $SVN_TREE/dir && test ! -d $SVN_TREE/dir/a"
83
84
@@ -91,13 +91,13 @@ git update-index --add dir/file/file
91 git commit -m "$name"
92
93 test_expect_code 1 "$name" \
94 - 'git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch' \
94 + 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch' \
95 || true
96
97
98 name='detect node change from directory to file #1'
99 rm -rf dir $GIT_DIR/index
100 -git checkout -b mybranch2 git-svn-HEAD
100 +git checkout -b mybranch2 remotes/git-svn
101 mv bar/zzz zzz
102 rm -rf bar
103 mv zzz bar
@@ -106,13 +106,13 @@ git update-index --add -- bar
106 git commit -m "$name"
107
108 test_expect_code 1 "$name" \
109 - 'git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch2' \
109 + 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch2' \
110 || true
111
112
113 name='detect node change from file to directory #2'
114 rm -f $GIT_DIR/index
115 -git checkout -b mybranch3 git-svn-HEAD
115 +git checkout -b mybranch3 remotes/git-svn
116 rm bar/zzz
117 git-update-index --remove bar/zzz
118 mkdir bar/zzz
@@ -121,13 +121,13 @@ git-update-index --add bar/zzz/yyy
121 git commit -m "$name"
122
123 test_expect_code 1 "$name" \
124 - 'git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch3' \
124 + 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch3' \
125 || true
126
127
128 name='detect node change from directory to file #2'
129 rm -f $GIT_DIR/index
130 -git checkout -b mybranch4 git-svn-HEAD
130 +git checkout -b mybranch4 remotes/git-svn
131 rm -rf dir
132 git update-index --remove -- dir/file
133 touch dir
@@ -136,19 +136,19 @@ git update-index --add -- dir
136 git commit -m "$name"
137
138 test_expect_code 1 "$name" \
139 - 'git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch4' \
139 + 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch4' \
140 || true
141
142
143 name='remove executable bit from a file'
144 rm -f $GIT_DIR/index
145 -git checkout -b mybranch5 git-svn-HEAD
145 +git checkout -b mybranch5 remotes/git-svn
146 chmod -x exec.sh
147 git update-index exec.sh
148 git commit -m "$name"
149
150 test_expect_success "$name" \
151 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch5 &&
151 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
152 test ! -x $SVN_TREE/exec.sh"
153
154
@@ -158,7 +158,7 @@ git update-index exec.sh
158 git commit -m "$name"
159
160 test_expect_success "$name" \
161 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch5 &&
161 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
162 test -x $SVN_TREE/exec.sh"
163
164
@@ -170,7 +170,7 @@ git update-index exec.sh
170 git commit -m "$name"
171
172 test_expect_success "$name" \
173 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch5 &&
173 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
174 test -L $SVN_TREE/exec.sh"
175
176
@@ -182,7 +182,7 @@ git update-index --add bar/zzz exec-2.sh
182 git commit -m "$name"
183
184 test_expect_success "$name" \
185 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch5 &&
185 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
186 test -x $SVN_TREE/bar/zzz &&
187 test -L $SVN_TREE/exec-2.sh"
188
@@ -196,7 +196,7 @@ git update-index exec-2.sh
196 git commit -m "$name"
197
198 test_expect_success "$name" \
199 - "git-svn commit --find-copies-harder --rmdir git-svn-HEAD..mybranch5 &&
199 + "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
200 test -f $SVN_TREE/exec-2.sh &&
201 test ! -L $SVN_TREE/exec-2.sh &&
202 diff -u help $SVN_TREE/exec-2.sh"
@@ -207,9 +207,9 @@ name='test fetch functionality (svn => git) with alternate GIT_SVN_ID'
207 GIT_SVN_ID=alt
208 export GIT_SVN_ID
209 test_expect_success "$name" \
210 - "git-svn init $svnrepo && git-svn fetch -v &&
211 - git-rev-list --pretty=raw git-svn-HEAD | grep ^tree | uniq > a &&
212 - git-rev-list --pretty=raw alt-HEAD | grep ^tree | uniq > b &&
210 + "git-svn init $svnrepo && git-svn fetch &&
211 + git-rev-list --pretty=raw remotes/git-svn | grep ^tree | uniq > a &&
212 + git-rev-list --pretty=raw remotes/alt | grep ^tree | uniq > b &&
213 diff -u a b"
214
215 test_done