6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 static timestamp_t oldest_have;
43 static int deepen_relative;
46 static int use_thin_pack, use_ofs_delta, use_include_tag;
47 static int no_progress, daemon_mode;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53 #define ALLOW_ANY_SHA1 07
54 static unsigned int allow_unadvertised_object_request;
55 static int shallow_nr;
56 static struct object_array extra_edge_obj;
57 static unsigned int timeout;
58 static int keepalive = 5;
60 * otherwise maximum packet size (up to 65520 bytes).
62 static int use_sideband;
63 static int stateless_rpc;
64 static const char *pack_objects_hook;
66 static int filter_capability_requested;
67 static int allow_filter;
68 static int allow_ref_in_want;
69 static struct list_objects_filter_options filter_options;
71 static void reset_timeout(void)
76 static void send_client_data(int fd, const char *data, ssize_t sz)
79 send_sideband(1, fd, data, sz, use_sideband);
86 /* XXX: are we happy to lose stuff here? */
90 write_or_die(fd, data, sz);
93 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
96 if (graft->nr_parent == -1)
97 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
101 static void create_pack_file(const struct object_array *have_obj,
102 const struct object_array *want_obj)
104 struct child_process pack_objects = CHILD_PROCESS_INIT;
105 char data[8193], progress[128];
106 char abort_msg[] = "aborting due to possible repository "
107 "corruption on the remote side.";
113 if (!pack_objects_hook)
114 pack_objects.git_cmd = 1;
116 argv_array_push(&pack_objects.args, pack_objects_hook);
117 argv_array_push(&pack_objects.args, "git");
118 pack_objects.use_shell = 1;
122 argv_array_push(&pack_objects.args, "--shallow-file");
123 argv_array_push(&pack_objects.args, "");
125 argv_array_push(&pack_objects.args, "pack-objects");
126 argv_array_push(&pack_objects.args, "--revs");
128 argv_array_push(&pack_objects.args, "--thin");
130 argv_array_push(&pack_objects.args, "--stdout");
132 argv_array_push(&pack_objects.args, "--shallow");
134 argv_array_push(&pack_objects.args, "--progress");
136 argv_array_push(&pack_objects.args, "--delta-base-offset");
138 argv_array_push(&pack_objects.args, "--include-tag");
139 if (filter_options.filter_spec) {
140 if (pack_objects.use_shell) {
141 struct strbuf buf = STRBUF_INIT;
142 sq_quote_buf(&buf, filter_options.filter_spec);
143 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
144 strbuf_release(&buf);
146 argv_array_pushf(&pack_objects.args, "--filter=%s",
147 filter_options.filter_spec);
151 pack_objects.in = -1;
152 pack_objects.out = -1;
153 pack_objects.err = -1;
155 if (start_command(&pack_objects))
156 die("git upload-pack: unable to fork git-pack-objects");
158 pipe_fd = xfdopen(pack_objects.in, "w");
161 for_each_commit_graft(write_one_shallow, pipe_fd);
163 for (i = 0; i < want_obj->nr; i++)
164 fprintf(pipe_fd, "%s\n",
165 oid_to_hex(&want_obj->objects[i].item->oid));
166 fprintf(pipe_fd, "--not\n");
167 for (i = 0; i < have_obj->nr; i++)
168 fprintf(pipe_fd, "%s\n",
169 oid_to_hex(&have_obj->objects[i].item->oid));
170 for (i = 0; i < extra_edge_obj.nr; i++)
171 fprintf(pipe_fd, "%s\n",
172 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
173 fprintf(pipe_fd, "\n");
177 /* We read from pack_objects.err to capture stderr output for
178 * progress bar, and pack_objects.out to capture the pack data.
182 struct pollfd pfd[2];
183 int pe, pu, pollsize;
191 if (0 <= pack_objects.out) {
192 pfd[pollsize].fd = pack_objects.out;
193 pfd[pollsize].events = POLLIN;
197 if (0 <= pack_objects.err) {
198 pfd[pollsize].fd = pack_objects.err;
199 pfd[pollsize].events = POLLIN;
207 ret = poll(pfd, pollsize,
208 keepalive < 0 ? -1 : 1000 * keepalive);
211 if (errno != EINTR) {
212 error_errno("poll failed, resuming");
217 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
218 /* Status ready; we ship that in the side-band
219 * or dump to the standard error.
221 sz = xread(pack_objects.err, progress,
224 send_client_data(2, progress, sz);
226 close(pack_objects.err);
227 pack_objects.err = -1;
231 /* give priority to status messages */
234 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
235 /* Data ready; we keep the last byte to ourselves
236 * in case we detect broken rev-list, so that we
237 * can leave the stream corrupted. This is
238 * unfortunate -- unpack-objects would happily
239 * accept a valid packdata with trailing garbage,
240 * so appending garbage after we pass all the
241 * pack data is not good enough to signal
242 * breakage to downstream.
250 sz = xread(pack_objects.out, cp,
251 sizeof(data) - outsz);
255 close(pack_objects.out);
256 pack_objects.out = -1;
262 buffered = data[sz-1] & 0xFF;
267 send_client_data(1, data, sz);
271 * We hit the keepalive timeout without saying anything; send
272 * an empty message on the data sideband just to let the other
273 * side know we're still working on it, but don't have any data
276 * If we don't have a sideband channel, there's no room in the
277 * protocol to say anything, so those clients are just out of
280 if (!ret && use_sideband) {
281 static const char buf[] = "0005\1";
282 write_or_die(1, buf, 5);
286 if (finish_command(&pack_objects)) {
287 error("git upload-pack: git-pack-objects died with error.");
294 send_client_data(1, data, 1);
295 fprintf(stderr, "flushed.\n");
302 send_client_data(3, abort_msg, sizeof(abort_msg));
303 die("git upload-pack: %s", abort_msg);
306 static int got_oid(const char *hex, struct object_id *oid,
307 struct object_array *have_obj)
310 int we_knew_they_have = 0;
312 if (get_oid_hex(hex, oid))
313 die("git upload-pack: expected SHA1 object, got '%s'", hex);
314 if (!has_object_file(oid))
317 o = parse_object(the_repository, oid);
319 die("oops (%s)", oid_to_hex(oid));
320 if (o->type == OBJ_COMMIT) {
321 struct commit_list *parents;
322 struct commit *commit = (struct commit *)o;
323 if (o->flags & THEY_HAVE)
324 we_knew_they_have = 1;
326 o->flags |= THEY_HAVE;
327 if (!oldest_have || (commit->date < oldest_have))
328 oldest_have = commit->date;
329 for (parents = commit->parents;
331 parents = parents->next)
332 parents->item->object.flags |= THEY_HAVE;
334 if (!we_knew_they_have) {
335 add_object_array(o, NULL, have_obj);
341 static int ok_to_give_up(const struct object_array *have_obj,
342 struct object_array *want_obj)
344 uint32_t min_generation = GENERATION_NUMBER_ZERO;
349 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
350 COMMON_KNOWN, oldest_have,
354 static int get_common_commits(struct object_array *have_obj,
355 struct object_array *want_obj)
357 struct object_id oid;
358 char last_hex[GIT_MAX_HEXSZ + 1];
363 save_commit_buffer = 0;
366 char *line = packet_read_line(0, NULL);
372 if (multi_ack == 2 && got_common
373 && !got_other && ok_to_give_up(have_obj, want_obj)) {
375 packet_write_fmt(1, "ACK %s ready\n", last_hex);
377 if (have_obj->nr == 0 || multi_ack)
378 packet_write_fmt(1, "NAK\n");
380 if (no_done && sent_ready) {
381 packet_write_fmt(1, "ACK %s\n", last_hex);
390 if (skip_prefix(line, "have ", &arg)) {
391 switch (got_oid(arg, &oid, have_obj)) {
392 case -1: /* they have what we do not */
394 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
395 const char *hex = oid_to_hex(&oid);
396 if (multi_ack == 2) {
398 packet_write_fmt(1, "ACK %s ready\n", hex);
400 packet_write_fmt(1, "ACK %s continue\n", hex);
405 oid_to_hex_r(last_hex, &oid);
407 packet_write_fmt(1, "ACK %s common\n", last_hex);
409 packet_write_fmt(1, "ACK %s continue\n", last_hex);
410 else if (have_obj->nr == 1)
411 packet_write_fmt(1, "ACK %s\n", last_hex);
416 if (!strcmp(line, "done")) {
417 if (have_obj->nr > 0) {
419 packet_write_fmt(1, "ACK %s\n", last_hex);
422 packet_write_fmt(1, "NAK\n");
425 die("git upload-pack: expected SHA1 list, got '%s'", line);
429 static int is_our_ref(struct object *o)
431 int allow_hidden_ref = (allow_unadvertised_object_request &
432 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
433 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
437 * on successful case, it's up to the caller to close cmd->out
439 static int do_reachable_revlist(struct child_process *cmd,
440 struct object_array *src,
441 struct object_array *reachable)
443 static const char *argv[] = {
444 "rev-list", "--stdin", NULL,
447 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
457 * If the next rev-list --stdin encounters an unknown commit,
458 * it terminates, which will cause SIGPIPE in the write loop
461 sigchain_push(SIGPIPE, SIG_IGN);
463 if (start_command(cmd))
467 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
468 for (i = get_max_object_index(); 0 < i; ) {
469 o = get_indexed_object(--i);
472 if (reachable && o->type == OBJ_COMMIT)
473 o->flags &= ~TMP_MARK;
476 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
477 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
480 namebuf[GIT_SHA1_HEXSZ] = '\n';
481 for (i = 0; i < src->nr; i++) {
482 o = src->objects[i].item;
485 add_object_array(o, NULL, reachable);
488 if (reachable && o->type == OBJ_COMMIT)
489 o->flags |= TMP_MARK;
490 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
491 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
496 sigchain_pop(SIGPIPE);
501 sigchain_pop(SIGPIPE);
510 static int get_reachable_list(struct object_array *src,
511 struct object_array *reachable)
513 struct child_process cmd = CHILD_PROCESS_INIT;
516 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
517 const unsigned hexsz = the_hash_algo->hexsz;
519 if (do_reachable_revlist(&cmd, src, reachable) < 0)
522 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
523 struct object_id sha1;
526 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
529 o = lookup_object(the_repository, sha1.hash);
530 if (o && o->type == OBJ_COMMIT) {
531 o->flags &= ~TMP_MARK;
534 for (i = get_max_object_index(); 0 < i; i--) {
535 o = get_indexed_object(i - 1);
536 if (o && o->type == OBJ_COMMIT &&
537 (o->flags & TMP_MARK)) {
538 add_object_array(o, NULL, reachable);
539 o->flags &= ~TMP_MARK;
544 if (finish_command(&cmd))
550 static int has_unreachable(struct object_array *src)
552 struct child_process cmd = CHILD_PROCESS_INIT;
556 if (do_reachable_revlist(&cmd, src, NULL) < 0)
560 * The commits out of the rev-list are not ancestors of
563 i = read_in_full(cmd.out, buf, 1);
570 * rev-list may have died by encountering a bad commit
571 * in the history, in which case we do want to bail out
572 * even when it showed no commit.
574 if (finish_command(&cmd))
577 /* All the non-tip ones are ancestors of what we advertised */
581 sigchain_pop(SIGPIPE);
587 static void check_non_tip(struct object_array *want_obj)
592 * In the normal in-process case without
593 * uploadpack.allowReachableSHA1InWant,
594 * non-tip requests can never happen.
596 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
598 if (!has_unreachable(want_obj))
599 /* All the non-tip ones are ancestors of what we advertised */
603 /* Pick one of them (we know there at least is one) */
604 for (i = 0; i < want_obj->nr; i++) {
605 struct object *o = want_obj->objects[i].item;
607 die("git upload-pack: not our ref %s",
608 oid_to_hex(&o->oid));
612 static void send_shallow(struct commit_list *result)
615 struct object *object = &result->item->object;
616 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
617 packet_write_fmt(1, "shallow %s",
618 oid_to_hex(&object->oid));
619 register_shallow(the_repository, &object->oid);
622 result = result->next;
626 static void send_unshallow(const struct object_array *shallows,
627 struct object_array *want_obj)
631 for (i = 0; i < shallows->nr; i++) {
632 struct object *object = shallows->objects[i].item;
633 if (object->flags & NOT_SHALLOW) {
634 struct commit_list *parents;
635 packet_write_fmt(1, "unshallow %s",
636 oid_to_hex(&object->oid));
637 object->flags &= ~CLIENT_SHALLOW;
639 * We want to _register_ "object" as shallow, but we
640 * also need to traverse object's parents to deepen a
641 * shallow clone. Unregister it for now so we can
642 * parse and add the parents to the want list, then
645 unregister_shallow(&object->oid);
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(the_repository, &object->oid);
661 static void deepen(int depth, int deepen_relative,
662 struct object_array *shallows, struct object_array *want_obj)
664 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
667 for (i = 0; i < shallows->nr; i++) {
668 struct object *object = shallows->objects[i].item;
669 object->flags |= NOT_SHALLOW;
671 } else if (deepen_relative) {
672 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
673 struct commit_list *result;
675 get_reachable_list(shallows, &reachable_shallows);
676 result = get_shallow_commits(&reachable_shallows,
678 SHALLOW, NOT_SHALLOW);
679 send_shallow(result);
680 free_commit_list(result);
681 object_array_clear(&reachable_shallows);
683 struct commit_list *result;
685 result = get_shallow_commits(want_obj, depth,
686 SHALLOW, NOT_SHALLOW);
687 send_shallow(result);
688 free_commit_list(result);
691 send_unshallow(shallows, want_obj);
694 static void deepen_by_rev_list(int ac, const char **av,
695 struct object_array *shallows,
696 struct object_array *want_obj)
698 struct commit_list *result;
700 close_commit_graph(the_repository);
701 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
702 send_shallow(result);
703 free_commit_list(result);
704 send_unshallow(shallows, want_obj);
707 /* Returns 1 if a shallow list is sent or 0 otherwise */
708 static int send_shallow_list(int depth, int deepen_rev_list,
709 timestamp_t deepen_since,
710 struct string_list *deepen_not,
711 struct object_array *shallows,
712 struct object_array *want_obj)
716 if (depth > 0 && deepen_rev_list)
717 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
719 deepen(depth, deepen_relative, shallows, want_obj);
721 } else if (deepen_rev_list) {
722 struct argv_array av = ARGV_ARRAY_INIT;
725 argv_array_push(&av, "rev-list");
727 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
728 if (deepen_not->nr) {
729 argv_array_push(&av, "--not");
730 for (i = 0; i < deepen_not->nr; i++) {
731 struct string_list_item *s = deepen_not->items + i;
732 argv_array_push(&av, s->string);
734 argv_array_push(&av, "--not");
736 for (i = 0; i < want_obj->nr; i++) {
737 struct object *o = want_obj->objects[i].item;
738 argv_array_push(&av, oid_to_hex(&o->oid));
740 deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
741 argv_array_clear(&av);
744 if (shallows->nr > 0) {
746 for (i = 0; i < shallows->nr; i++)
747 register_shallow(the_repository,
748 &shallows->objects[i].item->oid);
752 shallow_nr += shallows->nr;
756 static int process_shallow(const char *line, struct object_array *shallows)
759 if (skip_prefix(line, "shallow ", &arg)) {
760 struct object_id oid;
761 struct object *object;
762 if (get_oid_hex(arg, &oid))
763 die("invalid shallow line: %s", line);
764 object = parse_object(the_repository, &oid);
767 if (object->type != OBJ_COMMIT)
768 die("invalid shallow object %s", oid_to_hex(&oid));
769 if (!(object->flags & CLIENT_SHALLOW)) {
770 object->flags |= CLIENT_SHALLOW;
771 add_object_array(object, NULL, shallows);
779 static int process_deepen(const char *line, int *depth)
782 if (skip_prefix(line, "deepen ", &arg)) {
784 *depth = (int)strtol(arg, &end, 0);
785 if (!end || *end || *depth <= 0)
786 die("Invalid deepen: %s", line);
793 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
796 if (skip_prefix(line, "deepen-since ", &arg)) {
798 *deepen_since = parse_timestamp(arg, &end, 0);
799 if (!end || *end || !deepen_since ||
800 /* revisions.c's max_age -1 is special */
802 die("Invalid deepen-since: %s", line);
803 *deepen_rev_list = 1;
809 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
812 if (skip_prefix(line, "deepen-not ", &arg)) {
814 struct object_id oid;
815 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
816 die("git upload-pack: ambiguous deepen-not: %s", line);
817 string_list_append(deepen_not, ref);
819 *deepen_rev_list = 1;
825 static void receive_needs(struct object_array *want_obj)
827 struct object_array shallows = OBJECT_ARRAY_INIT;
828 struct string_list deepen_not = STRING_LIST_INIT_DUP;
831 timestamp_t deepen_since = 0;
832 int deepen_rev_list = 0;
837 const char *features;
838 struct object_id oid_buf;
839 char *line = packet_read_line(0, NULL);
846 if (process_shallow(line, &shallows))
848 if (process_deepen(line, &depth))
850 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
852 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
855 if (skip_prefix(line, "filter ", &arg)) {
856 if (!filter_capability_requested)
857 die("git upload-pack: filtering capability not negotiated");
858 parse_list_objects_filter(&filter_options, arg);
862 if (!skip_prefix(line, "want ", &arg) ||
863 parse_oid_hex(arg, &oid_buf, &features))
864 die("git upload-pack: protocol error, "
865 "expected to get object ID, not '%s'", line);
867 if (parse_feature_request(features, "deepen-relative"))
869 if (parse_feature_request(features, "multi_ack_detailed"))
871 else if (parse_feature_request(features, "multi_ack"))
873 if (parse_feature_request(features, "no-done"))
875 if (parse_feature_request(features, "thin-pack"))
877 if (parse_feature_request(features, "ofs-delta"))
879 if (parse_feature_request(features, "side-band-64k"))
880 use_sideband = LARGE_PACKET_MAX;
881 else if (parse_feature_request(features, "side-band"))
882 use_sideband = DEFAULT_PACKET_MAX;
883 if (parse_feature_request(features, "no-progress"))
885 if (parse_feature_request(features, "include-tag"))
887 if (allow_filter && parse_feature_request(features, "filter"))
888 filter_capability_requested = 1;
890 o = parse_object(the_repository, &oid_buf);
893 "ERR upload-pack: not our ref %s",
894 oid_to_hex(&oid_buf));
895 die("git upload-pack: not our ref %s",
896 oid_to_hex(&oid_buf));
898 if (!(o->flags & WANTED)) {
900 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
903 add_object_array(o, NULL, want_obj);
908 * We have sent all our refs already, and the other end
909 * should have chosen out of them. When we are operating
910 * in the stateless RPC mode, however, their choice may
911 * have been based on the set of older refs advertised
912 * by another process that handled the initial request.
915 check_non_tip(want_obj);
917 if (!use_sideband && daemon_mode)
920 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
923 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
924 &deepen_not, &shallows, want_obj))
926 object_array_clear(&shallows);
929 /* return non-zero if the ref is hidden, otherwise 0 */
930 static int mark_our_ref(const char *refname, const char *refname_full,
931 const struct object_id *oid)
933 struct object *o = lookup_unknown_object(oid->hash);
935 if (ref_is_hidden(refname, refname_full)) {
936 o->flags |= HIDDEN_REF;
943 static int check_ref(const char *refname_full, const struct object_id *oid,
944 int flag, void *cb_data)
946 const char *refname = strip_namespace(refname_full);
948 mark_our_ref(refname, refname_full, oid);
952 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
954 struct string_list_item *item;
958 for_each_string_list_item(item, symref)
959 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
962 static int send_ref(const char *refname, const struct object_id *oid,
963 int flag, void *cb_data)
965 static const char *capabilities = "multi_ack thin-pack side-band"
966 " side-band-64k ofs-delta shallow deepen-since deepen-not"
967 " deepen-relative no-progress include-tag multi_ack_detailed";
968 const char *refname_nons = strip_namespace(refname);
969 struct object_id peeled;
971 if (mark_our_ref(refname_nons, refname, oid))
975 struct strbuf symref_info = STRBUF_INIT;
977 format_symref_info(&symref_info, cb_data);
978 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
979 oid_to_hex(oid), refname_nons,
981 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
982 " allow-tip-sha1-in-want" : "",
983 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
984 " allow-reachable-sha1-in-want" : "",
985 stateless_rpc ? " no-done" : "",
987 allow_filter ? " filter" : "",
988 git_user_agent_sanitized());
989 strbuf_release(&symref_info);
991 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
994 if (!peel_ref(refname, &peeled))
995 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
999 static int find_symref(const char *refname, const struct object_id *oid,
1000 int flag, void *cb_data)
1002 const char *symref_target;
1003 struct string_list_item *item;
1005 if ((flag & REF_ISSYMREF) == 0)
1007 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1008 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1009 die("'%s' is a symref but it is not?", refname);
1010 item = string_list_append(cb_data, refname);
1011 item->util = xstrdup(symref_target);
1015 static int upload_pack_config(const char *var, const char *value, void *unused)
1017 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1018 if (git_config_bool(var, value))
1019 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1021 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1022 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1023 if (git_config_bool(var, value))
1024 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1026 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1027 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1028 if (git_config_bool(var, value))
1029 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1031 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1032 } else if (!strcmp("uploadpack.keepalive", var)) {
1033 keepalive = git_config_int(var, value);
1036 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1037 if (!strcmp("uploadpack.packobjectshook", var))
1038 return git_config_string(&pack_objects_hook, var, value);
1039 } else if (!strcmp("uploadpack.allowfilter", var)) {
1040 allow_filter = git_config_bool(var, value);
1041 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1042 allow_ref_in_want = git_config_bool(var, value);
1044 return parse_hide_refs_config(var, value, "uploadpack");
1047 void upload_pack(struct upload_pack_options *options)
1049 struct string_list symref = STRING_LIST_INIT_DUP;
1050 struct object_array want_obj = OBJECT_ARRAY_INIT;
1052 stateless_rpc = options->stateless_rpc;
1053 timeout = options->timeout;
1054 daemon_mode = options->daemon_mode;
1056 git_config(upload_pack_config, NULL);
1058 head_ref_namespaced(find_symref, &symref);
1060 if (options->advertise_refs || !stateless_rpc) {
1062 head_ref_namespaced(send_ref, &symref);
1063 for_each_namespaced_ref(send_ref, &symref);
1064 advertise_shallow_grafts(1);
1067 head_ref_namespaced(check_ref, NULL);
1068 for_each_namespaced_ref(check_ref, NULL);
1070 string_list_clear(&symref, 1);
1071 if (options->advertise_refs)
1074 receive_needs(&want_obj);
1076 struct object_array have_obj = OBJECT_ARRAY_INIT;
1077 get_common_commits(&have_obj, &want_obj);
1078 create_pack_file(&have_obj, &want_obj);
1082 struct upload_pack_data {
1083 struct object_array wants;
1084 struct string_list wanted_refs;
1085 struct oid_array haves;
1087 struct object_array shallows;
1088 struct string_list deepen_not;
1090 timestamp_t deepen_since;
1091 int deepen_rev_list;
1092 int deepen_relative;
1094 unsigned stateless_rpc : 1;
1096 unsigned use_thin_pack : 1;
1097 unsigned use_ofs_delta : 1;
1098 unsigned no_progress : 1;
1099 unsigned use_include_tag : 1;
1103 static void upload_pack_data_init(struct upload_pack_data *data)
1105 struct object_array wants = OBJECT_ARRAY_INIT;
1106 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1107 struct oid_array haves = OID_ARRAY_INIT;
1108 struct object_array shallows = OBJECT_ARRAY_INIT;
1109 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1111 memset(data, 0, sizeof(*data));
1112 data->wants = wants;
1113 data->wanted_refs = wanted_refs;
1114 data->haves = haves;
1115 data->shallows = shallows;
1116 data->deepen_not = deepen_not;
1119 static void upload_pack_data_clear(struct upload_pack_data *data)
1121 object_array_clear(&data->wants);
1122 string_list_clear(&data->wanted_refs, 1);
1123 oid_array_clear(&data->haves);
1124 object_array_clear(&data->shallows);
1125 string_list_clear(&data->deepen_not, 0);
1128 static int parse_want(const char *line, struct object_array *want_obj)
1131 if (skip_prefix(line, "want ", &arg)) {
1132 struct object_id oid;
1135 if (get_oid_hex(arg, &oid))
1136 die("git upload-pack: protocol error, "
1137 "expected to get oid, not '%s'", line);
1139 o = parse_object(the_repository, &oid);
1142 "ERR upload-pack: not our ref %s",
1144 die("git upload-pack: not our ref %s",
1148 if (!(o->flags & WANTED)) {
1150 add_object_array(o, NULL, want_obj);
1159 static int parse_want_ref(const char *line, struct string_list *wanted_refs,
1160 struct object_array *want_obj)
1163 if (skip_prefix(line, "want-ref ", &arg)) {
1164 struct object_id oid;
1165 struct string_list_item *item;
1168 if (read_ref(arg, &oid)) {
1169 packet_write_fmt(1, "ERR unknown ref %s", arg);
1170 die("unknown ref %s", arg);
1173 item = string_list_append(wanted_refs, arg);
1174 item->util = oiddup(&oid);
1176 o = parse_object_or_die(&oid, arg);
1177 if (!(o->flags & WANTED)) {
1179 add_object_array(o, NULL, want_obj);
1188 static int parse_have(const char *line, struct oid_array *haves)
1191 if (skip_prefix(line, "have ", &arg)) {
1192 struct object_id oid;
1194 if (get_oid_hex(arg, &oid))
1195 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1196 oid_array_append(haves, &oid);
1203 static void process_args(struct packet_reader *request,
1204 struct upload_pack_data *data,
1205 struct object_array *want_obj)
1207 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1208 const char *arg = request->line;
1212 if (parse_want(arg, want_obj))
1214 if (allow_ref_in_want &&
1215 parse_want_ref(arg, &data->wanted_refs, want_obj))
1217 /* process have line */
1218 if (parse_have(arg, &data->haves))
1221 /* process args like thin-pack */
1222 if (!strcmp(arg, "thin-pack")) {
1226 if (!strcmp(arg, "ofs-delta")) {
1230 if (!strcmp(arg, "no-progress")) {
1234 if (!strcmp(arg, "include-tag")) {
1235 use_include_tag = 1;
1238 if (!strcmp(arg, "done")) {
1243 /* Shallow related arguments */
1244 if (process_shallow(arg, &data->shallows))
1246 if (process_deepen(arg, &data->depth))
1248 if (process_deepen_since(arg, &data->deepen_since,
1249 &data->deepen_rev_list))
1251 if (process_deepen_not(arg, &data->deepen_not,
1252 &data->deepen_rev_list))
1254 if (!strcmp(arg, "deepen-relative")) {
1255 data->deepen_relative = 1;
1259 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1260 parse_list_objects_filter(&filter_options, p);
1264 /* ignore unknown lines maybe? */
1265 die("unexpected line: '%s'", arg);
1269 static int process_haves(struct oid_array *haves, struct oid_array *common,
1270 struct object_array *have_obj)
1275 for (i = 0; i < haves->nr; i++) {
1276 const struct object_id *oid = &haves->oid[i];
1278 int we_knew_they_have = 0;
1280 if (!has_object_file(oid))
1283 oid_array_append(common, oid);
1285 o = parse_object(the_repository, oid);
1287 die("oops (%s)", oid_to_hex(oid));
1288 if (o->type == OBJ_COMMIT) {
1289 struct commit_list *parents;
1290 struct commit *commit = (struct commit *)o;
1291 if (o->flags & THEY_HAVE)
1292 we_knew_they_have = 1;
1294 o->flags |= THEY_HAVE;
1295 if (!oldest_have || (commit->date < oldest_have))
1296 oldest_have = commit->date;
1297 for (parents = commit->parents;
1299 parents = parents->next)
1300 parents->item->object.flags |= THEY_HAVE;
1302 if (!we_knew_they_have)
1303 add_object_array(o, NULL, have_obj);
1309 static int send_acks(struct oid_array *acks, struct strbuf *response,
1310 const struct object_array *have_obj,
1311 struct object_array *want_obj)
1315 packet_buf_write(response, "acknowledgments\n");
1319 packet_buf_write(response, "NAK\n");
1321 for (i = 0; i < acks->nr; i++) {
1322 packet_buf_write(response, "ACK %s\n",
1323 oid_to_hex(&acks->oid[i]));
1326 if (ok_to_give_up(have_obj, want_obj)) {
1328 packet_buf_write(response, "ready\n");
1335 static int process_haves_and_send_acks(struct upload_pack_data *data,
1336 struct object_array *have_obj,
1337 struct object_array *want_obj)
1339 struct oid_array common = OID_ARRAY_INIT;
1340 struct strbuf response = STRBUF_INIT;
1343 process_haves(&data->haves, &common, have_obj);
1346 } else if (send_acks(&common, &response, have_obj, want_obj)) {
1347 packet_buf_delim(&response);
1351 packet_buf_flush(&response);
1356 write_or_die(1, response.buf, response.len);
1357 strbuf_release(&response);
1359 oid_array_clear(&data->haves);
1360 oid_array_clear(&common);
1364 static void send_wanted_ref_info(struct upload_pack_data *data)
1366 const struct string_list_item *item;
1368 if (!data->wanted_refs.nr)
1371 packet_write_fmt(1, "wanted-refs\n");
1373 for_each_string_list_item(item, &data->wanted_refs) {
1374 packet_write_fmt(1, "%s %s\n",
1375 oid_to_hex(item->util),
1382 static void send_shallow_info(struct upload_pack_data *data,
1383 struct object_array *want_obj)
1385 /* No shallow info needs to be sent */
1386 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1387 !is_repository_shallow(the_repository))
1390 packet_write_fmt(1, "shallow-info\n");
1392 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1393 data->deepen_since, &data->deepen_not,
1394 &data->shallows, want_obj) &&
1395 is_repository_shallow(the_repository))
1396 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
1403 FETCH_PROCESS_ARGS = 0,
1409 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1410 struct packet_reader *request)
1412 enum fetch_state state = FETCH_PROCESS_ARGS;
1413 struct upload_pack_data data;
1414 /* NEEDSWORK: make this non-static */
1415 static struct object_array have_obj;
1416 /* NEEDSWORK: make this non-static */
1417 static struct object_array want_obj;
1419 git_config(upload_pack_config, NULL);
1421 upload_pack_data_init(&data);
1422 use_sideband = LARGE_PACKET_MAX;
1424 while (state != FETCH_DONE) {
1426 case FETCH_PROCESS_ARGS:
1427 process_args(request, &data, &want_obj);
1431 * Request didn't contain any 'want' lines,
1432 * guess they didn't want anything.
1435 } else if (data.haves.nr) {
1437 * Request had 'have' lines, so lets ACK them.
1439 state = FETCH_SEND_ACKS;
1442 * Request had 'want's but no 'have's so we can
1443 * immedietly go to construct and send a pack.
1445 state = FETCH_SEND_PACK;
1448 case FETCH_SEND_ACKS:
1449 if (process_haves_and_send_acks(&data, &have_obj,
1451 state = FETCH_SEND_PACK;
1455 case FETCH_SEND_PACK:
1456 send_wanted_ref_info(&data);
1457 send_shallow_info(&data, &want_obj);
1459 packet_write_fmt(1, "packfile\n");
1460 create_pack_file(&have_obj, &want_obj);
1468 upload_pack_data_clear(&data);
1472 int upload_pack_advertise(struct repository *r,
1473 struct strbuf *value)
1476 int allow_filter_value;
1477 int allow_ref_in_want;
1479 strbuf_addstr(value, "shallow");
1481 if (!repo_config_get_bool(the_repository,
1482 "uploadpack.allowfilter",
1483 &allow_filter_value) &&
1485 strbuf_addstr(value, " filter");
1487 if (!repo_config_get_bool(the_repository,
1488 "uploadpack.allowrefinwant",
1489 &allow_ref_in_want) &&
1491 strbuf_addstr(value, " ref-in-want");