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