11 #include "list-objects.h"
14 #ifdef EXPAT_NEEDS_XMLPARSE_H
20 static const char http_push_usage
[] =
21 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
28 #define XML_STATUS_OK 1
29 #define XML_STATUS_ERROR 0
32 #define PREV_BUF_SIZE 4096
35 #define DAV_LOCK "LOCK"
36 #define DAV_MKCOL "MKCOL"
37 #define DAV_MOVE "MOVE"
38 #define DAV_PROPFIND "PROPFIND"
40 #define DAV_UNLOCK "UNLOCK"
41 #define DAV_DELETE "DELETE"
44 #define DAV_PROP_LOCKWR (1u << 0)
45 #define DAV_PROP_LOCKEX (1u << 1)
46 #define DAV_LOCK_OK (1u << 2)
48 /* DAV XML properties */
49 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
50 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
51 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
52 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
53 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
54 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
55 #define DAV_PROPFIND_RESP ".multistatus.response"
56 #define DAV_PROPFIND_NAME ".multistatus.response.href"
57 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
59 /* DAV request body templates */
60 #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>"
61 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
62 #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>"
65 #define LOCK_REFRESH 30
67 /* Remember to update object flag allocation in object.h */
68 #define LOCAL (1u<<16)
69 #define REMOTE (1u<<17)
70 #define FETCHING (1u<<18)
71 #define PUSHING (1u<<19)
73 /* We allow "recursive" symbolic refs. Only within reason, though */
78 static signed char remote_dir_exists
[256];
80 static int push_verbosely
;
81 static int push_all
= MATCH_REFS_NONE
;
84 static int helper_status
;
86 static struct object_list
*objects
;
93 int can_update_info_refs
;
95 struct packed_git
*packs
;
96 struct remote_lock
*locks
;
99 static struct repo
*repo
;
101 enum transfer_state
{
113 struct transfer_request
{
117 struct remote_lock
*lock
;
118 struct curl_slist
*headers
;
119 struct buffer buffer
;
120 enum transfer_state state
;
121 CURLcode curl_result
;
122 char errorstr
[CURL_ERROR_SIZE
];
125 struct active_request_slot
*slot
;
126 struct transfer_request
*next
;
129 static struct transfer_request
*request_queue_head
;
135 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
143 char tmpfile_suffix
[41];
147 struct remote_lock
*next
;
150 /* Flags that control remote_ls processing */
151 #define PROCESS_FILES (1u << 0)
152 #define PROCESS_DIRS (1u << 1)
153 #define RECURSIVE (1u << 2)
155 /* Flags that remote_ls passes to callback functions */
156 #define IS_DIR (1u << 0)
158 struct remote_ls_ctx
{
160 void (*userFunc
)(struct remote_ls_ctx
*ls
);
165 struct remote_ls_ctx
*parent
;
168 /* get_dav_token_headers options */
169 enum dav_header_flag
{
170 DAV_HEADER_IF
= (1u << 0),
171 DAV_HEADER_LOCK
= (1u << 1),
172 DAV_HEADER_TIMEOUT
= (1u << 2)
175 static char *xml_entities(const char *s
)
177 struct strbuf buf
= STRBUF_INIT
;
178 strbuf_addstr_xml_quoted(&buf
, s
);
179 return strbuf_detach(&buf
, NULL
);
182 static void curl_setup_http_get(CURL
*curl
, const char *url
,
183 const char *custom_req
)
185 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
186 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
187 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
188 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
191 static void curl_setup_http(CURL
*curl
, const char *url
,
192 const char *custom_req
, struct buffer
*buffer
,
193 curl_write_callback write_fn
)
195 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
196 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
197 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
198 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
199 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
200 #ifndef NO_CURL_IOCTL
201 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
202 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, buffer
);
204 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
205 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
206 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
207 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
210 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
212 struct strbuf buf
= STRBUF_INIT
;
213 struct curl_slist
*dav_headers
= NULL
;
215 if (options
& DAV_HEADER_IF
) {
216 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
217 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
220 if (options
& DAV_HEADER_LOCK
) {
221 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
222 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
225 if (options
& DAV_HEADER_TIMEOUT
) {
226 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
227 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
230 strbuf_release(&buf
);
235 static void finish_request(struct transfer_request
*request
);
236 static void release_request(struct transfer_request
*request
);
238 static void process_response(void *callback_data
)
240 struct transfer_request
*request
=
241 (struct transfer_request
*)callback_data
;
243 finish_request(request
);
246 #ifdef USE_CURL_MULTI
248 static void start_fetch_loose(struct transfer_request
*request
)
250 struct active_request_slot
*slot
;
251 struct http_object_request
*obj_req
;
253 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
254 if (obj_req
== NULL
) {
255 request
->state
= ABORTED
;
259 slot
= obj_req
->slot
;
260 slot
->callback_func
= process_response
;
261 slot
->callback_data
= request
;
262 request
->slot
= slot
;
263 request
->userData
= obj_req
;
265 /* Try to get the request started, abort the request on error */
266 request
->state
= RUN_FETCH_LOOSE
;
267 if (!start_active_slot(slot
)) {
268 fprintf(stderr
, "Unable to start GET request\n");
269 repo
->can_update_info_refs
= 0;
270 release_http_object_request(obj_req
);
271 release_request(request
);
275 static void start_mkcol(struct transfer_request
*request
)
277 char *hex
= sha1_to_hex(request
->obj
->sha1
);
278 struct active_request_slot
*slot
;
280 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
282 slot
= get_active_slot();
283 slot
->callback_func
= process_response
;
284 slot
->callback_data
= request
;
285 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
286 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
288 if (start_active_slot(slot
)) {
289 request
->slot
= slot
;
290 request
->state
= RUN_MKCOL
;
292 request
->state
= ABORTED
;
299 static void start_fetch_packed(struct transfer_request
*request
)
301 struct packed_git
*target
;
303 struct transfer_request
*check_request
= request_queue_head
;
304 struct http_pack_request
*preq
;
306 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
308 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
309 repo
->can_update_info_refs
= 0;
310 release_request(request
);
314 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
315 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
317 preq
= new_http_pack_request(target
, repo
->url
);
319 repo
->can_update_info_refs
= 0;
322 preq
->lst
= &repo
->packs
;
324 /* Make sure there isn't another open request for this pack */
325 while (check_request
) {
326 if (check_request
->state
== RUN_FETCH_PACKED
&&
327 !strcmp(check_request
->url
, preq
->url
)) {
328 release_http_pack_request(preq
);
329 release_request(request
);
332 check_request
= check_request
->next
;
335 preq
->slot
->callback_func
= process_response
;
336 preq
->slot
->callback_data
= request
;
337 request
->slot
= preq
->slot
;
338 request
->userData
= preq
;
340 /* Try to get the request started, abort the request on error */
341 request
->state
= RUN_FETCH_PACKED
;
342 if (!start_active_slot(preq
->slot
)) {
343 fprintf(stderr
, "Unable to start GET request\n");
344 release_http_pack_request(preq
);
345 repo
->can_update_info_refs
= 0;
346 release_request(request
);
350 static void start_put(struct transfer_request
*request
)
352 char *hex
= sha1_to_hex(request
->obj
->sha1
);
353 struct active_request_slot
*slot
;
354 struct strbuf buf
= STRBUF_INIT
;
355 enum object_type type
;
363 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
364 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
367 memset(&stream
, 0, sizeof(stream
));
368 git_deflate_init(&stream
, zlib_compression_level
);
369 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
370 strbuf_init(&request
->buffer
.buf
, size
);
371 request
->buffer
.posn
= 0;
374 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
375 stream
.avail_out
= size
;
378 stream
.next_in
= (void *)hdr
;
379 stream
.avail_in
= hdrlen
;
380 while (git_deflate(&stream
, 0) == Z_OK
)
383 /* Then the data itself.. */
384 stream
.next_in
= unpacked
;
385 stream
.avail_in
= len
;
386 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
388 git_deflate_end(&stream
);
391 request
->buffer
.buf
.len
= stream
.total_out
;
393 strbuf_addstr(&buf
, "Destination: ");
394 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
395 request
->dest
= strbuf_detach(&buf
, NULL
);
397 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
398 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
399 request
->url
= strbuf_detach(&buf
, NULL
);
401 slot
= get_active_slot();
402 slot
->callback_func
= process_response
;
403 slot
->callback_data
= request
;
404 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
405 &request
->buffer
, fwrite_null
);
407 if (start_active_slot(slot
)) {
408 request
->slot
= slot
;
409 request
->state
= RUN_PUT
;
411 request
->state
= ABORTED
;
417 static void start_move(struct transfer_request
*request
)
419 struct active_request_slot
*slot
;
420 struct curl_slist
*dav_headers
= NULL
;
422 slot
= get_active_slot();
423 slot
->callback_func
= process_response
;
424 slot
->callback_data
= request
;
425 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
426 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
427 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
428 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
430 if (start_active_slot(slot
)) {
431 request
->slot
= slot
;
432 request
->state
= RUN_MOVE
;
434 request
->state
= ABORTED
;
440 static int refresh_lock(struct remote_lock
*lock
)
442 struct active_request_slot
*slot
;
443 struct slot_results results
;
444 struct curl_slist
*dav_headers
;
447 lock
->refreshing
= 1;
449 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
451 slot
= get_active_slot();
452 slot
->results
= &results
;
453 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
454 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
456 if (start_active_slot(slot
)) {
457 run_active_slot(slot
);
458 if (results
.curl_result
!= CURLE_OK
) {
459 fprintf(stderr
, "LOCK HTTP error %ld\n",
462 lock
->start_time
= time(NULL
);
467 lock
->refreshing
= 0;
468 curl_slist_free_all(dav_headers
);
473 static void check_locks(void)
475 struct remote_lock
*lock
= repo
->locks
;
476 time_t current_time
= time(NULL
);
480 time_remaining
= lock
->start_time
+ lock
->timeout
-
482 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
483 if (!refresh_lock(lock
)) {
485 "Unable to refresh lock for %s\n",
495 static void release_request(struct transfer_request
*request
)
497 struct transfer_request
*entry
= request_queue_head
;
499 if (request
== request_queue_head
) {
500 request_queue_head
= request
->next
;
502 while (entry
->next
!= NULL
&& entry
->next
!= request
)
504 if (entry
->next
== request
)
505 entry
->next
= entry
->next
->next
;
512 static void finish_request(struct transfer_request
*request
)
514 struct http_pack_request
*preq
;
515 struct http_object_request
*obj_req
;
517 request
->curl_result
= request
->slot
->curl_result
;
518 request
->http_code
= request
->slot
->http_code
;
519 request
->slot
= NULL
;
521 /* Keep locks active */
524 if (request
->headers
!= NULL
)
525 curl_slist_free_all(request
->headers
);
527 /* URL is reused for MOVE after PUT */
528 if (request
->state
!= RUN_PUT
) {
533 if (request
->state
== RUN_MKCOL
) {
534 if (request
->curl_result
== CURLE_OK
||
535 request
->http_code
== 405) {
536 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
539 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
540 sha1_to_hex(request
->obj
->sha1
),
541 request
->curl_result
, request
->http_code
);
542 request
->state
= ABORTED
;
545 } else if (request
->state
== RUN_PUT
) {
546 if (request
->curl_result
== CURLE_OK
) {
549 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
550 sha1_to_hex(request
->obj
->sha1
),
551 request
->curl_result
, request
->http_code
);
552 request
->state
= ABORTED
;
555 } else if (request
->state
== RUN_MOVE
) {
556 if (request
->curl_result
== CURLE_OK
) {
558 fprintf(stderr
, " sent %s\n",
559 sha1_to_hex(request
->obj
->sha1
));
560 request
->obj
->flags
|= REMOTE
;
561 release_request(request
);
563 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
564 sha1_to_hex(request
->obj
->sha1
),
565 request
->curl_result
, request
->http_code
);
566 request
->state
= ABORTED
;
569 } else if (request
->state
== RUN_FETCH_LOOSE
) {
570 obj_req
= (struct http_object_request
*)request
->userData
;
572 if (finish_http_object_request(obj_req
) == 0)
573 if (obj_req
->rename
== 0)
574 request
->obj
->flags
|= (LOCAL
| REMOTE
);
576 /* Try fetching packed if necessary */
577 if (request
->obj
->flags
& LOCAL
) {
578 release_http_object_request(obj_req
);
579 release_request(request
);
581 start_fetch_packed(request
);
583 } else if (request
->state
== RUN_FETCH_PACKED
) {
585 if (request
->curl_result
!= CURLE_OK
) {
586 fprintf(stderr
, "Unable to get pack file %s\n%s",
587 request
->url
, curl_errorstr
);
589 preq
= (struct http_pack_request
*)request
->userData
;
592 if (finish_http_pack_request(preq
) == 0)
594 release_http_pack_request(preq
);
598 repo
->can_update_info_refs
= 0;
599 release_request(request
);
603 #ifdef USE_CURL_MULTI
604 static int is_running_queue
;
605 static int fill_active_slot(void *unused
)
607 struct transfer_request
*request
;
609 if (aborted
|| !is_running_queue
)
612 for (request
= request_queue_head
; request
; request
= request
->next
) {
613 if (request
->state
== NEED_FETCH
) {
614 start_fetch_loose(request
);
616 } else if (pushing
&& request
->state
== NEED_PUSH
) {
617 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
620 start_mkcol(request
);
629 static void get_remote_object_list(unsigned char parent
);
631 static void add_fetch_request(struct object
*obj
)
633 struct transfer_request
*request
;
638 * Don't fetch the object if it's known to exist locally
639 * or is already in the request queue
641 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
642 get_remote_object_list(obj
->sha1
[0]);
643 if (obj
->flags
& (LOCAL
| FETCHING
))
646 obj
->flags
|= FETCHING
;
647 request
= xmalloc(sizeof(*request
));
650 request
->lock
= NULL
;
651 request
->headers
= NULL
;
652 request
->state
= NEED_FETCH
;
653 request
->next
= request_queue_head
;
654 request_queue_head
= request
;
656 #ifdef USE_CURL_MULTI
662 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
664 struct transfer_request
*request
;
665 struct packed_git
*target
;
667 /* Keep locks active */
671 * Don't push the object if it's known to exist on the remote
672 * or is already in the request queue
674 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
675 get_remote_object_list(obj
->sha1
[0]);
676 if (obj
->flags
& (REMOTE
| PUSHING
))
678 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
680 obj
->flags
|= REMOTE
;
684 obj
->flags
|= PUSHING
;
685 request
= xmalloc(sizeof(*request
));
688 request
->lock
= lock
;
689 request
->headers
= NULL
;
690 request
->state
= NEED_PUSH
;
691 request
->next
= request_queue_head
;
692 request_queue_head
= request
;
694 #ifdef USE_CURL_MULTI
702 static int fetch_indices(void)
707 fprintf(stderr
, "Getting pack list\n");
709 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
711 case HTTP_MISSING_TARGET
:
721 static void one_remote_object(const unsigned char *sha1
)
725 obj
= lookup_object(sha1
);
727 obj
= parse_object(sha1
);
729 /* Ignore remote objects that don't exist locally */
733 obj
->flags
|= REMOTE
;
734 if (!object_list_contains(objects
, obj
))
735 object_list_insert(obj
, &objects
);
738 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
740 int *lock_flags
= (int *)ctx
->userData
;
743 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
744 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
745 (*lock_flags
& DAV_PROP_LOCKWR
)) {
746 *lock_flags
|= DAV_LOCK_OK
;
748 *lock_flags
&= DAV_LOCK_OK
;
749 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
750 *lock_flags
|= DAV_PROP_LOCKWR
;
751 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
752 *lock_flags
|= DAV_PROP_LOCKEX
;
757 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
759 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
761 unsigned char lock_token_sha1
[20];
763 if (tag_closed
&& ctx
->cdata
) {
764 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
765 lock
->owner
= xstrdup(ctx
->cdata
);
766 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
768 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
769 lock
->timeout
= strtol(arg
, NULL
, 10);
770 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
771 lock
->token
= xstrdup(ctx
->cdata
);
773 git_SHA1_Init(&sha_ctx
);
774 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
775 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
777 lock
->tmpfile_suffix
[0] = '_';
778 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
783 static void one_remote_ref(const char *refname
);
786 xml_start_tag(void *userData
, const char *name
, const char **atts
)
788 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
789 const char *c
= strchr(name
, ':');
797 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
799 if (new_len
> ctx
->len
) {
800 ctx
->name
= xrealloc(ctx
->name
, new_len
);
803 strcat(ctx
->name
, ".");
804 strcat(ctx
->name
, c
);
809 ctx
->userFunc(ctx
, 0);
813 xml_end_tag(void *userData
, const char *name
)
815 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
816 const char *c
= strchr(name
, ':');
819 ctx
->userFunc(ctx
, 1);
826 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
831 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
833 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
835 ctx
->cdata
= xmemdupz(s
, len
);
838 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
840 struct active_request_slot
*slot
;
841 struct slot_results results
;
842 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
843 struct strbuf in_buffer
= STRBUF_INIT
;
846 char timeout_header
[25];
847 struct remote_lock
*lock
= NULL
;
848 struct curl_slist
*dav_headers
= NULL
;
852 url
= xstrfmt("%s%s", repo
->url
, path
);
854 /* Make sure leading directories exist for the remote ref */
855 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
857 char saved_character
= ep
[1];
859 slot
= get_active_slot();
860 slot
->results
= &results
;
861 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
862 if (start_active_slot(slot
)) {
863 run_active_slot(slot
);
864 if (results
.curl_result
!= CURLE_OK
&&
865 results
.http_code
!= 405) {
867 "Unable to create branch path %s\n",
873 fprintf(stderr
, "Unable to start MKCOL request\n");
877 ep
[1] = saved_character
;
878 ep
= strchr(ep
+ 1, '/');
881 escaped
= xml_entities(ident_default_email());
882 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
885 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
886 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
887 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
889 slot
= get_active_slot();
890 slot
->results
= &results
;
891 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
892 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
893 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
895 lock
= xcalloc(1, sizeof(*lock
));
898 if (start_active_slot(slot
)) {
899 run_active_slot(slot
);
900 if (results
.curl_result
== CURLE_OK
) {
901 XML_Parser parser
= XML_ParserCreate(NULL
);
902 enum XML_Status result
;
903 ctx
.name
= xcalloc(10, 1);
906 ctx
.userFunc
= handle_new_lock_ctx
;
908 XML_SetUserData(parser
, &ctx
);
909 XML_SetElementHandler(parser
, xml_start_tag
,
911 XML_SetCharacterDataHandler(parser
, xml_cdata
);
912 result
= XML_Parse(parser
, in_buffer
.buf
,
915 if (result
!= XML_STATUS_OK
) {
916 fprintf(stderr
, "XML error: %s\n",
918 XML_GetErrorCode(parser
)));
921 XML_ParserFree(parser
);
924 fprintf(stderr
, "Unable to start LOCK request\n");
927 curl_slist_free_all(dav_headers
);
928 strbuf_release(&out_buffer
.buf
);
929 strbuf_release(&in_buffer
);
931 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
939 lock
->start_time
= time(NULL
);
940 lock
->next
= repo
->locks
;
947 static int unlock_remote(struct remote_lock
*lock
)
949 struct active_request_slot
*slot
;
950 struct slot_results results
;
951 struct remote_lock
*prev
= repo
->locks
;
952 struct curl_slist
*dav_headers
;
955 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
957 slot
= get_active_slot();
958 slot
->results
= &results
;
959 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
960 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
962 if (start_active_slot(slot
)) {
963 run_active_slot(slot
);
964 if (results
.curl_result
== CURLE_OK
)
967 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
970 fprintf(stderr
, "Unable to start UNLOCK request\n");
973 curl_slist_free_all(dav_headers
);
975 if (repo
->locks
== lock
) {
976 repo
->locks
= lock
->next
;
978 while (prev
&& prev
->next
!= lock
)
981 prev
->next
= prev
->next
->next
;
992 static void remove_locks(void)
994 struct remote_lock
*lock
= repo
->locks
;
996 fprintf(stderr
, "Removing remote locks...\n");
998 struct remote_lock
*next
= lock
->next
;
1004 static void remove_locks_on_signal(int signo
)
1007 sigchain_pop(signo
);
1011 static void remote_ls(const char *path
, int flags
,
1012 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1015 /* extract hex from sharded "xx/x{40}" filename */
1016 static int get_sha1_hex_from_objpath(const char *path
, unsigned char *sha1
)
1020 if (strlen(path
) != 41)
1023 memcpy(hex
, path
, 2);
1025 path
++; /* skip '/' */
1026 memcpy(hex
, path
, 38);
1028 return get_sha1_hex(hex
, sha1
);
1031 static void process_ls_object(struct remote_ls_ctx
*ls
)
1033 unsigned int *parent
= (unsigned int *)ls
->userData
;
1034 const char *path
= ls
->dentry_name
;
1035 unsigned char sha1
[20];
1037 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1038 remote_dir_exists
[*parent
] = 1;
1042 if (!skip_prefix(path
, "objects/", &path
) ||
1043 get_sha1_hex_from_objpath(path
, sha1
))
1046 one_remote_object(sha1
);
1049 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1051 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1052 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1056 if (!(ls
->dentry_flags
& IS_DIR
))
1057 one_remote_ref(ls
->dentry_name
);
1060 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1062 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1065 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1066 if (ls
->dentry_flags
& IS_DIR
) {
1068 /* ensure collection names end with slash */
1069 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1071 if (ls
->flags
& PROCESS_DIRS
) {
1074 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1075 ls
->flags
& RECURSIVE
) {
1076 remote_ls(ls
->dentry_name
,
1081 } else if (ls
->flags
& PROCESS_FILES
) {
1084 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1085 char *path
= ctx
->cdata
;
1086 if (*ctx
->cdata
== 'h') {
1087 path
= strstr(path
, "//");
1089 path
= strchr(path
+2, '/');
1093 const char *url
= repo
->url
;
1096 if (strncmp(path
, url
, repo
->path_len
))
1097 error("Parsed path '%s' does not match url: '%s'",
1100 path
+= repo
->path_len
;
1101 ls
->dentry_name
= xstrdup(path
);
1104 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1105 ls
->dentry_flags
|= IS_DIR
;
1107 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1108 free(ls
->dentry_name
);
1109 ls
->dentry_name
= NULL
;
1110 ls
->dentry_flags
= 0;
1115 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1116 * should _only_ heed the information from that file, instead of trying to
1117 * determine the refs from the remote file system (badly: it does not even
1118 * know about packed-refs).
1120 static void remote_ls(const char *path
, int flags
,
1121 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1124 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1125 struct active_request_slot
*slot
;
1126 struct slot_results results
;
1127 struct strbuf in_buffer
= STRBUF_INIT
;
1128 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1129 struct curl_slist
*dav_headers
= NULL
;
1131 struct remote_ls_ctx ls
;
1134 ls
.path
= xstrdup(path
);
1135 ls
.dentry_name
= NULL
;
1136 ls
.dentry_flags
= 0;
1137 ls
.userData
= userData
;
1138 ls
.userFunc
= userFunc
;
1140 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1142 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1143 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1145 slot
= get_active_slot();
1146 slot
->results
= &results
;
1147 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1148 &out_buffer
, fwrite_buffer
);
1149 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1150 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1152 if (start_active_slot(slot
)) {
1153 run_active_slot(slot
);
1154 if (results
.curl_result
== CURLE_OK
) {
1155 XML_Parser parser
= XML_ParserCreate(NULL
);
1156 enum XML_Status result
;
1157 ctx
.name
= xcalloc(10, 1);
1160 ctx
.userFunc
= handle_remote_ls_ctx
;
1162 XML_SetUserData(parser
, &ctx
);
1163 XML_SetElementHandler(parser
, xml_start_tag
,
1165 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1166 result
= XML_Parse(parser
, in_buffer
.buf
,
1170 if (result
!= XML_STATUS_OK
) {
1171 fprintf(stderr
, "XML error: %s\n",
1173 XML_GetErrorCode(parser
)));
1175 XML_ParserFree(parser
);
1178 fprintf(stderr
, "Unable to start PROPFIND request\n");
1183 strbuf_release(&out_buffer
.buf
);
1184 strbuf_release(&in_buffer
);
1185 curl_slist_free_all(dav_headers
);
1188 static void get_remote_object_list(unsigned char parent
)
1190 char path
[] = "objects/XX/";
1191 static const char hex
[] = "0123456789abcdef";
1192 unsigned int val
= parent
;
1194 path
[8] = hex
[val
>> 4];
1195 path
[9] = hex
[val
& 0xf];
1196 remote_dir_exists
[val
] = 0;
1197 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1198 process_ls_object
, &val
);
1201 static int locking_available(void)
1203 struct active_request_slot
*slot
;
1204 struct slot_results results
;
1205 struct strbuf in_buffer
= STRBUF_INIT
;
1206 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1207 struct curl_slist
*dav_headers
= NULL
;
1212 escaped
= xml_entities(repo
->url
);
1213 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1216 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1217 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1219 slot
= get_active_slot();
1220 slot
->results
= &results
;
1221 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1222 &out_buffer
, fwrite_buffer
);
1223 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1224 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1226 if (start_active_slot(slot
)) {
1227 run_active_slot(slot
);
1228 if (results
.curl_result
== CURLE_OK
) {
1229 XML_Parser parser
= XML_ParserCreate(NULL
);
1230 enum XML_Status result
;
1231 ctx
.name
= xcalloc(10, 1);
1234 ctx
.userFunc
= handle_lockprop_ctx
;
1235 ctx
.userData
= &lock_flags
;
1236 XML_SetUserData(parser
, &ctx
);
1237 XML_SetElementHandler(parser
, xml_start_tag
,
1239 result
= XML_Parse(parser
, in_buffer
.buf
,
1243 if (result
!= XML_STATUS_OK
) {
1244 fprintf(stderr
, "XML error: %s\n",
1246 XML_GetErrorCode(parser
)));
1249 XML_ParserFree(parser
);
1251 error("no DAV locking support on %s",
1255 error("Cannot access URL %s, return code %d",
1256 repo
->url
, results
.curl_result
);
1260 error("Unable to start PROPFIND request on %s", repo
->url
);
1263 strbuf_release(&out_buffer
.buf
);
1264 strbuf_release(&in_buffer
);
1265 curl_slist_free_all(dav_headers
);
1270 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1272 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1276 return &entry
->next
;
1279 static struct object_list
**process_blob(struct blob
*blob
,
1280 struct object_list
**p
,
1281 struct name_path
*path
,
1284 struct object
*obj
= &blob
->object
;
1286 obj
->flags
|= LOCAL
;
1288 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1292 return add_one_object(obj
, p
);
1295 static struct object_list
**process_tree(struct tree
*tree
,
1296 struct object_list
**p
,
1297 struct name_path
*path
,
1300 struct object
*obj
= &tree
->object
;
1301 struct tree_desc desc
;
1302 struct name_entry entry
;
1303 struct name_path me
;
1305 obj
->flags
|= LOCAL
;
1307 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1309 if (parse_tree(tree
) < 0)
1310 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1313 name
= xstrdup(name
);
1314 p
= add_one_object(obj
, p
);
1317 me
.elem_len
= strlen(name
);
1319 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1321 while (tree_entry(&desc
, &entry
))
1322 switch (object_type(entry
.mode
)) {
1324 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1327 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1330 /* Subproject commit - not in this repository */
1334 free_tree_buffer(tree
);
1338 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1341 struct commit
*commit
;
1342 struct object_list
**p
= &objects
;
1345 while ((commit
= get_revision(revs
)) != NULL
) {
1346 p
= process_tree(commit
->tree
, p
, NULL
, "");
1347 commit
->object
.flags
|= LOCAL
;
1348 if (!(commit
->object
.flags
& UNINTERESTING
))
1349 count
+= add_send_request(&commit
->object
, lock
);
1352 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1353 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1354 struct object
*obj
= entry
->item
;
1355 const char *name
= entry
->name
;
1357 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1359 if (obj
->type
== OBJ_TAG
) {
1361 p
= add_one_object(obj
, p
);
1364 if (obj
->type
== OBJ_TREE
) {
1365 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1368 if (obj
->type
== OBJ_BLOB
) {
1369 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1372 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1376 if (!(objects
->item
->flags
& UNINTERESTING
))
1377 count
+= add_send_request(objects
->item
, lock
);
1378 objects
= objects
->next
;
1384 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1386 struct active_request_slot
*slot
;
1387 struct slot_results results
;
1388 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1389 struct curl_slist
*dav_headers
;
1391 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1393 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1395 slot
= get_active_slot();
1396 slot
->results
= &results
;
1397 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1398 &out_buffer
, fwrite_null
);
1399 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1401 if (start_active_slot(slot
)) {
1402 run_active_slot(slot
);
1403 strbuf_release(&out_buffer
.buf
);
1404 if (results
.curl_result
!= CURLE_OK
) {
1406 "PUT error: curl result=%d, HTTP code=%ld\n",
1407 results
.curl_result
, results
.http_code
);
1408 /* We should attempt recovery? */
1412 strbuf_release(&out_buffer
.buf
);
1413 fprintf(stderr
, "Unable to start PUT request\n");
1420 static struct ref
*remote_refs
;
1422 static void one_remote_ref(const char *refname
)
1427 ref
= alloc_ref(refname
);
1429 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1431 "Unable to fetch ref %s from %s\n",
1432 refname
, repo
->url
);
1438 * Fetch a copy of the object if it doesn't exist locally - it
1439 * may be required for updating server info later.
1441 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1442 obj
= lookup_unknown_object(ref
->old_sha1
);
1444 fprintf(stderr
, " fetch %s for %s\n",
1445 sha1_to_hex(ref
->old_sha1
), refname
);
1446 add_fetch_request(obj
);
1450 ref
->next
= remote_refs
;
1454 static void get_dav_remote_heads(void)
1456 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1459 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1461 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1467 ref
= alloc_ref(ls
->dentry_name
);
1469 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1471 "Unable to fetch ref %s from %s\n",
1472 ls
->dentry_name
, repo
->url
);
1478 o
= parse_object(ref
->old_sha1
);
1481 "Unable to parse object %s for remote ref %s\n",
1482 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1488 len
= strlen(ls
->dentry_name
) + 42;
1489 ref_info
= xcalloc(len
+ 1, 1);
1490 sprintf(ref_info
, "%s %s\n",
1491 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1492 fwrite_buffer(ref_info
, 1, len
, buf
);
1495 if (o
->type
== OBJ_TAG
) {
1496 o
= deref_tag(o
, ls
->dentry_name
, 0);
1498 len
= strlen(ls
->dentry_name
) + 45;
1499 ref_info
= xcalloc(len
+ 1, 1);
1500 sprintf(ref_info
, "%s %s^{}\n",
1501 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1502 fwrite_buffer(ref_info
, 1, len
, buf
);
1509 static void update_remote_info_refs(struct remote_lock
*lock
)
1511 struct buffer buffer
= { STRBUF_INIT
, 0 };
1512 struct active_request_slot
*slot
;
1513 struct slot_results results
;
1514 struct curl_slist
*dav_headers
;
1516 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1517 add_remote_info_ref
, &buffer
.buf
);
1519 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1521 slot
= get_active_slot();
1522 slot
->results
= &results
;
1523 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1524 &buffer
, fwrite_null
);
1525 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1527 if (start_active_slot(slot
)) {
1528 run_active_slot(slot
);
1529 if (results
.curl_result
!= CURLE_OK
) {
1531 "PUT error: curl result=%d, HTTP code=%ld\n",
1532 results
.curl_result
, results
.http_code
);
1536 strbuf_release(&buffer
.buf
);
1539 static int remote_exists(const char *path
)
1541 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1545 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1549 case HTTP_MISSING_TARGET
:
1553 error("unable to access '%s': %s", url
, curl_errorstr
);
1561 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1563 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1564 struct strbuf buffer
= STRBUF_INIT
;
1567 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1568 die("Couldn't get %s for remote symref\n%s", url
,
1576 if (buffer
.len
== 0)
1579 /* Cut off trailing newline. */
1580 strbuf_rtrim(&buffer
);
1582 /* If it's a symref, set the refname; otherwise try for a sha1 */
1583 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1584 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1586 get_sha1_hex(buffer
.buf
, sha1
);
1589 strbuf_release(&buffer
);
1592 static int verify_merge_base(unsigned char *head_sha1
, struct ref
*remote
)
1594 struct commit
*head
= lookup_commit_or_die(head_sha1
, "HEAD");
1595 struct commit
*branch
= lookup_commit_or_die(remote
->old_sha1
, remote
->name
);
1597 return in_merge_bases(branch
, head
);
1600 static int delete_remote_branch(const char *pattern
, int force
)
1602 struct ref
*refs
= remote_refs
;
1603 struct ref
*remote_ref
= NULL
;
1604 unsigned char head_sha1
[20];
1605 char *symref
= NULL
;
1607 int patlen
= strlen(pattern
);
1609 struct active_request_slot
*slot
;
1610 struct slot_results results
;
1613 /* Find the remote branch(es) matching the specified branch name */
1614 for (match
= 0; refs
; refs
= refs
->next
) {
1615 char *name
= refs
->name
;
1616 int namelen
= strlen(name
);
1617 if (namelen
< patlen
||
1618 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1620 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1626 return error("No remote branch matches %s", pattern
);
1628 return error("More than one remote branch matches %s",
1632 * Remote HEAD must be a symref (not exactly foolproof; a remote
1633 * symlink to a symref will look like a symref)
1635 fetch_symref("HEAD", &symref
, head_sha1
);
1637 return error("Remote HEAD is not a symref");
1639 /* Remote branch must not be the remote HEAD */
1640 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1641 if (!strcmp(remote_ref
->name
, symref
))
1642 return error("Remote branch %s is the current HEAD",
1644 fetch_symref(symref
, &symref
, head_sha1
);
1647 /* Run extra sanity checks if delete is not forced */
1649 /* Remote HEAD must resolve to a known object */
1651 return error("Remote HEAD symrefs too deep");
1652 if (is_null_sha1(head_sha1
))
1653 return error("Unable to resolve remote HEAD");
1654 if (!has_sha1_file(head_sha1
))
1655 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1657 /* Remote branch must resolve to a known object */
1658 if (is_null_sha1(remote_ref
->old_sha1
))
1659 return error("Unable to resolve remote branch %s",
1661 if (!has_sha1_file(remote_ref
->old_sha1
))
1662 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref
->name
, sha1_to_hex(remote_ref
->old_sha1
));
1664 /* Remote branch must be an ancestor of remote HEAD */
1665 if (!verify_merge_base(head_sha1
, remote_ref
)) {
1666 return error("The branch '%s' is not an ancestor "
1667 "of your current HEAD.\n"
1668 "If you are sure you want to delete it,"
1669 " run:\n\t'git http-push -D %s %s'",
1670 remote_ref
->name
, repo
->url
, pattern
);
1674 /* Send delete request */
1675 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1678 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1679 slot
= get_active_slot();
1680 slot
->results
= &results
;
1681 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1682 if (start_active_slot(slot
)) {
1683 run_active_slot(slot
);
1685 if (results
.curl_result
!= CURLE_OK
)
1686 return error("DELETE request failed (%d/%ld)",
1687 results
.curl_result
, results
.http_code
);
1690 return error("Unable to start DELETE request");
1696 static void run_request_queue(void)
1698 #ifdef USE_CURL_MULTI
1699 is_running_queue
= 1;
1700 fill_active_slots();
1701 add_fill_function(NULL
, fill_active_slot
);
1704 finish_all_active_slots();
1705 #ifdef USE_CURL_MULTI
1706 fill_active_slots();
1708 } while (request_queue_head
&& !aborted
);
1710 #ifdef USE_CURL_MULTI
1711 is_running_queue
= 0;
1715 int main(int argc
, char **argv
)
1717 struct transfer_request
*request
;
1718 struct transfer_request
*next_request
;
1720 char **refspec
= NULL
;
1721 struct remote_lock
*ref_lock
= NULL
;
1722 struct remote_lock
*info_ref_lock
= NULL
;
1723 struct rev_info revs
;
1724 int delete_branch
= 0;
1725 int force_delete
= 0;
1726 int objects_to_send
;
1730 struct ref
*ref
, *local_refs
;
1732 git_setup_gettext();
1734 git_extract_argv0_path(argv
[0]);
1736 repo
= xcalloc(1, sizeof(*repo
));
1739 for (i
= 1; i
< argc
; i
++, argv
++) {
1743 if (!strcmp(arg
, "--all")) {
1744 push_all
= MATCH_REFS_ALL
;
1747 if (!strcmp(arg
, "--force")) {
1751 if (!strcmp(arg
, "--dry-run")) {
1755 if (!strcmp(arg
, "--helper-status")) {
1759 if (!strcmp(arg
, "--verbose")) {
1761 http_is_verbose
= 1;
1764 if (!strcmp(arg
, "-d")) {
1768 if (!strcmp(arg
, "-D")) {
1773 if (!strcmp(arg
, "-h"))
1774 usage(http_push_usage
);
1777 char *path
= strstr(arg
, "//");
1778 str_end_url_with_slash(arg
, &repo
->url
);
1779 repo
->path_len
= strlen(repo
->url
);
1781 repo
->path
= strchr(path
+2, '/');
1783 repo
->path_len
= strlen(repo
->path
);
1788 nr_refspec
= argc
- i
;
1792 #ifndef USE_CURL_MULTI
1793 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1797 usage(http_push_usage
);
1799 if (delete_branch
&& nr_refspec
!= 1)
1800 die("You must specify only one branch name when deleting a remote branch");
1802 setup_git_directory();
1804 memset(remote_dir_exists
, -1, 256);
1806 http_init(NULL
, repo
->url
, 1);
1808 #ifdef USE_CURL_MULTI
1809 is_running_queue
= 0;
1812 /* Verify DAV compliance/lock support */
1813 if (!locking_available()) {
1818 sigchain_push_common(remove_locks_on_signal
);
1820 /* Check whether the remote has server info files */
1821 repo
->can_update_info_refs
= 0;
1822 repo
->has_info_refs
= remote_exists("info/refs");
1823 repo
->has_info_packs
= remote_exists("objects/info/packs");
1824 if (repo
->has_info_refs
) {
1825 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1827 repo
->can_update_info_refs
= 1;
1829 error("cannot lock existing info/refs");
1834 if (repo
->has_info_packs
)
1837 /* Get a list of all local and remote heads to validate refspecs */
1838 local_refs
= get_local_heads();
1839 fprintf(stderr
, "Fetching remote heads...\n");
1840 get_dav_remote_heads();
1841 run_request_queue();
1843 /* Remove a remote branch if -d or -D was specified */
1844 if (delete_branch
) {
1845 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1846 fprintf(stderr
, "Unable to delete remote branch %s\n",
1849 printf("error %s cannot remove\n", refspec
[0]);
1855 if (match_push_refs(local_refs
, &remote_refs
,
1856 nr_refspec
, (const char **) refspec
, push_all
)) {
1861 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1863 printf("error null no match\n");
1869 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1870 char old_hex
[60], *new_hex
;
1871 const char *commit_argv
[5];
1873 char *new_sha1_hex
, *old_sha1_hex
;
1878 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1879 if (delete_remote_branch(ref
->name
, 1) == -1) {
1880 error("Could not remove %s", ref
->name
);
1882 printf("error %s cannot remove\n", ref
->name
);
1885 else if (helper_status
)
1886 printf("ok %s\n", ref
->name
);
1891 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1893 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1895 printf("ok %s up to date\n", ref
->name
);
1900 !is_null_sha1(ref
->old_sha1
) &&
1902 if (!has_sha1_file(ref
->old_sha1
) ||
1903 !ref_newer(ref
->peer_ref
->new_sha1
,
1906 * We do not have the remote ref, or
1907 * we know that the remote ref is not
1908 * an ancestor of what we are trying to
1909 * push. Either way this can be losing
1910 * commits at the remote end and likely
1911 * we were not up to date to begin with.
1913 error("remote '%s' is not an ancestor of\n"
1915 "Maybe you are not up-to-date and "
1916 "need to pull first?",
1918 ref
->peer_ref
->name
);
1920 printf("error %s non-fast forward\n", ref
->name
);
1925 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1927 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1928 new_hex
= sha1_to_hex(ref
->new_sha1
);
1930 fprintf(stderr
, "updating '%s'", ref
->name
);
1931 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1932 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1933 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
1936 printf("ok %s\n", ref
->name
);
1940 /* Lock remote branch ref */
1941 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1942 if (ref_lock
== NULL
) {
1943 fprintf(stderr
, "Unable to lock remote branch %s\n",
1946 printf("error %s lock error\n", ref
->name
);
1951 /* Set up revision info for this refspec */
1953 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
1954 old_sha1_hex
= NULL
;
1955 commit_argv
[1] = "--objects";
1956 commit_argv
[2] = new_sha1_hex
;
1957 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
1958 old_sha1_hex
= xmalloc(42);
1959 sprintf(old_sha1_hex
, "^%s",
1960 sha1_to_hex(ref
->old_sha1
));
1961 commit_argv
[3] = old_sha1_hex
;
1964 commit_argv
[commit_argc
] = NULL
;
1965 init_revisions(&revs
, setup_git_directory());
1966 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
1967 revs
.edge_hint
= 0; /* just in case */
1971 commit_argv
[1] = NULL
;
1974 /* Generate a list of objects that need to be pushed */
1976 if (prepare_revision_walk(&revs
))
1977 die("revision walk setup failed");
1978 mark_edges_uninteresting(&revs
, NULL
);
1979 objects_to_send
= get_delta(&revs
, ref_lock
);
1980 finish_all_active_slots();
1982 /* Push missing objects to remote, this would be a
1983 convenient time to pack them first if appropriate. */
1985 if (objects_to_send
)
1986 fprintf(stderr
, " sending %d objects\n",
1989 run_request_queue();
1991 /* Update the remote branch if all went well */
1992 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
1996 fprintf(stderr
, " done\n");
1998 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1999 unlock_remote(ref_lock
);
2003 /* Update remote server info if appropriate */
2004 if (repo
->has_info_refs
&& new_refs
) {
2005 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2006 fprintf(stderr
, "Updating remote server info\n");
2008 update_remote_info_refs(info_ref_lock
);
2010 fprintf(stderr
, "Unable to update server info\n");
2016 unlock_remote(info_ref_lock
);
2021 request
= request_queue_head
;
2022 while (request
!= NULL
) {
2023 next_request
= request
->next
;
2024 release_request(request
);
2025 request
= next_request
;