6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 static timestamp_t oldest_have;
43 static int deepen_relative;
46 static int use_thin_pack, use_ofs_delta, use_include_tag;
47 static int no_progress, daemon_mode;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53 #define ALLOW_ANY_SHA1 07
54 static unsigned int allow_unadvertised_object_request;
55 static int shallow_nr;
56 static struct object_array 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(const struct object_array *have_obj)
104 struct child_process pack_objects = CHILD_PROCESS_INIT;
105 char data[8193], progress[128];
106 char abort_msg[] = "aborting due to possible repository "
107 "corruption on the remote side.";
113 if (!pack_objects_hook)
114 pack_objects.git_cmd = 1;
116 argv_array_push(&pack_objects.args, pack_objects_hook);
117 argv_array_push(&pack_objects.args, "git");
118 pack_objects.use_shell = 1;
122 argv_array_push(&pack_objects.args, "--shallow-file");
123 argv_array_push(&pack_objects.args, "");
125 argv_array_push(&pack_objects.args, "pack-objects");
126 argv_array_push(&pack_objects.args, "--revs");
128 argv_array_push(&pack_objects.args, "--thin");
130 argv_array_push(&pack_objects.args, "--stdout");
132 argv_array_push(&pack_objects.args, "--shallow");
134 argv_array_push(&pack_objects.args, "--progress");
136 argv_array_push(&pack_objects.args, "--delta-base-offset");
138 argv_array_push(&pack_objects.args, "--include-tag");
139 if (filter_options.filter_spec) {
140 if (pack_objects.use_shell) {
141 struct strbuf buf = STRBUF_INIT;
142 sq_quote_buf(&buf, filter_options.filter_spec);
143 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
144 strbuf_release(&buf);
146 argv_array_pushf(&pack_objects.args, "--filter=%s",
147 filter_options.filter_spec);
151 pack_objects.in = -1;
152 pack_objects.out = -1;
153 pack_objects.err = -1;
155 if (start_command(&pack_objects))
156 die("git upload-pack: unable to fork git-pack-objects");
158 pipe_fd = xfdopen(pack_objects.in, "w");
161 for_each_commit_graft(write_one_shallow, pipe_fd);
163 for (i = 0; i < want_obj.nr; i++)
164 fprintf(pipe_fd, "%s\n",
165 oid_to_hex(&want_obj.objects[i].item->oid));
166 fprintf(pipe_fd, "--not\n");
167 for (i = 0; i < have_obj->nr; i++)
168 fprintf(pipe_fd, "%s\n",
169 oid_to_hex(&have_obj->objects[i].item->oid));
170 for (i = 0; i < extra_edge_obj.nr; i++)
171 fprintf(pipe_fd, "%s\n",
172 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
173 fprintf(pipe_fd, "\n");
177 /* We read from pack_objects.err to capture stderr output for
178 * progress bar, and pack_objects.out to capture the pack data.
182 struct pollfd pfd[2];
183 int pe, pu, pollsize;
191 if (0 <= pack_objects.out) {
192 pfd[pollsize].fd = pack_objects.out;
193 pfd[pollsize].events = POLLIN;
197 if (0 <= pack_objects.err) {
198 pfd[pollsize].fd = pack_objects.err;
199 pfd[pollsize].events = POLLIN;
207 ret = poll(pfd, pollsize,
208 keepalive < 0 ? -1 : 1000 * keepalive);
211 if (errno != EINTR) {
212 error_errno("poll failed, resuming");
217 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
218 /* Status ready; we ship that in the side-band
219 * or dump to the standard error.
221 sz = xread(pack_objects.err, progress,
224 send_client_data(2, progress, sz);
226 close(pack_objects.err);
227 pack_objects.err = -1;
231 /* give priority to status messages */
234 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
235 /* Data ready; we keep the last byte to ourselves
236 * in case we detect broken rev-list, so that we
237 * can leave the stream corrupted. This is
238 * unfortunate -- unpack-objects would happily
239 * accept a valid packdata with trailing garbage,
240 * so appending garbage after we pass all the
241 * pack data is not good enough to signal
242 * breakage to downstream.
250 sz = xread(pack_objects.out, cp,
251 sizeof(data) - outsz);
255 close(pack_objects.out);
256 pack_objects.out = -1;
262 buffered = data[sz-1] & 0xFF;
267 send_client_data(1, data, sz);
271 * We hit the keepalive timeout without saying anything; send
272 * an empty message on the data sideband just to let the other
273 * side know we're still working on it, but don't have any data
276 * If we don't have a sideband channel, there's no room in the
277 * protocol to say anything, so those clients are just out of
280 if (!ret && use_sideband) {
281 static const char buf[] = "0005\1";
282 write_or_die(1, buf, 5);
286 if (finish_command(&pack_objects)) {
287 error("git upload-pack: git-pack-objects died with error.");
294 send_client_data(1, data, 1);
295 fprintf(stderr, "flushed.\n");
302 send_client_data(3, abort_msg, sizeof(abort_msg));
303 die("git upload-pack: %s", abort_msg);
306 static int got_oid(const char *hex, struct object_id *oid,
307 struct object_array *have_obj)
310 int we_knew_they_have = 0;
312 if (get_oid_hex(hex, oid))
313 die("git upload-pack: expected SHA1 object, got '%s'", hex);
314 if (!has_object_file(oid))
317 o = parse_object(the_repository, oid);
319 die("oops (%s)", oid_to_hex(oid));
320 if (o->type == OBJ_COMMIT) {
321 struct commit_list *parents;
322 struct commit *commit = (struct commit *)o;
323 if (o->flags & THEY_HAVE)
324 we_knew_they_have = 1;
326 o->flags |= THEY_HAVE;
327 if (!oldest_have || (commit->date < oldest_have))
328 oldest_have = commit->date;
329 for (parents = commit->parents;
331 parents = parents->next)
332 parents->item->object.flags |= THEY_HAVE;
334 if (!we_knew_they_have) {
335 add_object_array(o, NULL, have_obj);
341 static int ok_to_give_up(const struct object_array *have_obj)
343 uint32_t min_generation = GENERATION_NUMBER_ZERO;
348 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
349 COMMON_KNOWN, oldest_have,
353 static int get_common_commits(struct object_array *have_obj)
355 struct object_id oid;
356 char last_hex[GIT_MAX_HEXSZ + 1];
361 save_commit_buffer = 0;
364 char *line = packet_read_line(0, NULL);
370 if (multi_ack == 2 && got_common
371 && !got_other && ok_to_give_up(have_obj)) {
373 packet_write_fmt(1, "ACK %s ready\n", last_hex);
375 if (have_obj->nr == 0 || multi_ack)
376 packet_write_fmt(1, "NAK\n");
378 if (no_done && sent_ready) {
379 packet_write_fmt(1, "ACK %s\n", last_hex);
388 if (skip_prefix(line, "have ", &arg)) {
389 switch (got_oid(arg, &oid, have_obj)) {
390 case -1: /* they have what we do not */
392 if (multi_ack && ok_to_give_up(have_obj)) {
393 const char *hex = oid_to_hex(&oid);
394 if (multi_ack == 2) {
396 packet_write_fmt(1, "ACK %s ready\n", hex);
398 packet_write_fmt(1, "ACK %s continue\n", hex);
403 oid_to_hex_r(last_hex, &oid);
405 packet_write_fmt(1, "ACK %s common\n", last_hex);
407 packet_write_fmt(1, "ACK %s continue\n", last_hex);
408 else if (have_obj->nr == 1)
409 packet_write_fmt(1, "ACK %s\n", last_hex);
414 if (!strcmp(line, "done")) {
415 if (have_obj->nr > 0) {
417 packet_write_fmt(1, "ACK %s\n", last_hex);
420 packet_write_fmt(1, "NAK\n");
423 die("git upload-pack: expected SHA1 list, got '%s'", line);
427 static int is_our_ref(struct object *o)
429 int allow_hidden_ref = (allow_unadvertised_object_request &
430 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
431 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
435 * on successful case, it's up to the caller to close cmd->out
437 static int do_reachable_revlist(struct child_process *cmd,
438 struct object_array *src,
439 struct object_array *reachable)
441 static const char *argv[] = {
442 "rev-list", "--stdin", NULL,
445 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
455 * If the next rev-list --stdin encounters an unknown commit,
456 * it terminates, which will cause SIGPIPE in the write loop
459 sigchain_push(SIGPIPE, SIG_IGN);
461 if (start_command(cmd))
465 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
466 for (i = get_max_object_index(); 0 < i; ) {
467 o = get_indexed_object(--i);
470 if (reachable && o->type == OBJ_COMMIT)
471 o->flags &= ~TMP_MARK;
474 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
475 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
478 namebuf[GIT_SHA1_HEXSZ] = '\n';
479 for (i = 0; i < src->nr; i++) {
480 o = src->objects[i].item;
483 add_object_array(o, NULL, reachable);
486 if (reachable && o->type == OBJ_COMMIT)
487 o->flags |= TMP_MARK;
488 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
489 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
494 sigchain_pop(SIGPIPE);
499 sigchain_pop(SIGPIPE);
508 static int get_reachable_list(struct object_array *src,
509 struct object_array *reachable)
511 struct child_process cmd = CHILD_PROCESS_INIT;
514 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
515 const unsigned hexsz = the_hash_algo->hexsz;
517 if (do_reachable_revlist(&cmd, src, reachable) < 0)
520 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
521 struct object_id sha1;
524 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
527 o = lookup_object(the_repository, sha1.hash);
528 if (o && o->type == OBJ_COMMIT) {
529 o->flags &= ~TMP_MARK;
532 for (i = get_max_object_index(); 0 < i; i--) {
533 o = get_indexed_object(i - 1);
534 if (o && o->type == OBJ_COMMIT &&
535 (o->flags & TMP_MARK)) {
536 add_object_array(o, NULL, reachable);
537 o->flags &= ~TMP_MARK;
542 if (finish_command(&cmd))
548 static int has_unreachable(struct object_array *src)
550 struct child_process cmd = CHILD_PROCESS_INIT;
554 if (do_reachable_revlist(&cmd, src, NULL) < 0)
558 * The commits out of the rev-list are not ancestors of
561 i = read_in_full(cmd.out, buf, 1);
568 * rev-list may have died by encountering a bad commit
569 * in the history, in which case we do want to bail out
570 * even when it showed no commit.
572 if (finish_command(&cmd))
575 /* All the non-tip ones are ancestors of what we advertised */
579 sigchain_pop(SIGPIPE);
585 static void check_non_tip(void)
590 * In the normal in-process case without
591 * uploadpack.allowReachableSHA1InWant,
592 * non-tip requests can never happen.
594 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
596 if (!has_unreachable(&want_obj))
597 /* All the non-tip ones are ancestors of what we advertised */
601 /* Pick one of them (we know there at least is one) */
602 for (i = 0; i < want_obj.nr; i++) {
603 struct object *o = want_obj.objects[i].item;
605 die("git upload-pack: not our ref %s",
606 oid_to_hex(&o->oid));
610 static void send_shallow(struct commit_list *result)
613 struct object *object = &result->item->object;
614 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
615 packet_write_fmt(1, "shallow %s",
616 oid_to_hex(&object->oid));
617 register_shallow(the_repository, &object->oid);
620 result = result->next;
624 static void send_unshallow(const struct object_array *shallows)
628 for (i = 0; i < shallows->nr; i++) {
629 struct object *object = shallows->objects[i].item;
630 if (object->flags & NOT_SHALLOW) {
631 struct commit_list *parents;
632 packet_write_fmt(1, "unshallow %s",
633 oid_to_hex(&object->oid));
634 object->flags &= ~CLIENT_SHALLOW;
636 * We want to _register_ "object" as shallow, but we
637 * also need to traverse object's parents to deepen a
638 * shallow clone. Unregister it for now so we can
639 * parse and add the parents to the want list, then
642 unregister_shallow(&object->oid);
644 parse_commit_or_die((struct commit *)object);
645 parents = ((struct commit *)object)->parents;
647 add_object_array(&parents->item->object,
649 parents = parents->next;
651 add_object_array(object, NULL, &extra_edge_obj);
653 /* make sure commit traversal conforms to client */
654 register_shallow(the_repository, &object->oid);
658 static void deepen(int depth, int deepen_relative,
659 struct object_array *shallows)
661 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
664 for (i = 0; i < shallows->nr; i++) {
665 struct object *object = shallows->objects[i].item;
666 object->flags |= NOT_SHALLOW;
668 } else if (deepen_relative) {
669 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
670 struct commit_list *result;
672 get_reachable_list(shallows, &reachable_shallows);
673 result = get_shallow_commits(&reachable_shallows,
675 SHALLOW, NOT_SHALLOW);
676 send_shallow(result);
677 free_commit_list(result);
678 object_array_clear(&reachable_shallows);
680 struct commit_list *result;
682 result = get_shallow_commits(&want_obj, depth,
683 SHALLOW, NOT_SHALLOW);
684 send_shallow(result);
685 free_commit_list(result);
688 send_unshallow(shallows);
691 static void deepen_by_rev_list(int ac, const char **av,
692 struct object_array *shallows)
694 struct commit_list *result;
696 close_commit_graph(the_repository);
697 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
698 send_shallow(result);
699 free_commit_list(result);
700 send_unshallow(shallows);
703 /* Returns 1 if a shallow list is sent or 0 otherwise */
704 static int send_shallow_list(int depth, int deepen_rev_list,
705 timestamp_t deepen_since,
706 struct string_list *deepen_not,
707 struct object_array *shallows)
711 if (depth > 0 && deepen_rev_list)
712 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
714 deepen(depth, deepen_relative, shallows);
716 } else if (deepen_rev_list) {
717 struct argv_array av = ARGV_ARRAY_INIT;
720 argv_array_push(&av, "rev-list");
722 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
723 if (deepen_not->nr) {
724 argv_array_push(&av, "--not");
725 for (i = 0; i < deepen_not->nr; i++) {
726 struct string_list_item *s = deepen_not->items + i;
727 argv_array_push(&av, s->string);
729 argv_array_push(&av, "--not");
731 for (i = 0; i < want_obj.nr; i++) {
732 struct object *o = want_obj.objects[i].item;
733 argv_array_push(&av, oid_to_hex(&o->oid));
735 deepen_by_rev_list(av.argc, av.argv, shallows);
736 argv_array_clear(&av);
739 if (shallows->nr > 0) {
741 for (i = 0; i < shallows->nr; i++)
742 register_shallow(the_repository,
743 &shallows->objects[i].item->oid);
747 shallow_nr += shallows->nr;
751 static int process_shallow(const char *line, struct object_array *shallows)
754 if (skip_prefix(line, "shallow ", &arg)) {
755 struct object_id oid;
756 struct object *object;
757 if (get_oid_hex(arg, &oid))
758 die("invalid shallow line: %s", line);
759 object = parse_object(the_repository, &oid);
762 if (object->type != OBJ_COMMIT)
763 die("invalid shallow object %s", oid_to_hex(&oid));
764 if (!(object->flags & CLIENT_SHALLOW)) {
765 object->flags |= CLIENT_SHALLOW;
766 add_object_array(object, NULL, shallows);
774 static int process_deepen(const char *line, int *depth)
777 if (skip_prefix(line, "deepen ", &arg)) {
779 *depth = (int)strtol(arg, &end, 0);
780 if (!end || *end || *depth <= 0)
781 die("Invalid deepen: %s", line);
788 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
791 if (skip_prefix(line, "deepen-since ", &arg)) {
793 *deepen_since = parse_timestamp(arg, &end, 0);
794 if (!end || *end || !deepen_since ||
795 /* revisions.c's max_age -1 is special */
797 die("Invalid deepen-since: %s", line);
798 *deepen_rev_list = 1;
804 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
807 if (skip_prefix(line, "deepen-not ", &arg)) {
809 struct object_id oid;
810 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
811 die("git upload-pack: ambiguous deepen-not: %s", line);
812 string_list_append(deepen_not, ref);
814 *deepen_rev_list = 1;
820 static void receive_needs(void)
822 struct object_array shallows = OBJECT_ARRAY_INIT;
823 struct string_list deepen_not = STRING_LIST_INIT_DUP;
826 timestamp_t deepen_since = 0;
827 int deepen_rev_list = 0;
832 const char *features;
833 struct object_id oid_buf;
834 char *line = packet_read_line(0, NULL);
841 if (process_shallow(line, &shallows))
843 if (process_deepen(line, &depth))
845 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
847 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
850 if (skip_prefix(line, "filter ", &arg)) {
851 if (!filter_capability_requested)
852 die("git upload-pack: filtering capability not negotiated");
853 parse_list_objects_filter(&filter_options, arg);
857 if (!skip_prefix(line, "want ", &arg) ||
858 parse_oid_hex(arg, &oid_buf, &features))
859 die("git upload-pack: protocol error, "
860 "expected to get object ID, not '%s'", line);
862 if (parse_feature_request(features, "deepen-relative"))
864 if (parse_feature_request(features, "multi_ack_detailed"))
866 else if (parse_feature_request(features, "multi_ack"))
868 if (parse_feature_request(features, "no-done"))
870 if (parse_feature_request(features, "thin-pack"))
872 if (parse_feature_request(features, "ofs-delta"))
874 if (parse_feature_request(features, "side-band-64k"))
875 use_sideband = LARGE_PACKET_MAX;
876 else if (parse_feature_request(features, "side-band"))
877 use_sideband = DEFAULT_PACKET_MAX;
878 if (parse_feature_request(features, "no-progress"))
880 if (parse_feature_request(features, "include-tag"))
882 if (allow_filter && parse_feature_request(features, "filter"))
883 filter_capability_requested = 1;
885 o = parse_object(the_repository, &oid_buf);
888 "ERR upload-pack: not our ref %s",
889 oid_to_hex(&oid_buf));
890 die("git upload-pack: not our ref %s",
891 oid_to_hex(&oid_buf));
893 if (!(o->flags & WANTED)) {
895 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
898 add_object_array(o, NULL, &want_obj);
903 * We have sent all our refs already, and the other end
904 * should have chosen out of them. When we are operating
905 * in the stateless RPC mode, however, their choice may
906 * have been based on the set of older refs advertised
907 * by another process that handled the initial request.
912 if (!use_sideband && daemon_mode)
915 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
918 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
919 &deepen_not, &shallows))
921 object_array_clear(&shallows);
924 /* return non-zero if the ref is hidden, otherwise 0 */
925 static int mark_our_ref(const char *refname, const char *refname_full,
926 const struct object_id *oid)
928 struct object *o = lookup_unknown_object(oid->hash);
930 if (ref_is_hidden(refname, refname_full)) {
931 o->flags |= HIDDEN_REF;
938 static int check_ref(const char *refname_full, const struct object_id *oid,
939 int flag, void *cb_data)
941 const char *refname = strip_namespace(refname_full);
943 mark_our_ref(refname, refname_full, oid);
947 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
949 struct string_list_item *item;
953 for_each_string_list_item(item, symref)
954 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
957 static int send_ref(const char *refname, const struct object_id *oid,
958 int flag, void *cb_data)
960 static const char *capabilities = "multi_ack thin-pack side-band"
961 " side-band-64k ofs-delta shallow deepen-since deepen-not"
962 " deepen-relative no-progress include-tag multi_ack_detailed";
963 const char *refname_nons = strip_namespace(refname);
964 struct object_id peeled;
966 if (mark_our_ref(refname_nons, refname, oid))
970 struct strbuf symref_info = STRBUF_INIT;
972 format_symref_info(&symref_info, cb_data);
973 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
974 oid_to_hex(oid), refname_nons,
976 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
977 " allow-tip-sha1-in-want" : "",
978 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
979 " allow-reachable-sha1-in-want" : "",
980 stateless_rpc ? " no-done" : "",
982 allow_filter ? " filter" : "",
983 git_user_agent_sanitized());
984 strbuf_release(&symref_info);
986 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
989 if (!peel_ref(refname, &peeled))
990 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
994 static int find_symref(const char *refname, const struct object_id *oid,
995 int flag, void *cb_data)
997 const char *symref_target;
998 struct string_list_item *item;
1000 if ((flag & REF_ISSYMREF) == 0)
1002 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1003 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1004 die("'%s' is a symref but it is not?", refname);
1005 item = string_list_append(cb_data, refname);
1006 item->util = xstrdup(symref_target);
1010 static int upload_pack_config(const char *var, const char *value, void *unused)
1012 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1013 if (git_config_bool(var, value))
1014 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1016 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1017 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1018 if (git_config_bool(var, value))
1019 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1021 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1022 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1023 if (git_config_bool(var, value))
1024 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1026 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1027 } else if (!strcmp("uploadpack.keepalive", var)) {
1028 keepalive = git_config_int(var, value);
1031 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1032 if (!strcmp("uploadpack.packobjectshook", var))
1033 return git_config_string(&pack_objects_hook, var, value);
1034 } else if (!strcmp("uploadpack.allowfilter", var)) {
1035 allow_filter = git_config_bool(var, value);
1036 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1037 allow_ref_in_want = git_config_bool(var, value);
1039 return parse_hide_refs_config(var, value, "uploadpack");
1042 void upload_pack(struct upload_pack_options *options)
1044 struct string_list symref = STRING_LIST_INIT_DUP;
1046 stateless_rpc = options->stateless_rpc;
1047 timeout = options->timeout;
1048 daemon_mode = options->daemon_mode;
1050 git_config(upload_pack_config, NULL);
1052 head_ref_namespaced(find_symref, &symref);
1054 if (options->advertise_refs || !stateless_rpc) {
1056 head_ref_namespaced(send_ref, &symref);
1057 for_each_namespaced_ref(send_ref, &symref);
1058 advertise_shallow_grafts(1);
1061 head_ref_namespaced(check_ref, NULL);
1062 for_each_namespaced_ref(check_ref, NULL);
1064 string_list_clear(&symref, 1);
1065 if (options->advertise_refs)
1070 struct object_array have_obj = OBJECT_ARRAY_INIT;
1071 get_common_commits(&have_obj);
1072 create_pack_file(&have_obj);
1076 struct upload_pack_data {
1077 struct object_array wants;
1078 struct string_list wanted_refs;
1079 struct oid_array haves;
1081 struct object_array shallows;
1082 struct string_list deepen_not;
1084 timestamp_t deepen_since;
1085 int deepen_rev_list;
1086 int deepen_relative;
1088 unsigned stateless_rpc : 1;
1090 unsigned use_thin_pack : 1;
1091 unsigned use_ofs_delta : 1;
1092 unsigned no_progress : 1;
1093 unsigned use_include_tag : 1;
1097 static void upload_pack_data_init(struct upload_pack_data *data)
1099 struct object_array wants = OBJECT_ARRAY_INIT;
1100 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1101 struct oid_array haves = OID_ARRAY_INIT;
1102 struct object_array shallows = OBJECT_ARRAY_INIT;
1103 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1105 memset(data, 0, sizeof(*data));
1106 data->wants = wants;
1107 data->wanted_refs = wanted_refs;
1108 data->haves = haves;
1109 data->shallows = shallows;
1110 data->deepen_not = deepen_not;
1113 static void upload_pack_data_clear(struct upload_pack_data *data)
1115 object_array_clear(&data->wants);
1116 string_list_clear(&data->wanted_refs, 1);
1117 oid_array_clear(&data->haves);
1118 object_array_clear(&data->shallows);
1119 string_list_clear(&data->deepen_not, 0);
1122 static int parse_want(const char *line)
1125 if (skip_prefix(line, "want ", &arg)) {
1126 struct object_id oid;
1129 if (get_oid_hex(arg, &oid))
1130 die("git upload-pack: protocol error, "
1131 "expected to get oid, not '%s'", line);
1133 o = parse_object(the_repository, &oid);
1136 "ERR upload-pack: not our ref %s",
1138 die("git upload-pack: not our ref %s",
1142 if (!(o->flags & WANTED)) {
1144 add_object_array(o, NULL, &want_obj);
1153 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1156 if (skip_prefix(line, "want-ref ", &arg)) {
1157 struct object_id oid;
1158 struct string_list_item *item;
1161 if (read_ref(arg, &oid)) {
1162 packet_write_fmt(1, "ERR unknown ref %s", arg);
1163 die("unknown ref %s", arg);
1166 item = string_list_append(wanted_refs, arg);
1167 item->util = oiddup(&oid);
1169 o = parse_object_or_die(&oid, arg);
1170 if (!(o->flags & WANTED)) {
1172 add_object_array(o, NULL, &want_obj);
1181 static int parse_have(const char *line, struct oid_array *haves)
1184 if (skip_prefix(line, "have ", &arg)) {
1185 struct object_id oid;
1187 if (get_oid_hex(arg, &oid))
1188 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1189 oid_array_append(haves, &oid);
1196 static void process_args(struct packet_reader *request,
1197 struct upload_pack_data *data)
1199 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1200 const char *arg = request->line;
1204 if (parse_want(arg))
1206 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1208 /* process have line */
1209 if (parse_have(arg, &data->haves))
1212 /* process args like thin-pack */
1213 if (!strcmp(arg, "thin-pack")) {
1217 if (!strcmp(arg, "ofs-delta")) {
1221 if (!strcmp(arg, "no-progress")) {
1225 if (!strcmp(arg, "include-tag")) {
1226 use_include_tag = 1;
1229 if (!strcmp(arg, "done")) {
1234 /* Shallow related arguments */
1235 if (process_shallow(arg, &data->shallows))
1237 if (process_deepen(arg, &data->depth))
1239 if (process_deepen_since(arg, &data->deepen_since,
1240 &data->deepen_rev_list))
1242 if (process_deepen_not(arg, &data->deepen_not,
1243 &data->deepen_rev_list))
1245 if (!strcmp(arg, "deepen-relative")) {
1246 data->deepen_relative = 1;
1250 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1251 parse_list_objects_filter(&filter_options, p);
1255 /* ignore unknown lines maybe? */
1256 die("unexpected line: '%s'", arg);
1260 static int process_haves(struct oid_array *haves, struct oid_array *common,
1261 struct object_array *have_obj)
1266 for (i = 0; i < haves->nr; i++) {
1267 const struct object_id *oid = &haves->oid[i];
1269 int we_knew_they_have = 0;
1271 if (!has_object_file(oid))
1274 oid_array_append(common, oid);
1276 o = parse_object(the_repository, oid);
1278 die("oops (%s)", oid_to_hex(oid));
1279 if (o->type == OBJ_COMMIT) {
1280 struct commit_list *parents;
1281 struct commit *commit = (struct commit *)o;
1282 if (o->flags & THEY_HAVE)
1283 we_knew_they_have = 1;
1285 o->flags |= THEY_HAVE;
1286 if (!oldest_have || (commit->date < oldest_have))
1287 oldest_have = commit->date;
1288 for (parents = commit->parents;
1290 parents = parents->next)
1291 parents->item->object.flags |= THEY_HAVE;
1293 if (!we_knew_they_have)
1294 add_object_array(o, NULL, have_obj);
1300 static int send_acks(struct oid_array *acks, struct strbuf *response,
1301 const struct object_array *have_obj)
1305 packet_buf_write(response, "acknowledgments\n");
1309 packet_buf_write(response, "NAK\n");
1311 for (i = 0; i < acks->nr; i++) {
1312 packet_buf_write(response, "ACK %s\n",
1313 oid_to_hex(&acks->oid[i]));
1316 if (ok_to_give_up(have_obj)) {
1318 packet_buf_write(response, "ready\n");
1325 static int process_haves_and_send_acks(struct upload_pack_data *data,
1326 struct object_array *have_obj)
1328 struct oid_array common = OID_ARRAY_INIT;
1329 struct strbuf response = STRBUF_INIT;
1332 process_haves(&data->haves, &common, have_obj);
1335 } else if (send_acks(&common, &response, have_obj)) {
1336 packet_buf_delim(&response);
1340 packet_buf_flush(&response);
1345 write_or_die(1, response.buf, response.len);
1346 strbuf_release(&response);
1348 oid_array_clear(&data->haves);
1349 oid_array_clear(&common);
1353 static void send_wanted_ref_info(struct upload_pack_data *data)
1355 const struct string_list_item *item;
1357 if (!data->wanted_refs.nr)
1360 packet_write_fmt(1, "wanted-refs\n");
1362 for_each_string_list_item(item, &data->wanted_refs) {
1363 packet_write_fmt(1, "%s %s\n",
1364 oid_to_hex(item->util),
1371 static void send_shallow_info(struct upload_pack_data *data)
1373 /* No shallow info needs to be sent */
1374 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1375 !is_repository_shallow(the_repository))
1378 packet_write_fmt(1, "shallow-info\n");
1380 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1381 data->deepen_since, &data->deepen_not,
1383 is_repository_shallow(the_repository))
1384 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1390 FETCH_PROCESS_ARGS = 0,
1396 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1397 struct packet_reader *request)
1399 enum fetch_state state = FETCH_PROCESS_ARGS;
1400 struct upload_pack_data data;
1401 /* NEEDSWORK: make this non-static */
1402 static struct object_array have_obj;
1404 git_config(upload_pack_config, NULL);
1406 upload_pack_data_init(&data);
1407 use_sideband = LARGE_PACKET_MAX;
1409 while (state != FETCH_DONE) {
1411 case FETCH_PROCESS_ARGS:
1412 process_args(request, &data);
1416 * Request didn't contain any 'want' lines,
1417 * guess they didn't want anything.
1420 } else if (data.haves.nr) {
1422 * Request had 'have' lines, so lets ACK them.
1424 state = FETCH_SEND_ACKS;
1427 * Request had 'want's but no 'have's so we can
1428 * immedietly go to construct and send a pack.
1430 state = FETCH_SEND_PACK;
1433 case FETCH_SEND_ACKS:
1434 if (process_haves_and_send_acks(&data, &have_obj))
1435 state = FETCH_SEND_PACK;
1439 case FETCH_SEND_PACK:
1440 send_wanted_ref_info(&data);
1441 send_shallow_info(&data);
1443 packet_write_fmt(1, "packfile\n");
1444 create_pack_file(&have_obj);
1452 upload_pack_data_clear(&data);
1456 int upload_pack_advertise(struct repository *r,
1457 struct strbuf *value)
1460 int allow_filter_value;
1461 int allow_ref_in_want;
1463 strbuf_addstr(value, "shallow");
1465 if (!repo_config_get_bool(the_repository,
1466 "uploadpack.allowfilter",
1467 &allow_filter_value) &&
1469 strbuf_addstr(value, " filter");
1471 if (!repo_config_get_bool(the_repository,
1472 "uploadpack.allowrefinwant",
1473 &allow_ref_in_want) &&
1475 strbuf_addstr(value, " ref-in-want");