11 #include "list-objects.h"
16 static const char http_push_usage
[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
28 #define PREV_BUF_SIZE 4096
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
40 #define DAV_PROP_LOCKWR (1u << 0)
41 #define DAV_PROP_LOCKEX (1u << 1)
42 #define DAV_LOCK_OK (1u << 2)
44 /* DAV XML properties */
45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
51 #define DAV_PROPFIND_RESP ".multistatus.response"
52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
55 /* DAV request body templates */
56 #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>"
57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
58 #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>"
61 #define LOCK_REFRESH 30
63 /* bits #0-15 in revision.h */
65 #define LOCAL (1u<<16)
66 #define REMOTE (1u<<17)
67 #define FETCHING (1u<<18)
68 #define PUSHING (1u<<19)
70 /* We allow "recursive" symbolic refs. Only within reason, though */
75 static signed char remote_dir_exists
[256];
77 static int push_verbosely
;
78 static int push_all
= MATCH_REFS_NONE
;
81 static int helper_status
;
83 static struct object_list
*objects
;
91 int can_update_info_refs
;
93 struct packed_git
*packs
;
94 struct remote_lock
*locks
;
97 static struct repo
*repo
;
111 struct transfer_request
116 struct remote_lock
*lock
;
117 struct curl_slist
*headers
;
118 struct buffer buffer
;
119 enum transfer_state state
;
120 CURLcode curl_result
;
121 char errorstr
[CURL_ERROR_SIZE
];
124 struct active_request_slot
*slot
;
125 struct transfer_request
*next
;
128 static struct transfer_request
*request_queue_head
;
135 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
144 char tmpfile_suffix
[41];
148 struct remote_lock
*next
;
151 /* Flags that control remote_ls processing */
152 #define PROCESS_FILES (1u << 0)
153 #define PROCESS_DIRS (1u << 1)
154 #define RECURSIVE (1u << 2)
156 /* Flags that remote_ls passes to callback functions */
157 #define IS_DIR (1u << 0)
162 void (*userFunc
)(struct remote_ls_ctx
*ls
);
167 struct remote_ls_ctx
*parent
;
170 /* get_dav_token_headers options */
171 enum dav_header_flag
{
172 DAV_HEADER_IF
= (1u << 0),
173 DAV_HEADER_LOCK
= (1u << 1),
174 DAV_HEADER_TIMEOUT
= (1u << 2)
177 static char *xml_entities(char *s
)
179 struct strbuf buf
= STRBUF_INIT
;
181 size_t len
= strcspn(s
, "\"<>&");
182 strbuf_add(&buf
, s
, len
);
186 strbuf_addstr(&buf
, """);
189 strbuf_addstr(&buf
, "<");
192 strbuf_addstr(&buf
, ">");
195 strbuf_addstr(&buf
, "&");
198 return strbuf_detach(&buf
, NULL
);
202 return strbuf_detach(&buf
, NULL
);
205 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
207 struct strbuf buf
= STRBUF_INIT
;
208 struct curl_slist
*dav_headers
= NULL
;
210 if (options
& DAV_HEADER_IF
) {
211 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
212 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
215 if (options
& DAV_HEADER_LOCK
) {
216 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
217 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
220 if (options
& DAV_HEADER_TIMEOUT
) {
221 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
222 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
225 strbuf_release(&buf
);
230 static void finish_request(struct transfer_request
*request
);
231 static void release_request(struct transfer_request
*request
);
233 static void process_response(void *callback_data
)
235 struct transfer_request
*request
=
236 (struct transfer_request
*)callback_data
;
238 finish_request(request
);
241 #ifdef USE_CURL_MULTI
243 static void start_fetch_loose(struct transfer_request
*request
)
245 struct active_request_slot
*slot
;
246 struct http_object_request
*obj_req
;
248 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
249 if (obj_req
== NULL
) {
250 request
->state
= ABORTED
;
254 slot
= obj_req
->slot
;
255 slot
->callback_func
= process_response
;
256 slot
->callback_data
= request
;
257 request
->slot
= slot
;
258 request
->userData
= obj_req
;
260 /* Try to get the request started, abort the request on error */
261 request
->state
= RUN_FETCH_LOOSE
;
262 if (!start_active_slot(slot
)) {
263 fprintf(stderr
, "Unable to start GET request\n");
264 repo
->can_update_info_refs
= 0;
265 release_http_object_request(obj_req
);
266 release_request(request
);
270 static void start_mkcol(struct transfer_request
*request
)
272 char *hex
= sha1_to_hex(request
->obj
->sha1
);
273 struct active_request_slot
*slot
;
275 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
277 slot
= get_active_slot();
278 slot
->callback_func
= process_response
;
279 slot
->callback_data
= request
;
280 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
281 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
282 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
283 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
284 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
286 if (start_active_slot(slot
)) {
287 request
->slot
= slot
;
288 request
->state
= RUN_MKCOL
;
290 request
->state
= ABORTED
;
297 static void start_fetch_packed(struct transfer_request
*request
)
299 struct packed_git
*target
;
301 struct transfer_request
*check_request
= request_queue_head
;
302 struct http_pack_request
*preq
;
304 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
306 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
307 repo
->can_update_info_refs
= 0;
308 release_request(request
);
312 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
313 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
315 preq
= new_http_pack_request(target
, repo
->url
);
317 release_http_pack_request(preq
);
318 repo
->can_update_info_refs
= 0;
321 preq
->lst
= &repo
->packs
;
323 /* Make sure there isn't another open request for this pack */
324 while (check_request
) {
325 if (check_request
->state
== RUN_FETCH_PACKED
&&
326 !strcmp(check_request
->url
, preq
->url
)) {
327 release_http_pack_request(preq
);
328 release_request(request
);
331 check_request
= check_request
->next
;
334 preq
->slot
->callback_func
= process_response
;
335 preq
->slot
->callback_data
= request
;
336 request
->slot
= preq
->slot
;
337 request
->userData
= preq
;
339 /* Try to get the request started, abort the request on error */
340 request
->state
= RUN_FETCH_PACKED
;
341 if (!start_active_slot(preq
->slot
)) {
342 fprintf(stderr
, "Unable to start GET request\n");
343 release_http_pack_request(preq
);
344 repo
->can_update_info_refs
= 0;
345 release_request(request
);
349 static void start_put(struct transfer_request
*request
)
351 char *hex
= sha1_to_hex(request
->obj
->sha1
);
352 struct active_request_slot
*slot
;
353 struct strbuf buf
= STRBUF_INIT
;
354 enum object_type type
;
362 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
363 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
366 memset(&stream
, 0, sizeof(stream
));
367 deflateInit(&stream
, zlib_compression_level
);
368 size
= deflateBound(&stream
, len
+ hdrlen
);
369 strbuf_init(&request
->buffer
.buf
, size
);
370 request
->buffer
.posn
= 0;
373 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
374 stream
.avail_out
= size
;
377 stream
.next_in
= (void *)hdr
;
378 stream
.avail_in
= hdrlen
;
379 while (deflate(&stream
, 0) == Z_OK
)
382 /* Then the data itself.. */
383 stream
.next_in
= unpacked
;
384 stream
.avail_in
= len
;
385 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
390 request
->buffer
.buf
.len
= stream
.total_out
;
392 strbuf_addstr(&buf
, "Destination: ");
393 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
394 request
->dest
= strbuf_detach(&buf
, NULL
);
396 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
397 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
398 request
->url
= strbuf_detach(&buf
, NULL
);
400 slot
= get_active_slot();
401 slot
->callback_func
= process_response
;
402 slot
->callback_data
= request
;
403 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
404 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
405 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
406 #ifndef NO_CURL_IOCTL
407 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
408 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &request
->buffer
);
410 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
411 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
412 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
413 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
414 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
415 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
417 if (start_active_slot(slot
)) {
418 request
->slot
= slot
;
419 request
->state
= RUN_PUT
;
421 request
->state
= ABORTED
;
427 static void start_move(struct transfer_request
*request
)
429 struct active_request_slot
*slot
;
430 struct curl_slist
*dav_headers
= NULL
;
432 slot
= get_active_slot();
433 slot
->callback_func
= process_response
;
434 slot
->callback_data
= request
;
435 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
436 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
437 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
438 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
439 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
440 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
441 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
443 if (start_active_slot(slot
)) {
444 request
->slot
= slot
;
445 request
->state
= RUN_MOVE
;
447 request
->state
= ABORTED
;
453 static int refresh_lock(struct remote_lock
*lock
)
455 struct active_request_slot
*slot
;
456 struct slot_results results
;
457 struct curl_slist
*dav_headers
;
460 lock
->refreshing
= 1;
462 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
464 slot
= get_active_slot();
465 slot
->results
= &results
;
466 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
467 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
468 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
469 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
470 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
472 if (start_active_slot(slot
)) {
473 run_active_slot(slot
);
474 if (results
.curl_result
!= CURLE_OK
) {
475 fprintf(stderr
, "LOCK HTTP error %ld\n",
478 lock
->start_time
= time(NULL
);
483 lock
->refreshing
= 0;
484 curl_slist_free_all(dav_headers
);
489 static void check_locks(void)
491 struct remote_lock
*lock
= repo
->locks
;
492 time_t current_time
= time(NULL
);
496 time_remaining
= lock
->start_time
+ lock
->timeout
-
498 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
499 if (!refresh_lock(lock
)) {
501 "Unable to refresh lock for %s\n",
511 static void release_request(struct transfer_request
*request
)
513 struct transfer_request
*entry
= request_queue_head
;
515 if (request
== request_queue_head
) {
516 request_queue_head
= request
->next
;
518 while (entry
->next
!= NULL
&& entry
->next
!= request
)
520 if (entry
->next
== request
)
521 entry
->next
= entry
->next
->next
;
528 static void finish_request(struct transfer_request
*request
)
530 struct http_pack_request
*preq
;
531 struct http_object_request
*obj_req
;
533 request
->curl_result
= request
->slot
->curl_result
;
534 request
->http_code
= request
->slot
->http_code
;
535 request
->slot
= NULL
;
537 /* Keep locks active */
540 if (request
->headers
!= NULL
)
541 curl_slist_free_all(request
->headers
);
543 /* URL is reused for MOVE after PUT */
544 if (request
->state
!= RUN_PUT
) {
549 if (request
->state
== RUN_MKCOL
) {
550 if (request
->curl_result
== CURLE_OK
||
551 request
->http_code
== 405) {
552 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
555 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
556 sha1_to_hex(request
->obj
->sha1
),
557 request
->curl_result
, request
->http_code
);
558 request
->state
= ABORTED
;
561 } else if (request
->state
== RUN_PUT
) {
562 if (request
->curl_result
== CURLE_OK
) {
565 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
566 sha1_to_hex(request
->obj
->sha1
),
567 request
->curl_result
, request
->http_code
);
568 request
->state
= ABORTED
;
571 } else if (request
->state
== RUN_MOVE
) {
572 if (request
->curl_result
== CURLE_OK
) {
574 fprintf(stderr
, " sent %s\n",
575 sha1_to_hex(request
->obj
->sha1
));
576 request
->obj
->flags
|= REMOTE
;
577 release_request(request
);
579 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
580 sha1_to_hex(request
->obj
->sha1
),
581 request
->curl_result
, request
->http_code
);
582 request
->state
= ABORTED
;
585 } else if (request
->state
== RUN_FETCH_LOOSE
) {
586 obj_req
= (struct http_object_request
*)request
->userData
;
588 if (finish_http_object_request(obj_req
) == 0)
589 if (obj_req
->rename
== 0)
590 request
->obj
->flags
|= (LOCAL
| REMOTE
);
592 /* Try fetching packed if necessary */
593 if (request
->obj
->flags
& LOCAL
) {
594 release_http_object_request(obj_req
);
595 release_request(request
);
597 start_fetch_packed(request
);
599 } else if (request
->state
== RUN_FETCH_PACKED
) {
601 if (request
->curl_result
!= CURLE_OK
) {
602 fprintf(stderr
, "Unable to get pack file %s\n%s",
603 request
->url
, curl_errorstr
);
605 preq
= (struct http_pack_request
*)request
->userData
;
608 if (finish_http_pack_request(preq
) == 0)
610 release_http_pack_request(preq
);
614 repo
->can_update_info_refs
= 0;
615 release_request(request
);
619 #ifdef USE_CURL_MULTI
620 static int is_running_queue
;
621 static int fill_active_slot(void *unused
)
623 struct transfer_request
*request
;
625 if (aborted
|| !is_running_queue
)
628 for (request
= request_queue_head
; request
; request
= request
->next
) {
629 if (request
->state
== NEED_FETCH
) {
630 start_fetch_loose(request
);
632 } else if (pushing
&& request
->state
== NEED_PUSH
) {
633 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
636 start_mkcol(request
);
645 static void get_remote_object_list(unsigned char parent
);
647 static void add_fetch_request(struct object
*obj
)
649 struct transfer_request
*request
;
654 * Don't fetch the object if it's known to exist locally
655 * or is already in the request queue
657 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
658 get_remote_object_list(obj
->sha1
[0]);
659 if (obj
->flags
& (LOCAL
| FETCHING
))
662 obj
->flags
|= FETCHING
;
663 request
= xmalloc(sizeof(*request
));
666 request
->lock
= NULL
;
667 request
->headers
= NULL
;
668 request
->state
= NEED_FETCH
;
669 request
->next
= request_queue_head
;
670 request_queue_head
= request
;
672 #ifdef USE_CURL_MULTI
678 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
680 struct transfer_request
*request
= request_queue_head
;
681 struct packed_git
*target
;
683 /* Keep locks active */
687 * Don't push the object if it's known to exist on the remote
688 * or is already in the request queue
690 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
691 get_remote_object_list(obj
->sha1
[0]);
692 if (obj
->flags
& (REMOTE
| PUSHING
))
694 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
696 obj
->flags
|= REMOTE
;
700 obj
->flags
|= PUSHING
;
701 request
= xmalloc(sizeof(*request
));
704 request
->lock
= lock
;
705 request
->headers
= NULL
;
706 request
->state
= NEED_PUSH
;
707 request
->next
= request_queue_head
;
708 request_queue_head
= request
;
710 #ifdef USE_CURL_MULTI
718 static int fetch_indices(void)
723 fprintf(stderr
, "Getting pack list\n");
725 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
727 case HTTP_MISSING_TARGET
:
737 static void one_remote_object(const char *hex
)
739 unsigned char sha1
[20];
742 if (get_sha1_hex(hex
, sha1
) != 0)
745 obj
= lookup_object(sha1
);
747 obj
= parse_object(sha1
);
749 /* Ignore remote objects that don't exist locally */
753 obj
->flags
|= REMOTE
;
754 if (!object_list_contains(objects
, obj
))
755 object_list_insert(obj
, &objects
);
758 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
760 int *lock_flags
= (int *)ctx
->userData
;
763 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
764 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
765 (*lock_flags
& DAV_PROP_LOCKWR
)) {
766 *lock_flags
|= DAV_LOCK_OK
;
768 *lock_flags
&= DAV_LOCK_OK
;
769 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
770 *lock_flags
|= DAV_PROP_LOCKWR
;
771 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
772 *lock_flags
|= DAV_PROP_LOCKEX
;
777 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
779 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
781 unsigned char lock_token_sha1
[20];
783 if (tag_closed
&& ctx
->cdata
) {
784 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
785 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
786 strcpy(lock
->owner
, ctx
->cdata
);
787 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
788 if (!prefixcmp(ctx
->cdata
, "Second-"))
790 strtol(ctx
->cdata
+ 7, NULL
, 10);
791 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
792 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
793 strcpy(lock
->token
, ctx
->cdata
);
795 git_SHA1_Init(&sha_ctx
);
796 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
797 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
799 lock
->tmpfile_suffix
[0] = '_';
800 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
805 static void one_remote_ref(char *refname
);
808 xml_start_tag(void *userData
, const char *name
, const char **atts
)
810 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
811 const char *c
= strchr(name
, ':');
819 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
821 if (new_len
> ctx
->len
) {
822 ctx
->name
= xrealloc(ctx
->name
, new_len
);
825 strcat(ctx
->name
, ".");
826 strcat(ctx
->name
, c
);
831 ctx
->userFunc(ctx
, 0);
835 xml_end_tag(void *userData
, const char *name
)
837 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
838 const char *c
= strchr(name
, ':');
841 ctx
->userFunc(ctx
, 1);
848 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
853 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
855 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
857 ctx
->cdata
= xmemdupz(s
, len
);
860 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
862 struct active_request_slot
*slot
;
863 struct slot_results results
;
864 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
865 struct strbuf in_buffer
= STRBUF_INIT
;
868 char timeout_header
[25];
869 struct remote_lock
*lock
= NULL
;
870 struct curl_slist
*dav_headers
= NULL
;
874 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
875 sprintf(url
, "%s%s", repo
->url
, path
);
877 /* Make sure leading directories exist for the remote ref */
878 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
880 char saved_character
= ep
[1];
882 slot
= get_active_slot();
883 slot
->results
= &results
;
884 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
885 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
886 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
887 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
888 if (start_active_slot(slot
)) {
889 run_active_slot(slot
);
890 if (results
.curl_result
!= CURLE_OK
&&
891 results
.http_code
!= 405) {
893 "Unable to create branch path %s\n",
899 fprintf(stderr
, "Unable to start MKCOL request\n");
903 ep
[1] = saved_character
;
904 ep
= strchr(ep
+ 1, '/');
907 escaped
= xml_entities(git_default_email
);
908 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
911 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
912 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
913 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
915 slot
= get_active_slot();
916 slot
->results
= &results
;
917 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
918 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
919 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
920 #ifndef NO_CURL_IOCTL
921 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
922 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
924 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
925 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
926 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
927 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
928 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
929 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
931 lock
= xcalloc(1, sizeof(*lock
));
934 if (start_active_slot(slot
)) {
935 run_active_slot(slot
);
936 if (results
.curl_result
== CURLE_OK
) {
937 XML_Parser parser
= XML_ParserCreate(NULL
);
938 enum XML_Status result
;
939 ctx
.name
= xcalloc(10, 1);
942 ctx
.userFunc
= handle_new_lock_ctx
;
944 XML_SetUserData(parser
, &ctx
);
945 XML_SetElementHandler(parser
, xml_start_tag
,
947 XML_SetCharacterDataHandler(parser
, xml_cdata
);
948 result
= XML_Parse(parser
, in_buffer
.buf
,
951 if (result
!= XML_STATUS_OK
) {
952 fprintf(stderr
, "XML error: %s\n",
954 XML_GetErrorCode(parser
)));
957 XML_ParserFree(parser
);
960 fprintf(stderr
, "Unable to start LOCK request\n");
963 curl_slist_free_all(dav_headers
);
964 strbuf_release(&out_buffer
.buf
);
965 strbuf_release(&in_buffer
);
967 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
975 lock
->start_time
= time(NULL
);
976 lock
->next
= repo
->locks
;
983 static int unlock_remote(struct remote_lock
*lock
)
985 struct active_request_slot
*slot
;
986 struct slot_results results
;
987 struct remote_lock
*prev
= repo
->locks
;
988 struct curl_slist
*dav_headers
;
991 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
993 slot
= get_active_slot();
994 slot
->results
= &results
;
995 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
996 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
997 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
998 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1000 if (start_active_slot(slot
)) {
1001 run_active_slot(slot
);
1002 if (results
.curl_result
== CURLE_OK
)
1005 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1008 fprintf(stderr
, "Unable to start UNLOCK request\n");
1011 curl_slist_free_all(dav_headers
);
1013 if (repo
->locks
== lock
) {
1014 repo
->locks
= lock
->next
;
1016 while (prev
&& prev
->next
!= lock
)
1019 prev
->next
= prev
->next
->next
;
1030 static void remove_locks(void)
1032 struct remote_lock
*lock
= repo
->locks
;
1034 fprintf(stderr
, "Removing remote locks...\n");
1036 struct remote_lock
*next
= lock
->next
;
1037 unlock_remote(lock
);
1042 static void remove_locks_on_signal(int signo
)
1045 sigchain_pop(signo
);
1049 static void remote_ls(const char *path
, int flags
,
1050 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1053 static void process_ls_object(struct remote_ls_ctx
*ls
)
1055 unsigned int *parent
= (unsigned int *)ls
->userData
;
1056 char *path
= ls
->dentry_name
;
1059 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1060 remote_dir_exists
[*parent
] = 1;
1064 if (strlen(path
) != 49)
1067 obj_hex
= xmalloc(strlen(path
));
1068 /* NB: path is not null-terminated, can not use strlcpy here */
1069 memcpy(obj_hex
, path
, 2);
1070 strcpy(obj_hex
+ 2, path
+ 3);
1071 one_remote_object(obj_hex
);
1075 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1077 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1078 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1082 if (!(ls
->dentry_flags
& IS_DIR
))
1083 one_remote_ref(ls
->dentry_name
);
1086 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1088 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1091 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1092 if (ls
->dentry_flags
& IS_DIR
) {
1094 /* ensure collection names end with slash */
1095 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1097 if (ls
->flags
& PROCESS_DIRS
) {
1100 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1101 ls
->flags
& RECURSIVE
) {
1102 remote_ls(ls
->dentry_name
,
1107 } else if (ls
->flags
& PROCESS_FILES
) {
1110 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1111 char *path
= ctx
->cdata
;
1112 if (*ctx
->cdata
== 'h') {
1113 path
= strstr(path
, "//");
1115 path
= strchr(path
+2, '/');
1119 path
+= repo
->path_len
;
1120 ls
->dentry_name
= xstrdup(path
);
1122 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1123 ls
->dentry_flags
|= IS_DIR
;
1125 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1126 free(ls
->dentry_name
);
1127 ls
->dentry_name
= NULL
;
1128 ls
->dentry_flags
= 0;
1133 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1134 * should _only_ heed the information from that file, instead of trying to
1135 * determine the refs from the remote file system (badly: it does not even
1136 * know about packed-refs).
1138 static void remote_ls(const char *path
, int flags
,
1139 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1142 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1143 struct active_request_slot
*slot
;
1144 struct slot_results results
;
1145 struct strbuf in_buffer
= STRBUF_INIT
;
1146 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1147 struct curl_slist
*dav_headers
= NULL
;
1149 struct remote_ls_ctx ls
;
1152 ls
.path
= xstrdup(path
);
1153 ls
.dentry_name
= NULL
;
1154 ls
.dentry_flags
= 0;
1155 ls
.userData
= userData
;
1156 ls
.userFunc
= userFunc
;
1158 sprintf(url
, "%s%s", repo
->url
, path
);
1160 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1162 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1163 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1165 slot
= get_active_slot();
1166 slot
->results
= &results
;
1167 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1168 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1169 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1170 #ifndef NO_CURL_IOCTL
1171 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1172 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1174 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1175 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1176 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1177 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1178 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1179 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1181 if (start_active_slot(slot
)) {
1182 run_active_slot(slot
);
1183 if (results
.curl_result
== CURLE_OK
) {
1184 XML_Parser parser
= XML_ParserCreate(NULL
);
1185 enum XML_Status result
;
1186 ctx
.name
= xcalloc(10, 1);
1189 ctx
.userFunc
= handle_remote_ls_ctx
;
1191 XML_SetUserData(parser
, &ctx
);
1192 XML_SetElementHandler(parser
, xml_start_tag
,
1194 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1195 result
= XML_Parse(parser
, in_buffer
.buf
,
1199 if (result
!= XML_STATUS_OK
) {
1200 fprintf(stderr
, "XML error: %s\n",
1202 XML_GetErrorCode(parser
)));
1204 XML_ParserFree(parser
);
1207 fprintf(stderr
, "Unable to start PROPFIND request\n");
1212 strbuf_release(&out_buffer
.buf
);
1213 strbuf_release(&in_buffer
);
1214 curl_slist_free_all(dav_headers
);
1217 static void get_remote_object_list(unsigned char parent
)
1219 char path
[] = "objects/XX/";
1220 static const char hex
[] = "0123456789abcdef";
1221 unsigned int val
= parent
;
1223 path
[8] = hex
[val
>> 4];
1224 path
[9] = hex
[val
& 0xf];
1225 remote_dir_exists
[val
] = 0;
1226 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1227 process_ls_object
, &val
);
1230 static int locking_available(void)
1232 struct active_request_slot
*slot
;
1233 struct slot_results results
;
1234 struct strbuf in_buffer
= STRBUF_INIT
;
1235 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1236 struct curl_slist
*dav_headers
= NULL
;
1241 escaped
= xml_entities(repo
->url
);
1242 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1245 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1246 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1248 slot
= get_active_slot();
1249 slot
->results
= &results
;
1250 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1251 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1252 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1253 #ifndef NO_CURL_IOCTL
1254 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1255 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1257 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1259 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1260 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1261 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1262 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1264 if (start_active_slot(slot
)) {
1265 run_active_slot(slot
);
1266 if (results
.curl_result
== CURLE_OK
) {
1267 XML_Parser parser
= XML_ParserCreate(NULL
);
1268 enum XML_Status result
;
1269 ctx
.name
= xcalloc(10, 1);
1272 ctx
.userFunc
= handle_lockprop_ctx
;
1273 ctx
.userData
= &lock_flags
;
1274 XML_SetUserData(parser
, &ctx
);
1275 XML_SetElementHandler(parser
, xml_start_tag
,
1277 result
= XML_Parse(parser
, in_buffer
.buf
,
1281 if (result
!= XML_STATUS_OK
) {
1282 fprintf(stderr
, "XML error: %s\n",
1284 XML_GetErrorCode(parser
)));
1287 XML_ParserFree(parser
);
1289 error("no DAV locking support on %s",
1293 error("Cannot access URL %s, return code %d",
1294 repo
->url
, results
.curl_result
);
1298 error("Unable to start PROPFIND request on %s", repo
->url
);
1301 strbuf_release(&out_buffer
.buf
);
1302 strbuf_release(&in_buffer
);
1303 curl_slist_free_all(dav_headers
);
1308 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1310 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1314 return &entry
->next
;
1317 static struct object_list
**process_blob(struct blob
*blob
,
1318 struct object_list
**p
,
1319 struct name_path
*path
,
1322 struct object
*obj
= &blob
->object
;
1324 obj
->flags
|= LOCAL
;
1326 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1330 return add_one_object(obj
, p
);
1333 static struct object_list
**process_tree(struct tree
*tree
,
1334 struct object_list
**p
,
1335 struct name_path
*path
,
1338 struct object
*obj
= &tree
->object
;
1339 struct tree_desc desc
;
1340 struct name_entry entry
;
1341 struct name_path me
;
1343 obj
->flags
|= LOCAL
;
1345 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1347 if (parse_tree(tree
) < 0)
1348 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1351 name
= xstrdup(name
);
1352 p
= add_one_object(obj
, p
);
1355 me
.elem_len
= strlen(name
);
1357 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1359 while (tree_entry(&desc
, &entry
))
1360 switch (object_type(entry
.mode
)) {
1362 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1365 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1368 /* Subproject commit - not in this repository */
1373 tree
->buffer
= NULL
;
1377 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1380 struct commit
*commit
;
1381 struct object_list
**p
= &objects
;
1384 while ((commit
= get_revision(revs
)) != NULL
) {
1385 p
= process_tree(commit
->tree
, p
, NULL
, "");
1386 commit
->object
.flags
|= LOCAL
;
1387 if (!(commit
->object
.flags
& UNINTERESTING
))
1388 count
+= add_send_request(&commit
->object
, lock
);
1391 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1392 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1393 struct object
*obj
= entry
->item
;
1394 const char *name
= entry
->name
;
1396 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1398 if (obj
->type
== OBJ_TAG
) {
1400 p
= add_one_object(obj
, p
);
1403 if (obj
->type
== OBJ_TREE
) {
1404 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1407 if (obj
->type
== OBJ_BLOB
) {
1408 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1411 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1415 if (!(objects
->item
->flags
& UNINTERESTING
))
1416 count
+= add_send_request(objects
->item
, lock
);
1417 objects
= objects
->next
;
1423 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1425 struct active_request_slot
*slot
;
1426 struct slot_results results
;
1427 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1428 struct curl_slist
*dav_headers
;
1430 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1432 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1434 slot
= get_active_slot();
1435 slot
->results
= &results
;
1436 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1437 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1438 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1439 #ifndef NO_CURL_IOCTL
1440 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1441 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1443 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1444 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1445 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1446 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1447 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1448 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1450 if (start_active_slot(slot
)) {
1451 run_active_slot(slot
);
1452 strbuf_release(&out_buffer
.buf
);
1453 if (results
.curl_result
!= CURLE_OK
) {
1455 "PUT error: curl result=%d, HTTP code=%ld\n",
1456 results
.curl_result
, results
.http_code
);
1457 /* We should attempt recovery? */
1461 strbuf_release(&out_buffer
.buf
);
1462 fprintf(stderr
, "Unable to start PUT request\n");
1469 static struct ref
*remote_refs
;
1471 static void one_remote_ref(char *refname
)
1476 ref
= alloc_ref(refname
);
1478 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1480 "Unable to fetch ref %s from %s\n",
1481 refname
, repo
->url
);
1487 * Fetch a copy of the object if it doesn't exist locally - it
1488 * may be required for updating server info later.
1490 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1491 obj
= lookup_unknown_object(ref
->old_sha1
);
1493 fprintf(stderr
, " fetch %s for %s\n",
1494 sha1_to_hex(ref
->old_sha1
), refname
);
1495 add_fetch_request(obj
);
1499 ref
->next
= remote_refs
;
1503 static void get_dav_remote_heads(void)
1505 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1508 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1510 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1516 ref
= alloc_ref(ls
->dentry_name
);
1518 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1520 "Unable to fetch ref %s from %s\n",
1521 ls
->dentry_name
, repo
->url
);
1527 o
= parse_object(ref
->old_sha1
);
1530 "Unable to parse object %s for remote ref %s\n",
1531 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1537 len
= strlen(ls
->dentry_name
) + 42;
1538 ref_info
= xcalloc(len
+ 1, 1);
1539 sprintf(ref_info
, "%s %s\n",
1540 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1541 fwrite_buffer(ref_info
, 1, len
, buf
);
1544 if (o
->type
== OBJ_TAG
) {
1545 o
= deref_tag(o
, ls
->dentry_name
, 0);
1547 len
= strlen(ls
->dentry_name
) + 45;
1548 ref_info
= xcalloc(len
+ 1, 1);
1549 sprintf(ref_info
, "%s %s^{}\n",
1550 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1551 fwrite_buffer(ref_info
, 1, len
, buf
);
1558 static void update_remote_info_refs(struct remote_lock
*lock
)
1560 struct buffer buffer
= { STRBUF_INIT
, 0 };
1561 struct active_request_slot
*slot
;
1562 struct slot_results results
;
1563 struct curl_slist
*dav_headers
;
1565 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1566 add_remote_info_ref
, &buffer
.buf
);
1568 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1570 slot
= get_active_slot();
1571 slot
->results
= &results
;
1572 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1573 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1574 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1575 #ifndef NO_CURL_IOCTL
1576 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1577 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1579 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1580 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1581 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1582 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1583 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1584 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1586 if (start_active_slot(slot
)) {
1587 run_active_slot(slot
);
1588 if (results
.curl_result
!= CURLE_OK
) {
1590 "PUT error: curl result=%d, HTTP code=%ld\n",
1591 results
.curl_result
, results
.http_code
);
1595 strbuf_release(&buffer
.buf
);
1598 static int remote_exists(const char *path
)
1600 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1603 sprintf(url
, "%s%s", repo
->url
, path
);
1605 switch (http_get_strbuf(url
, NULL
, 0)) {
1609 case HTTP_MISSING_TARGET
:
1613 http_error(url
, HTTP_ERROR
);
1621 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1624 struct strbuf buffer
= STRBUF_INIT
;
1626 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1627 sprintf(url
, "%s%s", repo
->url
, path
);
1629 if (http_get_strbuf(url
, &buffer
, 0) != HTTP_OK
)
1630 die("Couldn't get %s for remote symref\n%s", url
,
1638 if (buffer
.len
== 0)
1641 /* If it's a symref, set the refname; otherwise try for a sha1 */
1642 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
1643 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
1645 get_sha1_hex(buffer
.buf
, sha1
);
1648 strbuf_release(&buffer
);
1651 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
1653 struct commit
*head
= lookup_commit(head_sha1
);
1654 struct commit
*branch
= lookup_commit(branch_sha1
);
1655 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
1657 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
1660 static int delete_remote_branch(char *pattern
, int force
)
1662 struct ref
*refs
= remote_refs
;
1663 struct ref
*remote_ref
= NULL
;
1664 unsigned char head_sha1
[20];
1665 char *symref
= NULL
;
1667 int patlen
= strlen(pattern
);
1669 struct active_request_slot
*slot
;
1670 struct slot_results results
;
1673 /* Find the remote branch(es) matching the specified branch name */
1674 for (match
= 0; refs
; refs
= refs
->next
) {
1675 char *name
= refs
->name
;
1676 int namelen
= strlen(name
);
1677 if (namelen
< patlen
||
1678 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1680 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1686 return error("No remote branch matches %s", pattern
);
1688 return error("More than one remote branch matches %s",
1692 * Remote HEAD must be a symref (not exactly foolproof; a remote
1693 * symlink to a symref will look like a symref)
1695 fetch_symref("HEAD", &symref
, head_sha1
);
1697 return error("Remote HEAD is not a symref");
1699 /* Remote branch must not be the remote HEAD */
1700 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
1701 if (!strcmp(remote_ref
->name
, symref
))
1702 return error("Remote branch %s is the current HEAD",
1704 fetch_symref(symref
, &symref
, head_sha1
);
1707 /* Run extra sanity checks if delete is not forced */
1709 /* Remote HEAD must resolve to a known object */
1711 return error("Remote HEAD symrefs too deep");
1712 if (is_null_sha1(head_sha1
))
1713 return error("Unable to resolve remote HEAD");
1714 if (!has_sha1_file(head_sha1
))
1715 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1717 /* Remote branch must resolve to a known object */
1718 if (is_null_sha1(remote_ref
->old_sha1
))
1719 return error("Unable to resolve remote branch %s",
1721 if (!has_sha1_file(remote_ref
->old_sha1
))
1722 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref
->name
, sha1_to_hex(remote_ref
->old_sha1
));
1724 /* Remote branch must be an ancestor of remote HEAD */
1725 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
1726 return error("The branch '%s' is not an ancestor "
1727 "of your current HEAD.\n"
1728 "If you are sure you want to delete it,"
1729 " run:\n\t'git http-push -D %s %s'",
1730 remote_ref
->name
, repo
->url
, pattern
);
1734 /* Send delete request */
1735 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1738 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
1739 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
1740 slot
= get_active_slot();
1741 slot
->results
= &results
;
1742 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1743 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1744 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1745 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
1746 if (start_active_slot(slot
)) {
1747 run_active_slot(slot
);
1749 if (results
.curl_result
!= CURLE_OK
)
1750 return error("DELETE request failed (%d/%ld)\n",
1751 results
.curl_result
, results
.http_code
);
1754 return error("Unable to start DELETE request");
1760 static void run_request_queue(void)
1762 #ifdef USE_CURL_MULTI
1763 is_running_queue
= 1;
1764 fill_active_slots();
1765 add_fill_function(NULL
, fill_active_slot
);
1768 finish_all_active_slots();
1769 #ifdef USE_CURL_MULTI
1770 fill_active_slots();
1772 } while (request_queue_head
&& !aborted
);
1774 #ifdef USE_CURL_MULTI
1775 is_running_queue
= 0;
1779 int main(int argc
, char **argv
)
1781 struct transfer_request
*request
;
1782 struct transfer_request
*next_request
;
1784 char **refspec
= NULL
;
1785 struct remote_lock
*ref_lock
= NULL
;
1786 struct remote_lock
*info_ref_lock
= NULL
;
1787 struct rev_info revs
;
1788 int delete_branch
= 0;
1789 int force_delete
= 0;
1790 int objects_to_send
;
1794 struct ref
*ref
, *local_refs
;
1795 struct remote
*remote
;
1796 char *rewritten_url
= NULL
;
1798 git_extract_argv0_path(argv
[0]);
1800 repo
= xcalloc(sizeof(*repo
), 1);
1803 for (i
= 1; i
< argc
; i
++, argv
++) {
1807 if (!strcmp(arg
, "--all")) {
1808 push_all
= MATCH_REFS_ALL
;
1811 if (!strcmp(arg
, "--force")) {
1815 if (!strcmp(arg
, "--dry-run")) {
1819 if (!strcmp(arg
, "--helper-status")) {
1823 if (!strcmp(arg
, "--verbose")) {
1825 http_is_verbose
= 1;
1828 if (!strcmp(arg
, "-d")) {
1832 if (!strcmp(arg
, "-D")) {
1837 if (!strcmp(arg
, "-h"))
1838 usage(http_push_usage
);
1841 char *path
= strstr(arg
, "//");
1843 repo
->path_len
= strlen(arg
);
1845 repo
->path
= strchr(path
+2, '/');
1847 repo
->path_len
= strlen(repo
->path
);
1852 nr_refspec
= argc
- i
;
1856 #ifndef USE_CURL_MULTI
1857 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1861 usage(http_push_usage
);
1863 if (delete_branch
&& nr_refspec
!= 1)
1864 die("You must specify only one branch name when deleting a remote branch");
1866 setup_git_directory();
1868 memset(remote_dir_exists
, -1, 256);
1871 * Create a minimum remote by hand to give to http_init(),
1872 * primarily to allow it to look at the URL.
1874 remote
= xcalloc(sizeof(*remote
), 1);
1875 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
1876 remote
->url
[remote
->url_nr
++] = repo
->url
;
1879 if (repo
->url
&& repo
->url
[strlen(repo
->url
)-1] != '/') {
1880 rewritten_url
= xmalloc(strlen(repo
->url
)+2);
1881 strcpy(rewritten_url
, repo
->url
);
1882 strcat(rewritten_url
, "/");
1883 repo
->path
= rewritten_url
+ (repo
->path
- repo
->url
);
1885 repo
->url
= rewritten_url
;
1888 #ifdef USE_CURL_MULTI
1889 is_running_queue
= 0;
1892 /* Verify DAV compliance/lock support */
1893 if (!locking_available()) {
1898 sigchain_push_common(remove_locks_on_signal
);
1900 /* Check whether the remote has server info files */
1901 repo
->can_update_info_refs
= 0;
1902 repo
->has_info_refs
= remote_exists("info/refs");
1903 repo
->has_info_packs
= remote_exists("objects/info/packs");
1904 if (repo
->has_info_refs
) {
1905 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1907 repo
->can_update_info_refs
= 1;
1909 error("cannot lock existing info/refs");
1914 if (repo
->has_info_packs
)
1917 /* Get a list of all local and remote heads to validate refspecs */
1918 local_refs
= get_local_heads();
1919 fprintf(stderr
, "Fetching remote heads...\n");
1920 get_dav_remote_heads();
1921 run_request_queue();
1923 /* Remove a remote branch if -d or -D was specified */
1924 if (delete_branch
) {
1925 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1926 fprintf(stderr
, "Unable to delete remote branch %s\n",
1929 printf("error %s cannot remove\n", refspec
[0]);
1935 if (match_refs(local_refs
, &remote_refs
,
1936 nr_refspec
, (const char **) refspec
, push_all
)) {
1941 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1943 printf("error null no match\n");
1949 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1950 char old_hex
[60], *new_hex
;
1951 const char *commit_argv
[5];
1953 char *new_sha1_hex
, *old_sha1_hex
;
1958 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1959 if (delete_remote_branch(ref
->name
, 1) == -1) {
1960 error("Could not remove %s", ref
->name
);
1962 printf("error %s cannot remove\n", ref
->name
);
1965 else if (helper_status
)
1966 printf("ok %s\n", ref
->name
);
1971 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1973 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1975 printf("ok %s up to date\n", ref
->name
);
1980 !is_null_sha1(ref
->old_sha1
) &&
1982 if (!has_sha1_file(ref
->old_sha1
) ||
1983 !ref_newer(ref
->peer_ref
->new_sha1
,
1986 * We do not have the remote ref, or
1987 * we know that the remote ref is not
1988 * an ancestor of what we are trying to
1989 * push. Either way this can be losing
1990 * commits at the remote end and likely
1991 * we were not up to date to begin with.
1993 error("remote '%s' is not an ancestor of\n"
1995 "Maybe you are not up-to-date and "
1996 "need to pull first?",
1998 ref
->peer_ref
->name
);
2000 printf("error %s non-fast forward\n", ref
->name
);
2005 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2007 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2008 new_hex
= sha1_to_hex(ref
->new_sha1
);
2010 fprintf(stderr
, "updating '%s'", ref
->name
);
2011 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2012 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2013 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2016 printf("ok %s\n", ref
->name
);
2020 /* Lock remote branch ref */
2021 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2022 if (ref_lock
== NULL
) {
2023 fprintf(stderr
, "Unable to lock remote branch %s\n",
2026 printf("error %s lock error\n", ref
->name
);
2031 /* Set up revision info for this refspec */
2033 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2034 old_sha1_hex
= NULL
;
2035 commit_argv
[1] = "--objects";
2036 commit_argv
[2] = new_sha1_hex
;
2037 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
2038 old_sha1_hex
= xmalloc(42);
2039 sprintf(old_sha1_hex
, "^%s",
2040 sha1_to_hex(ref
->old_sha1
));
2041 commit_argv
[3] = old_sha1_hex
;
2044 commit_argv
[commit_argc
] = NULL
;
2045 init_revisions(&revs
, setup_git_directory());
2046 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2047 revs
.edge_hint
= 0; /* just in case */
2051 commit_argv
[1] = NULL
;
2054 /* Generate a list of objects that need to be pushed */
2056 if (prepare_revision_walk(&revs
))
2057 die("revision walk setup failed");
2058 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2059 objects_to_send
= get_delta(&revs
, ref_lock
);
2060 finish_all_active_slots();
2062 /* Push missing objects to remote, this would be a
2063 convenient time to pack them first if appropriate. */
2065 if (objects_to_send
)
2066 fprintf(stderr
, " sending %d objects\n",
2069 run_request_queue();
2071 /* Update the remote branch if all went well */
2072 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2076 fprintf(stderr
, " done\n");
2078 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
2079 unlock_remote(ref_lock
);
2083 /* Update remote server info if appropriate */
2084 if (repo
->has_info_refs
&& new_refs
) {
2085 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2086 fprintf(stderr
, "Updating remote server info\n");
2088 update_remote_info_refs(info_ref_lock
);
2090 fprintf(stderr
, "Unable to update server info\n");
2095 free(rewritten_url
);
2097 unlock_remote(info_ref_lock
);
2102 request
= request_queue_head
;
2103 while (request
!= NULL
) {
2104 next_request
= request
->next
;
2105 release_request(request
);
2106 request
= next_request
;