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 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
42 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
44 static timestamp_t oldest_have;
48 static int use_thin_pack, use_ofs_delta, use_include_tag;
49 static int no_progress, daemon_mode;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request;
57 static int shallow_nr;
58 static struct object_array extra_edge_obj;
59 static unsigned int timeout;
60 static int keepalive = 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband;
65 static int stateless_rpc;
66 static const char *pack_objects_hook;
68 static int filter_capability_requested;
69 static int allow_filter;
70 static int allow_ref_in_want;
71 static struct list_objects_filter_options filter_options;
73 static void reset_timeout(void)
78 static void send_client_data(int fd, const char *data, ssize_t sz)
81 send_sideband(1, fd, data, sz, use_sideband);
88 /* XXX: are we happy to lose stuff here? */
92 write_or_die(fd, data, sz);
95 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
98 if (graft->nr_parent == -1)
99 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
103 static void create_pack_file(const struct object_array *have_obj,
104 const struct object_array *want_obj)
106 struct child_process pack_objects = CHILD_PROCESS_INIT;
107 char data[8193], progress[128];
108 char abort_msg[] = "aborting due to possible repository "
109 "corruption on the remote side.";
115 if (!pack_objects_hook)
116 pack_objects.git_cmd = 1;
118 argv_array_push(&pack_objects.args, pack_objects_hook);
119 argv_array_push(&pack_objects.args, "git");
120 pack_objects.use_shell = 1;
124 argv_array_push(&pack_objects.args, "--shallow-file");
125 argv_array_push(&pack_objects.args, "");
127 argv_array_push(&pack_objects.args, "pack-objects");
128 argv_array_push(&pack_objects.args, "--revs");
130 argv_array_push(&pack_objects.args, "--thin");
132 argv_array_push(&pack_objects.args, "--stdout");
134 argv_array_push(&pack_objects.args, "--shallow");
136 argv_array_push(&pack_objects.args, "--progress");
138 argv_array_push(&pack_objects.args, "--delta-base-offset");
140 argv_array_push(&pack_objects.args, "--include-tag");
141 if (filter_options.filter_spec) {
142 struct strbuf expanded_filter_spec = STRBUF_INIT;
143 expand_list_objects_filter_spec(&filter_options,
144 &expanded_filter_spec);
145 if (pack_objects.use_shell) {
146 struct strbuf buf = STRBUF_INIT;
147 sq_quote_buf(&buf, expanded_filter_spec.buf);
148 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
149 strbuf_release(&buf);
151 argv_array_pushf(&pack_objects.args, "--filter=%s",
152 expanded_filter_spec.buf);
156 pack_objects.in = -1;
157 pack_objects.out = -1;
158 pack_objects.err = -1;
160 if (start_command(&pack_objects))
161 die("git upload-pack: unable to fork git-pack-objects");
163 pipe_fd = xfdopen(pack_objects.in, "w");
166 for_each_commit_graft(write_one_shallow, pipe_fd);
168 for (i = 0; i < want_obj->nr; i++)
169 fprintf(pipe_fd, "%s\n",
170 oid_to_hex(&want_obj->objects[i].item->oid));
171 fprintf(pipe_fd, "--not\n");
172 for (i = 0; i < have_obj->nr; i++)
173 fprintf(pipe_fd, "%s\n",
174 oid_to_hex(&have_obj->objects[i].item->oid));
175 for (i = 0; i < extra_edge_obj.nr; i++)
176 fprintf(pipe_fd, "%s\n",
177 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
178 fprintf(pipe_fd, "\n");
182 /* We read from pack_objects.err to capture stderr output for
183 * progress bar, and pack_objects.out to capture the pack data.
187 struct pollfd pfd[2];
188 int pe, pu, pollsize;
196 if (0 <= pack_objects.out) {
197 pfd[pollsize].fd = pack_objects.out;
198 pfd[pollsize].events = POLLIN;
202 if (0 <= pack_objects.err) {
203 pfd[pollsize].fd = pack_objects.err;
204 pfd[pollsize].events = POLLIN;
212 ret = poll(pfd, pollsize,
213 keepalive < 0 ? -1 : 1000 * keepalive);
216 if (errno != EINTR) {
217 error_errno("poll failed, resuming");
222 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
223 /* Status ready; we ship that in the side-band
224 * or dump to the standard error.
226 sz = xread(pack_objects.err, progress,
229 send_client_data(2, progress, sz);
231 close(pack_objects.err);
232 pack_objects.err = -1;
236 /* give priority to status messages */
239 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
240 /* Data ready; we keep the last byte to ourselves
241 * in case we detect broken rev-list, so that we
242 * can leave the stream corrupted. This is
243 * unfortunate -- unpack-objects would happily
244 * accept a valid packdata with trailing garbage,
245 * so appending garbage after we pass all the
246 * pack data is not good enough to signal
247 * breakage to downstream.
255 sz = xread(pack_objects.out, cp,
256 sizeof(data) - outsz);
260 close(pack_objects.out);
261 pack_objects.out = -1;
267 buffered = data[sz-1] & 0xFF;
272 send_client_data(1, data, sz);
276 * We hit the keepalive timeout without saying anything; send
277 * an empty message on the data sideband just to let the other
278 * side know we're still working on it, but don't have any data
281 * If we don't have a sideband channel, there's no room in the
282 * protocol to say anything, so those clients are just out of
285 if (!ret && use_sideband) {
286 static const char buf[] = "0005\1";
287 write_or_die(1, buf, 5);
291 if (finish_command(&pack_objects)) {
292 error("git upload-pack: git-pack-objects died with error.");
299 send_client_data(1, data, 1);
300 fprintf(stderr, "flushed.\n");
307 send_client_data(3, abort_msg, sizeof(abort_msg));
308 die("git upload-pack: %s", abort_msg);
311 static int got_oid(const char *hex, struct object_id *oid,
312 struct object_array *have_obj)
315 int we_knew_they_have = 0;
317 if (get_oid_hex(hex, oid))
318 die("git upload-pack: expected SHA1 object, got '%s'", hex);
319 if (!has_object_file(oid))
322 o = parse_object(the_repository, oid);
324 die("oops (%s)", oid_to_hex(oid));
325 if (o->type == OBJ_COMMIT) {
326 struct commit_list *parents;
327 struct commit *commit = (struct commit *)o;
328 if (o->flags & THEY_HAVE)
329 we_knew_they_have = 1;
331 o->flags |= THEY_HAVE;
332 if (!oldest_have || (commit->date < oldest_have))
333 oldest_have = commit->date;
334 for (parents = commit->parents;
336 parents = parents->next)
337 parents->item->object.flags |= THEY_HAVE;
339 if (!we_knew_they_have) {
340 add_object_array(o, NULL, have_obj);
346 static int ok_to_give_up(const struct object_array *have_obj,
347 struct object_array *want_obj)
349 uint32_t min_generation = GENERATION_NUMBER_ZERO;
354 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
355 COMMON_KNOWN, oldest_have,
359 static int get_common_commits(struct object_array *have_obj,
360 struct object_array *want_obj)
362 struct object_id oid;
363 char last_hex[GIT_MAX_HEXSZ + 1];
368 save_commit_buffer = 0;
371 char *line = packet_read_line(0, NULL);
377 if (multi_ack == 2 && got_common
378 && !got_other && ok_to_give_up(have_obj, want_obj)) {
380 packet_write_fmt(1, "ACK %s ready\n", last_hex);
382 if (have_obj->nr == 0 || multi_ack)
383 packet_write_fmt(1, "NAK\n");
385 if (no_done && sent_ready) {
386 packet_write_fmt(1, "ACK %s\n", last_hex);
395 if (skip_prefix(line, "have ", &arg)) {
396 switch (got_oid(arg, &oid, have_obj)) {
397 case -1: /* they have what we do not */
399 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
400 const char *hex = oid_to_hex(&oid);
401 if (multi_ack == 2) {
403 packet_write_fmt(1, "ACK %s ready\n", hex);
405 packet_write_fmt(1, "ACK %s continue\n", hex);
410 oid_to_hex_r(last_hex, &oid);
412 packet_write_fmt(1, "ACK %s common\n", last_hex);
414 packet_write_fmt(1, "ACK %s continue\n", last_hex);
415 else if (have_obj->nr == 1)
416 packet_write_fmt(1, "ACK %s\n", last_hex);
421 if (!strcmp(line, "done")) {
422 if (have_obj->nr > 0) {
424 packet_write_fmt(1, "ACK %s\n", last_hex);
427 packet_write_fmt(1, "NAK\n");
430 die("git upload-pack: expected SHA1 list, got '%s'", line);
434 static int is_our_ref(struct object *o)
436 int allow_hidden_ref = (allow_unadvertised_object_request &
437 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
438 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
442 * on successful case, it's up to the caller to close cmd->out
444 static int do_reachable_revlist(struct child_process *cmd,
445 struct object_array *src,
446 struct object_array *reachable)
448 static const char *argv[] = {
449 "rev-list", "--stdin", NULL,
452 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
454 const unsigned hexsz = the_hash_algo->hexsz;
463 * If the next rev-list --stdin encounters an unknown commit,
464 * it terminates, which will cause SIGPIPE in the write loop
467 sigchain_push(SIGPIPE, SIG_IGN);
469 if (start_command(cmd))
473 namebuf[hexsz + 1] = '\n';
474 for (i = get_max_object_index(); 0 < i; ) {
475 o = get_indexed_object(--i);
478 if (reachable && o->type == OBJ_COMMIT)
479 o->flags &= ~TMP_MARK;
482 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
483 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
486 namebuf[hexsz] = '\n';
487 for (i = 0; i < src->nr; i++) {
488 o = src->objects[i].item;
491 add_object_array(o, NULL, reachable);
494 if (reachable && o->type == OBJ_COMMIT)
495 o->flags |= TMP_MARK;
496 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
497 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
502 sigchain_pop(SIGPIPE);
507 sigchain_pop(SIGPIPE);
516 static int get_reachable_list(struct object_array *src,
517 struct object_array *reachable)
519 struct child_process cmd = CHILD_PROCESS_INIT;
522 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
523 const unsigned hexsz = the_hash_algo->hexsz;
525 if (do_reachable_revlist(&cmd, src, reachable) < 0)
528 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
529 struct object_id sha1;
532 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
535 o = lookup_object(the_repository, sha1.hash);
536 if (o && o->type == OBJ_COMMIT) {
537 o->flags &= ~TMP_MARK;
540 for (i = get_max_object_index(); 0 < i; i--) {
541 o = get_indexed_object(i - 1);
542 if (o && o->type == OBJ_COMMIT &&
543 (o->flags & TMP_MARK)) {
544 add_object_array(o, NULL, reachable);
545 o->flags &= ~TMP_MARK;
550 if (finish_command(&cmd))
556 static int has_unreachable(struct object_array *src)
558 struct child_process cmd = CHILD_PROCESS_INIT;
562 if (do_reachable_revlist(&cmd, src, NULL) < 0)
566 * The commits out of the rev-list are not ancestors of
569 i = read_in_full(cmd.out, buf, 1);
576 * rev-list may have died by encountering a bad commit
577 * in the history, in which case we do want to bail out
578 * even when it showed no commit.
580 if (finish_command(&cmd))
583 /* All the non-tip ones are ancestors of what we advertised */
587 sigchain_pop(SIGPIPE);
593 static void check_non_tip(struct object_array *want_obj)
598 * In the normal in-process case without
599 * uploadpack.allowReachableSHA1InWant,
600 * non-tip requests can never happen.
602 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
604 if (!has_unreachable(want_obj))
605 /* All the non-tip ones are ancestors of what we advertised */
609 /* Pick one of them (we know there at least is one) */
610 for (i = 0; i < want_obj->nr; i++) {
611 struct object *o = want_obj->objects[i].item;
613 die("git upload-pack: not our ref %s",
614 oid_to_hex(&o->oid));
618 static void send_shallow(struct commit_list *result)
621 struct object *object = &result->item->object;
622 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
623 packet_write_fmt(1, "shallow %s",
624 oid_to_hex(&object->oid));
625 register_shallow(the_repository, &object->oid);
628 result = result->next;
632 static void send_unshallow(const struct object_array *shallows,
633 struct object_array *want_obj)
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_fmt(1, "unshallow %s",
642 oid_to_hex(&object->oid));
643 object->flags &= ~CLIENT_SHALLOW;
645 * We want to _register_ "object" as shallow, but we
646 * also need to traverse object's parents to deepen a
647 * shallow clone. Unregister it for now so we can
648 * parse and add the parents to the want list, then
651 unregister_shallow(&object->oid);
653 parse_commit_or_die((struct commit *)object);
654 parents = ((struct commit *)object)->parents;
656 add_object_array(&parents->item->object,
658 parents = parents->next;
660 add_object_array(object, NULL, &extra_edge_obj);
662 /* make sure commit traversal conforms to client */
663 register_shallow(the_repository, &object->oid);
667 static int check_ref(const char *refname_full, const struct object_id *oid,
668 int flag, void *cb_data);
670 static void deepen(int depth, int deepen_relative,
671 struct object_array *shallows, struct object_array *want_obj)
673 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
676 for (i = 0; i < shallows->nr; i++) {
677 struct object *object = shallows->objects[i].item;
678 object->flags |= NOT_SHALLOW;
680 } else if (deepen_relative) {
681 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
682 struct commit_list *result;
685 * Checking for reachable shallows requires that our refs be
686 * marked with OUR_REF.
688 head_ref_namespaced(check_ref, NULL);
689 for_each_namespaced_ref(check_ref, NULL);
691 get_reachable_list(shallows, &reachable_shallows);
692 result = get_shallow_commits(&reachable_shallows,
694 SHALLOW, NOT_SHALLOW);
695 send_shallow(result);
696 free_commit_list(result);
697 object_array_clear(&reachable_shallows);
699 struct commit_list *result;
701 result = get_shallow_commits(want_obj, depth,
702 SHALLOW, NOT_SHALLOW);
703 send_shallow(result);
704 free_commit_list(result);
707 send_unshallow(shallows, want_obj);
710 static void deepen_by_rev_list(int ac, const char **av,
711 struct object_array *shallows,
712 struct object_array *want_obj)
714 struct commit_list *result;
716 close_commit_graph(the_repository);
717 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
718 send_shallow(result);
719 free_commit_list(result);
720 send_unshallow(shallows, want_obj);
723 /* Returns 1 if a shallow list is sent or 0 otherwise */
724 static int send_shallow_list(int depth, int deepen_rev_list,
725 timestamp_t deepen_since,
726 struct string_list *deepen_not,
728 struct object_array *shallows,
729 struct object_array *want_obj)
733 if (depth > 0 && deepen_rev_list)
734 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
736 deepen(depth, deepen_relative, shallows, want_obj);
738 } else if (deepen_rev_list) {
739 struct argv_array av = ARGV_ARRAY_INIT;
742 argv_array_push(&av, "rev-list");
744 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
745 if (deepen_not->nr) {
746 argv_array_push(&av, "--not");
747 for (i = 0; i < deepen_not->nr; i++) {
748 struct string_list_item *s = deepen_not->items + i;
749 argv_array_push(&av, s->string);
751 argv_array_push(&av, "--not");
753 for (i = 0; i < want_obj->nr; i++) {
754 struct object *o = want_obj->objects[i].item;
755 argv_array_push(&av, oid_to_hex(&o->oid));
757 deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
758 argv_array_clear(&av);
761 if (shallows->nr > 0) {
763 for (i = 0; i < shallows->nr; i++)
764 register_shallow(the_repository,
765 &shallows->objects[i].item->oid);
769 shallow_nr += shallows->nr;
773 static int process_shallow(const char *line, struct object_array *shallows)
776 if (skip_prefix(line, "shallow ", &arg)) {
777 struct object_id oid;
778 struct object *object;
779 if (get_oid_hex(arg, &oid))
780 die("invalid shallow line: %s", line);
781 object = parse_object(the_repository, &oid);
784 if (object->type != OBJ_COMMIT)
785 die("invalid shallow object %s", oid_to_hex(&oid));
786 if (!(object->flags & CLIENT_SHALLOW)) {
787 object->flags |= CLIENT_SHALLOW;
788 add_object_array(object, NULL, shallows);
796 static int process_deepen(const char *line, int *depth)
799 if (skip_prefix(line, "deepen ", &arg)) {
801 *depth = (int)strtol(arg, &end, 0);
802 if (!end || *end || *depth <= 0)
803 die("Invalid deepen: %s", line);
810 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
813 if (skip_prefix(line, "deepen-since ", &arg)) {
815 *deepen_since = parse_timestamp(arg, &end, 0);
816 if (!end || *end || !deepen_since ||
817 /* revisions.c's max_age -1 is special */
819 die("Invalid deepen-since: %s", line);
820 *deepen_rev_list = 1;
826 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
829 if (skip_prefix(line, "deepen-not ", &arg)) {
831 struct object_id oid;
832 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
833 die("git upload-pack: ambiguous deepen-not: %s", line);
834 string_list_append(deepen_not, ref);
836 *deepen_rev_list = 1;
842 static void receive_needs(struct object_array *want_obj)
844 struct object_array shallows = OBJECT_ARRAY_INIT;
845 struct string_list deepen_not = STRING_LIST_INIT_DUP;
848 timestamp_t deepen_since = 0;
849 int deepen_rev_list = 0;
850 int deepen_relative = 0;
855 const char *features;
856 struct object_id oid_buf;
857 char *line = packet_read_line(0, NULL);
864 if (process_shallow(line, &shallows))
866 if (process_deepen(line, &depth))
868 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
870 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
873 if (skip_prefix(line, "filter ", &arg)) {
874 if (!filter_capability_requested)
875 die("git upload-pack: filtering capability not negotiated");
876 parse_list_objects_filter(&filter_options, arg);
880 if (!skip_prefix(line, "want ", &arg) ||
881 parse_oid_hex(arg, &oid_buf, &features))
882 die("git upload-pack: protocol error, "
883 "expected to get object ID, not '%s'", line);
885 if (parse_feature_request(features, "deepen-relative"))
887 if (parse_feature_request(features, "multi_ack_detailed"))
889 else if (parse_feature_request(features, "multi_ack"))
891 if (parse_feature_request(features, "no-done"))
893 if (parse_feature_request(features, "thin-pack"))
895 if (parse_feature_request(features, "ofs-delta"))
897 if (parse_feature_request(features, "side-band-64k"))
898 use_sideband = LARGE_PACKET_MAX;
899 else if (parse_feature_request(features, "side-band"))
900 use_sideband = DEFAULT_PACKET_MAX;
901 if (parse_feature_request(features, "no-progress"))
903 if (parse_feature_request(features, "include-tag"))
905 if (allow_filter && parse_feature_request(features, "filter"))
906 filter_capability_requested = 1;
908 o = parse_object(the_repository, &oid_buf);
911 "ERR upload-pack: not our ref %s",
912 oid_to_hex(&oid_buf));
913 die("git upload-pack: not our ref %s",
914 oid_to_hex(&oid_buf));
916 if (!(o->flags & WANTED)) {
918 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
921 add_object_array(o, NULL, want_obj);
926 * We have sent all our refs already, and the other end
927 * should have chosen out of them. When we are operating
928 * in the stateless RPC mode, however, their choice may
929 * have been based on the set of older refs advertised
930 * by another process that handled the initial request.
933 check_non_tip(want_obj);
935 if (!use_sideband && daemon_mode)
938 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
941 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
942 &deepen_not, deepen_relative, &shallows,
945 object_array_clear(&shallows);
948 /* return non-zero if the ref is hidden, otherwise 0 */
949 static int mark_our_ref(const char *refname, const char *refname_full,
950 const struct object_id *oid)
952 struct object *o = lookup_unknown_object(oid->hash);
954 if (ref_is_hidden(refname, refname_full)) {
955 o->flags |= HIDDEN_REF;
962 static int check_ref(const char *refname_full, const struct object_id *oid,
963 int flag, void *cb_data)
965 const char *refname = strip_namespace(refname_full);
967 mark_our_ref(refname, refname_full, oid);
971 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
973 struct string_list_item *item;
977 for_each_string_list_item(item, symref)
978 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
981 static int send_ref(const char *refname, const struct object_id *oid,
982 int flag, void *cb_data)
984 static const char *capabilities = "multi_ack thin-pack side-band"
985 " side-band-64k ofs-delta shallow deepen-since deepen-not"
986 " deepen-relative no-progress include-tag multi_ack_detailed";
987 const char *refname_nons = strip_namespace(refname);
988 struct object_id peeled;
990 if (mark_our_ref(refname_nons, refname, oid))
994 struct strbuf symref_info = STRBUF_INIT;
996 format_symref_info(&symref_info, cb_data);
997 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
998 oid_to_hex(oid), refname_nons,
1000 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1001 " allow-tip-sha1-in-want" : "",
1002 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1003 " allow-reachable-sha1-in-want" : "",
1004 stateless_rpc ? " no-done" : "",
1006 allow_filter ? " filter" : "",
1007 git_user_agent_sanitized());
1008 strbuf_release(&symref_info);
1010 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1012 capabilities = NULL;
1013 if (!peel_ref(refname, &peeled))
1014 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1018 static int find_symref(const char *refname, const struct object_id *oid,
1019 int flag, void *cb_data)
1021 const char *symref_target;
1022 struct string_list_item *item;
1024 if ((flag & REF_ISSYMREF) == 0)
1026 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1027 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1028 die("'%s' is a symref but it is not?", refname);
1029 item = string_list_append(cb_data, refname);
1030 item->util = xstrdup(symref_target);
1034 static int upload_pack_config(const char *var, const char *value, void *unused)
1036 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1037 if (git_config_bool(var, value))
1038 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1040 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1041 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1042 if (git_config_bool(var, value))
1043 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1045 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1046 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1047 if (git_config_bool(var, value))
1048 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1050 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1051 } else if (!strcmp("uploadpack.keepalive", var)) {
1052 keepalive = git_config_int(var, value);
1055 } else if (!strcmp("uploadpack.allowfilter", var)) {
1056 allow_filter = git_config_bool(var, value);
1057 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1058 allow_ref_in_want = git_config_bool(var, value);
1061 if (current_config_scope() != CONFIG_SCOPE_REPO) {
1062 if (!strcmp("uploadpack.packobjectshook", var))
1063 return git_config_string(&pack_objects_hook, var, value);
1066 return parse_hide_refs_config(var, value, "uploadpack");
1069 void upload_pack(struct upload_pack_options *options)
1071 struct string_list symref = STRING_LIST_INIT_DUP;
1072 struct object_array want_obj = OBJECT_ARRAY_INIT;
1074 stateless_rpc = options->stateless_rpc;
1075 timeout = options->timeout;
1076 daemon_mode = options->daemon_mode;
1078 git_config(upload_pack_config, NULL);
1080 head_ref_namespaced(find_symref, &symref);
1082 if (options->advertise_refs || !stateless_rpc) {
1084 head_ref_namespaced(send_ref, &symref);
1085 for_each_namespaced_ref(send_ref, &symref);
1086 advertise_shallow_grafts(1);
1089 head_ref_namespaced(check_ref, NULL);
1090 for_each_namespaced_ref(check_ref, NULL);
1092 string_list_clear(&symref, 1);
1093 if (options->advertise_refs)
1096 receive_needs(&want_obj);
1098 struct object_array have_obj = OBJECT_ARRAY_INIT;
1099 get_common_commits(&have_obj, &want_obj);
1100 create_pack_file(&have_obj, &want_obj);
1104 struct upload_pack_data {
1105 struct object_array wants;
1106 struct string_list wanted_refs;
1107 struct oid_array haves;
1109 struct object_array shallows;
1110 struct string_list deepen_not;
1112 timestamp_t deepen_since;
1113 int deepen_rev_list;
1114 int deepen_relative;
1116 unsigned stateless_rpc : 1;
1118 unsigned use_thin_pack : 1;
1119 unsigned use_ofs_delta : 1;
1120 unsigned no_progress : 1;
1121 unsigned use_include_tag : 1;
1125 static void upload_pack_data_init(struct upload_pack_data *data)
1127 struct object_array wants = OBJECT_ARRAY_INIT;
1128 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1129 struct oid_array haves = OID_ARRAY_INIT;
1130 struct object_array shallows = OBJECT_ARRAY_INIT;
1131 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1133 memset(data, 0, sizeof(*data));
1134 data->wants = wants;
1135 data->wanted_refs = wanted_refs;
1136 data->haves = haves;
1137 data->shallows = shallows;
1138 data->deepen_not = deepen_not;
1141 static void upload_pack_data_clear(struct upload_pack_data *data)
1143 object_array_clear(&data->wants);
1144 string_list_clear(&data->wanted_refs, 1);
1145 oid_array_clear(&data->haves);
1146 object_array_clear(&data->shallows);
1147 string_list_clear(&data->deepen_not, 0);
1150 static int parse_want(const char *line, struct object_array *want_obj)
1153 if (skip_prefix(line, "want ", &arg)) {
1154 struct object_id oid;
1157 if (get_oid_hex(arg, &oid))
1158 die("git upload-pack: protocol error, "
1159 "expected to get oid, not '%s'", line);
1161 o = parse_object(the_repository, &oid);
1164 "ERR upload-pack: not our ref %s",
1166 die("git upload-pack: not our ref %s",
1170 if (!(o->flags & WANTED)) {
1172 add_object_array(o, NULL, want_obj);
1181 static int parse_want_ref(const char *line, struct string_list *wanted_refs,
1182 struct object_array *want_obj)
1185 if (skip_prefix(line, "want-ref ", &arg)) {
1186 struct object_id oid;
1187 struct string_list_item *item;
1190 if (read_ref(arg, &oid)) {
1191 packet_write_fmt(1, "ERR unknown ref %s", arg);
1192 die("unknown ref %s", arg);
1195 item = string_list_append(wanted_refs, arg);
1196 item->util = oiddup(&oid);
1198 o = parse_object_or_die(&oid, arg);
1199 if (!(o->flags & WANTED)) {
1201 add_object_array(o, NULL, want_obj);
1210 static int parse_have(const char *line, struct oid_array *haves)
1213 if (skip_prefix(line, "have ", &arg)) {
1214 struct object_id oid;
1216 if (get_oid_hex(arg, &oid))
1217 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1218 oid_array_append(haves, &oid);
1225 static void process_args(struct packet_reader *request,
1226 struct upload_pack_data *data,
1227 struct object_array *want_obj)
1229 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1230 const char *arg = request->line;
1234 if (parse_want(arg, want_obj))
1236 if (allow_ref_in_want &&
1237 parse_want_ref(arg, &data->wanted_refs, want_obj))
1239 /* process have line */
1240 if (parse_have(arg, &data->haves))
1243 /* process args like thin-pack */
1244 if (!strcmp(arg, "thin-pack")) {
1248 if (!strcmp(arg, "ofs-delta")) {
1252 if (!strcmp(arg, "no-progress")) {
1256 if (!strcmp(arg, "include-tag")) {
1257 use_include_tag = 1;
1260 if (!strcmp(arg, "done")) {
1265 /* Shallow related arguments */
1266 if (process_shallow(arg, &data->shallows))
1268 if (process_deepen(arg, &data->depth))
1270 if (process_deepen_since(arg, &data->deepen_since,
1271 &data->deepen_rev_list))
1273 if (process_deepen_not(arg, &data->deepen_not,
1274 &data->deepen_rev_list))
1276 if (!strcmp(arg, "deepen-relative")) {
1277 data->deepen_relative = 1;
1281 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1282 parse_list_objects_filter(&filter_options, p);
1286 /* ignore unknown lines maybe? */
1287 die("unexpected line: '%s'", arg);
1291 static int process_haves(struct oid_array *haves, struct oid_array *common,
1292 struct object_array *have_obj)
1297 for (i = 0; i < haves->nr; i++) {
1298 const struct object_id *oid = &haves->oid[i];
1300 int we_knew_they_have = 0;
1302 if (!has_object_file(oid))
1305 oid_array_append(common, oid);
1307 o = parse_object(the_repository, oid);
1309 die("oops (%s)", oid_to_hex(oid));
1310 if (o->type == OBJ_COMMIT) {
1311 struct commit_list *parents;
1312 struct commit *commit = (struct commit *)o;
1313 if (o->flags & THEY_HAVE)
1314 we_knew_they_have = 1;
1316 o->flags |= THEY_HAVE;
1317 if (!oldest_have || (commit->date < oldest_have))
1318 oldest_have = commit->date;
1319 for (parents = commit->parents;
1321 parents = parents->next)
1322 parents->item->object.flags |= THEY_HAVE;
1324 if (!we_knew_they_have)
1325 add_object_array(o, NULL, have_obj);
1331 static int send_acks(struct oid_array *acks, struct strbuf *response,
1332 const struct object_array *have_obj,
1333 struct object_array *want_obj)
1337 packet_buf_write(response, "acknowledgments\n");
1341 packet_buf_write(response, "NAK\n");
1343 for (i = 0; i < acks->nr; i++) {
1344 packet_buf_write(response, "ACK %s\n",
1345 oid_to_hex(&acks->oid[i]));
1348 if (ok_to_give_up(have_obj, want_obj)) {
1350 packet_buf_write(response, "ready\n");
1357 static int process_haves_and_send_acks(struct upload_pack_data *data,
1358 struct object_array *have_obj,
1359 struct object_array *want_obj)
1361 struct oid_array common = OID_ARRAY_INIT;
1362 struct strbuf response = STRBUF_INIT;
1365 process_haves(&data->haves, &common, have_obj);
1368 } else if (send_acks(&common, &response, have_obj, want_obj)) {
1369 packet_buf_delim(&response);
1373 packet_buf_flush(&response);
1378 write_or_die(1, response.buf, response.len);
1379 strbuf_release(&response);
1381 oid_array_clear(&data->haves);
1382 oid_array_clear(&common);
1386 static void send_wanted_ref_info(struct upload_pack_data *data)
1388 const struct string_list_item *item;
1390 if (!data->wanted_refs.nr)
1393 packet_write_fmt(1, "wanted-refs\n");
1395 for_each_string_list_item(item, &data->wanted_refs) {
1396 packet_write_fmt(1, "%s %s\n",
1397 oid_to_hex(item->util),
1404 static void send_shallow_info(struct upload_pack_data *data,
1405 struct object_array *want_obj)
1407 /* No shallow info needs to be sent */
1408 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1409 !is_repository_shallow(the_repository))
1412 packet_write_fmt(1, "shallow-info\n");
1414 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1415 data->deepen_since, &data->deepen_not,
1416 data->deepen_relative,
1417 &data->shallows, want_obj) &&
1418 is_repository_shallow(the_repository))
1419 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
1426 FETCH_PROCESS_ARGS = 0,
1432 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1433 struct packet_reader *request)
1435 enum fetch_state state = FETCH_PROCESS_ARGS;
1436 struct upload_pack_data data;
1437 struct object_array have_obj = OBJECT_ARRAY_INIT;
1438 struct object_array want_obj = OBJECT_ARRAY_INIT;
1440 clear_object_flags(ALL_FLAGS);
1442 git_config(upload_pack_config, NULL);
1444 upload_pack_data_init(&data);
1445 use_sideband = LARGE_PACKET_MAX;
1447 while (state != FETCH_DONE) {
1449 case FETCH_PROCESS_ARGS:
1450 process_args(request, &data, &want_obj);
1454 * Request didn't contain any 'want' lines,
1455 * guess they didn't want anything.
1458 } else if (data.haves.nr) {
1460 * Request had 'have' lines, so lets ACK them.
1462 state = FETCH_SEND_ACKS;
1465 * Request had 'want's but no 'have's so we can
1466 * immedietly go to construct and send a pack.
1468 state = FETCH_SEND_PACK;
1471 case FETCH_SEND_ACKS:
1472 if (process_haves_and_send_acks(&data, &have_obj,
1474 state = FETCH_SEND_PACK;
1478 case FETCH_SEND_PACK:
1479 send_wanted_ref_info(&data);
1480 send_shallow_info(&data, &want_obj);
1482 packet_write_fmt(1, "packfile\n");
1483 create_pack_file(&have_obj, &want_obj);
1491 upload_pack_data_clear(&data);
1492 object_array_clear(&have_obj);
1493 object_array_clear(&want_obj);
1497 int upload_pack_advertise(struct repository *r,
1498 struct strbuf *value)
1501 int allow_filter_value;
1502 int allow_ref_in_want;
1504 strbuf_addstr(value, "shallow");
1506 if (!repo_config_get_bool(the_repository,
1507 "uploadpack.allowfilter",
1508 &allow_filter_value) &&
1510 strbuf_addstr(value, " filter");
1512 if (!repo_config_get_bool(the_repository,
1513 "uploadpack.allowrefinwant",
1514 &allow_ref_in_want) &&
1516 strbuf_addstr(value, " ref-in-want");