2 #include "repository.h"
12 #include "list-objects.h"
14 #include "argv-array.h"
16 #include "object-store.h"
17 #include "commit-reach.h"
19 #ifdef EXPAT_NEEDS_XMLPARSE_H
25 static const char http_push_usage
[] =
26 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
33 #define XML_STATUS_OK 1
34 #define XML_STATUS_ERROR 0
37 #define PREV_BUF_SIZE 4096
40 #define DAV_LOCK "LOCK"
41 #define DAV_MKCOL "MKCOL"
42 #define DAV_MOVE "MOVE"
43 #define DAV_PROPFIND "PROPFIND"
45 #define DAV_UNLOCK "UNLOCK"
46 #define DAV_DELETE "DELETE"
49 #define DAV_PROP_LOCKWR (1u << 0)
50 #define DAV_PROP_LOCKEX (1u << 1)
51 #define DAV_LOCK_OK (1u << 2)
53 /* DAV XML properties */
54 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
55 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
56 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
57 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
58 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
59 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
60 #define DAV_PROPFIND_RESP ".multistatus.response"
61 #define DAV_PROPFIND_NAME ".multistatus.response.href"
62 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
64 /* DAV request body templates */
65 #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>"
66 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
67 #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>"
70 #define LOCK_REFRESH 30
72 /* Remember to update object flag allocation in object.h */
73 #define LOCAL (1u<<16)
74 #define REMOTE (1u<<17)
75 #define FETCHING (1u<<18)
76 #define PUSHING (1u<<19)
78 /* We allow "recursive" symbolic refs. Only within reason, though */
83 static signed char remote_dir_exists
[256];
85 static int push_verbosely
;
86 static int push_all
= MATCH_REFS_NONE
;
89 static int helper_status
;
91 static struct object_list
*objects
;
98 int can_update_info_refs
;
100 struct packed_git
*packs
;
101 struct remote_lock
*locks
;
104 static struct repo
*repo
;
106 enum transfer_state
{
118 struct transfer_request
{
122 struct remote_lock
*lock
;
123 struct curl_slist
*headers
;
124 struct buffer buffer
;
125 enum transfer_state state
;
126 CURLcode curl_result
;
127 char errorstr
[CURL_ERROR_SIZE
];
130 struct active_request_slot
*slot
;
131 struct transfer_request
*next
;
134 static struct transfer_request
*request_queue_head
;
140 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
148 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
152 struct remote_lock
*next
;
155 /* Flags that control remote_ls processing */
156 #define PROCESS_FILES (1u << 0)
157 #define PROCESS_DIRS (1u << 1)
158 #define RECURSIVE (1u << 2)
160 /* Flags that remote_ls passes to callback functions */
161 #define IS_DIR (1u << 0)
163 struct remote_ls_ctx
{
165 void (*userFunc
)(struct remote_ls_ctx
*ls
);
170 struct remote_ls_ctx
*parent
;
173 /* get_dav_token_headers options */
174 enum dav_header_flag
{
175 DAV_HEADER_IF
= (1u << 0),
176 DAV_HEADER_LOCK
= (1u << 1),
177 DAV_HEADER_TIMEOUT
= (1u << 2)
180 static char *xml_entities(const char *s
)
182 struct strbuf buf
= STRBUF_INIT
;
183 strbuf_addstr_xml_quoted(&buf
, s
);
184 return strbuf_detach(&buf
, NULL
);
187 static void curl_setup_http_get(CURL
*curl
, const char *url
,
188 const char *custom_req
)
190 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
191 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
192 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
193 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
196 static void curl_setup_http(CURL
*curl
, const char *url
,
197 const char *custom_req
, struct buffer
*buffer
,
198 curl_write_callback write_fn
)
200 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
201 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
202 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
203 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
204 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
205 #ifndef NO_CURL_IOCTL
206 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
207 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, buffer
);
209 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
210 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
211 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
212 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
215 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
217 struct strbuf buf
= STRBUF_INIT
;
218 struct curl_slist
*dav_headers
= http_copy_default_headers();
220 if (options
& DAV_HEADER_IF
) {
221 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
222 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
225 if (options
& DAV_HEADER_LOCK
) {
226 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
227 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
230 if (options
& DAV_HEADER_TIMEOUT
) {
231 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
232 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
235 strbuf_release(&buf
);
240 static void finish_request(struct transfer_request
*request
);
241 static void release_request(struct transfer_request
*request
);
243 static void process_response(void *callback_data
)
245 struct transfer_request
*request
=
246 (struct transfer_request
*)callback_data
;
248 finish_request(request
);
251 #ifdef USE_CURL_MULTI
253 static void start_fetch_loose(struct transfer_request
*request
)
255 struct active_request_slot
*slot
;
256 struct http_object_request
*obj_req
;
258 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
259 if (obj_req
== NULL
) {
260 request
->state
= ABORTED
;
264 slot
= obj_req
->slot
;
265 slot
->callback_func
= process_response
;
266 slot
->callback_data
= request
;
267 request
->slot
= slot
;
268 request
->userData
= obj_req
;
270 /* Try to get the request started, abort the request on error */
271 request
->state
= RUN_FETCH_LOOSE
;
272 if (!start_active_slot(slot
)) {
273 fprintf(stderr
, "Unable to start GET request\n");
274 repo
->can_update_info_refs
= 0;
275 release_http_object_request(obj_req
);
276 release_request(request
);
280 static void start_mkcol(struct transfer_request
*request
)
282 char *hex
= oid_to_hex(&request
->obj
->oid
);
283 struct active_request_slot
*slot
;
285 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
287 slot
= get_active_slot();
288 slot
->callback_func
= process_response
;
289 slot
->callback_data
= request
;
290 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
291 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
293 if (start_active_slot(slot
)) {
294 request
->slot
= slot
;
295 request
->state
= RUN_MKCOL
;
297 request
->state
= ABORTED
;
298 FREE_AND_NULL(request
->url
);
303 static void start_fetch_packed(struct transfer_request
*request
)
305 struct packed_git
*target
;
307 struct transfer_request
*check_request
= request_queue_head
;
308 struct http_pack_request
*preq
;
310 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
312 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
313 repo
->can_update_info_refs
= 0;
314 release_request(request
);
318 fprintf(stderr
, "Fetching pack %s\n",
319 hash_to_hex(target
->hash
));
320 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
322 preq
= new_http_pack_request(target
, repo
->url
);
324 repo
->can_update_info_refs
= 0;
327 preq
->lst
= &repo
->packs
;
329 /* Make sure there isn't another open request for this pack */
330 while (check_request
) {
331 if (check_request
->state
== RUN_FETCH_PACKED
&&
332 !strcmp(check_request
->url
, preq
->url
)) {
333 release_http_pack_request(preq
);
334 release_request(request
);
337 check_request
= check_request
->next
;
340 preq
->slot
->callback_func
= process_response
;
341 preq
->slot
->callback_data
= request
;
342 request
->slot
= preq
->slot
;
343 request
->userData
= preq
;
345 /* Try to get the request started, abort the request on error */
346 request
->state
= RUN_FETCH_PACKED
;
347 if (!start_active_slot(preq
->slot
)) {
348 fprintf(stderr
, "Unable to start GET request\n");
349 release_http_pack_request(preq
);
350 repo
->can_update_info_refs
= 0;
351 release_request(request
);
355 static void start_put(struct transfer_request
*request
)
357 char *hex
= oid_to_hex(&request
->obj
->oid
);
358 struct active_request_slot
*slot
;
359 struct strbuf buf
= STRBUF_INIT
;
360 enum object_type type
;
368 unpacked
= read_object_file(&request
->obj
->oid
, &type
, &len
);
369 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(type
), (uintmax_t)len
) + 1;
372 git_deflate_init(&stream
, zlib_compression_level
);
373 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
374 strbuf_init(&request
->buffer
.buf
, size
);
375 request
->buffer
.posn
= 0;
378 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
379 stream
.avail_out
= size
;
382 stream
.next_in
= (void *)hdr
;
383 stream
.avail_in
= hdrlen
;
384 while (git_deflate(&stream
, 0) == Z_OK
)
387 /* Then the data itself.. */
388 stream
.next_in
= unpacked
;
389 stream
.avail_in
= len
;
390 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
392 git_deflate_end(&stream
);
395 request
->buffer
.buf
.len
= stream
.total_out
;
397 strbuf_addstr(&buf
, "Destination: ");
398 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
399 request
->dest
= strbuf_detach(&buf
, NULL
);
401 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
402 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
403 request
->url
= strbuf_detach(&buf
, NULL
);
405 slot
= get_active_slot();
406 slot
->callback_func
= process_response
;
407 slot
->callback_data
= request
;
408 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
409 &request
->buffer
, fwrite_null
);
411 if (start_active_slot(slot
)) {
412 request
->slot
= slot
;
413 request
->state
= RUN_PUT
;
415 request
->state
= ABORTED
;
416 FREE_AND_NULL(request
->url
);
420 static void start_move(struct transfer_request
*request
)
422 struct active_request_slot
*slot
;
423 struct curl_slist
*dav_headers
= http_copy_default_headers();
425 slot
= get_active_slot();
426 slot
->callback_func
= process_response
;
427 slot
->callback_data
= request
;
428 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
429 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
430 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
431 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
433 if (start_active_slot(slot
)) {
434 request
->slot
= slot
;
435 request
->state
= RUN_MOVE
;
437 request
->state
= ABORTED
;
438 FREE_AND_NULL(request
->url
);
442 static int refresh_lock(struct remote_lock
*lock
)
444 struct active_request_slot
*slot
;
445 struct slot_results results
;
446 struct curl_slist
*dav_headers
;
449 lock
->refreshing
= 1;
451 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
453 slot
= get_active_slot();
454 slot
->results
= &results
;
455 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
456 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
458 if (start_active_slot(slot
)) {
459 run_active_slot(slot
);
460 if (results
.curl_result
!= CURLE_OK
) {
461 fprintf(stderr
, "LOCK HTTP error %ld\n",
464 lock
->start_time
= time(NULL
);
469 lock
->refreshing
= 0;
470 curl_slist_free_all(dav_headers
);
475 static void check_locks(void)
477 struct remote_lock
*lock
= repo
->locks
;
478 time_t current_time
= time(NULL
);
482 time_remaining
= lock
->start_time
+ lock
->timeout
-
484 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
485 if (!refresh_lock(lock
)) {
487 "Unable to refresh lock for %s\n",
497 static void release_request(struct transfer_request
*request
)
499 struct transfer_request
*entry
= request_queue_head
;
501 if (request
== request_queue_head
) {
502 request_queue_head
= request
->next
;
504 while (entry
->next
!= NULL
&& entry
->next
!= request
)
506 if (entry
->next
== request
)
507 entry
->next
= entry
->next
->next
;
514 static void finish_request(struct transfer_request
*request
)
516 struct http_pack_request
*preq
;
517 struct http_object_request
*obj_req
;
519 request
->curl_result
= request
->slot
->curl_result
;
520 request
->http_code
= request
->slot
->http_code
;
521 request
->slot
= NULL
;
523 /* Keep locks active */
526 if (request
->headers
!= NULL
)
527 curl_slist_free_all(request
->headers
);
529 /* URL is reused for MOVE after PUT and used during FETCH */
530 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
531 FREE_AND_NULL(request
->url
);
534 if (request
->state
== RUN_MKCOL
) {
535 if (request
->curl_result
== CURLE_OK
||
536 request
->http_code
== 405) {
537 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
540 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
541 oid_to_hex(&request
->obj
->oid
),
542 request
->curl_result
, request
->http_code
);
543 request
->state
= ABORTED
;
546 } else if (request
->state
== RUN_PUT
) {
547 if (request
->curl_result
== CURLE_OK
) {
550 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
551 oid_to_hex(&request
->obj
->oid
),
552 request
->curl_result
, request
->http_code
);
553 request
->state
= ABORTED
;
556 } else if (request
->state
== RUN_MOVE
) {
557 if (request
->curl_result
== CURLE_OK
) {
559 fprintf(stderr
, " sent %s\n",
560 oid_to_hex(&request
->obj
->oid
));
561 request
->obj
->flags
|= REMOTE
;
562 release_request(request
);
564 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
565 oid_to_hex(&request
->obj
->oid
),
566 request
->curl_result
, request
->http_code
);
567 request
->state
= ABORTED
;
570 } else if (request
->state
== RUN_FETCH_LOOSE
) {
571 obj_req
= (struct http_object_request
*)request
->userData
;
573 if (finish_http_object_request(obj_req
) == 0)
574 if (obj_req
->rename
== 0)
575 request
->obj
->flags
|= (LOCAL
| REMOTE
);
577 /* Try fetching packed if necessary */
578 if (request
->obj
->flags
& LOCAL
) {
579 release_http_object_request(obj_req
);
580 release_request(request
);
582 start_fetch_packed(request
);
584 } else if (request
->state
== RUN_FETCH_PACKED
) {
586 if (request
->curl_result
!= CURLE_OK
) {
587 fprintf(stderr
, "Unable to get pack file %s\n%s",
588 request
->url
, curl_errorstr
);
590 preq
= (struct http_pack_request
*)request
->userData
;
593 if (finish_http_pack_request(preq
) == 0)
595 release_http_pack_request(preq
);
599 repo
->can_update_info_refs
= 0;
600 release_request(request
);
604 #ifdef USE_CURL_MULTI
605 static int is_running_queue
;
606 static int fill_active_slot(void *unused
)
608 struct transfer_request
*request
;
610 if (aborted
|| !is_running_queue
)
613 for (request
= request_queue_head
; request
; request
= request
->next
) {
614 if (request
->state
== NEED_FETCH
) {
615 start_fetch_loose(request
);
617 } else if (pushing
&& request
->state
== NEED_PUSH
) {
618 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
621 start_mkcol(request
);
630 static void get_remote_object_list(unsigned char parent
);
632 static void add_fetch_request(struct object
*obj
)
634 struct transfer_request
*request
;
639 * Don't fetch the object if it's known to exist locally
640 * or is already in the request queue
642 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
643 get_remote_object_list(obj
->oid
.hash
[0]);
644 if (obj
->flags
& (LOCAL
| FETCHING
))
647 obj
->flags
|= FETCHING
;
648 request
= xmalloc(sizeof(*request
));
651 request
->lock
= NULL
;
652 request
->headers
= NULL
;
653 request
->state
= NEED_FETCH
;
654 request
->next
= request_queue_head
;
655 request_queue_head
= request
;
657 #ifdef USE_CURL_MULTI
663 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
665 struct transfer_request
*request
;
666 struct packed_git
*target
;
668 /* Keep locks active */
672 * Don't push the object if it's known to exist on the remote
673 * or is already in the request queue
675 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
676 get_remote_object_list(obj
->oid
.hash
[0]);
677 if (obj
->flags
& (REMOTE
| PUSHING
))
679 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
681 obj
->flags
|= REMOTE
;
685 obj
->flags
|= PUSHING
;
686 request
= xmalloc(sizeof(*request
));
689 request
->lock
= lock
;
690 request
->headers
= NULL
;
691 request
->state
= NEED_PUSH
;
692 request
->next
= request_queue_head
;
693 request_queue_head
= request
;
695 #ifdef USE_CURL_MULTI
703 static int fetch_indices(void)
708 fprintf(stderr
, "Getting pack list\n");
710 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
712 case HTTP_MISSING_TARGET
:
722 static void one_remote_object(const struct object_id
*oid
)
726 obj
= lookup_object(the_repository
, oid
->hash
);
728 obj
= parse_object(the_repository
, oid
);
730 /* Ignore remote objects that don't exist locally */
734 obj
->flags
|= REMOTE
;
735 if (!object_list_contains(objects
, obj
))
736 object_list_insert(obj
, &objects
);
739 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
741 int *lock_flags
= (int *)ctx
->userData
;
744 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
745 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
746 (*lock_flags
& DAV_PROP_LOCKWR
)) {
747 *lock_flags
|= DAV_LOCK_OK
;
749 *lock_flags
&= DAV_LOCK_OK
;
750 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
751 *lock_flags
|= DAV_PROP_LOCKWR
;
752 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
753 *lock_flags
|= DAV_PROP_LOCKEX
;
758 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
760 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
761 git_hash_ctx hash_ctx
;
762 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
764 if (tag_closed
&& ctx
->cdata
) {
765 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
766 lock
->owner
= xstrdup(ctx
->cdata
);
767 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
769 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
770 lock
->timeout
= strtol(arg
, NULL
, 10);
771 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
772 lock
->token
= xstrdup(ctx
->cdata
);
774 the_hash_algo
->init_fn(&hash_ctx
);
775 the_hash_algo
->update_fn(&hash_ctx
, lock
->token
, strlen(lock
->token
));
776 the_hash_algo
->final_fn(lock_token_hash
, &hash_ctx
);
778 lock
->tmpfile_suffix
[0] = '_';
779 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
784 static void one_remote_ref(const char *refname
);
787 xml_start_tag(void *userData
, const char *name
, const char **atts
)
789 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
790 const char *c
= strchr(name
, ':');
791 int old_namelen
, new_len
;
798 old_namelen
= strlen(ctx
->name
);
799 new_len
= old_namelen
+ strlen(c
) + 2;
801 if (new_len
> ctx
->len
) {
802 ctx
->name
= xrealloc(ctx
->name
, new_len
);
805 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
807 FREE_AND_NULL(ctx
->cdata
);
809 ctx
->userFunc(ctx
, 0);
813 xml_end_tag(void *userData
, const char *name
)
815 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
816 const char *c
= strchr(name
, ':');
819 ctx
->userFunc(ctx
, 1);
826 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
831 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
833 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
835 ctx
->cdata
= xmemdupz(s
, len
);
838 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
840 struct active_request_slot
*slot
;
841 struct slot_results results
;
842 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
843 struct strbuf in_buffer
= STRBUF_INIT
;
846 char timeout_header
[25];
847 struct remote_lock
*lock
= NULL
;
848 struct curl_slist
*dav_headers
= http_copy_default_headers();
852 url
= xstrfmt("%s%s", repo
->url
, path
);
854 /* Make sure leading directories exist for the remote ref */
855 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
857 char saved_character
= ep
[1];
859 slot
= get_active_slot();
860 slot
->results
= &results
;
861 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
862 if (start_active_slot(slot
)) {
863 run_active_slot(slot
);
864 if (results
.curl_result
!= CURLE_OK
&&
865 results
.http_code
!= 405) {
867 "Unable to create branch path %s\n",
873 fprintf(stderr
, "Unable to start MKCOL request\n");
877 ep
[1] = saved_character
;
878 ep
= strchr(ep
+ 1, '/');
881 escaped
= xml_entities(ident_default_email());
882 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
885 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
886 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
887 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
889 slot
= get_active_slot();
890 slot
->results
= &results
;
891 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
892 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
893 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
895 lock
= xcalloc(1, sizeof(*lock
));
898 if (start_active_slot(slot
)) {
899 run_active_slot(slot
);
900 if (results
.curl_result
== CURLE_OK
) {
901 XML_Parser parser
= XML_ParserCreate(NULL
);
902 enum XML_Status result
;
903 ctx
.name
= xcalloc(10, 1);
906 ctx
.userFunc
= handle_new_lock_ctx
;
908 XML_SetUserData(parser
, &ctx
);
909 XML_SetElementHandler(parser
, xml_start_tag
,
911 XML_SetCharacterDataHandler(parser
, xml_cdata
);
912 result
= XML_Parse(parser
, in_buffer
.buf
,
915 if (result
!= XML_STATUS_OK
) {
916 fprintf(stderr
, "XML error: %s\n",
918 XML_GetErrorCode(parser
)));
921 XML_ParserFree(parser
);
924 "error: curl result=%d, HTTP code=%ld\n",
925 results
.curl_result
, results
.http_code
);
928 fprintf(stderr
, "Unable to start LOCK request\n");
931 curl_slist_free_all(dav_headers
);
932 strbuf_release(&out_buffer
.buf
);
933 strbuf_release(&in_buffer
);
935 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
942 lock
->start_time
= time(NULL
);
943 lock
->next
= repo
->locks
;
950 static int unlock_remote(struct remote_lock
*lock
)
952 struct active_request_slot
*slot
;
953 struct slot_results results
;
954 struct remote_lock
*prev
= repo
->locks
;
955 struct curl_slist
*dav_headers
;
958 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
960 slot
= get_active_slot();
961 slot
->results
= &results
;
962 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
963 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
965 if (start_active_slot(slot
)) {
966 run_active_slot(slot
);
967 if (results
.curl_result
== CURLE_OK
)
970 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
973 fprintf(stderr
, "Unable to start UNLOCK request\n");
976 curl_slist_free_all(dav_headers
);
978 if (repo
->locks
== lock
) {
979 repo
->locks
= lock
->next
;
981 while (prev
&& prev
->next
!= lock
)
984 prev
->next
= prev
->next
->next
;
995 static void remove_locks(void)
997 struct remote_lock
*lock
= repo
->locks
;
999 fprintf(stderr
, "Removing remote locks...\n");
1001 struct remote_lock
*next
= lock
->next
;
1002 unlock_remote(lock
);
1007 static void remove_locks_on_signal(int signo
)
1010 sigchain_pop(signo
);
1014 static void remote_ls(const char *path
, int flags
,
1015 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1018 /* extract hex from sharded "xx/x{38}" filename */
1019 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1021 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1024 if (hex_to_bytes(oid
->hash
, path
, 1))
1027 path
++; /* skip '/' */
1029 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1032 static void process_ls_object(struct remote_ls_ctx
*ls
)
1034 unsigned int *parent
= (unsigned int *)ls
->userData
;
1035 const char *path
= ls
->dentry_name
;
1036 struct object_id oid
;
1038 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1039 remote_dir_exists
[*parent
] = 1;
1043 if (!skip_prefix(path
, "objects/", &path
) ||
1044 get_oid_hex_from_objpath(path
, &oid
))
1047 one_remote_object(&oid
);
1050 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1052 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1053 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1057 if (!(ls
->dentry_flags
& IS_DIR
))
1058 one_remote_ref(ls
->dentry_name
);
1061 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1063 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1066 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1067 if (ls
->dentry_flags
& IS_DIR
) {
1069 /* ensure collection names end with slash */
1070 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1072 if (ls
->flags
& PROCESS_DIRS
) {
1075 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1076 ls
->flags
& RECURSIVE
) {
1077 remote_ls(ls
->dentry_name
,
1082 } else if (ls
->flags
& PROCESS_FILES
) {
1085 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1086 char *path
= ctx
->cdata
;
1087 if (*ctx
->cdata
== 'h') {
1088 path
= strstr(path
, "//");
1090 path
= strchr(path
+2, '/');
1094 const char *url
= repo
->url
;
1097 if (strncmp(path
, url
, repo
->path_len
))
1098 error("Parsed path '%s' does not match url: '%s'",
1101 path
+= repo
->path_len
;
1102 ls
->dentry_name
= xstrdup(path
);
1105 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1106 ls
->dentry_flags
|= IS_DIR
;
1108 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1109 FREE_AND_NULL(ls
->dentry_name
);
1110 ls
->dentry_flags
= 0;
1115 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1116 * should _only_ heed the information from that file, instead of trying to
1117 * determine the refs from the remote file system (badly: it does not even
1118 * know about packed-refs).
1120 static void remote_ls(const char *path
, int flags
,
1121 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1124 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1125 struct active_request_slot
*slot
;
1126 struct slot_results results
;
1127 struct strbuf in_buffer
= STRBUF_INIT
;
1128 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1129 struct curl_slist
*dav_headers
= http_copy_default_headers();
1131 struct remote_ls_ctx ls
;
1134 ls
.path
= xstrdup(path
);
1135 ls
.dentry_name
= NULL
;
1136 ls
.dentry_flags
= 0;
1137 ls
.userData
= userData
;
1138 ls
.userFunc
= userFunc
;
1140 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1142 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1143 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1145 slot
= get_active_slot();
1146 slot
->results
= &results
;
1147 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1148 &out_buffer
, fwrite_buffer
);
1149 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1150 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1152 if (start_active_slot(slot
)) {
1153 run_active_slot(slot
);
1154 if (results
.curl_result
== CURLE_OK
) {
1155 XML_Parser parser
= XML_ParserCreate(NULL
);
1156 enum XML_Status result
;
1157 ctx
.name
= xcalloc(10, 1);
1160 ctx
.userFunc
= handle_remote_ls_ctx
;
1162 XML_SetUserData(parser
, &ctx
);
1163 XML_SetElementHandler(parser
, xml_start_tag
,
1165 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1166 result
= XML_Parse(parser
, in_buffer
.buf
,
1170 if (result
!= XML_STATUS_OK
) {
1171 fprintf(stderr
, "XML error: %s\n",
1173 XML_GetErrorCode(parser
)));
1175 XML_ParserFree(parser
);
1178 fprintf(stderr
, "Unable to start PROPFIND request\n");
1183 strbuf_release(&out_buffer
.buf
);
1184 strbuf_release(&in_buffer
);
1185 curl_slist_free_all(dav_headers
);
1188 static void get_remote_object_list(unsigned char parent
)
1190 char path
[] = "objects/XX/";
1191 static const char hex
[] = "0123456789abcdef";
1192 unsigned int val
= parent
;
1194 path
[8] = hex
[val
>> 4];
1195 path
[9] = hex
[val
& 0xf];
1196 remote_dir_exists
[val
] = 0;
1197 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1198 process_ls_object
, &val
);
1201 static int locking_available(void)
1203 struct active_request_slot
*slot
;
1204 struct slot_results results
;
1205 struct strbuf in_buffer
= STRBUF_INIT
;
1206 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1207 struct curl_slist
*dav_headers
= http_copy_default_headers();
1212 escaped
= xml_entities(repo
->url
);
1213 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1216 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1217 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1219 slot
= get_active_slot();
1220 slot
->results
= &results
;
1221 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1222 &out_buffer
, fwrite_buffer
);
1223 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1224 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1226 if (start_active_slot(slot
)) {
1227 run_active_slot(slot
);
1228 if (results
.curl_result
== CURLE_OK
) {
1229 XML_Parser parser
= XML_ParserCreate(NULL
);
1230 enum XML_Status result
;
1231 ctx
.name
= xcalloc(10, 1);
1234 ctx
.userFunc
= handle_lockprop_ctx
;
1235 ctx
.userData
= &lock_flags
;
1236 XML_SetUserData(parser
, &ctx
);
1237 XML_SetElementHandler(parser
, xml_start_tag
,
1239 result
= XML_Parse(parser
, in_buffer
.buf
,
1243 if (result
!= XML_STATUS_OK
) {
1244 fprintf(stderr
, "XML error: %s\n",
1246 XML_GetErrorCode(parser
)));
1249 XML_ParserFree(parser
);
1251 error("no DAV locking support on %s",
1255 error("Cannot access URL %s, return code %d",
1256 repo
->url
, results
.curl_result
);
1260 error("Unable to start PROPFIND request on %s", repo
->url
);
1263 strbuf_release(&out_buffer
.buf
);
1264 strbuf_release(&in_buffer
);
1265 curl_slist_free_all(dav_headers
);
1270 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1272 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1276 return &entry
->next
;
1279 static struct object_list
**process_blob(struct blob
*blob
,
1280 struct object_list
**p
)
1282 struct object
*obj
= &blob
->object
;
1284 obj
->flags
|= LOCAL
;
1286 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1290 return add_one_object(obj
, p
);
1293 static struct object_list
**process_tree(struct tree
*tree
,
1294 struct object_list
**p
)
1296 struct object
*obj
= &tree
->object
;
1297 struct tree_desc desc
;
1298 struct name_entry entry
;
1300 obj
->flags
|= LOCAL
;
1302 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1304 if (parse_tree(tree
) < 0)
1305 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1308 p
= add_one_object(obj
, p
);
1310 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1312 while (tree_entry(&desc
, &entry
))
1313 switch (object_type(entry
.mode
)) {
1315 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1319 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1323 /* Subproject commit - not in this repository */
1327 free_tree_buffer(tree
);
1331 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1334 struct commit
*commit
;
1335 struct object_list
**p
= &objects
;
1338 while ((commit
= get_revision(revs
)) != NULL
) {
1339 p
= process_tree(get_commit_tree(commit
), p
);
1340 commit
->object
.flags
|= LOCAL
;
1341 if (!(commit
->object
.flags
& UNINTERESTING
))
1342 count
+= add_send_request(&commit
->object
, lock
);
1345 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1346 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1347 struct object
*obj
= entry
->item
;
1348 const char *name
= entry
->name
;
1350 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1352 if (obj
->type
== OBJ_TAG
) {
1354 p
= add_one_object(obj
, p
);
1357 if (obj
->type
== OBJ_TREE
) {
1358 p
= process_tree((struct tree
*)obj
, p
);
1361 if (obj
->type
== OBJ_BLOB
) {
1362 p
= process_blob((struct blob
*)obj
, p
);
1365 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1369 if (!(objects
->item
->flags
& UNINTERESTING
))
1370 count
+= add_send_request(objects
->item
, lock
);
1371 objects
= objects
->next
;
1377 static int update_remote(const struct object_id
*oid
, struct remote_lock
*lock
)
1379 struct active_request_slot
*slot
;
1380 struct slot_results results
;
1381 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1382 struct curl_slist
*dav_headers
;
1384 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1386 strbuf_addf(&out_buffer
.buf
, "%s\n", oid_to_hex(oid
));
1388 slot
= get_active_slot();
1389 slot
->results
= &results
;
1390 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1391 &out_buffer
, fwrite_null
);
1392 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1394 if (start_active_slot(slot
)) {
1395 run_active_slot(slot
);
1396 strbuf_release(&out_buffer
.buf
);
1397 if (results
.curl_result
!= CURLE_OK
) {
1399 "PUT error: curl result=%d, HTTP code=%ld\n",
1400 results
.curl_result
, results
.http_code
);
1401 /* We should attempt recovery? */
1405 strbuf_release(&out_buffer
.buf
);
1406 fprintf(stderr
, "Unable to start PUT request\n");
1413 static struct ref
*remote_refs
;
1415 static void one_remote_ref(const char *refname
)
1420 ref
= alloc_ref(refname
);
1422 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1424 "Unable to fetch ref %s from %s\n",
1425 refname
, repo
->url
);
1431 * Fetch a copy of the object if it doesn't exist locally - it
1432 * may be required for updating server info later.
1434 if (repo
->can_update_info_refs
&& !has_object_file(&ref
->old_oid
)) {
1435 obj
= lookup_unknown_object(ref
->old_oid
.hash
);
1436 fprintf(stderr
, " fetch %s for %s\n",
1437 oid_to_hex(&ref
->old_oid
), refname
);
1438 add_fetch_request(obj
);
1441 ref
->next
= remote_refs
;
1445 static void get_dav_remote_heads(void)
1447 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1450 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1452 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1456 ref
= alloc_ref(ls
->dentry_name
);
1458 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1460 "Unable to fetch ref %s from %s\n",
1461 ls
->dentry_name
, repo
->url
);
1467 o
= parse_object(the_repository
, &ref
->old_oid
);
1470 "Unable to parse object %s for remote ref %s\n",
1471 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1477 strbuf_addf(buf
, "%s\t%s\n",
1478 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1480 if (o
->type
== OBJ_TAG
) {
1481 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1483 strbuf_addf(buf
, "%s\t%s^{}\n",
1484 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1489 static void update_remote_info_refs(struct remote_lock
*lock
)
1491 struct buffer buffer
= { STRBUF_INIT
, 0 };
1492 struct active_request_slot
*slot
;
1493 struct slot_results results
;
1494 struct curl_slist
*dav_headers
;
1496 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1497 add_remote_info_ref
, &buffer
.buf
);
1499 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1501 slot
= get_active_slot();
1502 slot
->results
= &results
;
1503 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1504 &buffer
, fwrite_null
);
1505 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1507 if (start_active_slot(slot
)) {
1508 run_active_slot(slot
);
1509 if (results
.curl_result
!= CURLE_OK
) {
1511 "PUT error: curl result=%d, HTTP code=%ld\n",
1512 results
.curl_result
, results
.http_code
);
1516 strbuf_release(&buffer
.buf
);
1519 static int remote_exists(const char *path
)
1521 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1525 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1529 case HTTP_MISSING_TARGET
:
1533 error("unable to access '%s': %s", url
, curl_errorstr
);
1542 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1544 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1545 struct strbuf buffer
= STRBUF_INIT
;
1548 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1549 die("Couldn't get %s for remote symref\n%s", url
,
1553 FREE_AND_NULL(*symref
);
1556 if (buffer
.len
== 0)
1559 /* Cut off trailing newline. */
1560 strbuf_rtrim(&buffer
);
1562 /* If it's a symref, set the refname; otherwise try for a sha1 */
1563 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1564 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1566 get_oid_hex(buffer
.buf
, oid
);
1569 strbuf_release(&buffer
);
1572 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1574 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1575 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1578 return in_merge_bases(branch
, head
);
1581 static int delete_remote_branch(const char *pattern
, int force
)
1583 struct ref
*refs
= remote_refs
;
1584 struct ref
*remote_ref
= NULL
;
1585 struct object_id head_oid
;
1586 char *symref
= NULL
;
1588 int patlen
= strlen(pattern
);
1590 struct active_request_slot
*slot
;
1591 struct slot_results results
;
1594 /* Find the remote branch(es) matching the specified branch name */
1595 for (match
= 0; refs
; refs
= refs
->next
) {
1596 char *name
= refs
->name
;
1597 int namelen
= strlen(name
);
1598 if (namelen
< patlen
||
1599 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1601 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1607 return error("No remote branch matches %s", pattern
);
1609 return error("More than one remote branch matches %s",
1613 * Remote HEAD must be a symref (not exactly foolproof; a remote
1614 * symlink to a symref will look like a symref)
1616 fetch_symref("HEAD", &symref
, &head_oid
);
1618 return error("Remote HEAD is not a symref");
1620 /* Remote branch must not be the remote HEAD */
1621 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1622 if (!strcmp(remote_ref
->name
, symref
))
1623 return error("Remote branch %s is the current HEAD",
1625 fetch_symref(symref
, &symref
, &head_oid
);
1628 /* Run extra sanity checks if delete is not forced */
1630 /* Remote HEAD must resolve to a known object */
1632 return error("Remote HEAD symrefs too deep");
1633 if (is_null_oid(&head_oid
))
1634 return error("Unable to resolve remote HEAD");
1635 if (!has_object_file(&head_oid
))
1636 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1638 /* Remote branch must resolve to a known object */
1639 if (is_null_oid(&remote_ref
->old_oid
))
1640 return error("Unable to resolve remote branch %s",
1642 if (!has_object_file(&remote_ref
->old_oid
))
1643 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
));
1645 /* Remote branch must be an ancestor of remote HEAD */
1646 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1647 return error("The branch '%s' is not an ancestor "
1648 "of your current HEAD.\n"
1649 "If you are sure you want to delete it,"
1650 " run:\n\t'git http-push -D %s %s'",
1651 remote_ref
->name
, repo
->url
, pattern
);
1655 /* Send delete request */
1656 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1659 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1660 slot
= get_active_slot();
1661 slot
->results
= &results
;
1662 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1663 if (start_active_slot(slot
)) {
1664 run_active_slot(slot
);
1666 if (results
.curl_result
!= CURLE_OK
)
1667 return error("DELETE request failed (%d/%ld)",
1668 results
.curl_result
, results
.http_code
);
1671 return error("Unable to start DELETE request");
1677 static void run_request_queue(void)
1679 #ifdef USE_CURL_MULTI
1680 is_running_queue
= 1;
1681 fill_active_slots();
1682 add_fill_function(NULL
, fill_active_slot
);
1685 finish_all_active_slots();
1686 #ifdef USE_CURL_MULTI
1687 fill_active_slots();
1689 } while (request_queue_head
&& !aborted
);
1691 #ifdef USE_CURL_MULTI
1692 is_running_queue
= 0;
1696 int cmd_main(int argc
, const char **argv
)
1698 struct transfer_request
*request
;
1699 struct transfer_request
*next_request
;
1700 struct refspec rs
= REFSPEC_INIT_PUSH
;
1701 struct remote_lock
*ref_lock
= NULL
;
1702 struct remote_lock
*info_ref_lock
= NULL
;
1703 struct rev_info revs
;
1704 int delete_branch
= 0;
1705 int force_delete
= 0;
1706 int objects_to_send
;
1710 struct ref
*ref
, *local_refs
;
1712 repo
= xcalloc(1, sizeof(*repo
));
1715 for (i
= 1; i
< argc
; i
++, argv
++) {
1716 const char *arg
= *argv
;
1719 if (!strcmp(arg
, "--all")) {
1720 push_all
= MATCH_REFS_ALL
;
1723 if (!strcmp(arg
, "--force")) {
1727 if (!strcmp(arg
, "--dry-run")) {
1731 if (!strcmp(arg
, "--helper-status")) {
1735 if (!strcmp(arg
, "--verbose")) {
1737 http_is_verbose
= 1;
1740 if (!strcmp(arg
, "-d")) {
1744 if (!strcmp(arg
, "-D")) {
1749 if (!strcmp(arg
, "-h"))
1750 usage(http_push_usage
);
1753 char *path
= strstr(arg
, "//");
1754 str_end_url_with_slash(arg
, &repo
->url
);
1755 repo
->path_len
= strlen(repo
->url
);
1757 repo
->path
= strchr(path
+2, '/');
1759 repo
->path_len
= strlen(repo
->path
);
1763 refspec_appendn(&rs
, argv
, argc
- i
);
1767 #ifndef USE_CURL_MULTI
1768 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1772 usage(http_push_usage
);
1774 if (delete_branch
&& rs
.nr
!= 1)
1775 die("You must specify only one branch name when deleting a remote branch");
1777 setup_git_directory();
1779 memset(remote_dir_exists
, -1, 256);
1781 http_init(NULL
, repo
->url
, 1);
1783 #ifdef USE_CURL_MULTI
1784 is_running_queue
= 0;
1787 /* Verify DAV compliance/lock support */
1788 if (!locking_available()) {
1793 sigchain_push_common(remove_locks_on_signal
);
1795 /* Check whether the remote has server info files */
1796 repo
->can_update_info_refs
= 0;
1797 repo
->has_info_refs
= remote_exists("info/refs");
1798 repo
->has_info_packs
= remote_exists("objects/info/packs");
1799 if (repo
->has_info_refs
) {
1800 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1802 repo
->can_update_info_refs
= 1;
1804 error("cannot lock existing info/refs");
1809 if (repo
->has_info_packs
)
1812 /* Get a list of all local and remote heads to validate refspecs */
1813 local_refs
= get_local_heads();
1814 fprintf(stderr
, "Fetching remote heads...\n");
1815 get_dav_remote_heads();
1816 run_request_queue();
1818 /* Remove a remote branch if -d or -D was specified */
1819 if (delete_branch
) {
1820 const char *branch
= rs
.items
[i
].src
;
1821 if (delete_remote_branch(branch
, force_delete
) == -1) {
1822 fprintf(stderr
, "Unable to delete remote branch %s\n",
1825 printf("error %s cannot remove\n", branch
);
1831 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1836 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1838 printf("error null no match\n");
1844 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1845 struct argv_array commit_argv
= ARGV_ARRAY_INIT
;
1850 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1851 if (delete_remote_branch(ref
->name
, 1) == -1) {
1852 error("Could not remove %s", ref
->name
);
1854 printf("error %s cannot remove\n", ref
->name
);
1857 else if (helper_status
)
1858 printf("ok %s\n", ref
->name
);
1863 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1865 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1867 printf("ok %s up to date\n", ref
->name
);
1872 !is_null_oid(&ref
->old_oid
) &&
1874 if (!has_object_file(&ref
->old_oid
) ||
1875 !ref_newer(&ref
->peer_ref
->new_oid
,
1878 * We do not have the remote ref, or
1879 * we know that the remote ref is not
1880 * an ancestor of what we are trying to
1881 * push. Either way this can be losing
1882 * commits at the remote end and likely
1883 * we were not up to date to begin with.
1885 error("remote '%s' is not an ancestor of\n"
1887 "Maybe you are not up-to-date and "
1888 "need to pull first?",
1890 ref
->peer_ref
->name
);
1892 printf("error %s non-fast forward\n", ref
->name
);
1897 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1900 fprintf(stderr
, "updating '%s'", ref
->name
);
1901 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1902 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1903 fprintf(stderr
, "\n from %s\n to %s\n",
1904 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1907 printf("ok %s\n", ref
->name
);
1911 /* Lock remote branch ref */
1912 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1913 if (ref_lock
== NULL
) {
1914 fprintf(stderr
, "Unable to lock remote branch %s\n",
1917 printf("error %s lock error\n", ref
->name
);
1922 /* Set up revision info for this refspec */
1923 argv_array_push(&commit_argv
, ""); /* ignored */
1924 argv_array_push(&commit_argv
, "--objects");
1925 argv_array_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1926 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1927 argv_array_pushf(&commit_argv
, "^%s",
1928 oid_to_hex(&ref
->old_oid
));
1929 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1930 setup_revisions(commit_argv
.argc
, commit_argv
.argv
, &revs
, NULL
);
1931 revs
.edge_hint
= 0; /* just in case */
1933 /* Generate a list of objects that need to be pushed */
1935 if (prepare_revision_walk(&revs
))
1936 die("revision walk setup failed");
1937 mark_edges_uninteresting(&revs
, NULL
, 0);
1938 objects_to_send
= get_delta(&revs
, ref_lock
);
1939 finish_all_active_slots();
1941 /* Push missing objects to remote, this would be a
1942 convenient time to pack them first if appropriate. */
1944 if (objects_to_send
)
1945 fprintf(stderr
, " sending %d objects\n",
1948 run_request_queue();
1950 /* Update the remote branch if all went well */
1951 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1955 fprintf(stderr
, " done\n");
1957 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1958 unlock_remote(ref_lock
);
1960 argv_array_clear(&commit_argv
);
1963 /* Update remote server info if appropriate */
1964 if (repo
->has_info_refs
&& new_refs
) {
1965 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1966 fprintf(stderr
, "Updating remote server info\n");
1968 update_remote_info_refs(info_ref_lock
);
1970 fprintf(stderr
, "Unable to update server info\n");
1976 unlock_remote(info_ref_lock
);
1981 request
= request_queue_head
;
1982 while (request
!= NULL
) {
1983 next_request
= request
->next
;
1984 release_request(request
);
1985 request
= next_request
;