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 struct list_objects_filter_options filter_options;
71 static void reset_timeout(void)
76 static void send_client_data(int fd, const char *data, ssize_t sz)
79 send_sideband(1, fd, data, sz, use_sideband);
86 /* XXX: are we happy to lose stuff here? */
90 write_or_die(fd, data, sz);
93 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
96 if (graft->nr_parent == -1)
97 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
101 static void create_pack_file(void)
103 struct child_process pack_objects = CHILD_PROCESS_INIT;
104 char data[8193], progress[128];
105 char abort_msg[] = "aborting due to possible repository "
106 "corruption on the remote side.";
112 if (!pack_objects_hook)
113 pack_objects.git_cmd = 1;
115 argv_array_push(&pack_objects.args, pack_objects_hook);
116 argv_array_push(&pack_objects.args, "git");
117 pack_objects.use_shell = 1;
121 argv_array_push(&pack_objects.args, "--shallow-file");
122 argv_array_push(&pack_objects.args, "");
124 argv_array_push(&pack_objects.args, "pack-objects");
125 argv_array_push(&pack_objects.args, "--revs");
127 argv_array_push(&pack_objects.args, "--thin");
129 argv_array_push(&pack_objects.args, "--stdout");
131 argv_array_push(&pack_objects.args, "--shallow");
133 argv_array_push(&pack_objects.args, "--progress");
135 argv_array_push(&pack_objects.args, "--delta-base-offset");
137 argv_array_push(&pack_objects.args, "--include-tag");
138 if (filter_options.filter_spec) {
139 if (pack_objects.use_shell) {
140 struct strbuf buf = STRBUF_INIT;
141 sq_quote_buf(&buf, filter_options.filter_spec);
142 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
143 strbuf_release(&buf);
145 argv_array_pushf(&pack_objects.args, "--filter=%s",
146 filter_options.filter_spec);
150 pack_objects.in = -1;
151 pack_objects.out = -1;
152 pack_objects.err = -1;
154 if (start_command(&pack_objects))
155 die("git upload-pack: unable to fork git-pack-objects");
157 pipe_fd = xfdopen(pack_objects.in, "w");
160 for_each_commit_graft(write_one_shallow, pipe_fd);
162 for (i = 0; i < want_obj.nr; i++)
163 fprintf(pipe_fd, "%s\n",
164 oid_to_hex(&want_obj.objects[i].item->oid));
165 fprintf(pipe_fd, "--not\n");
166 for (i = 0; i < have_obj.nr; i++)
167 fprintf(pipe_fd, "%s\n",
168 oid_to_hex(&have_obj.objects[i].item->oid));
169 for (i = 0; i < extra_edge_obj.nr; i++)
170 fprintf(pipe_fd, "%s\n",
171 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
172 fprintf(pipe_fd, "\n");
176 /* We read from pack_objects.err to capture stderr output for
177 * progress bar, and pack_objects.out to capture the pack data.
181 struct pollfd pfd[2];
182 int pe, pu, pollsize;
190 if (0 <= pack_objects.out) {
191 pfd[pollsize].fd = pack_objects.out;
192 pfd[pollsize].events = POLLIN;
196 if (0 <= pack_objects.err) {
197 pfd[pollsize].fd = pack_objects.err;
198 pfd[pollsize].events = POLLIN;
206 ret = poll(pfd, pollsize,
207 keepalive < 0 ? -1 : 1000 * keepalive);
210 if (errno != EINTR) {
211 error_errno("poll failed, resuming");
216 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
217 /* Status ready; we ship that in the side-band
218 * or dump to the standard error.
220 sz = xread(pack_objects.err, progress,
223 send_client_data(2, progress, sz);
225 close(pack_objects.err);
226 pack_objects.err = -1;
230 /* give priority to status messages */
233 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
234 /* Data ready; we keep the last byte to ourselves
235 * in case we detect broken rev-list, so that we
236 * can leave the stream corrupted. This is
237 * unfortunate -- unpack-objects would happily
238 * accept a valid packdata with trailing garbage,
239 * so appending garbage after we pass all the
240 * pack data is not good enough to signal
241 * breakage to downstream.
249 sz = xread(pack_objects.out, cp,
250 sizeof(data) - outsz);
254 close(pack_objects.out);
255 pack_objects.out = -1;
261 buffered = data[sz-1] & 0xFF;
266 send_client_data(1, data, sz);
270 * We hit the keepalive timeout without saying anything; send
271 * an empty message on the data sideband just to let the other
272 * side know we're still working on it, but don't have any data
275 * If we don't have a sideband channel, there's no room in the
276 * protocol to say anything, so those clients are just out of
279 if (!ret && use_sideband) {
280 static const char buf[] = "0005\1";
281 write_or_die(1, buf, 5);
285 if (finish_command(&pack_objects)) {
286 error("git upload-pack: git-pack-objects died with error.");
293 send_client_data(1, data, 1);
294 fprintf(stderr, "flushed.\n");
301 send_client_data(3, abort_msg, sizeof(abort_msg));
302 die("git upload-pack: %s", abort_msg);
305 static int got_oid(const char *hex, struct object_id *oid)
308 int we_knew_they_have = 0;
310 if (get_oid_hex(hex, oid))
311 die("git upload-pack: expected SHA1 object, got '%s'", hex);
312 if (!has_object_file(oid))
315 o = parse_object(the_repository, oid);
317 die("oops (%s)", oid_to_hex(oid));
318 if (o->type == OBJ_COMMIT) {
319 struct commit_list *parents;
320 struct commit *commit = (struct commit *)o;
321 if (o->flags & THEY_HAVE)
322 we_knew_they_have = 1;
324 o->flags |= THEY_HAVE;
325 if (!oldest_have || (commit->date < oldest_have))
326 oldest_have = commit->date;
327 for (parents = commit->parents;
329 parents = parents->next)
330 parents->item->object.flags |= THEY_HAVE;
332 if (!we_knew_they_have) {
333 add_object_array(o, NULL, &have_obj);
339 static int ok_to_give_up(void)
341 uint32_t min_generation = GENERATION_NUMBER_ZERO;
346 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
347 COMMON_KNOWN, oldest_have,
351 static int get_common_commits(void)
353 struct object_id oid;
354 char last_hex[GIT_MAX_HEXSZ + 1];
359 save_commit_buffer = 0;
362 char *line = packet_read_line(0, NULL);
368 if (multi_ack == 2 && got_common
369 && !got_other && ok_to_give_up()) {
371 packet_write_fmt(1, "ACK %s ready\n", last_hex);
373 if (have_obj.nr == 0 || multi_ack)
374 packet_write_fmt(1, "NAK\n");
376 if (no_done && sent_ready) {
377 packet_write_fmt(1, "ACK %s\n", last_hex);
386 if (skip_prefix(line, "have ", &arg)) {
387 switch (got_oid(arg, &oid)) {
388 case -1: /* they have what we do not */
390 if (multi_ack && ok_to_give_up()) {
391 const char *hex = oid_to_hex(&oid);
392 if (multi_ack == 2) {
394 packet_write_fmt(1, "ACK %s ready\n", hex);
396 packet_write_fmt(1, "ACK %s continue\n", hex);
401 oid_to_hex_r(last_hex, &oid);
403 packet_write_fmt(1, "ACK %s common\n", last_hex);
405 packet_write_fmt(1, "ACK %s continue\n", last_hex);
406 else if (have_obj.nr == 1)
407 packet_write_fmt(1, "ACK %s\n", last_hex);
412 if (!strcmp(line, "done")) {
413 if (have_obj.nr > 0) {
415 packet_write_fmt(1, "ACK %s\n", last_hex);
418 packet_write_fmt(1, "NAK\n");
421 die("git upload-pack: expected SHA1 list, got '%s'", line);
425 static int is_our_ref(struct object *o)
427 int allow_hidden_ref = (allow_unadvertised_object_request &
428 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
429 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
433 * on successful case, it's up to the caller to close cmd->out
435 static int do_reachable_revlist(struct child_process *cmd,
436 struct object_array *src,
437 struct object_array *reachable)
439 static const char *argv[] = {
440 "rev-list", "--stdin", NULL,
443 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
453 * If the next rev-list --stdin encounters an unknown commit,
454 * it terminates, which will cause SIGPIPE in the write loop
457 sigchain_push(SIGPIPE, SIG_IGN);
459 if (start_command(cmd))
463 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
464 for (i = get_max_object_index(); 0 < i; ) {
465 o = get_indexed_object(--i);
468 if (reachable && o->type == OBJ_COMMIT)
469 o->flags &= ~TMP_MARK;
472 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
473 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
476 namebuf[GIT_SHA1_HEXSZ] = '\n';
477 for (i = 0; i < src->nr; i++) {
478 o = src->objects[i].item;
481 add_object_array(o, NULL, reachable);
484 if (reachable && o->type == OBJ_COMMIT)
485 o->flags |= TMP_MARK;
486 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
487 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
492 sigchain_pop(SIGPIPE);
497 sigchain_pop(SIGPIPE);
506 static int get_reachable_list(struct object_array *src,
507 struct object_array *reachable)
509 struct child_process cmd = CHILD_PROCESS_INIT;
512 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
513 const unsigned hexsz = the_hash_algo->hexsz;
515 if (do_reachable_revlist(&cmd, src, reachable) < 0)
518 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
519 struct object_id sha1;
522 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
525 o = lookup_object(the_repository, sha1.hash);
526 if (o && o->type == OBJ_COMMIT) {
527 o->flags &= ~TMP_MARK;
530 for (i = get_max_object_index(); 0 < i; i--) {
531 o = get_indexed_object(i - 1);
532 if (o && o->type == OBJ_COMMIT &&
533 (o->flags & TMP_MARK)) {
534 add_object_array(o, NULL, reachable);
535 o->flags &= ~TMP_MARK;
540 if (finish_command(&cmd))
546 static int has_unreachable(struct object_array *src)
548 struct child_process cmd = CHILD_PROCESS_INIT;
552 if (do_reachable_revlist(&cmd, src, NULL) < 0)
556 * The commits out of the rev-list are not ancestors of
559 i = read_in_full(cmd.out, buf, 1);
566 * rev-list may have died by encountering a bad commit
567 * in the history, in which case we do want to bail out
568 * even when it showed no commit.
570 if (finish_command(&cmd))
573 /* All the non-tip ones are ancestors of what we advertised */
577 sigchain_pop(SIGPIPE);
583 static void check_non_tip(void)
588 * In the normal in-process case without
589 * uploadpack.allowReachableSHA1InWant,
590 * non-tip requests can never happen.
592 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
594 if (!has_unreachable(&want_obj))
595 /* All the non-tip ones are ancestors of what we advertised */
599 /* Pick one of them (we know there at least is one) */
600 for (i = 0; i < want_obj.nr; i++) {
601 struct object *o = want_obj.objects[i].item;
603 die("git upload-pack: not our ref %s",
604 oid_to_hex(&o->oid));
608 static void send_shallow(struct commit_list *result)
611 struct object *object = &result->item->object;
612 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
613 packet_write_fmt(1, "shallow %s",
614 oid_to_hex(&object->oid));
615 register_shallow(the_repository, &object->oid);
618 result = result->next;
622 static void send_unshallow(const struct object_array *shallows)
626 for (i = 0; i < shallows->nr; i++) {
627 struct object *object = shallows->objects[i].item;
628 if (object->flags & NOT_SHALLOW) {
629 struct commit_list *parents;
630 packet_write_fmt(1, "unshallow %s",
631 oid_to_hex(&object->oid));
632 object->flags &= ~CLIENT_SHALLOW;
634 * We want to _register_ "object" as shallow, but we
635 * also need to traverse object's parents to deepen a
636 * shallow clone. Unregister it for now so we can
637 * parse and add the parents to the want list, then
640 unregister_shallow(&object->oid);
642 parse_commit_or_die((struct commit *)object);
643 parents = ((struct commit *)object)->parents;
645 add_object_array(&parents->item->object,
647 parents = parents->next;
649 add_object_array(object, NULL, &extra_edge_obj);
651 /* make sure commit traversal conforms to client */
652 register_shallow(the_repository, &object->oid);
656 static void deepen(int depth, int deepen_relative,
657 struct object_array *shallows)
659 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
662 for (i = 0; i < shallows->nr; i++) {
663 struct object *object = shallows->objects[i].item;
664 object->flags |= NOT_SHALLOW;
666 } else if (deepen_relative) {
667 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
668 struct commit_list *result;
670 get_reachable_list(shallows, &reachable_shallows);
671 result = get_shallow_commits(&reachable_shallows,
673 SHALLOW, NOT_SHALLOW);
674 send_shallow(result);
675 free_commit_list(result);
676 object_array_clear(&reachable_shallows);
678 struct commit_list *result;
680 result = get_shallow_commits(&want_obj, depth,
681 SHALLOW, NOT_SHALLOW);
682 send_shallow(result);
683 free_commit_list(result);
686 send_unshallow(shallows);
689 static void deepen_by_rev_list(int ac, const char **av,
690 struct object_array *shallows)
692 struct commit_list *result;
694 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
695 send_shallow(result);
696 free_commit_list(result);
697 send_unshallow(shallows);
700 /* Returns 1 if a shallow list is sent or 0 otherwise */
701 static int send_shallow_list(int depth, int deepen_rev_list,
702 timestamp_t deepen_since,
703 struct string_list *deepen_not,
704 struct object_array *shallows)
708 if (depth > 0 && deepen_rev_list)
709 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
711 deepen(depth, deepen_relative, shallows);
713 } else if (deepen_rev_list) {
714 struct argv_array av = ARGV_ARRAY_INIT;
717 argv_array_push(&av, "rev-list");
719 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
720 if (deepen_not->nr) {
721 argv_array_push(&av, "--not");
722 for (i = 0; i < deepen_not->nr; i++) {
723 struct string_list_item *s = deepen_not->items + i;
724 argv_array_push(&av, s->string);
726 argv_array_push(&av, "--not");
728 for (i = 0; i < want_obj.nr; i++) {
729 struct object *o = want_obj.objects[i].item;
730 argv_array_push(&av, oid_to_hex(&o->oid));
732 deepen_by_rev_list(av.argc, av.argv, shallows);
733 argv_array_clear(&av);
736 if (shallows->nr > 0) {
738 for (i = 0; i < shallows->nr; i++)
739 register_shallow(the_repository,
740 &shallows->objects[i].item->oid);
744 shallow_nr += shallows->nr;
748 static int process_shallow(const char *line, struct object_array *shallows)
751 if (skip_prefix(line, "shallow ", &arg)) {
752 struct object_id oid;
753 struct object *object;
754 if (get_oid_hex(arg, &oid))
755 die("invalid shallow line: %s", line);
756 object = parse_object(the_repository, &oid);
759 if (object->type != OBJ_COMMIT)
760 die("invalid shallow object %s", oid_to_hex(&oid));
761 if (!(object->flags & CLIENT_SHALLOW)) {
762 object->flags |= CLIENT_SHALLOW;
763 add_object_array(object, NULL, shallows);
771 static int process_deepen(const char *line, int *depth)
774 if (skip_prefix(line, "deepen ", &arg)) {
776 *depth = (int)strtol(arg, &end, 0);
777 if (!end || *end || *depth <= 0)
778 die("Invalid deepen: %s", line);
785 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
788 if (skip_prefix(line, "deepen-since ", &arg)) {
790 *deepen_since = parse_timestamp(arg, &end, 0);
791 if (!end || *end || !deepen_since ||
792 /* revisions.c's max_age -1 is special */
794 die("Invalid deepen-since: %s", line);
795 *deepen_rev_list = 1;
801 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
804 if (skip_prefix(line, "deepen-not ", &arg)) {
806 struct object_id oid;
807 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
808 die("git upload-pack: ambiguous deepen-not: %s", line);
809 string_list_append(deepen_not, ref);
811 *deepen_rev_list = 1;
817 static void receive_needs(void)
819 struct object_array shallows = OBJECT_ARRAY_INIT;
820 struct string_list deepen_not = STRING_LIST_INIT_DUP;
823 timestamp_t deepen_since = 0;
824 int deepen_rev_list = 0;
829 const char *features;
830 struct object_id oid_buf;
831 char *line = packet_read_line(0, NULL);
838 if (process_shallow(line, &shallows))
840 if (process_deepen(line, &depth))
842 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
844 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
847 if (skip_prefix(line, "filter ", &arg)) {
848 if (!filter_capability_requested)
849 die("git upload-pack: filtering capability not negotiated");
850 parse_list_objects_filter(&filter_options, arg);
854 if (!skip_prefix(line, "want ", &arg) ||
855 parse_oid_hex(arg, &oid_buf, &features))
856 die("git upload-pack: protocol error, "
857 "expected to get object ID, not '%s'", line);
859 if (parse_feature_request(features, "deepen-relative"))
861 if (parse_feature_request(features, "multi_ack_detailed"))
863 else if (parse_feature_request(features, "multi_ack"))
865 if (parse_feature_request(features, "no-done"))
867 if (parse_feature_request(features, "thin-pack"))
869 if (parse_feature_request(features, "ofs-delta"))
871 if (parse_feature_request(features, "side-band-64k"))
872 use_sideband = LARGE_PACKET_MAX;
873 else if (parse_feature_request(features, "side-band"))
874 use_sideband = DEFAULT_PACKET_MAX;
875 if (parse_feature_request(features, "no-progress"))
877 if (parse_feature_request(features, "include-tag"))
879 if (allow_filter && parse_feature_request(features, "filter"))
880 filter_capability_requested = 1;
882 o = parse_object(the_repository, &oid_buf);
885 "ERR upload-pack: not our ref %s",
886 oid_to_hex(&oid_buf));
887 die("git upload-pack: not our ref %s",
888 oid_to_hex(&oid_buf));
890 if (!(o->flags & WANTED)) {
892 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
895 add_object_array(o, NULL, &want_obj);
900 * We have sent all our refs already, and the other end
901 * should have chosen out of them. When we are operating
902 * in the stateless RPC mode, however, their choice may
903 * have been based on the set of older refs advertised
904 * by another process that handled the initial request.
909 if (!use_sideband && daemon_mode)
912 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
915 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
916 &deepen_not, &shallows))
918 object_array_clear(&shallows);
921 /* return non-zero if the ref is hidden, otherwise 0 */
922 static int mark_our_ref(const char *refname, const char *refname_full,
923 const struct object_id *oid)
925 struct object *o = lookup_unknown_object(oid->hash);
927 if (ref_is_hidden(refname, refname_full)) {
928 o->flags |= HIDDEN_REF;
935 static int check_ref(const char *refname_full, const struct object_id *oid,
936 int flag, void *cb_data)
938 const char *refname = strip_namespace(refname_full);
940 mark_our_ref(refname, refname_full, oid);
944 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
946 struct string_list_item *item;
950 for_each_string_list_item(item, symref)
951 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
954 static int send_ref(const char *refname, const struct object_id *oid,
955 int flag, void *cb_data)
957 static const char *capabilities = "multi_ack thin-pack side-band"
958 " side-band-64k ofs-delta shallow deepen-since deepen-not"
959 " deepen-relative no-progress include-tag multi_ack_detailed";
960 const char *refname_nons = strip_namespace(refname);
961 struct object_id peeled;
963 if (mark_our_ref(refname_nons, refname, oid))
967 struct strbuf symref_info = STRBUF_INIT;
969 format_symref_info(&symref_info, cb_data);
970 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
971 oid_to_hex(oid), refname_nons,
973 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
974 " allow-tip-sha1-in-want" : "",
975 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
976 " allow-reachable-sha1-in-want" : "",
977 stateless_rpc ? " no-done" : "",
979 allow_filter ? " filter" : "",
980 git_user_agent_sanitized());
981 strbuf_release(&symref_info);
983 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
986 if (!peel_ref(refname, &peeled))
987 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
991 static int find_symref(const char *refname, const struct object_id *oid,
992 int flag, void *cb_data)
994 const char *symref_target;
995 struct string_list_item *item;
997 if ((flag & REF_ISSYMREF) == 0)
999 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1000 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1001 die("'%s' is a symref but it is not?", refname);
1002 item = string_list_append(cb_data, refname);
1003 item->util = xstrdup(symref_target);
1007 static int upload_pack_config(const char *var, const char *value, void *unused)
1009 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1010 if (git_config_bool(var, value))
1011 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1013 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1014 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1015 if (git_config_bool(var, value))
1016 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1018 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1019 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1020 if (git_config_bool(var, value))
1021 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1023 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1024 } else if (!strcmp("uploadpack.keepalive", var)) {
1025 keepalive = git_config_int(var, value);
1028 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1029 if (!strcmp("uploadpack.packobjectshook", var))
1030 return git_config_string(&pack_objects_hook, var, value);
1031 } else if (!strcmp("uploadpack.allowfilter", var)) {
1032 allow_filter = git_config_bool(var, value);
1034 return parse_hide_refs_config(var, value, "uploadpack");
1037 void upload_pack(struct upload_pack_options *options)
1039 struct string_list symref = STRING_LIST_INIT_DUP;
1041 stateless_rpc = options->stateless_rpc;
1042 timeout = options->timeout;
1043 daemon_mode = options->daemon_mode;
1045 git_config(upload_pack_config, NULL);
1047 head_ref_namespaced(find_symref, &symref);
1049 if (options->advertise_refs || !stateless_rpc) {
1051 head_ref_namespaced(send_ref, &symref);
1052 for_each_namespaced_ref(send_ref, &symref);
1053 advertise_shallow_grafts(1);
1056 head_ref_namespaced(check_ref, NULL);
1057 for_each_namespaced_ref(check_ref, NULL);
1059 string_list_clear(&symref, 1);
1060 if (options->advertise_refs)
1065 get_common_commits();
1070 struct upload_pack_data {
1071 struct object_array wants;
1072 struct oid_array haves;
1074 struct object_array shallows;
1075 struct string_list deepen_not;
1077 timestamp_t deepen_since;
1078 int deepen_rev_list;
1079 int deepen_relative;
1081 unsigned stateless_rpc : 1;
1083 unsigned use_thin_pack : 1;
1084 unsigned use_ofs_delta : 1;
1085 unsigned no_progress : 1;
1086 unsigned use_include_tag : 1;
1090 static void upload_pack_data_init(struct upload_pack_data *data)
1092 struct object_array wants = OBJECT_ARRAY_INIT;
1093 struct oid_array haves = OID_ARRAY_INIT;
1094 struct object_array shallows = OBJECT_ARRAY_INIT;
1095 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1097 memset(data, 0, sizeof(*data));
1098 data->wants = wants;
1099 data->haves = haves;
1100 data->shallows = shallows;
1101 data->deepen_not = deepen_not;
1104 static void upload_pack_data_clear(struct upload_pack_data *data)
1106 object_array_clear(&data->wants);
1107 oid_array_clear(&data->haves);
1108 object_array_clear(&data->shallows);
1109 string_list_clear(&data->deepen_not, 0);
1112 static int parse_want(const char *line)
1115 if (skip_prefix(line, "want ", &arg)) {
1116 struct object_id oid;
1119 if (get_oid_hex(arg, &oid))
1120 die("git upload-pack: protocol error, "
1121 "expected to get oid, not '%s'", line);
1123 o = parse_object(the_repository, &oid);
1126 "ERR upload-pack: not our ref %s",
1128 die("git upload-pack: not our ref %s",
1132 if (!(o->flags & WANTED)) {
1134 add_object_array(o, NULL, &want_obj);
1143 static int parse_have(const char *line, struct oid_array *haves)
1146 if (skip_prefix(line, "have ", &arg)) {
1147 struct object_id oid;
1149 if (get_oid_hex(arg, &oid))
1150 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1151 oid_array_append(haves, &oid);
1158 static void process_args(struct packet_reader *request,
1159 struct upload_pack_data *data)
1161 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1162 const char *arg = request->line;
1166 if (parse_want(arg))
1168 /* process have line */
1169 if (parse_have(arg, &data->haves))
1172 /* process args like thin-pack */
1173 if (!strcmp(arg, "thin-pack")) {
1177 if (!strcmp(arg, "ofs-delta")) {
1181 if (!strcmp(arg, "no-progress")) {
1185 if (!strcmp(arg, "include-tag")) {
1186 use_include_tag = 1;
1189 if (!strcmp(arg, "done")) {
1194 /* Shallow related arguments */
1195 if (process_shallow(arg, &data->shallows))
1197 if (process_deepen(arg, &data->depth))
1199 if (process_deepen_since(arg, &data->deepen_since,
1200 &data->deepen_rev_list))
1202 if (process_deepen_not(arg, &data->deepen_not,
1203 &data->deepen_rev_list))
1205 if (!strcmp(arg, "deepen-relative")) {
1206 data->deepen_relative = 1;
1210 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1211 parse_list_objects_filter(&filter_options, p);
1215 /* ignore unknown lines maybe? */
1216 die("unexpected line: '%s'", arg);
1220 static int process_haves(struct oid_array *haves, struct oid_array *common)
1225 for (i = 0; i < haves->nr; i++) {
1226 const struct object_id *oid = &haves->oid[i];
1228 int we_knew_they_have = 0;
1230 if (!has_object_file(oid))
1233 oid_array_append(common, oid);
1235 o = parse_object(the_repository, oid);
1237 die("oops (%s)", oid_to_hex(oid));
1238 if (o->type == OBJ_COMMIT) {
1239 struct commit_list *parents;
1240 struct commit *commit = (struct commit *)o;
1241 if (o->flags & THEY_HAVE)
1242 we_knew_they_have = 1;
1244 o->flags |= THEY_HAVE;
1245 if (!oldest_have || (commit->date < oldest_have))
1246 oldest_have = commit->date;
1247 for (parents = commit->parents;
1249 parents = parents->next)
1250 parents->item->object.flags |= THEY_HAVE;
1252 if (!we_knew_they_have)
1253 add_object_array(o, NULL, &have_obj);
1259 static int send_acks(struct oid_array *acks, struct strbuf *response)
1263 packet_buf_write(response, "acknowledgments\n");
1267 packet_buf_write(response, "NAK\n");
1269 for (i = 0; i < acks->nr; i++) {
1270 packet_buf_write(response, "ACK %s\n",
1271 oid_to_hex(&acks->oid[i]));
1274 if (ok_to_give_up()) {
1276 packet_buf_write(response, "ready\n");
1283 static int process_haves_and_send_acks(struct upload_pack_data *data)
1285 struct oid_array common = OID_ARRAY_INIT;
1286 struct strbuf response = STRBUF_INIT;
1289 process_haves(&data->haves, &common);
1292 } else if (send_acks(&common, &response)) {
1293 packet_buf_delim(&response);
1297 packet_buf_flush(&response);
1302 write_or_die(1, response.buf, response.len);
1303 strbuf_release(&response);
1305 oid_array_clear(&data->haves);
1306 oid_array_clear(&common);
1310 static void send_shallow_info(struct upload_pack_data *data)
1312 /* No shallow info needs to be sent */
1313 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1314 !is_repository_shallow(the_repository))
1317 packet_write_fmt(1, "shallow-info\n");
1319 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1320 data->deepen_since, &data->deepen_not,
1322 is_repository_shallow(the_repository))
1323 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1329 FETCH_PROCESS_ARGS = 0,
1335 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1336 struct packet_reader *request)
1338 enum fetch_state state = FETCH_PROCESS_ARGS;
1339 struct upload_pack_data data;
1341 git_config(upload_pack_config, NULL);
1343 upload_pack_data_init(&data);
1344 use_sideband = LARGE_PACKET_MAX;
1346 while (state != FETCH_DONE) {
1348 case FETCH_PROCESS_ARGS:
1349 process_args(request, &data);
1353 * Request didn't contain any 'want' lines,
1354 * guess they didn't want anything.
1357 } else if (data.haves.nr) {
1359 * Request had 'have' lines, so lets ACK them.
1361 state = FETCH_SEND_ACKS;
1364 * Request had 'want's but no 'have's so we can
1365 * immedietly go to construct and send a pack.
1367 state = FETCH_SEND_PACK;
1370 case FETCH_SEND_ACKS:
1371 if (process_haves_and_send_acks(&data))
1372 state = FETCH_SEND_PACK;
1376 case FETCH_SEND_PACK:
1377 send_shallow_info(&data);
1379 packet_write_fmt(1, "packfile\n");
1388 upload_pack_data_clear(&data);
1392 int upload_pack_advertise(struct repository *r,
1393 struct strbuf *value)
1396 int allow_filter_value;
1397 strbuf_addstr(value, "shallow");
1398 if (!repo_config_get_bool(the_repository,
1399 "uploadpack.allowfilter",
1400 &allow_filter_value) &&
1402 strbuf_addstr(value, " filter");