11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
18 static const char upload_pack_usage
[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
20 /* bits #0..7 in revision.h, #8..10 in commit.c */
21 #define THEY_HAVE (1u << 11)
22 #define OUR_REF (1u << 12)
23 #define WANTED (1u << 13)
24 #define COMMON_KNOWN (1u << 14)
25 #define REACHABLE (1u << 15)
27 #define SHALLOW (1u << 16)
28 #define NOT_SHALLOW (1u << 17)
29 #define CLIENT_SHALLOW (1u << 18)
30 #define HIDDEN_REF (1u << 19)
32 static unsigned long oldest_have
;
36 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
37 static int no_progress
, daemon_mode
;
38 static int allow_tip_sha1_in_want
;
39 static int shallow_nr
;
40 static struct object_array have_obj
;
41 static struct object_array want_obj
;
42 static struct object_array extra_edge_obj
;
43 static unsigned int timeout
;
44 static int keepalive
= 5;
46 * otherwise maximum packet size (up to 65520 bytes).
48 static int use_sideband
;
49 static int advertise_refs
;
50 static int stateless_rpc
;
52 static void reset_timeout(void)
57 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
60 return send_sideband(1, fd
, data
, sz
, use_sideband
);
65 /* XXX: are we happy to lose stuff here? */
69 write_or_die(fd
, data
, sz
);
73 static void create_pack_file(void)
75 struct child_process pack_objects
;
76 char data
[8193], progress
[128];
77 char abort_msg
[] = "aborting due to possible repository "
78 "corruption on the remote side.";
84 const char *shallow_file
= NULL
;
87 shallow_file
= setup_temporary_shallow(NULL
);
88 argv
[arg
++] = "--shallow-file";
89 argv
[arg
++] = shallow_file
;
91 argv
[arg
++] = "pack-objects";
92 argv
[arg
++] = "--revs";
94 argv
[arg
++] = "--thin";
96 argv
[arg
++] = "--stdout";
98 argv
[arg
++] = "--progress";
100 argv
[arg
++] = "--delta-base-offset";
102 argv
[arg
++] = "--include-tag";
105 memset(&pack_objects
, 0, sizeof(pack_objects
));
106 pack_objects
.in
= -1;
107 pack_objects
.out
= -1;
108 pack_objects
.err
= -1;
109 pack_objects
.git_cmd
= 1;
110 pack_objects
.argv
= argv
;
112 if (start_command(&pack_objects
))
113 die("git upload-pack: unable to fork git-pack-objects");
115 pipe_fd
= xfdopen(pack_objects
.in
, "w");
117 for (i
= 0; i
< want_obj
.nr
; i
++)
118 fprintf(pipe_fd
, "%s\n",
119 sha1_to_hex(want_obj
.objects
[i
].item
->sha1
));
120 fprintf(pipe_fd
, "--not\n");
121 for (i
= 0; i
< have_obj
.nr
; i
++)
122 fprintf(pipe_fd
, "%s\n",
123 sha1_to_hex(have_obj
.objects
[i
].item
->sha1
));
124 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
125 fprintf(pipe_fd
, "%s\n",
126 sha1_to_hex(extra_edge_obj
.objects
[i
].item
->sha1
));
127 fprintf(pipe_fd
, "\n");
131 /* We read from pack_objects.err to capture stderr output for
132 * progress bar, and pack_objects.out to capture the pack data.
136 struct pollfd pfd
[2];
137 int pe
, pu
, pollsize
;
145 if (0 <= pack_objects
.out
) {
146 pfd
[pollsize
].fd
= pack_objects
.out
;
147 pfd
[pollsize
].events
= POLLIN
;
151 if (0 <= pack_objects
.err
) {
152 pfd
[pollsize
].fd
= pack_objects
.err
;
153 pfd
[pollsize
].events
= POLLIN
;
161 ret
= poll(pfd
, pollsize
, 1000 * keepalive
);
163 if (errno
!= EINTR
) {
164 error("poll failed, resuming: %s",
170 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
171 /* Status ready; we ship that in the side-band
172 * or dump to the standard error.
174 sz
= xread(pack_objects
.err
, progress
,
177 send_client_data(2, progress
, sz
);
179 close(pack_objects
.err
);
180 pack_objects
.err
= -1;
184 /* give priority to status messages */
187 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
188 /* Data ready; we keep the last byte to ourselves
189 * in case we detect broken rev-list, so that we
190 * can leave the stream corrupted. This is
191 * unfortunate -- unpack-objects would happily
192 * accept a valid packdata with trailing garbage,
193 * so appending garbage after we pass all the
194 * pack data is not good enough to signal
195 * breakage to downstream.
203 sz
= xread(pack_objects
.out
, cp
,
204 sizeof(data
) - outsz
);
208 close(pack_objects
.out
);
209 pack_objects
.out
= -1;
215 buffered
= data
[sz
-1] & 0xFF;
220 sz
= send_client_data(1, data
, sz
);
226 * We hit the keepalive timeout without saying anything; send
227 * an empty message on the data sideband just to let the other
228 * side know we're still working on it, but don't have any data
231 * If we don't have a sideband channel, there's no room in the
232 * protocol to say anything, so those clients are just out of
235 if (!ret
&& use_sideband
) {
236 static const char buf
[] = "0005\1";
237 write_or_die(1, buf
, 5);
241 if (finish_command(&pack_objects
)) {
242 error("git upload-pack: git-pack-objects died with error.");
249 sz
= send_client_data(1, data
, 1);
252 fprintf(stderr
, "flushed.\n");
259 send_client_data(3, abort_msg
, sizeof(abort_msg
));
260 die("git upload-pack: %s", abort_msg
);
263 static int got_sha1(char *hex
, unsigned char *sha1
)
266 int we_knew_they_have
= 0;
268 if (get_sha1_hex(hex
, sha1
))
269 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
270 if (!has_sha1_file(sha1
))
273 o
= parse_object(sha1
);
275 die("oops (%s)", sha1_to_hex(sha1
));
276 if (o
->type
== OBJ_COMMIT
) {
277 struct commit_list
*parents
;
278 struct commit
*commit
= (struct commit
*)o
;
279 if (o
->flags
& THEY_HAVE
)
280 we_knew_they_have
= 1;
282 o
->flags
|= THEY_HAVE
;
283 if (!oldest_have
|| (commit
->date
< oldest_have
))
284 oldest_have
= commit
->date
;
285 for (parents
= commit
->parents
;
287 parents
= parents
->next
)
288 parents
->item
->object
.flags
|= THEY_HAVE
;
290 if (!we_knew_they_have
) {
291 add_object_array(o
, NULL
, &have_obj
);
297 static int reachable(struct commit
*want
)
299 struct commit_list
*work
= NULL
;
301 commit_list_insert_by_date(want
, &work
);
303 struct commit_list
*list
= work
->next
;
304 struct commit
*commit
= work
->item
;
308 if (commit
->object
.flags
& THEY_HAVE
) {
309 want
->object
.flags
|= COMMON_KNOWN
;
312 if (!commit
->object
.parsed
)
313 parse_object(commit
->object
.sha1
);
314 if (commit
->object
.flags
& REACHABLE
)
316 commit
->object
.flags
|= REACHABLE
;
317 if (commit
->date
< oldest_have
)
319 for (list
= commit
->parents
; list
; list
= list
->next
) {
320 struct commit
*parent
= list
->item
;
321 if (!(parent
->object
.flags
& REACHABLE
))
322 commit_list_insert_by_date(parent
, &work
);
325 want
->object
.flags
|= REACHABLE
;
326 clear_commit_marks(want
, REACHABLE
);
327 free_commit_list(work
);
328 return (want
->object
.flags
& COMMON_KNOWN
);
331 static int ok_to_give_up(void)
338 for (i
= 0; i
< want_obj
.nr
; i
++) {
339 struct object
*want
= want_obj
.objects
[i
].item
;
341 if (want
->flags
& COMMON_KNOWN
)
343 want
= deref_tag(want
, "a want line", 0);
344 if (!want
|| want
->type
!= OBJ_COMMIT
) {
345 /* no way to tell if this is reachable by
346 * looking at the ancestry chain alone, so
347 * leave a note to ourselves not to worry about
348 * this object anymore.
350 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
353 if (!reachable((struct commit
*)want
))
359 static int get_common_commits(void)
361 unsigned char sha1
[20];
367 save_commit_buffer
= 0;
370 char *line
= packet_read_line(0, NULL
);
374 if (multi_ack
== 2 && got_common
375 && !got_other
&& ok_to_give_up()) {
377 packet_write(1, "ACK %s ready\n", last_hex
);
379 if (have_obj
.nr
== 0 || multi_ack
)
380 packet_write(1, "NAK\n");
382 if (no_done
&& sent_ready
) {
383 packet_write(1, "ACK %s\n", last_hex
);
392 if (starts_with(line
, "have ")) {
393 switch (got_sha1(line
+5, sha1
)) {
394 case -1: /* they have what we do not */
396 if (multi_ack
&& ok_to_give_up()) {
397 const char *hex
= sha1_to_hex(sha1
);
398 if (multi_ack
== 2) {
400 packet_write(1, "ACK %s ready\n", hex
);
402 packet_write(1, "ACK %s continue\n", hex
);
407 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
409 packet_write(1, "ACK %s common\n", last_hex
);
411 packet_write(1, "ACK %s continue\n", last_hex
);
412 else if (have_obj
.nr
== 1)
413 packet_write(1, "ACK %s\n", last_hex
);
418 if (!strcmp(line
, "done")) {
419 if (have_obj
.nr
> 0) {
421 packet_write(1, "ACK %s\n", last_hex
);
424 packet_write(1, "NAK\n");
427 die("git upload-pack: expected SHA1 list, got '%s'", line
);
431 static int is_our_ref(struct object
*o
)
434 ((allow_tip_sha1_in_want
? HIDDEN_REF
: 0) | OUR_REF
);
437 static void check_non_tip(void)
439 static const char *argv
[] = {
440 "rev-list", "--stdin", NULL
,
442 static struct child_process cmd
;
444 char namebuf
[42]; /* ^ + SHA-1 + LF */
447 /* In the normal in-process case non-tip request can never happen */
457 if (start_command(&cmd
))
461 * If rev-list --stdin encounters an unknown commit, it
462 * terminates, which will cause SIGPIPE in the write loop
465 sigchain_push(SIGPIPE
, SIG_IGN
);
469 for (i
= get_max_object_index(); 0 < i
; ) {
470 o
= get_indexed_object(--i
);
475 memcpy(namebuf
+ 1, sha1_to_hex(o
->sha1
), 40);
476 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
480 for (i
= 0; i
< want_obj
.nr
; i
++) {
481 o
= want_obj
.objects
[i
].item
;
484 memcpy(namebuf
, sha1_to_hex(o
->sha1
), 40);
485 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
490 sigchain_pop(SIGPIPE
);
493 * The commits out of the rev-list are not ancestors of
496 i
= read_in_full(cmd
.out
, namebuf
, 1);
502 * rev-list may have died by encountering a bad commit
503 * in the history, in which case we do want to bail out
504 * even when it showed no commit.
506 if (finish_command(&cmd
))
509 /* All the non-tip ones are ancestors of what we advertised */
513 /* Pick one of them (we know there at least is one) */
514 for (i
= 0; i
< want_obj
.nr
; i
++) {
515 o
= want_obj
.objects
[i
].item
;
517 die("git upload-pack: not our ref %s",
518 sha1_to_hex(o
->sha1
));
522 static void receive_needs(void)
524 struct object_array shallows
= OBJECT_ARRAY_INIT
;
531 const char *features
;
532 unsigned char sha1_buf
[20];
533 char *line
= packet_read_line(0, NULL
);
538 if (starts_with(line
, "shallow ")) {
539 unsigned char sha1
[20];
540 struct object
*object
;
541 if (get_sha1_hex(line
+ 8, sha1
))
542 die("invalid shallow line: %s", line
);
543 object
= parse_object(sha1
);
546 if (object
->type
!= OBJ_COMMIT
)
547 die("invalid shallow object %s", sha1_to_hex(sha1
));
548 if (!(object
->flags
& CLIENT_SHALLOW
)) {
549 object
->flags
|= CLIENT_SHALLOW
;
550 add_object_array(object
, NULL
, &shallows
);
554 if (starts_with(line
, "deepen ")) {
556 depth
= strtol(line
+ 7, &end
, 0);
557 if (end
== line
+ 7 || depth
<= 0)
558 die("Invalid deepen: %s", line
);
561 if (!starts_with(line
, "want ") ||
562 get_sha1_hex(line
+5, sha1_buf
))
563 die("git upload-pack: protocol error, "
564 "expected to get sha, not '%s'", line
);
566 features
= line
+ 45;
568 if (parse_feature_request(features
, "multi_ack_detailed"))
570 else if (parse_feature_request(features
, "multi_ack"))
572 if (parse_feature_request(features
, "no-done"))
574 if (parse_feature_request(features
, "thin-pack"))
576 if (parse_feature_request(features
, "ofs-delta"))
578 if (parse_feature_request(features
, "side-band-64k"))
579 use_sideband
= LARGE_PACKET_MAX
;
580 else if (parse_feature_request(features
, "side-band"))
581 use_sideband
= DEFAULT_PACKET_MAX
;
582 if (parse_feature_request(features
, "no-progress"))
584 if (parse_feature_request(features
, "include-tag"))
587 o
= parse_object(sha1_buf
);
589 die("git upload-pack: not our ref %s",
590 sha1_to_hex(sha1_buf
));
591 if (!(o
->flags
& WANTED
)) {
595 add_object_array(o
, NULL
, &want_obj
);
600 * We have sent all our refs already, and the other end
601 * should have chosen out of them. When we are operating
602 * in the stateless RPC mode, however, their choice may
603 * have been based on the set of older refs advertised
604 * by another process that handled the initial request.
609 if (!use_sideband
&& daemon_mode
)
612 if (depth
== 0 && shallows
.nr
== 0)
615 struct commit_list
*result
= NULL
, *backup
= NULL
;
617 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow())
618 for (i
= 0; i
< shallows
.nr
; i
++) {
619 struct object
*object
= shallows
.objects
[i
].item
;
620 object
->flags
|= NOT_SHALLOW
;
624 get_shallow_commits(&want_obj
, depth
,
625 SHALLOW
, NOT_SHALLOW
);
627 struct object
*object
= &result
->item
->object
;
628 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
629 packet_write(1, "shallow %s",
630 sha1_to_hex(object
->sha1
));
631 register_shallow(object
->sha1
);
634 result
= result
->next
;
636 free_commit_list(backup
);
637 for (i
= 0; i
< shallows
.nr
; i
++) {
638 struct object
*object
= shallows
.objects
[i
].item
;
639 if (object
->flags
& NOT_SHALLOW
) {
640 struct commit_list
*parents
;
641 packet_write(1, "unshallow %s",
642 sha1_to_hex(object
->sha1
));
643 object
->flags
&= ~CLIENT_SHALLOW
;
644 /* make sure the real parents are parsed */
645 unregister_shallow(object
->sha1
);
647 parse_commit_or_die((struct commit
*)object
);
648 parents
= ((struct commit
*)object
)->parents
;
650 add_object_array(&parents
->item
->object
,
652 parents
= parents
->next
;
654 add_object_array(object
, NULL
, &extra_edge_obj
);
656 /* make sure commit traversal conforms to client */
657 register_shallow(object
->sha1
);
661 if (shallows
.nr
> 0) {
663 for (i
= 0; i
< shallows
.nr
; i
++)
664 register_shallow(shallows
.objects
[i
].item
->sha1
);
667 shallow_nr
+= shallows
.nr
;
668 free(shallows
.objects
);
671 /* return non-zero if the ref is hidden, otherwise 0 */
672 static int mark_our_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
674 struct object
*o
= lookup_unknown_object(sha1
);
676 if (ref_is_hidden(refname
)) {
677 o
->flags
|= HIDDEN_REF
;
681 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
686 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
688 struct string_list_item
*item
;
692 for_each_string_list_item(item
, symref
)
693 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
696 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
698 static const char *capabilities
= "multi_ack thin-pack side-band"
699 " side-band-64k ofs-delta shallow no-progress"
700 " include-tag multi_ack_detailed";
701 const char *refname_nons
= strip_namespace(refname
);
702 unsigned char peeled
[20];
704 if (mark_our_ref(refname
, sha1
, flag
, NULL
))
708 struct strbuf symref_info
= STRBUF_INIT
;
710 format_symref_info(&symref_info
, cb_data
);
711 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
712 sha1_to_hex(sha1
), refname_nons
,
714 allow_tip_sha1_in_want
? " allow-tip-sha1-in-want" : "",
715 stateless_rpc
? " no-done" : "",
717 git_user_agent_sanitized());
718 strbuf_release(&symref_info
);
720 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname_nons
);
723 if (!peel_ref(refname
, peeled
))
724 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled
), refname_nons
);
728 static int find_symref(const char *refname
, const unsigned char *sha1
, int flag
,
731 const char *symref_target
;
732 struct string_list_item
*item
;
733 unsigned char unused
[20];
735 if ((flag
& REF_ISSYMREF
) == 0)
737 symref_target
= resolve_ref_unsafe(refname
, unused
, 0, &flag
);
738 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
739 die("'%s' is a symref but it is not?", refname
);
740 item
= string_list_append(cb_data
, refname
);
741 item
->util
= xstrdup(symref_target
);
745 static void upload_pack(void)
747 struct string_list symref
= STRING_LIST_INIT_DUP
;
749 head_ref_namespaced(find_symref
, &symref
);
751 if (advertise_refs
|| !stateless_rpc
) {
753 head_ref_namespaced(send_ref
, &symref
);
754 for_each_namespaced_ref(send_ref
, &symref
);
755 advertise_shallow_grafts(1);
758 head_ref_namespaced(mark_our_ref
, NULL
);
759 for_each_namespaced_ref(mark_our_ref
, NULL
);
761 string_list_clear(&symref
, 1);
767 get_common_commits();
772 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
774 if (!strcmp("uploadpack.allowtipsha1inwant", var
))
775 allow_tip_sha1_in_want
= git_config_bool(var
, value
);
776 else if (!strcmp("uploadpack.keepalive", var
)) {
777 keepalive
= git_config_int(var
, value
);
781 return parse_hide_refs_config(var
, value
, "uploadpack");
784 int main(int argc
, char **argv
)
792 packet_trace_identity("upload-pack");
793 git_extract_argv0_path(argv
[0]);
794 read_replace_refs
= 0;
796 for (i
= 1; i
< argc
; i
++) {
801 if (!strcmp(arg
, "--advertise-refs")) {
805 if (!strcmp(arg
, "--stateless-rpc")) {
809 if (!strcmp(arg
, "--strict")) {
813 if (starts_with(arg
, "--timeout=")) {
814 timeout
= atoi(arg
+10);
818 if (!strcmp(arg
, "--")) {
825 usage(upload_pack_usage
);
831 if (!enter_repo(dir
, strict
))
832 die("'%s' does not appear to be a git repository", dir
);
834 git_config(upload_pack_config
, NULL
);