]>
Commit | Line | Data |
---|---|---|
3f571e0b | 1 | #!/bin/sh |
e95ab1ed JH |
2 | # |
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 | ||
8 | usage() { | |
aae4f42c | 9 | echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>" |
e95ab1ed JH |
10 | exit 1 |
11 | } | |
12 | ||
ba375acf LT |
13 | get_repo_base() { |
14 | (cd "$1" && (cd .git ; pwd)) 2> /dev/null | |
15 | } | |
16 | ||
0516de30 JH |
17 | if [ -n "$GIT_SSL_NO_VERIFY" ]; then |
18 | curl_extra_args="-k" | |
19 | fi | |
20 | ||
21 | http_fetch () { | |
22 | # $1 = Remote, $2 = Local | |
23 | curl -nsf $curl_extra_args "$1" >"$2" | |
24 | } | |
25 | ||
26 | clone_dumb_http () { | |
27 | # $1 - remote, $2 - local | |
28 | cd "$2" && | |
29 | clone_tmp='.git/clone-tmp' && | |
30 | mkdir -p "$clone_tmp" || exit 1 | |
31 | http_fetch "$1/info/refs" "$clone_tmp/refs" && | |
32 | http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || { | |
33 | echo >&2 "Cannot get remote repository information. | |
34 | Perhaps git-update-server-info needs to be run there?" | |
35 | exit 1; | |
36 | } | |
37 | while read type name | |
38 | do | |
39 | case "$type" in | |
40 | P) ;; | |
41 | *) continue ;; | |
42 | esac && | |
43 | ||
44 | idx=`expr "$name" : '\(.*\)\.pack'`.idx | |
45 | http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" && | |
46 | http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" && | |
47 | git-verify-pack ".git/objects/pack/$idx" || exit 1 | |
48 | done <"$clone_tmp/packs" | |
49 | ||
50 | while read sha1 refname | |
51 | do | |
52 | name=`expr "$refname" : 'refs/\(.*\)'` && | |
53 | git-http-pull -v -a -w "$name" "$name" "$1/" || exit 1 | |
54 | done <"$clone_tmp/refs" | |
55 | rm -fr "$clone_tmp" | |
56 | } | |
57 | ||
167a4a33 | 58 | quiet= |
e95ab1ed | 59 | use_local=no |
aae4f42c | 60 | local_shared=no |
6ec311da | 61 | upload_pack= |
e95ab1ed JH |
62 | while |
63 | case "$#,$1" in | |
64 | 0,*) break ;; | |
1cadb5a2 | 65 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; |
aae4f42c JH |
66 | *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) |
67 | local_shared=yes ;; | |
167a4a33 | 68 | *,-q|*,--quiet) quiet=-q ;; |
1cadb5a2 | 69 | 1,-u|1,--upload-pack) usage ;; |
6ec311da JH |
70 | *,-u|*,--upload-pack) |
71 | shift | |
1cadb5a2 | 72 | upload_pack="--exec=$1" ;; |
e95ab1ed JH |
73 | *,-*) usage ;; |
74 | *) break ;; | |
75 | esac | |
76 | do | |
77 | shift | |
78 | done | |
79 | ||
ba375acf LT |
80 | # Turn the source into an absolute path if |
81 | # it is local | |
3f571e0b | 82 | repo="$1" |
ba375acf LT |
83 | local=no |
84 | if base=$(get_repo_base "$repo"); then | |
85 | repo="$base" | |
86 | local=yes | |
87 | fi | |
88 | ||
3f571e0b | 89 | dir="$2" |
e95ab1ed JH |
90 | mkdir "$dir" && |
91 | D=$( | |
92 | (cd "$dir" && git-init-db && pwd) | |
93 | ) && | |
94 | test -d "$D" || usage | |
95 | ||
96 | # We do local magic only when the user tells us to. | |
ba375acf LT |
97 | case "$local,$use_local" in |
98 | yes,yes) | |
e95ab1ed | 99 | ( cd "$repo/objects" ) || { |
ab6625e0 JH |
100 | echo >&2 "-l flag seen but $repo is not local." |
101 | exit 1 | |
e95ab1ed JH |
102 | } |
103 | ||
aae4f42c JH |
104 | case "$local_shared" in |
105 | no) | |
106 | # See if we can hardlink and drop "l" if not. | |
107 | sample_file=$(cd "$repo" && \ | |
108 | find objects -type f -print | sed -e 1q) | |
e95ab1ed | 109 | |
aae4f42c JH |
110 | # objects directory should not be empty since we are cloning! |
111 | test -f "$repo/$sample_file" || exit | |
e95ab1ed | 112 | |
aae4f42c JH |
113 | l= |
114 | if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null | |
115 | then | |
116 | l=l | |
117 | fi && | |
118 | rm -f "$D/.git/objects/sample" && | |
119 | cd "$repo" && | |
120 | find objects -type f -print | | |
121 | cpio -puamd$l "$D/.git/" || exit 1 | |
122 | ;; | |
123 | yes) | |
124 | mkdir -p "$D/.git/objects/info" | |
0f87f893 JH |
125 | { |
126 | test -f "$repo/objects/info/alternates" && | |
127 | cat "$repo/objects/info/alternates"; | |
128 | echo "$repo/objects" | |
129 | } >"$D/.git/objects/info/alternates" | |
aae4f42c JH |
130 | ;; |
131 | esac | |
e95ab1ed JH |
132 | |
133 | # Make a duplicate of refs and HEAD pointer | |
134 | HEAD= | |
135 | if test -f "$repo/HEAD" | |
136 | then | |
137 | HEAD=HEAD | |
138 | fi | |
139 | tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1 | |
7558ef89 LT |
140 | ;; |
141 | *) | |
1cadb5a2 JH |
142 | case "$repo" in |
143 | rsync://*) | |
144 | rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" && | |
145 | rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/" | |
146 | ;; | |
147 | http://*) | |
0516de30 | 148 | clone_dumb_http "$repo" "$D" |
1cadb5a2 JH |
149 | ;; |
150 | *) | |
151 | cd "$D" && case "$upload_pack" in | |
152 | '') git-clone-pack $quiet "$repo" ;; | |
153 | *) git-clone-pack $quiet "$upload_pack" "$repo" ;; | |
154 | esac | |
155 | ;; | |
6ec311da | 156 | esac |
7558ef89 LT |
157 | ;; |
158 | esac | |
1cadb5a2 JH |
159 | |
160 | # Update origin. | |
6687f8fe JH |
161 | mkdir -p "$D/.git/remotes/" && |
162 | rm -f "$D/.git/remotes/origin" && | |
163 | echo >"$D/.git/remotes/origin" \ | |
164 | "URL: $repo | |
165 | Pull: master:origin" |