@matheusbsbs / matheus-bagliano / commits / b3661567cf
Fork 0

[PATCH] Add support for alternates in HTTP

This tries .../objects/info/http-alternates and then .../objects/info/alternates, looking for a file which specifies where else to download objects and packs from. It currently only supports absolute paths, and doesn't support full URLs. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Daniel Barkalow committed Sep 14, 2005 at 23:26 UTC b3661567cfa604abfa6a67d8e7bbd6db018c3fe6
1 file changed +134 -35
http-fetch.c
+134 -35
@@ -16,7 +16,17 @@
16 static CURL *curl;
17 static struct curl_slist *no_pragma_header;
18
19 -static char *base;
19 +static char *initial_base;
20 +
21 +struct alt_base
22 +{
23 + char *base;
24 + int got_indices;
25 + struct packed_git *packs;
26 + struct alt_base *next;
27 +};
28 +
29 +struct alt_base *alt = NULL;
30
31 static SHA_CTX c;
32 static z_stream stream;
@@ -72,11 +82,9 @@ void prefetch(unsigned char *sha1)
82 {
83 }
84
75 -static int got_indices = 0;
76 -
77 -static struct packed_git *packs = NULL;
85 +static int got_alternates = 0;
86
79 -static int fetch_index(unsigned char *sha1)
87 +static int fetch_index(struct alt_base *repo, unsigned char *sha1)
88 {
89 char *filename;
90 char *url;
@@ -90,9 +98,9 @@ static int fetch_index(unsigned char *sha1)
98 fprintf(stderr, "Getting index for pack %s\n",
99 sha1_to_hex(sha1));
100
93 - url = xmalloc(strlen(base) + 64);
101 + url = xmalloc(strlen(repo->base) + 64);
102 sprintf(url, "%s/objects/pack/pack-%s.idx",
95 - base, sha1_to_hex(sha1));
103 + repo->base, sha1_to_hex(sha1));
104
105 filename = sha1_pack_index_name(sha1);
106 indexfile = fopen(filename, "w");
@@ -114,22 +122,92 @@ static int fetch_index(unsigned char *sha1)
122 return 0;
123 }
124
117 -static int setup_index(unsigned char *sha1)
125 +static int setup_index(struct alt_base *repo, unsigned char *sha1)
126 {
127 struct packed_git *new_pack;
128 if (has_pack_file(sha1))
129 return 0; // don't list this as something we can get
130
123 - if (fetch_index(sha1))
131 + if (fetch_index(repo, sha1))
132 return -1;
133
134 new_pack = parse_pack_index(sha1);
127 - new_pack->next = packs;
128 - packs = new_pack;
135 + new_pack->next = repo->packs;
136 + repo->packs = new_pack;
137 return 0;
138 }
139
132 -static int fetch_indices(void)
140 +static int fetch_alternates(char *base)
141 +{
142 + int ret = 0;
143 + struct buffer buffer;
144 + char *url;
145 + char *data;
146 + int i = 0;
147 + if (got_alternates)
148 + return 0;
149 + data = xmalloc(4096);
150 + buffer.size = 4096;
151 + buffer.posn = 0;
152 + buffer.buffer = data;
153 +
154 + if (get_verbosely)
155 + fprintf(stderr, "Getting alternates list\n");
156 +
157 + url = xmalloc(strlen(base) + 31);
158 + sprintf(url, "%s/objects/info/http-alternates", base);
159 +
160 + curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
161 + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
162 + curl_easy_setopt(curl, CURLOPT_URL, url);
163 +
164 + if (curl_easy_perform(curl) || !buffer.posn) {
165 + sprintf(url, "%s/objects/info/alternates", base);
166 +
167 + curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
168 + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
169 + curl_easy_setopt(curl, CURLOPT_URL, url);
170 +
171 + if (curl_easy_perform(curl)) {
172 + return 0;
173 + }
174 + }
175 +
176 + while (i < buffer.posn) {
177 + int posn = i;
178 + while (posn < buffer.posn && data[posn] != '\n')
179 + posn++;
180 + if (data[posn] == '\n') {
181 + if (data[i] == '/') {
182 + int serverlen = strchr(base + 8, '/') - base;
183 + // skip 'objects' at end
184 + char *target =
185 + xmalloc(serverlen + posn - i - 6);
186 + struct alt_base *newalt;
187 + strncpy(target, base, serverlen);
188 + strncpy(target + serverlen, data + i,
189 + posn - i - 7);
190 + target[serverlen + posn - i - 7] = '\0';
191 + if (get_verbosely)
192 + fprintf(stderr,
193 + "Also look at %s\n", target);
194 + newalt = xmalloc(sizeof(*newalt));
195 + newalt->next = alt;
196 + newalt->base = target;
197 + newalt->got_indices = 0;
198 + newalt->packs = NULL;
199 + alt = newalt;
200 + ret++;
201 + }
202 + }
203 + i = posn + 1;
204 + }
205 + got_alternates = 1;
206 +
207 + return ret;
208 +}
209 +
210 +static int fetch_indices(struct alt_base *repo)
211 {
212 unsigned char sha1[20];
213 char *url;
@@ -137,7 +215,7 @@ static int fetch_indices(void)
215 char *data;
216 int i = 0;
217
140 - if (got_indices)
218 + if (repo->got_indices)
219 return 0;
220
221 data = xmalloc(4096);
@@ -148,8 +226,8 @@ static int fetch_indices(void)
226 if (get_verbosely)
227 fprintf(stderr, "Getting pack list\n");
228
151 - url = xmalloc(strlen(base) + 21);
152 - sprintf(url, "%s/objects/info/packs", base);
229 + url = xmalloc(strlen(repo->base) + 21);
230 + sprintf(url, "%s/objects/info/packs", repo->base);
231
232 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
233 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
@@ -160,7 +238,7 @@ static int fetch_indices(void)
238 return error("Unable to get pack index %s", url);
239 }
240
163 - do {
241 + while (i < buffer.posn) {
242 switch (data[i]) {
243 case 'P':
244 i++;
@@ -168,7 +246,7 @@ static int fetch_indices(void)
246 !strncmp(data + i, " pack-", 6) &&
247 !strncmp(data + i + 46, ".pack\n", 6)) {
248 get_sha1_hex(data + i + 6, sha1);
171 - setup_index(sha1);
249 + setup_index(repo, sha1);
250 i += 51;
251 break;
252 }
@@ -177,13 +255,13 @@ static int fetch_indices(void)
255 i++;
256 }
257 i++;
180 - } while (i < buffer.posn);
258 + }
259
182 - got_indices = 1;
260 + repo->got_indices = 1;
261 return 0;
262 }
263
186 -static int fetch_pack(unsigned char *sha1)
264 +static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
265 {
266 char *url;
267 struct packed_git *target;
@@ -191,12 +269,11 @@ static int fetch_pack(unsigned char *sha1)
269 FILE *packfile;
270 char *filename;
271
194 - if (fetch_indices())
272 + if (fetch_indices(repo))
273 return -1;
196 - target = find_sha1_pack(sha1, packs);
274 + target = find_sha1_pack(sha1, repo->packs);
275 if (!target)
198 - return error("Couldn't get %s: not separate or in any pack",
199 - sha1_to_hex(sha1));
276 + return -1;
277
278 if (get_verbosely) {
279 fprintf(stderr, "Getting pack %s\n",
@@ -205,9 +282,9 @@ static int fetch_pack(unsigned char *sha1)
282 sha1_to_hex(sha1));
283 }
284
208 - url = xmalloc(strlen(base) + 65);
285 + url = xmalloc(strlen(repo->base) + 65);
286 sprintf(url, "%s/objects/pack/pack-%s.pack",
210 - base, sha1_to_hex(target->sha1));
287 + repo->base, sha1_to_hex(target->sha1));
288
289 filename = sha1_pack_name(target->sha1);
290 packfile = fopen(filename, "w");
@@ -227,7 +304,7 @@ static int fetch_pack(unsigned char *sha1)
304
305 fclose(packfile);
306
230 - lst = &packs;
307 + lst = &repo->packs;
308 while (*lst != target)
309 lst = &((*lst)->next);
310 *lst = (*lst)->next;
@@ -237,7 +314,7 @@ static int fetch_pack(unsigned char *sha1)
314 return 0;
315 }
316
240 -int fetch(unsigned char *sha1)
317 +int fetch_object(struct alt_base *repo, unsigned char *sha1)
318 {
319 char *hex = sha1_to_hex(sha1);
320 char *filename = sha1_file_name(sha1);
@@ -261,9 +338,9 @@ int fetch(unsigned char *sha1)
338 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
339 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
340
264 - url = xmalloc(strlen(base) + 50);
265 - strcpy(url, base);
266 - posn = url + strlen(base);
341 + url = xmalloc(strlen(repo->base) + 50);
342 + strcpy(url, repo->base);
343 + posn = url + strlen(repo->base);
344 strcpy(posn, "objects/");
345 posn += 8;
346 memcpy(posn, hex, 2);
@@ -275,9 +352,7 @@ int fetch(unsigned char *sha1)
352
353 if (curl_easy_perform(curl)) {
354 unlink(filename);
278 - if (fetch_pack(sha1))
279 - return error("Tried %s", url);
280 - return 0;
355 + return -1;
356 }
357
358 close(local);
@@ -296,11 +371,30 @@ int fetch(unsigned char *sha1)
371 return 0;
372 }
373
374 +int fetch(unsigned char *sha1)
375 +{
376 + struct alt_base *altbase = alt;
377 + while (altbase) {
378 + if (!fetch_object(altbase, sha1))
379 + return 0;
380 + if (!fetch_pack(altbase, sha1))
381 + return 0;
382 + if (fetch_alternates(altbase->base) > 0) {
383 + altbase = alt;
384 + continue;
385 + }
386 + altbase = altbase->next;
387 + }
388 + return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
389 + initial_base);
390 +}
391 +
392 int fetch_ref(char *ref, unsigned char *sha1)
393 {
394 char *url, *posn;
395 char hex[42];
396 struct buffer buffer;
397 + char *base = initial_base;
398 buffer.size = 41;
399 buffer.posn = 0;
400 buffer.buffer = hex;
@@ -368,7 +462,12 @@ int main(int argc, char **argv)
462 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
463 #endif
464
371 - base = url;
465 + alt = xmalloc(sizeof(*alt));
466 + alt->base = url;
467 + alt->got_indices = 0;
468 + alt->packs = NULL;
469 + alt->next = NULL;
470 + initial_base = url;
471
472 if (pull(commit_id))
473 return 1;