11 #include "list-objects.h"
12 #include "run-command.h"
15 #include "string-list.h"
17 static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
19 /* bits #0..7 in revision.h, #8..10 in commit.c */
20 #define THEY_HAVE (1u << 11)
21 #define OUR_REF (1u << 12)
22 #define WANTED (1u << 13)
23 #define COMMON_KNOWN (1u << 14)
24 #define REACHABLE (1u << 15)
26 #define SHALLOW (1u << 16)
27 #define NOT_SHALLOW (1u << 17)
28 #define CLIENT_SHALLOW (1u << 18)
30 static unsigned long oldest_have;
34 static int use_thin_pack, use_ofs_delta, use_include_tag;
35 static int no_progress, daemon_mode;
36 static int shallow_nr;
37 static struct object_array have_obj;
38 static struct object_array want_obj;
39 static struct object_array extra_edge_obj;
40 static unsigned int timeout;
42 * otherwise maximum packet size (up to 65520 bytes).
44 static int use_sideband;
45 static int advertise_refs;
46 static int stateless_rpc;
48 static void reset_timeout(void)
53 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
56 return send_sideband(1, fd, data, sz, use_sideband);
61 /* XXX: are we happy to lose stuff here? */
65 write_or_die(fd, data, sz);
69 static FILE *pack_pipe = NULL;
70 static void show_commit(struct commit *commit, void *data)
72 if (commit->object.flags & BOUNDARY)
73 fputc('-', pack_pipe);
74 if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
75 die("broken output pipe");
76 fputc('\n', pack_pipe);
79 commit->buffer = NULL;
82 static void show_object(struct object *obj,
83 const struct name_path *path, const char *component,
86 show_object_with_name(pack_pipe, obj, path, component);
89 static void show_edge(struct commit *commit)
91 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
94 static int do_rev_list(int in, int out, void *user_data)
99 pack_pipe = xfdopen(out, "w");
100 init_revisions(&revs, NULL);
101 revs.tag_objects = 1;
102 revs.tree_objects = 1;
103 revs.blob_objects = 1;
107 for (i = 0; i < want_obj.nr; i++) {
108 struct object *o = want_obj.objects[i].item;
110 o->flags &= ~UNINTERESTING;
111 add_pending_object(&revs, o, NULL);
113 for (i = 0; i < have_obj.nr; i++) {
114 struct object *o = have_obj.objects[i].item;
115 o->flags |= UNINTERESTING;
116 add_pending_object(&revs, o, NULL);
118 setup_revisions(0, NULL, &revs, NULL);
119 if (prepare_revision_walk(&revs))
120 die("revision walk setup failed");
121 mark_edges_uninteresting(revs.commits, &revs, show_edge);
123 for (i = 0; i < extra_edge_obj.nr; i++)
124 fprintf(pack_pipe, "-%s\n", sha1_to_hex(
125 extra_edge_obj.objects[i].item->sha1));
126 traverse_commit_list(&revs, show_commit, show_object, NULL);
132 static void create_pack_file(void)
134 struct async rev_list;
135 struct child_process pack_objects;
136 char data[8193], progress[128];
137 char abort_msg[] = "aborting due to possible repository "
138 "corruption on the remote side.";
141 const char *argv[10];
144 argv[arg++] = "pack-objects";
146 argv[arg++] = "--revs";
148 argv[arg++] = "--thin";
151 argv[arg++] = "--stdout";
153 argv[arg++] = "--progress";
155 argv[arg++] = "--delta-base-offset";
157 argv[arg++] = "--include-tag";
160 memset(&pack_objects, 0, sizeof(pack_objects));
161 pack_objects.in = -1;
162 pack_objects.out = -1;
163 pack_objects.err = -1;
164 pack_objects.git_cmd = 1;
165 pack_objects.argv = argv;
167 if (start_command(&pack_objects))
168 die("git upload-pack: unable to fork git-pack-objects");
171 memset(&rev_list, 0, sizeof(rev_list));
172 rev_list.proc = do_rev_list;
173 rev_list.out = pack_objects.in;
174 if (start_async(&rev_list))
175 die("git upload-pack: unable to fork git-rev-list");
178 FILE *pipe_fd = xfdopen(pack_objects.in, "w");
181 for (i = 0; i < want_obj.nr; i++)
182 fprintf(pipe_fd, "%s\n",
183 sha1_to_hex(want_obj.objects[i].item->sha1));
184 fprintf(pipe_fd, "--not\n");
185 for (i = 0; i < have_obj.nr; i++)
186 fprintf(pipe_fd, "%s\n",
187 sha1_to_hex(have_obj.objects[i].item->sha1));
188 fprintf(pipe_fd, "\n");
194 /* We read from pack_objects.err to capture stderr output for
195 * progress bar, and pack_objects.out to capture the pack data.
199 struct pollfd pfd[2];
200 int pe, pu, pollsize;
207 if (0 <= pack_objects.out) {
208 pfd[pollsize].fd = pack_objects.out;
209 pfd[pollsize].events = POLLIN;
213 if (0 <= pack_objects.err) {
214 pfd[pollsize].fd = pack_objects.err;
215 pfd[pollsize].events = POLLIN;
223 if (poll(pfd, pollsize, -1) < 0) {
224 if (errno != EINTR) {
225 error("poll failed, resuming: %s",
231 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
232 /* Status ready; we ship that in the side-band
233 * or dump to the standard error.
235 sz = xread(pack_objects.err, progress,
238 send_client_data(2, progress, sz);
240 close(pack_objects.err);
241 pack_objects.err = -1;
245 /* give priority to status messages */
248 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
249 /* Data ready; we keep the last byte to ourselves
250 * in case we detect broken rev-list, so that we
251 * can leave the stream corrupted. This is
252 * unfortunate -- unpack-objects would happily
253 * accept a valid packdata with trailing garbage,
254 * so appending garbage after we pass all the
255 * pack data is not good enough to signal
256 * breakage to downstream.
264 sz = xread(pack_objects.out, cp,
265 sizeof(data) - outsz);
269 close(pack_objects.out);
270 pack_objects.out = -1;
276 buffered = data[sz-1] & 0xFF;
281 sz = send_client_data(1, data, sz);
287 if (finish_command(&pack_objects)) {
288 error("git upload-pack: git-pack-objects died with error.");
291 if (shallow_nr && finish_async(&rev_list))
292 goto fail; /* error was already reported */
297 sz = send_client_data(1, data, 1);
300 fprintf(stderr, "flushed.\n");
307 send_client_data(3, abort_msg, sizeof(abort_msg));
308 die("git upload-pack: %s", abort_msg);
311 static int got_sha1(char *hex, unsigned char *sha1)
314 int we_knew_they_have = 0;
316 if (get_sha1_hex(hex, sha1))
317 die("git upload-pack: expected SHA1 object, got '%s'", hex);
318 if (!has_sha1_file(sha1))
321 o = lookup_object(sha1);
322 if (!(o && o->parsed))
323 o = parse_object(sha1);
325 die("oops (%s)", sha1_to_hex(sha1));
326 if (o->type == OBJ_COMMIT) {
327 struct commit_list *parents;
328 struct commit *commit = (struct commit *)o;
329 if (o->flags & THEY_HAVE)
330 we_knew_they_have = 1;
332 o->flags |= THEY_HAVE;
333 if (!oldest_have || (commit->date < oldest_have))
334 oldest_have = commit->date;
335 for (parents = commit->parents;
337 parents = parents->next)
338 parents->item->object.flags |= THEY_HAVE;
340 if (!we_knew_they_have) {
341 add_object_array(o, NULL, &have_obj);
347 static int reachable(struct commit *want)
349 struct commit_list *work = NULL;
351 commit_list_insert_by_date(want, &work);
353 struct commit_list *list = work->next;
354 struct commit *commit = work->item;
358 if (commit->object.flags & THEY_HAVE) {
359 want->object.flags |= COMMON_KNOWN;
362 if (!commit->object.parsed)
363 parse_object(commit->object.sha1);
364 if (commit->object.flags & REACHABLE)
366 commit->object.flags |= REACHABLE;
367 if (commit->date < oldest_have)
369 for (list = commit->parents; list; list = list->next) {
370 struct commit *parent = list->item;
371 if (!(parent->object.flags & REACHABLE))
372 commit_list_insert_by_date(parent, &work);
375 want->object.flags |= REACHABLE;
376 clear_commit_marks(want, REACHABLE);
377 free_commit_list(work);
378 return (want->object.flags & COMMON_KNOWN);
381 static int ok_to_give_up(void)
388 for (i = 0; i < want_obj.nr; i++) {
389 struct object *want = want_obj.objects[i].item;
391 if (want->flags & COMMON_KNOWN)
393 want = deref_tag(want, "a want line", 0);
394 if (!want || want->type != OBJ_COMMIT) {
395 /* no way to tell if this is reachable by
396 * looking at the ancestry chain alone, so
397 * leave a note to ourselves not to worry about
398 * this object anymore.
400 want_obj.objects[i].item->flags |= COMMON_KNOWN;
403 if (!reachable((struct commit *)want))
409 static int get_common_commits(void)
411 static char line[1000];
412 unsigned char sha1[20];
418 save_commit_buffer = 0;
421 int len = packet_read_line(0, line, sizeof(line));
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 void check_non_tip(void)
484 static const char *argv[] = {
485 "rev-list", "--stdin", NULL,
487 static struct child_process cmd;
489 char namebuf[42]; /* ^ + SHA-1 + LF */
492 /* In the normal in-process case non-tip request can never happen */
502 if (start_command(&cmd))
506 * If rev-list --stdin encounters an unknown commit, it
507 * terminates, which will cause SIGPIPE in the write loop
510 sigchain_push(SIGPIPE, SIG_IGN);
514 for (i = get_max_object_index(); 0 < i; ) {
515 o = get_indexed_object(--i);
518 if (!(o->flags & OUR_REF))
520 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
521 if (write_in_full(cmd.in, namebuf, 42) < 0)
525 for (i = 0; i < want_obj.nr; i++) {
526 o = want_obj.objects[i].item;
527 if (o->flags & OUR_REF)
529 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
530 if (write_in_full(cmd.in, namebuf, 41) < 0)
535 sigchain_pop(SIGPIPE);
538 * The commits out of the rev-list are not ancestors of
541 i = read_in_full(cmd.out, namebuf, 1);
547 * rev-list may have died by encountering a bad commit
548 * in the history, in which case we do want to bail out
549 * even when it showed no commit.
551 if (finish_command(&cmd))
554 /* All the non-tip ones are ancestors of what we advertised */
558 /* Pick one of them (we know there at least is one) */
559 for (i = 0; i < want_obj.nr; i++) {
560 o = want_obj.objects[i].item;
561 if (!(o->flags & OUR_REF))
562 die("git upload-pack: not our ref %s",
563 sha1_to_hex(o->sha1));
567 static void receive_needs(void)
569 struct object_array shallows = OBJECT_ARRAY_INIT;
570 static char line[1000];
577 const char *features;
578 unsigned char sha1_buf[20];
579 len = packet_read_line(0, line, sizeof(line));
584 if (!prefixcmp(line, "shallow ")) {
585 unsigned char sha1[20];
586 struct object *object;
587 if (get_sha1_hex(line + 8, sha1))
588 die("invalid shallow line: %s", line);
589 object = parse_object(sha1);
591 die("did not find object for %s", line);
592 if (object->type != OBJ_COMMIT)
593 die("invalid shallow object %s", sha1_to_hex(sha1));
594 if (!(object->flags & CLIENT_SHALLOW)) {
595 object->flags |= CLIENT_SHALLOW;
596 add_object_array(object, NULL, &shallows);
600 if (!prefixcmp(line, "deepen ")) {
602 depth = strtol(line + 7, &end, 0);
603 if (end == line + 7 || depth <= 0)
604 die("Invalid deepen: %s", line);
607 if (prefixcmp(line, "want ") ||
608 get_sha1_hex(line+5, sha1_buf))
609 die("git upload-pack: protocol error, "
610 "expected to get sha, not '%s'", line);
612 features = line + 45;
614 if (parse_feature_request(features, "multi_ack_detailed"))
616 else if (parse_feature_request(features, "multi_ack"))
618 if (parse_feature_request(features, "no-done"))
620 if (parse_feature_request(features, "thin-pack"))
622 if (parse_feature_request(features, "ofs-delta"))
624 if (parse_feature_request(features, "side-band-64k"))
625 use_sideband = LARGE_PACKET_MAX;
626 else if (parse_feature_request(features, "side-band"))
627 use_sideband = DEFAULT_PACKET_MAX;
628 if (parse_feature_request(features, "no-progress"))
630 if (parse_feature_request(features, "include-tag"))
633 o = lookup_object(sha1_buf);
635 die("git upload-pack: not our ref %s",
636 sha1_to_hex(sha1_buf));
637 if (!(o->flags & WANTED)) {
639 if (!(o->flags & OUR_REF))
641 add_object_array(o, NULL, &want_obj);
646 * We have sent all our refs already, and the other end
647 * should have chosen out of them. When we are operating
648 * in the stateless RPC mode, however, their choice may
649 * have been based on the set of older refs advertised
650 * by another process that handled the initial request.
655 if (!use_sideband && daemon_mode)
658 if (depth == 0 && shallows.nr == 0)
661 struct commit_list *result = NULL, *backup = NULL;
663 if (depth == INFINITE_DEPTH)
664 for (i = 0; i < shallows.nr; i++) {
665 struct object *object = shallows.objects[i].item;
666 object->flags |= NOT_SHALLOW;
670 get_shallow_commits(&want_obj, depth,
671 SHALLOW, NOT_SHALLOW);
673 struct object *object = &result->item->object;
674 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
675 packet_write(1, "shallow %s",
676 sha1_to_hex(object->sha1));
677 register_shallow(object->sha1);
680 result = result->next;
682 free_commit_list(backup);
683 for (i = 0; i < shallows.nr; i++) {
684 struct object *object = shallows.objects[i].item;
685 if (object->flags & NOT_SHALLOW) {
686 struct commit_list *parents;
687 packet_write(1, "unshallow %s",
688 sha1_to_hex(object->sha1));
689 object->flags &= ~CLIENT_SHALLOW;
690 /* make sure the real parents are parsed */
691 unregister_shallow(object->sha1);
693 if (parse_commit((struct commit *)object))
694 die("invalid commit");
695 parents = ((struct commit *)object)->parents;
697 add_object_array(&parents->item->object,
699 parents = parents->next;
701 add_object_array(object, NULL, &extra_edge_obj);
703 /* make sure commit traversal conforms to client */
704 register_shallow(object->sha1);
708 if (shallows.nr > 0) {
710 for (i = 0; i < shallows.nr; i++)
711 register_shallow(shallows.objects[i].item->sha1);
714 shallow_nr += shallows.nr;
715 free(shallows.objects);
718 /* return non-zero if the ref is hidden, otherwise 0 */
719 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
721 struct object *o = lookup_unknown_object(sha1);
723 if (ref_is_hidden(refname))
726 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
731 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
733 static const char *capabilities = "multi_ack thin-pack side-band"
734 " side-band-64k ofs-delta shallow no-progress"
735 " include-tag multi_ack_detailed";
736 const char *refname_nons = strip_namespace(refname);
737 unsigned char peeled[20];
739 if (mark_our_ref(refname, sha1, flag, cb_data))
743 packet_write(1, "%s %s%c%s%s agent=%s\n",
744 sha1_to_hex(sha1), refname_nons,
746 stateless_rpc ? " no-done" : "",
747 git_user_agent_sanitized());
749 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
751 if (!peel_ref(refname, peeled))
752 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
756 static void upload_pack(void)
758 if (advertise_refs || !stateless_rpc) {
760 head_ref_namespaced(send_ref, NULL);
761 for_each_namespaced_ref(send_ref, NULL);
764 head_ref_namespaced(mark_our_ref, NULL);
765 for_each_namespaced_ref(mark_our_ref, NULL);
772 get_common_commits();
777 static int upload_pack_config(const char *var, const char *value, void *unused)
779 return parse_hide_refs_config(var, value, "uploadpack");
782 int main(int argc, char **argv)
790 packet_trace_identity("upload-pack");
791 git_extract_argv0_path(argv[0]);
792 read_replace_refs = 0;
794 for (i = 1; i < argc; i++) {
799 if (!strcmp(arg, "--advertise-refs")) {
803 if (!strcmp(arg, "--stateless-rpc")) {
807 if (!strcmp(arg, "--strict")) {
811 if (!prefixcmp(arg, "--timeout=")) {
812 timeout = atoi(arg+10);
816 if (!strcmp(arg, "--")) {
823 usage(upload_pack_usage);
829 if (!enter_repo(dir, strict))
830 die("'%s' does not appear to be a git repository", dir);
831 if (is_repository_shallow())
832 die("attempt to fetch/clone from a shallow repository");
833 git_config(upload_pack_config, NULL);