1 #include "git-compat-util.h"
2 #include "environment.h"
4 #include "repository.h"
14 #include "list-objects.h"
19 #include "tree-walk.h"
21 #include "object-store-ll.h"
22 #include "commit-reach.h"
24 #ifdef EXPAT_NEEDS_XMLPARSE_H
30 static const char http_push_usage
[] =
31 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
38 #define XML_STATUS_OK 1
39 #define XML_STATUS_ERROR 0
42 #define PREV_BUF_SIZE 4096
45 #define DAV_LOCK "LOCK"
46 #define DAV_MKCOL "MKCOL"
47 #define DAV_MOVE "MOVE"
48 #define DAV_PROPFIND "PROPFIND"
50 #define DAV_UNLOCK "UNLOCK"
51 #define DAV_DELETE "DELETE"
54 #define DAV_PROP_LOCKWR (1u << 0)
55 #define DAV_PROP_LOCKEX (1u << 1)
56 #define DAV_LOCK_OK (1u << 2)
58 /* DAV XML properties */
59 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
60 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
61 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
62 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
63 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
64 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
65 #define DAV_PROPFIND_RESP ".multistatus.response"
66 #define DAV_PROPFIND_NAME ".multistatus.response.href"
67 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
69 /* DAV request body templates */
70 #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>"
71 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
72 #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>"
75 #define LOCK_REFRESH 30
77 /* Remember to update object flag allocation in object.h */
78 #define LOCAL (1u<<11)
79 #define REMOTE (1u<<12)
80 #define FETCHING (1u<<13)
81 #define PUSHING (1u<<14)
83 /* We allow "recursive" symbolic refs. Only within reason, though */
88 static signed char remote_dir_exists
[256];
90 static int push_verbosely
;
91 static int push_all
= MATCH_REFS_NONE
;
94 static int helper_status
;
96 static struct object_list
*objects
;
103 int can_update_info_refs
;
105 struct packed_git
*packs
;
106 struct remote_lock
*locks
;
109 static struct repo
*repo
;
111 enum transfer_state
{
123 struct transfer_request
{
125 struct packed_git
*target
;
128 struct remote_lock
*lock
;
129 struct curl_slist
*headers
;
130 struct buffer buffer
;
131 enum transfer_state state
;
132 CURLcode curl_result
;
133 char errorstr
[CURL_ERROR_SIZE
];
136 struct active_request_slot
*slot
;
137 struct transfer_request
*next
;
140 static struct transfer_request
*request_queue_head
;
146 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
154 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
158 struct remote_lock
*next
;
161 /* Flags that control remote_ls processing */
162 #define PROCESS_FILES (1u << 0)
163 #define PROCESS_DIRS (1u << 1)
164 #define RECURSIVE (1u << 2)
166 /* Flags that remote_ls passes to callback functions */
167 #define IS_DIR (1u << 0)
169 struct remote_ls_ctx
{
171 void (*userFunc
)(struct remote_ls_ctx
*ls
);
176 struct remote_ls_ctx
*parent
;
179 /* get_dav_token_headers options */
180 enum dav_header_flag
{
181 DAV_HEADER_IF
= (1u << 0),
182 DAV_HEADER_LOCK
= (1u << 1),
183 DAV_HEADER_TIMEOUT
= (1u << 2)
186 static char *xml_entities(const char *s
)
188 struct strbuf buf
= STRBUF_INIT
;
189 strbuf_addstr_xml_quoted(&buf
, s
);
190 return strbuf_detach(&buf
, NULL
);
193 static void curl_setup_http_get(CURL
*curl
, const char *url
,
194 const char *custom_req
)
196 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
197 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
198 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
199 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
202 static void curl_setup_http(CURL
*curl
, const char *url
,
203 const char *custom_req
, struct buffer
*buffer
,
204 curl_write_callback write_fn
)
206 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
207 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
208 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
209 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
210 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
211 curl_easy_setopt(curl
, CURLOPT_SEEKFUNCTION
, seek_buffer
);
212 curl_easy_setopt(curl
, CURLOPT_SEEKDATA
, buffer
);
213 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
214 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
215 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
216 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
219 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
221 struct strbuf buf
= STRBUF_INIT
;
222 struct curl_slist
*dav_headers
= http_copy_default_headers();
224 if (options
& DAV_HEADER_IF
) {
225 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
226 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
229 if (options
& DAV_HEADER_LOCK
) {
230 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
231 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
234 if (options
& DAV_HEADER_TIMEOUT
) {
235 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
236 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
239 strbuf_release(&buf
);
244 static void finish_request(struct transfer_request
*request
);
245 static void release_request(struct transfer_request
*request
);
247 static void process_response(void *callback_data
)
249 struct transfer_request
*request
=
250 (struct transfer_request
*)callback_data
;
252 finish_request(request
);
255 static void start_fetch_loose(struct transfer_request
*request
)
257 struct active_request_slot
*slot
;
258 struct http_object_request
*obj_req
;
260 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
262 request
->state
= ABORTED
;
266 slot
= obj_req
->slot
;
267 slot
->callback_func
= process_response
;
268 slot
->callback_data
= request
;
269 request
->slot
= slot
;
270 request
->userData
= obj_req
;
272 /* Try to get the request started, abort the request on error */
273 request
->state
= RUN_FETCH_LOOSE
;
274 if (!start_active_slot(slot
)) {
275 fprintf(stderr
, "Unable to start GET request\n");
276 repo
->can_update_info_refs
= 0;
277 release_http_object_request(obj_req
);
278 release_request(request
);
282 static void start_mkcol(struct transfer_request
*request
)
284 char *hex
= oid_to_hex(&request
->obj
->oid
);
285 struct active_request_slot
*slot
;
287 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
289 slot
= get_active_slot();
290 slot
->callback_func
= process_response
;
291 slot
->callback_data
= request
;
292 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
293 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
295 if (start_active_slot(slot
)) {
296 request
->slot
= slot
;
297 request
->state
= RUN_MKCOL
;
299 request
->state
= ABORTED
;
300 FREE_AND_NULL(request
->url
);
304 static void start_fetch_packed(struct transfer_request
*request
)
306 struct packed_git
*target
;
308 struct transfer_request
*check_request
= request_queue_head
;
309 struct http_pack_request
*preq
;
311 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
313 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
314 repo
->can_update_info_refs
= 0;
315 release_request(request
);
318 close_pack_index(target
);
319 request
->target
= target
;
321 fprintf(stderr
, "Fetching pack %s\n",
322 hash_to_hex(target
->hash
));
323 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
325 preq
= new_http_pack_request(target
->hash
, repo
->url
);
327 repo
->can_update_info_refs
= 0;
331 /* Make sure there isn't another open request for this pack */
332 while (check_request
) {
333 if (check_request
->state
== RUN_FETCH_PACKED
&&
334 !strcmp(check_request
->url
, preq
->url
)) {
335 release_http_pack_request(preq
);
336 release_request(request
);
339 check_request
= check_request
->next
;
342 preq
->slot
->callback_func
= process_response
;
343 preq
->slot
->callback_data
= request
;
344 request
->slot
= preq
->slot
;
345 request
->userData
= preq
;
347 /* Try to get the request started, abort the request on error */
348 request
->state
= RUN_FETCH_PACKED
;
349 if (!start_active_slot(preq
->slot
)) {
350 fprintf(stderr
, "Unable to start GET request\n");
351 release_http_pack_request(preq
);
352 repo
->can_update_info_refs
= 0;
353 release_request(request
);
357 static void start_put(struct transfer_request
*request
)
359 char *hex
= oid_to_hex(&request
->obj
->oid
);
360 struct active_request_slot
*slot
;
361 struct strbuf buf
= STRBUF_INIT
;
362 enum object_type type
;
370 unpacked
= repo_read_object_file(the_repository
, &request
->obj
->oid
,
372 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, len
);
375 git_deflate_init(&stream
, zlib_compression_level
);
376 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
377 strbuf_init(&request
->buffer
.buf
, size
);
378 request
->buffer
.posn
= 0;
381 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
382 stream
.avail_out
= size
;
385 stream
.next_in
= (void *)hdr
;
386 stream
.avail_in
= hdrlen
;
387 while (git_deflate(&stream
, 0) == Z_OK
)
390 /* Then the data itself.. */
391 stream
.next_in
= unpacked
;
392 stream
.avail_in
= len
;
393 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
395 git_deflate_end(&stream
);
398 request
->buffer
.buf
.len
= stream
.total_out
;
400 strbuf_addstr(&buf
, "Destination: ");
401 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
402 request
->dest
= strbuf_detach(&buf
, NULL
);
404 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
405 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
406 request
->url
= strbuf_detach(&buf
, NULL
);
408 slot
= get_active_slot();
409 slot
->callback_func
= process_response
;
410 slot
->callback_data
= request
;
411 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
412 &request
->buffer
, fwrite_null
);
414 if (start_active_slot(slot
)) {
415 request
->slot
= slot
;
416 request
->state
= RUN_PUT
;
418 request
->state
= ABORTED
;
419 FREE_AND_NULL(request
->url
);
423 static void start_move(struct transfer_request
*request
)
425 struct active_request_slot
*slot
;
426 struct curl_slist
*dav_headers
= http_copy_default_headers();
428 slot
= get_active_slot();
429 slot
->callback_func
= process_response
;
430 slot
->callback_data
= request
;
431 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
432 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
433 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
434 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
436 if (start_active_slot(slot
)) {
437 request
->slot
= slot
;
438 request
->state
= RUN_MOVE
;
440 request
->state
= ABORTED
;
441 FREE_AND_NULL(request
->url
);
445 static int refresh_lock(struct remote_lock
*lock
)
447 struct active_request_slot
*slot
;
448 struct slot_results results
;
449 struct curl_slist
*dav_headers
;
452 lock
->refreshing
= 1;
454 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
456 slot
= get_active_slot();
457 slot
->results
= &results
;
458 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
459 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
461 if (start_active_slot(slot
)) {
462 run_active_slot(slot
);
463 if (results
.curl_result
!= CURLE_OK
) {
464 fprintf(stderr
, "LOCK HTTP error %ld\n",
467 lock
->start_time
= time(NULL
);
472 lock
->refreshing
= 0;
473 curl_slist_free_all(dav_headers
);
478 static void check_locks(void)
480 struct remote_lock
*lock
= repo
->locks
;
481 time_t current_time
= time(NULL
);
485 time_remaining
= lock
->start_time
+ lock
->timeout
-
487 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
488 if (!refresh_lock(lock
)) {
490 "Unable to refresh lock for %s\n",
500 static void release_request(struct transfer_request
*request
)
502 struct transfer_request
*entry
= request_queue_head
;
504 if (request
== request_queue_head
) {
505 request_queue_head
= request
->next
;
507 while (entry
&& entry
->next
!= request
)
510 entry
->next
= request
->next
;
517 static void finish_request(struct transfer_request
*request
)
519 struct http_pack_request
*preq
;
520 struct http_object_request
*obj_req
;
522 request
->curl_result
= request
->slot
->curl_result
;
523 request
->http_code
= request
->slot
->http_code
;
524 request
->slot
= NULL
;
526 /* Keep locks active */
529 if (request
->headers
)
530 curl_slist_free_all(request
->headers
);
532 /* URL is reused for MOVE after PUT and used during FETCH */
533 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
534 FREE_AND_NULL(request
->url
);
537 if (request
->state
== RUN_MKCOL
) {
538 if (request
->curl_result
== CURLE_OK
||
539 request
->http_code
== 405) {
540 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
543 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
544 oid_to_hex(&request
->obj
->oid
),
545 request
->curl_result
, request
->http_code
);
546 request
->state
= ABORTED
;
549 } else if (request
->state
== RUN_PUT
) {
550 if (request
->curl_result
== CURLE_OK
) {
553 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
554 oid_to_hex(&request
->obj
->oid
),
555 request
->curl_result
, request
->http_code
);
556 request
->state
= ABORTED
;
559 } else if (request
->state
== RUN_MOVE
) {
560 if (request
->curl_result
== CURLE_OK
) {
562 fprintf(stderr
, " sent %s\n",
563 oid_to_hex(&request
->obj
->oid
));
564 request
->obj
->flags
|= REMOTE
;
565 release_request(request
);
567 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
568 oid_to_hex(&request
->obj
->oid
),
569 request
->curl_result
, request
->http_code
);
570 request
->state
= ABORTED
;
573 } else if (request
->state
== RUN_FETCH_LOOSE
) {
574 obj_req
= (struct http_object_request
*)request
->userData
;
576 if (finish_http_object_request(obj_req
) == 0)
577 if (obj_req
->rename
== 0)
578 request
->obj
->flags
|= (LOCAL
| REMOTE
);
580 /* Try fetching packed if necessary */
581 if (request
->obj
->flags
& LOCAL
) {
582 release_http_object_request(obj_req
);
583 release_request(request
);
585 start_fetch_packed(request
);
587 } else if (request
->state
== RUN_FETCH_PACKED
) {
589 if (request
->curl_result
!= CURLE_OK
) {
590 fprintf(stderr
, "Unable to get pack file %s\n%s",
591 request
->url
, curl_errorstr
);
593 preq
= (struct http_pack_request
*)request
->userData
;
596 if (finish_http_pack_request(preq
) == 0)
598 release_http_pack_request(preq
);
602 repo
->can_update_info_refs
= 0;
604 http_install_packfile(request
->target
, &repo
->packs
);
605 release_request(request
);
609 static int is_running_queue
;
610 static int fill_active_slot(void *data UNUSED
)
612 struct transfer_request
*request
;
614 if (aborted
|| !is_running_queue
)
617 for (request
= request_queue_head
; request
; request
= request
->next
) {
618 if (request
->state
== NEED_FETCH
) {
619 start_fetch_loose(request
);
621 } else if (pushing
&& request
->state
== NEED_PUSH
) {
622 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
625 start_mkcol(request
);
633 static void get_remote_object_list(unsigned char parent
);
635 static void add_fetch_request(struct object
*obj
)
637 struct transfer_request
*request
;
642 * Don't fetch the object if it's known to exist locally
643 * or is already in the request queue
645 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
646 get_remote_object_list(obj
->oid
.hash
[0]);
647 if (obj
->flags
& (LOCAL
| FETCHING
))
650 obj
->flags
|= FETCHING
;
651 request
= xmalloc(sizeof(*request
));
654 request
->lock
= NULL
;
655 request
->headers
= NULL
;
656 request
->state
= NEED_FETCH
;
657 request
->next
= request_queue_head
;
658 request_queue_head
= request
;
664 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
666 struct transfer_request
*request
;
667 struct packed_git
*target
;
669 /* Keep locks active */
673 * Don't push the object if it's known to exist on the remote
674 * or is already in the request queue
676 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
677 get_remote_object_list(obj
->oid
.hash
[0]);
678 if (obj
->flags
& (REMOTE
| PUSHING
))
680 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
682 obj
->flags
|= REMOTE
;
686 obj
->flags
|= PUSHING
;
687 request
= xmalloc(sizeof(*request
));
690 request
->lock
= lock
;
691 request
->headers
= NULL
;
692 request
->state
= NEED_PUSH
;
693 request
->next
= request_queue_head
;
694 request_queue_head
= request
;
702 static int fetch_indices(void)
707 fprintf(stderr
, "Getting pack list\n");
709 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
711 case HTTP_MISSING_TARGET
:
721 static void one_remote_object(const struct object_id
*oid
)
725 obj
= lookup_object(the_repository
, oid
);
727 obj
= parse_object(the_repository
, oid
);
729 /* Ignore remote objects that don't exist locally */
733 obj
->flags
|= REMOTE
;
734 if (!object_list_contains(objects
, obj
))
735 object_list_insert(obj
, &objects
);
738 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
740 int *lock_flags
= (int *)ctx
->userData
;
743 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
744 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
745 (*lock_flags
& DAV_PROP_LOCKWR
)) {
746 *lock_flags
|= DAV_LOCK_OK
;
748 *lock_flags
&= DAV_LOCK_OK
;
749 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
750 *lock_flags
|= DAV_PROP_LOCKWR
;
751 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
752 *lock_flags
|= DAV_PROP_LOCKEX
;
757 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
759 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
760 git_hash_ctx hash_ctx
;
761 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
763 if (tag_closed
&& ctx
->cdata
) {
764 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
765 lock
->owner
= xstrdup(ctx
->cdata
);
766 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
768 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
769 lock
->timeout
= strtol(arg
, NULL
, 10);
770 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
771 lock
->token
= xstrdup(ctx
->cdata
);
773 the_hash_algo
->init_fn(&hash_ctx
);
774 the_hash_algo
->update_fn(&hash_ctx
, lock
->token
, strlen(lock
->token
));
775 the_hash_algo
->final_fn(lock_token_hash
, &hash_ctx
);
777 lock
->tmpfile_suffix
[0] = '_';
778 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
783 static void one_remote_ref(const char *refname
);
786 xml_start_tag(void *userData
, const char *name
, const char **atts
)
788 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
789 const char *c
= strchr(name
, ':');
790 int old_namelen
, new_len
;
797 old_namelen
= strlen(ctx
->name
);
798 new_len
= old_namelen
+ strlen(c
) + 2;
800 if (new_len
> ctx
->len
) {
801 ctx
->name
= xrealloc(ctx
->name
, new_len
);
804 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
806 FREE_AND_NULL(ctx
->cdata
);
808 ctx
->userFunc(ctx
, 0);
812 xml_end_tag(void *userData
, const char *name
)
814 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
815 const char *c
= strchr(name
, ':');
818 ctx
->userFunc(ctx
, 1);
825 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
830 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
832 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
834 ctx
->cdata
= xmemdupz(s
, len
);
837 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
839 struct active_request_slot
*slot
;
840 struct slot_results results
;
841 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
842 struct strbuf in_buffer
= STRBUF_INIT
;
845 char timeout_header
[25];
846 struct remote_lock
*lock
= NULL
;
847 struct curl_slist
*dav_headers
= http_copy_default_headers();
851 url
= xstrfmt("%s%s", repo
->url
, path
);
853 /* Make sure leading directories exist for the remote ref */
854 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
856 char saved_character
= ep
[1];
858 slot
= get_active_slot();
859 slot
->results
= &results
;
860 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
861 if (start_active_slot(slot
)) {
862 run_active_slot(slot
);
863 if (results
.curl_result
!= CURLE_OK
&&
864 results
.http_code
!= 405) {
866 "Unable to create branch path %s\n",
872 fprintf(stderr
, "Unable to start MKCOL request\n");
876 ep
[1] = saved_character
;
877 ep
= strchr(ep
+ 1, '/');
880 escaped
= xml_entities(ident_default_email());
881 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
884 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
885 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
886 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
888 slot
= get_active_slot();
889 slot
->results
= &results
;
890 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
891 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
892 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
894 CALLOC_ARRAY(lock
, 1);
897 if (start_active_slot(slot
)) {
898 run_active_slot(slot
);
899 if (results
.curl_result
== CURLE_OK
) {
900 XML_Parser parser
= XML_ParserCreate(NULL
);
901 enum XML_Status result
;
902 ctx
.name
= xcalloc(10, 1);
905 ctx
.userFunc
= handle_new_lock_ctx
;
907 XML_SetUserData(parser
, &ctx
);
908 XML_SetElementHandler(parser
, xml_start_tag
,
910 XML_SetCharacterDataHandler(parser
, xml_cdata
);
911 result
= XML_Parse(parser
, in_buffer
.buf
,
914 if (result
!= XML_STATUS_OK
) {
915 fprintf(stderr
, "XML error: %s\n",
917 XML_GetErrorCode(parser
)));
920 XML_ParserFree(parser
);
923 "error: curl result=%d, HTTP code=%ld\n",
924 results
.curl_result
, results
.http_code
);
927 fprintf(stderr
, "Unable to start LOCK request\n");
930 curl_slist_free_all(dav_headers
);
931 strbuf_release(&out_buffer
.buf
);
932 strbuf_release(&in_buffer
);
934 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
941 lock
->start_time
= time(NULL
);
942 lock
->next
= repo
->locks
;
949 static int unlock_remote(struct remote_lock
*lock
)
951 struct active_request_slot
*slot
;
952 struct slot_results results
;
953 struct remote_lock
*prev
= repo
->locks
;
954 struct curl_slist
*dav_headers
;
957 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
959 slot
= get_active_slot();
960 slot
->results
= &results
;
961 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
962 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
964 if (start_active_slot(slot
)) {
965 run_active_slot(slot
);
966 if (results
.curl_result
== CURLE_OK
)
969 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
972 fprintf(stderr
, "Unable to start UNLOCK request\n");
975 curl_slist_free_all(dav_headers
);
977 if (repo
->locks
== lock
) {
978 repo
->locks
= lock
->next
;
980 while (prev
&& prev
->next
!= lock
)
983 prev
->next
= lock
->next
;
994 static void remove_locks(void)
996 struct remote_lock
*lock
= repo
->locks
;
998 fprintf(stderr
, "Removing remote locks...\n");
1000 struct remote_lock
*next
= lock
->next
;
1001 unlock_remote(lock
);
1006 static void remove_locks_on_signal(int signo
)
1009 sigchain_pop(signo
);
1013 static void remote_ls(const char *path
, int flags
,
1014 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1017 /* extract hex from sharded "xx/x{38}" filename */
1018 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1020 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
1022 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1025 if (hex_to_bytes(oid
->hash
, path
, 1))
1028 path
++; /* skip '/' */
1030 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1033 static void process_ls_object(struct remote_ls_ctx
*ls
)
1035 unsigned int *parent
= (unsigned int *)ls
->userData
;
1036 const char *path
= ls
->dentry_name
;
1037 struct object_id oid
;
1039 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1040 remote_dir_exists
[*parent
] = 1;
1044 if (!skip_prefix(path
, "objects/", &path
) ||
1045 get_oid_hex_from_objpath(path
, &oid
))
1048 one_remote_object(&oid
);
1051 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1053 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1054 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1058 if (!(ls
->dentry_flags
& IS_DIR
))
1059 one_remote_ref(ls
->dentry_name
);
1062 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1064 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1067 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1068 if (ls
->dentry_flags
& IS_DIR
) {
1070 /* ensure collection names end with slash */
1071 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1073 if (ls
->flags
& PROCESS_DIRS
) {
1076 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1077 ls
->flags
& RECURSIVE
) {
1078 remote_ls(ls
->dentry_name
,
1083 } else if (ls
->flags
& PROCESS_FILES
) {
1086 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1087 char *path
= ctx
->cdata
;
1088 if (*ctx
->cdata
== 'h') {
1089 path
= strstr(path
, "//");
1091 path
= strchr(path
+2, '/');
1095 const char *url
= repo
->url
;
1098 if (strncmp(path
, url
, repo
->path_len
))
1099 error("Parsed path '%s' does not match url: '%s'",
1102 path
+= repo
->path_len
;
1103 ls
->dentry_name
= xstrdup(path
);
1106 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1107 ls
->dentry_flags
|= IS_DIR
;
1109 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1110 FREE_AND_NULL(ls
->dentry_name
);
1111 ls
->dentry_flags
= 0;
1116 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1117 * should _only_ heed the information from that file, instead of trying to
1118 * determine the refs from the remote file system (badly: it does not even
1119 * know about packed-refs).
1121 static void remote_ls(const char *path
, int flags
,
1122 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1125 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1126 struct active_request_slot
*slot
;
1127 struct slot_results results
;
1128 struct strbuf in_buffer
= STRBUF_INIT
;
1129 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1130 struct curl_slist
*dav_headers
= http_copy_default_headers();
1132 struct remote_ls_ctx ls
;
1135 ls
.path
= xstrdup(path
);
1136 ls
.dentry_name
= NULL
;
1137 ls
.dentry_flags
= 0;
1138 ls
.userData
= userData
;
1139 ls
.userFunc
= userFunc
;
1141 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1143 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1144 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1146 slot
= get_active_slot();
1147 slot
->results
= &results
;
1148 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1149 &out_buffer
, fwrite_buffer
);
1150 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1151 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1153 if (start_active_slot(slot
)) {
1154 run_active_slot(slot
);
1155 if (results
.curl_result
== CURLE_OK
) {
1156 XML_Parser parser
= XML_ParserCreate(NULL
);
1157 enum XML_Status result
;
1158 ctx
.name
= xcalloc(10, 1);
1161 ctx
.userFunc
= handle_remote_ls_ctx
;
1163 XML_SetUserData(parser
, &ctx
);
1164 XML_SetElementHandler(parser
, xml_start_tag
,
1166 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1167 result
= XML_Parse(parser
, in_buffer
.buf
,
1171 if (result
!= XML_STATUS_OK
) {
1172 fprintf(stderr
, "XML error: %s\n",
1174 XML_GetErrorCode(parser
)));
1176 XML_ParserFree(parser
);
1179 fprintf(stderr
, "Unable to start PROPFIND request\n");
1184 strbuf_release(&out_buffer
.buf
);
1185 strbuf_release(&in_buffer
);
1186 curl_slist_free_all(dav_headers
);
1189 static void get_remote_object_list(unsigned char parent
)
1191 char path
[] = "objects/XX/";
1192 static const char hex
[] = "0123456789abcdef";
1193 unsigned int val
= parent
;
1195 path
[8] = hex
[val
>> 4];
1196 path
[9] = hex
[val
& 0xf];
1197 remote_dir_exists
[val
] = 0;
1198 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1199 process_ls_object
, &val
);
1202 static int locking_available(void)
1204 struct active_request_slot
*slot
;
1205 struct slot_results results
;
1206 struct strbuf in_buffer
= STRBUF_INIT
;
1207 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1208 struct curl_slist
*dav_headers
= http_copy_default_headers();
1213 escaped
= xml_entities(repo
->url
);
1214 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1217 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1218 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1220 slot
= get_active_slot();
1221 slot
->results
= &results
;
1222 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1223 &out_buffer
, fwrite_buffer
);
1224 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1225 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1227 if (start_active_slot(slot
)) {
1228 run_active_slot(slot
);
1229 if (results
.curl_result
== CURLE_OK
) {
1230 XML_Parser parser
= XML_ParserCreate(NULL
);
1231 enum XML_Status result
;
1232 ctx
.name
= xcalloc(10, 1);
1235 ctx
.userFunc
= handle_lockprop_ctx
;
1236 ctx
.userData
= &lock_flags
;
1237 XML_SetUserData(parser
, &ctx
);
1238 XML_SetElementHandler(parser
, xml_start_tag
,
1240 result
= XML_Parse(parser
, in_buffer
.buf
,
1244 if (result
!= XML_STATUS_OK
) {
1245 fprintf(stderr
, "XML error: %s\n",
1247 XML_GetErrorCode(parser
)));
1250 XML_ParserFree(parser
);
1252 error("no DAV locking support on %s",
1256 error("Cannot access URL %s, return code %d",
1257 repo
->url
, results
.curl_result
);
1261 error("Unable to start PROPFIND request on %s", repo
->url
);
1264 strbuf_release(&out_buffer
.buf
);
1265 strbuf_release(&in_buffer
);
1266 curl_slist_free_all(dav_headers
);
1271 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1273 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1277 return &entry
->next
;
1280 static struct object_list
**process_blob(struct blob
*blob
,
1281 struct object_list
**p
)
1283 struct object
*obj
= &blob
->object
;
1285 obj
->flags
|= LOCAL
;
1287 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1291 return add_one_object(obj
, p
);
1294 static struct object_list
**process_tree(struct tree
*tree
,
1295 struct object_list
**p
)
1297 struct object
*obj
= &tree
->object
;
1298 struct tree_desc desc
;
1299 struct name_entry entry
;
1301 obj
->flags
|= LOCAL
;
1303 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1305 if (parse_tree(tree
) < 0)
1306 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1309 p
= add_one_object(obj
, p
);
1311 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1313 while (tree_entry(&desc
, &entry
))
1314 switch (object_type(entry
.mode
)) {
1316 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1320 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1324 /* Subproject commit - not in this repository */
1328 free_tree_buffer(tree
);
1332 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1335 struct commit
*commit
;
1336 struct object_list
**p
= &objects
;
1339 while ((commit
= get_revision(revs
)) != NULL
) {
1340 p
= process_tree(repo_get_commit_tree(the_repository
, commit
),
1342 commit
->object
.flags
|= LOCAL
;
1343 if (!(commit
->object
.flags
& UNINTERESTING
))
1344 count
+= add_send_request(&commit
->object
, lock
);
1347 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1348 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1349 struct object
*obj
= entry
->item
;
1350 const char *name
= entry
->name
;
1352 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1354 if (obj
->type
== OBJ_TAG
) {
1356 p
= add_one_object(obj
, p
);
1359 if (obj
->type
== OBJ_TREE
) {
1360 p
= process_tree((struct tree
*)obj
, p
);
1363 if (obj
->type
== OBJ_BLOB
) {
1364 p
= process_blob((struct blob
*)obj
, p
);
1367 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1371 if (!(objects
->item
->flags
& UNINTERESTING
))
1372 count
+= add_send_request(objects
->item
, lock
);
1373 objects
= objects
->next
;
1379 static int update_remote(const struct object_id
*oid
, struct remote_lock
*lock
)
1381 struct active_request_slot
*slot
;
1382 struct slot_results results
;
1383 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1384 struct curl_slist
*dav_headers
;
1386 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1388 strbuf_addf(&out_buffer
.buf
, "%s\n", oid_to_hex(oid
));
1390 slot
= get_active_slot();
1391 slot
->results
= &results
;
1392 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1393 &out_buffer
, fwrite_null
);
1394 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1396 if (start_active_slot(slot
)) {
1397 run_active_slot(slot
);
1398 strbuf_release(&out_buffer
.buf
);
1399 if (results
.curl_result
!= CURLE_OK
) {
1401 "PUT error: curl result=%d, HTTP code=%ld\n",
1402 results
.curl_result
, results
.http_code
);
1403 /* We should attempt recovery? */
1407 strbuf_release(&out_buffer
.buf
);
1408 fprintf(stderr
, "Unable to start PUT request\n");
1415 static struct ref
*remote_refs
;
1417 static void one_remote_ref(const char *refname
)
1422 ref
= alloc_ref(refname
);
1424 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1426 "Unable to fetch ref %s from %s\n",
1427 refname
, repo
->url
);
1433 * Fetch a copy of the object if it doesn't exist locally - it
1434 * may be required for updating server info later.
1436 if (repo
->can_update_info_refs
&& !repo_has_object_file(the_repository
, &ref
->old_oid
)) {
1437 obj
= lookup_unknown_object(the_repository
, &ref
->old_oid
);
1438 fprintf(stderr
, " fetch %s for %s\n",
1439 oid_to_hex(&ref
->old_oid
), refname
);
1440 add_fetch_request(obj
);
1443 ref
->next
= remote_refs
;
1447 static void get_dav_remote_heads(void)
1449 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1452 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1454 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1458 ref
= alloc_ref(ls
->dentry_name
);
1460 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1462 "Unable to fetch ref %s from %s\n",
1463 ls
->dentry_name
, repo
->url
);
1469 o
= parse_object(the_repository
, &ref
->old_oid
);
1472 "Unable to parse object %s for remote ref %s\n",
1473 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1479 strbuf_addf(buf
, "%s\t%s\n",
1480 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1482 if (o
->type
== OBJ_TAG
) {
1483 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1485 strbuf_addf(buf
, "%s\t%s^{}\n",
1486 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1491 static void update_remote_info_refs(struct remote_lock
*lock
)
1493 struct buffer buffer
= { STRBUF_INIT
, 0 };
1494 struct active_request_slot
*slot
;
1495 struct slot_results results
;
1496 struct curl_slist
*dav_headers
;
1498 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1499 add_remote_info_ref
, &buffer
.buf
);
1501 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1503 slot
= get_active_slot();
1504 slot
->results
= &results
;
1505 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1506 &buffer
, fwrite_null
);
1507 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1509 if (start_active_slot(slot
)) {
1510 run_active_slot(slot
);
1511 if (results
.curl_result
!= CURLE_OK
) {
1513 "PUT error: curl result=%d, HTTP code=%ld\n",
1514 results
.curl_result
, results
.http_code
);
1518 strbuf_release(&buffer
.buf
);
1521 static int remote_exists(const char *path
)
1523 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1527 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1531 case HTTP_MISSING_TARGET
:
1535 error("unable to access '%s': %s", url
, curl_errorstr
);
1544 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1546 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1547 struct strbuf buffer
= STRBUF_INIT
;
1550 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1551 die("Couldn't get %s for remote symref\n%s", url
,
1555 FREE_AND_NULL(*symref
);
1558 if (buffer
.len
== 0)
1561 /* Cut off trailing newline. */
1562 strbuf_rtrim(&buffer
);
1564 /* If it's a symref, set the refname; otherwise try for a sha1 */
1565 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1566 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1568 get_oid_hex(buffer
.buf
, oid
);
1571 strbuf_release(&buffer
);
1574 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1576 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1577 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1580 return repo_in_merge_bases(the_repository
, branch
, head
);
1583 static int delete_remote_branch(const char *pattern
, int force
)
1585 struct ref
*refs
= remote_refs
;
1586 struct ref
*remote_ref
= NULL
;
1587 struct object_id head_oid
;
1588 char *symref
= NULL
;
1590 int patlen
= strlen(pattern
);
1592 struct active_request_slot
*slot
;
1593 struct slot_results results
;
1596 /* Find the remote branch(es) matching the specified branch name */
1597 for (match
= 0; refs
; refs
= refs
->next
) {
1598 char *name
= refs
->name
;
1599 int namelen
= strlen(name
);
1600 if (namelen
< patlen
||
1601 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1603 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1609 return error("No remote branch matches %s", pattern
);
1611 return error("More than one remote branch matches %s",
1615 * Remote HEAD must be a symref (not exactly foolproof; a remote
1616 * symlink to a symref will look like a symref)
1618 fetch_symref("HEAD", &symref
, &head_oid
);
1620 return error("Remote HEAD is not a symref");
1622 /* Remote branch must not be the remote HEAD */
1623 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1624 if (!strcmp(remote_ref
->name
, symref
))
1625 return error("Remote branch %s is the current HEAD",
1627 fetch_symref(symref
, &symref
, &head_oid
);
1630 /* Run extra sanity checks if delete is not forced */
1632 /* Remote HEAD must resolve to a known object */
1634 return error("Remote HEAD symrefs too deep");
1635 if (is_null_oid(&head_oid
))
1636 return error("Unable to resolve remote HEAD");
1637 if (!repo_has_object_file(the_repository
, &head_oid
))
1638 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1640 /* Remote branch must resolve to a known object */
1641 if (is_null_oid(&remote_ref
->old_oid
))
1642 return error("Unable to resolve remote branch %s",
1644 if (!repo_has_object_file(the_repository
, &remote_ref
->old_oid
))
1645 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
));
1647 /* Remote branch must be an ancestor of remote HEAD */
1648 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1649 return error("The branch '%s' is not an ancestor "
1650 "of your current HEAD.\n"
1651 "If you are sure you want to delete it,"
1652 " run:\n\t'git http-push -D %s %s'",
1653 remote_ref
->name
, repo
->url
, pattern
);
1657 /* Send delete request */
1658 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1661 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1662 slot
= get_active_slot();
1663 slot
->results
= &results
;
1664 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1665 if (start_active_slot(slot
)) {
1666 run_active_slot(slot
);
1668 if (results
.curl_result
!= CURLE_OK
)
1669 return error("DELETE request failed (%d/%ld)",
1670 results
.curl_result
, results
.http_code
);
1673 return error("Unable to start DELETE request");
1679 static void run_request_queue(void)
1681 is_running_queue
= 1;
1682 fill_active_slots();
1683 add_fill_function(NULL
, fill_active_slot
);
1685 finish_all_active_slots();
1686 fill_active_slots();
1687 } while (request_queue_head
&& !aborted
);
1689 is_running_queue
= 0;
1692 int cmd_main(int argc
, const char **argv
)
1694 struct transfer_request
*request
;
1695 struct transfer_request
*next_request
;
1696 struct refspec rs
= REFSPEC_INIT_PUSH
;
1697 struct remote_lock
*ref_lock
= NULL
;
1698 struct remote_lock
*info_ref_lock
= NULL
;
1699 int delete_branch
= 0;
1700 int force_delete
= 0;
1701 int objects_to_send
;
1705 struct ref
*ref
, *local_refs
;
1707 CALLOC_ARRAY(repo
, 1);
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
);
1758 refspec_appendn(&rs
, argv
, argc
- i
);
1763 usage(http_push_usage
);
1765 if (delete_branch
&& rs
.nr
!= 1)
1766 die("You must specify only one branch name when deleting a remote branch");
1768 setup_git_directory();
1770 memset(remote_dir_exists
, -1, 256);
1772 http_init(NULL
, repo
->url
, 1);
1774 is_running_queue
= 0;
1776 /* Verify DAV compliance/lock support */
1777 if (!locking_available()) {
1782 sigchain_push_common(remove_locks_on_signal
);
1784 /* Check whether the remote has server info files */
1785 repo
->can_update_info_refs
= 0;
1786 repo
->has_info_refs
= remote_exists("info/refs");
1787 repo
->has_info_packs
= remote_exists("objects/info/packs");
1788 if (repo
->has_info_refs
) {
1789 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1791 repo
->can_update_info_refs
= 1;
1793 error("cannot lock existing info/refs");
1798 if (repo
->has_info_packs
)
1801 /* Get a list of all local and remote heads to validate refspecs */
1802 local_refs
= get_local_heads();
1803 fprintf(stderr
, "Fetching remote heads...\n");
1804 get_dav_remote_heads();
1805 run_request_queue();
1807 /* Remove a remote branch if -d or -D was specified */
1808 if (delete_branch
) {
1809 const char *branch
= rs
.items
[i
].src
;
1810 if (delete_remote_branch(branch
, force_delete
) == -1) {
1811 fprintf(stderr
, "Unable to delete remote branch %s\n",
1814 printf("error %s cannot remove\n", branch
);
1820 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1825 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1827 printf("error null no match\n");
1833 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1834 struct rev_info revs
;
1835 struct strvec commit_argv
= STRVEC_INIT
;
1840 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1841 if (delete_remote_branch(ref
->name
, 1) == -1) {
1842 error("Could not remove %s", ref
->name
);
1844 printf("error %s cannot remove\n", ref
->name
);
1847 else if (helper_status
)
1848 printf("ok %s\n", ref
->name
);
1853 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1855 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1857 printf("ok %s up to date\n", ref
->name
);
1862 !is_null_oid(&ref
->old_oid
) &&
1864 if (!repo_has_object_file(the_repository
, &ref
->old_oid
) ||
1865 !ref_newer(&ref
->peer_ref
->new_oid
,
1868 * We do not have the remote ref, or
1869 * we know that the remote ref is not
1870 * an ancestor of what we are trying to
1871 * push. Either way this can be losing
1872 * commits at the remote end and likely
1873 * we were not up to date to begin with.
1875 error("remote '%s' is not an ancestor of\n"
1877 "Maybe you are not up-to-date and "
1878 "need to pull first?",
1880 ref
->peer_ref
->name
);
1882 printf("error %s non-fast forward\n", ref
->name
);
1887 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1890 fprintf(stderr
, "updating '%s'", ref
->name
);
1891 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1892 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1893 fprintf(stderr
, "\n from %s\n to %s\n",
1894 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1897 printf("ok %s\n", ref
->name
);
1901 /* Lock remote branch ref */
1902 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1904 fprintf(stderr
, "Unable to lock remote branch %s\n",
1907 printf("error %s lock error\n", ref
->name
);
1912 /* Set up revision info for this refspec */
1913 strvec_push(&commit_argv
, ""); /* ignored */
1914 strvec_push(&commit_argv
, "--objects");
1915 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1916 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1917 strvec_pushf(&commit_argv
, "^%s",
1918 oid_to_hex(&ref
->old_oid
));
1919 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1920 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1921 revs
.edge_hint
= 0; /* just in case */
1923 /* Generate a list of objects that need to be pushed */
1925 if (prepare_revision_walk(&revs
))
1926 die("revision walk setup failed");
1927 mark_edges_uninteresting(&revs
, NULL
, 0);
1928 objects_to_send
= get_delta(&revs
, ref_lock
);
1929 finish_all_active_slots();
1931 /* Push missing objects to remote, this would be a
1932 convenient time to pack them first if appropriate. */
1934 if (objects_to_send
)
1935 fprintf(stderr
, " sending %d objects\n",
1938 run_request_queue();
1940 /* Update the remote branch if all went well */
1941 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1945 fprintf(stderr
, " done\n");
1947 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1948 unlock_remote(ref_lock
);
1950 strvec_clear(&commit_argv
);
1951 release_revisions(&revs
);
1954 /* Update remote server info if appropriate */
1955 if (repo
->has_info_refs
&& new_refs
) {
1956 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1957 fprintf(stderr
, "Updating remote server info\n");
1959 update_remote_info_refs(info_ref_lock
);
1961 fprintf(stderr
, "Unable to update server info\n");
1967 unlock_remote(info_ref_lock
);
1972 request
= request_queue_head
;
1973 while (request
!= NULL
) {
1974 next_request
= request
->next
;
1975 release_request(request
);
1976 request
= next_request
;