@matheusbsbs / matheus-bagliano / commits / 197e8951ab
Fork 0

http-push: support for updating remote info/refs

If info/refs exists on the remote, get a lock on info/refs, make sure that there is a local copy of the object referenced in each remote ref (in case someone else added a tag we don't have locally), do all the refspec updates, and generate and send an updated info/refs file. Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Mar 10, 2006 at 20:18 UTC 197e8951abd2ebf2c70d0847bb0b38b16b92175b
1 file changed +536 -11
http-push.c
+536 -11
@@ -22,6 +22,7 @@ enum XML_Status {
22 #define XML_STATUS_ERROR 0
23 #endif
24
25 +#define PREV_BUF_SIZE 4096
26 #define RANGE_HEADER_SIZE 30
27
28 /* DAV methods */
@@ -58,9 +59,10 @@ enum XML_Status {
59
60 /* bits #0-4 in revision.h */
61
61 -#define LOCAL (1u << 5)
62 -#define REMOTE (1u << 6)
63 -#define PUSHING (1u << 7)
62 +#define LOCAL (1u << 5)
63 +#define REMOTE (1u << 6)
64 +#define FETCHING (1u << 7)
65 +#define PUSHING (1u << 8)
66
67 static int pushing = 0;
68 static int aborted = 0;
@@ -79,6 +81,9 @@ struct repo
81 {
82 char *url;
83 int path_len;
84 + int has_info_refs;
85 + int can_update_info_refs;
86 + int has_info_packs;
87 struct packed_git *packs;
88 struct remote_lock *locks;
89 };
@@ -86,6 +91,9 @@ struct repo
91 static struct repo *remote = NULL;
92
93 enum transfer_state {
94 + NEED_FETCH,
95 + RUN_FETCH_LOOSE,
96 + RUN_FETCH_PACKED,
97 NEED_PUSH,
98 RUN_MKCOL,
99 RUN_PUT,
@@ -104,6 +112,8 @@ struct transfer_request
112 struct buffer buffer;
113 char filename[PATH_MAX];
114 char tmpfile[PATH_MAX];
115 + int local_fileno;
116 + FILE *local_stream;
117 enum transfer_state state;
118 CURLcode curl_result;
119 char errorstr[CURL_ERROR_SIZE];
@@ -113,6 +123,7 @@ struct transfer_request
123 z_stream stream;
124 int zret;
125 int rename;
126 + void *userData;
127 struct active_request_slot *slot;
128 struct transfer_request *next;
129 };
@@ -159,6 +170,7 @@ struct remote_ls_ctx
170 };
171
172 static void finish_request(struct transfer_request *request);
173 +static void release_request(struct transfer_request *request);
174
175 static void process_response(void *callback_data)
176 {
@@ -168,6 +180,258 @@ static void process_response(void *callback_data)
180 finish_request(request);
181 }
182
183 +static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
184 + void *data)
185 +{
186 + unsigned char expn[4096];
187 + size_t size = eltsize * nmemb;
188 + int posn = 0;
189 + struct transfer_request *request = (struct transfer_request *)data;
190 + do {
191 + ssize_t retval = write(request->local_fileno,
192 + ptr + posn, size - posn);
193 + if (retval < 0)
194 + return posn;
195 + posn += retval;
196 + } while (posn < size);
197 +
198 + request->stream.avail_in = size;
199 + request->stream.next_in = ptr;
200 + do {
201 + request->stream.next_out = expn;
202 + request->stream.avail_out = sizeof(expn);
203 + request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
204 + SHA1_Update(&request->c, expn,
205 + sizeof(expn) - request->stream.avail_out);
206 + } while (request->stream.avail_in && request->zret == Z_OK);
207 + data_received++;
208 + return size;
209 +}
210 +
211 +static void start_fetch_loose(struct transfer_request *request)
212 +{
213 + char *hex = sha1_to_hex(request->obj->sha1);
214 + char *filename;
215 + char prevfile[PATH_MAX];
216 + char *url;
217 + char *posn;
218 + int prevlocal;
219 + unsigned char prev_buf[PREV_BUF_SIZE];
220 + ssize_t prev_read = 0;
221 + long prev_posn = 0;
222 + char range[RANGE_HEADER_SIZE];
223 + struct curl_slist *range_header = NULL;
224 + struct active_request_slot *slot;
225 +
226 + filename = sha1_file_name(request->obj->sha1);
227 + snprintf(request->filename, sizeof(request->filename), "%s", filename);
228 + snprintf(request->tmpfile, sizeof(request->tmpfile),
229 + "%s.temp", filename);
230 +
231 + snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
232 + unlink(prevfile);
233 + rename(request->tmpfile, prevfile);
234 + unlink(request->tmpfile);
235 +
236 + if (request->local_fileno != -1)
237 + error("fd leakage in start: %d", request->local_fileno);
238 + request->local_fileno = open(request->tmpfile,
239 + O_WRONLY | O_CREAT | O_EXCL, 0666);
240 + /* This could have failed due to the "lazy directory creation";
241 + * try to mkdir the last path component.
242 + */
243 + if (request->local_fileno < 0 && errno == ENOENT) {
244 + char *dir = strrchr(request->tmpfile, '/');
245 + if (dir) {
246 + *dir = 0;
247 + mkdir(request->tmpfile, 0777);
248 + *dir = '/';
249 + }
250 + request->local_fileno = open(request->tmpfile,
251 + O_WRONLY | O_CREAT | O_EXCL, 0666);
252 + }
253 +
254 + if (request->local_fileno < 0) {
255 + request->state = ABORTED;
256 + error("Couldn't create temporary file %s for %s: %s",
257 + request->tmpfile, request->filename, strerror(errno));
258 + return;
259 + }
260 +
261 + memset(&request->stream, 0, sizeof(request->stream));
262 +
263 + inflateInit(&request->stream);
264 +
265 + SHA1_Init(&request->c);
266 +
267 + url = xmalloc(strlen(remote->url) + 50);
268 + request->url = xmalloc(strlen(remote->url) + 50);
269 + strcpy(url, remote->url);
270 + posn = url + strlen(remote->url);
271 + strcpy(posn, "objects/");
272 + posn += 8;
273 + memcpy(posn, hex, 2);
274 + posn += 2;
275 + *(posn++) = '/';
276 + strcpy(posn, hex + 2);
277 + strcpy(request->url, url);
278 +
279 + /* If a previous temp file is present, process what was already
280 + fetched. */
281 + prevlocal = open(prevfile, O_RDONLY);
282 + if (prevlocal != -1) {
283 + do {
284 + prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
285 + if (prev_read>0) {
286 + if (fwrite_sha1_file(prev_buf,
287 + 1,
288 + prev_read,
289 + request) == prev_read) {
290 + prev_posn += prev_read;
291 + } else {
292 + prev_read = -1;
293 + }
294 + }
295 + } while (prev_read > 0);
296 + close(prevlocal);
297 + }
298 + unlink(prevfile);
299 +
300 + /* Reset inflate/SHA1 if there was an error reading the previous temp
301 + file; also rewind to the beginning of the local file. */
302 + if (prev_read == -1) {
303 + memset(&request->stream, 0, sizeof(request->stream));
304 + inflateInit(&request->stream);
305 + SHA1_Init(&request->c);
306 + if (prev_posn>0) {
307 + prev_posn = 0;
308 + lseek(request->local_fileno, SEEK_SET, 0);
309 + ftruncate(request->local_fileno, 0);
310 + }
311 + }
312 +
313 + slot = get_active_slot();
314 + slot->callback_func = process_response;
315 + slot->callback_data = request;
316 + request->slot = slot;
317 +
318 + curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
319 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
320 + curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
321 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
322 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
323 +
324 + /* If we have successfully processed data from a previous fetch
325 + attempt, only fetch the data we don't already have. */
326 + if (prev_posn>0) {
327 + if (push_verbosely)
328 + fprintf(stderr,
329 + "Resuming fetch of object %s at byte %ld\n",
330 + hex, prev_posn);
331 + sprintf(range, "Range: bytes=%ld-", prev_posn);
332 + range_header = curl_slist_append(range_header, range);
333 + curl_easy_setopt(slot->curl,
334 + CURLOPT_HTTPHEADER, range_header);
335 + }
336 +
337 + /* Try to get the request started, abort the request on error */
338 + request->state = RUN_FETCH_LOOSE;
339 + if (!start_active_slot(slot)) {
340 + fprintf(stderr, "Unable to start GET request\n");
341 + remote->can_update_info_refs = 0;
342 + release_request(request);
343 + }
344 +}
345 +
346 +static void start_fetch_packed(struct transfer_request *request)
347 +{
348 + char *url;
349 + struct packed_git *target;
350 + FILE *packfile;
351 + char *filename;
352 + long prev_posn = 0;
353 + char range[RANGE_HEADER_SIZE];
354 + struct curl_slist *range_header = NULL;
355 +
356 + struct transfer_request *check_request = request_queue_head;
357 + struct active_request_slot *slot;
358 +
359 + target = find_sha1_pack(request->obj->sha1, remote->packs);
360 + if (!target) {
361 + fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
362 + remote->can_update_info_refs = 0;
363 + release_request(request);
364 + return;
365 + }
366 +
367 + fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
368 + fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
369 +
370 + filename = sha1_pack_name(target->sha1);
371 + snprintf(request->filename, sizeof(request->filename), "%s", filename);
372 + snprintf(request->tmpfile, sizeof(request->tmpfile),
373 + "%s.temp", filename);
374 +
375 + url = xmalloc(strlen(remote->url) + 64);
376 + sprintf(url, "%sobjects/pack/pack-%s.pack",
377 + remote->url, sha1_to_hex(target->sha1));
378 +
379 + /* Make sure there isn't another open request for this pack */
380 + while (check_request) {
381 + if (check_request->state == RUN_FETCH_PACKED &&
382 + !strcmp(check_request->url, url)) {
383 + free(url);
384 + release_request(request);
385 + return;
386 + }
387 + check_request = check_request->next;
388 + }
389 +
390 + packfile = fopen(request->tmpfile, "a");
391 + if (!packfile) {
392 + fprintf(stderr, "Unable to open local file %s for pack",
393 + filename);
394 + remote->can_update_info_refs = 0;
395 + free(url);
396 + return;
397 + }
398 +
399 + slot = get_active_slot();
400 + slot->callback_func = process_response;
401 + slot->callback_data = request;
402 + request->slot = slot;
403 + request->local_stream = packfile;
404 + request->userData = target;
405 +
406 + request->url = url;
407 + curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
408 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
409 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
410 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
411 + slot->local = packfile;
412 +
413 + /* If there is data present from a previous transfer attempt,
414 + resume where it left off */
415 + prev_posn = ftell(packfile);
416 + if (prev_posn>0) {
417 + if (push_verbosely)
418 + fprintf(stderr,
419 + "Resuming fetch of pack %s at byte %ld\n",
420 + sha1_to_hex(target->sha1), prev_posn);
421 + sprintf(range, "Range: bytes=%ld-", prev_posn);
422 + range_header = curl_slist_append(range_header, range);
423 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
424 + }
425 +
426 + /* Try to get the request started, abort the request on error */
427 + request->state = RUN_FETCH_PACKED;
428 + if (!start_active_slot(slot)) {
429 + fprintf(stderr, "Unable to start GET request\n");
430 + remote->can_update_info_refs = 0;
431 + release_request(request);
432 + }
433 +}
434 +
435 static void start_mkcol(struct transfer_request *request)
436 {
437 char *hex = sha1_to_hex(request->obj->sha1);
@@ -388,6 +652,10 @@ static void release_request(struct transfer_request *request)
652 entry->next = entry->next->next;
653 }
654
655 + if (request->local_fileno != -1)
656 + close(request->local_fileno);
657 + if (request->local_stream)
658 + fclose(request->local_stream);
659 if (request->url != NULL)
660 free(request->url);
661 free(request);
@@ -395,7 +663,11 @@ static void release_request(struct transfer_request *request)
663
664 static void finish_request(struct transfer_request *request)
665 {
398 - request->curl_result = request->slot->curl_result;
666 + struct stat st;
667 + struct packed_git *target;
668 + struct packed_git **lst;
669 +
670 + request->curl_result = request->slot->curl_result;
671 request->http_code = request->slot->http_code;
672 request->slot = NULL;
673
@@ -437,7 +709,6 @@ static void finish_request(struct transfer_request *request)
709 if (request->curl_result == CURLE_OK) {
710 fprintf(stderr, " sent %s\n",
711 sha1_to_hex(request->obj->sha1));
440 - request->state = COMPLETE;
712 request->obj->flags |= REMOTE;
713 release_request(request);
714 } else {
@@ -447,12 +718,73 @@ static void finish_request(struct transfer_request *request)
718 request->state = ABORTED;
719 aborted = 1;
720 }
721 + } else if (request->state == RUN_FETCH_LOOSE) {
722 + fchmod(request->local_fileno, 0444);
723 + close(request->local_fileno); request->local_fileno = -1;
724 +
725 + if (request->curl_result != CURLE_OK &&
726 + request->http_code != 416) {
727 + if (stat(request->tmpfile, &st) == 0) {
728 + if (st.st_size == 0)
729 + unlink(request->tmpfile);
730 + }
731 + } else {
732 + if (request->http_code == 416)
733 + fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
734 +
735 + inflateEnd(&request->stream);
736 + SHA1_Final(request->real_sha1, &request->c);
737 + if (request->zret != Z_STREAM_END) {
738 + unlink(request->tmpfile);
739 + } else if (memcmp(request->obj->sha1, request->real_sha1, 20)) {
740 + unlink(request->tmpfile);
741 + } else {
742 + request->rename =
743 + move_temp_to_file(
744 + request->tmpfile,
745 + request->filename);
746 + if (request->rename == 0) {
747 + request->obj->flags |= (LOCAL | REMOTE);
748 + }
749 + }
750 + }
751 +
752 + /* Try fetching packed if necessary */
753 + if (request->obj->flags & LOCAL)
754 + release_request(request);
755 + else
756 + start_fetch_packed(request);
757 +
758 + } else if (request->state == RUN_FETCH_PACKED) {
759 + if (request->curl_result != CURLE_OK) {
760 + fprintf(stderr, "Unable to get pack file %s\n%s",
761 + request->url, curl_errorstr);
762 + remote->can_update_info_refs = 0;
763 + } else {
764 + fclose(request->local_stream);
765 + request->local_stream = NULL;
766 + if (!move_temp_to_file(request->tmpfile,
767 + request->filename)) {
768 + target = (struct packed_git *)request->userData;
769 + lst = &remote->packs;
770 + while (*lst != target)
771 + lst = &((*lst)->next);
772 + *lst = (*lst)->next;
773 +
774 + if (!verify_pack(target, 0))
775 + install_packed_git(target);
776 + else
777 + remote->can_update_info_refs = 0;
778 + }
779 + }
780 + release_request(request);
781 }
782 }
783
784 void fill_active_slots(void)
785 {
786 struct transfer_request *request = request_queue_head;
787 + struct transfer_request *next;
788 struct active_request_slot *slot = active_queue_head;
789 int num_transfers;
790
@@ -460,7 +792,10 @@ void fill_active_slots(void)
792 return;
793
794 while (active_requests < max_requests && request != NULL) {
463 - if (pushing && request->state == NEED_PUSH) {
795 + next = request->next;
796 + if (request->state == NEED_FETCH) {
797 + start_fetch_loose(request);
798 + } else if (pushing && request->state == NEED_PUSH) {
799 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
800 start_put(request);
801 } else {
@@ -468,7 +803,7 @@ void fill_active_slots(void)
803 }
804 curl_multi_perform(curlm, &num_transfers);
805 }
471 - request = request->next;
806 + request = next;
807 }
808
809 while (slot != NULL) {
@@ -482,7 +817,38 @@ void fill_active_slots(void)
817
818 static void get_remote_object_list(unsigned char parent);
819
485 -static void add_request(struct object *obj, struct remote_lock *lock)
820 +static void add_fetch_request(struct object *obj)
821 +{
822 + struct transfer_request *request;
823 +
824 + check_locks();
825 +
826 + /*
827 + * Don't fetch the object if it's known to exist locally
828 + * or is already in the request queue
829 + */
830 + if (remote_dir_exists[obj->sha1[0]] == -1)
831 + get_remote_object_list(obj->sha1[0]);
832 + if (obj->flags & (LOCAL | FETCHING))
833 + return;
834 +
835 + obj->flags |= FETCHING;
836 + request = xmalloc(sizeof(*request));
837 + request->obj = obj;
838 + request->url = NULL;
839 + request->lock = NULL;
840 + request->headers = NULL;
841 + request->local_fileno = -1;
842 + request->local_stream = NULL;
843 + request->state = NEED_FETCH;
844 + request->next = request_queue_head;
845 + request_queue_head = request;
846 +
847 + fill_active_slots();
848 + step_active_slots();
849 +}
850 +
851 +static void add_send_request(struct object *obj, struct remote_lock *lock)
852 {
853 struct transfer_request *request = request_queue_head;
854 struct packed_git *target;
@@ -510,6 +876,8 @@ static void add_request(struct object *obj, struct remote_lock *lock)
876 request->url = NULL;
877 request->lock = lock;
878 request->headers = NULL;
879 + request->local_fileno = -1;
880 + request->local_stream = NULL;
881 request->state = NEED_PUSH;
882 request->next = request_queue_head;
883 request_queue_head = request;
@@ -1375,7 +1743,7 @@ static void get_delta(struct rev_info *revs, struct remote_lock *lock)
1743 p = process_tree(commit->tree, p, NULL, "");
1744 commit->object.flags |= LOCAL;
1745 if (!(commit->object.flags & UNINTERESTING))
1378 - add_request(&commit->object, lock);
1746 + add_send_request(&commit->object, lock);
1747 }
1748
1749 for (pending = revs->pending_objects; pending; pending = pending->next) {
@@ -1402,7 +1770,7 @@ static void get_delta(struct rev_info *revs, struct remote_lock *lock)
1770
1771 while (objects) {
1772 if (!(objects->item->flags & UNINTERESTING))
1405 - add_request(objects->item, lock);
1773 + add_send_request(objects->item, lock);
1774 objects = objects->next;
1775 }
1776 }
@@ -1483,6 +1851,7 @@ static void one_remote_ref(char *refname)
1851 {
1852 struct ref *ref;
1853 unsigned char remote_sha1[20];
1854 + struct object *obj;
1855
1856 if (fetch_ref(refname, remote_sha1) != 0) {
1857 fprintf(stderr,
@@ -1491,6 +1860,19 @@ static void one_remote_ref(char *refname)
1860 return;
1861 }
1862
1863 + /*
1864 + * Fetch a copy of the object if it doesn't exist locally - it
1865 + * may be required for updating server info later.
1866 + */
1867 + if (remote->can_update_info_refs && !has_sha1_file(remote_sha1)) {
1868 + obj = lookup_unknown_object(remote_sha1);
1869 + if (obj) {
1870 + fprintf(stderr, " fetch %s for %s\n",
1871 + sha1_to_hex(remote_sha1), refname);
1872 + add_fetch_request(obj);
1873 + }
1874 + }
1875 +
1876 int len = strlen(refname) + 1;
1877 ref = xcalloc(1, sizeof(*ref) + len);
1878 memcpy(ref->old_sha1, remote_sha1, 20);
@@ -1596,6 +1978,125 @@ static void mark_edges_uninteresting(struct commit_list *list)
1978 }
1979 }
1980
1981 +static void add_remote_info_ref(struct remote_ls_ctx *ls)
1982 +{
1983 + struct buffer *buf = (struct buffer *)ls->userData;
1984 + unsigned char remote_sha1[20];
1985 + struct object *o;
1986 + int len;
1987 + char *ref_info;
1988 +
1989 + if (fetch_ref(ls->dentry_name, remote_sha1) != 0) {
1990 + fprintf(stderr,
1991 + "Unable to fetch ref %s from %s\n",
1992 + ls->dentry_name, remote->url);
1993 + aborted = 1;
1994 + return;
1995 + }
1996 +
1997 + o = parse_object(remote_sha1);
1998 + if (!o) {
1999 + fprintf(stderr,
2000 + "Unable to parse object %s for remote ref %s\n",
2001 + sha1_to_hex(remote_sha1), ls->dentry_name);
2002 + aborted = 1;
2003 + return;
2004 + }
2005 +
2006 + len = strlen(ls->dentry_name) + 42;
2007 + ref_info = xcalloc(len + 1, 1);
2008 + sprintf(ref_info, "%s %s\n",
2009 + sha1_to_hex(remote_sha1), ls->dentry_name);
2010 + fwrite_buffer(ref_info, 1, len, buf);
2011 + free(ref_info);
2012 +
2013 + if (o->type == tag_type) {
2014 + o = deref_tag(o, ls->dentry_name, 0);
2015 + if (o) {
2016 + len = strlen(ls->dentry_name) + 45;
2017 + ref_info = xcalloc(len + 1, 1);
2018 + sprintf(ref_info, "%s %s^{}\n",
2019 + sha1_to_hex(o->sha1), ls->dentry_name);
2020 + fwrite_buffer(ref_info, 1, len, buf);
2021 + free(ref_info);
2022 + }
2023 + }
2024 +}
2025 +
2026 +static void update_remote_info_refs(struct remote_lock *lock)
2027 +{
2028 + struct buffer buffer;
2029 + struct active_request_slot *slot;
2030 + struct slot_results results;
2031 + char *if_header;
2032 + struct curl_slist *dav_headers = NULL;
2033 +
2034 + buffer.buffer = xmalloc(4096);
2035 + memset(buffer.buffer, 0, 4096);
2036 + buffer.size = 4096;
2037 + buffer.posn = 0;
2038 + remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
2039 + add_remote_info_ref, &buffer);
2040 + if (!aborted) {
2041 + if_header = xmalloc(strlen(lock->token) + 25);
2042 + sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
2043 + dav_headers = curl_slist_append(dav_headers, if_header);
2044 +
2045 + slot = get_active_slot();
2046 + slot->results = &results;
2047 + curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
2048 + curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.posn);
2049 + curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
2050 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2051 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
2052 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
2053 + curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
2054 + curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
2055 + curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
2056 +
2057 + buffer.posn = 0;
2058 +
2059 + if (start_active_slot(slot)) {
2060 + run_active_slot(slot);
2061 + if (results.curl_result != CURLE_OK) {
2062 + fprintf(stderr,
2063 + "PUT error: curl result=%d, HTTP code=%ld\n",
2064 + results.curl_result, results.http_code);
2065 + }
2066 + }
2067 + free(if_header);
2068 + }
2069 + free(buffer.buffer);
2070 +}
2071 +
2072 +static int remote_exists(const char *path)
2073 +{
2074 + char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2075 + struct active_request_slot *slot;
2076 + struct slot_results results;
2077 +
2078 + sprintf(url, "%s%s", remote->url, path);
2079 +
2080 + slot = get_active_slot();
2081 + slot->results = &results;
2082 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2083 + curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2084 +
2085 + if (start_active_slot(slot)) {
2086 + run_active_slot(slot);
2087 + if (results.http_code == 404)
2088 + return 0;
2089 + else if (results.curl_result == CURLE_OK)
2090 + return 1;
2091 + else
2092 + fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2093 + } else {
2094 + fprintf(stderr, "Unable to start HEAD request\n");
2095 + }
2096 +
2097 + return -1;
2098 +}
2099 +
2100 int main(int argc, char **argv)
2101 {
2102 struct transfer_request *request;
@@ -1603,6 +2104,7 @@ int main(int argc, char **argv)
2104 int nr_refspec = 0;
2105 char **refspec = NULL;
2106 struct remote_lock *ref_lock = NULL;
2107 + struct remote_lock *info_ref_lock = NULL;
2108 struct rev_info revs;
2109 int rc = 0;
2110 int i;
@@ -1667,6 +2169,18 @@ int main(int argc, char **argv)
2169 goto cleanup;
2170 }
2171
2172 + /* Check whether the remote has server info files */
2173 + remote->can_update_info_refs = 0;
2174 + remote->has_info_refs = remote_exists("info/refs");
2175 + remote->has_info_packs = remote_exists("objects/info/packs");
2176 + if (remote->has_info_refs) {
2177 + info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2178 + if (info_ref_lock)
2179 + remote->can_update_info_refs = 1;
2180 + }
2181 + if (remote->has_info_packs)
2182 + fetch_indices();
2183 +
2184 /* Get a list of all local and remote heads to validate refspecs */
2185 get_local_heads();
2186 fprintf(stderr, "Fetching remote heads...\n");
@@ -1769,7 +2283,6 @@ int main(int argc, char **argv)
2283 pushing = 0;
2284 prepare_revision_walk(&revs);
2285 mark_edges_uninteresting(revs.commits);
1772 - fetch_indices();
2286 get_delta(&revs, ref_lock);
2287 finish_all_active_slots();
2288
@@ -1792,6 +2305,18 @@ int main(int argc, char **argv)
2305 check_locks();
2306 }
2307
2308 + /* Update remote server info if appropriate */
2309 + if (remote->has_info_refs && new_refs) {
2310 + if (info_ref_lock && remote->can_update_info_refs) {
2311 + fprintf(stderr, "Updating remote server info\n");
2312 + update_remote_info_refs(info_ref_lock);
2313 + } else {
2314 + fprintf(stderr, "Unable to update server info\n");
2315 + }
2316 + }
2317 + if (info_ref_lock)
2318 + unlock_remote(info_ref_lock);
2319 +
2320 cleanup:
2321 free(remote);
2322