@matheusbsbs / matheus-bagliano / commits / dfeff66ed9
Fork 0

revamp git-clone.

This does two things. * A new flag --reference can be used to name a local repository that is to be used as an alternate. This is in response to an inquiry by James Cloos in the message on the list <m3r74ykue7.fsf@lugabout.cloos.reno.nv.us>. * A new flag --use-separate-remote stops contaminating local branch namespace by upstream branch names. The upstream branch heads are copied in .git/refs/remotes/ instead of .git/refs/heads/ and .git/remotes/origin file is set up to reflect this as well. It requires to have fetch/pull update to understand .git/refs/remotes by Eric Wong to further update the repository cloned this way. For the former change, git-fetch-pack is taught a new flag --all to fetch from all the remote heads. Nobody uses the git-clone-pack with this change, so we could deprecate the command, but removal of the command will be left to a separate round. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 20, 2006 at 00:21 UTC dfeff66ed9a3931d60f3cd600ad8c14b5cc3d9e5
2 files changed +166 -36
fetch-pack.c
+14 -4
@@ -7,8 +7,9 @@
7 static int keep_pack;
8 static int quiet;
9 static int verbose;
10 +static int fetch_all;
11 static const char fetch_pack_usage[] =
11 -"git-fetch-pack [-q] [-v] [-k] [--thin] [--exec=upload-pack] [host:]directory <refs>...";
12 +"git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [host:]directory <refs>...";
13 static const char *exec = "git-upload-pack";
14
15 #define COMPLETE (1U << 0)
@@ -266,8 +267,9 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
267 for (prev = NULL, current = *refs; current; current = next) {
268 next = current->next;
269 if ((!memcmp(current->name, "refs/", 5) &&
269 - check_ref_format(current->name + 5)) ||
270 - !path_match(current->name, nr_match, match)) {
270 + check_ref_format(current->name + 5)) ||
271 + (!fetch_all &&
272 + !path_match(current->name, nr_match, match))) {
273 if (prev == NULL)
274 *refs = next;
275 else
@@ -376,7 +378,11 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
378 goto all_done;
379 }
380 if (find_common(fd, sha1, ref) < 0)
379 - fprintf(stderr, "warning: no common commits\n");
381 + if (!keep_pack)
382 + /* When cloning, it is not unusual to have
383 + * no common commit.
384 + */
385 + fprintf(stderr, "warning: no common commits\n");
386
387 if (keep_pack)
388 status = receive_keep_pack(fd, "git-fetch-pack", quiet);
@@ -426,6 +432,10 @@ int main(int argc, char **argv)
432 use_thin_pack = 1;
433 continue;
434 }
435 + if (!strcmp("--all", arg)) {
436 + fetch_all = 1;
437 + continue;
438 + }
439 if (!strcmp("-v", arg)) {
440 verbose = 1;
441 continue;
git-clone.sh
+152 -32
@@ -9,7 +9,7 @@
9 unset CDPATH
10
11 usage() {
12 - echo >&2 "Usage: $0 [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
12 + echo >&2 "Usage: $0 [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
13 exit 1
14 }
15
@@ -40,22 +40,72 @@ Perhaps git-update-server-info needs to be run there?"
40 do
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
42 case "$name" in
43 - *^*) ;;
44 - *)
45 - git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
43 + *^*) continue;;
44 esac
45 + if test -n "$use_separate_remote" &&
46 + branch_name=`expr "$name" : 'heads/\(.*\)'`
47 + then
48 + tname="remotes/$branch_name"
49 + else
50 + tname=$name
51 + fi
52 + git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
53 done <"$clone_tmp/refs"
54 rm -fr "$clone_tmp"
55 + http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD"
56 +}
57 +
58 +# Read git-fetch-pack -k output and store the remote branches.
59 +copy_refs='
60 +use File::Path qw(mkpath);
61 +use File::Basename qw(dirname);
62 +my $git_dir = $ARGV[0];
63 +my $use_separate_remote = $ARGV[1];
64 +
65 +my $branch_top = ($use_separate_remote ? "remotes" : "heads");
66 +my $tag_top = "tags";
67 +
68 +sub store {
69 + my ($sha1, $name, $top) = @_;
70 + $name = "$git_dir/refs/$top/$name";
71 + mkpath(dirname($name));
72 + open O, ">", "$name";
73 + print O "$sha1\n";
74 + close O;
75 }
76
77 +open FH, "<", "$git_dir/CLONE_HEAD";
78 +while (<FH>) {
79 + my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
80 + next if ($name =~ /\^\173/);
81 + if ($name eq "HEAD") {
82 + open O, ">", "$git_dir/REMOTE_HEAD";
83 + print O "$sha1\n";
84 + close O;
85 + next;
86 + }
87 + if ($name =~ s/^refs\/heads\///) {
88 + store($sha1, $name, $branch_top);
89 + next;
90 + }
91 + if ($name =~ s/^refs\/tags\///) {
92 + store($sha1, $name, $tag_top);
93 + next;
94 + }
95 +}
96 +close FH;
97 +'
98 +
99 quiet=
100 use_local=no
101 local_shared=no
102 no_checkout=
103 upload_pack=
104 bare=
57 -origin=origin
105 +reference=
106 +origin=
107 origin_override=
108 +use_separate_remote=
109 while
110 case "$#,$1" in
111 0,*) break ;;
@@ -68,7 +118,14 @@ while
118 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
119 local_shared=yes; use_local=yes ;;
120 *,-q|*,--quiet) quiet=-q ;;
121 + *,--use-separate-remote)
122 + use_separate_remote=t ;;
123 1,-o) usage;;
124 + 1,--reference) usage ;;
125 + *,--reference)
126 + shift; reference="$1" ;;
127 + *,--reference=*)
128 + reference=`expr "$1" : '--reference=\(.*\)'` ;;
129 *,-o)
130 git-check-ref-format "$2" || {
131 echo >&2 "'$2' is not suitable for a branch name"
@@ -100,9 +157,24 @@ then
157 echo >&2 '--bare and -o $origin options are incompatible.'
158 exit 1
159 fi
160 + if test t = "$use_separate_remote"
161 + then
162 + echo >&2 '--bare and --use-separate-remote options are incompatible.'
163 + exit 1
164 + fi
165 no_checkout=yes
166 fi
167
168 +if test -z "$origin_override$origin"
169 +then
170 + if test -n "$use_separate_remote"
171 + then
172 + origin=remotes/master
173 + else
174 + origin=heads/origin
175 + fi
176 +fi
177 +
178 # Turn the source into an absolute path if
179 # it is local
180 repo="$1"
@@ -130,6 +202,28 @@ yes)
202 GIT_DIR="$D/.git" ;;
203 esac
204
205 +if test -n "$reference"
206 +then
207 + if test -d "$reference"
208 + then
209 + if test -d "$reference/.git/objects"
210 + then
211 + reference="$reference/.git"
212 + fi
213 + reference=$(cd "$reference" && pwd)
214 + echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
215 + (cd "$reference" && tar cf - refs) |
216 + (cd "$GIT_DIR/refs" &&
217 + mkdir reference-tmp &&
218 + cd reference-tmp &&
219 + tar xf -)
220 + else
221 + echo >&2 "$reference: not a local directory." && usage
222 + fi
223 +fi
224 +
225 +rm -f "$GIT_DIR/CLONE_HEAD"
226 +
227 # We do local magic only when the user tells us to.
228 case "$local,$use_local" in
229 yes,yes)
@@ -165,24 +259,14 @@ yes,yes)
259 } >"$GIT_DIR/objects/info/alternates"
260 ;;
261 esac
168 -
169 - # Make a duplicate of refs and HEAD pointer
170 - HEAD=
171 - if test -f "$repo/HEAD"
172 - then
173 - HEAD=HEAD
174 - fi
175 - (cd "$repo" && tar cf - refs $HEAD) |
176 - (cd "$GIT_DIR" && tar xf -) || exit 1
262 + git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
263 ;;
264 *)
265 case "$repo" in
266 rsync://*)
267 rsync $quiet -av --ignore-existing \
182 - --exclude info "$repo/objects/" "$GIT_DIR/objects/" &&
183 - rsync $quiet -av --ignore-existing \
184 - --exclude info "$repo/refs/" "$GIT_DIR/refs/" || exit
185 -
268 + --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
269 + exit
270 # Look at objects/info/alternates for rsync -- http will
271 # support it natively and git native ones will do it on the
272 # remote end. Not having that file is not a crime.
@@ -205,6 +289,7 @@ yes,yes)
289 done
290 rm -f "$GIT_DIR/TMP_ALT"
291 fi
292 + git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
293 ;;
294 http://*)
295 if test -z "@@NO_CURL@@"
@@ -217,37 +302,71 @@ yes,yes)
302 ;;
303 *)
304 cd "$D" && case "$upload_pack" in
220 - '') git-clone-pack $quiet "$repo" ;;
221 - *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
222 - esac || {
223 - echo >&2 "clone-pack from '$repo' failed."
305 + '') git-fetch-pack --all -k $quiet "$repo" ;;
306 + *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
307 + esac >"$GIT_DIR/CLONE_HEAD" || {
308 + echo >&2 "fetch-pack from '$repo' failed."
309 exit 1
310 }
311 ;;
312 esac
313 ;;
314 esac
315 +test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
316 +
317 +if test -f "$GIT_DIR/CLONE_HEAD"
318 +then
319 + # Figure out where the remote HEAD points at.
320 + perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote"
321 +fi
322
323 cd "$D" || exit
324
233 -if test -f "$GIT_DIR/HEAD" && test -z "$bare"
325 +if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
326 then
235 - head_points_at=`git-symbolic-ref HEAD`
327 + head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
328 + # Figure out which remote branch HEAD points at.
329 + case "$use_separate_remote" in
330 + '') remote_top=refs/heads ;;
331 + *) remote_top=refs/remotes ;;
332 + esac
333 + head_points_at=$(
334 + (
335 + echo "master"
336 + cd "$GIT_DIR/$remote_top" &&
337 + find . -type f -print | sed -e 's/^\.\///'
338 + ) | (
339 + done=f
340 + while read name
341 + do
342 + test t = $done && continue
343 + branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
344 + if test "$head_sha1" = "$branch_tip"
345 + then
346 + echo "$name"
347 + done=t
348 + fi
349 + done
350 + )
351 + )
352 case "$head_points_at" in
237 - refs/heads/*)
238 - head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
353 + ?*)
354 mkdir -p "$GIT_DIR/remotes" &&
355 echo >"$GIT_DIR/remotes/origin" \
356 "URL: $repo
242 -Pull: $head_points_at:$origin" &&
243 - git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) &&
244 - (cd "$GIT_DIR" && find "refs/heads" -type f -print) |
357 +Pull: refs/heads/$head_points_at:refs/$origin" &&
358 + case "$use_separate_remote" in
359 + t) git-update-ref HEAD "$head_sha1" ;;
360 + *) git-update-ref "refs/$origin" $(git-rev-parse HEAD)
361 + esac &&
362 + (cd "$GIT_DIR" && find "$remote_top" -type f -print) |
363 while read ref
364 do
247 - head=`expr "$ref" : 'refs/heads/\(.*\)'` &&
248 - test "$head_points_at" = "$head" ||
365 + head=`expr "$ref" : 'refs/\(.*\)'` &&
366 + name=`expr "$ref" : 'refs/[^\/]*/\(.*\)'` &&
367 + test "$head_points_at" = "$name" ||
368 test "$origin" = "$head" ||
250 - echo "Pull: ${head}:${head}"
369 + echo "Pull: refs/heads/${name}:$remote_top/${name}"
370 done >>"$GIT_DIR/remotes/origin"
371 esac
372
@@ -256,6 +375,7 @@ Pull: $head_points_at:$origin" &&
375 git-read-tree -m -u -v HEAD HEAD
376 esac
377 fi
378 +rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
379
380 trap - exit
381