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>] [-n] <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
-nsfL $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/\(.*\)'` &&
59 git-http-fetch
-v -a -w "$name" "$name" "$1/" ||
exit 1
61 done <"$clone_tmp/refs"
73 *,-n) no_checkout
=yes ;;
74 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
75 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
77 *,-q|
*,--quiet) quiet
=-q ;;
78 1,-u|
1,--upload-pack) usage
;;
81 upload_pack
="--exec=$1" ;;
89 # Turn the source into an absolute path if
93 if base
=$
(get_repo_base
"$repo"); then
101 (cd "$dir" && git-init-db
&& pwd)
103 test -d "$D" || usage
105 # We do local magic only when the user tells us to.
106 case "$local,$use_local" in
108 ( cd "$repo/objects" ) ||
{
109 echo >&2 "-l flag seen but $repo is not local."
113 case "$local_shared" in
115 # See if we can hardlink and drop "l" if not.
116 sample_file
=$
(cd "$repo" && \
117 find objects
-type f
-print |
sed -e 1q
)
119 # objects directory should not be empty since we are cloning!
120 test -f "$repo/$sample_file" ||
exit
123 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev
/null
127 rm -f "$D/.git/objects/sample" &&
129 find objects
-depth -print |
cpio -puamd$l "$D/.git/" ||
exit 1
132 mkdir
-p "$D/.git/objects/info"
134 test -f "$repo/objects/info/alternates" &&
135 cat "$repo/objects/info/alternates";
137 } >"$D/.git/objects/info/alternates"
141 # Make a duplicate of refs and HEAD pointer
143 if test -f "$repo/HEAD"
147 (cd "$repo" && tar cf
- refs
$HEAD) |
148 (cd "$D/.git" && tar xf
-) ||
exit 1
153 rsync
$quiet -av --ignore-existing \
154 --exclude info
"$repo/objects/" "$D/.git/objects/" &&
155 rsync
$quiet -av --ignore-existing \
156 --exclude info
"$repo/refs/" "$D/.git/refs/" ||
exit
158 # Look at objects/info/alternates for rsync -- http will
159 # support it natively and git native ones will do it on the
160 # remote end. Not having that file is not a crime.
161 rsync
-q "$repo/objects/info/alternates" \
162 "$D/.git/TMP_ALT" 2>/dev
/null ||
163 rm -f "$D/.git/TMP_ALT"
164 if test -f "$D/.git/TMP_ALT"
167 . git-parse-remote
&&
168 resolve_alternates
"$repo" <"./.git/TMP_ALT" ) |
171 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
173 '') echo >&2 "Getting alternate: $alt" ;;
175 rsync
$quiet -av --ignore-existing \
176 --exclude info
"$alt" "$D/.git/objects" ||
exit
178 rm -f "$D/.git/TMP_ALT"
182 clone_dumb_http
"$repo" "$D"
185 cd "$D" && case "$upload_pack" in
186 '') git-clone-pack
$quiet "$repo" ;;
187 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
196 if test -f ".git/HEAD"
198 head_points_at
=`git-symbolic-ref HEAD`
199 case "$head_points_at" in
201 head_points_at
=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
202 mkdir
-p .git
/remotes
&&
203 echo >.git
/remotes
/origin \
205 Pull: $head_points_at:origin" &&
206 cp ".git/refs/heads/$head_points_at" .git
/refs
/heads
/origin
&&
207 find .git
/refs
/heads
-type f
-print |
210 head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
211 test "$head_points_at" = "$head" ||
212 test "origin" = "$head" ||
213 echo "Pull: ${head}:${head}"
214 done >>.git
/remotes
/origin
217 case "$no_checkout" in