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"
61 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
62 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
64 *,--na|
*,--nak|
*,--nake|
*,--naked|\
65 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
66 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
67 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
68 local_shared
=yes; use_local
=yes ;;
69 *,-q|
*,--quiet) quiet
=-q ;;
72 git-check-ref-format
"$2" ||
{
73 echo >&2 "'$2' is not suitable for a branch name"
78 1,-u|
1,--upload-pack) usage
;;
81 upload_pack
="--exec=$1" ;;
89 # --bare implies --no-checkout
90 test =z
"$bare" || no_checkout
=yes
92 # Turn the source into an absolute path if
96 if base
=$
(get_repo_base
"$repo"); then
102 # Try using "humanish" part of source repo if user didn't specify one
103 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
104 [ -e "$dir" ] && echo "$dir already exists." && usage
106 D
=$
(cd "$dir" && pwd) &&
109 *) GIT_DIR
="$D/.git" ;;
110 esac && export GIT_DIR
&& git-init-db || usage
118 # We do local magic only when the user tells us to.
119 case "$local,$use_local" in
121 ( cd "$repo/objects" ) ||
{
122 echo >&2 "-l flag seen but $repo is not local."
126 case "$local_shared" in
128 # See if we can hardlink and drop "l" if not.
129 sample_file
=$
(cd "$repo" && \
130 find objects
-type f
-print |
sed -e 1q
)
132 # objects directory should not be empty since we are cloning!
133 test -f "$repo/$sample_file" ||
exit
136 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
140 rm -f "$GIT_DIR/objects/sample" &&
142 find objects
-depth -print |
cpio -puamd$l "$GIT_DIR/" ||
exit 1
145 mkdir
-p "$GIT_DIR/objects/info"
147 test -f "$repo/objects/info/alternates" &&
148 cat "$repo/objects/info/alternates";
150 } >"$GIT_DIR/objects/info/alternates"
154 # Make a duplicate of refs and HEAD pointer
156 if test -f "$repo/HEAD"
160 (cd "$repo" && tar cf
- refs
$HEAD) |
161 (cd "$GIT_DIR" && tar xf
-) ||
exit 1
166 rsync
$quiet -av --ignore-existing \
167 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" &&
168 rsync
$quiet -av --ignore-existing \
169 --exclude info
"$repo/refs/" "$GIT_DIR/refs/" ||
exit
171 # Look at objects/info/alternates for rsync -- http will
172 # support it natively and git native ones will do it on the
173 # remote end. Not having that file is not a crime.
174 rsync
-q "$repo/objects/info/alternates" \
175 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
176 rm -f "$GIT_DIR/TMP_ALT"
177 if test -f "$GIT_DIR/TMP_ALT"
180 . git-parse-remote
&&
181 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
184 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
186 '') echo >&2 "Getting alternate: $alt" ;;
188 rsync
$quiet -av --ignore-existing \
189 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
191 rm -f "$GIT_DIR/TMP_ALT"
195 clone_dumb_http
"$repo" "$D"
198 cd "$D" && case "$upload_pack" in
199 '') git-clone-pack
$quiet "$repo" ;;
200 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
202 echo >&2 "clone-pack from '$repo' failed."
212 if test -f "$GIT_DIR/HEAD"
214 head_points_at
=`git-symbolic-ref HEAD`
215 case "$head_points_at" in
217 head_points_at
=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
218 mkdir
-p "$GIT_DIR/remotes" &&
219 echo >"$GIT_DIR/remotes/origin" \
221 Pull: $head_points_at:$origin" &&
222 git-update-ref
"refs/heads/$origin" $
(git-rev-parse HEAD
) &&
223 (cd "$GIT_DIR" && find "refs/heads" -type f
-print) |
226 head=`expr "$ref" : 'refs/heads/\(.*\)'` &&
227 test "$head_points_at" = "$head" ||
228 test "$origin" = "$head" ||
229 echo "Pull: ${head}:${head}"
230 done >>"$GIT_DIR/remotes/origin"
233 case "$no_checkout" in