12 #include "list-objects.h"
13 #include "list-objects-filter.h"
14 #include "list-objects-filter-options.h"
15 #include "run-command.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
25 static const char * const upload_pack_usage[] = {
26 N_("git upload-pack [<options>] <dir>"),
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
35 #define REACHABLE (1u << 15)
37 #define SHALLOW (1u << 16)
38 #define NOT_SHALLOW (1u << 17)
39 #define CLIENT_SHALLOW (1u << 18)
40 #define HIDDEN_REF (1u << 19)
42 static timestamp_t oldest_have;
44 static int deepen_relative;
47 static int use_thin_pack, use_ofs_delta, use_include_tag;
48 static int no_progress, daemon_mode;
49 /* Allow specifying sha1 if it is a ref tip. */
50 #define ALLOW_TIP_SHA1 01
51 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52 #define ALLOW_REACHABLE_SHA1 02
53 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
54 #define ALLOW_ANY_SHA1 07
55 static unsigned int allow_unadvertised_object_request;
56 static int shallow_nr;
57 static struct object_array have_obj;
58 static struct object_array want_obj;
59 static struct object_array extra_edge_obj;
60 static unsigned int timeout;
61 static int keepalive = 5;
63 * otherwise maximum packet size (up to 65520 bytes).
65 static int use_sideband;
66 static int advertise_refs;
67 static int stateless_rpc;
68 static const char *pack_objects_hook;
70 static int filter_capability_requested;
71 static int filter_advertise;
72 static struct list_objects_filter_options filter_options;
74 static void reset_timeout(void)
79 static void send_client_data(int fd, const char *data, ssize_t sz)
82 send_sideband(1, fd, data, sz, use_sideband);
89 /* XXX: are we happy to lose stuff here? */
93 write_or_die(fd, data, sz);
96 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
99 if (graft->nr_parent == -1)
100 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
104 static void create_pack_file(void)
106 struct child_process pack_objects = CHILD_PROCESS_INIT;
107 char data[8193], progress[128];
108 char abort_msg[] = "aborting due to possible repository "
109 "corruption on the remote side.";
115 if (!pack_objects_hook)
116 pack_objects.git_cmd = 1;
118 argv_array_push(&pack_objects.args, pack_objects_hook);
119 argv_array_push(&pack_objects.args, "git");
120 pack_objects.use_shell = 1;
124 argv_array_push(&pack_objects.args, "--shallow-file");
125 argv_array_push(&pack_objects.args, "");
127 argv_array_push(&pack_objects.args, "pack-objects");
128 argv_array_push(&pack_objects.args, "--revs");
130 argv_array_push(&pack_objects.args, "--thin");
132 argv_array_push(&pack_objects.args, "--stdout");
134 argv_array_push(&pack_objects.args, "--shallow");
136 argv_array_push(&pack_objects.args, "--progress");
138 argv_array_push(&pack_objects.args, "--delta-base-offset");
140 argv_array_push(&pack_objects.args, "--include-tag");
141 if (filter_options.filter_spec) {
142 struct strbuf buf = STRBUF_INIT;
143 sq_quote_buf(&buf, filter_options.filter_spec);
144 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
145 strbuf_release(&buf);
148 pack_objects.in = -1;
149 pack_objects.out = -1;
150 pack_objects.err = -1;
152 if (start_command(&pack_objects))
153 die("git upload-pack: unable to fork git-pack-objects");
155 pipe_fd = xfdopen(pack_objects.in, "w");
158 for_each_commit_graft(write_one_shallow, pipe_fd);
160 for (i = 0; i < want_obj.nr; i++)
161 fprintf(pipe_fd, "%s\n",
162 oid_to_hex(&want_obj.objects[i].item->oid));
163 fprintf(pipe_fd, "--not\n");
164 for (i = 0; i < have_obj.nr; i++)
165 fprintf(pipe_fd, "%s\n",
166 oid_to_hex(&have_obj.objects[i].item->oid));
167 for (i = 0; i < extra_edge_obj.nr; i++)
168 fprintf(pipe_fd, "%s\n",
169 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
170 fprintf(pipe_fd, "\n");
174 /* We read from pack_objects.err to capture stderr output for
175 * progress bar, and pack_objects.out to capture the pack data.
179 struct pollfd pfd[2];
180 int pe, pu, pollsize;
188 if (0 <= pack_objects.out) {
189 pfd[pollsize].fd = pack_objects.out;
190 pfd[pollsize].events = POLLIN;
194 if (0 <= pack_objects.err) {
195 pfd[pollsize].fd = pack_objects.err;
196 pfd[pollsize].events = POLLIN;
204 ret = poll(pfd, pollsize,
205 keepalive < 0 ? -1 : 1000 * keepalive);
208 if (errno != EINTR) {
209 error_errno("poll failed, resuming");
214 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
215 /* Status ready; we ship that in the side-band
216 * or dump to the standard error.
218 sz = xread(pack_objects.err, progress,
221 send_client_data(2, progress, sz);
223 close(pack_objects.err);
224 pack_objects.err = -1;
228 /* give priority to status messages */
231 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
232 /* Data ready; we keep the last byte to ourselves
233 * in case we detect broken rev-list, so that we
234 * can leave the stream corrupted. This is
235 * unfortunate -- unpack-objects would happily
236 * accept a valid packdata with trailing garbage,
237 * so appending garbage after we pass all the
238 * pack data is not good enough to signal
239 * breakage to downstream.
247 sz = xread(pack_objects.out, cp,
248 sizeof(data) - outsz);
252 close(pack_objects.out);
253 pack_objects.out = -1;
259 buffered = data[sz-1] & 0xFF;
264 send_client_data(1, data, sz);
268 * We hit the keepalive timeout without saying anything; send
269 * an empty message on the data sideband just to let the other
270 * side know we're still working on it, but don't have any data
273 * If we don't have a sideband channel, there's no room in the
274 * protocol to say anything, so those clients are just out of
277 if (!ret && use_sideband) {
278 static const char buf[] = "0005\1";
279 write_or_die(1, buf, 5);
283 if (finish_command(&pack_objects)) {
284 error("git upload-pack: git-pack-objects died with error.");
291 send_client_data(1, data, 1);
292 fprintf(stderr, "flushed.\n");
299 send_client_data(3, abort_msg, sizeof(abort_msg));
300 die("git upload-pack: %s", abort_msg);
303 static int got_oid(const char *hex, struct object_id *oid)
306 int we_knew_they_have = 0;
308 if (get_oid_hex(hex, oid))
309 die("git upload-pack: expected SHA1 object, got '%s'", hex);
310 if (!has_object_file(oid))
313 o = parse_object(oid);
315 die("oops (%s)", oid_to_hex(oid));
316 if (o->type == OBJ_COMMIT) {
317 struct commit_list *parents;
318 struct commit *commit = (struct commit *)o;
319 if (o->flags & THEY_HAVE)
320 we_knew_they_have = 1;
322 o->flags |= THEY_HAVE;
323 if (!oldest_have || (commit->date < oldest_have))
324 oldest_have = commit->date;
325 for (parents = commit->parents;
327 parents = parents->next)
328 parents->item->object.flags |= THEY_HAVE;
330 if (!we_knew_they_have) {
331 add_object_array(o, NULL, &have_obj);
337 static int reachable(struct commit *want)
339 struct prio_queue work = { compare_commits_by_commit_date };
341 prio_queue_put(&work, want);
343 struct commit_list *list;
344 struct commit *commit = prio_queue_get(&work);
346 if (commit->object.flags & THEY_HAVE) {
347 want->object.flags |= COMMON_KNOWN;
350 if (!commit->object.parsed)
351 parse_object(&commit->object.oid);
352 if (commit->object.flags & REACHABLE)
354 commit->object.flags |= REACHABLE;
355 if (commit->date < oldest_have)
357 for (list = commit->parents; list; list = list->next) {
358 struct commit *parent = list->item;
359 if (!(parent->object.flags & REACHABLE))
360 prio_queue_put(&work, parent);
363 want->object.flags |= REACHABLE;
364 clear_commit_marks(want, REACHABLE);
365 clear_prio_queue(&work);
366 return (want->object.flags & COMMON_KNOWN);
369 static int ok_to_give_up(void)
376 for (i = 0; i < want_obj.nr; i++) {
377 struct object *want = want_obj.objects[i].item;
379 if (want->flags & COMMON_KNOWN)
381 want = deref_tag(want, "a want line", 0);
382 if (!want || want->type != OBJ_COMMIT) {
383 /* no way to tell if this is reachable by
384 * looking at the ancestry chain alone, so
385 * leave a note to ourselves not to worry about
386 * this object anymore.
388 want_obj.objects[i].item->flags |= COMMON_KNOWN;
391 if (!reachable((struct commit *)want))
397 static int get_common_commits(void)
399 struct object_id oid;
400 char last_hex[GIT_MAX_HEXSZ + 1];
405 save_commit_buffer = 0;
408 char *line = packet_read_line(0, NULL);
414 if (multi_ack == 2 && got_common
415 && !got_other && ok_to_give_up()) {
417 packet_write_fmt(1, "ACK %s ready\n", last_hex);
419 if (have_obj.nr == 0 || multi_ack)
420 packet_write_fmt(1, "NAK\n");
422 if (no_done && sent_ready) {
423 packet_write_fmt(1, "ACK %s\n", last_hex);
432 if (skip_prefix(line, "have ", &arg)) {
433 switch (got_oid(arg, &oid)) {
434 case -1: /* they have what we do not */
436 if (multi_ack && ok_to_give_up()) {
437 const char *hex = oid_to_hex(&oid);
438 if (multi_ack == 2) {
440 packet_write_fmt(1, "ACK %s ready\n", hex);
442 packet_write_fmt(1, "ACK %s continue\n", hex);
447 memcpy(last_hex, oid_to_hex(&oid), 41);
449 packet_write_fmt(1, "ACK %s common\n", last_hex);
451 packet_write_fmt(1, "ACK %s continue\n", last_hex);
452 else if (have_obj.nr == 1)
453 packet_write_fmt(1, "ACK %s\n", last_hex);
458 if (!strcmp(line, "done")) {
459 if (have_obj.nr > 0) {
461 packet_write_fmt(1, "ACK %s\n", last_hex);
464 packet_write_fmt(1, "NAK\n");
467 die("git upload-pack: expected SHA1 list, got '%s'", line);
471 static int is_our_ref(struct object *o)
473 int allow_hidden_ref = (allow_unadvertised_object_request &
474 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
475 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
479 * on successful case, it's up to the caller to close cmd->out
481 static int do_reachable_revlist(struct child_process *cmd,
482 struct object_array *src,
483 struct object_array *reachable)
485 static const char *argv[] = {
486 "rev-list", "--stdin", NULL,
489 char namebuf[42]; /* ^ + SHA-1 + LF */
499 * If the next rev-list --stdin encounters an unknown commit,
500 * it terminates, which will cause SIGPIPE in the write loop
503 sigchain_push(SIGPIPE, SIG_IGN);
505 if (start_command(cmd))
509 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
510 for (i = get_max_object_index(); 0 < i; ) {
511 o = get_indexed_object(--i);
514 if (reachable && o->type == OBJ_COMMIT)
515 o->flags &= ~TMP_MARK;
518 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
519 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
522 namebuf[GIT_SHA1_HEXSZ] = '\n';
523 for (i = 0; i < src->nr; i++) {
524 o = src->objects[i].item;
527 add_object_array(o, NULL, reachable);
530 if (reachable && o->type == OBJ_COMMIT)
531 o->flags |= TMP_MARK;
532 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
533 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
538 sigchain_pop(SIGPIPE);
543 sigchain_pop(SIGPIPE);
552 static int get_reachable_list(struct object_array *src,
553 struct object_array *reachable)
555 struct child_process cmd = CHILD_PROCESS_INIT;
558 char namebuf[42]; /* ^ + SHA-1 + LF */
560 if (do_reachable_revlist(&cmd, src, reachable) < 0)
563 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
564 struct object_id sha1;
566 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
569 o = lookup_object(sha1.hash);
570 if (o && o->type == OBJ_COMMIT) {
571 o->flags &= ~TMP_MARK;
574 for (i = get_max_object_index(); 0 < i; i--) {
575 o = get_indexed_object(i - 1);
576 if (o && o->type == OBJ_COMMIT &&
577 (o->flags & TMP_MARK)) {
578 add_object_array(o, NULL, reachable);
579 o->flags &= ~TMP_MARK;
584 if (finish_command(&cmd))
590 static int has_unreachable(struct object_array *src)
592 struct child_process cmd = CHILD_PROCESS_INIT;
596 if (do_reachable_revlist(&cmd, src, NULL) < 0)
600 * The commits out of the rev-list are not ancestors of
603 i = read_in_full(cmd.out, buf, 1);
610 * rev-list may have died by encountering a bad commit
611 * in the history, in which case we do want to bail out
612 * even when it showed no commit.
614 if (finish_command(&cmd))
617 /* All the non-tip ones are ancestors of what we advertised */
621 sigchain_pop(SIGPIPE);
627 static void check_non_tip(void)
632 * In the normal in-process case without
633 * uploadpack.allowReachableSHA1InWant,
634 * non-tip requests can never happen.
636 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
638 if (!has_unreachable(&want_obj))
639 /* All the non-tip ones are ancestors of what we advertised */
643 /* Pick one of them (we know there at least is one) */
644 for (i = 0; i < want_obj.nr; i++) {
645 struct object *o = want_obj.objects[i].item;
647 die("git upload-pack: not our ref %s",
648 oid_to_hex(&o->oid));
652 static void send_shallow(struct commit_list *result)
655 struct object *object = &result->item->object;
656 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
657 packet_write_fmt(1, "shallow %s",
658 oid_to_hex(&object->oid));
659 register_shallow(&object->oid);
662 result = result->next;
666 static void send_unshallow(const struct object_array *shallows)
670 for (i = 0; i < shallows->nr; i++) {
671 struct object *object = shallows->objects[i].item;
672 if (object->flags & NOT_SHALLOW) {
673 struct commit_list *parents;
674 packet_write_fmt(1, "unshallow %s",
675 oid_to_hex(&object->oid));
676 object->flags &= ~CLIENT_SHALLOW;
678 * We want to _register_ "object" as shallow, but we
679 * also need to traverse object's parents to deepen a
680 * shallow clone. Unregister it for now so we can
681 * parse and add the parents to the want list, then
684 unregister_shallow(&object->oid);
686 parse_commit_or_die((struct commit *)object);
687 parents = ((struct commit *)object)->parents;
689 add_object_array(&parents->item->object,
691 parents = parents->next;
693 add_object_array(object, NULL, &extra_edge_obj);
695 /* make sure commit traversal conforms to client */
696 register_shallow(&object->oid);
700 static void deepen(int depth, int deepen_relative,
701 struct object_array *shallows)
703 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
706 for (i = 0; i < shallows->nr; i++) {
707 struct object *object = shallows->objects[i].item;
708 object->flags |= NOT_SHALLOW;
710 } else if (deepen_relative) {
711 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
712 struct commit_list *result;
714 get_reachable_list(shallows, &reachable_shallows);
715 result = get_shallow_commits(&reachable_shallows,
717 SHALLOW, NOT_SHALLOW);
718 send_shallow(result);
719 free_commit_list(result);
720 object_array_clear(&reachable_shallows);
722 struct commit_list *result;
724 result = get_shallow_commits(&want_obj, depth,
725 SHALLOW, NOT_SHALLOW);
726 send_shallow(result);
727 free_commit_list(result);
730 send_unshallow(shallows);
734 static void deepen_by_rev_list(int ac, const char **av,
735 struct object_array *shallows)
737 struct commit_list *result;
739 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
740 send_shallow(result);
741 free_commit_list(result);
742 send_unshallow(shallows);
746 static void receive_needs(void)
748 struct object_array shallows = OBJECT_ARRAY_INIT;
749 struct string_list deepen_not = STRING_LIST_INIT_DUP;
752 timestamp_t deepen_since = 0;
753 int deepen_rev_list = 0;
758 const char *features;
759 struct object_id oid_buf;
760 char *line = packet_read_line(0, NULL);
767 if (skip_prefix(line, "shallow ", &arg)) {
768 struct object_id oid;
769 struct object *object;
770 if (get_oid_hex(arg, &oid))
771 die("invalid shallow line: %s", line);
772 object = parse_object(&oid);
775 if (object->type != OBJ_COMMIT)
776 die("invalid shallow object %s", oid_to_hex(&oid));
777 if (!(object->flags & CLIENT_SHALLOW)) {
778 object->flags |= CLIENT_SHALLOW;
779 add_object_array(object, NULL, &shallows);
783 if (skip_prefix(line, "deepen ", &arg)) {
785 depth = strtol(arg, &end, 0);
786 if (!end || *end || depth <= 0)
787 die("Invalid deepen: %s", line);
790 if (skip_prefix(line, "deepen-since ", &arg)) {
792 deepen_since = parse_timestamp(arg, &end, 0);
793 if (!end || *end || !deepen_since ||
794 /* revisions.c's max_age -1 is special */
796 die("Invalid deepen-since: %s", line);
800 if (skip_prefix(line, "deepen-not ", &arg)) {
802 struct object_id oid;
803 if (expand_ref(arg, strlen(arg), oid.hash, &ref) != 1)
804 die("git upload-pack: ambiguous deepen-not: %s", line);
805 string_list_append(&deepen_not, ref);
810 if (skip_prefix(line, "filter ", &arg)) {
811 if (!filter_capability_requested)
812 die("git upload-pack: filtering capability not negotiated");
813 parse_list_objects_filter(&filter_options, arg);
816 if (!skip_prefix(line, "want ", &arg) ||
817 get_oid_hex(arg, &oid_buf))
818 die("git upload-pack: protocol error, "
819 "expected to get sha, not '%s'", line);
823 if (parse_feature_request(features, "deepen-relative"))
825 if (parse_feature_request(features, "multi_ack_detailed"))
827 else if (parse_feature_request(features, "multi_ack"))
829 if (parse_feature_request(features, "no-done"))
831 if (parse_feature_request(features, "thin-pack"))
833 if (parse_feature_request(features, "ofs-delta"))
835 if (parse_feature_request(features, "side-band-64k"))
836 use_sideband = LARGE_PACKET_MAX;
837 else if (parse_feature_request(features, "side-band"))
838 use_sideband = DEFAULT_PACKET_MAX;
839 if (parse_feature_request(features, "no-progress"))
841 if (parse_feature_request(features, "include-tag"))
843 if (parse_feature_request(features, "filter"))
844 filter_capability_requested = 1;
846 o = parse_object(&oid_buf);
849 "ERR upload-pack: not our ref %s",
850 oid_to_hex(&oid_buf));
851 die("git upload-pack: not our ref %s",
852 oid_to_hex(&oid_buf));
854 if (!(o->flags & WANTED)) {
856 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
859 add_object_array(o, NULL, &want_obj);
864 * We have sent all our refs already, and the other end
865 * should have chosen out of them. When we are operating
866 * in the stateless RPC mode, however, their choice may
867 * have been based on the set of older refs advertised
868 * by another process that handled the initial request.
873 if (!use_sideband && daemon_mode)
876 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
878 if (depth > 0 && deepen_rev_list)
879 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
881 deepen(depth, deepen_relative, &shallows);
882 else if (deepen_rev_list) {
883 struct argv_array av = ARGV_ARRAY_INIT;
886 argv_array_push(&av, "rev-list");
888 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
890 argv_array_push(&av, "--not");
891 for (i = 0; i < deepen_not.nr; i++) {
892 struct string_list_item *s = deepen_not.items + i;
893 argv_array_push(&av, s->string);
895 argv_array_push(&av, "--not");
897 for (i = 0; i < want_obj.nr; i++) {
898 struct object *o = want_obj.objects[i].item;
899 argv_array_push(&av, oid_to_hex(&o->oid));
901 deepen_by_rev_list(av.argc, av.argv, &shallows);
902 argv_array_clear(&av);
905 if (shallows.nr > 0) {
907 for (i = 0; i < shallows.nr; i++)
908 register_shallow(&shallows.objects[i].item->oid);
911 shallow_nr += shallows.nr;
912 object_array_clear(&shallows);
915 /* return non-zero if the ref is hidden, otherwise 0 */
916 static int mark_our_ref(const char *refname, const char *refname_full,
917 const struct object_id *oid)
919 struct object *o = lookup_unknown_object(oid->hash);
921 if (ref_is_hidden(refname, refname_full)) {
922 o->flags |= HIDDEN_REF;
929 static int check_ref(const char *refname_full, const struct object_id *oid,
930 int flag, void *cb_data)
932 const char *refname = strip_namespace(refname_full);
934 mark_our_ref(refname, refname_full, oid);
938 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
940 struct string_list_item *item;
944 for_each_string_list_item(item, symref)
945 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
948 static int send_ref(const char *refname, const struct object_id *oid,
949 int flag, void *cb_data)
951 static const char *capabilities = "multi_ack thin-pack side-band"
952 " side-band-64k ofs-delta shallow deepen-since deepen-not"
953 " deepen-relative no-progress include-tag multi_ack_detailed";
954 const char *refname_nons = strip_namespace(refname);
955 struct object_id peeled;
957 if (mark_our_ref(refname_nons, refname, oid))
961 struct strbuf symref_info = STRBUF_INIT;
963 format_symref_info(&symref_info, cb_data);
964 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
965 oid_to_hex(oid), refname_nons,
967 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
968 " allow-tip-sha1-in-want" : "",
969 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
970 " allow-reachable-sha1-in-want" : "",
971 stateless_rpc ? " no-done" : "",
973 filter_advertise ? " filter" : "",
974 git_user_agent_sanitized());
975 strbuf_release(&symref_info);
977 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
980 if (!peel_ref(refname, peeled.hash))
981 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
985 static int find_symref(const char *refname, const struct object_id *oid,
986 int flag, void *cb_data)
988 const char *symref_target;
989 struct string_list_item *item;
991 if ((flag & REF_ISSYMREF) == 0)
993 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
994 if (!symref_target || (flag & REF_ISSYMREF) == 0)
995 die("'%s' is a symref but it is not?", refname);
996 item = string_list_append(cb_data, refname);
997 item->util = xstrdup(symref_target);
1001 static void upload_pack(void)
1003 struct string_list symref = STRING_LIST_INIT_DUP;
1005 head_ref_namespaced(find_symref, &symref);
1007 if (advertise_refs || !stateless_rpc) {
1009 head_ref_namespaced(send_ref, &symref);
1010 for_each_namespaced_ref(send_ref, &symref);
1011 advertise_shallow_grafts(1);
1014 head_ref_namespaced(check_ref, NULL);
1015 for_each_namespaced_ref(check_ref, NULL);
1017 string_list_clear(&symref, 1);
1023 get_common_commits();
1028 static int upload_pack_config(const char *var, const char *value, void *unused)
1030 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1031 if (git_config_bool(var, value))
1032 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1034 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1035 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1036 if (git_config_bool(var, value))
1037 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1039 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1040 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1041 if (git_config_bool(var, value))
1042 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1044 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1045 } else if (!strcmp("uploadpack.keepalive", var)) {
1046 keepalive = git_config_int(var, value);
1049 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1050 if (!strcmp("uploadpack.packobjectshook", var))
1051 return git_config_string(&pack_objects_hook, var, value);
1052 } else if (!strcmp("uploadpack.allowfilter", var)) {
1053 filter_advertise = git_config_bool(var, value);
1055 return parse_hide_refs_config(var, value, "uploadpack");
1058 int cmd_main(int argc, const char **argv)
1062 struct option options[] = {
1063 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1064 N_("quit after a single request/response exchange")),
1065 OPT_BOOL(0, "advertise-refs", &advertise_refs,
1066 N_("exit immediately after initial ref advertisement")),
1067 OPT_BOOL(0, "strict", &strict,
1068 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1069 OPT_INTEGER(0, "timeout", &timeout,
1070 N_("interrupt transfer after <n> seconds of inactivity")),
1074 packet_trace_identity("upload-pack");
1075 check_replace_refs = 0;
1077 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1080 usage_with_options(upload_pack_usage, options);
1089 if (!enter_repo(dir, strict))
1090 die("'%s' does not appear to be a git repository", dir);
1092 git_config(upload_pack_config, NULL);