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 /* Allow specifying sha1 if it is a ref tip. */
39 #define ALLOW_TIP_SHA1 01
40 static unsigned int allow_unadvertised_object_request;
41 static int shallow_nr;
42 static struct object_array have_obj;
43 static struct object_array want_obj;
44 static struct object_array extra_edge_obj;
45 static unsigned int timeout;
46 static int keepalive = 5;
48 * otherwise maximum packet size (up to 65520 bytes).
50 static int use_sideband;
51 static int advertise_refs;
52 static int stateless_rpc;
54 static void reset_timeout(void)
59 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
62 return send_sideband(1, fd, data, sz, use_sideband);
67 /* XXX: are we happy to lose stuff here? */
71 write_or_die(fd, data, sz);
75 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
78 if (graft->nr_parent == -1)
79 fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
83 static void create_pack_file(void)
85 struct child_process pack_objects = CHILD_PROCESS_INIT;
86 char data[8193], progress[128];
87 char abort_msg[] = "aborting due to possible repository "
88 "corruption on the remote side.";
96 argv[arg++] = "--shallow-file";
99 argv[arg++] = "pack-objects";
100 argv[arg++] = "--revs";
102 argv[arg++] = "--thin";
104 argv[arg++] = "--stdout";
106 argv[arg++] = "--shallow";
108 argv[arg++] = "--progress";
110 argv[arg++] = "--delta-base-offset";
112 argv[arg++] = "--include-tag";
115 pack_objects.in = -1;
116 pack_objects.out = -1;
117 pack_objects.err = -1;
118 pack_objects.git_cmd = 1;
119 pack_objects.argv = argv;
121 if (start_command(&pack_objects))
122 die("git upload-pack: unable to fork git-pack-objects");
124 pipe_fd = xfdopen(pack_objects.in, "w");
127 for_each_commit_graft(write_one_shallow, pipe_fd);
129 for (i = 0; i < want_obj.nr; i++)
130 fprintf(pipe_fd, "%s\n",
131 sha1_to_hex(want_obj.objects[i].item->sha1));
132 fprintf(pipe_fd, "--not\n");
133 for (i = 0; i < have_obj.nr; i++)
134 fprintf(pipe_fd, "%s\n",
135 sha1_to_hex(have_obj.objects[i].item->sha1));
136 for (i = 0; i < extra_edge_obj.nr; i++)
137 fprintf(pipe_fd, "%s\n",
138 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
139 fprintf(pipe_fd, "\n");
143 /* We read from pack_objects.err to capture stderr output for
144 * progress bar, and pack_objects.out to capture the pack data.
148 struct pollfd pfd[2];
149 int pe, pu, pollsize;
157 if (0 <= pack_objects.out) {
158 pfd[pollsize].fd = pack_objects.out;
159 pfd[pollsize].events = POLLIN;
163 if (0 <= pack_objects.err) {
164 pfd[pollsize].fd = pack_objects.err;
165 pfd[pollsize].events = POLLIN;
173 ret = poll(pfd, pollsize,
174 keepalive < 0 ? -1 : 1000 * keepalive);
177 if (errno != EINTR) {
178 error("poll failed, resuming: %s",
184 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
185 /* Status ready; we ship that in the side-band
186 * or dump to the standard error.
188 sz = xread(pack_objects.err, progress,
191 send_client_data(2, progress, sz);
193 close(pack_objects.err);
194 pack_objects.err = -1;
198 /* give priority to status messages */
201 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
202 /* Data ready; we keep the last byte to ourselves
203 * in case we detect broken rev-list, so that we
204 * can leave the stream corrupted. This is
205 * unfortunate -- unpack-objects would happily
206 * accept a valid packdata with trailing garbage,
207 * so appending garbage after we pass all the
208 * pack data is not good enough to signal
209 * breakage to downstream.
217 sz = xread(pack_objects.out, cp,
218 sizeof(data) - outsz);
222 close(pack_objects.out);
223 pack_objects.out = -1;
229 buffered = data[sz-1] & 0xFF;
234 sz = send_client_data(1, data, sz);
240 * We hit the keepalive timeout without saying anything; send
241 * an empty message on the data sideband just to let the other
242 * side know we're still working on it, but don't have any data
245 * If we don't have a sideband channel, there's no room in the
246 * protocol to say anything, so those clients are just out of
249 if (!ret && use_sideband) {
250 static const char buf[] = "0005\1";
251 write_or_die(1, buf, 5);
255 if (finish_command(&pack_objects)) {
256 error("git upload-pack: git-pack-objects died with error.");
263 sz = send_client_data(1, data, 1);
266 fprintf(stderr, "flushed.\n");
273 send_client_data(3, abort_msg, sizeof(abort_msg));
274 die("git upload-pack: %s", abort_msg);
277 static int got_sha1(char *hex, unsigned char *sha1)
280 int we_knew_they_have = 0;
282 if (get_sha1_hex(hex, sha1))
283 die("git upload-pack: expected SHA1 object, got '%s'", hex);
284 if (!has_sha1_file(sha1))
287 o = parse_object(sha1);
289 die("oops (%s)", sha1_to_hex(sha1));
290 if (o->type == OBJ_COMMIT) {
291 struct commit_list *parents;
292 struct commit *commit = (struct commit *)o;
293 if (o->flags & THEY_HAVE)
294 we_knew_they_have = 1;
296 o->flags |= THEY_HAVE;
297 if (!oldest_have || (commit->date < oldest_have))
298 oldest_have = commit->date;
299 for (parents = commit->parents;
301 parents = parents->next)
302 parents->item->object.flags |= THEY_HAVE;
304 if (!we_knew_they_have) {
305 add_object_array(o, NULL, &have_obj);
311 static int reachable(struct commit *want)
313 struct commit_list *work = NULL;
315 commit_list_insert_by_date(want, &work);
317 struct commit_list *list = work->next;
318 struct commit *commit = work->item;
322 if (commit->object.flags & THEY_HAVE) {
323 want->object.flags |= COMMON_KNOWN;
326 if (!commit->object.parsed)
327 parse_object(commit->object.sha1);
328 if (commit->object.flags & REACHABLE)
330 commit->object.flags |= REACHABLE;
331 if (commit->date < oldest_have)
333 for (list = commit->parents; list; list = list->next) {
334 struct commit *parent = list->item;
335 if (!(parent->object.flags & REACHABLE))
336 commit_list_insert_by_date(parent, &work);
339 want->object.flags |= REACHABLE;
340 clear_commit_marks(want, REACHABLE);
341 free_commit_list(work);
342 return (want->object.flags & COMMON_KNOWN);
345 static int ok_to_give_up(void)
352 for (i = 0; i < want_obj.nr; i++) {
353 struct object *want = want_obj.objects[i].item;
355 if (want->flags & COMMON_KNOWN)
357 want = deref_tag(want, "a want line", 0);
358 if (!want || want->type != OBJ_COMMIT) {
359 /* no way to tell if this is reachable by
360 * looking at the ancestry chain alone, so
361 * leave a note to ourselves not to worry about
362 * this object anymore.
364 want_obj.objects[i].item->flags |= COMMON_KNOWN;
367 if (!reachable((struct commit *)want))
373 static int get_common_commits(void)
375 unsigned char sha1[20];
381 save_commit_buffer = 0;
384 char *line = packet_read_line(0, NULL);
388 if (multi_ack == 2 && got_common
389 && !got_other && ok_to_give_up()) {
391 packet_write(1, "ACK %s ready\n", last_hex);
393 if (have_obj.nr == 0 || multi_ack)
394 packet_write(1, "NAK\n");
396 if (no_done && sent_ready) {
397 packet_write(1, "ACK %s\n", last_hex);
406 if (starts_with(line, "have ")) {
407 switch (got_sha1(line+5, sha1)) {
408 case -1: /* they have what we do not */
410 if (multi_ack && ok_to_give_up()) {
411 const char *hex = sha1_to_hex(sha1);
412 if (multi_ack == 2) {
414 packet_write(1, "ACK %s ready\n", hex);
416 packet_write(1, "ACK %s continue\n", hex);
421 memcpy(last_hex, sha1_to_hex(sha1), 41);
423 packet_write(1, "ACK %s common\n", last_hex);
425 packet_write(1, "ACK %s continue\n", last_hex);
426 else if (have_obj.nr == 1)
427 packet_write(1, "ACK %s\n", last_hex);
432 if (!strcmp(line, "done")) {
433 if (have_obj.nr > 0) {
435 packet_write(1, "ACK %s\n", last_hex);
438 packet_write(1, "NAK\n");
441 die("git upload-pack: expected SHA1 list, got '%s'", line);
445 static int is_our_ref(struct object *o)
447 int allow_hidden_ref = (allow_unadvertised_object_request & ALLOW_TIP_SHA1);
448 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
451 static void check_non_tip(void)
453 static const char *argv[] = {
454 "rev-list", "--stdin", NULL,
456 static struct child_process cmd = CHILD_PROCESS_INIT;
458 char namebuf[42]; /* ^ + SHA-1 + LF */
461 /* In the normal in-process case non-tip request can never happen */
471 if (start_command(&cmd))
475 * If rev-list --stdin encounters an unknown commit, it
476 * terminates, which will cause SIGPIPE in the write loop
479 sigchain_push(SIGPIPE, SIG_IGN);
483 for (i = get_max_object_index(); 0 < i; ) {
484 o = get_indexed_object(--i);
489 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
490 if (write_in_full(cmd.in, namebuf, 42) < 0)
494 for (i = 0; i < want_obj.nr; i++) {
495 o = want_obj.objects[i].item;
498 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
499 if (write_in_full(cmd.in, namebuf, 41) < 0)
504 sigchain_pop(SIGPIPE);
507 * The commits out of the rev-list are not ancestors of
510 i = read_in_full(cmd.out, namebuf, 1);
516 * rev-list may have died by encountering a bad commit
517 * in the history, in which case we do want to bail out
518 * even when it showed no commit.
520 if (finish_command(&cmd))
523 /* All the non-tip ones are ancestors of what we advertised */
527 /* Pick one of them (we know there at least is one) */
528 for (i = 0; i < want_obj.nr; i++) {
529 o = want_obj.objects[i].item;
531 die("git upload-pack: not our ref %s",
532 sha1_to_hex(o->sha1));
536 static void receive_needs(void)
538 struct object_array shallows = OBJECT_ARRAY_INIT;
545 const char *features;
546 unsigned char sha1_buf[20];
547 char *line = packet_read_line(0, NULL);
552 if (starts_with(line, "shallow ")) {
553 unsigned char sha1[20];
554 struct object *object;
555 if (get_sha1_hex(line + 8, sha1))
556 die("invalid shallow line: %s", line);
557 object = parse_object(sha1);
560 if (object->type != OBJ_COMMIT)
561 die("invalid shallow object %s", sha1_to_hex(sha1));
562 if (!(object->flags & CLIENT_SHALLOW)) {
563 object->flags |= CLIENT_SHALLOW;
564 add_object_array(object, NULL, &shallows);
568 if (starts_with(line, "deepen ")) {
570 depth = strtol(line + 7, &end, 0);
571 if (end == line + 7 || depth <= 0)
572 die("Invalid deepen: %s", line);
575 if (!starts_with(line, "want ") ||
576 get_sha1_hex(line+5, sha1_buf))
577 die("git upload-pack: protocol error, "
578 "expected to get sha, not '%s'", line);
580 features = line + 45;
582 if (parse_feature_request(features, "multi_ack_detailed"))
584 else if (parse_feature_request(features, "multi_ack"))
586 if (parse_feature_request(features, "no-done"))
588 if (parse_feature_request(features, "thin-pack"))
590 if (parse_feature_request(features, "ofs-delta"))
592 if (parse_feature_request(features, "side-band-64k"))
593 use_sideband = LARGE_PACKET_MAX;
594 else if (parse_feature_request(features, "side-band"))
595 use_sideband = DEFAULT_PACKET_MAX;
596 if (parse_feature_request(features, "no-progress"))
598 if (parse_feature_request(features, "include-tag"))
601 o = parse_object(sha1_buf);
603 die("git upload-pack: not our ref %s",
604 sha1_to_hex(sha1_buf));
605 if (!(o->flags & WANTED)) {
609 add_object_array(o, NULL, &want_obj);
614 * We have sent all our refs already, and the other end
615 * should have chosen out of them. When we are operating
616 * in the stateless RPC mode, however, their choice may
617 * have been based on the set of older refs advertised
618 * by another process that handled the initial request.
623 if (!use_sideband && daemon_mode)
626 if (depth == 0 && shallows.nr == 0)
629 struct commit_list *result = NULL, *backup = NULL;
631 if (depth == INFINITE_DEPTH && !is_repository_shallow())
632 for (i = 0; i < shallows.nr; i++) {
633 struct object *object = shallows.objects[i].item;
634 object->flags |= NOT_SHALLOW;
638 get_shallow_commits(&want_obj, depth,
639 SHALLOW, NOT_SHALLOW);
641 struct object *object = &result->item->object;
642 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
643 packet_write(1, "shallow %s",
644 sha1_to_hex(object->sha1));
645 register_shallow(object->sha1);
648 result = result->next;
650 free_commit_list(backup);
651 for (i = 0; i < shallows.nr; i++) {
652 struct object *object = shallows.objects[i].item;
653 if (object->flags & NOT_SHALLOW) {
654 struct commit_list *parents;
655 packet_write(1, "unshallow %s",
656 sha1_to_hex(object->sha1));
657 object->flags &= ~CLIENT_SHALLOW;
658 /* make sure the real parents are parsed */
659 unregister_shallow(object->sha1);
661 parse_commit_or_die((struct commit *)object);
662 parents = ((struct commit *)object)->parents;
664 add_object_array(&parents->item->object,
666 parents = parents->next;
668 add_object_array(object, NULL, &extra_edge_obj);
670 /* make sure commit traversal conforms to client */
671 register_shallow(object->sha1);
675 if (shallows.nr > 0) {
677 for (i = 0; i < shallows.nr; i++)
678 register_shallow(shallows.objects[i].item->sha1);
681 shallow_nr += shallows.nr;
682 free(shallows.objects);
685 /* return non-zero if the ref is hidden, otherwise 0 */
686 static int mark_our_ref(const char *refname, const unsigned char *sha1)
688 struct object *o = lookup_unknown_object(sha1);
690 if (ref_is_hidden(refname)) {
691 o->flags |= HIDDEN_REF;
698 static int check_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
700 mark_our_ref(refname, sha1);
704 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
706 struct string_list_item *item;
710 for_each_string_list_item(item, symref)
711 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
714 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
716 static const char *capabilities = "multi_ack thin-pack side-band"
717 " side-band-64k ofs-delta shallow no-progress"
718 " include-tag multi_ack_detailed";
719 const char *refname_nons = strip_namespace(refname);
720 unsigned char peeled[20];
722 if (mark_our_ref(refname, sha1))
726 struct strbuf symref_info = STRBUF_INIT;
728 format_symref_info(&symref_info, cb_data);
729 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
730 sha1_to_hex(sha1), refname_nons,
732 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
733 " allow-tip-sha1-in-want" : "",
734 stateless_rpc ? " no-done" : "",
736 git_user_agent_sanitized());
737 strbuf_release(&symref_info);
739 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
742 if (!peel_ref(refname, peeled))
743 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
747 static int find_symref(const char *refname, const unsigned char *sha1, int flag,
750 const char *symref_target;
751 struct string_list_item *item;
752 unsigned char unused[20];
754 if ((flag & REF_ISSYMREF) == 0)
756 symref_target = resolve_ref_unsafe(refname, 0, unused, &flag);
757 if (!symref_target || (flag & REF_ISSYMREF) == 0)
758 die("'%s' is a symref but it is not?", refname);
759 item = string_list_append(cb_data, refname);
760 item->util = xstrdup(symref_target);
764 static void upload_pack(void)
766 struct string_list symref = STRING_LIST_INIT_DUP;
768 head_ref_namespaced(find_symref, &symref);
770 if (advertise_refs || !stateless_rpc) {
772 head_ref_namespaced(send_ref, &symref);
773 for_each_namespaced_ref(send_ref, &symref);
774 advertise_shallow_grafts(1);
777 head_ref_namespaced(check_ref, NULL);
778 for_each_namespaced_ref(check_ref, NULL);
780 string_list_clear(&symref, 1);
786 get_common_commits();
791 static int upload_pack_config(const char *var, const char *value, void *unused)
793 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
794 if (git_config_bool(var, value))
795 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
797 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
798 } else if (!strcmp("uploadpack.keepalive", var)) {
799 keepalive = git_config_int(var, value);
803 return parse_hide_refs_config(var, value, "uploadpack");
806 int main(int argc, char **argv)
814 packet_trace_identity("upload-pack");
815 git_extract_argv0_path(argv[0]);
816 check_replace_refs = 0;
818 for (i = 1; i < argc; i++) {
823 if (!strcmp(arg, "--advertise-refs")) {
827 if (!strcmp(arg, "--stateless-rpc")) {
831 if (!strcmp(arg, "--strict")) {
835 if (starts_with(arg, "--timeout=")) {
836 timeout = atoi(arg+10);
840 if (!strcmp(arg, "--")) {
847 usage(upload_pack_usage);
853 if (!enter_repo(dir, strict))
854 die("'%s' does not appear to be a git repository", dir);
856 git_config(upload_pack_config, NULL);