1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "repository.h"
14 #include "list-objects.h"
19 #include "tree-walk.h"
22 #include "object-store-ll.h"
23 #include "commit-reach.h"
25 #ifdef EXPAT_NEEDS_XMLPARSE_H
31 static const char http_push_usage
[] =
32 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
39 #define XML_STATUS_OK 1
40 #define XML_STATUS_ERROR 0
43 #define PREV_BUF_SIZE 4096
46 #define DAV_LOCK "LOCK"
47 #define DAV_MKCOL "MKCOL"
48 #define DAV_MOVE "MOVE"
49 #define DAV_PROPFIND "PROPFIND"
51 #define DAV_UNLOCK "UNLOCK"
52 #define DAV_DELETE "DELETE"
55 #define DAV_PROP_LOCKWR (1u << 0)
56 #define DAV_PROP_LOCKEX (1u << 1)
57 #define DAV_LOCK_OK (1u << 2)
59 /* DAV XML properties */
60 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
61 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
62 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
63 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
64 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
65 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
66 #define DAV_PROPFIND_RESP ".multistatus.response"
67 #define DAV_PROPFIND_NAME ".multistatus.response.href"
68 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
70 /* DAV request body templates */
71 #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>"
72 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
73 #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>"
76 #define LOCK_REFRESH 30
78 /* Remember to update object flag allocation in object.h */
79 #define LOCAL (1u<<11)
80 #define REMOTE (1u<<12)
81 #define FETCHING (1u<<13)
82 #define PUSHING (1u<<14)
84 /* We allow "recursive" symbolic refs. Only within reason, though */
89 static signed char remote_dir_exists
[256];
91 static int push_verbosely
;
92 static int push_all
= MATCH_REFS_NONE
;
95 static int helper_status
;
97 static struct object_list
*objects
;
104 int can_update_info_refs
;
106 struct packed_git
*packs
;
107 struct remote_lock
*locks
;
110 static struct repo
*repo
;
112 enum transfer_state
{
124 struct transfer_request
{
126 struct packed_git
*target
;
129 struct remote_lock
*lock
;
130 struct curl_slist
*headers
;
131 struct buffer buffer
;
132 enum transfer_state state
;
133 CURLcode curl_result
;
134 char errorstr
[CURL_ERROR_SIZE
];
137 struct active_request_slot
*slot
;
138 struct transfer_request
*next
;
141 static struct transfer_request
*request_queue_head
;
147 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
155 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
159 struct remote_lock
*next
;
162 /* Flags that control remote_ls processing */
163 #define PROCESS_FILES (1u << 0)
164 #define PROCESS_DIRS (1u << 1)
165 #define RECURSIVE (1u << 2)
167 /* Flags that remote_ls passes to callback functions */
168 #define IS_DIR (1u << 0)
170 struct remote_ls_ctx
{
172 void (*userFunc
)(struct remote_ls_ctx
*ls
);
177 struct remote_ls_ctx
*parent
;
180 /* get_dav_token_headers options */
181 enum dav_header_flag
{
182 DAV_HEADER_IF
= (1u << 0),
183 DAV_HEADER_LOCK
= (1u << 1),
184 DAV_HEADER_TIMEOUT
= (1u << 2)
187 static char *xml_entities(const char *s
)
189 struct strbuf buf
= STRBUF_INIT
;
190 strbuf_addstr_xml_quoted(&buf
, s
);
191 return strbuf_detach(&buf
, NULL
);
194 static void curl_setup_http_get(CURL
*curl
, const char *url
,
195 const char *custom_req
)
197 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
198 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
199 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
200 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
203 static void curl_setup_http(CURL
*curl
, const char *url
,
204 const char *custom_req
, struct buffer
*buffer
,
205 curl_write_callback write_fn
)
207 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
208 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
209 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
210 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
211 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
212 curl_easy_setopt(curl
, CURLOPT_SEEKFUNCTION
, seek_buffer
);
213 curl_easy_setopt(curl
, CURLOPT_SEEKDATA
, buffer
);
214 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
215 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
216 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
217 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
220 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
222 struct strbuf buf
= STRBUF_INIT
;
223 struct curl_slist
*dav_headers
= http_copy_default_headers();
225 if (options
& DAV_HEADER_IF
) {
226 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
227 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
230 if (options
& DAV_HEADER_LOCK
) {
231 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
232 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
235 if (options
& DAV_HEADER_TIMEOUT
) {
236 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
237 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
240 strbuf_release(&buf
);
245 static void finish_request(struct transfer_request
*request
);
246 static void release_request(struct transfer_request
*request
);
248 static void process_response(void *callback_data
)
250 struct transfer_request
*request
=
251 (struct transfer_request
*)callback_data
;
253 finish_request(request
);
256 static void start_fetch_loose(struct transfer_request
*request
)
258 struct active_request_slot
*slot
;
259 struct http_object_request
*obj_req
;
261 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
263 request
->state
= ABORTED
;
267 slot
= obj_req
->slot
;
268 slot
->callback_func
= process_response
;
269 slot
->callback_data
= request
;
270 request
->slot
= slot
;
271 request
->userData
= obj_req
;
273 /* Try to get the request started, abort the request on error */
274 request
->state
= RUN_FETCH_LOOSE
;
275 if (!start_active_slot(slot
)) {
276 fprintf(stderr
, "Unable to start GET request\n");
277 repo
->can_update_info_refs
= 0;
278 release_http_object_request(obj_req
);
279 release_request(request
);
283 static void start_mkcol(struct transfer_request
*request
)
285 char *hex
= oid_to_hex(&request
->obj
->oid
);
286 struct active_request_slot
*slot
;
288 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
290 slot
= get_active_slot();
291 slot
->callback_func
= process_response
;
292 slot
->callback_data
= request
;
293 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
294 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
296 if (start_active_slot(slot
)) {
297 request
->slot
= slot
;
298 request
->state
= RUN_MKCOL
;
300 request
->state
= ABORTED
;
301 FREE_AND_NULL(request
->url
);
305 static void start_fetch_packed(struct transfer_request
*request
)
307 struct packed_git
*target
;
309 struct transfer_request
*check_request
= request_queue_head
;
310 struct http_pack_request
*preq
;
312 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
314 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
315 repo
->can_update_info_refs
= 0;
316 release_request(request
);
319 close_pack_index(target
);
320 request
->target
= target
;
322 fprintf(stderr
, "Fetching pack %s\n",
323 hash_to_hex(target
->hash
));
324 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
326 preq
= new_http_pack_request(target
->hash
, repo
->url
);
328 repo
->can_update_info_refs
= 0;
332 /* Make sure there isn't another open request for this pack */
333 while (check_request
) {
334 if (check_request
->state
== RUN_FETCH_PACKED
&&
335 !strcmp(check_request
->url
, preq
->url
)) {
336 release_http_pack_request(preq
);
337 release_request(request
);
340 check_request
= check_request
->next
;
343 preq
->slot
->callback_func
= process_response
;
344 preq
->slot
->callback_data
= request
;
345 request
->slot
= preq
->slot
;
346 request
->userData
= preq
;
348 /* Try to get the request started, abort the request on error */
349 request
->state
= RUN_FETCH_PACKED
;
350 if (!start_active_slot(preq
->slot
)) {
351 fprintf(stderr
, "Unable to start GET request\n");
352 release_http_pack_request(preq
);
353 repo
->can_update_info_refs
= 0;
354 release_request(request
);
358 static void start_put(struct transfer_request
*request
)
360 char *hex
= oid_to_hex(&request
->obj
->oid
);
361 struct active_request_slot
*slot
;
362 struct strbuf buf
= STRBUF_INIT
;
363 enum object_type type
;
371 unpacked
= repo_read_object_file(the_repository
, &request
->obj
->oid
,
373 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, len
);
376 git_deflate_init(&stream
, zlib_compression_level
);
377 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
378 strbuf_init(&request
->buffer
.buf
, size
);
379 request
->buffer
.posn
= 0;
382 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
383 stream
.avail_out
= size
;
386 stream
.next_in
= (void *)hdr
;
387 stream
.avail_in
= hdrlen
;
388 while (git_deflate(&stream
, 0) == Z_OK
)
391 /* Then the data itself.. */
392 stream
.next_in
= unpacked
;
393 stream
.avail_in
= len
;
394 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
396 git_deflate_end(&stream
);
399 request
->buffer
.buf
.len
= stream
.total_out
;
401 strbuf_addstr(&buf
, "Destination: ");
402 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
403 request
->dest
= strbuf_detach(&buf
, NULL
);
405 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
406 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
407 request
->url
= strbuf_detach(&buf
, NULL
);
409 slot
= get_active_slot();
410 slot
->callback_func
= process_response
;
411 slot
->callback_data
= request
;
412 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
413 &request
->buffer
, fwrite_null
);
415 if (start_active_slot(slot
)) {
416 request
->slot
= slot
;
417 request
->state
= RUN_PUT
;
419 request
->state
= ABORTED
;
420 FREE_AND_NULL(request
->url
);
424 static void start_move(struct transfer_request
*request
)
426 struct active_request_slot
*slot
;
427 struct curl_slist
*dav_headers
= http_copy_default_headers();
429 slot
= get_active_slot();
430 slot
->callback_func
= process_response
;
431 slot
->callback_data
= request
;
432 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
433 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
434 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
435 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
437 if (start_active_slot(slot
)) {
438 request
->slot
= slot
;
439 request
->state
= RUN_MOVE
;
441 request
->state
= ABORTED
;
442 FREE_AND_NULL(request
->url
);
446 static int refresh_lock(struct remote_lock
*lock
)
448 struct active_request_slot
*slot
;
449 struct slot_results results
;
450 struct curl_slist
*dav_headers
;
453 lock
->refreshing
= 1;
455 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
457 slot
= get_active_slot();
458 slot
->results
= &results
;
459 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
460 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
462 if (start_active_slot(slot
)) {
463 run_active_slot(slot
);
464 if (results
.curl_result
!= CURLE_OK
) {
465 fprintf(stderr
, "LOCK HTTP error %ld\n",
468 lock
->start_time
= time(NULL
);
473 lock
->refreshing
= 0;
474 curl_slist_free_all(dav_headers
);
479 static void check_locks(void)
481 struct remote_lock
*lock
= repo
->locks
;
482 time_t current_time
= time(NULL
);
486 time_remaining
= lock
->start_time
+ lock
->timeout
-
488 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
489 if (!refresh_lock(lock
)) {
491 "Unable to refresh lock for %s\n",
501 static void release_request(struct transfer_request
*request
)
503 struct transfer_request
*entry
= request_queue_head
;
505 if (request
== request_queue_head
) {
506 request_queue_head
= request
->next
;
508 while (entry
&& entry
->next
!= request
)
511 entry
->next
= request
->next
;
518 static void finish_request(struct transfer_request
*request
)
520 struct http_pack_request
*preq
;
521 struct http_object_request
*obj_req
;
523 request
->curl_result
= request
->slot
->curl_result
;
524 request
->http_code
= request
->slot
->http_code
;
525 request
->slot
= NULL
;
527 /* Keep locks active */
530 if (request
->headers
)
531 curl_slist_free_all(request
->headers
);
533 /* URL is reused for MOVE after PUT and used during FETCH */
534 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
535 FREE_AND_NULL(request
->url
);
538 if (request
->state
== RUN_MKCOL
) {
539 if (request
->curl_result
== CURLE_OK
||
540 request
->http_code
== 405) {
541 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
544 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
545 oid_to_hex(&request
->obj
->oid
),
546 request
->curl_result
, request
->http_code
);
547 request
->state
= ABORTED
;
550 } else if (request
->state
== RUN_PUT
) {
551 if (request
->curl_result
== CURLE_OK
) {
554 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
555 oid_to_hex(&request
->obj
->oid
),
556 request
->curl_result
, request
->http_code
);
557 request
->state
= ABORTED
;
560 } else if (request
->state
== RUN_MOVE
) {
561 if (request
->curl_result
== CURLE_OK
) {
563 fprintf(stderr
, " sent %s\n",
564 oid_to_hex(&request
->obj
->oid
));
565 request
->obj
->flags
|= REMOTE
;
566 release_request(request
);
568 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
569 oid_to_hex(&request
->obj
->oid
),
570 request
->curl_result
, request
->http_code
);
571 request
->state
= ABORTED
;
574 } else if (request
->state
== RUN_FETCH_LOOSE
) {
575 obj_req
= (struct http_object_request
*)request
->userData
;
577 if (finish_http_object_request(obj_req
) == 0)
578 if (obj_req
->rename
== 0)
579 request
->obj
->flags
|= (LOCAL
| REMOTE
);
581 /* Try fetching packed if necessary */
582 if (request
->obj
->flags
& LOCAL
) {
583 release_http_object_request(obj_req
);
584 release_request(request
);
586 start_fetch_packed(request
);
588 } else if (request
->state
== RUN_FETCH_PACKED
) {
590 if (request
->curl_result
!= CURLE_OK
) {
591 fprintf(stderr
, "Unable to get pack file %s\n%s",
592 request
->url
, curl_errorstr
);
594 preq
= (struct http_pack_request
*)request
->userData
;
597 if (finish_http_pack_request(preq
) == 0)
599 release_http_pack_request(preq
);
603 repo
->can_update_info_refs
= 0;
605 http_install_packfile(request
->target
, &repo
->packs
);
606 release_request(request
);
610 static int is_running_queue
;
611 static int fill_active_slot(void *data UNUSED
)
613 struct transfer_request
*request
;
615 if (aborted
|| !is_running_queue
)
618 for (request
= request_queue_head
; request
; request
= request
->next
) {
619 if (request
->state
== NEED_FETCH
) {
620 start_fetch_loose(request
);
622 } else if (pushing
&& request
->state
== NEED_PUSH
) {
623 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
626 start_mkcol(request
);
634 static void get_remote_object_list(unsigned char parent
);
636 static void add_fetch_request(struct object
*obj
)
638 struct transfer_request
*request
;
643 * Don't fetch the object if it's known to exist locally
644 * or is already in the request queue
646 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
647 get_remote_object_list(obj
->oid
.hash
[0]);
648 if (obj
->flags
& (LOCAL
| FETCHING
))
651 obj
->flags
|= FETCHING
;
652 request
= xmalloc(sizeof(*request
));
655 request
->lock
= NULL
;
656 request
->headers
= NULL
;
657 request
->state
= NEED_FETCH
;
658 request
->next
= request_queue_head
;
659 request_queue_head
= request
;
665 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
667 struct transfer_request
*request
;
668 struct packed_git
*target
;
670 /* Keep locks active */
674 * Don't push the object if it's known to exist on the remote
675 * or is already in the request queue
677 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
678 get_remote_object_list(obj
->oid
.hash
[0]);
679 if (obj
->flags
& (REMOTE
| PUSHING
))
681 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
683 obj
->flags
|= REMOTE
;
687 obj
->flags
|= PUSHING
;
688 request
= xmalloc(sizeof(*request
));
691 request
->lock
= lock
;
692 request
->headers
= NULL
;
693 request
->state
= NEED_PUSH
;
694 request
->next
= request_queue_head
;
695 request_queue_head
= request
;
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
);
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 UNUSED
)
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_WRITEDATA
, &in_buffer
);
895 CALLOC_ARRAY(lock
, 1);
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
= lock
->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 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
1022 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
1024 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1027 if (hex_to_bytes(oid
->hash
, path
, 1))
1030 path
++; /* skip '/' */
1032 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1035 static void process_ls_object(struct remote_ls_ctx
*ls
)
1037 unsigned int *parent
= (unsigned int *)ls
->userData
;
1038 const char *path
= ls
->dentry_name
;
1039 struct object_id oid
;
1041 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1042 remote_dir_exists
[*parent
] = 1;
1046 if (!skip_prefix(path
, "objects/", &path
) ||
1047 get_oid_hex_from_objpath(path
, &oid
))
1050 one_remote_object(&oid
);
1053 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1055 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1056 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1060 if (!(ls
->dentry_flags
& IS_DIR
))
1061 one_remote_ref(ls
->dentry_name
);
1064 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1066 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1069 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1070 if (ls
->dentry_flags
& IS_DIR
) {
1072 /* ensure collection names end with slash */
1073 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1075 if (ls
->flags
& PROCESS_DIRS
) {
1078 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1079 ls
->flags
& RECURSIVE
) {
1080 remote_ls(ls
->dentry_name
,
1085 } else if (ls
->flags
& PROCESS_FILES
) {
1088 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1089 char *path
= ctx
->cdata
;
1090 if (*ctx
->cdata
== 'h') {
1091 path
= strstr(path
, "//");
1093 path
= strchr(path
+2, '/');
1097 const char *url
= repo
->url
;
1100 if (strncmp(path
, url
, repo
->path_len
))
1101 error("Parsed path '%s' does not match url: '%s'",
1104 path
+= repo
->path_len
;
1105 ls
->dentry_name
= xstrdup(path
);
1108 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1109 ls
->dentry_flags
|= IS_DIR
;
1111 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1112 FREE_AND_NULL(ls
->dentry_name
);
1113 ls
->dentry_flags
= 0;
1118 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1119 * should _only_ heed the information from that file, instead of trying to
1120 * determine the refs from the remote file system (badly: it does not even
1121 * know about packed-refs).
1123 static void remote_ls(const char *path
, int flags
,
1124 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1127 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1128 struct active_request_slot
*slot
;
1129 struct slot_results results
;
1130 struct strbuf in_buffer
= STRBUF_INIT
;
1131 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1132 struct curl_slist
*dav_headers
= http_copy_default_headers();
1134 struct remote_ls_ctx ls
;
1137 ls
.path
= xstrdup(path
);
1138 ls
.dentry_name
= NULL
;
1139 ls
.dentry_flags
= 0;
1140 ls
.userData
= userData
;
1141 ls
.userFunc
= userFunc
;
1143 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1145 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1146 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1148 slot
= get_active_slot();
1149 slot
->results
= &results
;
1150 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1151 &out_buffer
, fwrite_buffer
);
1152 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1153 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1155 if (start_active_slot(slot
)) {
1156 run_active_slot(slot
);
1157 if (results
.curl_result
== CURLE_OK
) {
1158 XML_Parser parser
= XML_ParserCreate(NULL
);
1159 enum XML_Status result
;
1160 ctx
.name
= xcalloc(10, 1);
1163 ctx
.userFunc
= handle_remote_ls_ctx
;
1165 XML_SetUserData(parser
, &ctx
);
1166 XML_SetElementHandler(parser
, xml_start_tag
,
1168 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1169 result
= XML_Parse(parser
, in_buffer
.buf
,
1173 if (result
!= XML_STATUS_OK
) {
1174 fprintf(stderr
, "XML error: %s\n",
1176 XML_GetErrorCode(parser
)));
1178 XML_ParserFree(parser
);
1181 fprintf(stderr
, "Unable to start PROPFIND request\n");
1186 strbuf_release(&out_buffer
.buf
);
1187 strbuf_release(&in_buffer
);
1188 curl_slist_free_all(dav_headers
);
1191 static void get_remote_object_list(unsigned char parent
)
1193 char path
[] = "objects/XX/";
1194 static const char hex
[] = "0123456789abcdef";
1195 unsigned int val
= parent
;
1197 path
[8] = hex
[val
>> 4];
1198 path
[9] = hex
[val
& 0xf];
1199 remote_dir_exists
[val
] = 0;
1200 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1201 process_ls_object
, &val
);
1204 static int locking_available(void)
1206 struct active_request_slot
*slot
;
1207 struct slot_results results
;
1208 struct strbuf in_buffer
= STRBUF_INIT
;
1209 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1210 struct curl_slist
*dav_headers
= http_copy_default_headers();
1215 escaped
= xml_entities(repo
->url
);
1216 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1219 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1220 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1222 slot
= get_active_slot();
1223 slot
->results
= &results
;
1224 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1225 &out_buffer
, fwrite_buffer
);
1226 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1227 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1229 if (start_active_slot(slot
)) {
1230 run_active_slot(slot
);
1231 if (results
.curl_result
== CURLE_OK
) {
1232 XML_Parser parser
= XML_ParserCreate(NULL
);
1233 enum XML_Status result
;
1234 ctx
.name
= xcalloc(10, 1);
1237 ctx
.userFunc
= handle_lockprop_ctx
;
1238 ctx
.userData
= &lock_flags
;
1239 XML_SetUserData(parser
, &ctx
);
1240 XML_SetElementHandler(parser
, xml_start_tag
,
1242 result
= XML_Parse(parser
, in_buffer
.buf
,
1246 if (result
!= XML_STATUS_OK
) {
1247 fprintf(stderr
, "XML error: %s\n",
1249 XML_GetErrorCode(parser
)));
1252 XML_ParserFree(parser
);
1254 error("no DAV locking support on %s",
1258 error("Cannot access URL %s, return code %d",
1259 repo
->url
, results
.curl_result
);
1263 error("Unable to start PROPFIND request on %s", repo
->url
);
1266 strbuf_release(&out_buffer
.buf
);
1267 strbuf_release(&in_buffer
);
1268 curl_slist_free_all(dav_headers
);
1273 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1275 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1279 return &entry
->next
;
1282 static struct object_list
**process_blob(struct blob
*blob
,
1283 struct object_list
**p
)
1285 struct object
*obj
= &blob
->object
;
1287 obj
->flags
|= LOCAL
;
1289 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1293 return add_one_object(obj
, p
);
1296 static struct object_list
**process_tree(struct tree
*tree
,
1297 struct object_list
**p
)
1299 struct object
*obj
= &tree
->object
;
1300 struct tree_desc desc
;
1301 struct name_entry entry
;
1303 obj
->flags
|= LOCAL
;
1305 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1307 if (parse_tree(tree
) < 0)
1308 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1311 p
= add_one_object(obj
, p
);
1313 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
1315 while (tree_entry(&desc
, &entry
))
1316 switch (object_type(entry
.mode
)) {
1318 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1322 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1326 /* Subproject commit - not in this repository */
1330 free_tree_buffer(tree
);
1334 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1337 struct commit
*commit
;
1338 struct object_list
**p
= &objects
;
1341 while ((commit
= get_revision(revs
)) != NULL
) {
1342 p
= process_tree(repo_get_commit_tree(the_repository
, commit
),
1344 commit
->object
.flags
|= LOCAL
;
1345 if (!(commit
->object
.flags
& UNINTERESTING
))
1346 count
+= add_send_request(&commit
->object
, lock
);
1349 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1350 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1351 struct object
*obj
= entry
->item
;
1352 const char *name
= entry
->name
;
1354 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1356 if (obj
->type
== OBJ_TAG
) {
1358 p
= add_one_object(obj
, p
);
1361 if (obj
->type
== OBJ_TREE
) {
1362 p
= process_tree((struct tree
*)obj
, p
);
1365 if (obj
->type
== OBJ_BLOB
) {
1366 p
= process_blob((struct blob
*)obj
, p
);
1369 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1373 if (!(objects
->item
->flags
& UNINTERESTING
))
1374 count
+= add_send_request(objects
->item
, lock
);
1375 objects
= objects
->next
;
1381 static int update_remote(const struct object_id
*oid
, struct remote_lock
*lock
)
1383 struct active_request_slot
*slot
;
1384 struct slot_results results
;
1385 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1386 struct curl_slist
*dav_headers
;
1388 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1390 strbuf_addf(&out_buffer
.buf
, "%s\n", oid_to_hex(oid
));
1392 slot
= get_active_slot();
1393 slot
->results
= &results
;
1394 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1395 &out_buffer
, fwrite_null
);
1396 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1398 if (start_active_slot(slot
)) {
1399 run_active_slot(slot
);
1400 strbuf_release(&out_buffer
.buf
);
1401 if (results
.curl_result
!= CURLE_OK
) {
1403 "PUT error: curl result=%d, HTTP code=%ld\n",
1404 results
.curl_result
, results
.http_code
);
1405 /* We should attempt recovery? */
1409 strbuf_release(&out_buffer
.buf
);
1410 fprintf(stderr
, "Unable to start PUT request\n");
1417 static struct ref
*remote_refs
;
1419 static void one_remote_ref(const char *refname
)
1424 ref
= alloc_ref(refname
);
1426 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1428 "Unable to fetch ref %s from %s\n",
1429 refname
, repo
->url
);
1435 * Fetch a copy of the object if it doesn't exist locally - it
1436 * may be required for updating server info later.
1438 if (repo
->can_update_info_refs
&& !repo_has_object_file(the_repository
, &ref
->old_oid
)) {
1439 obj
= lookup_unknown_object(the_repository
, &ref
->old_oid
);
1440 fprintf(stderr
, " fetch %s for %s\n",
1441 oid_to_hex(&ref
->old_oid
), refname
);
1442 add_fetch_request(obj
);
1445 ref
->next
= remote_refs
;
1449 static void get_dav_remote_heads(void)
1451 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1454 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1456 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1460 ref
= alloc_ref(ls
->dentry_name
);
1462 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1464 "Unable to fetch ref %s from %s\n",
1465 ls
->dentry_name
, repo
->url
);
1471 o
= parse_object(the_repository
, &ref
->old_oid
);
1474 "Unable to parse object %s for remote ref %s\n",
1475 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1481 strbuf_addf(buf
, "%s\t%s\n",
1482 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1484 if (o
->type
== OBJ_TAG
) {
1485 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1487 strbuf_addf(buf
, "%s\t%s^{}\n",
1488 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1493 static void update_remote_info_refs(struct remote_lock
*lock
)
1495 struct buffer buffer
= { STRBUF_INIT
, 0 };
1496 struct active_request_slot
*slot
;
1497 struct slot_results results
;
1498 struct curl_slist
*dav_headers
;
1500 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1501 add_remote_info_ref
, &buffer
.buf
);
1503 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1505 slot
= get_active_slot();
1506 slot
->results
= &results
;
1507 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1508 &buffer
, fwrite_null
);
1509 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1511 if (start_active_slot(slot
)) {
1512 run_active_slot(slot
);
1513 if (results
.curl_result
!= CURLE_OK
) {
1515 "PUT error: curl result=%d, HTTP code=%ld\n",
1516 results
.curl_result
, results
.http_code
);
1520 strbuf_release(&buffer
.buf
);
1523 static int remote_exists(const char *path
)
1525 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1529 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1533 case HTTP_MISSING_TARGET
:
1537 error("unable to access '%s': %s", url
, curl_errorstr
);
1546 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1548 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1549 struct strbuf buffer
= STRBUF_INIT
;
1552 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1553 die("Couldn't get %s for remote symref\n%s", url
,
1557 FREE_AND_NULL(*symref
);
1558 oidclr(oid
, the_repository
->hash_algo
);
1560 if (buffer
.len
== 0)
1563 /* Cut off trailing newline. */
1564 strbuf_rtrim(&buffer
);
1566 /* If it's a symref, set the refname; otherwise try for a sha1 */
1567 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1568 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1570 get_oid_hex(buffer
.buf
, oid
);
1573 strbuf_release(&buffer
);
1576 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1578 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1579 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1581 int ret
= repo_in_merge_bases(the_repository
, branch
, head
);
1588 static int delete_remote_branch(const char *pattern
, int force
)
1590 struct ref
*refs
= remote_refs
;
1591 struct ref
*remote_ref
= NULL
;
1592 struct object_id head_oid
;
1593 char *symref
= NULL
;
1595 int patlen
= strlen(pattern
);
1597 struct active_request_slot
*slot
;
1598 struct slot_results results
;
1601 /* Find the remote branch(es) matching the specified branch name */
1602 for (match
= 0; refs
; refs
= refs
->next
) {
1603 char *name
= refs
->name
;
1604 int namelen
= strlen(name
);
1605 if (namelen
< patlen
||
1606 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1608 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1614 return error("No remote branch matches %s", pattern
);
1616 return error("More than one remote branch matches %s",
1620 * Remote HEAD must be a symref (not exactly foolproof; a remote
1621 * symlink to a symref will look like a symref)
1623 fetch_symref("HEAD", &symref
, &head_oid
);
1625 return error("Remote HEAD is not a symref");
1627 /* Remote branch must not be the remote HEAD */
1628 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1629 if (!strcmp(remote_ref
->name
, symref
))
1630 return error("Remote branch %s is the current HEAD",
1632 fetch_symref(symref
, &symref
, &head_oid
);
1635 /* Run extra sanity checks if delete is not forced */
1637 /* Remote HEAD must resolve to a known object */
1639 return error("Remote HEAD symrefs too deep");
1640 if (is_null_oid(&head_oid
))
1641 return error("Unable to resolve remote HEAD");
1642 if (!repo_has_object_file(the_repository
, &head_oid
))
1643 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1645 /* Remote branch must resolve to a known object */
1646 if (is_null_oid(&remote_ref
->old_oid
))
1647 return error("Unable to resolve remote branch %s",
1649 if (!repo_has_object_file(the_repository
, &remote_ref
->old_oid
))
1650 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
));
1652 /* Remote branch must be an ancestor of remote HEAD */
1653 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1654 return error("The branch '%s' is not an ancestor "
1655 "of your current HEAD.\n"
1656 "If you are sure you want to delete it,"
1657 " run:\n\t'git http-push -D %s %s'",
1658 remote_ref
->name
, repo
->url
, pattern
);
1662 /* Send delete request */
1663 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1666 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1667 slot
= get_active_slot();
1668 slot
->results
= &results
;
1669 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1670 if (start_active_slot(slot
)) {
1671 run_active_slot(slot
);
1673 if (results
.curl_result
!= CURLE_OK
)
1674 return error("DELETE request failed (%d/%ld)",
1675 results
.curl_result
, results
.http_code
);
1678 return error("Unable to start DELETE request");
1684 static void run_request_queue(void)
1686 is_running_queue
= 1;
1687 fill_active_slots();
1688 add_fill_function(NULL
, fill_active_slot
);
1690 finish_all_active_slots();
1691 fill_active_slots();
1692 } while (request_queue_head
&& !aborted
);
1694 is_running_queue
= 0;
1697 int cmd_main(int argc
, const char **argv
)
1699 struct transfer_request
*request
;
1700 struct transfer_request
*next_request
;
1701 struct refspec rs
= REFSPEC_INIT_PUSH
;
1702 struct remote_lock
*ref_lock
= NULL
;
1703 struct remote_lock
*info_ref_lock
= NULL
;
1704 int delete_branch
= 0;
1705 int force_delete
= 0;
1706 int objects_to_send
;
1710 struct ref
*ref
, *local_refs
;
1712 CALLOC_ARRAY(repo
, 1);
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
);
1768 usage(http_push_usage
);
1770 if (delete_branch
&& rs
.nr
!= 1)
1771 die("You must specify only one branch name when deleting a remote branch");
1773 setup_git_directory();
1775 memset(remote_dir_exists
, -1, 256);
1777 http_init(NULL
, repo
->url
, 1);
1779 is_running_queue
= 0;
1781 /* Verify DAV compliance/lock support */
1782 if (!locking_available()) {
1787 sigchain_push_common(remove_locks_on_signal
);
1789 /* Check whether the remote has server info files */
1790 repo
->can_update_info_refs
= 0;
1791 repo
->has_info_refs
= remote_exists("info/refs");
1792 repo
->has_info_packs
= remote_exists("objects/info/packs");
1793 if (repo
->has_info_refs
) {
1794 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1796 repo
->can_update_info_refs
= 1;
1798 error("cannot lock existing info/refs");
1803 if (repo
->has_info_packs
)
1806 /* Get a list of all local and remote heads to validate refspecs */
1807 local_refs
= get_local_heads();
1808 fprintf(stderr
, "Fetching remote heads...\n");
1809 get_dav_remote_heads();
1810 run_request_queue();
1812 /* Remove a remote branch if -d or -D was specified */
1813 if (delete_branch
) {
1814 const char *branch
= rs
.items
[i
].src
;
1815 if (delete_remote_branch(branch
, force_delete
) == -1) {
1816 fprintf(stderr
, "Unable to delete remote branch %s\n",
1819 printf("error %s cannot remove\n", branch
);
1825 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1830 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1832 printf("error null no match\n");
1838 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1839 struct rev_info revs
;
1840 struct strvec commit_argv
= STRVEC_INIT
;
1845 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1846 if (delete_remote_branch(ref
->name
, 1) == -1) {
1847 error("Could not remove %s", ref
->name
);
1849 printf("error %s cannot remove\n", ref
->name
);
1852 else if (helper_status
)
1853 printf("ok %s\n", ref
->name
);
1858 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1860 /* stable plumbing output; do not modify or localize */
1861 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1863 printf("ok %s up to date\n", ref
->name
);
1868 !is_null_oid(&ref
->old_oid
) &&
1870 if (!repo_has_object_file(the_repository
, &ref
->old_oid
) ||
1871 !ref_newer(&ref
->peer_ref
->new_oid
,
1874 * We do not have the remote ref, or
1875 * we know that the remote ref is not
1876 * an ancestor of what we are trying to
1877 * push. Either way this can be losing
1878 * commits at the remote end and likely
1879 * we were not up to date to begin with.
1881 /* stable plumbing output; do not modify or localize */
1882 error("remote '%s' is not an ancestor of\n"
1884 "Maybe you are not up-to-date and "
1885 "need to pull first?",
1887 ref
->peer_ref
->name
);
1889 printf("error %s non-fast forward\n", ref
->name
);
1894 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1897 fprintf(stderr
, "updating '%s'", ref
->name
);
1898 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1899 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1900 fprintf(stderr
, "\n from %s\n to %s\n",
1901 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1904 printf("ok %s\n", ref
->name
);
1908 /* Lock remote branch ref */
1909 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1911 fprintf(stderr
, "Unable to lock remote branch %s\n",
1914 printf("error %s lock error\n", ref
->name
);
1919 /* Set up revision info for this refspec */
1920 strvec_push(&commit_argv
, ""); /* ignored */
1921 strvec_push(&commit_argv
, "--objects");
1922 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1923 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1924 strvec_pushf(&commit_argv
, "^%s",
1925 oid_to_hex(&ref
->old_oid
));
1926 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1927 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1928 revs
.edge_hint
= 0; /* just in case */
1930 /* Generate a list of objects that need to be pushed */
1932 if (prepare_revision_walk(&revs
))
1933 die("revision walk setup failed");
1934 mark_edges_uninteresting(&revs
, NULL
, 0);
1935 objects_to_send
= get_delta(&revs
, ref_lock
);
1936 finish_all_active_slots();
1938 /* Push missing objects to remote, this would be a
1939 convenient time to pack them first if appropriate. */
1941 if (objects_to_send
)
1942 fprintf(stderr
, " sending %d objects\n",
1945 run_request_queue();
1947 /* Update the remote branch if all went well */
1948 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1952 fprintf(stderr
, " done\n");
1954 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1955 unlock_remote(ref_lock
);
1957 strvec_clear(&commit_argv
);
1958 release_revisions(&revs
);
1961 /* Update remote server info if appropriate */
1962 if (repo
->has_info_refs
&& new_refs
) {
1963 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1964 fprintf(stderr
, "Updating remote server info\n");
1966 update_remote_info_refs(info_ref_lock
);
1968 fprintf(stderr
, "Unable to update server info\n");
1974 unlock_remote(info_ref_lock
);
1979 request
= request_queue_head
;
1980 while (request
!= NULL
) {
1981 next_request
= request
->next
;
1982 release_request(request
);
1983 request
= next_request
;