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 "Usage: $0 [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-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 echo >&2 "Cannot get remote repository information.
36 Perhaps git-update-server-info needs to be run there?"
39 while read sha1 refname
41 name
=`expr "$refname" : 'refs/\(.*\)'` &&
45 git-http-fetch
-v -a -w "$name" "$name" "$1/" ||
exit 1
47 done <"$clone_tmp/refs"
60 *,-n) no_checkout
=yes ;;
61 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
62 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
63 local_shared
=yes; use_local
=yes ;;
64 *,-q|
*,--quiet) quiet
=-q ;;
67 git-check-ref-format
"$2" ||
{
68 echo >&2 "'$2' is not suitable for a branch name"
73 1,-u|
1,--upload-pack) usage
;;
76 upload_pack
="--exec=$1" ;;
84 # Turn the source into an absolute path if
88 if base
=$
(get_repo_base
"$repo"); then
94 # Try using "humanish" part of source repo if user didn't specify one
95 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g')
96 [ -e "$dir" ] && echo "$dir already exists." && usage
99 (cd "$dir" && git-init-db
&& pwd)
101 test -d "$D" || usage
103 # We do local magic only when the user tells us to.
104 case "$local,$use_local" in
106 ( cd "$repo/objects" ) ||
{
107 echo >&2 "-l flag seen but $repo is not local."
111 case "$local_shared" in
113 # See if we can hardlink and drop "l" if not.
114 sample_file
=$
(cd "$repo" && \
115 find objects
-type f
-print |
sed -e 1q
)
117 # objects directory should not be empty since we are cloning!
118 test -f "$repo/$sample_file" ||
exit
121 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev
/null
125 rm -f "$D/.git/objects/sample" &&
127 find objects
-depth -print |
cpio -puamd$l "$D/.git/" ||
exit 1
130 mkdir
-p "$D/.git/objects/info"
132 test -f "$repo/objects/info/alternates" &&
133 cat "$repo/objects/info/alternates";
135 } >"$D/.git/objects/info/alternates"
139 # Make a duplicate of refs and HEAD pointer
141 if test -f "$repo/HEAD"
145 (cd "$repo" && tar cf
- refs
$HEAD) |
146 (cd "$D/.git" && tar xf
-) ||
exit 1
151 rsync
$quiet -av --ignore-existing \
152 --exclude info
"$repo/objects/" "$D/.git/objects/" &&
153 rsync
$quiet -av --ignore-existing \
154 --exclude info
"$repo/refs/" "$D/.git/refs/" ||
exit
156 # Look at objects/info/alternates for rsync -- http will
157 # support it natively and git native ones will do it on the
158 # remote end. Not having that file is not a crime.
159 rsync
-q "$repo/objects/info/alternates" \
160 "$D/.git/TMP_ALT" 2>/dev
/null ||
161 rm -f "$D/.git/TMP_ALT"
162 if test -f "$D/.git/TMP_ALT"
165 . git-parse-remote
&&
166 resolve_alternates
"$repo" <"./.git/TMP_ALT" ) |
169 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
171 '') echo >&2 "Getting alternate: $alt" ;;
173 rsync
$quiet -av --ignore-existing \
174 --exclude info
"$alt" "$D/.git/objects" ||
exit
176 rm -f "$D/.git/TMP_ALT"
180 clone_dumb_http
"$repo" "$D"
183 cd "$D" && case "$upload_pack" in
184 '') git-clone-pack
$quiet "$repo" ;;
185 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
187 echo >&2 "clone-pack from '$repo' failed."
197 if test -f ".git/HEAD"
199 head_points_at
=`git-symbolic-ref HEAD`
200 case "$head_points_at" in
202 head_points_at
=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
203 mkdir
-p .git
/remotes
&&
204 echo >.git
/remotes
/origin \
206 Pull: $head_points_at:$origin" &&
207 git-update-ref
"refs/heads/$origin" $
(git-rev-parse HEAD
) &&
208 find .git
/refs
/heads
-type f
-print |
211 head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
212 test "$head_points_at" = "$head" ||
213 test "$origin" = "$head" ||
214 echo "Pull: ${head}:${head}"
215 done >>.git
/remotes
/origin
218 case "$no_checkout" in