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)
29 #define HIDDEN_REF (1u << 19)
31 static unsigned long oldest_have;
35 static int use_thin_pack, use_ofs_delta, use_include_tag;
36 static int no_progress, daemon_mode;
37 static int allow_tip_sha1_in_want;
38 static int shallow_nr;
39 static struct object_array have_obj;
40 static struct object_array want_obj;
41 static struct object_array extra_edge_obj;
42 static unsigned int timeout;
44 * otherwise maximum packet size (up to 65520 bytes).
46 static int use_sideband;
47 static int advertise_refs;
48 static int stateless_rpc;
50 static void reset_timeout(void)
55 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
58 return send_sideband(1, fd, data, sz, use_sideband);
63 /* XXX: are we happy to lose stuff here? */
67 write_or_die(fd, data, sz);
71 static void create_pack_file(void)
73 struct child_process pack_objects;
74 char data[8193], progress[128];
75 char abort_msg[] = "aborting due to possible repository "
76 "corruption on the remote side.";
82 char *shallow_file = NULL;
85 shallow_file = setup_temporary_shallow();
86 argv[arg++] = "--shallow-file";
87 argv[arg++] = shallow_file;
89 argv[arg++] = "pack-objects";
90 argv[arg++] = "--revs";
92 argv[arg++] = "--thin";
94 argv[arg++] = "--stdout";
96 argv[arg++] = "--progress";
98 argv[arg++] = "--delta-base-offset";
100 argv[arg++] = "--include-tag";
103 memset(&pack_objects, 0, sizeof(pack_objects));
104 pack_objects.in = -1;
105 pack_objects.out = -1;
106 pack_objects.err = -1;
107 pack_objects.git_cmd = 1;
108 pack_objects.argv = argv;
110 if (start_command(&pack_objects))
111 die("git upload-pack: unable to fork git-pack-objects");
113 pipe_fd = xfdopen(pack_objects.in, "w");
115 for (i = 0; i < want_obj.nr; i++)
116 fprintf(pipe_fd, "%s\n",
117 sha1_to_hex(want_obj.objects[i].item->sha1));
118 fprintf(pipe_fd, "--not\n");
119 for (i = 0; i < have_obj.nr; i++)
120 fprintf(pipe_fd, "%s\n",
121 sha1_to_hex(have_obj.objects[i].item->sha1));
122 for (i = 0; i < extra_edge_obj.nr; i++)
123 fprintf(pipe_fd, "%s\n",
124 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
125 fprintf(pipe_fd, "\n");
129 /* We read from pack_objects.err to capture stderr output for
130 * progress bar, and pack_objects.out to capture the pack data.
134 struct pollfd pfd[2];
135 int pe, pu, pollsize;
142 if (0 <= pack_objects.out) {
143 pfd[pollsize].fd = pack_objects.out;
144 pfd[pollsize].events = POLLIN;
148 if (0 <= pack_objects.err) {
149 pfd[pollsize].fd = pack_objects.err;
150 pfd[pollsize].events = POLLIN;
158 if (poll(pfd, pollsize, -1) < 0) {
159 if (errno != EINTR) {
160 error("poll failed, resuming: %s",
166 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
167 /* Status ready; we ship that in the side-band
168 * or dump to the standard error.
170 sz = xread(pack_objects.err, progress,
173 send_client_data(2, progress, sz);
175 close(pack_objects.err);
176 pack_objects.err = -1;
180 /* give priority to status messages */
183 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
184 /* Data ready; we keep the last byte to ourselves
185 * in case we detect broken rev-list, so that we
186 * can leave the stream corrupted. This is
187 * unfortunate -- unpack-objects would happily
188 * accept a valid packdata with trailing garbage,
189 * so appending garbage after we pass all the
190 * pack data is not good enough to signal
191 * breakage to downstream.
199 sz = xread(pack_objects.out, cp,
200 sizeof(data) - outsz);
204 close(pack_objects.out);
205 pack_objects.out = -1;
211 buffered = data[sz-1] & 0xFF;
216 sz = send_client_data(1, data, sz);
222 if (finish_command(&pack_objects)) {
223 error("git upload-pack: git-pack-objects died with error.");
228 unlink(shallow_file);
235 sz = send_client_data(1, data, 1);
238 fprintf(stderr, "flushed.\n");
245 send_client_data(3, abort_msg, sizeof(abort_msg));
246 die("git upload-pack: %s", abort_msg);
249 static int got_sha1(char *hex, unsigned char *sha1)
252 int we_knew_they_have = 0;
254 if (get_sha1_hex(hex, sha1))
255 die("git upload-pack: expected SHA1 object, got '%s'", hex);
256 if (!has_sha1_file(sha1))
259 o = parse_object(sha1);
261 die("oops (%s)", sha1_to_hex(sha1));
262 if (o->type == OBJ_COMMIT) {
263 struct commit_list *parents;
264 struct commit *commit = (struct commit *)o;
265 if (o->flags & THEY_HAVE)
266 we_knew_they_have = 1;
268 o->flags |= THEY_HAVE;
269 if (!oldest_have || (commit->date < oldest_have))
270 oldest_have = commit->date;
271 for (parents = commit->parents;
273 parents = parents->next)
274 parents->item->object.flags |= THEY_HAVE;
276 if (!we_knew_they_have) {
277 add_object_array(o, NULL, &have_obj);
283 static int reachable(struct commit *want)
285 struct commit_list *work = NULL;
287 commit_list_insert_by_date(want, &work);
289 struct commit_list *list = work->next;
290 struct commit *commit = work->item;
294 if (commit->object.flags & THEY_HAVE) {
295 want->object.flags |= COMMON_KNOWN;
298 if (!commit->object.parsed)
299 parse_object(commit->object.sha1);
300 if (commit->object.flags & REACHABLE)
302 commit->object.flags |= REACHABLE;
303 if (commit->date < oldest_have)
305 for (list = commit->parents; list; list = list->next) {
306 struct commit *parent = list->item;
307 if (!(parent->object.flags & REACHABLE))
308 commit_list_insert_by_date(parent, &work);
311 want->object.flags |= REACHABLE;
312 clear_commit_marks(want, REACHABLE);
313 free_commit_list(work);
314 return (want->object.flags & COMMON_KNOWN);
317 static int ok_to_give_up(void)
324 for (i = 0; i < want_obj.nr; i++) {
325 struct object *want = want_obj.objects[i].item;
327 if (want->flags & COMMON_KNOWN)
329 want = deref_tag(want, "a want line", 0);
330 if (!want || want->type != OBJ_COMMIT) {
331 /* no way to tell if this is reachable by
332 * looking at the ancestry chain alone, so
333 * leave a note to ourselves not to worry about
334 * this object anymore.
336 want_obj.objects[i].item->flags |= COMMON_KNOWN;
339 if (!reachable((struct commit *)want))
345 static int get_common_commits(void)
347 unsigned char sha1[20];
353 save_commit_buffer = 0;
356 char *line = packet_read_line(0, NULL);
360 if (multi_ack == 2 && got_common
361 && !got_other && ok_to_give_up()) {
363 packet_write(1, "ACK %s ready\n", last_hex);
365 if (have_obj.nr == 0 || multi_ack)
366 packet_write(1, "NAK\n");
368 if (no_done && sent_ready) {
369 packet_write(1, "ACK %s\n", last_hex);
378 if (!prefixcmp(line, "have ")) {
379 switch (got_sha1(line+5, sha1)) {
380 case -1: /* they have what we do not */
382 if (multi_ack && ok_to_give_up()) {
383 const char *hex = sha1_to_hex(sha1);
384 if (multi_ack == 2) {
386 packet_write(1, "ACK %s ready\n", hex);
388 packet_write(1, "ACK %s continue\n", hex);
393 memcpy(last_hex, sha1_to_hex(sha1), 41);
395 packet_write(1, "ACK %s common\n", last_hex);
397 packet_write(1, "ACK %s continue\n", last_hex);
398 else if (have_obj.nr == 1)
399 packet_write(1, "ACK %s\n", last_hex);
404 if (!strcmp(line, "done")) {
405 if (have_obj.nr > 0) {
407 packet_write(1, "ACK %s\n", last_hex);
410 packet_write(1, "NAK\n");
413 die("git upload-pack: expected SHA1 list, got '%s'", line);
417 static int is_our_ref(struct object *o)
420 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
423 static void check_non_tip(void)
425 static const char *argv[] = {
426 "rev-list", "--stdin", NULL,
428 static struct child_process cmd;
430 char namebuf[42]; /* ^ + SHA-1 + LF */
433 /* In the normal in-process case non-tip request can never happen */
443 if (start_command(&cmd))
447 * If rev-list --stdin encounters an unknown commit, it
448 * terminates, which will cause SIGPIPE in the write loop
451 sigchain_push(SIGPIPE, SIG_IGN);
455 for (i = get_max_object_index(); 0 < i; ) {
456 o = get_indexed_object(--i);
461 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
462 if (write_in_full(cmd.in, namebuf, 42) < 0)
466 for (i = 0; i < want_obj.nr; i++) {
467 o = want_obj.objects[i].item;
470 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
471 if (write_in_full(cmd.in, namebuf, 41) < 0)
476 sigchain_pop(SIGPIPE);
479 * The commits out of the rev-list are not ancestors of
482 i = read_in_full(cmd.out, namebuf, 1);
488 * rev-list may have died by encountering a bad commit
489 * in the history, in which case we do want to bail out
490 * even when it showed no commit.
492 if (finish_command(&cmd))
495 /* All the non-tip ones are ancestors of what we advertised */
499 /* Pick one of them (we know there at least is one) */
500 for (i = 0; i < want_obj.nr; i++) {
501 o = want_obj.objects[i].item;
503 die("git upload-pack: not our ref %s",
504 sha1_to_hex(o->sha1));
508 static void receive_needs(void)
510 struct object_array shallows = OBJECT_ARRAY_INIT;
517 const char *features;
518 unsigned char sha1_buf[20];
519 char *line = packet_read_line(0, NULL);
524 if (!prefixcmp(line, "shallow ")) {
525 unsigned char sha1[20];
526 struct object *object;
527 if (get_sha1_hex(line + 8, sha1))
528 die("invalid shallow line: %s", line);
529 object = parse_object(sha1);
532 if (object->type != OBJ_COMMIT)
533 die("invalid shallow object %s", sha1_to_hex(sha1));
534 if (!(object->flags & CLIENT_SHALLOW)) {
535 object->flags |= CLIENT_SHALLOW;
536 add_object_array(object, NULL, &shallows);
540 if (!prefixcmp(line, "deepen ")) {
542 depth = strtol(line + 7, &end, 0);
543 if (end == line + 7 || depth <= 0)
544 die("Invalid deepen: %s", line);
547 if (prefixcmp(line, "want ") ||
548 get_sha1_hex(line+5, sha1_buf))
549 die("git upload-pack: protocol error, "
550 "expected to get sha, not '%s'", line);
552 features = line + 45;
554 if (parse_feature_request(features, "multi_ack_detailed"))
556 else if (parse_feature_request(features, "multi_ack"))
558 if (parse_feature_request(features, "no-done"))
560 if (parse_feature_request(features, "thin-pack"))
562 if (parse_feature_request(features, "ofs-delta"))
564 if (parse_feature_request(features, "side-band-64k"))
565 use_sideband = LARGE_PACKET_MAX;
566 else if (parse_feature_request(features, "side-band"))
567 use_sideband = DEFAULT_PACKET_MAX;
568 if (parse_feature_request(features, "no-progress"))
570 if (parse_feature_request(features, "include-tag"))
573 o = parse_object(sha1_buf);
575 die("git upload-pack: not our ref %s",
576 sha1_to_hex(sha1_buf));
577 if (!(o->flags & WANTED)) {
581 add_object_array(o, NULL, &want_obj);
586 * We have sent all our refs already, and the other end
587 * should have chosen out of them. When we are operating
588 * in the stateless RPC mode, however, their choice may
589 * have been based on the set of older refs advertised
590 * by another process that handled the initial request.
595 if (!use_sideband && daemon_mode)
598 if (depth == 0 && shallows.nr == 0)
601 struct commit_list *result = NULL, *backup = NULL;
603 if (depth == INFINITE_DEPTH)
604 for (i = 0; i < shallows.nr; i++) {
605 struct object *object = shallows.objects[i].item;
606 object->flags |= NOT_SHALLOW;
610 get_shallow_commits(&want_obj, depth,
611 SHALLOW, NOT_SHALLOW);
613 struct object *object = &result->item->object;
614 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
615 packet_write(1, "shallow %s",
616 sha1_to_hex(object->sha1));
617 register_shallow(object->sha1);
620 result = result->next;
622 free_commit_list(backup);
623 for (i = 0; i < shallows.nr; i++) {
624 struct object *object = shallows.objects[i].item;
625 if (object->flags & NOT_SHALLOW) {
626 struct commit_list *parents;
627 packet_write(1, "unshallow %s",
628 sha1_to_hex(object->sha1));
629 object->flags &= ~CLIENT_SHALLOW;
630 /* make sure the real parents are parsed */
631 unregister_shallow(object->sha1);
633 if (parse_commit((struct commit *)object))
634 die("invalid commit");
635 parents = ((struct commit *)object)->parents;
637 add_object_array(&parents->item->object,
639 parents = parents->next;
641 add_object_array(object, NULL, &extra_edge_obj);
643 /* make sure commit traversal conforms to client */
644 register_shallow(object->sha1);
648 if (shallows.nr > 0) {
650 for (i = 0; i < shallows.nr; i++)
651 register_shallow(shallows.objects[i].item->sha1);
654 shallow_nr += shallows.nr;
655 free(shallows.objects);
658 /* return non-zero if the ref is hidden, otherwise 0 */
659 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
661 struct object *o = lookup_unknown_object(sha1);
663 if (ref_is_hidden(refname)) {
664 o->flags |= HIDDEN_REF;
668 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
673 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
675 static const char *capabilities = "multi_ack thin-pack side-band"
676 " side-band-64k ofs-delta shallow no-progress"
677 " include-tag multi_ack_detailed";
678 const char *refname_nons = strip_namespace(refname);
679 unsigned char peeled[20];
681 if (mark_our_ref(refname, sha1, flag, cb_data))
685 packet_write(1, "%s %s%c%s%s%s agent=%s\n",
686 sha1_to_hex(sha1), refname_nons,
688 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
689 stateless_rpc ? " no-done" : "",
690 git_user_agent_sanitized());
692 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
694 if (!peel_ref(refname, peeled))
695 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
699 static void upload_pack(void)
701 if (advertise_refs || !stateless_rpc) {
703 head_ref_namespaced(send_ref, NULL);
704 for_each_namespaced_ref(send_ref, NULL);
707 head_ref_namespaced(mark_our_ref, NULL);
708 for_each_namespaced_ref(mark_our_ref, NULL);
715 get_common_commits();
720 static int upload_pack_config(const char *var, const char *value, void *unused)
722 if (!strcmp("uploadpack.allowtipsha1inwant", var))
723 allow_tip_sha1_in_want = git_config_bool(var, value);
724 return parse_hide_refs_config(var, value, "uploadpack");
727 int main(int argc, char **argv)
735 packet_trace_identity("upload-pack");
736 git_extract_argv0_path(argv[0]);
737 read_replace_refs = 0;
739 for (i = 1; i < argc; i++) {
744 if (!strcmp(arg, "--advertise-refs")) {
748 if (!strcmp(arg, "--stateless-rpc")) {
752 if (!strcmp(arg, "--strict")) {
756 if (!prefixcmp(arg, "--timeout=")) {
757 timeout = atoi(arg+10);
761 if (!strcmp(arg, "--")) {
768 usage(upload_pack_usage);
774 if (!enter_repo(dir, strict))
775 die("'%s' does not appear to be a git repository", dir);
776 if (is_repository_shallow())
777 die("attempt to fetch/clone from a shallow repository");
778 git_config(upload_pack_config, NULL);