ls-remote: pass ref prefixes when requesting a remote's refs
[git.git] / upload-pack.c
blob1e8a9e1cafef72122b39e55532a4009ff95b2bef
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "tag.h"
7 #include "object.h"
8 #include "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 #include "run-command.h"
13 #include "connect.h"
14 #include "sigchain.h"
15 #include "version.h"
16 #include "string-list.h"
17 #include "argv-array.h"
18 #include "prio-queue.h"
19 #include "protocol.h"
20 #include "upload-pack.h"
22 /* Remember to update object flag allocation in object.h */
23 #define THEY_HAVE (1u << 11)
24 #define OUR_REF (1u << 12)
25 #define WANTED (1u << 13)
26 #define COMMON_KNOWN (1u << 14)
27 #define REACHABLE (1u << 15)
29 #define SHALLOW (1u << 16)
30 #define NOT_SHALLOW (1u << 17)
31 #define CLIENT_SHALLOW (1u << 18)
32 #define HIDDEN_REF (1u << 19)
34 static timestamp_t oldest_have;
36 static int deepen_relative;
37 static int multi_ack;
38 static int no_done;
39 static int use_thin_pack, use_ofs_delta, use_include_tag;
40 static int no_progress, daemon_mode;
41 /* Allow specifying sha1 if it is a ref tip. */
42 #define ALLOW_TIP_SHA1 01
43 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
44 #define ALLOW_REACHABLE_SHA1 02
45 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
46 #define ALLOW_ANY_SHA1 07
47 static unsigned int allow_unadvertised_object_request;
48 static int shallow_nr;
49 static struct object_array have_obj;
50 static struct object_array want_obj;
51 static struct object_array extra_edge_obj;
52 static unsigned int timeout;
53 static int keepalive = 5;
54 /* 0 for no sideband,
55 * otherwise maximum packet size (up to 65520 bytes).
57 static int use_sideband;
58 static int stateless_rpc;
59 static const char *pack_objects_hook;
61 static void reset_timeout(void)
63 alarm(timeout);
66 static void send_client_data(int fd, const char *data, ssize_t sz)
68 if (use_sideband) {
69 send_sideband(1, fd, data, sz, use_sideband);
70 return;
72 if (fd == 3)
73 /* emergency quit */
74 fd = 2;
75 if (fd == 2) {
76 /* XXX: are we happy to lose stuff here? */
77 xwrite(fd, data, sz);
78 return;
80 write_or_die(fd, data, sz);
83 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
85 FILE *fp = cb_data;
86 if (graft->nr_parent == -1)
87 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
88 return 0;
91 static void create_pack_file(void)
93 struct child_process pack_objects = CHILD_PROCESS_INIT;
94 char data[8193], progress[128];
95 char abort_msg[] = "aborting due to possible repository "
96 "corruption on the remote side.";
97 int buffered = -1;
98 ssize_t sz;
99 int i;
100 FILE *pipe_fd;
102 if (!pack_objects_hook)
103 pack_objects.git_cmd = 1;
104 else {
105 argv_array_push(&pack_objects.args, pack_objects_hook);
106 argv_array_push(&pack_objects.args, "git");
107 pack_objects.use_shell = 1;
110 if (shallow_nr) {
111 argv_array_push(&pack_objects.args, "--shallow-file");
112 argv_array_push(&pack_objects.args, "");
114 argv_array_push(&pack_objects.args, "pack-objects");
115 argv_array_push(&pack_objects.args, "--revs");
116 if (use_thin_pack)
117 argv_array_push(&pack_objects.args, "--thin");
119 argv_array_push(&pack_objects.args, "--stdout");
120 if (shallow_nr)
121 argv_array_push(&pack_objects.args, "--shallow");
122 if (!no_progress)
123 argv_array_push(&pack_objects.args, "--progress");
124 if (use_ofs_delta)
125 argv_array_push(&pack_objects.args, "--delta-base-offset");
126 if (use_include_tag)
127 argv_array_push(&pack_objects.args, "--include-tag");
129 pack_objects.in = -1;
130 pack_objects.out = -1;
131 pack_objects.err = -1;
133 if (start_command(&pack_objects))
134 die("git upload-pack: unable to fork git-pack-objects");
136 pipe_fd = xfdopen(pack_objects.in, "w");
138 if (shallow_nr)
139 for_each_commit_graft(write_one_shallow, pipe_fd);
141 for (i = 0; i < want_obj.nr; i++)
142 fprintf(pipe_fd, "%s\n",
143 oid_to_hex(&want_obj.objects[i].item->oid));
144 fprintf(pipe_fd, "--not\n");
145 for (i = 0; i < have_obj.nr; i++)
146 fprintf(pipe_fd, "%s\n",
147 oid_to_hex(&have_obj.objects[i].item->oid));
148 for (i = 0; i < extra_edge_obj.nr; i++)
149 fprintf(pipe_fd, "%s\n",
150 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
151 fprintf(pipe_fd, "\n");
152 fflush(pipe_fd);
153 fclose(pipe_fd);
155 /* We read from pack_objects.err to capture stderr output for
156 * progress bar, and pack_objects.out to capture the pack data.
159 while (1) {
160 struct pollfd pfd[2];
161 int pe, pu, pollsize;
162 int ret;
164 reset_timeout();
166 pollsize = 0;
167 pe = pu = -1;
169 if (0 <= pack_objects.out) {
170 pfd[pollsize].fd = pack_objects.out;
171 pfd[pollsize].events = POLLIN;
172 pu = pollsize;
173 pollsize++;
175 if (0 <= pack_objects.err) {
176 pfd[pollsize].fd = pack_objects.err;
177 pfd[pollsize].events = POLLIN;
178 pe = pollsize;
179 pollsize++;
182 if (!pollsize)
183 break;
185 ret = poll(pfd, pollsize,
186 keepalive < 0 ? -1 : 1000 * keepalive);
188 if (ret < 0) {
189 if (errno != EINTR) {
190 error_errno("poll failed, resuming");
191 sleep(1);
193 continue;
195 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
196 /* Status ready; we ship that in the side-band
197 * or dump to the standard error.
199 sz = xread(pack_objects.err, progress,
200 sizeof(progress));
201 if (0 < sz)
202 send_client_data(2, progress, sz);
203 else if (sz == 0) {
204 close(pack_objects.err);
205 pack_objects.err = -1;
207 else
208 goto fail;
209 /* give priority to status messages */
210 continue;
212 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
213 /* Data ready; we keep the last byte to ourselves
214 * in case we detect broken rev-list, so that we
215 * can leave the stream corrupted. This is
216 * unfortunate -- unpack-objects would happily
217 * accept a valid packdata with trailing garbage,
218 * so appending garbage after we pass all the
219 * pack data is not good enough to signal
220 * breakage to downstream.
222 char *cp = data;
223 ssize_t outsz = 0;
224 if (0 <= buffered) {
225 *cp++ = buffered;
226 outsz++;
228 sz = xread(pack_objects.out, cp,
229 sizeof(data) - outsz);
230 if (0 < sz)
232 else if (sz == 0) {
233 close(pack_objects.out);
234 pack_objects.out = -1;
236 else
237 goto fail;
238 sz += outsz;
239 if (1 < sz) {
240 buffered = data[sz-1] & 0xFF;
241 sz--;
243 else
244 buffered = -1;
245 send_client_data(1, data, sz);
249 * We hit the keepalive timeout without saying anything; send
250 * an empty message on the data sideband just to let the other
251 * side know we're still working on it, but don't have any data
252 * yet.
254 * If we don't have a sideband channel, there's no room in the
255 * protocol to say anything, so those clients are just out of
256 * luck.
258 if (!ret && use_sideband) {
259 static const char buf[] = "0005\1";
260 write_or_die(1, buf, 5);
264 if (finish_command(&pack_objects)) {
265 error("git upload-pack: git-pack-objects died with error.");
266 goto fail;
269 /* flush the data */
270 if (0 <= buffered) {
271 data[0] = buffered;
272 send_client_data(1, data, 1);
273 fprintf(stderr, "flushed.\n");
275 if (use_sideband)
276 packet_flush(1);
277 return;
279 fail:
280 send_client_data(3, abort_msg, sizeof(abort_msg));
281 die("git upload-pack: %s", abort_msg);
284 static int got_oid(const char *hex, struct object_id *oid)
286 struct object *o;
287 int we_knew_they_have = 0;
289 if (get_oid_hex(hex, oid))
290 die("git upload-pack: expected SHA1 object, got '%s'", hex);
291 if (!has_object_file(oid))
292 return -1;
294 o = parse_object(oid);
295 if (!o)
296 die("oops (%s)", oid_to_hex(oid));
297 if (o->type == OBJ_COMMIT) {
298 struct commit_list *parents;
299 struct commit *commit = (struct commit *)o;
300 if (o->flags & THEY_HAVE)
301 we_knew_they_have = 1;
302 else
303 o->flags |= THEY_HAVE;
304 if (!oldest_have || (commit->date < oldest_have))
305 oldest_have = commit->date;
306 for (parents = commit->parents;
307 parents;
308 parents = parents->next)
309 parents->item->object.flags |= THEY_HAVE;
311 if (!we_knew_they_have) {
312 add_object_array(o, NULL, &have_obj);
313 return 1;
315 return 0;
318 static int reachable(struct commit *want)
320 struct prio_queue work = { compare_commits_by_commit_date };
322 prio_queue_put(&work, want);
323 while (work.nr) {
324 struct commit_list *list;
325 struct commit *commit = prio_queue_get(&work);
327 if (commit->object.flags & THEY_HAVE) {
328 want->object.flags |= COMMON_KNOWN;
329 break;
331 if (!commit->object.parsed)
332 parse_object(&commit->object.oid);
333 if (commit->object.flags & REACHABLE)
334 continue;
335 commit->object.flags |= REACHABLE;
336 if (commit->date < oldest_have)
337 continue;
338 for (list = commit->parents; list; list = list->next) {
339 struct commit *parent = list->item;
340 if (!(parent->object.flags & REACHABLE))
341 prio_queue_put(&work, parent);
344 want->object.flags |= REACHABLE;
345 clear_commit_marks(want, REACHABLE);
346 clear_prio_queue(&work);
347 return (want->object.flags & COMMON_KNOWN);
350 static int ok_to_give_up(void)
352 int i;
354 if (!have_obj.nr)
355 return 0;
357 for (i = 0; i < want_obj.nr; i++) {
358 struct object *want = want_obj.objects[i].item;
360 if (want->flags & COMMON_KNOWN)
361 continue;
362 want = deref_tag(want, "a want line", 0);
363 if (!want || want->type != OBJ_COMMIT) {
364 /* no way to tell if this is reachable by
365 * looking at the ancestry chain alone, so
366 * leave a note to ourselves not to worry about
367 * this object anymore.
369 want_obj.objects[i].item->flags |= COMMON_KNOWN;
370 continue;
372 if (!reachable((struct commit *)want))
373 return 0;
375 return 1;
378 static int get_common_commits(void)
380 struct object_id oid;
381 char last_hex[GIT_MAX_HEXSZ + 1];
382 int got_common = 0;
383 int got_other = 0;
384 int sent_ready = 0;
386 save_commit_buffer = 0;
388 for (;;) {
389 char *line = packet_read_line(0, NULL);
390 const char *arg;
392 reset_timeout();
394 if (!line) {
395 if (multi_ack == 2 && got_common
396 && !got_other && ok_to_give_up()) {
397 sent_ready = 1;
398 packet_write_fmt(1, "ACK %s ready\n", last_hex);
400 if (have_obj.nr == 0 || multi_ack)
401 packet_write_fmt(1, "NAK\n");
403 if (no_done && sent_ready) {
404 packet_write_fmt(1, "ACK %s\n", last_hex);
405 return 0;
407 if (stateless_rpc)
408 exit(0);
409 got_common = 0;
410 got_other = 0;
411 continue;
413 if (skip_prefix(line, "have ", &arg)) {
414 switch (got_oid(arg, &oid)) {
415 case -1: /* they have what we do not */
416 got_other = 1;
417 if (multi_ack && ok_to_give_up()) {
418 const char *hex = oid_to_hex(&oid);
419 if (multi_ack == 2) {
420 sent_ready = 1;
421 packet_write_fmt(1, "ACK %s ready\n", hex);
422 } else
423 packet_write_fmt(1, "ACK %s continue\n", hex);
425 break;
426 default:
427 got_common = 1;
428 memcpy(last_hex, oid_to_hex(&oid), 41);
429 if (multi_ack == 2)
430 packet_write_fmt(1, "ACK %s common\n", last_hex);
431 else if (multi_ack)
432 packet_write_fmt(1, "ACK %s continue\n", last_hex);
433 else if (have_obj.nr == 1)
434 packet_write_fmt(1, "ACK %s\n", last_hex);
435 break;
437 continue;
439 if (!strcmp(line, "done")) {
440 if (have_obj.nr > 0) {
441 if (multi_ack)
442 packet_write_fmt(1, "ACK %s\n", last_hex);
443 return 0;
445 packet_write_fmt(1, "NAK\n");
446 return -1;
448 die("git upload-pack: expected SHA1 list, got '%s'", line);
452 static int is_our_ref(struct object *o)
454 int allow_hidden_ref = (allow_unadvertised_object_request &
455 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
456 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
460 * on successful case, it's up to the caller to close cmd->out
462 static int do_reachable_revlist(struct child_process *cmd,
463 struct object_array *src,
464 struct object_array *reachable)
466 static const char *argv[] = {
467 "rev-list", "--stdin", NULL,
469 struct object *o;
470 char namebuf[42]; /* ^ + SHA-1 + LF */
471 int i;
473 cmd->argv = argv;
474 cmd->git_cmd = 1;
475 cmd->no_stderr = 1;
476 cmd->in = -1;
477 cmd->out = -1;
480 * If the next rev-list --stdin encounters an unknown commit,
481 * it terminates, which will cause SIGPIPE in the write loop
482 * below.
484 sigchain_push(SIGPIPE, SIG_IGN);
486 if (start_command(cmd))
487 goto error;
489 namebuf[0] = '^';
490 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
491 for (i = get_max_object_index(); 0 < i; ) {
492 o = get_indexed_object(--i);
493 if (!o)
494 continue;
495 if (reachable && o->type == OBJ_COMMIT)
496 o->flags &= ~TMP_MARK;
497 if (!is_our_ref(o))
498 continue;
499 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
500 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
501 goto error;
503 namebuf[GIT_SHA1_HEXSZ] = '\n';
504 for (i = 0; i < src->nr; i++) {
505 o = src->objects[i].item;
506 if (is_our_ref(o)) {
507 if (reachable)
508 add_object_array(o, NULL, reachable);
509 continue;
511 if (reachable && o->type == OBJ_COMMIT)
512 o->flags |= TMP_MARK;
513 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
514 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
515 goto error;
517 close(cmd->in);
518 cmd->in = -1;
519 sigchain_pop(SIGPIPE);
521 return 0;
523 error:
524 sigchain_pop(SIGPIPE);
526 if (cmd->in >= 0)
527 close(cmd->in);
528 if (cmd->out >= 0)
529 close(cmd->out);
530 return -1;
533 static int get_reachable_list(struct object_array *src,
534 struct object_array *reachable)
536 struct child_process cmd = CHILD_PROCESS_INIT;
537 int i;
538 struct object *o;
539 char namebuf[42]; /* ^ + SHA-1 + LF */
541 if (do_reachable_revlist(&cmd, src, reachable) < 0)
542 return -1;
544 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
545 struct object_id sha1;
547 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
548 break;
550 o = lookup_object(sha1.hash);
551 if (o && o->type == OBJ_COMMIT) {
552 o->flags &= ~TMP_MARK;
555 for (i = get_max_object_index(); 0 < i; i--) {
556 o = get_indexed_object(i - 1);
557 if (o && o->type == OBJ_COMMIT &&
558 (o->flags & TMP_MARK)) {
559 add_object_array(o, NULL, reachable);
560 o->flags &= ~TMP_MARK;
563 close(cmd.out);
565 if (finish_command(&cmd))
566 return -1;
568 return 0;
571 static int has_unreachable(struct object_array *src)
573 struct child_process cmd = CHILD_PROCESS_INIT;
574 char buf[1];
575 int i;
577 if (do_reachable_revlist(&cmd, src, NULL) < 0)
578 return 1;
581 * The commits out of the rev-list are not ancestors of
582 * our ref.
584 i = read_in_full(cmd.out, buf, 1);
585 if (i)
586 goto error;
587 close(cmd.out);
588 cmd.out = -1;
591 * rev-list may have died by encountering a bad commit
592 * in the history, in which case we do want to bail out
593 * even when it showed no commit.
595 if (finish_command(&cmd))
596 goto error;
598 /* All the non-tip ones are ancestors of what we advertised */
599 return 0;
601 error:
602 sigchain_pop(SIGPIPE);
603 if (cmd.out >= 0)
604 close(cmd.out);
605 return 1;
608 static void check_non_tip(void)
610 int i;
613 * In the normal in-process case without
614 * uploadpack.allowReachableSHA1InWant,
615 * non-tip requests can never happen.
617 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
618 goto error;
619 if (!has_unreachable(&want_obj))
620 /* All the non-tip ones are ancestors of what we advertised */
621 return;
623 error:
624 /* Pick one of them (we know there at least is one) */
625 for (i = 0; i < want_obj.nr; i++) {
626 struct object *o = want_obj.objects[i].item;
627 if (!is_our_ref(o))
628 die("git upload-pack: not our ref %s",
629 oid_to_hex(&o->oid));
633 static void send_shallow(struct commit_list *result)
635 while (result) {
636 struct object *object = &result->item->object;
637 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
638 packet_write_fmt(1, "shallow %s",
639 oid_to_hex(&object->oid));
640 register_shallow(&object->oid);
641 shallow_nr++;
643 result = result->next;
647 static void send_unshallow(const struct object_array *shallows)
649 int i;
651 for (i = 0; i < shallows->nr; i++) {
652 struct object *object = shallows->objects[i].item;
653 if (object->flags & NOT_SHALLOW) {
654 struct commit_list *parents;
655 packet_write_fmt(1, "unshallow %s",
656 oid_to_hex(&object->oid));
657 object->flags &= ~CLIENT_SHALLOW;
659 * We want to _register_ "object" as shallow, but we
660 * also need to traverse object's parents to deepen a
661 * shallow clone. Unregister it for now so we can
662 * parse and add the parents to the want list, then
663 * re-register it.
665 unregister_shallow(&object->oid);
666 object->parsed = 0;
667 parse_commit_or_die((struct commit *)object);
668 parents = ((struct commit *)object)->parents;
669 while (parents) {
670 add_object_array(&parents->item->object,
671 NULL, &want_obj);
672 parents = parents->next;
674 add_object_array(object, NULL, &extra_edge_obj);
676 /* make sure commit traversal conforms to client */
677 register_shallow(&object->oid);
681 static void deepen(int depth, int deepen_relative,
682 struct object_array *shallows)
684 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
685 int i;
687 for (i = 0; i < shallows->nr; i++) {
688 struct object *object = shallows->objects[i].item;
689 object->flags |= NOT_SHALLOW;
691 } else if (deepen_relative) {
692 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
693 struct commit_list *result;
695 get_reachable_list(shallows, &reachable_shallows);
696 result = get_shallow_commits(&reachable_shallows,
697 depth + 1,
698 SHALLOW, NOT_SHALLOW);
699 send_shallow(result);
700 free_commit_list(result);
701 object_array_clear(&reachable_shallows);
702 } else {
703 struct commit_list *result;
705 result = get_shallow_commits(&want_obj, depth,
706 SHALLOW, NOT_SHALLOW);
707 send_shallow(result);
708 free_commit_list(result);
711 send_unshallow(shallows);
712 packet_flush(1);
715 static void deepen_by_rev_list(int ac, const char **av,
716 struct object_array *shallows)
718 struct commit_list *result;
720 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
721 send_shallow(result);
722 free_commit_list(result);
723 send_unshallow(shallows);
724 packet_flush(1);
727 static int process_shallow(const char *line, struct object_array *shallows)
729 const char *arg;
730 if (skip_prefix(line, "shallow ", &arg)) {
731 struct object_id oid;
732 struct object *object;
733 if (get_oid_hex(arg, &oid))
734 die("invalid shallow line: %s", line);
735 object = parse_object(&oid);
736 if (!object)
737 return 1;
738 if (object->type != OBJ_COMMIT)
739 die("invalid shallow object %s", oid_to_hex(&oid));
740 if (!(object->flags & CLIENT_SHALLOW)) {
741 object->flags |= CLIENT_SHALLOW;
742 add_object_array(object, NULL, shallows);
744 return 1;
747 return 0;
750 static int process_deepen(const char *line, int *depth)
752 const char *arg;
753 if (skip_prefix(line, "deepen ", &arg)) {
754 char *end = NULL;
755 *depth = (int)strtol(arg, &end, 0);
756 if (!end || *end || *depth <= 0)
757 die("Invalid deepen: %s", line);
758 return 1;
761 return 0;
764 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
766 const char *arg;
767 if (skip_prefix(line, "deepen-since ", &arg)) {
768 char *end = NULL;
769 *deepen_since = parse_timestamp(arg, &end, 0);
770 if (!end || *end || !deepen_since ||
771 /* revisions.c's max_age -1 is special */
772 *deepen_since == -1)
773 die("Invalid deepen-since: %s", line);
774 *deepen_rev_list = 1;
775 return 1;
777 return 0;
780 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
782 const char *arg;
783 if (skip_prefix(line, "deepen-not ", &arg)) {
784 char *ref = NULL;
785 struct object_id oid;
786 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
787 die("git upload-pack: ambiguous deepen-not: %s", line);
788 string_list_append(deepen_not, ref);
789 free(ref);
790 *deepen_rev_list = 1;
791 return 1;
793 return 0;
796 static void receive_needs(void)
798 struct object_array shallows = OBJECT_ARRAY_INIT;
799 struct string_list deepen_not = STRING_LIST_INIT_DUP;
800 int depth = 0;
801 int has_non_tip = 0;
802 timestamp_t deepen_since = 0;
803 int deepen_rev_list = 0;
805 shallow_nr = 0;
806 for (;;) {
807 struct object *o;
808 const char *features;
809 struct object_id oid_buf;
810 char *line = packet_read_line(0, NULL);
811 const char *arg;
813 reset_timeout();
814 if (!line)
815 break;
817 if (process_shallow(line, &shallows))
818 continue;
819 if (process_deepen(line, &depth))
820 continue;
821 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
822 continue;
823 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
824 continue;
826 if (!skip_prefix(line, "want ", &arg) ||
827 get_oid_hex(arg, &oid_buf))
828 die("git upload-pack: protocol error, "
829 "expected to get sha, not '%s'", line);
831 features = arg + 40;
833 if (parse_feature_request(features, "deepen-relative"))
834 deepen_relative = 1;
835 if (parse_feature_request(features, "multi_ack_detailed"))
836 multi_ack = 2;
837 else if (parse_feature_request(features, "multi_ack"))
838 multi_ack = 1;
839 if (parse_feature_request(features, "no-done"))
840 no_done = 1;
841 if (parse_feature_request(features, "thin-pack"))
842 use_thin_pack = 1;
843 if (parse_feature_request(features, "ofs-delta"))
844 use_ofs_delta = 1;
845 if (parse_feature_request(features, "side-band-64k"))
846 use_sideband = LARGE_PACKET_MAX;
847 else if (parse_feature_request(features, "side-band"))
848 use_sideband = DEFAULT_PACKET_MAX;
849 if (parse_feature_request(features, "no-progress"))
850 no_progress = 1;
851 if (parse_feature_request(features, "include-tag"))
852 use_include_tag = 1;
854 o = parse_object(&oid_buf);
855 if (!o) {
856 packet_write_fmt(1,
857 "ERR upload-pack: not our ref %s",
858 oid_to_hex(&oid_buf));
859 die("git upload-pack: not our ref %s",
860 oid_to_hex(&oid_buf));
862 if (!(o->flags & WANTED)) {
863 o->flags |= WANTED;
864 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
865 || is_our_ref(o)))
866 has_non_tip = 1;
867 add_object_array(o, NULL, &want_obj);
872 * We have sent all our refs already, and the other end
873 * should have chosen out of them. When we are operating
874 * in the stateless RPC mode, however, their choice may
875 * have been based on the set of older refs advertised
876 * by another process that handled the initial request.
878 if (has_non_tip)
879 check_non_tip();
881 if (!use_sideband && daemon_mode)
882 no_progress = 1;
884 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
885 return;
886 if (depth > 0 && deepen_rev_list)
887 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
888 if (depth > 0)
889 deepen(depth, deepen_relative, &shallows);
890 else if (deepen_rev_list) {
891 struct argv_array av = ARGV_ARRAY_INIT;
892 int i;
894 argv_array_push(&av, "rev-list");
895 if (deepen_since)
896 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
897 if (deepen_not.nr) {
898 argv_array_push(&av, "--not");
899 for (i = 0; i < deepen_not.nr; i++) {
900 struct string_list_item *s = deepen_not.items + i;
901 argv_array_push(&av, s->string);
903 argv_array_push(&av, "--not");
905 for (i = 0; i < want_obj.nr; i++) {
906 struct object *o = want_obj.objects[i].item;
907 argv_array_push(&av, oid_to_hex(&o->oid));
909 deepen_by_rev_list(av.argc, av.argv, &shallows);
910 argv_array_clear(&av);
912 else
913 if (shallows.nr > 0) {
914 int i;
915 for (i = 0; i < shallows.nr; i++)
916 register_shallow(&shallows.objects[i].item->oid);
919 shallow_nr += shallows.nr;
920 object_array_clear(&shallows);
923 /* return non-zero if the ref is hidden, otherwise 0 */
924 static int mark_our_ref(const char *refname, const char *refname_full,
925 const struct object_id *oid)
927 struct object *o = lookup_unknown_object(oid->hash);
929 if (ref_is_hidden(refname, refname_full)) {
930 o->flags |= HIDDEN_REF;
931 return 1;
933 o->flags |= OUR_REF;
934 return 0;
937 static int check_ref(const char *refname_full, const struct object_id *oid,
938 int flag, void *cb_data)
940 const char *refname = strip_namespace(refname_full);
942 mark_our_ref(refname, refname_full, oid);
943 return 0;
946 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
948 struct string_list_item *item;
950 if (!symref->nr)
951 return;
952 for_each_string_list_item(item, symref)
953 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
956 static int send_ref(const char *refname, const struct object_id *oid,
957 int flag, void *cb_data)
959 static const char *capabilities = "multi_ack thin-pack side-band"
960 " side-band-64k ofs-delta shallow deepen-since deepen-not"
961 " deepen-relative no-progress include-tag multi_ack_detailed";
962 const char *refname_nons = strip_namespace(refname);
963 struct object_id peeled;
965 if (mark_our_ref(refname_nons, refname, oid))
966 return 0;
968 if (capabilities) {
969 struct strbuf symref_info = STRBUF_INIT;
971 format_symref_info(&symref_info, cb_data);
972 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
973 oid_to_hex(oid), refname_nons,
974 0, capabilities,
975 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
976 " allow-tip-sha1-in-want" : "",
977 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
978 " allow-reachable-sha1-in-want" : "",
979 stateless_rpc ? " no-done" : "",
980 symref_info.buf,
981 git_user_agent_sanitized());
982 strbuf_release(&symref_info);
983 } else {
984 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
986 capabilities = NULL;
987 if (!peel_ref(refname, &peeled))
988 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
989 return 0;
992 static int find_symref(const char *refname, const struct object_id *oid,
993 int flag, void *cb_data)
995 const char *symref_target;
996 struct string_list_item *item;
998 if ((flag & REF_ISSYMREF) == 0)
999 return 0;
1000 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1001 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1002 die("'%s' is a symref but it is not?", refname);
1003 item = string_list_append(cb_data, refname);
1004 item->util = xstrdup(symref_target);
1005 return 0;
1008 static int upload_pack_config(const char *var, const char *value, void *unused)
1010 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1011 if (git_config_bool(var, value))
1012 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1013 else
1014 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1015 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1016 if (git_config_bool(var, value))
1017 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1018 else
1019 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1020 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1021 if (git_config_bool(var, value))
1022 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1023 else
1024 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1025 } else if (!strcmp("uploadpack.keepalive", var)) {
1026 keepalive = git_config_int(var, value);
1027 if (!keepalive)
1028 keepalive = -1;
1029 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1030 if (!strcmp("uploadpack.packobjectshook", var))
1031 return git_config_string(&pack_objects_hook, var, value);
1033 return parse_hide_refs_config(var, value, "uploadpack");
1036 void upload_pack(struct upload_pack_options *options)
1038 struct string_list symref = STRING_LIST_INIT_DUP;
1040 stateless_rpc = options->stateless_rpc;
1041 timeout = options->timeout;
1042 daemon_mode = options->daemon_mode;
1044 git_config(upload_pack_config, NULL);
1046 head_ref_namespaced(find_symref, &symref);
1048 if (options->advertise_refs || !stateless_rpc) {
1049 reset_timeout();
1050 head_ref_namespaced(send_ref, &symref);
1051 for_each_namespaced_ref(send_ref, &symref);
1052 advertise_shallow_grafts(1);
1053 packet_flush(1);
1054 } else {
1055 head_ref_namespaced(check_ref, NULL);
1056 for_each_namespaced_ref(check_ref, NULL);
1058 string_list_clear(&symref, 1);
1059 if (options->advertise_refs)
1060 return;
1062 receive_needs();
1063 if (want_obj.nr) {
1064 get_common_commits();
1065 create_pack_file();