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 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
41 #define ALLOW_REACHABLE_SHA1 02
42 static unsigned int allow_unadvertised_object_request;
43 static int shallow_nr;
44 static struct object_array have_obj;
45 static struct object_array want_obj;
46 static struct object_array extra_edge_obj;
47 static unsigned int timeout;
48 static int keepalive = 5;
50 * otherwise maximum packet size (up to 65520 bytes).
52 static int use_sideband;
53 static int advertise_refs;
54 static int stateless_rpc;
56 static void reset_timeout(void)
61 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
64 return send_sideband(1, fd, data, sz, use_sideband);
69 /* XXX: are we happy to lose stuff here? */
73 write_or_die(fd, data, sz);
77 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
80 if (graft->nr_parent == -1)
81 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
85 static void create_pack_file(void)
87 struct child_process pack_objects = CHILD_PROCESS_INIT;
88 char data[8193], progress[128];
89 char abort_msg[] = "aborting due to possible repository "
90 "corruption on the remote side.";
97 argv_array_push(&pack_objects.args, "--shallow-file");
98 argv_array_push(&pack_objects.args, "");
100 argv_array_push(&pack_objects.args, "pack-objects");
101 argv_array_push(&pack_objects.args, "--revs");
103 argv_array_push(&pack_objects.args, "--thin");
105 argv_array_push(&pack_objects.args, "--stdout");
107 argv_array_push(&pack_objects.args, "--shallow");
109 argv_array_push(&pack_objects.args, "--progress");
111 argv_array_push(&pack_objects.args, "--delta-base-offset");
113 argv_array_push(&pack_objects.args, "--include-tag");
115 pack_objects.in = -1;
116 pack_objects.out = -1;
117 pack_objects.err = -1;
118 pack_objects.git_cmd = 1;
120 if (start_command(&pack_objects))
121 die("git upload-pack: unable to fork git-pack-objects");
123 pipe_fd = xfdopen(pack_objects.in, "w");
126 for_each_commit_graft(write_one_shallow, pipe_fd);
128 for (i = 0; i < want_obj.nr; i++)
129 fprintf(pipe_fd, "%s\n",
130 oid_to_hex(&want_obj.objects[i].item->oid));
131 fprintf(pipe_fd, "--not\n");
132 for (i = 0; i < have_obj.nr; i++)
133 fprintf(pipe_fd, "%s\n",
134 oid_to_hex(&have_obj.objects[i].item->oid));
135 for (i = 0; i < extra_edge_obj.nr; i++)
136 fprintf(pipe_fd, "%s\n",
137 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
138 fprintf(pipe_fd, "\n");
142 /* We read from pack_objects.err to capture stderr output for
143 * progress bar, and pack_objects.out to capture the pack data.
147 struct pollfd pfd[2];
148 int pe, pu, pollsize;
156 if (0 <= pack_objects.out) {
157 pfd[pollsize].fd = pack_objects.out;
158 pfd[pollsize].events = POLLIN;
162 if (0 <= pack_objects.err) {
163 pfd[pollsize].fd = pack_objects.err;
164 pfd[pollsize].events = POLLIN;
172 ret = poll(pfd, pollsize,
173 keepalive < 0 ? -1 : 1000 * keepalive);
176 if (errno != EINTR) {
177 error_errno("poll failed, resuming");
182 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
183 /* Status ready; we ship that in the side-band
184 * or dump to the standard error.
186 sz = xread(pack_objects.err, progress,
189 send_client_data(2, progress, sz);
191 close(pack_objects.err);
192 pack_objects.err = -1;
196 /* give priority to status messages */
199 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
200 /* Data ready; we keep the last byte to ourselves
201 * in case we detect broken rev-list, so that we
202 * can leave the stream corrupted. This is
203 * unfortunate -- unpack-objects would happily
204 * accept a valid packdata with trailing garbage,
205 * so appending garbage after we pass all the
206 * pack data is not good enough to signal
207 * breakage to downstream.
215 sz = xread(pack_objects.out, cp,
216 sizeof(data) - outsz);
220 close(pack_objects.out);
221 pack_objects.out = -1;
227 buffered = data[sz-1] & 0xFF;
232 sz = send_client_data(1, data, sz);
238 * We hit the keepalive timeout without saying anything; send
239 * an empty message on the data sideband just to let the other
240 * side know we're still working on it, but don't have any data
243 * If we don't have a sideband channel, there's no room in the
244 * protocol to say anything, so those clients are just out of
247 if (!ret && use_sideband) {
248 static const char buf[] = "0005\1";
249 write_or_die(1, buf, 5);
253 if (finish_command(&pack_objects)) {
254 error("git upload-pack: git-pack-objects died with error.");
261 sz = send_client_data(1, data, 1);
264 fprintf(stderr, "flushed.\n");
271 send_client_data(3, abort_msg, sizeof(abort_msg));
272 die("git upload-pack: %s", abort_msg);
275 static int got_sha1(char *hex, unsigned char *sha1)
278 int we_knew_they_have = 0;
280 if (get_sha1_hex(hex, sha1))
281 die("git upload-pack: expected SHA1 object, got '%s'", hex);
282 if (!has_sha1_file(sha1))
285 o = parse_object(sha1);
287 die("oops (%s)", sha1_to_hex(sha1));
288 if (o->type == OBJ_COMMIT) {
289 struct commit_list *parents;
290 struct commit *commit = (struct commit *)o;
291 if (o->flags & THEY_HAVE)
292 we_knew_they_have = 1;
294 o->flags |= THEY_HAVE;
295 if (!oldest_have || (commit->date < oldest_have))
296 oldest_have = commit->date;
297 for (parents = commit->parents;
299 parents = parents->next)
300 parents->item->object.flags |= THEY_HAVE;
302 if (!we_knew_they_have) {
303 add_object_array(o, NULL, &have_obj);
309 static int reachable(struct commit *want)
311 struct commit_list *work = NULL;
313 commit_list_insert_by_date(want, &work);
315 struct commit_list *list;
316 struct commit *commit = pop_commit(&work);
318 if (commit->object.flags & THEY_HAVE) {
319 want->object.flags |= COMMON_KNOWN;
322 if (!commit->object.parsed)
323 parse_object(commit->object.oid.hash);
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)
443 int allow_hidden_ref = (allow_unadvertised_object_request &
444 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
445 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
448 static void check_non_tip(void)
450 static const char *argv[] = {
451 "rev-list", "--stdin", NULL,
453 static struct child_process cmd = CHILD_PROCESS_INIT;
455 char namebuf[42]; /* ^ + SHA-1 + LF */
459 * In the normal in-process case without
460 * uploadpack.allowReachableSHA1InWant,
461 * non-tip requests can never happen.
463 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
472 if (start_command(&cmd))
476 * If rev-list --stdin encounters an unknown commit, it
477 * terminates, which will cause SIGPIPE in the write loop
480 sigchain_push(SIGPIPE, SIG_IGN);
484 for (i = get_max_object_index(); 0 < i; ) {
485 o = get_indexed_object(--i);
490 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
491 if (write_in_full(cmd.in, namebuf, 42) < 0)
495 for (i = 0; i < want_obj.nr; i++) {
496 o = want_obj.objects[i].item;
499 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
500 if (write_in_full(cmd.in, namebuf, 41) < 0)
505 sigchain_pop(SIGPIPE);
508 * The commits out of the rev-list are not ancestors of
511 i = read_in_full(cmd.out, namebuf, 1);
517 * rev-list may have died by encountering a bad commit
518 * in the history, in which case we do want to bail out
519 * even when it showed no commit.
521 if (finish_command(&cmd))
524 /* All the non-tip ones are ancestors of what we advertised */
528 /* Pick one of them (we know there at least is one) */
529 for (i = 0; i < want_obj.nr; i++) {
530 o = want_obj.objects[i].item;
532 die("git upload-pack: not our ref %s",
533 oid_to_hex(&o->oid));
537 static void receive_needs(void)
539 struct object_array shallows = OBJECT_ARRAY_INIT;
546 const char *features;
547 unsigned char sha1_buf[20];
548 char *line = packet_read_line(0, NULL);
553 if (starts_with(line, "shallow ")) {
554 unsigned char sha1[20];
555 struct object *object;
556 if (get_sha1_hex(line + 8, sha1))
557 die("invalid shallow line: %s", line);
558 object = parse_object(sha1);
561 if (object->type != OBJ_COMMIT)
562 die("invalid shallow object %s", sha1_to_hex(sha1));
563 if (!(object->flags & CLIENT_SHALLOW)) {
564 object->flags |= CLIENT_SHALLOW;
565 add_object_array(object, NULL, &shallows);
569 if (starts_with(line, "deepen ")) {
571 depth = strtol(line + 7, &end, 0);
572 if (end == line + 7 || depth <= 0)
573 die("Invalid deepen: %s", line);
576 if (!starts_with(line, "want ") ||
577 get_sha1_hex(line+5, sha1_buf))
578 die("git upload-pack: protocol error, "
579 "expected to get sha, not '%s'", line);
581 features = line + 45;
583 if (parse_feature_request(features, "multi_ack_detailed"))
585 else if (parse_feature_request(features, "multi_ack"))
587 if (parse_feature_request(features, "no-done"))
589 if (parse_feature_request(features, "thin-pack"))
591 if (parse_feature_request(features, "ofs-delta"))
593 if (parse_feature_request(features, "side-band-64k"))
594 use_sideband = LARGE_PACKET_MAX;
595 else if (parse_feature_request(features, "side-band"))
596 use_sideband = DEFAULT_PACKET_MAX;
597 if (parse_feature_request(features, "no-progress"))
599 if (parse_feature_request(features, "include-tag"))
602 o = parse_object(sha1_buf);
604 die("git upload-pack: not our ref %s",
605 sha1_to_hex(sha1_buf));
606 if (!(o->flags & WANTED)) {
610 add_object_array(o, NULL, &want_obj);
615 * We have sent all our refs already, and the other end
616 * should have chosen out of them. When we are operating
617 * in the stateless RPC mode, however, their choice may
618 * have been based on the set of older refs advertised
619 * by another process that handled the initial request.
624 if (!use_sideband && daemon_mode)
627 if (depth == 0 && shallows.nr == 0)
630 struct commit_list *result = NULL, *backup = NULL;
632 if (depth == INFINITE_DEPTH && !is_repository_shallow())
633 for (i = 0; i < shallows.nr; i++) {
634 struct object *object = shallows.objects[i].item;
635 object->flags |= NOT_SHALLOW;
639 get_shallow_commits(&want_obj, depth,
640 SHALLOW, NOT_SHALLOW);
642 struct object *object = &result->item->object;
643 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
644 packet_write(1, "shallow %s",
645 oid_to_hex(&object->oid));
646 register_shallow(object->oid.hash);
649 result = result->next;
651 free_commit_list(backup);
652 for (i = 0; i < shallows.nr; i++) {
653 struct object *object = shallows.objects[i].item;
654 if (object->flags & NOT_SHALLOW) {
655 struct commit_list *parents;
656 packet_write(1, "unshallow %s",
657 oid_to_hex(&object->oid));
658 object->flags &= ~CLIENT_SHALLOW;
659 /* make sure the real parents are parsed */
660 unregister_shallow(object->oid.hash);
662 parse_commit_or_die((struct commit *)object);
663 parents = ((struct commit *)object)->parents;
665 add_object_array(&parents->item->object,
667 parents = parents->next;
669 add_object_array(object, NULL, &extra_edge_obj);
671 /* make sure commit traversal conforms to client */
672 register_shallow(object->oid.hash);
676 if (shallows.nr > 0) {
678 for (i = 0; i < shallows.nr; i++)
679 register_shallow(shallows.objects[i].item->oid.hash);
682 shallow_nr += shallows.nr;
683 free(shallows.objects);
686 /* return non-zero if the ref is hidden, otherwise 0 */
687 static int mark_our_ref(const char *refname, const char *refname_full,
688 const struct object_id *oid)
690 struct object *o = lookup_unknown_object(oid->hash);
692 if (ref_is_hidden(refname, refname_full)) {
693 o->flags |= HIDDEN_REF;
700 static int check_ref(const char *refname_full, const struct object_id *oid,
701 int flag, void *cb_data)
703 const char *refname = strip_namespace(refname_full);
705 mark_our_ref(refname, refname_full, oid);
709 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
711 struct string_list_item *item;
715 for_each_string_list_item(item, symref)
716 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
719 static int send_ref(const char *refname, const struct object_id *oid,
720 int flag, void *cb_data)
722 static const char *capabilities = "multi_ack thin-pack side-band"
723 " side-band-64k ofs-delta shallow no-progress"
724 " include-tag multi_ack_detailed";
725 const char *refname_nons = strip_namespace(refname);
726 struct object_id peeled;
728 if (mark_our_ref(refname_nons, refname, oid))
732 struct strbuf symref_info = STRBUF_INIT;
734 format_symref_info(&symref_info, cb_data);
735 packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
736 oid_to_hex(oid), refname_nons,
738 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
739 " allow-tip-sha1-in-want" : "",
740 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
741 " allow-reachable-sha1-in-want" : "",
742 stateless_rpc ? " no-done" : "",
744 git_user_agent_sanitized());
745 strbuf_release(&symref_info);
747 packet_write(1, "%s %s\n", oid_to_hex(oid), refname_nons);
750 if (!peel_ref(refname, peeled.hash))
751 packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
755 static int find_symref(const char *refname, const struct object_id *oid,
756 int flag, void *cb_data)
758 const char *symref_target;
759 struct string_list_item *item;
760 struct object_id unused;
762 if ((flag & REF_ISSYMREF) == 0)
764 symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
765 if (!symref_target || (flag & REF_ISSYMREF) == 0)
766 die("'%s' is a symref but it is not?", refname);
767 item = string_list_append(cb_data, refname);
768 item->util = xstrdup(symref_target);
772 static void upload_pack(void)
774 struct string_list symref = STRING_LIST_INIT_DUP;
776 head_ref_namespaced(find_symref, &symref);
778 if (advertise_refs || !stateless_rpc) {
780 head_ref_namespaced(send_ref, &symref);
781 for_each_namespaced_ref(send_ref, &symref);
782 advertise_shallow_grafts(1);
785 head_ref_namespaced(check_ref, NULL);
786 for_each_namespaced_ref(check_ref, NULL);
788 string_list_clear(&symref, 1);
794 get_common_commits();
799 static int upload_pack_config(const char *var, const char *value, void *unused)
801 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
802 if (git_config_bool(var, value))
803 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
805 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
806 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
807 if (git_config_bool(var, value))
808 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
810 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
811 } else if (!strcmp("uploadpack.keepalive", var)) {
812 keepalive = git_config_int(var, value);
816 return parse_hide_refs_config(var, value, "uploadpack");
819 int main(int argc, char **argv)
827 packet_trace_identity("upload-pack");
828 git_extract_argv0_path(argv[0]);
829 check_replace_refs = 0;
831 for (i = 1; i < argc; i++) {
836 if (!strcmp(arg, "--advertise-refs")) {
840 if (!strcmp(arg, "--stateless-rpc")) {
844 if (!strcmp(arg, "--strict")) {
848 if (starts_with(arg, "--timeout=")) {
849 timeout = atoi(arg+10);
853 if (!strcmp(arg, "--")) {
860 usage(upload_pack_usage);
866 if (!enter_repo(dir, strict))
867 die("'%s' does not appear to be a git repository", dir);
869 git_config(upload_pack_config, NULL);