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