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
);
318 unlink_or_warn(prevfile
);
319 rename(request
->tmpfile
, prevfile
);
320 unlink_or_warn(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);
375 unlink_or_warn(prevfile
);
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
;
727 struct active_request_slot
*slot
;
729 request
->curl_result
= request
->slot
->curl_result
;
730 request
->http_code
= request
->slot
->http_code
;
731 slot
= request
->slot
;
732 request
->slot
= NULL
;
734 /* Keep locks active */
737 if (request
->headers
!= NULL
)
738 curl_slist_free_all(request
->headers
);
740 /* URL is reused for MOVE after PUT */
741 if (request
->state
!= RUN_PUT
) {
746 if (request
->state
== RUN_MKCOL
) {
747 if (request
->curl_result
== CURLE_OK
||
748 request
->http_code
== 405) {
749 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
752 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
753 sha1_to_hex(request
->obj
->sha1
),
754 request
->curl_result
, request
->http_code
);
755 request
->state
= ABORTED
;
758 } else if (request
->state
== RUN_PUT
) {
759 if (request
->curl_result
== CURLE_OK
) {
762 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
763 sha1_to_hex(request
->obj
->sha1
),
764 request
->curl_result
, request
->http_code
);
765 request
->state
= ABORTED
;
768 } else if (request
->state
== RUN_MOVE
) {
769 if (request
->curl_result
== CURLE_OK
) {
771 fprintf(stderr
, " sent %s\n",
772 sha1_to_hex(request
->obj
->sha1
));
773 request
->obj
->flags
|= REMOTE
;
774 release_request(request
);
776 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
777 sha1_to_hex(request
->obj
->sha1
),
778 request
->curl_result
, request
->http_code
);
779 request
->state
= ABORTED
;
782 } else if (request
->state
== RUN_FETCH_LOOSE
) {
783 close(request
->local_fileno
); request
->local_fileno
= -1;
785 if (request
->curl_result
!= CURLE_OK
&&
786 request
->http_code
!= 416) {
787 if (stat(request
->tmpfile
, &st
) == 0) {
789 unlink_or_warn(request
->tmpfile
);
792 if (request
->http_code
== 416)
793 warning("requested range invalid; we may already have all the data.");
795 git_inflate_end(&request
->stream
);
796 git_SHA1_Final(request
->real_sha1
, &request
->c
);
797 if (request
->zret
!= Z_STREAM_END
) {
798 unlink_or_warn(request
->tmpfile
);
799 } else if (hashcmp(request
->obj
->sha1
, request
->real_sha1
)) {
800 unlink_or_warn(request
->tmpfile
);
806 if (request
->rename
== 0) {
807 request
->obj
->flags
|= (LOCAL
| REMOTE
);
812 /* Try fetching packed if necessary */
813 if (request
->obj
->flags
& LOCAL
)
814 release_request(request
);
816 start_fetch_packed(request
);
818 } else if (request
->state
== RUN_FETCH_PACKED
) {
819 if (request
->curl_result
!= CURLE_OK
) {
820 fprintf(stderr
, "Unable to get pack file %s\n%s",
821 request
->url
, curl_errorstr
);
822 repo
->can_update_info_refs
= 0;
824 off_t pack_size
= ftell(request
->local_stream
);
826 fclose(request
->local_stream
);
827 request
->local_stream
= NULL
;
829 if (!move_temp_to_file(request
->tmpfile
,
830 request
->filename
)) {
831 target
= (struct packed_git
*)request
->userData
;
832 target
->pack_size
= pack_size
;
834 while (*lst
!= target
)
835 lst
= &((*lst
)->next
);
838 if (!verify_pack(target
))
839 install_packed_git(target
);
841 repo
->can_update_info_refs
= 0;
844 release_request(request
);
848 #ifdef USE_CURL_MULTI
849 static int fill_active_slot(void *unused
)
851 struct transfer_request
*request
;
856 for (request
= request_queue_head
; request
; request
= request
->next
) {
857 if (request
->state
== NEED_FETCH
) {
858 start_fetch_loose(request
);
860 } else if (pushing
&& request
->state
== NEED_PUSH
) {
861 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
864 start_mkcol(request
);
873 static void get_remote_object_list(unsigned char parent
);
875 static void add_fetch_request(struct object
*obj
)
877 struct transfer_request
*request
;
882 * Don't fetch the object if it's known to exist locally
883 * or is already in the request queue
885 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
886 get_remote_object_list(obj
->sha1
[0]);
887 if (obj
->flags
& (LOCAL
| FETCHING
))
890 obj
->flags
|= FETCHING
;
891 request
= xmalloc(sizeof(*request
));
894 request
->lock
= NULL
;
895 request
->headers
= NULL
;
896 request
->local_fileno
= -1;
897 request
->local_stream
= NULL
;
898 request
->state
= NEED_FETCH
;
899 request
->next
= request_queue_head
;
900 request_queue_head
= request
;
902 #ifdef USE_CURL_MULTI
908 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
910 struct transfer_request
*request
= request_queue_head
;
911 struct packed_git
*target
;
913 /* Keep locks active */
917 * Don't push the object if it's known to exist on the remote
918 * or is already in the request queue
920 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
921 get_remote_object_list(obj
->sha1
[0]);
922 if (obj
->flags
& (REMOTE
| PUSHING
))
924 target
= find_sha1_pack(obj
->sha1
, repo
->packs
);
926 obj
->flags
|= REMOTE
;
930 obj
->flags
|= PUSHING
;
931 request
= xmalloc(sizeof(*request
));
934 request
->lock
= lock
;
935 request
->headers
= NULL
;
936 request
->local_fileno
= -1;
937 request
->local_stream
= NULL
;
938 request
->state
= NEED_PUSH
;
939 request
->next
= request_queue_head
;
940 request_queue_head
= request
;
942 #ifdef USE_CURL_MULTI
950 static int fetch_index(unsigned char *sha1
)
952 char *hex
= sha1_to_hex(sha1
);
955 char tmpfile
[PATH_MAX
];
957 char range
[RANGE_HEADER_SIZE
];
958 struct curl_slist
*range_header
= NULL
;
961 struct active_request_slot
*slot
;
962 struct slot_results results
;
964 /* Don't use the index if the pack isn't there */
965 url
= xmalloc(strlen(repo
->url
) + 64);
966 sprintf(url
, "%sobjects/pack/pack-%s.pack", repo
->url
, hex
);
967 slot
= get_active_slot();
968 slot
->results
= &results
;
969 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
970 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
971 if (start_active_slot(slot
)) {
972 run_active_slot(slot
);
973 if (results
.curl_result
!= CURLE_OK
) {
975 return error("Unable to verify pack %s is available",
980 return error("Unable to start request");
983 if (has_pack_index(sha1
)) {
989 fprintf(stderr
, "Getting index for pack %s\n", hex
);
991 sprintf(url
, "%sobjects/pack/pack-%s.idx", repo
->url
, hex
);
993 filename
= sha1_pack_index_name(sha1
);
994 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
995 indexfile
= fopen(tmpfile
, "a");
998 return error("Unable to open local file %s for pack index",
1002 slot
= get_active_slot();
1003 slot
->results
= &results
;
1004 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1005 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1006 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
1007 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1008 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1009 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1010 slot
->local
= indexfile
;
1012 /* If there is data present from a previous transfer attempt,
1013 resume where it left off */
1014 prev_posn
= ftell(indexfile
);
1018 "Resuming fetch of index for pack %s at byte %ld\n",
1020 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1021 range_header
= curl_slist_append(range_header
, range
);
1022 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
1025 if (start_active_slot(slot
)) {
1026 run_active_slot(slot
);
1027 if (results
.curl_result
!= CURLE_OK
) {
1031 return error("Unable to get pack index %s\n%s", url
,
1038 return error("Unable to start request");
1045 return move_temp_to_file(tmpfile
, filename
);
1048 static int setup_index(unsigned char *sha1
)
1050 struct packed_git
*new_pack
;
1052 if (fetch_index(sha1
))
1055 new_pack
= parse_pack_index(sha1
);
1056 new_pack
->next
= repo
->packs
;
1057 repo
->packs
= new_pack
;
1061 static int fetch_indices(void)
1063 unsigned char sha1
[20];
1065 struct strbuf buffer
= STRBUF_INIT
;
1069 struct active_request_slot
*slot
;
1070 struct slot_results results
;
1073 fprintf(stderr
, "Getting pack list\n");
1075 url
= xmalloc(strlen(repo
->url
) + 20);
1076 sprintf(url
, "%sobjects/info/packs", repo
->url
);
1078 slot
= get_active_slot();
1079 slot
->results
= &results
;
1080 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1081 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1082 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1083 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1084 if (start_active_slot(slot
)) {
1085 run_active_slot(slot
);
1086 if (results
.curl_result
!= CURLE_OK
) {
1087 strbuf_release(&buffer
);
1089 if (results
.http_code
== 404)
1092 return error("%s", curl_errorstr
);
1095 strbuf_release(&buffer
);
1097 return error("Unable to start request");
1102 while (i
< buffer
.len
) {
1106 if (i
+ 52 < buffer
.len
&&
1107 !prefixcmp(data
+ i
, " pack-") &&
1108 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1109 get_sha1_hex(data
+ i
+ 6, sha1
);
1115 while (data
[i
] != '\n')
1121 strbuf_release(&buffer
);
1125 static void one_remote_object(const char *hex
)
1127 unsigned char sha1
[20];
1130 if (get_sha1_hex(hex
, sha1
) != 0)
1133 obj
= lookup_object(sha1
);
1135 obj
= parse_object(sha1
);
1137 /* Ignore remote objects that don't exist locally */
1141 obj
->flags
|= REMOTE
;
1142 if (!object_list_contains(objects
, obj
))
1143 object_list_insert(obj
, &objects
);
1146 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1148 int *lock_flags
= (int *)ctx
->userData
;
1151 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
1152 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
1153 (*lock_flags
& DAV_PROP_LOCKWR
)) {
1154 *lock_flags
|= DAV_LOCK_OK
;
1156 *lock_flags
&= DAV_LOCK_OK
;
1157 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
1158 *lock_flags
|= DAV_PROP_LOCKWR
;
1159 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
1160 *lock_flags
|= DAV_PROP_LOCKEX
;
1165 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1167 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
1168 git_SHA_CTX sha_ctx
;
1169 unsigned char lock_token_sha1
[20];
1171 if (tag_closed
&& ctx
->cdata
) {
1172 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
1173 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
1174 strcpy(lock
->owner
, ctx
->cdata
);
1175 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
1176 if (!prefixcmp(ctx
->cdata
, "Second-"))
1178 strtol(ctx
->cdata
+ 7, NULL
, 10);
1179 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
1180 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
1181 strcpy(lock
->token
, ctx
->cdata
);
1183 git_SHA1_Init(&sha_ctx
);
1184 git_SHA1_Update(&sha_ctx
, lock
->token
, strlen(lock
->token
));
1185 git_SHA1_Final(lock_token_sha1
, &sha_ctx
);
1187 lock
->tmpfile_suffix
[0] = '_';
1188 memcpy(lock
->tmpfile_suffix
+ 1, sha1_to_hex(lock_token_sha1
), 40);
1193 static void one_remote_ref(char *refname
);
1196 xml_start_tag(void *userData
, const char *name
, const char **atts
)
1198 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1199 const char *c
= strchr(name
, ':');
1207 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
1209 if (new_len
> ctx
->len
) {
1210 ctx
->name
= xrealloc(ctx
->name
, new_len
);
1213 strcat(ctx
->name
, ".");
1214 strcat(ctx
->name
, c
);
1219 ctx
->userFunc(ctx
, 0);
1223 xml_end_tag(void *userData
, const char *name
)
1225 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1226 const char *c
= strchr(name
, ':');
1229 ctx
->userFunc(ctx
, 1);
1236 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
1241 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
1243 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1245 ctx
->cdata
= xmemdupz(s
, len
);
1248 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
1250 struct active_request_slot
*slot
;
1251 struct slot_results results
;
1252 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1253 struct strbuf in_buffer
= STRBUF_INIT
;
1256 char timeout_header
[25];
1257 struct remote_lock
*lock
= NULL
;
1258 struct curl_slist
*dav_headers
= NULL
;
1262 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1263 sprintf(url
, "%s%s", repo
->url
, path
);
1265 /* Make sure leading directories exist for the remote ref */
1266 ep
= strchr(url
+ strlen(repo
->url
) + 1, '/');
1268 char saved_character
= ep
[1];
1270 slot
= get_active_slot();
1271 slot
->results
= &results
;
1272 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1273 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1274 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1275 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1276 if (start_active_slot(slot
)) {
1277 run_active_slot(slot
);
1278 if (results
.curl_result
!= CURLE_OK
&&
1279 results
.http_code
!= 405) {
1281 "Unable to create branch path %s\n",
1287 fprintf(stderr
, "Unable to start MKCOL request\n");
1291 ep
[1] = saved_character
;
1292 ep
= strchr(ep
+ 1, '/');
1295 escaped
= xml_entities(git_default_email
);
1296 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, escaped
);
1299 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1300 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1301 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1303 slot
= get_active_slot();
1304 slot
->results
= &results
;
1305 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1306 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1307 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1308 #ifndef NO_CURL_IOCTL
1309 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1310 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1312 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1313 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1314 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1315 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1316 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1317 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1319 lock
= xcalloc(1, sizeof(*lock
));
1322 if (start_active_slot(slot
)) {
1323 run_active_slot(slot
);
1324 if (results
.curl_result
== CURLE_OK
) {
1325 XML_Parser parser
= XML_ParserCreate(NULL
);
1326 enum XML_Status result
;
1327 ctx
.name
= xcalloc(10, 1);
1330 ctx
.userFunc
= handle_new_lock_ctx
;
1331 ctx
.userData
= lock
;
1332 XML_SetUserData(parser
, &ctx
);
1333 XML_SetElementHandler(parser
, xml_start_tag
,
1335 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1336 result
= XML_Parse(parser
, in_buffer
.buf
,
1339 if (result
!= XML_STATUS_OK
) {
1340 fprintf(stderr
, "XML error: %s\n",
1342 XML_GetErrorCode(parser
)));
1345 XML_ParserFree(parser
);
1348 fprintf(stderr
, "Unable to start LOCK request\n");
1351 curl_slist_free_all(dav_headers
);
1352 strbuf_release(&out_buffer
.buf
);
1353 strbuf_release(&in_buffer
);
1355 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1363 lock
->start_time
= time(NULL
);
1364 lock
->next
= repo
->locks
;
1371 static int unlock_remote(struct remote_lock
*lock
)
1373 struct active_request_slot
*slot
;
1374 struct slot_results results
;
1375 struct remote_lock
*prev
= repo
->locks
;
1376 struct curl_slist
*dav_headers
;
1379 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
1381 slot
= get_active_slot();
1382 slot
->results
= &results
;
1383 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1384 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1385 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1386 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1388 if (start_active_slot(slot
)) {
1389 run_active_slot(slot
);
1390 if (results
.curl_result
== CURLE_OK
)
1393 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1396 fprintf(stderr
, "Unable to start UNLOCK request\n");
1399 curl_slist_free_all(dav_headers
);
1401 if (repo
->locks
== lock
) {
1402 repo
->locks
= lock
->next
;
1404 while (prev
&& prev
->next
!= lock
)
1407 prev
->next
= prev
->next
->next
;
1418 static void remove_locks(void)
1420 struct remote_lock
*lock
= repo
->locks
;
1422 fprintf(stderr
, "Removing remote locks...\n");
1424 unlock_remote(lock
);
1429 static void remove_locks_on_signal(int signo
)
1432 sigchain_pop(signo
);
1436 static void remote_ls(const char *path
, int flags
,
1437 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1440 static void process_ls_object(struct remote_ls_ctx
*ls
)
1442 unsigned int *parent
= (unsigned int *)ls
->userData
;
1443 char *path
= ls
->dentry_name
;
1446 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1447 remote_dir_exists
[*parent
] = 1;
1451 if (strlen(path
) != 49)
1454 obj_hex
= xmalloc(strlen(path
));
1455 /* NB: path is not null-terminated, can not use strlcpy here */
1456 memcpy(obj_hex
, path
, 2);
1457 strcpy(obj_hex
+ 2, path
+ 3);
1458 one_remote_object(obj_hex
);
1462 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1464 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1465 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1469 if (!(ls
->dentry_flags
& IS_DIR
))
1470 one_remote_ref(ls
->dentry_name
);
1473 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1475 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1478 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1479 if (ls
->dentry_flags
& IS_DIR
) {
1480 if (ls
->flags
& PROCESS_DIRS
) {
1483 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1484 ls
->flags
& RECURSIVE
) {
1485 remote_ls(ls
->dentry_name
,
1490 } else if (ls
->flags
& PROCESS_FILES
) {
1493 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1494 char *path
= ctx
->cdata
;
1495 if (*ctx
->cdata
== 'h') {
1496 path
= strstr(path
, "//");
1498 path
= strchr(path
+2, '/');
1502 path
+= repo
->path_len
;
1503 ls
->dentry_name
= xstrdup(path
);
1505 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1506 ls
->dentry_flags
|= IS_DIR
;
1508 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1509 free(ls
->dentry_name
);
1510 ls
->dentry_name
= NULL
;
1511 ls
->dentry_flags
= 0;
1516 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1517 * should _only_ heed the information from that file, instead of trying to
1518 * determine the refs from the remote file system (badly: it does not even
1519 * know about packed-refs).
1521 static void remote_ls(const char *path
, int flags
,
1522 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1525 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1526 struct active_request_slot
*slot
;
1527 struct slot_results results
;
1528 struct strbuf in_buffer
= STRBUF_INIT
;
1529 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1530 struct curl_slist
*dav_headers
= NULL
;
1532 struct remote_ls_ctx ls
;
1535 ls
.path
= xstrdup(path
);
1536 ls
.dentry_name
= NULL
;
1537 ls
.dentry_flags
= 0;
1538 ls
.userData
= userData
;
1539 ls
.userFunc
= userFunc
;
1541 sprintf(url
, "%s%s", repo
->url
, path
);
1543 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1545 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1546 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1548 slot
= get_active_slot();
1549 slot
->results
= &results
;
1550 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1551 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1552 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1553 #ifndef NO_CURL_IOCTL
1554 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1555 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1557 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1558 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1559 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1560 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1561 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1562 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1564 if (start_active_slot(slot
)) {
1565 run_active_slot(slot
);
1566 if (results
.curl_result
== CURLE_OK
) {
1567 XML_Parser parser
= XML_ParserCreate(NULL
);
1568 enum XML_Status result
;
1569 ctx
.name
= xcalloc(10, 1);
1572 ctx
.userFunc
= handle_remote_ls_ctx
;
1574 XML_SetUserData(parser
, &ctx
);
1575 XML_SetElementHandler(parser
, xml_start_tag
,
1577 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1578 result
= XML_Parse(parser
, in_buffer
.buf
,
1582 if (result
!= XML_STATUS_OK
) {
1583 fprintf(stderr
, "XML error: %s\n",
1585 XML_GetErrorCode(parser
)));
1587 XML_ParserFree(parser
);
1590 fprintf(stderr
, "Unable to start PROPFIND request\n");
1595 strbuf_release(&out_buffer
.buf
);
1596 strbuf_release(&in_buffer
);
1597 curl_slist_free_all(dav_headers
);
1600 static void get_remote_object_list(unsigned char parent
)
1602 char path
[] = "objects/XX/";
1603 static const char hex
[] = "0123456789abcdef";
1604 unsigned int val
= parent
;
1606 path
[8] = hex
[val
>> 4];
1607 path
[9] = hex
[val
& 0xf];
1608 remote_dir_exists
[val
] = 0;
1609 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1610 process_ls_object
, &val
);
1613 static int locking_available(void)
1615 struct active_request_slot
*slot
;
1616 struct slot_results results
;
1617 struct strbuf in_buffer
= STRBUF_INIT
;
1618 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1619 struct curl_slist
*dav_headers
= NULL
;
1624 escaped
= xml_entities(repo
->url
);
1625 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, escaped
);
1628 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1629 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1631 slot
= get_active_slot();
1632 slot
->results
= &results
;
1633 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1634 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1635 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1636 #ifndef NO_CURL_IOCTL
1637 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1638 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1640 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1641 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1642 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, repo
->url
);
1643 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1644 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1645 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1647 if (start_active_slot(slot
)) {
1648 run_active_slot(slot
);
1649 if (results
.curl_result
== CURLE_OK
) {
1650 XML_Parser parser
= XML_ParserCreate(NULL
);
1651 enum XML_Status result
;
1652 ctx
.name
= xcalloc(10, 1);
1655 ctx
.userFunc
= handle_lockprop_ctx
;
1656 ctx
.userData
= &lock_flags
;
1657 XML_SetUserData(parser
, &ctx
);
1658 XML_SetElementHandler(parser
, xml_start_tag
,
1660 result
= XML_Parse(parser
, in_buffer
.buf
,
1664 if (result
!= XML_STATUS_OK
) {
1665 fprintf(stderr
, "XML error: %s\n",
1667 XML_GetErrorCode(parser
)));
1670 XML_ParserFree(parser
);
1672 error("no DAV locking support on %s",
1676 error("Cannot access URL %s, return code %d",
1677 repo
->url
, results
.curl_result
);
1681 error("Unable to start PROPFIND request on %s", repo
->url
);
1684 strbuf_release(&out_buffer
.buf
);
1685 strbuf_release(&in_buffer
);
1686 curl_slist_free_all(dav_headers
);
1691 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1693 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1697 return &entry
->next
;
1700 static struct object_list
**process_blob(struct blob
*blob
,
1701 struct object_list
**p
,
1702 struct name_path
*path
,
1705 struct object
*obj
= &blob
->object
;
1707 obj
->flags
|= LOCAL
;
1709 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1713 return add_one_object(obj
, p
);
1716 static struct object_list
**process_tree(struct tree
*tree
,
1717 struct object_list
**p
,
1718 struct name_path
*path
,
1721 struct object
*obj
= &tree
->object
;
1722 struct tree_desc desc
;
1723 struct name_entry entry
;
1724 struct name_path me
;
1726 obj
->flags
|= LOCAL
;
1728 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1730 if (parse_tree(tree
) < 0)
1731 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1734 name
= xstrdup(name
);
1735 p
= add_one_object(obj
, p
);
1738 me
.elem_len
= strlen(name
);
1740 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1742 while (tree_entry(&desc
, &entry
))
1743 switch (object_type(entry
.mode
)) {
1745 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1748 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1751 /* Subproject commit - not in this repository */
1756 tree
->buffer
= NULL
;
1760 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1763 struct commit
*commit
;
1764 struct object_list
**p
= &objects
;
1767 while ((commit
= get_revision(revs
)) != NULL
) {
1768 p
= process_tree(commit
->tree
, p
, NULL
, "");
1769 commit
->object
.flags
|= LOCAL
;
1770 if (!(commit
->object
.flags
& UNINTERESTING
))
1771 count
+= add_send_request(&commit
->object
, lock
);
1774 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1775 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1776 struct object
*obj
= entry
->item
;
1777 const char *name
= entry
->name
;
1779 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1781 if (obj
->type
== OBJ_TAG
) {
1783 p
= add_one_object(obj
, p
);
1786 if (obj
->type
== OBJ_TREE
) {
1787 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1790 if (obj
->type
== OBJ_BLOB
) {
1791 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1794 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1798 if (!(objects
->item
->flags
& UNINTERESTING
))
1799 count
+= add_send_request(objects
->item
, lock
);
1800 objects
= objects
->next
;
1806 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1808 struct active_request_slot
*slot
;
1809 struct slot_results results
;
1810 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1811 struct curl_slist
*dav_headers
;
1813 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1815 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1817 slot
= get_active_slot();
1818 slot
->results
= &results
;
1819 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1820 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1821 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1822 #ifndef NO_CURL_IOCTL
1823 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1824 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &out_buffer
);
1826 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1827 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1828 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1829 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1830 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1831 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1833 if (start_active_slot(slot
)) {
1834 run_active_slot(slot
);
1835 strbuf_release(&out_buffer
.buf
);
1836 if (results
.curl_result
!= CURLE_OK
) {
1838 "PUT error: curl result=%d, HTTP code=%ld\n",
1839 results
.curl_result
, results
.http_code
);
1840 /* We should attempt recovery? */
1844 strbuf_release(&out_buffer
.buf
);
1845 fprintf(stderr
, "Unable to start PUT request\n");
1852 static struct ref
*remote_refs
, **remote_tail
;
1854 static void one_remote_ref(char *refname
)
1859 ref
= alloc_ref(refname
);
1861 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1863 "Unable to fetch ref %s from %s\n",
1864 refname
, repo
->url
);
1870 * Fetch a copy of the object if it doesn't exist locally - it
1871 * may be required for updating server info later.
1873 if (repo
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1874 obj
= lookup_unknown_object(ref
->old_sha1
);
1876 fprintf(stderr
, " fetch %s for %s\n",
1877 sha1_to_hex(ref
->old_sha1
), refname
);
1878 add_fetch_request(obj
);
1883 remote_tail
= &ref
->next
;
1886 static void get_dav_remote_heads(void)
1888 remote_tail
= &remote_refs
;
1889 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1892 static int is_zero_sha1(const unsigned char *sha1
)
1896 for (i
= 0; i
< 20; i
++) {
1903 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1905 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1911 ref
= alloc_ref(ls
->dentry_name
);
1913 if (http_fetch_ref(repo
->url
, ref
) != 0) {
1915 "Unable to fetch ref %s from %s\n",
1916 ls
->dentry_name
, repo
->url
);
1922 o
= parse_object(ref
->old_sha1
);
1925 "Unable to parse object %s for remote ref %s\n",
1926 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1932 len
= strlen(ls
->dentry_name
) + 42;
1933 ref_info
= xcalloc(len
+ 1, 1);
1934 sprintf(ref_info
, "%s %s\n",
1935 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1936 fwrite_buffer(ref_info
, 1, len
, buf
);
1939 if (o
->type
== OBJ_TAG
) {
1940 o
= deref_tag(o
, ls
->dentry_name
, 0);
1942 len
= strlen(ls
->dentry_name
) + 45;
1943 ref_info
= xcalloc(len
+ 1, 1);
1944 sprintf(ref_info
, "%s %s^{}\n",
1945 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1946 fwrite_buffer(ref_info
, 1, len
, buf
);
1953 static void update_remote_info_refs(struct remote_lock
*lock
)
1955 struct buffer buffer
= { STRBUF_INIT
, 0 };
1956 struct active_request_slot
*slot
;
1957 struct slot_results results
;
1958 struct curl_slist
*dav_headers
;
1960 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1961 add_remote_info_ref
, &buffer
.buf
);
1963 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1965 slot
= get_active_slot();
1966 slot
->results
= &results
;
1967 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1968 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1969 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1970 #ifndef NO_CURL_IOCTL
1971 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, ioctl_buffer
);
1972 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, &buffer
);
1974 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1975 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1976 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1977 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1978 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1979 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1981 if (start_active_slot(slot
)) {
1982 run_active_slot(slot
);
1983 if (results
.curl_result
!= CURLE_OK
) {
1985 "PUT error: curl result=%d, HTTP code=%ld\n",
1986 results
.curl_result
, results
.http_code
);
1990 strbuf_release(&buffer
.buf
);
1993 static int remote_exists(const char *path
)
1995 char *url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
1996 struct active_request_slot
*slot
;
1997 struct slot_results results
;
2000 sprintf(url
, "%s%s", repo
->url
, path
);
2002 slot
= get_active_slot();
2003 slot
->results
= &results
;
2004 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2005 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
2007 if (start_active_slot(slot
)) {
2008 run_active_slot(slot
);
2009 if (results
.http_code
== 404)
2011 else if (results
.curl_result
== CURLE_OK
)
2014 fprintf(stderr
, "HEAD HTTP error %ld\n", results
.http_code
);
2016 fprintf(stderr
, "Unable to start HEAD request\n");
2023 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
2026 struct strbuf buffer
= STRBUF_INIT
;
2027 struct active_request_slot
*slot
;
2028 struct slot_results results
;
2030 url
= xmalloc(strlen(repo
->url
) + strlen(path
) + 1);
2031 sprintf(url
, "%s%s", repo
->url
, path
);
2033 slot
= get_active_slot();
2034 slot
->results
= &results
;
2035 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
2036 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
2037 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
2038 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2039 if (start_active_slot(slot
)) {
2040 run_active_slot(slot
);
2041 if (results
.curl_result
!= CURLE_OK
) {
2042 die("Couldn't get %s for remote symref\n%s",
2043 url
, curl_errorstr
);
2046 die("Unable to start remote symref request");
2054 if (buffer
.len
== 0)
2057 /* If it's a symref, set the refname; otherwise try for a sha1 */
2058 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
2059 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
2061 get_sha1_hex(buffer
.buf
, sha1
);
2064 strbuf_release(&buffer
);
2067 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
2069 struct commit
*head
= lookup_commit(head_sha1
);
2070 struct commit
*branch
= lookup_commit(branch_sha1
);
2071 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
2073 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
2076 static int delete_remote_branch(char *pattern
, int force
)
2078 struct ref
*refs
= remote_refs
;
2079 struct ref
*remote_ref
= NULL
;
2080 unsigned char head_sha1
[20];
2081 char *symref
= NULL
;
2083 int patlen
= strlen(pattern
);
2085 struct active_request_slot
*slot
;
2086 struct slot_results results
;
2089 /* Find the remote branch(es) matching the specified branch name */
2090 for (match
= 0; refs
; refs
= refs
->next
) {
2091 char *name
= refs
->name
;
2092 int namelen
= strlen(name
);
2093 if (namelen
< patlen
||
2094 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
2096 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
2102 return error("No remote branch matches %s", pattern
);
2104 return error("More than one remote branch matches %s",
2108 * Remote HEAD must be a symref (not exactly foolproof; a remote
2109 * symlink to a symref will look like a symref)
2111 fetch_symref("HEAD", &symref
, head_sha1
);
2113 return error("Remote HEAD is not a symref");
2115 /* Remote branch must not be the remote HEAD */
2116 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
2117 if (!strcmp(remote_ref
->name
, symref
))
2118 return error("Remote branch %s is the current HEAD",
2120 fetch_symref(symref
, &symref
, head_sha1
);
2123 /* Run extra sanity checks if delete is not forced */
2125 /* Remote HEAD must resolve to a known object */
2127 return error("Remote HEAD symrefs too deep");
2128 if (is_zero_sha1(head_sha1
))
2129 return error("Unable to resolve remote HEAD");
2130 if (!has_sha1_file(head_sha1
))
2131 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
2133 /* Remote branch must resolve to a known object */
2134 if (is_zero_sha1(remote_ref
->old_sha1
))
2135 return error("Unable to resolve remote branch %s",
2137 if (!has_sha1_file(remote_ref
->old_sha1
))
2138 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
));
2140 /* Remote branch must be an ancestor of remote HEAD */
2141 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
2142 return error("The branch '%s' is not an ancestor "
2143 "of your current HEAD.\n"
2144 "If you are sure you want to delete it,"
2145 " run:\n\t'git http-push -D %s %s'",
2146 remote_ref
->name
, repo
->url
, pattern
);
2150 /* Send delete request */
2151 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
2154 url
= xmalloc(strlen(repo
->url
) + strlen(remote_ref
->name
) + 1);
2155 sprintf(url
, "%s%s", repo
->url
, remote_ref
->name
);
2156 slot
= get_active_slot();
2157 slot
->results
= &results
;
2158 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2159 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
2160 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2161 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
2162 if (start_active_slot(slot
)) {
2163 run_active_slot(slot
);
2165 if (results
.curl_result
!= CURLE_OK
)
2166 return error("DELETE request failed (%d/%ld)\n",
2167 results
.curl_result
, results
.http_code
);
2170 return error("Unable to start DELETE request");
2176 int main(int argc
, char **argv
)
2178 struct transfer_request
*request
;
2179 struct transfer_request
*next_request
;
2181 char **refspec
= NULL
;
2182 struct remote_lock
*ref_lock
= NULL
;
2183 struct remote_lock
*info_ref_lock
= NULL
;
2184 struct rev_info revs
;
2185 int delete_branch
= 0;
2186 int force_delete
= 0;
2187 int objects_to_send
;
2191 struct ref
*ref
, *local_refs
;
2192 struct remote
*remote
;
2193 char *rewritten_url
= NULL
;
2195 git_extract_argv0_path(argv
[0]);
2197 setup_git_directory();
2199 repo
= xcalloc(sizeof(*repo
), 1);
2202 for (i
= 1; i
< argc
; i
++, argv
++) {
2206 if (!strcmp(arg
, "--all")) {
2207 push_all
= MATCH_REFS_ALL
;
2210 if (!strcmp(arg
, "--force")) {
2214 if (!strcmp(arg
, "--dry-run")) {
2218 if (!strcmp(arg
, "--verbose")) {
2222 if (!strcmp(arg
, "-d")) {
2226 if (!strcmp(arg
, "-D")) {
2233 char *path
= strstr(arg
, "//");
2235 repo
->path_len
= strlen(arg
);
2237 repo
->path
= strchr(path
+2, '/');
2239 repo
->path_len
= strlen(repo
->path
);
2244 nr_refspec
= argc
- i
;
2248 #ifndef USE_CURL_MULTI
2249 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2253 usage(http_push_usage
);
2255 if (delete_branch
&& nr_refspec
!= 1)
2256 die("You must specify only one branch name when deleting a remote branch");
2258 memset(remote_dir_exists
, -1, 256);
2261 * Create a minimum remote by hand to give to http_init(),
2262 * primarily to allow it to look at the URL.
2264 remote
= xcalloc(sizeof(*remote
), 1);
2265 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
2266 remote
->url
[remote
->url_nr
++] = repo
->url
;
2269 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
2271 if (repo
->url
&& repo
->url
[strlen(repo
->url
)-1] != '/') {
2272 rewritten_url
= xmalloc(strlen(repo
->url
)+2);
2273 strcpy(rewritten_url
, repo
->url
);
2274 strcat(rewritten_url
, "/");
2275 repo
->path
= rewritten_url
+ (repo
->path
- repo
->url
);
2277 repo
->url
= rewritten_url
;
2280 /* Verify DAV compliance/lock support */
2281 if (!locking_available()) {
2286 sigchain_push_common(remove_locks_on_signal
);
2288 /* Check whether the remote has server info files */
2289 repo
->can_update_info_refs
= 0;
2290 repo
->has_info_refs
= remote_exists("info/refs");
2291 repo
->has_info_packs
= remote_exists("objects/info/packs");
2292 if (repo
->has_info_refs
) {
2293 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
2295 repo
->can_update_info_refs
= 1;
2297 error("cannot lock existing info/refs");
2302 if (repo
->has_info_packs
)
2305 /* Get a list of all local and remote heads to validate refspecs */
2306 local_refs
= get_local_heads();
2307 fprintf(stderr
, "Fetching remote heads...\n");
2308 get_dav_remote_heads();
2310 /* Remove a remote branch if -d or -D was specified */
2311 if (delete_branch
) {
2312 if (delete_remote_branch(refspec
[0], force_delete
) == -1)
2313 fprintf(stderr
, "Unable to delete remote branch %s\n",
2320 remote_tail
= &remote_refs
;
2321 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
2322 nr_refspec
, (const char **) refspec
, push_all
)) {
2327 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
2333 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2334 char old_hex
[60], *new_hex
;
2335 const char *commit_argv
[4];
2337 char *new_sha1_hex
, *old_sha1_hex
;
2342 if (is_zero_sha1(ref
->peer_ref
->new_sha1
)) {
2343 if (delete_remote_branch(ref
->name
, 1) == -1) {
2344 error("Could not remove %s", ref
->name
);
2351 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
2352 if (push_verbosely
|| 1)
2353 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
2358 !is_zero_sha1(ref
->old_sha1
) &&
2360 if (!has_sha1_file(ref
->old_sha1
) ||
2361 !ref_newer(ref
->peer_ref
->new_sha1
,
2364 * We do not have the remote ref, or
2365 * we know that the remote ref is not
2366 * an ancestor of what we are trying to
2367 * push. Either way this can be losing
2368 * commits at the remote end and likely
2369 * we were not up to date to begin with.
2371 error("remote '%s' is not an ancestor of\n"
2373 "Maybe you are not up-to-date and "
2374 "need to pull first?",
2376 ref
->peer_ref
->name
);
2381 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2383 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2384 new_hex
= sha1_to_hex(ref
->new_sha1
);
2386 fprintf(stderr
, "updating '%s'", ref
->name
);
2387 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2388 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2389 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2393 /* Lock remote branch ref */
2394 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2395 if (ref_lock
== NULL
) {
2396 fprintf(stderr
, "Unable to lock remote branch %s\n",
2402 /* Set up revision info for this refspec */
2404 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2405 old_sha1_hex
= NULL
;
2406 commit_argv
[1] = "--objects";
2407 commit_argv
[2] = new_sha1_hex
;
2408 if (!push_all
&& !is_zero_sha1(ref
->old_sha1
)) {
2409 old_sha1_hex
= xmalloc(42);
2410 sprintf(old_sha1_hex
, "^%s",
2411 sha1_to_hex(ref
->old_sha1
));
2412 commit_argv
[3] = old_sha1_hex
;
2415 init_revisions(&revs
, setup_git_directory());
2416 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2417 revs
.edge_hint
= 0; /* just in case */
2421 commit_argv
[1] = NULL
;
2424 /* Generate a list of objects that need to be pushed */
2426 if (prepare_revision_walk(&revs
))
2427 die("revision walk setup failed");
2428 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2429 objects_to_send
= get_delta(&revs
, ref_lock
);
2430 finish_all_active_slots();
2432 /* Push missing objects to remote, this would be a
2433 convenient time to pack them first if appropriate. */
2435 if (objects_to_send
)
2436 fprintf(stderr
, " sending %d objects\n",
2438 #ifdef USE_CURL_MULTI
2439 fill_active_slots();
2440 add_fill_function(NULL
, fill_active_slot
);
2443 finish_all_active_slots();
2444 #ifdef USE_CURL_MULTI
2445 fill_active_slots();
2447 } while (request_queue_head
&& !aborted
);
2449 /* Update the remote branch if all went well */
2450 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2454 fprintf(stderr
, " done\n");
2455 unlock_remote(ref_lock
);
2459 /* Update remote server info if appropriate */
2460 if (repo
->has_info_refs
&& new_refs
) {
2461 if (info_ref_lock
&& repo
->can_update_info_refs
) {
2462 fprintf(stderr
, "Updating remote server info\n");
2464 update_remote_info_refs(info_ref_lock
);
2466 fprintf(stderr
, "Unable to update server info\n");
2471 free(rewritten_url
);
2473 unlock_remote(info_ref_lock
);
2476 curl_slist_free_all(no_pragma_header
);
2480 request
= request_queue_head
;
2481 while (request
!= NULL
) {
2482 next_request
= request
->next
;
2483 release_request(request
);
2484 request
= next_request
;