l10n: de.po: translate 210 new messages
[git.git] / upload-pack.c
blobe0db8b42be2d6ba400da987f8cb2ba573c5eda01
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "sideband.h"
5 #include "tag.h"
6 #include "object.h"
7 #include "commit.h"
8 #include "exec_cmd.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 #include "run-command.h"
13 #include "connect.h"
14 #include "sigchain.h"
15 #include "version.h"
16 #include "string-list.h"
17 #include "parse-options.h"
18 #include "argv-array.h"
19 #include "prio-queue.h"
21 static const char * const upload_pack_usage[] = {
22 N_("git upload-pack [<options>] <dir>"),
23 NULL
26 /* Remember to update object flag allocation in object.h */
27 #define THEY_HAVE (1u << 11)
28 #define OUR_REF (1u << 12)
29 #define WANTED (1u << 13)
30 #define COMMON_KNOWN (1u << 14)
31 #define REACHABLE (1u << 15)
33 #define SHALLOW (1u << 16)
34 #define NOT_SHALLOW (1u << 17)
35 #define CLIENT_SHALLOW (1u << 18)
36 #define HIDDEN_REF (1u << 19)
38 static unsigned long oldest_have;
40 static int deepen_relative;
41 static int multi_ack;
42 static int no_done;
43 static int use_thin_pack, use_ofs_delta, use_include_tag;
44 static int no_progress, daemon_mode;
45 /* Allow specifying sha1 if it is a ref tip. */
46 #define ALLOW_TIP_SHA1 01
47 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
48 #define ALLOW_REACHABLE_SHA1 02
49 static unsigned int allow_unadvertised_object_request;
50 static int shallow_nr;
51 static struct object_array have_obj;
52 static struct object_array want_obj;
53 static struct object_array extra_edge_obj;
54 static unsigned int timeout;
55 static int keepalive = 5;
56 /* 0 for no sideband,
57 * otherwise maximum packet size (up to 65520 bytes).
59 static int use_sideband;
60 static int advertise_refs;
61 static int stateless_rpc;
62 static const char *pack_objects_hook;
64 static void reset_timeout(void)
66 alarm(timeout);
69 static void send_client_data(int fd, const char *data, ssize_t sz)
71 if (use_sideband) {
72 send_sideband(1, fd, data, sz, use_sideband);
73 return;
75 if (fd == 3)
76 /* emergency quit */
77 fd = 2;
78 if (fd == 2) {
79 /* XXX: are we happy to lose stuff here? */
80 xwrite(fd, data, sz);
81 return;
83 write_or_die(fd, data, sz);
86 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
88 FILE *fp = cb_data;
89 if (graft->nr_parent == -1)
90 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
91 return 0;
94 static void create_pack_file(void)
96 struct child_process pack_objects = CHILD_PROCESS_INIT;
97 char data[8193], progress[128];
98 char abort_msg[] = "aborting due to possible repository "
99 "corruption on the remote side.";
100 int buffered = -1;
101 ssize_t sz;
102 int i;
103 FILE *pipe_fd;
105 if (!pack_objects_hook)
106 pack_objects.git_cmd = 1;
107 else {
108 argv_array_push(&pack_objects.args, pack_objects_hook);
109 argv_array_push(&pack_objects.args, "git");
110 pack_objects.use_shell = 1;
113 if (shallow_nr) {
114 argv_array_push(&pack_objects.args, "--shallow-file");
115 argv_array_push(&pack_objects.args, "");
117 argv_array_push(&pack_objects.args, "pack-objects");
118 argv_array_push(&pack_objects.args, "--revs");
119 if (use_thin_pack)
120 argv_array_push(&pack_objects.args, "--thin");
122 argv_array_push(&pack_objects.args, "--stdout");
123 if (shallow_nr)
124 argv_array_push(&pack_objects.args, "--shallow");
125 if (!no_progress)
126 argv_array_push(&pack_objects.args, "--progress");
127 if (use_ofs_delta)
128 argv_array_push(&pack_objects.args, "--delta-base-offset");
129 if (use_include_tag)
130 argv_array_push(&pack_objects.args, "--include-tag");
132 pack_objects.in = -1;
133 pack_objects.out = -1;
134 pack_objects.err = -1;
136 if (start_command(&pack_objects))
137 die("git upload-pack: unable to fork git-pack-objects");
139 pipe_fd = xfdopen(pack_objects.in, "w");
141 if (shallow_nr)
142 for_each_commit_graft(write_one_shallow, pipe_fd);
144 for (i = 0; i < want_obj.nr; i++)
145 fprintf(pipe_fd, "%s\n",
146 oid_to_hex(&want_obj.objects[i].item->oid));
147 fprintf(pipe_fd, "--not\n");
148 for (i = 0; i < have_obj.nr; i++)
149 fprintf(pipe_fd, "%s\n",
150 oid_to_hex(&have_obj.objects[i].item->oid));
151 for (i = 0; i < extra_edge_obj.nr; i++)
152 fprintf(pipe_fd, "%s\n",
153 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
154 fprintf(pipe_fd, "\n");
155 fflush(pipe_fd);
156 fclose(pipe_fd);
158 /* We read from pack_objects.err to capture stderr output for
159 * progress bar, and pack_objects.out to capture the pack data.
162 while (1) {
163 struct pollfd pfd[2];
164 int pe, pu, pollsize;
165 int ret;
167 reset_timeout();
169 pollsize = 0;
170 pe = pu = -1;
172 if (0 <= pack_objects.out) {
173 pfd[pollsize].fd = pack_objects.out;
174 pfd[pollsize].events = POLLIN;
175 pu = pollsize;
176 pollsize++;
178 if (0 <= pack_objects.err) {
179 pfd[pollsize].fd = pack_objects.err;
180 pfd[pollsize].events = POLLIN;
181 pe = pollsize;
182 pollsize++;
185 if (!pollsize)
186 break;
188 ret = poll(pfd, pollsize,
189 keepalive < 0 ? -1 : 1000 * keepalive);
191 if (ret < 0) {
192 if (errno != EINTR) {
193 error_errno("poll failed, resuming");
194 sleep(1);
196 continue;
198 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
199 /* Status ready; we ship that in the side-band
200 * or dump to the standard error.
202 sz = xread(pack_objects.err, progress,
203 sizeof(progress));
204 if (0 < sz)
205 send_client_data(2, progress, sz);
206 else if (sz == 0) {
207 close(pack_objects.err);
208 pack_objects.err = -1;
210 else
211 goto fail;
212 /* give priority to status messages */
213 continue;
215 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
216 /* Data ready; we keep the last byte to ourselves
217 * in case we detect broken rev-list, so that we
218 * can leave the stream corrupted. This is
219 * unfortunate -- unpack-objects would happily
220 * accept a valid packdata with trailing garbage,
221 * so appending garbage after we pass all the
222 * pack data is not good enough to signal
223 * breakage to downstream.
225 char *cp = data;
226 ssize_t outsz = 0;
227 if (0 <= buffered) {
228 *cp++ = buffered;
229 outsz++;
231 sz = xread(pack_objects.out, cp,
232 sizeof(data) - outsz);
233 if (0 < sz)
235 else if (sz == 0) {
236 close(pack_objects.out);
237 pack_objects.out = -1;
239 else
240 goto fail;
241 sz += outsz;
242 if (1 < sz) {
243 buffered = data[sz-1] & 0xFF;
244 sz--;
246 else
247 buffered = -1;
248 send_client_data(1, data, sz);
252 * We hit the keepalive timeout without saying anything; send
253 * an empty message on the data sideband just to let the other
254 * side know we're still working on it, but don't have any data
255 * yet.
257 * If we don't have a sideband channel, there's no room in the
258 * protocol to say anything, so those clients are just out of
259 * luck.
261 if (!ret && use_sideband) {
262 static const char buf[] = "0005\1";
263 write_or_die(1, buf, 5);
267 if (finish_command(&pack_objects)) {
268 error("git upload-pack: git-pack-objects died with error.");
269 goto fail;
272 /* flush the data */
273 if (0 <= buffered) {
274 data[0] = buffered;
275 send_client_data(1, data, 1);
276 fprintf(stderr, "flushed.\n");
278 if (use_sideband)
279 packet_flush(1);
280 return;
282 fail:
283 send_client_data(3, abort_msg, sizeof(abort_msg));
284 die("git upload-pack: %s", abort_msg);
287 static int got_sha1(const char *hex, unsigned char *sha1)
289 struct object *o;
290 int we_knew_they_have = 0;
292 if (get_sha1_hex(hex, sha1))
293 die("git upload-pack: expected SHA1 object, got '%s'", hex);
294 if (!has_sha1_file(sha1))
295 return -1;
297 o = parse_object(sha1);
298 if (!o)
299 die("oops (%s)", sha1_to_hex(sha1));
300 if (o->type == OBJ_COMMIT) {
301 struct commit_list *parents;
302 struct commit *commit = (struct commit *)o;
303 if (o->flags & THEY_HAVE)
304 we_knew_they_have = 1;
305 else
306 o->flags |= THEY_HAVE;
307 if (!oldest_have || (commit->date < oldest_have))
308 oldest_have = commit->date;
309 for (parents = commit->parents;
310 parents;
311 parents = parents->next)
312 parents->item->object.flags |= THEY_HAVE;
314 if (!we_knew_they_have) {
315 add_object_array(o, NULL, &have_obj);
316 return 1;
318 return 0;
321 static int reachable(struct commit *want)
323 struct prio_queue work = { compare_commits_by_commit_date };
325 prio_queue_put(&work, want);
326 while (work.nr) {
327 struct commit_list *list;
328 struct commit *commit = prio_queue_get(&work);
330 if (commit->object.flags & THEY_HAVE) {
331 want->object.flags |= COMMON_KNOWN;
332 break;
334 if (!commit->object.parsed)
335 parse_object(commit->object.oid.hash);
336 if (commit->object.flags & REACHABLE)
337 continue;
338 commit->object.flags |= REACHABLE;
339 if (commit->date < oldest_have)
340 continue;
341 for (list = commit->parents; list; list = list->next) {
342 struct commit *parent = list->item;
343 if (!(parent->object.flags & REACHABLE))
344 prio_queue_put(&work, parent);
347 want->object.flags |= REACHABLE;
348 clear_commit_marks(want, REACHABLE);
349 clear_prio_queue(&work);
350 return (want->object.flags & COMMON_KNOWN);
353 static int ok_to_give_up(void)
355 int i;
357 if (!have_obj.nr)
358 return 0;
360 for (i = 0; i < want_obj.nr; i++) {
361 struct object *want = want_obj.objects[i].item;
363 if (want->flags & COMMON_KNOWN)
364 continue;
365 want = deref_tag(want, "a want line", 0);
366 if (!want || want->type != OBJ_COMMIT) {
367 /* no way to tell if this is reachable by
368 * looking at the ancestry chain alone, so
369 * leave a note to ourselves not to worry about
370 * this object anymore.
372 want_obj.objects[i].item->flags |= COMMON_KNOWN;
373 continue;
375 if (!reachable((struct commit *)want))
376 return 0;
378 return 1;
381 static int get_common_commits(void)
383 unsigned char sha1[20];
384 char last_hex[41];
385 int got_common = 0;
386 int got_other = 0;
387 int sent_ready = 0;
389 save_commit_buffer = 0;
391 for (;;) {
392 char *line = packet_read_line(0, NULL);
393 const char *arg;
395 reset_timeout();
397 if (!line) {
398 if (multi_ack == 2 && got_common
399 && !got_other && ok_to_give_up()) {
400 sent_ready = 1;
401 packet_write_fmt(1, "ACK %s ready\n", last_hex);
403 if (have_obj.nr == 0 || multi_ack)
404 packet_write_fmt(1, "NAK\n");
406 if (no_done && sent_ready) {
407 packet_write_fmt(1, "ACK %s\n", last_hex);
408 return 0;
410 if (stateless_rpc)
411 exit(0);
412 got_common = 0;
413 got_other = 0;
414 continue;
416 if (skip_prefix(line, "have ", &arg)) {
417 switch (got_sha1(arg, sha1)) {
418 case -1: /* they have what we do not */
419 got_other = 1;
420 if (multi_ack && ok_to_give_up()) {
421 const char *hex = sha1_to_hex(sha1);
422 if (multi_ack == 2) {
423 sent_ready = 1;
424 packet_write_fmt(1, "ACK %s ready\n", hex);
425 } else
426 packet_write_fmt(1, "ACK %s continue\n", hex);
428 break;
429 default:
430 got_common = 1;
431 memcpy(last_hex, sha1_to_hex(sha1), 41);
432 if (multi_ack == 2)
433 packet_write_fmt(1, "ACK %s common\n", last_hex);
434 else if (multi_ack)
435 packet_write_fmt(1, "ACK %s continue\n", last_hex);
436 else if (have_obj.nr == 1)
437 packet_write_fmt(1, "ACK %s\n", last_hex);
438 break;
440 continue;
442 if (!strcmp(line, "done")) {
443 if (have_obj.nr > 0) {
444 if (multi_ack)
445 packet_write_fmt(1, "ACK %s\n", last_hex);
446 return 0;
448 packet_write_fmt(1, "NAK\n");
449 return -1;
451 die("git upload-pack: expected SHA1 list, got '%s'", line);
455 static int is_our_ref(struct object *o)
457 int allow_hidden_ref = (allow_unadvertised_object_request &
458 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
459 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
463 * on successful case, it's up to the caller to close cmd->out
465 static int do_reachable_revlist(struct child_process *cmd,
466 struct object_array *src,
467 struct object_array *reachable)
469 static const char *argv[] = {
470 "rev-list", "--stdin", NULL,
472 struct object *o;
473 char namebuf[42]; /* ^ + SHA-1 + LF */
474 int i;
476 cmd->argv = argv;
477 cmd->git_cmd = 1;
478 cmd->no_stderr = 1;
479 cmd->in = -1;
480 cmd->out = -1;
483 * If the next rev-list --stdin encounters an unknown commit,
484 * it terminates, which will cause SIGPIPE in the write loop
485 * below.
487 sigchain_push(SIGPIPE, SIG_IGN);
489 if (start_command(cmd))
490 goto error;
492 namebuf[0] = '^';
493 namebuf[41] = '\n';
494 for (i = get_max_object_index(); 0 < i; ) {
495 o = get_indexed_object(--i);
496 if (!o)
497 continue;
498 if (reachable && o->type == OBJ_COMMIT)
499 o->flags &= ~TMP_MARK;
500 if (!is_our_ref(o))
501 continue;
502 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
503 if (write_in_full(cmd->in, namebuf, 42) < 0)
504 goto error;
506 namebuf[40] = '\n';
507 for (i = 0; i < src->nr; i++) {
508 o = src->objects[i].item;
509 if (is_our_ref(o)) {
510 if (reachable)
511 add_object_array(o, NULL, reachable);
512 continue;
514 if (reachable && o->type == OBJ_COMMIT)
515 o->flags |= TMP_MARK;
516 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
517 if (write_in_full(cmd->in, namebuf, 41) < 0)
518 goto error;
520 close(cmd->in);
521 cmd->in = -1;
522 sigchain_pop(SIGPIPE);
524 return 0;
526 error:
527 sigchain_pop(SIGPIPE);
529 if (cmd->in >= 0)
530 close(cmd->in);
531 if (cmd->out >= 0)
532 close(cmd->out);
533 return -1;
536 static int get_reachable_list(struct object_array *src,
537 struct object_array *reachable)
539 struct child_process cmd = CHILD_PROCESS_INIT;
540 int i;
541 struct object *o;
542 char namebuf[42]; /* ^ + SHA-1 + LF */
544 if (do_reachable_revlist(&cmd, src, reachable) < 0)
545 return -1;
547 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
548 struct object_id sha1;
550 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
551 break;
553 o = lookup_object(sha1.hash);
554 if (o && o->type == OBJ_COMMIT) {
555 o->flags &= ~TMP_MARK;
558 for (i = get_max_object_index(); 0 < i; i--) {
559 o = get_indexed_object(i - 1);
560 if (o && o->type == OBJ_COMMIT &&
561 (o->flags & TMP_MARK)) {
562 add_object_array(o, NULL, reachable);
563 o->flags &= ~TMP_MARK;
566 close(cmd.out);
568 if (finish_command(&cmd))
569 return -1;
571 return 0;
574 static int has_unreachable(struct object_array *src)
576 struct child_process cmd = CHILD_PROCESS_INIT;
577 char buf[1];
578 int i;
580 if (do_reachable_revlist(&cmd, src, NULL) < 0)
581 return 1;
584 * The commits out of the rev-list are not ancestors of
585 * our ref.
587 i = read_in_full(cmd.out, buf, 1);
588 if (i)
589 goto error;
590 close(cmd.out);
591 cmd.out = -1;
594 * rev-list may have died by encountering a bad commit
595 * in the history, in which case we do want to bail out
596 * even when it showed no commit.
598 if (finish_command(&cmd))
599 goto error;
601 /* All the non-tip ones are ancestors of what we advertised */
602 return 0;
604 error:
605 sigchain_pop(SIGPIPE);
606 if (cmd.out >= 0)
607 close(cmd.out);
608 return 1;
611 static void check_non_tip(void)
613 int i;
616 * In the normal in-process case without
617 * uploadpack.allowReachableSHA1InWant,
618 * non-tip requests can never happen.
620 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
621 goto error;
622 if (!has_unreachable(&want_obj))
623 /* All the non-tip ones are ancestors of what we advertised */
624 return;
626 error:
627 /* Pick one of them (we know there at least is one) */
628 for (i = 0; i < want_obj.nr; i++) {
629 struct object *o = want_obj.objects[i].item;
630 if (!is_our_ref(o))
631 die("git upload-pack: not our ref %s",
632 oid_to_hex(&o->oid));
636 static void send_shallow(struct commit_list *result)
638 while (result) {
639 struct object *object = &result->item->object;
640 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
641 packet_write_fmt(1, "shallow %s",
642 oid_to_hex(&object->oid));
643 register_shallow(object->oid.hash);
644 shallow_nr++;
646 result = result->next;
650 static void send_unshallow(const struct object_array *shallows)
652 int i;
654 for (i = 0; i < shallows->nr; i++) {
655 struct object *object = shallows->objects[i].item;
656 if (object->flags & NOT_SHALLOW) {
657 struct commit_list *parents;
658 packet_write_fmt(1, "unshallow %s",
659 oid_to_hex(&object->oid));
660 object->flags &= ~CLIENT_SHALLOW;
662 * We want to _register_ "object" as shallow, but we
663 * also need to traverse object's parents to deepen a
664 * shallow clone. Unregister it for now so we can
665 * parse and add the parents to the want list, then
666 * re-register it.
668 unregister_shallow(object->oid.hash);
669 object->parsed = 0;
670 parse_commit_or_die((struct commit *)object);
671 parents = ((struct commit *)object)->parents;
672 while (parents) {
673 add_object_array(&parents->item->object,
674 NULL, &want_obj);
675 parents = parents->next;
677 add_object_array(object, NULL, &extra_edge_obj);
679 /* make sure commit traversal conforms to client */
680 register_shallow(object->oid.hash);
684 static void deepen(int depth, int deepen_relative,
685 struct object_array *shallows)
687 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
688 int i;
690 for (i = 0; i < shallows->nr; i++) {
691 struct object *object = shallows->objects[i].item;
692 object->flags |= NOT_SHALLOW;
694 } else if (deepen_relative) {
695 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
696 struct commit_list *result;
698 get_reachable_list(shallows, &reachable_shallows);
699 result = get_shallow_commits(&reachable_shallows,
700 depth + 1,
701 SHALLOW, NOT_SHALLOW);
702 send_shallow(result);
703 free_commit_list(result);
704 object_array_clear(&reachable_shallows);
705 } else {
706 struct commit_list *result;
708 result = get_shallow_commits(&want_obj, depth,
709 SHALLOW, NOT_SHALLOW);
710 send_shallow(result);
711 free_commit_list(result);
714 send_unshallow(shallows);
715 packet_flush(1);
718 static void deepen_by_rev_list(int ac, const char **av,
719 struct object_array *shallows)
721 struct commit_list *result;
723 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
724 send_shallow(result);
725 free_commit_list(result);
726 send_unshallow(shallows);
727 packet_flush(1);
730 static void receive_needs(void)
732 struct object_array shallows = OBJECT_ARRAY_INIT;
733 struct string_list deepen_not = STRING_LIST_INIT_DUP;
734 int depth = 0;
735 int has_non_tip = 0;
736 unsigned long deepen_since = 0;
737 int deepen_rev_list = 0;
739 shallow_nr = 0;
740 for (;;) {
741 struct object *o;
742 const char *features;
743 unsigned char sha1_buf[20];
744 char *line = packet_read_line(0, NULL);
745 const char *arg;
747 reset_timeout();
748 if (!line)
749 break;
751 if (skip_prefix(line, "shallow ", &arg)) {
752 unsigned char sha1[20];
753 struct object *object;
754 if (get_sha1_hex(arg, sha1))
755 die("invalid shallow line: %s", line);
756 object = parse_object(sha1);
757 if (!object)
758 continue;
759 if (object->type != OBJ_COMMIT)
760 die("invalid shallow object %s", sha1_to_hex(sha1));
761 if (!(object->flags & CLIENT_SHALLOW)) {
762 object->flags |= CLIENT_SHALLOW;
763 add_object_array(object, NULL, &shallows);
765 continue;
767 if (skip_prefix(line, "deepen ", &arg)) {
768 char *end = NULL;
769 depth = strtol(arg, &end, 0);
770 if (!end || *end || depth <= 0)
771 die("Invalid deepen: %s", line);
772 continue;
774 if (skip_prefix(line, "deepen-since ", &arg)) {
775 char *end = NULL;
776 deepen_since = strtoul(arg, &end, 0);
777 if (!end || *end || !deepen_since ||
778 /* revisions.c's max_age -1 is special */
779 deepen_since == -1)
780 die("Invalid deepen-since: %s", line);
781 deepen_rev_list = 1;
782 continue;
784 if (skip_prefix(line, "deepen-not ", &arg)) {
785 char *ref = NULL;
786 unsigned char sha1[20];
787 if (expand_ref(arg, strlen(arg), sha1, &ref) != 1)
788 die("git upload-pack: ambiguous deepen-not: %s", line);
789 string_list_append(&deepen_not, ref);
790 free(ref);
791 deepen_rev_list = 1;
792 continue;
794 if (!skip_prefix(line, "want ", &arg) ||
795 get_sha1_hex(arg, sha1_buf))
796 die("git upload-pack: protocol error, "
797 "expected to get sha, not '%s'", line);
799 features = arg + 40;
801 if (parse_feature_request(features, "deepen-relative"))
802 deepen_relative = 1;
803 if (parse_feature_request(features, "multi_ack_detailed"))
804 multi_ack = 2;
805 else if (parse_feature_request(features, "multi_ack"))
806 multi_ack = 1;
807 if (parse_feature_request(features, "no-done"))
808 no_done = 1;
809 if (parse_feature_request(features, "thin-pack"))
810 use_thin_pack = 1;
811 if (parse_feature_request(features, "ofs-delta"))
812 use_ofs_delta = 1;
813 if (parse_feature_request(features, "side-band-64k"))
814 use_sideband = LARGE_PACKET_MAX;
815 else if (parse_feature_request(features, "side-band"))
816 use_sideband = DEFAULT_PACKET_MAX;
817 if (parse_feature_request(features, "no-progress"))
818 no_progress = 1;
819 if (parse_feature_request(features, "include-tag"))
820 use_include_tag = 1;
822 o = parse_object(sha1_buf);
823 if (!o)
824 die("git upload-pack: not our ref %s",
825 sha1_to_hex(sha1_buf));
826 if (!(o->flags & WANTED)) {
827 o->flags |= WANTED;
828 if (!is_our_ref(o))
829 has_non_tip = 1;
830 add_object_array(o, NULL, &want_obj);
835 * We have sent all our refs already, and the other end
836 * should have chosen out of them. When we are operating
837 * in the stateless RPC mode, however, their choice may
838 * have been based on the set of older refs advertised
839 * by another process that handled the initial request.
841 if (has_non_tip)
842 check_non_tip();
844 if (!use_sideband && daemon_mode)
845 no_progress = 1;
847 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
848 return;
849 if (depth > 0 && deepen_rev_list)
850 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
851 if (depth > 0)
852 deepen(depth, deepen_relative, &shallows);
853 else if (deepen_rev_list) {
854 struct argv_array av = ARGV_ARRAY_INIT;
855 int i;
857 argv_array_push(&av, "rev-list");
858 if (deepen_since)
859 argv_array_pushf(&av, "--max-age=%lu", deepen_since);
860 if (deepen_not.nr) {
861 argv_array_push(&av, "--not");
862 for (i = 0; i < deepen_not.nr; i++) {
863 struct string_list_item *s = deepen_not.items + i;
864 argv_array_push(&av, s->string);
866 argv_array_push(&av, "--not");
868 for (i = 0; i < want_obj.nr; i++) {
869 struct object *o = want_obj.objects[i].item;
870 argv_array_push(&av, oid_to_hex(&o->oid));
872 deepen_by_rev_list(av.argc, av.argv, &shallows);
873 argv_array_clear(&av);
875 else
876 if (shallows.nr > 0) {
877 int i;
878 for (i = 0; i < shallows.nr; i++)
879 register_shallow(shallows.objects[i].item->oid.hash);
882 shallow_nr += shallows.nr;
883 free(shallows.objects);
886 /* return non-zero if the ref is hidden, otherwise 0 */
887 static int mark_our_ref(const char *refname, const char *refname_full,
888 const struct object_id *oid)
890 struct object *o = lookup_unknown_object(oid->hash);
892 if (ref_is_hidden(refname, refname_full)) {
893 o->flags |= HIDDEN_REF;
894 return 1;
896 o->flags |= OUR_REF;
897 return 0;
900 static int check_ref(const char *refname_full, const struct object_id *oid,
901 int flag, void *cb_data)
903 const char *refname = strip_namespace(refname_full);
905 mark_our_ref(refname, refname_full, oid);
906 return 0;
909 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
911 struct string_list_item *item;
913 if (!symref->nr)
914 return;
915 for_each_string_list_item(item, symref)
916 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
919 static int send_ref(const char *refname, const struct object_id *oid,
920 int flag, void *cb_data)
922 static const char *capabilities = "multi_ack thin-pack side-band"
923 " side-band-64k ofs-delta shallow deepen-since deepen-not"
924 " deepen-relative no-progress include-tag multi_ack_detailed";
925 const char *refname_nons = strip_namespace(refname);
926 struct object_id peeled;
928 if (mark_our_ref(refname_nons, refname, oid))
929 return 0;
931 if (capabilities) {
932 struct strbuf symref_info = STRBUF_INIT;
934 format_symref_info(&symref_info, cb_data);
935 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
936 oid_to_hex(oid), refname_nons,
937 0, capabilities,
938 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
939 " allow-tip-sha1-in-want" : "",
940 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
941 " allow-reachable-sha1-in-want" : "",
942 stateless_rpc ? " no-done" : "",
943 symref_info.buf,
944 git_user_agent_sanitized());
945 strbuf_release(&symref_info);
946 } else {
947 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
949 capabilities = NULL;
950 if (!peel_ref(refname, peeled.hash))
951 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
952 return 0;
955 static int find_symref(const char *refname, const struct object_id *oid,
956 int flag, void *cb_data)
958 const char *symref_target;
959 struct string_list_item *item;
960 struct object_id unused;
962 if ((flag & REF_ISSYMREF) == 0)
963 return 0;
964 symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
965 if (!symref_target || (flag & REF_ISSYMREF) == 0)
966 die("'%s' is a symref but it is not?", refname);
967 item = string_list_append(cb_data, refname);
968 item->util = xstrdup(symref_target);
969 return 0;
972 static void upload_pack(void)
974 struct string_list symref = STRING_LIST_INIT_DUP;
976 head_ref_namespaced(find_symref, &symref);
978 if (advertise_refs || !stateless_rpc) {
979 reset_timeout();
980 head_ref_namespaced(send_ref, &symref);
981 for_each_namespaced_ref(send_ref, &symref);
982 advertise_shallow_grafts(1);
983 packet_flush(1);
984 } else {
985 head_ref_namespaced(check_ref, NULL);
986 for_each_namespaced_ref(check_ref, NULL);
988 string_list_clear(&symref, 1);
989 if (advertise_refs)
990 return;
992 receive_needs();
993 if (want_obj.nr) {
994 get_common_commits();
995 create_pack_file();
999 static int upload_pack_config(const char *var, const char *value, void *unused)
1001 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1002 if (git_config_bool(var, value))
1003 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1004 else
1005 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1006 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1007 if (git_config_bool(var, value))
1008 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1009 else
1010 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1011 } else if (!strcmp("uploadpack.keepalive", var)) {
1012 keepalive = git_config_int(var, value);
1013 if (!keepalive)
1014 keepalive = -1;
1015 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1016 if (!strcmp("uploadpack.packobjectshook", var))
1017 return git_config_string(&pack_objects_hook, var, value);
1019 return parse_hide_refs_config(var, value, "uploadpack");
1022 int cmd_main(int argc, const char **argv)
1024 const char *dir;
1025 int strict = 0;
1026 struct option options[] = {
1027 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1028 N_("quit after a single request/response exchange")),
1029 OPT_BOOL(0, "advertise-refs", &advertise_refs,
1030 N_("exit immediately after initial ref advertisement")),
1031 OPT_BOOL(0, "strict", &strict,
1032 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1033 OPT_INTEGER(0, "timeout", &timeout,
1034 N_("interrupt transfer after <n> seconds of inactivity")),
1035 OPT_END()
1038 packet_trace_identity("upload-pack");
1039 check_replace_refs = 0;
1041 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1043 if (argc != 1)
1044 usage_with_options(upload_pack_usage, options);
1046 if (timeout)
1047 daemon_mode = 1;
1049 setup_path();
1051 dir = argv[0];
1053 if (!enter_repo(dir, strict))
1054 die("'%s' does not appear to be a git repository", dir);
1056 git_config(upload_pack_config, NULL);
1057 upload_pack();
1058 return 0;