🌐 AI搜索 & 代理 主页
blob: 377d59e62cecd305fc7cf69aa927b13225fdb3eb [file] [log] [blame]
Linus Torvalds3f571e02005-06-23 01:49:431#!/bin/sh
Junio C Hamanoe95ab1e2005-07-06 20:04:212#
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-13 02:47:078# See git-sh-setup why.
9unset CDPATH
10
Junio C Hamanoe95ab1e2005-07-06 20:04:2111usage() {
Johannes Schindeline6c310f2005-12-22 22:37:2412 echo >&2 "Usage: $0 [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
Junio C Hamanoe95ab1e2005-07-06 20:04:2113 exit 1
14}
15
Linus Torvaldsba375ac2005-07-08 22:46:3316get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18}
19
Junio C Hamano0516de32005-09-05 07:47:3920if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
22fi
23
24http_fetch () {
25 # $1 = Remote, $2 = Local
Josef Weidendorfer66c9ec22005-11-10 13:12:1926 curl -nsfL $curl_extra_args "$1" >"$2"
Junio C Hamano0516de32005-09-05 07:47:3927}
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
Junio C Hamano05621102005-12-23 00:01:4634 http_fetch "$1/info/refs" "$clone_tmp/refs" || {
Junio C Hamano0516de32005-09-05 07:47:3935 echo >&2 "Cannot get remote repository information.
36Perhaps git-update-server-info needs to be run there?"
37 exit 1;
38 }
Junio C Hamano0516de32005-09-05 07:47:3939 while read sha1 refname
40 do
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
Junio C Hamanocdb39502005-10-18 04:47:0642 case "$name" in
43 *^*) ;;
44 *)
45 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
46 esac
Junio C Hamano0516de32005-09-05 07:47:3947 done <"$clone_tmp/refs"
48 rm -fr "$clone_tmp"
49}
50
Linus Torvalds167a4a32005-07-09 17:52:3551quiet=
Junio C Hamanoe95ab1e2005-07-06 20:04:2152use_local=no
Junio C Hamanoaae4f422005-08-15 00:25:5753local_shared=no
Junio C Hamano036a72d2005-09-27 00:17:0954no_checkout=
Junio C Hamano6ec311d2005-07-14 03:25:5455upload_pack=
Johannes Schindeline6c310f2005-12-22 22:37:2456origin=origin
Junio C Hamanoe95ab1e2005-07-06 20:04:2157while
58 case "$#,$1" in
59 0,*) break ;;
Junio C Hamano036a72d2005-09-27 00:17:0960 *,-n) no_checkout=yes ;;
Junio C Hamano1cadb5a2005-07-23 02:11:2261 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
Junio C Hamanoaae4f422005-08-15 00:25:5762 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
Pavel Roskin2c4ed382005-11-29 06:20:4963 local_shared=yes; use_local=yes ;;
Linus Torvalds167a4a32005-07-09 17:52:3564 *,-q|*,--quiet) quiet=-q ;;
Johannes Schindeline6c310f2005-12-22 22:37:2465 1,-o) usage;;
66 *,-o)
67 git-check-ref-format "$2" || {
68 echo >&2 "'$2' is not suitable for a branch name"
69 exit 1
70 }
71 origin="$2"; shift
72 ;;
Junio C Hamano1cadb5a2005-07-23 02:11:2273 1,-u|1,--upload-pack) usage ;;
Junio C Hamano6ec311d2005-07-14 03:25:5474 *,-u|*,--upload-pack)
75 shift
Junio C Hamano1cadb5a2005-07-23 02:11:2276 upload_pack="--exec=$1" ;;
Junio C Hamanoe95ab1e2005-07-06 20:04:2177 *,-*) usage ;;
78 *) break ;;
79 esac
80do
81 shift
82done
83
Linus Torvaldsba375ac2005-07-08 22:46:3384# Turn the source into an absolute path if
85# it is local
Linus Torvalds3f571e02005-06-23 01:49:4386repo="$1"
Linus Torvaldsba375ac2005-07-08 22:46:3387local=no
88if base=$(get_repo_base "$repo"); then
89 repo="$base"
90 local=yes
91fi
92
Linus Torvalds3f571e02005-06-23 01:49:4393dir="$2"
Andreas Ericsson0879aa22005-11-10 11:58:0894# Try using "humanish" part of source repo if user didn't specify one
95[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g')
Josef Weidendorferb0c698a2005-11-13 14:03:3196[ -e "$dir" ] && echo "$dir already exists." && usage
Andreas Ericsson7f10f7c2005-11-10 11:58:0897mkdir -p "$dir" &&
Junio C Hamanoe95ab1e2005-07-06 20:04:2198D=$(
99 (cd "$dir" && git-init-db && pwd)
100) &&
101test -d "$D" || usage
102
103# We do local magic only when the user tells us to.
Linus Torvaldsba375ac2005-07-08 22:46:33104case "$local,$use_local" in
105yes,yes)
Junio C Hamanoe95ab1e2005-07-06 20:04:21106 ( cd "$repo/objects" ) || {
Junio C Hamanoab6625e2005-07-11 20:30:54107 echo >&2 "-l flag seen but $repo is not local."
108 exit 1
Junio C Hamanoe95ab1e2005-07-06 20:04:21109 }
110
Junio C Hamanoaae4f422005-08-15 00:25:57111 case "$local_shared" in
112 no)
113 # See if we can hardlink and drop "l" if not.
114 sample_file=$(cd "$repo" && \
115 find objects -type f -print | sed -e 1q)
Junio C Hamanoe95ab1e2005-07-06 20:04:21116
Junio C Hamanoaae4f422005-08-15 00:25:57117 # objects directory should not be empty since we are cloning!
118 test -f "$repo/$sample_file" || exit
Junio C Hamanoe95ab1e2005-07-06 20:04:21119
Junio C Hamanoaae4f422005-08-15 00:25:57120 l=
121 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
122 then
123 l=l
124 fi &&
125 rm -f "$D/.git/objects/sample" &&
126 cd "$repo" &&
Junio C Hamano3d95bf02005-11-05 19:44:35127 find objects -depth -print | cpio -puamd$l "$D/.git/" || exit 1
Junio C Hamanoaae4f422005-08-15 00:25:57128 ;;
129 yes)
130 mkdir -p "$D/.git/objects/info"
Junio C Hamano0f87f892005-08-17 22:18:41131 {
132 test -f "$repo/objects/info/alternates" &&
133 cat "$repo/objects/info/alternates";
134 echo "$repo/objects"
135 } >"$D/.git/objects/info/alternates"
Junio C Hamanoaae4f422005-08-15 00:25:57136 ;;
137 esac
Junio C Hamanoe95ab1e2005-07-06 20:04:21138
139 # Make a duplicate of refs and HEAD pointer
140 HEAD=
141 if test -f "$repo/HEAD"
142 then
143 HEAD=HEAD
144 fi
Junio C Hamano229a7ed2005-09-23 17:41:40145 (cd "$repo" && tar cf - refs $HEAD) |
146 (cd "$D/.git" && tar xf -) || exit 1
Linus Torvalds7558ef82005-07-09 00:07:12147 ;;
148*)
Junio C Hamano1cadb5a2005-07-23 02:11:22149 case "$repo" in
150 rsync://*)
Junio C Hamano4447bad2005-09-17 18:56:41151 rsync $quiet -av --ignore-existing \
152 --exclude info "$repo/objects/" "$D/.git/objects/" &&
153 rsync $quiet -av --ignore-existing \
154 --exclude info "$repo/refs/" "$D/.git/refs/" || exit
155
156 # Look at objects/info/alternates for rsync -- http will
157 # support it natively and git native ones will do it on the
158 # remote end. Not having that file is not a crime.
Junio C Hamano89d844d2005-09-20 06:52:33159 rsync -q "$repo/objects/info/alternates" \
160 "$D/.git/TMP_ALT" 2>/dev/null ||
Junio C Hamano4447bad2005-09-17 18:56:41161 rm -f "$D/.git/TMP_ALT"
162 if test -f "$D/.git/TMP_ALT"
163 then
Pavel Roskin0e9ab022005-11-11 05:19:04164 ( cd "$D" &&
Junio C Hamano4447bad2005-09-17 18:56:41165 . git-parse-remote &&
166 resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
167 while read alt
168 do
169 case "$alt" in 'bad alternate: '*) die "$alt";; esac
170 case "$quiet" in
171 '') echo >&2 "Getting alternate: $alt" ;;
172 esac
173 rsync $quiet -av --ignore-existing \
174 --exclude info "$alt" "$D/.git/objects" || exit
175 done
176 rm -f "$D/.git/TMP_ALT"
177 fi
Junio C Hamano1cadb5a2005-07-23 02:11:22178 ;;
179 http://*)
Junio C Hamano0516de32005-09-05 07:47:39180 clone_dumb_http "$repo" "$D"
Junio C Hamano1cadb5a2005-07-23 02:11:22181 ;;
182 *)
183 cd "$D" && case "$upload_pack" in
184 '') git-clone-pack $quiet "$repo" ;;
185 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
Junio C Hamano0a8b4de2005-12-13 23:58:00186 esac || {
187 echo >&2 "clone-pack from '$repo' failed."
188 exit 1
189 }
Junio C Hamano1cadb5a2005-07-23 02:11:22190 ;;
Junio C Hamano6ec311d2005-07-14 03:25:54191 esac
Linus Torvalds7558ef82005-07-09 00:07:12192 ;;
193esac
Junio C Hamano1cadb5a2005-07-23 02:11:22194
Pavel Roskin0e9ab022005-11-11 05:19:04195cd "$D" || exit
Junio C Hamano036a72d2005-09-27 00:17:09196
197if test -f ".git/HEAD"
198then
Junio C Hamanoe125c1a2005-11-02 06:19:36199 head_points_at=`git-symbolic-ref HEAD`
200 case "$head_points_at" in
201 refs/heads/*)
202 head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
203 mkdir -p .git/remotes &&
204 echo >.git/remotes/origin \
205 "URL: $repo
Johannes Schindeline6c310f2005-12-22 22:37:24206Pull: $head_points_at:$origin" &&
207 git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) &&
Junio C Hamano95d117b2005-11-06 08:52:57208 find .git/refs/heads -type f -print |
209 while read ref
210 do
211 head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
212 test "$head_points_at" = "$head" ||
Johannes Schindeline6c310f2005-12-22 22:37:24213 test "$origin" = "$head" ||
Junio C Hamano95d117b2005-11-06 08:52:57214 echo "Pull: ${head}:${head}"
215 done >>.git/remotes/origin
Junio C Hamanoe125c1a2005-11-02 06:19:36216 esac
217
Junio C Hamano036a72d2005-09-27 00:17:09218 case "$no_checkout" in
219 '')
220 git checkout
221 esac
222fi