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 test "z$quiet" = z
&& v
=-v || v
=
46 while read sha1 refname
48 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
53 yes,* |
,heads
/* |
,tags
/*) ;;
56 if test -n "$use_separate_remote" &&
57 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
59 tname
="remotes/$origin/$branch_name"
63 git-http-fetch
$v -a -w "$tname" "$sha1" "$1" ||
exit 1
64 done <"$clone_tmp/refs"
66 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
67 rm -f "$GIT_DIR/REMOTE_HEAD"
84 test -t 1 || no_progress
=--no-progress
88 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
89 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
91 *,--na|
*,--nak|
*,--nake|
*,--naked|\
92 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
93 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
94 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
95 local_shared
=yes; use_local
=yes ;;
96 1,--template) usage
;;
98 shift; template
="--template=$1" ;;
101 *,-q|
*,--quiet) quiet
=-q ;;
102 *,--use-separate-remote) ;;
103 *,--no-separate-remote)
104 die
"clones are always made with separate-remote layout" ;;
105 1,--reference) usage
;;
107 shift; reference
="$1" ;;
109 reference
=`expr "z$1" : 'z--reference=\(.*\)'` ;;
110 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
115 die
"'$2' is not suitable for an origin name"
117 git-check-ref-format
"heads/$2" ||
118 die
"'$2' is not suitable for a branch name"
119 test -z "$origin_override" ||
120 die
"Do not give more than one --origin options."
124 1,-u|
1,--upload-pack) usage
;;
125 *,-u|
*,--upload-pack)
127 upload_pack
="--upload-pack=$1" ;;
129 upload_pack
=--upload-pack=$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
143 die
'you must specify a repository to clone.'
145 # --bare implies --no-checkout and --no-separate-remote
146 if test yes = "$bare"
148 if test yes = "$origin_override"
150 die
'--bare and --origin $origin options are incompatible.'
161 # Turn the source into an absolute path if
163 if base
=$
(get_repo_base
"$repo"); then
169 # Try using "humanish" part of source repo if user didn't specify one
170 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
171 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
173 D
=$
(cd "$dir" && pwd) &&
174 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
180 esac && export GIT_DIR
&& git-init
${template+"$template"} || usage
182 if test -n "$reference"
185 if test -d "$reference"
187 if test -d "$reference/.git/objects"
189 ref_git
="$reference/.git"
190 elif test -d "$reference/objects"
195 if test -n "$ref_git"
197 ref_git
=$
(cd "$ref_git" && pwd)
198 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
200 GIT_DIR
="$ref_git" git for-each-ref \
201 --format='%(objectname) %(*objectname)'
206 git update-ref
"refs/reference-tmp/$a" "$a"
208 git update-ref
"refs/reference-tmp/$b" "$b"
211 die
"reference repository '$reference' is not a local directory."
215 rm -f "$GIT_DIR/CLONE_HEAD"
217 # We do local magic only when the user tells us to.
218 case "$local,$use_local" in
220 ( cd "$repo/objects" ) ||
221 die
"-l flag seen but repository '$repo' is not local."
223 case "$local_shared" in
225 # See if we can hardlink and drop "l" if not.
226 sample_file
=$
(cd "$repo" && \
227 find objects
-type f
-print |
sed -e 1q
)
229 # objects directory should not be empty since we are cloning!
230 test -f "$repo/$sample_file" ||
exit
233 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
237 rm -f "$GIT_DIR/objects/sample" &&
239 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
242 mkdir
-p "$GIT_DIR/objects/info"
243 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
246 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
253 *) die
"shallow over rsync not supported" ;;
255 rsync
$quiet -av --ignore-existing \
256 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
258 # Look at objects/info/alternates for rsync -- http will
259 # support it natively and git native ones will do it on the
260 # remote end. Not having that file is not a crime.
261 rsync
-q "$repo/objects/info/alternates" \
262 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
263 rm -f "$GIT_DIR/TMP_ALT"
264 if test -f "$GIT_DIR/TMP_ALT"
267 . git-parse-remote
&&
268 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
271 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
273 '') echo >&2 "Getting alternate: $alt" ;;
275 rsync
$quiet -av --ignore-existing \
276 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
278 rm -f "$GIT_DIR/TMP_ALT"
280 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
282 https
://*|http
://*|
ftp://*)
285 *) die
"shallow over http or ftp not supported" ;;
287 if test -z "@@NO_CURL@@"
289 clone_dumb_http
"$repo" "$D"
291 die
"http transport not supported, rebuild Git with curl support"
295 case "$upload_pack" in
296 '') git-fetch-pack
--all -k $quiet $depth $no_progress "$repo";;
297 *) git-fetch-pack
--all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
298 esac >"$GIT_DIR/CLONE_HEAD" ||
299 die
"fetch-pack from '$repo' failed."
304 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
306 if test -f "$GIT_DIR/CLONE_HEAD"
308 # Read git-fetch-pack -k output and store the remote branches.
309 if [ -n "$use_separate_remote" ]
311 branch_top
="remotes/$origin"
322 destname
="REMOTE_HEAD" ;;
324 destname
="refs/$branch_top/${name#refs/heads/}" ;;
326 destname
="refs/$tag_top/${name#refs/tags/}" ;;
330 git-update-ref
-m "clone: from $repo" "$destname" "$sha1" ""
331 done < "$GIT_DIR/CLONE_HEAD"
336 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
338 # a non-bare repository is always in separate-remote layout
339 remote_top
="refs/remotes/$origin"
340 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
343 # Uh-oh, the remote told us (http transport done against
344 # new style repository with a symref HEAD).
345 # Ideally we should skip the guesswork but for now
346 # opt for minimum change.
347 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
348 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
352 # The name under $remote_top the remote HEAD seems to point at.
355 test -f "$GIT_DIR/$remote_top/master" && echo "master"
356 cd "$GIT_DIR/$remote_top" &&
357 find .
-type f
-print |
sed -e 's/^\.\///'
362 test t
= $done && continue
363 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
364 if test "$head_sha1" = "$branch_tip"
373 # Write out remote.$origin config, and update our "$head_points_at".
374 case "$head_points_at" in
376 # Local default branch
377 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
379 # Tracking branch for the primary branch at the remote.
380 origin_track
="$remote_top/$head_points_at" &&
381 git-update-ref HEAD
"$head_sha1" &&
384 git-config remote.
"$origin".url
"$repo" &&
386 # Set up the mappings to track the remote branches.
387 git-config remote.
"$origin".fetch \
388 "+refs/heads/*:$remote_top/*" '^$' &&
389 rm -f "refs/remotes/$origin/HEAD"
390 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
391 "refs/remotes/$origin/$head_points_at" &&
393 git-config branch.
"$head_points_at".remote
"$origin" &&
394 git-config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
397 case "$no_checkout" in
399 test "z$quiet" = z
-a "z$no_progress" = z
&& v
=-v || v
=
400 git-read-tree
-m -u $v HEAD HEAD
403 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"