"git-push $URL" without refspecs pushes only matching branches
[debian-git.git] / git-clone.sh
blob4cbf60f554cee4560559cc3aa4e7d6fbb90e68ed
1 #!/bin/sh
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
6 # Clone a repository into a different directory that does not yet exist.
8 # See git-sh-setup why.
9 unset CDPATH
11 die() {
12 echo >&2 "$@"
13 exit 1
16 usage() {
17 die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
20 get_repo_base() {
22 cd "`/bin/pwd`" &&
23 cd "$1" || cd "$1.git" &&
25 cd .git
26 pwd
28 ) 2>/dev/null
31 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
32 curl_extra_args="-k"
35 http_fetch () {
36 # $1 = Remote, $2 = Local
37 curl -nsfL $curl_extra_args "$1" >"$2"
40 clone_dumb_http () {
41 # $1 - remote, $2 - local
42 cd "$2" &&
43 clone_tmp="$GIT_DIR/clone-tmp" &&
44 mkdir -p "$clone_tmp" || exit 1
45 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
46 "`git-config --bool http.noEPSV`" = true ]; then
47 curl_extra_args="${curl_extra_args} --disable-epsv"
49 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
50 die "Cannot get remote repository information.
51 Perhaps git-update-server-info needs to be run there?"
52 test "z$quiet" = z && v=-v || v=
53 while read sha1 refname
55 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
56 case "$name" in
57 *^*) continue;;
58 esac
59 case "$bare,$name" in
60 yes,* | ,heads/* | ,tags/*) ;;
61 *) continue ;;
62 esac
63 if test -n "$use_separate_remote" &&
64 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
65 then
66 tname="remotes/$origin/$branch_name"
67 else
68 tname=$name
70 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
71 done <"$clone_tmp/refs"
72 rm -fr "$clone_tmp"
73 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
74 rm -f "$GIT_DIR/REMOTE_HEAD"
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/'*)
81 git-http-fetch $v -a "$head_sha1" "$1" ||
82 rm -f "$GIT_DIR/REMOTE_HEAD"
84 esac
88 quiet=
89 local=no
90 use_local=no
91 local_shared=no
92 unset template
93 no_checkout=
94 upload_pack=
95 bare=
96 reference=
97 origin=
98 origin_override=
99 use_separate_remote=t
100 depth=
101 no_progress=
102 test -t 1 || no_progress=--no-progress
103 while
104 case "$#,$1" in
105 0,*) break ;;
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 ;;
109 *,--na|*,--nak|*,--nake|*,--naked|\
110 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
111 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
112 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
113 local_shared=yes; use_local=yes ;;
114 1,--template) usage ;;
115 *,--template)
116 shift; template="--template=$1" ;;
117 *,--template=*)
118 template="$1" ;;
119 *,-q|*,--quiet) quiet=-q ;;
120 *,--use-separate-remote) ;;
121 *,--no-separate-remote)
122 die "clones are always made with separate-remote layout" ;;
123 1,--reference) usage ;;
124 *,--reference)
125 shift; reference="$1" ;;
126 *,--reference=*)
127 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
128 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
129 case "$2" in
131 usage ;;
132 */*)
133 die "'$2' is not suitable for an origin name"
134 esac
135 git-check-ref-format "heads/$2" ||
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."
139 origin_override=yes
140 origin="$2"; shift
142 1,-u|1,--upload-pack) usage ;;
143 *,-u|*,--upload-pack)
144 shift
145 upload_pack="--upload-pack=$1" ;;
146 *,--upload-pack=*)
147 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
148 1,--depth) usage;;
149 *,--depth)
150 shift
151 depth="--depth=$1";;
152 *,-*) usage ;;
153 *) break ;;
154 esac
156 shift
157 done
159 repo="$1"
160 test -n "$repo" ||
161 die 'you must specify a repository to clone.'
163 # --bare implies --no-checkout and --no-separate-remote
164 if test yes = "$bare"
165 then
166 if test yes = "$origin_override"
167 then
168 die '--bare and --origin $origin options are incompatible.'
170 no_checkout=yes
171 use_separate_remote=
174 if test -z "$origin"
175 then
176 origin=origin
179 # Turn the source into an absolute path if
180 # it is local
181 if base=$(get_repo_base "$repo"); then
182 repo="$base"
183 local=yes
186 dir="$2"
187 # Try using "humanish" part of source repo if user didn't specify one
188 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
189 [ -e "$dir" ] && die "destination directory '$dir' already exists."
190 mkdir -p "$dir" &&
191 D=$(cd "$dir" && pwd) &&
192 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
193 case "$bare" in
194 yes)
195 GIT_DIR="$D" ;;
197 GIT_DIR="$D/.git" ;;
198 esac && export GIT_DIR && git-init ${template+"$template"} || usage
200 if test -n "$reference"
201 then
202 ref_git=
203 if test -d "$reference"
204 then
205 if test -d "$reference/.git/objects"
206 then
207 ref_git="$reference/.git"
208 elif test -d "$reference/objects"
209 then
210 ref_git="$reference"
213 if test -n "$ref_git"
214 then
215 ref_git=$(cd "$ref_git" && pwd)
216 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
218 GIT_DIR="$ref_git" git for-each-ref \
219 --format='%(objectname) %(*objectname)'
221 while read a b
223 test -z "$a" ||
224 git update-ref "refs/reference-tmp/$a" "$a"
225 test -z "$b" ||
226 git update-ref "refs/reference-tmp/$b" "$b"
227 done
228 else
229 die "reference repository '$reference' is not a local directory."
233 rm -f "$GIT_DIR/CLONE_HEAD"
235 # We do local magic only when the user tells us to.
236 case "$local,$use_local" in
237 yes,yes)
238 ( cd "$repo/objects" ) ||
239 die "-l flag seen but repository '$repo' is not local."
241 case "$local_shared" in
243 # See if we can hardlink and drop "l" if not.
244 sample_file=$(cd "$repo" && \
245 find objects -type f -print | sed -e 1q)
247 # objects directory should not be empty since we are cloning!
248 test -f "$repo/$sample_file" || exit
251 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
252 then
254 fi &&
255 rm -f "$GIT_DIR/objects/sample" &&
256 cd "$repo" &&
257 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
259 yes)
260 mkdir -p "$GIT_DIR/objects/info"
261 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
263 esac
264 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
267 case "$repo" in
268 rsync://*)
269 case "$depth" in
270 "") ;;
271 *) die "shallow over rsync not supported" ;;
272 esac
273 rsync $quiet -av --ignore-existing \
274 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
275 exit
276 # Look at objects/info/alternates for rsync -- http will
277 # support it natively and git native ones will do it on the
278 # remote end. Not having that file is not a crime.
279 rsync -q "$repo/objects/info/alternates" \
280 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
281 rm -f "$GIT_DIR/TMP_ALT"
282 if test -f "$GIT_DIR/TMP_ALT"
283 then
284 ( cd "$D" &&
285 . git-parse-remote &&
286 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
287 while read alt
289 case "$alt" in 'bad alternate: '*) die "$alt";; esac
290 case "$quiet" in
291 '') echo >&2 "Getting alternate: $alt" ;;
292 esac
293 rsync $quiet -av --ignore-existing \
294 --exclude info "$alt" "$GIT_DIR/objects" || exit
295 done
296 rm -f "$GIT_DIR/TMP_ALT"
298 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
300 https://*|http://*|ftp://*)
301 case "$depth" in
302 "") ;;
303 *) die "shallow over http or ftp not supported" ;;
304 esac
305 if test -z "@@NO_CURL@@"
306 then
307 clone_dumb_http "$repo" "$D"
308 else
309 die "http transport not supported, rebuild Git with curl support"
313 case "$upload_pack" in
314 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
315 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
316 esac >"$GIT_DIR/CLONE_HEAD" ||
317 die "fetch-pack from '$repo' failed."
319 esac
321 esac
322 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
324 if test -f "$GIT_DIR/CLONE_HEAD"
325 then
326 # Read git-fetch-pack -k output and store the remote branches.
327 if [ -n "$use_separate_remote" ]
328 then
329 branch_top="remotes/$origin"
330 else
331 branch_top="heads"
333 tag_top="tags"
334 while read sha1 name
336 case "$name" in
337 *'^{}')
338 continue ;;
339 HEAD)
340 destname="REMOTE_HEAD" ;;
341 refs/heads/*)
342 destname="refs/$branch_top/${name#refs/heads/}" ;;
343 refs/tags/*)
344 destname="refs/$tag_top/${name#refs/tags/}" ;;
346 continue ;;
347 esac
348 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
349 done < "$GIT_DIR/CLONE_HEAD"
352 cd "$D" || exit
354 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
355 then
356 # a non-bare repository is always in separate-remote layout
357 remote_top="refs/remotes/$origin"
358 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
359 case "$head_sha1" in
360 'ref: refs/'*)
361 # Uh-oh, the remote told us (http transport done against
362 # new style repository with a symref HEAD).
363 # Ideally we should skip the guesswork but for now
364 # opt for minimum change.
365 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
366 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
368 esac
370 # The name under $remote_top the remote HEAD seems to point at.
371 head_points_at=$(
373 test -f "$GIT_DIR/$remote_top/master" && echo "master"
374 cd "$GIT_DIR/$remote_top" &&
375 find . -type f -print | sed -e 's/^\.\///'
376 ) | (
377 done=f
378 while read name
380 test t = $done && continue
381 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
382 if test "$head_sha1" = "$branch_tip"
383 then
384 echo "$name"
385 done=t
387 done
391 # Upstream URL
392 git-config remote."$origin".url "$repo" &&
394 # Set up the mappings to track the remote branches.
395 git-config remote."$origin".fetch \
396 "+refs/heads/*:$remote_top/*" '^$' &&
398 # Write out remote.$origin config, and update our "$head_points_at".
399 case "$head_points_at" in
401 # Local default branch
402 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
404 # Tracking branch for the primary branch at the remote.
405 git-update-ref HEAD "$head_sha1" &&
407 rm -f "refs/remotes/$origin/HEAD"
408 git-symbolic-ref "refs/remotes/$origin/HEAD" \
409 "refs/remotes/$origin/$head_points_at" &&
411 git-config branch."$head_points_at".remote "$origin" &&
412 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
415 # Source had detached HEAD pointing nowhere
416 git-update-ref --no-deref HEAD "$head_sha1" &&
417 rm -f "refs/remotes/$origin/HEAD"
419 esac
421 case "$no_checkout" in
423 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
424 git-read-tree -m -u $v HEAD HEAD
425 esac
427 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
429 trap - 0