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.
17 die
"Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
21 (cd "$1" && (cd .git
; pwd)) 2> /dev
/null
24 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
29 # $1 = Remote, $2 = Local
30 curl
-nsfL $curl_extra_args "$1" >"$2"
34 # $1 - remote, $2 - local
36 clone_tmp
="$GIT_DIR/clone-tmp" &&
37 mkdir
-p "$clone_tmp" ||
exit 1
38 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
39 "`git-config --bool http.noEPSV`" = true
]; then
40 curl_extra_args
="${curl_extra_args} --disable-epsv"
42 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
43 die
"Cannot get remote repository information.
44 Perhaps git-update-server-info needs to be run there?"
45 while read sha1 refname
47 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
52 yes,* |
,heads
/* |
,tags
/*) ;;
55 if test -n "$use_separate_remote" &&
56 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
58 tname
="remotes/$origin/$branch_name"
62 git-http-fetch
-v -a -w "$tname" "$name" "$1/" ||
exit 1
63 done <"$clone_tmp/refs"
65 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
66 rm -f "$GIT_DIR/REMOTE_HEAD"
85 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
86 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
88 *,--na|
*,--nak|
*,--nake|
*,--naked|\
89 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
90 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
91 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
92 local_shared
=yes; use_local
=yes ;;
93 1,--template) usage
;;
95 shift; template
="--template=$1" ;;
98 *,-q|
*,--quiet) quiet
=-q ;;
99 *,--use-separate-remote) ;;
100 *,--no-separate-remote)
101 die
"clones are always made with separate-remote layout" ;;
102 1,--reference) usage
;;
104 shift; reference
="$1" ;;
106 reference
=`expr "z$1" : 'z--reference=\(.*\)'` ;;
107 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
112 die
"'$2' is not suitable for an origin name"
114 git-check-ref-format
"heads/$2" ||
115 die
"'$2' is not suitable for a branch name"
116 test -z "$origin_override" ||
117 die
"Do not give more than one --origin options."
121 1,-u|
1,--upload-pack) usage
;;
122 *,-u|
*,--upload-pack)
124 upload_pack
="--upload-pack=$1" ;;
126 upload_pack
=--upload-pack=$
(expr "$1" : '-[^=]*=\(.*\)') ;;
140 die
'you must specify a repository to clone.'
142 # --bare implies --no-checkout and --no-separate-remote
143 if test yes = "$bare"
145 if test yes = "$origin_override"
147 die
'--bare and --origin $origin options are incompatible.'
158 # Turn the source into an absolute path if
160 if base
=$
(get_repo_base
"$repo"); then
166 # Try using "humanish" part of source repo if user didn't specify one
167 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
168 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
170 D
=$
(cd "$dir" && pwd) &&
171 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
177 esac && export GIT_DIR
&& git-init
${template+"$template"} || usage
179 if test -n "$reference"
181 if test -d "$reference"
183 if test -d "$reference/.git/objects"
185 reference
="$reference/.git"
187 reference
=$
(cd "$reference" && pwd)
188 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
189 (cd "$reference" && tar cf
- refs
) |
190 (cd "$GIT_DIR/refs" &&
191 mkdir reference-tmp
&&
195 die
"reference repository '$reference' is not a local directory."
199 rm -f "$GIT_DIR/CLONE_HEAD"
201 # We do local magic only when the user tells us to.
202 case "$local,$use_local" in
204 ( cd "$repo/objects" ) ||
205 die
"-l flag seen but repository '$repo' is not local."
207 case "$local_shared" in
209 # See if we can hardlink and drop "l" if not.
210 sample_file
=$
(cd "$repo" && \
211 find objects
-type f
-print |
sed -e 1q
)
213 # objects directory should not be empty since we are cloning!
214 test -f "$repo/$sample_file" ||
exit
217 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
221 rm -f "$GIT_DIR/objects/sample" &&
223 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
226 mkdir
-p "$GIT_DIR/objects/info"
227 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
230 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
237 *) die
"shallow over rsync not supported" ;;
239 rsync
$quiet -av --ignore-existing \
240 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
242 # Look at objects/info/alternates for rsync -- http will
243 # support it natively and git native ones will do it on the
244 # remote end. Not having that file is not a crime.
245 rsync
-q "$repo/objects/info/alternates" \
246 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
247 rm -f "$GIT_DIR/TMP_ALT"
248 if test -f "$GIT_DIR/TMP_ALT"
251 . git-parse-remote
&&
252 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
255 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
257 '') echo >&2 "Getting alternate: $alt" ;;
259 rsync
$quiet -av --ignore-existing \
260 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
262 rm -f "$GIT_DIR/TMP_ALT"
264 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
266 https
://*|http
://*|
ftp://*)
269 *) die
"shallow over http or ftp not supported" ;;
271 if test -z "@@NO_CURL@@"
273 clone_dumb_http
"$repo" "$D"
275 die
"http transport not supported, rebuild Git with curl support"
279 case "$upload_pack" in
280 '') git-fetch-pack
--all -k $quiet $depth "$repo" ;;
281 *) git-fetch-pack
--all -k $quiet "$upload_pack" $depth "$repo" ;;
282 esac >"$GIT_DIR/CLONE_HEAD" ||
283 die
"fetch-pack from '$repo' failed."
288 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
290 if test -f "$GIT_DIR/CLONE_HEAD"
292 # Read git-fetch-pack -k output and store the remote branches.
293 if [ -n "$use_separate_remote" ]
295 branch_top
="remotes/$origin"
306 destname
="REMOTE_HEAD" ;;
308 destname
="refs/$branch_top/${name#refs/heads/}" ;;
310 destname
="refs/$tag_top/${name#refs/tags/}" ;;
314 git-update-ref
-m "clone: from $repo" "$destname" "$sha1" ""
315 done < "$GIT_DIR/CLONE_HEAD"
320 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
322 # a non-bare repository is always in separate-remote layout
323 remote_top
="refs/remotes/$origin"
324 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
327 # Uh-oh, the remote told us (http transport done against
328 # new style repository with a symref HEAD).
329 # Ideally we should skip the guesswork but for now
330 # opt for minimum change.
331 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
332 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
336 # The name under $remote_top the remote HEAD seems to point at.
339 test -f "$GIT_DIR/$remote_top/master" && echo "master"
340 cd "$GIT_DIR/$remote_top" &&
341 find .
-type f
-print |
sed -e 's/^\.\///'
346 test t
= $done && continue
347 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
348 if test "$head_sha1" = "$branch_tip"
357 # Write out remote.$origin config, and update our "$head_points_at".
358 case "$head_points_at" in
360 # Local default branch
361 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
363 # Tracking branch for the primary branch at the remote.
364 origin_track
="$remote_top/$head_points_at" &&
365 git-update-ref HEAD
"$head_sha1" &&
368 git-config remote.
"$origin".url
"$repo" &&
370 # Set up the mappings to track the remote branches.
371 git-config remote.
"$origin".fetch \
372 "+refs/heads/*:$remote_top/*" '^$' &&
373 rm -f "refs/remotes/$origin/HEAD"
374 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
375 "refs/remotes/$origin/$head_points_at" &&
377 git-config branch.
"$head_points_at".remote
"$origin" &&
378 git-config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
381 case "$no_checkout" in
383 test "z$quiet" = z
&& v
=-v || v
=
384 git-read-tree
-m -u $v HEAD HEAD
387 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"