11 #include "list-objects.h"
14 #ifdef EXPAT_NEEDS_XMLPARSE_H
20 static const char http_push_usage
[] =
21 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
28 #define XML_STATUS_OK 1
29 #define XML_STATUS_ERROR 0
32 #define PREV_BUF_SIZE 4096
35 #define DAV_LOCK "LOCK"
36 #define DAV_MKCOL "MKCOL"
37 #define DAV_MOVE "MOVE"
38 #define DAV_PROPFIND "PROPFIND"
40 #define DAV_UNLOCK "UNLOCK"
41 #define DAV_DELETE "DELETE"
44 #define DAV_PROP_LOCKWR (1u << 0)
45 #define DAV_PROP_LOCKEX (1u << 1)
46 #define DAV_LOCK_OK (1u << 2)
48 /* DAV XML properties */
49 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
50 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
51 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
52 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
53 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
54 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
55 #define DAV_PROPFIND_RESP ".multistatus.response"
56 #define DAV_PROPFIND_NAME ".multistatus.response.href"
57 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
59 /* DAV request body templates */
60 #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>"
61 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
62 #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>"
65 #define LOCK_REFRESH 30
67 /* bits #0-15 in revision.h */
69 #define LOCAL (1u<<16)
70 #define REMOTE (1u<<17)
71 #define FETCHING (1u<<18)
72 #define PUSHING (1u<<19)
74 /* We allow "recursive" symbolic refs. Only within reason, though */
79 static signed char remote_dir_exists
[256];
81 static int push_verbosely
;
82 static int push_all
= MATCH_REFS_NONE
;
85 static int helper_status
;
87 static struct object_list
*objects
;
94 int can_update_info_refs
;
96 struct packed_git
*packs
;
97 struct remote_lock
*locks
;
100 static struct repo
*repo
;
102 enum transfer_state
{
114 struct transfer_request
{
118 struct remote_lock
*lock
;
119 struct curl_slist
*headers
;
120 struct buffer buffer
;
121 enum transfer_state state
;
122 CURLcode curl_result
;
123 char errorstr
[CURL_ERROR_SIZE
];
126 struct active_request_slot
*slot
;
127 struct transfer_request
*next
;
130 static struct transfer_request
*request_queue_head
;
136 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
144 char tmpfile_suffix
[41];
148 struct remote_lock
*next
;
151 /* Flags that control remote_ls processing */
152 #define PROCESS_FILES (1u << 0)
153 #define PROCESS_DIRS (1u << 1)
154 #define RECURSIVE (1u << 2)
156 /* Flags that remote_ls passes to callback functions */
157 #define IS_DIR (1u << 0)
159 struct remote_ls_ctx
{
161 void (*userFunc
)(struct remote_ls_ctx
*ls
);
166 struct remote_ls_ctx
*parent
;
169 /* get_dav_token_headers options */
170 enum dav_header_flag
{
171 DAV_HEADER_IF
= (1u << 0),
172 DAV_HEADER_LOCK
= (1u << 1),
173 DAV_HEADER_TIMEOUT
= (1u << 2)
176 static char *xml_entities(const char *s
)
178 struct strbuf buf
= STRBUF_INIT
;
179 strbuf_addstr_xml_quoted(&buf
, s
);
180 return strbuf_detach(&buf
, NULL
);
183 static void curl_setup_http_get(CURL
*curl
, const char *url
,
184 const char *custom_req
)
186 curl_easy_setopt(curl
, CURLOPT_HTTPGET
, 1);
187 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
188 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
189 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
192 static void curl_setup_http(CURL
*curl
, const char *url
,
193 const char *custom_req
, struct buffer
*buffer
,
194 curl_write_callback write_fn
)
196 curl_easy_setopt(curl
, CURLOPT_PUT
, 1);
197 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
198 curl_easy_setopt(curl
, CURLOPT_INFILE
, buffer
);
199 curl_easy_setopt(curl
, CURLOPT_INFILESIZE
, buffer
->buf
.len
);
200 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
201 #ifndef NO_CURL_IOCTL
202 curl_easy_setopt(curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
203 curl_easy_setopt(curl
, CURLOPT_IOCTLDATA
, &buffer
);
205 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_fn
);
206 curl_easy_setopt(curl
, CURLOPT_NOBODY
, 0);
207 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, custom_req
);
208 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1);
211 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
213 struct strbuf buf
= STRBUF_INIT
;
214 struct curl_slist
*dav_headers
= NULL
;
216 if (options
& DAV_HEADER_IF
) {
217 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
218 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
221 if (options
& DAV_HEADER_LOCK
) {
222 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
223 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
226 if (options
& DAV_HEADER_TIMEOUT
) {
227 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
228 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
231 strbuf_release(&buf
);
236 static void finish_request(struct transfer_request
*request
);
237 static void release_request(struct transfer_request
*request
);
239 static void process_response(void *callback_data
)
241 struct transfer_request
*request
=
242 (struct transfer_request
*)callback_data
;
244 finish_request(request
);
247 #ifdef USE_CURL_MULTI
249 static void start_fetch_loose(struct transfer_request
*request
)
251 struct active_request_slot
*slot
;
252 struct http_object_request
*obj_req
;
254 obj_req
= new_http_object_request(repo
->url
, request
->obj
->sha1
);
255 if (obj_req
== NULL
) {
256 request
->state
= ABORTED
;
260 slot
= obj_req
->slot
;
261 slot
->callback_func
= process_response
;
262 slot
->callback_data
= request
;
263 request
->slot
= slot
;
264 request
->userData
= obj_req
;
266 /* Try to get the request started, abort the request on error */
267 request
->state
= RUN_FETCH_LOOSE
;
268 if (!start_active_slot(slot
)) {
269 fprintf(stderr
, "Unable to start GET request\n");
270 repo
->can_update_info_refs
= 0;
271 release_http_object_request(obj_req
);
272 release_request(request
);
276 static void start_mkcol(struct transfer_request
*request
)
278 char *hex
= sha1_to_hex(request
->obj
->sha1
);
279 struct active_request_slot
*slot
;
281 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
283 slot
= get_active_slot();
284 slot
->callback_func
= process_response
;
285 slot
->callback_data
= request
;
286 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MKCOL
);
287 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
289 if (start_active_slot(slot
)) {
290 request
->slot
= slot
;
291 request
->state
= RUN_MKCOL
;
293 request
->state
= ABORTED
;
300 static void start_fetch_packed(struct transfer_request
*request
)
302 struct packed_git
*target
;
304 struct transfer_request
*check_request
= request_queue_head
;
305 struct http_pack_request
*preq
;
307 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
309 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
310 repo
->can_update_info_refs
= 0;
311 release_request(request
);
315 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
316 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
318 preq
= new_http_pack_request(target
, repo
->url
);
320 release_http_pack_request(preq
);
321 repo
->can_update_info_refs
= 0;
324 preq
->lst
= &repo
->packs
;
326 /* Make sure there isn't another open request for this pack */
327 while (check_request
) {
328 if (check_request
->state
== RUN_FETCH_PACKED
&&
329 !strcmp(check_request
->url
, preq
->url
)) {
330 release_http_pack_request(preq
);
331 release_request(request
);
334 check_request
= check_request
->next
;
337 preq
->slot
->callback_func
= process_response
;
338 preq
->slot
->callback_data
= request
;
339 request
->slot
= preq
->slot
;
340 request
->userData
= preq
;
342 /* Try to get the request started, abort the request on error */
343 request
->state
= RUN_FETCH_PACKED
;
344 if (!start_active_slot(preq
->slot
)) {
345 fprintf(stderr
, "Unable to start GET request\n");
346 release_http_pack_request(preq
);
347 repo
->can_update_info_refs
= 0;
348 release_request(request
);
352 static void start_put(struct transfer_request
*request
)
354 char *hex
= sha1_to_hex(request
->obj
->sha1
);
355 struct active_request_slot
*slot
;
356 struct strbuf buf
= STRBUF_INIT
;
357 enum object_type type
;
365 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
366 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
369 memset(&stream
, 0, sizeof(stream
));
370 git_deflate_init(&stream
, zlib_compression_level
);
371 size
= git_deflate_bound(&stream
, len
+ hdrlen
);
372 strbuf_init(&request
->buffer
.buf
, size
);
373 request
->buffer
.posn
= 0;
376 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
377 stream
.avail_out
= size
;
380 stream
.next_in
= (void *)hdr
;
381 stream
.avail_in
= hdrlen
;
382 while (git_deflate(&stream
, 0) == Z_OK
)
385 /* Then the data itself.. */
386 stream
.next_in
= unpacked
;
387 stream
.avail_in
= len
;
388 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
390 git_deflate_end(&stream
);
393 request
->buffer
.buf
.len
= stream
.total_out
;
395 strbuf_addstr(&buf
, "Destination: ");
396 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
397 request
->dest
= strbuf_detach(&buf
, NULL
);
399 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
400 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
401 request
->url
= strbuf_detach(&buf
, NULL
);
403 slot
= get_active_slot();
404 slot
->callback_func
= process_response
;
405 slot
->callback_data
= request
;
406 curl_setup_http(slot
->curl
, request
->url
, DAV_PUT
,
407 &request
->buffer
, fwrite_null
);
409 if (start_active_slot(slot
)) {
410 request
->slot
= slot
;
411 request
->state
= RUN_PUT
;
413 request
->state
= ABORTED
;
419 static void start_move(struct transfer_request
*request
)
421 struct active_request_slot
*slot
;
422 struct curl_slist
*dav_headers
= NULL
;
424 slot
= get_active_slot();
425 slot
->callback_func
= process_response
;
426 slot
->callback_data
= request
;
427 curl_setup_http_get(slot
->curl
, request
->url
, DAV_MOVE
);
428 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
429 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
430 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
432 if (start_active_slot(slot
)) {
433 request
->slot
= slot
;
434 request
->state
= RUN_MOVE
;
436 request
->state
= ABORTED
;
442 static int refresh_lock(struct remote_lock
*lock
)
444 struct active_request_slot
*slot
;
445 struct slot_results results
;
446 struct curl_slist
*dav_headers
;
449 lock
->refreshing
= 1;
451 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
453 slot
= get_active_slot();
454 slot
->results
= &results
;
455 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_LOCK
);
456 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
458 if (start_active_slot(slot
)) {
459 run_active_slot(slot
);
460 if (results
.curl_result
!= CURLE_OK
) {
461 fprintf(stderr
, "LOCK HTTP error %ld\n",
464 lock
->start_time
= time(NULL
);
469 lock
->refreshing
= 0;
470 curl_slist_free_all(dav_headers
);
475 static void check_locks(void)
477 struct remote_lock
*lock
= repo
->locks
;
478 time_t current_time
= time(NULL
);
482 time_remaining
= lock
->start_time
+ lock
->timeout
-
484 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
485 if (!refresh_lock(lock
)) {
487 "Unable to refresh lock for %s\n",
497 static void release_request(struct transfer_request
*request
)
499 struct transfer_request
*entry
= request_queue_head
;
501 if (request
== request_queue_head
) {
502 request_queue_head
= request
->next
;
504 while (entry
->next
!= NULL
&& entry
->next
!= request
)
506 if (entry
->next
== request
)
507 entry
->next
= entry
->next
->next
;
514 static void finish_request(struct transfer_request
*request
)
516 struct http_pack_request
*preq
;
517 struct http_object_request
*obj_req
;
519 request
->curl_result
= request
->slot
->curl_result
;
520 request
->http_code
= request
->slot
->http_code
;
521 request
->slot
= NULL
;
523 /* Keep locks active */
526 if (request
->headers
!= NULL
)
527 curl_slist_free_all(request
->headers
);
529 /* URL is reused for MOVE after PUT */
530 if (request
->state
!= RUN_PUT
) {
535 if (request
->state
== RUN_MKCOL
) {
536 if (request
->curl_result
== CURLE_OK
||
537 request
->http_code
== 405) {
538 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
541 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
542 sha1_to_hex(request
->obj
->sha1
),
543 request
->curl_result
, request
->http_code
);
544 request
->state
= ABORTED
;
547 } else if (request
->state
== RUN_PUT
) {
548 if (request
->curl_result
== CURLE_OK
) {
551 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
552 sha1_to_hex(request
->obj
->sha1
),
553 request
->curl_result
, request
->http_code
);
554 request
->state
= ABORTED
;
557 } else if (request
->state
== RUN_MOVE
) {
558 if (request
->curl_result
== CURLE_OK
) {
560 fprintf(stderr
, " sent %s\n",
561 sha1_to_hex(request
->obj
->sha1
));
562 request
->obj
->flags
|= REMOTE
;
563 release_request(request
);
565 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
566 sha1_to_hex(request
->obj
->sha1
),
567 request
->curl_result
, request
->http_code
);
568 request
->state
= ABORTED
;
571 } else if (request
->state
== RUN_FETCH_LOOSE
) {
572 obj_req
= (struct http_object_request
*)request
->userData
;
574 if (finish_http_object_request(obj_req
) == 0)
575 if (obj_req
->rename
== 0)
576 request
->obj
->flags
|= (LOCAL
| REMOTE
);
578 /* Try fetching packed if necessary */
579 if (request
->obj
->flags
& LOCAL
) {
580 release_http_object_request(obj_req
);
581 release_request(request
);
583 start_fetch_packed(request
);
585 } else if (request
->state
== RUN_FETCH_PACKED
) {
587 if (request
->curl_result
!= CURLE_OK
) {
588 fprintf(stderr
, "Unable to get pack file %s\n%s",
589 request
->url
, curl_errorstr
);
591 preq
= (struct http_pack_request
*)request
->userData
;
594 if (finish_http_pack_request(preq
) == 0)
596 release_http_pack_request(preq
);
600 repo
->can_update_info_refs
= 0;
601 release_request(request
);
605 #ifdef USE_CURL_MULTI
606 static int is_running_queue
;
607 static int fill_active_slot(void *unused
)
609 struct transfer_request
*request
;
611 if (aborted
|| !is_running_queue
)
614 for (request
= request_queue_head
; request
; request
= request
->next
) {
615 if (request
->state
== NEED_FETCH
) {
616 start_fetch_loose(request
);
618 } else if (pushing
&& request
->state
== NEED_PUSH
) {
619 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
622 start_mkcol(request
);
631 static void get_remote_object_list(unsigned char parent
);
633 static void add_fetch_request(struct object
*obj
)
635 struct transfer_request
*request
;
640 * Don't fetch the object if it's known to exist locally
641 * or is already in the request queue
643 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
644 get_remote_object_list(obj
->sha1
[0]);
645 if (obj
->flags
& (LOCAL
| FETCHING
))
648 obj
->flags
|= FETCHING
;
649 request
= xmalloc(sizeof(*request
));
652 request
->lock
= NULL
;
653 request
->headers
= NULL
;
654 request
->state
= NEED_FETCH
;
655 request
->next
= request_queue_head
;
656 request_queue_head
= request
;
658 #ifdef USE_CURL_MULTI
664 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
666 struct transfer_request
*request
;
667 struct packed_git
*target
;
669 /* Keep locks active */
673 * Don't push the object if it's known to exist on the remote
674 * or is already in the request queue
676 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
677 get_remote_object_list(obj
->sha1
[0]);
678 if (obj
->flags
& (REMOTE
| PUSHING
))
680 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
682 obj
->flags
|= REMOTE
;
686 obj
->flags
|= PUSHING
;
687 request
= xmalloc(sizeof(*request
));
690 request
->lock
= lock
;
691 request
->headers
= NULL
;
692 request
->state
= NEED_PUSH
;
693 request
->next
= request_queue_head
;
694 request_queue_head
= request
;
696 #ifdef USE_CURL_MULTI
704 static int fetch_indices(void)
709 fprintf(stderr
, "Getting pack list\n");
711 switch (http_get_info_packs(repo
->url
, &repo
->packs
)) {
713 case HTTP_MISSING_TARGET
:
723 static void one_remote_object(const char *hex
)
725 unsigned char sha1
[20];
728 if (get_sha1_hex(hex
, sha1
) != 0)
731 obj
= lookup_object(sha1
);
733 obj
= parse_object(sha1
);
735 /* Ignore remote objects that don't exist locally */
739 obj
->flags
|= REMOTE
;
740 if (!object_list_contains(objects
, obj
))
741 object_list_insert(obj
, &objects
);
744 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
746 int *lock_flags
= (int *)ctx
->userData
;
749 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
750 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
751 (*lock_flags
& DAV_PROP_LOCKWR
)) {
752 *lock_flags
|= DAV_LOCK_OK
;
754 *lock_flags
&= DAV_LOCK_OK
;
755 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
756 *lock_flags
|= DAV_PROP_LOCKWR
;
757 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
758 *lock_flags
|= DAV_PROP_LOCKEX
;
763 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
765 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
767 unsigned char lock_token_sha1
[20];
769 if (tag_closed
&& ctx
->cdata
) {
770 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
771 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
772 strcpy(lock
->owner
, ctx
->cdata
);
773 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
774 if (!prefixcmp(ctx
->cdata
, "Second-"))
776 strtol(ctx
->cdata
+ 7, NULL
, 10);
777 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
778 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
779 strcpy(lock
->token
, ctx
->cdata
);
781 git_SHA1_Init(&sha_ctx
);
782 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
783 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
785 lock
->tmpfile_suffix
[0] = '_';
786 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
791 static void one_remote_ref(const char *refname
);
794 xml_start_tag(void *userData
, const char *name
, const char **atts
)
796 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
797 const char *c
= strchr(name
, ':');
805 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
807 if (new_len
> ctx
->len
) {
808 ctx
->name
= xrealloc(ctx
->name
, new_len
);
811 strcat(ctx
->name
, ".");
812 strcat(ctx
->name
, c
);
817 ctx
->userFunc(ctx
, 0);
821 xml_end_tag(void *userData
, const char *name
)
823 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
824 const char *c
= strchr(name
, ':');
827 ctx
->userFunc(ctx
, 1);
834 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
839 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
841 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
843 ctx
->cdata
= xmemdupz(s
, len
);
846 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
848 struct active_request_slot
*slot
;
849 struct slot_results results
;
850 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
851 struct strbuf in_buffer
= STRBUF_INIT
;
854 char timeout_header
[25];
855 struct remote_lock
*lock
= NULL
;
856 struct curl_slist
*dav_headers
= NULL
;
860 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
861 sprintf(url
, "%s%s", repo
->url
, path
);
863 /* Make sure leading directories exist for the remote ref */
864 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
866 char saved_character
= ep
[1];
868 slot
= get_active_slot();
869 slot
->results
= &results
;
870 curl_setup_http_get(slot
->curl
, url
, DAV_MKCOL
);
871 if (start_active_slot(slot
)) {
872 run_active_slot(slot
);
873 if (results
.curl_result
!= CURLE_OK
&&
874 results
.http_code
!= 405) {
876 "Unable to create branch path %s\n",
882 fprintf(stderr
, "Unable to start MKCOL request\n");
886 ep
[1] = saved_character
;
887 ep
= strchr(ep
+ 1, '/');
890 escaped
= xml_entities(ident_default_email());
891 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
894 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
895 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
896 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
898 slot
= get_active_slot();
899 slot
->results
= &results
;
900 curl_setup_http(slot
->curl
, url
, DAV_LOCK
, &out_buffer
, fwrite_buffer
);
901 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
902 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
904 lock
= xcalloc(1, sizeof(*lock
));
907 if (start_active_slot(slot
)) {
908 run_active_slot(slot
);
909 if (results
.curl_result
== CURLE_OK
) {
910 XML_Parser parser
= XML_ParserCreate(NULL
);
911 enum XML_Status result
;
912 ctx
.name
= xcalloc(10, 1);
915 ctx
.userFunc
= handle_new_lock_ctx
;
917 XML_SetUserData(parser
, &ctx
);
918 XML_SetElementHandler(parser
, xml_start_tag
,
920 XML_SetCharacterDataHandler(parser
, xml_cdata
);
921 result
= XML_Parse(parser
, in_buffer
.buf
,
924 if (result
!= XML_STATUS_OK
) {
925 fprintf(stderr
, "XML error: %s\n",
927 XML_GetErrorCode(parser
)));
930 XML_ParserFree(parser
);
933 fprintf(stderr
, "Unable to start LOCK request\n");
936 curl_slist_free_all(dav_headers
);
937 strbuf_release(&out_buffer
.buf
);
938 strbuf_release(&in_buffer
);
940 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
948 lock
->start_time
= time(NULL
);
949 lock
->next
= repo
->locks
;
956 static int unlock_remote(struct remote_lock
*lock
)
958 struct active_request_slot
*slot
;
959 struct slot_results results
;
960 struct remote_lock
*prev
= repo
->locks
;
961 struct curl_slist
*dav_headers
;
964 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
966 slot
= get_active_slot();
967 slot
->results
= &results
;
968 curl_setup_http_get(slot
->curl
, lock
->url
, DAV_UNLOCK
);
969 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
971 if (start_active_slot(slot
)) {
972 run_active_slot(slot
);
973 if (results
.curl_result
== CURLE_OK
)
976 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
979 fprintf(stderr
, "Unable to start UNLOCK request\n");
982 curl_slist_free_all(dav_headers
);
984 if (repo
->locks
== lock
) {
985 repo
->locks
= lock
->next
;
987 while (prev
&& prev
->next
!= lock
)
990 prev
->next
= prev
->next
->next
;
1001 static void remove_locks(void)
1003 struct remote_lock
*lock
= repo
->locks
;
1005 fprintf(stderr
, "Removing remote locks...\n");
1007 struct remote_lock
*next
= lock
->next
;
1008 unlock_remote(lock
);
1013 static void remove_locks_on_signal(int signo
)
1016 sigchain_pop(signo
);
1020 static void remote_ls(const char *path
, int flags
,
1021 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1024 static void process_ls_object(struct remote_ls_ctx
*ls
)
1026 unsigned int *parent
= (unsigned int *)ls
->userData
;
1027 char *path
= ls
->dentry_name
;
1030 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1031 remote_dir_exists
[*parent
] = 1;
1035 if (strlen(path
) != 49)
1038 obj_hex
= xmalloc(strlen(path
));
1039 /* NB: path is not null-terminated, can not use strlcpy here */
1040 memcpy(obj_hex
, path
, 2);
1041 strcpy(obj_hex
+ 2, path
+ 3);
1042 one_remote_object(obj_hex
);
1046 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1048 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1049 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1053 if (!(ls
->dentry_flags
& IS_DIR
))
1054 one_remote_ref(ls
->dentry_name
);
1057 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1059 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1062 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1063 if (ls
->dentry_flags
& IS_DIR
) {
1065 /* ensure collection names end with slash */
1066 str_end_url_with_slash(ls
->dentry_name
, &ls
->dentry_name
);
1068 if (ls
->flags
& PROCESS_DIRS
) {
1071 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1072 ls
->flags
& RECURSIVE
) {
1073 remote_ls(ls
->dentry_name
,
1078 } else if (ls
->flags
& PROCESS_FILES
) {
1081 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1082 char *path
= ctx
->cdata
;
1083 if (*ctx
->cdata
== 'h') {
1084 path
= strstr(path
, "//");
1086 path
= strchr(path
+2, '/');
1090 const char *url
= repo
->url
;
1093 if (strncmp(path
, url
, repo
->path_len
))
1094 error("Parsed path '%s' does not match url: '%s'",
1097 path
+= repo
->path_len
;
1098 ls
->dentry_name
= xstrdup(path
);
1101 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1102 ls
->dentry_flags
|= IS_DIR
;
1104 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1105 free(ls
->dentry_name
);
1106 ls
->dentry_name
= NULL
;
1107 ls
->dentry_flags
= 0;
1112 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1113 * should _only_ heed the information from that file, instead of trying to
1114 * determine the refs from the remote file system (badly: it does not even
1115 * know about packed-refs).
1117 static void remote_ls(const char *path
, int flags
,
1118 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1121 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1122 struct active_request_slot
*slot
;
1123 struct slot_results results
;
1124 struct strbuf in_buffer
= STRBUF_INIT
;
1125 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1126 struct curl_slist
*dav_headers
= NULL
;
1128 struct remote_ls_ctx ls
;
1131 ls
.path
= xstrdup(path
);
1132 ls
.dentry_name
= NULL
;
1133 ls
.dentry_flags
= 0;
1134 ls
.userData
= userData
;
1135 ls
.userFunc
= userFunc
;
1137 sprintf(url
, "%s%s", repo
->url
, path
);
1139 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1141 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1142 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1144 slot
= get_active_slot();
1145 slot
->results
= &results
;
1146 curl_setup_http(slot
->curl
, url
, DAV_PROPFIND
,
1147 &out_buffer
, fwrite_buffer
);
1148 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1149 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1151 if (start_active_slot(slot
)) {
1152 run_active_slot(slot
);
1153 if (results
.curl_result
== CURLE_OK
) {
1154 XML_Parser parser
= XML_ParserCreate(NULL
);
1155 enum XML_Status result
;
1156 ctx
.name
= xcalloc(10, 1);
1159 ctx
.userFunc
= handle_remote_ls_ctx
;
1161 XML_SetUserData(parser
, &ctx
);
1162 XML_SetElementHandler(parser
, xml_start_tag
,
1164 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1165 result
= XML_Parse(parser
, in_buffer
.buf
,
1169 if (result
!= XML_STATUS_OK
) {
1170 fprintf(stderr
, "XML error: %s\n",
1172 XML_GetErrorCode(parser
)));
1174 XML_ParserFree(parser
);
1177 fprintf(stderr
, "Unable to start PROPFIND request\n");
1182 strbuf_release(&out_buffer
.buf
);
1183 strbuf_release(&in_buffer
);
1184 curl_slist_free_all(dav_headers
);
1187 static void get_remote_object_list(unsigned char parent
)
1189 char path
[] = "objects/XX/";
1190 static const char hex
[] = "0123456789abcdef";
1191 unsigned int val
= parent
;
1193 path
[8] = hex
[val
>> 4];
1194 path
[9] = hex
[val
& 0xf];
1195 remote_dir_exists
[val
] = 0;
1196 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1197 process_ls_object
, &val
);
1200 static int locking_available(void)
1202 struct active_request_slot
*slot
;
1203 struct slot_results results
;
1204 struct strbuf in_buffer
= STRBUF_INIT
;
1205 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1206 struct curl_slist
*dav_headers
= NULL
;
1211 escaped
= xml_entities(repo
->url
);
1212 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1215 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1216 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1218 slot
= get_active_slot();
1219 slot
->results
= &results
;
1220 curl_setup_http(slot
->curl
, repo
->url
, DAV_PROPFIND
,
1221 &out_buffer
, fwrite_buffer
);
1222 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1223 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1225 if (start_active_slot(slot
)) {
1226 run_active_slot(slot
);
1227 if (results
.curl_result
== CURLE_OK
) {
1228 XML_Parser parser
= XML_ParserCreate(NULL
);
1229 enum XML_Status result
;
1230 ctx
.name
= xcalloc(10, 1);
1233 ctx
.userFunc
= handle_lockprop_ctx
;
1234 ctx
.userData
= &lock_flags
;
1235 XML_SetUserData(parser
, &ctx
);
1236 XML_SetElementHandler(parser
, xml_start_tag
,
1238 result
= XML_Parse(parser
, in_buffer
.buf
,
1242 if (result
!= XML_STATUS_OK
) {
1243 fprintf(stderr
, "XML error: %s\n",
1245 XML_GetErrorCode(parser
)));
1248 XML_ParserFree(parser
);
1250 error("no DAV locking support on %s",
1254 error("Cannot access URL %s, return code %d",
1255 repo
->url
, results
.curl_result
);
1259 error("Unable to start PROPFIND request on %s", repo
->url
);
1262 strbuf_release(&out_buffer
.buf
);
1263 strbuf_release(&in_buffer
);
1264 curl_slist_free_all(dav_headers
);
1269 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1271 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1275 return &entry
->next
;
1278 static struct object_list
**process_blob(struct blob
*blob
,
1279 struct object_list
**p
,
1280 struct name_path
*path
,
1283 struct object
*obj
= &blob
->object
;
1285 obj
->flags
|= LOCAL
;
1287 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1291 return add_one_object(obj
, p
);
1294 static struct object_list
**process_tree(struct tree
*tree
,
1295 struct object_list
**p
,
1296 struct name_path
*path
,
1299 struct object
*obj
= &tree
->object
;
1300 struct tree_desc desc
;
1301 struct name_entry entry
;
1302 struct name_path me
;
1304 obj
->flags
|= LOCAL
;
1306 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1308 if (parse_tree(tree
) < 0)
1309 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1312 name
= xstrdup(name
);
1313 p
= add_one_object(obj
, p
);
1316 me
.elem_len
= strlen(name
);
1318 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1320 while (tree_entry(&desc
, &entry
))
1321 switch (object_type(entry
.mode
)) {
1323 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1326 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1329 /* Subproject commit - not in this repository */
1334 tree
->buffer
= NULL
;
1338 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1341 struct commit
*commit
;
1342 struct object_list
**p
= &objects
;
1345 while ((commit
= get_revision(revs
)) != NULL
) {
1346 p
= process_tree(commit
->tree
, p
, NULL
, "");
1347 commit
->object
.flags
|= LOCAL
;
1348 if (!(commit
->object
.flags
& UNINTERESTING
))
1349 count
+= add_send_request(&commit
->object
, lock
);
1352 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1353 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1354 struct object
*obj
= entry
->item
;
1355 const char *name
= entry
->name
;
1357 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1359 if (obj
->type
== OBJ_TAG
) {
1361 p
= add_one_object(obj
, p
);
1364 if (obj
->type
== OBJ_TREE
) {
1365 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1368 if (obj
->type
== OBJ_BLOB
) {
1369 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1372 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1376 if (!(objects
->item
->flags
& UNINTERESTING
))
1377 count
+= add_send_request(objects
->item
, lock
);
1378 objects
= objects
->next
;
1384 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1386 struct active_request_slot
*slot
;
1387 struct slot_results results
;
1388 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1389 struct curl_slist
*dav_headers
;
1391 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1393 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1395 slot
= get_active_slot();
1396 slot
->results
= &results
;
1397 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1398 &out_buffer
, fwrite_null
);
1399 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1401 if (start_active_slot(slot
)) {
1402 run_active_slot(slot
);
1403 strbuf_release(&out_buffer
.buf
);
1404 if (results
.curl_result
!= CURLE_OK
) {
1406 "PUT error: curl result=%d, HTTP code=%ld\n",
1407 results
.curl_result
, results
.http_code
);
1408 /* We should attempt recovery? */
1412 strbuf_release(&out_buffer
.buf
);
1413 fprintf(stderr
, "Unable to start PUT request\n");
1420 static struct ref
*remote_refs
;
1422 static void one_remote_ref(const char *refname
)
1427 ref
= alloc_ref(refname
);
1429 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1431 "Unable to fetch ref %s from %s\n",
1432 refname
, repo
->url
);
1438 * Fetch a copy of the object if it doesn't exist locally - it
1439 * may be required for updating server info later.
1441 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1442 obj
= lookup_unknown_object(ref
->old_sha1
);
1444 fprintf(stderr
, " fetch %s for %s\n",
1445 sha1_to_hex(ref
->old_sha1
), refname
);
1446 add_fetch_request(obj
);
1450 ref
->next
= remote_refs
;
1454 static void get_dav_remote_heads(void)
1456 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1459 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1461 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1467 ref
= alloc_ref(ls
->dentry_name
);
1469 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1471 "Unable to fetch ref %s from %s\n",
1472 ls
->dentry_name
, repo
->url
);
1478 o
= parse_object(ref
->old_sha1
);
1481 "Unable to parse object %s for remote ref %s\n",
1482 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1488 len
= strlen(ls
->dentry_name
) + 42;
1489 ref_info
= xcalloc(len
+ 1, 1);
1490 sprintf(ref_info
, "%s %s\n",
1491 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1492 fwrite_buffer(ref_info
, 1, len
, buf
);
1495 if (o
->type
== OBJ_TAG
) {
1496 o
= deref_tag(o
, ls
->dentry_name
, 0);
1498 len
= strlen(ls
->dentry_name
) + 45;
1499 ref_info
= xcalloc(len
+ 1, 1);
1500 sprintf(ref_info
, "%s %s^{}\n",
1501 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1502 fwrite_buffer(ref_info
, 1, len
, buf
);
1509 static void update_remote_info_refs(struct remote_lock
*lock
)
1511 struct buffer buffer
= { STRBUF_INIT
, 0 };
1512 struct active_request_slot
*slot
;
1513 struct slot_results results
;
1514 struct curl_slist
*dav_headers
;
1516 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1517 add_remote_info_ref
, &buffer
.buf
);
1519 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1521 slot
= get_active_slot();
1522 slot
->results
= &results
;
1523 curl_setup_http(slot
->curl
, lock
->url
, DAV_PUT
,
1524 &buffer
, fwrite_null
);
1525 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1527 if (start_active_slot(slot
)) {
1528 run_active_slot(slot
);
1529 if (results
.curl_result
!= CURLE_OK
) {
1531 "PUT error: curl result=%d, HTTP code=%ld\n",
1532 results
.curl_result
, results
.http_code
);
1536 strbuf_release(&buffer
.buf
);
1539 static int remote_exists(const char *path
)
1541 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1544 sprintf(url
, "%s%s", repo
->url
, path
);
1546 switch (http_get_strbuf(url
, NULL
, NULL
, 0)) {
1550 case HTTP_MISSING_TARGET
:
1554 error("unable to access '%s': %s", url
, curl_errorstr
);
1562 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1565 struct strbuf buffer
= STRBUF_INIT
;
1567 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1568 sprintf(url
, "%s%s", repo
->url
, path
);
1570 if (http_get_strbuf(url
, NULL
, &buffer
, 0) != HTTP_OK
)
1571 die("Couldn't get %s for remote symref\n%s", url
,
1579 if (buffer
.len
== 0)
1582 /* If it's a symref, set the refname; otherwise try for a sha1 */
1583 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
1584 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
1586 get_sha1_hex(buffer
.buf
, sha1
);
1589 strbuf_release(&buffer
);
1592 static int verify_merge_base(unsigned char *head_sha1
, struct ref
*remote
)
1594 struct commit
*head
= lookup_commit_or_die(head_sha1
, "HEAD");
1595 struct commit
*branch
= lookup_commit_or_die(remote
->old_sha1
, remote
->name
);
1597 return in_merge_bases(branch
, head
);
1600 static int delete_remote_branch(const char *pattern
, int force
)
1602 struct ref
*refs
= remote_refs
;
1603 struct ref
*remote_ref
= NULL
;
1604 unsigned char head_sha1
[20];
1605 char *symref
= NULL
;
1607 int patlen
= strlen(pattern
);
1609 struct active_request_slot
*slot
;
1610 struct slot_results results
;
1613 /* Find the remote branch(es) matching the specified branch name */
1614 for (match
= 0; refs
; refs
= refs
->next
) {
1615 char *name
= refs
->name
;
1616 int namelen
= strlen(name
);
1617 if (namelen
< patlen
||
1618 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
1620 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
1626 return error("No remote branch matches %s", pattern
);
1628 return error("More than one remote branch matches %s",
1632 * Remote HEAD must be a symref (not exactly foolproof; a remote
1633 * symlink to a symref will look like a symref)
1635 fetch_symref("HEAD", &symref
, head_sha1
);
1637 return error("Remote HEAD is not a symref");
1639 /* Remote branch must not be the remote HEAD */
1640 for (i
= 0; symref
&& i
< MAXDEPTH
; i
++) {
1641 if (!strcmp(remote_ref
->name
, symref
))
1642 return error("Remote branch %s is the current HEAD",
1644 fetch_symref(symref
, &symref
, head_sha1
);
1647 /* Run extra sanity checks if delete is not forced */
1649 /* Remote HEAD must resolve to a known object */
1651 return error("Remote HEAD symrefs too deep");
1652 if (is_null_sha1(head_sha1
))
1653 return error("Unable to resolve remote HEAD");
1654 if (!has_sha1_file(head_sha1
))
1655 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
1657 /* Remote branch must resolve to a known object */
1658 if (is_null_sha1(remote_ref
->old_sha1
))
1659 return error("Unable to resolve remote branch %s",
1661 if (!has_sha1_file(remote_ref
->old_sha1
))
1662 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
));
1664 /* Remote branch must be an ancestor of remote HEAD */
1665 if (!verify_merge_base(head_sha1
, remote_ref
)) {
1666 return error("The branch '%s' is not an ancestor "
1667 "of your current HEAD.\n"
1668 "If you are sure you want to delete it,"
1669 " run:\n\t'git http-push -D %s %s'",
1670 remote_ref
->name
, repo
->url
, pattern
);
1674 /* Send delete request */
1675 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
1678 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
1679 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
1680 slot
= get_active_slot();
1681 slot
->results
= &results
;
1682 curl_setup_http_get(slot
->curl
, url
, DAV_DELETE
);
1683 if (start_active_slot(slot
)) {
1684 run_active_slot(slot
);
1686 if (results
.curl_result
!= CURLE_OK
)
1687 return error("DELETE request failed (%d/%ld)",
1688 results
.curl_result
, results
.http_code
);
1691 return error("Unable to start DELETE request");
1697 static void run_request_queue(void)
1699 #ifdef USE_CURL_MULTI
1700 is_running_queue
= 1;
1701 fill_active_slots();
1702 add_fill_function(NULL
, fill_active_slot
);
1705 finish_all_active_slots();
1706 #ifdef USE_CURL_MULTI
1707 fill_active_slots();
1709 } while (request_queue_head
&& !aborted
);
1711 #ifdef USE_CURL_MULTI
1712 is_running_queue
= 0;
1716 int main(int argc
, char **argv
)
1718 struct transfer_request
*request
;
1719 struct transfer_request
*next_request
;
1721 char **refspec
= NULL
;
1722 struct remote_lock
*ref_lock
= NULL
;
1723 struct remote_lock
*info_ref_lock
= NULL
;
1724 struct rev_info revs
;
1725 int delete_branch
= 0;
1726 int force_delete
= 0;
1727 int objects_to_send
;
1731 struct ref
*ref
, *local_refs
;
1733 git_setup_gettext();
1735 git_extract_argv0_path(argv
[0]);
1737 repo
= xcalloc(sizeof(*repo
), 1);
1740 for (i
= 1; i
< argc
; i
++, argv
++) {
1744 if (!strcmp(arg
, "--all")) {
1745 push_all
= MATCH_REFS_ALL
;
1748 if (!strcmp(arg
, "--force")) {
1752 if (!strcmp(arg
, "--dry-run")) {
1756 if (!strcmp(arg
, "--helper-status")) {
1760 if (!strcmp(arg
, "--verbose")) {
1762 http_is_verbose
= 1;
1765 if (!strcmp(arg
, "-d")) {
1769 if (!strcmp(arg
, "-D")) {
1774 if (!strcmp(arg
, "-h"))
1775 usage(http_push_usage
);
1778 char *path
= strstr(arg
, "//");
1779 str_end_url_with_slash(arg
, &repo
->url
);
1780 repo
->path_len
= strlen(repo
->url
);
1782 repo
->path
= strchr(path
+2, '/');
1784 repo
->path_len
= strlen(repo
->path
);
1789 nr_refspec
= argc
- i
;
1793 #ifndef USE_CURL_MULTI
1794 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1798 usage(http_push_usage
);
1800 if (delete_branch
&& nr_refspec
!= 1)
1801 die("You must specify only one branch name when deleting a remote branch");
1803 setup_git_directory();
1805 memset(remote_dir_exists
, -1, 256);
1807 http_init(NULL
, repo
->url
, 1);
1809 #ifdef USE_CURL_MULTI
1810 is_running_queue
= 0;
1813 /* Verify DAV compliance/lock support */
1814 if (!locking_available()) {
1819 sigchain_push_common(remove_locks_on_signal
);
1821 /* Check whether the remote has server info files */
1822 repo
->can_update_info_refs
= 0;
1823 repo
->has_info_refs
= remote_exists("info/refs");
1824 repo
->has_info_packs
= remote_exists("objects/info/packs");
1825 if (repo
->has_info_refs
) {
1826 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
1828 repo
->can_update_info_refs
= 1;
1830 error("cannot lock existing info/refs");
1835 if (repo
->has_info_packs
)
1838 /* Get a list of all local and remote heads to validate refspecs */
1839 local_refs
= get_local_heads();
1840 fprintf(stderr
, "Fetching remote heads...\n");
1841 get_dav_remote_heads();
1842 run_request_queue();
1844 /* Remove a remote branch if -d or -D was specified */
1845 if (delete_branch
) {
1846 if (delete_remote_branch(refspec
[0], force_delete
) == -1) {
1847 fprintf(stderr
, "Unable to delete remote branch %s\n",
1850 printf("error %s cannot remove\n", refspec
[0]);
1856 if (match_push_refs(local_refs
, &remote_refs
,
1857 nr_refspec
, (const char **) refspec
, push_all
)) {
1862 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
1864 printf("error null no match\n");
1870 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1871 char old_hex
[60], *new_hex
;
1872 const char *commit_argv
[5];
1874 char *new_sha1_hex
, *old_sha1_hex
;
1879 if (is_null_sha1(ref
->peer_ref
->new_sha1
)) {
1880 if (delete_remote_branch(ref
->name
, 1) == -1) {
1881 error("Could not remove %s", ref
->name
);
1883 printf("error %s cannot remove\n", ref
->name
);
1886 else if (helper_status
)
1887 printf("ok %s\n", ref
->name
);
1892 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
1894 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
1896 printf("ok %s up to date\n", ref
->name
);
1901 !is_null_sha1(ref
->old_sha1
) &&
1903 if (!has_sha1_file(ref
->old_sha1
) ||
1904 !ref_newer(ref
->peer_ref
->new_sha1
,
1907 * We do not have the remote ref, or
1908 * we know that the remote ref is not
1909 * an ancestor of what we are trying to
1910 * push. Either way this can be losing
1911 * commits at the remote end and likely
1912 * we were not up to date to begin with.
1914 error("remote '%s' is not an ancestor of\n"
1916 "Maybe you are not up-to-date and "
1917 "need to pull first?",
1919 ref
->peer_ref
->name
);
1921 printf("error %s non-fast forward\n", ref
->name
);
1926 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
1928 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
1929 new_hex
= sha1_to_hex(ref
->new_sha1
);
1931 fprintf(stderr
, "updating '%s'", ref
->name
);
1932 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
1933 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
1934 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
1937 printf("ok %s\n", ref
->name
);
1941 /* Lock remote branch ref */
1942 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
1943 if (ref_lock
== NULL
) {
1944 fprintf(stderr
, "Unable to lock remote branch %s\n",
1947 printf("error %s lock error\n", ref
->name
);
1952 /* Set up revision info for this refspec */
1954 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
1955 old_sha1_hex
= NULL
;
1956 commit_argv
[1] = "--objects";
1957 commit_argv
[2] = new_sha1_hex
;
1958 if (!push_all
&& !is_null_sha1(ref
->old_sha1
)) {
1959 old_sha1_hex
= xmalloc(42);
1960 sprintf(old_sha1_hex
, "^%s",
1961 sha1_to_hex(ref
->old_sha1
));
1962 commit_argv
[3] = old_sha1_hex
;
1965 commit_argv
[commit_argc
] = NULL
;
1966 init_revisions(&revs
, setup_git_directory());
1967 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
1968 revs
.edge_hint
= 0; /* just in case */
1972 commit_argv
[1] = NULL
;
1975 /* Generate a list of objects that need to be pushed */
1977 if (prepare_revision_walk(&revs
))
1978 die("revision walk setup failed");
1979 mark_edges_uninteresting(&revs
, NULL
);
1980 objects_to_send
= get_delta(&revs
, ref_lock
);
1981 finish_all_active_slots();
1983 /* Push missing objects to remote, this would be a
1984 convenient time to pack them first if appropriate. */
1986 if (objects_to_send
)
1987 fprintf(stderr
, " sending %d objects\n",
1990 run_request_queue();
1992 /* Update the remote branch if all went well */
1993 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
1997 fprintf(stderr
, " done\n");
1999 printf("%s %s\n", !rc
? "ok" : "error", ref
->name
);
2000 unlock_remote(ref_lock
);
2004 /* Update remote server info if appropriate */
2005 if (repo
->has_info_refs
&& new_refs
) {
2006 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2007 fprintf(stderr
, "Updating remote server info\n");
2009 update_remote_info_refs(info_ref_lock
);
2011 fprintf(stderr
, "Unable to update server info\n");
2017 unlock_remote(info_ref_lock
);
2022 request
= request_queue_head
;
2023 while (request
!= NULL
) {
2024 next_request
= request
->next
;
2025 release_request(request
);
2026 request
= next_request
;