git-clone: PG13 --naked option to --bare.
[git/jrn.git] / git-clone.sh
blob8e65202320303d3c4fa591a9d39506b5b4374fd9
1 #!/bin/sh
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
5 #
6 # Clone a repository into a different directory that does not yet exist.
8 # See git-sh-setup why.
9 unset CDPATH
11 usage() {
12 echo >&2 "Usage: $0 [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
13 exit 1
16 get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
24 http_fetch () {
25 # $1 = Remote, $2 = Local
26 curl -nsfL $curl_extra_args "$1" >"$2"
29 clone_dumb_http () {
30 # $1 - remote, $2 - local
31 cd "$2" &&
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?"
37 exit 1;
39 while read sha1 refname
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
42 case "$name" in
43 *^*) ;;
45 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
46 esac
47 done <"$clone_tmp/refs"
48 rm -fr "$clone_tmp"
51 quiet=
52 use_local=no
53 local_shared=no
54 no_checkout=
55 upload_pack=
56 bare=
57 origin=origin
58 while
59 case "$#,$1" in
60 0,*) break ;;
61 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
62 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
63 no_checkout=yes ;;
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 ;;
70 1,-o) usage;;
71 *,-o)
72 git-check-ref-format "$2" || {
73 echo >&2 "'$2' is not suitable for a branch name"
74 exit 1
76 origin="$2"; shift
78 1,-u|1,--upload-pack) usage ;;
79 *,-u|*,--upload-pack)
80 shift
81 upload_pack="--exec=$1" ;;
82 *,-*) usage ;;
83 *) break ;;
84 esac
86 shift
87 done
89 # --bare implies --no-checkout
90 test =z "$bare" || no_checkout=yes
92 # Turn the source into an absolute path if
93 # it is local
94 repo="$1"
95 local=no
96 if base=$(get_repo_base "$repo"); then
97 repo="$base"
98 local=yes
101 dir="$2"
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
105 mkdir -p "$dir" &&
106 D=$(cd "$dir" && pwd) &&
107 case "$bare" in
108 yes) GIT_DIR="$D" ;;
109 *) GIT_DIR="$D/.git" ;;
110 esac && export GIT_DIR && git-init-db || usage
111 case "$bare" in
112 yes)
113 GIT_DIR="$D" ;;
115 GIT_DIR="$D/.git" ;;
116 esac
118 # We do local magic only when the user tells us to.
119 case "$local,$use_local" in
120 yes,yes)
121 ( cd "$repo/objects" ) || {
122 echo >&2 "-l flag seen but $repo is not local."
123 exit 1
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
137 then
139 fi &&
140 rm -f "$GIT_DIR/objects/sample" &&
141 cd "$repo" &&
142 find objects -depth -print | cpio -puamd$l "$GIT_DIR/" || exit 1
144 yes)
145 mkdir -p "$GIT_DIR/objects/info"
147 test -f "$repo/objects/info/alternates" &&
148 cat "$repo/objects/info/alternates";
149 echo "$repo/objects"
150 } >"$GIT_DIR/objects/info/alternates"
152 esac
154 # Make a duplicate of refs and HEAD pointer
155 HEAD=
156 if test -f "$repo/HEAD"
157 then
158 HEAD=HEAD
160 (cd "$repo" && tar cf - refs $HEAD) |
161 (cd "$GIT_DIR" && tar xf -) || exit 1
164 case "$repo" in
165 rsync://*)
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"
178 then
179 ( cd "$D" &&
180 . git-parse-remote &&
181 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
182 while read alt
184 case "$alt" in 'bad alternate: '*) die "$alt";; esac
185 case "$quiet" in
186 '') echo >&2 "Getting alternate: $alt" ;;
187 esac
188 rsync $quiet -av --ignore-existing \
189 --exclude info "$alt" "$GIT_DIR/objects" || exit
190 done
191 rm -f "$GIT_DIR/TMP_ALT"
194 http://*)
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" ;;
201 esac || {
202 echo >&2 "clone-pack from '$repo' failed."
203 exit 1
206 esac
208 esac
210 cd "$D" || exit
212 if test -f "$GIT_DIR/HEAD"
213 then
214 head_points_at=`git-symbolic-ref HEAD`
215 case "$head_points_at" in
216 refs/heads/*)
217 head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
218 mkdir -p "$GIT_DIR/remotes" &&
219 echo >"$GIT_DIR/remotes/origin" \
220 "URL: $repo
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) |
224 while read ref
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"
231 esac
233 case "$no_checkout" in
235 git checkout
236 esac