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 have_obj;
57 static struct object_array want_obj;
58 static struct object_array extra_edge_obj;
59 static unsigned int timeout;
60 static int keepalive = 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband;
65 static int stateless_rpc;
66 static const char *pack_objects_hook;
68 static int filter_capability_requested;
69 static int allow_filter;
70 static int allow_ref_in_want;
71 static struct list_objects_filter_options filter_options;
73 static void reset_timeout(void)
78 static void send_client_data(int fd, const char *data, ssize_t sz)
81 send_sideband(1, fd, data, sz, use_sideband);
88 /* XXX: are we happy to lose stuff here? */
92 write_or_die(fd, data, sz);
95 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
98 if (graft->nr_parent == -1)
99 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
103 static void create_pack_file(void)
105 struct child_process pack_objects = CHILD_PROCESS_INIT;
106 char data[8193], progress[128];
107 char abort_msg[] = "aborting due to possible repository "
108 "corruption on the remote side.";
114 if (!pack_objects_hook)
115 pack_objects.git_cmd = 1;
117 argv_array_push(&pack_objects.args, pack_objects_hook);
118 argv_array_push(&pack_objects.args, "git");
119 pack_objects.use_shell = 1;
123 argv_array_push(&pack_objects.args, "--shallow-file");
124 argv_array_push(&pack_objects.args, "");
126 argv_array_push(&pack_objects.args, "pack-objects");
127 argv_array_push(&pack_objects.args, "--revs");
129 argv_array_push(&pack_objects.args, "--thin");
131 argv_array_push(&pack_objects.args, "--stdout");
133 argv_array_push(&pack_objects.args, "--shallow");
135 argv_array_push(&pack_objects.args, "--progress");
137 argv_array_push(&pack_objects.args, "--delta-base-offset");
139 argv_array_push(&pack_objects.args, "--include-tag");
140 if (filter_options.filter_spec) {
141 if (pack_objects.use_shell) {
142 struct strbuf buf = STRBUF_INIT;
143 sq_quote_buf(&buf, filter_options.filter_spec);
144 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
145 strbuf_release(&buf);
147 argv_array_pushf(&pack_objects.args, "--filter=%s",
148 filter_options.filter_spec);
152 pack_objects.in = -1;
153 pack_objects.out = -1;
154 pack_objects.err = -1;
156 if (start_command(&pack_objects))
157 die("git upload-pack: unable to fork git-pack-objects");
159 pipe_fd = xfdopen(pack_objects.in, "w");
162 for_each_commit_graft(write_one_shallow, pipe_fd);
164 for (i = 0; i < want_obj.nr; i++)
165 fprintf(pipe_fd, "%s\n",
166 oid_to_hex(&want_obj.objects[i].item->oid));
167 fprintf(pipe_fd, "--not\n");
168 for (i = 0; i < have_obj.nr; i++)
169 fprintf(pipe_fd, "%s\n",
170 oid_to_hex(&have_obj.objects[i].item->oid));
171 for (i = 0; i < extra_edge_obj.nr; i++)
172 fprintf(pipe_fd, "%s\n",
173 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
174 fprintf(pipe_fd, "\n");
178 /* We read from pack_objects.err to capture stderr output for
179 * progress bar, and pack_objects.out to capture the pack data.
183 struct pollfd pfd[2];
184 int pe, pu, pollsize;
192 if (0 <= pack_objects.out) {
193 pfd[pollsize].fd = pack_objects.out;
194 pfd[pollsize].events = POLLIN;
198 if (0 <= pack_objects.err) {
199 pfd[pollsize].fd = pack_objects.err;
200 pfd[pollsize].events = POLLIN;
208 ret = poll(pfd, pollsize,
209 keepalive < 0 ? -1 : 1000 * keepalive);
212 if (errno != EINTR) {
213 error_errno("poll failed, resuming");
218 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
219 /* Status ready; we ship that in the side-band
220 * or dump to the standard error.
222 sz = xread(pack_objects.err, progress,
225 send_client_data(2, progress, sz);
227 close(pack_objects.err);
228 pack_objects.err = -1;
232 /* give priority to status messages */
235 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
236 /* Data ready; we keep the last byte to ourselves
237 * in case we detect broken rev-list, so that we
238 * can leave the stream corrupted. This is
239 * unfortunate -- unpack-objects would happily
240 * accept a valid packdata with trailing garbage,
241 * so appending garbage after we pass all the
242 * pack data is not good enough to signal
243 * breakage to downstream.
251 sz = xread(pack_objects.out, cp,
252 sizeof(data) - outsz);
256 close(pack_objects.out);
257 pack_objects.out = -1;
263 buffered = data[sz-1] & 0xFF;
268 send_client_data(1, data, sz);
272 * We hit the keepalive timeout without saying anything; send
273 * an empty message on the data sideband just to let the other
274 * side know we're still working on it, but don't have any data
277 * If we don't have a sideband channel, there's no room in the
278 * protocol to say anything, so those clients are just out of
281 if (!ret && use_sideband) {
282 static const char buf[] = "0005\1";
283 write_or_die(1, buf, 5);
287 if (finish_command(&pack_objects)) {
288 error("git upload-pack: git-pack-objects died with error.");
295 send_client_data(1, data, 1);
296 fprintf(stderr, "flushed.\n");
303 send_client_data(3, abort_msg, sizeof(abort_msg));
304 die("git upload-pack: %s", abort_msg);
307 static int got_oid(const char *hex, struct object_id *oid)
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(void)
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(void)
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()) {
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)) {
390 case -1: /* they have what we do not */
392 if (multi_ack && ok_to_give_up()) {
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 */
447 const unsigned hexsz = the_hash_algo->hexsz;
456 * If the next rev-list --stdin encounters an unknown commit,
457 * it terminates, which will cause SIGPIPE in the write loop
460 sigchain_push(SIGPIPE, SIG_IGN);
462 if (start_command(cmd))
466 namebuf[hexsz + 1] = '\n';
467 for (i = get_max_object_index(); 0 < i; ) {
468 o = get_indexed_object(--i);
471 if (reachable && o->type == OBJ_COMMIT)
472 o->flags &= ~TMP_MARK;
475 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
476 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
479 namebuf[hexsz] = '\n';
480 for (i = 0; i < src->nr; i++) {
481 o = src->objects[i].item;
484 add_object_array(o, NULL, reachable);
487 if (reachable && o->type == OBJ_COMMIT)
488 o->flags |= TMP_MARK;
489 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
490 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
495 sigchain_pop(SIGPIPE);
500 sigchain_pop(SIGPIPE);
509 static int get_reachable_list(struct object_array *src,
510 struct object_array *reachable)
512 struct child_process cmd = CHILD_PROCESS_INIT;
515 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
516 const unsigned hexsz = the_hash_algo->hexsz;
518 if (do_reachable_revlist(&cmd, src, reachable) < 0)
521 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
522 struct object_id sha1;
525 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
528 o = lookup_object(the_repository, sha1.hash);
529 if (o && o->type == OBJ_COMMIT) {
530 o->flags &= ~TMP_MARK;
533 for (i = get_max_object_index(); 0 < i; i--) {
534 o = get_indexed_object(i - 1);
535 if (o && o->type == OBJ_COMMIT &&
536 (o->flags & TMP_MARK)) {
537 add_object_array(o, NULL, reachable);
538 o->flags &= ~TMP_MARK;
543 if (finish_command(&cmd))
549 static int has_unreachable(struct object_array *src)
551 struct child_process cmd = CHILD_PROCESS_INIT;
555 if (do_reachable_revlist(&cmd, src, NULL) < 0)
559 * The commits out of the rev-list are not ancestors of
562 i = read_in_full(cmd.out, buf, 1);
569 * rev-list may have died by encountering a bad commit
570 * in the history, in which case we do want to bail out
571 * even when it showed no commit.
573 if (finish_command(&cmd))
576 /* All the non-tip ones are ancestors of what we advertised */
580 sigchain_pop(SIGPIPE);
586 static void check_non_tip(void)
591 * In the normal in-process case without
592 * uploadpack.allowReachableSHA1InWant,
593 * non-tip requests can never happen.
595 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
597 if (!has_unreachable(&want_obj))
598 /* All the non-tip ones are ancestors of what we advertised */
602 /* Pick one of them (we know there at least is one) */
603 for (i = 0; i < want_obj.nr; i++) {
604 struct object *o = want_obj.objects[i].item;
606 die("git upload-pack: not our ref %s",
607 oid_to_hex(&o->oid));
611 static void send_shallow(struct commit_list *result)
614 struct object *object = &result->item->object;
615 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
616 packet_write_fmt(1, "shallow %s",
617 oid_to_hex(&object->oid));
618 register_shallow(the_repository, &object->oid);
621 result = result->next;
625 static void send_unshallow(const struct object_array *shallows)
629 for (i = 0; i < shallows->nr; i++) {
630 struct object *object = shallows->objects[i].item;
631 if (object->flags & NOT_SHALLOW) {
632 struct commit_list *parents;
633 packet_write_fmt(1, "unshallow %s",
634 oid_to_hex(&object->oid));
635 object->flags &= ~CLIENT_SHALLOW;
637 * We want to _register_ "object" as shallow, but we
638 * also need to traverse object's parents to deepen a
639 * shallow clone. Unregister it for now so we can
640 * parse and add the parents to the want list, then
643 unregister_shallow(&object->oid);
645 parse_commit_or_die((struct commit *)object);
646 parents = ((struct commit *)object)->parents;
648 add_object_array(&parents->item->object,
650 parents = parents->next;
652 add_object_array(object, NULL, &extra_edge_obj);
654 /* make sure commit traversal conforms to client */
655 register_shallow(the_repository, &object->oid);
659 static void deepen(int depth, int deepen_relative,
660 struct object_array *shallows)
662 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
665 for (i = 0; i < shallows->nr; i++) {
666 struct object *object = shallows->objects[i].item;
667 object->flags |= NOT_SHALLOW;
669 } else if (deepen_relative) {
670 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
671 struct commit_list *result;
673 get_reachable_list(shallows, &reachable_shallows);
674 result = get_shallow_commits(&reachable_shallows,
676 SHALLOW, NOT_SHALLOW);
677 send_shallow(result);
678 free_commit_list(result);
679 object_array_clear(&reachable_shallows);
681 struct commit_list *result;
683 result = get_shallow_commits(&want_obj, depth,
684 SHALLOW, NOT_SHALLOW);
685 send_shallow(result);
686 free_commit_list(result);
689 send_unshallow(shallows);
692 static void deepen_by_rev_list(int ac, const char **av,
693 struct object_array *shallows)
695 struct commit_list *result;
697 close_commit_graph(the_repository);
698 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
699 send_shallow(result);
700 free_commit_list(result);
701 send_unshallow(shallows);
704 /* Returns 1 if a shallow list is sent or 0 otherwise */
705 static int send_shallow_list(int depth, int deepen_rev_list,
706 timestamp_t deepen_since,
707 struct string_list *deepen_not,
708 struct object_array *shallows)
712 if (depth > 0 && deepen_rev_list)
713 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
715 deepen(depth, deepen_relative, shallows);
717 } else if (deepen_rev_list) {
718 struct argv_array av = ARGV_ARRAY_INIT;
721 argv_array_push(&av, "rev-list");
723 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
724 if (deepen_not->nr) {
725 argv_array_push(&av, "--not");
726 for (i = 0; i < deepen_not->nr; i++) {
727 struct string_list_item *s = deepen_not->items + i;
728 argv_array_push(&av, s->string);
730 argv_array_push(&av, "--not");
732 for (i = 0; i < want_obj.nr; i++) {
733 struct object *o = want_obj.objects[i].item;
734 argv_array_push(&av, oid_to_hex(&o->oid));
736 deepen_by_rev_list(av.argc, av.argv, shallows);
737 argv_array_clear(&av);
740 if (shallows->nr > 0) {
742 for (i = 0; i < shallows->nr; i++)
743 register_shallow(the_repository,
744 &shallows->objects[i].item->oid);
748 shallow_nr += shallows->nr;
752 static int process_shallow(const char *line, struct object_array *shallows)
755 if (skip_prefix(line, "shallow ", &arg)) {
756 struct object_id oid;
757 struct object *object;
758 if (get_oid_hex(arg, &oid))
759 die("invalid shallow line: %s", line);
760 object = parse_object(the_repository, &oid);
763 if (object->type != OBJ_COMMIT)
764 die("invalid shallow object %s", oid_to_hex(&oid));
765 if (!(object->flags & CLIENT_SHALLOW)) {
766 object->flags |= CLIENT_SHALLOW;
767 add_object_array(object, NULL, shallows);
775 static int process_deepen(const char *line, int *depth)
778 if (skip_prefix(line, "deepen ", &arg)) {
780 *depth = (int)strtol(arg, &end, 0);
781 if (!end || *end || *depth <= 0)
782 die("Invalid deepen: %s", line);
789 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
792 if (skip_prefix(line, "deepen-since ", &arg)) {
794 *deepen_since = parse_timestamp(arg, &end, 0);
795 if (!end || *end || !deepen_since ||
796 /* revisions.c's max_age -1 is special */
798 die("Invalid deepen-since: %s", line);
799 *deepen_rev_list = 1;
805 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
808 if (skip_prefix(line, "deepen-not ", &arg)) {
810 struct object_id oid;
811 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
812 die("git upload-pack: ambiguous deepen-not: %s", line);
813 string_list_append(deepen_not, ref);
815 *deepen_rev_list = 1;
821 static void receive_needs(void)
823 struct object_array shallows = OBJECT_ARRAY_INIT;
824 struct string_list deepen_not = STRING_LIST_INIT_DUP;
827 timestamp_t deepen_since = 0;
828 int deepen_rev_list = 0;
833 const char *features;
834 struct object_id oid_buf;
835 char *line = packet_read_line(0, NULL);
842 if (process_shallow(line, &shallows))
844 if (process_deepen(line, &depth))
846 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
848 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
851 if (skip_prefix(line, "filter ", &arg)) {
852 if (!filter_capability_requested)
853 die("git upload-pack: filtering capability not negotiated");
854 parse_list_objects_filter(&filter_options, arg);
858 if (!skip_prefix(line, "want ", &arg) ||
859 parse_oid_hex(arg, &oid_buf, &features))
860 die("git upload-pack: protocol error, "
861 "expected to get object ID, not '%s'", line);
863 if (parse_feature_request(features, "deepen-relative"))
865 if (parse_feature_request(features, "multi_ack_detailed"))
867 else if (parse_feature_request(features, "multi_ack"))
869 if (parse_feature_request(features, "no-done"))
871 if (parse_feature_request(features, "thin-pack"))
873 if (parse_feature_request(features, "ofs-delta"))
875 if (parse_feature_request(features, "side-band-64k"))
876 use_sideband = LARGE_PACKET_MAX;
877 else if (parse_feature_request(features, "side-band"))
878 use_sideband = DEFAULT_PACKET_MAX;
879 if (parse_feature_request(features, "no-progress"))
881 if (parse_feature_request(features, "include-tag"))
883 if (allow_filter && parse_feature_request(features, "filter"))
884 filter_capability_requested = 1;
886 o = parse_object(the_repository, &oid_buf);
889 "ERR upload-pack: not our ref %s",
890 oid_to_hex(&oid_buf));
891 die("git upload-pack: not our ref %s",
892 oid_to_hex(&oid_buf));
894 if (!(o->flags & WANTED)) {
896 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
899 add_object_array(o, NULL, &want_obj);
904 * We have sent all our refs already, and the other end
905 * should have chosen out of them. When we are operating
906 * in the stateless RPC mode, however, their choice may
907 * have been based on the set of older refs advertised
908 * by another process that handled the initial request.
913 if (!use_sideband && daemon_mode)
916 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
919 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
920 &deepen_not, &shallows))
922 object_array_clear(&shallows);
925 /* return non-zero if the ref is hidden, otherwise 0 */
926 static int mark_our_ref(const char *refname, const char *refname_full,
927 const struct object_id *oid)
929 struct object *o = lookup_unknown_object(oid->hash);
931 if (ref_is_hidden(refname, refname_full)) {
932 o->flags |= HIDDEN_REF;
939 static int check_ref(const char *refname_full, const struct object_id *oid,
940 int flag, void *cb_data)
942 const char *refname = strip_namespace(refname_full);
944 mark_our_ref(refname, refname_full, oid);
948 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
950 struct string_list_item *item;
954 for_each_string_list_item(item, symref)
955 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
958 static int send_ref(const char *refname, const struct object_id *oid,
959 int flag, void *cb_data)
961 static const char *capabilities = "multi_ack thin-pack side-band"
962 " side-band-64k ofs-delta shallow deepen-since deepen-not"
963 " deepen-relative no-progress include-tag multi_ack_detailed";
964 const char *refname_nons = strip_namespace(refname);
965 struct object_id peeled;
967 if (mark_our_ref(refname_nons, refname, oid))
971 struct strbuf symref_info = STRBUF_INIT;
973 format_symref_info(&symref_info, cb_data);
974 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
975 oid_to_hex(oid), refname_nons,
977 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
978 " allow-tip-sha1-in-want" : "",
979 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
980 " allow-reachable-sha1-in-want" : "",
981 stateless_rpc ? " no-done" : "",
983 allow_filter ? " filter" : "",
984 git_user_agent_sanitized());
985 strbuf_release(&symref_info);
987 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
990 if (!peel_ref(refname, &peeled))
991 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
995 static int find_symref(const char *refname, const struct object_id *oid,
996 int flag, void *cb_data)
998 const char *symref_target;
999 struct string_list_item *item;
1001 if ((flag & REF_ISSYMREF) == 0)
1003 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1004 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1005 die("'%s' is a symref but it is not?", refname);
1006 item = string_list_append(cb_data, refname);
1007 item->util = xstrdup(symref_target);
1011 static int upload_pack_config(const char *var, const char *value, void *unused)
1013 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1014 if (git_config_bool(var, value))
1015 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1017 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1018 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1019 if (git_config_bool(var, value))
1020 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1022 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1023 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1024 if (git_config_bool(var, value))
1025 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1027 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1028 } else if (!strcmp("uploadpack.keepalive", var)) {
1029 keepalive = git_config_int(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);
1038 if (current_config_scope() != CONFIG_SCOPE_REPO) {
1039 if (!strcmp("uploadpack.packobjectshook", var))
1040 return git_config_string(&pack_objects_hook, var, value);
1043 return parse_hide_refs_config(var, value, "uploadpack");
1046 void upload_pack(struct upload_pack_options *options)
1048 struct string_list symref = STRING_LIST_INIT_DUP;
1050 stateless_rpc = options->stateless_rpc;
1051 timeout = options->timeout;
1052 daemon_mode = options->daemon_mode;
1054 git_config(upload_pack_config, NULL);
1056 head_ref_namespaced(find_symref, &symref);
1058 if (options->advertise_refs || !stateless_rpc) {
1060 head_ref_namespaced(send_ref, &symref);
1061 for_each_namespaced_ref(send_ref, &symref);
1062 advertise_shallow_grafts(1);
1065 head_ref_namespaced(check_ref, NULL);
1066 for_each_namespaced_ref(check_ref, NULL);
1068 string_list_clear(&symref, 1);
1069 if (options->advertise_refs)
1074 get_common_commits();
1079 struct upload_pack_data {
1080 struct object_array wants;
1081 struct string_list wanted_refs;
1082 struct oid_array haves;
1084 struct object_array shallows;
1085 struct string_list deepen_not;
1087 timestamp_t deepen_since;
1088 int deepen_rev_list;
1089 int deepen_relative;
1091 unsigned stateless_rpc : 1;
1093 unsigned use_thin_pack : 1;
1094 unsigned use_ofs_delta : 1;
1095 unsigned no_progress : 1;
1096 unsigned use_include_tag : 1;
1100 static void upload_pack_data_init(struct upload_pack_data *data)
1102 struct object_array wants = OBJECT_ARRAY_INIT;
1103 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1104 struct oid_array haves = OID_ARRAY_INIT;
1105 struct object_array shallows = OBJECT_ARRAY_INIT;
1106 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1108 memset(data, 0, sizeof(*data));
1109 data->wants = wants;
1110 data->wanted_refs = wanted_refs;
1111 data->haves = haves;
1112 data->shallows = shallows;
1113 data->deepen_not = deepen_not;
1116 static void upload_pack_data_clear(struct upload_pack_data *data)
1118 object_array_clear(&data->wants);
1119 string_list_clear(&data->wanted_refs, 1);
1120 oid_array_clear(&data->haves);
1121 object_array_clear(&data->shallows);
1122 string_list_clear(&data->deepen_not, 0);
1125 static int parse_want(const char *line)
1128 if (skip_prefix(line, "want ", &arg)) {
1129 struct object_id oid;
1132 if (get_oid_hex(arg, &oid))
1133 die("git upload-pack: protocol error, "
1134 "expected to get oid, not '%s'", line);
1136 o = parse_object(the_repository, &oid);
1139 "ERR upload-pack: not our ref %s",
1141 die("git upload-pack: not our ref %s",
1145 if (!(o->flags & WANTED)) {
1147 add_object_array(o, NULL, &want_obj);
1156 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1159 if (skip_prefix(line, "want-ref ", &arg)) {
1160 struct object_id oid;
1161 struct string_list_item *item;
1164 if (read_ref(arg, &oid)) {
1165 packet_write_fmt(1, "ERR unknown ref %s", arg);
1166 die("unknown ref %s", arg);
1169 item = string_list_append(wanted_refs, arg);
1170 item->util = oiddup(&oid);
1172 o = parse_object_or_die(&oid, arg);
1173 if (!(o->flags & WANTED)) {
1175 add_object_array(o, NULL, &want_obj);
1184 static int parse_have(const char *line, struct oid_array *haves)
1187 if (skip_prefix(line, "have ", &arg)) {
1188 struct object_id oid;
1190 if (get_oid_hex(arg, &oid))
1191 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1192 oid_array_append(haves, &oid);
1199 static void process_args(struct packet_reader *request,
1200 struct upload_pack_data *data)
1202 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1203 const char *arg = request->line;
1207 if (parse_want(arg))
1209 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1211 /* process have line */
1212 if (parse_have(arg, &data->haves))
1215 /* process args like thin-pack */
1216 if (!strcmp(arg, "thin-pack")) {
1220 if (!strcmp(arg, "ofs-delta")) {
1224 if (!strcmp(arg, "no-progress")) {
1228 if (!strcmp(arg, "include-tag")) {
1229 use_include_tag = 1;
1232 if (!strcmp(arg, "done")) {
1237 /* Shallow related arguments */
1238 if (process_shallow(arg, &data->shallows))
1240 if (process_deepen(arg, &data->depth))
1242 if (process_deepen_since(arg, &data->deepen_since,
1243 &data->deepen_rev_list))
1245 if (process_deepen_not(arg, &data->deepen_not,
1246 &data->deepen_rev_list))
1248 if (!strcmp(arg, "deepen-relative")) {
1249 data->deepen_relative = 1;
1253 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1254 parse_list_objects_filter(&filter_options, p);
1258 /* ignore unknown lines maybe? */
1259 die("unexpected line: '%s'", arg);
1263 static int process_haves(struct oid_array *haves, struct oid_array *common)
1268 for (i = 0; i < haves->nr; i++) {
1269 const struct object_id *oid = &haves->oid[i];
1271 int we_knew_they_have = 0;
1273 if (!has_object_file(oid))
1276 oid_array_append(common, oid);
1278 o = parse_object(the_repository, oid);
1280 die("oops (%s)", oid_to_hex(oid));
1281 if (o->type == OBJ_COMMIT) {
1282 struct commit_list *parents;
1283 struct commit *commit = (struct commit *)o;
1284 if (o->flags & THEY_HAVE)
1285 we_knew_they_have = 1;
1287 o->flags |= THEY_HAVE;
1288 if (!oldest_have || (commit->date < oldest_have))
1289 oldest_have = commit->date;
1290 for (parents = commit->parents;
1292 parents = parents->next)
1293 parents->item->object.flags |= THEY_HAVE;
1295 if (!we_knew_they_have)
1296 add_object_array(o, NULL, &have_obj);
1302 static int send_acks(struct oid_array *acks, struct strbuf *response)
1306 packet_buf_write(response, "acknowledgments\n");
1310 packet_buf_write(response, "NAK\n");
1312 for (i = 0; i < acks->nr; i++) {
1313 packet_buf_write(response, "ACK %s\n",
1314 oid_to_hex(&acks->oid[i]));
1317 if (ok_to_give_up()) {
1319 packet_buf_write(response, "ready\n");
1326 static int process_haves_and_send_acks(struct upload_pack_data *data)
1328 struct oid_array common = OID_ARRAY_INIT;
1329 struct strbuf response = STRBUF_INIT;
1332 process_haves(&data->haves, &common);
1335 } else if (send_acks(&common, &response)) {
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;
1402 git_config(upload_pack_config, NULL);
1404 upload_pack_data_init(&data);
1405 use_sideband = LARGE_PACKET_MAX;
1407 while (state != FETCH_DONE) {
1409 case FETCH_PROCESS_ARGS:
1410 process_args(request, &data);
1414 * Request didn't contain any 'want' lines,
1415 * guess they didn't want anything.
1418 } else if (data.haves.nr) {
1420 * Request had 'have' lines, so lets ACK them.
1422 state = FETCH_SEND_ACKS;
1425 * Request had 'want's but no 'have's so we can
1426 * immedietly go to construct and send a pack.
1428 state = FETCH_SEND_PACK;
1431 case FETCH_SEND_ACKS:
1432 if (process_haves_and_send_acks(&data))
1433 state = FETCH_SEND_PACK;
1437 case FETCH_SEND_PACK:
1438 send_wanted_ref_info(&data);
1439 send_shallow_info(&data);
1441 packet_write_fmt(1, "packfile\n");
1450 upload_pack_data_clear(&data);
1454 int upload_pack_advertise(struct repository *r,
1455 struct strbuf *value)
1458 int allow_filter_value;
1459 int allow_ref_in_want;
1461 strbuf_addstr(value, "shallow");
1463 if (!repo_config_get_bool(the_repository,
1464 "uploadpack.allowfilter",
1465 &allow_filter_value) &&
1467 strbuf_addstr(value, " filter");
1469 if (!repo_config_get_bool(the_repository,
1470 "uploadpack.allowrefinwant",
1471 &allow_ref_in_want) &&
1473 strbuf_addstr(value, " ref-in-want");