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
= xsnprintf(hdr
, sizeof(hdr
), "%s %lu", typename(type
), len
) + 1;
367 git_deflate_init(&stream
, zlib_compression_level
);
368 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
369 strbuf_init(&request
->buffer
.buf
, size
);
370 request
->buffer
.posn
= 0;
373 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
374 stream
.avail_out
= size
;
377 stream
.next_in
= (void *)hdr
;
378 stream
.avail_in
= hdrlen
;
379 while (git_deflate(&stream
, 0) == Z_OK
)
382 /* Then the data itself.. */
383 stream
.next_in
= unpacked
;
384 stream
.avail_in
= len
;
385 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
387 git_deflate_end(&stream
);
390 request
->buffer
.buf
.len
= stream
.total_out
;
392 strbuf_addstr(&buf
, "Destination: ");
393 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
394 request
->dest
= strbuf_detach(&buf
, NULL
);
396 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
397 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
398 request
->url
= strbuf_detach(&buf
, NULL
);
400 slot
= get_active_slot();
401 slot
->callback_func
= process_response
;
402 slot
->callback_data
= request
;
403 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
404 &request
->buffer
, fwrite_null
);
406 if (start_active_slot(slot
)) {
407 request
->slot
= slot
;
408 request
->state
= RUN_PUT
;
410 request
->state
= ABORTED
;
416 static void start_move(struct transfer_request
*request
)
418 struct active_request_slot
*slot
;
419 struct curl_slist
*dav_headers
= NULL
;
421 slot
= get_active_slot();
422 slot
->callback_func
= process_response
;
423 slot
->callback_data
= request
;
424 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
425 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
426 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
427 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
429 if (start_active_slot(slot
)) {
430 request
->slot
= slot
;
431 request
->state
= RUN_MOVE
;
433 request
->state
= ABORTED
;
439 static int refresh_lock(struct remote_lock
*lock
)
441 struct active_request_slot
*slot
;
442 struct slot_results results
;
443 struct curl_slist
*dav_headers
;
446 lock
->refreshing
= 1;
448 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
450 slot
= get_active_slot();
451 slot
->results
= &results
;
452 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
453 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
455 if (start_active_slot(slot
)) {
456 run_active_slot(slot
);
457 if (results
.curl_result
!= CURLE_OK
) {
458 fprintf(stderr
, "LOCK HTTP error %ld\n",
461 lock
->start_time
= time(NULL
);
466 lock
->refreshing
= 0;
467 curl_slist_free_all(dav_headers
);
472 static void check_locks(void)
474 struct remote_lock
*lock
= repo
->locks
;
475 time_t current_time
= time(NULL
);
479 time_remaining
= lock
->start_time
+ lock
->timeout
-
481 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
482 if (!refresh_lock(lock
)) {
484 "Unable to refresh lock for %s\n",
494 static void release_request(struct transfer_request
*request
)
496 struct transfer_request
*entry
= request_queue_head
;
498 if (request
== request_queue_head
) {
499 request_queue_head
= request
->next
;
501 while (entry
->next
!= NULL
&& entry
->next
!= request
)
503 if (entry
->next
== request
)
504 entry
->next
= entry
->next
->next
;
511 static void finish_request(struct transfer_request
*request
)
513 struct http_pack_request
*preq
;
514 struct http_object_request
*obj_req
;
516 request
->curl_result
= request
->slot
->curl_result
;
517 request
->http_code
= request
->slot
->http_code
;
518 request
->slot
= NULL
;
520 /* Keep locks active */
523 if (request
->headers
!= NULL
)
524 curl_slist_free_all(request
->headers
);
526 /* URL is reused for MOVE after PUT */
527 if (request
->state
!= RUN_PUT
) {
532 if (request
->state
== RUN_MKCOL
) {
533 if (request
->curl_result
== CURLE_OK
||
534 request
->http_code
== 405) {
535 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
538 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
539 sha1_to_hex(request
->obj
->sha1
),
540 request
->curl_result
, request
->http_code
);
541 request
->state
= ABORTED
;
544 } else if (request
->state
== RUN_PUT
) {
545 if (request
->curl_result
== CURLE_OK
) {
548 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
549 sha1_to_hex(request
->obj
->sha1
),
550 request
->curl_result
, request
->http_code
);
551 request
->state
= ABORTED
;
554 } else if (request
->state
== RUN_MOVE
) {
555 if (request
->curl_result
== CURLE_OK
) {
557 fprintf(stderr
, " sent %s\n",
558 sha1_to_hex(request
->obj
->sha1
));
559 request
->obj
->flags
|= REMOTE
;
560 release_request(request
);
562 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
563 sha1_to_hex(request
->obj
->sha1
),
564 request
->curl_result
, request
->http_code
);
565 request
->state
= ABORTED
;
568 } else if (request
->state
== RUN_FETCH_LOOSE
) {
569 obj_req
= (struct http_object_request
*)request
->userData
;
571 if (finish_http_object_request(obj_req
) == 0)
572 if (obj_req
->rename
== 0)
573 request
->obj
->flags
|= (LOCAL
| REMOTE
);
575 /* Try fetching packed if necessary */
576 if (request
->obj
->flags
& LOCAL
) {
577 release_http_object_request(obj_req
);
578 release_request(request
);
580 start_fetch_packed(request
);
582 } else if (request
->state
== RUN_FETCH_PACKED
) {
584 if (request
->curl_result
!= CURLE_OK
) {
585 fprintf(stderr
, "Unable to get pack file %s\n%s",
586 request
->url
, curl_errorstr
);
588 preq
= (struct http_pack_request
*)request
->userData
;
591 if (finish_http_pack_request(preq
) == 0)
593 release_http_pack_request(preq
);
597 repo
->can_update_info_refs
= 0;
598 release_request(request
);
602 #ifdef USE_CURL_MULTI
603 static int is_running_queue
;
604 static int fill_active_slot(void *unused
)
606 struct transfer_request
*request
;
608 if (aborted
|| !is_running_queue
)
611 for (request
= request_queue_head
; request
; request
= request
->next
) {
612 if (request
->state
== NEED_FETCH
) {
613 start_fetch_loose(request
);
615 } else if (pushing
&& request
->state
== NEED_PUSH
) {
616 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
619 start_mkcol(request
);
628 static void get_remote_object_list(unsigned char parent
);
630 static void add_fetch_request(struct object
*obj
)
632 struct transfer_request
*request
;
637 * Don't fetch the object if it's known to exist locally
638 * or is already in the request queue
640 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
641 get_remote_object_list(obj
->sha1
[0]);
642 if (obj
->flags
& (LOCAL
| FETCHING
))
645 obj
->flags
|= FETCHING
;
646 request
= xmalloc(sizeof(*request
));
649 request
->lock
= NULL
;
650 request
->headers
= NULL
;
651 request
->state
= NEED_FETCH
;
652 request
->next
= request_queue_head
;
653 request_queue_head
= request
;
655 #ifdef USE_CURL_MULTI
661 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
663 struct transfer_request
*request
;
664 struct packed_git
*target
;
666 /* Keep locks active */
670 * Don't push the object if it's known to exist on the remote
671 * or is already in the request queue
673 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
674 get_remote_object_list(obj
->sha1
[0]);
675 if (obj
->flags
& (REMOTE
| PUSHING
))
677 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
679 obj
->flags
|= REMOTE
;
683 obj
->flags
|= PUSHING
;
684 request
= xmalloc(sizeof(*request
));
687 request
->lock
= lock
;
688 request
->headers
= NULL
;
689 request
->state
= NEED_PUSH
;
690 request
->next
= request_queue_head
;
691 request_queue_head
= request
;
693 #ifdef USE_CURL_MULTI
701 static int fetch_indices(void)
706 fprintf(stderr
, "Getting pack list\n");
708 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
710 case HTTP_MISSING_TARGET
:
720 static void one_remote_object(const unsigned char *sha1
)
724 obj
= lookup_object(sha1
);
726 obj
= parse_object(sha1
);
728 /* Ignore remote objects that don't exist locally */
732 obj
->flags
|= REMOTE
;
733 if (!object_list_contains(objects
, obj
))
734 object_list_insert(obj
, &objects
);
737 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
739 int *lock_flags
= (int *)ctx
->userData
;
742 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
743 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
744 (*lock_flags
& DAV_PROP_LOCKWR
)) {
745 *lock_flags
|= DAV_LOCK_OK
;
747 *lock_flags
&= DAV_LOCK_OK
;
748 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
749 *lock_flags
|= DAV_PROP_LOCKWR
;
750 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
751 *lock_flags
|= DAV_PROP_LOCKEX
;
756 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
758 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
760 unsigned char lock_token_sha1
[20];
762 if (tag_closed
&& ctx
->cdata
) {
763 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
764 lock
->owner
= xstrdup(ctx
->cdata
);
765 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
767 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
768 lock
->timeout
= strtol(arg
, NULL
, 10);
769 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
770 lock
->token
= xstrdup(ctx
->cdata
);
772 git_SHA1_Init(&sha_ctx
);
773 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
774 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
776 lock
->tmpfile_suffix
[0] = '_';
777 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
782 static void one_remote_ref(const char *refname
);
785 xml_start_tag(void *userData
, const char *name
, const char **atts
)
787 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
788 const char *c
= strchr(name
, ':');
789 int old_namelen
, new_len
;
796 old_namelen
= strlen(ctx
->name
);
797 new_len
= old_namelen
+ strlen(c
) + 2;
799 if (new_len
> ctx
->len
) {
800 ctx
->name
= xrealloc(ctx
->name
, new_len
);
803 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
808 ctx
->userFunc(ctx
, 0);
812 xml_end_tag(void *userData
, const char *name
)
814 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
815 const char *c
= strchr(name
, ':');
818 ctx
->userFunc(ctx
, 1);
825 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
830 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
832 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
834 ctx
->cdata
= xmemdupz(s
, len
);
837 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
839 struct active_request_slot
*slot
;
840 struct slot_results results
;
841 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
842 struct strbuf in_buffer
= STRBUF_INIT
;
845 char timeout_header
[25];
846 struct remote_lock
*lock
= NULL
;
847 struct curl_slist
*dav_headers
= NULL
;
851 url
= xstrfmt("%s%s", repo
->url
, path
);
853 /* Make sure leading directories exist for the remote ref */
854 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
856 char saved_character
= ep
[1];
858 slot
= get_active_slot();
859 slot
->results
= &results
;
860 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
861 if (start_active_slot(slot
)) {
862 run_active_slot(slot
);
863 if (results
.curl_result
!= CURLE_OK
&&
864 results
.http_code
!= 405) {
866 "Unable to create branch path %s\n",
872 fprintf(stderr
, "Unable to start MKCOL request\n");
876 ep
[1] = saved_character
;
877 ep
= strchr(ep
+ 1, '/');
880 escaped
= xml_entities(ident_default_email());
881 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
884 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
885 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
886 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
888 slot
= get_active_slot();
889 slot
->results
= &results
;
890 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
891 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
892 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
894 lock
= xcalloc(1, sizeof(*lock
));
897 if (start_active_slot(slot
)) {
898 run_active_slot(slot
);
899 if (results
.curl_result
== CURLE_OK
) {
900 XML_Parser parser
= XML_ParserCreate(NULL
);
901 enum XML_Status result
;
902 ctx
.name
= xcalloc(10, 1);
905 ctx
.userFunc
= handle_new_lock_ctx
;
907 XML_SetUserData(parser
, &ctx
);
908 XML_SetElementHandler(parser
, xml_start_tag
,
910 XML_SetCharacterDataHandler(parser
, xml_cdata
);
911 result
= XML_Parse(parser
, in_buffer
.buf
,
914 if (result
!= XML_STATUS_OK
) {
915 fprintf(stderr
, "XML error: %s\n",
917 XML_GetErrorCode(parser
)));
920 XML_ParserFree(parser
);
923 fprintf(stderr
, "Unable to start LOCK request\n");
926 curl_slist_free_all(dav_headers
);
927 strbuf_release(&out_buffer
.buf
);
928 strbuf_release(&in_buffer
);
930 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
938 lock
->start_time
= time(NULL
);
939 lock
->next
= repo
->locks
;
946 static int unlock_remote(struct remote_lock
*lock
)
948 struct active_request_slot
*slot
;
949 struct slot_results results
;
950 struct remote_lock
*prev
= repo
->locks
;
951 struct curl_slist
*dav_headers
;
954 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
956 slot
= get_active_slot();
957 slot
->results
= &results
;
958 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
959 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
961 if (start_active_slot(slot
)) {
962 run_active_slot(slot
);
963 if (results
.curl_result
== CURLE_OK
)
966 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
969 fprintf(stderr
, "Unable to start UNLOCK request\n");
972 curl_slist_free_all(dav_headers
);
974 if (repo
->locks
== lock
) {
975 repo
->locks
= lock
->next
;
977 while (prev
&& prev
->next
!= lock
)
980 prev
->next
= prev
->next
->next
;
991 static void remove_locks(void)
993 struct remote_lock
*lock
= repo
->locks
;
995 fprintf(stderr
, "Removing remote locks...\n");
997 struct remote_lock
*next
= lock
->next
;
1003 static void remove_locks_on_signal(int signo
)
1006 sigchain_pop(signo
);
1010 static void remote_ls(const char *path
, int flags
,
1011 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1014 /* extract hex from sharded "xx/x{40}" filename */
1015 static int get_sha1_hex_from_objpath(const char *path
, unsigned char *sha1
)
1019 if (strlen(path
) != 41)
1022 memcpy(hex
, path
, 2);
1024 path
++; /* skip '/' */
1025 memcpy(hex
, path
, 38);
1027 return get_sha1_hex(hex
, sha1
);
1030 static void process_ls_object(struct remote_ls_ctx
*ls
)
1032 unsigned int *parent
= (unsigned int *)ls
->userData
;
1033 const char *path
= ls
->dentry_name
;
1034 unsigned char sha1
[20];
1036 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1037 remote_dir_exists
[*parent
] = 1;
1041 if (!skip_prefix(path
, "objects/", &path
) ||
1042 get_sha1_hex_from_objpath(path
, sha1
))
1045 one_remote_object(sha1
);
1048 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1050 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1051 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1055 if (!(ls
->dentry_flags
& IS_DIR
))
1056 one_remote_ref(ls
->dentry_name
);
1059 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1061 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1064 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1065 if (ls
->dentry_flags
& IS_DIR
) {
1067 /* ensure collection names end with slash */
1068 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1070 if (ls
->flags
& PROCESS_DIRS
) {
1073 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1074 ls
->flags
& RECURSIVE
) {
1075 remote_ls(ls
->dentry_name
,
1080 } else if (ls
->flags
& PROCESS_FILES
) {
1083 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1084 char *path
= ctx
->cdata
;
1085 if (*ctx
->cdata
== 'h') {
1086 path
= strstr(path
, "//");
1088 path
= strchr(path
+2, '/');
1092 const char *url
= repo
->url
;
1095 if (strncmp(path
, url
, repo
->path_len
))
1096 error("Parsed path '%s' does not match url: '%s'",
1099 path
+= repo
->path_len
;
1100 ls
->dentry_name
= xstrdup(path
);
1103 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1104 ls
->dentry_flags
|= IS_DIR
;
1106 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1107 free(ls
->dentry_name
);
1108 ls
->dentry_name
= NULL
;
1109 ls
->dentry_flags
= 0;
1114 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1115 * should _only_ heed the information from that file, instead of trying to
1116 * determine the refs from the remote file system (badly: it does not even
1117 * know about packed-refs).
1119 static void remote_ls(const char *path
, int flags
,
1120 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1123 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1124 struct active_request_slot
*slot
;
1125 struct slot_results results
;
1126 struct strbuf in_buffer
= STRBUF_INIT
;
1127 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1128 struct curl_slist
*dav_headers
= NULL
;
1130 struct remote_ls_ctx ls
;
1133 ls
.path
= xstrdup(path
);
1134 ls
.dentry_name
= NULL
;
1135 ls
.dentry_flags
= 0;
1136 ls
.userData
= userData
;
1137 ls
.userFunc
= userFunc
;
1139 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1141 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1142 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1144 slot
= get_active_slot();
1145 slot
->results
= &results
;
1146 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1147 &out_buffer
, fwrite_buffer
);
1148 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1149 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1151 if (start_active_slot(slot
)) {
1152 run_active_slot(slot
);
1153 if (results
.curl_result
== CURLE_OK
) {
1154 XML_Parser parser
= XML_ParserCreate(NULL
);
1155 enum XML_Status result
;
1156 ctx
.name
= xcalloc(10, 1);
1159 ctx
.userFunc
= handle_remote_ls_ctx
;
1161 XML_SetUserData(parser
, &ctx
);
1162 XML_SetElementHandler(parser
, xml_start_tag
,
1164 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1165 result
= XML_Parse(parser
, in_buffer
.buf
,
1169 if (result
!= XML_STATUS_OK
) {
1170 fprintf(stderr
, "XML error: %s\n",
1172 XML_GetErrorCode(parser
)));
1174 XML_ParserFree(parser
);
1177 fprintf(stderr
, "Unable to start PROPFIND request\n");
1182 strbuf_release(&out_buffer
.buf
);
1183 strbuf_release(&in_buffer
);
1184 curl_slist_free_all(dav_headers
);
1187 static void get_remote_object_list(unsigned char parent
)
1189 char path
[] = "objects/XX/";
1190 static const char hex
[] = "0123456789abcdef";
1191 unsigned int val
= parent
;
1193 path
[8] = hex
[val
>> 4];
1194 path
[9] = hex
[val
& 0xf];
1195 remote_dir_exists
[val
] = 0;
1196 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1197 process_ls_object
, &val
);
1200 static int locking_available(void)
1202 struct active_request_slot
*slot
;
1203 struct slot_results results
;
1204 struct strbuf in_buffer
= STRBUF_INIT
;
1205 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1206 struct curl_slist
*dav_headers
= NULL
;
1211 escaped
= xml_entities(repo
->url
);
1212 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1215 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1216 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1218 slot
= get_active_slot();
1219 slot
->results
= &results
;
1220 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1221 &out_buffer
, fwrite_buffer
);
1222 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1223 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1225 if (start_active_slot(slot
)) {
1226 run_active_slot(slot
);
1227 if (results
.curl_result
== CURLE_OK
) {
1228 XML_Parser parser
= XML_ParserCreate(NULL
);
1229 enum XML_Status result
;
1230 ctx
.name
= xcalloc(10, 1);
1233 ctx
.userFunc
= handle_lockprop_ctx
;
1234 ctx
.userData
= &lock_flags
;
1235 XML_SetUserData(parser
, &ctx
);
1236 XML_SetElementHandler(parser
, xml_start_tag
,
1238 result
= XML_Parse(parser
, in_buffer
.buf
,
1242 if (result
!= XML_STATUS_OK
) {
1243 fprintf(stderr
, "XML error: %s\n",
1245 XML_GetErrorCode(parser
)));
1248 XML_ParserFree(parser
);
1250 error("no DAV locking support on %s",
1254 error("Cannot access URL %s, return code %d",
1255 repo
->url
, results
.curl_result
);
1259 error("Unable to start PROPFIND request on %s", repo
->url
);
1262 strbuf_release(&out_buffer
.buf
);
1263 strbuf_release(&in_buffer
);
1264 curl_slist_free_all(dav_headers
);
1269 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1271 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1275 return &entry
->next
;
1278 static struct object_list
**process_blob(struct blob
*blob
,
1279 struct object_list
**p
,
1280 struct name_path
*path
,
1283 struct object
*obj
= &blob
->object
;
1285 obj
->flags
|= LOCAL
;
1287 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1291 return add_one_object(obj
, p
);
1294 static struct object_list
**process_tree(struct tree
*tree
,
1295 struct object_list
**p
,
1296 struct name_path
*path
,
1299 struct object
*obj
= &tree
->object
;
1300 struct tree_desc desc
;
1301 struct name_entry entry
;
1302 struct name_path me
;
1304 obj
->flags
|= LOCAL
;
1306 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1308 if (parse_tree(tree
) < 0)
1309 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1312 name
= xstrdup(name
);
1313 p
= add_one_object(obj
, p
);
1316 me
.elem_len
= strlen(name
);
1318 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1320 while (tree_entry(&desc
, &entry
))
1321 switch (object_type(entry
.mode
)) {
1323 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1326 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1329 /* Subproject commit - not in this repository */
1333 free_tree_buffer(tree
);
1337 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1340 struct commit
*commit
;
1341 struct object_list
**p
= &objects
;
1344 while ((commit
= get_revision(revs
)) != NULL
) {
1345 p
= process_tree(commit
->tree
, p
, NULL
, "");
1346 commit
->object
.flags
|= LOCAL
;
1347 if (!(commit
->object
.flags
& UNINTERESTING
))
1348 count
+= add_send_request(&commit
->object
, lock
);
1351 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1352 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1353 struct object
*obj
= entry
->item
;
1354 const char *name
= entry
->name
;
1356 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1358 if (obj
->type
== OBJ_TAG
) {
1360 p
= add_one_object(obj
, p
);
1363 if (obj
->type
== OBJ_TREE
) {
1364 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1367 if (obj
->type
== OBJ_BLOB
) {
1368 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1371 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1375 if (!(objects
->item
->flags
& UNINTERESTING
))
1376 count
+= add_send_request(objects
->item
, lock
);
1377 objects
= objects
->next
;
1383 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1385 struct active_request_slot
*slot
;
1386 struct slot_results results
;
1387 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1388 struct curl_slist
*dav_headers
;
1390 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1392 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1394 slot
= get_active_slot();
1395 slot
->results
= &results
;
1396 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1397 &out_buffer
, fwrite_null
);
1398 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1400 if (start_active_slot(slot
)) {
1401 run_active_slot(slot
);
1402 strbuf_release(&out_buffer
.buf
);
1403 if (results
.curl_result
!= CURLE_OK
) {
1405 "PUT error: curl result=%d, HTTP code=%ld\n",
1406 results
.curl_result
, results
.http_code
);
1407 /* We should attempt recovery? */
1411 strbuf_release(&out_buffer
.buf
);
1412 fprintf(stderr
, "Unable to start PUT request\n");
1419 static struct ref
*remote_refs
;
1421 static void one_remote_ref(const char *refname
)
1426 ref
= alloc_ref(refname
);
1428 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1430 "Unable to fetch ref %s from %s\n",
1431 refname
, repo
->url
);
1437 * Fetch a copy of the object if it doesn't exist locally - it
1438 * may be required for updating server info later.
1440 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1441 obj
= lookup_unknown_object(ref
->old_sha1
);
1443 fprintf(stderr
, " fetch %s for %s\n",
1444 sha1_to_hex(ref
->old_sha1
), refname
);
1445 add_fetch_request(obj
);
1449 ref
->next
= remote_refs
;
1453 static void get_dav_remote_heads(void)
1455 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1458 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1460 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1464 ref
= alloc_ref(ls
->dentry_name
);
1466 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1468 "Unable to fetch ref %s from %s\n",
1469 ls
->dentry_name
, repo
->url
);
1475 o
= parse_object(ref
->old_sha1
);
1478 "Unable to parse object %s for remote ref %s\n",
1479 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1485 strbuf_addf(buf
, "%s\t%s\n",
1486 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1488 if (o
->type
== OBJ_TAG
) {
1489 o
= deref_tag(o
, ls
->dentry_name
, 0);
1491 strbuf_addf(buf
, "%s\t%s^{}\n",
1492 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1497 static void update_remote_info_refs(struct remote_lock
*lock
)
1499 struct buffer buffer
= { STRBUF_INIT
, 0 };
1500 struct active_request_slot
*slot
;
1501 struct slot_results results
;
1502 struct curl_slist
*dav_headers
;
1504 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1505 add_remote_info_ref
, &buffer
.buf
);
1507 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1509 slot
= get_active_slot();
1510 slot
->results
= &results
;
1511 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1512 &buffer
, fwrite_null
);
1513 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1515 if (start_active_slot(slot
)) {
1516 run_active_slot(slot
);
1517 if (results
.curl_result
!= CURLE_OK
) {
1519 "PUT error: curl result=%d, HTTP code=%ld\n",
1520 results
.curl_result
, results
.http_code
);
1524 strbuf_release(&buffer
.buf
);
1527 static int remote_exists(const char *path
)
1529 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1533 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1537 case HTTP_MISSING_TARGET
:
1541 error("unable to access '%s': %s", url
, curl_errorstr
);
1549 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1551 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1552 struct strbuf buffer
= STRBUF_INIT
;
1555 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1556 die("Couldn't get %s for remote symref\n%s", url
,
1564 if (buffer
.len
== 0)
1567 /* Cut off trailing newline. */
1568 strbuf_rtrim(&buffer
);
1570 /* If it's a symref, set the refname; otherwise try for a sha1 */
1571 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1572 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1574 get_sha1_hex(buffer
.buf
, sha1
);
1577 strbuf_release(&buffer
);
1580 static int verify_merge_base(unsigned char *head_sha1
, struct ref
*remote
)
1582 struct commit
*head
= lookup_commit_or_die(head_sha1
, "HEAD");
1583 struct commit
*branch
= lookup_commit_or_die(remote
->old_sha1
, remote
->name
);
1585 return in_merge_bases(branch
, head
);
1588 static int delete_remote_branch(const char *pattern
, int force
)
1590 struct ref
*refs
= remote_refs
;
1591 struct ref
*remote_ref
= NULL
;
1592 unsigned char head_sha1
[20];
1593 char *symref
= NULL
;
1595 int patlen
= strlen(pattern
);
1597 struct active_request_slot
*slot
;
1598 struct slot_results results
;
1601 /* Find the remote branch(es) matching the specified branch name */
1602 for (match
= 0; refs
; refs
= refs
->next
) {
1603 char *name
= refs
->name
;
1604 int namelen
= strlen(name
);
1605 if (namelen
< patlen
||
1606 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1608 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1614 return error("No remote branch matches %s", pattern
);
1616 return error("More than one remote branch matches %s",
1620 * Remote HEAD must be a symref (not exactly foolproof; a remote
1621 * symlink to a symref will look like a symref)
1623 fetch_symref("HEAD", &symref
, head_sha1
);
1625 return error("Remote HEAD is not a symref");
1627 /* Remote branch must not be the remote HEAD */
1628 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1629 if (!strcmp(remote_ref
->name
, symref
))
1630 return error("Remote branch %s is the current HEAD",
1632 fetch_symref(symref
, &symref
, head_sha1
);
1635 /* Run extra sanity checks if delete is not forced */
1637 /* Remote HEAD must resolve to a known object */
1639 return error("Remote HEAD symrefs too deep");
1640 if (is_null_sha1(head_sha1
))
1641 return error("Unable to resolve remote HEAD");
1642 if (!has_sha1_file(head_sha1
))
1643 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1645 /* Remote branch must resolve to a known object */
1646 if (is_null_sha1(remote_ref
->old_sha1
))
1647 return error("Unable to resolve remote branch %s",
1649 if (!has_sha1_file(remote_ref
->old_sha1
))
1650 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
));
1652 /* Remote branch must be an ancestor of remote HEAD */
1653 if (!verify_merge_base(head_sha1
, remote_ref
)) {
1654 return error("The branch '%s' is not an ancestor "
1655 "of your current HEAD.\n"
1656 "If you are sure you want to delete it,"
1657 " run:\n\t'git http-push -D %s %s'",
1658 remote_ref
->name
, repo
->url
, pattern
);
1662 /* Send delete request */
1663 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1666 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1667 slot
= get_active_slot();
1668 slot
->results
= &results
;
1669 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1670 if (start_active_slot(slot
)) {
1671 run_active_slot(slot
);
1673 if (results
.curl_result
!= CURLE_OK
)
1674 return error("DELETE request failed (%d/%ld)",
1675 results
.curl_result
, results
.http_code
);
1678 return error("Unable to start DELETE request");
1684 static void run_request_queue(void)
1686 #ifdef USE_CURL_MULTI
1687 is_running_queue
= 1;
1688 fill_active_slots();
1689 add_fill_function(NULL
, fill_active_slot
);
1692 finish_all_active_slots();
1693 #ifdef USE_CURL_MULTI
1694 fill_active_slots();
1696 } while (request_queue_head
&& !aborted
);
1698 #ifdef USE_CURL_MULTI
1699 is_running_queue
= 0;
1703 int main(int argc
, char **argv
)
1705 struct transfer_request
*request
;
1706 struct transfer_request
*next_request
;
1708 char **refspec
= NULL
;
1709 struct remote_lock
*ref_lock
= NULL
;
1710 struct remote_lock
*info_ref_lock
= NULL
;
1711 struct rev_info revs
;
1712 int delete_branch
= 0;
1713 int force_delete
= 0;
1714 int objects_to_send
;
1718 struct ref
*ref
, *local_refs
;
1720 git_setup_gettext();
1722 git_extract_argv0_path(argv
[0]);
1724 repo
= xcalloc(1, sizeof(*repo
));
1727 for (i
= 1; i
< argc
; i
++, argv
++) {
1731 if (!strcmp(arg
, "--all")) {
1732 push_all
= MATCH_REFS_ALL
;
1735 if (!strcmp(arg
, "--force")) {
1739 if (!strcmp(arg
, "--dry-run")) {
1743 if (!strcmp(arg
, "--helper-status")) {
1747 if (!strcmp(arg
, "--verbose")) {
1749 http_is_verbose
= 1;
1752 if (!strcmp(arg
, "-d")) {
1756 if (!strcmp(arg
, "-D")) {
1761 if (!strcmp(arg
, "-h"))
1762 usage(http_push_usage
);
1765 char *path
= strstr(arg
, "//");
1766 str_end_url_with_slash(arg
, &repo
->url
);
1767 repo
->path_len
= strlen(repo
->url
);
1769 repo
->path
= strchr(path
+2, '/');
1771 repo
->path_len
= strlen(repo
->path
);
1776 nr_refspec
= argc
- i
;
1780 #ifndef USE_CURL_MULTI
1781 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1785 usage(http_push_usage
);
1787 if (delete_branch
&& nr_refspec
!= 1)
1788 die("You must specify only one branch name when deleting a remote branch");
1790 setup_git_directory();
1792 memset(remote_dir_exists
, -1, 256);
1794 http_init(NULL
, repo
->url
, 1);
1796 #ifdef USE_CURL_MULTI
1797 is_running_queue
= 0;
1800 /* Verify DAV compliance/lock support */
1801 if (!locking_available()) {
1806 sigchain_push_common(remove_locks_on_signal
);
1808 /* Check whether the remote has server info files */
1809 repo
->can_update_info_refs
= 0;
1810 repo
->has_info_refs
= remote_exists("info/refs");
1811 repo
->has_info_packs
= remote_exists("objects/info/packs");
1812 if (repo
->has_info_refs
) {
1813 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1815 repo
->can_update_info_refs
= 1;
1817 error("cannot lock existing info/refs");
1822 if (repo
->has_info_packs
)
1825 /* Get a list of all local and remote heads to validate refspecs */
1826 local_refs
= get_local_heads();
1827 fprintf(stderr
, "Fetching remote heads...\n");
1828 get_dav_remote_heads();
1829 run_request_queue();
1831 /* Remove a remote branch if -d or -D was specified */
1832 if (delete_branch
) {
1833 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1834 fprintf(stderr
, "Unable to delete remote branch %s\n",
1837 printf("error %s cannot remove\n", refspec
[0]);
1843 if (match_push_refs(local_refs
, &remote_refs
,
1844 nr_refspec
, (const char **) refspec
, push_all
)) {
1849 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1851 printf("error null no match\n");
1857 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1858 char old_hex
[60], *new_hex
;
1859 const char *commit_argv
[5];
1861 char *new_sha1_hex
, *old_sha1_hex
;
1866 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1867 if (delete_remote_branch(ref
->name
, 1) == -1) {
1868 error("Could not remove %s", ref
->name
);
1870 printf("error %s cannot remove\n", ref
->name
);
1873 else if (helper_status
)
1874 printf("ok %s\n", ref
->name
);
1879 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1881 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1883 printf("ok %s up to date\n", ref
->name
);
1888 !is_null_sha1(ref
->old_sha1
) &&
1890 if (!has_sha1_file(ref
->old_sha1
) ||
1891 !ref_newer(ref
->peer_ref
->new_sha1
,
1894 * We do not have the remote ref, or
1895 * we know that the remote ref is not
1896 * an ancestor of what we are trying to
1897 * push. Either way this can be losing
1898 * commits at the remote end and likely
1899 * we were not up to date to begin with.
1901 error("remote '%s' is not an ancestor of\n"
1903 "Maybe you are not up-to-date and "
1904 "need to pull first?",
1906 ref
->peer_ref
->name
);
1908 printf("error %s non-fast forward\n", ref
->name
);
1913 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1915 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1916 new_hex
= sha1_to_hex(ref
->new_sha1
);
1918 fprintf(stderr
, "updating '%s'", ref
->name
);
1919 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1920 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1921 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
1924 printf("ok %s\n", ref
->name
);
1928 /* Lock remote branch ref */
1929 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1930 if (ref_lock
== NULL
) {
1931 fprintf(stderr
, "Unable to lock remote branch %s\n",
1934 printf("error %s lock error\n", ref
->name
);
1939 /* Set up revision info for this refspec */
1941 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
1942 old_sha1_hex
= NULL
;
1943 commit_argv
[1] = "--objects";
1944 commit_argv
[2] = new_sha1_hex
;
1945 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
1946 old_sha1_hex
= xmalloc(42);
1947 sprintf(old_sha1_hex
, "^%s",
1948 sha1_to_hex(ref
->old_sha1
));
1949 commit_argv
[3] = old_sha1_hex
;
1952 commit_argv
[commit_argc
] = NULL
;
1953 init_revisions(&revs
, setup_git_directory());
1954 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
1955 revs
.edge_hint
= 0; /* just in case */
1959 commit_argv
[1] = NULL
;
1962 /* Generate a list of objects that need to be pushed */
1964 if (prepare_revision_walk(&revs
))
1965 die("revision walk setup failed");
1966 mark_edges_uninteresting(&revs
, NULL
);
1967 objects_to_send
= get_delta(&revs
, ref_lock
);
1968 finish_all_active_slots();
1970 /* Push missing objects to remote, this would be a
1971 convenient time to pack them first if appropriate. */
1973 if (objects_to_send
)
1974 fprintf(stderr
, " sending %d objects\n",
1977 run_request_queue();
1979 /* Update the remote branch if all went well */
1980 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
1984 fprintf(stderr
, " done\n");
1986 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1987 unlock_remote(ref_lock
);
1991 /* Update remote server info if appropriate */
1992 if (repo
->has_info_refs
&& new_refs
) {
1993 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1994 fprintf(stderr
, "Updating remote server info\n");
1996 update_remote_info_refs(info_ref_lock
);
1998 fprintf(stderr
, "Unable to update server info\n");
2004 unlock_remote(info_ref_lock
);
2009 request
= request_queue_head
;
2010 while (request
!= NULL
) {
2011 next_request
= request
->next
;
2012 release_request(request
);
2013 request
= next_request
;