11 #include "list-objects.h"
16 static const char http_push_usage
[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
28 #define PREV_BUF_SIZE 4096
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
40 #define DAV_PROP_LOCKWR (1u << 0)
41 #define DAV_PROP_LOCKEX (1u << 1)
42 #define DAV_LOCK_OK (1u << 2)
44 /* DAV XML properties */
45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
51 #define DAV_PROPFIND_RESP ".multistatus.response"
52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
55 /* DAV request body templates */
56 #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>"
57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
58 #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>"
61 #define LOCK_REFRESH 30
63 /* bits #0-15 in revision.h */
65 #define LOCAL (1u<<16)
66 #define REMOTE (1u<<17)
67 #define FETCHING (1u<<18)
68 #define PUSHING (1u<<19)
70 /* We allow "recursive" symbolic refs. Only within reason, though */
75 static signed char remote_dir_exists
[256];
77 static int push_verbosely
;
78 static int push_all
= MATCH_REFS_NONE
;
81 static int helper_status
;
83 static struct object_list
*objects
;
90 int can_update_info_refs
;
92 struct packed_git
*packs
;
93 struct remote_lock
*locks
;
96 static struct repo
*repo
;
110 struct transfer_request
{
114 struct remote_lock
*lock
;
115 struct curl_slist
*headers
;
116 struct buffer buffer
;
117 enum transfer_state state
;
118 CURLcode curl_result
;
119 char errorstr
[CURL_ERROR_SIZE
];
122 struct active_request_slot
*slot
;
123 struct transfer_request
*next
;
126 static struct transfer_request
*request_queue_head
;
132 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
140 char tmpfile_suffix
[41];
144 struct remote_lock
*next
;
147 /* Flags that control remote_ls processing */
148 #define PROCESS_FILES (1u << 0)
149 #define PROCESS_DIRS (1u << 1)
150 #define RECURSIVE (1u << 2)
152 /* Flags that remote_ls passes to callback functions */
153 #define IS_DIR (1u << 0)
155 struct remote_ls_ctx
{
157 void (*userFunc
)(struct remote_ls_ctx
*ls
);
162 struct remote_ls_ctx
*parent
;
165 /* get_dav_token_headers options */
166 enum dav_header_flag
{
167 DAV_HEADER_IF
= (1u << 0),
168 DAV_HEADER_LOCK
= (1u << 1),
169 DAV_HEADER_TIMEOUT
= (1u << 2)
172 static char *xml_entities(const char *s
)
174 struct strbuf buf
= STRBUF_INIT
;
175 strbuf_addstr_xml_quoted(&buf
, s
);
176 return strbuf_detach(&buf
, NULL
);
179 static void curl_setup_http_get(CURL
*curl
, const char *url
,
180 const char *custom_req
)
182 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
183 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
184 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
185 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
188 static void curl_setup_http(CURL
*curl
, const char *url
,
189 const char *custom_req
, struct buffer
*buffer
,
190 curl_write_callback write_fn
)
192 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
193 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
194 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
195 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
196 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
197 #ifndef NO_CURL_IOCTL
198 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
199 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, &buffer
);
201 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
202 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
203 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
204 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
207 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
209 struct strbuf buf
= STRBUF_INIT
;
210 struct curl_slist
*dav_headers
= NULL
;
212 if (options
& DAV_HEADER_IF
) {
213 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
214 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
217 if (options
& DAV_HEADER_LOCK
) {
218 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
219 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
222 if (options
& DAV_HEADER_TIMEOUT
) {
223 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
224 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
227 strbuf_release(&buf
);
232 static void finish_request(struct transfer_request
*request
);
233 static void release_request(struct transfer_request
*request
);
235 static void process_response(void *callback_data
)
237 struct transfer_request
*request
=
238 (struct transfer_request
*)callback_data
;
240 finish_request(request
);
243 #ifdef USE_CURL_MULTI
245 static void start_fetch_loose(struct transfer_request
*request
)
247 struct active_request_slot
*slot
;
248 struct http_object_request
*obj_req
;
250 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
251 if (obj_req
== NULL
) {
252 request
->state
= ABORTED
;
256 slot
= obj_req
->slot
;
257 slot
->callback_func
= process_response
;
258 slot
->callback_data
= request
;
259 request
->slot
= slot
;
260 request
->userData
= obj_req
;
262 /* Try to get the request started, abort the request on error */
263 request
->state
= RUN_FETCH_LOOSE
;
264 if (!start_active_slot(slot
)) {
265 fprintf(stderr
, "Unable to start GET request\n");
266 repo
->can_update_info_refs
= 0;
267 release_http_object_request(obj_req
);
268 release_request(request
);
272 static void start_mkcol(struct transfer_request
*request
)
274 char *hex
= sha1_to_hex(request
->obj
->sha1
);
275 struct active_request_slot
*slot
;
277 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
279 slot
= get_active_slot();
280 slot
->callback_func
= process_response
;
281 slot
->callback_data
= request
;
282 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
283 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
285 if (start_active_slot(slot
)) {
286 request
->slot
= slot
;
287 request
->state
= RUN_MKCOL
;
289 request
->state
= ABORTED
;
296 static void start_fetch_packed(struct transfer_request
*request
)
298 struct packed_git
*target
;
300 struct transfer_request
*check_request
= request_queue_head
;
301 struct http_pack_request
*preq
;
303 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
305 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
306 repo
->can_update_info_refs
= 0;
307 release_request(request
);
311 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
312 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
314 preq
= new_http_pack_request(target
, repo
->url
);
316 release_http_pack_request(preq
);
317 repo
->can_update_info_refs
= 0;
320 preq
->lst
= &repo
->packs
;
322 /* Make sure there isn't another open request for this pack */
323 while (check_request
) {
324 if (check_request
->state
== RUN_FETCH_PACKED
&&
325 !strcmp(check_request
->url
, preq
->url
)) {
326 release_http_pack_request(preq
);
327 release_request(request
);
330 check_request
= check_request
->next
;
333 preq
->slot
->callback_func
= process_response
;
334 preq
->slot
->callback_data
= request
;
335 request
->slot
= preq
->slot
;
336 request
->userData
= preq
;
338 /* Try to get the request started, abort the request on error */
339 request
->state
= RUN_FETCH_PACKED
;
340 if (!start_active_slot(preq
->slot
)) {
341 fprintf(stderr
, "Unable to start GET request\n");
342 release_http_pack_request(preq
);
343 repo
->can_update_info_refs
= 0;
344 release_request(request
);
348 static void start_put(struct transfer_request
*request
)
350 char *hex
= sha1_to_hex(request
->obj
->sha1
);
351 struct active_request_slot
*slot
;
352 struct strbuf buf
= STRBUF_INIT
;
353 enum object_type type
;
361 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
362 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
365 memset(&stream
, 0, sizeof(stream
));
366 git_deflate_init(&stream
, zlib_compression_level
);
367 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
368 strbuf_init(&request
->buffer
.buf
, size
);
369 request
->buffer
.posn
= 0;
372 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
373 stream
.avail_out
= size
;
376 stream
.next_in
= (void *)hdr
;
377 stream
.avail_in
= hdrlen
;
378 while (git_deflate(&stream
, 0) == Z_OK
)
381 /* Then the data itself.. */
382 stream
.next_in
= unpacked
;
383 stream
.avail_in
= len
;
384 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
386 git_deflate_end(&stream
);
389 request
->buffer
.buf
.len
= stream
.total_out
;
391 strbuf_addstr(&buf
, "Destination: ");
392 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
393 request
->dest
= strbuf_detach(&buf
, NULL
);
395 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
396 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
397 request
->url
= strbuf_detach(&buf
, NULL
);
399 slot
= get_active_slot();
400 slot
->callback_func
= process_response
;
401 slot
->callback_data
= request
;
402 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
403 &request
->buffer
, fwrite_null
);
405 if (start_active_slot(slot
)) {
406 request
->slot
= slot
;
407 request
->state
= RUN_PUT
;
409 request
->state
= ABORTED
;
415 static void start_move(struct transfer_request
*request
)
417 struct active_request_slot
*slot
;
418 struct curl_slist
*dav_headers
= NULL
;
420 slot
= get_active_slot();
421 slot
->callback_func
= process_response
;
422 slot
->callback_data
= request
;
423 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
424 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
425 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
426 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
428 if (start_active_slot(slot
)) {
429 request
->slot
= slot
;
430 request
->state
= RUN_MOVE
;
432 request
->state
= ABORTED
;
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
) {
531 if (request
->state
== RUN_MKCOL
) {
532 if (request
->curl_result
== CURLE_OK
||
533 request
->http_code
== 405) {
534 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
537 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
538 sha1_to_hex(request
->obj
->sha1
),
539 request
->curl_result
, request
->http_code
);
540 request
->state
= ABORTED
;
543 } else if (request
->state
== RUN_PUT
) {
544 if (request
->curl_result
== CURLE_OK
) {
547 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
548 sha1_to_hex(request
->obj
->sha1
),
549 request
->curl_result
, request
->http_code
);
550 request
->state
= ABORTED
;
553 } else if (request
->state
== RUN_MOVE
) {
554 if (request
->curl_result
== CURLE_OK
) {
556 fprintf(stderr
, " sent %s\n",
557 sha1_to_hex(request
->obj
->sha1
));
558 request
->obj
->flags
|= REMOTE
;
559 release_request(request
);
561 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
562 sha1_to_hex(request
->obj
->sha1
),
563 request
->curl_result
, request
->http_code
);
564 request
->state
= ABORTED
;
567 } else if (request
->state
== RUN_FETCH_LOOSE
) {
568 obj_req
= (struct http_object_request
*)request
->userData
;
570 if (finish_http_object_request(obj_req
) == 0)
571 if (obj_req
->rename
== 0)
572 request
->obj
->flags
|= (LOCAL
| REMOTE
);
574 /* Try fetching packed if necessary */
575 if (request
->obj
->flags
& LOCAL
) {
576 release_http_object_request(obj_req
);
577 release_request(request
);
579 start_fetch_packed(request
);
581 } else if (request
->state
== RUN_FETCH_PACKED
) {
583 if (request
->curl_result
!= CURLE_OK
) {
584 fprintf(stderr
, "Unable to get pack file %s\n%s",
585 request
->url
, curl_errorstr
);
587 preq
= (struct http_pack_request
*)request
->userData
;
590 if (finish_http_pack_request(preq
) == 0)
592 release_http_pack_request(preq
);
596 repo
->can_update_info_refs
= 0;
597 release_request(request
);
601 #ifdef USE_CURL_MULTI
602 static int is_running_queue
;
603 static int fill_active_slot(void *unused
)
605 struct transfer_request
*request
;
607 if (aborted
|| !is_running_queue
)
610 for (request
= request_queue_head
; request
; request
= request
->next
) {
611 if (request
->state
== NEED_FETCH
) {
612 start_fetch_loose(request
);
614 } else if (pushing
&& request
->state
== NEED_PUSH
) {
615 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
618 start_mkcol(request
);
627 static void get_remote_object_list(unsigned char parent
);
629 static void add_fetch_request(struct object
*obj
)
631 struct transfer_request
*request
;
636 * Don't fetch the object if it's known to exist locally
637 * or is already in the request queue
639 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
640 get_remote_object_list(obj
->sha1
[0]);
641 if (obj
->flags
& (LOCAL
| FETCHING
))
644 obj
->flags
|= FETCHING
;
645 request
= xmalloc(sizeof(*request
));
648 request
->lock
= NULL
;
649 request
->headers
= NULL
;
650 request
->state
= NEED_FETCH
;
651 request
->next
= request_queue_head
;
652 request_queue_head
= request
;
654 #ifdef USE_CURL_MULTI
660 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
662 struct transfer_request
*request
= request_queue_head
;
663 struct packed_git
*target
;
665 /* Keep locks active */
669 * Don't push the object if it's known to exist on the remote
670 * or is already in the request queue
672 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
673 get_remote_object_list(obj
->sha1
[0]);
674 if (obj
->flags
& (REMOTE
| PUSHING
))
676 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
678 obj
->flags
|= REMOTE
;
682 obj
->flags
|= PUSHING
;
683 request
= xmalloc(sizeof(*request
));
686 request
->lock
= lock
;
687 request
->headers
= NULL
;
688 request
->state
= NEED_PUSH
;
689 request
->next
= request_queue_head
;
690 request_queue_head
= request
;
692 #ifdef USE_CURL_MULTI
700 static int fetch_indices(void)
705 fprintf(stderr
, "Getting pack list\n");
707 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
709 case HTTP_MISSING_TARGET
:
719 static void one_remote_object(const char *hex
)
721 unsigned char sha1
[20];
724 if (get_sha1_hex(hex
, sha1
) != 0)
727 obj
= lookup_object(sha1
);
729 obj
= parse_object(sha1
);
731 /* Ignore remote objects that don't exist locally */
735 obj
->flags
|= REMOTE
;
736 if (!object_list_contains(objects
, obj
))
737 object_list_insert(obj
, &objects
);
740 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
742 int *lock_flags
= (int *)ctx
->userData
;
745 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
746 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
747 (*lock_flags
& DAV_PROP_LOCKWR
)) {
748 *lock_flags
|= DAV_LOCK_OK
;
750 *lock_flags
&= DAV_LOCK_OK
;
751 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
752 *lock_flags
|= DAV_PROP_LOCKWR
;
753 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
754 *lock_flags
|= DAV_PROP_LOCKEX
;
759 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
761 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
763 unsigned char lock_token_sha1
[20];
765 if (tag_closed
&& ctx
->cdata
) {
766 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
767 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
768 strcpy(lock
->owner
, ctx
->cdata
);
769 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
770 if (!prefixcmp(ctx
->cdata
, "Second-"))
772 strtol(ctx
->cdata
+ 7, NULL
, 10);
773 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
774 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
775 strcpy(lock
->token
, ctx
->cdata
);
777 git_SHA1_Init(&sha_ctx
);
778 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
779 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
781 lock
->tmpfile_suffix
[0] = '_';
782 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
787 static void one_remote_ref(const char *refname
);
790 xml_start_tag(void *userData
, const char *name
, const char **atts
)
792 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
793 const char *c
= strchr(name
, ':');
801 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
803 if (new_len
> ctx
->len
) {
804 ctx
->name
= xrealloc(ctx
->name
, new_len
);
807 strcat(ctx
->name
, ".");
808 strcat(ctx
->name
, c
);
813 ctx
->userFunc(ctx
, 0);
817 xml_end_tag(void *userData
, const char *name
)
819 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
820 const char *c
= strchr(name
, ':');
823 ctx
->userFunc(ctx
, 1);
830 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
835 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
837 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
839 ctx
->cdata
= xmemdupz(s
, len
);
842 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
844 struct active_request_slot
*slot
;
845 struct slot_results results
;
846 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
847 struct strbuf in_buffer
= STRBUF_INIT
;
850 char timeout_header
[25];
851 struct remote_lock
*lock
= NULL
;
852 struct curl_slist
*dav_headers
= NULL
;
856 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
857 sprintf(url
, "%s%s", repo
->url
, path
);
859 /* Make sure leading directories exist for the remote ref */
860 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
862 char saved_character
= ep
[1];
864 slot
= get_active_slot();
865 slot
->results
= &results
;
866 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
867 if (start_active_slot(slot
)) {
868 run_active_slot(slot
);
869 if (results
.curl_result
!= CURLE_OK
&&
870 results
.http_code
!= 405) {
872 "Unable to create branch path %s\n",
878 fprintf(stderr
, "Unable to start MKCOL request\n");
882 ep
[1] = saved_character
;
883 ep
= strchr(ep
+ 1, '/');
886 escaped
= xml_entities(ident_default_email());
887 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
890 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
891 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
892 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
894 slot
= get_active_slot();
895 slot
->results
= &results
;
896 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
897 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
898 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
900 lock
= xcalloc(1, sizeof(*lock
));
903 if (start_active_slot(slot
)) {
904 run_active_slot(slot
);
905 if (results
.curl_result
== CURLE_OK
) {
906 XML_Parser parser
= XML_ParserCreate(NULL
);
907 enum XML_Status result
;
908 ctx
.name
= xcalloc(10, 1);
911 ctx
.userFunc
= handle_new_lock_ctx
;
913 XML_SetUserData(parser
, &ctx
);
914 XML_SetElementHandler(parser
, xml_start_tag
,
916 XML_SetCharacterDataHandler(parser
, xml_cdata
);
917 result
= XML_Parse(parser
, in_buffer
.buf
,
920 if (result
!= XML_STATUS_OK
) {
921 fprintf(stderr
, "XML error: %s\n",
923 XML_GetErrorCode(parser
)));
926 XML_ParserFree(parser
);
929 fprintf(stderr
, "Unable to start LOCK request\n");
932 curl_slist_free_all(dav_headers
);
933 strbuf_release(&out_buffer
.buf
);
934 strbuf_release(&in_buffer
);
936 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
944 lock
->start_time
= time(NULL
);
945 lock
->next
= repo
->locks
;
952 static int unlock_remote(struct remote_lock
*lock
)
954 struct active_request_slot
*slot
;
955 struct slot_results results
;
956 struct remote_lock
*prev
= repo
->locks
;
957 struct curl_slist
*dav_headers
;
960 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
962 slot
= get_active_slot();
963 slot
->results
= &results
;
964 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
965 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
967 if (start_active_slot(slot
)) {
968 run_active_slot(slot
);
969 if (results
.curl_result
== CURLE_OK
)
972 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
975 fprintf(stderr
, "Unable to start UNLOCK request\n");
978 curl_slist_free_all(dav_headers
);
980 if (repo
->locks
== lock
) {
981 repo
->locks
= lock
->next
;
983 while (prev
&& prev
->next
!= lock
)
986 prev
->next
= prev
->next
->next
;
997 static void remove_locks(void)
999 struct remote_lock
*lock
= repo
->locks
;
1001 fprintf(stderr
, "Removing remote locks...\n");
1003 struct remote_lock
*next
= lock
->next
;
1004 unlock_remote(lock
);
1009 static void remove_locks_on_signal(int signo
)
1012 sigchain_pop(signo
);
1016 static void remote_ls(const char *path
, int flags
,
1017 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1020 static void process_ls_object(struct remote_ls_ctx
*ls
)
1022 unsigned int *parent
= (unsigned int *)ls
->userData
;
1023 char *path
= ls
->dentry_name
;
1026 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1027 remote_dir_exists
[*parent
] = 1;
1031 if (strlen(path
) != 49)
1034 obj_hex
= xmalloc(strlen(path
));
1035 /* NB: path is not null-terminated, can not use strlcpy here */
1036 memcpy(obj_hex
, path
, 2);
1037 strcpy(obj_hex
+ 2, path
+ 3);
1038 one_remote_object(obj_hex
);
1042 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1044 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1045 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1049 if (!(ls
->dentry_flags
& IS_DIR
))
1050 one_remote_ref(ls
->dentry_name
);
1053 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1055 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1058 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1059 if (ls
->dentry_flags
& IS_DIR
) {
1061 /* ensure collection names end with slash */
1062 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1064 if (ls
->flags
& PROCESS_DIRS
) {
1067 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1068 ls
->flags
& RECURSIVE
) {
1069 remote_ls(ls
->dentry_name
,
1074 } else if (ls
->flags
& PROCESS_FILES
) {
1077 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1078 char *path
= ctx
->cdata
;
1079 if (*ctx
->cdata
== 'h') {
1080 path
= strstr(path
, "//");
1082 path
= strchr(path
+2, '/');
1086 const char *url
= repo
->url
;
1089 if (strncmp(path
, url
, repo
->path_len
))
1090 error("Parsed path '%s' does not match url: '%s'",
1093 path
+= repo
->path_len
;
1094 ls
->dentry_name
= xstrdup(path
);
1097 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1098 ls
->dentry_flags
|= IS_DIR
;
1100 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1101 free(ls
->dentry_name
);
1102 ls
->dentry_name
= NULL
;
1103 ls
->dentry_flags
= 0;
1108 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1109 * should _only_ heed the information from that file, instead of trying to
1110 * determine the refs from the remote file system (badly: it does not even
1111 * know about packed-refs).
1113 static void remote_ls(const char *path
, int flags
,
1114 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1117 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1118 struct active_request_slot
*slot
;
1119 struct slot_results results
;
1120 struct strbuf in_buffer
= STRBUF_INIT
;
1121 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1122 struct curl_slist
*dav_headers
= NULL
;
1124 struct remote_ls_ctx ls
;
1127 ls
.path
= xstrdup(path
);
1128 ls
.dentry_name
= NULL
;
1129 ls
.dentry_flags
= 0;
1130 ls
.userData
= userData
;
1131 ls
.userFunc
= userFunc
;
1133 sprintf(url
, "%s%s", repo
->url
, path
);
1135 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1137 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1138 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1140 slot
= get_active_slot();
1141 slot
->results
= &results
;
1142 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1143 &out_buffer
, fwrite_buffer
);
1144 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1145 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1147 if (start_active_slot(slot
)) {
1148 run_active_slot(slot
);
1149 if (results
.curl_result
== CURLE_OK
) {
1150 XML_Parser parser
= XML_ParserCreate(NULL
);
1151 enum XML_Status result
;
1152 ctx
.name
= xcalloc(10, 1);
1155 ctx
.userFunc
= handle_remote_ls_ctx
;
1157 XML_SetUserData(parser
, &ctx
);
1158 XML_SetElementHandler(parser
, xml_start_tag
,
1160 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1161 result
= XML_Parse(parser
, in_buffer
.buf
,
1165 if (result
!= XML_STATUS_OK
) {
1166 fprintf(stderr
, "XML error: %s\n",
1168 XML_GetErrorCode(parser
)));
1170 XML_ParserFree(parser
);
1173 fprintf(stderr
, "Unable to start PROPFIND request\n");
1178 strbuf_release(&out_buffer
.buf
);
1179 strbuf_release(&in_buffer
);
1180 curl_slist_free_all(dav_headers
);
1183 static void get_remote_object_list(unsigned char parent
)
1185 char path
[] = "objects/XX/";
1186 static const char hex
[] = "0123456789abcdef";
1187 unsigned int val
= parent
;
1189 path
[8] = hex
[val
>> 4];
1190 path
[9] = hex
[val
& 0xf];
1191 remote_dir_exists
[val
] = 0;
1192 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1193 process_ls_object
, &val
);
1196 static int locking_available(void)
1198 struct active_request_slot
*slot
;
1199 struct slot_results results
;
1200 struct strbuf in_buffer
= STRBUF_INIT
;
1201 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1202 struct curl_slist
*dav_headers
= NULL
;
1207 escaped
= xml_entities(repo
->url
);
1208 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1211 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1212 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1214 slot
= get_active_slot();
1215 slot
->results
= &results
;
1216 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1217 &out_buffer
, fwrite_buffer
);
1218 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1219 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1221 if (start_active_slot(slot
)) {
1222 run_active_slot(slot
);
1223 if (results
.curl_result
== CURLE_OK
) {
1224 XML_Parser parser
= XML_ParserCreate(NULL
);
1225 enum XML_Status result
;
1226 ctx
.name
= xcalloc(10, 1);
1229 ctx
.userFunc
= handle_lockprop_ctx
;
1230 ctx
.userData
= &lock_flags
;
1231 XML_SetUserData(parser
, &ctx
);
1232 XML_SetElementHandler(parser
, xml_start_tag
,
1234 result
= XML_Parse(parser
, in_buffer
.buf
,
1238 if (result
!= XML_STATUS_OK
) {
1239 fprintf(stderr
, "XML error: %s\n",
1241 XML_GetErrorCode(parser
)));
1244 XML_ParserFree(parser
);
1246 error("no DAV locking support on %s",
1250 error("Cannot access URL %s, return code %d",
1251 repo
->url
, results
.curl_result
);
1255 error("Unable to start PROPFIND request on %s", repo
->url
);
1258 strbuf_release(&out_buffer
.buf
);
1259 strbuf_release(&in_buffer
);
1260 curl_slist_free_all(dav_headers
);
1265 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1267 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1271 return &entry
->next
;
1274 static struct object_list
**process_blob(struct blob
*blob
,
1275 struct object_list
**p
,
1276 struct name_path
*path
,
1279 struct object
*obj
= &blob
->object
;
1281 obj
->flags
|= LOCAL
;
1283 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1287 return add_one_object(obj
, p
);
1290 static struct object_list
**process_tree(struct tree
*tree
,
1291 struct object_list
**p
,
1292 struct name_path
*path
,
1295 struct object
*obj
= &tree
->object
;
1296 struct tree_desc desc
;
1297 struct name_entry entry
;
1298 struct name_path me
;
1300 obj
->flags
|= LOCAL
;
1302 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1304 if (parse_tree(tree
) < 0)
1305 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1308 name
= xstrdup(name
);
1309 p
= add_one_object(obj
, p
);
1312 me
.elem_len
= strlen(name
);
1314 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1316 while (tree_entry(&desc
, &entry
))
1317 switch (object_type(entry
.mode
)) {
1319 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1322 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1325 /* Subproject commit - not in this repository */
1330 tree
->buffer
= NULL
;
1334 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1337 struct commit
*commit
;
1338 struct object_list
**p
= &objects
;
1341 while ((commit
= get_revision(revs
)) != NULL
) {
1342 p
= process_tree(commit
->tree
, p
, NULL
, "");
1343 commit
->object
.flags
|= LOCAL
;
1344 if (!(commit
->object
.flags
& UNINTERESTING
))
1345 count
+= add_send_request(&commit
->object
, lock
);
1348 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1349 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1350 struct object
*obj
= entry
->item
;
1351 const char *name
= entry
->name
;
1353 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1355 if (obj
->type
== OBJ_TAG
) {
1357 p
= add_one_object(obj
, p
);
1360 if (obj
->type
== OBJ_TREE
) {
1361 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1364 if (obj
->type
== OBJ_BLOB
) {
1365 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1368 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1372 if (!(objects
->item
->flags
& UNINTERESTING
))
1373 count
+= add_send_request(objects
->item
, lock
);
1374 objects
= objects
->next
;
1380 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1382 struct active_request_slot
*slot
;
1383 struct slot_results results
;
1384 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1385 struct curl_slist
*dav_headers
;
1387 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1389 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1391 slot
= get_active_slot();
1392 slot
->results
= &results
;
1393 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1394 &out_buffer
, fwrite_null
);
1395 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1397 if (start_active_slot(slot
)) {
1398 run_active_slot(slot
);
1399 strbuf_release(&out_buffer
.buf
);
1400 if (results
.curl_result
!= CURLE_OK
) {
1402 "PUT error: curl result=%d, HTTP code=%ld\n",
1403 results
.curl_result
, results
.http_code
);
1404 /* We should attempt recovery? */
1408 strbuf_release(&out_buffer
.buf
);
1409 fprintf(stderr
, "Unable to start PUT request\n");
1416 static struct ref
*remote_refs
;
1418 static void one_remote_ref(const char *refname
)
1423 ref
= alloc_ref(refname
);
1425 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1427 "Unable to fetch ref %s from %s\n",
1428 refname
, repo
->url
);
1434 * Fetch a copy of the object if it doesn't exist locally - it
1435 * may be required for updating server info later.
1437 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1438 obj
= lookup_unknown_object(ref
->old_sha1
);
1440 fprintf(stderr
, " fetch %s for %s\n",
1441 sha1_to_hex(ref
->old_sha1
), refname
);
1442 add_fetch_request(obj
);
1446 ref
->next
= remote_refs
;
1450 static void get_dav_remote_heads(void)
1452 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1455 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1457 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1463 ref
= alloc_ref(ls
->dentry_name
);
1465 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1467 "Unable to fetch ref %s from %s\n",
1468 ls
->dentry_name
, repo
->url
);
1474 o
= parse_object(ref
->old_sha1
);
1477 "Unable to parse object %s for remote ref %s\n",
1478 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1484 len
= strlen(ls
->dentry_name
) + 42;
1485 ref_info
= xcalloc(len
+ 1, 1);
1486 sprintf(ref_info
, "%s %s\n",
1487 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1488 fwrite_buffer(ref_info
, 1, len
, buf
);
1491 if (o
->type
== OBJ_TAG
) {
1492 o
= deref_tag(o
, ls
->dentry_name
, 0);
1494 len
= strlen(ls
->dentry_name
) + 45;
1495 ref_info
= xcalloc(len
+ 1, 1);
1496 sprintf(ref_info
, "%s %s^{}\n",
1497 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1498 fwrite_buffer(ref_info
, 1, len
, buf
);
1505 static void update_remote_info_refs(struct remote_lock
*lock
)
1507 struct buffer buffer
= { STRBUF_INIT
, 0 };
1508 struct active_request_slot
*slot
;
1509 struct slot_results results
;
1510 struct curl_slist
*dav_headers
;
1512 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1513 add_remote_info_ref
, &buffer
.buf
);
1515 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1517 slot
= get_active_slot();
1518 slot
->results
= &results
;
1519 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1520 &buffer
, fwrite_null
);
1521 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1523 if (start_active_slot(slot
)) {
1524 run_active_slot(slot
);
1525 if (results
.curl_result
!= CURLE_OK
) {
1527 "PUT error: curl result=%d, HTTP code=%ld\n",
1528 results
.curl_result
, results
.http_code
);
1532 strbuf_release(&buffer
.buf
);
1535 static int remote_exists(const char *path
)
1537 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1540 sprintf(url
, "%s%s", repo
->url
, path
);
1542 switch (http_get_strbuf(url
, NULL
, 0)) {
1546 case HTTP_MISSING_TARGET
:
1550 http_error(url
, HTTP_ERROR
);
1558 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1561 struct strbuf buffer
= STRBUF_INIT
;
1563 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1564 sprintf(url
, "%s%s", repo
->url
, path
);
1566 if (http_get_strbuf(url
, &buffer
, 0) != HTTP_OK
)
1567 die("Couldn't get %s for remote symref\n%s", url
,
1575 if (buffer
.len
== 0)
1578 /* If it's a symref, set the refname; otherwise try for a sha1 */
1579 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
1580 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
1582 get_sha1_hex(buffer
.buf
, sha1
);
1585 strbuf_release(&buffer
);
1588 static int verify_merge_base(unsigned char *head_sha1
, struct ref
*remote
)
1590 struct commit
*head
= lookup_commit_or_die(head_sha1
, "HEAD");
1591 struct commit
*branch
= lookup_commit_or_die(remote
->old_sha1
, remote
->name
);
1593 return in_merge_bases(branch
, head
);
1596 static int delete_remote_branch(const char *pattern
, int force
)
1598 struct ref
*refs
= remote_refs
;
1599 struct ref
*remote_ref
= NULL
;
1600 unsigned char head_sha1
[20];
1601 char *symref
= NULL
;
1603 int patlen
= strlen(pattern
);
1605 struct active_request_slot
*slot
;
1606 struct slot_results results
;
1609 /* Find the remote branch(es) matching the specified branch name */
1610 for (match
= 0; refs
; refs
= refs
->next
) {
1611 char *name
= refs
->name
;
1612 int namelen
= strlen(name
);
1613 if (namelen
< patlen
||
1614 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1616 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1622 return error("No remote branch matches %s", pattern
);
1624 return error("More than one remote branch matches %s",
1628 * Remote HEAD must be a symref (not exactly foolproof; a remote
1629 * symlink to a symref will look like a symref)
1631 fetch_symref("HEAD", &symref
, head_sha1
);
1633 return error("Remote HEAD is not a symref");
1635 /* Remote branch must not be the remote HEAD */
1636 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1637 if (!strcmp(remote_ref
->name
, symref
))
1638 return error("Remote branch %s is the current HEAD",
1640 fetch_symref(symref
, &symref
, head_sha1
);
1643 /* Run extra sanity checks if delete is not forced */
1645 /* Remote HEAD must resolve to a known object */
1647 return error("Remote HEAD symrefs too deep");
1648 if (is_null_sha1(head_sha1
))
1649 return error("Unable to resolve remote HEAD");
1650 if (!has_sha1_file(head_sha1
))
1651 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1653 /* Remote branch must resolve to a known object */
1654 if (is_null_sha1(remote_ref
->old_sha1
))
1655 return error("Unable to resolve remote branch %s",
1657 if (!has_sha1_file(remote_ref
->old_sha1
))
1658 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
));
1660 /* Remote branch must be an ancestor of remote HEAD */
1661 if (!verify_merge_base(head_sha1
, remote_ref
)) {
1662 return error("The branch '%s' is not an ancestor "
1663 "of your current HEAD.\n"
1664 "If you are sure you want to delete it,"
1665 " run:\n\t'git http-push -D %s %s'",
1666 remote_ref
->name
, repo
->url
, pattern
);
1670 /* Send delete request */
1671 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1674 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
1675 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
1676 slot
= get_active_slot();
1677 slot
->results
= &results
;
1678 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1679 if (start_active_slot(slot
)) {
1680 run_active_slot(slot
);
1682 if (results
.curl_result
!= CURLE_OK
)
1683 return error("DELETE request failed (%d/%ld)",
1684 results
.curl_result
, results
.http_code
);
1687 return error("Unable to start DELETE request");
1693 static void run_request_queue(void)
1695 #ifdef USE_CURL_MULTI
1696 is_running_queue
= 1;
1697 fill_active_slots();
1698 add_fill_function(NULL
, fill_active_slot
);
1701 finish_all_active_slots();
1702 #ifdef USE_CURL_MULTI
1703 fill_active_slots();
1705 } while (request_queue_head
&& !aborted
);
1707 #ifdef USE_CURL_MULTI
1708 is_running_queue
= 0;
1712 int main(int argc
, char **argv
)
1714 struct transfer_request
*request
;
1715 struct transfer_request
*next_request
;
1717 char **refspec
= NULL
;
1718 struct remote_lock
*ref_lock
= NULL
;
1719 struct remote_lock
*info_ref_lock
= NULL
;
1720 struct rev_info revs
;
1721 int delete_branch
= 0;
1722 int force_delete
= 0;
1723 int objects_to_send
;
1727 struct ref
*ref
, *local_refs
;
1729 git_setup_gettext();
1731 git_extract_argv0_path(argv
[0]);
1733 repo
= xcalloc(sizeof(*repo
), 1);
1736 for (i
= 1; i
< argc
; i
++, argv
++) {
1740 if (!strcmp(arg
, "--all")) {
1741 push_all
= MATCH_REFS_ALL
;
1744 if (!strcmp(arg
, "--force")) {
1748 if (!strcmp(arg
, "--dry-run")) {
1752 if (!strcmp(arg
, "--helper-status")) {
1756 if (!strcmp(arg
, "--verbose")) {
1758 http_is_verbose
= 1;
1761 if (!strcmp(arg
, "-d")) {
1765 if (!strcmp(arg
, "-D")) {
1770 if (!strcmp(arg
, "-h"))
1771 usage(http_push_usage
);
1774 char *path
= strstr(arg
, "//");
1775 str_end_url_with_slash(arg
, &repo
->url
);
1776 repo
->path_len
= strlen(repo
->url
);
1778 repo
->path
= strchr(path
+2, '/');
1780 repo
->path_len
= strlen(repo
->path
);
1785 nr_refspec
= argc
- i
;
1789 #ifndef USE_CURL_MULTI
1790 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1794 usage(http_push_usage
);
1796 if (delete_branch
&& nr_refspec
!= 1)
1797 die("You must specify only one branch name when deleting a remote branch");
1799 setup_git_directory();
1801 memset(remote_dir_exists
, -1, 256);
1803 http_init(NULL
, repo
->url
, 1);
1805 #ifdef USE_CURL_MULTI
1806 is_running_queue
= 0;
1809 /* Verify DAV compliance/lock support */
1810 if (!locking_available()) {
1815 sigchain_push_common(remove_locks_on_signal
);
1817 /* Check whether the remote has server info files */
1818 repo
->can_update_info_refs
= 0;
1819 repo
->has_info_refs
= remote_exists("info/refs");
1820 repo
->has_info_packs
= remote_exists("objects/info/packs");
1821 if (repo
->has_info_refs
) {
1822 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1824 repo
->can_update_info_refs
= 1;
1826 error("cannot lock existing info/refs");
1831 if (repo
->has_info_packs
)
1834 /* Get a list of all local and remote heads to validate refspecs */
1835 local_refs
= get_local_heads();
1836 fprintf(stderr
, "Fetching remote heads...\n");
1837 get_dav_remote_heads();
1838 run_request_queue();
1840 /* Remove a remote branch if -d or -D was specified */
1841 if (delete_branch
) {
1842 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1843 fprintf(stderr
, "Unable to delete remote branch %s\n",
1846 printf("error %s cannot remove\n", refspec
[0]);
1852 if (match_push_refs(local_refs
, &remote_refs
,
1853 nr_refspec
, (const char **) refspec
, push_all
)) {
1858 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1860 printf("error null no match\n");
1866 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1867 char old_hex
[60], *new_hex
;
1868 const char *commit_argv
[5];
1870 char *new_sha1_hex
, *old_sha1_hex
;
1875 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1876 if (delete_remote_branch(ref
->name
, 1) == -1) {
1877 error("Could not remove %s", ref
->name
);
1879 printf("error %s cannot remove\n", ref
->name
);
1882 else if (helper_status
)
1883 printf("ok %s\n", ref
->name
);
1888 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1890 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1892 printf("ok %s up to date\n", ref
->name
);
1897 !is_null_sha1(ref
->old_sha1
) &&
1899 if (!has_sha1_file(ref
->old_sha1
) ||
1900 !ref_newer(ref
->peer_ref
->new_sha1
,
1903 * We do not have the remote ref, or
1904 * we know that the remote ref is not
1905 * an ancestor of what we are trying to
1906 * push. Either way this can be losing
1907 * commits at the remote end and likely
1908 * we were not up to date to begin with.
1910 error("remote '%s' is not an ancestor of\n"
1912 "Maybe you are not up-to-date and "
1913 "need to pull first?",
1915 ref
->peer_ref
->name
);
1917 printf("error %s non-fast forward\n", ref
->name
);
1922 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1924 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1925 new_hex
= sha1_to_hex(ref
->new_sha1
);
1927 fprintf(stderr
, "updating '%s'", ref
->name
);
1928 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1929 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1930 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
1933 printf("ok %s\n", ref
->name
);
1937 /* Lock remote branch ref */
1938 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1939 if (ref_lock
== NULL
) {
1940 fprintf(stderr
, "Unable to lock remote branch %s\n",
1943 printf("error %s lock error\n", ref
->name
);
1948 /* Set up revision info for this refspec */
1950 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
1951 old_sha1_hex
= NULL
;
1952 commit_argv
[1] = "--objects";
1953 commit_argv
[2] = new_sha1_hex
;
1954 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
1955 old_sha1_hex
= xmalloc(42);
1956 sprintf(old_sha1_hex
, "^%s",
1957 sha1_to_hex(ref
->old_sha1
));
1958 commit_argv
[3] = old_sha1_hex
;
1961 commit_argv
[commit_argc
] = NULL
;
1962 init_revisions(&revs
, setup_git_directory());
1963 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
1964 revs
.edge_hint
= 0; /* just in case */
1968 commit_argv
[1] = NULL
;
1971 /* Generate a list of objects that need to be pushed */
1973 if (prepare_revision_walk(&revs
))
1974 die("revision walk setup failed");
1975 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
1976 objects_to_send
= get_delta(&revs
, ref_lock
);
1977 finish_all_active_slots();
1979 /* Push missing objects to remote, this would be a
1980 convenient time to pack them first if appropriate. */
1982 if (objects_to_send
)
1983 fprintf(stderr
, " sending %d objects\n",
1986 run_request_queue();
1988 /* Update the remote branch if all went well */
1989 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
1993 fprintf(stderr
, " done\n");
1995 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1996 unlock_remote(ref_lock
);
2000 /* Update remote server info if appropriate */
2001 if (repo
->has_info_refs
&& new_refs
) {
2002 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2003 fprintf(stderr
, "Updating remote server info\n");
2005 update_remote_info_refs(info_ref_lock
);
2007 fprintf(stderr
, "Unable to update server info\n");
2013 unlock_remote(info_ref_lock
);
2018 request
= request_queue_head
;
2019 while (request
!= NULL
) {
2020 next_request
= request
->next
;
2021 release_request(request
);
2022 request
= next_request
;