Merge branch 'jt/http-redact-cookies' into maint
[git.git] / upload-pack.c
blobd5de18127c63a9d017578e0969743392d39526d4
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 "run-command.h"
14 #include "connect.h"
15 #include "sigchain.h"
16 #include "version.h"
17 #include "string-list.h"
18 #include "parse-options.h"
19 #include "argv-array.h"
20 #include "prio-queue.h"
21 #include "protocol.h"
23 static const char * const upload_pack_usage[] = {
24 N_("git upload-pack [<options>] <dir>"),
25 NULL
28 /* Remember to update object flag allocation in object.h */
29 #define THEY_HAVE (1u << 11)
30 #define OUR_REF (1u << 12)
31 #define WANTED (1u << 13)
32 #define COMMON_KNOWN (1u << 14)
33 #define REACHABLE (1u << 15)
35 #define SHALLOW (1u << 16)
36 #define NOT_SHALLOW (1u << 17)
37 #define CLIENT_SHALLOW (1u << 18)
38 #define HIDDEN_REF (1u << 19)
40 static timestamp_t oldest_have;
42 static int deepen_relative;
43 static int multi_ack;
44 static int no_done;
45 static int use_thin_pack, use_ofs_delta, use_include_tag;
46 static int no_progress, daemon_mode;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request;
54 static int shallow_nr;
55 static struct object_array have_obj;
56 static struct object_array want_obj;
57 static struct object_array extra_edge_obj;
58 static unsigned int timeout;
59 static int keepalive = 5;
60 /* 0 for no sideband,
61 * otherwise maximum packet size (up to 65520 bytes).
63 static int use_sideband;
64 static int advertise_refs;
65 static int stateless_rpc;
66 static const char *pack_objects_hook;
68 static void reset_timeout(void)
70 alarm(timeout);
73 static void send_client_data(int fd, const char *data, ssize_t sz)
75 if (use_sideband) {
76 send_sideband(1, fd, data, sz, use_sideband);
77 return;
79 if (fd == 3)
80 /* emergency quit */
81 fd = 2;
82 if (fd == 2) {
83 /* XXX: are we happy to lose stuff here? */
84 xwrite(fd, data, sz);
85 return;
87 write_or_die(fd, data, sz);
90 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
92 FILE *fp = cb_data;
93 if (graft->nr_parent == -1)
94 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
95 return 0;
98 static void create_pack_file(void)
100 struct child_process pack_objects = CHILD_PROCESS_INIT;
101 char data[8193], progress[128];
102 char abort_msg[] = "aborting due to possible repository "
103 "corruption on the remote side.";
104 int buffered = -1;
105 ssize_t sz;
106 int i;
107 FILE *pipe_fd;
109 if (!pack_objects_hook)
110 pack_objects.git_cmd = 1;
111 else {
112 argv_array_push(&pack_objects.args, pack_objects_hook);
113 argv_array_push(&pack_objects.args, "git");
114 pack_objects.use_shell = 1;
117 if (shallow_nr) {
118 argv_array_push(&pack_objects.args, "--shallow-file");
119 argv_array_push(&pack_objects.args, "");
121 argv_array_push(&pack_objects.args, "pack-objects");
122 argv_array_push(&pack_objects.args, "--revs");
123 if (use_thin_pack)
124 argv_array_push(&pack_objects.args, "--thin");
126 argv_array_push(&pack_objects.args, "--stdout");
127 if (shallow_nr)
128 argv_array_push(&pack_objects.args, "--shallow");
129 if (!no_progress)
130 argv_array_push(&pack_objects.args, "--progress");
131 if (use_ofs_delta)
132 argv_array_push(&pack_objects.args, "--delta-base-offset");
133 if (use_include_tag)
134 argv_array_push(&pack_objects.args, "--include-tag");
136 pack_objects.in = -1;
137 pack_objects.out = -1;
138 pack_objects.err = -1;
140 if (start_command(&pack_objects))
141 die("git upload-pack: unable to fork git-pack-objects");
143 pipe_fd = xfdopen(pack_objects.in, "w");
145 if (shallow_nr)
146 for_each_commit_graft(write_one_shallow, pipe_fd);
148 for (i = 0; i < want_obj.nr; i++)
149 fprintf(pipe_fd, "%s\n",
150 oid_to_hex(&want_obj.objects[i].item->oid));
151 fprintf(pipe_fd, "--not\n");
152 for (i = 0; i < have_obj.nr; i++)
153 fprintf(pipe_fd, "%s\n",
154 oid_to_hex(&have_obj.objects[i].item->oid));
155 for (i = 0; i < extra_edge_obj.nr; i++)
156 fprintf(pipe_fd, "%s\n",
157 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
158 fprintf(pipe_fd, "\n");
159 fflush(pipe_fd);
160 fclose(pipe_fd);
162 /* We read from pack_objects.err to capture stderr output for
163 * progress bar, and pack_objects.out to capture the pack data.
166 while (1) {
167 struct pollfd pfd[2];
168 int pe, pu, pollsize;
169 int ret;
171 reset_timeout();
173 pollsize = 0;
174 pe = pu = -1;
176 if (0 <= pack_objects.out) {
177 pfd[pollsize].fd = pack_objects.out;
178 pfd[pollsize].events = POLLIN;
179 pu = pollsize;
180 pollsize++;
182 if (0 <= pack_objects.err) {
183 pfd[pollsize].fd = pack_objects.err;
184 pfd[pollsize].events = POLLIN;
185 pe = pollsize;
186 pollsize++;
189 if (!pollsize)
190 break;
192 ret = poll(pfd, pollsize,
193 keepalive < 0 ? -1 : 1000 * keepalive);
195 if (ret < 0) {
196 if (errno != EINTR) {
197 error_errno("poll failed, resuming");
198 sleep(1);
200 continue;
202 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
203 /* Status ready; we ship that in the side-band
204 * or dump to the standard error.
206 sz = xread(pack_objects.err, progress,
207 sizeof(progress));
208 if (0 < sz)
209 send_client_data(2, progress, sz);
210 else if (sz == 0) {
211 close(pack_objects.err);
212 pack_objects.err = -1;
214 else
215 goto fail;
216 /* give priority to status messages */
217 continue;
219 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
220 /* Data ready; we keep the last byte to ourselves
221 * in case we detect broken rev-list, so that we
222 * can leave the stream corrupted. This is
223 * unfortunate -- unpack-objects would happily
224 * accept a valid packdata with trailing garbage,
225 * so appending garbage after we pass all the
226 * pack data is not good enough to signal
227 * breakage to downstream.
229 char *cp = data;
230 ssize_t outsz = 0;
231 if (0 <= buffered) {
232 *cp++ = buffered;
233 outsz++;
235 sz = xread(pack_objects.out, cp,
236 sizeof(data) - outsz);
237 if (0 < sz)
239 else if (sz == 0) {
240 close(pack_objects.out);
241 pack_objects.out = -1;
243 else
244 goto fail;
245 sz += outsz;
246 if (1 < sz) {
247 buffered = data[sz-1] & 0xFF;
248 sz--;
250 else
251 buffered = -1;
252 send_client_data(1, data, sz);
256 * We hit the keepalive timeout without saying anything; send
257 * an empty message on the data sideband just to let the other
258 * side know we're still working on it, but don't have any data
259 * yet.
261 * If we don't have a sideband channel, there's no room in the
262 * protocol to say anything, so those clients are just out of
263 * luck.
265 if (!ret && use_sideband) {
266 static const char buf[] = "0005\1";
267 write_or_die(1, buf, 5);
271 if (finish_command(&pack_objects)) {
272 error("git upload-pack: git-pack-objects died with error.");
273 goto fail;
276 /* flush the data */
277 if (0 <= buffered) {
278 data[0] = buffered;
279 send_client_data(1, data, 1);
280 fprintf(stderr, "flushed.\n");
282 if (use_sideband)
283 packet_flush(1);
284 return;
286 fail:
287 send_client_data(3, abort_msg, sizeof(abort_msg));
288 die("git upload-pack: %s", abort_msg);
291 static int got_oid(const char *hex, struct object_id *oid)
293 struct object *o;
294 int we_knew_they_have = 0;
296 if (get_oid_hex(hex, oid))
297 die("git upload-pack: expected SHA1 object, got '%s'", hex);
298 if (!has_object_file(oid))
299 return -1;
301 o = parse_object(oid);
302 if (!o)
303 die("oops (%s)", oid_to_hex(oid));
304 if (o->type == OBJ_COMMIT) {
305 struct commit_list *parents;
306 struct commit *commit = (struct commit *)o;
307 if (o->flags & THEY_HAVE)
308 we_knew_they_have = 1;
309 else
310 o->flags |= THEY_HAVE;
311 if (!oldest_have || (commit->date < oldest_have))
312 oldest_have = commit->date;
313 for (parents = commit->parents;
314 parents;
315 parents = parents->next)
316 parents->item->object.flags |= THEY_HAVE;
318 if (!we_knew_they_have) {
319 add_object_array(o, NULL, &have_obj);
320 return 1;
322 return 0;
325 static int reachable(struct commit *want)
327 struct prio_queue work = { compare_commits_by_commit_date };
329 prio_queue_put(&work, want);
330 while (work.nr) {
331 struct commit_list *list;
332 struct commit *commit = prio_queue_get(&work);
334 if (commit->object.flags & THEY_HAVE) {
335 want->object.flags |= COMMON_KNOWN;
336 break;
338 if (!commit->object.parsed)
339 parse_object(&commit->object.oid);
340 if (commit->object.flags & REACHABLE)
341 continue;
342 commit->object.flags |= REACHABLE;
343 if (commit->date < oldest_have)
344 continue;
345 for (list = commit->parents; list; list = list->next) {
346 struct commit *parent = list->item;
347 if (!(parent->object.flags & REACHABLE))
348 prio_queue_put(&work, parent);
351 want->object.flags |= REACHABLE;
352 clear_commit_marks(want, REACHABLE);
353 clear_prio_queue(&work);
354 return (want->object.flags & COMMON_KNOWN);
357 static int ok_to_give_up(void)
359 int i;
361 if (!have_obj.nr)
362 return 0;
364 for (i = 0; i < want_obj.nr; i++) {
365 struct object *want = want_obj.objects[i].item;
367 if (want->flags & COMMON_KNOWN)
368 continue;
369 want = deref_tag(want, "a want line", 0);
370 if (!want || want->type != OBJ_COMMIT) {
371 /* no way to tell if this is reachable by
372 * looking at the ancestry chain alone, so
373 * leave a note to ourselves not to worry about
374 * this object anymore.
376 want_obj.objects[i].item->flags |= COMMON_KNOWN;
377 continue;
379 if (!reachable((struct commit *)want))
380 return 0;
382 return 1;
385 static int get_common_commits(void)
387 struct object_id oid;
388 char last_hex[GIT_MAX_HEXSZ + 1];
389 int got_common = 0;
390 int got_other = 0;
391 int sent_ready = 0;
393 save_commit_buffer = 0;
395 for (;;) {
396 char *line = packet_read_line(0, NULL);
397 const char *arg;
399 reset_timeout();
401 if (!line) {
402 if (multi_ack == 2 && got_common
403 && !got_other && ok_to_give_up()) {
404 sent_ready = 1;
405 packet_write_fmt(1, "ACK %s ready\n", last_hex);
407 if (have_obj.nr == 0 || multi_ack)
408 packet_write_fmt(1, "NAK\n");
410 if (no_done && sent_ready) {
411 packet_write_fmt(1, "ACK %s\n", last_hex);
412 return 0;
414 if (stateless_rpc)
415 exit(0);
416 got_common = 0;
417 got_other = 0;
418 continue;
420 if (skip_prefix(line, "have ", &arg)) {
421 switch (got_oid(arg, &oid)) {
422 case -1: /* they have what we do not */
423 got_other = 1;
424 if (multi_ack && ok_to_give_up()) {
425 const char *hex = oid_to_hex(&oid);
426 if (multi_ack == 2) {
427 sent_ready = 1;
428 packet_write_fmt(1, "ACK %s ready\n", hex);
429 } else
430 packet_write_fmt(1, "ACK %s continue\n", hex);
432 break;
433 default:
434 got_common = 1;
435 memcpy(last_hex, oid_to_hex(&oid), 41);
436 if (multi_ack == 2)
437 packet_write_fmt(1, "ACK %s common\n", last_hex);
438 else if (multi_ack)
439 packet_write_fmt(1, "ACK %s continue\n", last_hex);
440 else if (have_obj.nr == 1)
441 packet_write_fmt(1, "ACK %s\n", last_hex);
442 break;
444 continue;
446 if (!strcmp(line, "done")) {
447 if (have_obj.nr > 0) {
448 if (multi_ack)
449 packet_write_fmt(1, "ACK %s\n", last_hex);
450 return 0;
452 packet_write_fmt(1, "NAK\n");
453 return -1;
455 die("git upload-pack: expected SHA1 list, got '%s'", line);
459 static int is_our_ref(struct object *o)
461 int allow_hidden_ref = (allow_unadvertised_object_request &
462 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
463 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
467 * on successful case, it's up to the caller to close cmd->out
469 static int do_reachable_revlist(struct child_process *cmd,
470 struct object_array *src,
471 struct object_array *reachable)
473 static const char *argv[] = {
474 "rev-list", "--stdin", NULL,
476 struct object *o;
477 char namebuf[42]; /* ^ + SHA-1 + LF */
478 int i;
480 cmd->argv = argv;
481 cmd->git_cmd = 1;
482 cmd->no_stderr = 1;
483 cmd->in = -1;
484 cmd->out = -1;
487 * If the next rev-list --stdin encounters an unknown commit,
488 * it terminates, which will cause SIGPIPE in the write loop
489 * below.
491 sigchain_push(SIGPIPE, SIG_IGN);
493 if (start_command(cmd))
494 goto error;
496 namebuf[0] = '^';
497 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
498 for (i = get_max_object_index(); 0 < i; ) {
499 o = get_indexed_object(--i);
500 if (!o)
501 continue;
502 if (reachable && o->type == OBJ_COMMIT)
503 o->flags &= ~TMP_MARK;
504 if (!is_our_ref(o))
505 continue;
506 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
507 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
508 goto error;
510 namebuf[GIT_SHA1_HEXSZ] = '\n';
511 for (i = 0; i < src->nr; i++) {
512 o = src->objects[i].item;
513 if (is_our_ref(o)) {
514 if (reachable)
515 add_object_array(o, NULL, reachable);
516 continue;
518 if (reachable && o->type == OBJ_COMMIT)
519 o->flags |= TMP_MARK;
520 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
521 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
522 goto error;
524 close(cmd->in);
525 cmd->in = -1;
526 sigchain_pop(SIGPIPE);
528 return 0;
530 error:
531 sigchain_pop(SIGPIPE);
533 if (cmd->in >= 0)
534 close(cmd->in);
535 if (cmd->out >= 0)
536 close(cmd->out);
537 return -1;
540 static int get_reachable_list(struct object_array *src,
541 struct object_array *reachable)
543 struct child_process cmd = CHILD_PROCESS_INIT;
544 int i;
545 struct object *o;
546 char namebuf[42]; /* ^ + SHA-1 + LF */
548 if (do_reachable_revlist(&cmd, src, reachable) < 0)
549 return -1;
551 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
552 struct object_id sha1;
554 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
555 break;
557 o = lookup_object(sha1.hash);
558 if (o && o->type == OBJ_COMMIT) {
559 o->flags &= ~TMP_MARK;
562 for (i = get_max_object_index(); 0 < i; i--) {
563 o = get_indexed_object(i - 1);
564 if (o && o->type == OBJ_COMMIT &&
565 (o->flags & TMP_MARK)) {
566 add_object_array(o, NULL, reachable);
567 o->flags &= ~TMP_MARK;
570 close(cmd.out);
572 if (finish_command(&cmd))
573 return -1;
575 return 0;
578 static int has_unreachable(struct object_array *src)
580 struct child_process cmd = CHILD_PROCESS_INIT;
581 char buf[1];
582 int i;
584 if (do_reachable_revlist(&cmd, src, NULL) < 0)
585 return 1;
588 * The commits out of the rev-list are not ancestors of
589 * our ref.
591 i = read_in_full(cmd.out, buf, 1);
592 if (i)
593 goto error;
594 close(cmd.out);
595 cmd.out = -1;
598 * rev-list may have died by encountering a bad commit
599 * in the history, in which case we do want to bail out
600 * even when it showed no commit.
602 if (finish_command(&cmd))
603 goto error;
605 /* All the non-tip ones are ancestors of what we advertised */
606 return 0;
608 error:
609 sigchain_pop(SIGPIPE);
610 if (cmd.out >= 0)
611 close(cmd.out);
612 return 1;
615 static void check_non_tip(void)
617 int i;
620 * In the normal in-process case without
621 * uploadpack.allowReachableSHA1InWant,
622 * non-tip requests can never happen.
624 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
625 goto error;
626 if (!has_unreachable(&want_obj))
627 /* All the non-tip ones are ancestors of what we advertised */
628 return;
630 error:
631 /* Pick one of them (we know there at least is one) */
632 for (i = 0; i < want_obj.nr; i++) {
633 struct object *o = want_obj.objects[i].item;
634 if (!is_our_ref(o))
635 die("git upload-pack: not our ref %s",
636 oid_to_hex(&o->oid));
640 static void send_shallow(struct commit_list *result)
642 while (result) {
643 struct object *object = &result->item->object;
644 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
645 packet_write_fmt(1, "shallow %s",
646 oid_to_hex(&object->oid));
647 register_shallow(&object->oid);
648 shallow_nr++;
650 result = result->next;
654 static void send_unshallow(const struct object_array *shallows)
656 int i;
658 for (i = 0; i < shallows->nr; i++) {
659 struct object *object = shallows->objects[i].item;
660 if (object->flags & NOT_SHALLOW) {
661 struct commit_list *parents;
662 packet_write_fmt(1, "unshallow %s",
663 oid_to_hex(&object->oid));
664 object->flags &= ~CLIENT_SHALLOW;
666 * We want to _register_ "object" as shallow, but we
667 * also need to traverse object's parents to deepen a
668 * shallow clone. Unregister it for now so we can
669 * parse and add the parents to the want list, then
670 * re-register it.
672 unregister_shallow(&object->oid);
673 object->parsed = 0;
674 parse_commit_or_die((struct commit *)object);
675 parents = ((struct commit *)object)->parents;
676 while (parents) {
677 add_object_array(&parents->item->object,
678 NULL, &want_obj);
679 parents = parents->next;
681 add_object_array(object, NULL, &extra_edge_obj);
683 /* make sure commit traversal conforms to client */
684 register_shallow(&object->oid);
688 static void deepen(int depth, int deepen_relative,
689 struct object_array *shallows)
691 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
692 int i;
694 for (i = 0; i < shallows->nr; i++) {
695 struct object *object = shallows->objects[i].item;
696 object->flags |= NOT_SHALLOW;
698 } else if (deepen_relative) {
699 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
700 struct commit_list *result;
702 get_reachable_list(shallows, &reachable_shallows);
703 result = get_shallow_commits(&reachable_shallows,
704 depth + 1,
705 SHALLOW, NOT_SHALLOW);
706 send_shallow(result);
707 free_commit_list(result);
708 object_array_clear(&reachable_shallows);
709 } else {
710 struct commit_list *result;
712 result = get_shallow_commits(&want_obj, depth,
713 SHALLOW, NOT_SHALLOW);
714 send_shallow(result);
715 free_commit_list(result);
718 send_unshallow(shallows);
719 packet_flush(1);
722 static void deepen_by_rev_list(int ac, const char **av,
723 struct object_array *shallows)
725 struct commit_list *result;
727 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
728 send_shallow(result);
729 free_commit_list(result);
730 send_unshallow(shallows);
731 packet_flush(1);
734 static void receive_needs(void)
736 struct object_array shallows = OBJECT_ARRAY_INIT;
737 struct string_list deepen_not = STRING_LIST_INIT_DUP;
738 int depth = 0;
739 int has_non_tip = 0;
740 timestamp_t deepen_since = 0;
741 int deepen_rev_list = 0;
743 shallow_nr = 0;
744 for (;;) {
745 struct object *o;
746 const char *features;
747 struct object_id oid_buf;
748 char *line = packet_read_line(0, NULL);
749 const char *arg;
751 reset_timeout();
752 if (!line)
753 break;
755 if (skip_prefix(line, "shallow ", &arg)) {
756 struct object_id oid;
757 struct object *object;
758 if (get_oid_hex(arg, &oid))
759 die("invalid shallow line: %s", line);
760 object = parse_object(&oid);
761 if (!object)
762 continue;
763 if (object->type != OBJ_COMMIT)
764 die("invalid shallow object %s", oid_to_hex(&oid));
765 if (!(object->flags & CLIENT_SHALLOW)) {
766 object->flags |= CLIENT_SHALLOW;
767 add_object_array(object, NULL, &shallows);
769 continue;
771 if (skip_prefix(line, "deepen ", &arg)) {
772 char *end = NULL;
773 depth = strtol(arg, &end, 0);
774 if (!end || *end || depth <= 0)
775 die("Invalid deepen: %s", line);
776 continue;
778 if (skip_prefix(line, "deepen-since ", &arg)) {
779 char *end = NULL;
780 deepen_since = parse_timestamp(arg, &end, 0);
781 if (!end || *end || !deepen_since ||
782 /* revisions.c's max_age -1 is special */
783 deepen_since == -1)
784 die("Invalid deepen-since: %s", line);
785 deepen_rev_list = 1;
786 continue;
788 if (skip_prefix(line, "deepen-not ", &arg)) {
789 char *ref = NULL;
790 struct object_id oid;
791 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
792 die("git upload-pack: ambiguous deepen-not: %s", line);
793 string_list_append(&deepen_not, ref);
794 free(ref);
795 deepen_rev_list = 1;
796 continue;
798 if (!skip_prefix(line, "want ", &arg) ||
799 get_oid_hex(arg, &oid_buf))
800 die("git upload-pack: protocol error, "
801 "expected to get sha, not '%s'", line);
803 features = arg + 40;
805 if (parse_feature_request(features, "deepen-relative"))
806 deepen_relative = 1;
807 if (parse_feature_request(features, "multi_ack_detailed"))
808 multi_ack = 2;
809 else if (parse_feature_request(features, "multi_ack"))
810 multi_ack = 1;
811 if (parse_feature_request(features, "no-done"))
812 no_done = 1;
813 if (parse_feature_request(features, "thin-pack"))
814 use_thin_pack = 1;
815 if (parse_feature_request(features, "ofs-delta"))
816 use_ofs_delta = 1;
817 if (parse_feature_request(features, "side-band-64k"))
818 use_sideband = LARGE_PACKET_MAX;
819 else if (parse_feature_request(features, "side-band"))
820 use_sideband = DEFAULT_PACKET_MAX;
821 if (parse_feature_request(features, "no-progress"))
822 no_progress = 1;
823 if (parse_feature_request(features, "include-tag"))
824 use_include_tag = 1;
826 o = parse_object(&oid_buf);
827 if (!o) {
828 packet_write_fmt(1,
829 "ERR upload-pack: not our ref %s",
830 oid_to_hex(&oid_buf));
831 die("git upload-pack: not our ref %s",
832 oid_to_hex(&oid_buf));
834 if (!(o->flags & WANTED)) {
835 o->flags |= WANTED;
836 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
837 || is_our_ref(o)))
838 has_non_tip = 1;
839 add_object_array(o, NULL, &want_obj);
844 * We have sent all our refs already, and the other end
845 * should have chosen out of them. When we are operating
846 * in the stateless RPC mode, however, their choice may
847 * have been based on the set of older refs advertised
848 * by another process that handled the initial request.
850 if (has_non_tip)
851 check_non_tip();
853 if (!use_sideband && daemon_mode)
854 no_progress = 1;
856 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
857 return;
858 if (depth > 0 && deepen_rev_list)
859 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
860 if (depth > 0)
861 deepen(depth, deepen_relative, &shallows);
862 else if (deepen_rev_list) {
863 struct argv_array av = ARGV_ARRAY_INIT;
864 int i;
866 argv_array_push(&av, "rev-list");
867 if (deepen_since)
868 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
869 if (deepen_not.nr) {
870 argv_array_push(&av, "--not");
871 for (i = 0; i < deepen_not.nr; i++) {
872 struct string_list_item *s = deepen_not.items + i;
873 argv_array_push(&av, s->string);
875 argv_array_push(&av, "--not");
877 for (i = 0; i < want_obj.nr; i++) {
878 struct object *o = want_obj.objects[i].item;
879 argv_array_push(&av, oid_to_hex(&o->oid));
881 deepen_by_rev_list(av.argc, av.argv, &shallows);
882 argv_array_clear(&av);
884 else
885 if (shallows.nr > 0) {
886 int i;
887 for (i = 0; i < shallows.nr; i++)
888 register_shallow(&shallows.objects[i].item->oid);
891 shallow_nr += shallows.nr;
892 object_array_clear(&shallows);
895 /* return non-zero if the ref is hidden, otherwise 0 */
896 static int mark_our_ref(const char *refname, const char *refname_full,
897 const struct object_id *oid)
899 struct object *o = lookup_unknown_object(oid->hash);
901 if (ref_is_hidden(refname, refname_full)) {
902 o->flags |= HIDDEN_REF;
903 return 1;
905 o->flags |= OUR_REF;
906 return 0;
909 static int check_ref(const char *refname_full, const struct object_id *oid,
910 int flag, void *cb_data)
912 const char *refname = strip_namespace(refname_full);
914 mark_our_ref(refname, refname_full, oid);
915 return 0;
918 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
920 struct string_list_item *item;
922 if (!symref->nr)
923 return;
924 for_each_string_list_item(item, symref)
925 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
928 static int send_ref(const char *refname, const struct object_id *oid,
929 int flag, void *cb_data)
931 static const char *capabilities = "multi_ack thin-pack side-band"
932 " side-band-64k ofs-delta shallow deepen-since deepen-not"
933 " deepen-relative no-progress include-tag multi_ack_detailed";
934 const char *refname_nons = strip_namespace(refname);
935 struct object_id peeled;
937 if (mark_our_ref(refname_nons, refname, oid))
938 return 0;
940 if (capabilities) {
941 struct strbuf symref_info = STRBUF_INIT;
943 format_symref_info(&symref_info, cb_data);
944 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
945 oid_to_hex(oid), refname_nons,
946 0, capabilities,
947 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
948 " allow-tip-sha1-in-want" : "",
949 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
950 " allow-reachable-sha1-in-want" : "",
951 stateless_rpc ? " no-done" : "",
952 symref_info.buf,
953 git_user_agent_sanitized());
954 strbuf_release(&symref_info);
955 } else {
956 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
958 capabilities = NULL;
959 if (!peel_ref(refname, &peeled))
960 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
961 return 0;
964 static int find_symref(const char *refname, const struct object_id *oid,
965 int flag, void *cb_data)
967 const char *symref_target;
968 struct string_list_item *item;
970 if ((flag & REF_ISSYMREF) == 0)
971 return 0;
972 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
973 if (!symref_target || (flag & REF_ISSYMREF) == 0)
974 die("'%s' is a symref but it is not?", refname);
975 item = string_list_append(cb_data, refname);
976 item->util = xstrdup(symref_target);
977 return 0;
980 static void upload_pack(void)
982 struct string_list symref = STRING_LIST_INIT_DUP;
984 head_ref_namespaced(find_symref, &symref);
986 if (advertise_refs || !stateless_rpc) {
987 reset_timeout();
988 head_ref_namespaced(send_ref, &symref);
989 for_each_namespaced_ref(send_ref, &symref);
990 advertise_shallow_grafts(1);
991 packet_flush(1);
992 } else {
993 head_ref_namespaced(check_ref, NULL);
994 for_each_namespaced_ref(check_ref, NULL);
996 string_list_clear(&symref, 1);
997 if (advertise_refs)
998 return;
1000 receive_needs();
1001 if (want_obj.nr) {
1002 get_common_commits();
1003 create_pack_file();
1007 static int upload_pack_config(const char *var, const char *value, void *unused)
1009 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1010 if (git_config_bool(var, value))
1011 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1012 else
1013 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1014 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1015 if (git_config_bool(var, value))
1016 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1017 else
1018 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1019 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1020 if (git_config_bool(var, value))
1021 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1022 else
1023 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1024 } else if (!strcmp("uploadpack.keepalive", var)) {
1025 keepalive = git_config_int(var, value);
1026 if (!keepalive)
1027 keepalive = -1;
1028 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1029 if (!strcmp("uploadpack.packobjectshook", var))
1030 return git_config_string(&pack_objects_hook, var, value);
1032 return parse_hide_refs_config(var, value, "uploadpack");
1035 int cmd_main(int argc, const char **argv)
1037 const char *dir;
1038 int strict = 0;
1039 struct option options[] = {
1040 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1041 N_("quit after a single request/response exchange")),
1042 OPT_BOOL(0, "advertise-refs", &advertise_refs,
1043 N_("exit immediately after initial ref advertisement")),
1044 OPT_BOOL(0, "strict", &strict,
1045 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1046 OPT_INTEGER(0, "timeout", &timeout,
1047 N_("interrupt transfer after <n> seconds of inactivity")),
1048 OPT_END()
1051 packet_trace_identity("upload-pack");
1052 check_replace_refs = 0;
1054 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1056 if (argc != 1)
1057 usage_with_options(upload_pack_usage, options);
1059 if (timeout)
1060 daemon_mode = 1;
1062 setup_path();
1064 dir = argv[0];
1066 if (!enter_repo(dir, strict))
1067 die("'%s' does not appear to be a git repository", dir);
1069 git_config(upload_pack_config, NULL);
1071 switch (determine_protocol_version_server()) {
1072 case protocol_v1:
1074 * v1 is just the original protocol with a version string,
1075 * so just fall through after writing the version string.
1077 if (advertise_refs || !stateless_rpc)
1078 packet_write_fmt(1, "version 1\n");
1080 /* fallthrough */
1081 case protocol_v0:
1082 upload_pack();
1083 break;
1084 case protocol_unknown_version:
1085 BUG("unknown protocol version");
1088 return 0;