13 static const char http_push_usage
[] =
14 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
21 #define XML_STATUS_OK 1
22 #define XML_STATUS_ERROR 0
25 #define RANGE_HEADER_SIZE 30
28 #define DAV_LOCK "LOCK"
29 #define DAV_MKCOL "MKCOL"
30 #define DAV_MOVE "MOVE"
31 #define DAV_PROPFIND "PROPFIND"
33 #define DAV_UNLOCK "UNLOCK"
36 #define DAV_PROP_LOCKWR (1u << 0)
37 #define DAV_PROP_LOCKEX (1u << 1)
38 #define DAV_LOCK_OK (1u << 2)
40 /* DAV XML properties */
41 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
42 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
43 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
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"
51 /* DAV request body templates */
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>"
57 #define LOCK_REFRESH 30
59 /* bits #0-4 in revision.h */
61 #define LOCAL (1u << 5)
62 #define REMOTE (1u << 6)
63 #define PUSHING (1u << 7)
65 static int pushing
= 0;
66 static int aborted
= 0;
67 static char remote_dir_exists
[256];
69 static struct curl_slist
*no_pragma_header
;
70 static struct curl_slist
*default_headers
;
72 static int push_verbosely
= 0;
73 static int push_all
= 0;
74 static int force_all
= 0;
76 static struct object_list
*objects
= NULL
;
82 struct packed_git
*packs
;
85 static struct repo
*remote
= NULL
;
86 static struct remote_lock
*remote_locks
= NULL
;
97 struct transfer_request
102 struct remote_lock
*lock
;
103 struct curl_slist
*headers
;
104 struct buffer buffer
;
105 char filename
[PATH_MAX
];
106 char tmpfile
[PATH_MAX
];
107 enum transfer_state state
;
108 CURLcode curl_result
;
109 char errorstr
[CURL_ERROR_SIZE
];
111 unsigned char real_sha1
[20];
116 struct active_request_slot
*slot
;
117 struct transfer_request
*next
;
120 static struct transfer_request
*request_queue_head
= NULL
;
127 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
140 struct remote_lock
*next
;
150 static void finish_request(struct transfer_request
*request
);
152 static void process_response(void *callback_data
)
154 struct transfer_request
*request
=
155 (struct transfer_request
*)callback_data
;
157 finish_request(request
);
160 static void start_mkcol(struct transfer_request
*request
)
162 char *hex
= sha1_to_hex(request
->obj
->sha1
);
163 struct active_request_slot
*slot
;
166 request
->url
= xmalloc(strlen(remote
->url
) + 13);
167 strcpy(request
->url
, remote
->url
);
168 posn
= request
->url
+ strlen(remote
->url
);
169 strcpy(posn
, "objects/");
171 memcpy(posn
, hex
, 2);
175 slot
= get_active_slot();
176 slot
->callback_func
= process_response
;
177 slot
->callback_data
= request
;
178 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
179 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
180 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
181 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
182 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
184 if (start_active_slot(slot
)) {
185 request
->slot
= slot
;
186 request
->state
= RUN_MKCOL
;
188 request
->state
= ABORTED
;
194 static void start_put(struct transfer_request
*request
)
196 char *hex
= sha1_to_hex(request
->obj
->sha1
);
197 struct active_request_slot
*slot
;
207 unpacked
= read_sha1_file(request
->obj
->sha1
, type
, &len
);
208 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
211 memset(&stream
, 0, sizeof(stream
));
212 deflateInit(&stream
, Z_BEST_COMPRESSION
);
213 size
= deflateBound(&stream
, len
+ hdrlen
);
214 request
->buffer
.buffer
= xmalloc(size
);
217 stream
.next_out
= request
->buffer
.buffer
;
218 stream
.avail_out
= size
;
221 stream
.next_in
= (void *)hdr
;
222 stream
.avail_in
= hdrlen
;
223 while (deflate(&stream
, 0) == Z_OK
)
226 /* Then the data itself.. */
227 stream
.next_in
= unpacked
;
228 stream
.avail_in
= len
;
229 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
234 request
->buffer
.size
= stream
.total_out
;
235 request
->buffer
.posn
= 0;
237 request
->url
= xmalloc(strlen(remote
->url
) +
238 strlen(request
->lock
->token
) + 51);
239 strcpy(request
->url
, remote
->url
);
240 posn
= request
->url
+ strlen(remote
->url
);
241 strcpy(posn
, "objects/");
243 memcpy(posn
, hex
, 2);
246 strcpy(posn
, hex
+ 2);
247 request
->dest
= xmalloc(strlen(request
->url
) + 14);
248 sprintf(request
->dest
, "Destination: %s", request
->url
);
251 strcpy(posn
, request
->lock
->token
);
253 slot
= get_active_slot();
254 slot
->callback_func
= process_response
;
255 slot
->callback_data
= request
;
256 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
257 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.size
);
258 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
259 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
260 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
261 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
262 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
263 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
264 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
266 if (start_active_slot(slot
)) {
267 request
->slot
= slot
;
268 request
->state
= RUN_PUT
;
270 request
->state
= ABORTED
;
276 static void start_move(struct transfer_request
*request
)
278 struct active_request_slot
*slot
;
279 struct curl_slist
*dav_headers
= NULL
;
281 slot
= get_active_slot();
282 slot
->callback_func
= process_response
;
283 slot
->callback_data
= request
;
284 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
285 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
286 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
287 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
288 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
289 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
290 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
292 if (start_active_slot(slot
)) {
293 request
->slot
= slot
;
294 request
->state
= RUN_MOVE
;
296 request
->state
= ABORTED
;
302 static int refresh_lock(struct remote_lock
*check_lock
)
304 struct active_request_slot
*slot
;
306 char timeout_header
[25];
307 struct curl_slist
*dav_headers
= NULL
;
308 struct remote_lock
*lock
;
312 /* Refresh all active locks if they're close to expiring */
313 for (lock
= remote_locks
; lock
; lock
= lock
->next
) {
317 current_time
= time(NULL
);
318 time_remaining
= lock
->start_time
+ lock
->timeout
320 if (time_remaining
> LOCK_REFRESH
)
323 lock
->refreshing
= 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
);
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
);
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
);
345 lock
->start_time
= time(NULL
);
349 lock
->refreshing
= 0;
350 curl_slist_free_all(dav_headers
);
355 return check_lock
->active
;
360 static void release_request(struct transfer_request
*request
)
362 struct transfer_request
*entry
= request_queue_head
;
364 if (request
== request_queue_head
) {
365 request_queue_head
= request
->next
;
367 while (entry
->next
!= NULL
&& entry
->next
!= request
)
369 if (entry
->next
== request
)
370 entry
->next
= entry
->next
->next
;
373 if (request
->url
!= NULL
)
378 static void finish_request(struct transfer_request
*request
)
380 request
->curl_result
= request
->slot
->curl_result
;
381 request
->http_code
= request
->slot
->http_code
;
382 request
->slot
= NULL
;
384 /* Keep locks active */
385 refresh_lock(request
->lock
);
387 if (request
->headers
!= NULL
)
388 curl_slist_free_all(request
->headers
);
390 /* URL is reused for MOVE after PUT */
391 if (request
->state
!= RUN_PUT
) {
396 if (request
->state
== RUN_MKCOL
) {
397 if (request
->curl_result
== CURLE_OK
||
398 request
->http_code
== 405) {
399 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
402 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
403 sha1_to_hex(request
->obj
->sha1
),
404 request
->curl_result
, request
->http_code
);
405 request
->state
= ABORTED
;
408 } else if (request
->state
== RUN_PUT
) {
409 if (request
->curl_result
== CURLE_OK
) {
412 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
413 sha1_to_hex(request
->obj
->sha1
),
414 request
->curl_result
, request
->http_code
);
415 request
->state
= ABORTED
;
418 } else if (request
->state
== RUN_MOVE
) {
419 if (request
->curl_result
== CURLE_OK
) {
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
);
426 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
427 sha1_to_hex(request
->obj
->sha1
),
428 request
->curl_result
, request
->http_code
);
429 request
->state
= ABORTED
;
435 void fill_active_slots(void)
437 struct transfer_request
*request
= request_queue_head
;
438 struct active_request_slot
*slot
= active_queue_head
;
444 while (active_requests
< max_requests
&& request
!= NULL
) {
445 if (pushing
&& request
->state
== NEED_PUSH
) {
446 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
449 start_mkcol(request
);
451 curl_multi_perform(curlm
, &num_transfers
);
453 request
= request
->next
;
456 while (slot
!= NULL
) {
457 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
458 curl_easy_cleanup(slot
->curl
);
465 static void get_remote_object_list(unsigned char parent
);
467 static void add_request(struct object
*obj
, struct remote_lock
*lock
)
469 struct transfer_request
*request
= request_queue_head
;
470 struct packed_git
*target
;
473 * Don't push the object if it's known to exist on the remote
474 * or is already in the request queue
476 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
477 get_remote_object_list(obj
->sha1
[0]);
478 if (obj
->flags
& (REMOTE
| PUSHING
))
480 target
= find_sha1_pack(obj
->sha1
, remote
->packs
);
482 obj
->flags
|= REMOTE
;
486 obj
->flags
|= PUSHING
;
487 request
= xmalloc(sizeof(*request
));
490 request
->lock
= lock
;
491 request
->headers
= NULL
;
492 request
->state
= NEED_PUSH
;
493 request
->next
= request_queue_head
;
494 request_queue_head
= request
;
500 static int fetch_index(unsigned char *sha1
)
502 char *hex
= sha1_to_hex(sha1
);
505 char tmpfile
[PATH_MAX
];
507 char range
[RANGE_HEADER_SIZE
];
508 struct curl_slist
*range_header
= NULL
;
511 struct active_request_slot
*slot
;
513 /* Don't use the index if the pack isn't there */
514 url
= xmalloc(strlen(remote
->url
) + 65);
515 sprintf(url
, "%s/objects/pack/pack-%s.pack", remote
->url
, hex
);
516 slot
= get_active_slot();
517 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
518 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
519 if (start_active_slot(slot
)) {
520 run_active_slot(slot
);
521 if (slot
->curl_result
!= CURLE_OK
) {
523 return error("Unable to verify pack %s is available",
527 return error("Unable to start request");
530 if (has_pack_index(sha1
))
534 fprintf(stderr
, "Getting index for pack %s\n", hex
);
536 sprintf(url
, "%s/objects/pack/pack-%s.idx", remote
->url
, hex
);
538 filename
= sha1_pack_index_name(sha1
);
539 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
540 indexfile
= fopen(tmpfile
, "a");
542 return error("Unable to open local file %s for pack index",
545 slot
= get_active_slot();
546 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
547 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
548 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
549 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
550 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
551 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
552 slot
->local
= indexfile
;
554 /* If there is data present from a previous transfer attempt,
555 resume where it left off */
556 prev_posn
= ftell(indexfile
);
560 "Resuming fetch of index for pack %s at byte %ld\n",
562 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
563 range_header
= curl_slist_append(range_header
, range
);
564 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
567 if (start_active_slot(slot
)) {
568 run_active_slot(slot
);
569 if (slot
->curl_result
!= CURLE_OK
) {
572 return error("Unable to get pack index %s\n%s", url
,
578 return error("Unable to start request");
584 return move_temp_to_file(tmpfile
, filename
);
587 static int setup_index(unsigned char *sha1
)
589 struct packed_git
*new_pack
;
591 if (fetch_index(sha1
))
594 new_pack
= parse_pack_index(sha1
);
595 new_pack
->next
= remote
->packs
;
596 remote
->packs
= new_pack
;
600 static int fetch_indices(void)
602 unsigned char sha1
[20];
604 struct buffer buffer
;
608 struct active_request_slot
*slot
;
610 data
= xmalloc(4096);
611 memset(data
, 0, 4096);
614 buffer
.buffer
= data
;
617 fprintf(stderr
, "Getting pack list\n");
619 url
= xmalloc(strlen(remote
->url
) + 21);
620 sprintf(url
, "%s/objects/info/packs", remote
->url
);
622 slot
= get_active_slot();
623 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
624 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
625 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
626 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
627 if (start_active_slot(slot
)) {
628 run_active_slot(slot
);
629 if (slot
->curl_result
!= CURLE_OK
) {
632 if (slot
->http_code
== 404)
635 return error("%s", curl_errorstr
);
640 return error("Unable to start request");
644 data
= buffer
.buffer
;
645 while (i
< buffer
.posn
) {
649 if (i
+ 52 < buffer
.posn
&&
650 !strncmp(data
+ i
, " pack-", 6) &&
651 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
652 get_sha1_hex(data
+ i
+ 6, sha1
);
658 while (data
[i
] != '\n')
668 static inline int needs_quote(int ch
)
671 case '/': case '-': case '.':
672 case 'A'...'Z': case 'a'...'z': case '0'...'9':
679 static inline int hex(int v
)
681 if (v
< 10) return '0' + v
;
682 else return 'A' + v
- 10;
685 static char *quote_ref_url(const char *base
, const char *ref
)
689 int len
, baselen
, ch
;
691 baselen
= strlen(base
);
693 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
695 len
+= 2; /* extra two hex plus replacement % */
697 memcpy(qref
, base
, baselen
);
698 for (cp
= ref
, dp
= qref
+ baselen
; (ch
= *cp
) != 0; cp
++) {
699 if (needs_quote(ch
)) {
701 *dp
++ = hex((ch
>> 4) & 0xF);
702 *dp
++ = hex(ch
& 0xF);
712 int fetch_ref(char *ref
, unsigned char *sha1
)
716 struct buffer buffer
;
717 char *base
= remote
->url
;
718 struct active_request_slot
*slot
;
724 url
= quote_ref_url(base
, ref
);
725 slot
= get_active_slot();
726 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
727 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
728 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
729 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
730 if (start_active_slot(slot
)) {
731 run_active_slot(slot
);
732 if (slot
->curl_result
!= CURLE_OK
)
733 return error("Couldn't get %s for %s\n%s",
734 url
, ref
, curl_errorstr
);
736 return error("Unable to start request");
740 get_sha1_hex(hex
, sha1
);
744 static void one_remote_object(const char *hex
)
746 unsigned char sha1
[20];
749 if (get_sha1_hex(hex
, sha1
) != 0)
752 obj
= lookup_object(sha1
);
754 obj
= parse_object(sha1
);
756 /* Ignore remote objects that don't exist locally */
760 obj
->flags
|= REMOTE
;
761 if (!object_list_contains(objects
, obj
))
762 add_object(obj
, &objects
, NULL
, "");
765 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
767 int *lock_flags
= (int *)ctx
->userData
;
770 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
771 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
772 (*lock_flags
& DAV_PROP_LOCKWR
)) {
773 *lock_flags
|= DAV_LOCK_OK
;
775 *lock_flags
&= DAV_LOCK_OK
;
776 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
777 *lock_flags
|= DAV_PROP_LOCKWR
;
778 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
779 *lock_flags
|= DAV_PROP_LOCKEX
;
784 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
786 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
788 if (tag_closed
&& ctx
->cdata
) {
789 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
790 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
791 strcpy(lock
->owner
, ctx
->cdata
);
792 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
793 if (!strncmp(ctx
->cdata
, "Second-", 7))
795 strtol(ctx
->cdata
+ 7, NULL
, 10);
796 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
797 if (!strncmp(ctx
->cdata
, "opaquelocktoken:", 16)) {
798 lock
->token
= xmalloc(strlen(ctx
->cdata
) - 15);
799 strcpy(lock
->token
, ctx
->cdata
+ 16);
805 static void one_remote_ref(char *refname
);
806 static void crawl_remote_refs(char *path
);
808 static void handle_crawl_ref_ctx(struct xml_ctx
*ctx
, int tag_closed
)
810 struct remote_dentry
*dentry
= (struct remote_dentry
*)ctx
->userData
;
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
);
820 one_remote_ref(dentry
->name
);
822 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
823 dentry
->name
= xmalloc(strlen(ctx
->cdata
) -
824 remote
->path_len
+ 1);
826 ctx
->cdata
+ remote
->path_len
);
827 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
830 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
836 static void handle_remote_object_list_ctx(struct xml_ctx
*ctx
, int tag_closed
)
842 if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
843 path
= ctx
->cdata
+ remote
->path_len
;
844 if (strlen(path
) != 50)
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
);
857 xml_start_tag(void *userData
, const char *name
, const char **atts
)
859 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
860 const char *c
= index(name
, ':');
868 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
870 if (new_len
> ctx
->len
) {
871 ctx
->name
= xrealloc(ctx
->name
, new_len
);
874 strcat(ctx
->name
, ".");
875 strcat(ctx
->name
, c
);
882 ctx
->userFunc(ctx
, 0);
886 xml_end_tag(void *userData
, const char *name
)
888 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
889 const char *c
= index(name
, ':');
892 ctx
->userFunc(ctx
, 1);
899 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
904 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
906 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
909 ctx
->cdata
= xcalloc(len
+1, 1);
910 strncpy(ctx
->cdata
, s
, len
);
913 static struct remote_lock
*lock_remote(char *path
, long timeout
)
915 struct active_request_slot
*slot
;
916 struct buffer out_buffer
;
917 struct buffer in_buffer
;
922 char timeout_header
[25];
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
;
929 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
930 sprintf(url
, "%s%s", remote
->url
, path
);
932 /* Make sure the url is not already locked */
933 while (lock
&& strcmp(lock
->url
, url
)) {
938 if (refresh_lock(lock
))
944 /* Make sure leading directories exist for the remote ref */
945 ep
= strchr(url
+ strlen(remote
->url
) + 11, '/');
948 slot
= get_active_slot();
949 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
950 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
951 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
952 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
953 if (start_active_slot(slot
)) {
954 run_active_slot(slot
);
955 if (slot
->curl_result
!= CURLE_OK
&&
956 slot
->http_code
!= 405) {
958 "Unable to create branch path %s\n",
964 fprintf(stderr
, "Unable to start request\n");
969 ep
= strchr(ep
+ 1, '/');
972 out_buffer
.size
= strlen(LOCK_REQUEST
) + strlen(git_default_email
) - 2;
973 out_data
= xmalloc(out_buffer
.size
+ 1);
974 snprintf(out_data
, out_buffer
.size
+ 1, LOCK_REQUEST
, git_default_email
);
976 out_buffer
.buffer
= out_data
;
978 in_buffer
.size
= 4096;
979 in_data
= xmalloc(in_buffer
.size
);
981 in_buffer
.buffer
= in_data
;
983 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
984 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
985 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
987 slot
= get_active_slot();
988 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
989 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
990 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
991 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
992 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
993 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
994 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
995 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
996 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
998 lock
= xcalloc(1, sizeof(*lock
));
1002 lock
->refreshing
= 0;
1004 if (start_active_slot(slot
)) {
1005 run_active_slot(slot
);
1006 if (slot
->curl_result
== CURLE_OK
) {
1007 ctx
.name
= xcalloc(10, 1);
1010 ctx
.userFunc
= handle_new_lock_ctx
;
1011 ctx
.userData
= lock
;
1012 XML_SetUserData(parser
, &ctx
);
1013 XML_SetElementHandler(parser
, xml_start_tag
,
1015 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1016 result
= XML_Parse(parser
, in_buffer
.buffer
,
1019 if (result
!= XML_STATUS_OK
) {
1020 fprintf(stderr
, "XML error: %s\n",
1022 XML_GetErrorCode(parser
)));
1027 fprintf(stderr
, "Unable to start request\n");
1030 curl_slist_free_all(dav_headers
);
1034 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1035 if (lock
->token
!= NULL
)
1037 if (lock
->owner
!= NULL
)
1045 lock
->start_time
= time(NULL
);
1046 lock
->next
= remote_locks
;
1047 remote_locks
= lock
;
1053 static int unlock_remote(struct remote_lock
*lock
)
1055 struct active_request_slot
*slot
;
1056 char *lock_token_header
;
1057 struct curl_slist
*dav_headers
= NULL
;
1060 lock_token_header
= xmalloc(strlen(lock
->token
) + 31);
1061 sprintf(lock_token_header
, "Lock-Token: <opaquelocktoken:%s>",
1063 dav_headers
= curl_slist_append(dav_headers
, lock_token_header
);
1065 slot
= get_active_slot();
1066 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1067 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1068 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1069 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1071 if (start_active_slot(slot
)) {
1072 run_active_slot(slot
);
1073 if (slot
->curl_result
== CURLE_OK
)
1076 fprintf(stderr
, "Got HTTP error %ld\n",
1079 fprintf(stderr
, "Unable to start request\n");
1082 curl_slist_free_all(dav_headers
);
1083 free(lock_token_header
);
1090 static void crawl_remote_refs(char *path
)
1093 struct active_request_slot
*slot
;
1094 struct buffer in_buffer
;
1095 struct buffer out_buffer
;
1098 XML_Parser parser
= XML_ParserCreate(NULL
);
1099 enum XML_Status result
;
1100 struct curl_slist
*dav_headers
= NULL
;
1102 struct remote_dentry dentry
;
1104 fprintf(stderr
, " %s\n", path
);
1110 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1111 sprintf(url
, "%s%s", remote
->url
, path
);
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
;
1119 in_buffer
.size
= 4096;
1120 in_data
= xmalloc(in_buffer
.size
);
1122 in_buffer
.buffer
= in_data
;
1124 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1125 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
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
);
1138 if (start_active_slot(slot
)) {
1139 run_active_slot(slot
);
1140 if (slot
->curl_result
== CURLE_OK
) {
1141 ctx
.name
= xcalloc(10, 1);
1144 ctx
.userFunc
= handle_crawl_ref_ctx
;
1145 ctx
.userData
= &dentry
;
1146 XML_SetUserData(parser
, &ctx
);
1147 XML_SetElementHandler(parser
, xml_start_tag
,
1149 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1150 result
= XML_Parse(parser
, in_buffer
.buffer
,
1154 if (result
!= XML_STATUS_OK
) {
1155 fprintf(stderr
, "XML error: %s\n",
1157 XML_GetErrorCode(parser
)));
1161 fprintf(stderr
, "Unable to start request\n");
1166 free(in_buffer
.buffer
);
1167 curl_slist_free_all(dav_headers
);
1170 static void get_remote_object_list(unsigned char parent
)
1173 struct active_request_slot
*slot
;
1174 struct buffer in_buffer
;
1175 struct buffer out_buffer
;
1178 XML_Parser parser
= XML_ParserCreate(NULL
);
1179 enum XML_Status result
;
1180 struct curl_slist
*dav_headers
= NULL
;
1182 char path
[] = "/objects/XX/";
1183 static const char hex
[] = "0123456789abcdef";
1184 unsigned int val
= parent
;
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
);
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
;
1197 in_buffer
.size
= 4096;
1198 in_data
= xmalloc(in_buffer
.size
);
1200 in_buffer
.buffer
= in_data
;
1202 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1203 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
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
);
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);
1223 ctx
.userFunc
= handle_remote_object_list_ctx
;
1224 XML_SetUserData(parser
, &ctx
);
1225 XML_SetElementHandler(parser
, xml_start_tag
,
1227 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1228 result
= XML_Parse(parser
, in_buffer
.buffer
,
1232 if (result
!= XML_STATUS_OK
) {
1233 fprintf(stderr
, "XML error: %s\n",
1235 XML_GetErrorCode(parser
)));
1238 remote_dir_exists
[parent
] = 0;
1241 fprintf(stderr
, "Unable to start request\n");
1246 free(in_buffer
.buffer
);
1247 curl_slist_free_all(dav_headers
);
1250 static int locking_available(void)
1252 struct active_request_slot
*slot
;
1253 struct buffer in_buffer
;
1254 struct buffer out_buffer
;
1257 XML_Parser parser
= XML_ParserCreate(NULL
);
1258 enum XML_Status result
;
1259 struct curl_slist
*dav_headers
= NULL
;
1264 strlen(PROPFIND_SUPPORTEDLOCK_REQUEST
) +
1265 strlen(remote
->url
) - 2;
1266 out_data
= xmalloc(out_buffer
.size
+ 1);
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
;
1272 in_buffer
.size
= 4096;
1273 in_data
= xmalloc(in_buffer
.size
);
1275 in_buffer
.buffer
= in_data
;
1277 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1278 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1280 slot
= get_active_slot();
1281 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1282 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1283 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1284 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1285 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1286 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1287 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1288 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1289 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1291 if (start_active_slot(slot
)) {
1292 run_active_slot(slot
);
1293 if (slot
->curl_result
== CURLE_OK
) {
1294 ctx
.name
= xcalloc(10, 1);
1297 ctx
.userFunc
= handle_lockprop_ctx
;
1298 ctx
.userData
= &lock_flags
;
1299 XML_SetUserData(parser
, &ctx
);
1300 XML_SetElementHandler(parser
, xml_start_tag
,
1302 result
= XML_Parse(parser
, in_buffer
.buffer
,
1306 if (result
!= XML_STATUS_OK
) {
1307 fprintf(stderr
, "XML error: %s\n",
1309 XML_GetErrorCode(parser
)));
1314 fprintf(stderr
, "Unable to start request\n");
1318 free(in_buffer
.buffer
);
1319 curl_slist_free_all(dav_headers
);
1324 static struct object_list
**process_blob(struct blob
*blob
,
1325 struct object_list
**p
,
1326 struct name_path
*path
,
1329 struct object
*obj
= &blob
->object
;
1331 obj
->flags
|= LOCAL
;
1333 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1337 return add_object(obj
, p
, path
, name
);
1340 static struct object_list
**process_tree(struct tree
*tree
,
1341 struct object_list
**p
,
1342 struct name_path
*path
,
1345 struct object
*obj
= &tree
->object
;
1346 struct tree_entry_list
*entry
;
1347 struct name_path me
;
1349 obj
->flags
|= LOCAL
;
1351 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1353 if (parse_tree(tree
) < 0)
1354 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1357 p
= add_object(obj
, p
, NULL
, name
);
1360 me
.elem_len
= strlen(name
);
1361 entry
= tree
->entries
;
1362 tree
->entries
= NULL
;
1364 struct tree_entry_list
*next
= entry
->next
;
1365 if (entry
->directory
)
1366 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
1368 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
1375 static void get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1377 struct commit
*commit
;
1378 struct object_list
**p
= &objects
, *pending
;
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
);
1387 for (pending
= revs
->pending_objects
; pending
; pending
= pending
->next
) {
1388 struct object
*obj
= pending
->item
;
1389 const char *name
= pending
->name
;
1391 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1393 if (obj
->type
== tag_type
) {
1395 p
= add_object(obj
, p
, NULL
, name
);
1398 if (obj
->type
== tree_type
) {
1399 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1402 if (obj
->type
== blob_type
) {
1403 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1406 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1410 if (!(objects
->item
->flags
& UNINTERESTING
))
1411 add_request(objects
->item
, lock
);
1412 objects
= objects
->next
;
1416 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1418 struct active_request_slot
*slot
;
1421 struct buffer out_buffer
;
1422 struct curl_slist
*dav_headers
= NULL
;
1425 if_header
= xmalloc(strlen(lock
->token
) + 25);
1426 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
1427 dav_headers
= curl_slist_append(dav_headers
, if_header
);
1429 out_buffer
.size
= 41;
1430 out_data
= xmalloc(out_buffer
.size
+ 1);
1431 i
= snprintf(out_data
, out_buffer
.size
+ 1, "%s\n", sha1_to_hex(sha1
));
1432 if (i
!= out_buffer
.size
) {
1433 fprintf(stderr
, "Unable to initialize PUT request body\n");
1436 out_buffer
.posn
= 0;
1437 out_buffer
.buffer
= out_data
;
1439 slot
= get_active_slot();
1440 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1441 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1442 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1443 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1444 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1445 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1446 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1447 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1448 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1450 if (start_active_slot(slot
)) {
1451 run_active_slot(slot
);
1454 if (slot
->curl_result
!= CURLE_OK
) {
1456 "PUT error: curl result=%d, HTTP code=%ld\n",
1457 slot
->curl_result
, slot
->http_code
);
1458 /* We should attempt recovery? */
1464 fprintf(stderr
, "Unable to start PUT request\n");
1471 static struct ref
*local_refs
, **local_tail
;
1472 static struct ref
*remote_refs
, **remote_tail
;
1474 static int one_local_ref(const char *refname
, const unsigned char *sha1
)
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
);
1482 local_tail
= &ref
->next
;
1486 static void one_remote_ref(char *refname
)
1489 unsigned char remote_sha1
[20];
1491 if (fetch_ref(refname
, remote_sha1
) != 0) {
1493 "Unable to fetch ref %s from %s\n",
1494 refname
, remote
->url
);
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
);
1503 remote_tail
= &ref
->next
;
1506 static void get_local_heads(void)
1508 local_tail
= &local_refs
;
1509 for_each_ref(one_local_ref
);
1512 static void get_dav_remote_heads(void)
1514 remote_tail
= &remote_refs
;
1515 crawl_remote_refs("refs/");
1518 static int is_zero_sha1(const unsigned char *sha1
)
1522 for (i
= 0; i
< 20; i
++) {
1529 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1532 struct commit_list
*temp
= list
;
1533 temp
->item
->object
.flags
&= ~mark
;
1539 static int ref_newer(const unsigned char *new_sha1
,
1540 const unsigned char *old_sha1
)
1543 struct commit
*old
, *new;
1544 struct commit_list
*list
, *used
;
1547 /* Both new and old must be commit-ish and new is descendant of
1548 * old. Otherwise we require --force.
1550 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1551 if (!o
|| o
->type
!= commit_type
)
1553 old
= (struct commit
*) o
;
1555 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1556 if (!o
|| o
->type
!= commit_type
)
1558 new = (struct commit
*) o
;
1560 if (parse_commit(new) < 0)
1564 commit_list_insert(new, &list
);
1566 new = pop_most_recent_commit(&list
, TMP_MARK
);
1567 commit_list_insert(new, &used
);
1573 unmark_and_free(list
, TMP_MARK
);
1574 unmark_and_free(used
, TMP_MARK
);
1578 static void mark_edge_parents_uninteresting(struct commit
*commit
)
1580 struct commit_list
*parents
;
1582 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1583 struct commit
*parent
= parents
->item
;
1584 if (!(parent
->object
.flags
& UNINTERESTING
))
1586 mark_tree_uninteresting(parent
->tree
);
1590 static void mark_edges_uninteresting(struct commit_list
*list
)
1592 for ( ; list
; list
= list
->next
) {
1593 struct commit
*commit
= list
->item
;
1595 if (commit
->object
.flags
& UNINTERESTING
) {
1596 mark_tree_uninteresting(commit
->tree
);
1599 mark_edge_parents_uninteresting(commit
);
1603 int main(int argc
, char **argv
)
1605 struct transfer_request
*request
;
1606 struct transfer_request
*next_request
;
1608 char **refspec
= NULL
;
1609 struct remote_lock
*ref_lock
;
1610 struct rev_info revs
;
1614 setup_git_directory();
1617 remote
= xmalloc(sizeof(*remote
));
1619 remote
->path_len
= 0;
1620 remote
->packs
= NULL
;
1623 for (i
= 1; i
< argc
; i
++, argv
++) {
1627 if (!strcmp(arg
, "--all")) {
1631 if (!strcmp(arg
, "--force")) {
1635 if (!strcmp(arg
, "--verbose")) {
1639 usage(http_push_usage
);
1643 char *path
= strstr(arg
, "//");
1645 path
= index(path
+2, '/');
1647 remote
->path_len
= strlen(path
);
1652 nr_refspec
= argc
- i
;
1657 usage(http_push_usage
);
1659 memset(remote_dir_exists
, -1, 256);
1663 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1664 default_headers
= curl_slist_append(default_headers
, "Range:");
1665 default_headers
= curl_slist_append(default_headers
, "Destination:");
1666 default_headers
= curl_slist_append(default_headers
, "If:");
1667 default_headers
= curl_slist_append(default_headers
,
1668 "Pragma: no-cache");
1670 /* Verify DAV compliance/lock support */
1671 if (!locking_available()) {
1672 fprintf(stderr
, "Error: no DAV locking support on remote repo %s\n", remote
->url
);
1677 /* Get a list of all local and remote heads to validate refspecs */
1679 fprintf(stderr
, "Fetching remote heads...\n");
1680 get_dav_remote_heads();
1684 remote_tail
= &remote_refs
;
1685 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
1686 nr_refspec
, refspec
, push_all
))
1689 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1696 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1697 char old_hex
[60], *new_hex
;
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
);
1707 !is_zero_sha1(ref
->old_sha1
) &&
1709 if (!has_sha1_file(ref
->old_sha1
) ||
1710 !ref_newer(ref
->peer_ref
->new_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.
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?",
1724 ref
->peer_ref
->name
);
1729 memcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
, 20);
1730 if (is_zero_sha1(ref
->new_sha1
)) {
1731 error("cannot happen anymore");
1736 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1737 new_hex
= sha1_to_hex(ref
->new_sha1
);
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
);
1745 /* Lock remote branch ref */
1746 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1747 if (ref_lock
== NULL
) {
1748 fprintf(stderr
, "Unable to lock remote branch %s\n",
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
;
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;
1775 commit_argv
[1] = NULL
;
1778 /* Generate a list of objects that need to be pushed */
1780 prepare_revision_walk(&revs
);
1781 mark_edges_uninteresting(revs
.commits
);
1783 get_delta(&revs
, ref_lock
);
1784 finish_all_active_slots();
1786 /* Push missing objects to remote, this would be a
1787 convenient time to pack them first if appropriate. */
1789 fill_active_slots();
1790 finish_all_active_slots();
1792 /* Update the remote branch if all went well */
1793 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
)) {
1800 fprintf(stderr
, " done\n");
1801 unlock_remote(ref_lock
);
1807 curl_slist_free_all(no_pragma_header
);
1808 curl_slist_free_all(default_headers
);
1812 request
= request_queue_head
;
1813 while (request
!= NULL
) {
1814 next_request
= request
->next
;
1815 release_request(request
);
1816 request
= next_request
;