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.
12 echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>"
17 (cd "$1" && (cd .git
; pwd)) 2> /dev
/null
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
25 # $1 = Remote, $2 = Local
26 curl
-nsf $curl_extra_args "$1" >"$2"
30 # $1 - remote, $2 - local
32 clone_tmp
='.git/clone-tmp' &&
33 mkdir
-p "$clone_tmp" ||
exit 1
34 http_fetch
"$1/info/refs" "$clone_tmp/refs" &&
35 http_fetch
"$1/objects/info/packs" "$clone_tmp/packs" ||
{
36 echo >&2 "Cannot get remote repository information.
37 Perhaps git-update-server-info needs to be run there?"
47 idx
=`expr "$name" : '\(.*\)\.pack'`.idx
48 http_fetch
"$1/objects/pack/$name" ".git/objects/pack/$name" &&
49 http_fetch
"$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
50 git-verify-pack
".git/objects/pack/$idx" ||
exit 1
51 done <"$clone_tmp/packs"
53 while read sha1 refname
55 name
=`expr "$refname" : 'refs/\(.*\)'` &&
56 git-http-fetch
-v -a -w "$name" "$name" "$1/" ||
exit 1
57 done <"$clone_tmp/refs"
68 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
69 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
71 *,-q|
*,--quiet) quiet
=-q ;;
72 1,-u|
1,--upload-pack) usage
;;
75 upload_pack
="--exec=$1" ;;
83 # Turn the source into an absolute path if
87 if base
=$
(get_repo_base
"$repo"); then
95 (cd "$dir" && git-init-db
&& pwd)
99 # We do local magic only when the user tells us to.
100 case "$local,$use_local" in
102 ( cd "$repo/objects" ) ||
{
103 echo >&2 "-l flag seen but $repo is not local."
107 case "$local_shared" in
109 # See if we can hardlink and drop "l" if not.
110 sample_file
=$
(cd "$repo" && \
111 find objects
-type f
-print |
sed -e 1q
)
113 # objects directory should not be empty since we are cloning!
114 test -f "$repo/$sample_file" ||
exit
117 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev
/null
121 rm -f "$D/.git/objects/sample" &&
123 find objects
-type f
-print |
124 cpio -puamd$l "$D/.git/" ||
exit 1
127 mkdir
-p "$D/.git/objects/info"
129 test -f "$repo/objects/info/alternates" &&
130 cat "$repo/objects/info/alternates";
132 } >"$D/.git/objects/info/alternates"
136 # Make a duplicate of refs and HEAD pointer
138 if test -f "$repo/HEAD"
142 tar Ccf
"$repo" - refs
$HEAD |
tar Cxf
"$D/.git" - ||
exit 1
147 rsync
$quiet -av --ignore-existing \
148 --exclude info
"$repo/objects/" "$D/.git/objects/" &&
149 rsync
$quiet -av --ignore-existing \
150 --exclude info
"$repo/refs/" "$D/.git/refs/" ||
exit
152 # Look at objects/info/alternates for rsync -- http will
153 # support it natively and git native ones will do it on the
154 # remote end. Not having that file is not a crime.
155 rsync
-q "$repo/objects/info/alternates" "$D/.git/TMP_ALT" ||
156 rm -f "$D/.git/TMP_ALT"
157 if test -f "$D/.git/TMP_ALT"
160 . git-parse-remote
&&
161 resolve_alternates
"$repo" <"./.git/TMP_ALT" ) |
164 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
166 '') echo >&2 "Getting alternate: $alt" ;;
168 rsync
$quiet -av --ignore-existing \
169 --exclude info
"$alt" "$D/.git/objects" ||
exit
171 rm -f "$D/.git/TMP_ALT"
175 clone_dumb_http
"$repo" "$D"
178 cd "$D" && case "$upload_pack" in
179 '') git-clone-pack
$quiet "$repo" ;;
180 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
188 mkdir
-p "$D/.git/remotes/" &&
189 rm -f "$D/.git/remotes/origin" &&
190 echo >"$D/.git/remotes/origin" \