12 #include "list-objects.h"
17 static const char http_push_usage
[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
29 #define PREV_BUF_SIZE 4096
30 #define RANGE_HEADER_SIZE 30
33 #define DAV_LOCK "LOCK"
34 #define DAV_MKCOL "MKCOL"
35 #define DAV_MOVE "MOVE"
36 #define DAV_PROPFIND "PROPFIND"
38 #define DAV_UNLOCK "UNLOCK"
39 #define DAV_DELETE "DELETE"
42 #define DAV_PROP_LOCKWR (1u << 0)
43 #define DAV_PROP_LOCKEX (1u << 1)
44 #define DAV_LOCK_OK (1u << 2)
46 /* DAV XML properties */
47 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
48 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
49 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
50 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
51 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
52 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
53 #define DAV_PROPFIND_RESP ".multistatus.response"
54 #define DAV_PROPFIND_NAME ".multistatus.response.href"
55 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
57 /* DAV request body templates */
58 #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>"
59 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
60 #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>"
63 #define LOCK_REFRESH 30
65 /* bits #0-15 in revision.h */
67 #define LOCAL (1u<<16)
68 #define REMOTE (1u<<17)
69 #define FETCHING (1u<<18)
70 #define PUSHING (1u<<19)
72 /* We allow "recursive" symbolic refs. Only within reason, though */
77 static signed char remote_dir_exists
[256];
79 static struct curl_slist
*no_pragma_header
;
81 static int push_verbosely
;
82 static int push_all
= MATCH_REFS_NONE
;
86 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
119 struct remote_lock
*lock
;
120 struct curl_slist
*headers
;
121 struct buffer buffer
;
122 char filename
[PATH_MAX
];
123 char tmpfile
[PATH_MAX
];
126 enum transfer_state state
;
127 CURLcode curl_result
;
128 char errorstr
[CURL_ERROR_SIZE
];
130 unsigned char real_sha1
[20];
136 struct active_request_slot
*slot
;
137 struct transfer_request
*next
;
140 static struct transfer_request
*request_queue_head
;
147 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
156 char tmpfile_suffix
[41];
160 struct remote_lock
*next
;
163 /* Flags that control remote_ls processing */
164 #define PROCESS_FILES (1u << 0)
165 #define PROCESS_DIRS (1u << 1)
166 #define RECURSIVE (1u << 2)
168 /* Flags that remote_ls passes to callback functions */
169 #define IS_DIR (1u << 0)
174 void (*userFunc
)(struct remote_ls_ctx
*ls
);
179 struct remote_ls_ctx
*parent
;
182 /* get_dav_token_headers options */
183 enum dav_header_flag
{
184 DAV_HEADER_IF
= (1u << 0),
185 DAV_HEADER_LOCK
= (1u << 1),
186 DAV_HEADER_TIMEOUT
= (1u << 2)
189 static char *xml_entities(char *s
)
191 struct strbuf buf
= STRBUF_INIT
;
193 size_t len
= strcspn(s
, "\"<>&");
194 strbuf_add(&buf
, s
, len
);
198 strbuf_addstr(&buf
, """);
201 strbuf_addstr(&buf
, "<");
204 strbuf_addstr(&buf
, ">");
207 strbuf_addstr(&buf
, "&");
212 return strbuf_detach(&buf
, NULL
);
215 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
217 struct strbuf buf
= STRBUF_INIT
;
218 struct curl_slist
*dav_headers
= NULL
;
220 if (options
& DAV_HEADER_IF
) {
221 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
222 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
225 if (options
& DAV_HEADER_LOCK
) {
226 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
227 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
230 if (options
& DAV_HEADER_TIMEOUT
) {
231 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
232 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
235 strbuf_release(&buf
);
240 static void append_remote_object_url(struct strbuf
*buf
, const char *url
,
242 int only_two_digit_prefix
)
244 strbuf_addf(buf
, "%sobjects/%.*s/", url
, 2, hex
);
245 if (!only_two_digit_prefix
)
246 strbuf_addf(buf
, "%s", hex
+2);
249 static void finish_request(struct transfer_request
*request
);
250 static void release_request(struct transfer_request
*request
);
252 static void process_response(void *callback_data
)
254 struct transfer_request
*request
=
255 (struct transfer_request
*)callback_data
;
257 finish_request(request
);
260 #ifdef USE_CURL_MULTI
262 static char *get_remote_object_url(const char *url
, const char *hex
,
263 int only_two_digit_prefix
)
265 struct strbuf buf
= STRBUF_INIT
;
266 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
267 return strbuf_detach(&buf
, NULL
);
270 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
273 unsigned char expn
[4096];
274 size_t size
= eltsize
* nmemb
;
276 struct transfer_request
*request
= (struct transfer_request
*)data
;
278 ssize_t retval
= xwrite(request
->local_fileno
,
279 (char *) ptr
+ posn
, size
- posn
);
283 } while (posn
< size
);
285 request
->stream
.avail_in
= size
;
286 request
->stream
.next_in
= ptr
;
288 request
->stream
.next_out
= expn
;
289 request
->stream
.avail_out
= sizeof(expn
);
290 request
->zret
= git_inflate(&request
->stream
, Z_SYNC_FLUSH
);
291 git_SHA1_Update(&request
->c
, expn
,
292 sizeof(expn
) - request
->stream
.avail_out
);
293 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
298 static void start_fetch_loose(struct transfer_request
*request
)
300 char *hex
= sha1_to_hex(request
->obj
->sha1
);
302 char prevfile
[PATH_MAX
];
305 unsigned char prev_buf
[PREV_BUF_SIZE
];
306 ssize_t prev_read
= 0;
308 char range
[RANGE_HEADER_SIZE
];
309 struct curl_slist
*range_header
= NULL
;
310 struct active_request_slot
*slot
;
312 filename
= sha1_file_name(request
->obj
->sha1
);
313 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
314 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
315 "%s.temp", filename
);
317 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
319 rename(request
->tmpfile
, prevfile
);
320 unlink(request
->tmpfile
);
322 if (request
->local_fileno
!= -1)
323 error("fd leakage in start: %d", request
->local_fileno
);
324 request
->local_fileno
= open(request
->tmpfile
,
325 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
326 /* This could have failed due to the "lazy directory creation";
327 * try to mkdir the last path component.
329 if (request
->local_fileno
< 0 && errno
== ENOENT
) {
330 char *dir
= strrchr(request
->tmpfile
, '/');
333 mkdir(request
->tmpfile
, 0777);
336 request
->local_fileno
= open(request
->tmpfile
,
337 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
340 if (request
->local_fileno
< 0) {
341 request
->state
= ABORTED
;
342 error("Couldn't create temporary file %s for %s: %s",
343 request
->tmpfile
, request
->filename
, strerror(errno
));
347 memset(&request
->stream
, 0, sizeof(request
->stream
));
349 git_inflate_init(&request
->stream
);
351 git_SHA1_Init(&request
->c
);
353 url
= get_remote_object_url(repo
->url
, hex
, 0);
354 request
->url
= xstrdup(url
);
356 /* If a previous temp file is present, process what was already
358 prevlocal
= open(prevfile
, O_RDONLY
);
359 if (prevlocal
!= -1) {
361 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
363 if (fwrite_sha1_file(prev_buf
,
366 request
) == prev_read
) {
367 prev_posn
+= prev_read
;
372 } while (prev_read
> 0);
377 /* Reset inflate/SHA1 if there was an error reading the previous temp
378 file; also rewind to the beginning of the local file. */
379 if (prev_read
== -1) {
380 memset(&request
->stream
, 0, sizeof(request
->stream
));
381 git_inflate_init(&request
->stream
);
382 git_SHA1_Init(&request
->c
);
385 lseek(request
->local_fileno
, 0, SEEK_SET
);
386 ftruncate(request
->local_fileno
, 0);
390 slot
= get_active_slot();
391 slot
->callback_func
= process_response
;
392 slot
->callback_data
= request
;
393 request
->slot
= slot
;
395 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
396 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
397 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
398 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
399 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
401 /* If we have successfully processed data from a previous fetch
402 attempt, only fetch the data we don't already have. */
406 "Resuming fetch of object %s at byte %ld\n",
408 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
409 range_header
= curl_slist_append(range_header
, range
);
410 curl_easy_setopt(slot
->curl
,
411 CURLOPT_HTTPHEADER
, range_header
);
414 /* Try to get the request started, abort the request on error */
415 request
->state
= RUN_FETCH_LOOSE
;
416 if (!start_active_slot(slot
)) {
417 fprintf(stderr
, "Unable to start GET request\n");
418 repo
->can_update_info_refs
= 0;
419 release_request(request
);
423 static void start_mkcol(struct transfer_request
*request
)
425 char *hex
= sha1_to_hex(request
->obj
->sha1
);
426 struct active_request_slot
*slot
;
428 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
430 slot
= get_active_slot();
431 slot
->callback_func
= process_response
;
432 slot
->callback_data
= request
;
433 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
434 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
435 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
436 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
437 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
439 if (start_active_slot(slot
)) {
440 request
->slot
= slot
;
441 request
->state
= RUN_MKCOL
;
443 request
->state
= ABORTED
;
450 static void start_fetch_packed(struct transfer_request
*request
)
453 struct packed_git
*target
;
457 char range
[RANGE_HEADER_SIZE
];
458 struct curl_slist
*range_header
= NULL
;
460 struct transfer_request
*check_request
= request_queue_head
;
461 struct active_request_slot
*slot
;
463 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
465 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
466 repo
->can_update_info_refs
= 0;
467 release_request(request
);
471 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
472 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
474 filename
= sha1_pack_name(target
->sha1
);
475 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
476 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
477 "%s.temp", filename
);
479 url
= xmalloc(strlen(repo
->url
) + 64);
480 sprintf(url
, "%sobjects/pack/pack-%s.pack",
481 repo
->url
, sha1_to_hex(target
->sha1
));
483 /* Make sure there isn't another open request for this pack */
484 while (check_request
) {
485 if (check_request
->state
== RUN_FETCH_PACKED
&&
486 !strcmp(check_request
->url
, url
)) {
488 release_request(request
);
491 check_request
= check_request
->next
;
494 packfile
= fopen(request
->tmpfile
, "a");
496 fprintf(stderr
, "Unable to open local file %s for pack",
498 repo
->can_update_info_refs
= 0;
503 slot
= get_active_slot();
504 slot
->callback_func
= process_response
;
505 slot
->callback_data
= request
;
506 request
->slot
= slot
;
507 request
->local_stream
= packfile
;
508 request
->userData
= target
;
511 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
512 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
513 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
514 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
515 slot
->local
= packfile
;
517 /* If there is data present from a previous transfer attempt,
518 resume where it left off */
519 prev_posn
= ftell(packfile
);
523 "Resuming fetch of pack %s at byte %ld\n",
524 sha1_to_hex(target
->sha1
), prev_posn
);
525 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
526 range_header
= curl_slist_append(range_header
, range
);
527 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
530 /* Try to get the request started, abort the request on error */
531 request
->state
= RUN_FETCH_PACKED
;
532 if (!start_active_slot(slot
)) {
533 fprintf(stderr
, "Unable to start GET request\n");
534 repo
->can_update_info_refs
= 0;
535 release_request(request
);
539 static void start_put(struct transfer_request
*request
)
541 char *hex
= sha1_to_hex(request
->obj
->sha1
);
542 struct active_request_slot
*slot
;
543 struct strbuf buf
= STRBUF_INIT
;
544 enum object_type type
;
552 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
553 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
556 memset(&stream
, 0, sizeof(stream
));
557 deflateInit(&stream
, zlib_compression_level
);
558 size
= deflateBound(&stream
, len
+ hdrlen
);
559 strbuf_init(&request
->buffer
.buf
, size
);
560 request
->buffer
.posn
= 0;
563 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
564 stream
.avail_out
= size
;
567 stream
.next_in
= (void *)hdr
;
568 stream
.avail_in
= hdrlen
;
569 while (deflate(&stream
, 0) == Z_OK
)
572 /* Then the data itself.. */
573 stream
.next_in
= unpacked
;
574 stream
.avail_in
= len
;
575 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
580 request
->buffer
.buf
.len
= stream
.total_out
;
582 strbuf_addstr(&buf
, "Destination: ");
583 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
584 request
->dest
= strbuf_detach(&buf
, NULL
);
586 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
587 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
588 request
->url
= strbuf_detach(&buf
, NULL
);
590 slot
= get_active_slot();
591 slot
->callback_func
= process_response
;
592 slot
->callback_data
= request
;
593 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
594 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
595 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
596 #ifndef NO_CURL_IOCTL
597 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
598 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &request
->buffer
);
600 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
601 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
602 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
603 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
604 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
605 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
607 if (start_active_slot(slot
)) {
608 request
->slot
= slot
;
609 request
->state
= RUN_PUT
;
611 request
->state
= ABORTED
;
617 static void start_move(struct transfer_request
*request
)
619 struct active_request_slot
*slot
;
620 struct curl_slist
*dav_headers
= NULL
;
622 slot
= get_active_slot();
623 slot
->callback_func
= process_response
;
624 slot
->callback_data
= request
;
625 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
626 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
627 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
628 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
629 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
630 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
631 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
633 if (start_active_slot(slot
)) {
634 request
->slot
= slot
;
635 request
->state
= RUN_MOVE
;
637 request
->state
= ABORTED
;
643 static int refresh_lock(struct remote_lock
*lock
)
645 struct active_request_slot
*slot
;
646 struct slot_results results
;
647 struct curl_slist
*dav_headers
;
650 lock
->refreshing
= 1;
652 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
654 slot
= get_active_slot();
655 slot
->results
= &results
;
656 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
657 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
658 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
659 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
660 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
662 if (start_active_slot(slot
)) {
663 run_active_slot(slot
);
664 if (results
.curl_result
!= CURLE_OK
) {
665 fprintf(stderr
, "LOCK HTTP error %ld\n",
668 lock
->start_time
= time(NULL
);
673 lock
->refreshing
= 0;
674 curl_slist_free_all(dav_headers
);
679 static void check_locks(void)
681 struct remote_lock
*lock
= repo
->locks
;
682 time_t current_time
= time(NULL
);
686 time_remaining
= lock
->start_time
+ lock
->timeout
-
688 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
689 if (!refresh_lock(lock
)) {
691 "Unable to refresh lock for %s\n",
701 static void release_request(struct transfer_request
*request
)
703 struct transfer_request
*entry
= request_queue_head
;
705 if (request
== request_queue_head
) {
706 request_queue_head
= request
->next
;
708 while (entry
->next
!= NULL
&& entry
->next
!= request
)
710 if (entry
->next
== request
)
711 entry
->next
= entry
->next
->next
;
714 if (request
->local_fileno
!= -1)
715 close(request
->local_fileno
);
716 if (request
->local_stream
)
717 fclose(request
->local_stream
);
722 static void finish_request(struct transfer_request
*request
)
725 struct packed_git
*target
;
726 struct packed_git
**lst
;
728 request
->curl_result
= request
->slot
->curl_result
;
729 request
->http_code
= request
->slot
->http_code
;
730 request
->slot
= NULL
;
732 /* Keep locks active */
735 if (request
->headers
!= NULL
)
736 curl_slist_free_all(request
->headers
);
738 /* URL is reused for MOVE after PUT */
739 if (request
->state
!= RUN_PUT
) {
744 if (request
->state
== RUN_MKCOL
) {
745 if (request
->curl_result
== CURLE_OK
||
746 request
->http_code
== 405) {
747 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
750 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
751 sha1_to_hex(request
->obj
->sha1
),
752 request
->curl_result
, request
->http_code
);
753 request
->state
= ABORTED
;
756 } else if (request
->state
== RUN_PUT
) {
757 if (request
->curl_result
== CURLE_OK
) {
760 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
761 sha1_to_hex(request
->obj
->sha1
),
762 request
->curl_result
, request
->http_code
);
763 request
->state
= ABORTED
;
766 } else if (request
->state
== RUN_MOVE
) {
767 if (request
->curl_result
== CURLE_OK
) {
769 fprintf(stderr
, " sent %s\n",
770 sha1_to_hex(request
->obj
->sha1
));
771 request
->obj
->flags
|= REMOTE
;
772 release_request(request
);
774 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
775 sha1_to_hex(request
->obj
->sha1
),
776 request
->curl_result
, request
->http_code
);
777 request
->state
= ABORTED
;
780 } else if (request
->state
== RUN_FETCH_LOOSE
) {
781 close(request
->local_fileno
); request
->local_fileno
= -1;
783 if (request
->curl_result
!= CURLE_OK
&&
784 request
->http_code
!= 416) {
785 if (stat(request
->tmpfile
, &st
) == 0) {
787 unlink(request
->tmpfile
);
790 if (request
->http_code
== 416)
791 warning("requested range invalid; we may already have all the data.");
793 git_inflate_end(&request
->stream
);
794 git_SHA1_Final(request
->real_sha1
, &request
->c
);
795 if (request
->zret
!= Z_STREAM_END
) {
796 unlink(request
->tmpfile
);
797 } else if (hashcmp(request
->obj
->sha1
, request
->real_sha1
)) {
798 unlink(request
->tmpfile
);
804 if (request
->rename
== 0) {
805 request
->obj
->flags
|= (LOCAL
| REMOTE
);
810 /* Try fetching packed if necessary */
811 if (request
->obj
->flags
& LOCAL
)
812 release_request(request
);
814 start_fetch_packed(request
);
816 } else if (request
->state
== RUN_FETCH_PACKED
) {
817 if (request
->curl_result
!= CURLE_OK
) {
818 fprintf(stderr
, "Unable to get pack file %s\n%s",
819 request
->url
, curl_errorstr
);
820 repo
->can_update_info_refs
= 0;
822 off_t pack_size
= ftell(request
->local_stream
);
824 fclose(request
->local_stream
);
825 request
->local_stream
= NULL
;
826 if (!move_temp_to_file(request
->tmpfile
,
827 request
->filename
)) {
828 target
= (struct packed_git
*)request
->userData
;
829 target
->pack_size
= pack_size
;
831 while (*lst
!= target
)
832 lst
= &((*lst
)->next
);
835 if (!verify_pack(target
))
836 install_packed_git(target
);
838 repo
->can_update_info_refs
= 0;
841 release_request(request
);
845 #ifdef USE_CURL_MULTI
846 static int fill_active_slot(void *unused
)
848 struct transfer_request
*request
;
853 for (request
= request_queue_head
; request
; request
= request
->next
) {
854 if (request
->state
== NEED_FETCH
) {
855 start_fetch_loose(request
);
857 } else if (pushing
&& request
->state
== NEED_PUSH
) {
858 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
861 start_mkcol(request
);
870 static void get_remote_object_list(unsigned char parent
);
872 static void add_fetch_request(struct object
*obj
)
874 struct transfer_request
*request
;
879 * Don't fetch the object if it's known to exist locally
880 * or is already in the request queue
882 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
883 get_remote_object_list(obj
->sha1
[0]);
884 if (obj
->flags
& (LOCAL
| FETCHING
))
887 obj
->flags
|= FETCHING
;
888 request
= xmalloc(sizeof(*request
));
891 request
->lock
= NULL
;
892 request
->headers
= NULL
;
893 request
->local_fileno
= -1;
894 request
->local_stream
= NULL
;
895 request
->state
= NEED_FETCH
;
896 request
->next
= request_queue_head
;
897 request_queue_head
= request
;
899 #ifdef USE_CURL_MULTI
905 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
907 struct transfer_request
*request
= request_queue_head
;
908 struct packed_git
*target
;
910 /* Keep locks active */
914 * Don't push the object if it's known to exist on the remote
915 * or is already in the request queue
917 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
918 get_remote_object_list(obj
->sha1
[0]);
919 if (obj
->flags
& (REMOTE
| PUSHING
))
921 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
923 obj
->flags
|= REMOTE
;
927 obj
->flags
|= PUSHING
;
928 request
= xmalloc(sizeof(*request
));
931 request
->lock
= lock
;
932 request
->headers
= NULL
;
933 request
->local_fileno
= -1;
934 request
->local_stream
= NULL
;
935 request
->state
= NEED_PUSH
;
936 request
->next
= request_queue_head
;
937 request_queue_head
= request
;
939 #ifdef USE_CURL_MULTI
947 static int fetch_index(unsigned char *sha1
)
949 char *hex
= sha1_to_hex(sha1
);
952 char tmpfile
[PATH_MAX
];
954 char range
[RANGE_HEADER_SIZE
];
955 struct curl_slist
*range_header
= NULL
;
958 struct active_request_slot
*slot
;
959 struct slot_results results
;
961 /* Don't use the index if the pack isn't there */
962 url
= xmalloc(strlen(repo
->url
) + 64);
963 sprintf(url
, "%sobjects/pack/pack-%s.pack", repo
->url
, hex
);
964 slot
= get_active_slot();
965 slot
->results
= &results
;
966 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
967 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
968 if (start_active_slot(slot
)) {
969 run_active_slot(slot
);
970 if (results
.curl_result
!= CURLE_OK
) {
972 return error("Unable to verify pack %s is available",
977 return error("Unable to start request");
980 if (has_pack_index(sha1
)) {
986 fprintf(stderr
, "Getting index for pack %s\n", hex
);
988 sprintf(url
, "%sobjects/pack/pack-%s.idx", repo
->url
, hex
);
990 filename
= sha1_pack_index_name(sha1
);
991 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
992 indexfile
= fopen(tmpfile
, "a");
995 return error("Unable to open local file %s for pack index",
999 slot
= get_active_slot();
1000 slot
->results
= &results
;
1001 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1002 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1003 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
1004 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1005 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1006 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1007 slot
->local
= indexfile
;
1009 /* If there is data present from a previous transfer attempt,
1010 resume where it left off */
1011 prev_posn
= ftell(indexfile
);
1015 "Resuming fetch of index for pack %s at byte %ld\n",
1017 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1018 range_header
= curl_slist_append(range_header
, range
);
1019 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
1022 if (start_active_slot(slot
)) {
1023 run_active_slot(slot
);
1024 if (results
.curl_result
!= CURLE_OK
) {
1027 return error("Unable to get pack index %s\n%s", url
,
1033 return error("Unable to start request");
1039 return move_temp_to_file(tmpfile
, filename
);
1042 static int setup_index(unsigned char *sha1
)
1044 struct packed_git
*new_pack
;
1046 if (fetch_index(sha1
))
1049 new_pack
= parse_pack_index(sha1
);
1050 new_pack
->next
= repo
->packs
;
1051 repo
->packs
= new_pack
;
1055 static int fetch_indices(void)
1057 unsigned char sha1
[20];
1059 struct strbuf buffer
= STRBUF_INIT
;
1063 struct active_request_slot
*slot
;
1064 struct slot_results results
;
1067 fprintf(stderr
, "Getting pack list\n");
1069 url
= xmalloc(strlen(repo
->url
) + 20);
1070 sprintf(url
, "%sobjects/info/packs", repo
->url
);
1072 slot
= get_active_slot();
1073 slot
->results
= &results
;
1074 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1075 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1076 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1077 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1078 if (start_active_slot(slot
)) {
1079 run_active_slot(slot
);
1080 if (results
.curl_result
!= CURLE_OK
) {
1081 strbuf_release(&buffer
);
1083 if (results
.http_code
== 404)
1086 return error("%s", curl_errorstr
);
1089 strbuf_release(&buffer
);
1091 return error("Unable to start request");
1096 while (i
< buffer
.len
) {
1100 if (i
+ 52 < buffer
.len
&&
1101 !prefixcmp(data
+ i
, " pack-") &&
1102 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1103 get_sha1_hex(data
+ i
+ 6, sha1
);
1109 while (data
[i
] != '\n')
1115 strbuf_release(&buffer
);
1119 static void one_remote_object(const char *hex
)
1121 unsigned char sha1
[20];
1124 if (get_sha1_hex(hex
, sha1
) != 0)
1127 obj
= lookup_object(sha1
);
1129 obj
= parse_object(sha1
);
1131 /* Ignore remote objects that don't exist locally */
1135 obj
->flags
|= REMOTE
;
1136 if (!object_list_contains(objects
, obj
))
1137 object_list_insert(obj
, &objects
);
1140 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1142 int *lock_flags
= (int *)ctx
->userData
;
1145 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
1146 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
1147 (*lock_flags
& DAV_PROP_LOCKWR
)) {
1148 *lock_flags
|= DAV_LOCK_OK
;
1150 *lock_flags
&= DAV_LOCK_OK
;
1151 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
1152 *lock_flags
|= DAV_PROP_LOCKWR
;
1153 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
1154 *lock_flags
|= DAV_PROP_LOCKEX
;
1159 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1161 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
1162 git_SHA_CTX sha_ctx
;
1163 unsigned char lock_token_sha1
[20];
1165 if (tag_closed
&& ctx
->cdata
) {
1166 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
1167 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
1168 strcpy(lock
->owner
, ctx
->cdata
);
1169 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
1170 if (!prefixcmp(ctx
->cdata
, "Second-"))
1172 strtol(ctx
->cdata
+ 7, NULL
, 10);
1173 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
1174 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
1175 strcpy(lock
->token
, ctx
->cdata
);
1177 git_SHA1_Init(&sha_ctx
);
1178 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
1179 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
1181 lock
->tmpfile_suffix
[0] = '_';
1182 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
1187 static void one_remote_ref(char *refname
);
1190 xml_start_tag(void *userData
, const char *name
, const char **atts
)
1192 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1193 const char *c
= strchr(name
, ':');
1201 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
1203 if (new_len
> ctx
->len
) {
1204 ctx
->name
= xrealloc(ctx
->name
, new_len
);
1207 strcat(ctx
->name
, ".");
1208 strcat(ctx
->name
, c
);
1213 ctx
->userFunc(ctx
, 0);
1217 xml_end_tag(void *userData
, const char *name
)
1219 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1220 const char *c
= strchr(name
, ':');
1223 ctx
->userFunc(ctx
, 1);
1230 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
1235 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
1237 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1239 ctx
->cdata
= xmemdupz(s
, len
);
1242 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
1244 struct active_request_slot
*slot
;
1245 struct slot_results results
;
1246 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1247 struct strbuf in_buffer
= STRBUF_INIT
;
1250 char timeout_header
[25];
1251 struct remote_lock
*lock
= NULL
;
1252 struct curl_slist
*dav_headers
= NULL
;
1256 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1257 sprintf(url
, "%s%s", repo
->url
, path
);
1259 /* Make sure leading directories exist for the remote ref */
1260 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
1262 char saved_character
= ep
[1];
1264 slot
= get_active_slot();
1265 slot
->results
= &results
;
1266 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1267 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1268 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1269 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1270 if (start_active_slot(slot
)) {
1271 run_active_slot(slot
);
1272 if (results
.curl_result
!= CURLE_OK
&&
1273 results
.http_code
!= 405) {
1275 "Unable to create branch path %s\n",
1281 fprintf(stderr
, "Unable to start MKCOL request\n");
1285 ep
[1] = saved_character
;
1286 ep
= strchr(ep
+ 1, '/');
1289 escaped
= xml_entities(git_default_email
);
1290 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
1293 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1294 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1295 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1297 slot
= get_active_slot();
1298 slot
->results
= &results
;
1299 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1300 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1301 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1302 #ifndef NO_CURL_IOCTL
1303 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1304 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1306 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1307 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1308 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1309 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1310 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1311 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1313 lock
= xcalloc(1, sizeof(*lock
));
1316 if (start_active_slot(slot
)) {
1317 run_active_slot(slot
);
1318 if (results
.curl_result
== CURLE_OK
) {
1319 XML_Parser parser
= XML_ParserCreate(NULL
);
1320 enum XML_Status result
;
1321 ctx
.name
= xcalloc(10, 1);
1324 ctx
.userFunc
= handle_new_lock_ctx
;
1325 ctx
.userData
= lock
;
1326 XML_SetUserData(parser
, &ctx
);
1327 XML_SetElementHandler(parser
, xml_start_tag
,
1329 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1330 result
= XML_Parse(parser
, in_buffer
.buf
,
1333 if (result
!= XML_STATUS_OK
) {
1334 fprintf(stderr
, "XML error: %s\n",
1336 XML_GetErrorCode(parser
)));
1339 XML_ParserFree(parser
);
1342 fprintf(stderr
, "Unable to start LOCK request\n");
1345 curl_slist_free_all(dav_headers
);
1346 strbuf_release(&out_buffer
.buf
);
1347 strbuf_release(&in_buffer
);
1349 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1357 lock
->start_time
= time(NULL
);
1358 lock
->next
= repo
->locks
;
1365 static int unlock_remote(struct remote_lock
*lock
)
1367 struct active_request_slot
*slot
;
1368 struct slot_results results
;
1369 struct remote_lock
*prev
= repo
->locks
;
1370 struct curl_slist
*dav_headers
;
1373 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
1375 slot
= get_active_slot();
1376 slot
->results
= &results
;
1377 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1378 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1379 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1380 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1382 if (start_active_slot(slot
)) {
1383 run_active_slot(slot
);
1384 if (results
.curl_result
== CURLE_OK
)
1387 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1390 fprintf(stderr
, "Unable to start UNLOCK request\n");
1393 curl_slist_free_all(dav_headers
);
1395 if (repo
->locks
== lock
) {
1396 repo
->locks
= lock
->next
;
1398 while (prev
&& prev
->next
!= lock
)
1401 prev
->next
= prev
->next
->next
;
1412 static void remove_locks(void)
1414 struct remote_lock
*lock
= repo
->locks
;
1416 fprintf(stderr
, "Removing remote locks...\n");
1418 unlock_remote(lock
);
1423 static void remove_locks_on_signal(int signo
)
1426 sigchain_pop(signo
);
1430 static void remote_ls(const char *path
, int flags
,
1431 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1434 static void process_ls_object(struct remote_ls_ctx
*ls
)
1436 unsigned int *parent
= (unsigned int *)ls
->userData
;
1437 char *path
= ls
->dentry_name
;
1440 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1441 remote_dir_exists
[*parent
] = 1;
1445 if (strlen(path
) != 49)
1448 obj_hex
= xmalloc(strlen(path
));
1449 /* NB: path is not null-terminated, can not use strlcpy here */
1450 memcpy(obj_hex
, path
, 2);
1451 strcpy(obj_hex
+ 2, path
+ 3);
1452 one_remote_object(obj_hex
);
1456 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1458 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1459 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1463 if (!(ls
->dentry_flags
& IS_DIR
))
1464 one_remote_ref(ls
->dentry_name
);
1467 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1469 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1472 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1473 if (ls
->dentry_flags
& IS_DIR
) {
1474 if (ls
->flags
& PROCESS_DIRS
) {
1477 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1478 ls
->flags
& RECURSIVE
) {
1479 remote_ls(ls
->dentry_name
,
1484 } else if (ls
->flags
& PROCESS_FILES
) {
1487 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1488 char *path
= ctx
->cdata
;
1489 if (*ctx
->cdata
== 'h') {
1490 path
= strstr(path
, "//");
1492 path
= strchr(path
+2, '/');
1496 path
+= repo
->path_len
;
1497 ls
->dentry_name
= xstrdup(path
);
1499 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1500 ls
->dentry_flags
|= IS_DIR
;
1502 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1503 free(ls
->dentry_name
);
1504 ls
->dentry_name
= NULL
;
1505 ls
->dentry_flags
= 0;
1510 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1511 * should _only_ heed the information from that file, instead of trying to
1512 * determine the refs from the remote file system (badly: it does not even
1513 * know about packed-refs).
1515 static void remote_ls(const char *path
, int flags
,
1516 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1519 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1520 struct active_request_slot
*slot
;
1521 struct slot_results results
;
1522 struct strbuf in_buffer
= STRBUF_INIT
;
1523 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1524 struct curl_slist
*dav_headers
= NULL
;
1526 struct remote_ls_ctx ls
;
1529 ls
.path
= xstrdup(path
);
1530 ls
.dentry_name
= NULL
;
1531 ls
.dentry_flags
= 0;
1532 ls
.userData
= userData
;
1533 ls
.userFunc
= userFunc
;
1535 sprintf(url
, "%s%s", repo
->url
, path
);
1537 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1539 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1540 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1542 slot
= get_active_slot();
1543 slot
->results
= &results
;
1544 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1545 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1546 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1547 #ifndef NO_CURL_IOCTL
1548 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1549 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1551 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1552 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1553 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1554 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1555 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1556 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1558 if (start_active_slot(slot
)) {
1559 run_active_slot(slot
);
1560 if (results
.curl_result
== CURLE_OK
) {
1561 XML_Parser parser
= XML_ParserCreate(NULL
);
1562 enum XML_Status result
;
1563 ctx
.name
= xcalloc(10, 1);
1566 ctx
.userFunc
= handle_remote_ls_ctx
;
1568 XML_SetUserData(parser
, &ctx
);
1569 XML_SetElementHandler(parser
, xml_start_tag
,
1571 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1572 result
= XML_Parse(parser
, in_buffer
.buf
,
1576 if (result
!= XML_STATUS_OK
) {
1577 fprintf(stderr
, "XML error: %s\n",
1579 XML_GetErrorCode(parser
)));
1581 XML_ParserFree(parser
);
1584 fprintf(stderr
, "Unable to start PROPFIND request\n");
1589 strbuf_release(&out_buffer
.buf
);
1590 strbuf_release(&in_buffer
);
1591 curl_slist_free_all(dav_headers
);
1594 static void get_remote_object_list(unsigned char parent
)
1596 char path
[] = "objects/XX/";
1597 static const char hex
[] = "0123456789abcdef";
1598 unsigned int val
= parent
;
1600 path
[8] = hex
[val
>> 4];
1601 path
[9] = hex
[val
& 0xf];
1602 remote_dir_exists
[val
] = 0;
1603 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1604 process_ls_object
, &val
);
1607 static int locking_available(void)
1609 struct active_request_slot
*slot
;
1610 struct slot_results results
;
1611 struct strbuf in_buffer
= STRBUF_INIT
;
1612 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1613 struct curl_slist
*dav_headers
= NULL
;
1618 escaped
= xml_entities(repo
->url
);
1619 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1622 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1623 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1625 slot
= get_active_slot();
1626 slot
->results
= &results
;
1627 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1628 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1629 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1630 #ifndef NO_CURL_IOCTL
1631 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1632 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1634 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1635 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1636 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1637 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1638 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1639 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1641 if (start_active_slot(slot
)) {
1642 run_active_slot(slot
);
1643 if (results
.curl_result
== CURLE_OK
) {
1644 XML_Parser parser
= XML_ParserCreate(NULL
);
1645 enum XML_Status result
;
1646 ctx
.name
= xcalloc(10, 1);
1649 ctx
.userFunc
= handle_lockprop_ctx
;
1650 ctx
.userData
= &lock_flags
;
1651 XML_SetUserData(parser
, &ctx
);
1652 XML_SetElementHandler(parser
, xml_start_tag
,
1654 result
= XML_Parse(parser
, in_buffer
.buf
,
1658 if (result
!= XML_STATUS_OK
) {
1659 fprintf(stderr
, "XML error: %s\n",
1661 XML_GetErrorCode(parser
)));
1664 XML_ParserFree(parser
);
1666 error("no DAV locking support on %s",
1670 error("Cannot access URL %s, return code %d",
1671 repo
->url
, results
.curl_result
);
1675 error("Unable to start PROPFIND request on %s", repo
->url
);
1678 strbuf_release(&out_buffer
.buf
);
1679 strbuf_release(&in_buffer
);
1680 curl_slist_free_all(dav_headers
);
1685 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1687 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1691 return &entry
->next
;
1694 static struct object_list
**process_blob(struct blob
*blob
,
1695 struct object_list
**p
,
1696 struct name_path
*path
,
1699 struct object
*obj
= &blob
->object
;
1701 obj
->flags
|= LOCAL
;
1703 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1707 return add_one_object(obj
, p
);
1710 static struct object_list
**process_tree(struct tree
*tree
,
1711 struct object_list
**p
,
1712 struct name_path
*path
,
1715 struct object
*obj
= &tree
->object
;
1716 struct tree_desc desc
;
1717 struct name_entry entry
;
1718 struct name_path me
;
1720 obj
->flags
|= LOCAL
;
1722 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1724 if (parse_tree(tree
) < 0)
1725 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1728 name
= xstrdup(name
);
1729 p
= add_one_object(obj
, p
);
1732 me
.elem_len
= strlen(name
);
1734 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1736 while (tree_entry(&desc
, &entry
))
1737 switch (object_type(entry
.mode
)) {
1739 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1742 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1745 /* Subproject commit - not in this repository */
1750 tree
->buffer
= NULL
;
1754 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1757 struct commit
*commit
;
1758 struct object_list
**p
= &objects
;
1761 while ((commit
= get_revision(revs
)) != NULL
) {
1762 p
= process_tree(commit
->tree
, p
, NULL
, "");
1763 commit
->object
.flags
|= LOCAL
;
1764 if (!(commit
->object
.flags
& UNINTERESTING
))
1765 count
+= add_send_request(&commit
->object
, lock
);
1768 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1769 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1770 struct object
*obj
= entry
->item
;
1771 const char *name
= entry
->name
;
1773 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1775 if (obj
->type
== OBJ_TAG
) {
1777 p
= add_one_object(obj
, p
);
1780 if (obj
->type
== OBJ_TREE
) {
1781 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1784 if (obj
->type
== OBJ_BLOB
) {
1785 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1788 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1792 if (!(objects
->item
->flags
& UNINTERESTING
))
1793 count
+= add_send_request(objects
->item
, lock
);
1794 objects
= objects
->next
;
1800 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1802 struct active_request_slot
*slot
;
1803 struct slot_results results
;
1804 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1805 struct curl_slist
*dav_headers
;
1807 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1809 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1811 slot
= get_active_slot();
1812 slot
->results
= &results
;
1813 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1814 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1815 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1816 #ifndef NO_CURL_IOCTL
1817 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1818 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1820 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1821 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1822 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1823 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1824 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1825 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1827 if (start_active_slot(slot
)) {
1828 run_active_slot(slot
);
1829 strbuf_release(&out_buffer
.buf
);
1830 if (results
.curl_result
!= CURLE_OK
) {
1832 "PUT error: curl result=%d, HTTP code=%ld\n",
1833 results
.curl_result
, results
.http_code
);
1834 /* We should attempt recovery? */
1838 strbuf_release(&out_buffer
.buf
);
1839 fprintf(stderr
, "Unable to start PUT request\n");
1846 static struct ref
*remote_refs
, **remote_tail
;
1848 static void one_remote_ref(char *refname
)
1853 ref
= alloc_ref(refname
);
1855 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1857 "Unable to fetch ref %s from %s\n",
1858 refname
, repo
->url
);
1864 * Fetch a copy of the object if it doesn't exist locally - it
1865 * may be required for updating server info later.
1867 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1868 obj
= lookup_unknown_object(ref
->old_sha1
);
1870 fprintf(stderr
, " fetch %s for %s\n",
1871 sha1_to_hex(ref
->old_sha1
), refname
);
1872 add_fetch_request(obj
);
1877 remote_tail
= &ref
->next
;
1880 static void get_dav_remote_heads(void)
1882 remote_tail
= &remote_refs
;
1883 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1886 static int is_zero_sha1(const unsigned char *sha1
)
1890 for (i
= 0; i
< 20; i
++) {
1897 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1899 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1905 ref
= alloc_ref(ls
->dentry_name
);
1907 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1909 "Unable to fetch ref %s from %s\n",
1910 ls
->dentry_name
, repo
->url
);
1916 o
= parse_object(ref
->old_sha1
);
1919 "Unable to parse object %s for remote ref %s\n",
1920 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1926 len
= strlen(ls
->dentry_name
) + 42;
1927 ref_info
= xcalloc(len
+ 1, 1);
1928 sprintf(ref_info
, "%s %s\n",
1929 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1930 fwrite_buffer(ref_info
, 1, len
, buf
);
1933 if (o
->type
== OBJ_TAG
) {
1934 o
= deref_tag(o
, ls
->dentry_name
, 0);
1936 len
= strlen(ls
->dentry_name
) + 45;
1937 ref_info
= xcalloc(len
+ 1, 1);
1938 sprintf(ref_info
, "%s %s^{}\n",
1939 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1940 fwrite_buffer(ref_info
, 1, len
, buf
);
1947 static void update_remote_info_refs(struct remote_lock
*lock
)
1949 struct buffer buffer
= { STRBUF_INIT
, 0 };
1950 struct active_request_slot
*slot
;
1951 struct slot_results results
;
1952 struct curl_slist
*dav_headers
;
1954 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1955 add_remote_info_ref
, &buffer
.buf
);
1957 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1959 slot
= get_active_slot();
1960 slot
->results
= &results
;
1961 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1962 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1963 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1964 #ifndef NO_CURL_IOCTL
1965 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1966 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1968 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1969 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1970 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1971 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1972 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1973 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1975 if (start_active_slot(slot
)) {
1976 run_active_slot(slot
);
1977 if (results
.curl_result
!= CURLE_OK
) {
1979 "PUT error: curl result=%d, HTTP code=%ld\n",
1980 results
.curl_result
, results
.http_code
);
1984 strbuf_release(&buffer
.buf
);
1987 static int remote_exists(const char *path
)
1989 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1990 struct active_request_slot
*slot
;
1991 struct slot_results results
;
1994 sprintf(url
, "%s%s", repo
->url
, path
);
1996 slot
= get_active_slot();
1997 slot
->results
= &results
;
1998 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1999 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
2001 if (start_active_slot(slot
)) {
2002 run_active_slot(slot
);
2003 if (results
.http_code
== 404)
2005 else if (results
.curl_result
== CURLE_OK
)
2008 fprintf(stderr
, "HEAD HTTP error %ld\n", results
.http_code
);
2010 fprintf(stderr
, "Unable to start HEAD request\n");
2017 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
2020 struct strbuf buffer
= STRBUF_INIT
;
2021 struct active_request_slot
*slot
;
2022 struct slot_results results
;
2024 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
2025 sprintf(url
, "%s%s", repo
->url
, path
);
2027 slot
= get_active_slot();
2028 slot
->results
= &results
;
2029 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
2030 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
2031 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
2032 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2033 if (start_active_slot(slot
)) {
2034 run_active_slot(slot
);
2035 if (results
.curl_result
!= CURLE_OK
) {
2036 die("Couldn't get %s for remote symref\n%s",
2037 url
, curl_errorstr
);
2040 die("Unable to start remote symref request");
2048 if (buffer
.len
== 0)
2051 /* If it's a symref, set the refname; otherwise try for a sha1 */
2052 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
2053 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
2055 get_sha1_hex(buffer
.buf
, sha1
);
2058 strbuf_release(&buffer
);
2061 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
2063 struct commit
*head
= lookup_commit(head_sha1
);
2064 struct commit
*branch
= lookup_commit(branch_sha1
);
2065 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
2067 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
2070 static int delete_remote_branch(char *pattern
, int force
)
2072 struct ref
*refs
= remote_refs
;
2073 struct ref
*remote_ref
= NULL
;
2074 unsigned char head_sha1
[20];
2075 char *symref
= NULL
;
2077 int patlen
= strlen(pattern
);
2079 struct active_request_slot
*slot
;
2080 struct slot_results results
;
2083 /* Find the remote branch(es) matching the specified branch name */
2084 for (match
= 0; refs
; refs
= refs
->next
) {
2085 char *name
= refs
->name
;
2086 int namelen
= strlen(name
);
2087 if (namelen
< patlen
||
2088 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
2090 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
2096 return error("No remote branch matches %s", pattern
);
2098 return error("More than one remote branch matches %s",
2102 * Remote HEAD must be a symref (not exactly foolproof; a remote
2103 * symlink to a symref will look like a symref)
2105 fetch_symref("HEAD", &symref
, head_sha1
);
2107 return error("Remote HEAD is not a symref");
2109 /* Remote branch must not be the remote HEAD */
2110 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
2111 if (!strcmp(remote_ref
->name
, symref
))
2112 return error("Remote branch %s is the current HEAD",
2114 fetch_symref(symref
, &symref
, head_sha1
);
2117 /* Run extra sanity checks if delete is not forced */
2119 /* Remote HEAD must resolve to a known object */
2121 return error("Remote HEAD symrefs too deep");
2122 if (is_zero_sha1(head_sha1
))
2123 return error("Unable to resolve remote HEAD");
2124 if (!has_sha1_file(head_sha1
))
2125 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
2127 /* Remote branch must resolve to a known object */
2128 if (is_zero_sha1(remote_ref
->old_sha1
))
2129 return error("Unable to resolve remote branch %s",
2131 if (!has_sha1_file(remote_ref
->old_sha1
))
2132 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
));
2134 /* Remote branch must be an ancestor of remote HEAD */
2135 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
2136 return error("The branch '%s' is not an ancestor "
2137 "of your current HEAD.\n"
2138 "If you are sure you want to delete it,"
2139 " run:\n\t'git http-push -D %s %s'",
2140 remote_ref
->name
, repo
->url
, pattern
);
2144 /* Send delete request */
2145 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
2148 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
2149 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
2150 slot
= get_active_slot();
2151 slot
->results
= &results
;
2152 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2153 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
2154 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2155 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
2156 if (start_active_slot(slot
)) {
2157 run_active_slot(slot
);
2159 if (results
.curl_result
!= CURLE_OK
)
2160 return error("DELETE request failed (%d/%ld)\n",
2161 results
.curl_result
, results
.http_code
);
2164 return error("Unable to start DELETE request");
2170 int main(int argc
, char **argv
)
2172 struct transfer_request
*request
;
2173 struct transfer_request
*next_request
;
2175 char **refspec
= NULL
;
2176 struct remote_lock
*ref_lock
= NULL
;
2177 struct remote_lock
*info_ref_lock
= NULL
;
2178 struct rev_info revs
;
2179 int delete_branch
= 0;
2180 int force_delete
= 0;
2181 int objects_to_send
;
2185 struct ref
*ref
, *local_refs
;
2186 struct remote
*remote
;
2187 char *rewritten_url
= NULL
;
2189 git_extract_argv0_path(argv
[0]);
2191 setup_git_directory();
2193 repo
= xcalloc(sizeof(*repo
), 1);
2196 for (i
= 1; i
< argc
; i
++, argv
++) {
2200 if (!strcmp(arg
, "--all")) {
2201 push_all
= MATCH_REFS_ALL
;
2204 if (!strcmp(arg
, "--force")) {
2208 if (!strcmp(arg
, "--dry-run")) {
2212 if (!strcmp(arg
, "--verbose")) {
2216 if (!strcmp(arg
, "-d")) {
2220 if (!strcmp(arg
, "-D")) {
2227 char *path
= strstr(arg
, "//");
2229 repo
->path_len
= strlen(arg
);
2231 repo
->path
= strchr(path
+2, '/');
2233 repo
->path_len
= strlen(repo
->path
);
2238 nr_refspec
= argc
- i
;
2242 #ifndef USE_CURL_MULTI
2243 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2247 usage(http_push_usage
);
2249 if (delete_branch
&& nr_refspec
!= 1)
2250 die("You must specify only one branch name when deleting a remote branch");
2252 memset(remote_dir_exists
, -1, 256);
2255 * Create a minimum remote by hand to give to http_init(),
2256 * primarily to allow it to look at the URL.
2258 remote
= xcalloc(sizeof(*remote
), 1);
2259 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
2260 remote
->url
[remote
->url_nr
++] = repo
->url
;
2263 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
2265 if (repo
->url
&& repo
->url
[strlen(repo
->url
)-1] != '/') {
2266 rewritten_url
= xmalloc(strlen(repo
->url
)+2);
2267 strcpy(rewritten_url
, repo
->url
);
2268 strcat(rewritten_url
, "/");
2269 repo
->path
= rewritten_url
+ (repo
->path
- repo
->url
);
2271 repo
->url
= rewritten_url
;
2274 /* Verify DAV compliance/lock support */
2275 if (!locking_available()) {
2280 sigchain_push_common(remove_locks_on_signal
);
2282 /* Check whether the remote has server info files */
2283 repo
->can_update_info_refs
= 0;
2284 repo
->has_info_refs
= remote_exists("info/refs");
2285 repo
->has_info_packs
= remote_exists("objects/info/packs");
2286 if (repo
->has_info_refs
) {
2287 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
2289 repo
->can_update_info_refs
= 1;
2291 error("cannot lock existing info/refs");
2296 if (repo
->has_info_packs
)
2299 /* Get a list of all local and remote heads to validate refspecs */
2300 local_refs
= get_local_heads();
2301 fprintf(stderr
, "Fetching remote heads...\n");
2302 get_dav_remote_heads();
2304 /* Remove a remote branch if -d or -D was specified */
2305 if (delete_branch
) {
2306 if (delete_remote_branch(refspec
[0], force_delete
) == -1)
2307 fprintf(stderr
, "Unable to delete remote branch %s\n",
2314 remote_tail
= &remote_refs
;
2315 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
2316 nr_refspec
, (const char **) refspec
, push_all
)) {
2321 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
2327 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2328 char old_hex
[60], *new_hex
;
2329 const char *commit_argv
[4];
2331 char *new_sha1_hex
, *old_sha1_hex
;
2336 if (is_zero_sha1(ref
->peer_ref
->new_sha1
)) {
2337 if (delete_remote_branch(ref
->name
, 1) == -1) {
2338 error("Could not remove %s", ref
->name
);
2345 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
2346 if (push_verbosely
|| 1)
2347 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
2352 !is_zero_sha1(ref
->old_sha1
) &&
2354 if (!has_sha1_file(ref
->old_sha1
) ||
2355 !ref_newer(ref
->peer_ref
->new_sha1
,
2358 * We do not have the remote ref, or
2359 * we know that the remote ref is not
2360 * an ancestor of what we are trying to
2361 * push. Either way this can be losing
2362 * commits at the remote end and likely
2363 * we were not up to date to begin with.
2365 error("remote '%s' is not an ancestor of\n"
2367 "Maybe you are not up-to-date and "
2368 "need to pull first?",
2370 ref
->peer_ref
->name
);
2375 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2377 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2378 new_hex
= sha1_to_hex(ref
->new_sha1
);
2380 fprintf(stderr
, "updating '%s'", ref
->name
);
2381 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2382 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2383 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2387 /* Lock remote branch ref */
2388 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2389 if (ref_lock
== NULL
) {
2390 fprintf(stderr
, "Unable to lock remote branch %s\n",
2396 /* Set up revision info for this refspec */
2398 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2399 old_sha1_hex
= NULL
;
2400 commit_argv
[1] = "--objects";
2401 commit_argv
[2] = new_sha1_hex
;
2402 if (!push_all
&& !is_zero_sha1(ref
->old_sha1
)) {
2403 old_sha1_hex
= xmalloc(42);
2404 sprintf(old_sha1_hex
, "^%s",
2405 sha1_to_hex(ref
->old_sha1
));
2406 commit_argv
[3] = old_sha1_hex
;
2409 init_revisions(&revs
, setup_git_directory());
2410 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2411 revs
.edge_hint
= 0; /* just in case */
2415 commit_argv
[1] = NULL
;
2418 /* Generate a list of objects that need to be pushed */
2420 if (prepare_revision_walk(&revs
))
2421 die("revision walk setup failed");
2422 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2423 objects_to_send
= get_delta(&revs
, ref_lock
);
2424 finish_all_active_slots();
2426 /* Push missing objects to remote, this would be a
2427 convenient time to pack them first if appropriate. */
2429 if (objects_to_send
)
2430 fprintf(stderr
, " sending %d objects\n",
2432 #ifdef USE_CURL_MULTI
2433 fill_active_slots();
2434 add_fill_function(NULL
, fill_active_slot
);
2437 finish_all_active_slots();
2438 #ifdef USE_CURL_MULTI
2439 fill_active_slots();
2441 } while (request_queue_head
&& !aborted
);
2443 /* Update the remote branch if all went well */
2444 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2448 fprintf(stderr
, " done\n");
2449 unlock_remote(ref_lock
);
2453 /* Update remote server info if appropriate */
2454 if (repo
->has_info_refs
&& new_refs
) {
2455 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2456 fprintf(stderr
, "Updating remote server info\n");
2458 update_remote_info_refs(info_ref_lock
);
2460 fprintf(stderr
, "Unable to update server info\n");
2465 free(rewritten_url
);
2467 unlock_remote(info_ref_lock
);
2470 curl_slist_free_all(no_pragma_header
);
2474 request
= request_queue_head
;
2475 while (request
!= NULL
) {
2476 next_request
= request
->next
;
2477 release_request(request
);
2478 request
= next_request
;