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