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
;
90 int can_update_info_refs
;
92 struct packed_git
*packs
;
93 struct remote_lock
*locks
;
96 static struct repo
*repo
;
110 struct transfer_request
{
114 struct remote_lock
*lock
;
115 struct curl_slist
*headers
;
116 struct buffer buffer
;
117 enum transfer_state state
;
118 CURLcode curl_result
;
119 char errorstr
[CURL_ERROR_SIZE
];
122 struct active_request_slot
*slot
;
123 struct transfer_request
*next
;
126 static struct transfer_request
*request_queue_head
;
132 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
140 char tmpfile_suffix
[41];
144 struct remote_lock
*next
;
147 /* Flags that control remote_ls processing */
148 #define PROCESS_FILES (1u << 0)
149 #define PROCESS_DIRS (1u << 1)
150 #define RECURSIVE (1u << 2)
152 /* Flags that remote_ls passes to callback functions */
153 #define IS_DIR (1u << 0)
155 struct remote_ls_ctx
{
157 void (*userFunc
)(struct remote_ls_ctx
*ls
);
162 struct remote_ls_ctx
*parent
;
165 /* get_dav_token_headers options */
166 enum dav_header_flag
{
167 DAV_HEADER_IF
= (1u << 0),
168 DAV_HEADER_LOCK
= (1u << 1),
169 DAV_HEADER_TIMEOUT
= (1u << 2)
172 static char *xml_entities(const char *s
)
174 struct strbuf buf
= STRBUF_INIT
;
176 size_t len
= strcspn(s
, "\"<>&");
177 strbuf_add(&buf
, s
, len
);
181 strbuf_addstr(&buf
, """);
184 strbuf_addstr(&buf
, "<");
187 strbuf_addstr(&buf
, ">");
190 strbuf_addstr(&buf
, "&");
193 return strbuf_detach(&buf
, NULL
);
197 return strbuf_detach(&buf
, NULL
);
200 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
202 struct strbuf buf
= STRBUF_INIT
;
203 struct curl_slist
*dav_headers
= NULL
;
205 if (options
& DAV_HEADER_IF
) {
206 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
207 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
210 if (options
& DAV_HEADER_LOCK
) {
211 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
212 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
215 if (options
& DAV_HEADER_TIMEOUT
) {
216 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
217 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
220 strbuf_release(&buf
);
225 static void finish_request(struct transfer_request
*request
);
226 static void release_request(struct transfer_request
*request
);
228 static void process_response(void *callback_data
)
230 struct transfer_request
*request
=
231 (struct transfer_request
*)callback_data
;
233 finish_request(request
);
236 #ifdef USE_CURL_MULTI
238 static void start_fetch_loose(struct transfer_request
*request
)
240 struct active_request_slot
*slot
;
241 struct http_object_request
*obj_req
;
243 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
244 if (obj_req
== NULL
) {
245 request
->state
= ABORTED
;
249 slot
= obj_req
->slot
;
250 slot
->callback_func
= process_response
;
251 slot
->callback_data
= request
;
252 request
->slot
= slot
;
253 request
->userData
= obj_req
;
255 /* Try to get the request started, abort the request on error */
256 request
->state
= RUN_FETCH_LOOSE
;
257 if (!start_active_slot(slot
)) {
258 fprintf(stderr
, "Unable to start GET request\n");
259 repo
->can_update_info_refs
= 0;
260 release_http_object_request(obj_req
);
261 release_request(request
);
265 static void start_mkcol(struct transfer_request
*request
)
267 char *hex
= sha1_to_hex(request
->obj
->sha1
);
268 struct active_request_slot
*slot
;
270 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
272 slot
= get_active_slot();
273 slot
->callback_func
= process_response
;
274 slot
->callback_data
= request
;
275 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
276 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
277 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
278 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
279 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
281 if (start_active_slot(slot
)) {
282 request
->slot
= slot
;
283 request
->state
= RUN_MKCOL
;
285 request
->state
= ABORTED
;
292 static void start_fetch_packed(struct transfer_request
*request
)
294 struct packed_git
*target
;
296 struct transfer_request
*check_request
= request_queue_head
;
297 struct http_pack_request
*preq
;
299 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
301 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
302 repo
->can_update_info_refs
= 0;
303 release_request(request
);
307 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
308 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
310 preq
= new_http_pack_request(target
, repo
->url
);
312 release_http_pack_request(preq
);
313 repo
->can_update_info_refs
= 0;
316 preq
->lst
= &repo
->packs
;
318 /* Make sure there isn't another open request for this pack */
319 while (check_request
) {
320 if (check_request
->state
== RUN_FETCH_PACKED
&&
321 !strcmp(check_request
->url
, preq
->url
)) {
322 release_http_pack_request(preq
);
323 release_request(request
);
326 check_request
= check_request
->next
;
329 preq
->slot
->callback_func
= process_response
;
330 preq
->slot
->callback_data
= request
;
331 request
->slot
= preq
->slot
;
332 request
->userData
= preq
;
334 /* Try to get the request started, abort the request on error */
335 request
->state
= RUN_FETCH_PACKED
;
336 if (!start_active_slot(preq
->slot
)) {
337 fprintf(stderr
, "Unable to start GET request\n");
338 release_http_pack_request(preq
);
339 repo
->can_update_info_refs
= 0;
340 release_request(request
);
344 static void start_put(struct transfer_request
*request
)
346 char *hex
= sha1_to_hex(request
->obj
->sha1
);
347 struct active_request_slot
*slot
;
348 struct strbuf buf
= STRBUF_INIT
;
349 enum object_type type
;
357 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
358 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
361 memset(&stream
, 0, sizeof(stream
));
362 deflateInit(&stream
, zlib_compression_level
);
363 size
= deflateBound(&stream
, len
+ hdrlen
);
364 strbuf_init(&request
->buffer
.buf
, size
);
365 request
->buffer
.posn
= 0;
368 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
369 stream
.avail_out
= size
;
372 stream
.next_in
= (void *)hdr
;
373 stream
.avail_in
= hdrlen
;
374 while (deflate(&stream
, 0) == Z_OK
)
377 /* Then the data itself.. */
378 stream
.next_in
= unpacked
;
379 stream
.avail_in
= len
;
380 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
385 request
->buffer
.buf
.len
= stream
.total_out
;
387 strbuf_addstr(&buf
, "Destination: ");
388 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
389 request
->dest
= strbuf_detach(&buf
, NULL
);
391 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
392 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
393 request
->url
= strbuf_detach(&buf
, NULL
);
395 slot
= get_active_slot();
396 slot
->callback_func
= process_response
;
397 slot
->callback_data
= request
;
398 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
399 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
400 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
401 #ifndef NO_CURL_IOCTL
402 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
403 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &request
->buffer
);
405 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
406 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
407 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
408 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
409 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
410 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
412 if (start_active_slot(slot
)) {
413 request
->slot
= slot
;
414 request
->state
= RUN_PUT
;
416 request
->state
= ABORTED
;
422 static void start_move(struct transfer_request
*request
)
424 struct active_request_slot
*slot
;
425 struct curl_slist
*dav_headers
= NULL
;
427 slot
= get_active_slot();
428 slot
->callback_func
= process_response
;
429 slot
->callback_data
= request
;
430 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
431 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
432 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
433 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
434 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
435 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
436 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
438 if (start_active_slot(slot
)) {
439 request
->slot
= slot
;
440 request
->state
= RUN_MOVE
;
442 request
->state
= ABORTED
;
448 static int refresh_lock(struct remote_lock
*lock
)
450 struct active_request_slot
*slot
;
451 struct slot_results results
;
452 struct curl_slist
*dav_headers
;
455 lock
->refreshing
= 1;
457 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
459 slot
= get_active_slot();
460 slot
->results
= &results
;
461 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
462 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
463 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
464 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
465 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
467 if (start_active_slot(slot
)) {
468 run_active_slot(slot
);
469 if (results
.curl_result
!= CURLE_OK
) {
470 fprintf(stderr
, "LOCK HTTP error %ld\n",
473 lock
->start_time
= time(NULL
);
478 lock
->refreshing
= 0;
479 curl_slist_free_all(dav_headers
);
484 static void check_locks(void)
486 struct remote_lock
*lock
= repo
->locks
;
487 time_t current_time
= time(NULL
);
491 time_remaining
= lock
->start_time
+ lock
->timeout
-
493 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
494 if (!refresh_lock(lock
)) {
496 "Unable to refresh lock for %s\n",
506 static void release_request(struct transfer_request
*request
)
508 struct transfer_request
*entry
= request_queue_head
;
510 if (request
== request_queue_head
) {
511 request_queue_head
= request
->next
;
513 while (entry
->next
!= NULL
&& entry
->next
!= request
)
515 if (entry
->next
== request
)
516 entry
->next
= entry
->next
->next
;
523 static void finish_request(struct transfer_request
*request
)
525 struct http_pack_request
*preq
;
526 struct http_object_request
*obj_req
;
528 request
->curl_result
= request
->slot
->curl_result
;
529 request
->http_code
= request
->slot
->http_code
;
530 request
->slot
= NULL
;
532 /* Keep locks active */
535 if (request
->headers
!= NULL
)
536 curl_slist_free_all(request
->headers
);
538 /* URL is reused for MOVE after PUT */
539 if (request
->state
!= RUN_PUT
) {
544 if (request
->state
== RUN_MKCOL
) {
545 if (request
->curl_result
== CURLE_OK
||
546 request
->http_code
== 405) {
547 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
550 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
551 sha1_to_hex(request
->obj
->sha1
),
552 request
->curl_result
, request
->http_code
);
553 request
->state
= ABORTED
;
556 } else if (request
->state
== RUN_PUT
) {
557 if (request
->curl_result
== CURLE_OK
) {
560 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
561 sha1_to_hex(request
->obj
->sha1
),
562 request
->curl_result
, request
->http_code
);
563 request
->state
= ABORTED
;
566 } else if (request
->state
== RUN_MOVE
) {
567 if (request
->curl_result
== CURLE_OK
) {
569 fprintf(stderr
, " sent %s\n",
570 sha1_to_hex(request
->obj
->sha1
));
571 request
->obj
->flags
|= REMOTE
;
572 release_request(request
);
574 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
575 sha1_to_hex(request
->obj
->sha1
),
576 request
->curl_result
, request
->http_code
);
577 request
->state
= ABORTED
;
580 } else if (request
->state
== RUN_FETCH_LOOSE
) {
581 obj_req
= (struct http_object_request
*)request
->userData
;
583 if (finish_http_object_request(obj_req
) == 0)
584 if (obj_req
->rename
== 0)
585 request
->obj
->flags
|= (LOCAL
| REMOTE
);
587 /* Try fetching packed if necessary */
588 if (request
->obj
->flags
& LOCAL
) {
589 release_http_object_request(obj_req
);
590 release_request(request
);
592 start_fetch_packed(request
);
594 } else if (request
->state
== RUN_FETCH_PACKED
) {
596 if (request
->curl_result
!= CURLE_OK
) {
597 fprintf(stderr
, "Unable to get pack file %s\n%s",
598 request
->url
, curl_errorstr
);
600 preq
= (struct http_pack_request
*)request
->userData
;
603 if (finish_http_pack_request(preq
) == 0)
605 release_http_pack_request(preq
);
609 repo
->can_update_info_refs
= 0;
610 release_request(request
);
614 #ifdef USE_CURL_MULTI
615 static int is_running_queue
;
616 static int fill_active_slot(void *unused
)
618 struct transfer_request
*request
;
620 if (aborted
|| !is_running_queue
)
623 for (request
= request_queue_head
; request
; request
= request
->next
) {
624 if (request
->state
== NEED_FETCH
) {
625 start_fetch_loose(request
);
627 } else if (pushing
&& request
->state
== NEED_PUSH
) {
628 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
631 start_mkcol(request
);
640 static void get_remote_object_list(unsigned char parent
);
642 static void add_fetch_request(struct object
*obj
)
644 struct transfer_request
*request
;
649 * Don't fetch the object if it's known to exist locally
650 * or is already in the request queue
652 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
653 get_remote_object_list(obj
->sha1
[0]);
654 if (obj
->flags
& (LOCAL
| FETCHING
))
657 obj
->flags
|= FETCHING
;
658 request
= xmalloc(sizeof(*request
));
661 request
->lock
= NULL
;
662 request
->headers
= NULL
;
663 request
->state
= NEED_FETCH
;
664 request
->next
= request_queue_head
;
665 request_queue_head
= request
;
667 #ifdef USE_CURL_MULTI
673 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
675 struct transfer_request
*request
= request_queue_head
;
676 struct packed_git
*target
;
678 /* Keep locks active */
682 * Don't push the object if it's known to exist on the remote
683 * or is already in the request queue
685 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
686 get_remote_object_list(obj
->sha1
[0]);
687 if (obj
->flags
& (REMOTE
| PUSHING
))
689 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
691 obj
->flags
|= REMOTE
;
695 obj
->flags
|= PUSHING
;
696 request
= xmalloc(sizeof(*request
));
699 request
->lock
= lock
;
700 request
->headers
= NULL
;
701 request
->state
= NEED_PUSH
;
702 request
->next
= request_queue_head
;
703 request_queue_head
= request
;
705 #ifdef USE_CURL_MULTI
713 static int fetch_indices(void)
718 fprintf(stderr
, "Getting pack list\n");
720 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
722 case HTTP_MISSING_TARGET
:
732 static void one_remote_object(const char *hex
)
734 unsigned char sha1
[20];
737 if (get_sha1_hex(hex
, sha1
) != 0)
740 obj
= lookup_object(sha1
);
742 obj
= parse_object(sha1
);
744 /* Ignore remote objects that don't exist locally */
748 obj
->flags
|= REMOTE
;
749 if (!object_list_contains(objects
, obj
))
750 object_list_insert(obj
, &objects
);
753 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
755 int *lock_flags
= (int *)ctx
->userData
;
758 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
759 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
760 (*lock_flags
& DAV_PROP_LOCKWR
)) {
761 *lock_flags
|= DAV_LOCK_OK
;
763 *lock_flags
&= DAV_LOCK_OK
;
764 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
765 *lock_flags
|= DAV_PROP_LOCKWR
;
766 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
767 *lock_flags
|= DAV_PROP_LOCKEX
;
772 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
774 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
776 unsigned char lock_token_sha1
[20];
778 if (tag_closed
&& ctx
->cdata
) {
779 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
780 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
781 strcpy(lock
->owner
, ctx
->cdata
);
782 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
783 if (!prefixcmp(ctx
->cdata
, "Second-"))
785 strtol(ctx
->cdata
+ 7, NULL
, 10);
786 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
787 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
788 strcpy(lock
->token
, ctx
->cdata
);
790 git_SHA1_Init(&sha_ctx
);
791 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
792 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
794 lock
->tmpfile_suffix
[0] = '_';
795 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
800 static void one_remote_ref(const char *refname
);
803 xml_start_tag(void *userData
, const char *name
, const char **atts
)
805 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
806 const char *c
= strchr(name
, ':');
814 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
816 if (new_len
> ctx
->len
) {
817 ctx
->name
= xrealloc(ctx
->name
, new_len
);
820 strcat(ctx
->name
, ".");
821 strcat(ctx
->name
, c
);
826 ctx
->userFunc(ctx
, 0);
830 xml_end_tag(void *userData
, const char *name
)
832 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
833 const char *c
= strchr(name
, ':');
836 ctx
->userFunc(ctx
, 1);
843 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
848 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
850 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
852 ctx
->cdata
= xmemdupz(s
, len
);
855 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
857 struct active_request_slot
*slot
;
858 struct slot_results results
;
859 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
860 struct strbuf in_buffer
= STRBUF_INIT
;
863 char timeout_header
[25];
864 struct remote_lock
*lock
= NULL
;
865 struct curl_slist
*dav_headers
= NULL
;
869 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
870 sprintf(url
, "%s%s", repo
->url
, path
);
872 /* Make sure leading directories exist for the remote ref */
873 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
875 char saved_character
= ep
[1];
877 slot
= get_active_slot();
878 slot
->results
= &results
;
879 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
880 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
881 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
882 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
883 if (start_active_slot(slot
)) {
884 run_active_slot(slot
);
885 if (results
.curl_result
!= CURLE_OK
&&
886 results
.http_code
!= 405) {
888 "Unable to create branch path %s\n",
894 fprintf(stderr
, "Unable to start MKCOL request\n");
898 ep
[1] = saved_character
;
899 ep
= strchr(ep
+ 1, '/');
902 escaped
= xml_entities(git_default_email
);
903 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
906 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
907 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
908 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
910 slot
= get_active_slot();
911 slot
->results
= &results
;
912 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
913 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
914 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
915 #ifndef NO_CURL_IOCTL
916 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
917 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
919 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
920 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
921 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
922 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
923 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
924 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
926 lock
= xcalloc(1, sizeof(*lock
));
929 if (start_active_slot(slot
)) {
930 run_active_slot(slot
);
931 if (results
.curl_result
== CURLE_OK
) {
932 XML_Parser parser
= XML_ParserCreate(NULL
);
933 enum XML_Status result
;
934 ctx
.name
= xcalloc(10, 1);
937 ctx
.userFunc
= handle_new_lock_ctx
;
939 XML_SetUserData(parser
, &ctx
);
940 XML_SetElementHandler(parser
, xml_start_tag
,
942 XML_SetCharacterDataHandler(parser
, xml_cdata
);
943 result
= XML_Parse(parser
, in_buffer
.buf
,
946 if (result
!= XML_STATUS_OK
) {
947 fprintf(stderr
, "XML error: %s\n",
949 XML_GetErrorCode(parser
)));
952 XML_ParserFree(parser
);
955 fprintf(stderr
, "Unable to start LOCK request\n");
958 curl_slist_free_all(dav_headers
);
959 strbuf_release(&out_buffer
.buf
);
960 strbuf_release(&in_buffer
);
962 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
970 lock
->start_time
= time(NULL
);
971 lock
->next
= repo
->locks
;
978 static int unlock_remote(struct remote_lock
*lock
)
980 struct active_request_slot
*slot
;
981 struct slot_results results
;
982 struct remote_lock
*prev
= repo
->locks
;
983 struct curl_slist
*dav_headers
;
986 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
988 slot
= get_active_slot();
989 slot
->results
= &results
;
990 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
991 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
992 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
993 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
995 if (start_active_slot(slot
)) {
996 run_active_slot(slot
);
997 if (results
.curl_result
== CURLE_OK
)
1000 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1003 fprintf(stderr
, "Unable to start UNLOCK request\n");
1006 curl_slist_free_all(dav_headers
);
1008 if (repo
->locks
== lock
) {
1009 repo
->locks
= lock
->next
;
1011 while (prev
&& prev
->next
!= lock
)
1014 prev
->next
= prev
->next
->next
;
1025 static void remove_locks(void)
1027 struct remote_lock
*lock
= repo
->locks
;
1029 fprintf(stderr
, "Removing remote locks...\n");
1031 struct remote_lock
*next
= lock
->next
;
1032 unlock_remote(lock
);
1037 static void remove_locks_on_signal(int signo
)
1040 sigchain_pop(signo
);
1044 static void remote_ls(const char *path
, int flags
,
1045 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1048 static void process_ls_object(struct remote_ls_ctx
*ls
)
1050 unsigned int *parent
= (unsigned int *)ls
->userData
;
1051 char *path
= ls
->dentry_name
;
1054 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1055 remote_dir_exists
[*parent
] = 1;
1059 if (strlen(path
) != 49)
1062 obj_hex
= xmalloc(strlen(path
));
1063 /* NB: path is not null-terminated, can not use strlcpy here */
1064 memcpy(obj_hex
, path
, 2);
1065 strcpy(obj_hex
+ 2, path
+ 3);
1066 one_remote_object(obj_hex
);
1070 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1072 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1073 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1077 if (!(ls
->dentry_flags
& IS_DIR
))
1078 one_remote_ref(ls
->dentry_name
);
1081 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1083 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1086 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1087 if (ls
->dentry_flags
& IS_DIR
) {
1089 /* ensure collection names end with slash */
1090 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1092 if (ls
->flags
& PROCESS_DIRS
) {
1095 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1096 ls
->flags
& RECURSIVE
) {
1097 remote_ls(ls
->dentry_name
,
1102 } else if (ls
->flags
& PROCESS_FILES
) {
1105 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1106 char *path
= ctx
->cdata
;
1107 if (*ctx
->cdata
== 'h') {
1108 path
= strstr(path
, "//");
1110 path
= strchr(path
+2, '/');
1114 const char *url
= repo
->url
;
1117 if (strncmp(path
, url
, repo
->path_len
))
1118 error("Parsed path '%s' does not match url: '%s'\n",
1121 path
+= repo
->path_len
;
1122 ls
->dentry_name
= xstrdup(path
);
1125 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1126 ls
->dentry_flags
|= IS_DIR
;
1128 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1129 free(ls
->dentry_name
);
1130 ls
->dentry_name
= NULL
;
1131 ls
->dentry_flags
= 0;
1136 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1137 * should _only_ heed the information from that file, instead of trying to
1138 * determine the refs from the remote file system (badly: it does not even
1139 * know about packed-refs).
1141 static void remote_ls(const char *path
, int flags
,
1142 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1145 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1146 struct active_request_slot
*slot
;
1147 struct slot_results results
;
1148 struct strbuf in_buffer
= STRBUF_INIT
;
1149 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1150 struct curl_slist
*dav_headers
= NULL
;
1152 struct remote_ls_ctx ls
;
1155 ls
.path
= xstrdup(path
);
1156 ls
.dentry_name
= NULL
;
1157 ls
.dentry_flags
= 0;
1158 ls
.userData
= userData
;
1159 ls
.userFunc
= userFunc
;
1161 sprintf(url
, "%s%s", repo
->url
, path
);
1163 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1165 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1166 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1168 slot
= get_active_slot();
1169 slot
->results
= &results
;
1170 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1171 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1172 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1173 #ifndef NO_CURL_IOCTL
1174 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1175 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1177 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1178 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1179 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1180 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1181 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1182 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1184 if (start_active_slot(slot
)) {
1185 run_active_slot(slot
);
1186 if (results
.curl_result
== CURLE_OK
) {
1187 XML_Parser parser
= XML_ParserCreate(NULL
);
1188 enum XML_Status result
;
1189 ctx
.name
= xcalloc(10, 1);
1192 ctx
.userFunc
= handle_remote_ls_ctx
;
1194 XML_SetUserData(parser
, &ctx
);
1195 XML_SetElementHandler(parser
, xml_start_tag
,
1197 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1198 result
= XML_Parse(parser
, in_buffer
.buf
,
1202 if (result
!= XML_STATUS_OK
) {
1203 fprintf(stderr
, "XML error: %s\n",
1205 XML_GetErrorCode(parser
)));
1207 XML_ParserFree(parser
);
1210 fprintf(stderr
, "Unable to start PROPFIND request\n");
1215 strbuf_release(&out_buffer
.buf
);
1216 strbuf_release(&in_buffer
);
1217 curl_slist_free_all(dav_headers
);
1220 static void get_remote_object_list(unsigned char parent
)
1222 char path
[] = "objects/XX/";
1223 static const char hex
[] = "0123456789abcdef";
1224 unsigned int val
= parent
;
1226 path
[8] = hex
[val
>> 4];
1227 path
[9] = hex
[val
& 0xf];
1228 remote_dir_exists
[val
] = 0;
1229 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1230 process_ls_object
, &val
);
1233 static int locking_available(void)
1235 struct active_request_slot
*slot
;
1236 struct slot_results results
;
1237 struct strbuf in_buffer
= STRBUF_INIT
;
1238 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1239 struct curl_slist
*dav_headers
= NULL
;
1244 escaped
= xml_entities(repo
->url
);
1245 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1248 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1249 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1251 slot
= get_active_slot();
1252 slot
->results
= &results
;
1253 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1254 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1255 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1256 #ifndef NO_CURL_IOCTL
1257 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1260 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1261 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1262 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1263 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1264 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1265 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1267 if (start_active_slot(slot
)) {
1268 run_active_slot(slot
);
1269 if (results
.curl_result
== CURLE_OK
) {
1270 XML_Parser parser
= XML_ParserCreate(NULL
);
1271 enum XML_Status result
;
1272 ctx
.name
= xcalloc(10, 1);
1275 ctx
.userFunc
= handle_lockprop_ctx
;
1276 ctx
.userData
= &lock_flags
;
1277 XML_SetUserData(parser
, &ctx
);
1278 XML_SetElementHandler(parser
, xml_start_tag
,
1280 result
= XML_Parse(parser
, in_buffer
.buf
,
1284 if (result
!= XML_STATUS_OK
) {
1285 fprintf(stderr
, "XML error: %s\n",
1287 XML_GetErrorCode(parser
)));
1290 XML_ParserFree(parser
);
1292 error("no DAV locking support on %s",
1296 error("Cannot access URL %s, return code %d",
1297 repo
->url
, results
.curl_result
);
1301 error("Unable to start PROPFIND request on %s", repo
->url
);
1304 strbuf_release(&out_buffer
.buf
);
1305 strbuf_release(&in_buffer
);
1306 curl_slist_free_all(dav_headers
);
1311 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1313 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1317 return &entry
->next
;
1320 static struct object_list
**process_blob(struct blob
*blob
,
1321 struct object_list
**p
,
1322 struct name_path
*path
,
1325 struct object
*obj
= &blob
->object
;
1327 obj
->flags
|= LOCAL
;
1329 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1333 return add_one_object(obj
, p
);
1336 static struct object_list
**process_tree(struct tree
*tree
,
1337 struct object_list
**p
,
1338 struct name_path
*path
,
1341 struct object
*obj
= &tree
->object
;
1342 struct tree_desc desc
;
1343 struct name_entry entry
;
1344 struct name_path me
;
1346 obj
->flags
|= LOCAL
;
1348 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1350 if (parse_tree(tree
) < 0)
1351 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1354 name
= xstrdup(name
);
1355 p
= add_one_object(obj
, p
);
1358 me
.elem_len
= strlen(name
);
1360 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1362 while (tree_entry(&desc
, &entry
))
1363 switch (object_type(entry
.mode
)) {
1365 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1368 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1371 /* Subproject commit - not in this repository */
1376 tree
->buffer
= NULL
;
1380 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1383 struct commit
*commit
;
1384 struct object_list
**p
= &objects
;
1387 while ((commit
= get_revision(revs
)) != NULL
) {
1388 p
= process_tree(commit
->tree
, p
, NULL
, "");
1389 commit
->object
.flags
|= LOCAL
;
1390 if (!(commit
->object
.flags
& UNINTERESTING
))
1391 count
+= add_send_request(&commit
->object
, lock
);
1394 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1395 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1396 struct object
*obj
= entry
->item
;
1397 const char *name
= entry
->name
;
1399 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1401 if (obj
->type
== OBJ_TAG
) {
1403 p
= add_one_object(obj
, p
);
1406 if (obj
->type
== OBJ_TREE
) {
1407 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1410 if (obj
->type
== OBJ_BLOB
) {
1411 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1414 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1418 if (!(objects
->item
->flags
& UNINTERESTING
))
1419 count
+= add_send_request(objects
->item
, lock
);
1420 objects
= objects
->next
;
1426 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1428 struct active_request_slot
*slot
;
1429 struct slot_results results
;
1430 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1431 struct curl_slist
*dav_headers
;
1433 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1435 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1437 slot
= get_active_slot();
1438 slot
->results
= &results
;
1439 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1440 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1441 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1442 #ifndef NO_CURL_IOCTL
1443 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1444 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1446 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1447 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1448 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1449 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1450 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1451 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1453 if (start_active_slot(slot
)) {
1454 run_active_slot(slot
);
1455 strbuf_release(&out_buffer
.buf
);
1456 if (results
.curl_result
!= CURLE_OK
) {
1458 "PUT error: curl result=%d, HTTP code=%ld\n",
1459 results
.curl_result
, results
.http_code
);
1460 /* We should attempt recovery? */
1464 strbuf_release(&out_buffer
.buf
);
1465 fprintf(stderr
, "Unable to start PUT request\n");
1472 static struct ref
*remote_refs
;
1474 static void one_remote_ref(const char *refname
)
1479 ref
= alloc_ref(refname
);
1481 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1483 "Unable to fetch ref %s from %s\n",
1484 refname
, repo
->url
);
1490 * Fetch a copy of the object if it doesn't exist locally - it
1491 * may be required for updating server info later.
1493 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1494 obj
= lookup_unknown_object(ref
->old_sha1
);
1496 fprintf(stderr
, " fetch %s for %s\n",
1497 sha1_to_hex(ref
->old_sha1
), refname
);
1498 add_fetch_request(obj
);
1502 ref
->next
= remote_refs
;
1506 static void get_dav_remote_heads(void)
1508 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1511 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1513 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1519 ref
= alloc_ref(ls
->dentry_name
);
1521 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1523 "Unable to fetch ref %s from %s\n",
1524 ls
->dentry_name
, repo
->url
);
1530 o
= parse_object(ref
->old_sha1
);
1533 "Unable to parse object %s for remote ref %s\n",
1534 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1540 len
= strlen(ls
->dentry_name
) + 42;
1541 ref_info
= xcalloc(len
+ 1, 1);
1542 sprintf(ref_info
, "%s %s\n",
1543 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1544 fwrite_buffer(ref_info
, 1, len
, buf
);
1547 if (o
->type
== OBJ_TAG
) {
1548 o
= deref_tag(o
, ls
->dentry_name
, 0);
1550 len
= strlen(ls
->dentry_name
) + 45;
1551 ref_info
= xcalloc(len
+ 1, 1);
1552 sprintf(ref_info
, "%s %s^{}\n",
1553 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1554 fwrite_buffer(ref_info
, 1, len
, buf
);
1561 static void update_remote_info_refs(struct remote_lock
*lock
)
1563 struct buffer buffer
= { STRBUF_INIT
, 0 };
1564 struct active_request_slot
*slot
;
1565 struct slot_results results
;
1566 struct curl_slist
*dav_headers
;
1568 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1569 add_remote_info_ref
, &buffer
.buf
);
1571 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1573 slot
= get_active_slot();
1574 slot
->results
= &results
;
1575 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1576 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1577 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1578 #ifndef NO_CURL_IOCTL
1579 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1580 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1582 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1583 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1584 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1585 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1586 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1587 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1589 if (start_active_slot(slot
)) {
1590 run_active_slot(slot
);
1591 if (results
.curl_result
!= CURLE_OK
) {
1593 "PUT error: curl result=%d, HTTP code=%ld\n",
1594 results
.curl_result
, results
.http_code
);
1598 strbuf_release(&buffer
.buf
);
1601 static int remote_exists(const char *path
)
1603 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1606 sprintf(url
, "%s%s", repo
->url
, path
);
1608 switch (http_get_strbuf(url
, NULL
, 0)) {
1612 case HTTP_MISSING_TARGET
:
1616 http_error(url
, HTTP_ERROR
);
1624 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1627 struct strbuf buffer
= STRBUF_INIT
;
1629 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1630 sprintf(url
, "%s%s", repo
->url
, path
);
1632 if (http_get_strbuf(url
, &buffer
, 0) != HTTP_OK
)
1633 die("Couldn't get %s for remote symref\n%s", url
,
1641 if (buffer
.len
== 0)
1644 /* If it's a symref, set the refname; otherwise try for a sha1 */
1645 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
1646 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
1648 get_sha1_hex(buffer
.buf
, sha1
);
1651 strbuf_release(&buffer
);
1654 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
1656 struct commit
*head
= lookup_commit(head_sha1
);
1657 struct commit
*branch
= lookup_commit(branch_sha1
);
1658 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
1660 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
1663 static int delete_remote_branch(const char *pattern
, int force
)
1665 struct ref
*refs
= remote_refs
;
1666 struct ref
*remote_ref
= NULL
;
1667 unsigned char head_sha1
[20];
1668 char *symref
= NULL
;
1670 int patlen
= strlen(pattern
);
1672 struct active_request_slot
*slot
;
1673 struct slot_results results
;
1676 /* Find the remote branch(es) matching the specified branch name */
1677 for (match
= 0; refs
; refs
= refs
->next
) {
1678 char *name
= refs
->name
;
1679 int namelen
= strlen(name
);
1680 if (namelen
< patlen
||
1681 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1683 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1689 return error("No remote branch matches %s", pattern
);
1691 return error("More than one remote branch matches %s",
1695 * Remote HEAD must be a symref (not exactly foolproof; a remote
1696 * symlink to a symref will look like a symref)
1698 fetch_symref("HEAD", &symref
, head_sha1
);
1700 return error("Remote HEAD is not a symref");
1702 /* Remote branch must not be the remote HEAD */
1703 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
1704 if (!strcmp(remote_ref
->name
, symref
))
1705 return error("Remote branch %s is the current HEAD",
1707 fetch_symref(symref
, &symref
, head_sha1
);
1710 /* Run extra sanity checks if delete is not forced */
1712 /* Remote HEAD must resolve to a known object */
1714 return error("Remote HEAD symrefs too deep");
1715 if (is_null_sha1(head_sha1
))
1716 return error("Unable to resolve remote HEAD");
1717 if (!has_sha1_file(head_sha1
))
1718 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1720 /* Remote branch must resolve to a known object */
1721 if (is_null_sha1(remote_ref
->old_sha1
))
1722 return error("Unable to resolve remote branch %s",
1724 if (!has_sha1_file(remote_ref
->old_sha1
))
1725 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
));
1727 /* Remote branch must be an ancestor of remote HEAD */
1728 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
1729 return error("The branch '%s' is not an ancestor "
1730 "of your current HEAD.\n"
1731 "If you are sure you want to delete it,"
1732 " run:\n\t'git http-push -D %s %s'",
1733 remote_ref
->name
, repo
->url
, pattern
);
1737 /* Send delete request */
1738 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1741 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
1742 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
1743 slot
= get_active_slot();
1744 slot
->results
= &results
;
1745 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1746 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1747 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1748 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
1749 if (start_active_slot(slot
)) {
1750 run_active_slot(slot
);
1752 if (results
.curl_result
!= CURLE_OK
)
1753 return error("DELETE request failed (%d/%ld)\n",
1754 results
.curl_result
, results
.http_code
);
1757 return error("Unable to start DELETE request");
1763 static void run_request_queue(void)
1765 #ifdef USE_CURL_MULTI
1766 is_running_queue
= 1;
1767 fill_active_slots();
1768 add_fill_function(NULL
, fill_active_slot
);
1771 finish_all_active_slots();
1772 #ifdef USE_CURL_MULTI
1773 fill_active_slots();
1775 } while (request_queue_head
&& !aborted
);
1777 #ifdef USE_CURL_MULTI
1778 is_running_queue
= 0;
1782 int main(int argc
, char **argv
)
1784 struct transfer_request
*request
;
1785 struct transfer_request
*next_request
;
1787 char **refspec
= NULL
;
1788 struct remote_lock
*ref_lock
= NULL
;
1789 struct remote_lock
*info_ref_lock
= NULL
;
1790 struct rev_info revs
;
1791 int delete_branch
= 0;
1792 int force_delete
= 0;
1793 int objects_to_send
;
1797 struct ref
*ref
, *local_refs
;
1798 struct remote
*remote
;
1800 git_extract_argv0_path(argv
[0]);
1802 repo
= xcalloc(sizeof(*repo
), 1);
1805 for (i
= 1; i
< argc
; i
++, argv
++) {
1809 if (!strcmp(arg
, "--all")) {
1810 push_all
= MATCH_REFS_ALL
;
1813 if (!strcmp(arg
, "--force")) {
1817 if (!strcmp(arg
, "--dry-run")) {
1821 if (!strcmp(arg
, "--helper-status")) {
1825 if (!strcmp(arg
, "--verbose")) {
1827 http_is_verbose
= 1;
1830 if (!strcmp(arg
, "-d")) {
1834 if (!strcmp(arg
, "-D")) {
1839 if (!strcmp(arg
, "-h"))
1840 usage(http_push_usage
);
1843 char *path
= strstr(arg
, "//");
1844 str_end_url_with_slash(arg
, &repo
->url
);
1845 repo
->path_len
= strlen(repo
->url
);
1847 repo
->path
= strchr(path
+2, '/');
1849 repo
->path_len
= strlen(repo
->path
);
1854 nr_refspec
= argc
- i
;
1858 #ifndef USE_CURL_MULTI
1859 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1863 usage(http_push_usage
);
1865 if (delete_branch
&& nr_refspec
!= 1)
1866 die("You must specify only one branch name when deleting a remote branch");
1868 setup_git_directory();
1870 memset(remote_dir_exists
, -1, 256);
1873 * Create a minimum remote by hand to give to http_init(),
1874 * primarily to allow it to look at the URL.
1876 remote
= xcalloc(sizeof(*remote
), 1);
1877 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
1878 remote
->url
[remote
->url_nr
++] = repo
->url
;
1881 #ifdef USE_CURL_MULTI
1882 is_running_queue
= 0;
1885 /* Verify DAV compliance/lock support */
1886 if (!locking_available()) {
1891 sigchain_push_common(remove_locks_on_signal
);
1893 /* Check whether the remote has server info files */
1894 repo
->can_update_info_refs
= 0;
1895 repo
->has_info_refs
= remote_exists("info/refs");
1896 repo
->has_info_packs
= remote_exists("objects/info/packs");
1897 if (repo
->has_info_refs
) {
1898 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1900 repo
->can_update_info_refs
= 1;
1902 error("cannot lock existing info/refs");
1907 if (repo
->has_info_packs
)
1910 /* Get a list of all local and remote heads to validate refspecs */
1911 local_refs
= get_local_heads();
1912 fprintf(stderr
, "Fetching remote heads...\n");
1913 get_dav_remote_heads();
1914 run_request_queue();
1916 /* Remove a remote branch if -d or -D was specified */
1917 if (delete_branch
) {
1918 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1919 fprintf(stderr
, "Unable to delete remote branch %s\n",
1922 printf("error %s cannot remove\n", refspec
[0]);
1928 if (match_refs(local_refs
, &remote_refs
,
1929 nr_refspec
, (const char **) refspec
, push_all
)) {
1934 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1936 printf("error null no match\n");
1942 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1943 char old_hex
[60], *new_hex
;
1944 const char *commit_argv
[5];
1946 char *new_sha1_hex
, *old_sha1_hex
;
1951 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1952 if (delete_remote_branch(ref
->name
, 1) == -1) {
1953 error("Could not remove %s", ref
->name
);
1955 printf("error %s cannot remove\n", ref
->name
);
1958 else if (helper_status
)
1959 printf("ok %s\n", ref
->name
);
1964 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1966 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1968 printf("ok %s up to date\n", ref
->name
);
1973 !is_null_sha1(ref
->old_sha1
) &&
1975 if (!has_sha1_file(ref
->old_sha1
) ||
1976 !ref_newer(ref
->peer_ref
->new_sha1
,
1979 * We do not have the remote ref, or
1980 * we know that the remote ref is not
1981 * an ancestor of what we are trying to
1982 * push. Either way this can be losing
1983 * commits at the remote end and likely
1984 * we were not up to date to begin with.
1986 error("remote '%s' is not an ancestor of\n"
1988 "Maybe you are not up-to-date and "
1989 "need to pull first?",
1991 ref
->peer_ref
->name
);
1993 printf("error %s non-fast forward\n", ref
->name
);
1998 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2000 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2001 new_hex
= sha1_to_hex(ref
->new_sha1
);
2003 fprintf(stderr
, "updating '%s'", ref
->name
);
2004 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2005 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2006 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2009 printf("ok %s\n", ref
->name
);
2013 /* Lock remote branch ref */
2014 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2015 if (ref_lock
== NULL
) {
2016 fprintf(stderr
, "Unable to lock remote branch %s\n",
2019 printf("error %s lock error\n", ref
->name
);
2024 /* Set up revision info for this refspec */
2026 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2027 old_sha1_hex
= NULL
;
2028 commit_argv
[1] = "--objects";
2029 commit_argv
[2] = new_sha1_hex
;
2030 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
2031 old_sha1_hex
= xmalloc(42);
2032 sprintf(old_sha1_hex
, "^%s",
2033 sha1_to_hex(ref
->old_sha1
));
2034 commit_argv
[3] = old_sha1_hex
;
2037 commit_argv
[commit_argc
] = NULL
;
2038 init_revisions(&revs
, setup_git_directory());
2039 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2040 revs
.edge_hint
= 0; /* just in case */
2044 commit_argv
[1] = NULL
;
2047 /* Generate a list of objects that need to be pushed */
2049 if (prepare_revision_walk(&revs
))
2050 die("revision walk setup failed");
2051 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2052 objects_to_send
= get_delta(&revs
, ref_lock
);
2053 finish_all_active_slots();
2055 /* Push missing objects to remote, this would be a
2056 convenient time to pack them first if appropriate. */
2058 if (objects_to_send
)
2059 fprintf(stderr
, " sending %d objects\n",
2062 run_request_queue();
2064 /* Update the remote branch if all went well */
2065 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2069 fprintf(stderr
, " done\n");
2071 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
2072 unlock_remote(ref_lock
);
2076 /* Update remote server info if appropriate */
2077 if (repo
->has_info_refs
&& new_refs
) {
2078 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2079 fprintf(stderr
, "Updating remote server info\n");
2081 update_remote_info_refs(info_ref_lock
);
2083 fprintf(stderr
, "Unable to update server info\n");
2089 unlock_remote(info_ref_lock
);
2094 request
= request_queue_head
;
2095 while (request
!= NULL
) {
2096 next_request
= request
->next
;
2097 release_request(request
);
2098 request
= next_request
;