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 [--naked] [-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) naked
=yes ;;
65 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
66 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
67 local_shared
=yes; use_local
=yes ;;
68 *,-q|
*,--quiet) quiet
=-q ;;
71 git-check-ref-format
"$2" ||
{
72 echo >&2 "'$2' is not suitable for a branch name"
77 1,-u|
1,--upload-pack) usage
;;
80 upload_pack
="--exec=$1" ;;
88 # --naked implies --no-checkout
89 test -z "$naked" || no_checkout
=yes
91 # Turn the source into an absolute path if
95 if base
=$
(get_repo_base
"$repo"); then
101 # Try using "humanish" part of source repo if user didn't specify one
102 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g')
103 [ -e "$dir" ] && echo "$dir already exists." && usage
105 D
=$
(cd "$dir" && pwd) &&
108 *) GIT_DIR
="$D/.git" ;;
109 esac && export GIT_DIR
&& git-init-db || usage
117 # We do local magic only when the user tells us to.
118 case "$local,$use_local" in
120 ( cd "$repo/objects" ) ||
{
121 echo >&2 "-l flag seen but $repo is not local."
125 case "$local_shared" in
127 # See if we can hardlink and drop "l" if not.
128 sample_file
=$
(cd "$repo" && \
129 find objects
-type f
-print |
sed -e 1q
)
131 # objects directory should not be empty since we are cloning!
132 test -f "$repo/$sample_file" ||
exit
135 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
139 rm -f "$GIT_DIR/objects/sample" &&
141 find objects
-depth -print |
cpio -puamd$l "$GIT_DIR/" ||
exit 1
144 mkdir
-p "$GIT_DIR/objects/info"
146 test -f "$repo/objects/info/alternates" &&
147 cat "$repo/objects/info/alternates";
149 } >"$GIT_DIR/objects/info/alternates"
153 # Make a duplicate of refs and HEAD pointer
155 if test -f "$repo/HEAD"
159 (cd "$repo" && tar cf
- refs
$HEAD) |
160 (cd "$GIT_DIR" && tar xf
-) ||
exit 1
165 rsync
$quiet -av --ignore-existing \
166 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" &&
167 rsync
$quiet -av --ignore-existing \
168 --exclude info
"$repo/refs/" "$GIT_DIR/refs/" ||
exit
170 # Look at objects/info/alternates for rsync -- http will
171 # support it natively and git native ones will do it on the
172 # remote end. Not having that file is not a crime.
173 rsync
-q "$repo/objects/info/alternates" \
174 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
175 rm -f "$GIT_DIR/TMP_ALT"
176 if test -f "$GIT_DIR/TMP_ALT"
179 . git-parse-remote
&&
180 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
183 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
185 '') echo >&2 "Getting alternate: $alt" ;;
187 rsync
$quiet -av --ignore-existing \
188 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
190 rm -f "$GIT_DIR/TMP_ALT"
194 clone_dumb_http
"$repo" "$D"
197 cd "$D" && case "$upload_pack" in
198 '') git-clone-pack
$quiet "$repo" ;;
199 *) git-clone-pack
$quiet "$upload_pack" "$repo" ;;
201 echo >&2 "clone-pack from '$repo' failed."
211 if test -f "$GIT_DIR/HEAD"
213 head_points_at
=`git-symbolic-ref HEAD`
214 case "$head_points_at" in
216 head_points_at
=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
217 mkdir
-p "$GIT_DIR/remotes" &&
218 echo >"$GIT_DIR/remotes/origin" \
220 Pull: $head_points_at:$origin" &&
221 git-update-ref
"refs/heads/$origin" $
(git-rev-parse HEAD
) &&
222 (cd "$GIT_DIR" && find "refs/heads" -type f
-print) |
225 head=`expr "$ref" : 'refs/heads/\(.*\)'` &&
226 test "$head_points_at" = "$head" ||
227 test "$origin" = "$head" ||
228 echo "Pull: ${head}:${head}"
229 done >>"$GIT_DIR/remotes/origin"
232 case "$no_checkout" in