]> git.ipfire.org Git - thirdparty/git.git/blame - git-clone.sh
git-apply --whitespace=fix: fix off by one thinko
[thirdparty/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
a6080a0a 5#
e95ab1ed
JH
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
94362599
PH
11OPTIONS_SPEC="\
12git-clone [options] [--] <repo> [<dir>]
13--
14n,no-checkout don't create a checkout
15bare create a bare repository
16naked create a bare repository
17l,local to clone from a local repository
18no-hardlinks don't use local hardlinks, always copy
19s,shared setup as a shared repository
20template= path to the template directory
21q,quiet be quiet
22reference= reference repository
23o,origin= use <name> instead of 'origin' to track upstream
24u,upload-pack= path to git-upload-pack on the remote
25depth= create a shallow clone of that depth
26
27use-separate-remote compatibility, do not use
28no-separate-remote compatibility, do not use"
29
87b787ac
DL
30die() {
31 echo >&2 "$@"
e95ab1ed
JH
32 exit 1
33}
34
87b787ac 35usage() {
94362599 36 exec "$0" -h
87b787ac
DL
37}
38
e817e3e8 39eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
94362599 40
ba375acf 41get_repo_base() {
c2f599e0
JH
42 (
43 cd "`/bin/pwd`" &&
defe13a2 44 cd "$1" || cd "$1.git" &&
c2f599e0 45 {
223fa327 46 cd .git
c2f599e0
JH
47 pwd
48 }
223fa327 49 ) 2>/dev/null
ba375acf
LT
50}
51
1c1f79a1
AB
52if [ -n "$GIT_SSL_NO_VERIFY" -o \
53 "`git config --bool http.sslVerify`" = false ]; then
0516de30
JH
54 curl_extra_args="-k"
55fi
56
57http_fetch () {
58 # $1 = Remote, $2 = Local
6851162a
JK
59 curl -nsfL $curl_extra_args "$1" >"$2"
60 curl_exit_status=$?
61 case $curl_exit_status in
62 126|127) exit ;;
63 *) return $curl_exit_status ;;
64 esac
0516de30
JH
65}
66
67clone_dumb_http () {
68 # $1 - remote, $2 - local
69 cd "$2" &&
5e3a620c 70 clone_tmp="$GIT_DIR/clone-tmp" &&
0516de30 71 mkdir -p "$clone_tmp" || exit 1
3ea099d4 72 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
5be60078 73 "`git config --bool http.noEPSV`" = true ]; then
3ea099d4
SK
74 curl_extra_args="${curl_extra_args} --disable-epsv"
75 fi
87b787ac
DL
76 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
77 die "Cannot get remote repository information.
0516de30 78Perhaps git-update-server-info needs to be run there?"
7e8c8255 79 test "z$quiet" = z && v=-v || v=
0516de30
JH
80 while read sha1 refname
81 do
f327dbce 82 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
cdb39508 83 case "$name" in
dfeff66e 84 *^*) continue;;
cdb39508 85 esac
983d2ee2
JH
86 case "$bare,$name" in
87 yes,* | ,heads/* | ,tags/*) ;;
88 *) continue ;;
89 esac
dfeff66e 90 if test -n "$use_separate_remote" &&
f327dbce 91 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
dfeff66e 92 then
5ceb05f8 93 tname="remotes/$origin/$branch_name"
dfeff66e
JH
94 else
95 tname=$name
96 fi
928c210a 97 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
0516de30
JH
98 done <"$clone_tmp/refs"
99 rm -fr "$clone_tmp"
c72112e6
JH
100 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
101 rm -f "$GIT_DIR/REMOTE_HEAD"
59c93929
SV
102 if test -f "$GIT_DIR/REMOTE_HEAD"; then
103 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
104 case "$head_sha1" in
105 'ref: refs/'*)
106 ;;
107 *)
108 git-http-fetch $v -a "$head_sha1" "$1" ||
109 rm -f "$GIT_DIR/REMOTE_HEAD"
110 ;;
111 esac
112 fi
dfeff66e
JH
113}
114
167a4a33 115quiet=
ef5b4eab 116local=no
3d5c418f 117use_local_hardlink=yes
aae4f42c 118local_shared=no
a57c8bac 119unset template
036a72d8 120no_checkout=
6ec311da 121upload_pack=
87e80c4b 122bare=
dfeff66e
JH
123reference=
124origin=
e6489a1b 125origin_override=
71821351 126use_separate_remote=t
016e6ccb 127depth=
83a5ad61 128no_progress=
23d53358 129local_explicitly_asked_for=
83a5ad61 130test -t 1 || no_progress=--no-progress
94362599
PH
131
132while test $# != 0
133do
134 case "$1" in
135 -n|--no-checkout)
136 no_checkout=yes ;;
137 --naked|--bare)
138 bare=yes ;;
139 -l|--local)
140 local_explicitly_asked_for=yes
141 use_local_hardlink=yes
142 ;;
143 --no-hardlinks)
144 use_local_hardlink=no ;;
145 -s|--shared)
146 local_shared=yes ;;
147 --template)
a57c8bac 148 shift; template="--template=$1" ;;
94362599
PH
149 -q|--quiet)
150 quiet=-q ;;
151 --use-separate-remote|--no-separate-remote)
63085fab 152 die "clones are always made with separate-remote layout" ;;
94362599 153 --reference)
dfeff66e 154 shift; reference="$1" ;;
9d81e03b 155 -o|--origin)
94362599
PH
156 shift;
157 case "$1" in
98a4fef3
YS
158 '')
159 usage ;;
47874d6d 160 */*)
94362599 161 die "'$1' is not suitable for an origin name"
47874d6d 162 esac
94362599
PH
163 git check-ref-format "heads/$1" ||
164 die "'$1' is not suitable for a branch name"
87b787ac
DL
165 test -z "$origin_override" ||
166 die "Do not give more than one --origin options."
e6489a1b 167 origin_override=yes
94362599 168 origin="$1"
e6c310fd 169 ;;
94362599 170 -u|--upload-pack)
6ec311da 171 shift
ae1dffcb 172 upload_pack="--upload-pack=$1" ;;
94362599 173 --depth)
016e6ccb 174 shift
94362599
PH
175 depth="--depth=$1" ;;
176 --)
19391c37
HO
177 shift
178 break ;;
94362599
PH
179 *)
180 usage ;;
e95ab1ed 181 esac
e95ab1ed
JH
182 shift
183done
184
ef5b4eab 185repo="$1"
87b787ac
DL
186test -n "$repo" ||
187 die 'you must specify a repository to clone.'
ef5b4eab 188
b360cca0 189# --bare implies --no-checkout and --no-separate-remote
e6489a1b
JH
190if test yes = "$bare"
191then
192 if test yes = "$origin_override"
193 then
87b787ac 194 die '--bare and --origin $origin options are incompatible.'
e6489a1b
JH
195 fi
196 no_checkout=yes
71821351 197 use_separate_remote=
e6489a1b 198fi
8a1a120c 199
47874d6d 200if test -z "$origin"
dfeff66e 201then
47874d6d 202 origin=origin
dfeff66e
JH
203fi
204
ba375acf
LT
205# Turn the source into an absolute path if
206# it is local
ba375acf
LT
207if base=$(get_repo_base "$repo"); then
208 repo="$base"
d4110a97
CB
209 if test -z "$depth"
210 then
211 local=yes
212 fi
ba375acf
LT
213fi
214
3f571e0b 215dir="$2"
0879aa28 216# Try using "humanish" part of source repo if user didn't specify one
e7555785 217[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
87b787ac 218[ -e "$dir" ] && die "destination directory '$dir' already exists."
20ccef49
ML
219[ yes = "$bare" ] && unset GIT_WORK_TREE
220[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
221die "working tree '$GIT_WORK_TREE' already exists."
68ad8910 222D=
20ccef49 223W=
68ad8910
ML
224cleanup() {
225 err=$?
226 test -z "$D" && rm -rf "$dir"
20ccef49 227 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
68ad8910
ML
228 cd ..
229 test -n "$D" && rm -rf "$D"
20ccef49 230 test -n "$W" && rm -rf "$W"
68ad8910
ML
231 exit $err
232}
233trap cleanup 0
234mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
20ccef49 235test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
3addc94a 236W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
20ccef49
ML
237if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
238 GIT_DIR="$D"
239else
240 GIT_DIR="$D/.git"
241fi &&
68ad8910 242export GIT_DIR &&
312efe9b
JH
243GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
244
245if test -n "$bare"
246then
247 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
248fi
e95ab1ed 249
dfeff66e
JH
250if test -n "$reference"
251then
099c7837 252 ref_git=
dfeff66e
JH
253 if test -d "$reference"
254 then
255 if test -d "$reference/.git/objects"
256 then
099c7837
JH
257 ref_git="$reference/.git"
258 elif test -d "$reference/objects"
259 then
260 ref_git="$reference"
dfeff66e 261 fi
099c7837
JH
262 fi
263 if test -n "$ref_git"
264 then
265 ref_git=$(cd "$ref_git" && pwd)
266 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
267 (
268 GIT_DIR="$ref_git" git for-each-ref \
269 --format='%(objectname) %(*objectname)'
270 ) |
271 while read a b
272 do
273 test -z "$a" ||
274 git update-ref "refs/reference-tmp/$a" "$a"
275 test -z "$b" ||
276 git update-ref "refs/reference-tmp/$b" "$b"
277 done
dfeff66e 278 else
87b787ac 279 die "reference repository '$reference' is not a local directory."
dfeff66e
JH
280 fi
281fi
282
283rm -f "$GIT_DIR/CLONE_HEAD"
284
e95ab1ed 285# We do local magic only when the user tells us to.
3d5c418f
JH
286case "$local" in
287yes)
87b787ac 288 ( cd "$repo/objects" ) ||
3d5c418f 289 die "cannot chdir to local '$repo/objects'."
e95ab1ed 290
3d5c418f
JH
291 if test "$local_shared" = yes
292 then
293 mkdir -p "$GIT_DIR/objects/info"
294 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
295 else
296 l= &&
297 if test "$use_local_hardlink" = yes
298 then
299 # See if we can hardlink and drop "l" if not.
300 sample_file=$(cd "$repo" && \
301 find objects -type f -print | sed -e 1q)
302 # objects directory should not be empty because
303 # we are cloning!
ef4cffde
JK
304 test -f "$repo/$sample_file" ||
305 die "fatal: cannot clone empty repository"
3d5c418f
JH
306 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
307 then
308 rm -f "$GIT_DIR/objects/sample"
309 l=l
23d53358
JH
310 elif test -n "$local_explicitly_asked_for"
311 then
3d5c418f
JH
312 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
313 fi
314 fi &&
315 cd "$repo" &&
316 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
317 fi
57a39690 318 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
7558ef89
LT
319 ;;
320*)
1cadb5a2
JH
321 case "$repo" in
322 rsync://*)
016e6ccb
JS
323 case "$depth" in
324 "") ;;
325 *) die "shallow over rsync not supported" ;;
326 esac
4447badc 327 rsync $quiet -av --ignore-existing \
dfeff66e
JH
328 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
329 exit
4447badc
JH
330 # Look at objects/info/alternates for rsync -- http will
331 # support it natively and git native ones will do it on the
332 # remote end. Not having that file is not a crime.
89d844d0 333 rsync -q "$repo/objects/info/alternates" \
8a1a120c
JH
334 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
335 rm -f "$GIT_DIR/TMP_ALT"
336 if test -f "$GIT_DIR/TMP_ALT"
4447badc 337 then
0e9ab02d 338 ( cd "$D" &&
4447badc 339 . git-parse-remote &&
8a1a120c 340 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
4447badc
JH
341 while read alt
342 do
343 case "$alt" in 'bad alternate: '*) die "$alt";; esac
344 case "$quiet" in
345 '') echo >&2 "Getting alternate: $alt" ;;
346 esac
347 rsync $quiet -av --ignore-existing \
8a1a120c 348 --exclude info "$alt" "$GIT_DIR/objects" || exit
4447badc 349 done
8a1a120c 350 rm -f "$GIT_DIR/TMP_ALT"
4447badc 351 fi
57a39690 352 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
1cadb5a2 353 ;;
38529e28 354 https://*|http://*|ftp://*)
016e6ccb
JS
355 case "$depth" in
356 "") ;;
357 *) die "shallow over http or ftp not supported" ;;
358 esac
6c5c62f3
FP
359 if test -z "@@NO_CURL@@"
360 then
361 clone_dumb_http "$repo" "$D"
362 else
87b787ac 363 die "http transport not supported, rebuild Git with curl support"
6c5c62f3 364 fi
1cadb5a2
JH
365 ;;
366 *)
ced78b39 367 case "$upload_pack" in
83a5ad61
JS
368 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
369 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
87b787ac
DL
370 esac >"$GIT_DIR/CLONE_HEAD" ||
371 die "fetch-pack from '$repo' failed."
1cadb5a2 372 ;;
6ec311da 373 esac
7558ef89
LT
374 ;;
375esac
dfeff66e
JH
376test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
377
378if test -f "$GIT_DIR/CLONE_HEAD"
379then
c72112e6 380 # Read git-fetch-pack -k output and store the remote branches.
def2747d
SS
381 if [ -n "$use_separate_remote" ]
382 then
383 branch_top="remotes/$origin"
384 else
385 branch_top="heads"
386 fi
387 tag_top="tags"
388 while read sha1 name
389 do
390 case "$name" in
391 *'^{}')
392 continue ;;
393 HEAD)
394 destname="REMOTE_HEAD" ;;
395 refs/heads/*)
396 destname="refs/$branch_top/${name#refs/heads/}" ;;
397 refs/tags/*)
398 destname="refs/$tag_top/${name#refs/tags/}" ;;
399 *)
400 continue ;;
401 esac
5be60078 402 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
def2747d 403 done < "$GIT_DIR/CLONE_HEAD"
dfeff66e 404fi
1cadb5a2 405
20ccef49
ML
406if test -n "$W"; then
407 cd "$W" || exit
408else
409 cd "$D" || exit
410fi
036a72d8 411
5274ba69 412if test -z "$bare"
036a72d8 413then
63085fab
JH
414 # a non-bare repository is always in separate-remote layout
415 remote_top="refs/remotes/$origin"
5274ba69
GP
416 head_sha1=
417 test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
c72112e6
JH
418 case "$head_sha1" in
419 'ref: refs/'*)
420 # Uh-oh, the remote told us (http transport done against
421 # new style repository with a symref HEAD).
422 # Ideally we should skip the guesswork but for now
423 # opt for minimum change.
f327dbce 424 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
c72112e6
JH
425 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
426 ;;
427 esac
47874d6d 428
c72112e6 429 # The name under $remote_top the remote HEAD seems to point at.
dfeff66e
JH
430 head_points_at=$(
431 (
3fe71f3a 432 test -f "$GIT_DIR/$remote_top/master" && echo "master"
dfeff66e
JH
433 cd "$GIT_DIR/$remote_top" &&
434 find . -type f -print | sed -e 's/^\.\///'
435 ) | (
436 done=f
437 while read name
438 do
439 test t = $done && continue
440 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
441 if test "$head_sha1" = "$branch_tip"
442 then
443 echo "$name"
444 done=t
445 fi
446 done
447 )
448 )
47874d6d 449
6762079a 450 # Upstream URL
5be60078 451 git config remote."$origin".url "$repo" &&
6762079a
NS
452
453 # Set up the mappings to track the remote branches.
5be60078 454 git config remote."$origin".fetch \
6762079a
NS
455 "+refs/heads/*:$remote_top/*" '^$' &&
456
61dde8f9 457 # Write out remote.$origin config, and update our "$head_points_at".
e125c1a7 458 case "$head_points_at" in
dfeff66e 459 ?*)
61dde8f9 460 # Local default branch
5be60078 461 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
61dde8f9
JH
462
463 # Tracking branch for the primary branch at the remote.
5be60078 464 git update-ref HEAD "$head_sha1" &&
61dde8f9 465
63085fab 466 rm -f "refs/remotes/$origin/HEAD"
5be60078 467 git symbolic-ref "refs/remotes/$origin/HEAD" \
63085fab 468 "refs/remotes/$origin/$head_points_at" &&
61dde8f9 469
5be60078
JH
470 git config branch."$head_points_at".remote "$origin" &&
471 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
6762079a
NS
472 ;;
473 '')
5274ba69
GP
474 if test -z "$head_sha1"
475 then
476 # Source had nonexistent ref in HEAD
477 echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
478 no_checkout=t
479 else
480 # Source had detached HEAD pointing nowhere
481 git update-ref --no-deref HEAD "$head_sha1" &&
482 rm -f "refs/remotes/$origin/HEAD"
483 fi
6762079a 484 ;;
e125c1a7
JH
485 esac
486
036a72d8
JH
487 case "$no_checkout" in
488 '')
b0e90897 489 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
5be60078 490 git read-tree -m -u $v HEAD HEAD
036a72d8
JH
491 esac
492fi
dfeff66e 493rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
41ff7a10 494
f803eec5 495trap - 0