Merge branch 'bw/c-plus-plus' into ds/lazy-load-trees
[git.git] / upload-pack.c
blobf51b6cfca9435d6b3b3f5b44b593a7c5cd2b1d3e
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "tag.h"
7 #include "object.h"
8 #include "commit.h"
9 #include "exec_cmd.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "list-objects.h"
13 #include "list-objects-filter.h"
14 #include "list-objects-filter-options.h"
15 #include "run-command.h"
16 #include "connect.h"
17 #include "sigchain.h"
18 #include "version.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
23 #include "protocol.h"
24 #include "quote.h"
26 static const char * const upload_pack_usage[] = {
27 N_("git upload-pack [<options>] <dir>"),
28 NULL
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
36 #define REACHABLE (1u << 15)
38 #define SHALLOW (1u << 16)
39 #define NOT_SHALLOW (1u << 17)
40 #define CLIENT_SHALLOW (1u << 18)
41 #define HIDDEN_REF (1u << 19)
43 static timestamp_t oldest_have;
45 static int deepen_relative;
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 have_obj;
59 static struct object_array want_obj;
60 static struct object_array extra_edge_obj;
61 static unsigned int timeout;
62 static int keepalive = 5;
63 /* 0 for no sideband,
64 * otherwise maximum packet size (up to 65520 bytes).
66 static int use_sideband;
67 static int advertise_refs;
68 static int stateless_rpc;
69 static const char *pack_objects_hook;
71 static int filter_capability_requested;
72 static int filter_advertise;
73 static struct list_objects_filter_options filter_options;
75 static void reset_timeout(void)
77 alarm(timeout);
80 static void send_client_data(int fd, const char *data, ssize_t sz)
82 if (use_sideband) {
83 send_sideband(1, fd, data, sz, use_sideband);
84 return;
86 if (fd == 3)
87 /* emergency quit */
88 fd = 2;
89 if (fd == 2) {
90 /* XXX: are we happy to lose stuff here? */
91 xwrite(fd, data, sz);
92 return;
94 write_or_die(fd, data, sz);
97 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
99 FILE *fp = cb_data;
100 if (graft->nr_parent == -1)
101 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
102 return 0;
105 static void create_pack_file(void)
107 struct child_process pack_objects = CHILD_PROCESS_INIT;
108 char data[8193], progress[128];
109 char abort_msg[] = "aborting due to possible repository "
110 "corruption on the remote side.";
111 int buffered = -1;
112 ssize_t sz;
113 int i;
114 FILE *pipe_fd;
116 if (!pack_objects_hook)
117 pack_objects.git_cmd = 1;
118 else {
119 argv_array_push(&pack_objects.args, pack_objects_hook);
120 argv_array_push(&pack_objects.args, "git");
121 pack_objects.use_shell = 1;
124 if (shallow_nr) {
125 argv_array_push(&pack_objects.args, "--shallow-file");
126 argv_array_push(&pack_objects.args, "");
128 argv_array_push(&pack_objects.args, "pack-objects");
129 argv_array_push(&pack_objects.args, "--revs");
130 if (use_thin_pack)
131 argv_array_push(&pack_objects.args, "--thin");
133 argv_array_push(&pack_objects.args, "--stdout");
134 if (shallow_nr)
135 argv_array_push(&pack_objects.args, "--shallow");
136 if (!no_progress)
137 argv_array_push(&pack_objects.args, "--progress");
138 if (use_ofs_delta)
139 argv_array_push(&pack_objects.args, "--delta-base-offset");
140 if (use_include_tag)
141 argv_array_push(&pack_objects.args, "--include-tag");
142 if (filter_options.filter_spec) {
143 if (pack_objects.use_shell) {
144 struct strbuf buf = STRBUF_INIT;
145 sq_quote_buf(&buf, filter_options.filter_spec);
146 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
147 strbuf_release(&buf);
148 } else {
149 argv_array_pushf(&pack_objects.args, "--filter=%s",
150 filter_options.filter_spec);
154 pack_objects.in = -1;
155 pack_objects.out = -1;
156 pack_objects.err = -1;
158 if (start_command(&pack_objects))
159 die("git upload-pack: unable to fork git-pack-objects");
161 pipe_fd = xfdopen(pack_objects.in, "w");
163 if (shallow_nr)
164 for_each_commit_graft(write_one_shallow, pipe_fd);
166 for (i = 0; i < want_obj.nr; i++)
167 fprintf(pipe_fd, "%s\n",
168 oid_to_hex(&want_obj.objects[i].item->oid));
169 fprintf(pipe_fd, "--not\n");
170 for (i = 0; i < have_obj.nr; i++)
171 fprintf(pipe_fd, "%s\n",
172 oid_to_hex(&have_obj.objects[i].item->oid));
173 for (i = 0; i < extra_edge_obj.nr; i++)
174 fprintf(pipe_fd, "%s\n",
175 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
176 fprintf(pipe_fd, "\n");
177 fflush(pipe_fd);
178 fclose(pipe_fd);
180 /* We read from pack_objects.err to capture stderr output for
181 * progress bar, and pack_objects.out to capture the pack data.
184 while (1) {
185 struct pollfd pfd[2];
186 int pe, pu, pollsize;
187 int ret;
189 reset_timeout();
191 pollsize = 0;
192 pe = pu = -1;
194 if (0 <= pack_objects.out) {
195 pfd[pollsize].fd = pack_objects.out;
196 pfd[pollsize].events = POLLIN;
197 pu = pollsize;
198 pollsize++;
200 if (0 <= pack_objects.err) {
201 pfd[pollsize].fd = pack_objects.err;
202 pfd[pollsize].events = POLLIN;
203 pe = pollsize;
204 pollsize++;
207 if (!pollsize)
208 break;
210 ret = poll(pfd, pollsize,
211 keepalive < 0 ? -1 : 1000 * keepalive);
213 if (ret < 0) {
214 if (errno != EINTR) {
215 error_errno("poll failed, resuming");
216 sleep(1);
218 continue;
220 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
221 /* Status ready; we ship that in the side-band
222 * or dump to the standard error.
224 sz = xread(pack_objects.err, progress,
225 sizeof(progress));
226 if (0 < sz)
227 send_client_data(2, progress, sz);
228 else if (sz == 0) {
229 close(pack_objects.err);
230 pack_objects.err = -1;
232 else
233 goto fail;
234 /* give priority to status messages */
235 continue;
237 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
238 /* Data ready; we keep the last byte to ourselves
239 * in case we detect broken rev-list, so that we
240 * can leave the stream corrupted. This is
241 * unfortunate -- unpack-objects would happily
242 * accept a valid packdata with trailing garbage,
243 * so appending garbage after we pass all the
244 * pack data is not good enough to signal
245 * breakage to downstream.
247 char *cp = data;
248 ssize_t outsz = 0;
249 if (0 <= buffered) {
250 *cp++ = buffered;
251 outsz++;
253 sz = xread(pack_objects.out, cp,
254 sizeof(data) - outsz);
255 if (0 < sz)
257 else if (sz == 0) {
258 close(pack_objects.out);
259 pack_objects.out = -1;
261 else
262 goto fail;
263 sz += outsz;
264 if (1 < sz) {
265 buffered = data[sz-1] & 0xFF;
266 sz--;
268 else
269 buffered = -1;
270 send_client_data(1, data, sz);
274 * We hit the keepalive timeout without saying anything; send
275 * an empty message on the data sideband just to let the other
276 * side know we're still working on it, but don't have any data
277 * yet.
279 * If we don't have a sideband channel, there's no room in the
280 * protocol to say anything, so those clients are just out of
281 * luck.
283 if (!ret && use_sideband) {
284 static const char buf[] = "0005\1";
285 write_or_die(1, buf, 5);
289 if (finish_command(&pack_objects)) {
290 error("git upload-pack: git-pack-objects died with error.");
291 goto fail;
294 /* flush the data */
295 if (0 <= buffered) {
296 data[0] = buffered;
297 send_client_data(1, data, 1);
298 fprintf(stderr, "flushed.\n");
300 if (use_sideband)
301 packet_flush(1);
302 return;
304 fail:
305 send_client_data(3, abort_msg, sizeof(abort_msg));
306 die("git upload-pack: %s", abort_msg);
309 static int got_oid(const char *hex, struct object_id *oid)
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(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 reachable(struct commit *want)
345 struct prio_queue work = { compare_commits_by_commit_date };
347 prio_queue_put(&work, want);
348 while (work.nr) {
349 struct commit_list *list;
350 struct commit *commit = prio_queue_get(&work);
352 if (commit->object.flags & THEY_HAVE) {
353 want->object.flags |= COMMON_KNOWN;
354 break;
356 if (!commit->object.parsed)
357 parse_object(&commit->object.oid);
358 if (commit->object.flags & REACHABLE)
359 continue;
360 commit->object.flags |= REACHABLE;
361 if (commit->date < oldest_have)
362 continue;
363 for (list = commit->parents; list; list = list->next) {
364 struct commit *parent = list->item;
365 if (!(parent->object.flags & REACHABLE))
366 prio_queue_put(&work, parent);
369 want->object.flags |= REACHABLE;
370 clear_commit_marks(want, REACHABLE);
371 clear_prio_queue(&work);
372 return (want->object.flags & COMMON_KNOWN);
375 static int ok_to_give_up(void)
377 int i;
379 if (!have_obj.nr)
380 return 0;
382 for (i = 0; i < want_obj.nr; i++) {
383 struct object *want = want_obj.objects[i].item;
385 if (want->flags & COMMON_KNOWN)
386 continue;
387 want = deref_tag(want, "a want line", 0);
388 if (!want || want->type != OBJ_COMMIT) {
389 /* no way to tell if this is reachable by
390 * looking at the ancestry chain alone, so
391 * leave a note to ourselves not to worry about
392 * this object anymore.
394 want_obj.objects[i].item->flags |= COMMON_KNOWN;
395 continue;
397 if (!reachable((struct commit *)want))
398 return 0;
400 return 1;
403 static int get_common_commits(void)
405 struct object_id oid;
406 char last_hex[GIT_MAX_HEXSZ + 1];
407 int got_common = 0;
408 int got_other = 0;
409 int sent_ready = 0;
411 save_commit_buffer = 0;
413 for (;;) {
414 char *line = packet_read_line(0, NULL);
415 const char *arg;
417 reset_timeout();
419 if (!line) {
420 if (multi_ack == 2 && got_common
421 && !got_other && ok_to_give_up()) {
422 sent_ready = 1;
423 packet_write_fmt(1, "ACK %s ready\n", last_hex);
425 if (have_obj.nr == 0 || multi_ack)
426 packet_write_fmt(1, "NAK\n");
428 if (no_done && sent_ready) {
429 packet_write_fmt(1, "ACK %s\n", last_hex);
430 return 0;
432 if (stateless_rpc)
433 exit(0);
434 got_common = 0;
435 got_other = 0;
436 continue;
438 if (skip_prefix(line, "have ", &arg)) {
439 switch (got_oid(arg, &oid)) {
440 case -1: /* they have what we do not */
441 got_other = 1;
442 if (multi_ack && ok_to_give_up()) {
443 const char *hex = oid_to_hex(&oid);
444 if (multi_ack == 2) {
445 sent_ready = 1;
446 packet_write_fmt(1, "ACK %s ready\n", hex);
447 } else
448 packet_write_fmt(1, "ACK %s continue\n", hex);
450 break;
451 default:
452 got_common = 1;
453 memcpy(last_hex, oid_to_hex(&oid), 41);
454 if (multi_ack == 2)
455 packet_write_fmt(1, "ACK %s common\n", last_hex);
456 else if (multi_ack)
457 packet_write_fmt(1, "ACK %s continue\n", last_hex);
458 else if (have_obj.nr == 1)
459 packet_write_fmt(1, "ACK %s\n", last_hex);
460 break;
462 continue;
464 if (!strcmp(line, "done")) {
465 if (have_obj.nr > 0) {
466 if (multi_ack)
467 packet_write_fmt(1, "ACK %s\n", last_hex);
468 return 0;
470 packet_write_fmt(1, "NAK\n");
471 return -1;
473 die("git upload-pack: expected SHA1 list, got '%s'", line);
477 static int is_our_ref(struct object *o)
479 int allow_hidden_ref = (allow_unadvertised_object_request &
480 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
481 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
485 * on successful case, it's up to the caller to close cmd->out
487 static int do_reachable_revlist(struct child_process *cmd,
488 struct object_array *src,
489 struct object_array *reachable)
491 static const char *argv[] = {
492 "rev-list", "--stdin", NULL,
494 struct object *o;
495 char namebuf[42]; /* ^ + SHA-1 + LF */
496 int i;
498 cmd->argv = argv;
499 cmd->git_cmd = 1;
500 cmd->no_stderr = 1;
501 cmd->in = -1;
502 cmd->out = -1;
505 * If the next rev-list --stdin encounters an unknown commit,
506 * it terminates, which will cause SIGPIPE in the write loop
507 * below.
509 sigchain_push(SIGPIPE, SIG_IGN);
511 if (start_command(cmd))
512 goto error;
514 namebuf[0] = '^';
515 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
516 for (i = get_max_object_index(); 0 < i; ) {
517 o = get_indexed_object(--i);
518 if (!o)
519 continue;
520 if (reachable && o->type == OBJ_COMMIT)
521 o->flags &= ~TMP_MARK;
522 if (!is_our_ref(o))
523 continue;
524 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
525 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
526 goto error;
528 namebuf[GIT_SHA1_HEXSZ] = '\n';
529 for (i = 0; i < src->nr; i++) {
530 o = src->objects[i].item;
531 if (is_our_ref(o)) {
532 if (reachable)
533 add_object_array(o, NULL, reachable);
534 continue;
536 if (reachable && o->type == OBJ_COMMIT)
537 o->flags |= TMP_MARK;
538 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
539 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
540 goto error;
542 close(cmd->in);
543 cmd->in = -1;
544 sigchain_pop(SIGPIPE);
546 return 0;
548 error:
549 sigchain_pop(SIGPIPE);
551 if (cmd->in >= 0)
552 close(cmd->in);
553 if (cmd->out >= 0)
554 close(cmd->out);
555 return -1;
558 static int get_reachable_list(struct object_array *src,
559 struct object_array *reachable)
561 struct child_process cmd = CHILD_PROCESS_INIT;
562 int i;
563 struct object *o;
564 char namebuf[42]; /* ^ + SHA-1 + LF */
566 if (do_reachable_revlist(&cmd, src, reachable) < 0)
567 return -1;
569 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
570 struct object_id sha1;
572 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
573 break;
575 o = lookup_object(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(&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(&object->oid);
706 static void deepen(int depth, int deepen_relative,
707 struct object_array *shallows)
709 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
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);
737 packet_flush(1);
740 static void deepen_by_rev_list(int ac, const char **av,
741 struct object_array *shallows)
743 struct commit_list *result;
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);
749 packet_flush(1);
752 static void receive_needs(void)
754 struct object_array shallows = OBJECT_ARRAY_INIT;
755 struct string_list deepen_not = STRING_LIST_INIT_DUP;
756 int depth = 0;
757 int has_non_tip = 0;
758 timestamp_t deepen_since = 0;
759 int deepen_rev_list = 0;
761 shallow_nr = 0;
762 for (;;) {
763 struct object *o;
764 const char *features;
765 struct object_id oid_buf;
766 char *line = packet_read_line(0, NULL);
767 const char *arg;
769 reset_timeout();
770 if (!line)
771 break;
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(&oid);
779 if (!object)
780 continue;
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 continue;
789 if (skip_prefix(line, "deepen ", &arg)) {
790 char *end = NULL;
791 depth = strtol(arg, &end, 0);
792 if (!end || *end || depth <= 0)
793 die("Invalid deepen: %s", line);
794 continue;
796 if (skip_prefix(line, "deepen-since ", &arg)) {
797 char *end = NULL;
798 deepen_since = parse_timestamp(arg, &end, 0);
799 if (!end || *end || !deepen_since ||
800 /* revisions.c's max_age -1 is special */
801 deepen_since == -1)
802 die("Invalid deepen-since: %s", line);
803 deepen_rev_list = 1;
804 continue;
806 if (skip_prefix(line, "deepen-not ", &arg)) {
807 char *ref = NULL;
808 struct object_id oid;
809 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
810 die("git upload-pack: ambiguous deepen-not: %s", line);
811 string_list_append(&deepen_not, ref);
812 free(ref);
813 deepen_rev_list = 1;
814 continue;
816 if (skip_prefix(line, "filter ", &arg)) {
817 if (!filter_capability_requested)
818 die("git upload-pack: filtering capability not negotiated");
819 parse_list_objects_filter(&filter_options, arg);
820 continue;
822 if (!skip_prefix(line, "want ", &arg) ||
823 get_oid_hex(arg, &oid_buf))
824 die("git upload-pack: protocol error, "
825 "expected to get sha, not '%s'", line);
827 features = arg + 40;
829 if (parse_feature_request(features, "deepen-relative"))
830 deepen_relative = 1;
831 if (parse_feature_request(features, "multi_ack_detailed"))
832 multi_ack = 2;
833 else if (parse_feature_request(features, "multi_ack"))
834 multi_ack = 1;
835 if (parse_feature_request(features, "no-done"))
836 no_done = 1;
837 if (parse_feature_request(features, "thin-pack"))
838 use_thin_pack = 1;
839 if (parse_feature_request(features, "ofs-delta"))
840 use_ofs_delta = 1;
841 if (parse_feature_request(features, "side-band-64k"))
842 use_sideband = LARGE_PACKET_MAX;
843 else if (parse_feature_request(features, "side-band"))
844 use_sideband = DEFAULT_PACKET_MAX;
845 if (parse_feature_request(features, "no-progress"))
846 no_progress = 1;
847 if (parse_feature_request(features, "include-tag"))
848 use_include_tag = 1;
849 if (parse_feature_request(features, "filter"))
850 filter_capability_requested = 1;
852 o = parse_object(&oid_buf);
853 if (!o) {
854 packet_write_fmt(1,
855 "ERR upload-pack: not our ref %s",
856 oid_to_hex(&oid_buf));
857 die("git upload-pack: not our ref %s",
858 oid_to_hex(&oid_buf));
860 if (!(o->flags & WANTED)) {
861 o->flags |= WANTED;
862 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
863 || is_our_ref(o)))
864 has_non_tip = 1;
865 add_object_array(o, NULL, &want_obj);
870 * We have sent all our refs already, and the other end
871 * should have chosen out of them. When we are operating
872 * in the stateless RPC mode, however, their choice may
873 * have been based on the set of older refs advertised
874 * by another process that handled the initial request.
876 if (has_non_tip)
877 check_non_tip();
879 if (!use_sideband && daemon_mode)
880 no_progress = 1;
882 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
883 return;
884 if (depth > 0 && deepen_rev_list)
885 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
886 if (depth > 0)
887 deepen(depth, deepen_relative, &shallows);
888 else if (deepen_rev_list) {
889 struct argv_array av = ARGV_ARRAY_INIT;
890 int i;
892 argv_array_push(&av, "rev-list");
893 if (deepen_since)
894 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
895 if (deepen_not.nr) {
896 argv_array_push(&av, "--not");
897 for (i = 0; i < deepen_not.nr; i++) {
898 struct string_list_item *s = deepen_not.items + i;
899 argv_array_push(&av, s->string);
901 argv_array_push(&av, "--not");
903 for (i = 0; i < want_obj.nr; i++) {
904 struct object *o = want_obj.objects[i].item;
905 argv_array_push(&av, oid_to_hex(&o->oid));
907 deepen_by_rev_list(av.argc, av.argv, &shallows);
908 argv_array_clear(&av);
910 else
911 if (shallows.nr > 0) {
912 int i;
913 for (i = 0; i < shallows.nr; i++)
914 register_shallow(&shallows.objects[i].item->oid);
917 shallow_nr += shallows.nr;
918 object_array_clear(&shallows);
921 /* return non-zero if the ref is hidden, otherwise 0 */
922 static int mark_our_ref(const char *refname, const char *refname_full,
923 const struct object_id *oid)
925 struct object *o = lookup_unknown_object(oid->hash);
927 if (ref_is_hidden(refname, refname_full)) {
928 o->flags |= HIDDEN_REF;
929 return 1;
931 o->flags |= OUR_REF;
932 return 0;
935 static int check_ref(const char *refname_full, const struct object_id *oid,
936 int flag, void *cb_data)
938 const char *refname = strip_namespace(refname_full);
940 mark_our_ref(refname, refname_full, oid);
941 return 0;
944 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
946 struct string_list_item *item;
948 if (!symref->nr)
949 return;
950 for_each_string_list_item(item, symref)
951 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
954 static int send_ref(const char *refname, const struct object_id *oid,
955 int flag, void *cb_data)
957 static const char *capabilities = "multi_ack thin-pack side-band"
958 " side-band-64k ofs-delta shallow deepen-since deepen-not"
959 " deepen-relative no-progress include-tag multi_ack_detailed";
960 const char *refname_nons = strip_namespace(refname);
961 struct object_id peeled;
963 if (mark_our_ref(refname_nons, refname, oid))
964 return 0;
966 if (capabilities) {
967 struct strbuf symref_info = STRBUF_INIT;
969 format_symref_info(&symref_info, cb_data);
970 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
971 oid_to_hex(oid), refname_nons,
972 0, capabilities,
973 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
974 " allow-tip-sha1-in-want" : "",
975 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
976 " allow-reachable-sha1-in-want" : "",
977 stateless_rpc ? " no-done" : "",
978 symref_info.buf,
979 filter_advertise ? " filter" : "",
980 git_user_agent_sanitized());
981 strbuf_release(&symref_info);
982 } else {
983 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
985 capabilities = NULL;
986 if (!peel_ref(refname, &peeled))
987 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
988 return 0;
991 static int find_symref(const char *refname, const struct object_id *oid,
992 int flag, void *cb_data)
994 const char *symref_target;
995 struct string_list_item *item;
997 if ((flag & REF_ISSYMREF) == 0)
998 return 0;
999 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1000 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1001 die("'%s' is a symref but it is not?", refname);
1002 item = string_list_append(cb_data, refname);
1003 item->util = xstrdup(symref_target);
1004 return 0;
1007 static void upload_pack(void)
1009 struct string_list symref = STRING_LIST_INIT_DUP;
1011 head_ref_namespaced(find_symref, &symref);
1013 if (advertise_refs || !stateless_rpc) {
1014 reset_timeout();
1015 head_ref_namespaced(send_ref, &symref);
1016 for_each_namespaced_ref(send_ref, &symref);
1017 advertise_shallow_grafts(1);
1018 packet_flush(1);
1019 } else {
1020 head_ref_namespaced(check_ref, NULL);
1021 for_each_namespaced_ref(check_ref, NULL);
1023 string_list_clear(&symref, 1);
1024 if (advertise_refs)
1025 return;
1027 receive_needs();
1028 if (want_obj.nr) {
1029 get_common_commits();
1030 create_pack_file();
1034 static int upload_pack_config(const char *var, const char *value, void *unused)
1036 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1037 if (git_config_bool(var, value))
1038 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1039 else
1040 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1041 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1042 if (git_config_bool(var, value))
1043 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1044 else
1045 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1046 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1047 if (git_config_bool(var, value))
1048 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1049 else
1050 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1051 } else if (!strcmp("uploadpack.keepalive", var)) {
1052 keepalive = git_config_int(var, value);
1053 if (!keepalive)
1054 keepalive = -1;
1055 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1056 if (!strcmp("uploadpack.packobjectshook", var))
1057 return git_config_string(&pack_objects_hook, var, value);
1058 } else if (!strcmp("uploadpack.allowfilter", var)) {
1059 filter_advertise = git_config_bool(var, value);
1061 return parse_hide_refs_config(var, value, "uploadpack");
1064 int cmd_main(int argc, const char **argv)
1066 const char *dir;
1067 int strict = 0;
1068 struct option options[] = {
1069 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1070 N_("quit after a single request/response exchange")),
1071 OPT_BOOL(0, "advertise-refs", &advertise_refs,
1072 N_("exit immediately after initial ref advertisement")),
1073 OPT_BOOL(0, "strict", &strict,
1074 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1075 OPT_INTEGER(0, "timeout", &timeout,
1076 N_("interrupt transfer after <n> seconds of inactivity")),
1077 OPT_END()
1080 packet_trace_identity("upload-pack");
1081 check_replace_refs = 0;
1083 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1085 if (argc != 1)
1086 usage_with_options(upload_pack_usage, options);
1088 if (timeout)
1089 daemon_mode = 1;
1091 setup_path();
1093 dir = argv[0];
1095 if (!enter_repo(dir, strict))
1096 die("'%s' does not appear to be a git repository", dir);
1098 git_config(upload_pack_config, NULL);
1100 switch (determine_protocol_version_server()) {
1101 case protocol_v1:
1103 * v1 is just the original protocol with a version string,
1104 * so just fall through after writing the version string.
1106 if (advertise_refs || !stateless_rpc)
1107 packet_write_fmt(1, "version 1\n");
1109 /* fallthrough */
1110 case protocol_v0:
1111 upload_pack();
1112 break;
1113 case protocol_unknown_version:
1114 BUG("unknown protocol version");
1117 return 0;