revamp git-clone (take #2).
This builds on top of the previous one. * --use-separate-remote uses .git/refs/remotes/$origin/ directory to keep track of the upstream branches. * The $origin above defaults to "origin" as usual, but the existing "-o $origin" option can be used to override it. I am not yet convinced if we should make "$origin" the synonym to "refs/remotes/$origin/$name" where $name is the primary branch name of $origin upstream, nor if so how we should decide which upstream branch is the primary one, but that is more or less orthogonal to what the clone does here. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 21, 2006 at 00:14 UTC
47874d6d9a7f49ade6388df049597f03365961ca
1 file changed
+31
-19
git-clone.sh
+31
-19
@@ -9,7 +9,7 @@
9
unset CDPATH
10
11
usage() {
12
- echo >&2 "Usage: $0 [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
12
+ echo >&2 "Usage: $0 [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
13
exit 1
14
}
15
@@ -61,8 +61,9 @@ 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
+my $origin = $ARGV[2];
65
65
-my $branch_top = ($use_separate_remote ? "remotes" : "heads");
66
+my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
67
my $tag_top = "tags";
68
69
sub store {
@@ -127,7 +128,12 @@ while
128
*,--reference=*)
129
reference=`expr "$1" : '--reference=\(.*\)'` ;;
130
*,-o)
130
- git-check-ref-format "$2" || {
131
+ case "$2" in
132
+ */*)
133
+ echo >&2 "'$2' is not suitable for an origin name"
134
+ exit 1
135
+ esac
136
+ git-check-ref-format "heads/$2" || {
137
echo >&2 "'$2' is not suitable for a branch name"
138
exit 1
139
}
@@ -165,14 +171,9 @@ then
171
no_checkout=yes
172
fi
173
168
-if test -z "$origin_override$origin"
174
+if test -z "$origin"
175
then
170
- if test -n "$use_separate_remote"
171
- then
172
- origin=remotes/master
173
- else
174
- origin=heads/origin
175
- fi
176
+ origin=origin
177
fi
178
179
# Turn the source into an absolute path if
@@ -317,7 +318,7 @@ test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
318
if test -f "$GIT_DIR/CLONE_HEAD"
319
then
320
# Figure out where the remote HEAD points at.
320
- perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote"
321
+ perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
322
fi
323
324
cd "$D" || exit
@@ -328,8 +329,18 @@ then
329
# Figure out which remote branch HEAD points at.
330
case "$use_separate_remote" in
331
'') remote_top=refs/heads ;;
331
- *) remote_top=refs/remotes ;;
332
+ *) remote_top="refs/remotes/$origin" ;;
333
esac
334
+
335
+ # What to use to track the remote primary branch
336
+ if test -n "$use_separate_remote"
337
+ then
338
+ origin_tracking="remotes/$origin/master"
339
+ else
340
+ origin_tracking="heads/$origin"
341
+ fi
342
+
343
+ # The name under $remote_top the remote HEAD seems to point at
344
head_points_at=$(
345
(
346
echo "master"
@@ -349,25 +360,26 @@ then
360
done
361
)
362
)
363
+
364
+ # Write out remotes/$origin file.
365
case "$head_points_at" in
366
?*)
367
mkdir -p "$GIT_DIR/remotes" &&
355
- echo >"$GIT_DIR/remotes/origin" \
368
+ echo >"$GIT_DIR/remotes/$origin" \
369
"URL: $repo
357
-Pull: refs/heads/$head_points_at:refs/$origin" &&
370
+Pull: refs/heads/$head_points_at:refs/$origin_tracking" &&
371
case "$use_separate_remote" in
372
t) git-update-ref HEAD "$head_sha1" ;;
373
*) git-update-ref "refs/$origin" $(git-rev-parse HEAD)
374
esac &&
362
- (cd "$GIT_DIR" && find "$remote_top" -type f -print) |
363
- while read ref
375
+ (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
376
+ while read dotslref
377
do
365
- head=`expr "$ref" : 'refs/\(.*\)'` &&
366
- name=`expr "$ref" : 'refs/[^\/]*/\(.*\)'` &&
378
+ name=`expr "$dotslref" : './\(.*\)'` &&
379
test "$head_points_at" = "$name" ||
380
test "$origin" = "$head" ||
381
echo "Pull: refs/heads/${name}:$remote_top/${name}"
370
- done >>"$GIT_DIR/remotes/origin"
382
+ done >>"$GIT_DIR/remotes/$origin"
383
esac
384
385
case "$no_checkout" in