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-reach.h"
29 /* Remember to update object flag allocation in object.h */
30 #define THEY_HAVE (1u << 11)
31 #define OUR_REF (1u << 12)
32 #define WANTED (1u << 13)
33 #define COMMON_KNOWN (1u << 14)
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 ok_to_give_up(void)
342 uint32_t min_generation = GENERATION_NUMBER_ZERO;
347 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
348 COMMON_KNOWN, oldest_have,
352 static int get_common_commits(void)
354 struct object_id oid;
355 char last_hex[GIT_MAX_HEXSZ + 1];
360 save_commit_buffer = 0;
363 char *line = packet_read_line(0, NULL);
369 if (multi_ack == 2 && got_common
370 && !got_other && ok_to_give_up()) {
372 packet_write_fmt(1, "ACK %s ready\n", last_hex);
374 if (have_obj.nr == 0 || multi_ack)
375 packet_write_fmt(1, "NAK\n");
377 if (no_done && sent_ready) {
378 packet_write_fmt(1, "ACK %s\n", last_hex);
387 if (skip_prefix(line, "have ", &arg)) {
388 switch (got_oid(arg, &oid)) {
389 case -1: /* they have what we do not */
391 if (multi_ack && ok_to_give_up()) {
392 const char *hex = oid_to_hex(&oid);
393 if (multi_ack == 2) {
395 packet_write_fmt(1, "ACK %s ready\n", hex);
397 packet_write_fmt(1, "ACK %s continue\n", hex);
402 oid_to_hex_r(last_hex, &oid);
404 packet_write_fmt(1, "ACK %s common\n", last_hex);
406 packet_write_fmt(1, "ACK %s continue\n", last_hex);
407 else if (have_obj.nr == 1)
408 packet_write_fmt(1, "ACK %s\n", last_hex);
413 if (!strcmp(line, "done")) {
414 if (have_obj.nr > 0) {
416 packet_write_fmt(1, "ACK %s\n", last_hex);
419 packet_write_fmt(1, "NAK\n");
422 die("git upload-pack: expected SHA1 list, got '%s'", line);
426 static int is_our_ref(struct object *o)
428 int allow_hidden_ref = (allow_unadvertised_object_request &
429 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
430 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
434 * on successful case, it's up to the caller to close cmd->out
436 static int do_reachable_revlist(struct child_process *cmd,
437 struct object_array *src,
438 struct object_array *reachable)
440 static const char *argv[] = {
441 "rev-list", "--stdin", NULL,
444 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
454 * If the next rev-list --stdin encounters an unknown commit,
455 * it terminates, which will cause SIGPIPE in the write loop
458 sigchain_push(SIGPIPE, SIG_IGN);
460 if (start_command(cmd))
464 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
465 for (i = get_max_object_index(); 0 < i; ) {
466 o = get_indexed_object(--i);
469 if (reachable && o->type == OBJ_COMMIT)
470 o->flags &= ~TMP_MARK;
473 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
474 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
477 namebuf[GIT_SHA1_HEXSZ] = '\n';
478 for (i = 0; i < src->nr; i++) {
479 o = src->objects[i].item;
482 add_object_array(o, NULL, reachable);
485 if (reachable && o->type == OBJ_COMMIT)
486 o->flags |= TMP_MARK;
487 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
488 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
493 sigchain_pop(SIGPIPE);
498 sigchain_pop(SIGPIPE);
507 static int get_reachable_list(struct object_array *src,
508 struct object_array *reachable)
510 struct child_process cmd = CHILD_PROCESS_INIT;
513 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
514 const unsigned hexsz = the_hash_algo->hexsz;
516 if (do_reachable_revlist(&cmd, src, reachable) < 0)
519 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
520 struct object_id sha1;
523 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
526 o = lookup_object(the_repository, sha1.hash);
527 if (o && o->type == OBJ_COMMIT) {
528 o->flags &= ~TMP_MARK;
531 for (i = get_max_object_index(); 0 < i; i--) {
532 o = get_indexed_object(i - 1);
533 if (o && o->type == OBJ_COMMIT &&
534 (o->flags & TMP_MARK)) {
535 add_object_array(o, NULL, reachable);
536 o->flags &= ~TMP_MARK;
541 if (finish_command(&cmd))
547 static int has_unreachable(struct object_array *src)
549 struct child_process cmd = CHILD_PROCESS_INIT;
553 if (do_reachable_revlist(&cmd, src, NULL) < 0)
557 * The commits out of the rev-list are not ancestors of
560 i = read_in_full(cmd.out, buf, 1);
567 * rev-list may have died by encountering a bad commit
568 * in the history, in which case we do want to bail out
569 * even when it showed no commit.
571 if (finish_command(&cmd))
574 /* All the non-tip ones are ancestors of what we advertised */
578 sigchain_pop(SIGPIPE);
584 static void check_non_tip(void)
589 * In the normal in-process case without
590 * uploadpack.allowReachableSHA1InWant,
591 * non-tip requests can never happen.
593 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
595 if (!has_unreachable(&want_obj))
596 /* All the non-tip ones are ancestors of what we advertised */
600 /* Pick one of them (we know there at least is one) */
601 for (i = 0; i < want_obj.nr; i++) {
602 struct object *o = want_obj.objects[i].item;
604 die("git upload-pack: not our ref %s",
605 oid_to_hex(&o->oid));
609 static void send_shallow(struct commit_list *result)
612 struct object *object = &result->item->object;
613 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
614 packet_write_fmt(1, "shallow %s",
615 oid_to_hex(&object->oid));
616 register_shallow(the_repository, &object->oid);
619 result = result->next;
623 static void send_unshallow(const struct object_array *shallows)
627 for (i = 0; i < shallows->nr; i++) {
628 struct object *object = shallows->objects[i].item;
629 if (object->flags & NOT_SHALLOW) {
630 struct commit_list *parents;
631 packet_write_fmt(1, "unshallow %s",
632 oid_to_hex(&object->oid));
633 object->flags &= ~CLIENT_SHALLOW;
635 * We want to _register_ "object" as shallow, but we
636 * also need to traverse object's parents to deepen a
637 * shallow clone. Unregister it for now so we can
638 * parse and add the parents to the want list, then
641 unregister_shallow(&object->oid);
643 parse_commit_or_die((struct commit *)object);
644 parents = ((struct commit *)object)->parents;
646 add_object_array(&parents->item->object,
648 parents = parents->next;
650 add_object_array(object, NULL, &extra_edge_obj);
652 /* make sure commit traversal conforms to client */
653 register_shallow(the_repository, &object->oid);
657 static void deepen(int depth, int deepen_relative,
658 struct object_array *shallows)
660 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
663 for (i = 0; i < shallows->nr; i++) {
664 struct object *object = shallows->objects[i].item;
665 object->flags |= NOT_SHALLOW;
667 } else if (deepen_relative) {
668 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
669 struct commit_list *result;
671 get_reachable_list(shallows, &reachable_shallows);
672 result = get_shallow_commits(&reachable_shallows,
674 SHALLOW, NOT_SHALLOW);
675 send_shallow(result);
676 free_commit_list(result);
677 object_array_clear(&reachable_shallows);
679 struct commit_list *result;
681 result = get_shallow_commits(&want_obj, depth,
682 SHALLOW, NOT_SHALLOW);
683 send_shallow(result);
684 free_commit_list(result);
687 send_unshallow(shallows);
690 static void deepen_by_rev_list(int ac, const char **av,
691 struct object_array *shallows)
693 struct commit_list *result;
695 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
696 send_shallow(result);
697 free_commit_list(result);
698 send_unshallow(shallows);
701 /* Returns 1 if a shallow list is sent or 0 otherwise */
702 static int send_shallow_list(int depth, int deepen_rev_list,
703 timestamp_t deepen_since,
704 struct string_list *deepen_not,
705 struct object_array *shallows)
709 if (depth > 0 && deepen_rev_list)
710 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
712 deepen(depth, deepen_relative, shallows);
714 } else if (deepen_rev_list) {
715 struct argv_array av = ARGV_ARRAY_INIT;
718 argv_array_push(&av, "rev-list");
720 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
721 if (deepen_not->nr) {
722 argv_array_push(&av, "--not");
723 for (i = 0; i < deepen_not->nr; i++) {
724 struct string_list_item *s = deepen_not->items + i;
725 argv_array_push(&av, s->string);
727 argv_array_push(&av, "--not");
729 for (i = 0; i < want_obj.nr; i++) {
730 struct object *o = want_obj.objects[i].item;
731 argv_array_push(&av, oid_to_hex(&o->oid));
733 deepen_by_rev_list(av.argc, av.argv, shallows);
734 argv_array_clear(&av);
737 if (shallows->nr > 0) {
739 for (i = 0; i < shallows->nr; i++)
740 register_shallow(the_repository,
741 &shallows->objects[i].item->oid);
745 shallow_nr += shallows->nr;
749 static int process_shallow(const char *line, struct object_array *shallows)
752 if (skip_prefix(line, "shallow ", &arg)) {
753 struct object_id oid;
754 struct object *object;
755 if (get_oid_hex(arg, &oid))
756 die("invalid shallow line: %s", line);
757 object = parse_object(the_repository, &oid);
760 if (object->type != OBJ_COMMIT)
761 die("invalid shallow object %s", oid_to_hex(&oid));
762 if (!(object->flags & CLIENT_SHALLOW)) {
763 object->flags |= CLIENT_SHALLOW;
764 add_object_array(object, NULL, shallows);
772 static int process_deepen(const char *line, int *depth)
775 if (skip_prefix(line, "deepen ", &arg)) {
777 *depth = (int)strtol(arg, &end, 0);
778 if (!end || *end || *depth <= 0)
779 die("Invalid deepen: %s", line);
786 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
789 if (skip_prefix(line, "deepen-since ", &arg)) {
791 *deepen_since = parse_timestamp(arg, &end, 0);
792 if (!end || *end || !deepen_since ||
793 /* revisions.c's max_age -1 is special */
795 die("Invalid deepen-since: %s", line);
796 *deepen_rev_list = 1;
802 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
805 if (skip_prefix(line, "deepen-not ", &arg)) {
807 struct object_id oid;
808 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
809 die("git upload-pack: ambiguous deepen-not: %s", line);
810 string_list_append(deepen_not, ref);
812 *deepen_rev_list = 1;
818 static void receive_needs(void)
820 struct object_array shallows = OBJECT_ARRAY_INIT;
821 struct string_list deepen_not = STRING_LIST_INIT_DUP;
824 timestamp_t deepen_since = 0;
825 int deepen_rev_list = 0;
830 const char *features;
831 struct object_id oid_buf;
832 char *line = packet_read_line(0, NULL);
839 if (process_shallow(line, &shallows))
841 if (process_deepen(line, &depth))
843 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
845 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
848 if (skip_prefix(line, "filter ", &arg)) {
849 if (!filter_capability_requested)
850 die("git upload-pack: filtering capability not negotiated");
851 parse_list_objects_filter(&filter_options, arg);
855 if (!skip_prefix(line, "want ", &arg) ||
856 parse_oid_hex(arg, &oid_buf, &features))
857 die("git upload-pack: protocol error, "
858 "expected to get object ID, not '%s'", line);
860 if (parse_feature_request(features, "deepen-relative"))
862 if (parse_feature_request(features, "multi_ack_detailed"))
864 else if (parse_feature_request(features, "multi_ack"))
866 if (parse_feature_request(features, "no-done"))
868 if (parse_feature_request(features, "thin-pack"))
870 if (parse_feature_request(features, "ofs-delta"))
872 if (parse_feature_request(features, "side-band-64k"))
873 use_sideband = LARGE_PACKET_MAX;
874 else if (parse_feature_request(features, "side-band"))
875 use_sideband = DEFAULT_PACKET_MAX;
876 if (parse_feature_request(features, "no-progress"))
878 if (parse_feature_request(features, "include-tag"))
880 if (allow_filter && parse_feature_request(features, "filter"))
881 filter_capability_requested = 1;
883 o = parse_object(the_repository, &oid_buf);
886 "ERR upload-pack: not our ref %s",
887 oid_to_hex(&oid_buf));
888 die("git upload-pack: not our ref %s",
889 oid_to_hex(&oid_buf));
891 if (!(o->flags & WANTED)) {
893 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
896 add_object_array(o, NULL, &want_obj);
901 * We have sent all our refs already, and the other end
902 * should have chosen out of them. When we are operating
903 * in the stateless RPC mode, however, their choice may
904 * have been based on the set of older refs advertised
905 * by another process that handled the initial request.
910 if (!use_sideband && daemon_mode)
913 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
916 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
917 &deepen_not, &shallows))
919 object_array_clear(&shallows);
922 /* return non-zero if the ref is hidden, otherwise 0 */
923 static int mark_our_ref(const char *refname, const char *refname_full,
924 const struct object_id *oid)
926 struct object *o = lookup_unknown_object(oid->hash);
928 if (ref_is_hidden(refname, refname_full)) {
929 o->flags |= HIDDEN_REF;
936 static int check_ref(const char *refname_full, const struct object_id *oid,
937 int flag, void *cb_data)
939 const char *refname = strip_namespace(refname_full);
941 mark_our_ref(refname, refname_full, oid);
945 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
947 struct string_list_item *item;
951 for_each_string_list_item(item, symref)
952 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
955 static int send_ref(const char *refname, const struct object_id *oid,
956 int flag, void *cb_data)
958 static const char *capabilities = "multi_ack thin-pack side-band"
959 " side-band-64k ofs-delta shallow deepen-since deepen-not"
960 " deepen-relative no-progress include-tag multi_ack_detailed";
961 const char *refname_nons = strip_namespace(refname);
962 struct object_id peeled;
964 if (mark_our_ref(refname_nons, refname, oid))
968 struct strbuf symref_info = STRBUF_INIT;
970 format_symref_info(&symref_info, cb_data);
971 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
972 oid_to_hex(oid), refname_nons,
974 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
975 " allow-tip-sha1-in-want" : "",
976 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
977 " allow-reachable-sha1-in-want" : "",
978 stateless_rpc ? " no-done" : "",
980 allow_filter ? " filter" : "",
981 git_user_agent_sanitized());
982 strbuf_release(&symref_info);
984 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
987 if (!peel_ref(refname, &peeled))
988 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
992 static int find_symref(const char *refname, const struct object_id *oid,
993 int flag, void *cb_data)
995 const char *symref_target;
996 struct string_list_item *item;
998 if ((flag & REF_ISSYMREF) == 0)
1000 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1001 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1002 die("'%s' is a symref but it is not?", refname);
1003 item = string_list_append(cb_data, refname);
1004 item->util = xstrdup(symref_target);
1008 static int upload_pack_config(const char *var, const char *value, void *unused)
1010 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1011 if (git_config_bool(var, value))
1012 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1014 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1015 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1016 if (git_config_bool(var, value))
1017 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1019 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1020 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1021 if (git_config_bool(var, value))
1022 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1024 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1025 } else if (!strcmp("uploadpack.keepalive", var)) {
1026 keepalive = git_config_int(var, value);
1029 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1030 if (!strcmp("uploadpack.packobjectshook", var))
1031 return git_config_string(&pack_objects_hook, var, value);
1032 } else if (!strcmp("uploadpack.allowfilter", var)) {
1033 allow_filter = git_config_bool(var, value);
1034 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1035 allow_ref_in_want = git_config_bool(var, value);
1037 return parse_hide_refs_config(var, value, "uploadpack");
1040 void upload_pack(struct upload_pack_options *options)
1042 struct string_list symref = STRING_LIST_INIT_DUP;
1044 stateless_rpc = options->stateless_rpc;
1045 timeout = options->timeout;
1046 daemon_mode = options->daemon_mode;
1048 git_config(upload_pack_config, NULL);
1050 head_ref_namespaced(find_symref, &symref);
1052 if (options->advertise_refs || !stateless_rpc) {
1054 head_ref_namespaced(send_ref, &symref);
1055 for_each_namespaced_ref(send_ref, &symref);
1056 advertise_shallow_grafts(1);
1059 head_ref_namespaced(check_ref, NULL);
1060 for_each_namespaced_ref(check_ref, NULL);
1062 string_list_clear(&symref, 1);
1063 if (options->advertise_refs)
1068 get_common_commits();
1073 struct upload_pack_data {
1074 struct object_array wants;
1075 struct string_list wanted_refs;
1076 struct oid_array haves;
1078 struct object_array shallows;
1079 struct string_list deepen_not;
1081 timestamp_t deepen_since;
1082 int deepen_rev_list;
1083 int deepen_relative;
1085 unsigned stateless_rpc : 1;
1087 unsigned use_thin_pack : 1;
1088 unsigned use_ofs_delta : 1;
1089 unsigned no_progress : 1;
1090 unsigned use_include_tag : 1;
1094 static void upload_pack_data_init(struct upload_pack_data *data)
1096 struct object_array wants = OBJECT_ARRAY_INIT;
1097 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1098 struct oid_array haves = OID_ARRAY_INIT;
1099 struct object_array shallows = OBJECT_ARRAY_INIT;
1100 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1102 memset(data, 0, sizeof(*data));
1103 data->wants = wants;
1104 data->wanted_refs = wanted_refs;
1105 data->haves = haves;
1106 data->shallows = shallows;
1107 data->deepen_not = deepen_not;
1110 static void upload_pack_data_clear(struct upload_pack_data *data)
1112 object_array_clear(&data->wants);
1113 string_list_clear(&data->wanted_refs, 1);
1114 oid_array_clear(&data->haves);
1115 object_array_clear(&data->shallows);
1116 string_list_clear(&data->deepen_not, 0);
1119 static int parse_want(const char *line)
1122 if (skip_prefix(line, "want ", &arg)) {
1123 struct object_id oid;
1126 if (get_oid_hex(arg, &oid))
1127 die("git upload-pack: protocol error, "
1128 "expected to get oid, not '%s'", line);
1130 o = parse_object(the_repository, &oid);
1133 "ERR upload-pack: not our ref %s",
1135 die("git upload-pack: not our ref %s",
1139 if (!(o->flags & WANTED)) {
1141 add_object_array(o, NULL, &want_obj);
1150 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1153 if (skip_prefix(line, "want-ref ", &arg)) {
1154 struct object_id oid;
1155 struct string_list_item *item;
1158 if (read_ref(arg, &oid)) {
1159 packet_write_fmt(1, "ERR unknown ref %s", arg);
1160 die("unknown ref %s", arg);
1163 item = string_list_append(wanted_refs, arg);
1164 item->util = oiddup(&oid);
1166 o = parse_object_or_die(&oid, arg);
1167 if (!(o->flags & WANTED)) {
1169 add_object_array(o, NULL, &want_obj);
1178 static int parse_have(const char *line, struct oid_array *haves)
1181 if (skip_prefix(line, "have ", &arg)) {
1182 struct object_id oid;
1184 if (get_oid_hex(arg, &oid))
1185 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1186 oid_array_append(haves, &oid);
1193 static void process_args(struct packet_reader *request,
1194 struct upload_pack_data *data)
1196 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1197 const char *arg = request->line;
1201 if (parse_want(arg))
1203 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1205 /* process have line */
1206 if (parse_have(arg, &data->haves))
1209 /* process args like thin-pack */
1210 if (!strcmp(arg, "thin-pack")) {
1214 if (!strcmp(arg, "ofs-delta")) {
1218 if (!strcmp(arg, "no-progress")) {
1222 if (!strcmp(arg, "include-tag")) {
1223 use_include_tag = 1;
1226 if (!strcmp(arg, "done")) {
1231 /* Shallow related arguments */
1232 if (process_shallow(arg, &data->shallows))
1234 if (process_deepen(arg, &data->depth))
1236 if (process_deepen_since(arg, &data->deepen_since,
1237 &data->deepen_rev_list))
1239 if (process_deepen_not(arg, &data->deepen_not,
1240 &data->deepen_rev_list))
1242 if (!strcmp(arg, "deepen-relative")) {
1243 data->deepen_relative = 1;
1247 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1248 parse_list_objects_filter(&filter_options, p);
1252 /* ignore unknown lines maybe? */
1253 die("unexpected line: '%s'", arg);
1257 static int process_haves(struct oid_array *haves, struct oid_array *common)
1262 for (i = 0; i < haves->nr; i++) {
1263 const struct object_id *oid = &haves->oid[i];
1265 int we_knew_they_have = 0;
1267 if (!has_object_file(oid))
1270 oid_array_append(common, oid);
1272 o = parse_object(the_repository, oid);
1274 die("oops (%s)", oid_to_hex(oid));
1275 if (o->type == OBJ_COMMIT) {
1276 struct commit_list *parents;
1277 struct commit *commit = (struct commit *)o;
1278 if (o->flags & THEY_HAVE)
1279 we_knew_they_have = 1;
1281 o->flags |= THEY_HAVE;
1282 if (!oldest_have || (commit->date < oldest_have))
1283 oldest_have = commit->date;
1284 for (parents = commit->parents;
1286 parents = parents->next)
1287 parents->item->object.flags |= THEY_HAVE;
1289 if (!we_knew_they_have)
1290 add_object_array(o, NULL, &have_obj);
1296 static int send_acks(struct oid_array *acks, struct strbuf *response)
1300 packet_buf_write(response, "acknowledgments\n");
1304 packet_buf_write(response, "NAK\n");
1306 for (i = 0; i < acks->nr; i++) {
1307 packet_buf_write(response, "ACK %s\n",
1308 oid_to_hex(&acks->oid[i]));
1311 if (ok_to_give_up()) {
1313 packet_buf_write(response, "ready\n");
1320 static int process_haves_and_send_acks(struct upload_pack_data *data)
1322 struct oid_array common = OID_ARRAY_INIT;
1323 struct strbuf response = STRBUF_INIT;
1326 process_haves(&data->haves, &common);
1329 } else if (send_acks(&common, &response)) {
1330 packet_buf_delim(&response);
1334 packet_buf_flush(&response);
1339 write_or_die(1, response.buf, response.len);
1340 strbuf_release(&response);
1342 oid_array_clear(&data->haves);
1343 oid_array_clear(&common);
1347 static void send_wanted_ref_info(struct upload_pack_data *data)
1349 const struct string_list_item *item;
1351 if (!data->wanted_refs.nr)
1354 packet_write_fmt(1, "wanted-refs\n");
1356 for_each_string_list_item(item, &data->wanted_refs) {
1357 packet_write_fmt(1, "%s %s\n",
1358 oid_to_hex(item->util),
1365 static void send_shallow_info(struct upload_pack_data *data)
1367 /* No shallow info needs to be sent */
1368 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1369 !is_repository_shallow(the_repository))
1372 packet_write_fmt(1, "shallow-info\n");
1374 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1375 data->deepen_since, &data->deepen_not,
1377 is_repository_shallow(the_repository))
1378 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1384 FETCH_PROCESS_ARGS = 0,
1390 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1391 struct packet_reader *request)
1393 enum fetch_state state = FETCH_PROCESS_ARGS;
1394 struct upload_pack_data data;
1396 git_config(upload_pack_config, NULL);
1398 upload_pack_data_init(&data);
1399 use_sideband = LARGE_PACKET_MAX;
1401 while (state != FETCH_DONE) {
1403 case FETCH_PROCESS_ARGS:
1404 process_args(request, &data);
1408 * Request didn't contain any 'want' lines,
1409 * guess they didn't want anything.
1412 } else if (data.haves.nr) {
1414 * Request had 'have' lines, so lets ACK them.
1416 state = FETCH_SEND_ACKS;
1419 * Request had 'want's but no 'have's so we can
1420 * immedietly go to construct and send a pack.
1422 state = FETCH_SEND_PACK;
1425 case FETCH_SEND_ACKS:
1426 if (process_haves_and_send_acks(&data))
1427 state = FETCH_SEND_PACK;
1431 case FETCH_SEND_PACK:
1432 send_wanted_ref_info(&data);
1433 send_shallow_info(&data);
1435 packet_write_fmt(1, "packfile\n");
1444 upload_pack_data_clear(&data);
1448 int upload_pack_advertise(struct repository *r,
1449 struct strbuf *value)
1452 int allow_filter_value;
1453 int allow_ref_in_want;
1455 strbuf_addstr(value, "shallow");
1457 if (!repo_config_get_bool(the_repository,
1458 "uploadpack.allowfilter",
1459 &allow_filter_value) &&
1461 strbuf_addstr(value, " filter");
1463 if (!repo_config_get_bool(the_repository,
1464 "uploadpack.allowrefinwant",
1465 &allow_ref_in_want) &&
1467 strbuf_addstr(value, " ref-in-want");