11 #include "list-objects.h"
13 #include "argv-array.h"
16 #ifdef EXPAT_NEEDS_XMLPARSE_H
22 static const char http_push_usage
[] =
23 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
30 #define XML_STATUS_OK 1
31 #define XML_STATUS_ERROR 0
34 #define PREV_BUF_SIZE 4096
37 #define DAV_LOCK "LOCK"
38 #define DAV_MKCOL "MKCOL"
39 #define DAV_MOVE "MOVE"
40 #define DAV_PROPFIND "PROPFIND"
42 #define DAV_UNLOCK "UNLOCK"
43 #define DAV_DELETE "DELETE"
46 #define DAV_PROP_LOCKWR (1u << 0)
47 #define DAV_PROP_LOCKEX (1u << 1)
48 #define DAV_LOCK_OK (1u << 2)
50 /* DAV XML properties */
51 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
52 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
53 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
54 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
55 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
56 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
57 #define DAV_PROPFIND_RESP ".multistatus.response"
58 #define DAV_PROPFIND_NAME ".multistatus.response.href"
59 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
61 /* DAV request body templates */
62 #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>"
63 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
64 #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>"
67 #define LOCK_REFRESH 30
69 /* Remember to update object flag allocation in object.h */
70 #define LOCAL (1u<<16)
71 #define REMOTE (1u<<17)
72 #define FETCHING (1u<<18)
73 #define PUSHING (1u<<19)
75 /* We allow "recursive" symbolic refs. Only within reason, though */
80 static signed char remote_dir_exists
[256];
82 static int push_verbosely
;
83 static int push_all
= MATCH_REFS_NONE
;
86 static int helper_status
;
88 static struct object_list
*objects
;
95 int can_update_info_refs
;
97 struct packed_git
*packs
;
98 struct remote_lock
*locks
;
101 static struct repo
*repo
;
103 enum transfer_state
{
115 struct transfer_request
{
119 struct remote_lock
*lock
;
120 struct curl_slist
*headers
;
121 struct buffer buffer
;
122 enum transfer_state state
;
123 CURLcode curl_result
;
124 char errorstr
[CURL_ERROR_SIZE
];
127 struct active_request_slot
*slot
;
128 struct transfer_request
*next
;
131 static struct transfer_request
*request_queue_head
;
137 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
145 char tmpfile_suffix
[41];
149 struct remote_lock
*next
;
152 /* Flags that control remote_ls processing */
153 #define PROCESS_FILES (1u << 0)
154 #define PROCESS_DIRS (1u << 1)
155 #define RECURSIVE (1u << 2)
157 /* Flags that remote_ls passes to callback functions */
158 #define IS_DIR (1u << 0)
160 struct remote_ls_ctx
{
162 void (*userFunc
)(struct remote_ls_ctx
*ls
);
167 struct remote_ls_ctx
*parent
;
170 /* get_dav_token_headers options */
171 enum dav_header_flag
{
172 DAV_HEADER_IF
= (1u << 0),
173 DAV_HEADER_LOCK
= (1u << 1),
174 DAV_HEADER_TIMEOUT
= (1u << 2)
177 static char *xml_entities(const char *s
)
179 struct strbuf buf
= STRBUF_INIT
;
180 strbuf_addstr_xml_quoted(&buf
, s
);
181 return strbuf_detach(&buf
, NULL
);
184 static void curl_setup_http_get(CURL
*curl
, const char *url
,
185 const char *custom_req
)
187 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
188 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
189 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
190 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
193 static void curl_setup_http(CURL
*curl
, const char *url
,
194 const char *custom_req
, struct buffer
*buffer
,
195 curl_write_callback write_fn
)
197 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
198 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
199 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
200 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
201 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
202 #ifndef NO_CURL_IOCTL
203 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
204 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, buffer
);
206 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
207 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
208 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
209 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
212 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
214 struct strbuf buf
= STRBUF_INIT
;
215 struct curl_slist
*dav_headers
= http_copy_default_headers();
217 if (options
& DAV_HEADER_IF
) {
218 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
219 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
222 if (options
& DAV_HEADER_LOCK
) {
223 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
224 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
227 if (options
& DAV_HEADER_TIMEOUT
) {
228 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
229 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
232 strbuf_release(&buf
);
237 static void finish_request(struct transfer_request
*request
);
238 static void release_request(struct transfer_request
*request
);
240 static void process_response(void *callback_data
)
242 struct transfer_request
*request
=
243 (struct transfer_request
*)callback_data
;
245 finish_request(request
);
248 #ifdef USE_CURL_MULTI
250 static void start_fetch_loose(struct transfer_request
*request
)
252 struct active_request_slot
*slot
;
253 struct http_object_request
*obj_req
;
255 obj_req
= new_http_object_request(repo
->url
, request
->obj
->oid
.hash
);
256 if (obj_req
== NULL
) {
257 request
->state
= ABORTED
;
261 slot
= obj_req
->slot
;
262 slot
->callback_func
= process_response
;
263 slot
->callback_data
= request
;
264 request
->slot
= slot
;
265 request
->userData
= obj_req
;
267 /* Try to get the request started, abort the request on error */
268 request
->state
= RUN_FETCH_LOOSE
;
269 if (!start_active_slot(slot
)) {
270 fprintf(stderr
, "Unable to start GET request\n");
271 repo
->can_update_info_refs
= 0;
272 release_http_object_request(obj_req
);
273 release_request(request
);
277 static void start_mkcol(struct transfer_request
*request
)
279 char *hex
= oid_to_hex(&request
->obj
->oid
);
280 struct active_request_slot
*slot
;
282 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
284 slot
= get_active_slot();
285 slot
->callback_func
= process_response
;
286 slot
->callback_data
= request
;
287 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
288 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
290 if (start_active_slot(slot
)) {
291 request
->slot
= slot
;
292 request
->state
= RUN_MKCOL
;
294 request
->state
= ABORTED
;
295 FREE_AND_NULL(request
->url
);
300 static void start_fetch_packed(struct transfer_request
*request
)
302 struct packed_git
*target
;
304 struct transfer_request
*check_request
= request_queue_head
;
305 struct http_pack_request
*preq
;
307 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
309 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
310 repo
->can_update_info_refs
= 0;
311 release_request(request
);
315 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
316 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
318 preq
= new_http_pack_request(target
, repo
->url
);
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
= oid_to_hex(&request
->obj
->oid
);
354 struct active_request_slot
*slot
;
355 struct strbuf buf
= STRBUF_INIT
;
356 enum object_type type
;
364 unpacked
= read_sha1_file(request
->obj
->oid
.hash
, &type
, &len
);
365 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %lu", typename(type
), len
) + 1;
368 git_deflate_init(&stream
, zlib_compression_level
);
369 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
370 strbuf_init(&request
->buffer
.buf
, size
);
371 request
->buffer
.posn
= 0;
374 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
375 stream
.avail_out
= size
;
378 stream
.next_in
= (void *)hdr
;
379 stream
.avail_in
= hdrlen
;
380 while (git_deflate(&stream
, 0) == Z_OK
)
383 /* Then the data itself.. */
384 stream
.next_in
= unpacked
;
385 stream
.avail_in
= len
;
386 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
388 git_deflate_end(&stream
);
391 request
->buffer
.buf
.len
= stream
.total_out
;
393 strbuf_addstr(&buf
, "Destination: ");
394 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
395 request
->dest
= strbuf_detach(&buf
, NULL
);
397 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
398 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
399 request
->url
= strbuf_detach(&buf
, NULL
);
401 slot
= get_active_slot();
402 slot
->callback_func
= process_response
;
403 slot
->callback_data
= request
;
404 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
405 &request
->buffer
, fwrite_null
);
407 if (start_active_slot(slot
)) {
408 request
->slot
= slot
;
409 request
->state
= RUN_PUT
;
411 request
->state
= ABORTED
;
412 FREE_AND_NULL(request
->url
);
416 static void start_move(struct transfer_request
*request
)
418 struct active_request_slot
*slot
;
419 struct curl_slist
*dav_headers
= http_copy_default_headers();
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
;
434 FREE_AND_NULL(request
->url
);
438 static int refresh_lock(struct remote_lock
*lock
)
440 struct active_request_slot
*slot
;
441 struct slot_results results
;
442 struct curl_slist
*dav_headers
;
445 lock
->refreshing
= 1;
447 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
449 slot
= get_active_slot();
450 slot
->results
= &results
;
451 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
452 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
454 if (start_active_slot(slot
)) {
455 run_active_slot(slot
);
456 if (results
.curl_result
!= CURLE_OK
) {
457 fprintf(stderr
, "LOCK HTTP error %ld\n",
460 lock
->start_time
= time(NULL
);
465 lock
->refreshing
= 0;
466 curl_slist_free_all(dav_headers
);
471 static void check_locks(void)
473 struct remote_lock
*lock
= repo
->locks
;
474 time_t current_time
= time(NULL
);
478 time_remaining
= lock
->start_time
+ lock
->timeout
-
480 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
481 if (!refresh_lock(lock
)) {
483 "Unable to refresh lock for %s\n",
493 static void release_request(struct transfer_request
*request
)
495 struct transfer_request
*entry
= request_queue_head
;
497 if (request
== request_queue_head
) {
498 request_queue_head
= request
->next
;
500 while (entry
->next
!= NULL
&& entry
->next
!= request
)
502 if (entry
->next
== request
)
503 entry
->next
= entry
->next
->next
;
510 static void finish_request(struct transfer_request
*request
)
512 struct http_pack_request
*preq
;
513 struct http_object_request
*obj_req
;
515 request
->curl_result
= request
->slot
->curl_result
;
516 request
->http_code
= request
->slot
->http_code
;
517 request
->slot
= NULL
;
519 /* Keep locks active */
522 if (request
->headers
!= NULL
)
523 curl_slist_free_all(request
->headers
);
525 /* URL is reused for MOVE after PUT */
526 if (request
->state
!= RUN_PUT
) {
527 FREE_AND_NULL(request
->url
);
530 if (request
->state
== RUN_MKCOL
) {
531 if (request
->curl_result
== CURLE_OK
||
532 request
->http_code
== 405) {
533 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
536 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
537 oid_to_hex(&request
->obj
->oid
),
538 request
->curl_result
, request
->http_code
);
539 request
->state
= ABORTED
;
542 } else if (request
->state
== RUN_PUT
) {
543 if (request
->curl_result
== CURLE_OK
) {
546 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
547 oid_to_hex(&request
->obj
->oid
),
548 request
->curl_result
, request
->http_code
);
549 request
->state
= ABORTED
;
552 } else if (request
->state
== RUN_MOVE
) {
553 if (request
->curl_result
== CURLE_OK
) {
555 fprintf(stderr
, " sent %s\n",
556 oid_to_hex(&request
->obj
->oid
));
557 request
->obj
->flags
|= REMOTE
;
558 release_request(request
);
560 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
561 oid_to_hex(&request
->obj
->oid
),
562 request
->curl_result
, request
->http_code
);
563 request
->state
= ABORTED
;
566 } else if (request
->state
== RUN_FETCH_LOOSE
) {
567 obj_req
= (struct http_object_request
*)request
->userData
;
569 if (finish_http_object_request(obj_req
) == 0)
570 if (obj_req
->rename
== 0)
571 request
->obj
->flags
|= (LOCAL
| REMOTE
);
573 /* Try fetching packed if necessary */
574 if (request
->obj
->flags
& LOCAL
) {
575 release_http_object_request(obj_req
);
576 release_request(request
);
578 start_fetch_packed(request
);
580 } else if (request
->state
== RUN_FETCH_PACKED
) {
582 if (request
->curl_result
!= CURLE_OK
) {
583 fprintf(stderr
, "Unable to get pack file %s\n%s",
584 request
->url
, curl_errorstr
);
586 preq
= (struct http_pack_request
*)request
->userData
;
589 if (finish_http_pack_request(preq
) == 0)
591 release_http_pack_request(preq
);
595 repo
->can_update_info_refs
= 0;
596 release_request(request
);
600 #ifdef USE_CURL_MULTI
601 static int is_running_queue
;
602 static int fill_active_slot(void *unused
)
604 struct transfer_request
*request
;
606 if (aborted
|| !is_running_queue
)
609 for (request
= request_queue_head
; request
; request
= request
->next
) {
610 if (request
->state
== NEED_FETCH
) {
611 start_fetch_loose(request
);
613 } else if (pushing
&& request
->state
== NEED_PUSH
) {
614 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
617 start_mkcol(request
);
626 static void get_remote_object_list(unsigned char parent
);
628 static void add_fetch_request(struct object
*obj
)
630 struct transfer_request
*request
;
635 * Don't fetch the object if it's known to exist locally
636 * or is already in the request queue
638 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
639 get_remote_object_list(obj
->oid
.hash
[0]);
640 if (obj
->flags
& (LOCAL
| FETCHING
))
643 obj
->flags
|= FETCHING
;
644 request
= xmalloc(sizeof(*request
));
647 request
->lock
= NULL
;
648 request
->headers
= NULL
;
649 request
->state
= NEED_FETCH
;
650 request
->next
= request_queue_head
;
651 request_queue_head
= request
;
653 #ifdef USE_CURL_MULTI
659 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
661 struct transfer_request
*request
;
662 struct packed_git
*target
;
664 /* Keep locks active */
668 * Don't push the object if it's known to exist on the remote
669 * or is already in the request queue
671 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
672 get_remote_object_list(obj
->oid
.hash
[0]);
673 if (obj
->flags
& (REMOTE
| PUSHING
))
675 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
677 obj
->flags
|= REMOTE
;
681 obj
->flags
|= PUSHING
;
682 request
= xmalloc(sizeof(*request
));
685 request
->lock
= lock
;
686 request
->headers
= NULL
;
687 request
->state
= NEED_PUSH
;
688 request
->next
= request_queue_head
;
689 request_queue_head
= request
;
691 #ifdef USE_CURL_MULTI
699 static int fetch_indices(void)
704 fprintf(stderr
, "Getting pack list\n");
706 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
708 case HTTP_MISSING_TARGET
:
718 static void one_remote_object(const struct object_id
*oid
)
722 obj
= lookup_object(oid
->hash
);
724 obj
= parse_object(oid
);
726 /* Ignore remote objects that don't exist locally */
730 obj
->flags
|= REMOTE
;
731 if (!object_list_contains(objects
, obj
))
732 object_list_insert(obj
, &objects
);
735 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
737 int *lock_flags
= (int *)ctx
->userData
;
740 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
741 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
742 (*lock_flags
& DAV_PROP_LOCKWR
)) {
743 *lock_flags
|= DAV_LOCK_OK
;
745 *lock_flags
&= DAV_LOCK_OK
;
746 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
747 *lock_flags
|= DAV_PROP_LOCKWR
;
748 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
749 *lock_flags
|= DAV_PROP_LOCKEX
;
754 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
756 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
758 unsigned char lock_token_sha1
[20];
760 if (tag_closed
&& ctx
->cdata
) {
761 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
762 lock
->owner
= xstrdup(ctx
->cdata
);
763 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
765 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
766 lock
->timeout
= strtol(arg
, NULL
, 10);
767 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
768 lock
->token
= xstrdup(ctx
->cdata
);
770 git_SHA1_Init(&sha_ctx
);
771 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
772 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
774 lock
->tmpfile_suffix
[0] = '_';
775 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
780 static void one_remote_ref(const char *refname
);
783 xml_start_tag(void *userData
, const char *name
, const char **atts
)
785 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
786 const char *c
= strchr(name
, ':');
787 int old_namelen
, new_len
;
794 old_namelen
= strlen(ctx
->name
);
795 new_len
= old_namelen
+ strlen(c
) + 2;
797 if (new_len
> ctx
->len
) {
798 ctx
->name
= xrealloc(ctx
->name
, new_len
);
801 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
803 FREE_AND_NULL(ctx
->cdata
);
805 ctx
->userFunc(ctx
, 0);
809 xml_end_tag(void *userData
, const char *name
)
811 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
812 const char *c
= strchr(name
, ':');
815 ctx
->userFunc(ctx
, 1);
822 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
827 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
829 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
831 ctx
->cdata
= xmemdupz(s
, len
);
834 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
836 struct active_request_slot
*slot
;
837 struct slot_results results
;
838 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
839 struct strbuf in_buffer
= STRBUF_INIT
;
842 char timeout_header
[25];
843 struct remote_lock
*lock
= NULL
;
844 struct curl_slist
*dav_headers
= http_copy_default_headers();
848 url
= xstrfmt("%s%s", repo
->url
, path
);
850 /* Make sure leading directories exist for the remote ref */
851 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
853 char saved_character
= ep
[1];
855 slot
= get_active_slot();
856 slot
->results
= &results
;
857 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
858 if (start_active_slot(slot
)) {
859 run_active_slot(slot
);
860 if (results
.curl_result
!= CURLE_OK
&&
861 results
.http_code
!= 405) {
863 "Unable to create branch path %s\n",
869 fprintf(stderr
, "Unable to start MKCOL request\n");
873 ep
[1] = saved_character
;
874 ep
= strchr(ep
+ 1, '/');
877 escaped
= xml_entities(ident_default_email());
878 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
881 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
882 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
883 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
885 slot
= get_active_slot();
886 slot
->results
= &results
;
887 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
888 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
889 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
891 lock
= xcalloc(1, sizeof(*lock
));
894 if (start_active_slot(slot
)) {
895 run_active_slot(slot
);
896 if (results
.curl_result
== CURLE_OK
) {
897 XML_Parser parser
= XML_ParserCreate(NULL
);
898 enum XML_Status result
;
899 ctx
.name
= xcalloc(10, 1);
902 ctx
.userFunc
= handle_new_lock_ctx
;
904 XML_SetUserData(parser
, &ctx
);
905 XML_SetElementHandler(parser
, xml_start_tag
,
907 XML_SetCharacterDataHandler(parser
, xml_cdata
);
908 result
= XML_Parse(parser
, in_buffer
.buf
,
911 if (result
!= XML_STATUS_OK
) {
912 fprintf(stderr
, "XML error: %s\n",
914 XML_GetErrorCode(parser
)));
917 XML_ParserFree(parser
);
920 "error: curl result=%d, HTTP code=%ld\n",
921 results
.curl_result
, results
.http_code
);
924 fprintf(stderr
, "Unable to start LOCK request\n");
927 curl_slist_free_all(dav_headers
);
928 strbuf_release(&out_buffer
.buf
);
929 strbuf_release(&in_buffer
);
931 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
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{38}" filename */
1015 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1017 if (strlen(path
) != GIT_SHA1_HEXSZ
+ 1)
1020 if (hex_to_bytes(oid
->hash
, path
, 1))
1023 path
++; /* skip '/' */
1025 return hex_to_bytes(oid
->hash
+ 1, path
, GIT_SHA1_RAWSZ
- 1);
1028 static void process_ls_object(struct remote_ls_ctx
*ls
)
1030 unsigned int *parent
= (unsigned int *)ls
->userData
;
1031 const char *path
= ls
->dentry_name
;
1032 struct object_id oid
;
1034 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1035 remote_dir_exists
[*parent
] = 1;
1039 if (!skip_prefix(path
, "objects/", &path
) ||
1040 get_oid_hex_from_objpath(path
, &oid
))
1043 one_remote_object(&oid
);
1046 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1048 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1049 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1053 if (!(ls
->dentry_flags
& IS_DIR
))
1054 one_remote_ref(ls
->dentry_name
);
1057 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1059 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1062 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1063 if (ls
->dentry_flags
& IS_DIR
) {
1065 /* ensure collection names end with slash */
1066 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1068 if (ls
->flags
& PROCESS_DIRS
) {
1071 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1072 ls
->flags
& RECURSIVE
) {
1073 remote_ls(ls
->dentry_name
,
1078 } else if (ls
->flags
& PROCESS_FILES
) {
1081 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1082 char *path
= ctx
->cdata
;
1083 if (*ctx
->cdata
== 'h') {
1084 path
= strstr(path
, "//");
1086 path
= strchr(path
+2, '/');
1090 const char *url
= repo
->url
;
1093 if (strncmp(path
, url
, repo
->path_len
))
1094 error("Parsed path '%s' does not match url: '%s'",
1097 path
+= repo
->path_len
;
1098 ls
->dentry_name
= xstrdup(path
);
1101 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1102 ls
->dentry_flags
|= IS_DIR
;
1104 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1105 FREE_AND_NULL(ls
->dentry_name
);
1106 ls
->dentry_flags
= 0;
1111 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1112 * should _only_ heed the information from that file, instead of trying to
1113 * determine the refs from the remote file system (badly: it does not even
1114 * know about packed-refs).
1116 static void remote_ls(const char *path
, int flags
,
1117 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1120 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1121 struct active_request_slot
*slot
;
1122 struct slot_results results
;
1123 struct strbuf in_buffer
= STRBUF_INIT
;
1124 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1125 struct curl_slist
*dav_headers
= http_copy_default_headers();
1127 struct remote_ls_ctx ls
;
1130 ls
.path
= xstrdup(path
);
1131 ls
.dentry_name
= NULL
;
1132 ls
.dentry_flags
= 0;
1133 ls
.userData
= userData
;
1134 ls
.userFunc
= userFunc
;
1136 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1138 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1139 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1141 slot
= get_active_slot();
1142 slot
->results
= &results
;
1143 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1144 &out_buffer
, fwrite_buffer
);
1145 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1146 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1148 if (start_active_slot(slot
)) {
1149 run_active_slot(slot
);
1150 if (results
.curl_result
== CURLE_OK
) {
1151 XML_Parser parser
= XML_ParserCreate(NULL
);
1152 enum XML_Status result
;
1153 ctx
.name
= xcalloc(10, 1);
1156 ctx
.userFunc
= handle_remote_ls_ctx
;
1158 XML_SetUserData(parser
, &ctx
);
1159 XML_SetElementHandler(parser
, xml_start_tag
,
1161 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1162 result
= XML_Parse(parser
, in_buffer
.buf
,
1166 if (result
!= XML_STATUS_OK
) {
1167 fprintf(stderr
, "XML error: %s\n",
1169 XML_GetErrorCode(parser
)));
1171 XML_ParserFree(parser
);
1174 fprintf(stderr
, "Unable to start PROPFIND request\n");
1179 strbuf_release(&out_buffer
.buf
);
1180 strbuf_release(&in_buffer
);
1181 curl_slist_free_all(dav_headers
);
1184 static void get_remote_object_list(unsigned char parent
)
1186 char path
[] = "objects/XX/";
1187 static const char hex
[] = "0123456789abcdef";
1188 unsigned int val
= parent
;
1190 path
[8] = hex
[val
>> 4];
1191 path
[9] = hex
[val
& 0xf];
1192 remote_dir_exists
[val
] = 0;
1193 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1194 process_ls_object
, &val
);
1197 static int locking_available(void)
1199 struct active_request_slot
*slot
;
1200 struct slot_results results
;
1201 struct strbuf in_buffer
= STRBUF_INIT
;
1202 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1203 struct curl_slist
*dav_headers
= http_copy_default_headers();
1208 escaped
= xml_entities(repo
->url
);
1209 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1212 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1213 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1215 slot
= get_active_slot();
1216 slot
->results
= &results
;
1217 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1218 &out_buffer
, fwrite_buffer
);
1219 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1220 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1222 if (start_active_slot(slot
)) {
1223 run_active_slot(slot
);
1224 if (results
.curl_result
== CURLE_OK
) {
1225 XML_Parser parser
= XML_ParserCreate(NULL
);
1226 enum XML_Status result
;
1227 ctx
.name
= xcalloc(10, 1);
1230 ctx
.userFunc
= handle_lockprop_ctx
;
1231 ctx
.userData
= &lock_flags
;
1232 XML_SetUserData(parser
, &ctx
);
1233 XML_SetElementHandler(parser
, xml_start_tag
,
1235 result
= XML_Parse(parser
, in_buffer
.buf
,
1239 if (result
!= XML_STATUS_OK
) {
1240 fprintf(stderr
, "XML error: %s\n",
1242 XML_GetErrorCode(parser
)));
1245 XML_ParserFree(parser
);
1247 error("no DAV locking support on %s",
1251 error("Cannot access URL %s, return code %d",
1252 repo
->url
, results
.curl_result
);
1256 error("Unable to start PROPFIND request on %s", repo
->url
);
1259 strbuf_release(&out_buffer
.buf
);
1260 strbuf_release(&in_buffer
);
1261 curl_slist_free_all(dav_headers
);
1266 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1268 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1272 return &entry
->next
;
1275 static struct object_list
**process_blob(struct blob
*blob
,
1276 struct object_list
**p
)
1278 struct object
*obj
= &blob
->object
;
1280 obj
->flags
|= LOCAL
;
1282 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1286 return add_one_object(obj
, p
);
1289 static struct object_list
**process_tree(struct tree
*tree
,
1290 struct object_list
**p
)
1292 struct object
*obj
= &tree
->object
;
1293 struct tree_desc desc
;
1294 struct name_entry entry
;
1296 obj
->flags
|= LOCAL
;
1298 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1300 if (parse_tree(tree
) < 0)
1301 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1304 p
= add_one_object(obj
, p
);
1306 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1308 while (tree_entry(&desc
, &entry
))
1309 switch (object_type(entry
.mode
)) {
1311 p
= process_tree(lookup_tree(entry
.oid
), p
);
1314 p
= process_blob(lookup_blob(entry
.oid
), p
);
1317 /* Subproject commit - not in this repository */
1321 free_tree_buffer(tree
);
1325 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1328 struct commit
*commit
;
1329 struct object_list
**p
= &objects
;
1332 while ((commit
= get_revision(revs
)) != NULL
) {
1333 p
= process_tree(commit
->tree
, p
);
1334 commit
->object
.flags
|= LOCAL
;
1335 if (!(commit
->object
.flags
& UNINTERESTING
))
1336 count
+= add_send_request(&commit
->object
, lock
);
1339 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1340 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1341 struct object
*obj
= entry
->item
;
1342 const char *name
= entry
->name
;
1344 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1346 if (obj
->type
== OBJ_TAG
) {
1348 p
= add_one_object(obj
, p
);
1351 if (obj
->type
== OBJ_TREE
) {
1352 p
= process_tree((struct tree
*)obj
, p
);
1355 if (obj
->type
== OBJ_BLOB
) {
1356 p
= process_blob((struct blob
*)obj
, p
);
1359 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1363 if (!(objects
->item
->flags
& UNINTERESTING
))
1364 count
+= add_send_request(objects
->item
, lock
);
1365 objects
= objects
->next
;
1371 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1373 struct active_request_slot
*slot
;
1374 struct slot_results results
;
1375 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1376 struct curl_slist
*dav_headers
;
1378 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1380 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1382 slot
= get_active_slot();
1383 slot
->results
= &results
;
1384 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1385 &out_buffer
, fwrite_null
);
1386 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1388 if (start_active_slot(slot
)) {
1389 run_active_slot(slot
);
1390 strbuf_release(&out_buffer
.buf
);
1391 if (results
.curl_result
!= CURLE_OK
) {
1393 "PUT error: curl result=%d, HTTP code=%ld\n",
1394 results
.curl_result
, results
.http_code
);
1395 /* We should attempt recovery? */
1399 strbuf_release(&out_buffer
.buf
);
1400 fprintf(stderr
, "Unable to start PUT request\n");
1407 static struct ref
*remote_refs
;
1409 static void one_remote_ref(const char *refname
)
1414 ref
= alloc_ref(refname
);
1416 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1418 "Unable to fetch ref %s from %s\n",
1419 refname
, repo
->url
);
1425 * Fetch a copy of the object if it doesn't exist locally - it
1426 * may be required for updating server info later.
1428 if (repo
->can_update_info_refs
&& !has_object_file(&ref
->old_oid
)) {
1429 obj
= lookup_unknown_object(ref
->old_oid
.hash
);
1430 fprintf(stderr
, " fetch %s for %s\n",
1431 oid_to_hex(&ref
->old_oid
), refname
);
1432 add_fetch_request(obj
);
1435 ref
->next
= remote_refs
;
1439 static void get_dav_remote_heads(void)
1441 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1444 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1446 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1450 ref
= alloc_ref(ls
->dentry_name
);
1452 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1454 "Unable to fetch ref %s from %s\n",
1455 ls
->dentry_name
, repo
->url
);
1461 o
= parse_object(&ref
->old_oid
);
1464 "Unable to parse object %s for remote ref %s\n",
1465 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1471 strbuf_addf(buf
, "%s\t%s\n",
1472 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1474 if (o
->type
== OBJ_TAG
) {
1475 o
= deref_tag(o
, ls
->dentry_name
, 0);
1477 strbuf_addf(buf
, "%s\t%s^{}\n",
1478 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1483 static void update_remote_info_refs(struct remote_lock
*lock
)
1485 struct buffer buffer
= { STRBUF_INIT
, 0 };
1486 struct active_request_slot
*slot
;
1487 struct slot_results results
;
1488 struct curl_slist
*dav_headers
;
1490 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1491 add_remote_info_ref
, &buffer
.buf
);
1493 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1495 slot
= get_active_slot();
1496 slot
->results
= &results
;
1497 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1498 &buffer
, fwrite_null
);
1499 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1501 if (start_active_slot(slot
)) {
1502 run_active_slot(slot
);
1503 if (results
.curl_result
!= CURLE_OK
) {
1505 "PUT error: curl result=%d, HTTP code=%ld\n",
1506 results
.curl_result
, results
.http_code
);
1510 strbuf_release(&buffer
.buf
);
1513 static int remote_exists(const char *path
)
1515 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1519 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1523 case HTTP_MISSING_TARGET
:
1527 error("unable to access '%s': %s", url
, curl_errorstr
);
1536 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1538 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1539 struct strbuf buffer
= STRBUF_INIT
;
1542 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1543 die("Couldn't get %s for remote symref\n%s", url
,
1547 FREE_AND_NULL(*symref
);
1550 if (buffer
.len
== 0)
1553 /* Cut off trailing newline. */
1554 strbuf_rtrim(&buffer
);
1556 /* If it's a symref, set the refname; otherwise try for a sha1 */
1557 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1558 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1560 get_oid_hex(buffer
.buf
, oid
);
1563 strbuf_release(&buffer
);
1566 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1568 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1569 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1572 return in_merge_bases(branch
, head
);
1575 static int delete_remote_branch(const char *pattern
, int force
)
1577 struct ref
*refs
= remote_refs
;
1578 struct ref
*remote_ref
= NULL
;
1579 struct object_id head_oid
;
1580 char *symref
= NULL
;
1582 int patlen
= strlen(pattern
);
1584 struct active_request_slot
*slot
;
1585 struct slot_results results
;
1588 /* Find the remote branch(es) matching the specified branch name */
1589 for (match
= 0; refs
; refs
= refs
->next
) {
1590 char *name
= refs
->name
;
1591 int namelen
= strlen(name
);
1592 if (namelen
< patlen
||
1593 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1595 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1601 return error("No remote branch matches %s", pattern
);
1603 return error("More than one remote branch matches %s",
1607 * Remote HEAD must be a symref (not exactly foolproof; a remote
1608 * symlink to a symref will look like a symref)
1610 fetch_symref("HEAD", &symref
, &head_oid
);
1612 return error("Remote HEAD is not a symref");
1614 /* Remote branch must not be the remote HEAD */
1615 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1616 if (!strcmp(remote_ref
->name
, symref
))
1617 return error("Remote branch %s is the current HEAD",
1619 fetch_symref(symref
, &symref
, &head_oid
);
1622 /* Run extra sanity checks if delete is not forced */
1624 /* Remote HEAD must resolve to a known object */
1626 return error("Remote HEAD symrefs too deep");
1627 if (is_null_oid(&head_oid
))
1628 return error("Unable to resolve remote HEAD");
1629 if (!has_object_file(&head_oid
))
1630 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1632 /* Remote branch must resolve to a known object */
1633 if (is_null_oid(&remote_ref
->old_oid
))
1634 return error("Unable to resolve remote branch %s",
1636 if (!has_object_file(&remote_ref
->old_oid
))
1637 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref
->name
, oid_to_hex(&remote_ref
->old_oid
));
1639 /* Remote branch must be an ancestor of remote HEAD */
1640 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1641 return error("The branch '%s' is not an ancestor "
1642 "of your current HEAD.\n"
1643 "If you are sure you want to delete it,"
1644 " run:\n\t'git http-push -D %s %s'",
1645 remote_ref
->name
, repo
->url
, pattern
);
1649 /* Send delete request */
1650 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1653 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1654 slot
= get_active_slot();
1655 slot
->results
= &results
;
1656 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1657 if (start_active_slot(slot
)) {
1658 run_active_slot(slot
);
1660 if (results
.curl_result
!= CURLE_OK
)
1661 return error("DELETE request failed (%d/%ld)",
1662 results
.curl_result
, results
.http_code
);
1665 return error("Unable to start DELETE request");
1671 static void run_request_queue(void)
1673 #ifdef USE_CURL_MULTI
1674 is_running_queue
= 1;
1675 fill_active_slots();
1676 add_fill_function(NULL
, fill_active_slot
);
1679 finish_all_active_slots();
1680 #ifdef USE_CURL_MULTI
1681 fill_active_slots();
1683 } while (request_queue_head
&& !aborted
);
1685 #ifdef USE_CURL_MULTI
1686 is_running_queue
= 0;
1690 int cmd_main(int argc
, const char **argv
)
1692 struct transfer_request
*request
;
1693 struct transfer_request
*next_request
;
1695 const char **refspec
= NULL
;
1696 struct remote_lock
*ref_lock
= NULL
;
1697 struct remote_lock
*info_ref_lock
= NULL
;
1698 struct rev_info revs
;
1699 int delete_branch
= 0;
1700 int force_delete
= 0;
1701 int objects_to_send
;
1705 struct ref
*ref
, *local_refs
;
1707 repo
= xcalloc(1, sizeof(*repo
));
1710 for (i
= 1; i
< argc
; i
++, argv
++) {
1711 const char *arg
= *argv
;
1714 if (!strcmp(arg
, "--all")) {
1715 push_all
= MATCH_REFS_ALL
;
1718 if (!strcmp(arg
, "--force")) {
1722 if (!strcmp(arg
, "--dry-run")) {
1726 if (!strcmp(arg
, "--helper-status")) {
1730 if (!strcmp(arg
, "--verbose")) {
1732 http_is_verbose
= 1;
1735 if (!strcmp(arg
, "-d")) {
1739 if (!strcmp(arg
, "-D")) {
1744 if (!strcmp(arg
, "-h"))
1745 usage(http_push_usage
);
1748 char *path
= strstr(arg
, "//");
1749 str_end_url_with_slash(arg
, &repo
->url
);
1750 repo
->path_len
= strlen(repo
->url
);
1752 repo
->path
= strchr(path
+2, '/');
1754 repo
->path_len
= strlen(repo
->path
);
1759 nr_refspec
= argc
- i
;
1763 #ifndef USE_CURL_MULTI
1764 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1768 usage(http_push_usage
);
1770 if (delete_branch
&& nr_refspec
!= 1)
1771 die("You must specify only one branch name when deleting a remote branch");
1773 setup_git_directory();
1775 memset(remote_dir_exists
, -1, 256);
1777 http_init(NULL
, repo
->url
, 1);
1779 #ifdef USE_CURL_MULTI
1780 is_running_queue
= 0;
1783 /* Verify DAV compliance/lock support */
1784 if (!locking_available()) {
1789 sigchain_push_common(remove_locks_on_signal
);
1791 /* Check whether the remote has server info files */
1792 repo
->can_update_info_refs
= 0;
1793 repo
->has_info_refs
= remote_exists("info/refs");
1794 repo
->has_info_packs
= remote_exists("objects/info/packs");
1795 if (repo
->has_info_refs
) {
1796 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1798 repo
->can_update_info_refs
= 1;
1800 error("cannot lock existing info/refs");
1805 if (repo
->has_info_packs
)
1808 /* Get a list of all local and remote heads to validate refspecs */
1809 local_refs
= get_local_heads();
1810 fprintf(stderr
, "Fetching remote heads...\n");
1811 get_dav_remote_heads();
1812 run_request_queue();
1814 /* Remove a remote branch if -d or -D was specified */
1815 if (delete_branch
) {
1816 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1817 fprintf(stderr
, "Unable to delete remote branch %s\n",
1820 printf("error %s cannot remove\n", refspec
[0]);
1826 if (match_push_refs(local_refs
, &remote_refs
,
1827 nr_refspec
, (const char **) refspec
, push_all
)) {
1832 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1834 printf("error null no match\n");
1840 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1841 struct argv_array commit_argv
= ARGV_ARRAY_INIT
;
1846 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1847 if (delete_remote_branch(ref
->name
, 1) == -1) {
1848 error("Could not remove %s", ref
->name
);
1850 printf("error %s cannot remove\n", ref
->name
);
1853 else if (helper_status
)
1854 printf("ok %s\n", ref
->name
);
1859 if (!oidcmp(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1861 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1863 printf("ok %s up to date\n", ref
->name
);
1868 !is_null_oid(&ref
->old_oid
) &&
1870 if (!has_object_file(&ref
->old_oid
) ||
1871 !ref_newer(&ref
->peer_ref
->new_oid
,
1874 * We do not have the remote ref, or
1875 * we know that the remote ref is not
1876 * an ancestor of what we are trying to
1877 * push. Either way this can be losing
1878 * commits at the remote end and likely
1879 * we were not up to date to begin with.
1881 error("remote '%s' is not an ancestor of\n"
1883 "Maybe you are not up-to-date and "
1884 "need to pull first?",
1886 ref
->peer_ref
->name
);
1888 printf("error %s non-fast forward\n", ref
->name
);
1893 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1896 fprintf(stderr
, "updating '%s'", ref
->name
);
1897 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1898 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1899 fprintf(stderr
, "\n from %s\n to %s\n",
1900 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1903 printf("ok %s\n", ref
->name
);
1907 /* Lock remote branch ref */
1908 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1909 if (ref_lock
== NULL
) {
1910 fprintf(stderr
, "Unable to lock remote branch %s\n",
1913 printf("error %s lock error\n", ref
->name
);
1918 /* Set up revision info for this refspec */
1919 argv_array_push(&commit_argv
, ""); /* ignored */
1920 argv_array_push(&commit_argv
, "--objects");
1921 argv_array_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1922 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1923 argv_array_pushf(&commit_argv
, "^%s",
1924 oid_to_hex(&ref
->old_oid
));
1925 init_revisions(&revs
, setup_git_directory());
1926 setup_revisions(commit_argv
.argc
, commit_argv
.argv
, &revs
, NULL
);
1927 revs
.edge_hint
= 0; /* just in case */
1929 /* Generate a list of objects that need to be pushed */
1931 if (prepare_revision_walk(&revs
))
1932 die("revision walk setup failed");
1933 mark_edges_uninteresting(&revs
, NULL
);
1934 objects_to_send
= get_delta(&revs
, ref_lock
);
1935 finish_all_active_slots();
1937 /* Push missing objects to remote, this would be a
1938 convenient time to pack them first if appropriate. */
1940 if (objects_to_send
)
1941 fprintf(stderr
, " sending %d objects\n",
1944 run_request_queue();
1946 /* Update the remote branch if all went well */
1947 if (aborted
|| !update_remote(ref
->new_oid
.hash
, ref_lock
))
1951 fprintf(stderr
, " done\n");
1953 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1954 unlock_remote(ref_lock
);
1956 argv_array_clear(&commit_argv
);
1959 /* Update remote server info if appropriate */
1960 if (repo
->has_info_refs
&& new_refs
) {
1961 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1962 fprintf(stderr
, "Updating remote server info\n");
1964 update_remote_info_refs(info_ref_lock
);
1966 fprintf(stderr
, "Unable to update server info\n");
1972 unlock_remote(info_ref_lock
);
1977 request
= request_queue_head
;
1978 while (request
!= NULL
) {
1979 next_request
= request
->next
;
1980 release_request(request
);
1981 request
= next_request
;