11 #include "list-objects.h"
17 static const char http_push_usage
[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
29 #define PREV_BUF_SIZE 4096
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #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>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #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>"
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
76 static signed char remote_dir_exists
[256];
78 static int push_verbosely
;
79 static int push_all
= MATCH_REFS_NONE
;
82 static int helper_status
;
84 static struct object_list
*objects
;
92 int can_update_info_refs
;
94 struct packed_git
*packs
;
95 struct remote_lock
*locks
;
98 static struct repo
*repo
;
100 enum transfer_state
{
112 struct transfer_request
117 struct remote_lock
*lock
;
118 struct curl_slist
*headers
;
119 struct buffer buffer
;
120 enum transfer_state state
;
121 CURLcode curl_result
;
122 char errorstr
[CURL_ERROR_SIZE
];
125 struct active_request_slot
*slot
;
126 struct transfer_request
*next
;
129 static struct transfer_request
*request_queue_head
;
136 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
145 char tmpfile_suffix
[41];
149 struct remote_lock
*next
;
152 /* Flags that control remote_ls processing */
153 #define PROCESS_FILES (1u << 0)
154 #define PROCESS_DIRS (1u << 1)
155 #define RECURSIVE (1u << 2)
157 /* Flags that remote_ls passes to callback functions */
158 #define IS_DIR (1u << 0)
163 void (*userFunc
)(struct remote_ls_ctx
*ls
);
168 struct remote_ls_ctx
*parent
;
171 /* get_dav_token_headers options */
172 enum dav_header_flag
{
173 DAV_HEADER_IF
= (1u << 0),
174 DAV_HEADER_LOCK
= (1u << 1),
175 DAV_HEADER_TIMEOUT
= (1u << 2)
178 static char *xml_entities(char *s
)
180 struct strbuf buf
= STRBUF_INIT
;
182 size_t len
= strcspn(s
, "\"<>&");
183 strbuf_add(&buf
, s
, len
);
187 strbuf_addstr(&buf
, """);
190 strbuf_addstr(&buf
, "<");
193 strbuf_addstr(&buf
, ">");
196 strbuf_addstr(&buf
, "&");
199 return strbuf_detach(&buf
, NULL
);
203 return strbuf_detach(&buf
, NULL
);
206 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
208 struct strbuf buf
= STRBUF_INIT
;
209 struct curl_slist
*dav_headers
= NULL
;
211 if (options
& DAV_HEADER_IF
) {
212 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
213 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
216 if (options
& DAV_HEADER_LOCK
) {
217 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
218 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
221 if (options
& DAV_HEADER_TIMEOUT
) {
222 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
223 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
226 strbuf_release(&buf
);
231 static void finish_request(struct transfer_request
*request
);
232 static void release_request(struct transfer_request
*request
);
234 static void process_response(void *callback_data
)
236 struct transfer_request
*request
=
237 (struct transfer_request
*)callback_data
;
239 finish_request(request
);
242 #ifdef USE_CURL_MULTI
244 static void start_fetch_loose(struct transfer_request
*request
)
246 struct active_request_slot
*slot
;
247 struct http_object_request
*obj_req
;
249 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
250 if (obj_req
== NULL
) {
251 request
->state
= ABORTED
;
255 slot
= obj_req
->slot
;
256 slot
->callback_func
= process_response
;
257 slot
->callback_data
= request
;
258 request
->slot
= slot
;
259 request
->userData
= obj_req
;
261 /* Try to get the request started, abort the request on error */
262 request
->state
= RUN_FETCH_LOOSE
;
263 if (!start_active_slot(slot
)) {
264 fprintf(stderr
, "Unable to start GET request\n");
265 repo
->can_update_info_refs
= 0;
266 release_http_object_request(obj_req
);
267 release_request(request
);
271 static void start_mkcol(struct transfer_request
*request
)
273 char *hex
= sha1_to_hex(request
->obj
->sha1
);
274 struct active_request_slot
*slot
;
276 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
278 slot
= get_active_slot();
279 slot
->callback_func
= process_response
;
280 slot
->callback_data
= request
;
281 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
282 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
283 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
284 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
285 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
287 if (start_active_slot(slot
)) {
288 request
->slot
= slot
;
289 request
->state
= RUN_MKCOL
;
291 request
->state
= ABORTED
;
298 static void start_fetch_packed(struct transfer_request
*request
)
300 struct packed_git
*target
;
302 struct transfer_request
*check_request
= request_queue_head
;
303 struct http_pack_request
*preq
;
305 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
307 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
308 repo
->can_update_info_refs
= 0;
309 release_request(request
);
313 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
314 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
316 preq
= new_http_pack_request(target
, repo
->url
);
318 release_http_pack_request(preq
);
319 repo
->can_update_info_refs
= 0;
322 preq
->lst
= &repo
->packs
;
324 /* Make sure there isn't another open request for this pack */
325 while (check_request
) {
326 if (check_request
->state
== RUN_FETCH_PACKED
&&
327 !strcmp(check_request
->url
, preq
->url
)) {
328 release_http_pack_request(preq
);
329 release_request(request
);
332 check_request
= check_request
->next
;
335 preq
->slot
->callback_func
= process_response
;
336 preq
->slot
->callback_data
= request
;
337 request
->slot
= preq
->slot
;
338 request
->userData
= preq
;
340 /* Try to get the request started, abort the request on error */
341 request
->state
= RUN_FETCH_PACKED
;
342 if (!start_active_slot(preq
->slot
)) {
343 fprintf(stderr
, "Unable to start GET request\n");
344 release_http_pack_request(preq
);
345 repo
->can_update_info_refs
= 0;
346 release_request(request
);
350 static void start_put(struct transfer_request
*request
)
352 char *hex
= sha1_to_hex(request
->obj
->sha1
);
353 struct active_request_slot
*slot
;
354 struct strbuf buf
= STRBUF_INIT
;
355 enum object_type type
;
363 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
364 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
367 memset(&stream
, 0, sizeof(stream
));
368 deflateInit(&stream
, zlib_compression_level
);
369 size
= deflateBound(&stream
, len
+ hdrlen
);
370 strbuf_init(&request
->buffer
.buf
, size
);
371 request
->buffer
.posn
= 0;
374 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
375 stream
.avail_out
= size
;
378 stream
.next_in
= (void *)hdr
;
379 stream
.avail_in
= hdrlen
;
380 while (deflate(&stream
, 0) == Z_OK
)
383 /* Then the data itself.. */
384 stream
.next_in
= unpacked
;
385 stream
.avail_in
= len
;
386 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
391 request
->buffer
.buf
.len
= stream
.total_out
;
393 strbuf_addstr(&buf
, "Destination: ");
394 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
395 request
->dest
= strbuf_detach(&buf
, NULL
);
397 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
398 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
399 request
->url
= strbuf_detach(&buf
, NULL
);
401 slot
= get_active_slot();
402 slot
->callback_func
= process_response
;
403 slot
->callback_data
= request
;
404 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
405 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
406 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
407 #ifndef NO_CURL_IOCTL
408 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
409 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &request
->buffer
);
411 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
412 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
413 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
414 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
415 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
416 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
418 if (start_active_slot(slot
)) {
419 request
->slot
= slot
;
420 request
->state
= RUN_PUT
;
422 request
->state
= ABORTED
;
428 static void start_move(struct transfer_request
*request
)
430 struct active_request_slot
*slot
;
431 struct curl_slist
*dav_headers
= NULL
;
433 slot
= get_active_slot();
434 slot
->callback_func
= process_response
;
435 slot
->callback_data
= request
;
436 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
437 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
438 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
439 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
440 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
441 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
442 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
444 if (start_active_slot(slot
)) {
445 request
->slot
= slot
;
446 request
->state
= RUN_MOVE
;
448 request
->state
= ABORTED
;
454 static int refresh_lock(struct remote_lock
*lock
)
456 struct active_request_slot
*slot
;
457 struct slot_results results
;
458 struct curl_slist
*dav_headers
;
461 lock
->refreshing
= 1;
463 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
465 slot
= get_active_slot();
466 slot
->results
= &results
;
467 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
468 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
469 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
470 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
471 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
473 if (start_active_slot(slot
)) {
474 run_active_slot(slot
);
475 if (results
.curl_result
!= CURLE_OK
) {
476 fprintf(stderr
, "LOCK HTTP error %ld\n",
479 lock
->start_time
= time(NULL
);
484 lock
->refreshing
= 0;
485 curl_slist_free_all(dav_headers
);
490 static void check_locks(void)
492 struct remote_lock
*lock
= repo
->locks
;
493 time_t current_time
= time(NULL
);
497 time_remaining
= lock
->start_time
+ lock
->timeout
-
499 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
500 if (!refresh_lock(lock
)) {
502 "Unable to refresh lock for %s\n",
512 static void release_request(struct transfer_request
*request
)
514 struct transfer_request
*entry
= request_queue_head
;
516 if (request
== request_queue_head
) {
517 request_queue_head
= request
->next
;
519 while (entry
->next
!= NULL
&& entry
->next
!= request
)
521 if (entry
->next
== request
)
522 entry
->next
= entry
->next
->next
;
529 static void finish_request(struct transfer_request
*request
)
531 struct http_pack_request
*preq
;
532 struct http_object_request
*obj_req
;
534 request
->curl_result
= request
->slot
->curl_result
;
535 request
->http_code
= request
->slot
->http_code
;
536 request
->slot
= NULL
;
538 /* Keep locks active */
541 if (request
->headers
!= NULL
)
542 curl_slist_free_all(request
->headers
);
544 /* URL is reused for MOVE after PUT */
545 if (request
->state
!= RUN_PUT
) {
550 if (request
->state
== RUN_MKCOL
) {
551 if (request
->curl_result
== CURLE_OK
||
552 request
->http_code
== 405) {
553 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
556 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
557 sha1_to_hex(request
->obj
->sha1
),
558 request
->curl_result
, request
->http_code
);
559 request
->state
= ABORTED
;
562 } else if (request
->state
== RUN_PUT
) {
563 if (request
->curl_result
== CURLE_OK
) {
566 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
567 sha1_to_hex(request
->obj
->sha1
),
568 request
->curl_result
, request
->http_code
);
569 request
->state
= ABORTED
;
572 } else if (request
->state
== RUN_MOVE
) {
573 if (request
->curl_result
== CURLE_OK
) {
575 fprintf(stderr
, " sent %s\n",
576 sha1_to_hex(request
->obj
->sha1
));
577 request
->obj
->flags
|= REMOTE
;
578 release_request(request
);
580 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
581 sha1_to_hex(request
->obj
->sha1
),
582 request
->curl_result
, request
->http_code
);
583 request
->state
= ABORTED
;
586 } else if (request
->state
== RUN_FETCH_LOOSE
) {
587 obj_req
= (struct http_object_request
*)request
->userData
;
589 if (finish_http_object_request(obj_req
) == 0)
590 if (obj_req
->rename
== 0)
591 request
->obj
->flags
|= (LOCAL
| REMOTE
);
593 /* Try fetching packed if necessary */
594 if (request
->obj
->flags
& LOCAL
) {
595 release_http_object_request(obj_req
);
596 release_request(request
);
598 start_fetch_packed(request
);
600 } else if (request
->state
== RUN_FETCH_PACKED
) {
602 if (request
->curl_result
!= CURLE_OK
) {
603 fprintf(stderr
, "Unable to get pack file %s\n%s",
604 request
->url
, curl_errorstr
);
606 preq
= (struct http_pack_request
*)request
->userData
;
609 if (finish_http_pack_request(preq
) == 0)
611 release_http_pack_request(preq
);
615 repo
->can_update_info_refs
= 0;
616 release_request(request
);
620 #ifdef USE_CURL_MULTI
621 static int is_running_queue
;
622 static int fill_active_slot(void *unused
)
624 struct transfer_request
*request
;
626 if (aborted
|| !is_running_queue
)
629 for (request
= request_queue_head
; request
; request
= request
->next
) {
630 if (request
->state
== NEED_FETCH
) {
631 start_fetch_loose(request
);
633 } else if (pushing
&& request
->state
== NEED_PUSH
) {
634 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
637 start_mkcol(request
);
646 static void get_remote_object_list(unsigned char parent
);
648 static void add_fetch_request(struct object
*obj
)
650 struct transfer_request
*request
;
655 * Don't fetch the object if it's known to exist locally
656 * or is already in the request queue
658 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
659 get_remote_object_list(obj
->sha1
[0]);
660 if (obj
->flags
& (LOCAL
| FETCHING
))
663 obj
->flags
|= FETCHING
;
664 request
= xmalloc(sizeof(*request
));
667 request
->lock
= NULL
;
668 request
->headers
= NULL
;
669 request
->state
= NEED_FETCH
;
670 request
->next
= request_queue_head
;
671 request_queue_head
= request
;
673 #ifdef USE_CURL_MULTI
679 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
681 struct transfer_request
*request
= request_queue_head
;
682 struct packed_git
*target
;
684 /* Keep locks active */
688 * Don't push the object if it's known to exist on the remote
689 * or is already in the request queue
691 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
692 get_remote_object_list(obj
->sha1
[0]);
693 if (obj
->flags
& (REMOTE
| PUSHING
))
695 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
697 obj
->flags
|= REMOTE
;
701 obj
->flags
|= PUSHING
;
702 request
= xmalloc(sizeof(*request
));
705 request
->lock
= lock
;
706 request
->headers
= NULL
;
707 request
->state
= NEED_PUSH
;
708 request
->next
= request_queue_head
;
709 request_queue_head
= request
;
711 #ifdef USE_CURL_MULTI
719 static int fetch_indices(void)
724 fprintf(stderr
, "Getting pack list\n");
726 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
728 case HTTP_MISSING_TARGET
:
738 static void one_remote_object(const char *hex
)
740 unsigned char sha1
[20];
743 if (get_sha1_hex(hex
, sha1
) != 0)
746 obj
= lookup_object(sha1
);
748 obj
= parse_object(sha1
);
750 /* Ignore remote objects that don't exist locally */
754 obj
->flags
|= REMOTE
;
755 if (!object_list_contains(objects
, obj
))
756 object_list_insert(obj
, &objects
);
759 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
761 int *lock_flags
= (int *)ctx
->userData
;
764 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
765 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
766 (*lock_flags
& DAV_PROP_LOCKWR
)) {
767 *lock_flags
|= DAV_LOCK_OK
;
769 *lock_flags
&= DAV_LOCK_OK
;
770 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
771 *lock_flags
|= DAV_PROP_LOCKWR
;
772 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
773 *lock_flags
|= DAV_PROP_LOCKEX
;
778 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
780 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
782 unsigned char lock_token_sha1
[20];
784 if (tag_closed
&& ctx
->cdata
) {
785 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
786 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
787 strcpy(lock
->owner
, ctx
->cdata
);
788 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
789 if (!prefixcmp(ctx
->cdata
, "Second-"))
791 strtol(ctx
->cdata
+ 7, NULL
, 10);
792 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
793 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
794 strcpy(lock
->token
, ctx
->cdata
);
796 git_SHA1_Init(&sha_ctx
);
797 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
798 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
800 lock
->tmpfile_suffix
[0] = '_';
801 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
806 static void one_remote_ref(char *refname
);
809 xml_start_tag(void *userData
, const char *name
, const char **atts
)
811 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
812 const char *c
= strchr(name
, ':');
820 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
822 if (new_len
> ctx
->len
) {
823 ctx
->name
= xrealloc(ctx
->name
, new_len
);
826 strcat(ctx
->name
, ".");
827 strcat(ctx
->name
, c
);
832 ctx
->userFunc(ctx
, 0);
836 xml_end_tag(void *userData
, const char *name
)
838 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
839 const char *c
= strchr(name
, ':');
842 ctx
->userFunc(ctx
, 1);
849 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
854 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
856 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
858 ctx
->cdata
= xmemdupz(s
, len
);
861 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
863 struct active_request_slot
*slot
;
864 struct slot_results results
;
865 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
866 struct strbuf in_buffer
= STRBUF_INIT
;
869 char timeout_header
[25];
870 struct remote_lock
*lock
= NULL
;
871 struct curl_slist
*dav_headers
= NULL
;
875 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
876 sprintf(url
, "%s%s", repo
->url
, path
);
878 /* Make sure leading directories exist for the remote ref */
879 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
881 char saved_character
= ep
[1];
883 slot
= get_active_slot();
884 slot
->results
= &results
;
885 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
886 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
887 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
888 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
889 if (start_active_slot(slot
)) {
890 run_active_slot(slot
);
891 if (results
.curl_result
!= CURLE_OK
&&
892 results
.http_code
!= 405) {
894 "Unable to create branch path %s\n",
900 fprintf(stderr
, "Unable to start MKCOL request\n");
904 ep
[1] = saved_character
;
905 ep
= strchr(ep
+ 1, '/');
908 escaped
= xml_entities(git_default_email
);
909 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
912 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
913 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
914 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
916 slot
= get_active_slot();
917 slot
->results
= &results
;
918 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
919 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
920 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
921 #ifndef NO_CURL_IOCTL
922 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
923 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
925 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
926 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
927 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
928 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
929 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
930 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
932 lock
= xcalloc(1, sizeof(*lock
));
935 if (start_active_slot(slot
)) {
936 run_active_slot(slot
);
937 if (results
.curl_result
== CURLE_OK
) {
938 XML_Parser parser
= XML_ParserCreate(NULL
);
939 enum XML_Status result
;
940 ctx
.name
= xcalloc(10, 1);
943 ctx
.userFunc
= handle_new_lock_ctx
;
945 XML_SetUserData(parser
, &ctx
);
946 XML_SetElementHandler(parser
, xml_start_tag
,
948 XML_SetCharacterDataHandler(parser
, xml_cdata
);
949 result
= XML_Parse(parser
, in_buffer
.buf
,
952 if (result
!= XML_STATUS_OK
) {
953 fprintf(stderr
, "XML error: %s\n",
955 XML_GetErrorCode(parser
)));
958 XML_ParserFree(parser
);
961 fprintf(stderr
, "Unable to start LOCK request\n");
964 curl_slist_free_all(dav_headers
);
965 strbuf_release(&out_buffer
.buf
);
966 strbuf_release(&in_buffer
);
968 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
976 lock
->start_time
= time(NULL
);
977 lock
->next
= repo
->locks
;
984 static int unlock_remote(struct remote_lock
*lock
)
986 struct active_request_slot
*slot
;
987 struct slot_results results
;
988 struct remote_lock
*prev
= repo
->locks
;
989 struct curl_slist
*dav_headers
;
992 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
994 slot
= get_active_slot();
995 slot
->results
= &results
;
996 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
997 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
998 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
999 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1001 if (start_active_slot(slot
)) {
1002 run_active_slot(slot
);
1003 if (results
.curl_result
== CURLE_OK
)
1006 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1009 fprintf(stderr
, "Unable to start UNLOCK request\n");
1012 curl_slist_free_all(dav_headers
);
1014 if (repo
->locks
== lock
) {
1015 repo
->locks
= lock
->next
;
1017 while (prev
&& prev
->next
!= lock
)
1020 prev
->next
= prev
->next
->next
;
1031 static void remove_locks(void)
1033 struct remote_lock
*lock
= repo
->locks
;
1035 fprintf(stderr
, "Removing remote locks...\n");
1037 struct remote_lock
*next
= lock
->next
;
1038 unlock_remote(lock
);
1043 static void remove_locks_on_signal(int signo
)
1046 sigchain_pop(signo
);
1050 static void remote_ls(const char *path
, int flags
,
1051 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1054 static void process_ls_object(struct remote_ls_ctx
*ls
)
1056 unsigned int *parent
= (unsigned int *)ls
->userData
;
1057 char *path
= ls
->dentry_name
;
1060 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1061 remote_dir_exists
[*parent
] = 1;
1065 if (strlen(path
) != 49)
1068 obj_hex
= xmalloc(strlen(path
));
1069 /* NB: path is not null-terminated, can not use strlcpy here */
1070 memcpy(obj_hex
, path
, 2);
1071 strcpy(obj_hex
+ 2, path
+ 3);
1072 one_remote_object(obj_hex
);
1076 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1078 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1079 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1083 if (!(ls
->dentry_flags
& IS_DIR
))
1084 one_remote_ref(ls
->dentry_name
);
1087 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1089 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1092 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1093 if (ls
->dentry_flags
& IS_DIR
) {
1094 if (ls
->flags
& PROCESS_DIRS
) {
1097 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1098 ls
->flags
& RECURSIVE
) {
1099 remote_ls(ls
->dentry_name
,
1104 } else if (ls
->flags
& PROCESS_FILES
) {
1107 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1108 char *path
= ctx
->cdata
;
1109 if (*ctx
->cdata
== 'h') {
1110 path
= strstr(path
, "//");
1112 path
= strchr(path
+2, '/');
1116 path
+= repo
->path_len
;
1117 ls
->dentry_name
= xstrdup(path
);
1119 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1120 ls
->dentry_flags
|= IS_DIR
;
1122 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1123 free(ls
->dentry_name
);
1124 ls
->dentry_name
= NULL
;
1125 ls
->dentry_flags
= 0;
1130 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1131 * should _only_ heed the information from that file, instead of trying to
1132 * determine the refs from the remote file system (badly: it does not even
1133 * know about packed-refs).
1135 static void remote_ls(const char *path
, int flags
,
1136 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1139 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1140 struct active_request_slot
*slot
;
1141 struct slot_results results
;
1142 struct strbuf in_buffer
= STRBUF_INIT
;
1143 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1144 struct curl_slist
*dav_headers
= NULL
;
1146 struct remote_ls_ctx ls
;
1149 ls
.path
= xstrdup(path
);
1150 ls
.dentry_name
= NULL
;
1151 ls
.dentry_flags
= 0;
1152 ls
.userData
= userData
;
1153 ls
.userFunc
= userFunc
;
1155 sprintf(url
, "%s%s", repo
->url
, path
);
1157 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1159 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1160 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1162 slot
= get_active_slot();
1163 slot
->results
= &results
;
1164 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1165 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1166 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1167 #ifndef NO_CURL_IOCTL
1168 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1169 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1171 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1172 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1173 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1174 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1175 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1176 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1178 if (start_active_slot(slot
)) {
1179 run_active_slot(slot
);
1180 if (results
.curl_result
== CURLE_OK
) {
1181 XML_Parser parser
= XML_ParserCreate(NULL
);
1182 enum XML_Status result
;
1183 ctx
.name
= xcalloc(10, 1);
1186 ctx
.userFunc
= handle_remote_ls_ctx
;
1188 XML_SetUserData(parser
, &ctx
);
1189 XML_SetElementHandler(parser
, xml_start_tag
,
1191 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1192 result
= XML_Parse(parser
, in_buffer
.buf
,
1196 if (result
!= XML_STATUS_OK
) {
1197 fprintf(stderr
, "XML error: %s\n",
1199 XML_GetErrorCode(parser
)));
1201 XML_ParserFree(parser
);
1204 fprintf(stderr
, "Unable to start PROPFIND request\n");
1209 strbuf_release(&out_buffer
.buf
);
1210 strbuf_release(&in_buffer
);
1211 curl_slist_free_all(dav_headers
);
1214 static void get_remote_object_list(unsigned char parent
)
1216 char path
[] = "objects/XX/";
1217 static const char hex
[] = "0123456789abcdef";
1218 unsigned int val
= parent
;
1220 path
[8] = hex
[val
>> 4];
1221 path
[9] = hex
[val
& 0xf];
1222 remote_dir_exists
[val
] = 0;
1223 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1224 process_ls_object
, &val
);
1227 static int locking_available(void)
1229 struct active_request_slot
*slot
;
1230 struct slot_results results
;
1231 struct strbuf in_buffer
= STRBUF_INIT
;
1232 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1233 struct curl_slist
*dav_headers
= NULL
;
1238 escaped
= xml_entities(repo
->url
);
1239 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1242 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1243 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1245 slot
= get_active_slot();
1246 slot
->results
= &results
;
1247 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1248 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1249 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1250 #ifndef NO_CURL_IOCTL
1251 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1252 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1254 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1255 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1256 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1257 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1259 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1261 if (start_active_slot(slot
)) {
1262 run_active_slot(slot
);
1263 if (results
.curl_result
== CURLE_OK
) {
1264 XML_Parser parser
= XML_ParserCreate(NULL
);
1265 enum XML_Status result
;
1266 ctx
.name
= xcalloc(10, 1);
1269 ctx
.userFunc
= handle_lockprop_ctx
;
1270 ctx
.userData
= &lock_flags
;
1271 XML_SetUserData(parser
, &ctx
);
1272 XML_SetElementHandler(parser
, xml_start_tag
,
1274 result
= XML_Parse(parser
, in_buffer
.buf
,
1278 if (result
!= XML_STATUS_OK
) {
1279 fprintf(stderr
, "XML error: %s\n",
1281 XML_GetErrorCode(parser
)));
1284 XML_ParserFree(parser
);
1286 error("no DAV locking support on %s",
1290 error("Cannot access URL %s, return code %d",
1291 repo
->url
, results
.curl_result
);
1295 error("Unable to start PROPFIND request on %s", repo
->url
);
1298 strbuf_release(&out_buffer
.buf
);
1299 strbuf_release(&in_buffer
);
1300 curl_slist_free_all(dav_headers
);
1305 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1307 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1311 return &entry
->next
;
1314 static struct object_list
**process_blob(struct blob
*blob
,
1315 struct object_list
**p
,
1316 struct name_path
*path
,
1319 struct object
*obj
= &blob
->object
;
1321 obj
->flags
|= LOCAL
;
1323 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1327 return add_one_object(obj
, p
);
1330 static struct object_list
**process_tree(struct tree
*tree
,
1331 struct object_list
**p
,
1332 struct name_path
*path
,
1335 struct object
*obj
= &tree
->object
;
1336 struct tree_desc desc
;
1337 struct name_entry entry
;
1338 struct name_path me
;
1340 obj
->flags
|= LOCAL
;
1342 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1344 if (parse_tree(tree
) < 0)
1345 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1348 name
= xstrdup(name
);
1349 p
= add_one_object(obj
, p
);
1352 me
.elem_len
= strlen(name
);
1354 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1356 while (tree_entry(&desc
, &entry
))
1357 switch (object_type(entry
.mode
)) {
1359 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1362 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1365 /* Subproject commit - not in this repository */
1370 tree
->buffer
= NULL
;
1374 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1377 struct commit
*commit
;
1378 struct object_list
**p
= &objects
;
1381 while ((commit
= get_revision(revs
)) != NULL
) {
1382 p
= process_tree(commit
->tree
, p
, NULL
, "");
1383 commit
->object
.flags
|= LOCAL
;
1384 if (!(commit
->object
.flags
& UNINTERESTING
))
1385 count
+= add_send_request(&commit
->object
, lock
);
1388 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1389 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1390 struct object
*obj
= entry
->item
;
1391 const char *name
= entry
->name
;
1393 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1395 if (obj
->type
== OBJ_TAG
) {
1397 p
= add_one_object(obj
, p
);
1400 if (obj
->type
== OBJ_TREE
) {
1401 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1404 if (obj
->type
== OBJ_BLOB
) {
1405 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1408 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1412 if (!(objects
->item
->flags
& UNINTERESTING
))
1413 count
+= add_send_request(objects
->item
, lock
);
1414 objects
= objects
->next
;
1420 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1422 struct active_request_slot
*slot
;
1423 struct slot_results results
;
1424 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1425 struct curl_slist
*dav_headers
;
1427 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1429 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1431 slot
= get_active_slot();
1432 slot
->results
= &results
;
1433 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1434 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1435 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1436 #ifndef NO_CURL_IOCTL
1437 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1438 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1440 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1441 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1442 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1443 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1444 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1445 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1447 if (start_active_slot(slot
)) {
1448 run_active_slot(slot
);
1449 strbuf_release(&out_buffer
.buf
);
1450 if (results
.curl_result
!= CURLE_OK
) {
1452 "PUT error: curl result=%d, HTTP code=%ld\n",
1453 results
.curl_result
, results
.http_code
);
1454 /* We should attempt recovery? */
1458 strbuf_release(&out_buffer
.buf
);
1459 fprintf(stderr
, "Unable to start PUT request\n");
1466 static struct ref
*remote_refs
;
1468 static void one_remote_ref(char *refname
)
1473 ref
= alloc_ref(refname
);
1475 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1477 "Unable to fetch ref %s from %s\n",
1478 refname
, repo
->url
);
1484 * Fetch a copy of the object if it doesn't exist locally - it
1485 * may be required for updating server info later.
1487 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1488 obj
= lookup_unknown_object(ref
->old_sha1
);
1490 fprintf(stderr
, " fetch %s for %s\n",
1491 sha1_to_hex(ref
->old_sha1
), refname
);
1492 add_fetch_request(obj
);
1496 ref
->next
= remote_refs
;
1500 static void get_dav_remote_heads(void)
1502 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1505 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1507 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1513 ref
= alloc_ref(ls
->dentry_name
);
1515 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1517 "Unable to fetch ref %s from %s\n",
1518 ls
->dentry_name
, repo
->url
);
1524 o
= parse_object(ref
->old_sha1
);
1527 "Unable to parse object %s for remote ref %s\n",
1528 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1534 len
= strlen(ls
->dentry_name
) + 42;
1535 ref_info
= xcalloc(len
+ 1, 1);
1536 sprintf(ref_info
, "%s %s\n",
1537 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1538 fwrite_buffer(ref_info
, 1, len
, buf
);
1541 if (o
->type
== OBJ_TAG
) {
1542 o
= deref_tag(o
, ls
->dentry_name
, 0);
1544 len
= strlen(ls
->dentry_name
) + 45;
1545 ref_info
= xcalloc(len
+ 1, 1);
1546 sprintf(ref_info
, "%s %s^{}\n",
1547 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1548 fwrite_buffer(ref_info
, 1, len
, buf
);
1555 static void update_remote_info_refs(struct remote_lock
*lock
)
1557 struct buffer buffer
= { STRBUF_INIT
, 0 };
1558 struct active_request_slot
*slot
;
1559 struct slot_results results
;
1560 struct curl_slist
*dav_headers
;
1562 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1563 add_remote_info_ref
, &buffer
.buf
);
1565 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1567 slot
= get_active_slot();
1568 slot
->results
= &results
;
1569 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1570 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1571 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1572 #ifndef NO_CURL_IOCTL
1573 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1574 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1576 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1577 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1578 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1579 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1580 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1581 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1583 if (start_active_slot(slot
)) {
1584 run_active_slot(slot
);
1585 if (results
.curl_result
!= CURLE_OK
) {
1587 "PUT error: curl result=%d, HTTP code=%ld\n",
1588 results
.curl_result
, results
.http_code
);
1592 strbuf_release(&buffer
.buf
);
1595 static int remote_exists(const char *path
)
1597 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1600 sprintf(url
, "%s%s", repo
->url
, path
);
1602 switch (http_get_strbuf(url
, NULL
, 0)) {
1606 case HTTP_MISSING_TARGET
:
1610 http_error(url
, HTTP_ERROR
);
1618 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1621 struct strbuf buffer
= STRBUF_INIT
;
1623 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1624 sprintf(url
, "%s%s", repo
->url
, path
);
1626 if (http_get_strbuf(url
, &buffer
, 0) != HTTP_OK
)
1627 die("Couldn't get %s for remote symref\n%s", url
,
1635 if (buffer
.len
== 0)
1638 /* If it's a symref, set the refname; otherwise try for a sha1 */
1639 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
1640 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
1642 get_sha1_hex(buffer
.buf
, sha1
);
1645 strbuf_release(&buffer
);
1648 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
1650 struct commit
*head
= lookup_commit(head_sha1
);
1651 struct commit
*branch
= lookup_commit(branch_sha1
);
1652 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
1654 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
1657 static int delete_remote_branch(char *pattern
, int force
)
1659 struct ref
*refs
= remote_refs
;
1660 struct ref
*remote_ref
= NULL
;
1661 unsigned char head_sha1
[20];
1662 char *symref
= NULL
;
1664 int patlen
= strlen(pattern
);
1666 struct active_request_slot
*slot
;
1667 struct slot_results results
;
1670 /* Find the remote branch(es) matching the specified branch name */
1671 for (match
= 0; refs
; refs
= refs
->next
) {
1672 char *name
= refs
->name
;
1673 int namelen
= strlen(name
);
1674 if (namelen
< patlen
||
1675 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1677 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1683 return error("No remote branch matches %s", pattern
);
1685 return error("More than one remote branch matches %s",
1689 * Remote HEAD must be a symref (not exactly foolproof; a remote
1690 * symlink to a symref will look like a symref)
1692 fetch_symref("HEAD", &symref
, head_sha1
);
1694 return error("Remote HEAD is not a symref");
1696 /* Remote branch must not be the remote HEAD */
1697 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
1698 if (!strcmp(remote_ref
->name
, symref
))
1699 return error("Remote branch %s is the current HEAD",
1701 fetch_symref(symref
, &symref
, head_sha1
);
1704 /* Run extra sanity checks if delete is not forced */
1706 /* Remote HEAD must resolve to a known object */
1708 return error("Remote HEAD symrefs too deep");
1709 if (is_null_sha1(head_sha1
))
1710 return error("Unable to resolve remote HEAD");
1711 if (!has_sha1_file(head_sha1
))
1712 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1714 /* Remote branch must resolve to a known object */
1715 if (is_null_sha1(remote_ref
->old_sha1
))
1716 return error("Unable to resolve remote branch %s",
1718 if (!has_sha1_file(remote_ref
->old_sha1
))
1719 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
));
1721 /* Remote branch must be an ancestor of remote HEAD */
1722 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
1723 return error("The branch '%s' is not an ancestor "
1724 "of your current HEAD.\n"
1725 "If you are sure you want to delete it,"
1726 " run:\n\t'git http-push -D %s %s'",
1727 remote_ref
->name
, repo
->url
, pattern
);
1731 /* Send delete request */
1732 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1735 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
1736 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
1737 slot
= get_active_slot();
1738 slot
->results
= &results
;
1739 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1740 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1741 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1742 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
1743 if (start_active_slot(slot
)) {
1744 run_active_slot(slot
);
1746 if (results
.curl_result
!= CURLE_OK
)
1747 return error("DELETE request failed (%d/%ld)\n",
1748 results
.curl_result
, results
.http_code
);
1751 return error("Unable to start DELETE request");
1757 static void run_request_queue(void)
1759 #ifdef USE_CURL_MULTI
1760 is_running_queue
= 1;
1761 fill_active_slots();
1762 add_fill_function(NULL
, fill_active_slot
);
1765 finish_all_active_slots();
1766 #ifdef USE_CURL_MULTI
1767 fill_active_slots();
1769 } while (request_queue_head
&& !aborted
);
1771 #ifdef USE_CURL_MULTI
1772 is_running_queue
= 0;
1776 int main(int argc
, char **argv
)
1778 struct transfer_request
*request
;
1779 struct transfer_request
*next_request
;
1781 char **refspec
= NULL
;
1782 struct remote_lock
*ref_lock
= NULL
;
1783 struct remote_lock
*info_ref_lock
= NULL
;
1784 struct rev_info revs
;
1785 int delete_branch
= 0;
1786 int force_delete
= 0;
1787 int objects_to_send
;
1791 struct ref
*ref
, *local_refs
;
1792 struct remote
*remote
;
1793 char *rewritten_url
= NULL
;
1795 git_setup_gettext();
1797 git_extract_argv0_path(argv
[0]);
1799 repo
= xcalloc(sizeof(*repo
), 1);
1802 for (i
= 1; i
< argc
; i
++, argv
++) {
1806 if (!strcmp(arg
, "--all")) {
1807 push_all
= MATCH_REFS_ALL
;
1810 if (!strcmp(arg
, "--force")) {
1814 if (!strcmp(arg
, "--dry-run")) {
1818 if (!strcmp(arg
, "--helper-status")) {
1822 if (!strcmp(arg
, "--verbose")) {
1824 http_is_verbose
= 1;
1827 if (!strcmp(arg
, "-d")) {
1831 if (!strcmp(arg
, "-D")) {
1836 if (!strcmp(arg
, "-h"))
1837 usage(http_push_usage
);
1840 char *path
= strstr(arg
, "//");
1842 repo
->path_len
= strlen(arg
);
1844 repo
->path
= strchr(path
+2, '/');
1846 repo
->path_len
= strlen(repo
->path
);
1851 nr_refspec
= argc
- i
;
1855 #ifndef USE_CURL_MULTI
1856 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1860 usage(http_push_usage
);
1862 if (delete_branch
&& nr_refspec
!= 1)
1863 die("You must specify only one branch name when deleting a remote branch");
1865 setup_git_directory();
1867 memset(remote_dir_exists
, -1, 256);
1870 * Create a minimum remote by hand to give to http_init(),
1871 * primarily to allow it to look at the URL.
1873 remote
= xcalloc(sizeof(*remote
), 1);
1874 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
1875 remote
->url
[remote
->url_nr
++] = repo
->url
;
1878 if (repo
->url
&& repo
->url
[strlen(repo
->url
)-1] != '/') {
1879 rewritten_url
= xmalloc(strlen(repo
->url
)+2);
1880 strcpy(rewritten_url
, repo
->url
);
1881 strcat(rewritten_url
, "/");
1882 repo
->path
= rewritten_url
+ (repo
->path
- repo
->url
);
1884 repo
->url
= rewritten_url
;
1887 #ifdef USE_CURL_MULTI
1888 is_running_queue
= 0;
1891 /* Verify DAV compliance/lock support */
1892 if (!locking_available()) {
1897 sigchain_push_common(remove_locks_on_signal
);
1899 /* Check whether the remote has server info files */
1900 repo
->can_update_info_refs
= 0;
1901 repo
->has_info_refs
= remote_exists("info/refs");
1902 repo
->has_info_packs
= remote_exists("objects/info/packs");
1903 if (repo
->has_info_refs
) {
1904 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1906 repo
->can_update_info_refs
= 1;
1908 error("cannot lock existing info/refs");
1913 if (repo
->has_info_packs
)
1916 /* Get a list of all local and remote heads to validate refspecs */
1917 local_refs
= get_local_heads();
1918 fprintf(stderr
, "Fetching remote heads...\n");
1919 get_dav_remote_heads();
1920 run_request_queue();
1922 /* Remove a remote branch if -d or -D was specified */
1923 if (delete_branch
) {
1924 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1925 fprintf(stderr
, "Unable to delete remote branch %s\n",
1928 printf("error %s cannot remove\n", refspec
[0]);
1934 if (match_refs(local_refs
, &remote_refs
,
1935 nr_refspec
, (const char **) refspec
, push_all
)) {
1940 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1942 printf("error null no match\n");
1948 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1949 char old_hex
[60], *new_hex
;
1950 const char *commit_argv
[5];
1952 char *new_sha1_hex
, *old_sha1_hex
;
1957 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1958 if (delete_remote_branch(ref
->name
, 1) == -1) {
1959 error("Could not remove %s", ref
->name
);
1961 printf("error %s cannot remove\n", ref
->name
);
1964 else if (helper_status
)
1965 printf("ok %s\n", ref
->name
);
1970 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1972 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1974 printf("ok %s up to date\n", ref
->name
);
1979 !is_null_sha1(ref
->old_sha1
) &&
1981 if (!has_sha1_file(ref
->old_sha1
) ||
1982 !ref_newer(ref
->peer_ref
->new_sha1
,
1985 * We do not have the remote ref, or
1986 * we know that the remote ref is not
1987 * an ancestor of what we are trying to
1988 * push. Either way this can be losing
1989 * commits at the remote end and likely
1990 * we were not up to date to begin with.
1992 error("remote '%s' is not an ancestor of\n"
1994 "Maybe you are not up-to-date and "
1995 "need to pull first?",
1997 ref
->peer_ref
->name
);
1999 printf("error %s non-fast forward\n", ref
->name
);
2004 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2006 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2007 new_hex
= sha1_to_hex(ref
->new_sha1
);
2009 fprintf(stderr
, "updating '%s'", ref
->name
);
2010 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2011 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2012 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2015 printf("ok %s\n", ref
->name
);
2019 /* Lock remote branch ref */
2020 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2021 if (ref_lock
== NULL
) {
2022 fprintf(stderr
, "Unable to lock remote branch %s\n",
2025 printf("error %s lock error\n", ref
->name
);
2030 /* Set up revision info for this refspec */
2032 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2033 old_sha1_hex
= NULL
;
2034 commit_argv
[1] = "--objects";
2035 commit_argv
[2] = new_sha1_hex
;
2036 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
2037 old_sha1_hex
= xmalloc(42);
2038 sprintf(old_sha1_hex
, "^%s",
2039 sha1_to_hex(ref
->old_sha1
));
2040 commit_argv
[3] = old_sha1_hex
;
2043 commit_argv
[commit_argc
] = NULL
;
2044 init_revisions(&revs
, setup_git_directory());
2045 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2046 revs
.edge_hint
= 0; /* just in case */
2050 commit_argv
[1] = NULL
;
2053 /* Generate a list of objects that need to be pushed */
2055 if (prepare_revision_walk(&revs
))
2056 die("revision walk setup failed");
2057 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2058 objects_to_send
= get_delta(&revs
, ref_lock
);
2059 finish_all_active_slots();
2061 /* Push missing objects to remote, this would be a
2062 convenient time to pack them first if appropriate. */
2064 if (objects_to_send
)
2065 fprintf(stderr
, " sending %d objects\n",
2068 run_request_queue();
2070 /* Update the remote branch if all went well */
2071 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2075 fprintf(stderr
, " done\n");
2077 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
2078 unlock_remote(ref_lock
);
2082 /* Update remote server info if appropriate */
2083 if (repo
->has_info_refs
&& new_refs
) {
2084 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2085 fprintf(stderr
, "Updating remote server info\n");
2087 update_remote_info_refs(info_ref_lock
);
2089 fprintf(stderr
, "Unable to update server info\n");
2094 free(rewritten_url
);
2096 unlock_remote(info_ref_lock
);
2101 request
= request_queue_head
;
2102 while (request
!= NULL
) {
2103 next_request
= request
->next
;
2104 release_request(request
);
2105 request
= next_request
;