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"
28 /* Remember to update object flag allocation in object.h */
29 #define THEY_HAVE (1u << 11)
30 #define OUR_REF (1u << 12)
31 #define WANTED (1u << 13)
32 #define COMMON_KNOWN (1u << 14)
33 #define REACHABLE (1u << 15)
35 #define SHALLOW (1u << 16)
36 #define NOT_SHALLOW (1u << 17)
37 #define CLIENT_SHALLOW (1u << 18)
38 #define HIDDEN_REF (1u << 19)
40 static timestamp_t oldest_have;
42 static int deepen_relative;
45 static int use_thin_pack, use_ofs_delta, use_include_tag;
46 static int no_progress, daemon_mode;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request;
54 static int shallow_nr;
55 static struct object_array have_obj;
56 static struct object_array want_obj;
57 static struct object_array extra_edge_obj;
58 static unsigned int timeout;
59 static int keepalive = 5;
61 * otherwise maximum packet size (up to 65520 bytes).
63 static int use_sideband;
64 static int stateless_rpc;
65 static const char *pack_objects_hook;
67 static int filter_capability_requested;
68 static int allow_filter;
69 static int allow_ref_in_want;
70 static struct list_objects_filter_options filter_options;
72 static void reset_timeout(void)
77 static void send_client_data(int fd, const char *data, ssize_t sz)
80 send_sideband(1, fd, data, sz, use_sideband);
87 /* XXX: are we happy to lose stuff here? */
91 write_or_die(fd, data, sz);
94 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
97 if (graft->nr_parent == -1)
98 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
102 static void create_pack_file(void)
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)
309 int we_knew_they_have = 0;
311 if (get_oid_hex(hex, oid))
312 die("git upload-pack: expected SHA1 object, got '%s'", hex);
313 if (!has_object_file(oid))
316 o = parse_object(the_repository, oid);
318 die("oops (%s)", oid_to_hex(oid));
319 if (o->type == OBJ_COMMIT) {
320 struct commit_list *parents;
321 struct commit *commit = (struct commit *)o;
322 if (o->flags & THEY_HAVE)
323 we_knew_they_have = 1;
325 o->flags |= THEY_HAVE;
326 if (!oldest_have || (commit->date < oldest_have))
327 oldest_have = commit->date;
328 for (parents = commit->parents;
330 parents = parents->next)
331 parents->item->object.flags |= THEY_HAVE;
333 if (!we_knew_they_have) {
334 add_object_array(o, NULL, &have_obj);
340 static int reachable(struct commit *want)
342 struct prio_queue work = { compare_commits_by_commit_date };
344 prio_queue_put(&work, want);
346 struct commit_list *list;
347 struct commit *commit = prio_queue_get(&work);
349 if (commit->object.flags & THEY_HAVE) {
350 want->object.flags |= COMMON_KNOWN;
353 if (!commit->object.parsed)
354 parse_object(the_repository, &commit->object.oid);
355 if (commit->object.flags & REACHABLE)
357 commit->object.flags |= REACHABLE;
358 if (commit->date < oldest_have)
360 for (list = commit->parents; list; list = list->next) {
361 struct commit *parent = list->item;
362 if (!(parent->object.flags & REACHABLE))
363 prio_queue_put(&work, parent);
366 want->object.flags |= REACHABLE;
367 clear_commit_marks(want, REACHABLE);
368 clear_prio_queue(&work);
369 return (want->object.flags & COMMON_KNOWN);
372 static int ok_to_give_up(void)
379 for (i = 0; i < want_obj.nr; i++) {
380 struct object *want = want_obj.objects[i].item;
382 if (want->flags & COMMON_KNOWN)
384 want = deref_tag(the_repository, want, "a want line", 0);
385 if (!want || want->type != OBJ_COMMIT) {
386 /* no way to tell if this is reachable by
387 * looking at the ancestry chain alone, so
388 * leave a note to ourselves not to worry about
389 * this object anymore.
391 want_obj.objects[i].item->flags |= COMMON_KNOWN;
394 if (!reachable((struct commit *)want))
400 static int get_common_commits(void)
402 struct object_id oid;
403 char last_hex[GIT_MAX_HEXSZ + 1];
408 save_commit_buffer = 0;
411 char *line = packet_read_line(0, NULL);
417 if (multi_ack == 2 && got_common
418 && !got_other && ok_to_give_up()) {
420 packet_write_fmt(1, "ACK %s ready\n", last_hex);
422 if (have_obj.nr == 0 || multi_ack)
423 packet_write_fmt(1, "NAK\n");
425 if (no_done && sent_ready) {
426 packet_write_fmt(1, "ACK %s\n", last_hex);
435 if (skip_prefix(line, "have ", &arg)) {
436 switch (got_oid(arg, &oid)) {
437 case -1: /* they have what we do not */
439 if (multi_ack && ok_to_give_up()) {
440 const char *hex = oid_to_hex(&oid);
441 if (multi_ack == 2) {
443 packet_write_fmt(1, "ACK %s ready\n", hex);
445 packet_write_fmt(1, "ACK %s continue\n", hex);
450 oid_to_hex_r(last_hex, &oid);
452 packet_write_fmt(1, "ACK %s common\n", last_hex);
454 packet_write_fmt(1, "ACK %s continue\n", last_hex);
455 else if (have_obj.nr == 1)
456 packet_write_fmt(1, "ACK %s\n", last_hex);
461 if (!strcmp(line, "done")) {
462 if (have_obj.nr > 0) {
464 packet_write_fmt(1, "ACK %s\n", last_hex);
467 packet_write_fmt(1, "NAK\n");
470 die("git upload-pack: expected SHA1 list, got '%s'", line);
474 static int is_our_ref(struct object *o)
476 int allow_hidden_ref = (allow_unadvertised_object_request &
477 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
478 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
482 * on successful case, it's up to the caller to close cmd->out
484 static int do_reachable_revlist(struct child_process *cmd,
485 struct object_array *src,
486 struct object_array *reachable)
488 static const char *argv[] = {
489 "rev-list", "--stdin", NULL,
492 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
502 * If the next rev-list --stdin encounters an unknown commit,
503 * it terminates, which will cause SIGPIPE in the write loop
506 sigchain_push(SIGPIPE, SIG_IGN);
508 if (start_command(cmd))
512 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
513 for (i = get_max_object_index(); 0 < i; ) {
514 o = get_indexed_object(--i);
517 if (reachable && o->type == OBJ_COMMIT)
518 o->flags &= ~TMP_MARK;
521 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
522 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
525 namebuf[GIT_SHA1_HEXSZ] = '\n';
526 for (i = 0; i < src->nr; i++) {
527 o = src->objects[i].item;
530 add_object_array(o, NULL, reachable);
533 if (reachable && o->type == OBJ_COMMIT)
534 o->flags |= TMP_MARK;
535 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
536 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
541 sigchain_pop(SIGPIPE);
546 sigchain_pop(SIGPIPE);
555 static int get_reachable_list(struct object_array *src,
556 struct object_array *reachable)
558 struct child_process cmd = CHILD_PROCESS_INIT;
561 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
562 const unsigned hexsz = the_hash_algo->hexsz;
564 if (do_reachable_revlist(&cmd, src, reachable) < 0)
567 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
568 struct object_id sha1;
571 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
574 o = lookup_object(the_repository, sha1.hash);
575 if (o && o->type == OBJ_COMMIT) {
576 o->flags &= ~TMP_MARK;
579 for (i = get_max_object_index(); 0 < i; i--) {
580 o = get_indexed_object(i - 1);
581 if (o && o->type == OBJ_COMMIT &&
582 (o->flags & TMP_MARK)) {
583 add_object_array(o, NULL, reachable);
584 o->flags &= ~TMP_MARK;
589 if (finish_command(&cmd))
595 static int has_unreachable(struct object_array *src)
597 struct child_process cmd = CHILD_PROCESS_INIT;
601 if (do_reachable_revlist(&cmd, src, NULL) < 0)
605 * The commits out of the rev-list are not ancestors of
608 i = read_in_full(cmd.out, buf, 1);
615 * rev-list may have died by encountering a bad commit
616 * in the history, in which case we do want to bail out
617 * even when it showed no commit.
619 if (finish_command(&cmd))
622 /* All the non-tip ones are ancestors of what we advertised */
626 sigchain_pop(SIGPIPE);
632 static void check_non_tip(void)
637 * In the normal in-process case without
638 * uploadpack.allowReachableSHA1InWant,
639 * non-tip requests can never happen.
641 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
643 if (!has_unreachable(&want_obj))
644 /* All the non-tip ones are ancestors of what we advertised */
648 /* Pick one of them (we know there at least is one) */
649 for (i = 0; i < want_obj.nr; i++) {
650 struct object *o = want_obj.objects[i].item;
652 die("git upload-pack: not our ref %s",
653 oid_to_hex(&o->oid));
657 static void send_shallow(struct commit_list *result)
660 struct object *object = &result->item->object;
661 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
662 packet_write_fmt(1, "shallow %s",
663 oid_to_hex(&object->oid));
664 register_shallow(the_repository, &object->oid);
667 result = result->next;
671 static void send_unshallow(const struct object_array *shallows)
675 for (i = 0; i < shallows->nr; i++) {
676 struct object *object = shallows->objects[i].item;
677 if (object->flags & NOT_SHALLOW) {
678 struct commit_list *parents;
679 packet_write_fmt(1, "unshallow %s",
680 oid_to_hex(&object->oid));
681 object->flags &= ~CLIENT_SHALLOW;
683 * We want to _register_ "object" as shallow, but we
684 * also need to traverse object's parents to deepen a
685 * shallow clone. Unregister it for now so we can
686 * parse and add the parents to the want list, then
689 unregister_shallow(&object->oid);
691 parse_commit_or_die((struct commit *)object);
692 parents = ((struct commit *)object)->parents;
694 add_object_array(&parents->item->object,
696 parents = parents->next;
698 add_object_array(object, NULL, &extra_edge_obj);
700 /* make sure commit traversal conforms to client */
701 register_shallow(the_repository, &object->oid);
705 static void deepen(int depth, int deepen_relative,
706 struct object_array *shallows)
708 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
711 for (i = 0; i < shallows->nr; i++) {
712 struct object *object = shallows->objects[i].item;
713 object->flags |= NOT_SHALLOW;
715 } else if (deepen_relative) {
716 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
717 struct commit_list *result;
719 get_reachable_list(shallows, &reachable_shallows);
720 result = get_shallow_commits(&reachable_shallows,
722 SHALLOW, NOT_SHALLOW);
723 send_shallow(result);
724 free_commit_list(result);
725 object_array_clear(&reachable_shallows);
727 struct commit_list *result;
729 result = get_shallow_commits(&want_obj, depth,
730 SHALLOW, NOT_SHALLOW);
731 send_shallow(result);
732 free_commit_list(result);
735 send_unshallow(shallows);
738 static void deepen_by_rev_list(int ac, const char **av,
739 struct object_array *shallows)
741 struct commit_list *result;
743 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
744 send_shallow(result);
745 free_commit_list(result);
746 send_unshallow(shallows);
749 /* Returns 1 if a shallow list is sent or 0 otherwise */
750 static int send_shallow_list(int depth, int deepen_rev_list,
751 timestamp_t deepen_since,
752 struct string_list *deepen_not,
753 struct object_array *shallows)
757 if (depth > 0 && deepen_rev_list)
758 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
760 deepen(depth, deepen_relative, shallows);
762 } else if (deepen_rev_list) {
763 struct argv_array av = ARGV_ARRAY_INIT;
766 argv_array_push(&av, "rev-list");
768 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
769 if (deepen_not->nr) {
770 argv_array_push(&av, "--not");
771 for (i = 0; i < deepen_not->nr; i++) {
772 struct string_list_item *s = deepen_not->items + i;
773 argv_array_push(&av, s->string);
775 argv_array_push(&av, "--not");
777 for (i = 0; i < want_obj.nr; i++) {
778 struct object *o = want_obj.objects[i].item;
779 argv_array_push(&av, oid_to_hex(&o->oid));
781 deepen_by_rev_list(av.argc, av.argv, shallows);
782 argv_array_clear(&av);
785 if (shallows->nr > 0) {
787 for (i = 0; i < shallows->nr; i++)
788 register_shallow(the_repository,
789 &shallows->objects[i].item->oid);
793 shallow_nr += shallows->nr;
797 static int process_shallow(const char *line, struct object_array *shallows)
800 if (skip_prefix(line, "shallow ", &arg)) {
801 struct object_id oid;
802 struct object *object;
803 if (get_oid_hex(arg, &oid))
804 die("invalid shallow line: %s", line);
805 object = parse_object(the_repository, &oid);
808 if (object->type != OBJ_COMMIT)
809 die("invalid shallow object %s", oid_to_hex(&oid));
810 if (!(object->flags & CLIENT_SHALLOW)) {
811 object->flags |= CLIENT_SHALLOW;
812 add_object_array(object, NULL, shallows);
820 static int process_deepen(const char *line, int *depth)
823 if (skip_prefix(line, "deepen ", &arg)) {
825 *depth = (int)strtol(arg, &end, 0);
826 if (!end || *end || *depth <= 0)
827 die("Invalid deepen: %s", line);
834 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
837 if (skip_prefix(line, "deepen-since ", &arg)) {
839 *deepen_since = parse_timestamp(arg, &end, 0);
840 if (!end || *end || !deepen_since ||
841 /* revisions.c's max_age -1 is special */
843 die("Invalid deepen-since: %s", line);
844 *deepen_rev_list = 1;
850 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
853 if (skip_prefix(line, "deepen-not ", &arg)) {
855 struct object_id oid;
856 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
857 die("git upload-pack: ambiguous deepen-not: %s", line);
858 string_list_append(deepen_not, ref);
860 *deepen_rev_list = 1;
866 static void receive_needs(void)
868 struct object_array shallows = OBJECT_ARRAY_INIT;
869 struct string_list deepen_not = STRING_LIST_INIT_DUP;
872 timestamp_t deepen_since = 0;
873 int deepen_rev_list = 0;
878 const char *features;
879 struct object_id oid_buf;
880 char *line = packet_read_line(0, NULL);
887 if (process_shallow(line, &shallows))
889 if (process_deepen(line, &depth))
891 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
893 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
896 if (skip_prefix(line, "filter ", &arg)) {
897 if (!filter_capability_requested)
898 die("git upload-pack: filtering capability not negotiated");
899 parse_list_objects_filter(&filter_options, arg);
903 if (!skip_prefix(line, "want ", &arg) ||
904 parse_oid_hex(arg, &oid_buf, &features))
905 die("git upload-pack: protocol error, "
906 "expected to get object ID, not '%s'", line);
908 if (parse_feature_request(features, "deepen-relative"))
910 if (parse_feature_request(features, "multi_ack_detailed"))
912 else if (parse_feature_request(features, "multi_ack"))
914 if (parse_feature_request(features, "no-done"))
916 if (parse_feature_request(features, "thin-pack"))
918 if (parse_feature_request(features, "ofs-delta"))
920 if (parse_feature_request(features, "side-band-64k"))
921 use_sideband = LARGE_PACKET_MAX;
922 else if (parse_feature_request(features, "side-band"))
923 use_sideband = DEFAULT_PACKET_MAX;
924 if (parse_feature_request(features, "no-progress"))
926 if (parse_feature_request(features, "include-tag"))
928 if (allow_filter && parse_feature_request(features, "filter"))
929 filter_capability_requested = 1;
931 o = parse_object(the_repository, &oid_buf);
934 "ERR upload-pack: not our ref %s",
935 oid_to_hex(&oid_buf));
936 die("git upload-pack: not our ref %s",
937 oid_to_hex(&oid_buf));
939 if (!(o->flags & WANTED)) {
941 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
944 add_object_array(o, NULL, &want_obj);
949 * We have sent all our refs already, and the other end
950 * should have chosen out of them. When we are operating
951 * in the stateless RPC mode, however, their choice may
952 * have been based on the set of older refs advertised
953 * by another process that handled the initial request.
958 if (!use_sideband && daemon_mode)
961 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
964 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
965 &deepen_not, &shallows))
967 object_array_clear(&shallows);
970 /* return non-zero if the ref is hidden, otherwise 0 */
971 static int mark_our_ref(const char *refname, const char *refname_full,
972 const struct object_id *oid)
974 struct object *o = lookup_unknown_object(oid->hash);
976 if (ref_is_hidden(refname, refname_full)) {
977 o->flags |= HIDDEN_REF;
984 static int check_ref(const char *refname_full, const struct object_id *oid,
985 int flag, void *cb_data)
987 const char *refname = strip_namespace(refname_full);
989 mark_our_ref(refname, refname_full, oid);
993 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
995 struct string_list_item *item;
999 for_each_string_list_item(item, symref)
1000 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1003 static int send_ref(const char *refname, const struct object_id *oid,
1004 int flag, void *cb_data)
1006 static const char *capabilities = "multi_ack thin-pack side-band"
1007 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1008 " deepen-relative no-progress include-tag multi_ack_detailed";
1009 const char *refname_nons = strip_namespace(refname);
1010 struct object_id peeled;
1012 if (mark_our_ref(refname_nons, refname, oid))
1016 struct strbuf symref_info = STRBUF_INIT;
1018 format_symref_info(&symref_info, cb_data);
1019 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1020 oid_to_hex(oid), refname_nons,
1022 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1023 " allow-tip-sha1-in-want" : "",
1024 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1025 " allow-reachable-sha1-in-want" : "",
1026 stateless_rpc ? " no-done" : "",
1028 allow_filter ? " filter" : "",
1029 git_user_agent_sanitized());
1030 strbuf_release(&symref_info);
1032 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1034 capabilities = NULL;
1035 if (!peel_ref(refname, &peeled))
1036 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1040 static int find_symref(const char *refname, const struct object_id *oid,
1041 int flag, void *cb_data)
1043 const char *symref_target;
1044 struct string_list_item *item;
1046 if ((flag & REF_ISSYMREF) == 0)
1048 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1049 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1050 die("'%s' is a symref but it is not?", refname);
1051 item = string_list_append(cb_data, refname);
1052 item->util = xstrdup(symref_target);
1056 static int upload_pack_config(const char *var, const char *value, void *unused)
1058 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1059 if (git_config_bool(var, value))
1060 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1062 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1063 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1064 if (git_config_bool(var, value))
1065 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1067 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1068 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1069 if (git_config_bool(var, value))
1070 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1072 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1073 } else if (!strcmp("uploadpack.keepalive", var)) {
1074 keepalive = git_config_int(var, value);
1077 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1078 if (!strcmp("uploadpack.packobjectshook", var))
1079 return git_config_string(&pack_objects_hook, var, value);
1080 } else if (!strcmp("uploadpack.allowfilter", var)) {
1081 allow_filter = git_config_bool(var, value);
1082 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1083 allow_ref_in_want = git_config_bool(var, value);
1085 return parse_hide_refs_config(var, value, "uploadpack");
1088 void upload_pack(struct upload_pack_options *options)
1090 struct string_list symref = STRING_LIST_INIT_DUP;
1092 stateless_rpc = options->stateless_rpc;
1093 timeout = options->timeout;
1094 daemon_mode = options->daemon_mode;
1096 git_config(upload_pack_config, NULL);
1098 head_ref_namespaced(find_symref, &symref);
1100 if (options->advertise_refs || !stateless_rpc) {
1102 head_ref_namespaced(send_ref, &symref);
1103 for_each_namespaced_ref(send_ref, &symref);
1104 advertise_shallow_grafts(1);
1107 head_ref_namespaced(check_ref, NULL);
1108 for_each_namespaced_ref(check_ref, NULL);
1110 string_list_clear(&symref, 1);
1111 if (options->advertise_refs)
1116 get_common_commits();
1121 struct upload_pack_data {
1122 struct object_array wants;
1123 struct string_list wanted_refs;
1124 struct oid_array haves;
1126 struct object_array shallows;
1127 struct string_list deepen_not;
1129 timestamp_t deepen_since;
1130 int deepen_rev_list;
1131 int deepen_relative;
1133 unsigned stateless_rpc : 1;
1135 unsigned use_thin_pack : 1;
1136 unsigned use_ofs_delta : 1;
1137 unsigned no_progress : 1;
1138 unsigned use_include_tag : 1;
1142 static void upload_pack_data_init(struct upload_pack_data *data)
1144 struct object_array wants = OBJECT_ARRAY_INIT;
1145 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1146 struct oid_array haves = OID_ARRAY_INIT;
1147 struct object_array shallows = OBJECT_ARRAY_INIT;
1148 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1150 memset(data, 0, sizeof(*data));
1151 data->wants = wants;
1152 data->wanted_refs = wanted_refs;
1153 data->haves = haves;
1154 data->shallows = shallows;
1155 data->deepen_not = deepen_not;
1158 static void upload_pack_data_clear(struct upload_pack_data *data)
1160 object_array_clear(&data->wants);
1161 string_list_clear(&data->wanted_refs, 1);
1162 oid_array_clear(&data->haves);
1163 object_array_clear(&data->shallows);
1164 string_list_clear(&data->deepen_not, 0);
1167 static int parse_want(const char *line)
1170 if (skip_prefix(line, "want ", &arg)) {
1171 struct object_id oid;
1174 if (get_oid_hex(arg, &oid))
1175 die("git upload-pack: protocol error, "
1176 "expected to get oid, not '%s'", line);
1178 o = parse_object(the_repository, &oid);
1181 "ERR upload-pack: not our ref %s",
1183 die("git upload-pack: not our ref %s",
1187 if (!(o->flags & WANTED)) {
1189 add_object_array(o, NULL, &want_obj);
1198 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1201 if (skip_prefix(line, "want-ref ", &arg)) {
1202 struct object_id oid;
1203 struct string_list_item *item;
1206 if (read_ref(arg, &oid)) {
1207 packet_write_fmt(1, "ERR unknown ref %s", arg);
1208 die("unknown ref %s", arg);
1211 item = string_list_append(wanted_refs, arg);
1212 item->util = oiddup(&oid);
1214 o = parse_object_or_die(&oid, arg);
1215 if (!(o->flags & WANTED)) {
1217 add_object_array(o, NULL, &want_obj);
1226 static int parse_have(const char *line, struct oid_array *haves)
1229 if (skip_prefix(line, "have ", &arg)) {
1230 struct object_id oid;
1232 if (get_oid_hex(arg, &oid))
1233 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1234 oid_array_append(haves, &oid);
1241 static void process_args(struct packet_reader *request,
1242 struct upload_pack_data *data)
1244 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1245 const char *arg = request->line;
1249 if (parse_want(arg))
1251 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1253 /* process have line */
1254 if (parse_have(arg, &data->haves))
1257 /* process args like thin-pack */
1258 if (!strcmp(arg, "thin-pack")) {
1262 if (!strcmp(arg, "ofs-delta")) {
1266 if (!strcmp(arg, "no-progress")) {
1270 if (!strcmp(arg, "include-tag")) {
1271 use_include_tag = 1;
1274 if (!strcmp(arg, "done")) {
1279 /* Shallow related arguments */
1280 if (process_shallow(arg, &data->shallows))
1282 if (process_deepen(arg, &data->depth))
1284 if (process_deepen_since(arg, &data->deepen_since,
1285 &data->deepen_rev_list))
1287 if (process_deepen_not(arg, &data->deepen_not,
1288 &data->deepen_rev_list))
1290 if (!strcmp(arg, "deepen-relative")) {
1291 data->deepen_relative = 1;
1295 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1296 parse_list_objects_filter(&filter_options, p);
1300 /* ignore unknown lines maybe? */
1301 die("unexpected line: '%s'", arg);
1305 static int process_haves(struct oid_array *haves, struct oid_array *common)
1310 for (i = 0; i < haves->nr; i++) {
1311 const struct object_id *oid = &haves->oid[i];
1313 int we_knew_they_have = 0;
1315 if (!has_object_file(oid))
1318 oid_array_append(common, oid);
1320 o = parse_object(the_repository, oid);
1322 die("oops (%s)", oid_to_hex(oid));
1323 if (o->type == OBJ_COMMIT) {
1324 struct commit_list *parents;
1325 struct commit *commit = (struct commit *)o;
1326 if (o->flags & THEY_HAVE)
1327 we_knew_they_have = 1;
1329 o->flags |= THEY_HAVE;
1330 if (!oldest_have || (commit->date < oldest_have))
1331 oldest_have = commit->date;
1332 for (parents = commit->parents;
1334 parents = parents->next)
1335 parents->item->object.flags |= THEY_HAVE;
1337 if (!we_knew_they_have)
1338 add_object_array(o, NULL, &have_obj);
1344 static int send_acks(struct oid_array *acks, struct strbuf *response)
1348 packet_buf_write(response, "acknowledgments\n");
1352 packet_buf_write(response, "NAK\n");
1354 for (i = 0; i < acks->nr; i++) {
1355 packet_buf_write(response, "ACK %s\n",
1356 oid_to_hex(&acks->oid[i]));
1359 if (ok_to_give_up()) {
1361 packet_buf_write(response, "ready\n");
1368 static int process_haves_and_send_acks(struct upload_pack_data *data)
1370 struct oid_array common = OID_ARRAY_INIT;
1371 struct strbuf response = STRBUF_INIT;
1374 process_haves(&data->haves, &common);
1377 } else if (send_acks(&common, &response)) {
1378 packet_buf_delim(&response);
1382 packet_buf_flush(&response);
1387 write_or_die(1, response.buf, response.len);
1388 strbuf_release(&response);
1390 oid_array_clear(&data->haves);
1391 oid_array_clear(&common);
1395 static void send_wanted_ref_info(struct upload_pack_data *data)
1397 const struct string_list_item *item;
1399 if (!data->wanted_refs.nr)
1402 packet_write_fmt(1, "wanted-refs\n");
1404 for_each_string_list_item(item, &data->wanted_refs) {
1405 packet_write_fmt(1, "%s %s\n",
1406 oid_to_hex(item->util),
1413 static void send_shallow_info(struct upload_pack_data *data)
1415 /* No shallow info needs to be sent */
1416 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1417 !is_repository_shallow(the_repository))
1420 packet_write_fmt(1, "shallow-info\n");
1422 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1423 data->deepen_since, &data->deepen_not,
1425 is_repository_shallow(the_repository))
1426 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1432 FETCH_PROCESS_ARGS = 0,
1438 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1439 struct packet_reader *request)
1441 enum fetch_state state = FETCH_PROCESS_ARGS;
1442 struct upload_pack_data data;
1444 git_config(upload_pack_config, NULL);
1446 upload_pack_data_init(&data);
1447 use_sideband = LARGE_PACKET_MAX;
1449 while (state != FETCH_DONE) {
1451 case FETCH_PROCESS_ARGS:
1452 process_args(request, &data);
1456 * Request didn't contain any 'want' lines,
1457 * guess they didn't want anything.
1460 } else if (data.haves.nr) {
1462 * Request had 'have' lines, so lets ACK them.
1464 state = FETCH_SEND_ACKS;
1467 * Request had 'want's but no 'have's so we can
1468 * immedietly go to construct and send a pack.
1470 state = FETCH_SEND_PACK;
1473 case FETCH_SEND_ACKS:
1474 if (process_haves_and_send_acks(&data))
1475 state = FETCH_SEND_PACK;
1479 case FETCH_SEND_PACK:
1480 send_wanted_ref_info(&data);
1481 send_shallow_info(&data);
1483 packet_write_fmt(1, "packfile\n");
1492 upload_pack_data_clear(&data);
1496 int upload_pack_advertise(struct repository *r,
1497 struct strbuf *value)
1500 int allow_filter_value;
1501 int allow_ref_in_want;
1503 strbuf_addstr(value, "shallow");
1505 if (!repo_config_get_bool(the_repository,
1506 "uploadpack.allowfilter",
1507 &allow_filter_value) &&
1509 strbuf_addstr(value, " filter");
1511 if (!repo_config_get_bool(the_repository,
1512 "uploadpack.allowrefinwant",
1513 &allow_ref_in_want) &&
1515 strbuf_addstr(value, " ref-in-want");