2 #include "repository.h"
12 #include "list-objects.h"
16 #include "object-store.h"
17 #include "commit-reach.h"
19 #ifdef EXPAT_NEEDS_XMLPARSE_H
25 static const char http_push_usage
[] =
26 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
33 #define XML_STATUS_OK 1
34 #define XML_STATUS_ERROR 0
37 #define PREV_BUF_SIZE 4096
40 #define DAV_LOCK "LOCK"
41 #define DAV_MKCOL "MKCOL"
42 #define DAV_MOVE "MOVE"
43 #define DAV_PROPFIND "PROPFIND"
45 #define DAV_UNLOCK "UNLOCK"
46 #define DAV_DELETE "DELETE"
49 #define DAV_PROP_LOCKWR (1u << 0)
50 #define DAV_PROP_LOCKEX (1u << 1)
51 #define DAV_LOCK_OK (1u << 2)
53 /* DAV XML properties */
54 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
55 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
56 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
57 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
58 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
59 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
60 #define DAV_PROPFIND_RESP ".multistatus.response"
61 #define DAV_PROPFIND_NAME ".multistatus.response.href"
62 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
64 /* DAV request body templates */
65 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
66 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
67 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
70 #define LOCK_REFRESH 30
72 /* Remember to update object flag allocation in object.h */
73 #define LOCAL (1u<<11)
74 #define REMOTE (1u<<12)
75 #define FETCHING (1u<<13)
76 #define PUSHING (1u<<14)
78 /* We allow "recursive" symbolic refs. Only within reason, though */
83 static signed char remote_dir_exists
[256];
85 static int push_verbosely
;
86 static int push_all
= MATCH_REFS_NONE
;
89 static int helper_status
;
91 static struct object_list
*objects
;
98 int can_update_info_refs
;
100 struct packed_git
*packs
;
101 struct remote_lock
*locks
;
104 static struct repo
*repo
;
106 enum transfer_state
{
118 struct transfer_request
{
120 struct packed_git
*target
;
123 struct remote_lock
*lock
;
124 struct curl_slist
*headers
;
125 struct buffer buffer
;
126 enum transfer_state state
;
127 CURLcode curl_result
;
128 char errorstr
[CURL_ERROR_SIZE
];
131 struct active_request_slot
*slot
;
132 struct transfer_request
*next
;
135 static struct transfer_request
*request_queue_head
;
141 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
149 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
153 struct remote_lock
*next
;
156 /* Flags that control remote_ls processing */
157 #define PROCESS_FILES (1u << 0)
158 #define PROCESS_DIRS (1u << 1)
159 #define RECURSIVE (1u << 2)
161 /* Flags that remote_ls passes to callback functions */
162 #define IS_DIR (1u << 0)
164 struct remote_ls_ctx
{
166 void (*userFunc
)(struct remote_ls_ctx
*ls
);
171 struct remote_ls_ctx
*parent
;
174 /* get_dav_token_headers options */
175 enum dav_header_flag
{
176 DAV_HEADER_IF
= (1u << 0),
177 DAV_HEADER_LOCK
= (1u << 1),
178 DAV_HEADER_TIMEOUT
= (1u << 2)
181 static char *xml_entities(const char *s
)
183 struct strbuf buf
= STRBUF_INIT
;
184 strbuf_addstr_xml_quoted(&buf
, s
);
185 return strbuf_detach(&buf
, NULL
);
188 static void curl_setup_http_get(CURL
*curl
, const char *url
,
189 const char *custom_req
)
191 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
192 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
193 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
194 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
197 static void curl_setup_http(CURL
*curl
, const char *url
,
198 const char *custom_req
, struct buffer
*buffer
,
199 curl_write_callback write_fn
)
201 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
202 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
203 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
204 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
205 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
206 #ifndef NO_CURL_IOCTL
207 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
208 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, buffer
);
210 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
211 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
212 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
213 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
216 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
218 struct strbuf buf
= STRBUF_INIT
;
219 struct curl_slist
*dav_headers
= http_copy_default_headers();
221 if (options
& DAV_HEADER_IF
) {
222 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
223 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
226 if (options
& DAV_HEADER_LOCK
) {
227 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
228 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
231 if (options
& DAV_HEADER_TIMEOUT
) {
232 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
233 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
236 strbuf_release(&buf
);
241 static void finish_request(struct transfer_request
*request
);
242 static void release_request(struct transfer_request
*request
);
244 static void process_response(void *callback_data
)
246 struct transfer_request
*request
=
247 (struct transfer_request
*)callback_data
;
249 finish_request(request
);
252 #ifdef USE_CURL_MULTI
254 static void start_fetch_loose(struct transfer_request
*request
)
256 struct active_request_slot
*slot
;
257 struct http_object_request
*obj_req
;
259 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
260 if (obj_req
== NULL
) {
261 request
->state
= ABORTED
;
265 slot
= obj_req
->slot
;
266 slot
->callback_func
= process_response
;
267 slot
->callback_data
= request
;
268 request
->slot
= slot
;
269 request
->userData
= obj_req
;
271 /* Try to get the request started, abort the request on error */
272 request
->state
= RUN_FETCH_LOOSE
;
273 if (!start_active_slot(slot
)) {
274 fprintf(stderr
, "Unable to start GET request\n");
275 repo
->can_update_info_refs
= 0;
276 release_http_object_request(obj_req
);
277 release_request(request
);
281 static void start_mkcol(struct transfer_request
*request
)
283 char *hex
= oid_to_hex(&request
->obj
->oid
);
284 struct active_request_slot
*slot
;
286 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
288 slot
= get_active_slot();
289 slot
->callback_func
= process_response
;
290 slot
->callback_data
= request
;
291 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
292 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
294 if (start_active_slot(slot
)) {
295 request
->slot
= slot
;
296 request
->state
= RUN_MKCOL
;
298 request
->state
= ABORTED
;
299 FREE_AND_NULL(request
->url
);
304 static void start_fetch_packed(struct transfer_request
*request
)
306 struct packed_git
*target
;
308 struct transfer_request
*check_request
= request_queue_head
;
309 struct http_pack_request
*preq
;
311 target
= find_sha1_pack(request
->obj
->oid
.hash
, repo
->packs
);
313 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
314 repo
->can_update_info_refs
= 0;
315 release_request(request
);
318 close_pack_index(target
);
319 request
->target
= target
;
321 fprintf(stderr
, "Fetching pack %s\n",
322 hash_to_hex(target
->hash
));
323 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
325 preq
= new_http_pack_request(target
->hash
, repo
->url
);
327 repo
->can_update_info_refs
= 0;
331 /* Make sure there isn't another open request for this pack */
332 while (check_request
) {
333 if (check_request
->state
== RUN_FETCH_PACKED
&&
334 !strcmp(check_request
->url
, preq
->url
)) {
335 release_http_pack_request(preq
);
336 release_request(request
);
339 check_request
= check_request
->next
;
342 preq
->slot
->callback_func
= process_response
;
343 preq
->slot
->callback_data
= request
;
344 request
->slot
= preq
->slot
;
345 request
->userData
= preq
;
347 /* Try to get the request started, abort the request on error */
348 request
->state
= RUN_FETCH_PACKED
;
349 if (!start_active_slot(preq
->slot
)) {
350 fprintf(stderr
, "Unable to start GET request\n");
351 release_http_pack_request(preq
);
352 repo
->can_update_info_refs
= 0;
353 release_request(request
);
357 static void start_put(struct transfer_request
*request
)
359 char *hex
= oid_to_hex(&request
->obj
->oid
);
360 struct active_request_slot
*slot
;
361 struct strbuf buf
= STRBUF_INIT
;
362 enum object_type type
;
370 unpacked
= read_object_file(&request
->obj
->oid
, &type
, &len
);
371 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(type
), (uintmax_t)len
) + 1;
374 git_deflate_init(&stream
, zlib_compression_level
);
375 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
376 strbuf_init(&request
->buffer
.buf
, size
);
377 request
->buffer
.posn
= 0;
380 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
381 stream
.avail_out
= size
;
384 stream
.next_in
= (void *)hdr
;
385 stream
.avail_in
= hdrlen
;
386 while (git_deflate(&stream
, 0) == Z_OK
)
389 /* Then the data itself.. */
390 stream
.next_in
= unpacked
;
391 stream
.avail_in
= len
;
392 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
394 git_deflate_end(&stream
);
397 request
->buffer
.buf
.len
= stream
.total_out
;
399 strbuf_addstr(&buf
, "Destination: ");
400 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
401 request
->dest
= strbuf_detach(&buf
, NULL
);
403 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
404 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
405 request
->url
= strbuf_detach(&buf
, NULL
);
407 slot
= get_active_slot();
408 slot
->callback_func
= process_response
;
409 slot
->callback_data
= request
;
410 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
411 &request
->buffer
, fwrite_null
);
413 if (start_active_slot(slot
)) {
414 request
->slot
= slot
;
415 request
->state
= RUN_PUT
;
417 request
->state
= ABORTED
;
418 FREE_AND_NULL(request
->url
);
422 static void start_move(struct transfer_request
*request
)
424 struct active_request_slot
*slot
;
425 struct curl_slist
*dav_headers
= http_copy_default_headers();
427 slot
= get_active_slot();
428 slot
->callback_func
= process_response
;
429 slot
->callback_data
= request
;
430 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
431 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
432 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
433 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
435 if (start_active_slot(slot
)) {
436 request
->slot
= slot
;
437 request
->state
= RUN_MOVE
;
439 request
->state
= ABORTED
;
440 FREE_AND_NULL(request
->url
);
444 static int refresh_lock(struct remote_lock
*lock
)
446 struct active_request_slot
*slot
;
447 struct slot_results results
;
448 struct curl_slist
*dav_headers
;
451 lock
->refreshing
= 1;
453 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
455 slot
= get_active_slot();
456 slot
->results
= &results
;
457 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
458 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
460 if (start_active_slot(slot
)) {
461 run_active_slot(slot
);
462 if (results
.curl_result
!= CURLE_OK
) {
463 fprintf(stderr
, "LOCK HTTP error %ld\n",
466 lock
->start_time
= time(NULL
);
471 lock
->refreshing
= 0;
472 curl_slist_free_all(dav_headers
);
477 static void check_locks(void)
479 struct remote_lock
*lock
= repo
->locks
;
480 time_t current_time
= time(NULL
);
484 time_remaining
= lock
->start_time
+ lock
->timeout
-
486 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
487 if (!refresh_lock(lock
)) {
489 "Unable to refresh lock for %s\n",
499 static void release_request(struct transfer_request
*request
)
501 struct transfer_request
*entry
= request_queue_head
;
503 if (request
== request_queue_head
) {
504 request_queue_head
= request
->next
;
506 while (entry
&& entry
->next
!= request
)
509 entry
->next
= request
->next
;
516 static void finish_request(struct transfer_request
*request
)
518 struct http_pack_request
*preq
;
519 struct http_object_request
*obj_req
;
521 request
->curl_result
= request
->slot
->curl_result
;
522 request
->http_code
= request
->slot
->http_code
;
523 request
->slot
= NULL
;
525 /* Keep locks active */
528 if (request
->headers
!= NULL
)
529 curl_slist_free_all(request
->headers
);
531 /* URL is reused for MOVE after PUT and used during FETCH */
532 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
533 FREE_AND_NULL(request
->url
);
536 if (request
->state
== RUN_MKCOL
) {
537 if (request
->curl_result
== CURLE_OK
||
538 request
->http_code
== 405) {
539 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
542 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
543 oid_to_hex(&request
->obj
->oid
),
544 request
->curl_result
, request
->http_code
);
545 request
->state
= ABORTED
;
548 } else if (request
->state
== RUN_PUT
) {
549 if (request
->curl_result
== CURLE_OK
) {
552 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
553 oid_to_hex(&request
->obj
->oid
),
554 request
->curl_result
, request
->http_code
);
555 request
->state
= ABORTED
;
558 } else if (request
->state
== RUN_MOVE
) {
559 if (request
->curl_result
== CURLE_OK
) {
561 fprintf(stderr
, " sent %s\n",
562 oid_to_hex(&request
->obj
->oid
));
563 request
->obj
->flags
|= REMOTE
;
564 release_request(request
);
566 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
567 oid_to_hex(&request
->obj
->oid
),
568 request
->curl_result
, request
->http_code
);
569 request
->state
= ABORTED
;
572 } else if (request
->state
== RUN_FETCH_LOOSE
) {
573 obj_req
= (struct http_object_request
*)request
->userData
;
575 if (finish_http_object_request(obj_req
) == 0)
576 if (obj_req
->rename
== 0)
577 request
->obj
->flags
|= (LOCAL
| REMOTE
);
579 /* Try fetching packed if necessary */
580 if (request
->obj
->flags
& LOCAL
) {
581 release_http_object_request(obj_req
);
582 release_request(request
);
584 start_fetch_packed(request
);
586 } else if (request
->state
== RUN_FETCH_PACKED
) {
588 if (request
->curl_result
!= CURLE_OK
) {
589 fprintf(stderr
, "Unable to get pack file %s\n%s",
590 request
->url
, curl_errorstr
);
592 preq
= (struct http_pack_request
*)request
->userData
;
595 if (finish_http_pack_request(preq
) == 0)
597 release_http_pack_request(preq
);
601 repo
->can_update_info_refs
= 0;
603 http_install_packfile(request
->target
, &repo
->packs
);
604 release_request(request
);
608 #ifdef USE_CURL_MULTI
609 static int is_running_queue
;
610 static int fill_active_slot(void *unused
)
612 struct transfer_request
*request
;
614 if (aborted
|| !is_running_queue
)
617 for (request
= request_queue_head
; request
; request
= request
->next
) {
618 if (request
->state
== NEED_FETCH
) {
619 start_fetch_loose(request
);
621 } else if (pushing
&& request
->state
== NEED_PUSH
) {
622 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
625 start_mkcol(request
);
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
;
661 #ifdef USE_CURL_MULTI
667 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
669 struct transfer_request
*request
;
670 struct packed_git
*target
;
672 /* Keep locks active */
676 * Don't push the object if it's known to exist on the remote
677 * or is already in the request queue
679 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
680 get_remote_object_list(obj
->oid
.hash
[0]);
681 if (obj
->flags
& (REMOTE
| PUSHING
))
683 target
= find_sha1_pack(obj
->oid
.hash
, repo
->packs
);
685 obj
->flags
|= REMOTE
;
689 obj
->flags
|= PUSHING
;
690 request
= xmalloc(sizeof(*request
));
693 request
->lock
= lock
;
694 request
->headers
= NULL
;
695 request
->state
= NEED_PUSH
;
696 request
->next
= request_queue_head
;
697 request_queue_head
= request
;
699 #ifdef USE_CURL_MULTI
707 static int fetch_indices(void)
712 fprintf(stderr
, "Getting pack list\n");
714 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
716 case HTTP_MISSING_TARGET
:
726 static void one_remote_object(const struct object_id
*oid
)
730 obj
= lookup_object(the_repository
, oid
);
732 obj
= parse_object(the_repository
, oid
);
734 /* Ignore remote objects that don't exist locally */
738 obj
->flags
|= REMOTE
;
739 if (!object_list_contains(objects
, obj
))
740 object_list_insert(obj
, &objects
);
743 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
745 int *lock_flags
= (int *)ctx
->userData
;
748 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
749 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
750 (*lock_flags
& DAV_PROP_LOCKWR
)) {
751 *lock_flags
|= DAV_LOCK_OK
;
753 *lock_flags
&= DAV_LOCK_OK
;
754 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
755 *lock_flags
|= DAV_PROP_LOCKWR
;
756 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
757 *lock_flags
|= DAV_PROP_LOCKEX
;
762 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
764 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
765 git_hash_ctx hash_ctx
;
766 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
768 if (tag_closed
&& ctx
->cdata
) {
769 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
770 lock
->owner
= xstrdup(ctx
->cdata
);
771 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
773 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
774 lock
->timeout
= strtol(arg
, NULL
, 10);
775 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
776 lock
->token
= xstrdup(ctx
->cdata
);
778 the_hash_algo
->init_fn(&hash_ctx
);
779 the_hash_algo
->update_fn(&hash_ctx
, lock
->token
, strlen(lock
->token
));
780 the_hash_algo
->final_fn(lock_token_hash
, &hash_ctx
);
782 lock
->tmpfile_suffix
[0] = '_';
783 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
788 static void one_remote_ref(const char *refname
);
791 xml_start_tag(void *userData
, const char *name
, const char **atts
)
793 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
794 const char *c
= strchr(name
, ':');
795 int old_namelen
, new_len
;
802 old_namelen
= strlen(ctx
->name
);
803 new_len
= old_namelen
+ strlen(c
) + 2;
805 if (new_len
> ctx
->len
) {
806 ctx
->name
= xrealloc(ctx
->name
, new_len
);
809 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
811 FREE_AND_NULL(ctx
->cdata
);
813 ctx
->userFunc(ctx
, 0);
817 xml_end_tag(void *userData
, const char *name
)
819 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
820 const char *c
= strchr(name
, ':');
823 ctx
->userFunc(ctx
, 1);
830 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
835 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
837 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
839 ctx
->cdata
= xmemdupz(s
, len
);
842 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
844 struct active_request_slot
*slot
;
845 struct slot_results results
;
846 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
847 struct strbuf in_buffer
= STRBUF_INIT
;
850 char timeout_header
[25];
851 struct remote_lock
*lock
= NULL
;
852 struct curl_slist
*dav_headers
= http_copy_default_headers();
856 url
= xstrfmt("%s%s", repo
->url
, path
);
858 /* Make sure leading directories exist for the remote ref */
859 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
861 char saved_character
= ep
[1];
863 slot
= get_active_slot();
864 slot
->results
= &results
;
865 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
866 if (start_active_slot(slot
)) {
867 run_active_slot(slot
);
868 if (results
.curl_result
!= CURLE_OK
&&
869 results
.http_code
!= 405) {
871 "Unable to create branch path %s\n",
877 fprintf(stderr
, "Unable to start MKCOL request\n");
881 ep
[1] = saved_character
;
882 ep
= strchr(ep
+ 1, '/');
885 escaped
= xml_entities(ident_default_email());
886 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
889 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
890 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
891 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
893 slot
= get_active_slot();
894 slot
->results
= &results
;
895 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
896 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
897 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
899 CALLOC_ARRAY(lock
, 1);
902 if (start_active_slot(slot
)) {
903 run_active_slot(slot
);
904 if (results
.curl_result
== CURLE_OK
) {
905 XML_Parser parser
= XML_ParserCreate(NULL
);
906 enum XML_Status result
;
907 ctx
.name
= xcalloc(10, 1);
910 ctx
.userFunc
= handle_new_lock_ctx
;
912 XML_SetUserData(parser
, &ctx
);
913 XML_SetElementHandler(parser
, xml_start_tag
,
915 XML_SetCharacterDataHandler(parser
, xml_cdata
);
916 result
= XML_Parse(parser
, in_buffer
.buf
,
919 if (result
!= XML_STATUS_OK
) {
920 fprintf(stderr
, "XML error: %s\n",
922 XML_GetErrorCode(parser
)));
925 XML_ParserFree(parser
);
928 "error: curl result=%d, HTTP code=%ld\n",
929 results
.curl_result
, results
.http_code
);
932 fprintf(stderr
, "Unable to start LOCK request\n");
935 curl_slist_free_all(dav_headers
);
936 strbuf_release(&out_buffer
.buf
);
937 strbuf_release(&in_buffer
);
939 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
946 lock
->start_time
= time(NULL
);
947 lock
->next
= repo
->locks
;
954 static int unlock_remote(struct remote_lock
*lock
)
956 struct active_request_slot
*slot
;
957 struct slot_results results
;
958 struct remote_lock
*prev
= repo
->locks
;
959 struct curl_slist
*dav_headers
;
962 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
964 slot
= get_active_slot();
965 slot
->results
= &results
;
966 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
967 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
969 if (start_active_slot(slot
)) {
970 run_active_slot(slot
);
971 if (results
.curl_result
== CURLE_OK
)
974 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
977 fprintf(stderr
, "Unable to start UNLOCK request\n");
980 curl_slist_free_all(dav_headers
);
982 if (repo
->locks
== lock
) {
983 repo
->locks
= lock
->next
;
985 while (prev
&& prev
->next
!= lock
)
988 prev
->next
= lock
->next
;
999 static void remove_locks(void)
1001 struct remote_lock
*lock
= repo
->locks
;
1003 fprintf(stderr
, "Removing remote locks...\n");
1005 struct remote_lock
*next
= lock
->next
;
1006 unlock_remote(lock
);
1011 static void remove_locks_on_signal(int signo
)
1014 sigchain_pop(signo
);
1018 static void remote_ls(const char *path
, int flags
,
1019 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1022 /* extract hex from sharded "xx/x{38}" filename */
1023 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1025 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1028 if (hex_to_bytes(oid
->hash
, path
, 1))
1031 path
++; /* skip '/' */
1033 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1036 static void process_ls_object(struct remote_ls_ctx
*ls
)
1038 unsigned int *parent
= (unsigned int *)ls
->userData
;
1039 const char *path
= ls
->dentry_name
;
1040 struct object_id oid
;
1042 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1043 remote_dir_exists
[*parent
] = 1;
1047 if (!skip_prefix(path
, "objects/", &path
) ||
1048 get_oid_hex_from_objpath(path
, &oid
))
1051 one_remote_object(&oid
);
1054 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1056 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1057 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1061 if (!(ls
->dentry_flags
& IS_DIR
))
1062 one_remote_ref(ls
->dentry_name
);
1065 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1067 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1070 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1071 if (ls
->dentry_flags
& IS_DIR
) {
1073 /* ensure collection names end with slash */
1074 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1076 if (ls
->flags
& PROCESS_DIRS
) {
1079 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1080 ls
->flags
& RECURSIVE
) {
1081 remote_ls(ls
->dentry_name
,
1086 } else if (ls
->flags
& PROCESS_FILES
) {
1089 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1090 char *path
= ctx
->cdata
;
1091 if (*ctx
->cdata
== 'h') {
1092 path
= strstr(path
, "//");
1094 path
= strchr(path
+2, '/');
1098 const char *url
= repo
->url
;
1101 if (strncmp(path
, url
, repo
->path_len
))
1102 error("Parsed path '%s' does not match url: '%s'",
1105 path
+= repo
->path_len
;
1106 ls
->dentry_name
= xstrdup(path
);
1109 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1110 ls
->dentry_flags
|= IS_DIR
;
1112 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1113 FREE_AND_NULL(ls
->dentry_name
);
1114 ls
->dentry_flags
= 0;
1119 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1120 * should _only_ heed the information from that file, instead of trying to
1121 * determine the refs from the remote file system (badly: it does not even
1122 * know about packed-refs).
1124 static void remote_ls(const char *path
, int flags
,
1125 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1128 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1129 struct active_request_slot
*slot
;
1130 struct slot_results results
;
1131 struct strbuf in_buffer
= STRBUF_INIT
;
1132 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1133 struct curl_slist
*dav_headers
= http_copy_default_headers();
1135 struct remote_ls_ctx ls
;
1138 ls
.path
= xstrdup(path
);
1139 ls
.dentry_name
= NULL
;
1140 ls
.dentry_flags
= 0;
1141 ls
.userData
= userData
;
1142 ls
.userFunc
= userFunc
;
1144 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1146 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1147 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1149 slot
= get_active_slot();
1150 slot
->results
= &results
;
1151 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1152 &out_buffer
, fwrite_buffer
);
1153 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1154 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1156 if (start_active_slot(slot
)) {
1157 run_active_slot(slot
);
1158 if (results
.curl_result
== CURLE_OK
) {
1159 XML_Parser parser
= XML_ParserCreate(NULL
);
1160 enum XML_Status result
;
1161 ctx
.name
= xcalloc(10, 1);
1164 ctx
.userFunc
= handle_remote_ls_ctx
;
1166 XML_SetUserData(parser
, &ctx
);
1167 XML_SetElementHandler(parser
, xml_start_tag
,
1169 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1170 result
= XML_Parse(parser
, in_buffer
.buf
,
1174 if (result
!= XML_STATUS_OK
) {
1175 fprintf(stderr
, "XML error: %s\n",
1177 XML_GetErrorCode(parser
)));
1179 XML_ParserFree(parser
);
1182 fprintf(stderr
, "Unable to start PROPFIND request\n");
1187 strbuf_release(&out_buffer
.buf
);
1188 strbuf_release(&in_buffer
);
1189 curl_slist_free_all(dav_headers
);
1192 static void get_remote_object_list(unsigned char parent
)
1194 char path
[] = "objects/XX/";
1195 static const char hex
[] = "0123456789abcdef";
1196 unsigned int val
= parent
;
1198 path
[8] = hex
[val
>> 4];
1199 path
[9] = hex
[val
& 0xf];
1200 remote_dir_exists
[val
] = 0;
1201 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1202 process_ls_object
, &val
);
1205 static int locking_available(void)
1207 struct active_request_slot
*slot
;
1208 struct slot_results results
;
1209 struct strbuf in_buffer
= STRBUF_INIT
;
1210 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1211 struct curl_slist
*dav_headers
= http_copy_default_headers();
1216 escaped
= xml_entities(repo
->url
);
1217 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1220 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1221 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1223 slot
= get_active_slot();
1224 slot
->results
= &results
;
1225 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1226 &out_buffer
, fwrite_buffer
);
1227 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1228 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1230 if (start_active_slot(slot
)) {
1231 run_active_slot(slot
);
1232 if (results
.curl_result
== CURLE_OK
) {
1233 XML_Parser parser
= XML_ParserCreate(NULL
);
1234 enum XML_Status result
;
1235 ctx
.name
= xcalloc(10, 1);
1238 ctx
.userFunc
= handle_lockprop_ctx
;
1239 ctx
.userData
= &lock_flags
;
1240 XML_SetUserData(parser
, &ctx
);
1241 XML_SetElementHandler(parser
, xml_start_tag
,
1243 result
= XML_Parse(parser
, in_buffer
.buf
,
1247 if (result
!= XML_STATUS_OK
) {
1248 fprintf(stderr
, "XML error: %s\n",
1250 XML_GetErrorCode(parser
)));
1253 XML_ParserFree(parser
);
1255 error("no DAV locking support on %s",
1259 error("Cannot access URL %s, return code %d",
1260 repo
->url
, results
.curl_result
);
1264 error("Unable to start PROPFIND request on %s", repo
->url
);
1267 strbuf_release(&out_buffer
.buf
);
1268 strbuf_release(&in_buffer
);
1269 curl_slist_free_all(dav_headers
);
1274 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1276 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1280 return &entry
->next
;
1283 static struct object_list
**process_blob(struct blob
*blob
,
1284 struct object_list
**p
)
1286 struct object
*obj
= &blob
->object
;
1288 obj
->flags
|= LOCAL
;
1290 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1294 return add_one_object(obj
, p
);
1297 static struct object_list
**process_tree(struct tree
*tree
,
1298 struct object_list
**p
)
1300 struct object
*obj
= &tree
->object
;
1301 struct tree_desc desc
;
1302 struct name_entry entry
;
1304 obj
->flags
|= LOCAL
;
1306 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1308 if (parse_tree(tree
) < 0)
1309 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1312 p
= add_one_object(obj
, p
);
1314 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1316 while (tree_entry(&desc
, &entry
))
1317 switch (object_type(entry
.mode
)) {
1319 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1323 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1327 /* Subproject commit - not in this repository */
1331 free_tree_buffer(tree
);
1335 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1338 struct commit
*commit
;
1339 struct object_list
**p
= &objects
;
1342 while ((commit
= get_revision(revs
)) != NULL
) {
1343 p
= process_tree(get_commit_tree(commit
), p
);
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
&& !has_object_file(&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
);
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
,
1582 return in_merge_bases(branch
, head
);
1585 static int delete_remote_branch(const char *pattern
, int force
)
1587 struct ref
*refs
= remote_refs
;
1588 struct ref
*remote_ref
= NULL
;
1589 struct object_id head_oid
;
1590 char *symref
= NULL
;
1592 int patlen
= strlen(pattern
);
1594 struct active_request_slot
*slot
;
1595 struct slot_results results
;
1598 /* Find the remote branch(es) matching the specified branch name */
1599 for (match
= 0; refs
; refs
= refs
->next
) {
1600 char *name
= refs
->name
;
1601 int namelen
= strlen(name
);
1602 if (namelen
< patlen
||
1603 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1605 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1611 return error("No remote branch matches %s", pattern
);
1613 return error("More than one remote branch matches %s",
1617 * Remote HEAD must be a symref (not exactly foolproof; a remote
1618 * symlink to a symref will look like a symref)
1620 fetch_symref("HEAD", &symref
, &head_oid
);
1622 return error("Remote HEAD is not a symref");
1624 /* Remote branch must not be the remote HEAD */
1625 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1626 if (!strcmp(remote_ref
->name
, symref
))
1627 return error("Remote branch %s is the current HEAD",
1629 fetch_symref(symref
, &symref
, &head_oid
);
1632 /* Run extra sanity checks if delete is not forced */
1634 /* Remote HEAD must resolve to a known object */
1636 return error("Remote HEAD symrefs too deep");
1637 if (is_null_oid(&head_oid
))
1638 return error("Unable to resolve remote HEAD");
1639 if (!has_object_file(&head_oid
))
1640 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1642 /* Remote branch must resolve to a known object */
1643 if (is_null_oid(&remote_ref
->old_oid
))
1644 return error("Unable to resolve remote branch %s",
1646 if (!has_object_file(&remote_ref
->old_oid
))
1647 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
));
1649 /* Remote branch must be an ancestor of remote HEAD */
1650 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1651 return error("The branch '%s' is not an ancestor "
1652 "of your current HEAD.\n"
1653 "If you are sure you want to delete it,"
1654 " run:\n\t'git http-push -D %s %s'",
1655 remote_ref
->name
, repo
->url
, pattern
);
1659 /* Send delete request */
1660 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1663 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1664 slot
= get_active_slot();
1665 slot
->results
= &results
;
1666 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1667 if (start_active_slot(slot
)) {
1668 run_active_slot(slot
);
1670 if (results
.curl_result
!= CURLE_OK
)
1671 return error("DELETE request failed (%d/%ld)",
1672 results
.curl_result
, results
.http_code
);
1675 return error("Unable to start DELETE request");
1681 static void run_request_queue(void)
1683 #ifdef USE_CURL_MULTI
1684 is_running_queue
= 1;
1685 fill_active_slots();
1686 add_fill_function(NULL
, fill_active_slot
);
1689 finish_all_active_slots();
1690 #ifdef USE_CURL_MULTI
1691 fill_active_slots();
1693 } while (request_queue_head
&& !aborted
);
1695 #ifdef USE_CURL_MULTI
1696 is_running_queue
= 0;
1700 int cmd_main(int argc
, const char **argv
)
1702 struct transfer_request
*request
;
1703 struct transfer_request
*next_request
;
1704 struct refspec rs
= REFSPEC_INIT_PUSH
;
1705 struct remote_lock
*ref_lock
= NULL
;
1706 struct remote_lock
*info_ref_lock
= NULL
;
1707 struct rev_info revs
;
1708 int delete_branch
= 0;
1709 int force_delete
= 0;
1710 int objects_to_send
;
1714 struct ref
*ref
, *local_refs
;
1716 CALLOC_ARRAY(repo
, 1);
1719 for (i
= 1; i
< argc
; i
++, argv
++) {
1720 const char *arg
= *argv
;
1723 if (!strcmp(arg
, "--all")) {
1724 push_all
= MATCH_REFS_ALL
;
1727 if (!strcmp(arg
, "--force")) {
1731 if (!strcmp(arg
, "--dry-run")) {
1735 if (!strcmp(arg
, "--helper-status")) {
1739 if (!strcmp(arg
, "--verbose")) {
1741 http_is_verbose
= 1;
1744 if (!strcmp(arg
, "-d")) {
1748 if (!strcmp(arg
, "-D")) {
1753 if (!strcmp(arg
, "-h"))
1754 usage(http_push_usage
);
1757 char *path
= strstr(arg
, "//");
1758 str_end_url_with_slash(arg
, &repo
->url
);
1759 repo
->path_len
= strlen(repo
->url
);
1761 repo
->path
= strchr(path
+2, '/');
1763 repo
->path_len
= strlen(repo
->path
);
1767 refspec_appendn(&rs
, argv
, argc
- i
);
1771 #ifndef USE_CURL_MULTI
1772 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1776 usage(http_push_usage
);
1778 if (delete_branch
&& rs
.nr
!= 1)
1779 die("You must specify only one branch name when deleting a remote branch");
1781 setup_git_directory();
1783 memset(remote_dir_exists
, -1, 256);
1785 http_init(NULL
, repo
->url
, 1);
1787 #ifdef USE_CURL_MULTI
1788 is_running_queue
= 0;
1791 /* Verify DAV compliance/lock support */
1792 if (!locking_available()) {
1797 sigchain_push_common(remove_locks_on_signal
);
1799 /* Check whether the remote has server info files */
1800 repo
->can_update_info_refs
= 0;
1801 repo
->has_info_refs
= remote_exists("info/refs");
1802 repo
->has_info_packs
= remote_exists("objects/info/packs");
1803 if (repo
->has_info_refs
) {
1804 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1806 repo
->can_update_info_refs
= 1;
1808 error("cannot lock existing info/refs");
1813 if (repo
->has_info_packs
)
1816 /* Get a list of all local and remote heads to validate refspecs */
1817 local_refs
= get_local_heads();
1818 fprintf(stderr
, "Fetching remote heads...\n");
1819 get_dav_remote_heads();
1820 run_request_queue();
1822 /* Remove a remote branch if -d or -D was specified */
1823 if (delete_branch
) {
1824 const char *branch
= rs
.items
[i
].src
;
1825 if (delete_remote_branch(branch
, force_delete
) == -1) {
1826 fprintf(stderr
, "Unable to delete remote branch %s\n",
1829 printf("error %s cannot remove\n", branch
);
1835 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1840 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1842 printf("error null no match\n");
1848 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1849 struct strvec commit_argv
= STRVEC_INIT
;
1854 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1855 if (delete_remote_branch(ref
->name
, 1) == -1) {
1856 error("Could not remove %s", ref
->name
);
1858 printf("error %s cannot remove\n", ref
->name
);
1861 else if (helper_status
)
1862 printf("ok %s\n", ref
->name
);
1867 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1869 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1871 printf("ok %s up to date\n", ref
->name
);
1876 !is_null_oid(&ref
->old_oid
) &&
1878 if (!has_object_file(&ref
->old_oid
) ||
1879 !ref_newer(&ref
->peer_ref
->new_oid
,
1882 * We do not have the remote ref, or
1883 * we know that the remote ref is not
1884 * an ancestor of what we are trying to
1885 * push. Either way this can be losing
1886 * commits at the remote end and likely
1887 * we were not up to date to begin with.
1889 error("remote '%s' is not an ancestor of\n"
1891 "Maybe you are not up-to-date and "
1892 "need to pull first?",
1894 ref
->peer_ref
->name
);
1896 printf("error %s non-fast forward\n", ref
->name
);
1901 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1904 fprintf(stderr
, "updating '%s'", ref
->name
);
1905 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1906 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1907 fprintf(stderr
, "\n from %s\n to %s\n",
1908 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1911 printf("ok %s\n", ref
->name
);
1915 /* Lock remote branch ref */
1916 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1917 if (ref_lock
== NULL
) {
1918 fprintf(stderr
, "Unable to lock remote branch %s\n",
1921 printf("error %s lock error\n", ref
->name
);
1926 /* Set up revision info for this refspec */
1927 strvec_push(&commit_argv
, ""); /* ignored */
1928 strvec_push(&commit_argv
, "--objects");
1929 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1930 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1931 strvec_pushf(&commit_argv
, "^%s",
1932 oid_to_hex(&ref
->old_oid
));
1933 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1934 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1935 revs
.edge_hint
= 0; /* just in case */
1937 /* Generate a list of objects that need to be pushed */
1939 if (prepare_revision_walk(&revs
))
1940 die("revision walk setup failed");
1941 mark_edges_uninteresting(&revs
, NULL
, 0);
1942 objects_to_send
= get_delta(&revs
, ref_lock
);
1943 finish_all_active_slots();
1945 /* Push missing objects to remote, this would be a
1946 convenient time to pack them first if appropriate. */
1948 if (objects_to_send
)
1949 fprintf(stderr
, " sending %d objects\n",
1952 run_request_queue();
1954 /* Update the remote branch if all went well */
1955 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1959 fprintf(stderr
, " done\n");
1961 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1962 unlock_remote(ref_lock
);
1964 strvec_clear(&commit_argv
);
1967 /* Update remote server info if appropriate */
1968 if (repo
->has_info_refs
&& new_refs
) {
1969 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1970 fprintf(stderr
, "Updating remote server info\n");
1972 update_remote_info_refs(info_ref_lock
);
1974 fprintf(stderr
, "Unable to update server info\n");
1980 unlock_remote(info_ref_lock
);
1985 request
= request_queue_head
;
1986 while (request
!= NULL
) {
1987 next_request
= request
->next
;
1988 release_request(request
);
1989 request
= next_request
;