1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "repository.h"
14 #include "list-objects.h"
19 #include "tree-walk.h"
22 #include "object-store-ll.h"
23 #include "commit-reach.h"
25 #ifdef EXPAT_NEEDS_XMLPARSE_H
31 static const char http_push_usage
[] =
32 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
39 #define XML_STATUS_OK 1
40 #define XML_STATUS_ERROR 0
43 #define PREV_BUF_SIZE 4096
46 #define DAV_LOCK "LOCK"
47 #define DAV_MKCOL "MKCOL"
48 #define DAV_MOVE "MOVE"
49 #define DAV_PROPFIND "PROPFIND"
51 #define DAV_UNLOCK "UNLOCK"
52 #define DAV_DELETE "DELETE"
55 #define DAV_PROP_LOCKWR (1u << 0)
56 #define DAV_PROP_LOCKEX (1u << 1)
57 #define DAV_LOCK_OK (1u << 2)
59 /* DAV XML properties */
60 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
61 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
62 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
63 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
64 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
65 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
66 #define DAV_PROPFIND_RESP ".multistatus.response"
67 #define DAV_PROPFIND_NAME ".multistatus.response.href"
68 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
70 /* DAV request body templates */
71 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
72 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
73 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
76 #define LOCK_REFRESH 30
78 /* Remember to update object flag allocation in object.h */
79 #define LOCAL (1u<<11)
80 #define REMOTE (1u<<12)
81 #define FETCHING (1u<<13)
82 #define PUSHING (1u<<14)
84 /* We allow "recursive" symbolic refs. Only within reason, though */
89 static signed char remote_dir_exists
[256];
91 static int push_verbosely
;
92 static int push_all
= MATCH_REFS_NONE
;
95 static int helper_status
;
97 static struct object_list
*objects
;
104 int can_update_info_refs
;
106 struct packed_git
*packs
;
107 struct remote_lock
*locks
;
110 static struct repo
*repo
;
112 enum transfer_state
{
124 struct transfer_request
{
126 struct packed_git
*target
;
129 struct remote_lock
*lock
;
130 struct curl_slist
*headers
;
131 struct buffer buffer
;
132 enum transfer_state state
;
133 CURLcode curl_result
;
134 char errorstr
[CURL_ERROR_SIZE
];
137 struct active_request_slot
*slot
;
138 struct transfer_request
*next
;
141 static struct transfer_request
*request_queue_head
;
147 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
155 char tmpfile_suffix
[GIT_MAX_HEXSZ
+ 1];
159 struct remote_lock
*next
;
162 /* Flags that control remote_ls processing */
163 #define PROCESS_FILES (1u << 0)
164 #define PROCESS_DIRS (1u << 1)
165 #define RECURSIVE (1u << 2)
167 /* Flags that remote_ls passes to callback functions */
168 #define IS_DIR (1u << 0)
170 struct remote_ls_ctx
{
172 void (*userFunc
)(struct remote_ls_ctx
*ls
);
177 struct remote_ls_ctx
*parent
;
180 /* get_dav_token_headers options */
181 enum dav_header_flag
{
182 DAV_HEADER_IF
= (1u << 0),
183 DAV_HEADER_LOCK
= (1u << 1),
184 DAV_HEADER_TIMEOUT
= (1u << 2)
187 static char *xml_entities(const char *s
)
189 struct strbuf buf
= STRBUF_INIT
;
190 strbuf_addstr_xml_quoted(&buf
, s
);
191 return strbuf_detach(&buf
, NULL
);
194 static void curl_setup_http_get(CURL
*curl
, const char *url
,
195 const char *custom_req
)
197 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
198 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
199 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
200 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
203 static void curl_setup_http(CURL
*curl
, const char *url
,
204 const char *custom_req
, struct buffer
*buffer
,
205 curl_write_callback write_fn
)
207 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
208 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
209 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
210 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
211 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
212 curl_easy_setopt(curl
, CURLOPT_SEEKFUNCTION
, seek_buffer
);
213 curl_easy_setopt(curl
, CURLOPT_SEEKDATA
, buffer
);
214 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
215 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
216 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
217 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
220 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
222 struct strbuf buf
= STRBUF_INIT
;
223 struct curl_slist
*dav_headers
= http_copy_default_headers();
225 if (options
& DAV_HEADER_IF
) {
226 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
227 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
230 if (options
& DAV_HEADER_LOCK
) {
231 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
232 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
235 if (options
& DAV_HEADER_TIMEOUT
) {
236 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
237 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
240 strbuf_release(&buf
);
245 static void finish_request(struct transfer_request
*request
);
246 static void release_request(struct transfer_request
*request
);
248 static void process_response(void *callback_data
)
250 struct transfer_request
*request
=
251 (struct transfer_request
*)callback_data
;
253 finish_request(request
);
256 static void start_fetch_loose(struct transfer_request
*request
)
258 struct active_request_slot
*slot
;
259 struct http_object_request
*obj_req
;
261 obj_req
= new_http_object_request(repo
->url
, &request
->obj
->oid
);
263 request
->state
= ABORTED
;
267 slot
= obj_req
->slot
;
268 slot
->callback_func
= process_response
;
269 slot
->callback_data
= request
;
270 request
->slot
= slot
;
271 request
->userData
= obj_req
;
273 /* Try to get the request started, abort the request on error */
274 request
->state
= RUN_FETCH_LOOSE
;
275 if (!start_active_slot(slot
)) {
276 fprintf(stderr
, "Unable to start GET request\n");
277 repo
->can_update_info_refs
= 0;
278 release_http_object_request(&obj_req
);
279 release_request(request
);
283 static void start_mkcol(struct transfer_request
*request
)
285 char *hex
= oid_to_hex(&request
->obj
->oid
);
286 struct active_request_slot
*slot
;
288 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
290 slot
= get_active_slot();
291 slot
->callback_func
= process_response
;
292 slot
->callback_data
= request
;
293 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
294 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
296 if (start_active_slot(slot
)) {
297 request
->slot
= slot
;
298 request
->state
= RUN_MKCOL
;
300 request
->state
= ABORTED
;
301 FREE_AND_NULL(request
->url
);
305 static void start_fetch_packed(struct transfer_request
*request
)
307 struct packed_git
*target
;
309 struct transfer_request
*check_request
= request_queue_head
;
310 struct http_pack_request
*preq
;
312 target
= find_oid_pack(&request
->obj
->oid
, repo
->packs
);
314 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request
->obj
->oid
));
315 repo
->can_update_info_refs
= 0;
316 release_request(request
);
319 close_pack_index(target
);
320 request
->target
= target
;
322 fprintf(stderr
, "Fetching pack %s\n",
323 hash_to_hex(target
->hash
));
324 fprintf(stderr
, " which contains %s\n", oid_to_hex(&request
->obj
->oid
));
326 preq
= new_http_pack_request(target
->hash
, repo
->url
);
328 repo
->can_update_info_refs
= 0;
332 /* Make sure there isn't another open request for this pack */
333 while (check_request
) {
334 if (check_request
->state
== RUN_FETCH_PACKED
&&
335 !strcmp(check_request
->url
, preq
->url
)) {
336 release_http_pack_request(preq
);
337 release_request(request
);
340 check_request
= check_request
->next
;
343 preq
->slot
->callback_func
= process_response
;
344 preq
->slot
->callback_data
= request
;
345 request
->slot
= preq
->slot
;
346 request
->userData
= preq
;
348 /* Try to get the request started, abort the request on error */
349 request
->state
= RUN_FETCH_PACKED
;
350 if (!start_active_slot(preq
->slot
)) {
351 fprintf(stderr
, "Unable to start GET request\n");
352 release_http_pack_request(preq
);
353 repo
->can_update_info_refs
= 0;
354 release_request(request
);
358 static void start_put(struct transfer_request
*request
)
360 char *hex
= oid_to_hex(&request
->obj
->oid
);
361 struct active_request_slot
*slot
;
362 struct strbuf buf
= STRBUF_INIT
;
363 enum object_type type
;
371 unpacked
= repo_read_object_file(the_repository
, &request
->obj
->oid
,
373 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, len
);
376 git_deflate_init(&stream
, zlib_compression_level
);
377 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
378 strbuf_grow(&request
->buffer
.buf
, size
);
379 request
->buffer
.posn
= 0;
382 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
383 stream
.avail_out
= size
;
386 stream
.next_in
= (void *)hdr
;
387 stream
.avail_in
= hdrlen
;
388 while (git_deflate(&stream
, 0) == Z_OK
)
391 /* Then the data itself.. */
392 stream
.next_in
= unpacked
;
393 stream
.avail_in
= len
;
394 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
396 git_deflate_end(&stream
);
399 request
->buffer
.buf
.len
= stream
.total_out
;
401 strbuf_addstr(&buf
, "Destination: ");
402 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
403 request
->dest
= strbuf_detach(&buf
, NULL
);
405 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
406 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, the_hash_algo
->hexsz
+ 1);
407 request
->url
= strbuf_detach(&buf
, NULL
);
409 slot
= get_active_slot();
410 slot
->callback_func
= process_response
;
411 slot
->callback_data
= request
;
412 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
413 &request
->buffer
, fwrite_null
);
415 if (start_active_slot(slot
)) {
416 request
->slot
= slot
;
417 request
->state
= RUN_PUT
;
419 request
->state
= ABORTED
;
420 FREE_AND_NULL(request
->url
);
424 static void start_move(struct transfer_request
*request
)
426 struct active_request_slot
*slot
;
427 struct curl_slist
*dav_headers
= http_copy_default_headers();
429 slot
= get_active_slot();
430 slot
->callback_func
= process_response
;
431 slot
->callback_data
= request
;
432 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
433 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
434 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
435 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
437 if (start_active_slot(slot
)) {
438 request
->slot
= slot
;
439 request
->state
= RUN_MOVE
;
440 request
->headers
= dav_headers
;
442 request
->state
= ABORTED
;
443 FREE_AND_NULL(request
->url
);
444 curl_slist_free_all(dav_headers
);
448 static int refresh_lock(struct remote_lock
*lock
)
450 struct active_request_slot
*slot
;
451 struct slot_results results
;
452 struct curl_slist
*dav_headers
;
455 lock
->refreshing
= 1;
457 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
459 slot
= get_active_slot();
460 slot
->results
= &results
;
461 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
462 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
464 if (start_active_slot(slot
)) {
465 run_active_slot(slot
);
466 if (results
.curl_result
!= CURLE_OK
) {
467 fprintf(stderr
, "LOCK HTTP error %ld\n",
470 lock
->start_time
= time(NULL
);
475 lock
->refreshing
= 0;
476 curl_slist_free_all(dav_headers
);
481 static void check_locks(void)
483 struct remote_lock
*lock
= repo
->locks
;
484 time_t current_time
= time(NULL
);
488 time_remaining
= lock
->start_time
+ lock
->timeout
-
490 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
491 if (!refresh_lock(lock
)) {
493 "Unable to refresh lock for %s\n",
503 static void release_request(struct transfer_request
*request
)
505 struct transfer_request
*entry
= request_queue_head
;
507 if (request
== request_queue_head
) {
508 request_queue_head
= request
->next
;
510 while (entry
&& entry
->next
!= request
)
513 entry
->next
= request
->next
;
518 strbuf_release(&request
->buffer
.buf
);
522 static void finish_request(struct transfer_request
*request
)
524 struct http_pack_request
*preq
;
525 struct http_object_request
*obj_req
;
527 request
->curl_result
= request
->slot
->curl_result
;
528 request
->http_code
= request
->slot
->http_code
;
529 request
->slot
= NULL
;
531 /* Keep locks active */
534 if (request
->headers
)
535 curl_slist_free_all(request
->headers
);
537 /* URL is reused for MOVE after PUT and used during FETCH */
538 if (request
->state
!= RUN_PUT
&& request
->state
!= RUN_FETCH_PACKED
) {
539 FREE_AND_NULL(request
->url
);
542 if (request
->state
== RUN_MKCOL
) {
543 if (request
->curl_result
== CURLE_OK
||
544 request
->http_code
== 405) {
545 remote_dir_exists
[request
->obj
->oid
.hash
[0]] = 1;
548 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
549 oid_to_hex(&request
->obj
->oid
),
550 request
->curl_result
, request
->http_code
);
551 request
->state
= ABORTED
;
554 } else if (request
->state
== RUN_PUT
) {
555 if (request
->curl_result
== CURLE_OK
) {
558 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
559 oid_to_hex(&request
->obj
->oid
),
560 request
->curl_result
, request
->http_code
);
561 request
->state
= ABORTED
;
564 } else if (request
->state
== RUN_MOVE
) {
565 if (request
->curl_result
== CURLE_OK
) {
567 fprintf(stderr
, " sent %s\n",
568 oid_to_hex(&request
->obj
->oid
));
569 request
->obj
->flags
|= REMOTE
;
570 release_request(request
);
572 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
573 oid_to_hex(&request
->obj
->oid
),
574 request
->curl_result
, request
->http_code
);
575 request
->state
= ABORTED
;
578 } else if (request
->state
== RUN_FETCH_LOOSE
) {
579 obj_req
= (struct http_object_request
*)request
->userData
;
581 if (finish_http_object_request(obj_req
) == 0)
582 if (obj_req
->rename
== 0)
583 request
->obj
->flags
|= (LOCAL
| REMOTE
);
585 release_http_object_request(&obj_req
);
587 /* Try fetching packed if necessary */
588 if (request
->obj
->flags
& LOCAL
) {
589 release_request(request
);
591 start_fetch_packed(request
);
593 } else if (request
->state
== RUN_FETCH_PACKED
) {
595 if (request
->curl_result
!= CURLE_OK
) {
596 fprintf(stderr
, "Unable to get pack file %s\n%s",
597 request
->url
, curl_errorstr
);
599 preq
= (struct http_pack_request
*)request
->userData
;
602 if (finish_http_pack_request(preq
) == 0)
604 release_http_pack_request(preq
);
608 repo
->can_update_info_refs
= 0;
610 http_install_packfile(request
->target
, &repo
->packs
);
611 release_request(request
);
615 static int is_running_queue
;
616 static int fill_active_slot(void *data UNUSED
)
618 struct transfer_request
*request
;
620 if (aborted
|| !is_running_queue
)
623 for (request
= request_queue_head
; request
; request
= request
->next
) {
624 if (request
->state
== NEED_FETCH
) {
625 start_fetch_loose(request
);
627 } else if (pushing
&& request
->state
== NEED_PUSH
) {
628 if (remote_dir_exists
[request
->obj
->oid
.hash
[0]] == 1) {
631 start_mkcol(request
);
639 static void get_remote_object_list(unsigned char parent
);
641 static void add_fetch_request(struct object
*obj
)
643 struct transfer_request
*request
;
648 * Don't fetch the object if it's known to exist locally
649 * or is already in the request queue
651 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
652 get_remote_object_list(obj
->oid
.hash
[0]);
653 if (obj
->flags
& (LOCAL
| FETCHING
))
656 obj
->flags
|= FETCHING
;
657 CALLOC_ARRAY(request
, 1);
659 request
->state
= NEED_FETCH
;
660 strbuf_init(&request
->buffer
.buf
, 0);
661 request
->next
= request_queue_head
;
662 request_queue_head
= request
;
668 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
670 struct transfer_request
*request
;
671 struct packed_git
*target
;
673 /* Keep locks active */
677 * Don't push the object if it's known to exist on the remote
678 * or is already in the request queue
680 if (remote_dir_exists
[obj
->oid
.hash
[0]] == -1)
681 get_remote_object_list(obj
->oid
.hash
[0]);
682 if (obj
->flags
& (REMOTE
| PUSHING
))
684 target
= find_oid_pack(&obj
->oid
, repo
->packs
);
686 obj
->flags
|= REMOTE
;
690 obj
->flags
|= PUSHING
;
691 CALLOC_ARRAY(request
, 1);
693 request
->lock
= lock
;
694 request
->state
= NEED_PUSH
;
695 strbuf_init(&request
->buffer
.buf
, 0);
696 request
->next
= request_queue_head
;
697 request_queue_head
= request
;
705 static int fetch_indices(void)
710 fprintf(stderr
, "Getting pack list\n");
712 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
714 case HTTP_MISSING_TARGET
:
724 static void one_remote_object(const struct object_id
*oid
)
728 obj
= lookup_object(the_repository
, oid
);
730 obj
= parse_object(the_repository
, oid
);
732 /* Ignore remote objects that don't exist locally */
736 obj
->flags
|= REMOTE
;
737 if (!object_list_contains(objects
, obj
))
738 object_list_insert(obj
, &objects
);
741 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
743 int *lock_flags
= (int *)ctx
->userData
;
746 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
747 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
748 (*lock_flags
& DAV_PROP_LOCKWR
)) {
749 *lock_flags
|= DAV_LOCK_OK
;
751 *lock_flags
&= DAV_LOCK_OK
;
752 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
753 *lock_flags
|= DAV_PROP_LOCKWR
;
754 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
755 *lock_flags
|= DAV_PROP_LOCKEX
;
760 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
762 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
763 git_hash_ctx hash_ctx
;
764 unsigned char lock_token_hash
[GIT_MAX_RAWSZ
];
766 if (tag_closed
&& ctx
->cdata
) {
767 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
768 lock
->owner
= xstrdup(ctx
->cdata
);
769 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
771 if (skip_prefix(ctx
->cdata
, "Second-", &arg
))
772 lock
->timeout
= strtol(arg
, NULL
, 10);
773 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
774 lock
->token
= xstrdup(ctx
->cdata
);
776 the_hash_algo
->init_fn(&hash_ctx
);
777 the_hash_algo
->update_fn(&hash_ctx
, lock
->token
, strlen(lock
->token
));
778 the_hash_algo
->final_fn(lock_token_hash
, &hash_ctx
);
780 lock
->tmpfile_suffix
[0] = '_';
781 memcpy(lock
->tmpfile_suffix
+ 1, hash_to_hex(lock_token_hash
), the_hash_algo
->hexsz
);
786 static void one_remote_ref(const char *refname
);
789 xml_start_tag(void *userData
, const char *name
, const char **atts UNUSED
)
791 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
792 const char *c
= strchr(name
, ':');
793 int old_namelen
, new_len
;
800 old_namelen
= strlen(ctx
->name
);
801 new_len
= old_namelen
+ strlen(c
) + 2;
803 if (new_len
> ctx
->len
) {
804 ctx
->name
= xrealloc(ctx
->name
, new_len
);
807 xsnprintf(ctx
->name
+ old_namelen
, ctx
->len
- old_namelen
, ".%s", c
);
809 FREE_AND_NULL(ctx
->cdata
);
811 ctx
->userFunc(ctx
, 0);
815 xml_end_tag(void *userData
, const char *name
)
817 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
818 const char *c
= strchr(name
, ':');
821 ctx
->userFunc(ctx
, 1);
828 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
833 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
835 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
837 ctx
->cdata
= xmemdupz(s
, len
);
840 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
842 struct active_request_slot
*slot
;
843 struct slot_results results
;
844 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
845 struct strbuf in_buffer
= STRBUF_INIT
;
848 char timeout_header
[25];
849 struct remote_lock
*lock
= NULL
;
850 struct curl_slist
*dav_headers
= http_copy_default_headers();
854 url
= xstrfmt("%s%s", repo
->url
, path
);
856 /* Make sure leading directories exist for the remote ref */
857 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
859 char saved_character
= ep
[1];
861 slot
= get_active_slot();
862 slot
->results
= &results
;
863 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
864 if (start_active_slot(slot
)) {
865 run_active_slot(slot
);
866 if (results
.curl_result
!= CURLE_OK
&&
867 results
.http_code
!= 405) {
869 "Unable to create branch path %s\n",
875 fprintf(stderr
, "Unable to start MKCOL request\n");
879 ep
[1] = saved_character
;
880 ep
= strchr(ep
+ 1, '/');
883 escaped
= xml_entities(ident_default_email());
884 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
887 xsnprintf(timeout_header
, sizeof(timeout_header
), "Timeout: Second-%ld", timeout
);
888 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
889 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
891 slot
= get_active_slot();
892 slot
->results
= &results
;
893 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
894 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
895 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
897 CALLOC_ARRAY(lock
, 1);
900 if (start_active_slot(slot
)) {
901 run_active_slot(slot
);
902 if (results
.curl_result
== CURLE_OK
) {
903 XML_Parser parser
= XML_ParserCreate(NULL
);
904 enum XML_Status result
;
905 ctx
.name
= xcalloc(10, 1);
908 ctx
.userFunc
= handle_new_lock_ctx
;
910 XML_SetUserData(parser
, &ctx
);
911 XML_SetElementHandler(parser
, xml_start_tag
,
913 XML_SetCharacterDataHandler(parser
, xml_cdata
);
914 result
= XML_Parse(parser
, in_buffer
.buf
,
918 if (result
!= XML_STATUS_OK
) {
919 fprintf(stderr
, "XML error: %s\n",
921 XML_GetErrorCode(parser
)));
924 XML_ParserFree(parser
);
927 "error: curl result=%d, HTTP code=%ld\n",
928 results
.curl_result
, results
.http_code
);
931 fprintf(stderr
, "Unable to start LOCK request\n");
934 curl_slist_free_all(dav_headers
);
935 strbuf_release(&out_buffer
.buf
);
936 strbuf_release(&in_buffer
);
938 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
945 lock
->start_time
= time(NULL
);
946 lock
->next
= repo
->locks
;
953 static int unlock_remote(struct remote_lock
*lock
)
955 struct active_request_slot
*slot
;
956 struct slot_results results
;
957 struct remote_lock
*prev
= repo
->locks
;
958 struct curl_slist
*dav_headers
;
961 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
963 slot
= get_active_slot();
964 slot
->results
= &results
;
965 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
966 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
968 if (start_active_slot(slot
)) {
969 run_active_slot(slot
);
970 if (results
.curl_result
== CURLE_OK
)
973 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
976 fprintf(stderr
, "Unable to start UNLOCK request\n");
979 curl_slist_free_all(dav_headers
);
981 if (repo
->locks
== lock
) {
982 repo
->locks
= lock
->next
;
984 while (prev
&& prev
->next
!= lock
)
987 prev
->next
= lock
->next
;
998 static void remove_locks(void)
1000 struct remote_lock
*lock
= repo
->locks
;
1002 fprintf(stderr
, "Removing remote locks...\n");
1004 struct remote_lock
*next
= lock
->next
;
1005 unlock_remote(lock
);
1010 static void remove_locks_on_signal(int signo
)
1013 sigchain_pop(signo
);
1017 static void remote_ls(const char *path
, int flags
,
1018 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1021 /* extract hex from sharded "xx/x{38}" filename */
1022 static int get_oid_hex_from_objpath(const char *path
, struct object_id
*oid
)
1024 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
1025 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
1027 if (strlen(path
) != the_hash_algo
->hexsz
+ 1)
1030 if (hex_to_bytes(oid
->hash
, path
, 1))
1033 path
++; /* skip '/' */
1035 return hex_to_bytes(oid
->hash
+ 1, path
, the_hash_algo
->rawsz
- 1);
1038 static void process_ls_object(struct remote_ls_ctx
*ls
)
1040 unsigned int *parent
= (unsigned int *)ls
->userData
;
1041 const char *path
= ls
->dentry_name
;
1042 struct object_id oid
;
1044 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1045 remote_dir_exists
[*parent
] = 1;
1049 if (!skip_prefix(path
, "objects/", &path
) ||
1050 get_oid_hex_from_objpath(path
, &oid
))
1053 one_remote_object(&oid
);
1056 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1058 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1059 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1063 if (!(ls
->dentry_flags
& IS_DIR
))
1064 one_remote_ref(ls
->dentry_name
);
1067 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1069 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1072 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1073 if (ls
->dentry_flags
& IS_DIR
) {
1075 /* ensure collection names end with slash */
1076 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1078 if (ls
->flags
& PROCESS_DIRS
) {
1081 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1082 ls
->flags
& RECURSIVE
) {
1083 remote_ls(ls
->dentry_name
,
1088 } else if (ls
->flags
& PROCESS_FILES
) {
1091 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1092 char *path
= ctx
->cdata
;
1093 if (*ctx
->cdata
== 'h') {
1094 path
= strstr(path
, "//");
1096 path
= strchr(path
+2, '/');
1100 const char *url
= repo
->url
;
1103 if (strncmp(path
, url
, repo
->path_len
))
1104 error("Parsed path '%s' does not match url: '%s'",
1107 path
+= repo
->path_len
;
1108 ls
->dentry_name
= xstrdup(path
);
1111 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1112 ls
->dentry_flags
|= IS_DIR
;
1114 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1115 FREE_AND_NULL(ls
->dentry_name
);
1116 ls
->dentry_flags
= 0;
1121 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1122 * should _only_ heed the information from that file, instead of trying to
1123 * determine the refs from the remote file system (badly: it does not even
1124 * know about packed-refs).
1126 static void remote_ls(const char *path
, int flags
,
1127 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1130 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1131 struct active_request_slot
*slot
;
1132 struct slot_results results
;
1133 struct strbuf in_buffer
= STRBUF_INIT
;
1134 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1135 struct curl_slist
*dav_headers
= http_copy_default_headers();
1137 struct remote_ls_ctx ls
;
1140 ls
.path
= xstrdup(path
);
1141 ls
.dentry_name
= NULL
;
1142 ls
.dentry_flags
= 0;
1143 ls
.userData
= userData
;
1144 ls
.userFunc
= userFunc
;
1146 strbuf_addstr(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1148 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1149 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1151 slot
= get_active_slot();
1152 slot
->results
= &results
;
1153 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1154 &out_buffer
, fwrite_buffer
);
1155 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1156 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1158 if (start_active_slot(slot
)) {
1159 run_active_slot(slot
);
1160 if (results
.curl_result
== CURLE_OK
) {
1161 XML_Parser parser
= XML_ParserCreate(NULL
);
1162 enum XML_Status result
;
1163 ctx
.name
= xcalloc(10, 1);
1166 ctx
.userFunc
= handle_remote_ls_ctx
;
1168 XML_SetUserData(parser
, &ctx
);
1169 XML_SetElementHandler(parser
, xml_start_tag
,
1171 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1172 result
= XML_Parse(parser
, in_buffer
.buf
,
1177 if (result
!= XML_STATUS_OK
) {
1178 fprintf(stderr
, "XML error: %s\n",
1180 XML_GetErrorCode(parser
)));
1182 XML_ParserFree(parser
);
1185 fprintf(stderr
, "Unable to start PROPFIND request\n");
1189 free(ls
.dentry_name
);
1191 strbuf_release(&out_buffer
.buf
);
1192 strbuf_release(&in_buffer
);
1193 curl_slist_free_all(dav_headers
);
1196 static void get_remote_object_list(unsigned char parent
)
1198 char path
[] = "objects/XX/";
1199 static const char hex
[] = "0123456789abcdef";
1200 unsigned int val
= parent
;
1202 path
[8] = hex
[val
>> 4];
1203 path
[9] = hex
[val
& 0xf];
1204 remote_dir_exists
[val
] = 0;
1205 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1206 process_ls_object
, &val
);
1209 static int locking_available(void)
1211 struct active_request_slot
*slot
;
1212 struct slot_results results
;
1213 struct strbuf in_buffer
= STRBUF_INIT
;
1214 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1215 struct curl_slist
*dav_headers
= http_copy_default_headers();
1220 escaped
= xml_entities(repo
->url
);
1221 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1224 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1225 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1227 slot
= get_active_slot();
1228 slot
->results
= &results
;
1229 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1230 &out_buffer
, fwrite_buffer
);
1231 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1232 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &in_buffer
);
1234 if (start_active_slot(slot
)) {
1235 run_active_slot(slot
);
1236 if (results
.curl_result
== CURLE_OK
) {
1237 XML_Parser parser
= XML_ParserCreate(NULL
);
1238 enum XML_Status result
;
1239 ctx
.name
= xcalloc(10, 1);
1242 ctx
.userFunc
= handle_lockprop_ctx
;
1243 ctx
.userData
= &lock_flags
;
1244 XML_SetUserData(parser
, &ctx
);
1245 XML_SetElementHandler(parser
, xml_start_tag
,
1247 result
= XML_Parse(parser
, in_buffer
.buf
,
1251 if (result
!= XML_STATUS_OK
) {
1252 fprintf(stderr
, "XML error: %s\n",
1254 XML_GetErrorCode(parser
)));
1257 XML_ParserFree(parser
);
1259 error("no DAV locking support on %s",
1263 error("Cannot access URL %s, return code %d",
1264 repo
->url
, results
.curl_result
);
1268 error("Unable to start PROPFIND request on %s", repo
->url
);
1271 strbuf_release(&out_buffer
.buf
);
1272 strbuf_release(&in_buffer
);
1273 curl_slist_free_all(dav_headers
);
1278 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1280 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1284 return &entry
->next
;
1287 static struct object_list
**process_blob(struct blob
*blob
,
1288 struct object_list
**p
)
1290 struct object
*obj
= &blob
->object
;
1292 obj
->flags
|= LOCAL
;
1294 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1298 return add_one_object(obj
, p
);
1301 static struct object_list
**process_tree(struct tree
*tree
,
1302 struct object_list
**p
)
1304 struct object
*obj
= &tree
->object
;
1305 struct tree_desc desc
;
1306 struct name_entry entry
;
1308 obj
->flags
|= LOCAL
;
1310 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1312 if (parse_tree(tree
) < 0)
1313 die("bad tree object %s", oid_to_hex(&obj
->oid
));
1316 p
= add_one_object(obj
, p
);
1318 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
1320 while (tree_entry(&desc
, &entry
))
1321 switch (object_type(entry
.mode
)) {
1323 p
= process_tree(lookup_tree(the_repository
, &entry
.oid
),
1327 p
= process_blob(lookup_blob(the_repository
, &entry
.oid
),
1331 /* Subproject commit - not in this repository */
1335 free_tree_buffer(tree
);
1339 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 (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
&& !repo_has_object_file(the_repository
, &ref
->old_oid
)) {
1450 obj
= lookup_unknown_object(the_repository
, &ref
->old_oid
);
1451 fprintf(stderr
, " fetch %s for %s\n",
1452 oid_to_hex(&ref
->old_oid
), refname
);
1453 add_fetch_request(obj
);
1456 ref
->next
= remote_refs
;
1460 static void get_dav_remote_heads(void)
1462 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1465 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1467 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1471 ref
= alloc_ref(ls
->dentry_name
);
1473 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1475 "Unable to fetch ref %s from %s\n",
1476 ls
->dentry_name
, repo
->url
);
1482 o
= parse_object(the_repository
, &ref
->old_oid
);
1485 "Unable to parse object %s for remote ref %s\n",
1486 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1492 strbuf_addf(buf
, "%s\t%s\n",
1493 oid_to_hex(&ref
->old_oid
), ls
->dentry_name
);
1495 if (o
->type
== OBJ_TAG
) {
1496 o
= deref_tag(the_repository
, o
, ls
->dentry_name
, 0);
1498 strbuf_addf(buf
, "%s\t%s^{}\n",
1499 oid_to_hex(&o
->oid
), ls
->dentry_name
);
1504 static void update_remote_info_refs(struct remote_lock
*lock
)
1506 struct buffer buffer
= { STRBUF_INIT
, 0 };
1507 struct active_request_slot
*slot
;
1508 struct slot_results results
;
1509 struct curl_slist
*dav_headers
;
1511 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1512 add_remote_info_ref
, &buffer
.buf
);
1514 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1516 slot
= get_active_slot();
1517 slot
->results
= &results
;
1518 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1519 &buffer
, fwrite_null
);
1520 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1522 if (start_active_slot(slot
)) {
1523 run_active_slot(slot
);
1524 if (results
.curl_result
!= CURLE_OK
) {
1526 "PUT error: curl result=%d, HTTP code=%ld\n",
1527 results
.curl_result
, results
.http_code
);
1530 curl_slist_free_all(dav_headers
);
1532 strbuf_release(&buffer
.buf
);
1535 static int remote_exists(const char *path
)
1537 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1541 switch (http_get_strbuf(url
, NULL
, NULL
)) {
1545 case HTTP_MISSING_TARGET
:
1549 error("unable to access '%s': %s", url
, curl_errorstr
);
1558 static void fetch_symref(const char *path
, char **symref
, struct object_id
*oid
)
1560 char *url
= xstrfmt("%s%s", repo
->url
, path
);
1561 struct strbuf buffer
= STRBUF_INIT
;
1564 if (http_get_strbuf(url
, &buffer
, NULL
) != HTTP_OK
)
1565 die("Couldn't get %s for remote symref\n%s", url
,
1569 FREE_AND_NULL(*symref
);
1570 oidclr(oid
, the_repository
->hash_algo
);
1572 if (buffer
.len
== 0)
1575 /* Cut off trailing newline. */
1576 strbuf_rtrim(&buffer
);
1578 /* If it's a symref, set the refname; otherwise try for a sha1 */
1579 if (skip_prefix(buffer
.buf
, "ref: ", &name
)) {
1580 *symref
= xmemdupz(name
, buffer
.len
- (name
- buffer
.buf
));
1582 get_oid_hex(buffer
.buf
, oid
);
1585 strbuf_release(&buffer
);
1588 static int verify_merge_base(struct object_id
*head_oid
, struct ref
*remote
)
1590 struct commit
*head
= lookup_commit_or_die(head_oid
, "HEAD");
1591 struct commit
*branch
= lookup_commit_or_die(&remote
->old_oid
,
1593 int ret
= repo_in_merge_bases(the_repository
, branch
, head
);
1600 static int delete_remote_branch(const char *pattern
, int force
)
1602 struct ref
*refs
= remote_refs
;
1603 struct ref
*remote_ref
= NULL
;
1604 struct object_id head_oid
;
1605 char *symref
= NULL
;
1607 int patlen
= strlen(pattern
);
1609 struct active_request_slot
*slot
;
1610 struct slot_results results
;
1613 /* Find the remote branch(es) matching the specified branch name */
1614 for (match
= 0; refs
; refs
= refs
->next
) {
1615 char *name
= refs
->name
;
1616 int namelen
= strlen(name
);
1617 if (namelen
< patlen
||
1618 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1620 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1626 return error("No remote branch matches %s", pattern
);
1628 return error("More than one remote branch matches %s",
1632 * Remote HEAD must be a symref (not exactly foolproof; a remote
1633 * symlink to a symref will look like a symref)
1635 fetch_symref("HEAD", &symref
, &head_oid
);
1637 return error("Remote HEAD is not a symref");
1639 /* Remote branch must not be the remote HEAD */
1640 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1641 if (!strcmp(remote_ref
->name
, symref
))
1642 return error("Remote branch %s is the current HEAD",
1644 fetch_symref(symref
, &symref
, &head_oid
);
1647 /* Run extra sanity checks if delete is not forced */
1649 /* Remote HEAD must resolve to a known object */
1651 return error("Remote HEAD symrefs too deep");
1652 if (is_null_oid(&head_oid
))
1653 return error("Unable to resolve remote HEAD");
1654 if (!repo_has_object_file(the_repository
, &head_oid
))
1655 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid
));
1657 /* Remote branch must resolve to a known object */
1658 if (is_null_oid(&remote_ref
->old_oid
))
1659 return error("Unable to resolve remote branch %s",
1661 if (!repo_has_object_file(the_repository
, &remote_ref
->old_oid
))
1662 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
));
1664 /* Remote branch must be an ancestor of remote HEAD */
1665 if (!verify_merge_base(&head_oid
, remote_ref
)) {
1666 return error("The branch '%s' is not an ancestor "
1667 "of your current HEAD.\n"
1668 "If you are sure you want to delete it,"
1669 " run:\n\t'git http-push -D %s %s'",
1670 remote_ref
->name
, repo
->url
, pattern
);
1674 /* Send delete request */
1675 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1678 url
= xstrfmt("%s%s", repo
->url
, remote_ref
->name
);
1679 slot
= get_active_slot();
1680 slot
->results
= &results
;
1681 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1682 if (start_active_slot(slot
)) {
1683 run_active_slot(slot
);
1685 if (results
.curl_result
!= CURLE_OK
)
1686 return error("DELETE request failed (%d/%ld)",
1687 results
.curl_result
, results
.http_code
);
1690 return error("Unable to start DELETE request");
1696 static void run_request_queue(void)
1698 is_running_queue
= 1;
1699 fill_active_slots();
1700 add_fill_function(NULL
, fill_active_slot
);
1702 finish_all_active_slots();
1703 fill_active_slots();
1704 } while (request_queue_head
&& !aborted
);
1706 is_running_queue
= 0;
1709 int cmd_main(int argc
, const char **argv
)
1711 struct transfer_request
*request
;
1712 struct transfer_request
*next_request
;
1713 struct refspec rs
= REFSPEC_INIT_PUSH
;
1714 struct remote_lock
*ref_lock
= NULL
;
1715 struct remote_lock
*info_ref_lock
= NULL
;
1716 int delete_branch
= 0;
1717 int force_delete
= 0;
1718 int objects_to_send
;
1722 struct ref
*ref
, *local_refs
= NULL
;
1724 CALLOC_ARRAY(repo
, 1);
1727 for (i
= 1; i
< argc
; i
++, argv
++) {
1728 const char *arg
= *argv
;
1731 if (!strcmp(arg
, "--all")) {
1732 push_all
= MATCH_REFS_ALL
;
1735 if (!strcmp(arg
, "--force")) {
1739 if (!strcmp(arg
, "--dry-run")) {
1743 if (!strcmp(arg
, "--helper-status")) {
1747 if (!strcmp(arg
, "--verbose")) {
1749 http_is_verbose
= 1;
1752 if (!strcmp(arg
, "-d")) {
1756 if (!strcmp(arg
, "-D")) {
1761 if (!strcmp(arg
, "-h"))
1762 usage(http_push_usage
);
1765 char *path
= strstr(arg
, "//");
1766 str_end_url_with_slash(arg
, &repo
->url
);
1767 repo
->path_len
= strlen(repo
->url
);
1769 repo
->path
= strchr(path
+2, '/');
1771 repo
->path_len
= strlen(repo
->path
);
1775 refspec_appendn(&rs
, argv
, argc
- i
);
1780 usage(http_push_usage
);
1782 if (delete_branch
&& rs
.nr
!= 1)
1783 die("You must specify only one branch name when deleting a remote branch");
1785 setup_git_directory();
1787 memset(remote_dir_exists
, -1, 256);
1789 http_init(NULL
, repo
->url
, 1);
1791 is_running_queue
= 0;
1793 /* Verify DAV compliance/lock support */
1794 if (!locking_available()) {
1799 sigchain_push_common(remove_locks_on_signal
);
1801 /* Check whether the remote has server info files */
1802 repo
->can_update_info_refs
= 0;
1803 repo
->has_info_refs
= remote_exists("info/refs");
1804 repo
->has_info_packs
= remote_exists("objects/info/packs");
1805 if (repo
->has_info_refs
) {
1806 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1808 repo
->can_update_info_refs
= 1;
1810 error("cannot lock existing info/refs");
1815 if (repo
->has_info_packs
)
1818 /* Get a list of all local and remote heads to validate refspecs */
1819 local_refs
= get_local_heads();
1820 fprintf(stderr
, "Fetching remote heads...\n");
1821 get_dav_remote_heads();
1822 run_request_queue();
1824 /* Remove a remote branch if -d or -D was specified */
1825 if (delete_branch
) {
1826 const char *branch
= rs
.items
[i
].src
;
1827 if (delete_remote_branch(branch
, force_delete
) == -1) {
1828 fprintf(stderr
, "Unable to delete remote branch %s\n",
1831 printf("error %s cannot remove\n", branch
);
1837 if (match_push_refs(local_refs
, &remote_refs
, &rs
, push_all
)) {
1842 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1844 printf("error null no match\n");
1850 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1851 struct rev_info revs
;
1852 struct strvec commit_argv
= STRVEC_INIT
;
1857 if (is_null_oid(&ref
->peer_ref
->new_oid
)) {
1858 if (delete_remote_branch(ref
->name
, 1) == -1) {
1859 error("Could not remove %s", ref
->name
);
1861 printf("error %s cannot remove\n", ref
->name
);
1864 else if (helper_status
)
1865 printf("ok %s\n", ref
->name
);
1870 if (oideq(&ref
->old_oid
, &ref
->peer_ref
->new_oid
)) {
1872 /* stable plumbing output; do not modify or localize */
1873 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1875 printf("ok %s up to date\n", ref
->name
);
1880 !is_null_oid(&ref
->old_oid
) &&
1882 if (!repo_has_object_file(the_repository
, &ref
->old_oid
) ||
1883 !ref_newer(&ref
->peer_ref
->new_oid
,
1886 * We do not have the remote ref, or
1887 * we know that the remote ref is not
1888 * an ancestor of what we are trying to
1889 * push. Either way this can be losing
1890 * commits at the remote end and likely
1891 * we were not up to date to begin with.
1893 /* stable plumbing output; do not modify or localize */
1894 error("remote '%s' is not an ancestor of\n"
1896 "Maybe you are not up-to-date and "
1897 "need to pull first?",
1899 ref
->peer_ref
->name
);
1901 printf("error %s non-fast forward\n", ref
->name
);
1906 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1909 fprintf(stderr
, "updating '%s'", ref
->name
);
1910 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1911 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1912 fprintf(stderr
, "\n from %s\n to %s\n",
1913 oid_to_hex(&ref
->old_oid
), oid_to_hex(&ref
->new_oid
));
1916 printf("ok %s\n", ref
->name
);
1920 /* Lock remote branch ref */
1921 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1923 fprintf(stderr
, "Unable to lock remote branch %s\n",
1926 printf("error %s lock error\n", ref
->name
);
1931 /* Set up revision info for this refspec */
1932 strvec_push(&commit_argv
, ""); /* ignored */
1933 strvec_push(&commit_argv
, "--objects");
1934 strvec_push(&commit_argv
, oid_to_hex(&ref
->new_oid
));
1935 if (!push_all
&& !is_null_oid(&ref
->old_oid
))
1936 strvec_pushf(&commit_argv
, "^%s",
1937 oid_to_hex(&ref
->old_oid
));
1938 repo_init_revisions(the_repository
, &revs
, setup_git_directory());
1939 setup_revisions(commit_argv
.nr
, commit_argv
.v
, &revs
, NULL
);
1940 revs
.edge_hint
= 0; /* just in case */
1942 /* Generate a list of objects that need to be pushed */
1944 if (prepare_revision_walk(&revs
))
1945 die("revision walk setup failed");
1946 mark_edges_uninteresting(&revs
, NULL
, 0);
1947 objects_to_send
= get_delta(&revs
, ref_lock
);
1948 finish_all_active_slots();
1950 /* Push missing objects to remote, this would be a
1951 convenient time to pack them first if appropriate. */
1953 if (objects_to_send
)
1954 fprintf(stderr
, " sending %d objects\n",
1957 run_request_queue();
1959 /* Update the remote branch if all went well */
1960 if (aborted
|| !update_remote(&ref
->new_oid
, ref_lock
))
1964 fprintf(stderr
, " done\n");
1966 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
1967 unlock_remote(ref_lock
);
1969 strvec_clear(&commit_argv
);
1970 release_revisions(&revs
);
1973 /* Update remote server info if appropriate */
1974 if (repo
->has_info_refs
&& new_refs
) {
1975 if (info_ref_lock
&& repo
->can_update_info_refs
) {
1976 fprintf(stderr
, "Updating remote server info\n");
1978 update_remote_info_refs(info_ref_lock
);
1980 fprintf(stderr
, "Unable to update server info\n");
1986 unlock_remote(info_ref_lock
);
1992 request
= request_queue_head
;
1993 while (request
!= NULL
) {
1994 next_request
= request
->next
;
1995 release_request(request
);
1996 request
= next_request
;
2000 free_refs(local_refs
);