Merge branch 'ah/doc-updates' into maint
[git/git-svn.git] / upload-pack.c
blob2ae9d9bb475214070529e22cc3f186afebbc6c7c
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 (current_config_scope() != CONFIG_SCOPE_REPO) {
1080 if (!strcmp("uploadpack.packobjectshook", var))
1081 return git_config_string(&pack_objects_hook, var, value);
1082 } else if (!strcmp("uploadpack.allowfilter", var)) {
1083 allow_filter = git_config_bool(var, value);
1084 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1085 allow_ref_in_want = git_config_bool(var, value);
1087 return parse_hide_refs_config(var, value, "uploadpack");
1090 void upload_pack(struct upload_pack_options *options)
1092 struct string_list symref = STRING_LIST_INIT_DUP;
1094 stateless_rpc = options->stateless_rpc;
1095 timeout = options->timeout;
1096 daemon_mode = options->daemon_mode;
1098 git_config(upload_pack_config, NULL);
1100 head_ref_namespaced(find_symref, &symref);
1102 if (options->advertise_refs || !stateless_rpc) {
1103 reset_timeout();
1104 head_ref_namespaced(send_ref, &symref);
1105 for_each_namespaced_ref(send_ref, &symref);
1106 advertise_shallow_grafts(1);
1107 packet_flush(1);
1108 } else {
1109 head_ref_namespaced(check_ref, NULL);
1110 for_each_namespaced_ref(check_ref, NULL);
1112 string_list_clear(&symref, 1);
1113 if (options->advertise_refs)
1114 return;
1116 receive_needs();
1117 if (want_obj.nr) {
1118 get_common_commits();
1119 create_pack_file();
1123 struct upload_pack_data {
1124 struct object_array wants;
1125 struct string_list wanted_refs;
1126 struct oid_array haves;
1128 struct object_array shallows;
1129 struct string_list deepen_not;
1130 int depth;
1131 timestamp_t deepen_since;
1132 int deepen_rev_list;
1133 int deepen_relative;
1135 unsigned stateless_rpc : 1;
1137 unsigned use_thin_pack : 1;
1138 unsigned use_ofs_delta : 1;
1139 unsigned no_progress : 1;
1140 unsigned use_include_tag : 1;
1141 unsigned done : 1;
1144 static void upload_pack_data_init(struct upload_pack_data *data)
1146 struct object_array wants = OBJECT_ARRAY_INIT;
1147 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1148 struct oid_array haves = OID_ARRAY_INIT;
1149 struct object_array shallows = OBJECT_ARRAY_INIT;
1150 struct string_list deepen_not = STRING_LIST_INIT_DUP;
1152 memset(data, 0, sizeof(*data));
1153 data->wants = wants;
1154 data->wanted_refs = wanted_refs;
1155 data->haves = haves;
1156 data->shallows = shallows;
1157 data->deepen_not = deepen_not;
1160 static void upload_pack_data_clear(struct upload_pack_data *data)
1162 object_array_clear(&data->wants);
1163 string_list_clear(&data->wanted_refs, 1);
1164 oid_array_clear(&data->haves);
1165 object_array_clear(&data->shallows);
1166 string_list_clear(&data->deepen_not, 0);
1169 static int parse_want(const char *line)
1171 const char *arg;
1172 if (skip_prefix(line, "want ", &arg)) {
1173 struct object_id oid;
1174 struct object *o;
1176 if (get_oid_hex(arg, &oid))
1177 die("git upload-pack: protocol error, "
1178 "expected to get oid, not '%s'", line);
1180 o = parse_object(the_repository, &oid);
1181 if (!o) {
1182 packet_write_fmt(1,
1183 "ERR upload-pack: not our ref %s",
1184 oid_to_hex(&oid));
1185 die("git upload-pack: not our ref %s",
1186 oid_to_hex(&oid));
1189 if (!(o->flags & WANTED)) {
1190 o->flags |= WANTED;
1191 add_object_array(o, NULL, &want_obj);
1194 return 1;
1197 return 0;
1200 static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1202 const char *arg;
1203 if (skip_prefix(line, "want-ref ", &arg)) {
1204 struct object_id oid;
1205 struct string_list_item *item;
1206 struct object *o;
1208 if (read_ref(arg, &oid)) {
1209 packet_write_fmt(1, "ERR unknown ref %s", arg);
1210 die("unknown ref %s", arg);
1213 item = string_list_append(wanted_refs, arg);
1214 item->util = oiddup(&oid);
1216 o = parse_object_or_die(&oid, arg);
1217 if (!(o->flags & WANTED)) {
1218 o->flags |= WANTED;
1219 add_object_array(o, NULL, &want_obj);
1222 return 1;
1225 return 0;
1228 static int parse_have(const char *line, struct oid_array *haves)
1230 const char *arg;
1231 if (skip_prefix(line, "have ", &arg)) {
1232 struct object_id oid;
1234 if (get_oid_hex(arg, &oid))
1235 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1236 oid_array_append(haves, &oid);
1237 return 1;
1240 return 0;
1243 static void process_args(struct packet_reader *request,
1244 struct upload_pack_data *data)
1246 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1247 const char *arg = request->line;
1248 const char *p;
1250 /* process want */
1251 if (parse_want(arg))
1252 continue;
1253 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1254 continue;
1255 /* process have line */
1256 if (parse_have(arg, &data->haves))
1257 continue;
1259 /* process args like thin-pack */
1260 if (!strcmp(arg, "thin-pack")) {
1261 use_thin_pack = 1;
1262 continue;
1264 if (!strcmp(arg, "ofs-delta")) {
1265 use_ofs_delta = 1;
1266 continue;
1268 if (!strcmp(arg, "no-progress")) {
1269 no_progress = 1;
1270 continue;
1272 if (!strcmp(arg, "include-tag")) {
1273 use_include_tag = 1;
1274 continue;
1276 if (!strcmp(arg, "done")) {
1277 data->done = 1;
1278 continue;
1281 /* Shallow related arguments */
1282 if (process_shallow(arg, &data->shallows))
1283 continue;
1284 if (process_deepen(arg, &data->depth))
1285 continue;
1286 if (process_deepen_since(arg, &data->deepen_since,
1287 &data->deepen_rev_list))
1288 continue;
1289 if (process_deepen_not(arg, &data->deepen_not,
1290 &data->deepen_rev_list))
1291 continue;
1292 if (!strcmp(arg, "deepen-relative")) {
1293 data->deepen_relative = 1;
1294 continue;
1297 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1298 parse_list_objects_filter(&filter_options, p);
1299 continue;
1302 /* ignore unknown lines maybe? */
1303 die("unexpected line: '%s'", arg);
1307 static int process_haves(struct oid_array *haves, struct oid_array *common)
1309 int i;
1311 /* Process haves */
1312 for (i = 0; i < haves->nr; i++) {
1313 const struct object_id *oid = &haves->oid[i];
1314 struct object *o;
1315 int we_knew_they_have = 0;
1317 if (!has_object_file(oid))
1318 continue;
1320 oid_array_append(common, oid);
1322 o = parse_object(the_repository, oid);
1323 if (!o)
1324 die("oops (%s)", oid_to_hex(oid));
1325 if (o->type == OBJ_COMMIT) {
1326 struct commit_list *parents;
1327 struct commit *commit = (struct commit *)o;
1328 if (o->flags & THEY_HAVE)
1329 we_knew_they_have = 1;
1330 else
1331 o->flags |= THEY_HAVE;
1332 if (!oldest_have || (commit->date < oldest_have))
1333 oldest_have = commit->date;
1334 for (parents = commit->parents;
1335 parents;
1336 parents = parents->next)
1337 parents->item->object.flags |= THEY_HAVE;
1339 if (!we_knew_they_have)
1340 add_object_array(o, NULL, &have_obj);
1343 return 0;
1346 static int send_acks(struct oid_array *acks, struct strbuf *response)
1348 int i;
1350 packet_buf_write(response, "acknowledgments\n");
1352 /* Send Acks */
1353 if (!acks->nr)
1354 packet_buf_write(response, "NAK\n");
1356 for (i = 0; i < acks->nr; i++) {
1357 packet_buf_write(response, "ACK %s\n",
1358 oid_to_hex(&acks->oid[i]));
1361 if (ok_to_give_up()) {
1362 /* Send Ready */
1363 packet_buf_write(response, "ready\n");
1364 return 1;
1367 return 0;
1370 static int process_haves_and_send_acks(struct upload_pack_data *data)
1372 struct oid_array common = OID_ARRAY_INIT;
1373 struct strbuf response = STRBUF_INIT;
1374 int ret = 0;
1376 process_haves(&data->haves, &common);
1377 if (data->done) {
1378 ret = 1;
1379 } else if (send_acks(&common, &response)) {
1380 packet_buf_delim(&response);
1381 ret = 1;
1382 } else {
1383 /* Add Flush */
1384 packet_buf_flush(&response);
1385 ret = 0;
1388 /* Send response */
1389 write_or_die(1, response.buf, response.len);
1390 strbuf_release(&response);
1392 oid_array_clear(&data->haves);
1393 oid_array_clear(&common);
1394 return ret;
1397 static void send_wanted_ref_info(struct upload_pack_data *data)
1399 const struct string_list_item *item;
1401 if (!data->wanted_refs.nr)
1402 return;
1404 packet_write_fmt(1, "wanted-refs\n");
1406 for_each_string_list_item(item, &data->wanted_refs) {
1407 packet_write_fmt(1, "%s %s\n",
1408 oid_to_hex(item->util),
1409 item->string);
1412 packet_delim(1);
1415 static void send_shallow_info(struct upload_pack_data *data)
1417 /* No shallow info needs to be sent */
1418 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1419 !is_repository_shallow(the_repository))
1420 return;
1422 packet_write_fmt(1, "shallow-info\n");
1424 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1425 data->deepen_since, &data->deepen_not,
1426 &data->shallows) &&
1427 is_repository_shallow(the_repository))
1428 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1430 packet_delim(1);
1433 enum fetch_state {
1434 FETCH_PROCESS_ARGS = 0,
1435 FETCH_SEND_ACKS,
1436 FETCH_SEND_PACK,
1437 FETCH_DONE,
1440 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1441 struct packet_reader *request)
1443 enum fetch_state state = FETCH_PROCESS_ARGS;
1444 struct upload_pack_data data;
1446 git_config(upload_pack_config, NULL);
1448 upload_pack_data_init(&data);
1449 use_sideband = LARGE_PACKET_MAX;
1451 while (state != FETCH_DONE) {
1452 switch (state) {
1453 case FETCH_PROCESS_ARGS:
1454 process_args(request, &data);
1456 if (!want_obj.nr) {
1458 * Request didn't contain any 'want' lines,
1459 * guess they didn't want anything.
1461 state = FETCH_DONE;
1462 } else if (data.haves.nr) {
1464 * Request had 'have' lines, so lets ACK them.
1466 state = FETCH_SEND_ACKS;
1467 } else {
1469 * Request had 'want's but no 'have's so we can
1470 * immedietly go to construct and send a pack.
1472 state = FETCH_SEND_PACK;
1474 break;
1475 case FETCH_SEND_ACKS:
1476 if (process_haves_and_send_acks(&data))
1477 state = FETCH_SEND_PACK;
1478 else
1479 state = FETCH_DONE;
1480 break;
1481 case FETCH_SEND_PACK:
1482 send_wanted_ref_info(&data);
1483 send_shallow_info(&data);
1485 packet_write_fmt(1, "packfile\n");
1486 create_pack_file();
1487 state = FETCH_DONE;
1488 break;
1489 case FETCH_DONE:
1490 continue;
1494 upload_pack_data_clear(&data);
1495 return 0;
1498 int upload_pack_advertise(struct repository *r,
1499 struct strbuf *value)
1501 if (value) {
1502 int allow_filter_value;
1503 int allow_ref_in_want;
1505 strbuf_addstr(value, "shallow");
1507 if (!repo_config_get_bool(the_repository,
1508 "uploadpack.allowfilter",
1509 &allow_filter_value) &&
1510 allow_filter_value)
1511 strbuf_addstr(value, " filter");
1513 if (!repo_config_get_bool(the_repository,
1514 "uploadpack.allowrefinwant",
1515 &allow_ref_in_want) &&
1516 allow_ref_in_want)
1517 strbuf_addstr(value, " ref-in-want");
1520 return 1;