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 /* Remember to update object flag allocation in object.h */
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 int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
76 if (graft
->nr_parent
== -1)
77 fprintf(fp
, "--shallow %s\n", sha1_to_hex(graft
->sha1
));
81 static void create_pack_file(void)
83 struct child_process pack_objects
;
84 char data
[8193], progress
[128];
85 char abort_msg
[] = "aborting due to possible repository "
86 "corruption on the remote side.";
94 argv
[arg
++] = "--shallow-file";
97 argv
[arg
++] = "pack-objects";
98 argv
[arg
++] = "--revs";
100 argv
[arg
++] = "--thin";
102 argv
[arg
++] = "--stdout";
104 argv
[arg
++] = "--progress";
106 argv
[arg
++] = "--delta-base-offset";
108 argv
[arg
++] = "--include-tag";
111 memset(&pack_objects
, 0, sizeof(pack_objects
));
112 pack_objects
.in
= -1;
113 pack_objects
.out
= -1;
114 pack_objects
.err
= -1;
115 pack_objects
.git_cmd
= 1;
116 pack_objects
.argv
= argv
;
118 if (start_command(&pack_objects
))
119 die("git upload-pack: unable to fork git-pack-objects");
121 pipe_fd
= xfdopen(pack_objects
.in
, "w");
124 for_each_commit_graft(write_one_shallow
, pipe_fd
);
126 for (i
= 0; i
< want_obj
.nr
; i
++)
127 fprintf(pipe_fd
, "%s\n",
128 sha1_to_hex(want_obj
.objects
[i
].item
->sha1
));
129 fprintf(pipe_fd
, "--not\n");
130 for (i
= 0; i
< have_obj
.nr
; i
++)
131 fprintf(pipe_fd
, "%s\n",
132 sha1_to_hex(have_obj
.objects
[i
].item
->sha1
));
133 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
134 fprintf(pipe_fd
, "%s\n",
135 sha1_to_hex(extra_edge_obj
.objects
[i
].item
->sha1
));
136 fprintf(pipe_fd
, "\n");
140 /* We read from pack_objects.err to capture stderr output for
141 * progress bar, and pack_objects.out to capture the pack data.
145 struct pollfd pfd
[2];
146 int pe
, pu
, pollsize
;
154 if (0 <= pack_objects
.out
) {
155 pfd
[pollsize
].fd
= pack_objects
.out
;
156 pfd
[pollsize
].events
= POLLIN
;
160 if (0 <= pack_objects
.err
) {
161 pfd
[pollsize
].fd
= pack_objects
.err
;
162 pfd
[pollsize
].events
= POLLIN
;
170 ret
= poll(pfd
, pollsize
,
171 keepalive
< 0 ? -1 : 1000 * keepalive
);
174 if (errno
!= EINTR
) {
175 error("poll failed, resuming: %s",
181 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
182 /* Status ready; we ship that in the side-band
183 * or dump to the standard error.
185 sz
= xread(pack_objects
.err
, progress
,
188 send_client_data(2, progress
, sz
);
190 close(pack_objects
.err
);
191 pack_objects
.err
= -1;
195 /* give priority to status messages */
198 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
199 /* Data ready; we keep the last byte to ourselves
200 * in case we detect broken rev-list, so that we
201 * can leave the stream corrupted. This is
202 * unfortunate -- unpack-objects would happily
203 * accept a valid packdata with trailing garbage,
204 * so appending garbage after we pass all the
205 * pack data is not good enough to signal
206 * breakage to downstream.
214 sz
= xread(pack_objects
.out
, cp
,
215 sizeof(data
) - outsz
);
219 close(pack_objects
.out
);
220 pack_objects
.out
= -1;
226 buffered
= data
[sz
-1] & 0xFF;
231 sz
= send_client_data(1, data
, sz
);
237 * We hit the keepalive timeout without saying anything; send
238 * an empty message on the data sideband just to let the other
239 * side know we're still working on it, but don't have any data
242 * If we don't have a sideband channel, there's no room in the
243 * protocol to say anything, so those clients are just out of
246 if (!ret
&& use_sideband
) {
247 static const char buf
[] = "0005\1";
248 write_or_die(1, buf
, 5);
252 if (finish_command(&pack_objects
)) {
253 error("git upload-pack: git-pack-objects died with error.");
260 sz
= send_client_data(1, data
, 1);
263 fprintf(stderr
, "flushed.\n");
270 send_client_data(3, abort_msg
, sizeof(abort_msg
));
271 die("git upload-pack: %s", abort_msg
);
274 static int got_sha1(char *hex
, unsigned char *sha1
)
277 int we_knew_they_have
= 0;
279 if (get_sha1_hex(hex
, sha1
))
280 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
281 if (!has_sha1_file(sha1
))
284 o
= parse_object(sha1
);
286 die("oops (%s)", sha1_to_hex(sha1
));
287 if (o
->type
== OBJ_COMMIT
) {
288 struct commit_list
*parents
;
289 struct commit
*commit
= (struct commit
*)o
;
290 if (o
->flags
& THEY_HAVE
)
291 we_knew_they_have
= 1;
293 o
->flags
|= THEY_HAVE
;
294 if (!oldest_have
|| (commit
->date
< oldest_have
))
295 oldest_have
= commit
->date
;
296 for (parents
= commit
->parents
;
298 parents
= parents
->next
)
299 parents
->item
->object
.flags
|= THEY_HAVE
;
301 if (!we_knew_they_have
) {
302 add_object_array(o
, NULL
, &have_obj
);
308 static int reachable(struct commit
*want
)
310 struct commit_list
*work
= NULL
;
312 commit_list_insert_by_date(want
, &work
);
314 struct commit_list
*list
= work
->next
;
315 struct commit
*commit
= work
->item
;
319 if (commit
->object
.flags
& THEY_HAVE
) {
320 want
->object
.flags
|= COMMON_KNOWN
;
323 if (!commit
->object
.parsed
)
324 parse_object(commit
->object
.sha1
);
325 if (commit
->object
.flags
& REACHABLE
)
327 commit
->object
.flags
|= REACHABLE
;
328 if (commit
->date
< oldest_have
)
330 for (list
= commit
->parents
; list
; list
= list
->next
) {
331 struct commit
*parent
= list
->item
;
332 if (!(parent
->object
.flags
& REACHABLE
))
333 commit_list_insert_by_date(parent
, &work
);
336 want
->object
.flags
|= REACHABLE
;
337 clear_commit_marks(want
, REACHABLE
);
338 free_commit_list(work
);
339 return (want
->object
.flags
& COMMON_KNOWN
);
342 static int ok_to_give_up(void)
349 for (i
= 0; i
< want_obj
.nr
; i
++) {
350 struct object
*want
= want_obj
.objects
[i
].item
;
352 if (want
->flags
& COMMON_KNOWN
)
354 want
= deref_tag(want
, "a want line", 0);
355 if (!want
|| want
->type
!= OBJ_COMMIT
) {
356 /* no way to tell if this is reachable by
357 * looking at the ancestry chain alone, so
358 * leave a note to ourselves not to worry about
359 * this object anymore.
361 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
364 if (!reachable((struct commit
*)want
))
370 static int get_common_commits(void)
372 unsigned char sha1
[20];
378 save_commit_buffer
= 0;
381 char *line
= packet_read_line(0, NULL
);
385 if (multi_ack
== 2 && got_common
386 && !got_other
&& ok_to_give_up()) {
388 packet_write(1, "ACK %s ready\n", last_hex
);
390 if (have_obj
.nr
== 0 || multi_ack
)
391 packet_write(1, "NAK\n");
393 if (no_done
&& sent_ready
) {
394 packet_write(1, "ACK %s\n", last_hex
);
403 if (starts_with(line
, "have ")) {
404 switch (got_sha1(line
+5, sha1
)) {
405 case -1: /* they have what we do not */
407 if (multi_ack
&& ok_to_give_up()) {
408 const char *hex
= sha1_to_hex(sha1
);
409 if (multi_ack
== 2) {
411 packet_write(1, "ACK %s ready\n", hex
);
413 packet_write(1, "ACK %s continue\n", hex
);
418 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
420 packet_write(1, "ACK %s common\n", last_hex
);
422 packet_write(1, "ACK %s continue\n", last_hex
);
423 else if (have_obj
.nr
== 1)
424 packet_write(1, "ACK %s\n", last_hex
);
429 if (!strcmp(line
, "done")) {
430 if (have_obj
.nr
> 0) {
432 packet_write(1, "ACK %s\n", last_hex
);
435 packet_write(1, "NAK\n");
438 die("git upload-pack: expected SHA1 list, got '%s'", line
);
442 static int is_our_ref(struct object
*o
)
445 ((allow_tip_sha1_in_want
? HIDDEN_REF
: 0) | OUR_REF
);
448 static void check_non_tip(void)
450 static const char *argv
[] = {
451 "rev-list", "--stdin", NULL
,
453 static struct child_process cmd
;
455 char namebuf
[42]; /* ^ + SHA-1 + LF */
458 /* In the normal in-process case non-tip request can never happen */
468 if (start_command(&cmd
))
472 * If rev-list --stdin encounters an unknown commit, it
473 * terminates, which will cause SIGPIPE in the write loop
476 sigchain_push(SIGPIPE
, SIG_IGN
);
480 for (i
= get_max_object_index(); 0 < i
; ) {
481 o
= get_indexed_object(--i
);
486 memcpy(namebuf
+ 1, sha1_to_hex(o
->sha1
), 40);
487 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
491 for (i
= 0; i
< want_obj
.nr
; i
++) {
492 o
= want_obj
.objects
[i
].item
;
495 memcpy(namebuf
, sha1_to_hex(o
->sha1
), 40);
496 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
501 sigchain_pop(SIGPIPE
);
504 * The commits out of the rev-list are not ancestors of
507 i
= read_in_full(cmd
.out
, namebuf
, 1);
513 * rev-list may have died by encountering a bad commit
514 * in the history, in which case we do want to bail out
515 * even when it showed no commit.
517 if (finish_command(&cmd
))
520 /* All the non-tip ones are ancestors of what we advertised */
524 /* Pick one of them (we know there at least is one) */
525 for (i
= 0; i
< want_obj
.nr
; i
++) {
526 o
= want_obj
.objects
[i
].item
;
528 die("git upload-pack: not our ref %s",
529 sha1_to_hex(o
->sha1
));
533 static void receive_needs(void)
535 struct object_array shallows
= OBJECT_ARRAY_INIT
;
542 const char *features
;
543 unsigned char sha1_buf
[20];
544 char *line
= packet_read_line(0, NULL
);
549 if (starts_with(line
, "shallow ")) {
550 unsigned char sha1
[20];
551 struct object
*object
;
552 if (get_sha1_hex(line
+ 8, sha1
))
553 die("invalid shallow line: %s", line
);
554 object
= parse_object(sha1
);
557 if (object
->type
!= OBJ_COMMIT
)
558 die("invalid shallow object %s", sha1_to_hex(sha1
));
559 if (!(object
->flags
& CLIENT_SHALLOW
)) {
560 object
->flags
|= CLIENT_SHALLOW
;
561 add_object_array(object
, NULL
, &shallows
);
565 if (starts_with(line
, "deepen ")) {
567 depth
= strtol(line
+ 7, &end
, 0);
568 if (end
== line
+ 7 || depth
<= 0)
569 die("Invalid deepen: %s", line
);
572 if (!starts_with(line
, "want ") ||
573 get_sha1_hex(line
+5, sha1_buf
))
574 die("git upload-pack: protocol error, "
575 "expected to get sha, not '%s'", line
);
577 features
= line
+ 45;
579 if (parse_feature_request(features
, "multi_ack_detailed"))
581 else if (parse_feature_request(features
, "multi_ack"))
583 if (parse_feature_request(features
, "no-done"))
585 if (parse_feature_request(features
, "thin-pack"))
587 if (parse_feature_request(features
, "ofs-delta"))
589 if (parse_feature_request(features
, "side-band-64k"))
590 use_sideband
= LARGE_PACKET_MAX
;
591 else if (parse_feature_request(features
, "side-band"))
592 use_sideband
= DEFAULT_PACKET_MAX
;
593 if (parse_feature_request(features
, "no-progress"))
595 if (parse_feature_request(features
, "include-tag"))
598 o
= parse_object(sha1_buf
);
600 die("git upload-pack: not our ref %s",
601 sha1_to_hex(sha1_buf
));
602 if (!(o
->flags
& WANTED
)) {
606 add_object_array(o
, NULL
, &want_obj
);
611 * We have sent all our refs already, and the other end
612 * should have chosen out of them. When we are operating
613 * in the stateless RPC mode, however, their choice may
614 * have been based on the set of older refs advertised
615 * by another process that handled the initial request.
620 if (!use_sideband
&& daemon_mode
)
623 if (depth
== 0 && shallows
.nr
== 0)
626 struct commit_list
*result
= NULL
, *backup
= NULL
;
628 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow())
629 for (i
= 0; i
< shallows
.nr
; i
++) {
630 struct object
*object
= shallows
.objects
[i
].item
;
631 object
->flags
|= NOT_SHALLOW
;
635 get_shallow_commits(&want_obj
, depth
,
636 SHALLOW
, NOT_SHALLOW
);
638 struct object
*object
= &result
->item
->object
;
639 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
640 packet_write(1, "shallow %s",
641 sha1_to_hex(object
->sha1
));
642 register_shallow(object
->sha1
);
645 result
= result
->next
;
647 free_commit_list(backup
);
648 for (i
= 0; i
< shallows
.nr
; i
++) {
649 struct object
*object
= shallows
.objects
[i
].item
;
650 if (object
->flags
& NOT_SHALLOW
) {
651 struct commit_list
*parents
;
652 packet_write(1, "unshallow %s",
653 sha1_to_hex(object
->sha1
));
654 object
->flags
&= ~CLIENT_SHALLOW
;
655 /* make sure the real parents are parsed */
656 unregister_shallow(object
->sha1
);
658 parse_commit_or_die((struct commit
*)object
);
659 parents
= ((struct commit
*)object
)->parents
;
661 add_object_array(&parents
->item
->object
,
663 parents
= parents
->next
;
665 add_object_array(object
, NULL
, &extra_edge_obj
);
667 /* make sure commit traversal conforms to client */
668 register_shallow(object
->sha1
);
672 if (shallows
.nr
> 0) {
674 for (i
= 0; i
< shallows
.nr
; i
++)
675 register_shallow(shallows
.objects
[i
].item
->sha1
);
678 shallow_nr
+= shallows
.nr
;
679 free(shallows
.objects
);
682 /* return non-zero if the ref is hidden, otherwise 0 */
683 static int mark_our_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
685 struct object
*o
= lookup_unknown_object(sha1
);
687 if (ref_is_hidden(refname
)) {
688 o
->flags
|= HIDDEN_REF
;
692 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
697 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
699 struct string_list_item
*item
;
703 for_each_string_list_item(item
, symref
)
704 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
707 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
709 static const char *capabilities
= "multi_ack thin-pack side-band"
710 " side-band-64k ofs-delta shallow no-progress"
711 " include-tag multi_ack_detailed";
712 const char *refname_nons
= strip_namespace(refname
);
713 unsigned char peeled
[20];
715 if (mark_our_ref(refname
, sha1
, flag
, NULL
))
719 struct strbuf symref_info
= STRBUF_INIT
;
721 format_symref_info(&symref_info
, cb_data
);
722 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
723 sha1_to_hex(sha1
), refname_nons
,
725 allow_tip_sha1_in_want
? " allow-tip-sha1-in-want" : "",
726 stateless_rpc
? " no-done" : "",
728 git_user_agent_sanitized());
729 strbuf_release(&symref_info
);
731 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname_nons
);
734 if (!peel_ref(refname
, peeled
))
735 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled
), refname_nons
);
739 static int find_symref(const char *refname
, const unsigned char *sha1
, int flag
,
742 const char *symref_target
;
743 struct string_list_item
*item
;
744 unsigned char unused
[20];
746 if ((flag
& REF_ISSYMREF
) == 0)
748 symref_target
= resolve_ref_unsafe(refname
, unused
, 0, &flag
);
749 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
750 die("'%s' is a symref but it is not?", refname
);
751 item
= string_list_append(cb_data
, refname
);
752 item
->util
= xstrdup(symref_target
);
756 static void upload_pack(void)
758 struct string_list symref
= STRING_LIST_INIT_DUP
;
760 head_ref_namespaced(find_symref
, &symref
);
762 if (advertise_refs
|| !stateless_rpc
) {
764 head_ref_namespaced(send_ref
, &symref
);
765 for_each_namespaced_ref(send_ref
, &symref
);
766 advertise_shallow_grafts(1);
769 head_ref_namespaced(mark_our_ref
, NULL
);
770 for_each_namespaced_ref(mark_our_ref
, NULL
);
772 string_list_clear(&symref
, 1);
778 get_common_commits();
783 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
785 if (!strcmp("uploadpack.allowtipsha1inwant", var
))
786 allow_tip_sha1_in_want
= git_config_bool(var
, value
);
787 else if (!strcmp("uploadpack.keepalive", var
)) {
788 keepalive
= git_config_int(var
, value
);
792 return parse_hide_refs_config(var
, value
, "uploadpack");
795 int main(int argc
, char **argv
)
803 packet_trace_identity("upload-pack");
804 git_extract_argv0_path(argv
[0]);
805 check_replace_refs
= 0;
807 for (i
= 1; i
< argc
; i
++) {
812 if (!strcmp(arg
, "--advertise-refs")) {
816 if (!strcmp(arg
, "--stateless-rpc")) {
820 if (!strcmp(arg
, "--strict")) {
824 if (starts_with(arg
, "--timeout=")) {
825 timeout
= atoi(arg
+10);
829 if (!strcmp(arg
, "--")) {
836 usage(upload_pack_usage
);
842 if (!enter_repo(dir
, strict
))
843 die("'%s' does not appear to be a git repository", dir
);
845 git_config(upload_pack_config
, NULL
);