upload-pack: refactor ok_to_give_up()
[git.git] / upload-pack.c
blob9fe19003c6b7f511c2fcb3b5280c3c823dc33eaa
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"
28 /* Remember to update object flag allocation in object.h */
29 #define THEY_HAVE (1u << 11)
30 #define OUR_REF (1u << 12)
31 #define WANTED (1u << 13)
32 #define COMMON_KNOWN (1u << 14)
33 #define REACHABLE (1u << 15)
35 #define SHALLOW (1u << 16)
36 #define NOT_SHALLOW (1u << 17)
37 #define CLIENT_SHALLOW (1u << 18)
38 #define HIDDEN_REF (1u << 19)
40 static timestamp_t oldest_have;
42 static int deepen_relative;
43 static int multi_ack;
44 static int no_done;
45 static int use_thin_pack, use_ofs_delta, use_include_tag;
46 static int no_progress, daemon_mode;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request;
54 static int shallow_nr;
55 static struct object_array have_obj;
56 static struct object_array want_obj;
57 static struct object_array extra_edge_obj;
58 static unsigned int timeout;
59 static int keepalive = 5;
60 /* 0 for no sideband,
61 * otherwise maximum packet size (up to 65520 bytes).
63 static int use_sideband;
64 static int stateless_rpc;
65 static const char *pack_objects_hook;
67 static int filter_capability_requested;
68 static int allow_filter;
69 static struct list_objects_filter_options filter_options;
71 static void reset_timeout(void)
73 alarm(timeout);
76 static void send_client_data(int fd, const char *data, ssize_t sz)
78 if (use_sideband) {
79 send_sideband(1, fd, data, sz, use_sideband);
80 return;
82 if (fd == 3)
83 /* emergency quit */
84 fd = 2;
85 if (fd == 2) {
86 /* XXX: are we happy to lose stuff here? */
87 xwrite(fd, data, sz);
88 return;
90 write_or_die(fd, data, sz);
93 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
95 FILE *fp = cb_data;
96 if (graft->nr_parent == -1)
97 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
98 return 0;
101 static void create_pack_file(void)
103 struct child_process pack_objects = CHILD_PROCESS_INIT;
104 char data[8193], progress[128];
105 char abort_msg[] = "aborting due to possible repository "
106 "corruption on the remote side.";
107 int buffered = -1;
108 ssize_t sz;
109 int i;
110 FILE *pipe_fd;
112 if (!pack_objects_hook)
113 pack_objects.git_cmd = 1;
114 else {
115 argv_array_push(&pack_objects.args, pack_objects_hook);
116 argv_array_push(&pack_objects.args, "git");
117 pack_objects.use_shell = 1;
120 if (shallow_nr) {
121 argv_array_push(&pack_objects.args, "--shallow-file");
122 argv_array_push(&pack_objects.args, "");
124 argv_array_push(&pack_objects.args, "pack-objects");
125 argv_array_push(&pack_objects.args, "--revs");
126 if (use_thin_pack)
127 argv_array_push(&pack_objects.args, "--thin");
129 argv_array_push(&pack_objects.args, "--stdout");
130 if (shallow_nr)
131 argv_array_push(&pack_objects.args, "--shallow");
132 if (!no_progress)
133 argv_array_push(&pack_objects.args, "--progress");
134 if (use_ofs_delta)
135 argv_array_push(&pack_objects.args, "--delta-base-offset");
136 if (use_include_tag)
137 argv_array_push(&pack_objects.args, "--include-tag");
138 if (filter_options.filter_spec) {
139 if (pack_objects.use_shell) {
140 struct strbuf buf = STRBUF_INIT;
141 sq_quote_buf(&buf, filter_options.filter_spec);
142 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
143 strbuf_release(&buf);
144 } else {
145 argv_array_pushf(&pack_objects.args, "--filter=%s",
146 filter_options.filter_spec);
150 pack_objects.in = -1;
151 pack_objects.out = -1;
152 pack_objects.err = -1;
154 if (start_command(&pack_objects))
155 die("git upload-pack: unable to fork git-pack-objects");
157 pipe_fd = xfdopen(pack_objects.in, "w");
159 if (shallow_nr)
160 for_each_commit_graft(write_one_shallow, pipe_fd);
162 for (i = 0; i < want_obj.nr; i++)
163 fprintf(pipe_fd, "%s\n",
164 oid_to_hex(&want_obj.objects[i].item->oid));
165 fprintf(pipe_fd, "--not\n");
166 for (i = 0; i < have_obj.nr; i++)
167 fprintf(pipe_fd, "%s\n",
168 oid_to_hex(&have_obj.objects[i].item->oid));
169 for (i = 0; i < extra_edge_obj.nr; i++)
170 fprintf(pipe_fd, "%s\n",
171 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
172 fprintf(pipe_fd, "\n");
173 fflush(pipe_fd);
174 fclose(pipe_fd);
176 /* We read from pack_objects.err to capture stderr output for
177 * progress bar, and pack_objects.out to capture the pack data.
180 while (1) {
181 struct pollfd pfd[2];
182 int pe, pu, pollsize;
183 int ret;
185 reset_timeout();
187 pollsize = 0;
188 pe = pu = -1;
190 if (0 <= pack_objects.out) {
191 pfd[pollsize].fd = pack_objects.out;
192 pfd[pollsize].events = POLLIN;
193 pu = pollsize;
194 pollsize++;
196 if (0 <= pack_objects.err) {
197 pfd[pollsize].fd = pack_objects.err;
198 pfd[pollsize].events = POLLIN;
199 pe = pollsize;
200 pollsize++;
203 if (!pollsize)
204 break;
206 ret = poll(pfd, pollsize,
207 keepalive < 0 ? -1 : 1000 * keepalive);
209 if (ret < 0) {
210 if (errno != EINTR) {
211 error_errno("poll failed, resuming");
212 sleep(1);
214 continue;
216 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
217 /* Status ready; we ship that in the side-band
218 * or dump to the standard error.
220 sz = xread(pack_objects.err, progress,
221 sizeof(progress));
222 if (0 < sz)
223 send_client_data(2, progress, sz);
224 else if (sz == 0) {
225 close(pack_objects.err);
226 pack_objects.err = -1;
228 else
229 goto fail;
230 /* give priority to status messages */
231 continue;
233 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
234 /* Data ready; we keep the last byte to ourselves
235 * in case we detect broken rev-list, so that we
236 * can leave the stream corrupted. This is
237 * unfortunate -- unpack-objects would happily
238 * accept a valid packdata with trailing garbage,
239 * so appending garbage after we pass all the
240 * pack data is not good enough to signal
241 * breakage to downstream.
243 char *cp = data;
244 ssize_t outsz = 0;
245 if (0 <= buffered) {
246 *cp++ = buffered;
247 outsz++;
249 sz = xread(pack_objects.out, cp,
250 sizeof(data) - outsz);
251 if (0 < sz)
253 else if (sz == 0) {
254 close(pack_objects.out);
255 pack_objects.out = -1;
257 else
258 goto fail;
259 sz += outsz;
260 if (1 < sz) {
261 buffered = data[sz-1] & 0xFF;
262 sz--;
264 else
265 buffered = -1;
266 send_client_data(1, data, sz);
270 * We hit the keepalive timeout without saying anything; send
271 * an empty message on the data sideband just to let the other
272 * side know we're still working on it, but don't have any data
273 * yet.
275 * If we don't have a sideband channel, there's no room in the
276 * protocol to say anything, so those clients are just out of
277 * luck.
279 if (!ret && use_sideband) {
280 static const char buf[] = "0005\1";
281 write_or_die(1, buf, 5);
285 if (finish_command(&pack_objects)) {
286 error("git upload-pack: git-pack-objects died with error.");
287 goto fail;
290 /* flush the data */
291 if (0 <= buffered) {
292 data[0] = buffered;
293 send_client_data(1, data, 1);
294 fprintf(stderr, "flushed.\n");
296 if (use_sideband)
297 packet_flush(1);
298 return;
300 fail:
301 send_client_data(3, abort_msg, sizeof(abort_msg));
302 die("git upload-pack: %s", abort_msg);
305 static int got_oid(const char *hex, struct object_id *oid)
307 struct object *o;
308 int we_knew_they_have = 0;
310 if (get_oid_hex(hex, oid))
311 die("git upload-pack: expected SHA1 object, got '%s'", hex);
312 if (!has_object_file(oid))
313 return -1;
315 o = parse_object(the_repository, oid);
316 if (!o)
317 die("oops (%s)", oid_to_hex(oid));
318 if (o->type == OBJ_COMMIT) {
319 struct commit_list *parents;
320 struct commit *commit = (struct commit *)o;
321 if (o->flags & THEY_HAVE)
322 we_knew_they_have = 1;
323 else
324 o->flags |= THEY_HAVE;
325 if (!oldest_have || (commit->date < oldest_have))
326 oldest_have = commit->date;
327 for (parents = commit->parents;
328 parents;
329 parents = parents->next)
330 parents->item->object.flags |= THEY_HAVE;
332 if (!we_knew_they_have) {
333 add_object_array(o, NULL, &have_obj);
334 return 1;
336 return 0;
339 static int reachable(struct commit *from, unsigned int with_flag,
340 unsigned int assign_flag)
342 struct prio_queue work = { compare_commits_by_commit_date };
344 prio_queue_put(&work, from);
345 while (work.nr) {
346 struct commit_list *list;
347 struct commit *commit = prio_queue_get(&work);
349 if (commit->object.flags & with_flag) {
350 from->object.flags |= assign_flag;
351 break;
353 if (!commit->object.parsed)
354 parse_object(the_repository, &commit->object.oid);
355 if (commit->object.flags & REACHABLE)
356 continue;
357 commit->object.flags |= REACHABLE;
358 if (commit->date < oldest_have)
359 continue;
360 for (list = commit->parents; list; list = list->next) {
361 struct commit *parent = list->item;
362 if (!(parent->object.flags & REACHABLE))
363 prio_queue_put(&work, parent);
366 from->object.flags |= REACHABLE;
367 clear_commit_marks(from, REACHABLE);
368 clear_prio_queue(&work);
369 return (from->object.flags & assign_flag);
373 * Determine if every commit in 'from' can reach at least one commit
374 * that is marked with 'with_flag'. As we traverse, use 'assign_flag'
375 * as a marker for commits that are already visited.
377 static int can_all_from_reach_with_flag(struct object_array *from,
378 unsigned int with_flag,
379 unsigned int assign_flag)
381 int i;
383 for (i = 0; i < from->nr; i++) {
384 struct object *from_one = from->objects[i].item;
386 if (from_one->flags & assign_flag)
387 continue;
388 from_one = deref_tag(the_repository, from_one, "a from object", 0);
389 if (!from_one || from_one->type != OBJ_COMMIT) {
390 /* no way to tell if this is reachable by
391 * looking at the ancestry chain alone, so
392 * leave a note to ourselves not to worry about
393 * this object anymore.
395 from->objects[i].item->flags |= assign_flag;
396 continue;
398 if (!reachable((struct commit *)from_one, with_flag, assign_flag))
399 return 0;
401 return 1;
404 static int ok_to_give_up(void)
406 if (!have_obj.nr)
407 return 0;
409 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE, COMMON_KNOWN);
412 static int get_common_commits(void)
414 struct object_id oid;
415 char last_hex[GIT_MAX_HEXSZ + 1];
416 int got_common = 0;
417 int got_other = 0;
418 int sent_ready = 0;
420 save_commit_buffer = 0;
422 for (;;) {
423 char *line = packet_read_line(0, NULL);
424 const char *arg;
426 reset_timeout();
428 if (!line) {
429 if (multi_ack == 2 && got_common
430 && !got_other && ok_to_give_up()) {
431 sent_ready = 1;
432 packet_write_fmt(1, "ACK %s ready\n", last_hex);
434 if (have_obj.nr == 0 || multi_ack)
435 packet_write_fmt(1, "NAK\n");
437 if (no_done && sent_ready) {
438 packet_write_fmt(1, "ACK %s\n", last_hex);
439 return 0;
441 if (stateless_rpc)
442 exit(0);
443 got_common = 0;
444 got_other = 0;
445 continue;
447 if (skip_prefix(line, "have ", &arg)) {
448 switch (got_oid(arg, &oid)) {
449 case -1: /* they have what we do not */
450 got_other = 1;
451 if (multi_ack && ok_to_give_up()) {
452 const char *hex = oid_to_hex(&oid);
453 if (multi_ack == 2) {
454 sent_ready = 1;
455 packet_write_fmt(1, "ACK %s ready\n", hex);
456 } else
457 packet_write_fmt(1, "ACK %s continue\n", hex);
459 break;
460 default:
461 got_common = 1;
462 oid_to_hex_r(last_hex, &oid);
463 if (multi_ack == 2)
464 packet_write_fmt(1, "ACK %s common\n", last_hex);
465 else if (multi_ack)
466 packet_write_fmt(1, "ACK %s continue\n", last_hex);
467 else if (have_obj.nr == 1)
468 packet_write_fmt(1, "ACK %s\n", last_hex);
469 break;
471 continue;
473 if (!strcmp(line, "done")) {
474 if (have_obj.nr > 0) {
475 if (multi_ack)
476 packet_write_fmt(1, "ACK %s\n", last_hex);
477 return 0;
479 packet_write_fmt(1, "NAK\n");
480 return -1;
482 die("git upload-pack: expected SHA1 list, got '%s'", line);
486 static int is_our_ref(struct object *o)
488 int allow_hidden_ref = (allow_unadvertised_object_request &
489 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
490 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
494 * on successful case, it's up to the caller to close cmd->out
496 static int do_reachable_revlist(struct child_process *cmd,
497 struct object_array *src,
498 struct object_array *reachable)
500 static const char *argv[] = {
501 "rev-list", "--stdin", NULL,
503 struct object *o;
504 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
505 int i;
507 cmd->argv = argv;
508 cmd->git_cmd = 1;
509 cmd->no_stderr = 1;
510 cmd->in = -1;
511 cmd->out = -1;
514 * If the next rev-list --stdin encounters an unknown commit,
515 * it terminates, which will cause SIGPIPE in the write loop
516 * below.
518 sigchain_push(SIGPIPE, SIG_IGN);
520 if (start_command(cmd))
521 goto error;
523 namebuf[0] = '^';
524 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
525 for (i = get_max_object_index(); 0 < i; ) {
526 o = get_indexed_object(--i);
527 if (!o)
528 continue;
529 if (reachable && o->type == OBJ_COMMIT)
530 o->flags &= ~TMP_MARK;
531 if (!is_our_ref(o))
532 continue;
533 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
534 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
535 goto error;
537 namebuf[GIT_SHA1_HEXSZ] = '\n';
538 for (i = 0; i < src->nr; i++) {
539 o = src->objects[i].item;
540 if (is_our_ref(o)) {
541 if (reachable)
542 add_object_array(o, NULL, reachable);
543 continue;
545 if (reachable && o->type == OBJ_COMMIT)
546 o->flags |= TMP_MARK;
547 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
548 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
549 goto error;
551 close(cmd->in);
552 cmd->in = -1;
553 sigchain_pop(SIGPIPE);
555 return 0;
557 error:
558 sigchain_pop(SIGPIPE);
560 if (cmd->in >= 0)
561 close(cmd->in);
562 if (cmd->out >= 0)
563 close(cmd->out);
564 return -1;
567 static int get_reachable_list(struct object_array *src,
568 struct object_array *reachable)
570 struct child_process cmd = CHILD_PROCESS_INIT;
571 int i;
572 struct object *o;
573 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
574 const unsigned hexsz = the_hash_algo->hexsz;
576 if (do_reachable_revlist(&cmd, src, reachable) < 0)
577 return -1;
579 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
580 struct object_id sha1;
581 const char *p;
583 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
584 break;
586 o = lookup_object(the_repository, sha1.hash);
587 if (o && o->type == OBJ_COMMIT) {
588 o->flags &= ~TMP_MARK;
591 for (i = get_max_object_index(); 0 < i; i--) {
592 o = get_indexed_object(i - 1);
593 if (o && o->type == OBJ_COMMIT &&
594 (o->flags & TMP_MARK)) {
595 add_object_array(o, NULL, reachable);
596 o->flags &= ~TMP_MARK;
599 close(cmd.out);
601 if (finish_command(&cmd))
602 return -1;
604 return 0;
607 static int has_unreachable(struct object_array *src)
609 struct child_process cmd = CHILD_PROCESS_INIT;
610 char buf[1];
611 int i;
613 if (do_reachable_revlist(&cmd, src, NULL) < 0)
614 return 1;
617 * The commits out of the rev-list are not ancestors of
618 * our ref.
620 i = read_in_full(cmd.out, buf, 1);
621 if (i)
622 goto error;
623 close(cmd.out);
624 cmd.out = -1;
627 * rev-list may have died by encountering a bad commit
628 * in the history, in which case we do want to bail out
629 * even when it showed no commit.
631 if (finish_command(&cmd))
632 goto error;
634 /* All the non-tip ones are ancestors of what we advertised */
635 return 0;
637 error:
638 sigchain_pop(SIGPIPE);
639 if (cmd.out >= 0)
640 close(cmd.out);
641 return 1;
644 static void check_non_tip(void)
646 int i;
649 * In the normal in-process case without
650 * uploadpack.allowReachableSHA1InWant,
651 * non-tip requests can never happen.
653 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
654 goto error;
655 if (!has_unreachable(&want_obj))
656 /* All the non-tip ones are ancestors of what we advertised */
657 return;
659 error:
660 /* Pick one of them (we know there at least is one) */
661 for (i = 0; i < want_obj.nr; i++) {
662 struct object *o = want_obj.objects[i].item;
663 if (!is_our_ref(o))
664 die("git upload-pack: not our ref %s",
665 oid_to_hex(&o->oid));
669 static void send_shallow(struct commit_list *result)
671 while (result) {
672 struct object *object = &result->item->object;
673 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
674 packet_write_fmt(1, "shallow %s",
675 oid_to_hex(&object->oid));
676 register_shallow(the_repository, &object->oid);
677 shallow_nr++;
679 result = result->next;
683 static void send_unshallow(const struct object_array *shallows)
685 int i;
687 for (i = 0; i < shallows->nr; i++) {
688 struct object *object = shallows->objects[i].item;
689 if (object->flags & NOT_SHALLOW) {
690 struct commit_list *parents;
691 packet_write_fmt(1, "unshallow %s",
692 oid_to_hex(&object->oid));
693 object->flags &= ~CLIENT_SHALLOW;
695 * We want to _register_ "object" as shallow, but we
696 * also need to traverse object's parents to deepen a
697 * shallow clone. Unregister it for now so we can
698 * parse and add the parents to the want list, then
699 * re-register it.
701 unregister_shallow(&object->oid);
702 object->parsed = 0;
703 parse_commit_or_die((struct commit *)object);
704 parents = ((struct commit *)object)->parents;
705 while (parents) {
706 add_object_array(&parents->item->object,
707 NULL, &want_obj);
708 parents = parents->next;
710 add_object_array(object, NULL, &extra_edge_obj);
712 /* make sure commit traversal conforms to client */
713 register_shallow(the_repository, &object->oid);
717 static void deepen(int depth, int deepen_relative,
718 struct object_array *shallows)
720 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
721 int i;
723 for (i = 0; i < shallows->nr; i++) {
724 struct object *object = shallows->objects[i].item;
725 object->flags |= NOT_SHALLOW;
727 } else if (deepen_relative) {
728 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
729 struct commit_list *result;
731 get_reachable_list(shallows, &reachable_shallows);
732 result = get_shallow_commits(&reachable_shallows,
733 depth + 1,
734 SHALLOW, NOT_SHALLOW);
735 send_shallow(result);
736 free_commit_list(result);
737 object_array_clear(&reachable_shallows);
738 } else {
739 struct commit_list *result;
741 result = get_shallow_commits(&want_obj, depth,
742 SHALLOW, NOT_SHALLOW);
743 send_shallow(result);
744 free_commit_list(result);
747 send_unshallow(shallows);
750 static void deepen_by_rev_list(int ac, const char **av,
751 struct object_array *shallows)
753 struct commit_list *result;
755 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
756 send_shallow(result);
757 free_commit_list(result);
758 send_unshallow(shallows);
761 /* Returns 1 if a shallow list is sent or 0 otherwise */
762 static int send_shallow_list(int depth, int deepen_rev_list,
763 timestamp_t deepen_since,
764 struct string_list *deepen_not,
765 struct object_array *shallows)
767 int ret = 0;
769 if (depth > 0 && deepen_rev_list)
770 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
771 if (depth > 0) {
772 deepen(depth, deepen_relative, shallows);
773 ret = 1;
774 } else if (deepen_rev_list) {
775 struct argv_array av = ARGV_ARRAY_INIT;
776 int i;
778 argv_array_push(&av, "rev-list");
779 if (deepen_since)
780 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
781 if (deepen_not->nr) {
782 argv_array_push(&av, "--not");
783 for (i = 0; i < deepen_not->nr; i++) {
784 struct string_list_item *s = deepen_not->items + i;
785 argv_array_push(&av, s->string);
787 argv_array_push(&av, "--not");
789 for (i = 0; i < want_obj.nr; i++) {
790 struct object *o = want_obj.objects[i].item;
791 argv_array_push(&av, oid_to_hex(&o->oid));
793 deepen_by_rev_list(av.argc, av.argv, shallows);
794 argv_array_clear(&av);
795 ret = 1;
796 } else {
797 if (shallows->nr > 0) {
798 int i;
799 for (i = 0; i < shallows->nr; i++)
800 register_shallow(the_repository,
801 &shallows->objects[i].item->oid);
805 shallow_nr += shallows->nr;
806 return ret;
809 static int process_shallow(const char *line, struct object_array *shallows)
811 const char *arg;
812 if (skip_prefix(line, "shallow ", &arg)) {
813 struct object_id oid;
814 struct object *object;
815 if (get_oid_hex(arg, &oid))
816 die("invalid shallow line: %s", line);
817 object = parse_object(the_repository, &oid);
818 if (!object)
819 return 1;
820 if (object->type != OBJ_COMMIT)
821 die("invalid shallow object %s", oid_to_hex(&oid));
822 if (!(object->flags & CLIENT_SHALLOW)) {
823 object->flags |= CLIENT_SHALLOW;
824 add_object_array(object, NULL, shallows);
826 return 1;
829 return 0;
832 static int process_deepen(const char *line, int *depth)
834 const char *arg;
835 if (skip_prefix(line, "deepen ", &arg)) {
836 char *end = NULL;
837 *depth = (int)strtol(arg, &end, 0);
838 if (!end || *end || *depth <= 0)
839 die("Invalid deepen: %s", line);
840 return 1;
843 return 0;
846 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
848 const char *arg;
849 if (skip_prefix(line, "deepen-since ", &arg)) {
850 char *end = NULL;
851 *deepen_since = parse_timestamp(arg, &end, 0);
852 if (!end || *end || !deepen_since ||
853 /* revisions.c's max_age -1 is special */
854 *deepen_since == -1)
855 die("Invalid deepen-since: %s", line);
856 *deepen_rev_list = 1;
857 return 1;
859 return 0;
862 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
864 const char *arg;
865 if (skip_prefix(line, "deepen-not ", &arg)) {
866 char *ref = NULL;
867 struct object_id oid;
868 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
869 die("git upload-pack: ambiguous deepen-not: %s", line);
870 string_list_append(deepen_not, ref);
871 free(ref);
872 *deepen_rev_list = 1;
873 return 1;
875 return 0;
878 static void receive_needs(void)
880 struct object_array shallows = OBJECT_ARRAY_INIT;
881 struct string_list deepen_not = STRING_LIST_INIT_DUP;
882 int depth = 0;
883 int has_non_tip = 0;
884 timestamp_t deepen_since = 0;
885 int deepen_rev_list = 0;
887 shallow_nr = 0;
888 for (;;) {
889 struct object *o;
890 const char *features;
891 struct object_id oid_buf;
892 char *line = packet_read_line(0, NULL);
893 const char *arg;
895 reset_timeout();
896 if (!line)
897 break;
899 if (process_shallow(line, &shallows))
900 continue;
901 if (process_deepen(line, &depth))
902 continue;
903 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
904 continue;
905 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
906 continue;
908 if (skip_prefix(line, "filter ", &arg)) {
909 if (!filter_capability_requested)
910 die("git upload-pack: filtering capability not negotiated");
911 parse_list_objects_filter(&filter_options, arg);
912 continue;
915 if (!skip_prefix(line, "want ", &arg) ||
916 parse_oid_hex(arg, &oid_buf, &features))
917 die("git upload-pack: protocol error, "
918 "expected to get object ID, not '%s'", line);
920 if (parse_feature_request(features, "deepen-relative"))
921 deepen_relative = 1;
922 if (parse_feature_request(features, "multi_ack_detailed"))
923 multi_ack = 2;
924 else if (parse_feature_request(features, "multi_ack"))
925 multi_ack = 1;
926 if (parse_feature_request(features, "no-done"))
927 no_done = 1;
928 if (parse_feature_request(features, "thin-pack"))
929 use_thin_pack = 1;
930 if (parse_feature_request(features, "ofs-delta"))
931 use_ofs_delta = 1;
932 if (parse_feature_request(features, "side-band-64k"))
933 use_sideband = LARGE_PACKET_MAX;
934 else if (parse_feature_request(features, "side-band"))
935 use_sideband = DEFAULT_PACKET_MAX;
936 if (parse_feature_request(features, "no-progress"))
937 no_progress = 1;
938 if (parse_feature_request(features, "include-tag"))
939 use_include_tag = 1;
940 if (allow_filter && parse_feature_request(features, "filter"))
941 filter_capability_requested = 1;
943 o = parse_object(the_repository, &oid_buf);
944 if (!o) {
945 packet_write_fmt(1,
946 "ERR upload-pack: not our ref %s",
947 oid_to_hex(&oid_buf));
948 die("git upload-pack: not our ref %s",
949 oid_to_hex(&oid_buf));
951 if (!(o->flags & WANTED)) {
952 o->flags |= WANTED;
953 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
954 || is_our_ref(o)))
955 has_non_tip = 1;
956 add_object_array(o, NULL, &want_obj);
961 * We have sent all our refs already, and the other end
962 * should have chosen out of them. When we are operating
963 * in the stateless RPC mode, however, their choice may
964 * have been based on the set of older refs advertised
965 * by another process that handled the initial request.
967 if (has_non_tip)
968 check_non_tip();
970 if (!use_sideband && daemon_mode)
971 no_progress = 1;
973 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
974 return;
976 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
977 &deepen_not, &shallows))
978 packet_flush(1);
979 object_array_clear(&shallows);
982 /* return non-zero if the ref is hidden, otherwise 0 */
983 static int mark_our_ref(const char *refname, const char *refname_full,
984 const struct object_id *oid)
986 struct object *o = lookup_unknown_object(oid->hash);
988 if (ref_is_hidden(refname, refname_full)) {
989 o->flags |= HIDDEN_REF;
990 return 1;
992 o->flags |= OUR_REF;
993 return 0;
996 static int check_ref(const char *refname_full, const struct object_id *oid,
997 int flag, void *cb_data)
999 const char *refname = strip_namespace(refname_full);
1001 mark_our_ref(refname, refname_full, oid);
1002 return 0;
1005 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1007 struct string_list_item *item;
1009 if (!symref->nr)
1010 return;
1011 for_each_string_list_item(item, symref)
1012 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1015 static int send_ref(const char *refname, const struct object_id *oid,
1016 int flag, void *cb_data)
1018 static const char *capabilities = "multi_ack thin-pack side-band"
1019 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1020 " deepen-relative no-progress include-tag multi_ack_detailed";
1021 const char *refname_nons = strip_namespace(refname);
1022 struct object_id peeled;
1024 if (mark_our_ref(refname_nons, refname, oid))
1025 return 0;
1027 if (capabilities) {
1028 struct strbuf symref_info = STRBUF_INIT;
1030 format_symref_info(&symref_info, cb_data);
1031 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1032 oid_to_hex(oid), refname_nons,
1033 0, capabilities,
1034 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1035 " allow-tip-sha1-in-want" : "",
1036 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1037 " allow-reachable-sha1-in-want" : "",
1038 stateless_rpc ? " no-done" : "",
1039 symref_info.buf,
1040 allow_filter ? " filter" : "",
1041 git_user_agent_sanitized());
1042 strbuf_release(&symref_info);
1043 } else {
1044 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1046 capabilities = NULL;
1047 if (!peel_ref(refname, &peeled))
1048 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1049 return 0;
1052 static int find_symref(const char *refname, const struct object_id *oid,
1053 int flag, void *cb_data)
1055 const char *symref_target;
1056 struct string_list_item *item;
1058 if ((flag & REF_ISSYMREF) == 0)
1059 return 0;
1060 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1061 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1062 die("'%s' is a symref but it is not?", refname);
1063 item = string_list_append(cb_data, refname);
1064 item->util = xstrdup(symref_target);
1065 return 0;
1068 static int upload_pack_config(const char *var, const char *value, void *unused)
1070 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1071 if (git_config_bool(var, value))
1072 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1073 else
1074 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1075 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1076 if (git_config_bool(var, value))
1077 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1078 else
1079 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1080 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1081 if (git_config_bool(var, value))
1082 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1083 else
1084 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1085 } else if (!strcmp("uploadpack.keepalive", var)) {
1086 keepalive = git_config_int(var, value);
1087 if (!keepalive)
1088 keepalive = -1;
1089 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1090 if (!strcmp("uploadpack.packobjectshook", var))
1091 return git_config_string(&pack_objects_hook, var, value);
1092 } else if (!strcmp("uploadpack.allowfilter", var)) {
1093 allow_filter = git_config_bool(var, value);
1095 return parse_hide_refs_config(var, value, "uploadpack");
1098 void upload_pack(struct upload_pack_options *options)
1100 struct string_list symref = STRING_LIST_INIT_DUP;
1102 stateless_rpc = options->stateless_rpc;
1103 timeout = options->timeout;
1104 daemon_mode = options->daemon_mode;
1106 git_config(upload_pack_config, NULL);
1108 head_ref_namespaced(find_symref, &symref);
1110 if (options->advertise_refs || !stateless_rpc) {
1111 reset_timeout();
1112 head_ref_namespaced(send_ref, &symref);
1113 for_each_namespaced_ref(send_ref, &symref);
1114 advertise_shallow_grafts(1);
1115 packet_flush(1);
1116 } else {
1117 head_ref_namespaced(check_ref, NULL);
1118 for_each_namespaced_ref(check_ref, NULL);
1120 string_list_clear(&symref, 1);
1121 if (options->advertise_refs)
1122 return;
1124 receive_needs();
1125 if (want_obj.nr) {
1126 get_common_commits();
1127 create_pack_file();
1131 struct upload_pack_data {
1132 struct object_array wants;
1133 struct oid_array haves;
1135 struct object_array shallows;
1136 struct string_list deepen_not;
1137 int depth;
1138 timestamp_t deepen_since;
1139 int deepen_rev_list;
1140 int deepen_relative;
1142 unsigned stateless_rpc : 1;
1144 unsigned use_thin_pack : 1;
1145 unsigned use_ofs_delta : 1;
1146 unsigned no_progress : 1;
1147 unsigned use_include_tag : 1;
1148 unsigned done : 1;
1151 static void upload_pack_data_init(struct upload_pack_data *data)
1153 struct object_array wants = OBJECT_ARRAY_INIT;
1154 struct oid_array haves = OID_ARRAY_INIT;
1155 struct object_array shallows = OBJECT_ARRAY_INIT;
1156 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1158 memset(data, 0, sizeof(*data));
1159 data->wants = wants;
1160 data->haves = haves;
1161 data->shallows = shallows;
1162 data->deepen_not = deepen_not;
1165 static void upload_pack_data_clear(struct upload_pack_data *data)
1167 object_array_clear(&data->wants);
1168 oid_array_clear(&data->haves);
1169 object_array_clear(&data->shallows);
1170 string_list_clear(&data->deepen_not, 0);
1173 static int parse_want(const char *line)
1175 const char *arg;
1176 if (skip_prefix(line, "want ", &arg)) {
1177 struct object_id oid;
1178 struct object *o;
1180 if (get_oid_hex(arg, &oid))
1181 die("git upload-pack: protocol error, "
1182 "expected to get oid, not '%s'", line);
1184 o = parse_object(the_repository, &oid);
1185 if (!o) {
1186 packet_write_fmt(1,
1187 "ERR upload-pack: not our ref %s",
1188 oid_to_hex(&oid));
1189 die("git upload-pack: not our ref %s",
1190 oid_to_hex(&oid));
1193 if (!(o->flags & WANTED)) {
1194 o->flags |= WANTED;
1195 add_object_array(o, NULL, &want_obj);
1198 return 1;
1201 return 0;
1204 static int parse_have(const char *line, struct oid_array *haves)
1206 const char *arg;
1207 if (skip_prefix(line, "have ", &arg)) {
1208 struct object_id oid;
1210 if (get_oid_hex(arg, &oid))
1211 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1212 oid_array_append(haves, &oid);
1213 return 1;
1216 return 0;
1219 static void process_args(struct packet_reader *request,
1220 struct upload_pack_data *data)
1222 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1223 const char *arg = request->line;
1224 const char *p;
1226 /* process want */
1227 if (parse_want(arg))
1228 continue;
1229 /* process have line */
1230 if (parse_have(arg, &data->haves))
1231 continue;
1233 /* process args like thin-pack */
1234 if (!strcmp(arg, "thin-pack")) {
1235 use_thin_pack = 1;
1236 continue;
1238 if (!strcmp(arg, "ofs-delta")) {
1239 use_ofs_delta = 1;
1240 continue;
1242 if (!strcmp(arg, "no-progress")) {
1243 no_progress = 1;
1244 continue;
1246 if (!strcmp(arg, "include-tag")) {
1247 use_include_tag = 1;
1248 continue;
1250 if (!strcmp(arg, "done")) {
1251 data->done = 1;
1252 continue;
1255 /* Shallow related arguments */
1256 if (process_shallow(arg, &data->shallows))
1257 continue;
1258 if (process_deepen(arg, &data->depth))
1259 continue;
1260 if (process_deepen_since(arg, &data->deepen_since,
1261 &data->deepen_rev_list))
1262 continue;
1263 if (process_deepen_not(arg, &data->deepen_not,
1264 &data->deepen_rev_list))
1265 continue;
1266 if (!strcmp(arg, "deepen-relative")) {
1267 data->deepen_relative = 1;
1268 continue;
1271 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1272 parse_list_objects_filter(&filter_options, p);
1273 continue;
1276 /* ignore unknown lines maybe? */
1277 die("unexpected line: '%s'", arg);
1281 static int process_haves(struct oid_array *haves, struct oid_array *common)
1283 int i;
1285 /* Process haves */
1286 for (i = 0; i < haves->nr; i++) {
1287 const struct object_id *oid = &haves->oid[i];
1288 struct object *o;
1289 int we_knew_they_have = 0;
1291 if (!has_object_file(oid))
1292 continue;
1294 oid_array_append(common, oid);
1296 o = parse_object(the_repository, oid);
1297 if (!o)
1298 die("oops (%s)", oid_to_hex(oid));
1299 if (o->type == OBJ_COMMIT) {
1300 struct commit_list *parents;
1301 struct commit *commit = (struct commit *)o;
1302 if (o->flags & THEY_HAVE)
1303 we_knew_they_have = 1;
1304 else
1305 o->flags |= THEY_HAVE;
1306 if (!oldest_have || (commit->date < oldest_have))
1307 oldest_have = commit->date;
1308 for (parents = commit->parents;
1309 parents;
1310 parents = parents->next)
1311 parents->item->object.flags |= THEY_HAVE;
1313 if (!we_knew_they_have)
1314 add_object_array(o, NULL, &have_obj);
1317 return 0;
1320 static int send_acks(struct oid_array *acks, struct strbuf *response)
1322 int i;
1324 packet_buf_write(response, "acknowledgments\n");
1326 /* Send Acks */
1327 if (!acks->nr)
1328 packet_buf_write(response, "NAK\n");
1330 for (i = 0; i < acks->nr; i++) {
1331 packet_buf_write(response, "ACK %s\n",
1332 oid_to_hex(&acks->oid[i]));
1335 if (ok_to_give_up()) {
1336 /* Send Ready */
1337 packet_buf_write(response, "ready\n");
1338 return 1;
1341 return 0;
1344 static int process_haves_and_send_acks(struct upload_pack_data *data)
1346 struct oid_array common = OID_ARRAY_INIT;
1347 struct strbuf response = STRBUF_INIT;
1348 int ret = 0;
1350 process_haves(&data->haves, &common);
1351 if (data->done) {
1352 ret = 1;
1353 } else if (send_acks(&common, &response)) {
1354 packet_buf_delim(&response);
1355 ret = 1;
1356 } else {
1357 /* Add Flush */
1358 packet_buf_flush(&response);
1359 ret = 0;
1362 /* Send response */
1363 write_or_die(1, response.buf, response.len);
1364 strbuf_release(&response);
1366 oid_array_clear(&data->haves);
1367 oid_array_clear(&common);
1368 return ret;
1371 static void send_shallow_info(struct upload_pack_data *data)
1373 /* No shallow info needs to be sent */
1374 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1375 !is_repository_shallow(the_repository))
1376 return;
1378 packet_write_fmt(1, "shallow-info\n");
1380 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1381 data->deepen_since, &data->deepen_not,
1382 &data->shallows) &&
1383 is_repository_shallow(the_repository))
1384 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1386 packet_delim(1);
1389 enum fetch_state {
1390 FETCH_PROCESS_ARGS = 0,
1391 FETCH_SEND_ACKS,
1392 FETCH_SEND_PACK,
1393 FETCH_DONE,
1396 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1397 struct packet_reader *request)
1399 enum fetch_state state = FETCH_PROCESS_ARGS;
1400 struct upload_pack_data data;
1402 git_config(upload_pack_config, NULL);
1404 upload_pack_data_init(&data);
1405 use_sideband = LARGE_PACKET_MAX;
1407 while (state != FETCH_DONE) {
1408 switch (state) {
1409 case FETCH_PROCESS_ARGS:
1410 process_args(request, &data);
1412 if (!want_obj.nr) {
1414 * Request didn't contain any 'want' lines,
1415 * guess they didn't want anything.
1417 state = FETCH_DONE;
1418 } else if (data.haves.nr) {
1420 * Request had 'have' lines, so lets ACK them.
1422 state = FETCH_SEND_ACKS;
1423 } else {
1425 * Request had 'want's but no 'have's so we can
1426 * immedietly go to construct and send a pack.
1428 state = FETCH_SEND_PACK;
1430 break;
1431 case FETCH_SEND_ACKS:
1432 if (process_haves_and_send_acks(&data))
1433 state = FETCH_SEND_PACK;
1434 else
1435 state = FETCH_DONE;
1436 break;
1437 case FETCH_SEND_PACK:
1438 send_shallow_info(&data);
1440 packet_write_fmt(1, "packfile\n");
1441 create_pack_file();
1442 state = FETCH_DONE;
1443 break;
1444 case FETCH_DONE:
1445 continue;
1449 upload_pack_data_clear(&data);
1450 return 0;
1453 int upload_pack_advertise(struct repository *r,
1454 struct strbuf *value)
1456 if (value) {
1457 int allow_filter_value;
1458 strbuf_addstr(value, "shallow");
1459 if (!repo_config_get_bool(the_repository,
1460 "uploadpack.allowfilter",
1461 &allow_filter_value) &&
1462 allow_filter_value)
1463 strbuf_addstr(value, " filter");
1465 return 1;