commit: add repository argument to read_graft_file
[git.git] / upload-pack.c
bloba11c6d192ceaab6771b2bc3e1e9c1c26cdae77c0
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "object-store.h"
7 #include "tag.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "exec_cmd.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 "parse-options.h"
22 #include "argv-array.h"
23 #include "prio-queue.h"
24 #include "protocol.h"
25 #include "quote.h"
27 static const char * const upload_pack_usage[] = {
28 N_("git upload-pack [<options>] <dir>"),
29 NULL
32 /* Remember to update object flag allocation in object.h */
33 #define THEY_HAVE (1u << 11)
34 #define OUR_REF (1u << 12)
35 #define WANTED (1u << 13)
36 #define COMMON_KNOWN (1u << 14)
37 #define REACHABLE (1u << 15)
39 #define SHALLOW (1u << 16)
40 #define NOT_SHALLOW (1u << 17)
41 #define CLIENT_SHALLOW (1u << 18)
42 #define HIDDEN_REF (1u << 19)
44 static timestamp_t oldest_have;
46 static int deepen_relative;
47 static int multi_ack;
48 static int no_done;
49 static int use_thin_pack, use_ofs_delta, use_include_tag;
50 static int no_progress, daemon_mode;
51 /* Allow specifying sha1 if it is a ref tip. */
52 #define ALLOW_TIP_SHA1 01
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 #define ALLOW_REACHABLE_SHA1 02
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 #define ALLOW_ANY_SHA1 07
57 static unsigned int allow_unadvertised_object_request;
58 static int shallow_nr;
59 static struct object_array have_obj;
60 static struct object_array want_obj;
61 static struct object_array extra_edge_obj;
62 static unsigned int timeout;
63 static int keepalive = 5;
64 /* 0 for no sideband,
65 * otherwise maximum packet size (up to 65520 bytes).
67 static int use_sideband;
68 static int advertise_refs;
69 static int stateless_rpc;
70 static const char *pack_objects_hook;
72 static int filter_capability_requested;
73 static int allow_filter;
74 static struct list_objects_filter_options filter_options;
76 static void reset_timeout(void)
78 alarm(timeout);
81 static void send_client_data(int fd, const char *data, ssize_t sz)
83 if (use_sideband) {
84 send_sideband(1, fd, data, sz, use_sideband);
85 return;
87 if (fd == 3)
88 /* emergency quit */
89 fd = 2;
90 if (fd == 2) {
91 /* XXX: are we happy to lose stuff here? */
92 xwrite(fd, data, sz);
93 return;
95 write_or_die(fd, data, sz);
98 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
100 FILE *fp = cb_data;
101 if (graft->nr_parent == -1)
102 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
103 return 0;
106 static void create_pack_file(void)
108 struct child_process pack_objects = CHILD_PROCESS_INIT;
109 char data[8193], progress[128];
110 char abort_msg[] = "aborting due to possible repository "
111 "corruption on the remote side.";
112 int buffered = -1;
113 ssize_t sz;
114 int i;
115 FILE *pipe_fd;
117 if (!pack_objects_hook)
118 pack_objects.git_cmd = 1;
119 else {
120 argv_array_push(&pack_objects.args, pack_objects_hook);
121 argv_array_push(&pack_objects.args, "git");
122 pack_objects.use_shell = 1;
125 if (shallow_nr) {
126 argv_array_push(&pack_objects.args, "--shallow-file");
127 argv_array_push(&pack_objects.args, "");
129 argv_array_push(&pack_objects.args, "pack-objects");
130 argv_array_push(&pack_objects.args, "--revs");
131 if (use_thin_pack)
132 argv_array_push(&pack_objects.args, "--thin");
134 argv_array_push(&pack_objects.args, "--stdout");
135 if (shallow_nr)
136 argv_array_push(&pack_objects.args, "--shallow");
137 if (!no_progress)
138 argv_array_push(&pack_objects.args, "--progress");
139 if (use_ofs_delta)
140 argv_array_push(&pack_objects.args, "--delta-base-offset");
141 if (use_include_tag)
142 argv_array_push(&pack_objects.args, "--include-tag");
143 if (filter_options.filter_spec) {
144 if (pack_objects.use_shell) {
145 struct strbuf buf = STRBUF_INIT;
146 sq_quote_buf(&buf, filter_options.filter_spec);
147 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
148 strbuf_release(&buf);
149 } else {
150 argv_array_pushf(&pack_objects.args, "--filter=%s",
151 filter_options.filter_spec);
155 pack_objects.in = -1;
156 pack_objects.out = -1;
157 pack_objects.err = -1;
159 if (start_command(&pack_objects))
160 die("git upload-pack: unable to fork git-pack-objects");
162 pipe_fd = xfdopen(pack_objects.in, "w");
164 if (shallow_nr)
165 for_each_commit_graft(write_one_shallow, pipe_fd);
167 for (i = 0; i < want_obj.nr; i++)
168 fprintf(pipe_fd, "%s\n",
169 oid_to_hex(&want_obj.objects[i].item->oid));
170 fprintf(pipe_fd, "--not\n");
171 for (i = 0; i < have_obj.nr; i++)
172 fprintf(pipe_fd, "%s\n",
173 oid_to_hex(&have_obj.objects[i].item->oid));
174 for (i = 0; i < extra_edge_obj.nr; i++)
175 fprintf(pipe_fd, "%s\n",
176 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
177 fprintf(pipe_fd, "\n");
178 fflush(pipe_fd);
179 fclose(pipe_fd);
181 /* We read from pack_objects.err to capture stderr output for
182 * progress bar, and pack_objects.out to capture the pack data.
185 while (1) {
186 struct pollfd pfd[2];
187 int pe, pu, pollsize;
188 int ret;
190 reset_timeout();
192 pollsize = 0;
193 pe = pu = -1;
195 if (0 <= pack_objects.out) {
196 pfd[pollsize].fd = pack_objects.out;
197 pfd[pollsize].events = POLLIN;
198 pu = pollsize;
199 pollsize++;
201 if (0 <= pack_objects.err) {
202 pfd[pollsize].fd = pack_objects.err;
203 pfd[pollsize].events = POLLIN;
204 pe = pollsize;
205 pollsize++;
208 if (!pollsize)
209 break;
211 ret = poll(pfd, pollsize,
212 keepalive < 0 ? -1 : 1000 * keepalive);
214 if (ret < 0) {
215 if (errno != EINTR) {
216 error_errno("poll failed, resuming");
217 sleep(1);
219 continue;
221 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
222 /* Status ready; we ship that in the side-band
223 * or dump to the standard error.
225 sz = xread(pack_objects.err, progress,
226 sizeof(progress));
227 if (0 < sz)
228 send_client_data(2, progress, sz);
229 else if (sz == 0) {
230 close(pack_objects.err);
231 pack_objects.err = -1;
233 else
234 goto fail;
235 /* give priority to status messages */
236 continue;
238 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
239 /* Data ready; we keep the last byte to ourselves
240 * in case we detect broken rev-list, so that we
241 * can leave the stream corrupted. This is
242 * unfortunate -- unpack-objects would happily
243 * accept a valid packdata with trailing garbage,
244 * so appending garbage after we pass all the
245 * pack data is not good enough to signal
246 * breakage to downstream.
248 char *cp = data;
249 ssize_t outsz = 0;
250 if (0 <= buffered) {
251 *cp++ = buffered;
252 outsz++;
254 sz = xread(pack_objects.out, cp,
255 sizeof(data) - outsz);
256 if (0 < sz)
258 else if (sz == 0) {
259 close(pack_objects.out);
260 pack_objects.out = -1;
262 else
263 goto fail;
264 sz += outsz;
265 if (1 < sz) {
266 buffered = data[sz-1] & 0xFF;
267 sz--;
269 else
270 buffered = -1;
271 send_client_data(1, data, sz);
275 * We hit the keepalive timeout without saying anything; send
276 * an empty message on the data sideband just to let the other
277 * side know we're still working on it, but don't have any data
278 * yet.
280 * If we don't have a sideband channel, there's no room in the
281 * protocol to say anything, so those clients are just out of
282 * luck.
284 if (!ret && use_sideband) {
285 static const char buf[] = "0005\1";
286 write_or_die(1, buf, 5);
290 if (finish_command(&pack_objects)) {
291 error("git upload-pack: git-pack-objects died with error.");
292 goto fail;
295 /* flush the data */
296 if (0 <= buffered) {
297 data[0] = buffered;
298 send_client_data(1, data, 1);
299 fprintf(stderr, "flushed.\n");
301 if (use_sideband)
302 packet_flush(1);
303 return;
305 fail:
306 send_client_data(3, abort_msg, sizeof(abort_msg));
307 die("git upload-pack: %s", abort_msg);
310 static int got_oid(const char *hex, struct object_id *oid)
312 struct object *o;
313 int we_knew_they_have = 0;
315 if (get_oid_hex(hex, oid))
316 die("git upload-pack: expected SHA1 object, got '%s'", hex);
317 if (!has_object_file(oid))
318 return -1;
320 o = parse_object(oid);
321 if (!o)
322 die("oops (%s)", oid_to_hex(oid));
323 if (o->type == OBJ_COMMIT) {
324 struct commit_list *parents;
325 struct commit *commit = (struct commit *)o;
326 if (o->flags & THEY_HAVE)
327 we_knew_they_have = 1;
328 else
329 o->flags |= THEY_HAVE;
330 if (!oldest_have || (commit->date < oldest_have))
331 oldest_have = commit->date;
332 for (parents = commit->parents;
333 parents;
334 parents = parents->next)
335 parents->item->object.flags |= THEY_HAVE;
337 if (!we_knew_they_have) {
338 add_object_array(o, NULL, &have_obj);
339 return 1;
341 return 0;
344 static int reachable(struct commit *want)
346 struct prio_queue work = { compare_commits_by_commit_date };
348 prio_queue_put(&work, want);
349 while (work.nr) {
350 struct commit_list *list;
351 struct commit *commit = prio_queue_get(&work);
353 if (commit->object.flags & THEY_HAVE) {
354 want->object.flags |= COMMON_KNOWN;
355 break;
357 if (!commit->object.parsed)
358 parse_object(&commit->object.oid);
359 if (commit->object.flags & REACHABLE)
360 continue;
361 commit->object.flags |= REACHABLE;
362 if (commit->date < oldest_have)
363 continue;
364 for (list = commit->parents; list; list = list->next) {
365 struct commit *parent = list->item;
366 if (!(parent->object.flags & REACHABLE))
367 prio_queue_put(&work, parent);
370 want->object.flags |= REACHABLE;
371 clear_commit_marks(want, REACHABLE);
372 clear_prio_queue(&work);
373 return (want->object.flags & COMMON_KNOWN);
376 static int ok_to_give_up(void)
378 int i;
380 if (!have_obj.nr)
381 return 0;
383 for (i = 0; i < want_obj.nr; i++) {
384 struct object *want = want_obj.objects[i].item;
386 if (want->flags & COMMON_KNOWN)
387 continue;
388 want = deref_tag(want, "a want line", 0);
389 if (!want || want->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 want_obj.objects[i].item->flags |= COMMON_KNOWN;
396 continue;
398 if (!reachable((struct commit *)want))
399 return 0;
401 return 1;
404 static int get_common_commits(void)
406 struct object_id oid;
407 char last_hex[GIT_MAX_HEXSZ + 1];
408 int got_common = 0;
409 int got_other = 0;
410 int sent_ready = 0;
412 save_commit_buffer = 0;
414 for (;;) {
415 char *line = packet_read_line(0, NULL);
416 const char *arg;
418 reset_timeout();
420 if (!line) {
421 if (multi_ack == 2 && got_common
422 && !got_other && ok_to_give_up()) {
423 sent_ready = 1;
424 packet_write_fmt(1, "ACK %s ready\n", last_hex);
426 if (have_obj.nr == 0 || multi_ack)
427 packet_write_fmt(1, "NAK\n");
429 if (no_done && sent_ready) {
430 packet_write_fmt(1, "ACK %s\n", last_hex);
431 return 0;
433 if (stateless_rpc)
434 exit(0);
435 got_common = 0;
436 got_other = 0;
437 continue;
439 if (skip_prefix(line, "have ", &arg)) {
440 switch (got_oid(arg, &oid)) {
441 case -1: /* they have what we do not */
442 got_other = 1;
443 if (multi_ack && ok_to_give_up()) {
444 const char *hex = oid_to_hex(&oid);
445 if (multi_ack == 2) {
446 sent_ready = 1;
447 packet_write_fmt(1, "ACK %s ready\n", hex);
448 } else
449 packet_write_fmt(1, "ACK %s continue\n", hex);
451 break;
452 default:
453 got_common = 1;
454 memcpy(last_hex, oid_to_hex(&oid), 41);
455 if (multi_ack == 2)
456 packet_write_fmt(1, "ACK %s common\n", last_hex);
457 else if (multi_ack)
458 packet_write_fmt(1, "ACK %s continue\n", last_hex);
459 else if (have_obj.nr == 1)
460 packet_write_fmt(1, "ACK %s\n", last_hex);
461 break;
463 continue;
465 if (!strcmp(line, "done")) {
466 if (have_obj.nr > 0) {
467 if (multi_ack)
468 packet_write_fmt(1, "ACK %s\n", last_hex);
469 return 0;
471 packet_write_fmt(1, "NAK\n");
472 return -1;
474 die("git upload-pack: expected SHA1 list, got '%s'", line);
478 static int is_our_ref(struct object *o)
480 int allow_hidden_ref = (allow_unadvertised_object_request &
481 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
482 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
486 * on successful case, it's up to the caller to close cmd->out
488 static int do_reachable_revlist(struct child_process *cmd,
489 struct object_array *src,
490 struct object_array *reachable)
492 static const char *argv[] = {
493 "rev-list", "--stdin", NULL,
495 struct object *o;
496 char namebuf[42]; /* ^ + SHA-1 + LF */
497 int i;
499 cmd->argv = argv;
500 cmd->git_cmd = 1;
501 cmd->no_stderr = 1;
502 cmd->in = -1;
503 cmd->out = -1;
506 * If the next rev-list --stdin encounters an unknown commit,
507 * it terminates, which will cause SIGPIPE in the write loop
508 * below.
510 sigchain_push(SIGPIPE, SIG_IGN);
512 if (start_command(cmd))
513 goto error;
515 namebuf[0] = '^';
516 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
517 for (i = get_max_object_index(); 0 < i; ) {
518 o = get_indexed_object(--i);
519 if (!o)
520 continue;
521 if (reachable && o->type == OBJ_COMMIT)
522 o->flags &= ~TMP_MARK;
523 if (!is_our_ref(o))
524 continue;
525 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
526 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
527 goto error;
529 namebuf[GIT_SHA1_HEXSZ] = '\n';
530 for (i = 0; i < src->nr; i++) {
531 o = src->objects[i].item;
532 if (is_our_ref(o)) {
533 if (reachable)
534 add_object_array(o, NULL, reachable);
535 continue;
537 if (reachable && o->type == OBJ_COMMIT)
538 o->flags |= TMP_MARK;
539 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
540 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
541 goto error;
543 close(cmd->in);
544 cmd->in = -1;
545 sigchain_pop(SIGPIPE);
547 return 0;
549 error:
550 sigchain_pop(SIGPIPE);
552 if (cmd->in >= 0)
553 close(cmd->in);
554 if (cmd->out >= 0)
555 close(cmd->out);
556 return -1;
559 static int get_reachable_list(struct object_array *src,
560 struct object_array *reachable)
562 struct child_process cmd = CHILD_PROCESS_INIT;
563 int i;
564 struct object *o;
565 char namebuf[42]; /* ^ + SHA-1 + LF */
567 if (do_reachable_revlist(&cmd, src, reachable) < 0)
568 return -1;
570 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
571 struct object_id sha1;
573 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
574 break;
576 o = lookup_object(sha1.hash);
577 if (o && o->type == OBJ_COMMIT) {
578 o->flags &= ~TMP_MARK;
581 for (i = get_max_object_index(); 0 < i; i--) {
582 o = get_indexed_object(i - 1);
583 if (o && o->type == OBJ_COMMIT &&
584 (o->flags & TMP_MARK)) {
585 add_object_array(o, NULL, reachable);
586 o->flags &= ~TMP_MARK;
589 close(cmd.out);
591 if (finish_command(&cmd))
592 return -1;
594 return 0;
597 static int has_unreachable(struct object_array *src)
599 struct child_process cmd = CHILD_PROCESS_INIT;
600 char buf[1];
601 int i;
603 if (do_reachable_revlist(&cmd, src, NULL) < 0)
604 return 1;
607 * The commits out of the rev-list are not ancestors of
608 * our ref.
610 i = read_in_full(cmd.out, buf, 1);
611 if (i)
612 goto error;
613 close(cmd.out);
614 cmd.out = -1;
617 * rev-list may have died by encountering a bad commit
618 * in the history, in which case we do want to bail out
619 * even when it showed no commit.
621 if (finish_command(&cmd))
622 goto error;
624 /* All the non-tip ones are ancestors of what we advertised */
625 return 0;
627 error:
628 sigchain_pop(SIGPIPE);
629 if (cmd.out >= 0)
630 close(cmd.out);
631 return 1;
634 static void check_non_tip(void)
636 int i;
639 * In the normal in-process case without
640 * uploadpack.allowReachableSHA1InWant,
641 * non-tip requests can never happen.
643 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
644 goto error;
645 if (!has_unreachable(&want_obj))
646 /* All the non-tip ones are ancestors of what we advertised */
647 return;
649 error:
650 /* Pick one of them (we know there at least is one) */
651 for (i = 0; i < want_obj.nr; i++) {
652 struct object *o = want_obj.objects[i].item;
653 if (!is_our_ref(o))
654 die("git upload-pack: not our ref %s",
655 oid_to_hex(&o->oid));
659 static void send_shallow(struct commit_list *result)
661 while (result) {
662 struct object *object = &result->item->object;
663 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
664 packet_write_fmt(1, "shallow %s",
665 oid_to_hex(&object->oid));
666 register_shallow(&object->oid);
667 shallow_nr++;
669 result = result->next;
673 static void send_unshallow(const struct object_array *shallows)
675 int i;
677 for (i = 0; i < shallows->nr; i++) {
678 struct object *object = shallows->objects[i].item;
679 if (object->flags & NOT_SHALLOW) {
680 struct commit_list *parents;
681 packet_write_fmt(1, "unshallow %s",
682 oid_to_hex(&object->oid));
683 object->flags &= ~CLIENT_SHALLOW;
685 * We want to _register_ "object" as shallow, but we
686 * also need to traverse object's parents to deepen a
687 * shallow clone. Unregister it for now so we can
688 * parse and add the parents to the want list, then
689 * re-register it.
691 unregister_shallow(&object->oid);
692 object->parsed = 0;
693 parse_commit_or_die((struct commit *)object);
694 parents = ((struct commit *)object)->parents;
695 while (parents) {
696 add_object_array(&parents->item->object,
697 NULL, &want_obj);
698 parents = parents->next;
700 add_object_array(object, NULL, &extra_edge_obj);
702 /* make sure commit traversal conforms to client */
703 register_shallow(&object->oid);
707 static void deepen(int depth, int deepen_relative,
708 struct object_array *shallows)
710 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
711 int i;
713 for (i = 0; i < shallows->nr; i++) {
714 struct object *object = shallows->objects[i].item;
715 object->flags |= NOT_SHALLOW;
717 } else if (deepen_relative) {
718 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
719 struct commit_list *result;
721 get_reachable_list(shallows, &reachable_shallows);
722 result = get_shallow_commits(&reachable_shallows,
723 depth + 1,
724 SHALLOW, NOT_SHALLOW);
725 send_shallow(result);
726 free_commit_list(result);
727 object_array_clear(&reachable_shallows);
728 } else {
729 struct commit_list *result;
731 result = get_shallow_commits(&want_obj, depth,
732 SHALLOW, NOT_SHALLOW);
733 send_shallow(result);
734 free_commit_list(result);
737 send_unshallow(shallows);
738 packet_flush(1);
741 static void deepen_by_rev_list(int ac, const char **av,
742 struct object_array *shallows)
744 struct commit_list *result;
746 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
747 send_shallow(result);
748 free_commit_list(result);
749 send_unshallow(shallows);
750 packet_flush(1);
753 static void receive_needs(void)
755 struct object_array shallows = OBJECT_ARRAY_INIT;
756 struct string_list deepen_not = STRING_LIST_INIT_DUP;
757 int depth = 0;
758 int has_non_tip = 0;
759 timestamp_t deepen_since = 0;
760 int deepen_rev_list = 0;
762 shallow_nr = 0;
763 for (;;) {
764 struct object *o;
765 const char *features;
766 struct object_id oid_buf;
767 char *line = packet_read_line(0, NULL);
768 const char *arg;
770 reset_timeout();
771 if (!line)
772 break;
774 if (skip_prefix(line, "shallow ", &arg)) {
775 struct object_id oid;
776 struct object *object;
777 if (get_oid_hex(arg, &oid))
778 die("invalid shallow line: %s", line);
779 object = parse_object(&oid);
780 if (!object)
781 continue;
782 if (object->type != OBJ_COMMIT)
783 die("invalid shallow object %s", oid_to_hex(&oid));
784 if (!(object->flags & CLIENT_SHALLOW)) {
785 object->flags |= CLIENT_SHALLOW;
786 add_object_array(object, NULL, &shallows);
788 continue;
790 if (skip_prefix(line, "deepen ", &arg)) {
791 char *end = NULL;
792 depth = strtol(arg, &end, 0);
793 if (!end || *end || depth <= 0)
794 die("Invalid deepen: %s", line);
795 continue;
797 if (skip_prefix(line, "deepen-since ", &arg)) {
798 char *end = NULL;
799 deepen_since = parse_timestamp(arg, &end, 0);
800 if (!end || *end || !deepen_since ||
801 /* revisions.c's max_age -1 is special */
802 deepen_since == -1)
803 die("Invalid deepen-since: %s", line);
804 deepen_rev_list = 1;
805 continue;
807 if (skip_prefix(line, "deepen-not ", &arg)) {
808 char *ref = NULL;
809 struct object_id oid;
810 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
811 die("git upload-pack: ambiguous deepen-not: %s", line);
812 string_list_append(&deepen_not, ref);
813 free(ref);
814 deepen_rev_list = 1;
815 continue;
817 if (skip_prefix(line, "filter ", &arg)) {
818 if (!filter_capability_requested)
819 die("git upload-pack: filtering capability not negotiated");
820 parse_list_objects_filter(&filter_options, arg);
821 continue;
823 if (!skip_prefix(line, "want ", &arg) ||
824 get_oid_hex(arg, &oid_buf))
825 die("git upload-pack: protocol error, "
826 "expected to get sha, not '%s'", line);
828 features = arg + 40;
830 if (parse_feature_request(features, "deepen-relative"))
831 deepen_relative = 1;
832 if (parse_feature_request(features, "multi_ack_detailed"))
833 multi_ack = 2;
834 else if (parse_feature_request(features, "multi_ack"))
835 multi_ack = 1;
836 if (parse_feature_request(features, "no-done"))
837 no_done = 1;
838 if (parse_feature_request(features, "thin-pack"))
839 use_thin_pack = 1;
840 if (parse_feature_request(features, "ofs-delta"))
841 use_ofs_delta = 1;
842 if (parse_feature_request(features, "side-band-64k"))
843 use_sideband = LARGE_PACKET_MAX;
844 else if (parse_feature_request(features, "side-band"))
845 use_sideband = DEFAULT_PACKET_MAX;
846 if (parse_feature_request(features, "no-progress"))
847 no_progress = 1;
848 if (parse_feature_request(features, "include-tag"))
849 use_include_tag = 1;
850 if (allow_filter && parse_feature_request(features, "filter"))
851 filter_capability_requested = 1;
853 o = parse_object(&oid_buf);
854 if (!o) {
855 packet_write_fmt(1,
856 "ERR upload-pack: not our ref %s",
857 oid_to_hex(&oid_buf));
858 die("git upload-pack: not our ref %s",
859 oid_to_hex(&oid_buf));
861 if (!(o->flags & WANTED)) {
862 o->flags |= WANTED;
863 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
864 || is_our_ref(o)))
865 has_non_tip = 1;
866 add_object_array(o, NULL, &want_obj);
871 * We have sent all our refs already, and the other end
872 * should have chosen out of them. When we are operating
873 * in the stateless RPC mode, however, their choice may
874 * have been based on the set of older refs advertised
875 * by another process that handled the initial request.
877 if (has_non_tip)
878 check_non_tip();
880 if (!use_sideband && daemon_mode)
881 no_progress = 1;
883 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
884 return;
885 if (depth > 0 && deepen_rev_list)
886 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
887 if (depth > 0)
888 deepen(depth, deepen_relative, &shallows);
889 else if (deepen_rev_list) {
890 struct argv_array av = ARGV_ARRAY_INIT;
891 int i;
893 argv_array_push(&av, "rev-list");
894 if (deepen_since)
895 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
896 if (deepen_not.nr) {
897 argv_array_push(&av, "--not");
898 for (i = 0; i < deepen_not.nr; i++) {
899 struct string_list_item *s = deepen_not.items + i;
900 argv_array_push(&av, s->string);
902 argv_array_push(&av, "--not");
904 for (i = 0; i < want_obj.nr; i++) {
905 struct object *o = want_obj.objects[i].item;
906 argv_array_push(&av, oid_to_hex(&o->oid));
908 deepen_by_rev_list(av.argc, av.argv, &shallows);
909 argv_array_clear(&av);
911 else
912 if (shallows.nr > 0) {
913 int i;
914 for (i = 0; i < shallows.nr; i++)
915 register_shallow(&shallows.objects[i].item->oid);
918 shallow_nr += shallows.nr;
919 object_array_clear(&shallows);
922 /* return non-zero if the ref is hidden, otherwise 0 */
923 static int mark_our_ref(const char *refname, const char *refname_full,
924 const struct object_id *oid)
926 struct object *o = lookup_unknown_object(oid->hash);
928 if (ref_is_hidden(refname, refname_full)) {
929 o->flags |= HIDDEN_REF;
930 return 1;
932 o->flags |= OUR_REF;
933 return 0;
936 static int check_ref(const char *refname_full, const struct object_id *oid,
937 int flag, void *cb_data)
939 const char *refname = strip_namespace(refname_full);
941 mark_our_ref(refname, refname_full, oid);
942 return 0;
945 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
947 struct string_list_item *item;
949 if (!symref->nr)
950 return;
951 for_each_string_list_item(item, symref)
952 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
955 static int send_ref(const char *refname, const struct object_id *oid,
956 int flag, void *cb_data)
958 static const char *capabilities = "multi_ack thin-pack side-band"
959 " side-band-64k ofs-delta shallow deepen-since deepen-not"
960 " deepen-relative no-progress include-tag multi_ack_detailed";
961 const char *refname_nons = strip_namespace(refname);
962 struct object_id peeled;
964 if (mark_our_ref(refname_nons, refname, oid))
965 return 0;
967 if (capabilities) {
968 struct strbuf symref_info = STRBUF_INIT;
970 format_symref_info(&symref_info, cb_data);
971 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
972 oid_to_hex(oid), refname_nons,
973 0, capabilities,
974 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
975 " allow-tip-sha1-in-want" : "",
976 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
977 " allow-reachable-sha1-in-want" : "",
978 stateless_rpc ? " no-done" : "",
979 symref_info.buf,
980 allow_filter ? " filter" : "",
981 git_user_agent_sanitized());
982 strbuf_release(&symref_info);
983 } else {
984 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
986 capabilities = NULL;
987 if (!peel_ref(refname, &peeled))
988 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
989 return 0;
992 static int find_symref(const char *refname, const struct object_id *oid,
993 int flag, void *cb_data)
995 const char *symref_target;
996 struct string_list_item *item;
998 if ((flag & REF_ISSYMREF) == 0)
999 return 0;
1000 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1001 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1002 die("'%s' is a symref but it is not?", refname);
1003 item = string_list_append(cb_data, refname);
1004 item->util = xstrdup(symref_target);
1005 return 0;
1008 static void upload_pack(void)
1010 struct string_list symref = STRING_LIST_INIT_DUP;
1012 head_ref_namespaced(find_symref, &symref);
1014 if (advertise_refs || !stateless_rpc) {
1015 reset_timeout();
1016 head_ref_namespaced(send_ref, &symref);
1017 for_each_namespaced_ref(send_ref, &symref);
1018 advertise_shallow_grafts(1);
1019 packet_flush(1);
1020 } else {
1021 head_ref_namespaced(check_ref, NULL);
1022 for_each_namespaced_ref(check_ref, NULL);
1024 string_list_clear(&symref, 1);
1025 if (advertise_refs)
1026 return;
1028 receive_needs();
1029 if (want_obj.nr) {
1030 get_common_commits();
1031 create_pack_file();
1035 static int upload_pack_config(const char *var, const char *value, void *unused)
1037 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1038 if (git_config_bool(var, value))
1039 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1040 else
1041 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1042 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1043 if (git_config_bool(var, value))
1044 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1045 else
1046 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1047 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1048 if (git_config_bool(var, value))
1049 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1050 else
1051 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1052 } else if (!strcmp("uploadpack.keepalive", var)) {
1053 keepalive = git_config_int(var, value);
1054 if (!keepalive)
1055 keepalive = -1;
1056 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1057 if (!strcmp("uploadpack.packobjectshook", var))
1058 return git_config_string(&pack_objects_hook, var, value);
1059 } else if (!strcmp("uploadpack.allowfilter", var)) {
1060 allow_filter = git_config_bool(var, value);
1062 return parse_hide_refs_config(var, value, "uploadpack");
1065 int cmd_main(int argc, const char **argv)
1067 const char *dir;
1068 int strict = 0;
1069 struct option options[] = {
1070 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1071 N_("quit after a single request/response exchange")),
1072 OPT_BOOL(0, "advertise-refs", &advertise_refs,
1073 N_("exit immediately after initial ref advertisement")),
1074 OPT_BOOL(0, "strict", &strict,
1075 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1076 OPT_INTEGER(0, "timeout", &timeout,
1077 N_("interrupt transfer after <n> seconds of inactivity")),
1078 OPT_END()
1081 packet_trace_identity("upload-pack");
1082 check_replace_refs = 0;
1084 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1086 if (argc != 1)
1087 usage_with_options(upload_pack_usage, options);
1089 if (timeout)
1090 daemon_mode = 1;
1092 setup_path();
1094 dir = argv[0];
1096 if (!enter_repo(dir, strict))
1097 die("'%s' does not appear to be a git repository", dir);
1099 git_config(upload_pack_config, NULL);
1101 switch (determine_protocol_version_server()) {
1102 case protocol_v1:
1104 * v1 is just the original protocol with a version string,
1105 * so just fall through after writing the version string.
1107 if (advertise_refs || !stateless_rpc)
1108 packet_write_fmt(1, "version 1\n");
1110 /* fallthrough */
1111 case protocol_v0:
1112 upload_pack();
1113 break;
1114 case protocol_unknown_version:
1115 BUG("unknown protocol version");
1118 return 0;