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 struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
191 struct strbuf buf
= STRBUF_INIT
;
192 struct curl_slist
*dav_headers
= NULL
;
194 if (options
& DAV_HEADER_IF
) {
195 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
196 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
199 if (options
& DAV_HEADER_LOCK
) {
200 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
201 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
204 if (options
& DAV_HEADER_TIMEOUT
) {
205 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
206 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
209 strbuf_release(&buf
);
214 static void append_remote_object_url(struct strbuf
*buf
, const char *url
,
216 int only_two_digit_prefix
)
218 strbuf_addf(buf
, "%sobjects/%.*s/", url
, 2, hex
);
219 if (!only_two_digit_prefix
)
220 strbuf_addf(buf
, "%s", hex
+2);
223 static void finish_request(struct transfer_request
*request
);
224 static void release_request(struct transfer_request
*request
);
226 static void process_response(void *callback_data
)
228 struct transfer_request
*request
=
229 (struct transfer_request
*)callback_data
;
231 finish_request(request
);
234 #ifdef USE_CURL_MULTI
236 static char *get_remote_object_url(const char *url
, const char *hex
,
237 int only_two_digit_prefix
)
239 struct strbuf buf
= STRBUF_INIT
;
240 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
241 return strbuf_detach(&buf
, NULL
);
244 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
247 unsigned char expn
[4096];
248 size_t size
= eltsize
* nmemb
;
250 struct transfer_request
*request
= (struct transfer_request
*)data
;
252 ssize_t retval
= xwrite(request
->local_fileno
,
253 (char *) ptr
+ posn
, size
- posn
);
257 } while (posn
< size
);
259 request
->stream
.avail_in
= size
;
260 request
->stream
.next_in
= ptr
;
262 request
->stream
.next_out
= expn
;
263 request
->stream
.avail_out
= sizeof(expn
);
264 request
->zret
= git_inflate(&request
->stream
, Z_SYNC_FLUSH
);
265 git_SHA1_Update(&request
->c
, expn
,
266 sizeof(expn
) - request
->stream
.avail_out
);
267 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
272 static void start_fetch_loose(struct transfer_request
*request
)
274 char *hex
= sha1_to_hex(request
->obj
->sha1
);
276 char prevfile
[PATH_MAX
];
279 unsigned char prev_buf
[PREV_BUF_SIZE
];
280 ssize_t prev_read
= 0;
282 char range
[RANGE_HEADER_SIZE
];
283 struct curl_slist
*range_header
= NULL
;
284 struct active_request_slot
*slot
;
286 filename
= sha1_file_name(request
->obj
->sha1
);
287 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
288 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
289 "%s.temp", filename
);
291 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
293 rename(request
->tmpfile
, prevfile
);
294 unlink(request
->tmpfile
);
296 if (request
->local_fileno
!= -1)
297 error("fd leakage in start: %d", request
->local_fileno
);
298 request
->local_fileno
= open(request
->tmpfile
,
299 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
300 /* This could have failed due to the "lazy directory creation";
301 * try to mkdir the last path component.
303 if (request
->local_fileno
< 0 && errno
== ENOENT
) {
304 char *dir
= strrchr(request
->tmpfile
, '/');
307 mkdir(request
->tmpfile
, 0777);
310 request
->local_fileno
= open(request
->tmpfile
,
311 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
314 if (request
->local_fileno
< 0) {
315 request
->state
= ABORTED
;
316 error("Couldn't create temporary file %s for %s: %s",
317 request
->tmpfile
, request
->filename
, strerror(errno
));
321 memset(&request
->stream
, 0, sizeof(request
->stream
));
323 git_inflate_init(&request
->stream
);
325 git_SHA1_Init(&request
->c
);
327 url
= get_remote_object_url(repo
->url
, hex
, 0);
328 request
->url
= xstrdup(url
);
330 /* If a previous temp file is present, process what was already
332 prevlocal
= open(prevfile
, O_RDONLY
);
333 if (prevlocal
!= -1) {
335 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
337 if (fwrite_sha1_file(prev_buf
,
340 request
) == prev_read
) {
341 prev_posn
+= prev_read
;
346 } while (prev_read
> 0);
351 /* Reset inflate/SHA1 if there was an error reading the previous temp
352 file; also rewind to the beginning of the local file. */
353 if (prev_read
== -1) {
354 memset(&request
->stream
, 0, sizeof(request
->stream
));
355 git_inflate_init(&request
->stream
);
356 git_SHA1_Init(&request
->c
);
359 lseek(request
->local_fileno
, 0, SEEK_SET
);
360 ftruncate(request
->local_fileno
, 0);
364 slot
= get_active_slot();
365 slot
->callback_func
= process_response
;
366 slot
->callback_data
= request
;
367 request
->slot
= slot
;
369 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
370 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
371 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
372 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
373 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
375 /* If we have successfully processed data from a previous fetch
376 attempt, only fetch the data we don't already have. */
380 "Resuming fetch of object %s at byte %ld\n",
382 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
383 range_header
= curl_slist_append(range_header
, range
);
384 curl_easy_setopt(slot
->curl
,
385 CURLOPT_HTTPHEADER
, range_header
);
388 /* Try to get the request started, abort the request on error */
389 request
->state
= RUN_FETCH_LOOSE
;
390 if (!start_active_slot(slot
)) {
391 fprintf(stderr
, "Unable to start GET request\n");
392 repo
->can_update_info_refs
= 0;
393 release_request(request
);
397 static void start_mkcol(struct transfer_request
*request
)
399 char *hex
= sha1_to_hex(request
->obj
->sha1
);
400 struct active_request_slot
*slot
;
402 request
->url
= get_remote_object_url(repo
->url
, hex
, 1);
404 slot
= get_active_slot();
405 slot
->callback_func
= process_response
;
406 slot
->callback_data
= request
;
407 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
408 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
409 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
410 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
411 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
413 if (start_active_slot(slot
)) {
414 request
->slot
= slot
;
415 request
->state
= RUN_MKCOL
;
417 request
->state
= ABORTED
;
424 static void start_fetch_packed(struct transfer_request
*request
)
427 struct packed_git
*target
;
431 char range
[RANGE_HEADER_SIZE
];
432 struct curl_slist
*range_header
= NULL
;
434 struct transfer_request
*check_request
= request_queue_head
;
435 struct active_request_slot
*slot
;
437 target
= find_sha1_pack(request
->obj
->sha1
, repo
->packs
);
439 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
440 repo
->can_update_info_refs
= 0;
441 release_request(request
);
445 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
446 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
448 filename
= sha1_pack_name(target
->sha1
);
449 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
450 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
451 "%s.temp", filename
);
453 url
= xmalloc(strlen(repo
->url
) + 64);
454 sprintf(url
, "%sobjects/pack/pack-%s.pack",
455 repo
->url
, sha1_to_hex(target
->sha1
));
457 /* Make sure there isn't another open request for this pack */
458 while (check_request
) {
459 if (check_request
->state
== RUN_FETCH_PACKED
&&
460 !strcmp(check_request
->url
, url
)) {
462 release_request(request
);
465 check_request
= check_request
->next
;
468 packfile
= fopen(request
->tmpfile
, "a");
470 fprintf(stderr
, "Unable to open local file %s for pack",
472 repo
->can_update_info_refs
= 0;
477 slot
= get_active_slot();
478 slot
->callback_func
= process_response
;
479 slot
->callback_data
= request
;
480 request
->slot
= slot
;
481 request
->local_stream
= packfile
;
482 request
->userData
= target
;
485 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
486 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
487 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
488 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
489 slot
->local
= packfile
;
491 /* If there is data present from a previous transfer attempt,
492 resume where it left off */
493 prev_posn
= ftell(packfile
);
497 "Resuming fetch of pack %s at byte %ld\n",
498 sha1_to_hex(target
->sha1
), prev_posn
);
499 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
500 range_header
= curl_slist_append(range_header
, range
);
501 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
504 /* Try to get the request started, abort the request on error */
505 request
->state
= RUN_FETCH_PACKED
;
506 if (!start_active_slot(slot
)) {
507 fprintf(stderr
, "Unable to start GET request\n");
508 repo
->can_update_info_refs
= 0;
509 release_request(request
);
513 static void start_put(struct transfer_request
*request
)
515 char *hex
= sha1_to_hex(request
->obj
->sha1
);
516 struct active_request_slot
*slot
;
517 struct strbuf buf
= STRBUF_INIT
;
518 enum object_type type
;
526 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
527 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
530 memset(&stream
, 0, sizeof(stream
));
531 deflateInit(&stream
, zlib_compression_level
);
532 size
= deflateBound(&stream
, len
+ hdrlen
);
533 strbuf_init(&request
->buffer
.buf
, size
);
534 request
->buffer
.posn
= 0;
537 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
538 stream
.avail_out
= size
;
541 stream
.next_in
= (void *)hdr
;
542 stream
.avail_in
= hdrlen
;
543 while (deflate(&stream
, 0) == Z_OK
)
546 /* Then the data itself.. */
547 stream
.next_in
= unpacked
;
548 stream
.avail_in
= len
;
549 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
554 request
->buffer
.buf
.len
= stream
.total_out
;
556 strbuf_addstr(&buf
, "Destination: ");
557 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
558 request
->dest
= strbuf_detach(&buf
, NULL
);
560 append_remote_object_url(&buf
, repo
->url
, hex
, 0);
561 strbuf_add(&buf
, request
->lock
->tmpfile_suffix
, 41);
562 request
->url
= strbuf_detach(&buf
, NULL
);
564 slot
= get_active_slot();
565 slot
->callback_func
= process_response
;
566 slot
->callback_data
= request
;
567 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
568 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
569 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
570 #ifndef NO_CURL_IOCTL
571 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
572 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &request
->buffer
);
574 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
575 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
576 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
577 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
578 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
579 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
581 if (start_active_slot(slot
)) {
582 request
->slot
= slot
;
583 request
->state
= RUN_PUT
;
585 request
->state
= ABORTED
;
591 static void start_move(struct transfer_request
*request
)
593 struct active_request_slot
*slot
;
594 struct curl_slist
*dav_headers
= NULL
;
596 slot
= get_active_slot();
597 slot
->callback_func
= process_response
;
598 slot
->callback_data
= request
;
599 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
600 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
601 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
602 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
603 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
604 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
605 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
607 if (start_active_slot(slot
)) {
608 request
->slot
= slot
;
609 request
->state
= RUN_MOVE
;
611 request
->state
= ABORTED
;
617 static int refresh_lock(struct remote_lock
*lock
)
619 struct active_request_slot
*slot
;
620 struct slot_results results
;
621 struct curl_slist
*dav_headers
;
624 lock
->refreshing
= 1;
626 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
628 slot
= get_active_slot();
629 slot
->results
= &results
;
630 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
631 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
632 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
633 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
634 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
636 if (start_active_slot(slot
)) {
637 run_active_slot(slot
);
638 if (results
.curl_result
!= CURLE_OK
) {
639 fprintf(stderr
, "LOCK HTTP error %ld\n",
642 lock
->start_time
= time(NULL
);
647 lock
->refreshing
= 0;
648 curl_slist_free_all(dav_headers
);
653 static void check_locks(void)
655 struct remote_lock
*lock
= repo
->locks
;
656 time_t current_time
= time(NULL
);
660 time_remaining
= lock
->start_time
+ lock
->timeout
-
662 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
663 if (!refresh_lock(lock
)) {
665 "Unable to refresh lock for %s\n",
675 static void release_request(struct transfer_request
*request
)
677 struct transfer_request
*entry
= request_queue_head
;
679 if (request
== request_queue_head
) {
680 request_queue_head
= request
->next
;
682 while (entry
->next
!= NULL
&& entry
->next
!= request
)
684 if (entry
->next
== request
)
685 entry
->next
= entry
->next
->next
;
688 if (request
->local_fileno
!= -1)
689 close(request
->local_fileno
);
690 if (request
->local_stream
)
691 fclose(request
->local_stream
);
696 static void finish_request(struct transfer_request
*request
)
699 struct packed_git
*target
;
700 struct packed_git
**lst
;
702 request
->curl_result
= request
->slot
->curl_result
;
703 request
->http_code
= request
->slot
->http_code
;
704 request
->slot
= NULL
;
706 /* Keep locks active */
709 if (request
->headers
!= NULL
)
710 curl_slist_free_all(request
->headers
);
712 /* URL is reused for MOVE after PUT */
713 if (request
->state
!= RUN_PUT
) {
718 if (request
->state
== RUN_MKCOL
) {
719 if (request
->curl_result
== CURLE_OK
||
720 request
->http_code
== 405) {
721 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
724 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
725 sha1_to_hex(request
->obj
->sha1
),
726 request
->curl_result
, request
->http_code
);
727 request
->state
= ABORTED
;
730 } else if (request
->state
== RUN_PUT
) {
731 if (request
->curl_result
== CURLE_OK
) {
734 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
735 sha1_to_hex(request
->obj
->sha1
),
736 request
->curl_result
, request
->http_code
);
737 request
->state
= ABORTED
;
740 } else if (request
->state
== RUN_MOVE
) {
741 if (request
->curl_result
== CURLE_OK
) {
743 fprintf(stderr
, " sent %s\n",
744 sha1_to_hex(request
->obj
->sha1
));
745 request
->obj
->flags
|= REMOTE
;
746 release_request(request
);
748 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
749 sha1_to_hex(request
->obj
->sha1
),
750 request
->curl_result
, request
->http_code
);
751 request
->state
= ABORTED
;
754 } else if (request
->state
== RUN_FETCH_LOOSE
) {
755 fchmod(request
->local_fileno
, 0444);
756 close(request
->local_fileno
); request
->local_fileno
= -1;
758 if (request
->curl_result
!= CURLE_OK
&&
759 request
->http_code
!= 416) {
760 if (stat(request
->tmpfile
, &st
) == 0) {
762 unlink(request
->tmpfile
);
765 if (request
->http_code
== 416)
766 warning("requested range invalid; we may already have all the data.");
768 git_inflate_end(&request
->stream
);
769 git_SHA1_Final(request
->real_sha1
, &request
->c
);
770 if (request
->zret
!= Z_STREAM_END
) {
771 unlink(request
->tmpfile
);
772 } else if (hashcmp(request
->obj
->sha1
, request
->real_sha1
)) {
773 unlink(request
->tmpfile
);
779 if (request
->rename
== 0) {
780 request
->obj
->flags
|= (LOCAL
| REMOTE
);
785 /* Try fetching packed if necessary */
786 if (request
->obj
->flags
& LOCAL
)
787 release_request(request
);
789 start_fetch_packed(request
);
791 } else if (request
->state
== RUN_FETCH_PACKED
) {
792 if (request
->curl_result
!= CURLE_OK
) {
793 fprintf(stderr
, "Unable to get pack file %s\n%s",
794 request
->url
, curl_errorstr
);
795 repo
->can_update_info_refs
= 0;
797 off_t pack_size
= ftell(request
->local_stream
);
799 fclose(request
->local_stream
);
800 request
->local_stream
= NULL
;
801 if (!move_temp_to_file(request
->tmpfile
,
802 request
->filename
)) {
803 target
= (struct packed_git
*)request
->userData
;
804 target
->pack_size
= pack_size
;
806 while (*lst
!= target
)
807 lst
= &((*lst
)->next
);
810 if (!verify_pack(target
))
811 install_packed_git(target
);
813 repo
->can_update_info_refs
= 0;
816 release_request(request
);
820 #ifdef USE_CURL_MULTI
821 static int fill_active_slot(void *unused
)
823 struct transfer_request
*request
;
828 for (request
= request_queue_head
; request
; request
= request
->next
) {
829 if (request
->state
== NEED_FETCH
) {
830 start_fetch_loose(request
);
832 } else if (pushing
&& request
->state
== NEED_PUSH
) {
833 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
836 start_mkcol(request
);
845 static void get_remote_object_list(unsigned char parent
);
847 static void add_fetch_request(struct object
*obj
)
849 struct transfer_request
*request
;
854 * Don't fetch the object if it's known to exist locally
855 * or is already in the request queue
857 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
858 get_remote_object_list(obj
->sha1
[0]);
859 if (obj
->flags
& (LOCAL
| FETCHING
))
862 obj
->flags
|= FETCHING
;
863 request
= xmalloc(sizeof(*request
));
866 request
->lock
= NULL
;
867 request
->headers
= NULL
;
868 request
->local_fileno
= -1;
869 request
->local_stream
= NULL
;
870 request
->state
= NEED_FETCH
;
871 request
->next
= request_queue_head
;
872 request_queue_head
= request
;
874 #ifdef USE_CURL_MULTI
880 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
882 struct transfer_request
*request
= request_queue_head
;
883 struct packed_git
*target
;
885 /* Keep locks active */
889 * Don't push the object if it's known to exist on the remote
890 * or is already in the request queue
892 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
893 get_remote_object_list(obj
->sha1
[0]);
894 if (obj
->flags
& (REMOTE
| PUSHING
))
896 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
898 obj
->flags
|= REMOTE
;
902 obj
->flags
|= PUSHING
;
903 request
= xmalloc(sizeof(*request
));
906 request
->lock
= lock
;
907 request
->headers
= NULL
;
908 request
->local_fileno
= -1;
909 request
->local_stream
= NULL
;
910 request
->state
= NEED_PUSH
;
911 request
->next
= request_queue_head
;
912 request_queue_head
= request
;
914 #ifdef USE_CURL_MULTI
922 static int fetch_index(unsigned char *sha1
)
924 char *hex
= sha1_to_hex(sha1
);
927 char tmpfile
[PATH_MAX
];
929 char range
[RANGE_HEADER_SIZE
];
930 struct curl_slist
*range_header
= NULL
;
933 struct active_request_slot
*slot
;
934 struct slot_results results
;
936 /* Don't use the index if the pack isn't there */
937 url
= xmalloc(strlen(repo
->url
) + 64);
938 sprintf(url
, "%sobjects/pack/pack-%s.pack", repo
->url
, hex
);
939 slot
= get_active_slot();
940 slot
->results
= &results
;
941 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
942 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
943 if (start_active_slot(slot
)) {
944 run_active_slot(slot
);
945 if (results
.curl_result
!= CURLE_OK
) {
947 return error("Unable to verify pack %s is available",
952 return error("Unable to start request");
955 if (has_pack_index(sha1
)) {
961 fprintf(stderr
, "Getting index for pack %s\n", hex
);
963 sprintf(url
, "%sobjects/pack/pack-%s.idx", repo
->url
, hex
);
965 filename
= sha1_pack_index_name(sha1
);
966 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
967 indexfile
= fopen(tmpfile
, "a");
970 return error("Unable to open local file %s for pack index",
974 slot
= get_active_slot();
975 slot
->results
= &results
;
976 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
977 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
978 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
979 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
980 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
981 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
982 slot
->local
= indexfile
;
984 /* If there is data present from a previous transfer attempt,
985 resume where it left off */
986 prev_posn
= ftell(indexfile
);
990 "Resuming fetch of index for pack %s at byte %ld\n",
992 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
993 range_header
= curl_slist_append(range_header
, range
);
994 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
997 if (start_active_slot(slot
)) {
998 run_active_slot(slot
);
999 if (results
.curl_result
!= CURLE_OK
) {
1002 return error("Unable to get pack index %s\n%s", url
,
1008 return error("Unable to start request");
1014 return move_temp_to_file(tmpfile
, filename
);
1017 static int setup_index(unsigned char *sha1
)
1019 struct packed_git
*new_pack
;
1021 if (fetch_index(sha1
))
1024 new_pack
= parse_pack_index(sha1
);
1025 new_pack
->next
= repo
->packs
;
1026 repo
->packs
= new_pack
;
1030 static int fetch_indices(void)
1032 unsigned char sha1
[20];
1034 struct strbuf buffer
= STRBUF_INIT
;
1038 struct active_request_slot
*slot
;
1039 struct slot_results results
;
1042 fprintf(stderr
, "Getting pack list\n");
1044 url
= xmalloc(strlen(repo
->url
) + 20);
1045 sprintf(url
, "%sobjects/info/packs", repo
->url
);
1047 slot
= get_active_slot();
1048 slot
->results
= &results
;
1049 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1050 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1051 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1052 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1053 if (start_active_slot(slot
)) {
1054 run_active_slot(slot
);
1055 if (results
.curl_result
!= CURLE_OK
) {
1056 strbuf_release(&buffer
);
1058 if (results
.http_code
== 404)
1061 return error("%s", curl_errorstr
);
1064 strbuf_release(&buffer
);
1066 return error("Unable to start request");
1071 while (i
< buffer
.len
) {
1075 if (i
+ 52 < buffer
.len
&&
1076 !prefixcmp(data
+ i
, " pack-") &&
1077 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1078 get_sha1_hex(data
+ i
+ 6, sha1
);
1084 while (data
[i
] != '\n')
1090 strbuf_release(&buffer
);
1094 static void one_remote_object(const char *hex
)
1096 unsigned char sha1
[20];
1099 if (get_sha1_hex(hex
, sha1
) != 0)
1102 obj
= lookup_object(sha1
);
1104 obj
= parse_object(sha1
);
1106 /* Ignore remote objects that don't exist locally */
1110 obj
->flags
|= REMOTE
;
1111 if (!object_list_contains(objects
, obj
))
1112 object_list_insert(obj
, &objects
);
1115 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1117 int *lock_flags
= (int *)ctx
->userData
;
1120 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
1121 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
1122 (*lock_flags
& DAV_PROP_LOCKWR
)) {
1123 *lock_flags
|= DAV_LOCK_OK
;
1125 *lock_flags
&= DAV_LOCK_OK
;
1126 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
1127 *lock_flags
|= DAV_PROP_LOCKWR
;
1128 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
1129 *lock_flags
|= DAV_PROP_LOCKEX
;
1134 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1136 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
1137 git_SHA_CTX sha_ctx
;
1138 unsigned char lock_token_sha1
[20];
1140 if (tag_closed
&& ctx
->cdata
) {
1141 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
1142 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
1143 strcpy(lock
->owner
, ctx
->cdata
);
1144 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
1145 if (!prefixcmp(ctx
->cdata
, "Second-"))
1147 strtol(ctx
->cdata
+ 7, NULL
, 10);
1148 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
1149 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
1150 strcpy(lock
->token
, ctx
->cdata
);
1152 git_SHA1_Init(&sha_ctx
);
1153 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
1154 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
1156 lock
->tmpfile_suffix
[0] = '_';
1157 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
1162 static void one_remote_ref(char *refname
);
1165 xml_start_tag(void *userData
, const char *name
, const char **atts
)
1167 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1168 const char *c
= strchr(name
, ':');
1176 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
1178 if (new_len
> ctx
->len
) {
1179 ctx
->name
= xrealloc(ctx
->name
, new_len
);
1182 strcat(ctx
->name
, ".");
1183 strcat(ctx
->name
, c
);
1188 ctx
->userFunc(ctx
, 0);
1192 xml_end_tag(void *userData
, const char *name
)
1194 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1195 const char *c
= strchr(name
, ':');
1198 ctx
->userFunc(ctx
, 1);
1205 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
1210 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
1212 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1214 ctx
->cdata
= xmemdupz(s
, len
);
1217 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
1219 struct active_request_slot
*slot
;
1220 struct slot_results results
;
1221 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1222 struct strbuf in_buffer
= STRBUF_INIT
;
1225 char timeout_header
[25];
1226 struct remote_lock
*lock
= NULL
;
1227 struct curl_slist
*dav_headers
= NULL
;
1230 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1231 sprintf(url
, "%s%s", repo
->url
, path
);
1233 /* Make sure leading directories exist for the remote ref */
1234 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
1236 char saved_character
= ep
[1];
1238 slot
= get_active_slot();
1239 slot
->results
= &results
;
1240 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1241 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1242 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1243 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1244 if (start_active_slot(slot
)) {
1245 run_active_slot(slot
);
1246 if (results
.curl_result
!= CURLE_OK
&&
1247 results
.http_code
!= 405) {
1249 "Unable to create branch path %s\n",
1255 fprintf(stderr
, "Unable to start MKCOL request\n");
1259 ep
[1] = saved_character
;
1260 ep
= strchr(ep
+ 1, '/');
1263 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, git_default_email
);
1265 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1266 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1267 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1269 slot
= get_active_slot();
1270 slot
->results
= &results
;
1271 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1272 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1273 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1274 #ifndef NO_CURL_IOCTL
1275 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1276 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1278 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1279 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1280 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1281 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1282 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1283 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1285 lock
= xcalloc(1, sizeof(*lock
));
1288 if (start_active_slot(slot
)) {
1289 run_active_slot(slot
);
1290 if (results
.curl_result
== CURLE_OK
) {
1291 XML_Parser parser
= XML_ParserCreate(NULL
);
1292 enum XML_Status result
;
1293 ctx
.name
= xcalloc(10, 1);
1296 ctx
.userFunc
= handle_new_lock_ctx
;
1297 ctx
.userData
= lock
;
1298 XML_SetUserData(parser
, &ctx
);
1299 XML_SetElementHandler(parser
, xml_start_tag
,
1301 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1302 result
= XML_Parse(parser
, in_buffer
.buf
,
1305 if (result
!= XML_STATUS_OK
) {
1306 fprintf(stderr
, "XML error: %s\n",
1308 XML_GetErrorCode(parser
)));
1311 XML_ParserFree(parser
);
1314 fprintf(stderr
, "Unable to start LOCK request\n");
1317 curl_slist_free_all(dav_headers
);
1318 strbuf_release(&out_buffer
.buf
);
1319 strbuf_release(&in_buffer
);
1321 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1329 lock
->start_time
= time(NULL
);
1330 lock
->next
= repo
->locks
;
1337 static int unlock_remote(struct remote_lock
*lock
)
1339 struct active_request_slot
*slot
;
1340 struct slot_results results
;
1341 struct remote_lock
*prev
= repo
->locks
;
1342 struct curl_slist
*dav_headers
;
1345 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
1347 slot
= get_active_slot();
1348 slot
->results
= &results
;
1349 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1350 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1351 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1352 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1354 if (start_active_slot(slot
)) {
1355 run_active_slot(slot
);
1356 if (results
.curl_result
== CURLE_OK
)
1359 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1362 fprintf(stderr
, "Unable to start UNLOCK request\n");
1365 curl_slist_free_all(dav_headers
);
1367 if (repo
->locks
== lock
) {
1368 repo
->locks
= lock
->next
;
1370 while (prev
&& prev
->next
!= lock
)
1373 prev
->next
= prev
->next
->next
;
1384 static void remove_locks(void)
1386 struct remote_lock
*lock
= repo
->locks
;
1388 fprintf(stderr
, "Removing remote locks...\n");
1390 unlock_remote(lock
);
1395 static void remove_locks_on_signal(int signo
)
1398 sigchain_pop(signo
);
1402 static void remote_ls(const char *path
, int flags
,
1403 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1406 static void process_ls_object(struct remote_ls_ctx
*ls
)
1408 unsigned int *parent
= (unsigned int *)ls
->userData
;
1409 char *path
= ls
->dentry_name
;
1412 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1413 remote_dir_exists
[*parent
] = 1;
1417 if (strlen(path
) != 49)
1420 obj_hex
= xmalloc(strlen(path
));
1421 /* NB: path is not null-terminated, can not use strlcpy here */
1422 memcpy(obj_hex
, path
, 2);
1423 strcpy(obj_hex
+ 2, path
+ 3);
1424 one_remote_object(obj_hex
);
1428 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1430 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1431 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1435 if (!(ls
->dentry_flags
& IS_DIR
))
1436 one_remote_ref(ls
->dentry_name
);
1439 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1441 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1444 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1445 if (ls
->dentry_flags
& IS_DIR
) {
1446 if (ls
->flags
& PROCESS_DIRS
) {
1449 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1450 ls
->flags
& RECURSIVE
) {
1451 remote_ls(ls
->dentry_name
,
1456 } else if (ls
->flags
& PROCESS_FILES
) {
1459 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1460 char *path
= ctx
->cdata
;
1461 if (*ctx
->cdata
== 'h') {
1462 path
= strstr(path
, "//");
1464 path
= strchr(path
+2, '/');
1468 path
+= repo
->path_len
;
1469 ls
->dentry_name
= xstrdup(path
);
1471 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1472 ls
->dentry_flags
|= IS_DIR
;
1474 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1475 free(ls
->dentry_name
);
1476 ls
->dentry_name
= NULL
;
1477 ls
->dentry_flags
= 0;
1482 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1483 * should _only_ heed the information from that file, instead of trying to
1484 * determine the refs from the remote file system (badly: it does not even
1485 * know about packed-refs).
1487 static void remote_ls(const char *path
, int flags
,
1488 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1491 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1492 struct active_request_slot
*slot
;
1493 struct slot_results results
;
1494 struct strbuf in_buffer
= STRBUF_INIT
;
1495 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1496 struct curl_slist
*dav_headers
= NULL
;
1498 struct remote_ls_ctx ls
;
1501 ls
.path
= xstrdup(path
);
1502 ls
.dentry_name
= NULL
;
1503 ls
.dentry_flags
= 0;
1504 ls
.userData
= userData
;
1505 ls
.userFunc
= userFunc
;
1507 sprintf(url
, "%s%s", repo
->url
, path
);
1509 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1511 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1512 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1514 slot
= get_active_slot();
1515 slot
->results
= &results
;
1516 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1517 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1518 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1519 #ifndef NO_CURL_IOCTL
1520 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1521 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1523 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1524 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1525 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1526 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1527 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1528 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1530 if (start_active_slot(slot
)) {
1531 run_active_slot(slot
);
1532 if (results
.curl_result
== CURLE_OK
) {
1533 XML_Parser parser
= XML_ParserCreate(NULL
);
1534 enum XML_Status result
;
1535 ctx
.name
= xcalloc(10, 1);
1538 ctx
.userFunc
= handle_remote_ls_ctx
;
1540 XML_SetUserData(parser
, &ctx
);
1541 XML_SetElementHandler(parser
, xml_start_tag
,
1543 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1544 result
= XML_Parse(parser
, in_buffer
.buf
,
1548 if (result
!= XML_STATUS_OK
) {
1549 fprintf(stderr
, "XML error: %s\n",
1551 XML_GetErrorCode(parser
)));
1553 XML_ParserFree(parser
);
1556 fprintf(stderr
, "Unable to start PROPFIND request\n");
1561 strbuf_release(&out_buffer
.buf
);
1562 strbuf_release(&in_buffer
);
1563 curl_slist_free_all(dav_headers
);
1566 static void get_remote_object_list(unsigned char parent
)
1568 char path
[] = "objects/XX/";
1569 static const char hex
[] = "0123456789abcdef";
1570 unsigned int val
= parent
;
1572 path
[8] = hex
[val
>> 4];
1573 path
[9] = hex
[val
& 0xf];
1574 remote_dir_exists
[val
] = 0;
1575 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1576 process_ls_object
, &val
);
1579 static int locking_available(void)
1581 struct active_request_slot
*slot
;
1582 struct slot_results results
;
1583 struct strbuf in_buffer
= STRBUF_INIT
;
1584 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1585 struct curl_slist
*dav_headers
= NULL
;
1589 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, repo
->url
);
1591 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1592 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1594 slot
= get_active_slot();
1595 slot
->results
= &results
;
1596 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1597 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1598 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1599 #ifndef NO_CURL_IOCTL
1600 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1601 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1603 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1604 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1605 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1606 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1607 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1608 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1610 if (start_active_slot(slot
)) {
1611 run_active_slot(slot
);
1612 if (results
.curl_result
== CURLE_OK
) {
1613 XML_Parser parser
= XML_ParserCreate(NULL
);
1614 enum XML_Status result
;
1615 ctx
.name
= xcalloc(10, 1);
1618 ctx
.userFunc
= handle_lockprop_ctx
;
1619 ctx
.userData
= &lock_flags
;
1620 XML_SetUserData(parser
, &ctx
);
1621 XML_SetElementHandler(parser
, xml_start_tag
,
1623 result
= XML_Parse(parser
, in_buffer
.buf
,
1627 if (result
!= XML_STATUS_OK
) {
1628 fprintf(stderr
, "XML error: %s\n",
1630 XML_GetErrorCode(parser
)));
1633 XML_ParserFree(parser
);
1635 error("no DAV locking support on %s",
1639 error("Cannot access URL %s, return code %d",
1640 repo
->url
, results
.curl_result
);
1644 error("Unable to start PROPFIND request on %s", repo
->url
);
1647 strbuf_release(&out_buffer
.buf
);
1648 strbuf_release(&in_buffer
);
1649 curl_slist_free_all(dav_headers
);
1654 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1656 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1660 return &entry
->next
;
1663 static struct object_list
**process_blob(struct blob
*blob
,
1664 struct object_list
**p
,
1665 struct name_path
*path
,
1668 struct object
*obj
= &blob
->object
;
1670 obj
->flags
|= LOCAL
;
1672 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1676 return add_one_object(obj
, p
);
1679 static struct object_list
**process_tree(struct tree
*tree
,
1680 struct object_list
**p
,
1681 struct name_path
*path
,
1684 struct object
*obj
= &tree
->object
;
1685 struct tree_desc desc
;
1686 struct name_entry entry
;
1687 struct name_path me
;
1689 obj
->flags
|= LOCAL
;
1691 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1693 if (parse_tree(tree
) < 0)
1694 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1697 name
= xstrdup(name
);
1698 p
= add_one_object(obj
, p
);
1701 me
.elem_len
= strlen(name
);
1703 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1705 while (tree_entry(&desc
, &entry
))
1706 switch (object_type(entry
.mode
)) {
1708 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1711 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1714 /* Subproject commit - not in this repository */
1719 tree
->buffer
= NULL
;
1723 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1726 struct commit
*commit
;
1727 struct object_list
**p
= &objects
;
1730 while ((commit
= get_revision(revs
)) != NULL
) {
1731 p
= process_tree(commit
->tree
, p
, NULL
, "");
1732 commit
->object
.flags
|= LOCAL
;
1733 if (!(commit
->object
.flags
& UNINTERESTING
))
1734 count
+= add_send_request(&commit
->object
, lock
);
1737 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1738 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1739 struct object
*obj
= entry
->item
;
1740 const char *name
= entry
->name
;
1742 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1744 if (obj
->type
== OBJ_TAG
) {
1746 p
= add_one_object(obj
, p
);
1749 if (obj
->type
== OBJ_TREE
) {
1750 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1753 if (obj
->type
== OBJ_BLOB
) {
1754 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1757 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1761 if (!(objects
->item
->flags
& UNINTERESTING
))
1762 count
+= add_send_request(objects
->item
, lock
);
1763 objects
= objects
->next
;
1769 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1771 struct active_request_slot
*slot
;
1772 struct slot_results results
;
1773 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1774 struct curl_slist
*dav_headers
;
1776 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1778 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1780 slot
= get_active_slot();
1781 slot
->results
= &results
;
1782 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1783 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1784 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1785 #ifndef NO_CURL_IOCTL
1786 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1787 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1789 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1790 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1791 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1792 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1793 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1794 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1796 if (start_active_slot(slot
)) {
1797 run_active_slot(slot
);
1798 strbuf_release(&out_buffer
.buf
);
1799 if (results
.curl_result
!= CURLE_OK
) {
1801 "PUT error: curl result=%d, HTTP code=%ld\n",
1802 results
.curl_result
, results
.http_code
);
1803 /* We should attempt recovery? */
1807 strbuf_release(&out_buffer
.buf
);
1808 fprintf(stderr
, "Unable to start PUT request\n");
1815 static struct ref
*remote_refs
, **remote_tail
;
1817 static void one_remote_ref(char *refname
)
1822 ref
= alloc_ref(refname
);
1824 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1826 "Unable to fetch ref %s from %s\n",
1827 refname
, repo
->url
);
1833 * Fetch a copy of the object if it doesn't exist locally - it
1834 * may be required for updating server info later.
1836 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1837 obj
= lookup_unknown_object(ref
->old_sha1
);
1839 fprintf(stderr
, " fetch %s for %s\n",
1840 sha1_to_hex(ref
->old_sha1
), refname
);
1841 add_fetch_request(obj
);
1846 remote_tail
= &ref
->next
;
1849 static void get_dav_remote_heads(void)
1851 remote_tail
= &remote_refs
;
1852 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1855 static int is_zero_sha1(const unsigned char *sha1
)
1859 for (i
= 0; i
< 20; i
++) {
1866 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1868 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1874 ref
= alloc_ref(ls
->dentry_name
);
1876 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1878 "Unable to fetch ref %s from %s\n",
1879 ls
->dentry_name
, repo
->url
);
1885 o
= parse_object(ref
->old_sha1
);
1888 "Unable to parse object %s for remote ref %s\n",
1889 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1895 len
= strlen(ls
->dentry_name
) + 42;
1896 ref_info
= xcalloc(len
+ 1, 1);
1897 sprintf(ref_info
, "%s %s\n",
1898 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1899 fwrite_buffer(ref_info
, 1, len
, buf
);
1902 if (o
->type
== OBJ_TAG
) {
1903 o
= deref_tag(o
, ls
->dentry_name
, 0);
1905 len
= strlen(ls
->dentry_name
) + 45;
1906 ref_info
= xcalloc(len
+ 1, 1);
1907 sprintf(ref_info
, "%s %s^{}\n",
1908 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1909 fwrite_buffer(ref_info
, 1, len
, buf
);
1916 static void update_remote_info_refs(struct remote_lock
*lock
)
1918 struct buffer buffer
= { STRBUF_INIT
, 0 };
1919 struct active_request_slot
*slot
;
1920 struct slot_results results
;
1921 struct curl_slist
*dav_headers
;
1923 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1924 add_remote_info_ref
, &buffer
.buf
);
1926 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1928 slot
= get_active_slot();
1929 slot
->results
= &results
;
1930 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1931 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1932 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1933 #ifndef NO_CURL_IOCTL
1934 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1935 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1937 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1938 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1939 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1940 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1941 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1942 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1944 if (start_active_slot(slot
)) {
1945 run_active_slot(slot
);
1946 if (results
.curl_result
!= CURLE_OK
) {
1948 "PUT error: curl result=%d, HTTP code=%ld\n",
1949 results
.curl_result
, results
.http_code
);
1953 strbuf_release(&buffer
.buf
);
1956 static int remote_exists(const char *path
)
1958 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1959 struct active_request_slot
*slot
;
1960 struct slot_results results
;
1963 sprintf(url
, "%s%s", repo
->url
, path
);
1965 slot
= get_active_slot();
1966 slot
->results
= &results
;
1967 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1968 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1970 if (start_active_slot(slot
)) {
1971 run_active_slot(slot
);
1972 if (results
.http_code
== 404)
1974 else if (results
.curl_result
== CURLE_OK
)
1977 fprintf(stderr
, "HEAD HTTP error %ld\n", results
.http_code
);
1979 fprintf(stderr
, "Unable to start HEAD request\n");
1986 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
1989 struct strbuf buffer
= STRBUF_INIT
;
1990 struct active_request_slot
*slot
;
1991 struct slot_results results
;
1993 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1994 sprintf(url
, "%s%s", repo
->url
, path
);
1996 slot
= get_active_slot();
1997 slot
->results
= &results
;
1998 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1999 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
2000 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
2001 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2002 if (start_active_slot(slot
)) {
2003 run_active_slot(slot
);
2004 if (results
.curl_result
!= CURLE_OK
) {
2005 die("Couldn't get %s for remote symref\n%s",
2006 url
, curl_errorstr
);
2009 die("Unable to start remote symref request");
2017 if (buffer
.len
== 0)
2020 /* If it's a symref, set the refname; otherwise try for a sha1 */
2021 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
2022 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
2024 get_sha1_hex(buffer
.buf
, sha1
);
2027 strbuf_release(&buffer
);
2030 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
2032 struct commit
*head
= lookup_commit(head_sha1
);
2033 struct commit
*branch
= lookup_commit(branch_sha1
);
2034 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
2036 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
2039 static int delete_remote_branch(char *pattern
, int force
)
2041 struct ref
*refs
= remote_refs
;
2042 struct ref
*remote_ref
= NULL
;
2043 unsigned char head_sha1
[20];
2044 char *symref
= NULL
;
2046 int patlen
= strlen(pattern
);
2048 struct active_request_slot
*slot
;
2049 struct slot_results results
;
2052 /* Find the remote branch(es) matching the specified branch name */
2053 for (match
= 0; refs
; refs
= refs
->next
) {
2054 char *name
= refs
->name
;
2055 int namelen
= strlen(name
);
2056 if (namelen
< patlen
||
2057 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
2059 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
2065 return error("No remote branch matches %s", pattern
);
2067 return error("More than one remote branch matches %s",
2071 * Remote HEAD must be a symref (not exactly foolproof; a remote
2072 * symlink to a symref will look like a symref)
2074 fetch_symref("HEAD", &symref
, head_sha1
);
2076 return error("Remote HEAD is not a symref");
2078 /* Remote branch must not be the remote HEAD */
2079 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
2080 if (!strcmp(remote_ref
->name
, symref
))
2081 return error("Remote branch %s is the current HEAD",
2083 fetch_symref(symref
, &symref
, head_sha1
);
2086 /* Run extra sanity checks if delete is not forced */
2088 /* Remote HEAD must resolve to a known object */
2090 return error("Remote HEAD symrefs too deep");
2091 if (is_zero_sha1(head_sha1
))
2092 return error("Unable to resolve remote HEAD");
2093 if (!has_sha1_file(head_sha1
))
2094 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
2096 /* Remote branch must resolve to a known object */
2097 if (is_zero_sha1(remote_ref
->old_sha1
))
2098 return error("Unable to resolve remote branch %s",
2100 if (!has_sha1_file(remote_ref
->old_sha1
))
2101 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
));
2103 /* Remote branch must be an ancestor of remote HEAD */
2104 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
2105 return error("The branch '%s' is not an ancestor "
2106 "of your current HEAD.\n"
2107 "If you are sure you want to delete it,"
2108 " run:\n\t'git http-push -D %s %s'",
2109 remote_ref
->name
, repo
->url
, pattern
);
2113 /* Send delete request */
2114 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
2117 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
2118 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
2119 slot
= get_active_slot();
2120 slot
->results
= &results
;
2121 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2122 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
2123 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2124 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
2125 if (start_active_slot(slot
)) {
2126 run_active_slot(slot
);
2128 if (results
.curl_result
!= CURLE_OK
)
2129 return error("DELETE request failed (%d/%ld)\n",
2130 results
.curl_result
, results
.http_code
);
2133 return error("Unable to start DELETE request");
2139 int main(int argc
, char **argv
)
2141 struct transfer_request
*request
;
2142 struct transfer_request
*next_request
;
2144 char **refspec
= NULL
;
2145 struct remote_lock
*ref_lock
= NULL
;
2146 struct remote_lock
*info_ref_lock
= NULL
;
2147 struct rev_info revs
;
2148 int delete_branch
= 0;
2149 int force_delete
= 0;
2150 int objects_to_send
;
2154 struct ref
*ref
, *local_refs
;
2155 struct remote
*remote
;
2156 char *rewritten_url
= NULL
;
2158 git_extract_argv0_path(argv
[0]);
2160 setup_git_directory();
2162 repo
= xcalloc(sizeof(*repo
), 1);
2165 for (i
= 1; i
< argc
; i
++, argv
++) {
2169 if (!strcmp(arg
, "--all")) {
2170 push_all
= MATCH_REFS_ALL
;
2173 if (!strcmp(arg
, "--force")) {
2177 if (!strcmp(arg
, "--dry-run")) {
2181 if (!strcmp(arg
, "--verbose")) {
2185 if (!strcmp(arg
, "-d")) {
2189 if (!strcmp(arg
, "-D")) {
2196 char *path
= strstr(arg
, "//");
2198 repo
->path_len
= strlen(arg
);
2200 repo
->path
= strchr(path
+2, '/');
2202 repo
->path_len
= strlen(repo
->path
);
2207 nr_refspec
= argc
- i
;
2211 #ifndef USE_CURL_MULTI
2212 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2216 usage(http_push_usage
);
2218 if (delete_branch
&& nr_refspec
!= 1)
2219 die("You must specify only one branch name when deleting a remote branch");
2221 memset(remote_dir_exists
, -1, 256);
2224 * Create a minimum remote by hand to give to http_init(),
2225 * primarily to allow it to look at the URL.
2227 remote
= xcalloc(sizeof(*remote
), 1);
2228 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
2229 remote
->url
[remote
->url_nr
++] = repo
->url
;
2232 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
2234 if (repo
->url
&& repo
->url
[strlen(repo
->url
)-1] != '/') {
2235 rewritten_url
= xmalloc(strlen(repo
->url
)+2);
2236 strcpy(rewritten_url
, repo
->url
);
2237 strcat(rewritten_url
, "/");
2238 repo
->path
= rewritten_url
+ (repo
->path
- repo
->url
);
2240 repo
->url
= rewritten_url
;
2243 /* Verify DAV compliance/lock support */
2244 if (!locking_available()) {
2249 sigchain_push_common(remove_locks_on_signal
);
2251 /* Check whether the remote has server info files */
2252 repo
->can_update_info_refs
= 0;
2253 repo
->has_info_refs
= remote_exists("info/refs");
2254 repo
->has_info_packs
= remote_exists("objects/info/packs");
2255 if (repo
->has_info_refs
) {
2256 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
2258 repo
->can_update_info_refs
= 1;
2260 error("cannot lock existing info/refs");
2265 if (repo
->has_info_packs
)
2268 /* Get a list of all local and remote heads to validate refspecs */
2269 local_refs
= get_local_heads();
2270 fprintf(stderr
, "Fetching remote heads...\n");
2271 get_dav_remote_heads();
2273 /* Remove a remote branch if -d or -D was specified */
2274 if (delete_branch
) {
2275 if (delete_remote_branch(refspec
[0], force_delete
) == -1)
2276 fprintf(stderr
, "Unable to delete remote branch %s\n",
2283 remote_tail
= &remote_refs
;
2284 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
2285 nr_refspec
, (const char **) refspec
, push_all
)) {
2290 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
2296 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2297 char old_hex
[60], *new_hex
;
2298 const char *commit_argv
[4];
2300 char *new_sha1_hex
, *old_sha1_hex
;
2305 if (is_zero_sha1(ref
->peer_ref
->new_sha1
)) {
2306 if (delete_remote_branch(ref
->name
, 1) == -1) {
2307 error("Could not remove %s", ref
->name
);
2314 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
2315 if (push_verbosely
|| 1)
2316 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
2321 !is_zero_sha1(ref
->old_sha1
) &&
2323 if (!has_sha1_file(ref
->old_sha1
) ||
2324 !ref_newer(ref
->peer_ref
->new_sha1
,
2327 * We do not have the remote ref, or
2328 * we know that the remote ref is not
2329 * an ancestor of what we are trying to
2330 * push. Either way this can be losing
2331 * commits at the remote end and likely
2332 * we were not up to date to begin with.
2334 error("remote '%s' is not an ancestor of\n"
2336 "Maybe you are not up-to-date and "
2337 "need to pull first?",
2339 ref
->peer_ref
->name
);
2344 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2346 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2347 new_hex
= sha1_to_hex(ref
->new_sha1
);
2349 fprintf(stderr
, "updating '%s'", ref
->name
);
2350 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2351 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2352 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2356 /* Lock remote branch ref */
2357 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2358 if (ref_lock
== NULL
) {
2359 fprintf(stderr
, "Unable to lock remote branch %s\n",
2365 /* Set up revision info for this refspec */
2367 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2368 old_sha1_hex
= NULL
;
2369 commit_argv
[1] = "--objects";
2370 commit_argv
[2] = new_sha1_hex
;
2371 if (!push_all
&& !is_zero_sha1(ref
->old_sha1
)) {
2372 old_sha1_hex
= xmalloc(42);
2373 sprintf(old_sha1_hex
, "^%s",
2374 sha1_to_hex(ref
->old_sha1
));
2375 commit_argv
[3] = old_sha1_hex
;
2378 init_revisions(&revs
, setup_git_directory());
2379 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2380 revs
.edge_hint
= 0; /* just in case */
2384 commit_argv
[1] = NULL
;
2387 /* Generate a list of objects that need to be pushed */
2389 if (prepare_revision_walk(&revs
))
2390 die("revision walk setup failed");
2391 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2392 objects_to_send
= get_delta(&revs
, ref_lock
);
2393 finish_all_active_slots();
2395 /* Push missing objects to remote, this would be a
2396 convenient time to pack them first if appropriate. */
2398 if (objects_to_send
)
2399 fprintf(stderr
, " sending %d objects\n",
2401 #ifdef USE_CURL_MULTI
2402 fill_active_slots();
2403 add_fill_function(NULL
, fill_active_slot
);
2406 finish_all_active_slots();
2407 #ifdef USE_CURL_MULTI
2408 fill_active_slots();
2410 } while (request_queue_head
&& !aborted
);
2412 /* Update the remote branch if all went well */
2413 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2417 fprintf(stderr
, " done\n");
2418 unlock_remote(ref_lock
);
2422 /* Update remote server info if appropriate */
2423 if (repo
->has_info_refs
&& new_refs
) {
2424 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2425 fprintf(stderr
, "Updating remote server info\n");
2427 update_remote_info_refs(info_ref_lock
);
2429 fprintf(stderr
, "Unable to update server info\n");
2434 free(rewritten_url
);
2436 unlock_remote(info_ref_lock
);
2439 curl_slist_free_all(no_pragma_header
);
2443 request
= request_queue_head
;
2444 while (request
!= NULL
) {
2445 next_request
= request
->next
;
2446 release_request(request
);
2447 request
= next_request
;