1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "repository.h"
14 #include "list-objects.h"
19 #include "tree-walk.h"
22 #include "object-file.h"
23 #include "object-store.h"
24 #include "commit-reach.h"
26 #ifdef EXPAT_NEEDS_XMLPARSE_H
32 static const char http_push_usage
[] =
33 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
40 #define XML_STATUS_OK 1
41 #define XML_STATUS_ERROR 0
44 #define PREV_BUF_SIZE 4096
47 #define DAV_LOCK "LOCK"
48 #define DAV_MKCOL "MKCOL"
49 #define DAV_MOVE "MOVE"
50 #define DAV_PROPFIND "PROPFIND"
52 #define DAV_UNLOCK "UNLOCK"
53 #define DAV_DELETE "DELETE"
56 #define DAV_PROP_LOCKWR (1u << 0)
57 #define DAV_PROP_LOCKEX (1u << 1)
58 #define DAV_LOCK_OK (1u << 2)
60 /* DAV XML properties */
61 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
62 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
63 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
64 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
65 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
66 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
67 #define DAV_PROPFIND_RESP ".multistatus.response"
68 #define DAV_PROPFIND_NAME ".multistatus.response.href"
69 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
71 /* DAV request body templates */
72 #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>"
73 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
74 #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>"
77 #define LOCK_REFRESH 30
79 /* Remember to update object flag allocation in object.h */
80 #define LOCAL (1u<<11)
81 #define REMOTE (1u<<12)
82 #define FETCHING (1u<<13)
83 #define PUSHING (1u<<14)
85 /* We allow "recursive" symbolic refs. Only within reason, though */
90 static signed char remote_dir_exists
[256];
92 static int push_verbosely
;
93 static int push_all
= MATCH_REFS_NONE
;
96 static int helper_status
;
98 static struct object_list
*objects
;
105 int can_update_info_refs
;
107 struct packed_git
*packs
;
108 struct remote_lock
*locks
;
111 static struct repo
*repo
;
113 enum transfer_state
{
125 struct transfer_request
{
127 struct packed_git
*target
;
130 struct remote_lock
*lock
;
131 struct curl_slist
*headers
;
132 struct buffer buffer
;
133 enum transfer_state state
;
134 CURLcode curl_result
;
135 char errorstr
[CURL_ERROR_SIZE
];
138 struct active_request_slot
*slot
;
139 struct transfer_request
*next
;
142 static struct transfer_request
*request_queue_head
;
148 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
156 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
160 struct remote_lock
*next
;
163 /* Flags that control remote_ls processing */
164 #define PROCESS_FILES (1u << 0)
165 #define PROCESS_DIRS (1u << 1)
166 #define RECURSIVE (1u << 2)
168 /* Flags that remote_ls passes to callback functions */
169 #define IS_DIR (1u << 0)
171 struct remote_ls_ctx
{
173 void (*userFunc
)(struct remote_ls_ctx
*ls
);
178 struct remote_ls_ctx
*parent
;
181 /* get_dav_token_headers options */
182 enum dav_header_flag
{
183 DAV_HEADER_IF
= (1u << 0),
184 DAV_HEADER_LOCK
= (1u << 1),
185 DAV_HEADER_TIMEOUT
= (1u << 2)
188 static char *xml_entities(const char *s
)
190 struct strbuf buf
= STRBUF_INIT
;
191 strbuf_addstr_xml_quoted(&buf
, s
);
192 return strbuf_detach(&buf
, NULL
);
195 static void curl_setup_http_get(CURL
*curl
, const char *url
,
196 const char *custom_req
)
198 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1L);
199 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
200 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
201 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
204 static void curl_setup_http(CURL
*curl
, const char *url
,
205 const char *custom_req
, struct buffer
*buffer
,
206 curl_write_callback write_fn
)
208 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
209 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
210 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
211 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
212 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
213 curl_easy_setopt(curl
, CURLOPT_SEEKFUNCTION
, seek_buffer
);
214 curl_easy_setopt(curl
, CURLOPT_SEEKDATA
, buffer
);
215 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
216 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0L);
217 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
218 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
221 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
223 struct strbuf buf
= STRBUF_INIT
;
224 struct curl_slist
*dav_headers
= http_copy_default_headers();
226 if (options
& DAV_HEADER_IF
) {
227 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
228 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
231 if (options
& DAV_HEADER_LOCK
) {
232 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
233 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
236 if (options
& DAV_HEADER_TIMEOUT
) {
237 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
238 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
241 strbuf_release(&buf
);
246 static void finish_request(struct transfer_request
*request
);
247 static void release_request(struct transfer_request
*request
);
249 static void process_response(void *callback_data
)
251 struct transfer_request
*request
=
252 (struct transfer_request
*)callback_data
;
254 finish_request(request
);
257 static void start_fetch_loose(struct transfer_request
*request
)
259 struct active_request_slot
*slot
;
260 struct http_object_request
*obj_req
;
262 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
264 request
->state
= ABORTED
;
268 slot
= obj_req
->slot
;
269 slot
->callback_func
= process_response
;
270 slot
->callback_data
= request
;
271 request
->slot
= slot
;
272 request
->userData
= obj_req
;
274 /* Try to get the request started, abort the request on error */
275 request
->state
= RUN_FETCH_LOOSE
;
276 if (!start_active_slot(slot
)) {
277 fprintf(stderr
, "Unable to start GET request\n");
278 repo
->can_update_info_refs
= 0;
279 release_http_object_request(&obj_req
);
280 release_request(request
);
284 static void start_mkcol(struct transfer_request
*request
)
286 char *hex
= oid_to_hex(&request
->obj
->oid
);
287 struct active_request_slot
*slot
;
289 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
291 slot
= get_active_slot();
292 slot
->callback_func
= process_response
;
293 slot
->callback_data
= request
;
294 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
295 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
297 if (start_active_slot(slot
)) {
298 request
->slot
= slot
;
299 request
->state
= RUN_MKCOL
;
301 request
->state
= ABORTED
;
302 FREE_AND_NULL(request
->url
);
306 static void start_fetch_packed(struct transfer_request
*request
)
308 struct packed_git
*target
;
310 struct transfer_request
*check_request
= request_queue_head
;
311 struct http_pack_request
*preq
;
313 target
= find_oid_pack(&request
->obj
->oid
, repo
->packs
);
315 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
316 repo
->can_update_info_refs
= 0;
317 release_request(request
);
320 close_pack_index(target
);
321 request
->target
= target
;
323 fprintf(stderr
, "Fetching pack %s\n",
324 hash_to_hex(target
->hash
));
325 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
327 preq
= new_http_pack_request(target
->hash
, repo
->url
);
329 repo
->can_update_info_refs
= 0;
333 /* Make sure there isn't another open request for this pack */
334 while (check_request
) {
335 if (check_request
->state
== RUN_FETCH_PACKED
&&
336 !strcmp(check_request
->url
, preq
->url
)) {
337 release_http_pack_request(preq
);
338 release_request(request
);
341 check_request
= check_request
->next
;
344 preq
->slot
->callback_func
= process_response
;
345 preq
->slot
->callback_data
= request
;
346 request
->slot
= preq
->slot
;
347 request
->userData
= preq
;
349 /* Try to get the request started, abort the request on error */
350 request
->state
= RUN_FETCH_PACKED
;
351 if (!start_active_slot(preq
->slot
)) {
352 fprintf(stderr
, "Unable to start GET request\n");
353 release_http_pack_request(preq
);
354 repo
->can_update_info_refs
= 0;
355 release_request(request
);
359 static void start_put(struct transfer_request
*request
)
361 char *hex
= oid_to_hex(&request
->obj
->oid
);
362 struct active_request_slot
*slot
;
363 struct strbuf buf
= STRBUF_INIT
;
364 enum object_type type
;
372 unpacked
= repo_read_object_file(the_repository
, &request
->obj
->oid
,
374 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, len
);
377 git_deflate_init(&stream
, zlib_compression_level
);
378 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
379 strbuf_grow(&request
->buffer
.buf
, size
);
380 request
->buffer
.posn
= 0;
383 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
384 stream
.avail_out
= size
;
387 stream
.next_in
= (void *)hdr
;
388 stream
.avail_in
= hdrlen
;
389 while (git_deflate(&stream
, 0) == Z_OK
)
392 /* Then the data itself.. */
393 stream
.next_in
= unpacked
;
394 stream
.avail_in
= len
;
395 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
397 git_deflate_end(&stream
);
400 request
->buffer
.buf
.len
= stream
.total_out
;
402 strbuf_addstr(&buf
, "Destination: ");
403 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
404 request
->dest
= strbuf_detach(&buf
, NULL
);
406 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
407 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
408 request
->url
= strbuf_detach(&buf
, NULL
);
410 slot
= get_active_slot();
411 slot
->callback_func
= process_response
;
412 slot
->callback_data
= request
;
413 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
414 &request
->buffer
, fwrite_null
);
416 if (start_active_slot(slot
)) {
417 request
->slot
= slot
;
418 request
->state
= RUN_PUT
;
420 request
->state
= ABORTED
;
421 FREE_AND_NULL(request
->url
);
425 static void start_move(struct transfer_request
*request
)
427 struct active_request_slot
*slot
;
428 struct curl_slist
*dav_headers
= http_copy_default_headers();
430 slot
= get_active_slot();
431 slot
->callback_func
= process_response
;
432 slot
->callback_data
= request
;
433 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
434 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
435 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
436 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
438 if (start_active_slot(slot
)) {
439 request
->slot
= slot
;
440 request
->state
= RUN_MOVE
;
441 request
->headers
= dav_headers
;
443 request
->state
= ABORTED
;
444 FREE_AND_NULL(request
->url
);
445 curl_slist_free_all(dav_headers
);
449 static int refresh_lock(struct remote_lock
*lock
)
451 struct active_request_slot
*slot
;
452 struct slot_results results
;
453 struct curl_slist
*dav_headers
;
456 lock
->refreshing
= 1;
458 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
460 slot
= get_active_slot();
461 slot
->results
= &results
;
462 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
463 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
465 if (start_active_slot(slot
)) {
466 run_active_slot(slot
);
467 if (results
.curl_result
!= CURLE_OK
) {
468 fprintf(stderr
, "LOCK HTTP error %ld\n",
471 lock
->start_time
= time(NULL
);
476 lock
->refreshing
= 0;
477 curl_slist_free_all(dav_headers
);
482 static void check_locks(void)
484 struct remote_lock
*lock
= repo
->locks
;
485 time_t current_time
= time(NULL
);
489 time_remaining
= lock
->start_time
+ lock
->timeout
-
491 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
492 if (!refresh_lock(lock
)) {
494 "Unable to refresh lock for %s\n",
504 static void release_request(struct transfer_request
*request
)
506 struct transfer_request
*entry
= request_queue_head
;
508 if (request
== request_queue_head
) {
509 request_queue_head
= request
->next
;
511 while (entry
&& entry
->next
!= request
)
514 entry
->next
= request
->next
;
519 strbuf_release(&request
->buffer
.buf
);
523 static void finish_request(struct transfer_request
*request
)
525 struct http_pack_request
*preq
;
526 struct http_object_request
*obj_req
;
528 request
->curl_result
= request
->slot
->curl_result
;
529 request
->http_code
= request
->slot
->http_code
;
530 request
->slot
= NULL
;
532 /* Keep locks active */
535 if (request
->headers
)
536 curl_slist_free_all(request
->headers
);
538 /* URL is reused for MOVE after PUT and used during FETCH */
539 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
540 FREE_AND_NULL(request
->url
);
543 if (request
->state
== RUN_MKCOL
) {
544 if (request
->curl_result
== CURLE_OK
||
545 request
->http_code
== 405) {
546 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
549 fprintf(stderr
, "MKCOL %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_PUT
) {
556 if (request
->curl_result
== CURLE_OK
) {
559 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
560 oid_to_hex(&request
->obj
->oid
),
561 request
->curl_result
, request
->http_code
);
562 request
->state
= ABORTED
;
565 } else if (request
->state
== RUN_MOVE
) {
566 if (request
->curl_result
== CURLE_OK
) {
568 fprintf(stderr
, " sent %s\n",
569 oid_to_hex(&request
->obj
->oid
));
570 request
->obj
->flags
|= REMOTE
;
571 release_request(request
);
573 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
574 oid_to_hex(&request
->obj
->oid
),
575 request
->curl_result
, request
->http_code
);
576 request
->state
= ABORTED
;
579 } else if (request
->state
== RUN_FETCH_LOOSE
) {
580 obj_req
= (struct http_object_request
*)request
->userData
;
582 if (finish_http_object_request(obj_req
) == 0)
583 if (obj_req
->rename
== 0)
584 request
->obj
->flags
|= (LOCAL
| REMOTE
);
586 release_http_object_request(&obj_req
);
588 /* Try fetching packed if necessary */
589 if (request
->obj
->flags
& LOCAL
) {
590 release_request(request
);
592 start_fetch_packed(request
);
594 } else if (request
->state
== RUN_FETCH_PACKED
) {
596 if (request
->curl_result
!= CURLE_OK
) {
597 fprintf(stderr
, "Unable to get pack file %s\n%s",
598 request
->url
, curl_errorstr
);
600 preq
= (struct http_pack_request
*)request
->userData
;
603 if (finish_http_pack_request(preq
) == 0)
605 release_http_pack_request(preq
);
609 repo
->can_update_info_refs
= 0;
611 http_install_packfile(request
->target
, &repo
->packs
);
612 release_request(request
);
616 static int is_running_queue
;
617 static int fill_active_slot(void *data UNUSED
)
619 struct transfer_request
*request
;
621 if (aborted
|| !is_running_queue
)
624 for (request
= request_queue_head
; request
; request
= request
->next
) {
625 if (request
->state
== NEED_FETCH
) {
626 start_fetch_loose(request
);
628 } else if (pushing
&& request
->state
== NEED_PUSH
) {
629 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
632 start_mkcol(request
);
640 static void get_remote_object_list(unsigned char parent
);
642 static void add_fetch_request(struct object
*obj
)
644 struct transfer_request
*request
;
649 * Don't fetch the object if it's known to exist locally
650 * or is already in the request queue
652 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
653 get_remote_object_list(obj
->oid
.hash
[0]);
654 if (obj
->flags
& (LOCAL
| FETCHING
))
657 obj
->flags
|= FETCHING
;
658 CALLOC_ARRAY(request
, 1);
660 request
->state
= NEED_FETCH
;
661 strbuf_init(&request
->buffer
.buf
, 0);
662 request
->next
= request_queue_head
;
663 request_queue_head
= request
;
669 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
671 struct transfer_request
*request
;
672 struct packed_git
*target
;
674 /* Keep locks active */
678 * Don't push the object if it's known to exist on the remote
679 * or is already in the request queue
681 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
682 get_remote_object_list(obj
->oid
.hash
[0]);
683 if (obj
->flags
& (REMOTE
| PUSHING
))
685 target
= find_oid_pack(&obj
->oid
, repo
->packs
);
687 obj
->flags
|= REMOTE
;
691 obj
->flags
|= PUSHING
;
692 CALLOC_ARRAY(request
, 1);
694 request
->lock
= lock
;
695 request
->state
= NEED_PUSH
;
696 strbuf_init(&request
->buffer
.buf
, 0);
697 request
->next
= request_queue_head
;
698 request_queue_head
= request
;
706 static int fetch_indices(void)
711 fprintf(stderr
, "Getting pack list\n");
713 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
715 case HTTP_MISSING_TARGET
:
725 static void one_remote_object(const struct object_id
*oid
)
729 obj
= lookup_object(the_repository
, oid
);
731 obj
= parse_object(the_repository
, oid
);
733 /* Ignore remote objects that don't exist locally */
737 obj
->flags
|= REMOTE
;
738 if (!object_list_contains(objects
, obj
))
739 object_list_insert(obj
, &objects
);
742 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
744 int *lock_flags
= (int *)ctx
->userData
;
747 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
748 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
749 (*lock_flags
& DAV_PROP_LOCKWR
)) {
750 *lock_flags
|= DAV_LOCK_OK
;
752 *lock_flags
&= DAV_LOCK_OK
;
753 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
754 *lock_flags
|= DAV_PROP_LOCKWR
;
755 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
756 *lock_flags
|= DAV_PROP_LOCKEX
;
761 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
763 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
764 struct git_hash_ctx hash_ctx
;
765 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
767 if (tag_closed
&& ctx
->cdata
) {
768 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
769 lock
->owner
= xstrdup(ctx
->cdata
);
770 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
772 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
773 lock
->timeout
= strtol(arg
, NULL
, 10);
774 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
775 lock
->token
= xstrdup(ctx
->cdata
);
777 the_hash_algo
->init_fn(&hash_ctx
);
778 git_hash_update(&hash_ctx
, lock
->token
, strlen(lock
->token
));
779 git_hash_final(lock_token_hash
, &hash_ctx
);
781 lock
->tmpfile_suffix
[0] = '_';
782 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
787 static void one_remote_ref(const char *refname
);
790 xml_start_tag(void *userData
, const char *name
, const char **atts UNUSED
)
792 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
793 const char *c
= strchr(name
, ':');
794 int old_namelen
, new_len
;
801 old_namelen
= strlen(ctx
->name
);
802 new_len
= old_namelen
+ strlen(c
) + 2;
804 if (new_len
> ctx
->len
) {
805 ctx
->name
= xrealloc(ctx
->name
, new_len
);
808 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
810 FREE_AND_NULL(ctx
->cdata
);
812 ctx
->userFunc(ctx
, 0);
816 xml_end_tag(void *userData
, const char *name
)
818 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
819 const char *c
= strchr(name
, ':');
822 ctx
->userFunc(ctx
, 1);
829 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
834 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
836 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
838 ctx
->cdata
= xmemdupz(s
, len
);
841 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
843 struct active_request_slot
*slot
;
844 struct slot_results results
;
845 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
846 struct strbuf in_buffer
= STRBUF_INIT
;
849 char timeout_header
[25];
850 struct remote_lock
*lock
= NULL
;
851 struct curl_slist
*dav_headers
= http_copy_default_headers();
855 url
= xstrfmt("%s%s", repo
->url
, path
);
857 /* Make sure leading directories exist for the remote ref */
858 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
860 char saved_character
= ep
[1];
862 slot
= get_active_slot();
863 slot
->results
= &results
;
864 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
865 if (start_active_slot(slot
)) {
866 run_active_slot(slot
);
867 if (results
.curl_result
!= CURLE_OK
&&
868 results
.http_code
!= 405) {
870 "Unable to create branch path %s\n",
876 fprintf(stderr
, "Unable to start MKCOL request\n");
880 ep
[1] = saved_character
;
881 ep
= strchr(ep
+ 1, '/');
884 escaped
= xml_entities(ident_default_email());
885 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
888 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
889 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
890 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
892 slot
= get_active_slot();
893 slot
->results
= &results
;
894 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
895 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
896 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
898 CALLOC_ARRAY(lock
, 1);
901 if (start_active_slot(slot
)) {
902 run_active_slot(slot
);
903 if (results
.curl_result
== CURLE_OK
) {
904 XML_Parser parser
= XML_ParserCreate(NULL
);
905 enum XML_Status result
;
906 ctx
.name
= xcalloc(10, 1);
909 ctx
.userFunc
= handle_new_lock_ctx
;
911 XML_SetUserData(parser
, &ctx
);
912 XML_SetElementHandler(parser
, xml_start_tag
,
914 XML_SetCharacterDataHandler(parser
, xml_cdata
);
915 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 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
1026 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
1028 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1031 if (hex_to_bytes(oid
->hash
, path
, 1))
1034 path
++; /* skip '/' */
1036 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1039 static void process_ls_object(struct remote_ls_ctx
*ls
)
1041 unsigned int *parent
= (unsigned int *)ls
->userData
;
1042 const char *path
= ls
->dentry_name
;
1043 struct object_id oid
;
1045 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1046 remote_dir_exists
[*parent
] = 1;
1050 if (!skip_prefix(path
, "objects/", &path
) ||
1051 get_oid_hex_from_objpath(path
, &oid
))
1054 one_remote_object(&oid
);
1057 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1059 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1060 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1064 if (!(ls
->dentry_flags
& IS_DIR
))
1065 one_remote_ref(ls
->dentry_name
);
1068 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1070 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1073 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1074 if (ls
->dentry_flags
& IS_DIR
) {
1076 /* ensure collection names end with slash */
1077 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1079 if (ls
->flags
& PROCESS_DIRS
) {
1082 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1083 ls
->flags
& RECURSIVE
) {
1084 remote_ls(ls
->dentry_name
,
1089 } else if (ls
->flags
& PROCESS_FILES
) {
1092 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1093 char *path
= ctx
->cdata
;
1094 if (*ctx
->cdata
== 'h') {
1095 path
= strstr(path
, "//");
1097 path
= strchr(path
+2, '/');
1101 const char *url
= repo
->url
;
1104 if (strncmp(path
, url
, repo
->path_len
))
1105 error("Parsed path '%s' does not match url: '%s'",
1108 path
+= repo
->path_len
;
1109 ls
->dentry_name
= xstrdup(path
);
1112 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1113 ls
->dentry_flags
|= IS_DIR
;
1115 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1116 FREE_AND_NULL(ls
->dentry_name
);
1117 ls
->dentry_flags
= 0;
1122 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1123 * should _only_ heed the information from that file, instead of trying to
1124 * determine the refs from the remote file system (badly: it does not even
1125 * know about packed-refs).
1127 static void remote_ls(const char *path
, int flags
,
1128 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1131 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1132 struct active_request_slot
*slot
;
1133 struct slot_results results
;
1134 struct strbuf in_buffer
= STRBUF_INIT
;
1135 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1136 struct curl_slist
*dav_headers
= http_copy_default_headers();
1138 struct remote_ls_ctx ls
;
1141 ls
.path
= xstrdup(path
);
1142 ls
.dentry_name
= NULL
;
1143 ls
.dentry_flags
= 0;
1144 ls
.userData
= userData
;
1145 ls
.userFunc
= userFunc
;
1147 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1149 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1150 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1152 slot
= get_active_slot();
1153 slot
->results
= &results
;
1154 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1155 &out_buffer
, fwrite_buffer
);
1156 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1157 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1159 if (start_active_slot(slot
)) {
1160 run_active_slot(slot
);
1161 if (results
.curl_result
== CURLE_OK
) {
1162 XML_Parser parser
= XML_ParserCreate(NULL
);
1163 enum XML_Status result
;
1164 ctx
.name
= xcalloc(10, 1);
1167 ctx
.userFunc
= handle_remote_ls_ctx
;
1169 XML_SetUserData(parser
, &ctx
);
1170 XML_SetElementHandler(parser
, xml_start_tag
,
1172 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1173 result
= XML_Parse(parser
, in_buffer
.buf
,
1178 if (result
!= XML_STATUS_OK
) {
1179 fprintf(stderr
, "XML error: %s\n",
1181 XML_GetErrorCode(parser
)));
1183 XML_ParserFree(parser
);
1186 fprintf(stderr
, "Unable to start PROPFIND request\n");
1190 free(ls
.dentry_name
);
1192 strbuf_release(&out_buffer
.buf
);
1193 strbuf_release(&in_buffer
);
1194 curl_slist_free_all(dav_headers
);
1197 static void get_remote_object_list(unsigned char parent
)
1199 char path
[] = "objects/XX/";
1200 static const char hex
[] = "0123456789abcdef";
1201 unsigned int val
= parent
;
1203 path
[8] = hex
[val
>> 4];
1204 path
[9] = hex
[val
& 0xf];
1205 remote_dir_exists
[val
] = 0;
1206 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1207 process_ls_object
, &val
);
1210 static int locking_available(void)
1212 struct active_request_slot
*slot
;
1213 struct slot_results results
;
1214 struct strbuf in_buffer
= STRBUF_INIT
;
1215 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1216 struct curl_slist
*dav_headers
= http_copy_default_headers();
1221 escaped
= xml_entities(repo
->url
);
1222 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1225 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1226 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1228 slot
= get_active_slot();
1229 slot
->results
= &results
;
1230 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1231 &out_buffer
, fwrite_buffer
);
1232 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1233 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1235 if (start_active_slot(slot
)) {
1236 run_active_slot(slot
);
1237 if (results
.curl_result
== CURLE_OK
) {
1238 XML_Parser parser
= XML_ParserCreate(NULL
);
1239 enum XML_Status result
;
1240 ctx
.name
= xcalloc(10, 1);
1243 ctx
.userFunc
= handle_lockprop_ctx
;
1244 ctx
.userData
= &lock_flags
;
1245 XML_SetUserData(parser
, &ctx
);
1246 XML_SetElementHandler(parser
, xml_start_tag
,
1248 result
= XML_Parse(parser
, in_buffer
.buf
,
1252 if (result
!= XML_STATUS_OK
) {
1253 fprintf(stderr
, "XML error: %s\n",
1255 XML_GetErrorCode(parser
)));
1258 XML_ParserFree(parser
);
1260 error("no DAV locking support on %s",
1264 error("Cannot access URL %s, return code %d",
1265 repo
->url
, results
.curl_result
);
1269 error("Unable to start PROPFIND request on %s", repo
->url
);
1272 strbuf_release(&out_buffer
.buf
);
1273 strbuf_release(&in_buffer
);
1274 curl_slist_free_all(dav_headers
);
1279 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1281 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1285 return &entry
->next
;
1288 static struct object_list
**process_blob(struct blob
*blob
,
1289 struct object_list
**p
)
1291 struct object
*obj
= &blob
->object
;
1293 obj
->flags
|= LOCAL
;
1295 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1299 return add_one_object(obj
, p
);
1302 static struct object_list
**process_tree(struct tree
*tree
,
1303 struct object_list
**p
)
1305 struct object
*obj
= &tree
->object
;
1306 struct tree_desc desc
;
1307 struct name_entry entry
;
1309 obj
->flags
|= LOCAL
;
1311 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1313 if (parse_tree(tree
) < 0)
1314 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1317 p
= add_one_object(obj
, p
);
1319 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
1321 while (tree_entry(&desc
, &entry
))
1322 switch (object_type(entry
.mode
)) {
1324 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1328 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1332 /* Subproject commit - not in this repository */
1336 free_tree_buffer(tree
);
1340 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1342 struct commit
*commit
;
1343 struct object_list
**p
= &objects
;
1346 while ((commit
= get_revision(revs
)) != NULL
) {
1347 p
= process_tree(repo_get_commit_tree(the_repository
, commit
),
1349 commit
->object
.flags
|= LOCAL
;
1350 if (!(commit
->object
.flags
& UNINTERESTING
))
1351 count
+= add_send_request(&commit
->object
, lock
);
1354 for (size_t i
= 0; i
< revs
->pending
.nr
; i
++) {
1355 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1356 struct object
*obj
= entry
->item
;
1357 const char *name
= entry
->name
;
1359 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1361 if (obj
->type
== OBJ_TAG
) {
1363 p
= add_one_object(obj
, p
);
1366 if (obj
->type
== OBJ_TREE
) {
1367 p
= process_tree((struct tree
*)obj
, p
);
1370 if (obj
->type
== OBJ_BLOB
) {
1371 p
= process_blob((struct blob
*)obj
, p
);
1374 die("unknown pending object %s (%s)", oid_to_hex(&obj
->oid
), name
);
1378 struct object_list
*next
= objects
->next
;
1380 if (!(objects
->item
->flags
& UNINTERESTING
))
1381 count
+= add_send_request(objects
->item
, lock
);
1390 static int update_remote(const struct object_id
*oid
, struct remote_lock
*lock
)
1392 struct active_request_slot
*slot
;
1393 struct slot_results results
;
1394 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1395 struct curl_slist
*dav_headers
;
1397 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1399 strbuf_addf(&out_buffer
.buf
, "%s\n", oid_to_hex(oid
));
1401 slot
= get_active_slot();
1402 slot
->results
= &results
;
1403 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1404 &out_buffer
, fwrite_null
);
1405 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1407 if (start_active_slot(slot
)) {
1408 run_active_slot(slot
);
1409 strbuf_release(&out_buffer
.buf
);
1410 curl_slist_free_all(dav_headers
);
1411 if (results
.curl_result
!= CURLE_OK
) {
1413 "PUT error: curl result=%d, HTTP code=%ld\n",
1414 results
.curl_result
, results
.http_code
);
1415 /* We should attempt recovery? */
1419 strbuf_release(&out_buffer
.buf
);
1420 curl_slist_free_all(dav_headers
);
1421 fprintf(stderr
, "Unable to start PUT request\n");
1428 static struct ref
*remote_refs
;
1430 static void one_remote_ref(const char *refname
)
1435 ref
= alloc_ref(refname
);
1437 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1439 "Unable to fetch ref %s from %s\n",
1440 refname
, repo
->url
);
1446 * Fetch a copy of the object if it doesn't exist locally - it
1447 * may be required for updating server info later.
1449 if (repo
->can_update_info_refs
&&
1450 !has_object(the_repository
, &ref
->old_oid
,
1451 HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
)) {
1452 obj
= lookup_unknown_object(the_repository
, &ref
->old_oid
);
1453 fprintf(stderr
, " fetch %s for %s\n",
1454 oid_to_hex(&ref
->old_oid
), refname
);
1455 add_fetch_request(obj
);
1458 ref
->next
= remote_refs
;
1462 static void get_dav_remote_heads(void)
1464 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1467 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1469 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1473 ref
= alloc_ref(ls
->dentry_name
);
1475 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1477 "Unable to fetch ref %s from %s\n",
1478 ls
->dentry_name
, repo
->url
);
1484 o
= parse_object(the_repository
, &ref
->old_oid
);
1487 "Unable to parse object %s for remote ref %s\n",
1488 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1494 strbuf_addf(buf
, "%s\t%s\n",
1495 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1497 if (o
->type
== OBJ_TAG
) {
1498 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1500 strbuf_addf(buf
, "%s\t%s^{}\n",
1501 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1506 static void update_remote_info_refs(struct remote_lock
*lock
)
1508 struct buffer buffer
= { STRBUF_INIT
, 0 };
1509 struct active_request_slot
*slot
;
1510 struct slot_results results
;
1511 struct curl_slist
*dav_headers
;
1513 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1514 add_remote_info_ref
, &buffer
.buf
);
1516 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1518 slot
= get_active_slot();
1519 slot
->results
= &results
;
1520 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1521 &buffer
, fwrite_null
);
1522 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1524 if (start_active_slot(slot
)) {
1525 run_active_slot(slot
);
1526 if (results
.curl_result
!= CURLE_OK
) {
1528 "PUT error: curl result=%d, HTTP code=%ld\n",
1529 results
.curl_result
, results
.http_code
);
1532 curl_slist_free_all(dav_headers
);
1534 strbuf_release(&buffer
.buf
);
1537 static int remote_exists(const char *path
)
1539 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1543 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1547 case HTTP_MISSING_TARGET
:
1551 error("unable to access '%s': %s", url
, curl_errorstr
);
1560 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1562 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1563 struct strbuf buffer
= STRBUF_INIT
;
1566 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1567 die("Couldn't get %s for remote symref\n%s", url
,
1571 FREE_AND_NULL(*symref
);
1572 oidclr(oid
, the_repository
->hash_algo
);
1574 if (buffer
.len
== 0)
1577 /* Cut off trailing newline. */
1578 strbuf_rtrim(&buffer
);
1580 /* If it's a symref, set the refname; otherwise try for a sha1 */
1581 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1582 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1584 get_oid_hex(buffer
.buf
, oid
);
1587 strbuf_release(&buffer
);
1590 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1592 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1593 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1595 int ret
= repo_in_merge_bases(the_repository
, branch
, head
);
1602 static int delete_remote_branch(const char *pattern
, int force
)
1604 struct ref
*refs
= remote_refs
;
1605 struct ref
*remote_ref
= NULL
;
1606 struct object_id head_oid
;
1607 char *symref
= NULL
;
1609 int patlen
= strlen(pattern
);
1611 struct active_request_slot
*slot
;
1612 struct slot_results results
;
1615 /* Find the remote branch(es) matching the specified branch name */
1616 for (match
= 0; refs
; refs
= refs
->next
) {
1617 char *name
= refs
->name
;
1618 int namelen
= strlen(name
);
1619 if (namelen
< patlen
||
1620 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1622 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1628 return error("No remote branch matches %s", pattern
);
1630 return error("More than one remote branch matches %s",
1634 * Remote HEAD must be a symref (not exactly foolproof; a remote
1635 * symlink to a symref will look like a symref)
1637 fetch_symref("HEAD", &symref
, &head_oid
);
1639 return error("Remote HEAD is not a symref");
1641 /* Remote branch must not be the remote HEAD */
1642 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1643 if (!strcmp(remote_ref
->name
, symref
))
1644 return error("Remote branch %s is the current HEAD",
1646 fetch_symref(symref
, &symref
, &head_oid
);
1649 /* Run extra sanity checks if delete is not forced */
1651 /* Remote HEAD must resolve to a known object */
1653 return error("Remote HEAD symrefs too deep");
1654 if (is_null_oid(&head_oid
))
1655 return error("Unable to resolve remote HEAD");
1656 if (!has_object(the_repository
, &head_oid
, HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
))
1657 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1659 /* Remote branch must resolve to a known object */
1660 if (is_null_oid(&remote_ref
->old_oid
))
1661 return error("Unable to resolve remote branch %s",
1663 if (!has_object(the_repository
, &remote_ref
->old_oid
, HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
))
1664 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
));
1666 /* Remote branch must be an ancestor of remote HEAD */
1667 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1668 return error("The branch '%s' is not an ancestor "
1669 "of your current HEAD.\n"
1670 "If you are sure you want to delete it,"
1671 " run:\n\t'git http-push -D %s %s'",
1672 remote_ref
->name
, repo
->url
, pattern
);
1676 /* Send delete request */
1677 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1680 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1681 slot
= get_active_slot();
1682 slot
->results
= &results
;
1683 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1684 if (start_active_slot(slot
)) {
1685 run_active_slot(slot
);
1687 if (results
.curl_result
!= CURLE_OK
)
1688 return error("DELETE request failed (%d/%ld)",
1689 results
.curl_result
, results
.http_code
);
1692 return error("Unable to start DELETE request");
1698 static void run_request_queue(void)
1700 is_running_queue
= 1;
1701 fill_active_slots();
1702 add_fill_function(NULL
, fill_active_slot
);
1704 finish_all_active_slots();
1705 fill_active_slots();
1706 } while (request_queue_head
&& !aborted
);
1708 is_running_queue
= 0;
1711 int cmd_main(int argc
, const char **argv
)
1713 struct transfer_request
*request
;
1714 struct transfer_request
*next_request
;
1715 struct refspec rs
= REFSPEC_INIT_PUSH
;
1716 struct remote_lock
*ref_lock
= NULL
;
1717 struct remote_lock
*info_ref_lock
= NULL
;
1718 int delete_branch
= 0;
1719 int force_delete
= 0;
1720 int objects_to_send
;
1724 struct ref
*ref
, *local_refs
= NULL
;
1726 CALLOC_ARRAY(repo
, 1);
1729 for (i
= 1; i
< argc
; i
++, argv
++) {
1730 const char *arg
= *argv
;
1733 if (!strcmp(arg
, "--all")) {
1734 push_all
= MATCH_REFS_ALL
;
1737 if (!strcmp(arg
, "--force")) {
1741 if (!strcmp(arg
, "--dry-run")) {
1745 if (!strcmp(arg
, "--helper-status")) {
1749 if (!strcmp(arg
, "--verbose")) {
1751 http_is_verbose
= 1;
1754 if (!strcmp(arg
, "-d")) {
1758 if (!strcmp(arg
, "-D")) {
1763 if (!strcmp(arg
, "-h"))
1764 usage(http_push_usage
);
1767 char *path
= strstr(arg
, "//");
1768 str_end_url_with_slash(arg
, &repo
->url
);
1769 repo
->path_len
= strlen(repo
->url
);
1771 repo
->path
= strchr(path
+2, '/');
1773 repo
->path_len
= strlen(repo
->path
);
1777 refspec_appendn(&rs
, argv
, argc
- i
);
1782 usage(http_push_usage
);
1784 if (delete_branch
&& rs
.nr
!= 1)
1785 die("You must specify only one branch name when deleting a remote branch");
1787 setup_git_directory();
1789 memset(remote_dir_exists
, -1, 256);
1791 http_init(NULL
, repo
->url
, 1);
1793 is_running_queue
= 0;
1795 /* Verify DAV compliance/lock support */
1796 if (!locking_available()) {
1801 sigchain_push_common(remove_locks_on_signal
);
1803 /* Check whether the remote has server info files */
1804 repo
->can_update_info_refs
= 0;
1805 repo
->has_info_refs
= remote_exists("info/refs");
1806 repo
->has_info_packs
= remote_exists("objects/info/packs");
1807 if (repo
->has_info_refs
) {
1808 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1810 repo
->can_update_info_refs
= 1;
1812 error("cannot lock existing info/refs");
1817 if (repo
->has_info_packs
)
1820 /* Get a list of all local and remote heads to validate refspecs */
1821 local_refs
= get_local_heads();
1822 fprintf(stderr
, "Fetching remote heads...\n");
1823 get_dav_remote_heads();
1824 run_request_queue();
1826 /* Remove a remote branch if -d or -D was specified */
1827 if (delete_branch
) {
1828 const char *branch
= rs
.items
[i
].src
;
1829 if (delete_remote_branch(branch
, force_delete
) == -1) {
1830 fprintf(stderr
, "Unable to delete remote branch %s\n",
1833 printf("error %s cannot remove\n", branch
);
1839 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1844 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1846 printf("error null no match\n");
1852 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1853 struct rev_info revs
;
1854 struct strvec commit_argv
= STRVEC_INIT
;
1859 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1860 if (delete_remote_branch(ref
->name
, 1) == -1) {
1861 error("Could not remove %s", ref
->name
);
1863 printf("error %s cannot remove\n", ref
->name
);
1866 else if (helper_status
)
1867 printf("ok %s\n", ref
->name
);
1872 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1874 /* stable plumbing output; do not modify or localize */
1875 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1877 printf("ok %s up to date\n", ref
->name
);
1882 !is_null_oid(&ref
->old_oid
) &&
1884 if (!has_object(the_repository
, &ref
->old_oid
,
1885 HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
) ||
1886 !ref_newer(&ref
->peer_ref
->new_oid
,
1889 * We do not have the remote ref, or
1890 * we know that the remote ref is not
1891 * an ancestor of what we are trying to
1892 * push. Either way this can be losing
1893 * commits at the remote end and likely
1894 * we were not up to date to begin with.
1896 /* stable plumbing output; do not modify or localize */
1897 error("remote '%s' is not an ancestor of\n"
1899 "Maybe you are not up-to-date and "
1900 "need to pull first?",
1902 ref
->peer_ref
->name
);
1904 printf("error %s non-fast forward\n", ref
->name
);
1909 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1912 fprintf(stderr
, "updating '%s'", ref
->name
);
1913 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1914 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1915 fprintf(stderr
, "\n from %s\n to %s\n",
1916 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1919 printf("ok %s\n", ref
->name
);
1923 /* Lock remote branch ref */
1924 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1926 fprintf(stderr
, "Unable to lock remote branch %s\n",
1929 printf("error %s lock error\n", ref
->name
);
1934 /* Set up revision info for this refspec */
1935 strvec_push(&commit_argv
, ""); /* ignored */
1936 strvec_push(&commit_argv
, "--objects");
1937 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1938 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1939 strvec_pushf(&commit_argv
, "^%s",
1940 oid_to_hex(&ref
->old_oid
));
1941 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1942 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1943 revs
.edge_hint
= 0; /* just in case */
1945 /* Generate a list of objects that need to be pushed */
1947 if (prepare_revision_walk(&revs
))
1948 die("revision walk setup failed");
1949 mark_edges_uninteresting(&revs
, NULL
, 0);
1950 objects_to_send
= get_delta(&revs
, ref_lock
);
1951 finish_all_active_slots();
1953 /* Push missing objects to remote, this would be a
1954 convenient time to pack them first if appropriate. */
1956 if (objects_to_send
)
1957 fprintf(stderr
, " sending %d objects\n",
1960 run_request_queue();
1962 /* Update the remote branch if all went well */
1963 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1967 fprintf(stderr
, " done\n");
1969 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1970 unlock_remote(ref_lock
);
1972 strvec_clear(&commit_argv
);
1973 release_revisions(&revs
);
1976 /* Update remote server info if appropriate */
1977 if (repo
->has_info_refs
&& new_refs
) {
1978 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1979 fprintf(stderr
, "Updating remote server info\n");
1981 update_remote_info_refs(info_ref_lock
);
1983 fprintf(stderr
, "Unable to update server info\n");
1989 unlock_remote(info_ref_lock
);
1995 request
= request_queue_head
;
1996 while (request
!= NULL
) {
1997 next_request
= request
->next
;
1998 release_request(request
);
1999 request
= next_request
;
2003 free_refs(local_refs
);