11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
17 #include "argv-array.h"
18 #include "prio-queue.h"
20 #include "upload-pack.h"
22 /* Remember to update object flag allocation in object.h */
23 #define THEY_HAVE (1u << 11)
24 #define OUR_REF (1u << 12)
25 #define WANTED (1u << 13)
26 #define COMMON_KNOWN (1u << 14)
27 #define REACHABLE (1u << 15)
29 #define SHALLOW (1u << 16)
30 #define NOT_SHALLOW (1u << 17)
31 #define CLIENT_SHALLOW (1u << 18)
32 #define HIDDEN_REF (1u << 19)
34 static timestamp_t oldest_have
;
36 static int deepen_relative
;
39 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
40 static int no_progress
, daemon_mode
;
41 /* Allow specifying sha1 if it is a ref tip. */
42 #define ALLOW_TIP_SHA1 01
43 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
44 #define ALLOW_REACHABLE_SHA1 02
45 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
46 #define ALLOW_ANY_SHA1 07
47 static unsigned int allow_unadvertised_object_request
;
48 static int shallow_nr
;
49 static struct object_array have_obj
;
50 static struct object_array want_obj
;
51 static struct object_array extra_edge_obj
;
52 static unsigned int timeout
;
53 static int keepalive
= 5;
55 * otherwise maximum packet size (up to 65520 bytes).
57 static int use_sideband
;
58 static int stateless_rpc
;
59 static const char *pack_objects_hook
;
61 static void reset_timeout(void)
66 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
69 send_sideband(1, fd
, data
, sz
, use_sideband
);
76 /* XXX: are we happy to lose stuff here? */
80 write_or_die(fd
, data
, sz
);
83 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
86 if (graft
->nr_parent
== -1)
87 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
91 static void create_pack_file(void)
93 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
94 char data
[8193], progress
[128];
95 char abort_msg
[] = "aborting due to possible repository "
96 "corruption on the remote side.";
102 if (!pack_objects_hook
)
103 pack_objects
.git_cmd
= 1;
105 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
106 argv_array_push(&pack_objects
.args
, "git");
107 pack_objects
.use_shell
= 1;
111 argv_array_push(&pack_objects
.args
, "--shallow-file");
112 argv_array_push(&pack_objects
.args
, "");
114 argv_array_push(&pack_objects
.args
, "pack-objects");
115 argv_array_push(&pack_objects
.args
, "--revs");
117 argv_array_push(&pack_objects
.args
, "--thin");
119 argv_array_push(&pack_objects
.args
, "--stdout");
121 argv_array_push(&pack_objects
.args
, "--shallow");
123 argv_array_push(&pack_objects
.args
, "--progress");
125 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
127 argv_array_push(&pack_objects
.args
, "--include-tag");
129 pack_objects
.in
= -1;
130 pack_objects
.out
= -1;
131 pack_objects
.err
= -1;
133 if (start_command(&pack_objects
))
134 die("git upload-pack: unable to fork git-pack-objects");
136 pipe_fd
= xfdopen(pack_objects
.in
, "w");
139 for_each_commit_graft(write_one_shallow
, pipe_fd
);
141 for (i
= 0; i
< want_obj
.nr
; i
++)
142 fprintf(pipe_fd
, "%s\n",
143 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
144 fprintf(pipe_fd
, "--not\n");
145 for (i
= 0; i
< have_obj
.nr
; i
++)
146 fprintf(pipe_fd
, "%s\n",
147 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
148 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
149 fprintf(pipe_fd
, "%s\n",
150 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
151 fprintf(pipe_fd
, "\n");
155 /* We read from pack_objects.err to capture stderr output for
156 * progress bar, and pack_objects.out to capture the pack data.
160 struct pollfd pfd
[2];
161 int pe
, pu
, pollsize
;
169 if (0 <= pack_objects
.out
) {
170 pfd
[pollsize
].fd
= pack_objects
.out
;
171 pfd
[pollsize
].events
= POLLIN
;
175 if (0 <= pack_objects
.err
) {
176 pfd
[pollsize
].fd
= pack_objects
.err
;
177 pfd
[pollsize
].events
= POLLIN
;
185 ret
= poll(pfd
, pollsize
,
186 keepalive
< 0 ? -1 : 1000 * keepalive
);
189 if (errno
!= EINTR
) {
190 error_errno("poll failed, resuming");
195 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
196 /* Status ready; we ship that in the side-band
197 * or dump to the standard error.
199 sz
= xread(pack_objects
.err
, progress
,
202 send_client_data(2, progress
, sz
);
204 close(pack_objects
.err
);
205 pack_objects
.err
= -1;
209 /* give priority to status messages */
212 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
213 /* Data ready; we keep the last byte to ourselves
214 * in case we detect broken rev-list, so that we
215 * can leave the stream corrupted. This is
216 * unfortunate -- unpack-objects would happily
217 * accept a valid packdata with trailing garbage,
218 * so appending garbage after we pass all the
219 * pack data is not good enough to signal
220 * breakage to downstream.
228 sz
= xread(pack_objects
.out
, cp
,
229 sizeof(data
) - outsz
);
233 close(pack_objects
.out
);
234 pack_objects
.out
= -1;
240 buffered
= data
[sz
-1] & 0xFF;
245 send_client_data(1, data
, sz
);
249 * We hit the keepalive timeout without saying anything; send
250 * an empty message on the data sideband just to let the other
251 * side know we're still working on it, but don't have any data
254 * If we don't have a sideband channel, there's no room in the
255 * protocol to say anything, so those clients are just out of
258 if (!ret
&& use_sideband
) {
259 static const char buf
[] = "0005\1";
260 write_or_die(1, buf
, 5);
264 if (finish_command(&pack_objects
)) {
265 error("git upload-pack: git-pack-objects died with error.");
272 send_client_data(1, data
, 1);
273 fprintf(stderr
, "flushed.\n");
280 send_client_data(3, abort_msg
, sizeof(abort_msg
));
281 die("git upload-pack: %s", abort_msg
);
284 static int got_oid(const char *hex
, struct object_id
*oid
)
287 int we_knew_they_have
= 0;
289 if (get_oid_hex(hex
, oid
))
290 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
291 if (!has_object_file(oid
))
294 o
= parse_object(oid
);
296 die("oops (%s)", oid_to_hex(oid
));
297 if (o
->type
== OBJ_COMMIT
) {
298 struct commit_list
*parents
;
299 struct commit
*commit
= (struct commit
*)o
;
300 if (o
->flags
& THEY_HAVE
)
301 we_knew_they_have
= 1;
303 o
->flags
|= THEY_HAVE
;
304 if (!oldest_have
|| (commit
->date
< oldest_have
))
305 oldest_have
= commit
->date
;
306 for (parents
= commit
->parents
;
308 parents
= parents
->next
)
309 parents
->item
->object
.flags
|= THEY_HAVE
;
311 if (!we_knew_they_have
) {
312 add_object_array(o
, NULL
, &have_obj
);
318 static int reachable(struct commit
*want
)
320 struct prio_queue work
= { compare_commits_by_commit_date
};
322 prio_queue_put(&work
, want
);
324 struct commit_list
*list
;
325 struct commit
*commit
= prio_queue_get(&work
);
327 if (commit
->object
.flags
& THEY_HAVE
) {
328 want
->object
.flags
|= COMMON_KNOWN
;
331 if (!commit
->object
.parsed
)
332 parse_object(&commit
->object
.oid
);
333 if (commit
->object
.flags
& REACHABLE
)
335 commit
->object
.flags
|= REACHABLE
;
336 if (commit
->date
< oldest_have
)
338 for (list
= commit
->parents
; list
; list
= list
->next
) {
339 struct commit
*parent
= list
->item
;
340 if (!(parent
->object
.flags
& REACHABLE
))
341 prio_queue_put(&work
, parent
);
344 want
->object
.flags
|= REACHABLE
;
345 clear_commit_marks(want
, REACHABLE
);
346 clear_prio_queue(&work
);
347 return (want
->object
.flags
& COMMON_KNOWN
);
350 static int ok_to_give_up(void)
357 for (i
= 0; i
< want_obj
.nr
; i
++) {
358 struct object
*want
= want_obj
.objects
[i
].item
;
360 if (want
->flags
& COMMON_KNOWN
)
362 want
= deref_tag(want
, "a want line", 0);
363 if (!want
|| want
->type
!= OBJ_COMMIT
) {
364 /* no way to tell if this is reachable by
365 * looking at the ancestry chain alone, so
366 * leave a note to ourselves not to worry about
367 * this object anymore.
369 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
372 if (!reachable((struct commit
*)want
))
378 static int get_common_commits(void)
380 struct object_id oid
;
381 char last_hex
[GIT_MAX_HEXSZ
+ 1];
386 save_commit_buffer
= 0;
389 char *line
= packet_read_line(0, NULL
);
395 if (multi_ack
== 2 && got_common
396 && !got_other
&& ok_to_give_up()) {
398 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
400 if (have_obj
.nr
== 0 || multi_ack
)
401 packet_write_fmt(1, "NAK\n");
403 if (no_done
&& sent_ready
) {
404 packet_write_fmt(1, "ACK %s\n", last_hex
);
413 if (skip_prefix(line
, "have ", &arg
)) {
414 switch (got_oid(arg
, &oid
)) {
415 case -1: /* they have what we do not */
417 if (multi_ack
&& ok_to_give_up()) {
418 const char *hex
= oid_to_hex(&oid
);
419 if (multi_ack
== 2) {
421 packet_write_fmt(1, "ACK %s ready\n", hex
);
423 packet_write_fmt(1, "ACK %s continue\n", hex
);
428 memcpy(last_hex
, oid_to_hex(&oid
), 41);
430 packet_write_fmt(1, "ACK %s common\n", last_hex
);
432 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
433 else if (have_obj
.nr
== 1)
434 packet_write_fmt(1, "ACK %s\n", last_hex
);
439 if (!strcmp(line
, "done")) {
440 if (have_obj
.nr
> 0) {
442 packet_write_fmt(1, "ACK %s\n", last_hex
);
445 packet_write_fmt(1, "NAK\n");
448 die("git upload-pack: expected SHA1 list, got '%s'", line
);
452 static int is_our_ref(struct object
*o
)
454 int allow_hidden_ref
= (allow_unadvertised_object_request
&
455 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
456 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
460 * on successful case, it's up to the caller to close cmd->out
462 static int do_reachable_revlist(struct child_process
*cmd
,
463 struct object_array
*src
,
464 struct object_array
*reachable
)
466 static const char *argv
[] = {
467 "rev-list", "--stdin", NULL
,
470 char namebuf
[42]; /* ^ + SHA-1 + LF */
480 * If the next rev-list --stdin encounters an unknown commit,
481 * it terminates, which will cause SIGPIPE in the write loop
484 sigchain_push(SIGPIPE
, SIG_IGN
);
486 if (start_command(cmd
))
490 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
491 for (i
= get_max_object_index(); 0 < i
; ) {
492 o
= get_indexed_object(--i
);
495 if (reachable
&& o
->type
== OBJ_COMMIT
)
496 o
->flags
&= ~TMP_MARK
;
499 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
500 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
503 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
504 for (i
= 0; i
< src
->nr
; i
++) {
505 o
= src
->objects
[i
].item
;
508 add_object_array(o
, NULL
, reachable
);
511 if (reachable
&& o
->type
== OBJ_COMMIT
)
512 o
->flags
|= TMP_MARK
;
513 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
514 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
519 sigchain_pop(SIGPIPE
);
524 sigchain_pop(SIGPIPE
);
533 static int get_reachable_list(struct object_array
*src
,
534 struct object_array
*reachable
)
536 struct child_process cmd
= CHILD_PROCESS_INIT
;
539 char namebuf
[42]; /* ^ + SHA-1 + LF */
541 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
544 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
545 struct object_id sha1
;
547 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
550 o
= lookup_object(sha1
.hash
);
551 if (o
&& o
->type
== OBJ_COMMIT
) {
552 o
->flags
&= ~TMP_MARK
;
555 for (i
= get_max_object_index(); 0 < i
; i
--) {
556 o
= get_indexed_object(i
- 1);
557 if (o
&& o
->type
== OBJ_COMMIT
&&
558 (o
->flags
& TMP_MARK
)) {
559 add_object_array(o
, NULL
, reachable
);
560 o
->flags
&= ~TMP_MARK
;
565 if (finish_command(&cmd
))
571 static int has_unreachable(struct object_array
*src
)
573 struct child_process cmd
= CHILD_PROCESS_INIT
;
577 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
581 * The commits out of the rev-list are not ancestors of
584 i
= read_in_full(cmd
.out
, buf
, 1);
591 * rev-list may have died by encountering a bad commit
592 * in the history, in which case we do want to bail out
593 * even when it showed no commit.
595 if (finish_command(&cmd
))
598 /* All the non-tip ones are ancestors of what we advertised */
602 sigchain_pop(SIGPIPE
);
608 static void check_non_tip(void)
613 * In the normal in-process case without
614 * uploadpack.allowReachableSHA1InWant,
615 * non-tip requests can never happen.
617 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
619 if (!has_unreachable(&want_obj
))
620 /* All the non-tip ones are ancestors of what we advertised */
624 /* Pick one of them (we know there at least is one) */
625 for (i
= 0; i
< want_obj
.nr
; i
++) {
626 struct object
*o
= want_obj
.objects
[i
].item
;
628 die("git upload-pack: not our ref %s",
629 oid_to_hex(&o
->oid
));
633 static void send_shallow(struct commit_list
*result
)
636 struct object
*object
= &result
->item
->object
;
637 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
638 packet_write_fmt(1, "shallow %s",
639 oid_to_hex(&object
->oid
));
640 register_shallow(&object
->oid
);
643 result
= result
->next
;
647 static void send_unshallow(const struct object_array
*shallows
)
651 for (i
= 0; i
< shallows
->nr
; i
++) {
652 struct object
*object
= shallows
->objects
[i
].item
;
653 if (object
->flags
& NOT_SHALLOW
) {
654 struct commit_list
*parents
;
655 packet_write_fmt(1, "unshallow %s",
656 oid_to_hex(&object
->oid
));
657 object
->flags
&= ~CLIENT_SHALLOW
;
659 * We want to _register_ "object" as shallow, but we
660 * also need to traverse object's parents to deepen a
661 * shallow clone. Unregister it for now so we can
662 * parse and add the parents to the want list, then
665 unregister_shallow(&object
->oid
);
667 parse_commit_or_die((struct commit
*)object
);
668 parents
= ((struct commit
*)object
)->parents
;
670 add_object_array(&parents
->item
->object
,
672 parents
= parents
->next
;
674 add_object_array(object
, NULL
, &extra_edge_obj
);
676 /* make sure commit traversal conforms to client */
677 register_shallow(&object
->oid
);
681 static void deepen(int depth
, int deepen_relative
,
682 struct object_array
*shallows
)
684 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow()) {
687 for (i
= 0; i
< shallows
->nr
; i
++) {
688 struct object
*object
= shallows
->objects
[i
].item
;
689 object
->flags
|= NOT_SHALLOW
;
691 } else if (deepen_relative
) {
692 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
693 struct commit_list
*result
;
695 get_reachable_list(shallows
, &reachable_shallows
);
696 result
= get_shallow_commits(&reachable_shallows
,
698 SHALLOW
, NOT_SHALLOW
);
699 send_shallow(result
);
700 free_commit_list(result
);
701 object_array_clear(&reachable_shallows
);
703 struct commit_list
*result
;
705 result
= get_shallow_commits(&want_obj
, depth
,
706 SHALLOW
, NOT_SHALLOW
);
707 send_shallow(result
);
708 free_commit_list(result
);
711 send_unshallow(shallows
);
715 static void deepen_by_rev_list(int ac
, const char **av
,
716 struct object_array
*shallows
)
718 struct commit_list
*result
;
720 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
721 send_shallow(result
);
722 free_commit_list(result
);
723 send_unshallow(shallows
);
727 static int process_shallow(const char *line
, struct object_array
*shallows
)
730 if (skip_prefix(line
, "shallow ", &arg
)) {
731 struct object_id oid
;
732 struct object
*object
;
733 if (get_oid_hex(arg
, &oid
))
734 die("invalid shallow line: %s", line
);
735 object
= parse_object(&oid
);
738 if (object
->type
!= OBJ_COMMIT
)
739 die("invalid shallow object %s", oid_to_hex(&oid
));
740 if (!(object
->flags
& CLIENT_SHALLOW
)) {
741 object
->flags
|= CLIENT_SHALLOW
;
742 add_object_array(object
, NULL
, shallows
);
750 static int process_deepen(const char *line
, int *depth
)
753 if (skip_prefix(line
, "deepen ", &arg
)) {
755 *depth
= (int)strtol(arg
, &end
, 0);
756 if (!end
|| *end
|| *depth
<= 0)
757 die("Invalid deepen: %s", line
);
764 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
767 if (skip_prefix(line
, "deepen-since ", &arg
)) {
769 *deepen_since
= parse_timestamp(arg
, &end
, 0);
770 if (!end
|| *end
|| !deepen_since
||
771 /* revisions.c's max_age -1 is special */
773 die("Invalid deepen-since: %s", line
);
774 *deepen_rev_list
= 1;
780 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
783 if (skip_prefix(line
, "deepen-not ", &arg
)) {
785 struct object_id oid
;
786 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
787 die("git upload-pack: ambiguous deepen-not: %s", line
);
788 string_list_append(deepen_not
, ref
);
790 *deepen_rev_list
= 1;
796 static void receive_needs(void)
798 struct object_array shallows
= OBJECT_ARRAY_INIT
;
799 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
802 timestamp_t deepen_since
= 0;
803 int deepen_rev_list
= 0;
808 const char *features
;
809 struct object_id oid_buf
;
810 char *line
= packet_read_line(0, NULL
);
817 if (process_shallow(line
, &shallows
))
819 if (process_deepen(line
, &depth
))
821 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
823 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
826 if (!skip_prefix(line
, "want ", &arg
) ||
827 get_oid_hex(arg
, &oid_buf
))
828 die("git upload-pack: protocol error, "
829 "expected to get sha, not '%s'", line
);
833 if (parse_feature_request(features
, "deepen-relative"))
835 if (parse_feature_request(features
, "multi_ack_detailed"))
837 else if (parse_feature_request(features
, "multi_ack"))
839 if (parse_feature_request(features
, "no-done"))
841 if (parse_feature_request(features
, "thin-pack"))
843 if (parse_feature_request(features
, "ofs-delta"))
845 if (parse_feature_request(features
, "side-band-64k"))
846 use_sideband
= LARGE_PACKET_MAX
;
847 else if (parse_feature_request(features
, "side-band"))
848 use_sideband
= DEFAULT_PACKET_MAX
;
849 if (parse_feature_request(features
, "no-progress"))
851 if (parse_feature_request(features
, "include-tag"))
854 o
= parse_object(&oid_buf
);
857 "ERR upload-pack: not our ref %s",
858 oid_to_hex(&oid_buf
));
859 die("git upload-pack: not our ref %s",
860 oid_to_hex(&oid_buf
));
862 if (!(o
->flags
& WANTED
)) {
864 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
867 add_object_array(o
, NULL
, &want_obj
);
872 * We have sent all our refs already, and the other end
873 * should have chosen out of them. When we are operating
874 * in the stateless RPC mode, however, their choice may
875 * have been based on the set of older refs advertised
876 * by another process that handled the initial request.
881 if (!use_sideband
&& daemon_mode
)
884 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
886 if (depth
> 0 && deepen_rev_list
)
887 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
889 deepen(depth
, deepen_relative
, &shallows
);
890 else if (deepen_rev_list
) {
891 struct argv_array av
= ARGV_ARRAY_INIT
;
894 argv_array_push(&av
, "rev-list");
896 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
898 argv_array_push(&av
, "--not");
899 for (i
= 0; i
< deepen_not
.nr
; i
++) {
900 struct string_list_item
*s
= deepen_not
.items
+ i
;
901 argv_array_push(&av
, s
->string
);
903 argv_array_push(&av
, "--not");
905 for (i
= 0; i
< want_obj
.nr
; i
++) {
906 struct object
*o
= want_obj
.objects
[i
].item
;
907 argv_array_push(&av
, oid_to_hex(&o
->oid
));
909 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
910 argv_array_clear(&av
);
913 if (shallows
.nr
> 0) {
915 for (i
= 0; i
< shallows
.nr
; i
++)
916 register_shallow(&shallows
.objects
[i
].item
->oid
);
919 shallow_nr
+= shallows
.nr
;
920 object_array_clear(&shallows
);
923 /* return non-zero if the ref is hidden, otherwise 0 */
924 static int mark_our_ref(const char *refname
, const char *refname_full
,
925 const struct object_id
*oid
)
927 struct object
*o
= lookup_unknown_object(oid
->hash
);
929 if (ref_is_hidden(refname
, refname_full
)) {
930 o
->flags
|= HIDDEN_REF
;
937 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
938 int flag
, void *cb_data
)
940 const char *refname
= strip_namespace(refname_full
);
942 mark_our_ref(refname
, refname_full
, oid
);
946 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
948 struct string_list_item
*item
;
952 for_each_string_list_item(item
, symref
)
953 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
956 static int send_ref(const char *refname
, const struct object_id
*oid
,
957 int flag
, void *cb_data
)
959 static const char *capabilities
= "multi_ack thin-pack side-band"
960 " side-band-64k ofs-delta shallow deepen-since deepen-not"
961 " deepen-relative no-progress include-tag multi_ack_detailed";
962 const char *refname_nons
= strip_namespace(refname
);
963 struct object_id peeled
;
965 if (mark_our_ref(refname_nons
, refname
, oid
))
969 struct strbuf symref_info
= STRBUF_INIT
;
971 format_symref_info(&symref_info
, cb_data
);
972 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
973 oid_to_hex(oid
), refname_nons
,
975 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
976 " allow-tip-sha1-in-want" : "",
977 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
978 " allow-reachable-sha1-in-want" : "",
979 stateless_rpc
? " no-done" : "",
981 git_user_agent_sanitized());
982 strbuf_release(&symref_info
);
984 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
987 if (!peel_ref(refname
, &peeled
))
988 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
992 static int find_symref(const char *refname
, const struct object_id
*oid
,
993 int flag
, void *cb_data
)
995 const char *symref_target
;
996 struct string_list_item
*item
;
998 if ((flag
& REF_ISSYMREF
) == 0)
1000 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1001 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1002 die("'%s' is a symref but it is not?", refname
);
1003 item
= string_list_append(cb_data
, refname
);
1004 item
->util
= xstrdup(symref_target
);
1008 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1010 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1011 if (git_config_bool(var
, value
))
1012 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1014 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1015 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1016 if (git_config_bool(var
, value
))
1017 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1019 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1020 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1021 if (git_config_bool(var
, value
))
1022 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1024 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1025 } else if (!strcmp("uploadpack.keepalive", var
)) {
1026 keepalive
= git_config_int(var
, value
);
1029 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1030 if (!strcmp("uploadpack.packobjectshook", var
))
1031 return git_config_string(&pack_objects_hook
, var
, value
);
1033 return parse_hide_refs_config(var
, value
, "uploadpack");
1036 void upload_pack(struct upload_pack_options
*options
)
1038 struct string_list symref
= STRING_LIST_INIT_DUP
;
1040 stateless_rpc
= options
->stateless_rpc
;
1041 timeout
= options
->timeout
;
1042 daemon_mode
= options
->daemon_mode
;
1044 git_config(upload_pack_config
, NULL
);
1046 head_ref_namespaced(find_symref
, &symref
);
1048 if (options
->advertise_refs
|| !stateless_rpc
) {
1050 head_ref_namespaced(send_ref
, &symref
);
1051 for_each_namespaced_ref(send_ref
, &symref
);
1052 advertise_shallow_grafts(1);
1055 head_ref_namespaced(check_ref
, NULL
);
1056 for_each_namespaced_ref(check_ref
, NULL
);
1058 string_list_clear(&symref
, 1);
1059 if (options
->advertise_refs
)
1064 get_common_commits();