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 /* bits #0..7 in revision.h, #8..10 in commit.c */
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;
45 * otherwise maximum packet size (up to 65520 bytes).
47 static int use_sideband;
48 static int advertise_refs;
49 static int stateless_rpc;
51 static void reset_timeout(void)
56 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
59 return send_sideband(1, fd, data, sz, use_sideband);
64 /* XXX: are we happy to lose stuff here? */
68 write_or_die(fd, data, sz);
72 static FILE *pack_pipe = NULL;
73 static void show_commit(struct commit *commit, void *data)
75 if (commit->object.flags & BOUNDARY)
76 fputc('-', pack_pipe);
77 if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
78 die("broken output pipe");
79 fputc('\n', pack_pipe);
82 commit->buffer = NULL;
85 static void show_object(struct object *obj,
86 const struct name_path *path, const char *component,
89 show_object_with_name(pack_pipe, obj, path, component);
92 static void show_edge(struct commit *commit)
94 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
97 static int do_rev_list(int in, int out, void *user_data)
100 struct rev_info revs;
102 pack_pipe = xfdopen(out, "w");
103 init_revisions(&revs, NULL);
104 revs.tag_objects = 1;
105 revs.tree_objects = 1;
106 revs.blob_objects = 1;
110 for (i = 0; i < want_obj.nr; i++) {
111 struct object *o = want_obj.objects[i].item;
113 o->flags &= ~UNINTERESTING;
114 add_pending_object(&revs, o, NULL);
116 for (i = 0; i < have_obj.nr; i++) {
117 struct object *o = have_obj.objects[i].item;
118 o->flags |= UNINTERESTING;
119 add_pending_object(&revs, o, NULL);
121 setup_revisions(0, NULL, &revs, NULL);
122 if (prepare_revision_walk(&revs))
123 die("revision walk setup failed");
124 mark_edges_uninteresting(revs.commits, &revs, show_edge);
126 for (i = 0; i < extra_edge_obj.nr; i++)
127 fprintf(pack_pipe, "-%s\n", sha1_to_hex(
128 extra_edge_obj.objects[i].item->sha1));
129 traverse_commit_list(&revs, show_commit, show_object, NULL);
135 static void create_pack_file(void)
137 struct async rev_list;
138 struct child_process pack_objects;
139 char data[8193], progress[128];
140 char abort_msg[] = "aborting due to possible repository "
141 "corruption on the remote side.";
144 const char *argv[10];
147 argv[arg++] = "pack-objects";
149 argv[arg++] = "--revs";
151 argv[arg++] = "--thin";
154 argv[arg++] = "--stdout";
156 argv[arg++] = "--progress";
158 argv[arg++] = "--delta-base-offset";
160 argv[arg++] = "--include-tag";
163 memset(&pack_objects, 0, sizeof(pack_objects));
164 pack_objects.in = -1;
165 pack_objects.out = -1;
166 pack_objects.err = -1;
167 pack_objects.git_cmd = 1;
168 pack_objects.argv = argv;
170 if (start_command(&pack_objects))
171 die("git upload-pack: unable to fork git-pack-objects");
174 memset(&rev_list, 0, sizeof(rev_list));
175 rev_list.proc = do_rev_list;
176 rev_list.out = pack_objects.in;
177 if (start_async(&rev_list))
178 die("git upload-pack: unable to fork git-rev-list");
181 FILE *pipe_fd = xfdopen(pack_objects.in, "w");
184 for (i = 0; i < want_obj.nr; i++)
185 fprintf(pipe_fd, "%s\n",
186 sha1_to_hex(want_obj.objects[i].item->sha1));
187 fprintf(pipe_fd, "--not\n");
188 for (i = 0; i < have_obj.nr; i++)
189 fprintf(pipe_fd, "%s\n",
190 sha1_to_hex(have_obj.objects[i].item->sha1));
191 fprintf(pipe_fd, "\n");
197 /* We read from pack_objects.err to capture stderr output for
198 * progress bar, and pack_objects.out to capture the pack data.
202 struct pollfd pfd[2];
203 int pe, pu, pollsize;
210 if (0 <= pack_objects.out) {
211 pfd[pollsize].fd = pack_objects.out;
212 pfd[pollsize].events = POLLIN;
216 if (0 <= pack_objects.err) {
217 pfd[pollsize].fd = pack_objects.err;
218 pfd[pollsize].events = POLLIN;
226 if (poll(pfd, pollsize, -1) < 0) {
227 if (errno != EINTR) {
228 error("poll failed, resuming: %s",
234 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
235 /* Status ready; we ship that in the side-band
236 * or dump to the standard error.
238 sz = xread(pack_objects.err, progress,
241 send_client_data(2, progress, sz);
243 close(pack_objects.err);
244 pack_objects.err = -1;
248 /* give priority to status messages */
251 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
252 /* Data ready; we keep the last byte to ourselves
253 * in case we detect broken rev-list, so that we
254 * can leave the stream corrupted. This is
255 * unfortunate -- unpack-objects would happily
256 * accept a valid packdata with trailing garbage,
257 * so appending garbage after we pass all the
258 * pack data is not good enough to signal
259 * breakage to downstream.
267 sz = xread(pack_objects.out, cp,
268 sizeof(data) - outsz);
272 close(pack_objects.out);
273 pack_objects.out = -1;
279 buffered = data[sz-1] & 0xFF;
284 sz = send_client_data(1, data, sz);
290 if (finish_command(&pack_objects)) {
291 error("git upload-pack: git-pack-objects died with error.");
294 if (shallow_nr && finish_async(&rev_list))
295 goto fail; /* error was already reported */
300 sz = send_client_data(1, data, 1);
303 fprintf(stderr, "flushed.\n");
310 send_client_data(3, abort_msg, sizeof(abort_msg));
311 die("git upload-pack: %s", abort_msg);
314 static int got_sha1(char *hex, unsigned char *sha1)
317 int we_knew_they_have = 0;
319 if (get_sha1_hex(hex, sha1))
320 die("git upload-pack: expected SHA1 object, got '%s'", hex);
321 if (!has_sha1_file(sha1))
324 o = parse_object(sha1);
326 die("oops (%s)", sha1_to_hex(sha1));
327 if (o->type == OBJ_COMMIT) {
328 struct commit_list *parents;
329 struct commit *commit = (struct commit *)o;
330 if (o->flags & THEY_HAVE)
331 we_knew_they_have = 1;
333 o->flags |= THEY_HAVE;
334 if (!oldest_have || (commit->date < oldest_have))
335 oldest_have = commit->date;
336 for (parents = commit->parents;
338 parents = parents->next)
339 parents->item->object.flags |= THEY_HAVE;
341 if (!we_knew_they_have) {
342 add_object_array(o, NULL, &have_obj);
348 static int reachable(struct commit *want)
350 struct commit_list *work = NULL;
352 commit_list_insert_by_date(want, &work);
354 struct commit_list *list = work->next;
355 struct commit *commit = work->item;
359 if (commit->object.flags & THEY_HAVE) {
360 want->object.flags |= COMMON_KNOWN;
363 if (!commit->object.parsed)
364 parse_object(commit->object.sha1);
365 if (commit->object.flags & REACHABLE)
367 commit->object.flags |= REACHABLE;
368 if (commit->date < oldest_have)
370 for (list = commit->parents; list; list = list->next) {
371 struct commit *parent = list->item;
372 if (!(parent->object.flags & REACHABLE))
373 commit_list_insert_by_date(parent, &work);
376 want->object.flags |= REACHABLE;
377 clear_commit_marks(want, REACHABLE);
378 free_commit_list(work);
379 return (want->object.flags & COMMON_KNOWN);
382 static int ok_to_give_up(void)
389 for (i = 0; i < want_obj.nr; i++) {
390 struct object *want = want_obj.objects[i].item;
392 if (want->flags & COMMON_KNOWN)
394 want = deref_tag(want, "a want line", 0);
395 if (!want || want->type != OBJ_COMMIT) {
396 /* no way to tell if this is reachable by
397 * looking at the ancestry chain alone, so
398 * leave a note to ourselves not to worry about
399 * this object anymore.
401 want_obj.objects[i].item->flags |= COMMON_KNOWN;
404 if (!reachable((struct commit *)want))
410 static int get_common_commits(void)
412 unsigned char sha1[20];
418 save_commit_buffer = 0;
421 char *line = packet_read_line(0, NULL);
425 if (multi_ack == 2 && got_common
426 && !got_other && ok_to_give_up()) {
428 packet_write(1, "ACK %s ready\n", last_hex);
430 if (have_obj.nr == 0 || multi_ack)
431 packet_write(1, "NAK\n");
433 if (no_done && sent_ready) {
434 packet_write(1, "ACK %s\n", last_hex);
443 if (!prefixcmp(line, "have ")) {
444 switch (got_sha1(line+5, sha1)) {
445 case -1: /* they have what we do not */
447 if (multi_ack && ok_to_give_up()) {
448 const char *hex = sha1_to_hex(sha1);
449 if (multi_ack == 2) {
451 packet_write(1, "ACK %s ready\n", hex);
453 packet_write(1, "ACK %s continue\n", hex);
458 memcpy(last_hex, sha1_to_hex(sha1), 41);
460 packet_write(1, "ACK %s common\n", last_hex);
462 packet_write(1, "ACK %s continue\n", last_hex);
463 else if (have_obj.nr == 1)
464 packet_write(1, "ACK %s\n", last_hex);
469 if (!strcmp(line, "done")) {
470 if (have_obj.nr > 0) {
472 packet_write(1, "ACK %s\n", last_hex);
475 packet_write(1, "NAK\n");
478 die("git upload-pack: expected SHA1 list, got '%s'", line);
482 static int is_our_ref(struct object *o)
485 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
488 static void check_non_tip(void)
490 static const char *argv[] = {
491 "rev-list", "--stdin", NULL,
493 static struct child_process cmd;
495 char namebuf[42]; /* ^ + SHA-1 + LF */
498 /* In the normal in-process case non-tip request can never happen */
508 if (start_command(&cmd))
512 * If rev-list --stdin encounters an unknown commit, it
513 * terminates, which will cause SIGPIPE in the write loop
516 sigchain_push(SIGPIPE, SIG_IGN);
520 for (i = get_max_object_index(); 0 < i; ) {
521 o = get_indexed_object(--i);
526 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
527 if (write_in_full(cmd.in, namebuf, 42) < 0)
531 for (i = 0; i < want_obj.nr; i++) {
532 o = want_obj.objects[i].item;
535 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
536 if (write_in_full(cmd.in, namebuf, 41) < 0)
541 sigchain_pop(SIGPIPE);
544 * The commits out of the rev-list are not ancestors of
547 i = read_in_full(cmd.out, namebuf, 1);
553 * rev-list may have died by encountering a bad commit
554 * in the history, in which case we do want to bail out
555 * even when it showed no commit.
557 if (finish_command(&cmd))
560 /* All the non-tip ones are ancestors of what we advertised */
564 /* Pick one of them (we know there at least is one) */
565 for (i = 0; i < want_obj.nr; i++) {
566 o = want_obj.objects[i].item;
568 die("git upload-pack: not our ref %s",
569 sha1_to_hex(o->sha1));
573 static void receive_needs(void)
575 struct object_array shallows = OBJECT_ARRAY_INIT;
582 const char *features;
583 unsigned char sha1_buf[20];
584 char *line = packet_read_line(0, NULL);
589 if (!prefixcmp(line, "shallow ")) {
590 unsigned char sha1[20];
591 struct object *object;
592 if (get_sha1_hex(line + 8, sha1))
593 die("invalid shallow line: %s", line);
594 object = parse_object(sha1);
597 if (object->type != OBJ_COMMIT)
598 die("invalid shallow object %s", sha1_to_hex(sha1));
599 if (!(object->flags & CLIENT_SHALLOW)) {
600 object->flags |= CLIENT_SHALLOW;
601 add_object_array(object, NULL, &shallows);
605 if (!prefixcmp(line, "deepen ")) {
607 depth = strtol(line + 7, &end, 0);
608 if (end == line + 7 || depth <= 0)
609 die("Invalid deepen: %s", line);
612 if (prefixcmp(line, "want ") ||
613 get_sha1_hex(line+5, sha1_buf))
614 die("git upload-pack: protocol error, "
615 "expected to get sha, not '%s'", line);
617 features = line + 45;
619 if (parse_feature_request(features, "multi_ack_detailed"))
621 else if (parse_feature_request(features, "multi_ack"))
623 if (parse_feature_request(features, "no-done"))
625 if (parse_feature_request(features, "thin-pack"))
627 if (parse_feature_request(features, "ofs-delta"))
629 if (parse_feature_request(features, "side-band-64k"))
630 use_sideband = LARGE_PACKET_MAX;
631 else if (parse_feature_request(features, "side-band"))
632 use_sideband = DEFAULT_PACKET_MAX;
633 if (parse_feature_request(features, "no-progress"))
635 if (parse_feature_request(features, "include-tag"))
638 o = parse_object(sha1_buf);
640 die("git upload-pack: not our ref %s",
641 sha1_to_hex(sha1_buf));
642 if (!(o->flags & WANTED)) {
646 add_object_array(o, NULL, &want_obj);
651 * We have sent all our refs already, and the other end
652 * should have chosen out of them. When we are operating
653 * in the stateless RPC mode, however, their choice may
654 * have been based on the set of older refs advertised
655 * by another process that handled the initial request.
660 if (!use_sideband && daemon_mode)
663 if (depth == 0 && shallows.nr == 0)
666 struct commit_list *result = NULL, *backup = NULL;
668 if (depth == INFINITE_DEPTH)
669 for (i = 0; i < shallows.nr; i++) {
670 struct object *object = shallows.objects[i].item;
671 object->flags |= NOT_SHALLOW;
675 get_shallow_commits(&want_obj, depth,
676 SHALLOW, NOT_SHALLOW);
678 struct object *object = &result->item->object;
679 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
680 packet_write(1, "shallow %s",
681 sha1_to_hex(object->sha1));
682 register_shallow(object->sha1);
685 result = result->next;
687 free_commit_list(backup);
688 for (i = 0; i < shallows.nr; i++) {
689 struct object *object = shallows.objects[i].item;
690 if (object->flags & NOT_SHALLOW) {
691 struct commit_list *parents;
692 packet_write(1, "unshallow %s",
693 sha1_to_hex(object->sha1));
694 object->flags &= ~CLIENT_SHALLOW;
695 /* make sure the real parents are parsed */
696 unregister_shallow(object->sha1);
698 if (parse_commit((struct commit *)object))
699 die("invalid commit");
700 parents = ((struct commit *)object)->parents;
702 add_object_array(&parents->item->object,
704 parents = parents->next;
706 add_object_array(object, NULL, &extra_edge_obj);
708 /* make sure commit traversal conforms to client */
709 register_shallow(object->sha1);
713 if (shallows.nr > 0) {
715 for (i = 0; i < shallows.nr; i++)
716 register_shallow(shallows.objects[i].item->sha1);
719 shallow_nr += shallows.nr;
720 free(shallows.objects);
723 /* return non-zero if the ref is hidden, otherwise 0 */
724 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
726 struct object *o = lookup_unknown_object(sha1);
728 if (ref_is_hidden(refname)) {
729 o->flags |= HIDDEN_REF;
733 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
738 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
740 static const char *capabilities = "multi_ack thin-pack side-band"
741 " side-band-64k ofs-delta shallow no-progress"
742 " include-tag multi_ack_detailed";
743 const char *refname_nons = strip_namespace(refname);
744 unsigned char peeled[20];
746 if (mark_our_ref(refname, sha1, flag, cb_data))
750 packet_write(1, "%s %s%c%s%s%s agent=%s\n",
751 sha1_to_hex(sha1), refname_nons,
753 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
754 stateless_rpc ? " no-done" : "",
755 git_user_agent_sanitized());
757 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
759 if (!peel_ref(refname, peeled))
760 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
764 static void upload_pack(void)
766 if (advertise_refs || !stateless_rpc) {
768 head_ref_namespaced(send_ref, NULL);
769 for_each_namespaced_ref(send_ref, NULL);
772 head_ref_namespaced(mark_our_ref, NULL);
773 for_each_namespaced_ref(mark_our_ref, NULL);
780 get_common_commits();
785 static int upload_pack_config(const char *var, const char *value, void *unused)
787 if (!strcmp("uploadpack.allowtipsha1inwant", var))
788 allow_tip_sha1_in_want = git_config_bool(var, value);
789 return parse_hide_refs_config(var, value, "uploadpack");
792 int main(int argc, char **argv)
800 packet_trace_identity("upload-pack");
801 git_extract_argv0_path(argv[0]);
802 read_replace_refs = 0;
804 for (i = 1; i < argc; i++) {
809 if (!strcmp(arg, "--advertise-refs")) {
813 if (!strcmp(arg, "--stateless-rpc")) {
817 if (!strcmp(arg, "--strict")) {
821 if (!prefixcmp(arg, "--timeout=")) {
822 timeout = atoi(arg+10);
826 if (!strcmp(arg, "--")) {
833 usage(upload_pack_usage);
839 if (!enter_repo(dir, strict))
840 die("'%s' does not appear to be a git repository", dir);
841 if (is_repository_shallow())
842 die("attempt to fetch/clone from a shallow repository");
843 git_config(upload_pack_config, NULL);