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

Update http-push functionality

This brings http-push functionality more in line with the ssh/git version, by borrowing bits from send-pack and rev-list to process refspecs and revision history in more standard ways. Also, the status of remote objects is determined using PROPFIND requests for the object directory rather than HEAD requests for each object - while it may be less efficient for small numbers of objects, this approach is able to get the status of all remote loose objects in a maximum of 256 requests. Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Mar 7, 2006 at 16:13 UTC aa1dbc9897822c8acb284b35c40da60f3debca91
2 files changed +727 -335
Makefile
+1 -1
@@ -526,7 +526,7 @@ git-http-fetch$X: fetch.o http.o http-fetch.o $(LIB_FILE)
526 $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
527 $(LIBS) $(CURL_LIBCURL)
528
529 -git-http-push$X: http.o http-push.o $(LIB_FILE)
529 +git-http-push$X: revision.o http.o http-push.o $(LIB_FILE)
530 $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
531 $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
532
http-push.c
+726 -334
@@ -5,6 +5,8 @@
5 #include "tag.h"
6 #include "blob.h"
7 #include "http.h"
8 +#include "refs.h"
9 +#include "revision.h"
10
11 #include <expat.h>
12
@@ -42,14 +44,24 @@ enum XML_Status {
44 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
45 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
46 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
47 +#define DAV_PROPFIND_RESP ".multistatus.response"
48 +#define DAV_PROPFIND_NAME ".multistatus.response.href"
49 +#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
50
51 /* DAV request body templates */
47 -#define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
52 +#define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
53 +#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
54 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
55
56 #define LOCK_TIME 600
57 #define LOCK_REFRESH 30
58
59 +/* bits #0-4 in revision.h */
60 +
61 +#define LOCAL (1u << 5)
62 +#define REMOTE (1u << 6)
63 +#define PUSHING (1u << 7)
64 +
65 static int pushing = 0;
66 static int aborted = 0;
67 static char remote_dir_exists[256];
@@ -61,17 +73,19 @@ static int push_verbosely = 0;
73 static int push_all = 0;
74 static int force_all = 0;
75
76 +static struct object_list *objects = NULL;
77 +
78 struct repo
79 {
80 char *url;
81 + int path_len;
82 struct packed_git *packs;
83 };
84
85 static struct repo *remote = NULL;
86 +static struct remote_lock *remote_locks = NULL;
87
88 enum transfer_state {
73 - NEED_CHECK,
74 - RUN_HEAD,
89 NEED_PUSH,
90 RUN_MKCOL,
91 RUN_PUT,
@@ -82,10 +96,10 @@ enum transfer_state {
96
97 struct transfer_request
98 {
85 - unsigned char sha1[20];
99 + struct object *obj;
100 char *url;
101 char *dest;
88 - struct active_lock *lock;
102 + struct remote_lock *lock;
103 struct curl_slist *headers;
104 struct buffer buffer;
105 char filename[PATH_MAX];
@@ -114,14 +128,23 @@ struct xml_ctx
128 void *userData;
129 };
130
117 -struct active_lock
131 +struct remote_lock
132 {
133 char *url;
134 char *owner;
135 char *token;
136 time_t start_time;
137 long timeout;
138 + int active;
139 int refreshing;
140 + struct remote_lock *next;
141 +};
142 +
143 +struct remote_dentry
144 +{
145 + char *base;
146 + char *name;
147 + int is_dir;
148 };
149
150 static void finish_request(struct transfer_request *request);
@@ -134,42 +157,9 @@ static void process_response(void *callback_data)
157 finish_request(request);
158 }
159
137 -static void start_check(struct transfer_request *request)
138 -{
139 - char *hex = sha1_to_hex(request->sha1);
140 - struct active_request_slot *slot;
141 - char *posn;
142 -
143 - request->url = xmalloc(strlen(remote->url) + 55);
144 - strcpy(request->url, remote->url);
145 - posn = request->url + strlen(remote->url);
146 - strcpy(posn, "objects/");
147 - posn += 8;
148 - memcpy(posn, hex, 2);
149 - posn += 2;
150 - *(posn++) = '/';
151 - strcpy(posn, hex + 2);
152 -
153 - slot = get_active_slot();
154 - slot->callback_func = process_response;
155 - slot->callback_data = request;
156 - curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
157 - curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
158 - curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
159 -
160 - if (start_active_slot(slot)) {
161 - request->slot = slot;
162 - request->state = RUN_HEAD;
163 - } else {
164 - request->state = ABORTED;
165 - free(request->url);
166 - request->url = NULL;
167 - }
168 -}
169 -
160 static void start_mkcol(struct transfer_request *request)
161 {
172 - char *hex = sha1_to_hex(request->sha1);
162 + char *hex = sha1_to_hex(request->obj->sha1);
163 struct active_request_slot *slot;
164 char *posn;
165
@@ -203,7 +193,7 @@ static void start_mkcol(struct transfer_request *request)
193
194 static void start_put(struct transfer_request *request)
195 {
206 - char *hex = sha1_to_hex(request->sha1);
196 + char *hex = sha1_to_hex(request->obj->sha1);
197 struct active_request_slot *slot;
198 char *posn;
199 char type[20];
@@ -214,7 +204,7 @@ static void start_put(struct transfer_request *request)
204 ssize_t size;
205 z_stream stream;
206
217 - unpacked = read_sha1_file(request->sha1, type, &len);
207 + unpacked = read_sha1_file(request->obj->sha1, type, &len);
208 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
209
210 /* Set it up */
@@ -309,64 +299,90 @@ static void start_move(struct transfer_request *request)
299 }
300 }
301
312 -static int refresh_lock(struct active_lock *lock)
302 +static int refresh_lock(struct remote_lock *check_lock)
303 {
304 struct active_request_slot *slot;
305 char *if_header;
306 char timeout_header[25];
307 struct curl_slist *dav_headers = NULL;
318 - int rc = 0;
308 + struct remote_lock *lock;
309 + int time_remaining;
310 + time_t current_time;
311
320 - lock->refreshing = 1;
312 + /* Refresh all active locks if they're close to expiring */
313 + for (lock = remote_locks; lock; lock = lock->next) {
314 + if (!lock->active)
315 + continue;
316
322 - if_header = xmalloc(strlen(lock->token) + 25);
323 - sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
324 - sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
325 - dav_headers = curl_slist_append(dav_headers, if_header);
326 - dav_headers = curl_slist_append(dav_headers, timeout_header);
317 + current_time = time(NULL);
318 + time_remaining = lock->start_time + lock->timeout
319 + - current_time;
320 + if (time_remaining > LOCK_REFRESH)
321 + continue;
322
328 - slot = get_active_slot();
329 - curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
330 - curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
331 - curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
332 - curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
333 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
323 + lock->refreshing = 1;
324
335 - if (start_active_slot(slot)) {
336 - run_active_slot(slot);
337 - if (slot->curl_result != CURLE_OK) {
338 - fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
339 - } else {
340 - lock->start_time = time(NULL);
341 - rc = 1;
325 + if_header = xmalloc(strlen(lock->token) + 25);
326 + sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
327 + sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
328 + dav_headers = curl_slist_append(dav_headers, if_header);
329 + dav_headers = curl_slist_append(dav_headers, timeout_header);
330 +
331 + slot = get_active_slot();
332 + curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
333 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
334 + curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
335 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
336 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
337 +
338 + if (start_active_slot(slot)) {
339 + run_active_slot(slot);
340 + if (slot->curl_result != CURLE_OK) {
341 + fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
342 + lock->active = 0;
343 + } else {
344 + lock->active = 1;
345 + lock->start_time = time(NULL);
346 + }
347 }
348 +
349 + lock->refreshing = 0;
350 + curl_slist_free_all(dav_headers);
351 + free(if_header);
352 }
353
345 - lock->refreshing = 0;
346 - curl_slist_free_all(dav_headers);
347 - free(if_header);
354 + if (check_lock)
355 + return check_lock->active;
356 + else
357 + return 0;
358 +}
359
349 - return rc;
360 +static void release_request(struct transfer_request *request)
361 +{
362 + struct transfer_request *entry = request_queue_head;
363 +
364 + if (request == request_queue_head) {
365 + request_queue_head = request->next;
366 + } else {
367 + while (entry->next != NULL && entry->next != request)
368 + entry = entry->next;
369 + if (entry->next == request)
370 + entry->next = entry->next->next;
371 + }
372 +
373 + if (request->url != NULL)
374 + free(request->url);
375 + free(request);
376 }
377
378 static void finish_request(struct transfer_request *request)
379 {
354 - time_t current_time = time(NULL);
355 - int time_remaining;
356 -
380 request->curl_result = request->slot->curl_result;
381 request->http_code = request->slot->http_code;
382 request->slot = NULL;
383
361 - /* Refresh the lock if it is close to timing out */
362 - time_remaining = request->lock->start_time + request->lock->timeout
363 - - current_time;
364 - if (time_remaining < LOCK_REFRESH && !request->lock->refreshing) {
365 - if (!refresh_lock(request->lock)) {
366 - fprintf(stderr, "Unable to refresh remote lock\n");
367 - aborted = 1;
368 - }
369 - }
384 + /* Keep locks active */
385 + refresh_lock(request->lock);
386
387 if (request->headers != NULL)
388 curl_slist_free_all(request->headers);
@@ -375,29 +391,16 @@ static void finish_request(struct transfer_request *request)
391 if (request->state != RUN_PUT) {
392 free(request->url);
393 request->url = NULL;
378 - }
394 + }
395
380 - if (request->state == RUN_HEAD) {
381 - if (request->http_code == 404) {
382 - request->state = NEED_PUSH;
383 - } else if (request->curl_result == CURLE_OK) {
384 - remote_dir_exists[request->sha1[0]] = 1;
385 - request->state = COMPLETE;
386 - } else {
387 - fprintf(stderr, "HEAD %s failed, aborting (%d/%ld)\n",
388 - sha1_to_hex(request->sha1),
389 - request->curl_result, request->http_code);
390 - request->state = ABORTED;
391 - aborted = 1;
392 - }
393 - } else if (request->state == RUN_MKCOL) {
396 + if (request->state == RUN_MKCOL) {
397 if (request->curl_result == CURLE_OK ||
398 request->http_code == 405) {
396 - remote_dir_exists[request->sha1[0]] = 1;
399 + remote_dir_exists[request->obj->sha1[0]] = 1;
400 start_put(request);
401 } else {
402 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
400 - sha1_to_hex(request->sha1),
403 + sha1_to_hex(request->obj->sha1),
404 request->curl_result, request->http_code);
405 request->state = ABORTED;
406 aborted = 1;
@@ -407,21 +410,21 @@ static void finish_request(struct transfer_request *request)
410 start_move(request);
411 } else {
412 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
410 - sha1_to_hex(request->sha1),
413 + sha1_to_hex(request->obj->sha1),
414 request->curl_result, request->http_code);
415 request->state = ABORTED;
416 aborted = 1;
417 }
418 } else if (request->state == RUN_MOVE) {
419 if (request->curl_result == CURLE_OK) {
417 - if (push_verbosely)
418 - fprintf(stderr,
419 - "sent %s\n",
420 - sha1_to_hex(request->sha1));
420 + fprintf(stderr, " sent %s\n",
421 + sha1_to_hex(request->obj->sha1));
422 request->state = COMPLETE;
423 + request->obj->flags |= REMOTE;
424 + release_request(request);
425 } else {
426 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
424 - sha1_to_hex(request->sha1),
427 + sha1_to_hex(request->obj->sha1),
428 request->curl_result, request->http_code);
429 request->state = ABORTED;
430 aborted = 1;
@@ -429,24 +432,6 @@ static void finish_request(struct transfer_request *request)
432 }
433 }
434
432 -static void release_request(struct transfer_request *request)
433 -{
434 - struct transfer_request *entry = request_queue_head;
435 -
436 - if (request == request_queue_head) {
437 - request_queue_head = request->next;
438 - } else {
439 - while (entry->next != NULL && entry->next != request)
440 - entry = entry->next;
441 - if (entry->next == request)
442 - entry->next = entry->next->next;
443 - }
444 -
445 - if (request->url != NULL)
446 - free(request->url);
447 - free(request);
448 -}
449 -
435 void fill_active_slots(void)
436 {
437 struct transfer_request *request = request_queue_head;
@@ -457,14 +442,12 @@ void fill_active_slots(void)
442 return;
443
444 while (active_requests < max_requests && request != NULL) {
460 - if (!pushing && request->state == NEED_CHECK) {
461 - start_check(request);
462 - curl_multi_perform(curlm, &num_transfers);
463 - } else if (pushing && request->state == NEED_PUSH) {
464 - if (remote_dir_exists[request->sha1[0]])
445 + if (pushing && request->state == NEED_PUSH) {
446 + if (remote_dir_exists[request->obj->sha1[0]] == 1) {
447 start_put(request);
466 - else
448 + } else {
449 start_mkcol(request);
450 + }
451 curl_multi_perform(curlm, &num_transfers);
452 }
453 request = request->next;
@@ -476,29 +459,37 @@ void fill_active_slots(void)
459 slot->curl = NULL;
460 }
461 slot = slot->next;
479 - }
462 + }
463 }
464
482 -static void add_request(unsigned char *sha1, struct active_lock *lock)
465 +static void get_remote_object_list(unsigned char parent);
466 +
467 +static void add_request(struct object *obj, struct remote_lock *lock)
468 {
469 struct transfer_request *request = request_queue_head;
470 struct packed_git *target;
486 -
487 - while (request != NULL && memcmp(request->sha1, sha1, 20))
488 - request = request->next;
489 - if (request != NULL)
490 - return;
471
492 - target = find_sha1_pack(sha1, remote->packs);
493 - if (target)
472 + /*
473 + * Don't push the object if it's known to exist on the remote
474 + * or is already in the request queue
475 + */
476 + if (remote_dir_exists[obj->sha1[0]] == -1)
477 + get_remote_object_list(obj->sha1[0]);
478 + if (obj->flags & (REMOTE | PUSHING))
479 + return;
480 + target = find_sha1_pack(obj->sha1, remote->packs);
481 + if (target) {
482 + obj->flags |= REMOTE;
483 return;
484 + }
485
486 + obj->flags |= PUSHING;
487 request = xmalloc(sizeof(*request));
497 - memcpy(request->sha1, sha1, 20);
488 + request->obj = obj;
489 request->url = NULL;
490 request->lock = lock;
491 request->headers = NULL;
501 - request->state = NEED_CHECK;
492 + request->state = NEED_PUSH;
493 request->next = request_queue_head;
494 request_queue_head = request;
495
@@ -698,14 +689,13 @@ static char *quote_ref_url(const char *base, const char *ref)
689 int len, baselen, ch;
690
691 baselen = strlen(base);
701 - len = baselen + 12; /* "refs/heads/" + NUL */
692 + len = baselen + 1;
693 for (cp = ref; (ch = *cp) != 0; cp++, len++)
694 if (needs_quote(ch))
695 len += 2; /* extra two hex plus replacement % */
696 qref = xmalloc(len);
697 memcpy(qref, base, baselen);
707 - memcpy(qref + baselen, "refs/heads/", 11);
708 - for (cp = ref, dp = qref + baselen + 11; (ch = *cp) != 0; cp++) {
698 + for (cp = ref, dp = qref + baselen; (ch = *cp) != 0; cp++) {
699 if (needs_quote(ch)) {
700 *dp++ = '%';
701 *dp++ = hex((ch >> 4) & 0xF);
@@ -751,6 +741,27 @@ int fetch_ref(char *ref, unsigned char *sha1)
741 return 0;
742 }
743
744 +static void one_remote_object(const char *hex)
745 +{
746 + unsigned char sha1[20];
747 + struct object *obj;
748 +
749 + if (get_sha1_hex(hex, sha1) != 0)
750 + return;
751 +
752 + obj = lookup_object(sha1);
753 + if (!obj)
754 + obj = parse_object(sha1);
755 +
756 + /* Ignore remote objects that don't exist locally */
757 + if (!obj)
758 + return;
759 +
760 + obj->flags |= REMOTE;
761 + if (!object_list_contains(objects, obj))
762 + add_object(obj, &objects, NULL, "");
763 +}
764 +
765 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
766 {
767 int *lock_flags = (int *)ctx->userData;
@@ -772,7 +783,7 @@ static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
783
784 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
785 {
775 - struct active_lock *lock = (struct active_lock *)ctx->userData;
786 + struct remote_lock *lock = (struct remote_lock *)ctx->userData;
787
788 if (tag_closed && ctx->cdata) {
789 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
@@ -791,6 +802,57 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
802 }
803 }
804
805 +static void one_remote_ref(char *refname);
806 +static void crawl_remote_refs(char *path);
807 +
808 +static void handle_crawl_ref_ctx(struct xml_ctx *ctx, int tag_closed)
809 +{
810 + struct remote_dentry *dentry = (struct remote_dentry *)ctx->userData;
811 +
812 +
813 + if (tag_closed) {
814 + if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && dentry->name) {
815 + if (dentry->is_dir) {
816 + if (strcmp(dentry->name, dentry->base)) {
817 + crawl_remote_refs(dentry->name);
818 + }
819 + } else {
820 + one_remote_ref(dentry->name);
821 + }
822 + } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
823 + dentry->name = xmalloc(strlen(ctx->cdata) -
824 + remote->path_len + 1);
825 + strcpy(dentry->name,
826 + ctx->cdata + remote->path_len);
827 + } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
828 + dentry->is_dir = 1;
829 + }
830 + } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
831 + dentry->name = NULL;
832 + dentry->is_dir = 0;
833 + }
834 +}
835 +
836 +static void handle_remote_object_list_ctx(struct xml_ctx *ctx, int tag_closed)
837 +{
838 + char *path;
839 + char *obj_hex;
840 +
841 + if (tag_closed) {
842 + if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
843 + path = ctx->cdata + remote->path_len;
844 + if (strlen(path) != 50)
845 + return;
846 + path += 9;
847 + obj_hex = xmalloc(strlen(path));
848 + strncpy(obj_hex, path, 2);
849 + strcpy(obj_hex + 2, path + 3);
850 + one_remote_object(obj_hex);
851 + free(obj_hex);
852 + }
853 + }
854 +}
855 +
856 static void
857 xml_start_tag(void *userData, const char *name, const char **atts)
858 {
@@ -848,7 +910,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
910 strncpy(ctx->cdata, s, len);
911 }
912
851 -static struct active_lock *lock_remote(char *file, long timeout)
913 +static struct remote_lock *lock_remote(char *path, long timeout)
914 {
915 struct active_request_slot *slot;
916 struct buffer out_buffer;
@@ -858,14 +920,26 @@ static struct active_lock *lock_remote(char *file, long timeout)
920 char *url;
921 char *ep;
922 char timeout_header[25];
861 - struct active_lock *new_lock = NULL;
923 + struct remote_lock *lock = remote_locks;
924 XML_Parser parser = XML_ParserCreate(NULL);
925 enum XML_Status result;
926 struct curl_slist *dav_headers = NULL;
927 struct xml_ctx ctx;
928
867 - url = xmalloc(strlen(remote->url) + strlen(file) + 1);
868 - sprintf(url, "%s%s", remote->url, file);
929 + url = xmalloc(strlen(remote->url) + strlen(path) + 1);
930 + sprintf(url, "%s%s", remote->url, path);
931 +
932 + /* Make sure the url is not already locked */
933 + while (lock && strcmp(lock->url, url)) {
934 + lock = lock->next;
935 + }
936 + if (lock) {
937 + free(url);
938 + if (refresh_lock(lock))
939 + return lock;
940 + else
941 + return NULL;
942 + }
943
944 /* Make sure leading directories exist for the remote ref */
945 ep = strchr(url + strlen(remote->url) + 11, '/');
@@ -921,11 +995,11 @@ static struct active_lock *lock_remote(char *file, long timeout)
995 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
996 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
997
924 - new_lock = xcalloc(1, sizeof(*new_lock));
925 - new_lock->owner = NULL;
926 - new_lock->token = NULL;
927 - new_lock->timeout = -1;
928 - new_lock->refreshing = 0;
998 + lock = xcalloc(1, sizeof(*lock));
999 + lock->owner = NULL;
1000 + lock->token = NULL;
1001 + lock->timeout = -1;
1002 + lock->refreshing = 0;
1003
1004 if (start_active_slot(slot)) {
1005 run_active_slot(slot);
@@ -934,7 +1008,7 @@ static struct active_lock *lock_remote(char *file, long timeout)
1008 ctx.len = 0;
1009 ctx.cdata = NULL;
1010 ctx.userFunc = handle_new_lock_ctx;
937 - ctx.userData = new_lock;
1011 + ctx.userData = lock;
1012 XML_SetUserData(parser, &ctx);
1013 XML_SetElementHandler(parser, xml_start_tag,
1014 xml_end_tag);
@@ -946,7 +1020,7 @@ static struct active_lock *lock_remote(char *file, long timeout)
1020 fprintf(stderr, "XML error: %s\n",
1021 XML_ErrorString(
1022 XML_GetErrorCode(parser)));
949 - new_lock->timeout = -1;
1023 + lock->timeout = -1;
1024 }
1025 }
1026 } else {
@@ -957,23 +1031,26 @@ static struct active_lock *lock_remote(char *file, long timeout)
1031 free(out_data);
1032 free(in_data);
1033
960 - if (new_lock->token == NULL || new_lock->timeout <= 0) {
961 - if (new_lock->token != NULL)
962 - free(new_lock->token);
963 - if (new_lock->owner != NULL)
964 - free(new_lock->owner);
1034 + if (lock->token == NULL || lock->timeout <= 0) {
1035 + if (lock->token != NULL)
1036 + free(lock->token);
1037 + if (lock->owner != NULL)
1038 + free(lock->owner);
1039 free(url);
966 - free(new_lock);
967 - new_lock = NULL;
1040 + free(lock);
1041 + lock = NULL;
1042 } else {
969 - new_lock->url = url;
970 - new_lock->start_time = time(NULL);
1043 + lock->url = url;
1044 + lock->active = 1;
1045 + lock->start_time = time(NULL);
1046 + lock->next = remote_locks;
1047 + remote_locks = lock;
1048 }
1049
973 - return new_lock;
1050 + return lock;
1051 }
1052
976 -static int unlock_remote(struct active_lock *lock)
1053 +static int unlock_remote(struct remote_lock *lock)
1054 {
1055 struct active_request_slot *slot;
1056 char *lock_token_header;
@@ -1005,15 +1082,171 @@ static int unlock_remote(struct active_lock *lock)
1082 curl_slist_free_all(dav_headers);
1083 free(lock_token_header);
1084
1008 - if (lock->owner != NULL)
1009 - free(lock->owner);
1010 - free(lock->url);
1011 - free(lock->token);
1012 - free(lock);
1085 + lock->active = 0;
1086
1087 return rc;
1088 }
1089
1090 +static void crawl_remote_refs(char *path)
1091 +{
1092 + char *url;
1093 + struct active_request_slot *slot;
1094 + struct buffer in_buffer;
1095 + struct buffer out_buffer;
1096 + char *in_data;
1097 + char *out_data;
1098 + XML_Parser parser = XML_ParserCreate(NULL);
1099 + enum XML_Status result;
1100 + struct curl_slist *dav_headers = NULL;
1101 + struct xml_ctx ctx;
1102 + struct remote_dentry dentry;
1103 +
1104 + fprintf(stderr, " %s\n", path);
1105 +
1106 + dentry.base = path;
1107 + dentry.name = NULL;
1108 + dentry.is_dir = 0;
1109 +
1110 + url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1111 + sprintf(url, "%s%s", remote->url, path);
1112 +
1113 + out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
1114 + out_data = xmalloc(out_buffer.size + 1);
1115 + snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
1116 + out_buffer.posn = 0;
1117 + out_buffer.buffer = out_data;
1118 +
1119 + in_buffer.size = 4096;
1120 + in_data = xmalloc(in_buffer.size);
1121 + in_buffer.posn = 0;
1122 + in_buffer.buffer = in_data;
1123 +
1124 + dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1125 + dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1126 +
1127 + slot = get_active_slot();
1128 + curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1129 + curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1130 + curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1131 + curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1132 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1133 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1134 + curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1135 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1136 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1137 +
1138 + if (start_active_slot(slot)) {
1139 + run_active_slot(slot);
1140 + if (slot->curl_result == CURLE_OK) {
1141 + ctx.name = xcalloc(10, 1);
1142 + ctx.len = 0;
1143 + ctx.cdata = NULL;
1144 + ctx.userFunc = handle_crawl_ref_ctx;
1145 + ctx.userData = &dentry;
1146 + XML_SetUserData(parser, &ctx);
1147 + XML_SetElementHandler(parser, xml_start_tag,
1148 + xml_end_tag);
1149 + XML_SetCharacterDataHandler(parser, xml_cdata);
1150 + result = XML_Parse(parser, in_buffer.buffer,
1151 + in_buffer.posn, 1);
1152 + free(ctx.name);
1153 +
1154 + if (result != XML_STATUS_OK) {
1155 + fprintf(stderr, "XML error: %s\n",
1156 + XML_ErrorString(
1157 + XML_GetErrorCode(parser)));
1158 + }
1159 + }
1160 + } else {
1161 + fprintf(stderr, "Unable to start request\n");
1162 + }
1163 +
1164 + free(url);
1165 + free(out_data);
1166 + free(in_buffer.buffer);
1167 + curl_slist_free_all(dav_headers);
1168 +}
1169 +
1170 +static void get_remote_object_list(unsigned char parent)
1171 +{
1172 + char *url;
1173 + struct active_request_slot *slot;
1174 + struct buffer in_buffer;
1175 + struct buffer out_buffer;
1176 + char *in_data;
1177 + char *out_data;
1178 + XML_Parser parser = XML_ParserCreate(NULL);
1179 + enum XML_Status result;
1180 + struct curl_slist *dav_headers = NULL;
1181 + struct xml_ctx ctx;
1182 + char path[] = "/objects/XX/";
1183 + static const char hex[] = "0123456789abcdef";
1184 + unsigned int val = parent;
1185 +
1186 + path[9] = hex[val >> 4];
1187 + path[10] = hex[val & 0xf];
1188 + url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1189 + sprintf(url, "%s%s", remote->url, path);
1190 +
1191 + out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
1192 + out_data = xmalloc(out_buffer.size + 1);
1193 + snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
1194 + out_buffer.posn = 0;
1195 + out_buffer.buffer = out_data;
1196 +
1197 + in_buffer.size = 4096;
1198 + in_data = xmalloc(in_buffer.size);
1199 + in_buffer.posn = 0;
1200 + in_buffer.buffer = in_data;
1201 +
1202 + dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1203 + dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1204 +
1205 + slot = get_active_slot();
1206 + curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1207 + curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1208 + curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1209 + curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1210 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1211 + curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1212 + curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1213 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1214 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1215 +
1216 + if (start_active_slot(slot)) {
1217 + run_active_slot(slot);
1218 + if (slot->curl_result == CURLE_OK) {
1219 + remote_dir_exists[parent] = 1;
1220 + ctx.name = xcalloc(10, 1);
1221 + ctx.len = 0;
1222 + ctx.cdata = NULL;
1223 + ctx.userFunc = handle_remote_object_list_ctx;
1224 + XML_SetUserData(parser, &ctx);
1225 + XML_SetElementHandler(parser, xml_start_tag,
1226 + xml_end_tag);
1227 + XML_SetCharacterDataHandler(parser, xml_cdata);
1228 + result = XML_Parse(parser, in_buffer.buffer,
1229 + in_buffer.posn, 1);
1230 + free(ctx.name);
1231 +
1232 + if (result != XML_STATUS_OK) {
1233 + fprintf(stderr, "XML error: %s\n",
1234 + XML_ErrorString(
1235 + XML_GetErrorCode(parser)));
1236 + }
1237 + } else {
1238 + remote_dir_exists[parent] = 0;
1239 + }
1240 + } else {
1241 + fprintf(stderr, "Unable to start request\n");
1242 + }
1243 +
1244 + free(url);
1245 + free(out_data);
1246 + free(in_buffer.buffer);
1247 + curl_slist_free_all(dav_headers);
1248 +}
1249 +
1250 static int locking_available(void)
1251 {
1252 struct active_request_slot *slot;
@@ -1027,9 +1260,12 @@ static int locking_available(void)
1260 struct xml_ctx ctx;
1261 int lock_flags = 0;
1262
1030 - out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1263 + out_buffer.size =
1264 + strlen(PROPFIND_SUPPORTEDLOCK_REQUEST) +
1265 + strlen(remote->url) - 2;
1266 out_data = xmalloc(out_buffer.size + 1);
1032 - snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url);
1267 + snprintf(out_data, out_buffer.size + 1,
1268 + PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1269 out_buffer.posn = 0;
1270 out_buffer.buffer = out_data;
1271
@@ -1085,85 +1321,99 @@ static int locking_available(void)
1321 return lock_flags;
1322 }
1323
1088 -static int is_ancestor(unsigned char *sha1, struct commit *commit)
1324 +static struct object_list **process_blob(struct blob *blob,
1325 + struct object_list **p,
1326 + struct name_path *path,
1327 + const char *name)
1328 {
1090 - struct commit_list *parents;
1329 + struct object *obj = &blob->object;
1330
1092 - if (parse_commit(commit))
1093 - return 0;
1094 - parents = commit->parents;
1095 - for (; parents; parents = parents->next) {
1096 - if (!memcmp(sha1, parents->item->object.sha1, 20)) {
1097 - return 1;
1098 - } else if (parents->item->object.type == commit_type) {
1099 - if (is_ancestor(
1100 - sha1,
1101 - (struct commit *)&parents->item->object
1102 - ))
1103 - return 1;
1104 - }
1331 + obj->flags |= LOCAL;
1332 +
1333 + if (obj->flags & (UNINTERESTING | SEEN))
1334 + return p;
1335 +
1336 + obj->flags |= SEEN;
1337 + return add_object(obj, p, path, name);
1338 +}
1339 +
1340 +static struct object_list **process_tree(struct tree *tree,
1341 + struct object_list **p,
1342 + struct name_path *path,
1343 + const char *name)
1344 +{
1345 + struct object *obj = &tree->object;
1346 + struct tree_entry_list *entry;
1347 + struct name_path me;
1348 +
1349 + obj->flags |= LOCAL;
1350 +
1351 + if (obj->flags & (UNINTERESTING | SEEN))
1352 + return p;
1353 + if (parse_tree(tree) < 0)
1354 + die("bad tree object %s", sha1_to_hex(obj->sha1));
1355 +
1356 + obj->flags |= SEEN;
1357 + p = add_object(obj, p, NULL, name);
1358 + me.up = path;
1359 + me.elem = name;
1360 + me.elem_len = strlen(name);
1361 + entry = tree->entries;
1362 + tree->entries = NULL;
1363 + while (entry) {
1364 + struct tree_entry_list *next = entry->next;
1365 + if (entry->directory)
1366 + p = process_tree(entry->item.tree, p, &me, entry->name);
1367 + else
1368 + p = process_blob(entry->item.blob, p, &me, entry->name);
1369 + free(entry);
1370 + entry = next;
1371 }
1106 - return 0;
1372 + return p;
1373 }
1374
1109 -static void get_delta(unsigned char *sha1, struct object *obj,
1110 - struct active_lock *lock)
1375 +static void get_delta(struct rev_info *revs, struct remote_lock *lock)
1376 {
1377 struct commit *commit;
1113 - struct commit_list *parents;
1114 - struct tree *tree;
1115 - struct tree_entry_list *entry;
1378 + struct object_list **p = &objects, *pending;
1379
1117 - if (sha1 && !memcmp(sha1, obj->sha1, 20))
1118 - return;
1380 + while ((commit = get_revision(revs)) != NULL) {
1381 + p = process_tree(commit->tree, p, NULL, "");
1382 + commit->object.flags |= LOCAL;
1383 + if (!(commit->object.flags & UNINTERESTING))
1384 + add_request(&commit->object, lock);
1385 + }
1386
1120 - if (aborted)
1121 - return;
1387 + for (pending = revs->pending_objects; pending; pending = pending->next) {
1388 + struct object *obj = pending->item;
1389 + const char *name = pending->name;
1390
1123 - if (obj->type == commit_type) {
1124 - if (push_verbosely)
1125 - fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1126 - add_request(obj->sha1, lock);
1127 - commit = (struct commit *)obj;
1128 - if (parse_commit(commit)) {
1129 - fprintf(stderr, "Error parsing commit %s\n",
1130 - sha1_to_hex(obj->sha1));
1131 - aborted = 1;
1132 - return;
1391 + if (obj->flags & (UNINTERESTING | SEEN))
1392 + continue;
1393 + if (obj->type == tag_type) {
1394 + obj->flags |= SEEN;
1395 + p = add_object(obj, p, NULL, name);
1396 + continue;
1397 }
1134 - parents = commit->parents;
1135 - for (; parents; parents = parents->next)
1136 - if (sha1 == NULL ||
1137 - memcmp(sha1, parents->item->object.sha1, 20))
1138 - get_delta(sha1, &parents->item->object,
1139 - lock);
1140 - get_delta(sha1, &commit->tree->object, lock);
1141 - } else if (obj->type == tree_type) {
1142 - if (push_verbosely)
1143 - fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1144 - add_request(obj->sha1, lock);
1145 - tree = (struct tree *)obj;
1146 - if (parse_tree(tree)) {
1147 - fprintf(stderr, "Error parsing tree %s\n",
1148 - sha1_to_hex(obj->sha1));
1149 - aborted = 1;
1150 - return;
1398 + if (obj->type == tree_type) {
1399 + p = process_tree((struct tree *)obj, p, NULL, name);
1400 + continue;
1401 }
1152 - entry = tree->entries;
1153 - tree->entries = NULL;
1154 - while (entry) {
1155 - struct tree_entry_list *next = entry->next;
1156 - get_delta(sha1, entry->item.any, lock);
1157 - free(entry->name);
1158 - free(entry);
1159 - entry = next;
1402 + if (obj->type == blob_type) {
1403 + p = process_blob((struct blob *)obj, p, NULL, name);
1404 + continue;
1405 }
1161 - } else if (obj->type == blob_type || obj->type == tag_type) {
1162 - add_request(obj->sha1, lock);
1406 + die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1407 + }
1408 +
1409 + while (objects) {
1410 + if (!(objects->item->flags & UNINTERESTING))
1411 + add_request(objects->item, lock);
1412 + objects = objects->next;
1413 }
1414 }
1415
1166 -static int update_remote(unsigned char *sha1, struct active_lock *lock)
1416 +static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1417 {
1418 struct active_request_slot *slot;
1419 char *out_data;
@@ -1218,22 +1468,146 @@ static int update_remote(unsigned char *sha1, struct active_lock *lock)
1468 return 1;
1469 }
1470
1471 +static struct ref *local_refs, **local_tail;
1472 +static struct ref *remote_refs, **remote_tail;
1473 +
1474 +static int one_local_ref(const char *refname, const unsigned char *sha1)
1475 +{
1476 + struct ref *ref;
1477 + int len = strlen(refname) + 1;
1478 + ref = xcalloc(1, sizeof(*ref) + len);
1479 + memcpy(ref->new_sha1, sha1, 20);
1480 + memcpy(ref->name, refname, len);
1481 + *local_tail = ref;
1482 + local_tail = &ref->next;
1483 + return 0;
1484 +}
1485 +
1486 +static void one_remote_ref(char *refname)
1487 +{
1488 + struct ref *ref;
1489 + unsigned char remote_sha1[20];
1490 +
1491 + if (fetch_ref(refname, remote_sha1) != 0) {
1492 + fprintf(stderr,
1493 + "Unable to fetch ref %s from %s\n",
1494 + refname, remote->url);
1495 + return;
1496 + }
1497 +
1498 + int len = strlen(refname) + 1;
1499 + ref = xcalloc(1, sizeof(*ref) + len);
1500 + memcpy(ref->old_sha1, remote_sha1, 20);
1501 + memcpy(ref->name, refname, len);
1502 + *remote_tail = ref;
1503 + remote_tail = &ref->next;
1504 +}
1505 +
1506 +static void get_local_heads(void)
1507 +{
1508 + local_tail = &local_refs;
1509 + for_each_ref(one_local_ref);
1510 +}
1511 +
1512 +static void get_dav_remote_heads(void)
1513 +{
1514 + remote_tail = &remote_refs;
1515 + crawl_remote_refs("refs/");
1516 +}
1517 +
1518 +static int is_zero_sha1(const unsigned char *sha1)
1519 +{
1520 + int i;
1521 +
1522 + for (i = 0; i < 20; i++) {
1523 + if (*sha1++)
1524 + return 0;
1525 + }
1526 + return 1;
1527 +}
1528 +
1529 +static void unmark_and_free(struct commit_list *list, unsigned int mark)
1530 +{
1531 + while (list) {
1532 + struct commit_list *temp = list;
1533 + temp->item->object.flags &= ~mark;
1534 + list = temp->next;
1535 + free(temp);
1536 + }
1537 +}
1538 +
1539 +static int ref_newer(const unsigned char *new_sha1,
1540 + const unsigned char *old_sha1)
1541 +{
1542 + struct object *o;
1543 + struct commit *old, *new;
1544 + struct commit_list *list, *used;
1545 + int found = 0;
1546 +
1547 + /* Both new and old must be commit-ish and new is descendant of
1548 + * old. Otherwise we require --force.
1549 + */
1550 + o = deref_tag(parse_object(old_sha1), NULL, 0);
1551 + if (!o || o->type != commit_type)
1552 + return 0;
1553 + old = (struct commit *) o;
1554 +
1555 + o = deref_tag(parse_object(new_sha1), NULL, 0);
1556 + if (!o || o->type != commit_type)
1557 + return 0;
1558 + new = (struct commit *) o;
1559 +
1560 + if (parse_commit(new) < 0)
1561 + return 0;
1562 +
1563 + used = list = NULL;
1564 + commit_list_insert(new, &list);
1565 + while (list) {
1566 + new = pop_most_recent_commit(&list, TMP_MARK);
1567 + commit_list_insert(new, &used);
1568 + if (new == old) {
1569 + found = 1;
1570 + break;
1571 + }
1572 + }
1573 + unmark_and_free(list, TMP_MARK);
1574 + unmark_and_free(used, TMP_MARK);
1575 + return found;
1576 +}
1577 +
1578 +static void mark_edge_parents_uninteresting(struct commit *commit)
1579 +{
1580 + struct commit_list *parents;
1581 +
1582 + for (parents = commit->parents; parents; parents = parents->next) {
1583 + struct commit *parent = parents->item;
1584 + if (!(parent->object.flags & UNINTERESTING))
1585 + continue;
1586 + mark_tree_uninteresting(parent->tree);
1587 + }
1588 +}
1589 +
1590 +static void mark_edges_uninteresting(struct commit_list *list)
1591 +{
1592 + for ( ; list; list = list->next) {
1593 + struct commit *commit = list->item;
1594 +
1595 + if (commit->object.flags & UNINTERESTING) {
1596 + mark_tree_uninteresting(commit->tree);
1597 + continue;
1598 + }
1599 + mark_edge_parents_uninteresting(commit);
1600 + }
1601 +}
1602 +
1603 int main(int argc, char **argv)
1604 {
1605 struct transfer_request *request;
1606 struct transfer_request *next_request;
1607 int nr_refspec = 0;
1608 char **refspec = NULL;
1227 - int do_remote_update;
1228 - int new_branch;
1229 - int force_this;
1230 - char *local_ref;
1231 - unsigned char local_sha1[20];
1232 - struct object *local_object = NULL;
1233 - char *remote_ref = NULL;
1234 - unsigned char remote_sha1[20];
1235 - struct active_lock *remote_lock;
1236 - char *remote_path = NULL;
1609 + struct remote_lock *ref_lock;
1610 + struct rev_info revs;
1611 int rc = 0;
1612 int i;
1613
@@ -1242,6 +1616,7 @@ int main(int argc, char **argv)
1616
1617 remote = xmalloc(sizeof(*remote));
1618 remote->url = NULL;
1619 + remote->path_len = 0;
1620 remote->packs = NULL;
1621
1622 argv++;
@@ -1249,7 +1624,7 @@ int main(int argc, char **argv)
1624 char *arg = *argv;
1625
1626 if (*arg == '-') {
1252 - if (!strcmp(arg, "--complete")) {
1627 + if (!strcmp(arg, "--all")) {
1628 push_all = 1;
1629 continue;
1630 }
@@ -1265,6 +1640,12 @@ int main(int argc, char **argv)
1640 }
1641 if (!remote->url) {
1642 remote->url = arg;
1643 + char *path = strstr(arg, "//");
1644 + if (path) {
1645 + path = index(path+2, '/');
1646 + if (path)
1647 + remote->path_len = strlen(path);
1648 + }
1649 continue;
1650 }
1651 refspec = argv;
@@ -1275,7 +1656,7 @@ int main(int argc, char **argv)
1656 if (!remote->url)
1657 usage(http_push_usage);
1658
1278 - memset(remote_dir_exists, 0, 256);
1659 + memset(remote_dir_exists, -1, 256);
1660
1661 http_init();
1662
@@ -1293,92 +1674,113 @@ int main(int argc, char **argv)
1674 goto cleanup;
1675 }
1676
1296 - /* Process each refspec */
1297 - for (i = 0; i < nr_refspec; i++) {
1298 - char *ep;
1299 - force_this = 0;
1300 - do_remote_update = 0;
1301 - new_branch = 0;
1302 - local_ref = refspec[i];
1303 - if (*local_ref == '+') {
1304 - force_this = 1;
1305 - local_ref++;
1677 + /* Get a list of all local and remote heads to validate refspecs */
1678 + get_local_heads();
1679 + fprintf(stderr, "Fetching remote heads...\n");
1680 + get_dav_remote_heads();
1681 +
1682 + /* match them up */
1683 + if (!remote_tail)
1684 + remote_tail = &remote_refs;
1685 + if (match_refs(local_refs, remote_refs, &remote_tail,
1686 + nr_refspec, refspec, push_all))
1687 + return -1;
1688 + if (!remote_refs) {
1689 + fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1690 + return 0;
1691 + }
1692 +
1693 + int ret = 0;
1694 + int new_refs = 0;
1695 + struct ref *ref;
1696 + for (ref = remote_refs; ref; ref = ref->next) {
1697 + char old_hex[60], *new_hex;
1698 + if (!ref->peer_ref)
1699 + continue;
1700 + if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
1701 + if (push_verbosely || 1)
1702 + fprintf(stderr, "'%s': up-to-date\n", ref->name);
1703 + continue;
1704 + }
1705 +
1706 + if (!force_all &&
1707 + !is_zero_sha1(ref->old_sha1) &&
1708 + !ref->force) {
1709 + if (!has_sha1_file(ref->old_sha1) ||
1710 + !ref_newer(ref->peer_ref->new_sha1,
1711 + ref->old_sha1)) {
1712 + /* We do not have the remote ref, or
1713 + * we know that the remote ref is not
1714 + * an ancestor of what we are trying to
1715 + * push. Either way this can be losing
1716 + * commits at the remote end and likely
1717 + * we were not up to date to begin with.
1718 + */
1719 + error("remote '%s' is not a strict "
1720 + "subset of local ref '%s'. "
1721 + "maybe you are not up-to-date and "
1722 + "need to pull first?",
1723 + ref->name,
1724 + ref->peer_ref->name);
1725 + ret = -2;
1726 + continue;
1727 + }
1728 }
1307 - ep = strchr(local_ref, ':');
1308 - if (ep) {
1309 - remote_ref = ep + 1;
1310 - *ep = 0;
1729 + memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
1730 + if (is_zero_sha1(ref->new_sha1)) {
1731 + error("cannot happen anymore");
1732 + ret = -3;
1733 + continue;
1734 }
1312 - else
1313 - remote_ref = local_ref;
1735 + new_refs++;
1736 + strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1737 + new_hex = sha1_to_hex(ref->new_sha1);
1738 +
1739 + fprintf(stderr, "updating '%s'", ref->name);
1740 + if (strcmp(ref->name, ref->peer_ref->name))
1741 + fprintf(stderr, " using '%s'", ref->peer_ref->name);
1742 + fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
1743 +
1744
1745 /* Lock remote branch ref */
1316 - if (remote_path)
1317 - free(remote_path);
1318 - remote_path = xmalloc(strlen(remote_ref) + 12);
1319 - sprintf(remote_path, "refs/heads/%s", remote_ref);
1320 - remote_lock = lock_remote(remote_path, LOCK_TIME);
1321 - if (remote_lock == NULL) {
1746 + ref_lock = lock_remote(ref->name, LOCK_TIME);
1747 + if (ref_lock == NULL) {
1748 fprintf(stderr, "Unable to lock remote branch %s\n",
1323 - remote_ref);
1749 + ref->name);
1750 rc = 1;
1751 continue;
1752 }
1753
1328 - /* Resolve local and remote refs */
1329 - if (fetch_ref(remote_ref, remote_sha1) != 0) {
1330 - fprintf(stderr,
1331 - "Remote branch %s does not exist on %s\n",
1332 - remote_ref, remote->url);
1333 - new_branch = 1;
1334 - }
1335 - if (get_sha1(local_ref, local_sha1) != 0) {
1336 - fprintf(stderr, "Error resolving local branch %s\n",
1337 - local_ref);
1338 - rc = 1;
1339 - goto unlock;
1754 + /* Set up revision info for this refspec */
1755 + const char *commit_argv[3];
1756 + int commit_argc = 2;
1757 + char *new_sha1_hex = strdup(sha1_to_hex(ref->new_sha1));
1758 + char *old_sha1_hex = NULL;
1759 + commit_argv[1] = new_sha1_hex;
1760 + if (!push_all && !is_zero_sha1(ref->old_sha1)) {
1761 + old_sha1_hex = xmalloc(42);
1762 + sprintf(old_sha1_hex, "^%s",
1763 + sha1_to_hex(ref->old_sha1));
1764 + commit_argv[2] = old_sha1_hex;
1765 + commit_argc++;
1766 }
1341 -
1342 - /* Find relationship between local and remote */
1343 - local_object = parse_object(local_sha1);
1344 - if (!local_object) {
1345 - fprintf(stderr, "Unable to parse local object %s\n",
1346 - sha1_to_hex(local_sha1));
1347 - rc = 1;
1348 - goto unlock;
1349 - } else if (new_branch) {
1350 - do_remote_update = 1;
1351 - } else {
1352 - if (!memcmp(local_sha1, remote_sha1, 20)) {
1353 - fprintf(stderr,
1354 - "* %s: same as branch '%s' of %s\n",
1355 - local_ref, remote_ref, remote->url);
1356 - } else if (is_ancestor(remote_sha1,
1357 - (struct commit *)local_object)) {
1358 - fprintf(stderr,
1359 - "Remote %s will fast-forward to local %s\n",
1360 - remote_ref, local_ref);
1361 - do_remote_update = 1;
1362 - } else if (force_all || force_this) {
1363 - fprintf(stderr,
1364 - "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1365 - remote_ref, remote->url, local_ref);
1366 - do_remote_update = 1;
1367 - } else {
1368 - fprintf(stderr,
1369 - "* %s on %s does not fast forward to local branch '%s'\n",
1370 - remote_ref, remote->url, local_ref);
1371 - rc = 1;
1372 - goto unlock;
1373 - }
1767 + revs.commits = NULL;
1768 + setup_revisions(commit_argc, commit_argv, &revs, NULL);
1769 + revs.tag_objects = 1;
1770 + revs.tree_objects = 1;
1771 + revs.blob_objects = 1;
1772 + free(new_sha1_hex);
1773 + if (old_sha1_hex) {
1774 + free(old_sha1_hex);
1775 + commit_argv[1] = NULL;
1776 }
1777
1376 - /* Generate and check list of required objects */
1778 + /* Generate a list of objects that need to be pushed */
1779 pushing = 0;
1378 - if (do_remote_update || push_all)
1379 - fetch_indices();
1380 - get_delta(push_all ? NULL : remote_sha1,
1381 - local_object, remote_lock);
1780 + prepare_revision_walk(&revs);
1781 + mark_edges_uninteresting(revs.commits);
1782 + fetch_indices();
1783 + get_delta(&revs, ref_lock);
1784 finish_all_active_slots();
1785
1786 /* Push missing objects to remote, this would be a
@@ -1388,25 +1790,15 @@ int main(int argc, char **argv)
1790 finish_all_active_slots();
1791
1792 /* Update the remote branch if all went well */
1391 - if (do_remote_update) {
1392 - if (!aborted && update_remote(local_sha1,
1393 - remote_lock)) {
1394 - fprintf(stderr, "%s remote branch %s\n",
1395 - new_branch ? "Created" : "Updated",
1396 - remote_ref);
1397 - } else {
1398 - fprintf(stderr,
1399 - "Unable to %s remote branch %s\n",
1400 - new_branch ? "create" : "update",
1401 - remote_ref);
1402 - rc = 1;
1403 - goto unlock;
1404 - }
1793 + if (aborted || !update_remote(ref->new_sha1, ref_lock)) {
1794 + rc = 1;
1795 + goto unlock;
1796 }
1797
1798 unlock:
1408 - unlock_remote(remote_lock);
1409 - free(remote_path);
1799 + if (!rc)
1800 + fprintf(stderr, " done\n");
1801 + unlock_remote(ref_lock);
1802 }
1803
1804 cleanup: