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