11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
18 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
20 /* Remember to update object flag allocation in object.h */
21 #define THEY_HAVE (1u << 11)
22 #define OUR_REF (1u << 12)
23 #define WANTED (1u << 13)
24 #define COMMON_KNOWN (1u << 14)
25 #define REACHABLE (1u << 15)
27 #define SHALLOW (1u << 16)
28 #define NOT_SHALLOW (1u << 17)
29 #define CLIENT_SHALLOW (1u << 18)
30 #define HIDDEN_REF (1u << 19)
32 static unsigned long oldest_have;
36 static int use_thin_pack, use_ofs_delta, use_include_tag;
37 static int no_progress, daemon_mode;
38 static int allow_tip_sha1_in_want;
39 static int shallow_nr;
40 static struct object_array have_obj;
41 static struct object_array want_obj;
42 static struct object_array extra_edge_obj;
43 static unsigned int timeout;
44 static int keepalive = 5;
46 * otherwise maximum packet size (up to 65520 bytes).
48 static int use_sideband;
49 static int advertise_refs;
50 static int stateless_rpc;
52 static void reset_timeout(void)
57 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
60 return send_sideband(1, fd, data, sz, use_sideband);
65 /* XXX: are we happy to lose stuff here? */
69 write_or_die(fd, data, sz);
73 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
76 if (graft->nr_parent == -1)
77 fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
81 static void create_pack_file(void)
83 struct child_process pack_objects = CHILD_PROCESS_INIT;
84 char data[8193], progress[128];
85 char abort_msg[] = "aborting due to possible repository "
86 "corruption on the remote side.";
94 argv[arg++] = "--shallow-file";
97 argv[arg++] = "pack-objects";
98 argv[arg++] = "--revs";
100 argv[arg++] = "--thin";
102 argv[arg++] = "--stdout";
104 argv[arg++] = "--progress";
106 argv[arg++] = "--delta-base-offset";
108 argv[arg++] = "--include-tag";
111 pack_objects.in = -1;
112 pack_objects.out = -1;
113 pack_objects.err = -1;
114 pack_objects.git_cmd = 1;
115 pack_objects.argv = argv;
117 if (start_command(&pack_objects))
118 die("git upload-pack: unable to fork git-pack-objects");
120 pipe_fd = xfdopen(pack_objects.in, "w");
123 for_each_commit_graft(write_one_shallow, pipe_fd);
125 for (i = 0; i < want_obj.nr; i++)
126 fprintf(pipe_fd, "%s\n",
127 sha1_to_hex(want_obj.objects[i].item->sha1));
128 fprintf(pipe_fd, "--not\n");
129 for (i = 0; i < have_obj.nr; i++)
130 fprintf(pipe_fd, "%s\n",
131 sha1_to_hex(have_obj.objects[i].item->sha1));
132 for (i = 0; i < extra_edge_obj.nr; i++)
133 fprintf(pipe_fd, "%s\n",
134 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
135 fprintf(pipe_fd, "\n");
139 /* We read from pack_objects.err to capture stderr output for
140 * progress bar, and pack_objects.out to capture the pack data.
144 struct pollfd pfd[2];
145 int pe, pu, pollsize;
153 if (0 <= pack_objects.out) {
154 pfd[pollsize].fd = pack_objects.out;
155 pfd[pollsize].events = POLLIN;
159 if (0 <= pack_objects.err) {
160 pfd[pollsize].fd = pack_objects.err;
161 pfd[pollsize].events = POLLIN;
169 ret = poll(pfd, pollsize,
170 keepalive < 0 ? -1 : 1000 * keepalive);
173 if (errno != EINTR) {
174 error("poll failed, resuming: %s",
180 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
181 /* Status ready; we ship that in the side-band
182 * or dump to the standard error.
184 sz = xread(pack_objects.err, progress,
187 send_client_data(2, progress, sz);
189 close(pack_objects.err);
190 pack_objects.err = -1;
194 /* give priority to status messages */
197 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
198 /* Data ready; we keep the last byte to ourselves
199 * in case we detect broken rev-list, so that we
200 * can leave the stream corrupted. This is
201 * unfortunate -- unpack-objects would happily
202 * accept a valid packdata with trailing garbage,
203 * so appending garbage after we pass all the
204 * pack data is not good enough to signal
205 * breakage to downstream.
213 sz = xread(pack_objects.out, cp,
214 sizeof(data) - outsz);
218 close(pack_objects.out);
219 pack_objects.out = -1;
225 buffered = data[sz-1] & 0xFF;
230 sz = send_client_data(1, data, sz);
236 * We hit the keepalive timeout without saying anything; send
237 * an empty message on the data sideband just to let the other
238 * side know we're still working on it, but don't have any data
241 * If we don't have a sideband channel, there's no room in the
242 * protocol to say anything, so those clients are just out of
245 if (!ret && use_sideband) {
246 static const char buf[] = "0005\1";
247 write_or_die(1, buf, 5);
251 if (finish_command(&pack_objects)) {
252 error("git upload-pack: git-pack-objects died with error.");
259 sz = send_client_data(1, data, 1);
262 fprintf(stderr, "flushed.\n");
269 send_client_data(3, abort_msg, sizeof(abort_msg));
270 die("git upload-pack: %s", abort_msg);
273 static int got_sha1(char *hex, unsigned char *sha1)
276 int we_knew_they_have = 0;
278 if (get_sha1_hex(hex, sha1))
279 die("git upload-pack: expected SHA1 object, got '%s'", hex);
280 if (!has_sha1_file(sha1))
283 o = parse_object(sha1);
285 die("oops (%s)", sha1_to_hex(sha1));
286 if (o->type == OBJ_COMMIT) {
287 struct commit_list *parents;
288 struct commit *commit = (struct commit *)o;
289 if (o->flags & THEY_HAVE)
290 we_knew_they_have = 1;
292 o->flags |= THEY_HAVE;
293 if (!oldest_have || (commit->date < oldest_have))
294 oldest_have = commit->date;
295 for (parents = commit->parents;
297 parents = parents->next)
298 parents->item->object.flags |= THEY_HAVE;
300 if (!we_knew_they_have) {
301 add_object_array(o, NULL, &have_obj);
307 static int reachable(struct commit *want)
309 struct commit_list *work = NULL;
311 commit_list_insert_by_date(want, &work);
313 struct commit_list *list = work->next;
314 struct commit *commit = work->item;
318 if (commit->object.flags & THEY_HAVE) {
319 want->object.flags |= COMMON_KNOWN;
322 if (!commit->object.parsed)
323 parse_object(commit->object.sha1);
324 if (commit->object.flags & REACHABLE)
326 commit->object.flags |= REACHABLE;
327 if (commit->date < oldest_have)
329 for (list = commit->parents; list; list = list->next) {
330 struct commit *parent = list->item;
331 if (!(parent->object.flags & REACHABLE))
332 commit_list_insert_by_date(parent, &work);
335 want->object.flags |= REACHABLE;
336 clear_commit_marks(want, REACHABLE);
337 free_commit_list(work);
338 return (want->object.flags & COMMON_KNOWN);
341 static int ok_to_give_up(void)
348 for (i = 0; i < want_obj.nr; i++) {
349 struct object *want = want_obj.objects[i].item;
351 if (want->flags & COMMON_KNOWN)
353 want = deref_tag(want, "a want line", 0);
354 if (!want || want->type != OBJ_COMMIT) {
355 /* no way to tell if this is reachable by
356 * looking at the ancestry chain alone, so
357 * leave a note to ourselves not to worry about
358 * this object anymore.
360 want_obj.objects[i].item->flags |= COMMON_KNOWN;
363 if (!reachable((struct commit *)want))
369 static int get_common_commits(void)
371 unsigned char sha1[20];
377 save_commit_buffer = 0;
380 char *line = packet_read_line(0, NULL);
384 if (multi_ack == 2 && got_common
385 && !got_other && ok_to_give_up()) {
387 packet_write(1, "ACK %s ready\n", last_hex);
389 if (have_obj.nr == 0 || multi_ack)
390 packet_write(1, "NAK\n");
392 if (no_done && sent_ready) {
393 packet_write(1, "ACK %s\n", last_hex);
402 if (starts_with(line, "have ")) {
403 switch (got_sha1(line+5, sha1)) {
404 case -1: /* they have what we do not */
406 if (multi_ack && ok_to_give_up()) {
407 const char *hex = sha1_to_hex(sha1);
408 if (multi_ack == 2) {
410 packet_write(1, "ACK %s ready\n", hex);
412 packet_write(1, "ACK %s continue\n", hex);
417 memcpy(last_hex, sha1_to_hex(sha1), 41);
419 packet_write(1, "ACK %s common\n", last_hex);
421 packet_write(1, "ACK %s continue\n", last_hex);
422 else if (have_obj.nr == 1)
423 packet_write(1, "ACK %s\n", last_hex);
428 if (!strcmp(line, "done")) {
429 if (have_obj.nr > 0) {
431 packet_write(1, "ACK %s\n", last_hex);
434 packet_write(1, "NAK\n");
437 die("git upload-pack: expected SHA1 list, got '%s'", line);
441 static int is_our_ref(struct object *o)
444 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
447 static void check_non_tip(void)
449 static const char *argv[] = {
450 "rev-list", "--stdin", NULL,
452 static struct child_process cmd = CHILD_PROCESS_INIT;
454 char namebuf[42]; /* ^ + SHA-1 + LF */
457 /* In the normal in-process case non-tip request can never happen */
467 if (start_command(&cmd))
471 * If rev-list --stdin encounters an unknown commit, it
472 * terminates, which will cause SIGPIPE in the write loop
475 sigchain_push(SIGPIPE, SIG_IGN);
479 for (i = get_max_object_index(); 0 < i; ) {
480 o = get_indexed_object(--i);
485 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
486 if (write_in_full(cmd.in, namebuf, 42) < 0)
490 for (i = 0; i < want_obj.nr; i++) {
491 o = want_obj.objects[i].item;
494 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
495 if (write_in_full(cmd.in, namebuf, 41) < 0)
500 sigchain_pop(SIGPIPE);
503 * The commits out of the rev-list are not ancestors of
506 i = read_in_full(cmd.out, namebuf, 1);
512 * rev-list may have died by encountering a bad commit
513 * in the history, in which case we do want to bail out
514 * even when it showed no commit.
516 if (finish_command(&cmd))
519 /* All the non-tip ones are ancestors of what we advertised */
523 /* Pick one of them (we know there at least is one) */
524 for (i = 0; i < want_obj.nr; i++) {
525 o = want_obj.objects[i].item;
527 die("git upload-pack: not our ref %s",
528 sha1_to_hex(o->sha1));
532 static void receive_needs(void)
534 struct object_array shallows = OBJECT_ARRAY_INIT;
541 const char *features;
542 unsigned char sha1_buf[20];
543 char *line = packet_read_line(0, NULL);
548 if (starts_with(line, "shallow ")) {
549 unsigned char sha1[20];
550 struct object *object;
551 if (get_sha1_hex(line + 8, sha1))
552 die("invalid shallow line: %s", line);
553 object = parse_object(sha1);
556 if (object->type != OBJ_COMMIT)
557 die("invalid shallow object %s", sha1_to_hex(sha1));
558 if (!(object->flags & CLIENT_SHALLOW)) {
559 object->flags |= CLIENT_SHALLOW;
560 add_object_array(object, NULL, &shallows);
564 if (starts_with(line, "deepen ")) {
566 depth = strtol(line + 7, &end, 0);
567 if (end == line + 7 || depth <= 0)
568 die("Invalid deepen: %s", line);
571 if (!starts_with(line, "want ") ||
572 get_sha1_hex(line+5, sha1_buf))
573 die("git upload-pack: protocol error, "
574 "expected to get sha, not '%s'", line);
576 features = line + 45;
578 if (parse_feature_request(features, "multi_ack_detailed"))
580 else if (parse_feature_request(features, "multi_ack"))
582 if (parse_feature_request(features, "no-done"))
584 if (parse_feature_request(features, "thin-pack"))
586 if (parse_feature_request(features, "ofs-delta"))
588 if (parse_feature_request(features, "side-band-64k"))
589 use_sideband = LARGE_PACKET_MAX;
590 else if (parse_feature_request(features, "side-band"))
591 use_sideband = DEFAULT_PACKET_MAX;
592 if (parse_feature_request(features, "no-progress"))
594 if (parse_feature_request(features, "include-tag"))
597 o = parse_object(sha1_buf);
599 die("git upload-pack: not our ref %s",
600 sha1_to_hex(sha1_buf));
601 if (!(o->flags & WANTED)) {
605 add_object_array(o, NULL, &want_obj);
610 * We have sent all our refs already, and the other end
611 * should have chosen out of them. When we are operating
612 * in the stateless RPC mode, however, their choice may
613 * have been based on the set of older refs advertised
614 * by another process that handled the initial request.
619 if (!use_sideband && daemon_mode)
622 if (depth == 0 && shallows.nr == 0)
625 struct commit_list *result = NULL, *backup = NULL;
627 if (depth == INFINITE_DEPTH && !is_repository_shallow())
628 for (i = 0; i < shallows.nr; i++) {
629 struct object *object = shallows.objects[i].item;
630 object->flags |= NOT_SHALLOW;
634 get_shallow_commits(&want_obj, depth,
635 SHALLOW, NOT_SHALLOW);
637 struct object *object = &result->item->object;
638 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
639 packet_write(1, "shallow %s",
640 sha1_to_hex(object->sha1));
641 register_shallow(object->sha1);
644 result = result->next;
646 free_commit_list(backup);
647 for (i = 0; i < shallows.nr; i++) {
648 struct object *object = shallows.objects[i].item;
649 if (object->flags & NOT_SHALLOW) {
650 struct commit_list *parents;
651 packet_write(1, "unshallow %s",
652 sha1_to_hex(object->sha1));
653 object->flags &= ~CLIENT_SHALLOW;
654 /* make sure the real parents are parsed */
655 unregister_shallow(object->sha1);
657 parse_commit_or_die((struct commit *)object);
658 parents = ((struct commit *)object)->parents;
660 add_object_array(&parents->item->object,
662 parents = parents->next;
664 add_object_array(object, NULL, &extra_edge_obj);
666 /* make sure commit traversal conforms to client */
667 register_shallow(object->sha1);
671 if (shallows.nr > 0) {
673 for (i = 0; i < shallows.nr; i++)
674 register_shallow(shallows.objects[i].item->sha1);
677 shallow_nr += shallows.nr;
678 free(shallows.objects);
681 /* return non-zero if the ref is hidden, otherwise 0 */
682 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
684 struct object *o = lookup_unknown_object(sha1);
686 if (ref_is_hidden(refname)) {
687 o->flags |= HIDDEN_REF;
691 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
696 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
698 struct string_list_item *item;
702 for_each_string_list_item(item, symref)
703 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
706 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
708 static const char *capabilities = "multi_ack thin-pack side-band"
709 " side-band-64k ofs-delta shallow no-progress"
710 " include-tag multi_ack_detailed";
711 const char *refname_nons = strip_namespace(refname);
712 unsigned char peeled[20];
714 if (mark_our_ref(refname, sha1, flag, NULL))
718 struct strbuf symref_info = STRBUF_INIT;
720 format_symref_info(&symref_info, cb_data);
721 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
722 sha1_to_hex(sha1), refname_nons,
724 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
725 stateless_rpc ? " no-done" : "",
727 git_user_agent_sanitized());
728 strbuf_release(&symref_info);
730 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
733 if (!peel_ref(refname, peeled))
734 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
738 static int find_symref(const char *refname, const unsigned char *sha1, int flag,
741 const char *symref_target;
742 struct string_list_item *item;
743 unsigned char unused[20];
745 if ((flag & REF_ISSYMREF) == 0)
747 symref_target = resolve_ref_unsafe(refname, unused, 0, &flag);
748 if (!symref_target || (flag & REF_ISSYMREF) == 0)
749 die("'%s' is a symref but it is not?", refname);
750 item = string_list_append(cb_data, refname);
751 item->util = xstrdup(symref_target);
755 static void upload_pack(void)
757 struct string_list symref = STRING_LIST_INIT_DUP;
759 head_ref_namespaced(find_symref, &symref);
761 if (advertise_refs || !stateless_rpc) {
763 head_ref_namespaced(send_ref, &symref);
764 for_each_namespaced_ref(send_ref, &symref);
765 advertise_shallow_grafts(1);
768 head_ref_namespaced(mark_our_ref, NULL);
769 for_each_namespaced_ref(mark_our_ref, NULL);
771 string_list_clear(&symref, 1);
777 get_common_commits();
782 static int upload_pack_config(const char *var, const char *value, void *unused)
784 if (!strcmp("uploadpack.allowtipsha1inwant", var))
785 allow_tip_sha1_in_want = git_config_bool(var, value);
786 else if (!strcmp("uploadpack.keepalive", var)) {
787 keepalive = git_config_int(var, value);
791 return parse_hide_refs_config(var, value, "uploadpack");
794 int main(int argc, char **argv)
802 packet_trace_identity("upload-pack");
803 git_extract_argv0_path(argv[0]);
804 check_replace_refs = 0;
806 for (i = 1; i < argc; i++) {
811 if (!strcmp(arg, "--advertise-refs")) {
815 if (!strcmp(arg, "--stateless-rpc")) {
819 if (!strcmp(arg, "--strict")) {
823 if (starts_with(arg, "--timeout=")) {
824 timeout = atoi(arg+10);
828 if (!strcmp(arg, "--")) {
835 usage(upload_pack_usage);
841 if (!enter_repo(dir, strict))
842 die("'%s' does not appear to be a git repository", dir);
844 git_config(upload_pack_config, NULL);