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", oid_to_hex(&graft->oid));
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++] = "--shallow";
106 argv[arg++] = "--progress";
108 argv[arg++] = "--delta-base-offset";
110 argv[arg++] = "--include-tag";
113 pack_objects.in = -1;
114 pack_objects.out = -1;
115 pack_objects.err = -1;
116 pack_objects.git_cmd = 1;
117 pack_objects.argv = argv;
119 if (start_command(&pack_objects))
120 die("git upload-pack: unable to fork git-pack-objects");
122 pipe_fd = xfdopen(pack_objects.in, "w");
125 for_each_commit_graft(write_one_shallow, pipe_fd);
127 for (i = 0; i < want_obj.nr; i++)
128 fprintf(pipe_fd, "%s\n",
129 sha1_to_hex(want_obj.objects[i].item->sha1));
130 fprintf(pipe_fd, "--not\n");
131 for (i = 0; i < have_obj.nr; i++)
132 fprintf(pipe_fd, "%s\n",
133 sha1_to_hex(have_obj.objects[i].item->sha1));
134 for (i = 0; i < extra_edge_obj.nr; i++)
135 fprintf(pipe_fd, "%s\n",
136 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
137 fprintf(pipe_fd, "\n");
141 /* We read from pack_objects.err to capture stderr output for
142 * progress bar, and pack_objects.out to capture the pack data.
146 struct pollfd pfd[2];
147 int pe, pu, pollsize;
155 if (0 <= pack_objects.out) {
156 pfd[pollsize].fd = pack_objects.out;
157 pfd[pollsize].events = POLLIN;
161 if (0 <= pack_objects.err) {
162 pfd[pollsize].fd = pack_objects.err;
163 pfd[pollsize].events = POLLIN;
171 ret = poll(pfd, pollsize,
172 keepalive < 0 ? -1 : 1000 * keepalive);
175 if (errno != EINTR) {
176 error("poll failed, resuming: %s",
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 = work->next;
316 struct commit *commit = work->item;
320 if (commit->object.flags & THEY_HAVE) {
321 want->object.flags |= COMMON_KNOWN;
324 if (!commit->object.parsed)
325 parse_object(commit->object.sha1);
326 if (commit->object.flags & REACHABLE)
328 commit->object.flags |= REACHABLE;
329 if (commit->date < oldest_have)
331 for (list = commit->parents; list; list = list->next) {
332 struct commit *parent = list->item;
333 if (!(parent->object.flags & REACHABLE))
334 commit_list_insert_by_date(parent, &work);
337 want->object.flags |= REACHABLE;
338 clear_commit_marks(want, REACHABLE);
339 free_commit_list(work);
340 return (want->object.flags & COMMON_KNOWN);
343 static int ok_to_give_up(void)
350 for (i = 0; i < want_obj.nr; i++) {
351 struct object *want = want_obj.objects[i].item;
353 if (want->flags & COMMON_KNOWN)
355 want = deref_tag(want, "a want line", 0);
356 if (!want || want->type != OBJ_COMMIT) {
357 /* no way to tell if this is reachable by
358 * looking at the ancestry chain alone, so
359 * leave a note to ourselves not to worry about
360 * this object anymore.
362 want_obj.objects[i].item->flags |= COMMON_KNOWN;
365 if (!reachable((struct commit *)want))
371 static int get_common_commits(void)
373 unsigned char sha1[20];
379 save_commit_buffer = 0;
382 char *line = packet_read_line(0, NULL);
386 if (multi_ack == 2 && got_common
387 && !got_other && ok_to_give_up()) {
389 packet_write(1, "ACK %s ready\n", last_hex);
391 if (have_obj.nr == 0 || multi_ack)
392 packet_write(1, "NAK\n");
394 if (no_done && sent_ready) {
395 packet_write(1, "ACK %s\n", last_hex);
404 if (starts_with(line, "have ")) {
405 switch (got_sha1(line+5, sha1)) {
406 case -1: /* they have what we do not */
408 if (multi_ack && ok_to_give_up()) {
409 const char *hex = sha1_to_hex(sha1);
410 if (multi_ack == 2) {
412 packet_write(1, "ACK %s ready\n", hex);
414 packet_write(1, "ACK %s continue\n", hex);
419 memcpy(last_hex, sha1_to_hex(sha1), 41);
421 packet_write(1, "ACK %s common\n", last_hex);
423 packet_write(1, "ACK %s continue\n", last_hex);
424 else if (have_obj.nr == 1)
425 packet_write(1, "ACK %s\n", last_hex);
430 if (!strcmp(line, "done")) {
431 if (have_obj.nr > 0) {
433 packet_write(1, "ACK %s\n", last_hex);
436 packet_write(1, "NAK\n");
439 die("git upload-pack: expected SHA1 list, got '%s'", line);
443 static int is_our_ref(struct object *o)
446 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
449 static void check_non_tip(void)
451 static const char *argv[] = {
452 "rev-list", "--stdin", NULL,
454 static struct child_process cmd = CHILD_PROCESS_INIT;
456 char namebuf[42]; /* ^ + SHA-1 + LF */
459 /* In the normal in-process case non-tip request can never happen */
469 if (start_command(&cmd))
473 * If rev-list --stdin encounters an unknown commit, it
474 * terminates, which will cause SIGPIPE in the write loop
477 sigchain_push(SIGPIPE, SIG_IGN);
481 for (i = get_max_object_index(); 0 < i; ) {
482 o = get_indexed_object(--i);
487 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
488 if (write_in_full(cmd.in, namebuf, 42) < 0)
492 for (i = 0; i < want_obj.nr; i++) {
493 o = want_obj.objects[i].item;
496 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
497 if (write_in_full(cmd.in, namebuf, 41) < 0)
502 sigchain_pop(SIGPIPE);
505 * The commits out of the rev-list are not ancestors of
508 i = read_in_full(cmd.out, namebuf, 1);
514 * rev-list may have died by encountering a bad commit
515 * in the history, in which case we do want to bail out
516 * even when it showed no commit.
518 if (finish_command(&cmd))
521 /* All the non-tip ones are ancestors of what we advertised */
525 /* Pick one of them (we know there at least is one) */
526 for (i = 0; i < want_obj.nr; i++) {
527 o = want_obj.objects[i].item;
529 die("git upload-pack: not our ref %s",
530 sha1_to_hex(o->sha1));
534 static void receive_needs(void)
536 struct object_array shallows = OBJECT_ARRAY_INIT;
543 const char *features;
544 unsigned char sha1_buf[20];
545 char *line = packet_read_line(0, NULL);
550 if (starts_with(line, "shallow ")) {
551 unsigned char sha1[20];
552 struct object *object;
553 if (get_sha1_hex(line + 8, sha1))
554 die("invalid shallow line: %s", line);
555 object = parse_object(sha1);
558 if (object->type != OBJ_COMMIT)
559 die("invalid shallow object %s", sha1_to_hex(sha1));
560 if (!(object->flags & CLIENT_SHALLOW)) {
561 object->flags |= CLIENT_SHALLOW;
562 add_object_array(object, NULL, &shallows);
566 if (starts_with(line, "deepen ")) {
568 depth = strtol(line + 7, &end, 0);
569 if (end == line + 7 || depth <= 0)
570 die("Invalid deepen: %s", line);
573 if (!starts_with(line, "want ") ||
574 get_sha1_hex(line+5, sha1_buf))
575 die("git upload-pack: protocol error, "
576 "expected to get sha, not '%s'", line);
578 features = line + 45;
580 if (parse_feature_request(features, "multi_ack_detailed"))
582 else if (parse_feature_request(features, "multi_ack"))
584 if (parse_feature_request(features, "no-done"))
586 if (parse_feature_request(features, "thin-pack"))
588 if (parse_feature_request(features, "ofs-delta"))
590 if (parse_feature_request(features, "side-band-64k"))
591 use_sideband = LARGE_PACKET_MAX;
592 else if (parse_feature_request(features, "side-band"))
593 use_sideband = DEFAULT_PACKET_MAX;
594 if (parse_feature_request(features, "no-progress"))
596 if (parse_feature_request(features, "include-tag"))
599 o = parse_object(sha1_buf);
601 die("git upload-pack: not our ref %s",
602 sha1_to_hex(sha1_buf));
603 if (!(o->flags & WANTED)) {
607 add_object_array(o, NULL, &want_obj);
612 * We have sent all our refs already, and the other end
613 * should have chosen out of them. When we are operating
614 * in the stateless RPC mode, however, their choice may
615 * have been based on the set of older refs advertised
616 * by another process that handled the initial request.
621 if (!use_sideband && daemon_mode)
624 if (depth == 0 && shallows.nr == 0)
627 struct commit_list *result = NULL, *backup = NULL;
629 if (depth == INFINITE_DEPTH && !is_repository_shallow())
630 for (i = 0; i < shallows.nr; i++) {
631 struct object *object = shallows.objects[i].item;
632 object->flags |= NOT_SHALLOW;
636 get_shallow_commits(&want_obj, depth,
637 SHALLOW, NOT_SHALLOW);
639 struct object *object = &result->item->object;
640 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
641 packet_write(1, "shallow %s",
642 sha1_to_hex(object->sha1));
643 register_shallow(object->sha1);
646 result = result->next;
648 free_commit_list(backup);
649 for (i = 0; i < shallows.nr; i++) {
650 struct object *object = shallows.objects[i].item;
651 if (object->flags & NOT_SHALLOW) {
652 struct commit_list *parents;
653 packet_write(1, "unshallow %s",
654 sha1_to_hex(object->sha1));
655 object->flags &= ~CLIENT_SHALLOW;
656 /* make sure the real parents are parsed */
657 unregister_shallow(object->sha1);
659 parse_commit_or_die((struct commit *)object);
660 parents = ((struct commit *)object)->parents;
662 add_object_array(&parents->item->object,
664 parents = parents->next;
666 add_object_array(object, NULL, &extra_edge_obj);
668 /* make sure commit traversal conforms to client */
669 register_shallow(object->sha1);
673 if (shallows.nr > 0) {
675 for (i = 0; i < shallows.nr; i++)
676 register_shallow(shallows.objects[i].item->sha1);
679 shallow_nr += shallows.nr;
680 free(shallows.objects);
683 /* return non-zero if the ref is hidden, otherwise 0 */
684 static int mark_our_ref(const char *refname, const unsigned char *sha1)
686 struct object *o = lookup_unknown_object(sha1);
688 if (ref_is_hidden(refname)) {
689 o->flags |= HIDDEN_REF;
696 static int check_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
698 mark_our_ref(refname, sha1);
702 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
704 struct string_list_item *item;
708 for_each_string_list_item(item, symref)
709 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
712 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
714 static const char *capabilities = "multi_ack thin-pack side-band"
715 " side-band-64k ofs-delta shallow no-progress"
716 " include-tag multi_ack_detailed";
717 const char *refname_nons = strip_namespace(refname);
718 unsigned char peeled[20];
720 if (mark_our_ref(refname, sha1))
724 struct strbuf symref_info = STRBUF_INIT;
726 format_symref_info(&symref_info, cb_data);
727 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
728 sha1_to_hex(sha1), refname_nons,
730 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
731 stateless_rpc ? " no-done" : "",
733 git_user_agent_sanitized());
734 strbuf_release(&symref_info);
736 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
739 if (!peel_ref(refname, peeled))
740 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
744 static int find_symref(const char *refname, const unsigned char *sha1, int flag,
747 const char *symref_target;
748 struct string_list_item *item;
749 unsigned char unused[20];
751 if ((flag & REF_ISSYMREF) == 0)
753 symref_target = resolve_ref_unsafe(refname, 0, unused, &flag);
754 if (!symref_target || (flag & REF_ISSYMREF) == 0)
755 die("'%s' is a symref but it is not?", refname);
756 item = string_list_append(cb_data, refname);
757 item->util = xstrdup(symref_target);
761 static void upload_pack(void)
763 struct string_list symref = STRING_LIST_INIT_DUP;
765 head_ref_namespaced(find_symref, &symref);
767 if (advertise_refs || !stateless_rpc) {
769 head_ref_namespaced(send_ref, &symref);
770 for_each_namespaced_ref(send_ref, &symref);
771 advertise_shallow_grafts(1);
774 head_ref_namespaced(check_ref, NULL);
775 for_each_namespaced_ref(check_ref, NULL);
777 string_list_clear(&symref, 1);
783 get_common_commits();
788 static int upload_pack_config(const char *var, const char *value, void *unused)
790 if (!strcmp("uploadpack.allowtipsha1inwant", var))
791 allow_tip_sha1_in_want = git_config_bool(var, value);
792 else if (!strcmp("uploadpack.keepalive", var)) {
793 keepalive = git_config_int(var, value);
797 return parse_hide_refs_config(var, value, "uploadpack");
800 int main(int argc, char **argv)
808 packet_trace_identity("upload-pack");
809 git_extract_argv0_path(argv[0]);
810 check_replace_refs = 0;
812 for (i = 1; i < argc; i++) {
817 if (!strcmp(arg, "--advertise-refs")) {
821 if (!strcmp(arg, "--stateless-rpc")) {
825 if (!strcmp(arg, "--strict")) {
829 if (starts_with(arg, "--timeout=")) {
830 timeout = atoi(arg+10);
834 if (!strcmp(arg, "--")) {
841 usage(upload_pack_usage);
847 if (!enter_repo(dir, strict))
848 die("'%s' does not appear to be a git repository", dir);
850 git_config(upload_pack_config, NULL);