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 release_http_pack_request(preq
);
320 repo
->can_update_info_refs
= 0;
323 preq
->lst
= &repo
->packs
;
325 /* Make sure there isn't another open request for this pack */
326 while (check_request
) {
327 if (check_request
->state
== RUN_FETCH_PACKED
&&
328 !strcmp(check_request
->url
, preq
->url
)) {
329 release_http_pack_request(preq
);
330 release_request(request
);
333 check_request
= check_request
->next
;
336 preq
->slot
->callback_func
= process_response
;
337 preq
->slot
->callback_data
= request
;
338 request
->slot
= preq
->slot
;
339 request
->userData
= preq
;
341 /* Try to get the request started, abort the request on error */
342 request
->state
= RUN_FETCH_PACKED
;
343 if (!start_active_slot(preq
->slot
)) {
344 fprintf(stderr
, "Unable to start GET request\n");
345 release_http_pack_request(preq
);
346 repo
->can_update_info_refs
= 0;
347 release_request(request
);
351 static void start_put(struct transfer_request
*request
)
353 char *hex
= sha1_to_hex(request
->obj
->sha1
);
354 struct active_request_slot
*slot
;
355 struct strbuf buf
= STRBUF_INIT
;
356 enum object_type type
;
364 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
365 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
368 memset(&stream
, 0, sizeof(stream
));
369 git_deflate_init(&stream
, zlib_compression_level
);
370 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
371 strbuf_init(&request
->buffer
.buf
, size
);
372 request
->buffer
.posn
= 0;
375 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
376 stream
.avail_out
= size
;
379 stream
.next_in
= (void *)hdr
;
380 stream
.avail_in
= hdrlen
;
381 while (git_deflate(&stream
, 0) == Z_OK
)
384 /* Then the data itself.. */
385 stream
.next_in
= unpacked
;
386 stream
.avail_in
= len
;
387 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
389 git_deflate_end(&stream
);
392 request
->buffer
.buf
.len
= stream
.total_out
;
394 strbuf_addstr(&buf
, "Destination: ");
395 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
396 request
->dest
= strbuf_detach(&buf
, NULL
);
398 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
399 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
400 request
->url
= strbuf_detach(&buf
, NULL
);
402 slot
= get_active_slot();
403 slot
->callback_func
= process_response
;
404 slot
->callback_data
= request
;
405 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
406 &request
->buffer
, fwrite_null
);
408 if (start_active_slot(slot
)) {
409 request
->slot
= slot
;
410 request
->state
= RUN_PUT
;
412 request
->state
= ABORTED
;
418 static void start_move(struct transfer_request
*request
)
420 struct active_request_slot
*slot
;
421 struct curl_slist
*dav_headers
= NULL
;
423 slot
= get_active_slot();
424 slot
->callback_func
= process_response
;
425 slot
->callback_data
= request
;
426 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
427 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
428 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
429 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
431 if (start_active_slot(slot
)) {
432 request
->slot
= slot
;
433 request
->state
= RUN_MOVE
;
435 request
->state
= ABORTED
;
441 static int refresh_lock(struct remote_lock
*lock
)
443 struct active_request_slot
*slot
;
444 struct slot_results results
;
445 struct curl_slist
*dav_headers
;
448 lock
->refreshing
= 1;
450 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
452 slot
= get_active_slot();
453 slot
->results
= &results
;
454 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
455 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
457 if (start_active_slot(slot
)) {
458 run_active_slot(slot
);
459 if (results
.curl_result
!= CURLE_OK
) {
460 fprintf(stderr
, "LOCK HTTP error %ld\n",
463 lock
->start_time
= time(NULL
);
468 lock
->refreshing
= 0;
469 curl_slist_free_all(dav_headers
);
474 static void check_locks(void)
476 struct remote_lock
*lock
= repo
->locks
;
477 time_t current_time
= time(NULL
);
481 time_remaining
= lock
->start_time
+ lock
->timeout
-
483 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
484 if (!refresh_lock(lock
)) {
486 "Unable to refresh lock for %s\n",
496 static void release_request(struct transfer_request
*request
)
498 struct transfer_request
*entry
= request_queue_head
;
500 if (request
== request_queue_head
) {
501 request_queue_head
= request
->next
;
503 while (entry
->next
!= NULL
&& entry
->next
!= request
)
505 if (entry
->next
== request
)
506 entry
->next
= entry
->next
->next
;
513 static void finish_request(struct transfer_request
*request
)
515 struct http_pack_request
*preq
;
516 struct http_object_request
*obj_req
;
518 request
->curl_result
= request
->slot
->curl_result
;
519 request
->http_code
= request
->slot
->http_code
;
520 request
->slot
= NULL
;
522 /* Keep locks active */
525 if (request
->headers
!= NULL
)
526 curl_slist_free_all(request
->headers
);
528 /* URL is reused for MOVE after PUT */
529 if (request
->state
!= RUN_PUT
) {
534 if (request
->state
== RUN_MKCOL
) {
535 if (request
->curl_result
== CURLE_OK
||
536 request
->http_code
== 405) {
537 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
540 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
541 sha1_to_hex(request
->obj
->sha1
),
542 request
->curl_result
, request
->http_code
);
543 request
->state
= ABORTED
;
546 } else if (request
->state
== RUN_PUT
) {
547 if (request
->curl_result
== CURLE_OK
) {
550 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
551 sha1_to_hex(request
->obj
->sha1
),
552 request
->curl_result
, request
->http_code
);
553 request
->state
= ABORTED
;
556 } else if (request
->state
== RUN_MOVE
) {
557 if (request
->curl_result
== CURLE_OK
) {
559 fprintf(stderr
, " sent %s\n",
560 sha1_to_hex(request
->obj
->sha1
));
561 request
->obj
->flags
|= REMOTE
;
562 release_request(request
);
564 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
565 sha1_to_hex(request
->obj
->sha1
),
566 request
->curl_result
, request
->http_code
);
567 request
->state
= ABORTED
;
570 } else if (request
->state
== RUN_FETCH_LOOSE
) {
571 obj_req
= (struct http_object_request
*)request
->userData
;
573 if (finish_http_object_request(obj_req
) == 0)
574 if (obj_req
->rename
== 0)
575 request
->obj
->flags
|= (LOCAL
| REMOTE
);
577 /* Try fetching packed if necessary */
578 if (request
->obj
->flags
& LOCAL
) {
579 release_http_object_request(obj_req
);
580 release_request(request
);
582 start_fetch_packed(request
);
584 } else if (request
->state
== RUN_FETCH_PACKED
) {
586 if (request
->curl_result
!= CURLE_OK
) {
587 fprintf(stderr
, "Unable to get pack file %s\n%s",
588 request
->url
, curl_errorstr
);
590 preq
= (struct http_pack_request
*)request
->userData
;
593 if (finish_http_pack_request(preq
) == 0)
595 release_http_pack_request(preq
);
599 repo
->can_update_info_refs
= 0;
600 release_request(request
);
604 #ifdef USE_CURL_MULTI
605 static int is_running_queue
;
606 static int fill_active_slot(void *unused
)
608 struct transfer_request
*request
;
610 if (aborted
|| !is_running_queue
)
613 for (request
= request_queue_head
; request
; request
= request
->next
) {
614 if (request
->state
== NEED_FETCH
) {
615 start_fetch_loose(request
);
617 } else if (pushing
&& request
->state
== NEED_PUSH
) {
618 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
621 start_mkcol(request
);
630 static void get_remote_object_list(unsigned char parent
);
632 static void add_fetch_request(struct object
*obj
)
634 struct transfer_request
*request
;
639 * Don't fetch the object if it's known to exist locally
640 * or is already in the request queue
642 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
643 get_remote_object_list(obj
->sha1
[0]);
644 if (obj
->flags
& (LOCAL
| FETCHING
))
647 obj
->flags
|= FETCHING
;
648 request
= xmalloc(sizeof(*request
));
651 request
->lock
= NULL
;
652 request
->headers
= NULL
;
653 request
->state
= NEED_FETCH
;
654 request
->next
= request_queue_head
;
655 request_queue_head
= request
;
657 #ifdef USE_CURL_MULTI
663 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
665 struct transfer_request
*request
;
666 struct packed_git
*target
;
668 /* Keep locks active */
672 * Don't push the object if it's known to exist on the remote
673 * or is already in the request queue
675 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
676 get_remote_object_list(obj
->sha1
[0]);
677 if (obj
->flags
& (REMOTE
| PUSHING
))
679 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
681 obj
->flags
|= REMOTE
;
685 obj
->flags
|= PUSHING
;
686 request
= xmalloc(sizeof(*request
));
689 request
->lock
= lock
;
690 request
->headers
= NULL
;
691 request
->state
= NEED_PUSH
;
692 request
->next
= request_queue_head
;
693 request_queue_head
= request
;
695 #ifdef USE_CURL_MULTI
703 static int fetch_indices(void)
708 fprintf(stderr
, "Getting pack list\n");
710 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
712 case HTTP_MISSING_TARGET
:
722 static void one_remote_object(const unsigned char *sha1
)
726 obj
= lookup_object(sha1
);
728 obj
= parse_object(sha1
);
730 /* Ignore remote objects that don't exist locally */
734 obj
->flags
|= REMOTE
;
735 if (!object_list_contains(objects
, obj
))
736 object_list_insert(obj
, &objects
);
739 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
741 int *lock_flags
= (int *)ctx
->userData
;
744 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
745 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
746 (*lock_flags
& DAV_PROP_LOCKWR
)) {
747 *lock_flags
|= DAV_LOCK_OK
;
749 *lock_flags
&= DAV_LOCK_OK
;
750 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
751 *lock_flags
|= DAV_PROP_LOCKWR
;
752 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
753 *lock_flags
|= DAV_PROP_LOCKEX
;
758 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
760 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
762 unsigned char lock_token_sha1
[20];
764 if (tag_closed
&& ctx
->cdata
) {
765 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
766 lock
->owner
= xstrdup(ctx
->cdata
);
767 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
769 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
770 lock
->timeout
= strtol(arg
, NULL
, 10);
771 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
772 lock
->token
= xstrdup(ctx
->cdata
);
774 git_SHA1_Init(&sha_ctx
);
775 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
776 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
778 lock
->tmpfile_suffix
[0] = '_';
779 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
784 static void one_remote_ref(const char *refname
);
787 xml_start_tag(void *userData
, const char *name
, const char **atts
)
789 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
790 const char *c
= strchr(name
, ':');
798 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
800 if (new_len
> ctx
->len
) {
801 ctx
->name
= xrealloc(ctx
->name
, new_len
);
804 strcat(ctx
->name
, ".");
805 strcat(ctx
->name
, c
);
810 ctx
->userFunc(ctx
, 0);
814 xml_end_tag(void *userData
, const char *name
)
816 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
817 const char *c
= strchr(name
, ':');
820 ctx
->userFunc(ctx
, 1);
827 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
832 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
834 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
836 ctx
->cdata
= xmemdupz(s
, len
);
839 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
841 struct active_request_slot
*slot
;
842 struct slot_results results
;
843 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
844 struct strbuf in_buffer
= STRBUF_INIT
;
847 char timeout_header
[25];
848 struct remote_lock
*lock
= NULL
;
849 struct curl_slist
*dav_headers
= NULL
;
853 url
= xstrfmt("%s%s", repo
->url
, path
);
855 /* Make sure leading directories exist for the remote ref */
856 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
858 char saved_character
= ep
[1];
860 slot
= get_active_slot();
861 slot
->results
= &results
;
862 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
863 if (start_active_slot(slot
)) {
864 run_active_slot(slot
);
865 if (results
.curl_result
!= CURLE_OK
&&
866 results
.http_code
!= 405) {
868 "Unable to create branch path %s\n",
874 fprintf(stderr
, "Unable to start MKCOL request\n");
878 ep
[1] = saved_character
;
879 ep
= strchr(ep
+ 1, '/');
882 escaped
= xml_entities(ident_default_email());
883 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
886 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
887 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
888 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
890 slot
= get_active_slot();
891 slot
->results
= &results
;
892 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
893 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
894 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
896 lock
= xcalloc(1, sizeof(*lock
));
899 if (start_active_slot(slot
)) {
900 run_active_slot(slot
);
901 if (results
.curl_result
== CURLE_OK
) {
902 XML_Parser parser
= XML_ParserCreate(NULL
);
903 enum XML_Status result
;
904 ctx
.name
= xcalloc(10, 1);
907 ctx
.userFunc
= handle_new_lock_ctx
;
909 XML_SetUserData(parser
, &ctx
);
910 XML_SetElementHandler(parser
, xml_start_tag
,
912 XML_SetCharacterDataHandler(parser
, xml_cdata
);
913 result
= XML_Parse(parser
, in_buffer
.buf
,
916 if (result
!= XML_STATUS_OK
) {
917 fprintf(stderr
, "XML error: %s\n",
919 XML_GetErrorCode(parser
)));
922 XML_ParserFree(parser
);
925 fprintf(stderr
, "Unable to start LOCK request\n");
928 curl_slist_free_all(dav_headers
);
929 strbuf_release(&out_buffer
.buf
);
930 strbuf_release(&in_buffer
);
932 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
940 lock
->start_time
= time(NULL
);
941 lock
->next
= repo
->locks
;
948 static int unlock_remote(struct remote_lock
*lock
)
950 struct active_request_slot
*slot
;
951 struct slot_results results
;
952 struct remote_lock
*prev
= repo
->locks
;
953 struct curl_slist
*dav_headers
;
956 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
958 slot
= get_active_slot();
959 slot
->results
= &results
;
960 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
961 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
963 if (start_active_slot(slot
)) {
964 run_active_slot(slot
);
965 if (results
.curl_result
== CURLE_OK
)
968 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
971 fprintf(stderr
, "Unable to start UNLOCK request\n");
974 curl_slist_free_all(dav_headers
);
976 if (repo
->locks
== lock
) {
977 repo
->locks
= lock
->next
;
979 while (prev
&& prev
->next
!= lock
)
982 prev
->next
= prev
->next
->next
;
993 static void remove_locks(void)
995 struct remote_lock
*lock
= repo
->locks
;
997 fprintf(stderr
, "Removing remote locks...\n");
999 struct remote_lock
*next
= lock
->next
;
1000 unlock_remote(lock
);
1005 static void remove_locks_on_signal(int signo
)
1008 sigchain_pop(signo
);
1012 static void remote_ls(const char *path
, int flags
,
1013 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1016 /* extract hex from sharded "xx/x{40}" filename */
1017 static int get_sha1_hex_from_objpath(const char *path
, unsigned char *sha1
)
1021 if (strlen(path
) != 41)
1024 memcpy(hex
, path
, 2);
1026 path
++; /* skip '/' */
1027 memcpy(hex
, path
, 38);
1029 return get_sha1_hex(hex
, sha1
);
1032 static void process_ls_object(struct remote_ls_ctx
*ls
)
1034 unsigned int *parent
= (unsigned int *)ls
->userData
;
1035 const char *path
= ls
->dentry_name
;
1036 unsigned char sha1
[20];
1038 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1039 remote_dir_exists
[*parent
] = 1;
1043 if (!skip_prefix(path
, "objects/", &path
) ||
1044 get_sha1_hex_from_objpath(path
, sha1
))
1047 one_remote_object(sha1
);
1050 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1052 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1053 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1057 if (!(ls
->dentry_flags
& IS_DIR
))
1058 one_remote_ref(ls
->dentry_name
);
1061 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1063 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1066 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1067 if (ls
->dentry_flags
& IS_DIR
) {
1069 /* ensure collection names end with slash */
1070 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1072 if (ls
->flags
& PROCESS_DIRS
) {
1075 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1076 ls
->flags
& RECURSIVE
) {
1077 remote_ls(ls
->dentry_name
,
1082 } else if (ls
->flags
& PROCESS_FILES
) {
1085 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1086 char *path
= ctx
->cdata
;
1087 if (*ctx
->cdata
== 'h') {
1088 path
= strstr(path
, "//");
1090 path
= strchr(path
+2, '/');
1094 const char *url
= repo
->url
;
1097 if (strncmp(path
, url
, repo
->path_len
))
1098 error("Parsed path '%s' does not match url: '%s'",
1101 path
+= repo
->path_len
;
1102 ls
->dentry_name
= xstrdup(path
);
1105 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1106 ls
->dentry_flags
|= IS_DIR
;
1108 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1109 free(ls
->dentry_name
);
1110 ls
->dentry_name
= NULL
;
1111 ls
->dentry_flags
= 0;
1116 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1117 * should _only_ heed the information from that file, instead of trying to
1118 * determine the refs from the remote file system (badly: it does not even
1119 * know about packed-refs).
1121 static void remote_ls(const char *path
, int flags
,
1122 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1125 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1126 struct active_request_slot
*slot
;
1127 struct slot_results results
;
1128 struct strbuf in_buffer
= STRBUF_INIT
;
1129 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1130 struct curl_slist
*dav_headers
= NULL
;
1132 struct remote_ls_ctx ls
;
1135 ls
.path
= xstrdup(path
);
1136 ls
.dentry_name
= NULL
;
1137 ls
.dentry_flags
= 0;
1138 ls
.userData
= userData
;
1139 ls
.userFunc
= userFunc
;
1141 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1143 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1144 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1146 slot
= get_active_slot();
1147 slot
->results
= &results
;
1148 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1149 &out_buffer
, fwrite_buffer
);
1150 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1151 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1153 if (start_active_slot(slot
)) {
1154 run_active_slot(slot
);
1155 if (results
.curl_result
== CURLE_OK
) {
1156 XML_Parser parser
= XML_ParserCreate(NULL
);
1157 enum XML_Status result
;
1158 ctx
.name
= xcalloc(10, 1);
1161 ctx
.userFunc
= handle_remote_ls_ctx
;
1163 XML_SetUserData(parser
, &ctx
);
1164 XML_SetElementHandler(parser
, xml_start_tag
,
1166 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1167 result
= XML_Parse(parser
, in_buffer
.buf
,
1171 if (result
!= XML_STATUS_OK
) {
1172 fprintf(stderr
, "XML error: %s\n",
1174 XML_GetErrorCode(parser
)));
1176 XML_ParserFree(parser
);
1179 fprintf(stderr
, "Unable to start PROPFIND request\n");
1184 strbuf_release(&out_buffer
.buf
);
1185 strbuf_release(&in_buffer
);
1186 curl_slist_free_all(dav_headers
);
1189 static void get_remote_object_list(unsigned char parent
)
1191 char path
[] = "objects/XX/";
1192 static const char hex
[] = "0123456789abcdef";
1193 unsigned int val
= parent
;
1195 path
[8] = hex
[val
>> 4];
1196 path
[9] = hex
[val
& 0xf];
1197 remote_dir_exists
[val
] = 0;
1198 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1199 process_ls_object
, &val
);
1202 static int locking_available(void)
1204 struct active_request_slot
*slot
;
1205 struct slot_results results
;
1206 struct strbuf in_buffer
= STRBUF_INIT
;
1207 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1208 struct curl_slist
*dav_headers
= NULL
;
1213 escaped
= xml_entities(repo
->url
);
1214 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1217 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1218 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1220 slot
= get_active_slot();
1221 slot
->results
= &results
;
1222 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1223 &out_buffer
, fwrite_buffer
);
1224 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1225 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1227 if (start_active_slot(slot
)) {
1228 run_active_slot(slot
);
1229 if (results
.curl_result
== CURLE_OK
) {
1230 XML_Parser parser
= XML_ParserCreate(NULL
);
1231 enum XML_Status result
;
1232 ctx
.name
= xcalloc(10, 1);
1235 ctx
.userFunc
= handle_lockprop_ctx
;
1236 ctx
.userData
= &lock_flags
;
1237 XML_SetUserData(parser
, &ctx
);
1238 XML_SetElementHandler(parser
, xml_start_tag
,
1240 result
= XML_Parse(parser
, in_buffer
.buf
,
1244 if (result
!= XML_STATUS_OK
) {
1245 fprintf(stderr
, "XML error: %s\n",
1247 XML_GetErrorCode(parser
)));
1250 XML_ParserFree(parser
);
1252 error("no DAV locking support on %s",
1256 error("Cannot access URL %s, return code %d",
1257 repo
->url
, results
.curl_result
);
1261 error("Unable to start PROPFIND request on %s", repo
->url
);
1264 strbuf_release(&out_buffer
.buf
);
1265 strbuf_release(&in_buffer
);
1266 curl_slist_free_all(dav_headers
);
1271 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1273 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1277 return &entry
->next
;
1280 static struct object_list
**process_blob(struct blob
*blob
,
1281 struct object_list
**p
,
1282 struct name_path
*path
,
1285 struct object
*obj
= &blob
->object
;
1287 obj
->flags
|= LOCAL
;
1289 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1293 return add_one_object(obj
, p
);
1296 static struct object_list
**process_tree(struct tree
*tree
,
1297 struct object_list
**p
,
1298 struct name_path
*path
,
1301 struct object
*obj
= &tree
->object
;
1302 struct tree_desc desc
;
1303 struct name_entry entry
;
1304 struct name_path me
;
1306 obj
->flags
|= LOCAL
;
1308 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1310 if (parse_tree(tree
) < 0)
1311 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1314 name
= xstrdup(name
);
1315 p
= add_one_object(obj
, p
);
1318 me
.elem_len
= strlen(name
);
1320 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1322 while (tree_entry(&desc
, &entry
))
1323 switch (object_type(entry
.mode
)) {
1325 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1328 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1331 /* Subproject commit - not in this repository */
1335 free_tree_buffer(tree
);
1339 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1342 struct commit
*commit
;
1343 struct object_list
**p
= &objects
;
1346 while ((commit
= get_revision(revs
)) != NULL
) {
1347 p
= process_tree(commit
->tree
, p
, NULL
, "");
1348 commit
->object
.flags
|= LOCAL
;
1349 if (!(commit
->object
.flags
& UNINTERESTING
))
1350 count
+= add_send_request(&commit
->object
, lock
);
1353 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1354 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1355 struct object
*obj
= entry
->item
;
1356 const char *name
= entry
->name
;
1358 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1360 if (obj
->type
== OBJ_TAG
) {
1362 p
= add_one_object(obj
, p
);
1365 if (obj
->type
== OBJ_TREE
) {
1366 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1369 if (obj
->type
== OBJ_BLOB
) {
1370 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1373 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1377 if (!(objects
->item
->flags
& UNINTERESTING
))
1378 count
+= add_send_request(objects
->item
, lock
);
1379 objects
= objects
->next
;
1385 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1387 struct active_request_slot
*slot
;
1388 struct slot_results results
;
1389 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1390 struct curl_slist
*dav_headers
;
1392 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1394 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1396 slot
= get_active_slot();
1397 slot
->results
= &results
;
1398 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1399 &out_buffer
, fwrite_null
);
1400 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1402 if (start_active_slot(slot
)) {
1403 run_active_slot(slot
);
1404 strbuf_release(&out_buffer
.buf
);
1405 if (results
.curl_result
!= CURLE_OK
) {
1407 "PUT error: curl result=%d, HTTP code=%ld\n",
1408 results
.curl_result
, results
.http_code
);
1409 /* We should attempt recovery? */
1413 strbuf_release(&out_buffer
.buf
);
1414 fprintf(stderr
, "Unable to start PUT request\n");
1421 static struct ref
*remote_refs
;
1423 static void one_remote_ref(const char *refname
)
1428 ref
= alloc_ref(refname
);
1430 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1432 "Unable to fetch ref %s from %s\n",
1433 refname
, repo
->url
);
1439 * Fetch a copy of the object if it doesn't exist locally - it
1440 * may be required for updating server info later.
1442 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1443 obj
= lookup_unknown_object(ref
->old_sha1
);
1445 fprintf(stderr
, " fetch %s for %s\n",
1446 sha1_to_hex(ref
->old_sha1
), refname
);
1447 add_fetch_request(obj
);
1451 ref
->next
= remote_refs
;
1455 static void get_dav_remote_heads(void)
1457 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1460 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1462 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1468 ref
= alloc_ref(ls
->dentry_name
);
1470 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1472 "Unable to fetch ref %s from %s\n",
1473 ls
->dentry_name
, repo
->url
);
1479 o
= parse_object(ref
->old_sha1
);
1482 "Unable to parse object %s for remote ref %s\n",
1483 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1489 len
= strlen(ls
->dentry_name
) + 42;
1490 ref_info
= xcalloc(len
+ 1, 1);
1491 sprintf(ref_info
, "%s %s\n",
1492 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1493 fwrite_buffer(ref_info
, 1, len
, buf
);
1496 if (o
->type
== OBJ_TAG
) {
1497 o
= deref_tag(o
, ls
->dentry_name
, 0);
1499 len
= strlen(ls
->dentry_name
) + 45;
1500 ref_info
= xcalloc(len
+ 1, 1);
1501 sprintf(ref_info
, "%s %s^{}\n",
1502 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1503 fwrite_buffer(ref_info
, 1, len
, buf
);
1510 static void update_remote_info_refs(struct remote_lock
*lock
)
1512 struct buffer buffer
= { STRBUF_INIT
, 0 };
1513 struct active_request_slot
*slot
;
1514 struct slot_results results
;
1515 struct curl_slist
*dav_headers
;
1517 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1518 add_remote_info_ref
, &buffer
.buf
);
1520 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1522 slot
= get_active_slot();
1523 slot
->results
= &results
;
1524 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1525 &buffer
, fwrite_null
);
1526 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1528 if (start_active_slot(slot
)) {
1529 run_active_slot(slot
);
1530 if (results
.curl_result
!= CURLE_OK
) {
1532 "PUT error: curl result=%d, HTTP code=%ld\n",
1533 results
.curl_result
, results
.http_code
);
1537 strbuf_release(&buffer
.buf
);
1540 static int remote_exists(const char *path
)
1542 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1546 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1550 case HTTP_MISSING_TARGET
:
1554 error("unable to access '%s': %s", url
, curl_errorstr
);
1562 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1564 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1565 struct strbuf buffer
= STRBUF_INIT
;
1568 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1569 die("Couldn't get %s for remote symref\n%s", url
,
1577 if (buffer
.len
== 0)
1580 /* Cut off trailing newline. */
1581 strbuf_rtrim(&buffer
);
1583 /* If it's a symref, set the refname; otherwise try for a sha1 */
1584 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1585 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1587 get_sha1_hex(buffer
.buf
, sha1
);
1590 strbuf_release(&buffer
);
1593 static int verify_merge_base(unsigned char *head_sha1
, struct ref
*remote
)
1595 struct commit
*head
= lookup_commit_or_die(head_sha1
, "HEAD");
1596 struct commit
*branch
= lookup_commit_or_die(remote
->old_sha1
, remote
->name
);
1598 return in_merge_bases(branch
, head
);
1601 static int delete_remote_branch(const char *pattern
, int force
)
1603 struct ref
*refs
= remote_refs
;
1604 struct ref
*remote_ref
= NULL
;
1605 unsigned char head_sha1
[20];
1606 char *symref
= NULL
;
1608 int patlen
= strlen(pattern
);
1610 struct active_request_slot
*slot
;
1611 struct slot_results results
;
1614 /* Find the remote branch(es) matching the specified branch name */
1615 for (match
= 0; refs
; refs
= refs
->next
) {
1616 char *name
= refs
->name
;
1617 int namelen
= strlen(name
);
1618 if (namelen
< patlen
||
1619 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1621 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1627 return error("No remote branch matches %s", pattern
);
1629 return error("More than one remote branch matches %s",
1633 * Remote HEAD must be a symref (not exactly foolproof; a remote
1634 * symlink to a symref will look like a symref)
1636 fetch_symref("HEAD", &symref
, head_sha1
);
1638 return error("Remote HEAD is not a symref");
1640 /* Remote branch must not be the remote HEAD */
1641 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1642 if (!strcmp(remote_ref
->name
, symref
))
1643 return error("Remote branch %s is the current HEAD",
1645 fetch_symref(symref
, &symref
, head_sha1
);
1648 /* Run extra sanity checks if delete is not forced */
1650 /* Remote HEAD must resolve to a known object */
1652 return error("Remote HEAD symrefs too deep");
1653 if (is_null_sha1(head_sha1
))
1654 return error("Unable to resolve remote HEAD");
1655 if (!has_sha1_file(head_sha1
))
1656 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1658 /* Remote branch must resolve to a known object */
1659 if (is_null_sha1(remote_ref
->old_sha1
))
1660 return error("Unable to resolve remote branch %s",
1662 if (!has_sha1_file(remote_ref
->old_sha1
))
1663 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
));
1665 /* Remote branch must be an ancestor of remote HEAD */
1666 if (!verify_merge_base(head_sha1
, remote_ref
)) {
1667 return error("The branch '%s' is not an ancestor "
1668 "of your current HEAD.\n"
1669 "If you are sure you want to delete it,"
1670 " run:\n\t'git http-push -D %s %s'",
1671 remote_ref
->name
, repo
->url
, pattern
);
1675 /* Send delete request */
1676 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1679 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1680 slot
= get_active_slot();
1681 slot
->results
= &results
;
1682 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1683 if (start_active_slot(slot
)) {
1684 run_active_slot(slot
);
1686 if (results
.curl_result
!= CURLE_OK
)
1687 return error("DELETE request failed (%d/%ld)",
1688 results
.curl_result
, results
.http_code
);
1691 return error("Unable to start DELETE request");
1697 static void run_request_queue(void)
1699 #ifdef USE_CURL_MULTI
1700 is_running_queue
= 1;
1701 fill_active_slots();
1702 add_fill_function(NULL
, fill_active_slot
);
1705 finish_all_active_slots();
1706 #ifdef USE_CURL_MULTI
1707 fill_active_slots();
1709 } while (request_queue_head
&& !aborted
);
1711 #ifdef USE_CURL_MULTI
1712 is_running_queue
= 0;
1716 int main(int argc
, char **argv
)
1718 struct transfer_request
*request
;
1719 struct transfer_request
*next_request
;
1721 char **refspec
= NULL
;
1722 struct remote_lock
*ref_lock
= NULL
;
1723 struct remote_lock
*info_ref_lock
= NULL
;
1724 struct rev_info revs
;
1725 int delete_branch
= 0;
1726 int force_delete
= 0;
1727 int objects_to_send
;
1731 struct ref
*ref
, *local_refs
;
1733 git_setup_gettext();
1735 git_extract_argv0_path(argv
[0]);
1737 repo
= xcalloc(1, sizeof(*repo
));
1740 for (i
= 1; i
< argc
; i
++, argv
++) {
1744 if (!strcmp(arg
, "--all")) {
1745 push_all
= MATCH_REFS_ALL
;
1748 if (!strcmp(arg
, "--force")) {
1752 if (!strcmp(arg
, "--dry-run")) {
1756 if (!strcmp(arg
, "--helper-status")) {
1760 if (!strcmp(arg
, "--verbose")) {
1762 http_is_verbose
= 1;
1765 if (!strcmp(arg
, "-d")) {
1769 if (!strcmp(arg
, "-D")) {
1774 if (!strcmp(arg
, "-h"))
1775 usage(http_push_usage
);
1778 char *path
= strstr(arg
, "//");
1779 str_end_url_with_slash(arg
, &repo
->url
);
1780 repo
->path_len
= strlen(repo
->url
);
1782 repo
->path
= strchr(path
+2, '/');
1784 repo
->path_len
= strlen(repo
->path
);
1789 nr_refspec
= argc
- i
;
1793 #ifndef USE_CURL_MULTI
1794 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1798 usage(http_push_usage
);
1800 if (delete_branch
&& nr_refspec
!= 1)
1801 die("You must specify only one branch name when deleting a remote branch");
1803 setup_git_directory();
1805 memset(remote_dir_exists
, -1, 256);
1807 http_init(NULL
, repo
->url
, 1);
1809 #ifdef USE_CURL_MULTI
1810 is_running_queue
= 0;
1813 /* Verify DAV compliance/lock support */
1814 if (!locking_available()) {
1819 sigchain_push_common(remove_locks_on_signal
);
1821 /* Check whether the remote has server info files */
1822 repo
->can_update_info_refs
= 0;
1823 repo
->has_info_refs
= remote_exists("info/refs");
1824 repo
->has_info_packs
= remote_exists("objects/info/packs");
1825 if (repo
->has_info_refs
) {
1826 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1828 repo
->can_update_info_refs
= 1;
1830 error("cannot lock existing info/refs");
1835 if (repo
->has_info_packs
)
1838 /* Get a list of all local and remote heads to validate refspecs */
1839 local_refs
= get_local_heads();
1840 fprintf(stderr
, "Fetching remote heads...\n");
1841 get_dav_remote_heads();
1842 run_request_queue();
1844 /* Remove a remote branch if -d or -D was specified */
1845 if (delete_branch
) {
1846 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1847 fprintf(stderr
, "Unable to delete remote branch %s\n",
1850 printf("error %s cannot remove\n", refspec
[0]);
1856 if (match_push_refs(local_refs
, &remote_refs
,
1857 nr_refspec
, (const char **) refspec
, push_all
)) {
1862 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1864 printf("error null no match\n");
1870 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1871 char old_hex
[60], *new_hex
;
1872 const char *commit_argv
[5];
1874 char *new_sha1_hex
, *old_sha1_hex
;
1879 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1880 if (delete_remote_branch(ref
->name
, 1) == -1) {
1881 error("Could not remove %s", ref
->name
);
1883 printf("error %s cannot remove\n", ref
->name
);
1886 else if (helper_status
)
1887 printf("ok %s\n", ref
->name
);
1892 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1894 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1896 printf("ok %s up to date\n", ref
->name
);
1901 !is_null_sha1(ref
->old_sha1
) &&
1903 if (!has_sha1_file(ref
->old_sha1
) ||
1904 !ref_newer(ref
->peer_ref
->new_sha1
,
1907 * We do not have the remote ref, or
1908 * we know that the remote ref is not
1909 * an ancestor of what we are trying to
1910 * push. Either way this can be losing
1911 * commits at the remote end and likely
1912 * we were not up to date to begin with.
1914 error("remote '%s' is not an ancestor of\n"
1916 "Maybe you are not up-to-date and "
1917 "need to pull first?",
1919 ref
->peer_ref
->name
);
1921 printf("error %s non-fast forward\n", ref
->name
);
1926 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1928 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1929 new_hex
= sha1_to_hex(ref
->new_sha1
);
1931 fprintf(stderr
, "updating '%s'", ref
->name
);
1932 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1933 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1934 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
1937 printf("ok %s\n", ref
->name
);
1941 /* Lock remote branch ref */
1942 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1943 if (ref_lock
== NULL
) {
1944 fprintf(stderr
, "Unable to lock remote branch %s\n",
1947 printf("error %s lock error\n", ref
->name
);
1952 /* Set up revision info for this refspec */
1954 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
1955 old_sha1_hex
= NULL
;
1956 commit_argv
[1] = "--objects";
1957 commit_argv
[2] = new_sha1_hex
;
1958 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
1959 old_sha1_hex
= xmalloc(42);
1960 sprintf(old_sha1_hex
, "^%s",
1961 sha1_to_hex(ref
->old_sha1
));
1962 commit_argv
[3] = old_sha1_hex
;
1965 commit_argv
[commit_argc
] = NULL
;
1966 init_revisions(&revs
, setup_git_directory());
1967 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
1968 revs
.edge_hint
= 0; /* just in case */
1972 commit_argv
[1] = NULL
;
1975 /* Generate a list of objects that need to be pushed */
1977 if (prepare_revision_walk(&revs
))
1978 die("revision walk setup failed");
1979 mark_edges_uninteresting(&revs
, NULL
);
1980 objects_to_send
= get_delta(&revs
, ref_lock
);
1981 finish_all_active_slots();
1983 /* Push missing objects to remote, this would be a
1984 convenient time to pack them first if appropriate. */
1986 if (objects_to_send
)
1987 fprintf(stderr
, " sending %d objects\n",
1990 run_request_queue();
1992 /* Update the remote branch if all went well */
1993 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
1997 fprintf(stderr
, " done\n");
1999 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
2000 unlock_remote(ref_lock
);
2004 /* Update remote server info if appropriate */
2005 if (repo
->has_info_refs
&& new_refs
) {
2006 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2007 fprintf(stderr
, "Updating remote server info\n");
2009 update_remote_info_refs(info_ref_lock
);
2011 fprintf(stderr
, "Unable to update server info\n");
2017 unlock_remote(info_ref_lock
);
2022 request
= request_queue_head
;
2023 while (request
!= NULL
) {
2024 next_request
= request
->next
;
2025 release_request(request
);
2026 request
= next_request
;