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 [--bare] [-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"
62 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
63 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
65 *,--na|
*,--nak|
*,--nake|
*,--naked|\
66 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
67 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
68 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
69 local_shared
=yes; use_local
=yes ;;
70 *,-q|
*,--quiet) quiet
=-q ;;
73 git-check-ref-format
"$2" ||
{
74 echo >&2 "'$2' is not suitable for a branch name"
77 test -z "$origin_override" ||
{
78 echo >&2 "Do not give more than one -o options."
84 1,-u|
1,--upload-pack) usage
;;
87 upload_pack
="--exec=$1" ;;
95 # --bare implies --no-checkout
98 if test yes = "$origin_override"
100 echo >&2 '--bare and -o $origin options are incompatible.'
106 # Turn the source into an absolute path if
110 if base
=$
(get_repo_base
"$repo"); then
116 # Try using "humanish" part of source repo if user didn't specify one
117 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
118 [ -e "$dir" ] && echo "$dir already exists." && usage
120 D
=$
(cd "$dir" && pwd) &&
121 trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
124 *) GIT_DIR
="$D/.git" ;;
125 esac && export GIT_DIR
&& git-init-db || usage
133 # We do local magic only when the user tells us to.
134 case "$local,$use_local" in
136 ( cd "$repo/objects" ) ||
{
137 echo >&2 "-l flag seen but $repo is not local."
141 case "$local_shared" in
143 # See if we can hardlink and drop "l" if not.
144 sample_file
=$
(cd "$repo" && \
145 find objects
-type f
-print |
sed -e 1q
)
147 # objects directory should not be empty since we are cloning!
148 test -f "$repo/$sample_file" ||
exit
151 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
155 rm -f "$GIT_DIR/objects/sample" &&
157 find objects
-depth -print |
cpio -puamd$l "$GIT_DIR/" ||
exit 1
160 mkdir
-p "$GIT_DIR/objects/info"
162 test -f "$repo/objects/info/alternates" &&
163 cat "$repo/objects/info/alternates";
165 } >"$GIT_DIR/objects/info/alternates"
169 # Make a duplicate of refs and HEAD pointer
171 if test -f "$repo/HEAD"
175 (cd "$repo" && tar cf
- refs
$HEAD) |
176 (cd "$GIT_DIR" && tar xf
-) ||
exit 1
181 rsync
$quiet -av --ignore-existing \
182 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" &&
183 rsync
$quiet -av --ignore-existing \
184 --exclude info
"$repo/refs/" "$GIT_DIR/refs/" ||
exit
186 # Look at objects/info/alternates for rsync -- http will
187 # support it natively and git native ones will do it on the
188 # remote end. Not having that file is not a crime.
189 rsync
-q "$repo/objects/info/alternates" \
190 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
191 rm -f "$GIT_DIR/TMP_ALT"
192 if test -f "$GIT_DIR/TMP_ALT"
195 . git-parse-remote
&&
196 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
199 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
201 '') echo >&2 "Getting alternate: $alt" ;;
203 rsync
$quiet -av --ignore-existing \
204 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
206 rm -f "$GIT_DIR/TMP_ALT"
210 if test -z "@@NO_CURL@@"
212 clone_dumb_http
"$repo" "$D"
214 echo >&2 "http transport not supported, rebuild Git with curl support"
219 cd "$D" && case "$upload_pack" in
220 '') git-clone-pack
$quiet "$repo" ;;
221 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
223 echo >&2 "clone-pack from '$repo' failed."
233 if test -f "$GIT_DIR/HEAD" && test -z "$bare"
235 head_points_at
=`git-symbolic-ref HEAD`
236 case "$head_points_at" in
238 head_points_at
=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
239 mkdir
-p "$GIT_DIR/remotes" &&
240 echo >"$GIT_DIR/remotes/origin" \
242 Pull: $head_points_at:$origin" &&
243 git-update-ref
"refs/heads/$origin" $
(git-rev-parse HEAD
) &&
244 (cd "$GIT_DIR" && find "refs/heads" -type f
-print) |
247 head=`expr "$ref" : 'refs/heads/\(.*\)'` &&
248 test "$head_points_at" = "$head" ||
249 test "$origin" = "$head" ||
250 echo "Pull: ${head}:${head}"
251 done >>"$GIT_DIR/remotes/origin"
254 case "$no_checkout" in