upload-pack: support hidden refs with protocol v2
[git.git] / upload-pack.c
blob9d4d6ad82f588d153dd7385f6c84600bbca53a5f
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "repository.h"
7 #include "object-store.h"
8 #include "tag.h"
9 #include "object.h"
10 #include "commit.h"
11 #include "diff.h"
12 #include "revision.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
17 #include "connect.h"
18 #include "sigchain.h"
19 #include "version.h"
20 #include "string-list.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
23 #include "protocol.h"
24 #include "quote.h"
25 #include "upload-pack.h"
26 #include "serve.h"
27 #include "commit-graph.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)
34 #define REACHABLE (1u << 15)
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;
44 static int multi_ack;
45 static int no_done;
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;
61 /* 0 for no sideband,
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)
75 alarm(timeout);
78 static void send_client_data(int fd, const char *data, ssize_t sz)
80 if (use_sideband) {
81 send_sideband(1, fd, data, sz, use_sideband);
82 return;
84 if (fd == 3)
85 /* emergency quit */
86 fd = 2;
87 if (fd == 2) {
88 /* XXX: are we happy to lose stuff here? */
89 xwrite(fd, data, sz);
90 return;
92 write_or_die(fd, data, sz);
95 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
97 FILE *fp = cb_data;
98 if (graft->nr_parent == -1)
99 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
100 return 0;
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.";
109 int buffered = -1;
110 ssize_t sz;
111 int i;
112 FILE *pipe_fd;
114 if (!pack_objects_hook)
115 pack_objects.git_cmd = 1;
116 else {
117 argv_array_push(&pack_objects.args, pack_objects_hook);
118 argv_array_push(&pack_objects.args, "git");
119 pack_objects.use_shell = 1;
122 if (shallow_nr) {
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");
128 if (use_thin_pack)
129 argv_array_push(&pack_objects.args, "--thin");
131 argv_array_push(&pack_objects.args, "--stdout");
132 if (shallow_nr)
133 argv_array_push(&pack_objects.args, "--shallow");
134 if (!no_progress)
135 argv_array_push(&pack_objects.args, "--progress");
136 if (use_ofs_delta)
137 argv_array_push(&pack_objects.args, "--delta-base-offset");
138 if (use_include_tag)
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);
146 } else {
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");
161 if (shallow_nr)
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");
175 fflush(pipe_fd);
176 fclose(pipe_fd);
178 /* We read from pack_objects.err to capture stderr output for
179 * progress bar, and pack_objects.out to capture the pack data.
182 while (1) {
183 struct pollfd pfd[2];
184 int pe, pu, pollsize;
185 int ret;
187 reset_timeout();
189 pollsize = 0;
190 pe = pu = -1;
192 if (0 <= pack_objects.out) {
193 pfd[pollsize].fd = pack_objects.out;
194 pfd[pollsize].events = POLLIN;
195 pu = pollsize;
196 pollsize++;
198 if (0 <= pack_objects.err) {
199 pfd[pollsize].fd = pack_objects.err;
200 pfd[pollsize].events = POLLIN;
201 pe = pollsize;
202 pollsize++;
205 if (!pollsize)
206 break;
208 ret = poll(pfd, pollsize,
209 keepalive < 0 ? -1 : 1000 * keepalive);
211 if (ret < 0) {
212 if (errno != EINTR) {
213 error_errno("poll failed, resuming");
214 sleep(1);
216 continue;
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,
223 sizeof(progress));
224 if (0 < sz)
225 send_client_data(2, progress, sz);
226 else if (sz == 0) {
227 close(pack_objects.err);
228 pack_objects.err = -1;
230 else
231 goto fail;
232 /* give priority to status messages */
233 continue;
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.
245 char *cp = data;
246 ssize_t outsz = 0;
247 if (0 <= buffered) {
248 *cp++ = buffered;
249 outsz++;
251 sz = xread(pack_objects.out, cp,
252 sizeof(data) - outsz);
253 if (0 < sz)
255 else if (sz == 0) {
256 close(pack_objects.out);
257 pack_objects.out = -1;
259 else
260 goto fail;
261 sz += outsz;
262 if (1 < sz) {
263 buffered = data[sz-1] & 0xFF;
264 sz--;
266 else
267 buffered = -1;
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
275 * yet.
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
279 * luck.
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.");
289 goto fail;
292 /* flush the data */
293 if (0 <= buffered) {
294 data[0] = buffered;
295 send_client_data(1, data, 1);
296 fprintf(stderr, "flushed.\n");
298 if (use_sideband)
299 packet_flush(1);
300 return;
302 fail:
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)
309 struct object *o;
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))
315 return -1;
317 o = parse_object(the_repository, oid);
318 if (!o)
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;
325 else
326 o->flags |= THEY_HAVE;
327 if (!oldest_have || (commit->date < oldest_have))
328 oldest_have = commit->date;
329 for (parents = commit->parents;
330 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);
336 return 1;
338 return 0;
341 static int reachable(struct commit *want)
343 struct prio_queue work = { compare_commits_by_commit_date };
345 prio_queue_put(&work, want);
346 while (work.nr) {
347 struct commit_list *list;
348 struct commit *commit = prio_queue_get(&work);
350 if (commit->object.flags & THEY_HAVE) {
351 want->object.flags |= COMMON_KNOWN;
352 break;
354 if (!commit->object.parsed)
355 parse_object(the_repository, &commit->object.oid);
356 if (commit->object.flags & REACHABLE)
357 continue;
358 commit->object.flags |= REACHABLE;
359 if (commit->date < oldest_have)
360 continue;
361 for (list = commit->parents; list; list = list->next) {
362 struct commit *parent = list->item;
363 if (!(parent->object.flags & REACHABLE))
364 prio_queue_put(&work, parent);
367 want->object.flags |= REACHABLE;
368 clear_commit_marks(want, REACHABLE);
369 clear_prio_queue(&work);
370 return (want->object.flags & COMMON_KNOWN);
373 static int ok_to_give_up(void)
375 int i;
377 if (!have_obj.nr)
378 return 0;
380 for (i = 0; i < want_obj.nr; i++) {
381 struct object *want = want_obj.objects[i].item;
383 if (want->flags & COMMON_KNOWN)
384 continue;
385 want = deref_tag(the_repository, want, "a want line", 0);
386 if (!want || want->type != OBJ_COMMIT) {
387 /* no way to tell if this is reachable by
388 * looking at the ancestry chain alone, so
389 * leave a note to ourselves not to worry about
390 * this object anymore.
392 want_obj.objects[i].item->flags |= COMMON_KNOWN;
393 continue;
395 if (!reachable((struct commit *)want))
396 return 0;
398 return 1;
401 static int get_common_commits(void)
403 struct object_id oid;
404 char last_hex[GIT_MAX_HEXSZ + 1];
405 int got_common = 0;
406 int got_other = 0;
407 int sent_ready = 0;
409 save_commit_buffer = 0;
411 for (;;) {
412 char *line = packet_read_line(0, NULL);
413 const char *arg;
415 reset_timeout();
417 if (!line) {
418 if (multi_ack == 2 && got_common
419 && !got_other && ok_to_give_up()) {
420 sent_ready = 1;
421 packet_write_fmt(1, "ACK %s ready\n", last_hex);
423 if (have_obj.nr == 0 || multi_ack)
424 packet_write_fmt(1, "NAK\n");
426 if (no_done && sent_ready) {
427 packet_write_fmt(1, "ACK %s\n", last_hex);
428 return 0;
430 if (stateless_rpc)
431 exit(0);
432 got_common = 0;
433 got_other = 0;
434 continue;
436 if (skip_prefix(line, "have ", &arg)) {
437 switch (got_oid(arg, &oid)) {
438 case -1: /* they have what we do not */
439 got_other = 1;
440 if (multi_ack && ok_to_give_up()) {
441 const char *hex = oid_to_hex(&oid);
442 if (multi_ack == 2) {
443 sent_ready = 1;
444 packet_write_fmt(1, "ACK %s ready\n", hex);
445 } else
446 packet_write_fmt(1, "ACK %s continue\n", hex);
448 break;
449 default:
450 got_common = 1;
451 oid_to_hex_r(last_hex, &oid);
452 if (multi_ack == 2)
453 packet_write_fmt(1, "ACK %s common\n", last_hex);
454 else if (multi_ack)
455 packet_write_fmt(1, "ACK %s continue\n", last_hex);
456 else if (have_obj.nr == 1)
457 packet_write_fmt(1, "ACK %s\n", last_hex);
458 break;
460 continue;
462 if (!strcmp(line, "done")) {
463 if (have_obj.nr > 0) {
464 if (multi_ack)
465 packet_write_fmt(1, "ACK %s\n", last_hex);
466 return 0;
468 packet_write_fmt(1, "NAK\n");
469 return -1;
471 die("git upload-pack: expected SHA1 list, got '%s'", line);
475 static int is_our_ref(struct object *o)
477 int allow_hidden_ref = (allow_unadvertised_object_request &
478 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
479 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
483 * on successful case, it's up to the caller to close cmd->out
485 static int do_reachable_revlist(struct child_process *cmd,
486 struct object_array *src,
487 struct object_array *reachable)
489 static const char *argv[] = {
490 "rev-list", "--stdin", NULL,
492 struct object *o;
493 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
494 int i;
496 cmd->argv = argv;
497 cmd->git_cmd = 1;
498 cmd->no_stderr = 1;
499 cmd->in = -1;
500 cmd->out = -1;
503 * If the next rev-list --stdin encounters an unknown commit,
504 * it terminates, which will cause SIGPIPE in the write loop
505 * below.
507 sigchain_push(SIGPIPE, SIG_IGN);
509 if (start_command(cmd))
510 goto error;
512 namebuf[0] = '^';
513 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
514 for (i = get_max_object_index(); 0 < i; ) {
515 o = get_indexed_object(--i);
516 if (!o)
517 continue;
518 if (reachable && o->type == OBJ_COMMIT)
519 o->flags &= ~TMP_MARK;
520 if (!is_our_ref(o))
521 continue;
522 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
523 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
524 goto error;
526 namebuf[GIT_SHA1_HEXSZ] = '\n';
527 for (i = 0; i < src->nr; i++) {
528 o = src->objects[i].item;
529 if (is_our_ref(o)) {
530 if (reachable)
531 add_object_array(o, NULL, reachable);
532 continue;
534 if (reachable && o->type == OBJ_COMMIT)
535 o->flags |= TMP_MARK;
536 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
537 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
538 goto error;
540 close(cmd->in);
541 cmd->in = -1;
542 sigchain_pop(SIGPIPE);
544 return 0;
546 error:
547 sigchain_pop(SIGPIPE);
549 if (cmd->in >= 0)
550 close(cmd->in);
551 if (cmd->out >= 0)
552 close(cmd->out);
553 return -1;
556 static int get_reachable_list(struct object_array *src,
557 struct object_array *reachable)
559 struct child_process cmd = CHILD_PROCESS_INIT;
560 int i;
561 struct object *o;
562 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
563 const unsigned hexsz = the_hash_algo->hexsz;
565 if (do_reachable_revlist(&cmd, src, reachable) < 0)
566 return -1;
568 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
569 struct object_id sha1;
570 const char *p;
572 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
573 break;
575 o = lookup_object(the_repository, sha1.hash);
576 if (o && o->type == OBJ_COMMIT) {
577 o->flags &= ~TMP_MARK;
580 for (i = get_max_object_index(); 0 < i; i--) {
581 o = get_indexed_object(i - 1);
582 if (o && o->type == OBJ_COMMIT &&
583 (o->flags & TMP_MARK)) {
584 add_object_array(o, NULL, reachable);
585 o->flags &= ~TMP_MARK;
588 close(cmd.out);
590 if (finish_command(&cmd))
591 return -1;
593 return 0;
596 static int has_unreachable(struct object_array *src)
598 struct child_process cmd = CHILD_PROCESS_INIT;
599 char buf[1];
600 int i;
602 if (do_reachable_revlist(&cmd, src, NULL) < 0)
603 return 1;
606 * The commits out of the rev-list are not ancestors of
607 * our ref.
609 i = read_in_full(cmd.out, buf, 1);
610 if (i)
611 goto error;
612 close(cmd.out);
613 cmd.out = -1;
616 * rev-list may have died by encountering a bad commit
617 * in the history, in which case we do want to bail out
618 * even when it showed no commit.
620 if (finish_command(&cmd))
621 goto error;
623 /* All the non-tip ones are ancestors of what we advertised */
624 return 0;
626 error:
627 sigchain_pop(SIGPIPE);
628 if (cmd.out >= 0)
629 close(cmd.out);
630 return 1;
633 static void check_non_tip(void)
635 int i;
638 * In the normal in-process case without
639 * uploadpack.allowReachableSHA1InWant,
640 * non-tip requests can never happen.
642 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
643 goto error;
644 if (!has_unreachable(&want_obj))
645 /* All the non-tip ones are ancestors of what we advertised */
646 return;
648 error:
649 /* Pick one of them (we know there at least is one) */
650 for (i = 0; i < want_obj.nr; i++) {
651 struct object *o = want_obj.objects[i].item;
652 if (!is_our_ref(o))
653 die("git upload-pack: not our ref %s",
654 oid_to_hex(&o->oid));
658 static void send_shallow(struct commit_list *result)
660 while (result) {
661 struct object *object = &result->item->object;
662 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
663 packet_write_fmt(1, "shallow %s",
664 oid_to_hex(&object->oid));
665 register_shallow(the_repository, &object->oid);
666 shallow_nr++;
668 result = result->next;
672 static void send_unshallow(const struct object_array *shallows)
674 int i;
676 for (i = 0; i < shallows->nr; i++) {
677 struct object *object = shallows->objects[i].item;
678 if (object->flags & NOT_SHALLOW) {
679 struct commit_list *parents;
680 packet_write_fmt(1, "unshallow %s",
681 oid_to_hex(&object->oid));
682 object->flags &= ~CLIENT_SHALLOW;
684 * We want to _register_ "object" as shallow, but we
685 * also need to traverse object's parents to deepen a
686 * shallow clone. Unregister it for now so we can
687 * parse and add the parents to the want list, then
688 * re-register it.
690 unregister_shallow(&object->oid);
691 object->parsed = 0;
692 parse_commit_or_die((struct commit *)object);
693 parents = ((struct commit *)object)->parents;
694 while (parents) {
695 add_object_array(&parents->item->object,
696 NULL, &want_obj);
697 parents = parents->next;
699 add_object_array(object, NULL, &extra_edge_obj);
701 /* make sure commit traversal conforms to client */
702 register_shallow(the_repository, &object->oid);
706 static void deepen(int depth, int deepen_relative,
707 struct object_array *shallows)
709 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
710 int i;
712 for (i = 0; i < shallows->nr; i++) {
713 struct object *object = shallows->objects[i].item;
714 object->flags |= NOT_SHALLOW;
716 } else if (deepen_relative) {
717 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
718 struct commit_list *result;
720 get_reachable_list(shallows, &reachable_shallows);
721 result = get_shallow_commits(&reachable_shallows,
722 depth + 1,
723 SHALLOW, NOT_SHALLOW);
724 send_shallow(result);
725 free_commit_list(result);
726 object_array_clear(&reachable_shallows);
727 } else {
728 struct commit_list *result;
730 result = get_shallow_commits(&want_obj, depth,
731 SHALLOW, NOT_SHALLOW);
732 send_shallow(result);
733 free_commit_list(result);
736 send_unshallow(shallows);
739 static void deepen_by_rev_list(int ac, const char **av,
740 struct object_array *shallows)
742 struct commit_list *result;
744 close_commit_graph(the_repository);
745 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
746 send_shallow(result);
747 free_commit_list(result);
748 send_unshallow(shallows);
751 /* Returns 1 if a shallow list is sent or 0 otherwise */
752 static int send_shallow_list(int depth, int deepen_rev_list,
753 timestamp_t deepen_since,
754 struct string_list *deepen_not,
755 struct object_array *shallows)
757 int ret = 0;
759 if (depth > 0 && deepen_rev_list)
760 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
761 if (depth > 0) {
762 deepen(depth, deepen_relative, shallows);
763 ret = 1;
764 } else if (deepen_rev_list) {
765 struct argv_array av = ARGV_ARRAY_INIT;
766 int i;
768 argv_array_push(&av, "rev-list");
769 if (deepen_since)
770 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
771 if (deepen_not->nr) {
772 argv_array_push(&av, "--not");
773 for (i = 0; i < deepen_not->nr; i++) {
774 struct string_list_item *s = deepen_not->items + i;
775 argv_array_push(&av, s->string);
777 argv_array_push(&av, "--not");
779 for (i = 0; i < want_obj.nr; i++) {
780 struct object *o = want_obj.objects[i].item;
781 argv_array_push(&av, oid_to_hex(&o->oid));
783 deepen_by_rev_list(av.argc, av.argv, shallows);
784 argv_array_clear(&av);
785 ret = 1;
786 } else {
787 if (shallows->nr > 0) {
788 int i;
789 for (i = 0; i < shallows->nr; i++)
790 register_shallow(the_repository,
791 &shallows->objects[i].item->oid);
795 shallow_nr += shallows->nr;
796 return ret;
799 static int process_shallow(const char *line, struct object_array *shallows)
801 const char *arg;
802 if (skip_prefix(line, "shallow ", &arg)) {
803 struct object_id oid;
804 struct object *object;
805 if (get_oid_hex(arg, &oid))
806 die("invalid shallow line: %s", line);
807 object = parse_object(the_repository, &oid);
808 if (!object)
809 return 1;
810 if (object->type != OBJ_COMMIT)
811 die("invalid shallow object %s", oid_to_hex(&oid));
812 if (!(object->flags & CLIENT_SHALLOW)) {
813 object->flags |= CLIENT_SHALLOW;
814 add_object_array(object, NULL, shallows);
816 return 1;
819 return 0;
822 static int process_deepen(const char *line, int *depth)
824 const char *arg;
825 if (skip_prefix(line, "deepen ", &arg)) {
826 char *end = NULL;
827 *depth = (int)strtol(arg, &end, 0);
828 if (!end || *end || *depth <= 0)
829 die("Invalid deepen: %s", line);
830 return 1;
833 return 0;
836 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
838 const char *arg;
839 if (skip_prefix(line, "deepen-since ", &arg)) {
840 char *end = NULL;
841 *deepen_since = parse_timestamp(arg, &end, 0);
842 if (!end || *end || !deepen_since ||
843 /* revisions.c's max_age -1 is special */
844 *deepen_since == -1)
845 die("Invalid deepen-since: %s", line);
846 *deepen_rev_list = 1;
847 return 1;
849 return 0;
852 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
854 const char *arg;
855 if (skip_prefix(line, "deepen-not ", &arg)) {
856 char *ref = NULL;
857 struct object_id oid;
858 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
859 die("git upload-pack: ambiguous deepen-not: %s", line);
860 string_list_append(deepen_not, ref);
861 free(ref);
862 *deepen_rev_list = 1;
863 return 1;
865 return 0;
868 static void receive_needs(void)
870 struct object_array shallows = OBJECT_ARRAY_INIT;
871 struct string_list deepen_not = STRING_LIST_INIT_DUP;
872 int depth = 0;
873 int has_non_tip = 0;
874 timestamp_t deepen_since = 0;
875 int deepen_rev_list = 0;
877 shallow_nr = 0;
878 for (;;) {
879 struct object *o;
880 const char *features;
881 struct object_id oid_buf;
882 char *line = packet_read_line(0, NULL);
883 const char *arg;
885 reset_timeout();
886 if (!line)
887 break;
889 if (process_shallow(line, &shallows))
890 continue;
891 if (process_deepen(line, &depth))
892 continue;
893 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
894 continue;
895 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
896 continue;
898 if (skip_prefix(line, "filter ", &arg)) {
899 if (!filter_capability_requested)
900 die("git upload-pack: filtering capability not negotiated");
901 parse_list_objects_filter(&filter_options, arg);
902 continue;
905 if (!skip_prefix(line, "want ", &arg) ||
906 parse_oid_hex(arg, &oid_buf, &features))
907 die("git upload-pack: protocol error, "
908 "expected to get object ID, not '%s'", line);
910 if (parse_feature_request(features, "deepen-relative"))
911 deepen_relative = 1;
912 if (parse_feature_request(features, "multi_ack_detailed"))
913 multi_ack = 2;
914 else if (parse_feature_request(features, "multi_ack"))
915 multi_ack = 1;
916 if (parse_feature_request(features, "no-done"))
917 no_done = 1;
918 if (parse_feature_request(features, "thin-pack"))
919 use_thin_pack = 1;
920 if (parse_feature_request(features, "ofs-delta"))
921 use_ofs_delta = 1;
922 if (parse_feature_request(features, "side-band-64k"))
923 use_sideband = LARGE_PACKET_MAX;
924 else if (parse_feature_request(features, "side-band"))
925 use_sideband = DEFAULT_PACKET_MAX;
926 if (parse_feature_request(features, "no-progress"))
927 no_progress = 1;
928 if (parse_feature_request(features, "include-tag"))
929 use_include_tag = 1;
930 if (allow_filter && parse_feature_request(features, "filter"))
931 filter_capability_requested = 1;
933 o = parse_object(the_repository, &oid_buf);
934 if (!o) {
935 packet_write_fmt(1,
936 "ERR upload-pack: not our ref %s",
937 oid_to_hex(&oid_buf));
938 die("git upload-pack: not our ref %s",
939 oid_to_hex(&oid_buf));
941 if (!(o->flags & WANTED)) {
942 o->flags |= WANTED;
943 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
944 || is_our_ref(o)))
945 has_non_tip = 1;
946 add_object_array(o, NULL, &want_obj);
951 * We have sent all our refs already, and the other end
952 * should have chosen out of them. When we are operating
953 * in the stateless RPC mode, however, their choice may
954 * have been based on the set of older refs advertised
955 * by another process that handled the initial request.
957 if (has_non_tip)
958 check_non_tip();
960 if (!use_sideband && daemon_mode)
961 no_progress = 1;
963 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
964 return;
966 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
967 &deepen_not, &shallows))
968 packet_flush(1);
969 object_array_clear(&shallows);
972 /* return non-zero if the ref is hidden, otherwise 0 */
973 static int mark_our_ref(const char *refname, const char *refname_full,
974 const struct object_id *oid)
976 struct object *o = lookup_unknown_object(oid->hash);
978 if (ref_is_hidden(refname, refname_full)) {
979 o->flags |= HIDDEN_REF;
980 return 1;
982 o->flags |= OUR_REF;
983 return 0;
986 static int check_ref(const char *refname_full, const struct object_id *oid,
987 int flag, void *cb_data)
989 const char *refname = strip_namespace(refname_full);
991 mark_our_ref(refname, refname_full, oid);
992 return 0;
995 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
997 struct string_list_item *item;
999 if (!symref->nr)
1000 return;
1001 for_each_string_list_item(item, symref)
1002 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1005 static int send_ref(const char *refname, const struct object_id *oid,
1006 int flag, void *cb_data)
1008 static const char *capabilities = "multi_ack thin-pack side-band"
1009 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1010 " deepen-relative no-progress include-tag multi_ack_detailed";
1011 const char *refname_nons = strip_namespace(refname);
1012 struct object_id peeled;
1014 if (mark_our_ref(refname_nons, refname, oid))
1015 return 0;
1017 if (capabilities) {
1018 struct strbuf symref_info = STRBUF_INIT;
1020 format_symref_info(&symref_info, cb_data);
1021 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1022 oid_to_hex(oid), refname_nons,
1023 0, capabilities,
1024 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1025 " allow-tip-sha1-in-want" : "",
1026 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1027 " allow-reachable-sha1-in-want" : "",
1028 stateless_rpc ? " no-done" : "",
1029 symref_info.buf,
1030 allow_filter ? " filter" : "",
1031 git_user_agent_sanitized());
1032 strbuf_release(&symref_info);
1033 } else {
1034 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1036 capabilities = NULL;
1037 if (!peel_ref(refname, &peeled))
1038 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1039 return 0;
1042 static int find_symref(const char *refname, const struct object_id *oid,
1043 int flag, void *cb_data)
1045 const char *symref_target;
1046 struct string_list_item *item;
1048 if ((flag & REF_ISSYMREF) == 0)
1049 return 0;
1050 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1051 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1052 die("'%s' is a symref but it is not?", refname);
1053 item = string_list_append(cb_data, refname);
1054 item->util = xstrdup(symref_target);
1055 return 0;
1058 static int upload_pack_config(const char *var, const char *value, void *unused)
1060 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1061 if (git_config_bool(var, value))
1062 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1063 else
1064 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1065 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1066 if (git_config_bool(var, value))
1067 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1068 else
1069 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1070 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1071 if (git_config_bool(var, value))
1072 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1073 else
1074 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1075 } else if (!strcmp("uploadpack.keepalive", var)) {
1076 keepalive = git_config_int(var, value);
1077 if (!keepalive)
1078 keepalive = -1;
1079 } else if (!strcmp("uploadpack.allowfilter", var)) {
1080 allow_filter = git_config_bool(var, value);
1081 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1082 allow_ref_in_want = git_config_bool(var, value);
1085 if (current_config_scope() != CONFIG_SCOPE_REPO) {
1086 if (!strcmp("uploadpack.packobjectshook", var))
1087 return git_config_string(&pack_objects_hook, var, value);
1090 return parse_hide_refs_config(var, value, "uploadpack");
1093 void upload_pack(struct upload_pack_options *options)
1095 struct string_list symref = STRING_LIST_INIT_DUP;
1097 stateless_rpc = options->stateless_rpc;
1098 timeout = options->timeout;
1099 daemon_mode = options->daemon_mode;
1101 git_config(upload_pack_config, NULL);
1103 head_ref_namespaced(find_symref, &symref);
1105 if (options->advertise_refs || !stateless_rpc) {
1106 reset_timeout();
1107 head_ref_namespaced(send_ref, &symref);
1108 for_each_namespaced_ref(send_ref, &symref);
1109 advertise_shallow_grafts(1);
1110 packet_flush(1);
1111 } else {
1112 head_ref_namespaced(check_ref, NULL);
1113 for_each_namespaced_ref(check_ref, NULL);
1115 string_list_clear(&symref, 1);
1116 if (options->advertise_refs)
1117 return;
1119 receive_needs();
1120 if (want_obj.nr) {
1121 get_common_commits();
1122 create_pack_file();
1126 struct upload_pack_data {
1127 struct object_array wants;
1128 struct string_list wanted_refs;
1129 struct oid_array haves;
1131 struct object_array shallows;
1132 struct string_list deepen_not;
1133 int depth;
1134 timestamp_t deepen_since;
1135 int deepen_rev_list;
1136 int deepen_relative;
1138 unsigned stateless_rpc : 1;
1140 unsigned use_thin_pack : 1;
1141 unsigned use_ofs_delta : 1;
1142 unsigned no_progress : 1;
1143 unsigned use_include_tag : 1;
1144 unsigned done : 1;
1147 static void upload_pack_data_init(struct upload_pack_data *data)
1149 struct object_array wants = OBJECT_ARRAY_INIT;
1150 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1151 struct oid_array haves = OID_ARRAY_INIT;
1152 struct object_array shallows = OBJECT_ARRAY_INIT;
1153 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1155 memset(data, 0, sizeof(*data));
1156 data->wants = wants;
1157 data->wanted_refs = wanted_refs;
1158 data->haves = haves;
1159 data->shallows = shallows;
1160 data->deepen_not = deepen_not;
1163 static void upload_pack_data_clear(struct upload_pack_data *data)
1165 object_array_clear(&data->wants);
1166 string_list_clear(&data->wanted_refs, 1);
1167 oid_array_clear(&data->haves);
1168 object_array_clear(&data->shallows);
1169 string_list_clear(&data->deepen_not, 0);
1172 static int parse_want(const char *line)
1174 const char *arg;
1175 if (skip_prefix(line, "want ", &arg)) {
1176 struct object_id oid;
1177 struct object *o;
1179 if (get_oid_hex(arg, &oid))
1180 die("git upload-pack: protocol error, "
1181 "expected to get oid, not '%s'", line);
1183 o = parse_object(the_repository, &oid);
1184 if (!o) {
1185 packet_write_fmt(1,
1186 "ERR upload-pack: not our ref %s",
1187 oid_to_hex(&oid));
1188 die("git upload-pack: not our ref %s",
1189 oid_to_hex(&oid));
1192 if (!(o->flags & WANTED)) {
1193 o->flags |= WANTED;
1194 add_object_array(o, NULL, &want_obj);
1197 return 1;
1200 return 0;
1203 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1205 const char *arg;
1206 if (skip_prefix(line, "want-ref ", &arg)) {
1207 struct object_id oid;
1208 struct string_list_item *item;
1209 struct object *o;
1211 if (read_ref(arg, &oid)) {
1212 packet_write_fmt(1, "ERR unknown ref %s", arg);
1213 die("unknown ref %s", arg);
1216 item = string_list_append(wanted_refs, arg);
1217 item->util = oiddup(&oid);
1219 o = parse_object_or_die(&oid, arg);
1220 if (!(o->flags & WANTED)) {
1221 o->flags |= WANTED;
1222 add_object_array(o, NULL, &want_obj);
1225 return 1;
1228 return 0;
1231 static int parse_have(const char *line, struct oid_array *haves)
1233 const char *arg;
1234 if (skip_prefix(line, "have ", &arg)) {
1235 struct object_id oid;
1237 if (get_oid_hex(arg, &oid))
1238 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1239 oid_array_append(haves, &oid);
1240 return 1;
1243 return 0;
1246 static void process_args(struct packet_reader *request,
1247 struct upload_pack_data *data)
1249 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1250 const char *arg = request->line;
1251 const char *p;
1253 /* process want */
1254 if (parse_want(arg))
1255 continue;
1256 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1257 continue;
1258 /* process have line */
1259 if (parse_have(arg, &data->haves))
1260 continue;
1262 /* process args like thin-pack */
1263 if (!strcmp(arg, "thin-pack")) {
1264 use_thin_pack = 1;
1265 continue;
1267 if (!strcmp(arg, "ofs-delta")) {
1268 use_ofs_delta = 1;
1269 continue;
1271 if (!strcmp(arg, "no-progress")) {
1272 no_progress = 1;
1273 continue;
1275 if (!strcmp(arg, "include-tag")) {
1276 use_include_tag = 1;
1277 continue;
1279 if (!strcmp(arg, "done")) {
1280 data->done = 1;
1281 continue;
1284 /* Shallow related arguments */
1285 if (process_shallow(arg, &data->shallows))
1286 continue;
1287 if (process_deepen(arg, &data->depth))
1288 continue;
1289 if (process_deepen_since(arg, &data->deepen_since,
1290 &data->deepen_rev_list))
1291 continue;
1292 if (process_deepen_not(arg, &data->deepen_not,
1293 &data->deepen_rev_list))
1294 continue;
1295 if (!strcmp(arg, "deepen-relative")) {
1296 data->deepen_relative = 1;
1297 continue;
1300 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1301 parse_list_objects_filter(&filter_options, p);
1302 continue;
1305 /* ignore unknown lines maybe? */
1306 die("unexpected line: '%s'", arg);
1310 static int process_haves(struct oid_array *haves, struct oid_array *common)
1312 int i;
1314 /* Process haves */
1315 for (i = 0; i < haves->nr; i++) {
1316 const struct object_id *oid = &haves->oid[i];
1317 struct object *o;
1318 int we_knew_they_have = 0;
1320 if (!has_object_file(oid))
1321 continue;
1323 oid_array_append(common, oid);
1325 o = parse_object(the_repository, oid);
1326 if (!o)
1327 die("oops (%s)", oid_to_hex(oid));
1328 if (o->type == OBJ_COMMIT) {
1329 struct commit_list *parents;
1330 struct commit *commit = (struct commit *)o;
1331 if (o->flags & THEY_HAVE)
1332 we_knew_they_have = 1;
1333 else
1334 o->flags |= THEY_HAVE;
1335 if (!oldest_have || (commit->date < oldest_have))
1336 oldest_have = commit->date;
1337 for (parents = commit->parents;
1338 parents;
1339 parents = parents->next)
1340 parents->item->object.flags |= THEY_HAVE;
1342 if (!we_knew_they_have)
1343 add_object_array(o, NULL, &have_obj);
1346 return 0;
1349 static int send_acks(struct oid_array *acks, struct strbuf *response)
1351 int i;
1353 packet_buf_write(response, "acknowledgments\n");
1355 /* Send Acks */
1356 if (!acks->nr)
1357 packet_buf_write(response, "NAK\n");
1359 for (i = 0; i < acks->nr; i++) {
1360 packet_buf_write(response, "ACK %s\n",
1361 oid_to_hex(&acks->oid[i]));
1364 if (ok_to_give_up()) {
1365 /* Send Ready */
1366 packet_buf_write(response, "ready\n");
1367 return 1;
1370 return 0;
1373 static int process_haves_and_send_acks(struct upload_pack_data *data)
1375 struct oid_array common = OID_ARRAY_INIT;
1376 struct strbuf response = STRBUF_INIT;
1377 int ret = 0;
1379 process_haves(&data->haves, &common);
1380 if (data->done) {
1381 ret = 1;
1382 } else if (send_acks(&common, &response)) {
1383 packet_buf_delim(&response);
1384 ret = 1;
1385 } else {
1386 /* Add Flush */
1387 packet_buf_flush(&response);
1388 ret = 0;
1391 /* Send response */
1392 write_or_die(1, response.buf, response.len);
1393 strbuf_release(&response);
1395 oid_array_clear(&data->haves);
1396 oid_array_clear(&common);
1397 return ret;
1400 static void send_wanted_ref_info(struct upload_pack_data *data)
1402 const struct string_list_item *item;
1404 if (!data->wanted_refs.nr)
1405 return;
1407 packet_write_fmt(1, "wanted-refs\n");
1409 for_each_string_list_item(item, &data->wanted_refs) {
1410 packet_write_fmt(1, "%s %s\n",
1411 oid_to_hex(item->util),
1412 item->string);
1415 packet_delim(1);
1418 static void send_shallow_info(struct upload_pack_data *data)
1420 /* No shallow info needs to be sent */
1421 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1422 !is_repository_shallow(the_repository))
1423 return;
1425 packet_write_fmt(1, "shallow-info\n");
1427 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1428 data->deepen_since, &data->deepen_not,
1429 &data->shallows) &&
1430 is_repository_shallow(the_repository))
1431 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1433 packet_delim(1);
1436 enum fetch_state {
1437 FETCH_PROCESS_ARGS = 0,
1438 FETCH_SEND_ACKS,
1439 FETCH_SEND_PACK,
1440 FETCH_DONE,
1443 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1444 struct packet_reader *request)
1446 enum fetch_state state = FETCH_PROCESS_ARGS;
1447 struct upload_pack_data data;
1449 git_config(upload_pack_config, NULL);
1451 upload_pack_data_init(&data);
1452 use_sideband = LARGE_PACKET_MAX;
1454 while (state != FETCH_DONE) {
1455 switch (state) {
1456 case FETCH_PROCESS_ARGS:
1457 process_args(request, &data);
1459 if (!want_obj.nr) {
1461 * Request didn't contain any 'want' lines,
1462 * guess they didn't want anything.
1464 state = FETCH_DONE;
1465 } else if (data.haves.nr) {
1467 * Request had 'have' lines, so lets ACK them.
1469 state = FETCH_SEND_ACKS;
1470 } else {
1472 * Request had 'want's but no 'have's so we can
1473 * immedietly go to construct and send a pack.
1475 state = FETCH_SEND_PACK;
1477 break;
1478 case FETCH_SEND_ACKS:
1479 if (process_haves_and_send_acks(&data))
1480 state = FETCH_SEND_PACK;
1481 else
1482 state = FETCH_DONE;
1483 break;
1484 case FETCH_SEND_PACK:
1485 send_wanted_ref_info(&data);
1486 send_shallow_info(&data);
1488 packet_write_fmt(1, "packfile\n");
1489 create_pack_file();
1490 state = FETCH_DONE;
1491 break;
1492 case FETCH_DONE:
1493 continue;
1497 upload_pack_data_clear(&data);
1498 return 0;
1501 int upload_pack_advertise(struct repository *r,
1502 struct strbuf *value)
1504 if (value) {
1505 int allow_filter_value;
1506 int allow_ref_in_want;
1508 strbuf_addstr(value, "shallow");
1510 if (!repo_config_get_bool(the_repository,
1511 "uploadpack.allowfilter",
1512 &allow_filter_value) &&
1513 allow_filter_value)
1514 strbuf_addstr(value, " filter");
1516 if (!repo_config_get_bool(the_repository,
1517 "uploadpack.allowrefinwant",
1518 &allow_ref_in_want) &&
1519 allow_ref_in_want)
1520 strbuf_addstr(value, " ref-in-want");
1523 return 1;