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