blob: 4fdd6525148d50bd3a014bfc0f60eefd0f16efb9 [file] [log] [blame]
Linus Torvalds3f571e02005-06-22 18:49:43 -07001#!/bin/sh
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -07002#
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.
7
Junio C Hamano365527a2005-09-12 19:47:07 -07008# See git-sh-setup why.
9unset CDPATH
10
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070011usage() {
Junio C Hamano036a72d2005-09-26 17:17:09 -070012 echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] [-n] <repo> <dir>"
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070013 exit 1
14}
15
Linus Torvaldsba375ac2005-07-08 15:46:33 -070016get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18}
19
Junio C Hamano0516de32005-09-05 00:47:39 -070020if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
22fi
23
24http_fetch () {
25 # $1 = Remote, $2 = Local
26 curl -nsf $curl_extra_args "$1" >"$2"
27}
28
29clone_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 http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
36 echo >&2 "Cannot get remote repository information.
37Perhaps git-update-server-info needs to be run there?"
38 exit 1;
39 }
40 while read type name
41 do
42 case "$type" in
43 P) ;;
44 *) continue ;;
45 esac &&
46
47 idx=`expr "$name" : '\(.*\)\.pack'`.idx
48 http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
49 http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
50 git-verify-pack ".git/objects/pack/$idx" || exit 1
51 done <"$clone_tmp/packs"
52
53 while read sha1 refname
54 do
55 name=`expr "$refname" : 'refs/\(.*\)'` &&
Junio C Hamanocdb39502005-10-17 21:47:06 -070056 case "$name" in
57 *^*) ;;
58 *)
59 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
60 esac
Junio C Hamano0516de32005-09-05 00:47:39 -070061 done <"$clone_tmp/refs"
62 rm -fr "$clone_tmp"
63}
64
Linus Torvalds167a4a32005-07-09 10:52:35 -070065quiet=
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070066use_local=no
Junio C Hamanoaae4f422005-08-14 17:25:57 -070067local_shared=no
Junio C Hamano036a72d2005-09-26 17:17:09 -070068no_checkout=
Junio C Hamano6ec311d2005-07-13 20:25:54 -070069upload_pack=
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070070while
71 case "$#,$1" in
72 0,*) break ;;
Junio C Hamano036a72d2005-09-26 17:17:09 -070073 *,-n) no_checkout=yes ;;
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070074 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
Junio C Hamanoaae4f422005-08-14 17:25:57 -070075 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
76 local_shared=yes ;;
Linus Torvalds167a4a32005-07-09 10:52:35 -070077 *,-q|*,--quiet) quiet=-q ;;
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070078 1,-u|1,--upload-pack) usage ;;
Junio C Hamano6ec311d2005-07-13 20:25:54 -070079 *,-u|*,--upload-pack)
80 shift
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070081 upload_pack="--exec=$1" ;;
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070082 *,-*) usage ;;
83 *) break ;;
84 esac
85do
86 shift
87done
88
Linus Torvaldsba375ac2005-07-08 15:46:33 -070089# Turn the source into an absolute path if
90# it is local
Linus Torvalds3f571e02005-06-22 18:49:43 -070091repo="$1"
Linus Torvaldsba375ac2005-07-08 15:46:33 -070092local=no
93if base=$(get_repo_base "$repo"); then
94 repo="$base"
95 local=yes
96fi
97
Linus Torvalds3f571e02005-06-22 18:49:43 -070098dir="$2"
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070099mkdir "$dir" &&
100D=$(
101 (cd "$dir" && git-init-db && pwd)
102) &&
103test -d "$D" || usage
104
105# We do local magic only when the user tells us to.
Linus Torvaldsba375ac2005-07-08 15:46:33 -0700106case "$local,$use_local" in
107yes,yes)
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700108 ( cd "$repo/objects" ) || {
Junio C Hamanoab6625e2005-07-11 13:30:54 -0700109 echo >&2 "-l flag seen but $repo is not local."
110 exit 1
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700111 }
112
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700113 case "$local_shared" in
114 no)
115 # See if we can hardlink and drop "l" if not.
116 sample_file=$(cd "$repo" && \
117 find objects -type f -print | sed -e 1q)
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700118
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700119 # objects directory should not be empty since we are cloning!
120 test -f "$repo/$sample_file" || exit
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700121
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700122 l=
123 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
124 then
125 l=l
126 fi &&
127 rm -f "$D/.git/objects/sample" &&
128 cd "$repo" &&
Junio C Hamano3d95bf02005-11-05 11:44:35 -0800129 find objects -depth -print | cpio -puamd$l "$D/.git/" || exit 1
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700130 ;;
131 yes)
132 mkdir -p "$D/.git/objects/info"
Junio C Hamano0f87f892005-08-17 15:18:41 -0700133 {
134 test -f "$repo/objects/info/alternates" &&
135 cat "$repo/objects/info/alternates";
136 echo "$repo/objects"
137 } >"$D/.git/objects/info/alternates"
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700138 ;;
139 esac
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700140
141 # Make a duplicate of refs and HEAD pointer
142 HEAD=
143 if test -f "$repo/HEAD"
144 then
145 HEAD=HEAD
146 fi
Junio C Hamano229a7ed2005-09-23 10:41:40 -0700147 (cd "$repo" && tar cf - refs $HEAD) |
148 (cd "$D/.git" && tar xf -) || exit 1
Linus Torvalds7558ef82005-07-08 17:07:12 -0700149 ;;
150*)
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700151 case "$repo" in
152 rsync://*)
Junio C Hamano4447bad2005-09-17 11:56:41 -0700153 rsync $quiet -av --ignore-existing \
154 --exclude info "$repo/objects/" "$D/.git/objects/" &&
155 rsync $quiet -av --ignore-existing \
156 --exclude info "$repo/refs/" "$D/.git/refs/" || exit
157
158 # Look at objects/info/alternates for rsync -- http will
159 # support it natively and git native ones will do it on the
160 # remote end. Not having that file is not a crime.
Junio C Hamano89d844d2005-09-19 23:52:33 -0700161 rsync -q "$repo/objects/info/alternates" \
162 "$D/.git/TMP_ALT" 2>/dev/null ||
Junio C Hamano4447bad2005-09-17 11:56:41 -0700163 rm -f "$D/.git/TMP_ALT"
164 if test -f "$D/.git/TMP_ALT"
165 then
166 ( cd $D &&
167 . git-parse-remote &&
168 resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
169 while read alt
170 do
171 case "$alt" in 'bad alternate: '*) die "$alt";; esac
172 case "$quiet" in
173 '') echo >&2 "Getting alternate: $alt" ;;
174 esac
175 rsync $quiet -av --ignore-existing \
176 --exclude info "$alt" "$D/.git/objects" || exit
177 done
178 rm -f "$D/.git/TMP_ALT"
179 fi
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700180 ;;
181 http://*)
Junio C Hamano0516de32005-09-05 00:47:39 -0700182 clone_dumb_http "$repo" "$D"
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700183 ;;
184 *)
185 cd "$D" && case "$upload_pack" in
186 '') git-clone-pack $quiet "$repo" ;;
187 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
188 esac
189 ;;
Junio C Hamano6ec311d2005-07-13 20:25:54 -0700190 esac
Linus Torvalds7558ef82005-07-08 17:07:12 -0700191 ;;
192esac
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700193
Junio C Hamano036a72d2005-09-26 17:17:09 -0700194cd $D || exit
195
196if test -f ".git/HEAD"
197then
Junio C Hamanoe125c1a2005-11-01 22:19:36 -0800198 head_points_at=`git-symbolic-ref HEAD`
199 case "$head_points_at" in
200 refs/heads/*)
201 head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
202 mkdir -p .git/remotes &&
203 echo >.git/remotes/origin \
204 "URL: $repo
Junio C Hamano95d117b2005-11-06 00:52:57 -0800205Pull: $head_points_at:origin" &&
206 cp ".git/refs/heads/$head_points_at" .git/refs/heads/origin &&
207 find .git/refs/heads -type f -print |
208 while read ref
209 do
210 head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
211 test "$head_points_at" = "$head" ||
212 test "origin" = "$head" ||
213 echo "Pull: ${head}:${head}"
214 done >>.git/remotes/origin
Junio C Hamanoe125c1a2005-11-01 22:19:36 -0800215 esac
216
Junio C Hamano036a72d2005-09-26 17:17:09 -0700217 case "$no_checkout" in
218 '')
219 git checkout
220 esac
221fi