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
, "_");
559 strbuf_addstr(&buf
, request
->lock
->token
);
560 request
->url
= strbuf_detach(&buf
, NULL
);
562 slot
= get_active_slot();
563 slot
->callback_func
= process_response
;
564 slot
->callback_data
= request
;
565 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
566 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.buf
.len
);
567 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
568 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
569 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
570 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
571 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
572 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
573 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
575 if (start_active_slot(slot
)) {
576 request
->slot
= slot
;
577 request
->state
= RUN_PUT
;
579 request
->state
= ABORTED
;
585 static void start_move(struct transfer_request
*request
)
587 struct active_request_slot
*slot
;
588 struct curl_slist
*dav_headers
= NULL
;
590 slot
= get_active_slot();
591 slot
->callback_func
= process_response
;
592 slot
->callback_data
= request
;
593 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
594 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
595 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
596 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
597 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
598 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
599 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
601 if (start_active_slot(slot
)) {
602 request
->slot
= slot
;
603 request
->state
= RUN_MOVE
;
605 request
->state
= ABORTED
;
611 static int refresh_lock(struct remote_lock
*lock
)
613 struct active_request_slot
*slot
;
614 struct slot_results results
;
615 struct curl_slist
*dav_headers
;
618 lock
->refreshing
= 1;
620 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
| DAV_HEADER_TIMEOUT
);
622 slot
= get_active_slot();
623 slot
->results
= &results
;
624 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
625 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
626 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
627 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
628 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
630 if (start_active_slot(slot
)) {
631 run_active_slot(slot
);
632 if (results
.curl_result
!= CURLE_OK
) {
633 fprintf(stderr
, "LOCK HTTP error %ld\n",
636 lock
->start_time
= time(NULL
);
641 lock
->refreshing
= 0;
642 curl_slist_free_all(dav_headers
);
647 static void check_locks(void)
649 struct remote_lock
*lock
= remote
->locks
;
650 time_t current_time
= time(NULL
);
654 time_remaining
= lock
->start_time
+ lock
->timeout
-
656 if (!lock
->refreshing
&& time_remaining
< LOCK_REFRESH
) {
657 if (!refresh_lock(lock
)) {
659 "Unable to refresh lock for %s\n",
669 static void release_request(struct transfer_request
*request
)
671 struct transfer_request
*entry
= request_queue_head
;
673 if (request
== request_queue_head
) {
674 request_queue_head
= request
->next
;
676 while (entry
->next
!= NULL
&& entry
->next
!= request
)
678 if (entry
->next
== request
)
679 entry
->next
= entry
->next
->next
;
682 if (request
->local_fileno
!= -1)
683 close(request
->local_fileno
);
684 if (request
->local_stream
)
685 fclose(request
->local_stream
);
690 static void finish_request(struct transfer_request
*request
)
693 struct packed_git
*target
;
694 struct packed_git
**lst
;
696 request
->curl_result
= request
->slot
->curl_result
;
697 request
->http_code
= request
->slot
->http_code
;
698 request
->slot
= NULL
;
700 /* Keep locks active */
703 if (request
->headers
!= NULL
)
704 curl_slist_free_all(request
->headers
);
706 /* URL is reused for MOVE after PUT */
707 if (request
->state
!= RUN_PUT
) {
712 if (request
->state
== RUN_MKCOL
) {
713 if (request
->curl_result
== CURLE_OK
||
714 request
->http_code
== 405) {
715 remote_dir_exists
[request
->obj
->sha1
[0]] = 1;
718 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
719 sha1_to_hex(request
->obj
->sha1
),
720 request
->curl_result
, request
->http_code
);
721 request
->state
= ABORTED
;
724 } else if (request
->state
== RUN_PUT
) {
725 if (request
->curl_result
== CURLE_OK
) {
728 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
729 sha1_to_hex(request
->obj
->sha1
),
730 request
->curl_result
, request
->http_code
);
731 request
->state
= ABORTED
;
734 } else if (request
->state
== RUN_MOVE
) {
735 if (request
->curl_result
== CURLE_OK
) {
737 fprintf(stderr
, " sent %s\n",
738 sha1_to_hex(request
->obj
->sha1
));
739 request
->obj
->flags
|= REMOTE
;
740 release_request(request
);
742 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
743 sha1_to_hex(request
->obj
->sha1
),
744 request
->curl_result
, request
->http_code
);
745 request
->state
= ABORTED
;
748 } else if (request
->state
== RUN_FETCH_LOOSE
) {
749 fchmod(request
->local_fileno
, 0444);
750 close(request
->local_fileno
); request
->local_fileno
= -1;
752 if (request
->curl_result
!= CURLE_OK
&&
753 request
->http_code
!= 416) {
754 if (stat(request
->tmpfile
, &st
) == 0) {
756 unlink(request
->tmpfile
);
759 if (request
->http_code
== 416)
760 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
762 git_inflate_end(&request
->stream
);
763 git_SHA1_Final(request
->real_sha1
, &request
->c
);
764 if (request
->zret
!= Z_STREAM_END
) {
765 unlink(request
->tmpfile
);
766 } else if (hashcmp(request
->obj
->sha1
, request
->real_sha1
)) {
767 unlink(request
->tmpfile
);
773 if (request
->rename
== 0) {
774 request
->obj
->flags
|= (LOCAL
| REMOTE
);
779 /* Try fetching packed if necessary */
780 if (request
->obj
->flags
& LOCAL
)
781 release_request(request
);
783 start_fetch_packed(request
);
785 } else if (request
->state
== RUN_FETCH_PACKED
) {
786 if (request
->curl_result
!= CURLE_OK
) {
787 fprintf(stderr
, "Unable to get pack file %s\n%s",
788 request
->url
, curl_errorstr
);
789 remote
->can_update_info_refs
= 0;
791 off_t pack_size
= ftell(request
->local_stream
);
793 fclose(request
->local_stream
);
794 request
->local_stream
= NULL
;
795 if (!move_temp_to_file(request
->tmpfile
,
796 request
->filename
)) {
797 target
= (struct packed_git
*)request
->userData
;
798 target
->pack_size
= pack_size
;
799 lst
= &remote
->packs
;
800 while (*lst
!= target
)
801 lst
= &((*lst
)->next
);
804 if (!verify_pack(target
))
805 install_packed_git(target
);
807 remote
->can_update_info_refs
= 0;
810 release_request(request
);
814 #ifdef USE_CURL_MULTI
815 static int fill_active_slot(void *unused
)
817 struct transfer_request
*request
= request_queue_head
;
822 for (request
= request_queue_head
; request
; request
= request
->next
) {
823 if (request
->state
== NEED_FETCH
) {
824 start_fetch_loose(request
);
826 } else if (pushing
&& request
->state
== NEED_PUSH
) {
827 if (remote_dir_exists
[request
->obj
->sha1
[0]] == 1) {
830 start_mkcol(request
);
839 static void get_remote_object_list(unsigned char parent
);
841 static void add_fetch_request(struct object
*obj
)
843 struct transfer_request
*request
;
848 * Don't fetch the object if it's known to exist locally
849 * or is already in the request queue
851 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
852 get_remote_object_list(obj
->sha1
[0]);
853 if (obj
->flags
& (LOCAL
| FETCHING
))
856 obj
->flags
|= FETCHING
;
857 request
= xmalloc(sizeof(*request
));
860 request
->lock
= NULL
;
861 request
->headers
= NULL
;
862 request
->local_fileno
= -1;
863 request
->local_stream
= NULL
;
864 request
->state
= NEED_FETCH
;
865 request
->next
= request_queue_head
;
866 request_queue_head
= request
;
868 #ifdef USE_CURL_MULTI
874 static int add_send_request(struct object
*obj
, struct remote_lock
*lock
)
876 struct transfer_request
*request
= request_queue_head
;
877 struct packed_git
*target
;
879 /* Keep locks active */
883 * Don't push the object if it's known to exist on the remote
884 * or is already in the request queue
886 if (remote_dir_exists
[obj
->sha1
[0]] == -1)
887 get_remote_object_list(obj
->sha1
[0]);
888 if (obj
->flags
& (REMOTE
| PUSHING
))
890 target
= find_sha1_pack(obj
->sha1
, remote
->packs
);
892 obj
->flags
|= REMOTE
;
896 obj
->flags
|= PUSHING
;
897 request
= xmalloc(sizeof(*request
));
900 request
->lock
= lock
;
901 request
->headers
= NULL
;
902 request
->local_fileno
= -1;
903 request
->local_stream
= NULL
;
904 request
->state
= NEED_PUSH
;
905 request
->next
= request_queue_head
;
906 request_queue_head
= request
;
908 #ifdef USE_CURL_MULTI
916 static int fetch_index(unsigned char *sha1
)
918 char *hex
= sha1_to_hex(sha1
);
921 char tmpfile
[PATH_MAX
];
923 char range
[RANGE_HEADER_SIZE
];
924 struct curl_slist
*range_header
= NULL
;
927 struct active_request_slot
*slot
;
928 struct slot_results results
;
930 /* Don't use the index if the pack isn't there */
931 url
= xmalloc(strlen(remote
->url
) + 64);
932 sprintf(url
, "%sobjects/pack/pack-%s.pack", remote
->url
, hex
);
933 slot
= get_active_slot();
934 slot
->results
= &results
;
935 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
936 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
937 if (start_active_slot(slot
)) {
938 run_active_slot(slot
);
939 if (results
.curl_result
!= CURLE_OK
) {
941 return error("Unable to verify pack %s is available",
946 return error("Unable to start request");
949 if (has_pack_index(sha1
)) {
955 fprintf(stderr
, "Getting index for pack %s\n", hex
);
957 sprintf(url
, "%sobjects/pack/pack-%s.idx", remote
->url
, hex
);
959 filename
= sha1_pack_index_name(sha1
);
960 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
961 indexfile
= fopen(tmpfile
, "a");
964 return error("Unable to open local file %s for pack index",
968 slot
= get_active_slot();
969 slot
->results
= &results
;
970 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
971 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
972 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
973 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
974 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
975 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
976 slot
->local
= indexfile
;
978 /* If there is data present from a previous transfer attempt,
979 resume where it left off */
980 prev_posn
= ftell(indexfile
);
984 "Resuming fetch of index for pack %s at byte %ld\n",
986 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
987 range_header
= curl_slist_append(range_header
, range
);
988 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
991 if (start_active_slot(slot
)) {
992 run_active_slot(slot
);
993 if (results
.curl_result
!= CURLE_OK
) {
996 return error("Unable to get pack index %s\n%s", url
,
1002 return error("Unable to start request");
1008 return move_temp_to_file(tmpfile
, filename
);
1011 static int setup_index(unsigned char *sha1
)
1013 struct packed_git
*new_pack
;
1015 if (fetch_index(sha1
))
1018 new_pack
= parse_pack_index(sha1
);
1019 new_pack
->next
= remote
->packs
;
1020 remote
->packs
= new_pack
;
1024 static int fetch_indices(void)
1026 unsigned char sha1
[20];
1028 struct strbuf buffer
= STRBUF_INIT
;
1032 struct active_request_slot
*slot
;
1033 struct slot_results results
;
1036 fprintf(stderr
, "Getting pack list\n");
1038 url
= xmalloc(strlen(remote
->url
) + 20);
1039 sprintf(url
, "%sobjects/info/packs", remote
->url
);
1041 slot
= get_active_slot();
1042 slot
->results
= &results
;
1043 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1044 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1045 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1046 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1047 if (start_active_slot(slot
)) {
1048 run_active_slot(slot
);
1049 if (results
.curl_result
!= CURLE_OK
) {
1050 strbuf_release(&buffer
);
1052 if (results
.http_code
== 404)
1055 return error("%s", curl_errorstr
);
1058 strbuf_release(&buffer
);
1060 return error("Unable to start request");
1065 while (i
< buffer
.len
) {
1069 if (i
+ 52 < buffer
.len
&&
1070 !prefixcmp(data
+ i
, " pack-") &&
1071 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1072 get_sha1_hex(data
+ i
+ 6, sha1
);
1078 while (data
[i
] != '\n')
1084 strbuf_release(&buffer
);
1088 static void one_remote_object(const char *hex
)
1090 unsigned char sha1
[20];
1093 if (get_sha1_hex(hex
, sha1
) != 0)
1096 obj
= lookup_object(sha1
);
1098 obj
= parse_object(sha1
);
1100 /* Ignore remote objects that don't exist locally */
1104 obj
->flags
|= REMOTE
;
1105 if (!object_list_contains(objects
, obj
))
1106 object_list_insert(obj
, &objects
);
1109 static void handle_lockprop_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1111 int *lock_flags
= (int *)ctx
->userData
;
1114 if (!strcmp(ctx
->name
, DAV_CTX_LOCKENTRY
)) {
1115 if ((*lock_flags
& DAV_PROP_LOCKEX
) &&
1116 (*lock_flags
& DAV_PROP_LOCKWR
)) {
1117 *lock_flags
|= DAV_LOCK_OK
;
1119 *lock_flags
&= DAV_LOCK_OK
;
1120 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_WRITE
)) {
1121 *lock_flags
|= DAV_PROP_LOCKWR
;
1122 } else if (!strcmp(ctx
->name
, DAV_CTX_LOCKTYPE_EXCLUSIVE
)) {
1123 *lock_flags
|= DAV_PROP_LOCKEX
;
1128 static void handle_new_lock_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1130 struct remote_lock
*lock
= (struct remote_lock
*)ctx
->userData
;
1132 if (tag_closed
&& ctx
->cdata
) {
1133 if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_OWNER
)) {
1134 lock
->owner
= xmalloc(strlen(ctx
->cdata
) + 1);
1135 strcpy(lock
->owner
, ctx
->cdata
);
1136 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TIMEOUT
)) {
1137 if (!prefixcmp(ctx
->cdata
, "Second-"))
1139 strtol(ctx
->cdata
+ 7, NULL
, 10);
1140 } else if (!strcmp(ctx
->name
, DAV_ACTIVELOCK_TOKEN
)) {
1141 lock
->token
= xmalloc(strlen(ctx
->cdata
) + 1);
1142 strcpy(lock
->token
, ctx
->cdata
);
1147 static void one_remote_ref(char *refname
);
1150 xml_start_tag(void *userData
, const char *name
, const char **atts
)
1152 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1153 const char *c
= strchr(name
, ':');
1161 new_len
= strlen(ctx
->name
) + strlen(c
) + 2;
1163 if (new_len
> ctx
->len
) {
1164 ctx
->name
= xrealloc(ctx
->name
, new_len
);
1167 strcat(ctx
->name
, ".");
1168 strcat(ctx
->name
, c
);
1173 ctx
->userFunc(ctx
, 0);
1177 xml_end_tag(void *userData
, const char *name
)
1179 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1180 const char *c
= strchr(name
, ':');
1183 ctx
->userFunc(ctx
, 1);
1190 ep
= ctx
->name
+ strlen(ctx
->name
) - strlen(c
) - 1;
1195 xml_cdata(void *userData
, const XML_Char
*s
, int len
)
1197 struct xml_ctx
*ctx
= (struct xml_ctx
*)userData
;
1199 ctx
->cdata
= xmemdupz(s
, len
);
1202 static struct remote_lock
*lock_remote(const char *path
, long timeout
)
1204 struct active_request_slot
*slot
;
1205 struct slot_results results
;
1206 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1207 struct strbuf in_buffer
= STRBUF_INIT
;
1210 char timeout_header
[25];
1211 struct remote_lock
*lock
= NULL
;
1212 struct curl_slist
*dav_headers
= NULL
;
1215 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1216 sprintf(url
, "%s%s", remote
->url
, path
);
1218 /* Make sure leading directories exist for the remote ref */
1219 ep
= strchr(url
+ strlen(remote
->url
) + 1, '/');
1221 char saved_character
= ep
[1];
1223 slot
= get_active_slot();
1224 slot
->results
= &results
;
1225 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1226 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1227 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1228 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1229 if (start_active_slot(slot
)) {
1230 run_active_slot(slot
);
1231 if (results
.curl_result
!= CURLE_OK
&&
1232 results
.http_code
!= 405) {
1234 "Unable to create branch path %s\n",
1240 fprintf(stderr
, "Unable to start MKCOL request\n");
1244 ep
[1] = saved_character
;
1245 ep
= strchr(ep
+ 1, '/');
1248 strbuf_addf(&out_buffer
.buf
, LOCK_REQUEST
, git_default_email
);
1250 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1251 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1252 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1254 slot
= get_active_slot();
1255 slot
->results
= &results
;
1256 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1257 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1259 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1260 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1261 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1262 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1263 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1264 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1266 lock
= xcalloc(1, sizeof(*lock
));
1269 if (start_active_slot(slot
)) {
1270 run_active_slot(slot
);
1271 if (results
.curl_result
== CURLE_OK
) {
1272 XML_Parser parser
= XML_ParserCreate(NULL
);
1273 enum XML_Status result
;
1274 ctx
.name
= xcalloc(10, 1);
1277 ctx
.userFunc
= handle_new_lock_ctx
;
1278 ctx
.userData
= lock
;
1279 XML_SetUserData(parser
, &ctx
);
1280 XML_SetElementHandler(parser
, xml_start_tag
,
1282 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1283 result
= XML_Parse(parser
, in_buffer
.buf
,
1286 if (result
!= XML_STATUS_OK
) {
1287 fprintf(stderr
, "XML error: %s\n",
1289 XML_GetErrorCode(parser
)));
1292 XML_ParserFree(parser
);
1295 fprintf(stderr
, "Unable to start LOCK request\n");
1298 curl_slist_free_all(dav_headers
);
1299 strbuf_release(&out_buffer
.buf
);
1300 strbuf_release(&in_buffer
);
1302 if (lock
->token
== NULL
|| lock
->timeout
<= 0) {
1310 lock
->start_time
= time(NULL
);
1311 lock
->next
= remote
->locks
;
1312 remote
->locks
= lock
;
1318 static int unlock_remote(struct remote_lock
*lock
)
1320 struct active_request_slot
*slot
;
1321 struct slot_results results
;
1322 struct remote_lock
*prev
= remote
->locks
;
1323 struct curl_slist
*dav_headers
;
1326 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_LOCK
);
1328 slot
= get_active_slot();
1329 slot
->results
= &results
;
1330 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1331 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1332 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1333 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1335 if (start_active_slot(slot
)) {
1336 run_active_slot(slot
);
1337 if (results
.curl_result
== CURLE_OK
)
1340 fprintf(stderr
, "UNLOCK HTTP error %ld\n",
1343 fprintf(stderr
, "Unable to start UNLOCK request\n");
1346 curl_slist_free_all(dav_headers
);
1348 if (remote
->locks
== lock
) {
1349 remote
->locks
= lock
->next
;
1351 while (prev
&& prev
->next
!= lock
)
1354 prev
->next
= prev
->next
->next
;
1365 static void remove_locks(void)
1367 struct remote_lock
*lock
= remote
->locks
;
1369 fprintf(stderr
, "Removing remote locks...\n");
1371 unlock_remote(lock
);
1376 static void remove_locks_on_signal(int signo
)
1379 signal(signo
, SIG_DFL
);
1383 static void remote_ls(const char *path
, int flags
,
1384 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1387 static void process_ls_object(struct remote_ls_ctx
*ls
)
1389 unsigned int *parent
= (unsigned int *)ls
->userData
;
1390 char *path
= ls
->dentry_name
;
1393 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->flags
& IS_DIR
)) {
1394 remote_dir_exists
[*parent
] = 1;
1398 if (strlen(path
) != 49)
1401 obj_hex
= xmalloc(strlen(path
));
1402 /* NB: path is not null-terminated, can not use strlcpy here */
1403 memcpy(obj_hex
, path
, 2);
1404 strcpy(obj_hex
+ 2, path
+ 3);
1405 one_remote_object(obj_hex
);
1409 static void process_ls_ref(struct remote_ls_ctx
*ls
)
1411 if (!strcmp(ls
->path
, ls
->dentry_name
) && (ls
->dentry_flags
& IS_DIR
)) {
1412 fprintf(stderr
, " %s\n", ls
->dentry_name
);
1416 if (!(ls
->dentry_flags
& IS_DIR
))
1417 one_remote_ref(ls
->dentry_name
);
1420 static void handle_remote_ls_ctx(struct xml_ctx
*ctx
, int tag_closed
)
1422 struct remote_ls_ctx
*ls
= (struct remote_ls_ctx
*)ctx
->userData
;
1425 if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
) && ls
->dentry_name
) {
1426 if (ls
->dentry_flags
& IS_DIR
) {
1427 if (ls
->flags
& PROCESS_DIRS
) {
1430 if (strcmp(ls
->dentry_name
, ls
->path
) &&
1431 ls
->flags
& RECURSIVE
) {
1432 remote_ls(ls
->dentry_name
,
1437 } else if (ls
->flags
& PROCESS_FILES
) {
1440 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_NAME
) && ctx
->cdata
) {
1441 char *path
= ctx
->cdata
;
1442 if (*ctx
->cdata
== 'h') {
1443 path
= strstr(path
, "//");
1445 path
= strchr(path
+2, '/');
1449 path
+= remote
->path_len
;
1450 ls
->dentry_name
= xstrdup(path
);
1452 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_COLLECTION
)) {
1453 ls
->dentry_flags
|= IS_DIR
;
1455 } else if (!strcmp(ctx
->name
, DAV_PROPFIND_RESP
)) {
1456 free(ls
->dentry_name
);
1457 ls
->dentry_name
= NULL
;
1458 ls
->dentry_flags
= 0;
1463 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1464 * should _only_ heed the information from that file, instead of trying to
1465 * determine the refs from the remote file system (badly: it does not even
1466 * know about packed-refs).
1468 static void remote_ls(const char *path
, int flags
,
1469 void (*userFunc
)(struct remote_ls_ctx
*ls
),
1472 char *url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1473 struct active_request_slot
*slot
;
1474 struct slot_results results
;
1475 struct strbuf in_buffer
= STRBUF_INIT
;
1476 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1477 struct curl_slist
*dav_headers
= NULL
;
1479 struct remote_ls_ctx ls
;
1482 ls
.path
= xstrdup(path
);
1483 ls
.dentry_name
= NULL
;
1484 ls
.dentry_flags
= 0;
1485 ls
.userData
= userData
;
1486 ls
.userFunc
= userFunc
;
1488 sprintf(url
, "%s%s", remote
->url
, path
);
1490 strbuf_addf(&out_buffer
.buf
, PROPFIND_ALL_REQUEST
);
1492 dav_headers
= curl_slist_append(dav_headers
, "Depth: 1");
1493 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1495 slot
= get_active_slot();
1496 slot
->results
= &results
;
1497 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1498 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1499 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1500 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1501 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1502 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1503 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1504 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1505 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1507 if (start_active_slot(slot
)) {
1508 run_active_slot(slot
);
1509 if (results
.curl_result
== CURLE_OK
) {
1510 XML_Parser parser
= XML_ParserCreate(NULL
);
1511 enum XML_Status result
;
1512 ctx
.name
= xcalloc(10, 1);
1515 ctx
.userFunc
= handle_remote_ls_ctx
;
1517 XML_SetUserData(parser
, &ctx
);
1518 XML_SetElementHandler(parser
, xml_start_tag
,
1520 XML_SetCharacterDataHandler(parser
, xml_cdata
);
1521 result
= XML_Parse(parser
, in_buffer
.buf
,
1525 if (result
!= XML_STATUS_OK
) {
1526 fprintf(stderr
, "XML error: %s\n",
1528 XML_GetErrorCode(parser
)));
1530 XML_ParserFree(parser
);
1533 fprintf(stderr
, "Unable to start PROPFIND request\n");
1538 strbuf_release(&out_buffer
.buf
);
1539 strbuf_release(&in_buffer
);
1540 curl_slist_free_all(dav_headers
);
1543 static void get_remote_object_list(unsigned char parent
)
1545 char path
[] = "objects/XX/";
1546 static const char hex
[] = "0123456789abcdef";
1547 unsigned int val
= parent
;
1549 path
[8] = hex
[val
>> 4];
1550 path
[9] = hex
[val
& 0xf];
1551 remote_dir_exists
[val
] = 0;
1552 remote_ls(path
, (PROCESS_FILES
| PROCESS_DIRS
),
1553 process_ls_object
, &val
);
1556 static int locking_available(void)
1558 struct active_request_slot
*slot
;
1559 struct slot_results results
;
1560 struct strbuf in_buffer
= STRBUF_INIT
;
1561 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1562 struct curl_slist
*dav_headers
= NULL
;
1566 strbuf_addf(&out_buffer
.buf
, PROPFIND_SUPPORTEDLOCK_REQUEST
, remote
->url
);
1568 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1569 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1571 slot
= get_active_slot();
1572 slot
->results
= &results
;
1573 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1574 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1575 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1576 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1577 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1578 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1579 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1580 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1581 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1583 if (start_active_slot(slot
)) {
1584 run_active_slot(slot
);
1585 if (results
.curl_result
== CURLE_OK
) {
1586 XML_Parser parser
= XML_ParserCreate(NULL
);
1587 enum XML_Status result
;
1588 ctx
.name
= xcalloc(10, 1);
1591 ctx
.userFunc
= handle_lockprop_ctx
;
1592 ctx
.userData
= &lock_flags
;
1593 XML_SetUserData(parser
, &ctx
);
1594 XML_SetElementHandler(parser
, xml_start_tag
,
1596 result
= XML_Parse(parser
, in_buffer
.buf
,
1600 if (result
!= XML_STATUS_OK
) {
1601 fprintf(stderr
, "XML error: %s\n",
1603 XML_GetErrorCode(parser
)));
1606 XML_ParserFree(parser
);
1608 error("Error: no DAV locking support on %s",
1612 error("Cannot access URL %s, return code %d",
1613 remote
->url
, results
.curl_result
);
1617 error("Unable to start PROPFIND request on %s", remote
->url
);
1620 strbuf_release(&out_buffer
.buf
);
1621 strbuf_release(&in_buffer
);
1622 curl_slist_free_all(dav_headers
);
1627 static struct object_list
**add_one_object(struct object
*obj
, struct object_list
**p
)
1629 struct object_list
*entry
= xmalloc(sizeof(struct object_list
));
1633 return &entry
->next
;
1636 static struct object_list
**process_blob(struct blob
*blob
,
1637 struct object_list
**p
,
1638 struct name_path
*path
,
1641 struct object
*obj
= &blob
->object
;
1643 obj
->flags
|= LOCAL
;
1645 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1649 return add_one_object(obj
, p
);
1652 static struct object_list
**process_tree(struct tree
*tree
,
1653 struct object_list
**p
,
1654 struct name_path
*path
,
1657 struct object
*obj
= &tree
->object
;
1658 struct tree_desc desc
;
1659 struct name_entry entry
;
1660 struct name_path me
;
1662 obj
->flags
|= LOCAL
;
1664 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1666 if (parse_tree(tree
) < 0)
1667 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
1670 name
= xstrdup(name
);
1671 p
= add_one_object(obj
, p
);
1674 me
.elem_len
= strlen(name
);
1676 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
1678 while (tree_entry(&desc
, &entry
))
1679 switch (object_type(entry
.mode
)) {
1681 p
= process_tree(lookup_tree(entry
.sha1
), p
, &me
, name
);
1684 p
= process_blob(lookup_blob(entry
.sha1
), p
, &me
, name
);
1687 /* Subproject commit - not in this repository */
1692 tree
->buffer
= NULL
;
1696 static int get_delta(struct rev_info
*revs
, struct remote_lock
*lock
)
1699 struct commit
*commit
;
1700 struct object_list
**p
= &objects
;
1703 while ((commit
= get_revision(revs
)) != NULL
) {
1704 p
= process_tree(commit
->tree
, p
, NULL
, "");
1705 commit
->object
.flags
|= LOCAL
;
1706 if (!(commit
->object
.flags
& UNINTERESTING
))
1707 count
+= add_send_request(&commit
->object
, lock
);
1710 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1711 struct object_array_entry
*entry
= revs
->pending
.objects
+ i
;
1712 struct object
*obj
= entry
->item
;
1713 const char *name
= entry
->name
;
1715 if (obj
->flags
& (UNINTERESTING
| SEEN
))
1717 if (obj
->type
== OBJ_TAG
) {
1719 p
= add_one_object(obj
, p
);
1722 if (obj
->type
== OBJ_TREE
) {
1723 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
1726 if (obj
->type
== OBJ_BLOB
) {
1727 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
1730 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
1734 if (!(objects
->item
->flags
& UNINTERESTING
))
1735 count
+= add_send_request(objects
->item
, lock
);
1736 objects
= objects
->next
;
1742 static int update_remote(unsigned char *sha1
, struct remote_lock
*lock
)
1744 struct active_request_slot
*slot
;
1745 struct slot_results results
;
1746 struct buffer out_buffer
= { STRBUF_INIT
, 0 };
1747 struct curl_slist
*dav_headers
;
1749 dav_headers
= get_dav_token_headers(lock
, DAV_HEADER_IF
);
1751 strbuf_addf(&out_buffer
.buf
, "%s\n", sha1_to_hex(sha1
));
1753 slot
= get_active_slot();
1754 slot
->results
= &results
;
1755 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1756 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.buf
.len
);
1757 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1758 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1759 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1760 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1761 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1762 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1763 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1765 if (start_active_slot(slot
)) {
1766 run_active_slot(slot
);
1767 strbuf_release(&out_buffer
.buf
);
1768 if (results
.curl_result
!= CURLE_OK
) {
1770 "PUT error: curl result=%d, HTTP code=%ld\n",
1771 results
.curl_result
, results
.http_code
);
1772 /* We should attempt recovery? */
1776 strbuf_release(&out_buffer
.buf
);
1777 fprintf(stderr
, "Unable to start PUT request\n");
1784 static struct ref
*local_refs
, **local_tail
;
1785 static struct ref
*remote_refs
, **remote_tail
;
1787 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
1790 int len
= strlen(refname
) + 1;
1791 ref
= xcalloc(1, sizeof(*ref
) + len
);
1792 hashcpy(ref
->new_sha1
, sha1
);
1793 memcpy(ref
->name
, refname
, len
);
1795 local_tail
= &ref
->next
;
1799 static void one_remote_ref(char *refname
)
1804 ref
= alloc_ref(refname
);
1806 if (http_fetch_ref(remote
->url
, ref
) != 0) {
1808 "Unable to fetch ref %s from %s\n",
1809 refname
, remote
->url
);
1815 * Fetch a copy of the object if it doesn't exist locally - it
1816 * may be required for updating server info later.
1818 if (remote
->can_update_info_refs
&& !has_sha1_file(ref
->old_sha1
)) {
1819 obj
= lookup_unknown_object(ref
->old_sha1
);
1821 fprintf(stderr
, " fetch %s for %s\n",
1822 sha1_to_hex(ref
->old_sha1
), refname
);
1823 add_fetch_request(obj
);
1828 remote_tail
= &ref
->next
;
1831 static void get_local_heads(void)
1833 local_tail
= &local_refs
;
1834 for_each_ref(one_local_ref
, NULL
);
1837 static void get_dav_remote_heads(void)
1839 remote_tail
= &remote_refs
;
1840 remote_ls("refs/", (PROCESS_FILES
| PROCESS_DIRS
| RECURSIVE
), process_ls_ref
, NULL
);
1843 static int is_zero_sha1(const unsigned char *sha1
)
1847 for (i
= 0; i
< 20; i
++) {
1854 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
1857 struct commit_list
*temp
= list
;
1858 temp
->item
->object
.flags
&= ~mark
;
1864 static int ref_newer(const unsigned char *new_sha1
,
1865 const unsigned char *old_sha1
)
1868 struct commit
*old
, *new;
1869 struct commit_list
*list
, *used
;
1872 /* Both new and old must be commit-ish and new is descendant of
1873 * old. Otherwise we require --force.
1875 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
1876 if (!o
|| o
->type
!= OBJ_COMMIT
)
1878 old
= (struct commit
*) o
;
1880 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
1881 if (!o
|| o
->type
!= OBJ_COMMIT
)
1883 new = (struct commit
*) o
;
1885 if (parse_commit(new) < 0)
1889 commit_list_insert(new, &list
);
1891 new = pop_most_recent_commit(&list
, TMP_MARK
);
1892 commit_list_insert(new, &used
);
1898 unmark_and_free(list
, TMP_MARK
);
1899 unmark_and_free(used
, TMP_MARK
);
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(remote
->url
, ref
) != 0) {
1915 "Unable to fetch ref %s from %s\n",
1916 ls
->dentry_name
, remote
->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 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1971 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1972 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1973 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1974 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1975 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1977 if (start_active_slot(slot
)) {
1978 run_active_slot(slot
);
1979 if (results
.curl_result
!= CURLE_OK
) {
1981 "PUT error: curl result=%d, HTTP code=%ld\n",
1982 results
.curl_result
, results
.http_code
);
1986 strbuf_release(&buffer
.buf
);
1989 static int remote_exists(const char *path
)
1991 char *url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
1992 struct active_request_slot
*slot
;
1993 struct slot_results results
;
1996 sprintf(url
, "%s%s", remote
->url
, path
);
1998 slot
= get_active_slot();
1999 slot
->results
= &results
;
2000 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2001 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
2003 if (start_active_slot(slot
)) {
2004 run_active_slot(slot
);
2005 if (results
.http_code
== 404)
2007 else if (results
.curl_result
== CURLE_OK
)
2010 fprintf(stderr
, "HEAD HTTP error %ld\n", results
.http_code
);
2012 fprintf(stderr
, "Unable to start HEAD request\n");
2019 static void fetch_symref(const char *path
, char **symref
, unsigned char *sha1
)
2022 struct strbuf buffer
= STRBUF_INIT
;
2023 struct active_request_slot
*slot
;
2024 struct slot_results results
;
2026 url
= xmalloc(strlen(remote
->url
) + strlen(path
) + 1);
2027 sprintf(url
, "%s%s", remote
->url
, path
);
2029 slot
= get_active_slot();
2030 slot
->results
= &results
;
2031 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
2032 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
2033 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
2034 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2035 if (start_active_slot(slot
)) {
2036 run_active_slot(slot
);
2037 if (results
.curl_result
!= CURLE_OK
) {
2038 die("Couldn't get %s for remote symref\n%s",
2039 url
, curl_errorstr
);
2042 die("Unable to start remote symref request");
2050 if (buffer
.len
== 0)
2053 /* If it's a symref, set the refname; otherwise try for a sha1 */
2054 if (!prefixcmp((char *)buffer
.buf
, "ref: ")) {
2055 *symref
= xmemdupz((char *)buffer
.buf
+ 5, buffer
.len
- 6);
2057 get_sha1_hex(buffer
.buf
, sha1
);
2060 strbuf_release(&buffer
);
2063 static int verify_merge_base(unsigned char *head_sha1
, unsigned char *branch_sha1
)
2065 struct commit
*head
= lookup_commit(head_sha1
);
2066 struct commit
*branch
= lookup_commit(branch_sha1
);
2067 struct commit_list
*merge_bases
= get_merge_bases(head
, branch
, 1);
2069 return (merge_bases
&& !merge_bases
->next
&& merge_bases
->item
== branch
);
2072 static int delete_remote_branch(char *pattern
, int force
)
2074 struct ref
*refs
= remote_refs
;
2075 struct ref
*remote_ref
= NULL
;
2076 unsigned char head_sha1
[20];
2077 char *symref
= NULL
;
2079 int patlen
= strlen(pattern
);
2081 struct active_request_slot
*slot
;
2082 struct slot_results results
;
2085 /* Find the remote branch(es) matching the specified branch name */
2086 for (match
= 0; refs
; refs
= refs
->next
) {
2087 char *name
= refs
->name
;
2088 int namelen
= strlen(name
);
2089 if (namelen
< patlen
||
2090 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
2092 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
2098 return error("No remote branch matches %s", pattern
);
2100 return error("More than one remote branch matches %s",
2104 * Remote HEAD must be a symref (not exactly foolproof; a remote
2105 * symlink to a symref will look like a symref)
2107 fetch_symref("HEAD", &symref
, head_sha1
);
2109 return error("Remote HEAD is not a symref");
2111 /* Remote branch must not be the remote HEAD */
2112 for (i
=0; symref
&& i
<MAXDEPTH
; i
++) {
2113 if (!strcmp(remote_ref
->name
, symref
))
2114 return error("Remote branch %s is the current HEAD",
2116 fetch_symref(symref
, &symref
, head_sha1
);
2119 /* Run extra sanity checks if delete is not forced */
2121 /* Remote HEAD must resolve to a known object */
2123 return error("Remote HEAD symrefs too deep");
2124 if (is_zero_sha1(head_sha1
))
2125 return error("Unable to resolve remote HEAD");
2126 if (!has_sha1_file(head_sha1
))
2127 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1
));
2129 /* Remote branch must resolve to a known object */
2130 if (is_zero_sha1(remote_ref
->old_sha1
))
2131 return error("Unable to resolve remote branch %s",
2133 if (!has_sha1_file(remote_ref
->old_sha1
))
2134 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
));
2136 /* Remote branch must be an ancestor of remote HEAD */
2137 if (!verify_merge_base(head_sha1
, remote_ref
->old_sha1
)) {
2138 return error("The branch '%s' is not an ancestor "
2139 "of your current HEAD.\n"
2140 "If you are sure you want to delete it,"
2141 " run:\n\t'git http-push -D %s %s'",
2142 remote_ref
->name
, remote
->url
, pattern
);
2146 /* Send delete request */
2147 fprintf(stderr
, "Removing remote branch '%s'\n", remote_ref
->name
);
2150 url
= xmalloc(strlen(remote
->url
) + strlen(remote_ref
->name
) + 1);
2151 sprintf(url
, "%s%s", remote
->url
, remote_ref
->name
);
2152 slot
= get_active_slot();
2153 slot
->results
= &results
;
2154 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2155 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
2156 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2157 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_DELETE
);
2158 if (start_active_slot(slot
)) {
2159 run_active_slot(slot
);
2161 if (results
.curl_result
!= CURLE_OK
)
2162 return error("DELETE request failed (%d/%ld)\n",
2163 results
.curl_result
, results
.http_code
);
2166 return error("Unable to start DELETE request");
2172 int main(int argc
, char **argv
)
2174 struct transfer_request
*request
;
2175 struct transfer_request
*next_request
;
2177 char **refspec
= NULL
;
2178 struct remote_lock
*ref_lock
= NULL
;
2179 struct remote_lock
*info_ref_lock
= NULL
;
2180 struct rev_info revs
;
2181 int delete_branch
= 0;
2182 int force_delete
= 0;
2183 int objects_to_send
;
2188 char *rewritten_url
= NULL
;
2190 setup_git_directory();
2192 remote
= xcalloc(sizeof(*remote
), 1);
2195 for (i
= 1; i
< argc
; i
++, argv
++) {
2199 if (!strcmp(arg
, "--all")) {
2200 push_all
= MATCH_REFS_ALL
;
2203 if (!strcmp(arg
, "--force")) {
2207 if (!strcmp(arg
, "--dry-run")) {
2211 if (!strcmp(arg
, "--verbose")) {
2215 if (!strcmp(arg
, "-d")) {
2219 if (!strcmp(arg
, "-D")) {
2226 char *path
= strstr(arg
, "//");
2228 remote
->path_len
= strlen(arg
);
2230 remote
->path
= strchr(path
+2, '/');
2232 remote
->path_len
= strlen(remote
->path
);
2237 nr_refspec
= argc
- i
;
2241 #ifndef USE_CURL_MULTI
2242 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2246 usage(http_push_usage
);
2248 if (delete_branch
&& nr_refspec
!= 1)
2249 die("You must specify only one branch name when deleting a remote branch");
2251 memset(remote_dir_exists
, -1, 256);
2255 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
2257 if (remote
->url
&& remote
->url
[strlen(remote
->url
)-1] != '/') {
2258 rewritten_url
= xmalloc(strlen(remote
->url
)+2);
2259 strcpy(rewritten_url
, remote
->url
);
2260 strcat(rewritten_url
, "/");
2261 remote
->path
= rewritten_url
+ (remote
->path
- remote
->url
);
2263 remote
->url
= rewritten_url
;
2266 /* Verify DAV compliance/lock support */
2267 if (!locking_available()) {
2272 signal(SIGINT
, remove_locks_on_signal
);
2273 signal(SIGHUP
, remove_locks_on_signal
);
2274 signal(SIGQUIT
, remove_locks_on_signal
);
2275 signal(SIGTERM
, remove_locks_on_signal
);
2277 /* Check whether the remote has server info files */
2278 remote
->can_update_info_refs
= 0;
2279 remote
->has_info_refs
= remote_exists("info/refs");
2280 remote
->has_info_packs
= remote_exists("objects/info/packs");
2281 if (remote
->has_info_refs
) {
2282 info_ref_lock
= lock_remote("info/refs", LOCK_TIME
);
2284 remote
->can_update_info_refs
= 1;
2286 fprintf(stderr
, "Error: cannot lock existing info/refs\n");
2291 if (remote
->has_info_packs
)
2294 /* Get a list of all local and remote heads to validate refspecs */
2296 fprintf(stderr
, "Fetching remote heads...\n");
2297 get_dav_remote_heads();
2299 /* Remove a remote branch if -d or -D was specified */
2300 if (delete_branch
) {
2301 if (delete_remote_branch(refspec
[0], force_delete
) == -1)
2302 fprintf(stderr
, "Unable to delete remote branch %s\n",
2309 remote_tail
= &remote_refs
;
2310 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
2311 nr_refspec
, (const char **) refspec
, push_all
)) {
2316 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
2322 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2323 char old_hex
[60], *new_hex
;
2324 const char *commit_argv
[4];
2326 char *new_sha1_hex
, *old_sha1_hex
;
2331 if (is_zero_sha1(ref
->peer_ref
->new_sha1
)) {
2332 if (delete_remote_branch(ref
->name
, 1) == -1) {
2333 error("Could not remove %s", ref
->name
);
2340 if (!hashcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
)) {
2341 if (push_verbosely
|| 1)
2342 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
2347 !is_zero_sha1(ref
->old_sha1
) &&
2349 if (!has_sha1_file(ref
->old_sha1
) ||
2350 !ref_newer(ref
->peer_ref
->new_sha1
,
2353 * We do not have the remote ref, or
2354 * we know that the remote ref is not
2355 * an ancestor of what we are trying to
2356 * push. Either way this can be losing
2357 * commits at the remote end and likely
2358 * we were not up to date to begin with.
2360 error("remote '%s' is not an ancestor of\n"
2362 "Maybe you are not up-to-date and "
2363 "need to pull first?",
2365 ref
->peer_ref
->name
);
2370 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
2372 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
2373 new_hex
= sha1_to_hex(ref
->new_sha1
);
2375 fprintf(stderr
, "updating '%s'", ref
->name
);
2376 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
2377 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
2378 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
2382 /* Lock remote branch ref */
2383 ref_lock
= lock_remote(ref
->name
, LOCK_TIME
);
2384 if (ref_lock
== NULL
) {
2385 fprintf(stderr
, "Unable to lock remote branch %s\n",
2391 /* Set up revision info for this refspec */
2393 new_sha1_hex
= xstrdup(sha1_to_hex(ref
->new_sha1
));
2394 old_sha1_hex
= NULL
;
2395 commit_argv
[1] = "--objects";
2396 commit_argv
[2] = new_sha1_hex
;
2397 if (!push_all
&& !is_zero_sha1(ref
->old_sha1
)) {
2398 old_sha1_hex
= xmalloc(42);
2399 sprintf(old_sha1_hex
, "^%s",
2400 sha1_to_hex(ref
->old_sha1
));
2401 commit_argv
[3] = old_sha1_hex
;
2404 init_revisions(&revs
, setup_git_directory());
2405 setup_revisions(commit_argc
, commit_argv
, &revs
, NULL
);
2406 revs
.edge_hint
= 0; /* just in case */
2410 commit_argv
[1] = NULL
;
2413 /* Generate a list of objects that need to be pushed */
2415 if (prepare_revision_walk(&revs
))
2416 die("revision walk setup failed");
2417 mark_edges_uninteresting(revs
.commits
, &revs
, NULL
);
2418 objects_to_send
= get_delta(&revs
, ref_lock
);
2419 finish_all_active_slots();
2421 /* Push missing objects to remote, this would be a
2422 convenient time to pack them first if appropriate. */
2424 if (objects_to_send
)
2425 fprintf(stderr
, " sending %d objects\n",
2427 #ifdef USE_CURL_MULTI
2428 fill_active_slots();
2429 add_fill_function(NULL
, fill_active_slot
);
2432 finish_all_active_slots();
2433 #ifdef USE_CURL_MULTI
2434 fill_active_slots();
2436 } while (request_queue_head
&& !aborted
);
2438 /* Update the remote branch if all went well */
2439 if (aborted
|| !update_remote(ref
->new_sha1
, ref_lock
))
2443 fprintf(stderr
, " done\n");
2444 unlock_remote(ref_lock
);
2448 /* Update remote server info if appropriate */
2449 if (remote
->has_info_refs
&& new_refs
) {
2450 if (info_ref_lock
&& remote
->can_update_info_refs
) {
2451 fprintf(stderr
, "Updating remote server info\n");
2453 update_remote_info_refs(info_ref_lock
);
2455 fprintf(stderr
, "Unable to update server info\n");
2460 free(rewritten_url
);
2462 unlock_remote(info_ref_lock
);
2465 curl_slist_free_all(no_pragma_header
);
2469 request
= request_queue_head
;
2470 while (request
!= NULL
) {
2471 next_request
= request
->next
;
2472 release_request(request
);
2473 request
= next_request
;