upload-pack: teach deepen-relative in protocol v2
[git.git] / upload-pack.c
blob9df27b55a098135629ef4eccea4cb18aadb7dad3
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"
28 #include "commit-reach.h"
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
42 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
44 static timestamp_t oldest_have;
46 static int multi_ack;
47 static int no_done;
48 static int use_thin_pack, use_ofs_delta, use_include_tag;
49 static int no_progress, daemon_mode;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request;
57 static int shallow_nr;
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(const struct object_array *have_obj,
104 const struct object_array *want_obj)
106 struct child_process pack_objects = CHILD_PROCESS_INIT;
107 char data[8193], progress[128];
108 char abort_msg[] = "aborting due to possible repository "
109 "corruption on the remote side.";
110 int buffered = -1;
111 ssize_t sz;
112 int i;
113 FILE *pipe_fd;
115 if (!pack_objects_hook)
116 pack_objects.git_cmd = 1;
117 else {
118 argv_array_push(&pack_objects.args, pack_objects_hook);
119 argv_array_push(&pack_objects.args, "git");
120 pack_objects.use_shell = 1;
123 if (shallow_nr) {
124 argv_array_push(&pack_objects.args, "--shallow-file");
125 argv_array_push(&pack_objects.args, "");
127 argv_array_push(&pack_objects.args, "pack-objects");
128 argv_array_push(&pack_objects.args, "--revs");
129 if (use_thin_pack)
130 argv_array_push(&pack_objects.args, "--thin");
132 argv_array_push(&pack_objects.args, "--stdout");
133 if (shallow_nr)
134 argv_array_push(&pack_objects.args, "--shallow");
135 if (!no_progress)
136 argv_array_push(&pack_objects.args, "--progress");
137 if (use_ofs_delta)
138 argv_array_push(&pack_objects.args, "--delta-base-offset");
139 if (use_include_tag)
140 argv_array_push(&pack_objects.args, "--include-tag");
141 if (filter_options.filter_spec) {
142 if (pack_objects.use_shell) {
143 struct strbuf buf = STRBUF_INIT;
144 sq_quote_buf(&buf, filter_options.filter_spec);
145 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
146 strbuf_release(&buf);
147 } else {
148 argv_array_pushf(&pack_objects.args, "--filter=%s",
149 filter_options.filter_spec);
153 pack_objects.in = -1;
154 pack_objects.out = -1;
155 pack_objects.err = -1;
157 if (start_command(&pack_objects))
158 die("git upload-pack: unable to fork git-pack-objects");
160 pipe_fd = xfdopen(pack_objects.in, "w");
162 if (shallow_nr)
163 for_each_commit_graft(write_one_shallow, pipe_fd);
165 for (i = 0; i < want_obj->nr; i++)
166 fprintf(pipe_fd, "%s\n",
167 oid_to_hex(&want_obj->objects[i].item->oid));
168 fprintf(pipe_fd, "--not\n");
169 for (i = 0; i < have_obj->nr; i++)
170 fprintf(pipe_fd, "%s\n",
171 oid_to_hex(&have_obj->objects[i].item->oid));
172 for (i = 0; i < extra_edge_obj.nr; i++)
173 fprintf(pipe_fd, "%s\n",
174 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
175 fprintf(pipe_fd, "\n");
176 fflush(pipe_fd);
177 fclose(pipe_fd);
179 /* We read from pack_objects.err to capture stderr output for
180 * progress bar, and pack_objects.out to capture the pack data.
183 while (1) {
184 struct pollfd pfd[2];
185 int pe, pu, pollsize;
186 int ret;
188 reset_timeout();
190 pollsize = 0;
191 pe = pu = -1;
193 if (0 <= pack_objects.out) {
194 pfd[pollsize].fd = pack_objects.out;
195 pfd[pollsize].events = POLLIN;
196 pu = pollsize;
197 pollsize++;
199 if (0 <= pack_objects.err) {
200 pfd[pollsize].fd = pack_objects.err;
201 pfd[pollsize].events = POLLIN;
202 pe = pollsize;
203 pollsize++;
206 if (!pollsize)
207 break;
209 ret = poll(pfd, pollsize,
210 keepalive < 0 ? -1 : 1000 * keepalive);
212 if (ret < 0) {
213 if (errno != EINTR) {
214 error_errno("poll failed, resuming");
215 sleep(1);
217 continue;
219 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
220 /* Status ready; we ship that in the side-band
221 * or dump to the standard error.
223 sz = xread(pack_objects.err, progress,
224 sizeof(progress));
225 if (0 < sz)
226 send_client_data(2, progress, sz);
227 else if (sz == 0) {
228 close(pack_objects.err);
229 pack_objects.err = -1;
231 else
232 goto fail;
233 /* give priority to status messages */
234 continue;
236 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
237 /* Data ready; we keep the last byte to ourselves
238 * in case we detect broken rev-list, so that we
239 * can leave the stream corrupted. This is
240 * unfortunate -- unpack-objects would happily
241 * accept a valid packdata with trailing garbage,
242 * so appending garbage after we pass all the
243 * pack data is not good enough to signal
244 * breakage to downstream.
246 char *cp = data;
247 ssize_t outsz = 0;
248 if (0 <= buffered) {
249 *cp++ = buffered;
250 outsz++;
252 sz = xread(pack_objects.out, cp,
253 sizeof(data) - outsz);
254 if (0 < sz)
256 else if (sz == 0) {
257 close(pack_objects.out);
258 pack_objects.out = -1;
260 else
261 goto fail;
262 sz += outsz;
263 if (1 < sz) {
264 buffered = data[sz-1] & 0xFF;
265 sz--;
267 else
268 buffered = -1;
269 send_client_data(1, data, sz);
273 * We hit the keepalive timeout without saying anything; send
274 * an empty message on the data sideband just to let the other
275 * side know we're still working on it, but don't have any data
276 * yet.
278 * If we don't have a sideband channel, there's no room in the
279 * protocol to say anything, so those clients are just out of
280 * luck.
282 if (!ret && use_sideband) {
283 static const char buf[] = "0005\1";
284 write_or_die(1, buf, 5);
288 if (finish_command(&pack_objects)) {
289 error("git upload-pack: git-pack-objects died with error.");
290 goto fail;
293 /* flush the data */
294 if (0 <= buffered) {
295 data[0] = buffered;
296 send_client_data(1, data, 1);
297 fprintf(stderr, "flushed.\n");
299 if (use_sideband)
300 packet_flush(1);
301 return;
303 fail:
304 send_client_data(3, abort_msg, sizeof(abort_msg));
305 die("git upload-pack: %s", abort_msg);
308 static int got_oid(const char *hex, struct object_id *oid,
309 struct object_array *have_obj)
311 struct object *o;
312 int we_knew_they_have = 0;
314 if (get_oid_hex(hex, oid))
315 die("git upload-pack: expected SHA1 object, got '%s'", hex);
316 if (!has_object_file(oid))
317 return -1;
319 o = parse_object(the_repository, oid);
320 if (!o)
321 die("oops (%s)", oid_to_hex(oid));
322 if (o->type == OBJ_COMMIT) {
323 struct commit_list *parents;
324 struct commit *commit = (struct commit *)o;
325 if (o->flags & THEY_HAVE)
326 we_knew_they_have = 1;
327 else
328 o->flags |= THEY_HAVE;
329 if (!oldest_have || (commit->date < oldest_have))
330 oldest_have = commit->date;
331 for (parents = commit->parents;
332 parents;
333 parents = parents->next)
334 parents->item->object.flags |= THEY_HAVE;
336 if (!we_knew_they_have) {
337 add_object_array(o, NULL, have_obj);
338 return 1;
340 return 0;
343 static int ok_to_give_up(const struct object_array *have_obj,
344 struct object_array *want_obj)
346 uint32_t min_generation = GENERATION_NUMBER_ZERO;
348 if (!have_obj->nr)
349 return 0;
351 return can_all_from_reach_with_flag(want_obj, THEY_HAVE,
352 COMMON_KNOWN, oldest_have,
353 min_generation);
356 static int get_common_commits(struct object_array *have_obj,
357 struct object_array *want_obj)
359 struct object_id oid;
360 char last_hex[GIT_MAX_HEXSZ + 1];
361 int got_common = 0;
362 int got_other = 0;
363 int sent_ready = 0;
365 save_commit_buffer = 0;
367 for (;;) {
368 char *line = packet_read_line(0, NULL);
369 const char *arg;
371 reset_timeout();
373 if (!line) {
374 if (multi_ack == 2 && got_common
375 && !got_other && ok_to_give_up(have_obj, want_obj)) {
376 sent_ready = 1;
377 packet_write_fmt(1, "ACK %s ready\n", last_hex);
379 if (have_obj->nr == 0 || multi_ack)
380 packet_write_fmt(1, "NAK\n");
382 if (no_done && sent_ready) {
383 packet_write_fmt(1, "ACK %s\n", last_hex);
384 return 0;
386 if (stateless_rpc)
387 exit(0);
388 got_common = 0;
389 got_other = 0;
390 continue;
392 if (skip_prefix(line, "have ", &arg)) {
393 switch (got_oid(arg, &oid, have_obj)) {
394 case -1: /* they have what we do not */
395 got_other = 1;
396 if (multi_ack && ok_to_give_up(have_obj, want_obj)) {
397 const char *hex = oid_to_hex(&oid);
398 if (multi_ack == 2) {
399 sent_ready = 1;
400 packet_write_fmt(1, "ACK %s ready\n", hex);
401 } else
402 packet_write_fmt(1, "ACK %s continue\n", hex);
404 break;
405 default:
406 got_common = 1;
407 oid_to_hex_r(last_hex, &oid);
408 if (multi_ack == 2)
409 packet_write_fmt(1, "ACK %s common\n", last_hex);
410 else if (multi_ack)
411 packet_write_fmt(1, "ACK %s continue\n", last_hex);
412 else if (have_obj->nr == 1)
413 packet_write_fmt(1, "ACK %s\n", last_hex);
414 break;
416 continue;
418 if (!strcmp(line, "done")) {
419 if (have_obj->nr > 0) {
420 if (multi_ack)
421 packet_write_fmt(1, "ACK %s\n", last_hex);
422 return 0;
424 packet_write_fmt(1, "NAK\n");
425 return -1;
427 die("git upload-pack: expected SHA1 list, got '%s'", line);
431 static int is_our_ref(struct object *o)
433 int allow_hidden_ref = (allow_unadvertised_object_request &
434 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
435 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
439 * on successful case, it's up to the caller to close cmd->out
441 static int do_reachable_revlist(struct child_process *cmd,
442 struct object_array *src,
443 struct object_array *reachable)
445 static const char *argv[] = {
446 "rev-list", "--stdin", NULL,
448 struct object *o;
449 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
450 int i;
451 const unsigned hexsz = the_hash_algo->hexsz;
453 cmd->argv = argv;
454 cmd->git_cmd = 1;
455 cmd->no_stderr = 1;
456 cmd->in = -1;
457 cmd->out = -1;
460 * If the next rev-list --stdin encounters an unknown commit,
461 * it terminates, which will cause SIGPIPE in the write loop
462 * below.
464 sigchain_push(SIGPIPE, SIG_IGN);
466 if (start_command(cmd))
467 goto error;
469 namebuf[0] = '^';
470 namebuf[hexsz + 1] = '\n';
471 for (i = get_max_object_index(); 0 < i; ) {
472 o = get_indexed_object(--i);
473 if (!o)
474 continue;
475 if (reachable && o->type == OBJ_COMMIT)
476 o->flags &= ~TMP_MARK;
477 if (!is_our_ref(o))
478 continue;
479 memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
480 if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
481 goto error;
483 namebuf[hexsz] = '\n';
484 for (i = 0; i < src->nr; i++) {
485 o = src->objects[i].item;
486 if (is_our_ref(o)) {
487 if (reachable)
488 add_object_array(o, NULL, reachable);
489 continue;
491 if (reachable && o->type == OBJ_COMMIT)
492 o->flags |= TMP_MARK;
493 memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
494 if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
495 goto error;
497 close(cmd->in);
498 cmd->in = -1;
499 sigchain_pop(SIGPIPE);
501 return 0;
503 error:
504 sigchain_pop(SIGPIPE);
506 if (cmd->in >= 0)
507 close(cmd->in);
508 if (cmd->out >= 0)
509 close(cmd->out);
510 return -1;
513 static int get_reachable_list(struct object_array *src,
514 struct object_array *reachable)
516 struct child_process cmd = CHILD_PROCESS_INIT;
517 int i;
518 struct object *o;
519 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
520 const unsigned hexsz = the_hash_algo->hexsz;
522 if (do_reachable_revlist(&cmd, src, reachable) < 0)
523 return -1;
525 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
526 struct object_id sha1;
527 const char *p;
529 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
530 break;
532 o = lookup_object(the_repository, sha1.hash);
533 if (o && o->type == OBJ_COMMIT) {
534 o->flags &= ~TMP_MARK;
537 for (i = get_max_object_index(); 0 < i; i--) {
538 o = get_indexed_object(i - 1);
539 if (o && o->type == OBJ_COMMIT &&
540 (o->flags & TMP_MARK)) {
541 add_object_array(o, NULL, reachable);
542 o->flags &= ~TMP_MARK;
545 close(cmd.out);
547 if (finish_command(&cmd))
548 return -1;
550 return 0;
553 static int has_unreachable(struct object_array *src)
555 struct child_process cmd = CHILD_PROCESS_INIT;
556 char buf[1];
557 int i;
559 if (do_reachable_revlist(&cmd, src, NULL) < 0)
560 return 1;
563 * The commits out of the rev-list are not ancestors of
564 * our ref.
566 i = read_in_full(cmd.out, buf, 1);
567 if (i)
568 goto error;
569 close(cmd.out);
570 cmd.out = -1;
573 * rev-list may have died by encountering a bad commit
574 * in the history, in which case we do want to bail out
575 * even when it showed no commit.
577 if (finish_command(&cmd))
578 goto error;
580 /* All the non-tip ones are ancestors of what we advertised */
581 return 0;
583 error:
584 sigchain_pop(SIGPIPE);
585 if (cmd.out >= 0)
586 close(cmd.out);
587 return 1;
590 static void check_non_tip(struct object_array *want_obj)
592 int i;
595 * In the normal in-process case without
596 * uploadpack.allowReachableSHA1InWant,
597 * non-tip requests can never happen.
599 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
600 goto error;
601 if (!has_unreachable(want_obj))
602 /* All the non-tip ones are ancestors of what we advertised */
603 return;
605 error:
606 /* Pick one of them (we know there at least is one) */
607 for (i = 0; i < want_obj->nr; i++) {
608 struct object *o = want_obj->objects[i].item;
609 if (!is_our_ref(o))
610 die("git upload-pack: not our ref %s",
611 oid_to_hex(&o->oid));
615 static void send_shallow(struct commit_list *result)
617 while (result) {
618 struct object *object = &result->item->object;
619 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
620 packet_write_fmt(1, "shallow %s",
621 oid_to_hex(&object->oid));
622 register_shallow(the_repository, &object->oid);
623 shallow_nr++;
625 result = result->next;
629 static void send_unshallow(const struct object_array *shallows,
630 struct object_array *want_obj)
632 int i;
634 for (i = 0; i < shallows->nr; i++) {
635 struct object *object = shallows->objects[i].item;
636 if (object->flags & NOT_SHALLOW) {
637 struct commit_list *parents;
638 packet_write_fmt(1, "unshallow %s",
639 oid_to_hex(&object->oid));
640 object->flags &= ~CLIENT_SHALLOW;
642 * We want to _register_ "object" as shallow, but we
643 * also need to traverse object's parents to deepen a
644 * shallow clone. Unregister it for now so we can
645 * parse and add the parents to the want list, then
646 * re-register it.
648 unregister_shallow(&object->oid);
649 object->parsed = 0;
650 parse_commit_or_die((struct commit *)object);
651 parents = ((struct commit *)object)->parents;
652 while (parents) {
653 add_object_array(&parents->item->object,
654 NULL, want_obj);
655 parents = parents->next;
657 add_object_array(object, NULL, &extra_edge_obj);
659 /* make sure commit traversal conforms to client */
660 register_shallow(the_repository, &object->oid);
664 static int check_ref(const char *refname_full, const struct object_id *oid,
665 int flag, void *cb_data);
667 static void deepen(int depth, int deepen_relative,
668 struct object_array *shallows, struct object_array *want_obj)
670 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
671 int i;
673 for (i = 0; i < shallows->nr; i++) {
674 struct object *object = shallows->objects[i].item;
675 object->flags |= NOT_SHALLOW;
677 } else if (deepen_relative) {
678 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
679 struct commit_list *result;
682 * Checking for reachable shallows requires that our refs be
683 * marked with OUR_REF.
685 head_ref_namespaced(check_ref, NULL);
686 for_each_namespaced_ref(check_ref, NULL);
688 get_reachable_list(shallows, &reachable_shallows);
689 result = get_shallow_commits(&reachable_shallows,
690 depth + 1,
691 SHALLOW, NOT_SHALLOW);
692 send_shallow(result);
693 free_commit_list(result);
694 object_array_clear(&reachable_shallows);
695 } else {
696 struct commit_list *result;
698 result = get_shallow_commits(want_obj, depth,
699 SHALLOW, NOT_SHALLOW);
700 send_shallow(result);
701 free_commit_list(result);
704 send_unshallow(shallows, want_obj);
707 static void deepen_by_rev_list(int ac, const char **av,
708 struct object_array *shallows,
709 struct object_array *want_obj)
711 struct commit_list *result;
713 close_commit_graph(the_repository);
714 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
715 send_shallow(result);
716 free_commit_list(result);
717 send_unshallow(shallows, want_obj);
720 /* Returns 1 if a shallow list is sent or 0 otherwise */
721 static int send_shallow_list(int depth, int deepen_rev_list,
722 timestamp_t deepen_since,
723 struct string_list *deepen_not,
724 int deepen_relative,
725 struct object_array *shallows,
726 struct object_array *want_obj)
728 int ret = 0;
730 if (depth > 0 && deepen_rev_list)
731 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
732 if (depth > 0) {
733 deepen(depth, deepen_relative, shallows, want_obj);
734 ret = 1;
735 } else if (deepen_rev_list) {
736 struct argv_array av = ARGV_ARRAY_INIT;
737 int i;
739 argv_array_push(&av, "rev-list");
740 if (deepen_since)
741 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
742 if (deepen_not->nr) {
743 argv_array_push(&av, "--not");
744 for (i = 0; i < deepen_not->nr; i++) {
745 struct string_list_item *s = deepen_not->items + i;
746 argv_array_push(&av, s->string);
748 argv_array_push(&av, "--not");
750 for (i = 0; i < want_obj->nr; i++) {
751 struct object *o = want_obj->objects[i].item;
752 argv_array_push(&av, oid_to_hex(&o->oid));
754 deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
755 argv_array_clear(&av);
756 ret = 1;
757 } else {
758 if (shallows->nr > 0) {
759 int i;
760 for (i = 0; i < shallows->nr; i++)
761 register_shallow(the_repository,
762 &shallows->objects[i].item->oid);
766 shallow_nr += shallows->nr;
767 return ret;
770 static int process_shallow(const char *line, struct object_array *shallows)
772 const char *arg;
773 if (skip_prefix(line, "shallow ", &arg)) {
774 struct object_id oid;
775 struct object *object;
776 if (get_oid_hex(arg, &oid))
777 die("invalid shallow line: %s", line);
778 object = parse_object(the_repository, &oid);
779 if (!object)
780 return 1;
781 if (object->type != OBJ_COMMIT)
782 die("invalid shallow object %s", oid_to_hex(&oid));
783 if (!(object->flags & CLIENT_SHALLOW)) {
784 object->flags |= CLIENT_SHALLOW;
785 add_object_array(object, NULL, shallows);
787 return 1;
790 return 0;
793 static int process_deepen(const char *line, int *depth)
795 const char *arg;
796 if (skip_prefix(line, "deepen ", &arg)) {
797 char *end = NULL;
798 *depth = (int)strtol(arg, &end, 0);
799 if (!end || *end || *depth <= 0)
800 die("Invalid deepen: %s", line);
801 return 1;
804 return 0;
807 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
809 const char *arg;
810 if (skip_prefix(line, "deepen-since ", &arg)) {
811 char *end = NULL;
812 *deepen_since = parse_timestamp(arg, &end, 0);
813 if (!end || *end || !deepen_since ||
814 /* revisions.c's max_age -1 is special */
815 *deepen_since == -1)
816 die("Invalid deepen-since: %s", line);
817 *deepen_rev_list = 1;
818 return 1;
820 return 0;
823 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
825 const char *arg;
826 if (skip_prefix(line, "deepen-not ", &arg)) {
827 char *ref = NULL;
828 struct object_id oid;
829 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
830 die("git upload-pack: ambiguous deepen-not: %s", line);
831 string_list_append(deepen_not, ref);
832 free(ref);
833 *deepen_rev_list = 1;
834 return 1;
836 return 0;
839 static void receive_needs(struct object_array *want_obj)
841 struct object_array shallows = OBJECT_ARRAY_INIT;
842 struct string_list deepen_not = STRING_LIST_INIT_DUP;
843 int depth = 0;
844 int has_non_tip = 0;
845 timestamp_t deepen_since = 0;
846 int deepen_rev_list = 0;
847 int deepen_relative = 0;
849 shallow_nr = 0;
850 for (;;) {
851 struct object *o;
852 const char *features;
853 struct object_id oid_buf;
854 char *line = packet_read_line(0, NULL);
855 const char *arg;
857 reset_timeout();
858 if (!line)
859 break;
861 if (process_shallow(line, &shallows))
862 continue;
863 if (process_deepen(line, &depth))
864 continue;
865 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
866 continue;
867 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
868 continue;
870 if (skip_prefix(line, "filter ", &arg)) {
871 if (!filter_capability_requested)
872 die("git upload-pack: filtering capability not negotiated");
873 parse_list_objects_filter(&filter_options, arg);
874 continue;
877 if (!skip_prefix(line, "want ", &arg) ||
878 parse_oid_hex(arg, &oid_buf, &features))
879 die("git upload-pack: protocol error, "
880 "expected to get object ID, not '%s'", line);
882 if (parse_feature_request(features, "deepen-relative"))
883 deepen_relative = 1;
884 if (parse_feature_request(features, "multi_ack_detailed"))
885 multi_ack = 2;
886 else if (parse_feature_request(features, "multi_ack"))
887 multi_ack = 1;
888 if (parse_feature_request(features, "no-done"))
889 no_done = 1;
890 if (parse_feature_request(features, "thin-pack"))
891 use_thin_pack = 1;
892 if (parse_feature_request(features, "ofs-delta"))
893 use_ofs_delta = 1;
894 if (parse_feature_request(features, "side-band-64k"))
895 use_sideband = LARGE_PACKET_MAX;
896 else if (parse_feature_request(features, "side-band"))
897 use_sideband = DEFAULT_PACKET_MAX;
898 if (parse_feature_request(features, "no-progress"))
899 no_progress = 1;
900 if (parse_feature_request(features, "include-tag"))
901 use_include_tag = 1;
902 if (allow_filter && parse_feature_request(features, "filter"))
903 filter_capability_requested = 1;
905 o = parse_object(the_repository, &oid_buf);
906 if (!o) {
907 packet_write_fmt(1,
908 "ERR upload-pack: not our ref %s",
909 oid_to_hex(&oid_buf));
910 die("git upload-pack: not our ref %s",
911 oid_to_hex(&oid_buf));
913 if (!(o->flags & WANTED)) {
914 o->flags |= WANTED;
915 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
916 || is_our_ref(o)))
917 has_non_tip = 1;
918 add_object_array(o, NULL, want_obj);
923 * We have sent all our refs already, and the other end
924 * should have chosen out of them. When we are operating
925 * in the stateless RPC mode, however, their choice may
926 * have been based on the set of older refs advertised
927 * by another process that handled the initial request.
929 if (has_non_tip)
930 check_non_tip(want_obj);
932 if (!use_sideband && daemon_mode)
933 no_progress = 1;
935 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
936 return;
938 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
939 &deepen_not, deepen_relative, &shallows,
940 want_obj))
941 packet_flush(1);
942 object_array_clear(&shallows);
945 /* return non-zero if the ref is hidden, otherwise 0 */
946 static int mark_our_ref(const char *refname, const char *refname_full,
947 const struct object_id *oid)
949 struct object *o = lookup_unknown_object(oid->hash);
951 if (ref_is_hidden(refname, refname_full)) {
952 o->flags |= HIDDEN_REF;
953 return 1;
955 o->flags |= OUR_REF;
956 return 0;
959 static int check_ref(const char *refname_full, const struct object_id *oid,
960 int flag, void *cb_data)
962 const char *refname = strip_namespace(refname_full);
964 mark_our_ref(refname, refname_full, oid);
965 return 0;
968 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
970 struct string_list_item *item;
972 if (!symref->nr)
973 return;
974 for_each_string_list_item(item, symref)
975 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
978 static int send_ref(const char *refname, const struct object_id *oid,
979 int flag, void *cb_data)
981 static const char *capabilities = "multi_ack thin-pack side-band"
982 " side-band-64k ofs-delta shallow deepen-since deepen-not"
983 " deepen-relative no-progress include-tag multi_ack_detailed";
984 const char *refname_nons = strip_namespace(refname);
985 struct object_id peeled;
987 if (mark_our_ref(refname_nons, refname, oid))
988 return 0;
990 if (capabilities) {
991 struct strbuf symref_info = STRBUF_INIT;
993 format_symref_info(&symref_info, cb_data);
994 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
995 oid_to_hex(oid), refname_nons,
996 0, capabilities,
997 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
998 " allow-tip-sha1-in-want" : "",
999 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1000 " allow-reachable-sha1-in-want" : "",
1001 stateless_rpc ? " no-done" : "",
1002 symref_info.buf,
1003 allow_filter ? " filter" : "",
1004 git_user_agent_sanitized());
1005 strbuf_release(&symref_info);
1006 } else {
1007 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1009 capabilities = NULL;
1010 if (!peel_ref(refname, &peeled))
1011 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1012 return 0;
1015 static int find_symref(const char *refname, const struct object_id *oid,
1016 int flag, void *cb_data)
1018 const char *symref_target;
1019 struct string_list_item *item;
1021 if ((flag & REF_ISSYMREF) == 0)
1022 return 0;
1023 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1024 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1025 die("'%s' is a symref but it is not?", refname);
1026 item = string_list_append(cb_data, refname);
1027 item->util = xstrdup(symref_target);
1028 return 0;
1031 static int upload_pack_config(const char *var, const char *value, void *unused)
1033 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1034 if (git_config_bool(var, value))
1035 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1036 else
1037 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1038 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1039 if (git_config_bool(var, value))
1040 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1041 else
1042 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1043 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1044 if (git_config_bool(var, value))
1045 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1046 else
1047 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1048 } else if (!strcmp("uploadpack.keepalive", var)) {
1049 keepalive = git_config_int(var, value);
1050 if (!keepalive)
1051 keepalive = -1;
1052 } else if (!strcmp("uploadpack.allowfilter", var)) {
1053 allow_filter = git_config_bool(var, value);
1054 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1055 allow_ref_in_want = git_config_bool(var, value);
1058 if (current_config_scope() != CONFIG_SCOPE_REPO) {
1059 if (!strcmp("uploadpack.packobjectshook", var))
1060 return git_config_string(&pack_objects_hook, var, value);
1063 return parse_hide_refs_config(var, value, "uploadpack");
1066 void upload_pack(struct upload_pack_options *options)
1068 struct string_list symref = STRING_LIST_INIT_DUP;
1069 struct object_array want_obj = OBJECT_ARRAY_INIT;
1071 stateless_rpc = options->stateless_rpc;
1072 timeout = options->timeout;
1073 daemon_mode = options->daemon_mode;
1075 git_config(upload_pack_config, NULL);
1077 head_ref_namespaced(find_symref, &symref);
1079 if (options->advertise_refs || !stateless_rpc) {
1080 reset_timeout();
1081 head_ref_namespaced(send_ref, &symref);
1082 for_each_namespaced_ref(send_ref, &symref);
1083 advertise_shallow_grafts(1);
1084 packet_flush(1);
1085 } else {
1086 head_ref_namespaced(check_ref, NULL);
1087 for_each_namespaced_ref(check_ref, NULL);
1089 string_list_clear(&symref, 1);
1090 if (options->advertise_refs)
1091 return;
1093 receive_needs(&want_obj);
1094 if (want_obj.nr) {
1095 struct object_array have_obj = OBJECT_ARRAY_INIT;
1096 get_common_commits(&have_obj, &want_obj);
1097 create_pack_file(&have_obj, &want_obj);
1101 struct upload_pack_data {
1102 struct object_array wants;
1103 struct string_list wanted_refs;
1104 struct oid_array haves;
1106 struct object_array shallows;
1107 struct string_list deepen_not;
1108 int depth;
1109 timestamp_t deepen_since;
1110 int deepen_rev_list;
1111 int deepen_relative;
1113 unsigned stateless_rpc : 1;
1115 unsigned use_thin_pack : 1;
1116 unsigned use_ofs_delta : 1;
1117 unsigned no_progress : 1;
1118 unsigned use_include_tag : 1;
1119 unsigned done : 1;
1122 static void upload_pack_data_init(struct upload_pack_data *data)
1124 struct object_array wants = OBJECT_ARRAY_INIT;
1125 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1126 struct oid_array haves = OID_ARRAY_INIT;
1127 struct object_array shallows = OBJECT_ARRAY_INIT;
1128 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1130 memset(data, 0, sizeof(*data));
1131 data->wants = wants;
1132 data->wanted_refs = wanted_refs;
1133 data->haves = haves;
1134 data->shallows = shallows;
1135 data->deepen_not = deepen_not;
1138 static void upload_pack_data_clear(struct upload_pack_data *data)
1140 object_array_clear(&data->wants);
1141 string_list_clear(&data->wanted_refs, 1);
1142 oid_array_clear(&data->haves);
1143 object_array_clear(&data->shallows);
1144 string_list_clear(&data->deepen_not, 0);
1147 static int parse_want(const char *line, struct object_array *want_obj)
1149 const char *arg;
1150 if (skip_prefix(line, "want ", &arg)) {
1151 struct object_id oid;
1152 struct object *o;
1154 if (get_oid_hex(arg, &oid))
1155 die("git upload-pack: protocol error, "
1156 "expected to get oid, not '%s'", line);
1158 o = parse_object(the_repository, &oid);
1159 if (!o) {
1160 packet_write_fmt(1,
1161 "ERR upload-pack: not our ref %s",
1162 oid_to_hex(&oid));
1163 die("git upload-pack: not our ref %s",
1164 oid_to_hex(&oid));
1167 if (!(o->flags & WANTED)) {
1168 o->flags |= WANTED;
1169 add_object_array(o, NULL, want_obj);
1172 return 1;
1175 return 0;
1178 static int parse_want_ref(const char *line, struct string_list *wanted_refs,
1179 struct object_array *want_obj)
1181 const char *arg;
1182 if (skip_prefix(line, "want-ref ", &arg)) {
1183 struct object_id oid;
1184 struct string_list_item *item;
1185 struct object *o;
1187 if (read_ref(arg, &oid)) {
1188 packet_write_fmt(1, "ERR unknown ref %s", arg);
1189 die("unknown ref %s", arg);
1192 item = string_list_append(wanted_refs, arg);
1193 item->util = oiddup(&oid);
1195 o = parse_object_or_die(&oid, arg);
1196 if (!(o->flags & WANTED)) {
1197 o->flags |= WANTED;
1198 add_object_array(o, NULL, want_obj);
1201 return 1;
1204 return 0;
1207 static int parse_have(const char *line, struct oid_array *haves)
1209 const char *arg;
1210 if (skip_prefix(line, "have ", &arg)) {
1211 struct object_id oid;
1213 if (get_oid_hex(arg, &oid))
1214 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1215 oid_array_append(haves, &oid);
1216 return 1;
1219 return 0;
1222 static void process_args(struct packet_reader *request,
1223 struct upload_pack_data *data,
1224 struct object_array *want_obj)
1226 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1227 const char *arg = request->line;
1228 const char *p;
1230 /* process want */
1231 if (parse_want(arg, want_obj))
1232 continue;
1233 if (allow_ref_in_want &&
1234 parse_want_ref(arg, &data->wanted_refs, want_obj))
1235 continue;
1236 /* process have line */
1237 if (parse_have(arg, &data->haves))
1238 continue;
1240 /* process args like thin-pack */
1241 if (!strcmp(arg, "thin-pack")) {
1242 use_thin_pack = 1;
1243 continue;
1245 if (!strcmp(arg, "ofs-delta")) {
1246 use_ofs_delta = 1;
1247 continue;
1249 if (!strcmp(arg, "no-progress")) {
1250 no_progress = 1;
1251 continue;
1253 if (!strcmp(arg, "include-tag")) {
1254 use_include_tag = 1;
1255 continue;
1257 if (!strcmp(arg, "done")) {
1258 data->done = 1;
1259 continue;
1262 /* Shallow related arguments */
1263 if (process_shallow(arg, &data->shallows))
1264 continue;
1265 if (process_deepen(arg, &data->depth))
1266 continue;
1267 if (process_deepen_since(arg, &data->deepen_since,
1268 &data->deepen_rev_list))
1269 continue;
1270 if (process_deepen_not(arg, &data->deepen_not,
1271 &data->deepen_rev_list))
1272 continue;
1273 if (!strcmp(arg, "deepen-relative")) {
1274 data->deepen_relative = 1;
1275 continue;
1278 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1279 parse_list_objects_filter(&filter_options, p);
1280 continue;
1283 /* ignore unknown lines maybe? */
1284 die("unexpected line: '%s'", arg);
1288 static int process_haves(struct oid_array *haves, struct oid_array *common,
1289 struct object_array *have_obj)
1291 int i;
1293 /* Process haves */
1294 for (i = 0; i < haves->nr; i++) {
1295 const struct object_id *oid = &haves->oid[i];
1296 struct object *o;
1297 int we_knew_they_have = 0;
1299 if (!has_object_file(oid))
1300 continue;
1302 oid_array_append(common, oid);
1304 o = parse_object(the_repository, oid);
1305 if (!o)
1306 die("oops (%s)", oid_to_hex(oid));
1307 if (o->type == OBJ_COMMIT) {
1308 struct commit_list *parents;
1309 struct commit *commit = (struct commit *)o;
1310 if (o->flags & THEY_HAVE)
1311 we_knew_they_have = 1;
1312 else
1313 o->flags |= THEY_HAVE;
1314 if (!oldest_have || (commit->date < oldest_have))
1315 oldest_have = commit->date;
1316 for (parents = commit->parents;
1317 parents;
1318 parents = parents->next)
1319 parents->item->object.flags |= THEY_HAVE;
1321 if (!we_knew_they_have)
1322 add_object_array(o, NULL, have_obj);
1325 return 0;
1328 static int send_acks(struct oid_array *acks, struct strbuf *response,
1329 const struct object_array *have_obj,
1330 struct object_array *want_obj)
1332 int i;
1334 packet_buf_write(response, "acknowledgments\n");
1336 /* Send Acks */
1337 if (!acks->nr)
1338 packet_buf_write(response, "NAK\n");
1340 for (i = 0; i < acks->nr; i++) {
1341 packet_buf_write(response, "ACK %s\n",
1342 oid_to_hex(&acks->oid[i]));
1345 if (ok_to_give_up(have_obj, want_obj)) {
1346 /* Send Ready */
1347 packet_buf_write(response, "ready\n");
1348 return 1;
1351 return 0;
1354 static int process_haves_and_send_acks(struct upload_pack_data *data,
1355 struct object_array *have_obj,
1356 struct object_array *want_obj)
1358 struct oid_array common = OID_ARRAY_INIT;
1359 struct strbuf response = STRBUF_INIT;
1360 int ret = 0;
1362 process_haves(&data->haves, &common, have_obj);
1363 if (data->done) {
1364 ret = 1;
1365 } else if (send_acks(&common, &response, have_obj, want_obj)) {
1366 packet_buf_delim(&response);
1367 ret = 1;
1368 } else {
1369 /* Add Flush */
1370 packet_buf_flush(&response);
1371 ret = 0;
1374 /* Send response */
1375 write_or_die(1, response.buf, response.len);
1376 strbuf_release(&response);
1378 oid_array_clear(&data->haves);
1379 oid_array_clear(&common);
1380 return ret;
1383 static void send_wanted_ref_info(struct upload_pack_data *data)
1385 const struct string_list_item *item;
1387 if (!data->wanted_refs.nr)
1388 return;
1390 packet_write_fmt(1, "wanted-refs\n");
1392 for_each_string_list_item(item, &data->wanted_refs) {
1393 packet_write_fmt(1, "%s %s\n",
1394 oid_to_hex(item->util),
1395 item->string);
1398 packet_delim(1);
1401 static void send_shallow_info(struct upload_pack_data *data,
1402 struct object_array *want_obj)
1404 /* No shallow info needs to be sent */
1405 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1406 !is_repository_shallow(the_repository))
1407 return;
1409 packet_write_fmt(1, "shallow-info\n");
1411 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1412 data->deepen_since, &data->deepen_not,
1413 data->deepen_relative,
1414 &data->shallows, want_obj) &&
1415 is_repository_shallow(the_repository))
1416 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
1417 want_obj);
1419 packet_delim(1);
1422 enum fetch_state {
1423 FETCH_PROCESS_ARGS = 0,
1424 FETCH_SEND_ACKS,
1425 FETCH_SEND_PACK,
1426 FETCH_DONE,
1429 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1430 struct packet_reader *request)
1432 enum fetch_state state = FETCH_PROCESS_ARGS;
1433 struct upload_pack_data data;
1434 struct object_array have_obj = OBJECT_ARRAY_INIT;
1435 struct object_array want_obj = OBJECT_ARRAY_INIT;
1437 clear_object_flags(ALL_FLAGS);
1439 git_config(upload_pack_config, NULL);
1441 upload_pack_data_init(&data);
1442 use_sideband = LARGE_PACKET_MAX;
1444 while (state != FETCH_DONE) {
1445 switch (state) {
1446 case FETCH_PROCESS_ARGS:
1447 process_args(request, &data, &want_obj);
1449 if (!want_obj.nr) {
1451 * Request didn't contain any 'want' lines,
1452 * guess they didn't want anything.
1454 state = FETCH_DONE;
1455 } else if (data.haves.nr) {
1457 * Request had 'have' lines, so lets ACK them.
1459 state = FETCH_SEND_ACKS;
1460 } else {
1462 * Request had 'want's but no 'have's so we can
1463 * immedietly go to construct and send a pack.
1465 state = FETCH_SEND_PACK;
1467 break;
1468 case FETCH_SEND_ACKS:
1469 if (process_haves_and_send_acks(&data, &have_obj,
1470 &want_obj))
1471 state = FETCH_SEND_PACK;
1472 else
1473 state = FETCH_DONE;
1474 break;
1475 case FETCH_SEND_PACK:
1476 send_wanted_ref_info(&data);
1477 send_shallow_info(&data, &want_obj);
1479 packet_write_fmt(1, "packfile\n");
1480 create_pack_file(&have_obj, &want_obj);
1481 state = FETCH_DONE;
1482 break;
1483 case FETCH_DONE:
1484 continue;
1488 upload_pack_data_clear(&data);
1489 object_array_clear(&have_obj);
1490 object_array_clear(&want_obj);
1491 return 0;
1494 int upload_pack_advertise(struct repository *r,
1495 struct strbuf *value)
1497 if (value) {
1498 int allow_filter_value;
1499 int allow_ref_in_want;
1501 strbuf_addstr(value, "shallow");
1503 if (!repo_config_get_bool(the_repository,
1504 "uploadpack.allowfilter",
1505 &allow_filter_value) &&
1506 allow_filter_value)
1507 strbuf_addstr(value, " filter");
1509 if (!repo_config_get_bool(the_repository,
1510 "uploadpack.allowrefinwant",
1511 &allow_ref_in_want) &&
1512 allow_ref_in_want)
1513 strbuf_addstr(value, " ref-in-want");
1516 return 1;