3 #include "repository.h"
13 #include "list-objects.h"
17 #include "object-store.h"
18 #include "commit-reach.h"
20 #ifdef EXPAT_NEEDS_XMLPARSE_H
26 static const char http_push_usage
[] =
27 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
34 #define XML_STATUS_OK 1
35 #define XML_STATUS_ERROR 0
38 #define PREV_BUF_SIZE 4096
41 #define DAV_LOCK "LOCK"
42 #define DAV_MKCOL "MKCOL"
43 #define DAV_MOVE "MOVE"
44 #define DAV_PROPFIND "PROPFIND"
46 #define DAV_UNLOCK "UNLOCK"
47 #define DAV_DELETE "DELETE"
50 #define DAV_PROP_LOCKWR (1u << 0)
51 #define DAV_PROP_LOCKEX (1u << 1)
52 #define DAV_LOCK_OK (1u << 2)
54 /* DAV XML properties */
55 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
56 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
57 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
58 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
59 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
60 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
61 #define DAV_PROPFIND_RESP ".multistatus.response"
62 #define DAV_PROPFIND_NAME ".multistatus.response.href"
63 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
65 /* DAV request body templates */
66 #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>"
67 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
68 #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>"
71 #define LOCK_REFRESH 30
73 /* Remember to update object flag allocation in object.h */
74 #define LOCAL (1u<<11)
75 #define REMOTE (1u<<12)
76 #define FETCHING (1u<<13)
77 #define PUSHING (1u<<14)
79 /* We allow "recursive" symbolic refs. Only within reason, though */
84 static signed char remote_dir_exists
[256];
86 static int push_verbosely
;
87 static int push_all
= MATCH_REFS_NONE
;
90 static int helper_status
;
92 static struct object_list
*objects
;
99 int can_update_info_refs
;
101 struct packed_git
*packs
;
102 struct remote_lock
*locks
;
105 static struct repo
*repo
;
107 enum transfer_state
{
119 struct transfer_request
{
121 struct packed_git
*target
;
124 struct remote_lock
*lock
;
125 struct curl_slist
*headers
;
126 struct buffer buffer
;
127 enum transfer_state state
;
128 CURLcode curl_result
;
129 char errorstr
[CURL_ERROR_SIZE
];
132 struct active_request_slot
*slot
;
133 struct transfer_request
*next
;
136 static struct transfer_request
*request_queue_head
;
142 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
150 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
154 struct remote_lock
*next
;
157 /* Flags that control remote_ls processing */
158 #define PROCESS_FILES (1u << 0)
159 #define PROCESS_DIRS (1u << 1)
160 #define RECURSIVE (1u << 2)
162 /* Flags that remote_ls passes to callback functions */
163 #define IS_DIR (1u << 0)
165 struct remote_ls_ctx
{
167 void (*userFunc
)(struct remote_ls_ctx
*ls
);
172 struct remote_ls_ctx
*parent
;
175 /* get_dav_token_headers options */
176 enum dav_header_flag
{
177 DAV_HEADER_IF
= (1u << 0),
178 DAV_HEADER_LOCK
= (1u << 1),
179 DAV_HEADER_TIMEOUT
= (1u << 2)
182 static char *xml_entities(const char *s
)
184 struct strbuf buf
= STRBUF_INIT
;
185 strbuf_addstr_xml_quoted(&buf
, s
);
186 return strbuf_detach(&buf
, NULL
);
189 static void curl_setup_http_get(CURL
*curl
, const char *url
,
190 const char *custom_req
)
192 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
193 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
194 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
195 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
198 static void curl_setup_http(CURL
*curl
, const char *url
,
199 const char *custom_req
, struct buffer
*buffer
,
200 curl_write_callback write_fn
)
202 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
203 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
204 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
205 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
206 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
207 curl_easy_setopt(curl
, CURLOPT_SEEKFUNCTION
, seek_buffer
);
208 curl_easy_setopt(curl
, CURLOPT_SEEKDATA
, 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 static void start_fetch_loose(struct transfer_request
*request
)
253 struct active_request_slot
*slot
;
254 struct http_object_request
*obj_req
;
256 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
258 request
->state
= ABORTED
;
262 slot
= obj_req
->slot
;
263 slot
->callback_func
= process_response
;
264 slot
->callback_data
= request
;
265 request
->slot
= slot
;
266 request
->userData
= obj_req
;
268 /* Try to get the request started, abort the request on error */
269 request
->state
= RUN_FETCH_LOOSE
;
270 if (!start_active_slot(slot
)) {
271 fprintf(stderr
, "Unable to start GET request\n");
272 repo
->can_update_info_refs
= 0;
273 release_http_object_request(obj_req
);
274 release_request(request
);
278 static void start_mkcol(struct transfer_request
*request
)
280 char *hex
= oid_to_hex(&request
->obj
->oid
);
281 struct active_request_slot
*slot
;
283 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
285 slot
= get_active_slot();
286 slot
->callback_func
= process_response
;
287 slot
->callback_data
= request
;
288 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
289 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
291 if (start_active_slot(slot
)) {
292 request
->slot
= slot
;
293 request
->state
= RUN_MKCOL
;
295 request
->state
= ABORTED
;
296 FREE_AND_NULL(request
->url
);
300 static void start_fetch_packed(struct transfer_request
*request
)
302 struct packed_git
*target
;
304 struct transfer_request
*check_request
= request_queue_head
;
305 struct http_pack_request
*preq
;
307 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
309 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
310 repo
->can_update_info_refs
= 0;
311 release_request(request
);
314 close_pack_index(target
);
315 request
->target
= target
;
317 fprintf(stderr
, "Fetching pack %s\n",
318 hash_to_hex(target
->hash
));
319 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
321 preq
= new_http_pack_request(target
->hash
, repo
->url
);
323 repo
->can_update_info_refs
= 0;
327 /* Make sure there isn't another open request for this pack */
328 while (check_request
) {
329 if (check_request
->state
== RUN_FETCH_PACKED
&&
330 !strcmp(check_request
->url
, preq
->url
)) {
331 release_http_pack_request(preq
);
332 release_request(request
);
335 check_request
= check_request
->next
;
338 preq
->slot
->callback_func
= process_response
;
339 preq
->slot
->callback_data
= request
;
340 request
->slot
= preq
->slot
;
341 request
->userData
= preq
;
343 /* Try to get the request started, abort the request on error */
344 request
->state
= RUN_FETCH_PACKED
;
345 if (!start_active_slot(preq
->slot
)) {
346 fprintf(stderr
, "Unable to start GET request\n");
347 release_http_pack_request(preq
);
348 repo
->can_update_info_refs
= 0;
349 release_request(request
);
353 static void start_put(struct transfer_request
*request
)
355 char *hex
= oid_to_hex(&request
->obj
->oid
);
356 struct active_request_slot
*slot
;
357 struct strbuf buf
= STRBUF_INIT
;
358 enum object_type type
;
366 unpacked
= repo_read_object_file(the_repository
, &request
->obj
->oid
,
368 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, len
);
371 git_deflate_init(&stream
, zlib_compression_level
);
372 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
373 strbuf_init(&request
->buffer
.buf
, size
);
374 request
->buffer
.posn
= 0;
377 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
378 stream
.avail_out
= size
;
381 stream
.next_in
= (void *)hdr
;
382 stream
.avail_in
= hdrlen
;
383 while (git_deflate(&stream
, 0) == Z_OK
)
386 /* Then the data itself.. */
387 stream
.next_in
= unpacked
;
388 stream
.avail_in
= len
;
389 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
391 git_deflate_end(&stream
);
394 request
->buffer
.buf
.len
= stream
.total_out
;
396 strbuf_addstr(&buf
, "Destination: ");
397 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
398 request
->dest
= strbuf_detach(&buf
, NULL
);
400 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
401 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
402 request
->url
= strbuf_detach(&buf
, NULL
);
404 slot
= get_active_slot();
405 slot
->callback_func
= process_response
;
406 slot
->callback_data
= request
;
407 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
408 &request
->buffer
, fwrite_null
);
410 if (start_active_slot(slot
)) {
411 request
->slot
= slot
;
412 request
->state
= RUN_PUT
;
414 request
->state
= ABORTED
;
415 FREE_AND_NULL(request
->url
);
419 static void start_move(struct transfer_request
*request
)
421 struct active_request_slot
*slot
;
422 struct curl_slist
*dav_headers
= http_copy_default_headers();
424 slot
= get_active_slot();
425 slot
->callback_func
= process_response
;
426 slot
->callback_data
= request
;
427 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
428 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
429 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
430 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
432 if (start_active_slot(slot
)) {
433 request
->slot
= slot
;
434 request
->state
= RUN_MOVE
;
436 request
->state
= ABORTED
;
437 FREE_AND_NULL(request
->url
);
441 static int refresh_lock(struct remote_lock
*lock
)
443 struct active_request_slot
*slot
;
444 struct slot_results results
;
445 struct curl_slist
*dav_headers
;
448 lock
->refreshing
= 1;
450 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
452 slot
= get_active_slot();
453 slot
->results
= &results
;
454 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
455 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
457 if (start_active_slot(slot
)) {
458 run_active_slot(slot
);
459 if (results
.curl_result
!= CURLE_OK
) {
460 fprintf(stderr
, "LOCK HTTP error %ld\n",
463 lock
->start_time
= time(NULL
);
468 lock
->refreshing
= 0;
469 curl_slist_free_all(dav_headers
);
474 static void check_locks(void)
476 struct remote_lock
*lock
= repo
->locks
;
477 time_t current_time
= time(NULL
);
481 time_remaining
= lock
->start_time
+ lock
->timeout
-
483 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
484 if (!refresh_lock(lock
)) {
486 "Unable to refresh lock for %s\n",
496 static void release_request(struct transfer_request
*request
)
498 struct transfer_request
*entry
= request_queue_head
;
500 if (request
== request_queue_head
) {
501 request_queue_head
= request
->next
;
503 while (entry
&& entry
->next
!= request
)
506 entry
->next
= request
->next
;
513 static void finish_request(struct transfer_request
*request
)
515 struct http_pack_request
*preq
;
516 struct http_object_request
*obj_req
;
518 request
->curl_result
= request
->slot
->curl_result
;
519 request
->http_code
= request
->slot
->http_code
;
520 request
->slot
= NULL
;
522 /* Keep locks active */
525 if (request
->headers
)
526 curl_slist_free_all(request
->headers
);
528 /* URL is reused for MOVE after PUT and used during FETCH */
529 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
530 FREE_AND_NULL(request
->url
);
533 if (request
->state
== RUN_MKCOL
) {
534 if (request
->curl_result
== CURLE_OK
||
535 request
->http_code
== 405) {
536 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
539 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
540 oid_to_hex(&request
->obj
->oid
),
541 request
->curl_result
, request
->http_code
);
542 request
->state
= ABORTED
;
545 } else if (request
->state
== RUN_PUT
) {
546 if (request
->curl_result
== CURLE_OK
) {
549 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
550 oid_to_hex(&request
->obj
->oid
),
551 request
->curl_result
, request
->http_code
);
552 request
->state
= ABORTED
;
555 } else if (request
->state
== RUN_MOVE
) {
556 if (request
->curl_result
== CURLE_OK
) {
558 fprintf(stderr
, " sent %s\n",
559 oid_to_hex(&request
->obj
->oid
));
560 request
->obj
->flags
|= REMOTE
;
561 release_request(request
);
563 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
564 oid_to_hex(&request
->obj
->oid
),
565 request
->curl_result
, request
->http_code
);
566 request
->state
= ABORTED
;
569 } else if (request
->state
== RUN_FETCH_LOOSE
) {
570 obj_req
= (struct http_object_request
*)request
->userData
;
572 if (finish_http_object_request(obj_req
) == 0)
573 if (obj_req
->rename
== 0)
574 request
->obj
->flags
|= (LOCAL
| REMOTE
);
576 /* Try fetching packed if necessary */
577 if (request
->obj
->flags
& LOCAL
) {
578 release_http_object_request(obj_req
);
579 release_request(request
);
581 start_fetch_packed(request
);
583 } else if (request
->state
== RUN_FETCH_PACKED
) {
585 if (request
->curl_result
!= CURLE_OK
) {
586 fprintf(stderr
, "Unable to get pack file %s\n%s",
587 request
->url
, curl_errorstr
);
589 preq
= (struct http_pack_request
*)request
->userData
;
592 if (finish_http_pack_request(preq
) == 0)
594 release_http_pack_request(preq
);
598 repo
->can_update_info_refs
= 0;
600 http_install_packfile(request
->target
, &repo
->packs
);
601 release_request(request
);
605 static int is_running_queue
;
606 static int fill_active_slot(void *data 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
);
629 static void get_remote_object_list(unsigned char parent
);
631 static void add_fetch_request(struct object
*obj
)
633 struct transfer_request
*request
;
638 * Don't fetch the object if it's known to exist locally
639 * or is already in the request queue
641 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
642 get_remote_object_list(obj
->oid
.hash
[0]);
643 if (obj
->flags
& (LOCAL
| FETCHING
))
646 obj
->flags
|= FETCHING
;
647 request
= xmalloc(sizeof(*request
));
650 request
->lock
= NULL
;
651 request
->headers
= NULL
;
652 request
->state
= NEED_FETCH
;
653 request
->next
= request_queue_head
;
654 request_queue_head
= request
;
660 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
662 struct transfer_request
*request
;
663 struct packed_git
*target
;
665 /* Keep locks active */
669 * Don't push the object if it's known to exist on the remote
670 * or is already in the request queue
672 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
673 get_remote_object_list(obj
->oid
.hash
[0]);
674 if (obj
->flags
& (REMOTE
| PUSHING
))
676 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
678 obj
->flags
|= REMOTE
;
682 obj
->flags
|= PUSHING
;
683 request
= xmalloc(sizeof(*request
));
686 request
->lock
= lock
;
687 request
->headers
= NULL
;
688 request
->state
= NEED_PUSH
;
689 request
->next
= request_queue_head
;
690 request_queue_head
= request
;
698 static int fetch_indices(void)
703 fprintf(stderr
, "Getting pack list\n");
705 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
707 case HTTP_MISSING_TARGET
:
717 static void one_remote_object(const struct object_id
*oid
)
721 obj
= lookup_object(the_repository
, oid
);
723 obj
= parse_object(the_repository
, oid
);
725 /* Ignore remote objects that don't exist locally */
729 obj
->flags
|= REMOTE
;
730 if (!object_list_contains(objects
, obj
))
731 object_list_insert(obj
, &objects
);
734 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
736 int *lock_flags
= (int *)ctx
->userData
;
739 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
740 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
741 (*lock_flags
& DAV_PROP_LOCKWR
)) {
742 *lock_flags
|= DAV_LOCK_OK
;
744 *lock_flags
&= DAV_LOCK_OK
;
745 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
746 *lock_flags
|= DAV_PROP_LOCKWR
;
747 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
748 *lock_flags
|= DAV_PROP_LOCKEX
;
753 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
755 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
756 git_hash_ctx hash_ctx
;
757 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
759 if (tag_closed
&& ctx
->cdata
) {
760 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
761 lock
->owner
= xstrdup(ctx
->cdata
);
762 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
764 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
765 lock
->timeout
= strtol(arg
, NULL
, 10);
766 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
767 lock
->token
= xstrdup(ctx
->cdata
);
769 the_hash_algo
->init_fn(&hash_ctx
);
770 the_hash_algo
->update_fn(&hash_ctx
, lock
->token
, strlen(lock
->token
));
771 the_hash_algo
->final_fn(lock_token_hash
, &hash_ctx
);
773 lock
->tmpfile_suffix
[0] = '_';
774 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
779 static void one_remote_ref(const char *refname
);
782 xml_start_tag(void *userData
, const char *name
, const char **atts
)
784 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
785 const char *c
= strchr(name
, ':');
786 int old_namelen
, new_len
;
793 old_namelen
= strlen(ctx
->name
);
794 new_len
= old_namelen
+ strlen(c
) + 2;
796 if (new_len
> ctx
->len
) {
797 ctx
->name
= xrealloc(ctx
->name
, new_len
);
800 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
802 FREE_AND_NULL(ctx
->cdata
);
804 ctx
->userFunc(ctx
, 0);
808 xml_end_tag(void *userData
, const char *name
)
810 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
811 const char *c
= strchr(name
, ':');
814 ctx
->userFunc(ctx
, 1);
821 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
826 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
828 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
830 ctx
->cdata
= xmemdupz(s
, len
);
833 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
835 struct active_request_slot
*slot
;
836 struct slot_results results
;
837 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
838 struct strbuf in_buffer
= STRBUF_INIT
;
841 char timeout_header
[25];
842 struct remote_lock
*lock
= NULL
;
843 struct curl_slist
*dav_headers
= http_copy_default_headers();
847 url
= xstrfmt("%s%s", repo
->url
, path
);
849 /* Make sure leading directories exist for the remote ref */
850 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
852 char saved_character
= ep
[1];
854 slot
= get_active_slot();
855 slot
->results
= &results
;
856 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
857 if (start_active_slot(slot
)) {
858 run_active_slot(slot
);
859 if (results
.curl_result
!= CURLE_OK
&&
860 results
.http_code
!= 405) {
862 "Unable to create branch path %s\n",
868 fprintf(stderr
, "Unable to start MKCOL request\n");
872 ep
[1] = saved_character
;
873 ep
= strchr(ep
+ 1, '/');
876 escaped
= xml_entities(ident_default_email());
877 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
880 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
881 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
882 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
884 slot
= get_active_slot();
885 slot
->results
= &results
;
886 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
887 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
888 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
890 CALLOC_ARRAY(lock
, 1);
893 if (start_active_slot(slot
)) {
894 run_active_slot(slot
);
895 if (results
.curl_result
== CURLE_OK
) {
896 XML_Parser parser
= XML_ParserCreate(NULL
);
897 enum XML_Status result
;
898 ctx
.name
= xcalloc(10, 1);
901 ctx
.userFunc
= handle_new_lock_ctx
;
903 XML_SetUserData(parser
, &ctx
);
904 XML_SetElementHandler(parser
, xml_start_tag
,
906 XML_SetCharacterDataHandler(parser
, xml_cdata
);
907 result
= XML_Parse(parser
, in_buffer
.buf
,
910 if (result
!= XML_STATUS_OK
) {
911 fprintf(stderr
, "XML error: %s\n",
913 XML_GetErrorCode(parser
)));
916 XML_ParserFree(parser
);
919 "error: curl result=%d, HTTP code=%ld\n",
920 results
.curl_result
, results
.http_code
);
923 fprintf(stderr
, "Unable to start LOCK request\n");
926 curl_slist_free_all(dav_headers
);
927 strbuf_release(&out_buffer
.buf
);
928 strbuf_release(&in_buffer
);
930 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
937 lock
->start_time
= time(NULL
);
938 lock
->next
= repo
->locks
;
945 static int unlock_remote(struct remote_lock
*lock
)
947 struct active_request_slot
*slot
;
948 struct slot_results results
;
949 struct remote_lock
*prev
= repo
->locks
;
950 struct curl_slist
*dav_headers
;
953 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
955 slot
= get_active_slot();
956 slot
->results
= &results
;
957 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
958 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
960 if (start_active_slot(slot
)) {
961 run_active_slot(slot
);
962 if (results
.curl_result
== CURLE_OK
)
965 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
968 fprintf(stderr
, "Unable to start UNLOCK request\n");
971 curl_slist_free_all(dav_headers
);
973 if (repo
->locks
== lock
) {
974 repo
->locks
= lock
->next
;
976 while (prev
&& prev
->next
!= lock
)
979 prev
->next
= lock
->next
;
990 static void remove_locks(void)
992 struct remote_lock
*lock
= repo
->locks
;
994 fprintf(stderr
, "Removing remote locks...\n");
996 struct remote_lock
*next
= lock
->next
;
1002 static void remove_locks_on_signal(int signo
)
1005 sigchain_pop(signo
);
1009 static void remote_ls(const char *path
, int flags
,
1010 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1013 /* extract hex from sharded "xx/x{38}" filename */
1014 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1016 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
1018 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1021 if (hex_to_bytes(oid
->hash
, path
, 1))
1024 path
++; /* skip '/' */
1026 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1029 static void process_ls_object(struct remote_ls_ctx
*ls
)
1031 unsigned int *parent
= (unsigned int *)ls
->userData
;
1032 const char *path
= ls
->dentry_name
;
1033 struct object_id oid
;
1035 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1036 remote_dir_exists
[*parent
] = 1;
1040 if (!skip_prefix(path
, "objects/", &path
) ||
1041 get_oid_hex_from_objpath(path
, &oid
))
1044 one_remote_object(&oid
);
1047 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1049 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1050 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1054 if (!(ls
->dentry_flags
& IS_DIR
))
1055 one_remote_ref(ls
->dentry_name
);
1058 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1060 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1063 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1064 if (ls
->dentry_flags
& IS_DIR
) {
1066 /* ensure collection names end with slash */
1067 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1069 if (ls
->flags
& PROCESS_DIRS
) {
1072 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1073 ls
->flags
& RECURSIVE
) {
1074 remote_ls(ls
->dentry_name
,
1079 } else if (ls
->flags
& PROCESS_FILES
) {
1082 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1083 char *path
= ctx
->cdata
;
1084 if (*ctx
->cdata
== 'h') {
1085 path
= strstr(path
, "//");
1087 path
= strchr(path
+2, '/');
1091 const char *url
= repo
->url
;
1094 if (strncmp(path
, url
, repo
->path_len
))
1095 error("Parsed path '%s' does not match url: '%s'",
1098 path
+= repo
->path_len
;
1099 ls
->dentry_name
= xstrdup(path
);
1102 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1103 ls
->dentry_flags
|= IS_DIR
;
1105 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1106 FREE_AND_NULL(ls
->dentry_name
);
1107 ls
->dentry_flags
= 0;
1112 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1113 * should _only_ heed the information from that file, instead of trying to
1114 * determine the refs from the remote file system (badly: it does not even
1115 * know about packed-refs).
1117 static void remote_ls(const char *path
, int flags
,
1118 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1121 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1122 struct active_request_slot
*slot
;
1123 struct slot_results results
;
1124 struct strbuf in_buffer
= STRBUF_INIT
;
1125 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1126 struct curl_slist
*dav_headers
= http_copy_default_headers();
1128 struct remote_ls_ctx ls
;
1131 ls
.path
= xstrdup(path
);
1132 ls
.dentry_name
= NULL
;
1133 ls
.dentry_flags
= 0;
1134 ls
.userData
= userData
;
1135 ls
.userFunc
= userFunc
;
1137 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1139 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1140 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1142 slot
= get_active_slot();
1143 slot
->results
= &results
;
1144 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1145 &out_buffer
, fwrite_buffer
);
1146 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1147 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1149 if (start_active_slot(slot
)) {
1150 run_active_slot(slot
);
1151 if (results
.curl_result
== CURLE_OK
) {
1152 XML_Parser parser
= XML_ParserCreate(NULL
);
1153 enum XML_Status result
;
1154 ctx
.name
= xcalloc(10, 1);
1157 ctx
.userFunc
= handle_remote_ls_ctx
;
1159 XML_SetUserData(parser
, &ctx
);
1160 XML_SetElementHandler(parser
, xml_start_tag
,
1162 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1163 result
= XML_Parse(parser
, in_buffer
.buf
,
1167 if (result
!= XML_STATUS_OK
) {
1168 fprintf(stderr
, "XML error: %s\n",
1170 XML_GetErrorCode(parser
)));
1172 XML_ParserFree(parser
);
1175 fprintf(stderr
, "Unable to start PROPFIND request\n");
1180 strbuf_release(&out_buffer
.buf
);
1181 strbuf_release(&in_buffer
);
1182 curl_slist_free_all(dav_headers
);
1185 static void get_remote_object_list(unsigned char parent
)
1187 char path
[] = "objects/XX/";
1188 static const char hex
[] = "0123456789abcdef";
1189 unsigned int val
= parent
;
1191 path
[8] = hex
[val
>> 4];
1192 path
[9] = hex
[val
& 0xf];
1193 remote_dir_exists
[val
] = 0;
1194 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1195 process_ls_object
, &val
);
1198 static int locking_available(void)
1200 struct active_request_slot
*slot
;
1201 struct slot_results results
;
1202 struct strbuf in_buffer
= STRBUF_INIT
;
1203 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1204 struct curl_slist
*dav_headers
= http_copy_default_headers();
1209 escaped
= xml_entities(repo
->url
);
1210 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1213 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1214 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1216 slot
= get_active_slot();
1217 slot
->results
= &results
;
1218 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1219 &out_buffer
, fwrite_buffer
);
1220 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1221 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1223 if (start_active_slot(slot
)) {
1224 run_active_slot(slot
);
1225 if (results
.curl_result
== CURLE_OK
) {
1226 XML_Parser parser
= XML_ParserCreate(NULL
);
1227 enum XML_Status result
;
1228 ctx
.name
= xcalloc(10, 1);
1231 ctx
.userFunc
= handle_lockprop_ctx
;
1232 ctx
.userData
= &lock_flags
;
1233 XML_SetUserData(parser
, &ctx
);
1234 XML_SetElementHandler(parser
, xml_start_tag
,
1236 result
= XML_Parse(parser
, in_buffer
.buf
,
1240 if (result
!= XML_STATUS_OK
) {
1241 fprintf(stderr
, "XML error: %s\n",
1243 XML_GetErrorCode(parser
)));
1246 XML_ParserFree(parser
);
1248 error("no DAV locking support on %s",
1252 error("Cannot access URL %s, return code %d",
1253 repo
->url
, results
.curl_result
);
1257 error("Unable to start PROPFIND request on %s", repo
->url
);
1260 strbuf_release(&out_buffer
.buf
);
1261 strbuf_release(&in_buffer
);
1262 curl_slist_free_all(dav_headers
);
1267 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1269 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1273 return &entry
->next
;
1276 static struct object_list
**process_blob(struct blob
*blob
,
1277 struct object_list
**p
)
1279 struct object
*obj
= &blob
->object
;
1281 obj
->flags
|= LOCAL
;
1283 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1287 return add_one_object(obj
, p
);
1290 static struct object_list
**process_tree(struct tree
*tree
,
1291 struct object_list
**p
)
1293 struct object
*obj
= &tree
->object
;
1294 struct tree_desc desc
;
1295 struct name_entry entry
;
1297 obj
->flags
|= LOCAL
;
1299 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1301 if (parse_tree(tree
) < 0)
1302 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1305 p
= add_one_object(obj
, p
);
1307 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1309 while (tree_entry(&desc
, &entry
))
1310 switch (object_type(entry
.mode
)) {
1312 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1316 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1320 /* Subproject commit - not in this repository */
1324 free_tree_buffer(tree
);
1328 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1331 struct commit
*commit
;
1332 struct object_list
**p
= &objects
;
1335 while ((commit
= get_revision(revs
)) != NULL
) {
1336 p
= process_tree(repo_get_commit_tree(the_repository
, commit
),
1338 commit
->object
.flags
|= LOCAL
;
1339 if (!(commit
->object
.flags
& UNINTERESTING
))
1340 count
+= add_send_request(&commit
->object
, lock
);
1343 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1344 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1345 struct object
*obj
= entry
->item
;
1346 const char *name
= entry
->name
;
1348 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1350 if (obj
->type
== OBJ_TAG
) {
1352 p
= add_one_object(obj
, p
);
1355 if (obj
->type
== OBJ_TREE
) {
1356 p
= process_tree((struct tree
*)obj
, p
);
1359 if (obj
->type
== OBJ_BLOB
) {
1360 p
= process_blob((struct blob
*)obj
, p
);
1363 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1367 if (!(objects
->item
->flags
& UNINTERESTING
))
1368 count
+= add_send_request(objects
->item
, lock
);
1369 objects
= objects
->next
;
1375 static int update_remote(const struct object_id
*oid
, struct remote_lock
*lock
)
1377 struct active_request_slot
*slot
;
1378 struct slot_results results
;
1379 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1380 struct curl_slist
*dav_headers
;
1382 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1384 strbuf_addf(&out_buffer
.buf
, "%s\n", oid_to_hex(oid
));
1386 slot
= get_active_slot();
1387 slot
->results
= &results
;
1388 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1389 &out_buffer
, fwrite_null
);
1390 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1392 if (start_active_slot(slot
)) {
1393 run_active_slot(slot
);
1394 strbuf_release(&out_buffer
.buf
);
1395 if (results
.curl_result
!= CURLE_OK
) {
1397 "PUT error: curl result=%d, HTTP code=%ld\n",
1398 results
.curl_result
, results
.http_code
);
1399 /* We should attempt recovery? */
1403 strbuf_release(&out_buffer
.buf
);
1404 fprintf(stderr
, "Unable to start PUT request\n");
1411 static struct ref
*remote_refs
;
1413 static void one_remote_ref(const char *refname
)
1418 ref
= alloc_ref(refname
);
1420 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1422 "Unable to fetch ref %s from %s\n",
1423 refname
, repo
->url
);
1429 * Fetch a copy of the object if it doesn't exist locally - it
1430 * may be required for updating server info later.
1432 if (repo
->can_update_info_refs
&& !repo_has_object_file(the_repository
, &ref
->old_oid
)) {
1433 obj
= lookup_unknown_object(the_repository
, &ref
->old_oid
);
1434 fprintf(stderr
, " fetch %s for %s\n",
1435 oid_to_hex(&ref
->old_oid
), refname
);
1436 add_fetch_request(obj
);
1439 ref
->next
= remote_refs
;
1443 static void get_dav_remote_heads(void)
1445 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1448 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1450 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1454 ref
= alloc_ref(ls
->dentry_name
);
1456 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1458 "Unable to fetch ref %s from %s\n",
1459 ls
->dentry_name
, repo
->url
);
1465 o
= parse_object(the_repository
, &ref
->old_oid
);
1468 "Unable to parse object %s for remote ref %s\n",
1469 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1475 strbuf_addf(buf
, "%s\t%s\n",
1476 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1478 if (o
->type
== OBJ_TAG
) {
1479 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1481 strbuf_addf(buf
, "%s\t%s^{}\n",
1482 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1487 static void update_remote_info_refs(struct remote_lock
*lock
)
1489 struct buffer buffer
= { STRBUF_INIT
, 0 };
1490 struct active_request_slot
*slot
;
1491 struct slot_results results
;
1492 struct curl_slist
*dav_headers
;
1494 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1495 add_remote_info_ref
, &buffer
.buf
);
1497 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1499 slot
= get_active_slot();
1500 slot
->results
= &results
;
1501 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1502 &buffer
, fwrite_null
);
1503 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1505 if (start_active_slot(slot
)) {
1506 run_active_slot(slot
);
1507 if (results
.curl_result
!= CURLE_OK
) {
1509 "PUT error: curl result=%d, HTTP code=%ld\n",
1510 results
.curl_result
, results
.http_code
);
1514 strbuf_release(&buffer
.buf
);
1517 static int remote_exists(const char *path
)
1519 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1523 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1527 case HTTP_MISSING_TARGET
:
1531 error("unable to access '%s': %s", url
, curl_errorstr
);
1540 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1542 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1543 struct strbuf buffer
= STRBUF_INIT
;
1546 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1547 die("Couldn't get %s for remote symref\n%s", url
,
1551 FREE_AND_NULL(*symref
);
1554 if (buffer
.len
== 0)
1557 /* Cut off trailing newline. */
1558 strbuf_rtrim(&buffer
);
1560 /* If it's a symref, set the refname; otherwise try for a sha1 */
1561 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1562 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1564 get_oid_hex(buffer
.buf
, oid
);
1567 strbuf_release(&buffer
);
1570 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1572 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1573 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1576 return repo_in_merge_bases(the_repository
, branch
, head
);
1579 static int delete_remote_branch(const char *pattern
, int force
)
1581 struct ref
*refs
= remote_refs
;
1582 struct ref
*remote_ref
= NULL
;
1583 struct object_id head_oid
;
1584 char *symref
= NULL
;
1586 int patlen
= strlen(pattern
);
1588 struct active_request_slot
*slot
;
1589 struct slot_results results
;
1592 /* Find the remote branch(es) matching the specified branch name */
1593 for (match
= 0; refs
; refs
= refs
->next
) {
1594 char *name
= refs
->name
;
1595 int namelen
= strlen(name
);
1596 if (namelen
< patlen
||
1597 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1599 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1605 return error("No remote branch matches %s", pattern
);
1607 return error("More than one remote branch matches %s",
1611 * Remote HEAD must be a symref (not exactly foolproof; a remote
1612 * symlink to a symref will look like a symref)
1614 fetch_symref("HEAD", &symref
, &head_oid
);
1616 return error("Remote HEAD is not a symref");
1618 /* Remote branch must not be the remote HEAD */
1619 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1620 if (!strcmp(remote_ref
->name
, symref
))
1621 return error("Remote branch %s is the current HEAD",
1623 fetch_symref(symref
, &symref
, &head_oid
);
1626 /* Run extra sanity checks if delete is not forced */
1628 /* Remote HEAD must resolve to a known object */
1630 return error("Remote HEAD symrefs too deep");
1631 if (is_null_oid(&head_oid
))
1632 return error("Unable to resolve remote HEAD");
1633 if (!repo_has_object_file(the_repository
, &head_oid
))
1634 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1636 /* Remote branch must resolve to a known object */
1637 if (is_null_oid(&remote_ref
->old_oid
))
1638 return error("Unable to resolve remote branch %s",
1640 if (!repo_has_object_file(the_repository
, &remote_ref
->old_oid
))
1641 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
));
1643 /* Remote branch must be an ancestor of remote HEAD */
1644 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1645 return error("The branch '%s' is not an ancestor "
1646 "of your current HEAD.\n"
1647 "If you are sure you want to delete it,"
1648 " run:\n\t'git http-push -D %s %s'",
1649 remote_ref
->name
, repo
->url
, pattern
);
1653 /* Send delete request */
1654 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1657 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1658 slot
= get_active_slot();
1659 slot
->results
= &results
;
1660 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1661 if (start_active_slot(slot
)) {
1662 run_active_slot(slot
);
1664 if (results
.curl_result
!= CURLE_OK
)
1665 return error("DELETE request failed (%d/%ld)",
1666 results
.curl_result
, results
.http_code
);
1669 return error("Unable to start DELETE request");
1675 static void run_request_queue(void)
1677 is_running_queue
= 1;
1678 fill_active_slots();
1679 add_fill_function(NULL
, fill_active_slot
);
1681 finish_all_active_slots();
1682 fill_active_slots();
1683 } while (request_queue_head
&& !aborted
);
1685 is_running_queue
= 0;
1688 int cmd_main(int argc
, const char **argv
)
1690 struct transfer_request
*request
;
1691 struct transfer_request
*next_request
;
1692 struct refspec rs
= REFSPEC_INIT_PUSH
;
1693 struct remote_lock
*ref_lock
= NULL
;
1694 struct remote_lock
*info_ref_lock
= NULL
;
1695 int delete_branch
= 0;
1696 int force_delete
= 0;
1697 int objects_to_send
;
1701 struct ref
*ref
, *local_refs
;
1703 CALLOC_ARRAY(repo
, 1);
1706 for (i
= 1; i
< argc
; i
++, argv
++) {
1707 const char *arg
= *argv
;
1710 if (!strcmp(arg
, "--all")) {
1711 push_all
= MATCH_REFS_ALL
;
1714 if (!strcmp(arg
, "--force")) {
1718 if (!strcmp(arg
, "--dry-run")) {
1722 if (!strcmp(arg
, "--helper-status")) {
1726 if (!strcmp(arg
, "--verbose")) {
1728 http_is_verbose
= 1;
1731 if (!strcmp(arg
, "-d")) {
1735 if (!strcmp(arg
, "-D")) {
1740 if (!strcmp(arg
, "-h"))
1741 usage(http_push_usage
);
1744 char *path
= strstr(arg
, "//");
1745 str_end_url_with_slash(arg
, &repo
->url
);
1746 repo
->path_len
= strlen(repo
->url
);
1748 repo
->path
= strchr(path
+2, '/');
1750 repo
->path_len
= strlen(repo
->path
);
1754 refspec_appendn(&rs
, argv
, argc
- i
);
1759 usage(http_push_usage
);
1761 if (delete_branch
&& rs
.nr
!= 1)
1762 die("You must specify only one branch name when deleting a remote branch");
1764 setup_git_directory();
1766 memset(remote_dir_exists
, -1, 256);
1768 http_init(NULL
, repo
->url
, 1);
1770 is_running_queue
= 0;
1772 /* Verify DAV compliance/lock support */
1773 if (!locking_available()) {
1778 sigchain_push_common(remove_locks_on_signal
);
1780 /* Check whether the remote has server info files */
1781 repo
->can_update_info_refs
= 0;
1782 repo
->has_info_refs
= remote_exists("info/refs");
1783 repo
->has_info_packs
= remote_exists("objects/info/packs");
1784 if (repo
->has_info_refs
) {
1785 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1787 repo
->can_update_info_refs
= 1;
1789 error("cannot lock existing info/refs");
1794 if (repo
->has_info_packs
)
1797 /* Get a list of all local and remote heads to validate refspecs */
1798 local_refs
= get_local_heads();
1799 fprintf(stderr
, "Fetching remote heads...\n");
1800 get_dav_remote_heads();
1801 run_request_queue();
1803 /* Remove a remote branch if -d or -D was specified */
1804 if (delete_branch
) {
1805 const char *branch
= rs
.items
[i
].src
;
1806 if (delete_remote_branch(branch
, force_delete
) == -1) {
1807 fprintf(stderr
, "Unable to delete remote branch %s\n",
1810 printf("error %s cannot remove\n", branch
);
1816 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1821 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1823 printf("error null no match\n");
1829 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1830 struct rev_info revs
;
1831 struct strvec commit_argv
= STRVEC_INIT
;
1836 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1837 if (delete_remote_branch(ref
->name
, 1) == -1) {
1838 error("Could not remove %s", ref
->name
);
1840 printf("error %s cannot remove\n", ref
->name
);
1843 else if (helper_status
)
1844 printf("ok %s\n", ref
->name
);
1849 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1851 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1853 printf("ok %s up to date\n", ref
->name
);
1858 !is_null_oid(&ref
->old_oid
) &&
1860 if (!repo_has_object_file(the_repository
, &ref
->old_oid
) ||
1861 !ref_newer(&ref
->peer_ref
->new_oid
,
1864 * We do not have the remote ref, or
1865 * we know that the remote ref is not
1866 * an ancestor of what we are trying to
1867 * push. Either way this can be losing
1868 * commits at the remote end and likely
1869 * we were not up to date to begin with.
1871 error("remote '%s' is not an ancestor of\n"
1873 "Maybe you are not up-to-date and "
1874 "need to pull first?",
1876 ref
->peer_ref
->name
);
1878 printf("error %s non-fast forward\n", ref
->name
);
1883 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1886 fprintf(stderr
, "updating '%s'", ref
->name
);
1887 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1888 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1889 fprintf(stderr
, "\n from %s\n to %s\n",
1890 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1893 printf("ok %s\n", ref
->name
);
1897 /* Lock remote branch ref */
1898 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1900 fprintf(stderr
, "Unable to lock remote branch %s\n",
1903 printf("error %s lock error\n", ref
->name
);
1908 /* Set up revision info for this refspec */
1909 strvec_push(&commit_argv
, ""); /* ignored */
1910 strvec_push(&commit_argv
, "--objects");
1911 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1912 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1913 strvec_pushf(&commit_argv
, "^%s",
1914 oid_to_hex(&ref
->old_oid
));
1915 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1916 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1917 revs
.edge_hint
= 0; /* just in case */
1919 /* Generate a list of objects that need to be pushed */
1921 if (prepare_revision_walk(&revs
))
1922 die("revision walk setup failed");
1923 mark_edges_uninteresting(&revs
, NULL
, 0);
1924 objects_to_send
= get_delta(&revs
, ref_lock
);
1925 finish_all_active_slots();
1927 /* Push missing objects to remote, this would be a
1928 convenient time to pack them first if appropriate. */
1930 if (objects_to_send
)
1931 fprintf(stderr
, " sending %d objects\n",
1934 run_request_queue();
1936 /* Update the remote branch if all went well */
1937 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1941 fprintf(stderr
, " done\n");
1943 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1944 unlock_remote(ref_lock
);
1946 strvec_clear(&commit_argv
);
1947 release_revisions(&revs
);
1950 /* Update remote server info if appropriate */
1951 if (repo
->has_info_refs
&& new_refs
) {
1952 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1953 fprintf(stderr
, "Updating remote server info\n");
1955 update_remote_info_refs(info_ref_lock
);
1957 fprintf(stderr
, "Unable to update server info\n");
1963 unlock_remote(info_ref_lock
);
1968 request
= request_queue_head
;
1969 while (request
!= NULL
) {
1970 next_request
= request
->next
;
1971 release_request(request
);
1972 request
= next_request
;