11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
17 #include "argv-array.h"
18 #include "prio-queue.h"
20 #include "upload-pack.h"
23 /* Remember to update object flag allocation in object.h */
24 #define THEY_HAVE (1u << 11)
25 #define OUR_REF (1u << 12)
26 #define WANTED (1u << 13)
27 #define COMMON_KNOWN (1u << 14)
28 #define REACHABLE (1u << 15)
30 #define SHALLOW (1u << 16)
31 #define NOT_SHALLOW (1u << 17)
32 #define CLIENT_SHALLOW (1u << 18)
33 #define HIDDEN_REF (1u << 19)
35 static timestamp_t oldest_have;
37 static int deepen_relative;
40 static int use_thin_pack, use_ofs_delta, use_include_tag;
41 static int no_progress, daemon_mode;
42 /* Allow specifying sha1 if it is a ref tip. */
43 #define ALLOW_TIP_SHA1 01
44 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
45 #define ALLOW_REACHABLE_SHA1 02
46 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
47 #define ALLOW_ANY_SHA1 07
48 static unsigned int allow_unadvertised_object_request;
49 static int shallow_nr;
50 static struct object_array have_obj;
51 static struct object_array want_obj;
52 static struct object_array extra_edge_obj;
53 static unsigned int timeout;
54 static int keepalive = 5;
56 * otherwise maximum packet size (up to 65520 bytes).
58 static int use_sideband;
59 static int stateless_rpc;
60 static const char *pack_objects_hook;
62 static void reset_timeout(void)
67 static void send_client_data(int fd, const char *data, ssize_t sz)
70 send_sideband(1, fd, data, sz, use_sideband);
77 /* XXX: are we happy to lose stuff here? */
81 write_or_die(fd, data, sz);
84 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
87 if (graft->nr_parent == -1)
88 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
92 static void create_pack_file(void)
94 struct child_process pack_objects = CHILD_PROCESS_INIT;
95 char data[8193], progress[128];
96 char abort_msg[] = "aborting due to possible repository "
97 "corruption on the remote side.";
103 if (!pack_objects_hook)
104 pack_objects.git_cmd = 1;
106 argv_array_push(&pack_objects.args, pack_objects_hook);
107 argv_array_push(&pack_objects.args, "git");
108 pack_objects.use_shell = 1;
112 argv_array_push(&pack_objects.args, "--shallow-file");
113 argv_array_push(&pack_objects.args, "");
115 argv_array_push(&pack_objects.args, "pack-objects");
116 argv_array_push(&pack_objects.args, "--revs");
118 argv_array_push(&pack_objects.args, "--thin");
120 argv_array_push(&pack_objects.args, "--stdout");
122 argv_array_push(&pack_objects.args, "--shallow");
124 argv_array_push(&pack_objects.args, "--progress");
126 argv_array_push(&pack_objects.args, "--delta-base-offset");
128 argv_array_push(&pack_objects.args, "--include-tag");
130 pack_objects.in = -1;
131 pack_objects.out = -1;
132 pack_objects.err = -1;
134 if (start_command(&pack_objects))
135 die("git upload-pack: unable to fork git-pack-objects");
137 pipe_fd = xfdopen(pack_objects.in, "w");
140 for_each_commit_graft(write_one_shallow, pipe_fd);
142 for (i = 0; i < want_obj.nr; i++)
143 fprintf(pipe_fd, "%s\n",
144 oid_to_hex(&want_obj.objects[i].item->oid));
145 fprintf(pipe_fd, "--not\n");
146 for (i = 0; i < have_obj.nr; i++)
147 fprintf(pipe_fd, "%s\n",
148 oid_to_hex(&have_obj.objects[i].item->oid));
149 for (i = 0; i < extra_edge_obj.nr; i++)
150 fprintf(pipe_fd, "%s\n",
151 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
152 fprintf(pipe_fd, "\n");
156 /* We read from pack_objects.err to capture stderr output for
157 * progress bar, and pack_objects.out to capture the pack data.
161 struct pollfd pfd[2];
162 int pe, pu, pollsize;
170 if (0 <= pack_objects.out) {
171 pfd[pollsize].fd = pack_objects.out;
172 pfd[pollsize].events = POLLIN;
176 if (0 <= pack_objects.err) {
177 pfd[pollsize].fd = pack_objects.err;
178 pfd[pollsize].events = POLLIN;
186 ret = poll(pfd, pollsize,
187 keepalive < 0 ? -1 : 1000 * keepalive);
190 if (errno != EINTR) {
191 error_errno("poll failed, resuming");
196 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
197 /* Status ready; we ship that in the side-band
198 * or dump to the standard error.
200 sz = xread(pack_objects.err, progress,
203 send_client_data(2, progress, sz);
205 close(pack_objects.err);
206 pack_objects.err = -1;
210 /* give priority to status messages */
213 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
214 /* Data ready; we keep the last byte to ourselves
215 * in case we detect broken rev-list, so that we
216 * can leave the stream corrupted. This is
217 * unfortunate -- unpack-objects would happily
218 * accept a valid packdata with trailing garbage,
219 * so appending garbage after we pass all the
220 * pack data is not good enough to signal
221 * breakage to downstream.
229 sz = xread(pack_objects.out, cp,
230 sizeof(data) - outsz);
234 close(pack_objects.out);
235 pack_objects.out = -1;
241 buffered = data[sz-1] & 0xFF;
246 send_client_data(1, data, sz);
250 * We hit the keepalive timeout without saying anything; send
251 * an empty message on the data sideband just to let the other
252 * side know we're still working on it, but don't have any data
255 * If we don't have a sideband channel, there's no room in the
256 * protocol to say anything, so those clients are just out of
259 if (!ret && use_sideband) {
260 static const char buf[] = "0005\1";
261 write_or_die(1, buf, 5);
265 if (finish_command(&pack_objects)) {
266 error("git upload-pack: git-pack-objects died with error.");
273 send_client_data(1, data, 1);
274 fprintf(stderr, "flushed.\n");
281 send_client_data(3, abort_msg, sizeof(abort_msg));
282 die("git upload-pack: %s", abort_msg);
285 static int got_oid(const char *hex, struct object_id *oid)
288 int we_knew_they_have = 0;
290 if (get_oid_hex(hex, oid))
291 die("git upload-pack: expected SHA1 object, got '%s'", hex);
292 if (!has_object_file(oid))
295 o = parse_object(oid);
297 die("oops (%s)", oid_to_hex(oid));
298 if (o->type == OBJ_COMMIT) {
299 struct commit_list *parents;
300 struct commit *commit = (struct commit *)o;
301 if (o->flags & THEY_HAVE)
302 we_knew_they_have = 1;
304 o->flags |= THEY_HAVE;
305 if (!oldest_have || (commit->date < oldest_have))
306 oldest_have = commit->date;
307 for (parents = commit->parents;
309 parents = parents->next)
310 parents->item->object.flags |= THEY_HAVE;
312 if (!we_knew_they_have) {
313 add_object_array(o, NULL, &have_obj);
319 static int reachable(struct commit *want)
321 struct prio_queue work = { compare_commits_by_commit_date };
323 prio_queue_put(&work, want);
325 struct commit_list *list;
326 struct commit *commit = prio_queue_get(&work);
328 if (commit->object.flags & THEY_HAVE) {
329 want->object.flags |= COMMON_KNOWN;
332 if (!commit->object.parsed)
333 parse_object(&commit->object.oid);
334 if (commit->object.flags & REACHABLE)
336 commit->object.flags |= REACHABLE;
337 if (commit->date < oldest_have)
339 for (list = commit->parents; list; list = list->next) {
340 struct commit *parent = list->item;
341 if (!(parent->object.flags & REACHABLE))
342 prio_queue_put(&work, parent);
345 want->object.flags |= REACHABLE;
346 clear_commit_marks(want, REACHABLE);
347 clear_prio_queue(&work);
348 return (want->object.flags & COMMON_KNOWN);
351 static int ok_to_give_up(void)
358 for (i = 0; i < want_obj.nr; i++) {
359 struct object *want = want_obj.objects[i].item;
361 if (want->flags & COMMON_KNOWN)
363 want = deref_tag(want, "a want line", 0);
364 if (!want || want->type != OBJ_COMMIT) {
365 /* no way to tell if this is reachable by
366 * looking at the ancestry chain alone, so
367 * leave a note to ourselves not to worry about
368 * this object anymore.
370 want_obj.objects[i].item->flags |= COMMON_KNOWN;
373 if (!reachable((struct commit *)want))
379 static int get_common_commits(void)
381 struct object_id oid;
382 char last_hex[GIT_MAX_HEXSZ + 1];
387 save_commit_buffer = 0;
390 char *line = packet_read_line(0, NULL);
396 if (multi_ack == 2 && got_common
397 && !got_other && ok_to_give_up()) {
399 packet_write_fmt(1, "ACK %s ready\n", last_hex);
401 if (have_obj.nr == 0 || multi_ack)
402 packet_write_fmt(1, "NAK\n");
404 if (no_done && sent_ready) {
405 packet_write_fmt(1, "ACK %s\n", last_hex);
414 if (skip_prefix(line, "have ", &arg)) {
415 switch (got_oid(arg, &oid)) {
416 case -1: /* they have what we do not */
418 if (multi_ack && ok_to_give_up()) {
419 const char *hex = oid_to_hex(&oid);
420 if (multi_ack == 2) {
422 packet_write_fmt(1, "ACK %s ready\n", hex);
424 packet_write_fmt(1, "ACK %s continue\n", hex);
429 memcpy(last_hex, oid_to_hex(&oid), 41);
431 packet_write_fmt(1, "ACK %s common\n", last_hex);
433 packet_write_fmt(1, "ACK %s continue\n", last_hex);
434 else if (have_obj.nr == 1)
435 packet_write_fmt(1, "ACK %s\n", last_hex);
440 if (!strcmp(line, "done")) {
441 if (have_obj.nr > 0) {
443 packet_write_fmt(1, "ACK %s\n", last_hex);
446 packet_write_fmt(1, "NAK\n");
449 die("git upload-pack: expected SHA1 list, got '%s'", line);
453 static int is_our_ref(struct object *o)
455 int allow_hidden_ref = (allow_unadvertised_object_request &
456 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
457 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
461 * on successful case, it's up to the caller to close cmd->out
463 static int do_reachable_revlist(struct child_process *cmd,
464 struct object_array *src,
465 struct object_array *reachable)
467 static const char *argv[] = {
468 "rev-list", "--stdin", NULL,
471 char namebuf[42]; /* ^ + SHA-1 + LF */
481 * If the next rev-list --stdin encounters an unknown commit,
482 * it terminates, which will cause SIGPIPE in the write loop
485 sigchain_push(SIGPIPE, SIG_IGN);
487 if (start_command(cmd))
491 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
492 for (i = get_max_object_index(); 0 < i; ) {
493 o = get_indexed_object(--i);
496 if (reachable && o->type == OBJ_COMMIT)
497 o->flags &= ~TMP_MARK;
500 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
501 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
504 namebuf[GIT_SHA1_HEXSZ] = '\n';
505 for (i = 0; i < src->nr; i++) {
506 o = src->objects[i].item;
509 add_object_array(o, NULL, reachable);
512 if (reachable && o->type == OBJ_COMMIT)
513 o->flags |= TMP_MARK;
514 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
515 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
520 sigchain_pop(SIGPIPE);
525 sigchain_pop(SIGPIPE);
534 static int get_reachable_list(struct object_array *src,
535 struct object_array *reachable)
537 struct child_process cmd = CHILD_PROCESS_INIT;
540 char namebuf[42]; /* ^ + SHA-1 + LF */
542 if (do_reachable_revlist(&cmd, src, reachable) < 0)
545 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
546 struct object_id sha1;
548 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
551 o = lookup_object(sha1.hash);
552 if (o && o->type == OBJ_COMMIT) {
553 o->flags &= ~TMP_MARK;
556 for (i = get_max_object_index(); 0 < i; i--) {
557 o = get_indexed_object(i - 1);
558 if (o && o->type == OBJ_COMMIT &&
559 (o->flags & TMP_MARK)) {
560 add_object_array(o, NULL, reachable);
561 o->flags &= ~TMP_MARK;
566 if (finish_command(&cmd))
572 static int has_unreachable(struct object_array *src)
574 struct child_process cmd = CHILD_PROCESS_INIT;
578 if (do_reachable_revlist(&cmd, src, NULL) < 0)
582 * The commits out of the rev-list are not ancestors of
585 i = read_in_full(cmd.out, buf, 1);
592 * rev-list may have died by encountering a bad commit
593 * in the history, in which case we do want to bail out
594 * even when it showed no commit.
596 if (finish_command(&cmd))
599 /* All the non-tip ones are ancestors of what we advertised */
603 sigchain_pop(SIGPIPE);
609 static void check_non_tip(void)
614 * In the normal in-process case without
615 * uploadpack.allowReachableSHA1InWant,
616 * non-tip requests can never happen.
618 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
620 if (!has_unreachable(&want_obj))
621 /* All the non-tip ones are ancestors of what we advertised */
625 /* Pick one of them (we know there at least is one) */
626 for (i = 0; i < want_obj.nr; i++) {
627 struct object *o = want_obj.objects[i].item;
629 die("git upload-pack: not our ref %s",
630 oid_to_hex(&o->oid));
634 static void send_shallow(struct commit_list *result)
637 struct object *object = &result->item->object;
638 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
639 packet_write_fmt(1, "shallow %s",
640 oid_to_hex(&object->oid));
641 register_shallow(&object->oid);
644 result = result->next;
648 static void send_unshallow(const struct object_array *shallows)
652 for (i = 0; i < shallows->nr; i++) {
653 struct object *object = shallows->objects[i].item;
654 if (object->flags & NOT_SHALLOW) {
655 struct commit_list *parents;
656 packet_write_fmt(1, "unshallow %s",
657 oid_to_hex(&object->oid));
658 object->flags &= ~CLIENT_SHALLOW;
660 * We want to _register_ "object" as shallow, but we
661 * also need to traverse object's parents to deepen a
662 * shallow clone. Unregister it for now so we can
663 * parse and add the parents to the want list, then
666 unregister_shallow(&object->oid);
668 parse_commit_or_die((struct commit *)object);
669 parents = ((struct commit *)object)->parents;
671 add_object_array(&parents->item->object,
673 parents = parents->next;
675 add_object_array(object, NULL, &extra_edge_obj);
677 /* make sure commit traversal conforms to client */
678 register_shallow(&object->oid);
682 static void deepen(int depth, int deepen_relative,
683 struct object_array *shallows)
685 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
688 for (i = 0; i < shallows->nr; i++) {
689 struct object *object = shallows->objects[i].item;
690 object->flags |= NOT_SHALLOW;
692 } else if (deepen_relative) {
693 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
694 struct commit_list *result;
696 get_reachable_list(shallows, &reachable_shallows);
697 result = get_shallow_commits(&reachable_shallows,
699 SHALLOW, NOT_SHALLOW);
700 send_shallow(result);
701 free_commit_list(result);
702 object_array_clear(&reachable_shallows);
704 struct commit_list *result;
706 result = get_shallow_commits(&want_obj, depth,
707 SHALLOW, NOT_SHALLOW);
708 send_shallow(result);
709 free_commit_list(result);
712 send_unshallow(shallows);
716 static void deepen_by_rev_list(int ac, const char **av,
717 struct object_array *shallows)
719 struct commit_list *result;
721 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
722 send_shallow(result);
723 free_commit_list(result);
724 send_unshallow(shallows);
728 static int process_shallow(const char *line, struct object_array *shallows)
731 if (skip_prefix(line, "shallow ", &arg)) {
732 struct object_id oid;
733 struct object *object;
734 if (get_oid_hex(arg, &oid))
735 die("invalid shallow line: %s", line);
736 object = parse_object(&oid);
739 if (object->type != OBJ_COMMIT)
740 die("invalid shallow object %s", oid_to_hex(&oid));
741 if (!(object->flags & CLIENT_SHALLOW)) {
742 object->flags |= CLIENT_SHALLOW;
743 add_object_array(object, NULL, shallows);
751 static int process_deepen(const char *line, int *depth)
754 if (skip_prefix(line, "deepen ", &arg)) {
756 *depth = (int)strtol(arg, &end, 0);
757 if (!end || *end || *depth <= 0)
758 die("Invalid deepen: %s", line);
765 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
768 if (skip_prefix(line, "deepen-since ", &arg)) {
770 *deepen_since = parse_timestamp(arg, &end, 0);
771 if (!end || *end || !deepen_since ||
772 /* revisions.c's max_age -1 is special */
774 die("Invalid deepen-since: %s", line);
775 *deepen_rev_list = 1;
781 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
784 if (skip_prefix(line, "deepen-not ", &arg)) {
786 struct object_id oid;
787 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
788 die("git upload-pack: ambiguous deepen-not: %s", line);
789 string_list_append(deepen_not, ref);
791 *deepen_rev_list = 1;
797 static void receive_needs(void)
799 struct object_array shallows = OBJECT_ARRAY_INIT;
800 struct string_list deepen_not = STRING_LIST_INIT_DUP;
803 timestamp_t deepen_since = 0;
804 int deepen_rev_list = 0;
809 const char *features;
810 struct object_id oid_buf;
811 char *line = packet_read_line(0, NULL);
818 if (process_shallow(line, &shallows))
820 if (process_deepen(line, &depth))
822 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
824 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
827 if (!skip_prefix(line, "want ", &arg) ||
828 get_oid_hex(arg, &oid_buf))
829 die("git upload-pack: protocol error, "
830 "expected to get sha, not '%s'", line);
834 if (parse_feature_request(features, "deepen-relative"))
836 if (parse_feature_request(features, "multi_ack_detailed"))
838 else if (parse_feature_request(features, "multi_ack"))
840 if (parse_feature_request(features, "no-done"))
842 if (parse_feature_request(features, "thin-pack"))
844 if (parse_feature_request(features, "ofs-delta"))
846 if (parse_feature_request(features, "side-band-64k"))
847 use_sideband = LARGE_PACKET_MAX;
848 else if (parse_feature_request(features, "side-band"))
849 use_sideband = DEFAULT_PACKET_MAX;
850 if (parse_feature_request(features, "no-progress"))
852 if (parse_feature_request(features, "include-tag"))
855 o = parse_object(&oid_buf);
858 "ERR upload-pack: not our ref %s",
859 oid_to_hex(&oid_buf));
860 die("git upload-pack: not our ref %s",
861 oid_to_hex(&oid_buf));
863 if (!(o->flags & WANTED)) {
865 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
868 add_object_array(o, NULL, &want_obj);
873 * We have sent all our refs already, and the other end
874 * should have chosen out of them. When we are operating
875 * in the stateless RPC mode, however, their choice may
876 * have been based on the set of older refs advertised
877 * by another process that handled the initial request.
882 if (!use_sideband && daemon_mode)
885 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
887 if (depth > 0 && deepen_rev_list)
888 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
890 deepen(depth, deepen_relative, &shallows);
891 else if (deepen_rev_list) {
892 struct argv_array av = ARGV_ARRAY_INIT;
895 argv_array_push(&av, "rev-list");
897 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
899 argv_array_push(&av, "--not");
900 for (i = 0; i < deepen_not.nr; i++) {
901 struct string_list_item *s = deepen_not.items + i;
902 argv_array_push(&av, s->string);
904 argv_array_push(&av, "--not");
906 for (i = 0; i < want_obj.nr; i++) {
907 struct object *o = want_obj.objects[i].item;
908 argv_array_push(&av, oid_to_hex(&o->oid));
910 deepen_by_rev_list(av.argc, av.argv, &shallows);
911 argv_array_clear(&av);
914 if (shallows.nr > 0) {
916 for (i = 0; i < shallows.nr; i++)
917 register_shallow(&shallows.objects[i].item->oid);
920 shallow_nr += shallows.nr;
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 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 git_user_agent_sanitized());
983 strbuf_release(&symref_info);
985 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
988 if (!peel_ref(refname, &peeled))
989 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
993 static int find_symref(const char *refname, const struct object_id *oid,
994 int flag, void *cb_data)
996 const char *symref_target;
997 struct string_list_item *item;
999 if ((flag & REF_ISSYMREF) == 0)
1001 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1002 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1003 die("'%s' is a symref but it is not?", refname);
1004 item = string_list_append(cb_data, refname);
1005 item->util = xstrdup(symref_target);
1009 static int upload_pack_config(const char *var, const char *value, void *unused)
1011 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1012 if (git_config_bool(var, value))
1013 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1015 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1016 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1017 if (git_config_bool(var, value))
1018 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1020 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1021 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1022 if (git_config_bool(var, value))
1023 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1025 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1026 } else if (!strcmp("uploadpack.keepalive", var)) {
1027 keepalive = git_config_int(var, value);
1030 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1031 if (!strcmp("uploadpack.packobjectshook", var))
1032 return git_config_string(&pack_objects_hook, 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 unsigned stateless_rpc : 1;
1076 unsigned use_thin_pack : 1;
1077 unsigned use_ofs_delta : 1;
1078 unsigned no_progress : 1;
1079 unsigned use_include_tag : 1;
1083 #define UPLOAD_PACK_DATA_INIT { OBJECT_ARRAY_INIT, OID_ARRAY_INIT, 0, 0, 0, 0, 0, 0 }
1085 static void upload_pack_data_clear(struct upload_pack_data *data)
1087 object_array_clear(&data->wants);
1088 oid_array_clear(&data->haves);
1091 static int parse_want(const char *line)
1094 if (skip_prefix(line, "want ", &arg)) {
1095 struct object_id oid;
1098 if (get_oid_hex(arg, &oid))
1099 die("git upload-pack: protocol error, "
1100 "expected to get oid, not '%s'", line);
1102 o = parse_object(&oid);
1105 "ERR upload-pack: not our ref %s",
1107 die("git upload-pack: not our ref %s",
1111 if (!(o->flags & WANTED)) {
1113 add_object_array(o, NULL, &want_obj);
1122 static int parse_have(const char *line, struct oid_array *haves)
1125 if (skip_prefix(line, "have ", &arg)) {
1126 struct object_id oid;
1128 if (get_oid_hex(arg, &oid))
1129 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1130 oid_array_append(haves, &oid);
1137 static void process_args(struct argv_array *args, struct upload_pack_data *data)
1141 for (i = 0; i < args->argc; i++) {
1142 const char *arg = args->argv[i];
1145 if (parse_want(arg))
1147 /* process have line */
1148 if (parse_have(arg, &data->haves))
1151 /* process args like thin-pack */
1152 if (!strcmp(arg, "thin-pack")) {
1156 if (!strcmp(arg, "ofs-delta")) {
1160 if (!strcmp(arg, "no-progress")) {
1164 if (!strcmp(arg, "include-tag")) {
1165 use_include_tag = 1;
1168 if (!strcmp(arg, "done")) {
1173 /* ignore unknown lines maybe? */
1174 die("unexpect line: '%s'", arg);
1178 static void read_haves(struct upload_pack_data *data)
1180 struct packet_reader reader;
1181 packet_reader_init(&reader, 0, NULL, 0,
1182 PACKET_READ_CHOMP_NEWLINE);
1184 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
1186 if (parse_have(reader.line, &data->haves))
1188 if (!strcmp(reader.line, "done")) {
1193 if (reader.status != PACKET_READ_FLUSH)
1197 static int process_haves(struct oid_array *haves, struct oid_array *common)
1202 for (i = 0; i < haves->nr; i++) {
1203 const struct object_id *oid = &haves->oid[i];
1205 int we_knew_they_have = 0;
1207 if (!has_object_file(oid))
1210 oid_array_append(common, oid);
1212 o = parse_object(oid);
1214 die("oops (%s)", oid_to_hex(oid));
1215 if (o->type == OBJ_COMMIT) {
1216 struct commit_list *parents;
1217 struct commit *commit = (struct commit *)o;
1218 if (o->flags & THEY_HAVE)
1219 we_knew_they_have = 1;
1221 o->flags |= THEY_HAVE;
1222 if (!oldest_have || (commit->date < oldest_have))
1223 oldest_have = commit->date;
1224 for (parents = commit->parents;
1226 parents = parents->next)
1227 parents->item->object.flags |= THEY_HAVE;
1229 if (!we_knew_they_have)
1230 add_object_array(o, NULL, &have_obj);
1236 static int send_acks(struct oid_array *acks, struct strbuf *response)
1240 packet_buf_write(response, "acknowledgments\n");
1244 packet_buf_write(response, "NAK\n");
1246 for (i = 0; i < acks->nr; i++) {
1247 packet_buf_write(response, "ACK %s\n",
1248 oid_to_hex(&acks->oid[i]));
1251 if (ok_to_give_up()) {
1253 packet_buf_write(response, "ready\n");
1260 static int process_haves_and_send_acks(struct upload_pack_data *data)
1262 struct oid_array common = OID_ARRAY_INIT;
1263 struct strbuf response = STRBUF_INIT;
1266 process_haves(&data->haves, &common);
1269 } else if (send_acks(&common, &response)) {
1270 packet_buf_delim(&response);
1274 packet_buf_flush(&response);
1279 write_or_die(1, response.buf, response.len);
1280 strbuf_release(&response);
1282 oid_array_clear(&data->haves);
1283 oid_array_clear(&common);
1288 FETCH_PROCESS_ARGS = 0,
1295 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1296 struct argv_array *args)
1298 enum fetch_state state = FETCH_PROCESS_ARGS;
1299 struct upload_pack_data data = UPLOAD_PACK_DATA_INIT;
1300 use_sideband = LARGE_PACKET_MAX;
1302 while (state != FETCH_DONE) {
1304 case FETCH_PROCESS_ARGS:
1305 process_args(args, &data);
1309 * Request didn't contain any 'want' lines,
1310 * guess they didn't want anything.
1313 } else if (data.haves.nr) {
1315 * Request had 'have' lines, so lets ACK them.
1317 state = FETCH_SEND_ACKS;
1320 * Request had 'want's but no 'have's so we can
1321 * immedietly go to construct and send a pack.
1323 state = FETCH_SEND_PACK;
1326 case FETCH_READ_HAVES:
1328 state = FETCH_SEND_ACKS;
1330 case FETCH_SEND_ACKS:
1331 if (process_haves_and_send_acks(&data))
1332 state = FETCH_SEND_PACK;
1336 case FETCH_SEND_PACK:
1337 packet_write_fmt(1, "packfile\n");
1346 upload_pack_data_clear(&data);