git-clone: fix handling of upsteram whose HEAD does not point at master.
[git/git.git] / git-clone.sh
CommitLineData
3f571e0b 1#!/bin/sh
e95ab1ed
JH
2#
3# Copyright (c) 2005, Linus Torvalds
4# Copyright (c) 2005, Junio C Hamano
5#
6# Clone a repository into a different directory that does not yet exist.
7
365527ad
JH
8# See git-sh-setup why.
9unset CDPATH
10
e95ab1ed 11usage() {
98a4fef3 12 echo >&2 "Usage: $0 [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
e95ab1ed
JH
13 exit 1
14}
15
ba375acf
LT
16get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18}
19
0516de30
JH
20if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
22fi
23
24http_fetch () {
25 # $1 = Remote, $2 = Local
66c9ec25 26 curl -nsfL $curl_extra_args "$1" >"$2"
0516de30
JH
27}
28
29clone_dumb_http () {
30 # $1 - remote, $2 - local
31 cd "$2" &&
32 clone_tmp='.git/clone-tmp' &&
33 mkdir -p "$clone_tmp" || exit 1
05621105 34 http_fetch "$1/info/refs" "$clone_tmp/refs" || {
0516de30
JH
35 echo >&2 "Cannot get remote repository information.
36Perhaps git-update-server-info needs to be run there?"
37 exit 1;
38 }
0516de30
JH
39 while read sha1 refname
40 do
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
cdb39508 42 case "$name" in
dfeff66e 43 *^*) continue;;
cdb39508 44 esac
dfeff66e
JH
45 if test -n "$use_separate_remote" &&
46 branch_name=`expr "$name" : 'heads/\(.*\)'`
47 then
5ceb05f8 48 tname="remotes/$origin/$branch_name"
dfeff66e
JH
49 else
50 tname=$name
51 fi
52 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
0516de30
JH
53 done <"$clone_tmp/refs"
54 rm -fr "$clone_tmp"
c72112e6
JH
55 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
56 rm -f "$GIT_DIR/REMOTE_HEAD"
dfeff66e
JH
57}
58
59# Read git-fetch-pack -k output and store the remote branches.
60copy_refs='
61use File::Path qw(mkpath);
62use File::Basename qw(dirname);
63my $git_dir = $ARGV[0];
64my $use_separate_remote = $ARGV[1];
47874d6d 65my $origin = $ARGV[2];
dfeff66e 66
47874d6d 67my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
dfeff66e
JH
68my $tag_top = "tags";
69
70sub store {
71 my ($sha1, $name, $top) = @_;
72 $name = "$git_dir/refs/$top/$name";
73 mkpath(dirname($name));
74 open O, ">", "$name";
75 print O "$sha1\n";
76 close O;
0516de30
JH
77}
78
dfeff66e
JH
79open FH, "<", "$git_dir/CLONE_HEAD";
80while (<FH>) {
81 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
82 next if ($name =~ /\^\173/);
83 if ($name eq "HEAD") {
84 open O, ">", "$git_dir/REMOTE_HEAD";
85 print O "$sha1\n";
86 close O;
87 next;
88 }
89 if ($name =~ s/^refs\/heads\///) {
90 store($sha1, $name, $branch_top);
91 next;
92 }
93 if ($name =~ s/^refs\/tags\///) {
94 store($sha1, $name, $tag_top);
95 next;
96 }
97}
98close FH;
99'
100
167a4a33 101quiet=
ef5b4eab 102local=no
e95ab1ed 103use_local=no
aae4f42c 104local_shared=no
036a72d8 105no_checkout=
6ec311da 106upload_pack=
87e80c4b 107bare=
dfeff66e
JH
108reference=
109origin=
e6489a1b 110origin_override=
dfeff66e 111use_separate_remote=
e95ab1ed
JH
112while
113 case "$#,$1" in
114 0,*) break ;;
8a1a120c
JH
115 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
116 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
117 no_checkout=yes ;;
87e80c4b
JH
118 *,--na|*,--nak|*,--nake|*,--naked|\
119 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
1cadb5a2 120 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
aae4f42c 121 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
2c4ed386 122 local_shared=yes; use_local=yes ;;
167a4a33 123 *,-q|*,--quiet) quiet=-q ;;
dfeff66e
JH
124 *,--use-separate-remote)
125 use_separate_remote=t ;;
dfeff66e
JH
126 1,--reference) usage ;;
127 *,--reference)
128 shift; reference="$1" ;;
129 *,--reference=*)
130 reference=`expr "$1" : '--reference=\(.*\)'` ;;
98a4fef3 131 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
47874d6d 132 case "$2" in
98a4fef3
YS
133 '')
134 usage ;;
47874d6d
JH
135 */*)
136 echo >&2 "'$2' is not suitable for an origin name"
137 exit 1
138 esac
139 git-check-ref-format "heads/$2" || {
e6c310fd
JS
140 echo >&2 "'$2' is not suitable for a branch name"
141 exit 1
142 }
e6489a1b 143 test -z "$origin_override" || {
98a4fef3 144 echo >&2 "Do not give more than one --origin options."
e6489a1b
JH
145 exit 1
146 }
147 origin_override=yes
e6c310fd
JS
148 origin="$2"; shift
149 ;;
1cadb5a2 150 1,-u|1,--upload-pack) usage ;;
6ec311da
JH
151 *,-u|*,--upload-pack)
152 shift
1cadb5a2 153 upload_pack="--exec=$1" ;;
e95ab1ed
JH
154 *,-*) usage ;;
155 *) break ;;
156 esac
157do
158 shift
159done
160
ef5b4eab
YS
161repo="$1"
162if test -z "$repo"
163then
164 echo >&2 'you must specify a repository to clone.'
165 exit 1
166fi
167
87e80c4b 168# --bare implies --no-checkout
e6489a1b
JH
169if test yes = "$bare"
170then
171 if test yes = "$origin_override"
172 then
98a4fef3 173 echo >&2 '--bare and --origin $origin options are incompatible.'
e6489a1b
JH
174 exit 1
175 fi
dfeff66e
JH
176 if test t = "$use_separate_remote"
177 then
178 echo >&2 '--bare and --use-separate-remote options are incompatible.'
179 exit 1
180 fi
e6489a1b
JH
181 no_checkout=yes
182fi
8a1a120c 183
47874d6d 184if test -z "$origin"
dfeff66e 185then
47874d6d 186 origin=origin
dfeff66e
JH
187fi
188
ba375acf
LT
189# Turn the source into an absolute path if
190# it is local
ba375acf
LT
191if base=$(get_repo_base "$repo"); then
192 repo="$base"
193 local=yes
194fi
195
3f571e0b 196dir="$2"
0879aa28 197# Try using "humanish" part of source repo if user didn't specify one
e7555785 198[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
b0c698a6 199[ -e "$dir" ] && echo "$dir already exists." && usage
7f10f7c4 200mkdir -p "$dir" &&
8a1a120c 201D=$(cd "$dir" && pwd) &&
edd3ebfe 202trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
87e80c4b 203case "$bare" in
8a1a120c
JH
204yes) GIT_DIR="$D" ;;
205*) GIT_DIR="$D/.git" ;;
206esac && export GIT_DIR && git-init-db || usage
87e80c4b 207case "$bare" in
8a1a120c
JH
208yes)
209 GIT_DIR="$D" ;;
210*)
211 GIT_DIR="$D/.git" ;;
212esac
e95ab1ed 213
dfeff66e
JH
214if test -n "$reference"
215then
216 if test -d "$reference"
217 then
218 if test -d "$reference/.git/objects"
219 then
220 reference="$reference/.git"
221 fi
222 reference=$(cd "$reference" && pwd)
223 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
224 (cd "$reference" && tar cf - refs) |
225 (cd "$GIT_DIR/refs" &&
226 mkdir reference-tmp &&
227 cd reference-tmp &&
228 tar xf -)
229 else
230 echo >&2 "$reference: not a local directory." && usage
231 fi
232fi
233
234rm -f "$GIT_DIR/CLONE_HEAD"
235
e95ab1ed 236# We do local magic only when the user tells us to.
ba375acf
LT
237case "$local,$use_local" in
238yes,yes)
e95ab1ed 239 ( cd "$repo/objects" ) || {
ab6625e0
JH
240 echo >&2 "-l flag seen but $repo is not local."
241 exit 1
e95ab1ed
JH
242 }
243
aae4f42c
JH
244 case "$local_shared" in
245 no)
246 # See if we can hardlink and drop "l" if not.
247 sample_file=$(cd "$repo" && \
248 find objects -type f -print | sed -e 1q)
e95ab1ed 249
aae4f42c
JH
250 # objects directory should not be empty since we are cloning!
251 test -f "$repo/$sample_file" || exit
e95ab1ed 252
aae4f42c 253 l=
8a1a120c 254 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
aae4f42c
JH
255 then
256 l=l
257 fi &&
8a1a120c 258 rm -f "$GIT_DIR/objects/sample" &&
aae4f42c 259 cd "$repo" &&
8e1618f9 260 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
aae4f42c
JH
261 ;;
262 yes)
8a1a120c 263 mkdir -p "$GIT_DIR/objects/info"
0f87f893
JH
264 {
265 test -f "$repo/objects/info/alternates" &&
266 cat "$repo/objects/info/alternates";
267 echo "$repo/objects"
8a1a120c 268 } >"$GIT_DIR/objects/info/alternates"
aae4f42c
JH
269 ;;
270 esac
dfeff66e 271 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
7558ef89
LT
272 ;;
273*)
1cadb5a2
JH
274 case "$repo" in
275 rsync://*)
4447badc 276 rsync $quiet -av --ignore-existing \
dfeff66e
JH
277 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
278 exit
4447badc
JH
279 # Look at objects/info/alternates for rsync -- http will
280 # support it natively and git native ones will do it on the
281 # remote end. Not having that file is not a crime.
89d844d0 282 rsync -q "$repo/objects/info/alternates" \
8a1a120c
JH
283 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
284 rm -f "$GIT_DIR/TMP_ALT"
285 if test -f "$GIT_DIR/TMP_ALT"
4447badc 286 then
0e9ab02d 287 ( cd "$D" &&
4447badc 288 . git-parse-remote &&
8a1a120c 289 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
4447badc
JH
290 while read alt
291 do
292 case "$alt" in 'bad alternate: '*) die "$alt";; esac
293 case "$quiet" in
294 '') echo >&2 "Getting alternate: $alt" ;;
295 esac
296 rsync $quiet -av --ignore-existing \
8a1a120c 297 --exclude info "$alt" "$GIT_DIR/objects" || exit
4447badc 298 done
8a1a120c 299 rm -f "$GIT_DIR/TMP_ALT"
4447badc 300 fi
dfeff66e 301 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
1cadb5a2
JH
302 ;;
303 http://*)
6c5c62f3
FP
304 if test -z "@@NO_CURL@@"
305 then
306 clone_dumb_http "$repo" "$D"
307 else
308 echo >&2 "http transport not supported, rebuild Git with curl support"
309 exit 1
310 fi
1cadb5a2
JH
311 ;;
312 *)
313 cd "$D" && case "$upload_pack" in
dfeff66e
JH
314 '') git-fetch-pack --all -k $quiet "$repo" ;;
315 *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
316 esac >"$GIT_DIR/CLONE_HEAD" || {
317 echo >&2 "fetch-pack from '$repo' failed."
0a8b4def
JH
318 exit 1
319 }
1cadb5a2 320 ;;
6ec311da 321 esac
7558ef89
LT
322 ;;
323esac
dfeff66e
JH
324test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
325
326if test -f "$GIT_DIR/CLONE_HEAD"
327then
c72112e6 328 # Read git-fetch-pack -k output and store the remote branches.
47874d6d 329 perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
dfeff66e 330fi
1cadb5a2 331
0e9ab02d 332cd "$D" || exit
036a72d8 333
dfeff66e 334if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
036a72d8 335then
dfeff66e
JH
336 # Figure out which remote branch HEAD points at.
337 case "$use_separate_remote" in
338 '') remote_top=refs/heads ;;
47874d6d 339 *) remote_top="refs/remotes/$origin" ;;
dfeff66e 340 esac
47874d6d 341
c72112e6
JH
342 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
343 case "$head_sha1" in
344 'ref: refs/'*)
345 # Uh-oh, the remote told us (http transport done against
346 # new style repository with a symref HEAD).
347 # Ideally we should skip the guesswork but for now
348 # opt for minimum change.
349 head_sha1=`expr "$head_sha1" : 'ref: refs/heads/\(.*\)'`
350 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
351 ;;
352 esac
47874d6d 353
c72112e6 354 # The name under $remote_top the remote HEAD seems to point at.
dfeff66e
JH
355 head_points_at=$(
356 (
357 echo "master"
358 cd "$GIT_DIR/$remote_top" &&
359 find . -type f -print | sed -e 's/^\.\///'
360 ) | (
361 done=f
362 while read name
363 do
364 test t = $done && continue
365 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
366 if test "$head_sha1" = "$branch_tip"
367 then
368 echo "$name"
369 done=t
370 fi
371 done
372 )
373 )
47874d6d 374
c72112e6 375 # Write out remotes/$origin file, and update our "$head_points_at".
e125c1a7 376 case "$head_points_at" in
dfeff66e 377 ?*)
8a1a120c 378 mkdir -p "$GIT_DIR/remotes" &&
c72112e6 379 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
dfeff66e 380 case "$use_separate_remote" in
c72112e6
JH
381 t) origin_track="$remote_top/$head_points_at"
382 git-update-ref HEAD "$head_sha1" ;;
383 *) origin_track="$remote_top/$origin"
384 git-update-ref "refs/heads/$origin" "$head_sha1" ;;
dfeff66e 385 esac &&
c72112e6
JH
386 echo >"$GIT_DIR/remotes/$origin" \
387 "URL: $repo
388Pull: refs/heads/$head_points_at:$origin_track" &&
47874d6d
JH
389 (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
390 while read dotslref
95d117b6 391 do
47874d6d 392 name=`expr "$dotslref" : './\(.*\)'` &&
c72112e6
JH
393 test "$use_separate_remote" = '' && {
394 test "$head_points_at" = "$name" ||
395 test "$origin" = "$name"
396 } ||
dfeff66e 397 echo "Pull: refs/heads/${name}:$remote_top/${name}"
5ceb05f8
JH
398 done >>"$GIT_DIR/remotes/$origin" &&
399 case "$use_separate_remote" in
400 t)
401 rm -f "refs/remotes/$origin/HEAD"
402 git-symbolic-ref "refs/remotes/$origin/HEAD" \
403 "refs/remotes/$origin/$head_points_at"
404 esac
e125c1a7
JH
405 esac
406
036a72d8
JH
407 case "$no_checkout" in
408 '')
744633cb 409 git-read-tree -m -u -v HEAD HEAD
036a72d8
JH
410 esac
411fi
dfeff66e 412rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
41ff7a10
CW
413
414trap - exit
415