12 #include "list-objects.h"
16 static const char http_push_usage
[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
28 #define PREV_BUF_SIZE 4096
29 #define RANGE_HEADER_SIZE 30
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #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>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #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>"
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
76 static signed char remote_dir_exists
[256];
78 static struct curl_slist
*no_pragma_header
;
80 static int push_verbosely
;
81 static int push_all
= MATCH_REFS_NONE
;
85 static struct object_list
*objects
;
93 int can_update_info_refs
;
95 struct packed_git
*packs
;
96 struct remote_lock
*locks
;
99 static struct repo
*remote
;
101 enum transfer_state
{
113 struct transfer_request
118 struct remote_lock
*lock
;
119 struct curl_slist
*headers
;
120 struct buffer buffer
;
121 char filename
[PATH_MAX
];
122 char tmpfile
[PATH_MAX
];
125 enum transfer_state state
;
126 CURLcode curl_result
;
127 char errorstr
[CURL_ERROR_SIZE
];
129 unsigned char real_sha1
[20];
135 struct active_request_slot
*slot
;
136 struct transfer_request
*next
;
139 static struct transfer_request
*request_queue_head
;
146 void (*userFunc
)(struct xml_ctx
*ctx
, int tag_closed
);
158 struct remote_lock
*next
;
161 /* Flags that control remote_ls processing */
162 #define PROCESS_FILES (1u << 0)
163 #define PROCESS_DIRS (1u << 1)
164 #define RECURSIVE (1u << 2)
166 /* Flags that remote_ls passes to callback functions */
167 #define IS_DIR (1u << 0)
172 void (*userFunc
)(struct remote_ls_ctx
*ls
);
177 struct remote_ls_ctx
*parent
;
180 /* get_dav_token_headers options */
181 enum dav_header_flag
{
182 DAV_HEADER_IF
= (1u << 0),
183 DAV_HEADER_LOCK
= (1u << 1),
184 DAV_HEADER_TIMEOUT
= (1u << 2)
187 static struct curl_slist
*get_dav_token_headers(struct remote_lock
*lock
, enum dav_header_flag options
)
189 struct strbuf buf
= STRBUF_INIT
;
190 struct curl_slist
*dav_headers
= NULL
;
192 if (options
& DAV_HEADER_IF
) {
193 strbuf_addf(&buf
, "If: (<%s>)", lock
->token
);
194 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
197 if (options
& DAV_HEADER_LOCK
) {
198 strbuf_addf(&buf
, "Lock-Token: <%s>", lock
->token
);
199 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
202 if (options
& DAV_HEADER_TIMEOUT
) {
203 strbuf_addf(&buf
, "Timeout: Second-%ld", lock
->timeout
);
204 dav_headers
= curl_slist_append(dav_headers
, buf
.buf
);
207 strbuf_release(&buf
);
212 static void append_remote_object_url(struct strbuf
*buf
, const char *url
,
214 int only_two_digit_prefix
)
216 strbuf_addf(buf
, "%sobjects/%.*s/", url
, 2, hex
);
217 if (!only_two_digit_prefix
)
218 strbuf_addf(buf
, "%s", hex
+2);
221 static void finish_request(struct transfer_request
*request
);
222 static void release_request(struct transfer_request
*request
);
224 static void process_response(void *callback_data
)
226 struct transfer_request
*request
=
227 (struct transfer_request
*)callback_data
;
229 finish_request(request
);
232 #ifdef USE_CURL_MULTI
234 static char *get_remote_object_url(const char *url
, const char *hex
, int only_two_digit_prefix
)
236 struct strbuf buf
= STRBUF_INIT
;
237 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
238 return strbuf_detach(&buf
, NULL
);
241 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
244 unsigned char expn
[4096];
245 size_t size
= eltsize
* nmemb
;
247 struct transfer_request
*request
= (struct transfer_request
*)data
;
249 ssize_t retval
= xwrite(request
->local_fileno
,
250 (char *) ptr
+ posn
, size
- posn
);
254 } while (posn
< size
);
256 request
->stream
.avail_in
= size
;
257 request
->stream
.next_in
= ptr
;
259 request
->stream
.next_out
= expn
;
260 request
->stream
.avail_out
= sizeof(expn
);
261 request
->zret
= git_inflate(&request
->stream
, Z_SYNC_FLUSH
);
262 git_SHA1_Update(&request
->c
, expn
,
263 sizeof(expn
) - request
->stream
.avail_out
);
264 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
269 static void start_fetch_loose(struct transfer_request
*request
)
271 char *hex
= sha1_to_hex(request
->obj
->sha1
);
273 char prevfile
[PATH_MAX
];
276 unsigned char prev_buf
[PREV_BUF_SIZE
];
277 ssize_t prev_read
= 0;
279 char range
[RANGE_HEADER_SIZE
];
280 struct curl_slist
*range_header
= NULL
;
281 struct active_request_slot
*slot
;
283 filename
= sha1_file_name(request
->obj
->sha1
);
284 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
285 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
286 "%s.temp", filename
);
288 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
290 rename(request
->tmpfile
, prevfile
);
291 unlink(request
->tmpfile
);
293 if (request
->local_fileno
!= -1)
294 error("fd leakage in start: %d", request
->local_fileno
);
295 request
->local_fileno
= open(request
->tmpfile
,
296 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
297 /* This could have failed due to the "lazy directory creation";
298 * try to mkdir the last path component.
300 if (request
->local_fileno
< 0 && errno
== ENOENT
) {
301 char *dir
= strrchr(request
->tmpfile
, '/');
304 mkdir(request
->tmpfile
, 0777);
307 request
->local_fileno
= open(request
->tmpfile
,
308 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
311 if (request
->local_fileno
< 0) {
312 request
->state
= ABORTED
;
313 error("Couldn't create temporary file %s for %s: %s",
314 request
->tmpfile
, request
->filename
, strerror(errno
));
318 memset(&request
->stream
, 0, sizeof(request
->stream
));
320 git_inflate_init(&request
->stream
);
322 git_SHA1_Init(&request
->c
);
324 url
= get_remote_object_url(remote
->url
, hex
, 0);
325 request
->url
= xstrdup(url
);
327 /* If a previous temp file is present, process what was already
329 prevlocal
= open(prevfile
, O_RDONLY
);
330 if (prevlocal
!= -1) {
332 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
334 if (fwrite_sha1_file(prev_buf
,
337 request
) == prev_read
) {
338 prev_posn
+= prev_read
;
343 } while (prev_read
> 0);
348 /* Reset inflate/SHA1 if there was an error reading the previous temp
349 file; also rewind to the beginning of the local file. */
350 if (prev_read
== -1) {
351 memset(&request
->stream
, 0, sizeof(request
->stream
));
352 git_inflate_init(&request
->stream
);
353 git_SHA1_Init(&request
->c
);
356 lseek(request
->local_fileno
, 0, SEEK_SET
);
357 ftruncate(request
->local_fileno
, 0);
361 slot
= get_active_slot();
362 slot
->callback_func
= process_response
;
363 slot
->callback_data
= request
;
364 request
->slot
= slot
;
366 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
367 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
368 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
369 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
370 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
372 /* If we have successfully processed data from a previous fetch
373 attempt, only fetch the data we don't already have. */
377 "Resuming fetch of object %s at byte %ld\n",
379 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
380 range_header
= curl_slist_append(range_header
, range
);
381 curl_easy_setopt(slot
->curl
,
382 CURLOPT_HTTPHEADER
, range_header
);
385 /* Try to get the request started, abort the request on error */
386 request
->state
= RUN_FETCH_LOOSE
;
387 if (!start_active_slot(slot
)) {
388 fprintf(stderr
, "Unable to start GET request\n");
389 remote
->can_update_info_refs
= 0;
390 release_request(request
);
394 static void start_mkcol(struct transfer_request
*request
)
396 char *hex
= sha1_to_hex(request
->obj
->sha1
);
397 struct active_request_slot
*slot
;
399 request
->url
= get_remote_object_url(remote
->url
, hex
, 1);
401 slot
= get_active_slot();
402 slot
->callback_func
= process_response
;
403 slot
->callback_data
= request
;
404 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
405 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
406 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
407 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
408 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
410 if (start_active_slot(slot
)) {
411 request
->slot
= slot
;
412 request
->state
= RUN_MKCOL
;
414 request
->state
= ABORTED
;
421 static void start_fetch_packed(struct transfer_request
*request
)
424 struct packed_git
*target
;
428 char range
[RANGE_HEADER_SIZE
];
429 struct curl_slist
*range_header
= NULL
;
431 struct transfer_request
*check_request
= request_queue_head
;
432 struct active_request_slot
*slot
;
434 target
= find_sha1_pack(request
->obj
->sha1
, remote
->packs
);
436 fprintf(stderr
, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request
->obj
->sha1
));
437 remote
->can_update_info_refs
= 0;
438 release_request(request
);
442 fprintf(stderr
, "Fetching pack %s\n", sha1_to_hex(target
->sha1
));
443 fprintf(stderr
, " which contains %s\n", sha1_to_hex(request
->obj
->sha1
));
445 filename
= sha1_pack_name(target
->sha1
);
446 snprintf(request
->filename
, sizeof(request
->filename
), "%s", filename
);
447 snprintf(request
->tmpfile
, sizeof(request
->tmpfile
),
448 "%s.temp", filename
);
450 url
= xmalloc(strlen(remote
->url
) + 64);
451 sprintf(url
, "%sobjects/pack/pack-%s.pack",
452 remote
->url
, sha1_to_hex(target
->sha1
));
454 /* Make sure there isn't another open request for this pack */
455 while (check_request
) {
456 if (check_request
->state
== RUN_FETCH_PACKED
&&
457 !strcmp(check_request
->url
, url
)) {
459 release_request(request
);
462 check_request
= check_request
->next
;
465 packfile
= fopen(request
->tmpfile
, "a");
467 fprintf(stderr
, "Unable to open local file %s for pack",
469 remote
->can_update_info_refs
= 0;
474 slot
= get_active_slot();
475 slot
->callback_func
= process_response
;
476 slot
->callback_data
= request
;
477 request
->slot
= slot
;
478 request
->local_stream
= packfile
;
479 request
->userData
= target
;
482 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
483 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
484 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
485 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
486 slot
->local
= packfile
;
488 /* If there is data present from a previous transfer attempt,
489 resume where it left off */
490 prev_posn
= ftell(packfile
);
494 "Resuming fetch of pack %s at byte %ld\n",
495 sha1_to_hex(target
->sha1
), prev_posn
);
496 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
497 range_header
= curl_slist_append(range_header
, range
);
498 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
501 /* Try to get the request started, abort the request on error */
502 request
->state
= RUN_FETCH_PACKED
;
503 if (!start_active_slot(slot
)) {
504 fprintf(stderr
, "Unable to start GET request\n");
505 remote
->can_update_info_refs
= 0;
506 release_request(request
);
510 static void start_put(struct transfer_request
*request
)
512 char *hex
= sha1_to_hex(request
->obj
->sha1
);
513 struct active_request_slot
*slot
;
514 struct strbuf buf
= STRBUF_INIT
;
515 enum object_type type
;
523 unpacked
= read_sha1_file(request
->obj
->sha1
, &type
, &len
);
524 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
527 memset(&stream
, 0, sizeof(stream
));
528 deflateInit(&stream
, zlib_compression_level
);
529 size
= deflateBound(&stream
, len
+ hdrlen
);
530 strbuf_init(&request
->buffer
.buf
, size
);
531 request
->buffer
.posn
= 0;
534 stream
.next_out
= (unsigned char *)request
->buffer
.buf
.buf
;
535 stream
.avail_out
= size
;
538 stream
.next_in
= (void *)hdr
;
539 stream
.avail_in
= hdrlen
;
540 while (deflate(&stream
, 0) == Z_OK
)
543 /* Then the data itself.. */
544 stream
.next_in
= unpacked
;
545 stream
.avail_in
= len
;
546 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
551 request
->buffer
.buf
.len
= stream
.total_out
;
553 strbuf_addstr(&buf
, "Destination: ");
554 append_remote_object_url(&buf
, remote
->url
, hex
, 0);
555 request
->dest
= strbuf_detach(&buf
, NULL
);
557 append_remote_object_url(&buf
, remote
->url
, hex
, 0);
558 strbuf_addstr(&buf
, request
->lock
->token
);
559 request
->url
= strbuf_detach(&buf
, NULL
);
561 slot
= get_active_slot();
562 slot
->callback_func
= process_response
;
563 slot
->callback_data
= request
;
564 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
565 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
566 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
567 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
568 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
569 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
570 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
571 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
572 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
574 if (start_active_slot(slot
)) {
575 request
->slot
= slot
;
576 request
->state
= RUN_PUT
;
578 request
->state
= ABORTED
;
584 static void start_move(struct transfer_request
*request
)
586 struct active_request_slot
*slot
;
587 struct curl_slist
*dav_headers
= NULL
;
589 slot
= get_active_slot();
590 slot
->callback_func
= process_response
;
591 slot
->callback_data
= request
;
592 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
593 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
594 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
595 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
596 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
597 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
598 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
600 if (start_active_slot(slot
)) {
601 request
->slot
= slot
;
602 request
->state
= RUN_MOVE
;
604 request
->state
= ABORTED
;
610 static int refresh_lock(struct remote_lock
*lock
)
612 struct active_request_slot
*slot
;
613 struct slot_results results
;
614 struct curl_slist
*dav_headers
;
617 lock
->refreshing
= 1;
619 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
621 slot
= get_active_slot();
622 slot
->results
= &results
;
623 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
624 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
625 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
626 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
627 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
629 if (start_active_slot(slot
)) {
630 run_active_slot(slot
);
631 if (results
.curl_result
!= CURLE_OK
) {
632 fprintf(stderr
, "LOCK HTTP error %ld\n",
635 lock
->start_time
= time(NULL
);
640 lock
->refreshing
= 0;
641 curl_slist_free_all(dav_headers
);
646 static void check_locks(void)
648 struct remote_lock
*lock
= remote
->locks
;
649 time_t current_time
= time(NULL
);
653 time_remaining
= lock
->start_time
+ lock
->timeout
-
655 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
656 if (!refresh_lock(lock
)) {
658 "Unable to refresh lock for %s\n",
668 static void release_request(struct transfer_request
*request
)
670 struct transfer_request
*entry
= request_queue_head
;
672 if (request
== request_queue_head
) {
673 request_queue_head
= request
->next
;
675 while (entry
->next
!= NULL
&& entry
->next
!= request
)
677 if (entry
->next
== request
)
678 entry
->next
= entry
->next
->next
;
681 if (request
->local_fileno
!= -1)
682 close(request
->local_fileno
);
683 if (request
->local_stream
)
684 fclose(request
->local_stream
);
689 static void finish_request(struct transfer_request
*request
)
692 struct packed_git
*target
;
693 struct packed_git
**lst
;
695 request
->curl_result
= request
->slot
->curl_result
;
696 request
->http_code
= request
->slot
->http_code
;
697 request
->slot
= NULL
;
699 /* Keep locks active */
702 if (request
->headers
!= NULL
)
703 curl_slist_free_all(request
->headers
);
705 /* URL is reused for MOVE after PUT */
706 if (request
->state
!= RUN_PUT
) {
711 if (request
->state
== RUN_MKCOL
) {
712 if (request
->curl_result
== CURLE_OK
||
713 request
->http_code
== 405) {
714 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
717 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
718 sha1_to_hex(request
->obj
->sha1
),
719 request
->curl_result
, request
->http_code
);
720 request
->state
= ABORTED
;
723 } else if (request
->state
== RUN_PUT
) {
724 if (request
->curl_result
== CURLE_OK
) {
727 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
728 sha1_to_hex(request
->obj
->sha1
),
729 request
->curl_result
, request
->http_code
);
730 request
->state
= ABORTED
;
733 } else if (request
->state
== RUN_MOVE
) {
734 if (request
->curl_result
== CURLE_OK
) {
736 fprintf(stderr
, " sent %s\n",
737 sha1_to_hex(request
->obj
->sha1
));
738 request
->obj
->flags
|= REMOTE
;
739 release_request(request
);
741 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
742 sha1_to_hex(request
->obj
->sha1
),
743 request
->curl_result
, request
->http_code
);
744 request
->state
= ABORTED
;
747 } else if (request
->state
== RUN_FETCH_LOOSE
) {
748 fchmod(request
->local_fileno
, 0444);
749 close(request
->local_fileno
); request
->local_fileno
= -1;
751 if (request
->curl_result
!= CURLE_OK
&&
752 request
->http_code
!= 416) {
753 if (stat(request
->tmpfile
, &st
) == 0) {
755 unlink(request
->tmpfile
);
758 if (request
->http_code
== 416)
759 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
761 git_inflate_end(&request
->stream
);
762 git_SHA1_Final(request
->real_sha1
, &request
->c
);
763 if (request
->zret
!= Z_STREAM_END
) {
764 unlink(request
->tmpfile
);
765 } else if (hashcmp(request
->obj
->sha1
, request
->real_sha1
)) {
766 unlink(request
->tmpfile
);
772 if (request
->rename
== 0) {
773 request
->obj
->flags
|= (LOCAL
| REMOTE
);
778 /* Try fetching packed if necessary */
779 if (request
->obj
->flags
& LOCAL
)
780 release_request(request
);
782 start_fetch_packed(request
);
784 } else if (request
->state
== RUN_FETCH_PACKED
) {
785 if (request
->curl_result
!= CURLE_OK
) {
786 fprintf(stderr
, "Unable to get pack file %s\n%s",
787 request
->url
, curl_errorstr
);
788 remote
->can_update_info_refs
= 0;
790 off_t pack_size
= ftell(request
->local_stream
);
792 fclose(request
->local_stream
);
793 request
->local_stream
= NULL
;
794 if (!move_temp_to_file(request
->tmpfile
,
795 request
->filename
)) {
796 target
= (struct packed_git
*)request
->userData
;
797 target
->pack_size
= pack_size
;
798 lst
= &remote
->packs
;
799 while (*lst
!= target
)
800 lst
= &((*lst
)->next
);
803 if (!verify_pack(target
))
804 install_packed_git(target
);
806 remote
->can_update_info_refs
= 0;
809 release_request(request
);
813 #ifdef USE_CURL_MULTI
814 static int fill_active_slot(void *unused
)
816 struct transfer_request
*request
= request_queue_head
;
821 for (request
= request_queue_head
; request
; request
= request
->next
) {
822 if (request
->state
== NEED_FETCH
) {
823 start_fetch_loose(request
);
825 } else if (pushing
&& request
->state
== NEED_PUSH
) {
826 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
829 start_mkcol(request
);
838 static void get_remote_object_list(unsigned char parent
);
840 static void add_fetch_request(struct object
*obj
)
842 struct transfer_request
*request
;
847 * Don't fetch the object if it's known to exist locally
848 * or is already in the request queue
850 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
851 get_remote_object_list(obj
->sha1
[0]);
852 if (obj
->flags
& (LOCAL
| FETCHING
))
855 obj
->flags
|= FETCHING
;
856 request
= xmalloc(sizeof(*request
));
859 request
->lock
= NULL
;
860 request
->headers
= NULL
;
861 request
->local_fileno
= -1;
862 request
->local_stream
= NULL
;
863 request
->state
= NEED_FETCH
;
864 request
->next
= request_queue_head
;
865 request_queue_head
= request
;
867 #ifdef USE_CURL_MULTI
873 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
875 struct transfer_request
*request
= request_queue_head
;
876 struct packed_git
*target
;
878 /* Keep locks active */
882 * Don't push the object if it's known to exist on the remote
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
& (REMOTE
| PUSHING
))
889 target
= find_sha1_pack(obj
->sha1
, remote
->packs
);
891 obj
->flags
|= REMOTE
;
895 obj
->flags
|= PUSHING
;
896 request
= xmalloc(sizeof(*request
));
899 request
->lock
= lock
;
900 request
->headers
= NULL
;
901 request
->local_fileno
= -1;
902 request
->local_stream
= NULL
;
903 request
->state
= NEED_PUSH
;
904 request
->next
= request_queue_head
;
905 request_queue_head
= request
;
907 #ifdef USE_CURL_MULTI
915 static int fetch_index(unsigned char *sha1
)
917 char *hex
= sha1_to_hex(sha1
);
920 char tmpfile
[PATH_MAX
];
922 char range
[RANGE_HEADER_SIZE
];
923 struct curl_slist
*range_header
= NULL
;
926 struct active_request_slot
*slot
;
927 struct slot_results results
;
929 /* Don't use the index if the pack isn't there */
930 url
= xmalloc(strlen(remote
->url
) + 64);
931 sprintf(url
, "%sobjects/pack/pack-%s.pack", remote
->url
, hex
);
932 slot
= get_active_slot();
933 slot
->results
= &results
;
934 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
935 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
936 if (start_active_slot(slot
)) {
937 run_active_slot(slot
);
938 if (results
.curl_result
!= CURLE_OK
) {
940 return error("Unable to verify pack %s is available",
945 return error("Unable to start request");
948 if (has_pack_index(sha1
)) {
954 fprintf(stderr
, "Getting index for pack %s\n", hex
);
956 sprintf(url
, "%sobjects/pack/pack-%s.idx", remote
->url
, hex
);
958 filename
= sha1_pack_index_name(sha1
);
959 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
960 indexfile
= fopen(tmpfile
, "a");
963 return error("Unable to open local file %s for pack index",
967 slot
= get_active_slot();
968 slot
->results
= &results
;
969 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
970 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
971 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
972 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
973 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
974 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
975 slot
->local
= indexfile
;
977 /* If there is data present from a previous transfer attempt,
978 resume where it left off */
979 prev_posn
= ftell(indexfile
);
983 "Resuming fetch of index for pack %s at byte %ld\n",
985 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
986 range_header
= curl_slist_append(range_header
, range
);
987 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
990 if (start_active_slot(slot
)) {
991 run_active_slot(slot
);
992 if (results
.curl_result
!= CURLE_OK
) {
995 return error("Unable to get pack index %s\n%s", url
,
1001 return error("Unable to start request");
1007 return move_temp_to_file(tmpfile
, filename
);
1010 static int setup_index(unsigned char *sha1
)
1012 struct packed_git
*new_pack
;
1014 if (fetch_index(sha1
))
1017 new_pack
= parse_pack_index(sha1
);
1018 new_pack
->next
= remote
->packs
;
1019 remote
->packs
= new_pack
;
1023 static int fetch_indices(void)
1025 unsigned char sha1
[20];
1027 struct strbuf buffer
= STRBUF_INIT
;
1031 struct active_request_slot
*slot
;
1032 struct slot_results results
;
1035 fprintf(stderr
, "Getting pack list\n");
1037 url
= xmalloc(strlen(remote
->url
) + 20);
1038 sprintf(url
, "%sobjects/info/packs", remote
->url
);
1040 slot
= get_active_slot();
1041 slot
->results
= &results
;
1042 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1043 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1044 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1045 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1046 if (start_active_slot(slot
)) {
1047 run_active_slot(slot
);
1048 if (results
.curl_result
!= CURLE_OK
) {
1049 strbuf_release(&buffer
);
1051 if (results
.http_code
== 404)
1054 return error("%s", curl_errorstr
);
1057 strbuf_release(&buffer
);
1059 return error("Unable to start request");
1064 while (i
< buffer
.len
) {
1068 if (i
+ 52 < buffer
.len
&&
1069 !prefixcmp(data
+ i
, " pack-") &&
1070 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1071 get_sha1_hex(data
+ i
+ 6, sha1
);
1077 while (data
[i
] != '\n')
1083 strbuf_release(&buffer
);
1087 static void one_remote_object(const char *hex
)
1089 unsigned char sha1
[20];
1092 if (get_sha1_hex(hex
, sha1
) != 0)
1095 obj
= lookup_object(sha1
);
1097 obj
= parse_object(sha1
);
1099 /* Ignore remote objects that don't exist locally */
1103 obj
->flags
|= REMOTE
;
1104 if (!object_list_contains(objects
, obj
))
1105 object_list_insert(obj
, &objects
);
1108 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1110 int *lock_flags
= (int *)ctx
->userData
;
1113 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
1114 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
1115 (*lock_flags
& DAV_PROP_LOCKWR
)) {
1116 *lock_flags
|= DAV_LOCK_OK
;
1118 *lock_flags
&= DAV_LOCK_OK
;
1119 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
1120 *lock_flags
|= DAV_PROP_LOCKWR
;
1121 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
1122 *lock_flags
|= DAV_PROP_LOCKEX
;
1127 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1129 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
1131 if (tag_closed
&& ctx
->cdata
) {
1132 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
1133 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
1134 strcpy(lock
->owner
, ctx
->cdata
);
1135 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
1136 if (!prefixcmp(ctx
->cdata
, "Second-"))
1138 strtol(ctx
->cdata
+ 7, NULL
, 10);
1139 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
1140 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
1141 strcpy(lock
->token
, ctx
->cdata
);
1146 static void one_remote_ref(char *refname
);
1149 xml_start_tag(void *userData
, const char *name
, const char **atts
)
1151 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1152 const char *c
= strchr(name
, ':');
1160 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
1162 if (new_len
> ctx
->len
) {
1163 ctx
->name
= xrealloc(ctx
->name
, new_len
);
1166 strcat(ctx
->name
, ".");
1167 strcat(ctx
->name
, c
);
1172 ctx
->userFunc(ctx
, 0);
1176 xml_end_tag(void *userData
, const char *name
)
1178 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1179 const char *c
= strchr(name
, ':');
1182 ctx
->userFunc(ctx
, 1);
1189 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
1194 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
1196 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1198 ctx
->cdata
= xmemdupz(s
, len
);
1201 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
1203 struct active_request_slot
*slot
;
1204 struct slot_results results
;
1205 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1206 struct strbuf in_buffer
= STRBUF_INIT
;
1209 char timeout_header
[25];
1210 struct remote_lock
*lock
= NULL
;
1211 struct curl_slist
*dav_headers
= NULL
;
1214 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1215 sprintf(url
, "%s%s", remote
->url
, path
);
1217 /* Make sure leading directories exist for the remote ref */
1218 ep
= strchr(url
+ strlen(remote
->url
) + 1, '/');
1220 char saved_character
= ep
[1];
1222 slot
= get_active_slot();
1223 slot
->results
= &results
;
1224 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1225 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1226 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1227 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1228 if (start_active_slot(slot
)) {
1229 run_active_slot(slot
);
1230 if (results
.curl_result
!= CURLE_OK
&&
1231 results
.http_code
!= 405) {
1233 "Unable to create branch path %s\n",
1239 fprintf(stderr
, "Unable to start MKCOL request\n");
1243 ep
[1] = saved_character
;
1244 ep
= strchr(ep
+ 1, '/');
1247 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, git_default_email
);
1249 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1250 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1251 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1253 slot
= get_active_slot();
1254 slot
->results
= &results
;
1255 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1256 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1257 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1259 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1260 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1261 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1262 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1263 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1265 lock
= xcalloc(1, sizeof(*lock
));
1268 if (start_active_slot(slot
)) {
1269 run_active_slot(slot
);
1270 if (results
.curl_result
== CURLE_OK
) {
1271 XML_Parser parser
= XML_ParserCreate(NULL
);
1272 enum XML_Status result
;
1273 ctx
.name
= xcalloc(10, 1);
1276 ctx
.userFunc
= handle_new_lock_ctx
;
1277 ctx
.userData
= lock
;
1278 XML_SetUserData(parser
, &ctx
);
1279 XML_SetElementHandler(parser
, xml_start_tag
,
1281 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1282 result
= XML_Parse(parser
, in_buffer
.buf
,
1285 if (result
!= XML_STATUS_OK
) {
1286 fprintf(stderr
, "XML error: %s\n",
1288 XML_GetErrorCode(parser
)));
1291 XML_ParserFree(parser
);
1294 fprintf(stderr
, "Unable to start LOCK request\n");
1297 curl_slist_free_all(dav_headers
);
1298 strbuf_release(&out_buffer
.buf
);
1299 strbuf_release(&in_buffer
);
1301 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1309 lock
->start_time
= time(NULL
);
1310 lock
->next
= remote
->locks
;
1311 remote
->locks
= lock
;
1317 static int unlock_remote(struct remote_lock
*lock
)
1319 struct active_request_slot
*slot
;
1320 struct slot_results results
;
1321 struct remote_lock
*prev
= remote
->locks
;
1322 struct curl_slist
*dav_headers
;
1325 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
1327 slot
= get_active_slot();
1328 slot
->results
= &results
;
1329 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1330 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1331 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1332 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1334 if (start_active_slot(slot
)) {
1335 run_active_slot(slot
);
1336 if (results
.curl_result
== CURLE_OK
)
1339 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1342 fprintf(stderr
, "Unable to start UNLOCK request\n");
1345 curl_slist_free_all(dav_headers
);
1347 if (remote
->locks
== lock
) {
1348 remote
->locks
= lock
->next
;
1350 while (prev
&& prev
->next
!= lock
)
1353 prev
->next
= prev
->next
->next
;
1364 static void remove_locks(void)
1366 struct remote_lock
*lock
= remote
->locks
;
1368 fprintf(stderr
, "Removing remote locks...\n");
1370 unlock_remote(lock
);
1375 static void remove_locks_on_signal(int signo
)
1378 signal(signo
, SIG_DFL
);
1382 static void remote_ls(const char *path
, int flags
,
1383 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1386 static void process_ls_object(struct remote_ls_ctx
*ls
)
1388 unsigned int *parent
= (unsigned int *)ls
->userData
;
1389 char *path
= ls
->dentry_name
;
1392 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1393 remote_dir_exists
[*parent
] = 1;
1397 if (strlen(path
) != 49)
1400 obj_hex
= xmalloc(strlen(path
));
1401 /* NB: path is not null-terminated, can not use strlcpy here */
1402 memcpy(obj_hex
, path
, 2);
1403 strcpy(obj_hex
+ 2, path
+ 3);
1404 one_remote_object(obj_hex
);
1408 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1410 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1411 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1415 if (!(ls
->dentry_flags
& IS_DIR
))
1416 one_remote_ref(ls
->dentry_name
);
1419 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1421 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1424 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1425 if (ls
->dentry_flags
& IS_DIR
) {
1426 if (ls
->flags
& PROCESS_DIRS
) {
1429 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1430 ls
->flags
& RECURSIVE
) {
1431 remote_ls(ls
->dentry_name
,
1436 } else if (ls
->flags
& PROCESS_FILES
) {
1439 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1440 char *path
= ctx
->cdata
;
1441 if (*ctx
->cdata
== 'h') {
1442 path
= strstr(path
, "//");
1444 path
= strchr(path
+2, '/');
1448 path
+= remote
->path_len
;
1449 ls
->dentry_name
= xstrdup(path
);
1451 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1452 ls
->dentry_flags
|= IS_DIR
;
1454 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1455 free(ls
->dentry_name
);
1456 ls
->dentry_name
= NULL
;
1457 ls
->dentry_flags
= 0;
1462 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1463 * should _only_ heed the information from that file, instead of trying to
1464 * determine the refs from the remote file system (badly: it does not even
1465 * know about packed-refs).
1467 static void remote_ls(const char *path
, int flags
,
1468 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1471 char *url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1472 struct active_request_slot
*slot
;
1473 struct slot_results results
;
1474 struct strbuf in_buffer
= STRBUF_INIT
;
1475 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1476 struct curl_slist
*dav_headers
= NULL
;
1478 struct remote_ls_ctx ls
;
1481 ls
.path
= xstrdup(path
);
1482 ls
.dentry_name
= NULL
;
1483 ls
.dentry_flags
= 0;
1484 ls
.userData
= userData
;
1485 ls
.userFunc
= userFunc
;
1487 sprintf(url
, "%s%s", remote
->url
, path
);
1489 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1491 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1492 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1494 slot
= get_active_slot();
1495 slot
->results
= &results
;
1496 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1497 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1498 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1499 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1500 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1501 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1502 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1503 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1504 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1506 if (start_active_slot(slot
)) {
1507 run_active_slot(slot
);
1508 if (results
.curl_result
== CURLE_OK
) {
1509 XML_Parser parser
= XML_ParserCreate(NULL
);
1510 enum XML_Status result
;
1511 ctx
.name
= xcalloc(10, 1);
1514 ctx
.userFunc
= handle_remote_ls_ctx
;
1516 XML_SetUserData(parser
, &ctx
);
1517 XML_SetElementHandler(parser
, xml_start_tag
,
1519 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1520 result
= XML_Parse(parser
, in_buffer
.buf
,
1524 if (result
!= XML_STATUS_OK
) {
1525 fprintf(stderr
, "XML error: %s\n",
1527 XML_GetErrorCode(parser
)));
1529 XML_ParserFree(parser
);
1532 fprintf(stderr
, "Unable to start PROPFIND request\n");
1537 strbuf_release(&out_buffer
.buf
);
1538 strbuf_release(&in_buffer
);
1539 curl_slist_free_all(dav_headers
);
1542 static void get_remote_object_list(unsigned char parent
)
1544 char path
[] = "objects/XX/";
1545 static const char hex
[] = "0123456789abcdef";
1546 unsigned int val
= parent
;
1548 path
[8] = hex
[val
>> 4];
1549 path
[9] = hex
[val
& 0xf];
1550 remote_dir_exists
[val
] = 0;
1551 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1552 process_ls_object
, &val
);
1555 static int locking_available(void)
1557 struct active_request_slot
*slot
;
1558 struct slot_results results
;
1559 struct strbuf in_buffer
= STRBUF_INIT
;
1560 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1561 struct curl_slist
*dav_headers
= NULL
;
1565 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, remote
->url
);
1567 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1568 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1570 slot
= get_active_slot();
1571 slot
->results
= &results
;
1572 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1573 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1574 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1575 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1576 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1577 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1578 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1579 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1580 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1582 if (start_active_slot(slot
)) {
1583 run_active_slot(slot
);
1584 if (results
.curl_result
== CURLE_OK
) {
1585 XML_Parser parser
= XML_ParserCreate(NULL
);
1586 enum XML_Status result
;
1587 ctx
.name
= xcalloc(10, 1);
1590 ctx
.userFunc
= handle_lockprop_ctx
;
1591 ctx
.userData
= &lock_flags
;
1592 XML_SetUserData(parser
, &ctx
);
1593 XML_SetElementHandler(parser
, xml_start_tag
,
1595 result
= XML_Parse(parser
, in_buffer
.buf
,
1599 if (result
!= XML_STATUS_OK
) {
1600 fprintf(stderr
, "XML error: %s\n",
1602 XML_GetErrorCode(parser
)));
1605 XML_ParserFree(parser
);
1607 error("Error: no DAV locking support on %s",
1611 error("Cannot access URL %s, return code %d",
1612 remote
->url
, results
.curl_result
);
1616 error("Unable to start PROPFIND request on %s", remote
->url
);
1619 strbuf_release(&out_buffer
.buf
);
1620 strbuf_release(&in_buffer
);
1621 curl_slist_free_all(dav_headers
);
1626 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1628 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1632 return &entry
->next
;
1635 static struct object_list
**process_blob(struct blob
*blob
,
1636 struct object_list
**p
,
1637 struct name_path
*path
,
1640 struct object
*obj
= &blob
->object
;
1642 obj
->flags
|= LOCAL
;
1644 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1648 return add_one_object(obj
, p
);
1651 static struct object_list
**process_tree(struct tree
*tree
,
1652 struct object_list
**p
,
1653 struct name_path
*path
,
1656 struct object
*obj
= &tree
->object
;
1657 struct tree_desc desc
;
1658 struct name_entry entry
;
1659 struct name_path me
;
1661 obj
->flags
|= LOCAL
;
1663 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1665 if (parse_tree(tree
) < 0)
1666 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1669 name
= xstrdup(name
);
1670 p
= add_one_object(obj
, p
);
1673 me
.elem_len
= strlen(name
);
1675 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1677 while (tree_entry(&desc
, &entry
))
1678 switch (object_type(entry
.mode
)) {
1680 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1683 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1686 /* Subproject commit - not in this repository */
1691 tree
->buffer
= NULL
;
1695 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1698 struct commit
*commit
;
1699 struct object_list
**p
= &objects
;
1702 while ((commit
= get_revision(revs
)) != NULL
) {
1703 p
= process_tree(commit
->tree
, p
, NULL
, "");
1704 commit
->object
.flags
|= LOCAL
;
1705 if (!(commit
->object
.flags
& UNINTERESTING
))
1706 count
+= add_send_request(&commit
->object
, lock
);
1709 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1710 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1711 struct object
*obj
= entry
->item
;
1712 const char *name
= entry
->name
;
1714 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1716 if (obj
->type
== OBJ_TAG
) {
1718 p
= add_one_object(obj
, p
);
1721 if (obj
->type
== OBJ_TREE
) {
1722 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1725 if (obj
->type
== OBJ_BLOB
) {
1726 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1729 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1733 if (!(objects
->item
->flags
& UNINTERESTING
))
1734 count
+= add_send_request(objects
->item
, lock
);
1735 objects
= objects
->next
;
1741 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1743 struct active_request_slot
*slot
;
1744 struct slot_results results
;
1745 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1746 struct curl_slist
*dav_headers
;
1748 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1750 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1752 slot
= get_active_slot();
1753 slot
->results
= &results
;
1754 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1755 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1756 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1757 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1758 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1759 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1760 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1761 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1762 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1764 if (start_active_slot(slot
)) {
1765 run_active_slot(slot
);
1766 strbuf_release(&out_buffer
.buf
);
1767 if (results
.curl_result
!= CURLE_OK
) {
1769 "PUT error: curl result=%d, HTTP code=%ld\n",
1770 results
.curl_result
, results
.http_code
);
1771 /* We should attempt recovery? */
1775 strbuf_release(&out_buffer
.buf
);
1776 fprintf(stderr
, "Unable to start PUT request\n");
1783 static struct ref
*local_refs
, **local_tail
;
1784 static struct ref
*remote_refs
, **remote_tail
;
1786 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1789 int len
= strlen(refname
) + 1;
1790 ref
= xcalloc(1, sizeof(*ref
) + len
);
1791 hashcpy(ref
->new_sha1
, sha1
);
1792 memcpy(ref
->name
, refname
, len
);
1794 local_tail
= &ref
->next
;
1798 static void one_remote_ref(char *refname
)
1803 ref
= alloc_ref(refname
);
1805 if (http_fetch_ref(remote
->url
, ref
) != 0) {
1807 "Unable to fetch ref %s from %s\n",
1808 refname
, remote
->url
);
1814 * Fetch a copy of the object if it doesn't exist locally - it
1815 * may be required for updating server info later.
1817 if (remote
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1818 obj
= lookup_unknown_object(ref
->old_sha1
);
1820 fprintf(stderr
, " fetch %s for %s\n",
1821 sha1_to_hex(ref
->old_sha1
), refname
);
1822 add_fetch_request(obj
);
1827 remote_tail
= &ref
->next
;
1830 static void get_local_heads(void)
1832 local_tail
= &local_refs
;
1833 for_each_ref(one_local_ref
, NULL
);
1836 static void get_dav_remote_heads(void)
1838 remote_tail
= &remote_refs
;
1839 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1842 static int is_zero_sha1(const unsigned char *sha1
)
1846 for (i
= 0; i
< 20; i
++) {
1853 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1856 struct commit_list
*temp
= list
;
1857 temp
->item
->object
.flags
&= ~mark
;
1863 static int ref_newer(const unsigned char *new_sha1
,
1864 const unsigned char *old_sha1
)
1867 struct commit
*old
, *new;
1868 struct commit_list
*list
, *used
;
1871 /* Both new and old must be commit-ish and new is descendant of
1872 * old. Otherwise we require --force.
1874 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1875 if (!o
|| o
->type
!= OBJ_COMMIT
)
1877 old
= (struct commit
*) o
;
1879 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1880 if (!o
|| o
->type
!= OBJ_COMMIT
)
1882 new = (struct commit
*) o
;
1884 if (parse_commit(new) < 0)
1888 commit_list_insert(new, &list
);
1890 new = pop_most_recent_commit(&list
, TMP_MARK
);
1891 commit_list_insert(new, &used
);
1897 unmark_and_free(list
, TMP_MARK
);
1898 unmark_and_free(used
, TMP_MARK
);
1902 static void add_remote_info_ref(struct remote_ls_ctx
*ls
)
1904 struct strbuf
*buf
= (struct strbuf
*)ls
->userData
;
1910 ref
= alloc_ref(ls
->dentry_name
);
1912 if (http_fetch_ref(remote
->url
, ref
) != 0) {
1914 "Unable to fetch ref %s from %s\n",
1915 ls
->dentry_name
, remote
->url
);
1921 o
= parse_object(ref
->old_sha1
);
1924 "Unable to parse object %s for remote ref %s\n",
1925 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1931 len
= strlen(ls
->dentry_name
) + 42;
1932 ref_info
= xcalloc(len
+ 1, 1);
1933 sprintf(ref_info
, "%s %s\n",
1934 sha1_to_hex(ref
->old_sha1
), ls
->dentry_name
);
1935 fwrite_buffer(ref_info
, 1, len
, buf
);
1938 if (o
->type
== OBJ_TAG
) {
1939 o
= deref_tag(o
, ls
->dentry_name
, 0);
1941 len
= strlen(ls
->dentry_name
) + 45;
1942 ref_info
= xcalloc(len
+ 1, 1);
1943 sprintf(ref_info
, "%s %s^{}\n",
1944 sha1_to_hex(o
->sha1
), ls
->dentry_name
);
1945 fwrite_buffer(ref_info
, 1, len
, buf
);
1952 static void update_remote_info_refs(struct remote_lock
*lock
)
1954 struct buffer buffer
= { STRBUF_INIT
, 0 };
1955 struct active_request_slot
*slot
;
1956 struct slot_results results
;
1957 struct curl_slist
*dav_headers
;
1959 remote_ls("refs/", (PROCESS_FILES
| RECURSIVE
),
1960 add_remote_info_ref
, &buffer
.buf
);
1962 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1964 slot
= get_active_slot();
1965 slot
->results
= &results
;
1966 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &buffer
);
1967 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, buffer
.buf
.len
);
1968 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1969 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1970 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1971 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1972 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1973 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1974 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1976 if (start_active_slot(slot
)) {
1977 run_active_slot(slot
);
1978 if (results
.curl_result
!= CURLE_OK
) {
1980 "PUT error: curl result=%d, HTTP code=%ld\n",
1981 results
.curl_result
, results
.http_code
);
1985 strbuf_release(&buffer
.buf
);
1988 static int remote_exists(const char *path
)
1990 char *url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1991 struct active_request_slot
*slot
;
1992 struct slot_results results
;
1995 sprintf(url
, "%s%s", remote
->url
, path
);
1997 slot
= get_active_slot();
1998 slot
->results
= &results
;
1999 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2000 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
2002 if (start_active_slot(slot
)) {
2003 run_active_slot(slot
);
2004 if (results
.http_code
== 404)
2006 else if (results
.curl_result
== CURLE_OK
)
2009 fprintf(stderr
, "HEAD HTTP error %ld\n", results
.http_code
);
2011 fprintf(stderr
, "Unable to start HEAD request\n");
2018 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
2021 struct strbuf buffer
= STRBUF_INIT
;
2022 struct active_request_slot
*slot
;
2023 struct slot_results results
;
2025 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
2026 sprintf(url
, "%s%s", remote
->url
, path
);
2028 slot
= get_active_slot();
2029 slot
->results
= &results
;
2030 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
2031 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
2032 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
2033 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2034 if (start_active_slot(slot
)) {
2035 run_active_slot(slot
);
2036 if (results
.curl_result
!= CURLE_OK
) {
2037 die("Couldn't get %s for remote symref\n%s",
2038 url
, curl_errorstr
);
2041 die("Unable to start remote symref request");
2049 if (buffer
.len
== 0)
2052 /* If it's a symref, set the refname; otherwise try for a sha1 */
2053 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
2054 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
2056 get_sha1_hex(buffer
.buf
, sha1
);
2059 strbuf_release(&buffer
);
2062 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
2064 struct commit
*head
= lookup_commit(head_sha1
);
2065 struct commit
*branch
= lookup_commit(branch_sha1
);
2066 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
2068 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
2071 static int delete_remote_branch(char *pattern
, int force
)
2073 struct ref
*refs
= remote_refs
;
2074 struct ref
*remote_ref
= NULL
;
2075 unsigned char head_sha1
[20];
2076 char *symref
= NULL
;
2078 int patlen
= strlen(pattern
);
2080 struct active_request_slot
*slot
;
2081 struct slot_results results
;
2084 /* Find the remote branch(es) matching the specified branch name */
2085 for (match
= 0; refs
; refs
= refs
->next
) {
2086 char *name
= refs
->name
;
2087 int namelen
= strlen(name
);
2088 if (namelen
< patlen
||
2089 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
2091 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
2097 return error("No remote branch matches %s", pattern
);
2099 return error("More than one remote branch matches %s",
2103 * Remote HEAD must be a symref (not exactly foolproof; a remote
2104 * symlink to a symref will look like a symref)
2106 fetch_symref("HEAD", &symref
, head_sha1
);
2108 return error("Remote HEAD is not a symref");
2110 /* Remote branch must not be the remote HEAD */
2111 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
2112 if (!strcmp(remote_ref
->name
, symref
))
2113 return error("Remote branch %s is the current HEAD",
2115 fetch_symref(symref
, &symref
, head_sha1
);
2118 /* Run extra sanity checks if delete is not forced */
2120 /* Remote HEAD must resolve to a known object */
2122 return error("Remote HEAD symrefs too deep");
2123 if (is_zero_sha1(head_sha1
))
2124 return error("Unable to resolve remote HEAD");
2125 if (!has_sha1_file(head_sha1
))
2126 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
2128 /* Remote branch must resolve to a known object */
2129 if (is_zero_sha1(remote_ref
->old_sha1
))
2130 return error("Unable to resolve remote branch %s",
2132 if (!has_sha1_file(remote_ref
->old_sha1
))
2133 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
));
2135 /* Remote branch must be an ancestor of remote HEAD */
2136 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
2137 return error("The branch '%s' is not an ancestor "
2138 "of your current HEAD.\n"
2139 "If you are sure you want to delete it,"
2140 " run:\n\t'git http-push -D %s %s'",
2141 remote_ref
->name
, remote
->url
, pattern
);
2145 /* Send delete request */
2146 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
2149 url
= xmalloc(strlen(remote
->url
) + strlen(remote_ref
->name
) + 1);
2150 sprintf(url
, "%s%s", remote
->url
, remote_ref
->name
);
2151 slot
= get_active_slot();
2152 slot
->results
= &results
;
2153 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2154 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
2155 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2156 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
2157 if (start_active_slot(slot
)) {
2158 run_active_slot(slot
);
2160 if (results
.curl_result
!= CURLE_OK
)
2161 return error("DELETE request failed (%d/%ld)\n",
2162 results
.curl_result
, results
.http_code
);
2165 return error("Unable to start DELETE request");
2171 int main(int argc
, char **argv
)
2173 struct transfer_request
*request
;
2174 struct transfer_request
*next_request
;
2176 char **refspec
= NULL
;
2177 struct remote_lock
*ref_lock
= NULL
;
2178 struct remote_lock
*info_ref_lock
= NULL
;
2179 struct rev_info revs
;
2180 int delete_branch
= 0;
2181 int force_delete
= 0;
2182 int objects_to_send
;
2187 char *rewritten_url
= NULL
;
2189 setup_git_directory();
2191 remote
= xcalloc(sizeof(*remote
), 1);
2194 for (i
= 1; i
< argc
; i
++, argv
++) {
2198 if (!strcmp(arg
, "--all")) {
2199 push_all
= MATCH_REFS_ALL
;
2202 if (!strcmp(arg
, "--force")) {
2206 if (!strcmp(arg
, "--dry-run")) {
2210 if (!strcmp(arg
, "--verbose")) {
2214 if (!strcmp(arg
, "-d")) {
2218 if (!strcmp(arg
, "-D")) {
2225 char *path
= strstr(arg
, "//");
2227 remote
->path_len
= strlen(arg
);
2229 remote
->path
= strchr(path
+2, '/');
2231 remote
->path_len
= strlen(remote
->path
);
2236 nr_refspec
= argc
- i
;
2240 #ifndef USE_CURL_MULTI
2241 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2245 usage(http_push_usage
);
2247 if (delete_branch
&& nr_refspec
!= 1)
2248 die("You must specify only one branch name when deleting a remote branch");
2250 memset(remote_dir_exists
, -1, 256);
2254 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
2256 if (remote
->url
&& remote
->url
[strlen(remote
->url
)-1] != '/') {
2257 rewritten_url
= xmalloc(strlen(remote
->url
)+2);
2258 strcpy(rewritten_url
, remote
->url
);
2259 strcat(rewritten_url
, "/");
2260 remote
->path
= rewritten_url
+ (remote
->path
- remote
->url
);
2262 remote
->url
= rewritten_url
;
2265 /* Verify DAV compliance/lock support */
2266 if (!locking_available()) {
2271 signal(SIGINT
, remove_locks_on_signal
);
2272 signal(SIGHUP
, remove_locks_on_signal
);
2273 signal(SIGQUIT
, remove_locks_on_signal
);
2274 signal(SIGTERM
, remove_locks_on_signal
);
2276 /* Check whether the remote has server info files */
2277 remote
->can_update_info_refs
= 0;
2278 remote
->has_info_refs
= remote_exists("info/refs");
2279 remote
->has_info_packs
= remote_exists("objects/info/packs");
2280 if (remote
->has_info_refs
) {
2281 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
2283 remote
->can_update_info_refs
= 1;
2285 fprintf(stderr
, "Error: cannot lock existing info/refs\n");
2290 if (remote
->has_info_packs
)
2293 /* Get a list of all local and remote heads to validate refspecs */
2295 fprintf(stderr
, "Fetching remote heads...\n");
2296 get_dav_remote_heads();
2298 /* Remove a remote branch if -d or -D was specified */
2299 if (delete_branch
) {
2300 if (delete_remote_branch(refspec
[0], force_delete
) == -1)
2301 fprintf(stderr
, "Unable to delete remote branch %s\n",
2308 remote_tail
= &remote_refs
;
2309 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
2310 nr_refspec
, (const char **) refspec
, push_all
)) {
2315 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
2321 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2322 char old_hex
[60], *new_hex
;
2323 const char *commit_argv
[4];
2325 char *new_sha1_hex
, *old_sha1_hex
;
2330 if (is_zero_sha1(ref
->peer_ref
->new_sha1
)) {
2331 if (delete_remote_branch(ref
->name
, 1) == -1) {
2332 error("Could not remove %s", ref
->name
);
2339 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
2340 if (push_verbosely
|| 1)
2341 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
2346 !is_zero_sha1(ref
->old_sha1
) &&
2348 if (!has_sha1_file(ref
->old_sha1
) ||
2349 !ref_newer(ref
->peer_ref
->new_sha1
,
2352 * We do not have the remote ref, or
2353 * we know that the remote ref is not
2354 * an ancestor of what we are trying to
2355 * push. Either way this can be losing
2356 * commits at the remote end and likely
2357 * we were not up to date to begin with.
2359 error("remote '%s' is not an ancestor of\n"
2361 "Maybe you are not up-to-date and "
2362 "need to pull first?",
2364 ref
->peer_ref
->name
);
2369 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2371 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2372 new_hex
= sha1_to_hex(ref
->new_sha1
);
2374 fprintf(stderr
, "updating '%s'", ref
->name
);
2375 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2376 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2377 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2381 /* Lock remote branch ref */
2382 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2383 if (ref_lock
== NULL
) {
2384 fprintf(stderr
, "Unable to lock remote branch %s\n",
2390 /* Set up revision info for this refspec */
2392 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2393 old_sha1_hex
= NULL
;
2394 commit_argv
[1] = "--objects";
2395 commit_argv
[2] = new_sha1_hex
;
2396 if (!push_all
&& !is_zero_sha1(ref
->old_sha1
)) {
2397 old_sha1_hex
= xmalloc(42);
2398 sprintf(old_sha1_hex
, "^%s",
2399 sha1_to_hex(ref
->old_sha1
));
2400 commit_argv
[3] = old_sha1_hex
;
2403 init_revisions(&revs
, setup_git_directory());
2404 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2405 revs
.edge_hint
= 0; /* just in case */
2409 commit_argv
[1] = NULL
;
2412 /* Generate a list of objects that need to be pushed */
2414 if (prepare_revision_walk(&revs
))
2415 die("revision walk setup failed");
2416 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2417 objects_to_send
= get_delta(&revs
, ref_lock
);
2418 finish_all_active_slots();
2420 /* Push missing objects to remote, this would be a
2421 convenient time to pack them first if appropriate. */
2423 if (objects_to_send
)
2424 fprintf(stderr
, " sending %d objects\n",
2426 #ifdef USE_CURL_MULTI
2427 fill_active_slots();
2428 add_fill_function(NULL
, fill_active_slot
);
2431 finish_all_active_slots();
2432 #ifdef USE_CURL_MULTI
2433 fill_active_slots();
2435 } while (request_queue_head
&& !aborted
);
2437 /* Update the remote branch if all went well */
2438 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2442 fprintf(stderr
, " done\n");
2443 unlock_remote(ref_lock
);
2447 /* Update remote server info if appropriate */
2448 if (remote
->has_info_refs
&& new_refs
) {
2449 if (info_ref_lock
&& remote
->can_update_info_refs
) {
2450 fprintf(stderr
, "Updating remote server info\n");
2452 update_remote_info_refs(info_ref_lock
);
2454 fprintf(stderr
, "Unable to update server info\n");
2459 free(rewritten_url
);
2461 unlock_remote(info_ref_lock
);
2464 curl_slist_free_all(no_pragma_header
);
2468 request
= request_queue_head
;
2469 while (request
!= NULL
) {
2470 next_request
= request
->next
;
2471 release_request(request
);
2472 request
= next_request
;