1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
6 #include "environment.h"
12 #include "repository.h"
14 #include "oid-array.h"
19 #include "list-objects-filter-options.h"
20 #include "run-command.h"
24 #include "string-list.h"
28 #include "upload-pack.h"
29 #include "commit-graph.h"
30 #include "commit-reach.h"
32 #include "write-or-die.h"
33 #include "json-writer.h"
35 #include "promisor-remote.h"
37 /* Remember to update object flag allocation in object.h */
38 #define THEY_HAVE (1u << 11)
39 #define OUR_REF (1u << 12)
40 #define WANTED (1u << 13)
41 #define COMMON_KNOWN (1u << 14)
43 #define SHALLOW (1u << 16)
44 #define NOT_SHALLOW (1u << 17)
45 #define CLIENT_SHALLOW (1u << 18)
46 #define HIDDEN_REF (1u << 19)
48 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
49 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
51 /* Enum for allowed unadvertised object request (UOR) */
53 /* Allow specifying sha1 if it is a ref tip. */
54 ALLOW_TIP_SHA1
= 0x01,
55 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56 ALLOW_REACHABLE_SHA1
= 0x02,
57 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
62 * Please annotate, and if possible group together, fields used only
63 * for protocol v0 or only for protocol v2.
65 struct upload_pack_data
{
66 struct string_list symref
; /* v0 only */
67 struct object_array want_obj
;
68 struct object_array have_obj
;
69 struct strmap wanted_refs
; /* v2 only */
70 struct strvec hidden_refs
;
72 struct object_array shallows
;
73 struct oidset deepen_not
;
74 struct object_array extra_edge_obj
;
76 timestamp_t deepen_since
;
81 timestamp_t oldest_have
;
83 unsigned int timeout
; /* v0 only */
87 MULTI_ACK_DETAILED
= 2
88 } multi_ack
; /* v0 only */
90 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
93 struct string_list uri_protocols
;
94 enum allow_uor allow_uor
;
96 struct list_objects_filter_options filter_options
;
97 struct string_list allowed_filters
;
99 struct packet_writer writer
;
101 char *pack_objects_hook
;
103 unsigned stateless_rpc
: 1; /* v0 only */
104 unsigned no_done
: 1; /* v0 only */
105 unsigned daemon_mode
: 1; /* v0 only */
106 unsigned filter_capability_requested
: 1; /* v0 only */
108 unsigned use_thin_pack
: 1;
109 unsigned use_ofs_delta
: 1;
110 unsigned no_progress
: 1;
111 unsigned use_include_tag
: 1;
112 unsigned wait_for_done
: 1;
113 unsigned allow_filter
: 1;
114 unsigned allow_filter_fallback
: 1;
115 unsigned long tree_filter_max_depth
;
117 unsigned done
: 1; /* v2 only */
118 unsigned allow_ref_in_want
: 1; /* v2 only */
119 unsigned allow_sideband_all
: 1; /* v2 only */
120 unsigned seen_haves
: 1; /* v2 only */
121 unsigned allow_packfile_uris
: 1; /* v2 only */
122 unsigned advertise_sid
: 1;
123 unsigned sent_capabilities
: 1;
126 static void upload_pack_data_init(struct upload_pack_data
*data
)
128 struct string_list symref
= STRING_LIST_INIT_DUP
;
129 struct strmap wanted_refs
= STRMAP_INIT
;
130 struct strvec hidden_refs
= STRVEC_INIT
;
131 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
132 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
133 struct object_array shallows
= OBJECT_ARRAY_INIT
;
134 struct oidset deepen_not
= OID_ARRAY_INIT
;
135 struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
136 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
137 struct string_list allowed_filters
= STRING_LIST_INIT_DUP
;
139 memset(data
, 0, sizeof(*data
));
140 data
->symref
= symref
;
141 data
->wanted_refs
= wanted_refs
;
142 data
->hidden_refs
= hidden_refs
;
143 data
->want_obj
= want_obj
;
144 data
->have_obj
= have_obj
;
145 data
->shallows
= shallows
;
146 data
->deepen_not
= deepen_not
;
147 data
->uri_protocols
= uri_protocols
;
148 data
->extra_edge_obj
= extra_edge_obj
;
149 data
->allowed_filters
= allowed_filters
;
150 data
->allow_filter_fallback
= 1;
151 data
->tree_filter_max_depth
= ULONG_MAX
;
152 packet_writer_init(&data
->writer
, 1);
153 list_objects_filter_init(&data
->filter_options
);
156 data
->advertise_sid
= 0;
159 static void upload_pack_data_clear(struct upload_pack_data
*data
)
161 string_list_clear(&data
->symref
, 1);
162 strmap_clear(&data
->wanted_refs
, 1);
163 strvec_clear(&data
->hidden_refs
);
164 object_array_clear(&data
->want_obj
);
165 object_array_clear(&data
->have_obj
);
166 object_array_clear(&data
->shallows
);
167 oidset_clear(&data
->deepen_not
);
168 object_array_clear(&data
->extra_edge_obj
);
169 list_objects_filter_release(&data
->filter_options
);
170 string_list_clear(&data
->allowed_filters
, 0);
171 string_list_clear(&data
->uri_protocols
, 0);
173 free((char *)data
->pack_objects_hook
);
176 static void reset_timeout(unsigned int timeout
)
181 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
185 send_sideband(1, fd
, data
, sz
, use_sideband
);
192 /* XXX: are we happy to lose stuff here? */
193 xwrite(fd
, data
, sz
);
196 write_or_die(fd
, data
, sz
);
199 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
202 if (graft
->nr_parent
== -1)
203 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
207 struct output_state
{
209 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
210 * sideband-64k the band designator takes up 1 byte of space. Because
211 * relay_pack_data keeps the last byte to itself, we make the buffer 1
212 * byte bigger than the intended maximum write size.
214 char buffer
[(LARGE_PACKET_DATA_MAX
- 1) + 1];
216 unsigned packfile_uris_started
: 1;
217 unsigned packfile_started
: 1;
220 static int relay_pack_data(int pack_objects_out
, struct output_state
*os
,
221 int use_sideband
, int write_packfile_line
)
224 * We keep the last byte to ourselves
225 * in case we detect broken rev-list, so that we
226 * can leave the stream corrupted. This is
227 * unfortunate -- unpack-objects would happily
228 * accept a valid packdata with trailing garbage,
229 * so appending garbage after we pass all the
230 * pack data is not good enough to signal
231 * breakage to downstream.
235 readsz
= xread(pack_objects_out
, os
->buffer
+ os
->used
,
236 sizeof(os
->buffer
) - os
->used
);
242 while (!os
->packfile_started
) {
244 if (os
->used
>= 4 && !memcmp(os
->buffer
, "PACK", 4)) {
245 os
->packfile_started
= 1;
246 if (write_packfile_line
) {
247 if (os
->packfile_uris_started
)
249 packet_write_fmt(1, "\1packfile\n");
253 if ((p
= memchr(os
->buffer
, '\n', os
->used
))) {
254 if (!os
->packfile_uris_started
) {
255 os
->packfile_uris_started
= 1;
256 if (!write_packfile_line
)
257 BUG("packfile_uris requires sideband-all");
258 packet_write_fmt(1, "\1packfile-uris\n");
261 packet_write_fmt(1, "\1%s\n", os
->buffer
);
263 os
->used
-= p
- os
->buffer
+ 1;
264 memmove(os
->buffer
, p
+ 1, os
->used
);
274 send_client_data(1, os
->buffer
, os
->used
- 1, use_sideband
);
275 os
->buffer
[0] = os
->buffer
[os
->used
- 1];
278 send_client_data(1, os
->buffer
, os
->used
, use_sideband
);
285 static void create_pack_file(struct upload_pack_data
*pack_data
,
286 const struct string_list
*uri_protocols
)
288 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
289 struct output_state
*output_state
= xcalloc(1, sizeof(struct output_state
));
291 char abort_msg
[] = "aborting due to possible repository "
292 "corruption on the remote side.";
297 if (!pack_data
->pack_objects_hook
)
298 pack_objects
.git_cmd
= 1;
300 strvec_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
301 strvec_push(&pack_objects
.args
, "git");
302 pack_objects
.use_shell
= 1;
305 if (pack_data
->shallow_nr
) {
306 strvec_push(&pack_objects
.args
, "--shallow-file");
307 strvec_push(&pack_objects
.args
, "");
309 strvec_push(&pack_objects
.args
, "pack-objects");
310 strvec_push(&pack_objects
.args
, "--revs");
311 if (pack_data
->use_thin_pack
)
312 strvec_push(&pack_objects
.args
, "--thin");
314 strvec_push(&pack_objects
.args
, "--stdout");
315 if (pack_data
->shallow_nr
)
316 strvec_push(&pack_objects
.args
, "--shallow");
317 if (!pack_data
->no_progress
)
318 strvec_push(&pack_objects
.args
, "--progress");
319 if (pack_data
->use_ofs_delta
)
320 strvec_push(&pack_objects
.args
, "--delta-base-offset");
321 if (pack_data
->use_include_tag
)
322 strvec_push(&pack_objects
.args
, "--include-tag");
323 if (repo_has_accepted_promisor_remote(the_repository
))
324 strvec_push(&pack_objects
.args
, "--missing=allow-promisor");
325 if (pack_data
->filter_options
.choice
) {
327 expand_list_objects_filter_spec(&pack_data
->filter_options
);
328 strvec_pushf(&pack_objects
.args
, "--filter=%s", spec
);
331 for (i
= 0; i
< uri_protocols
->nr
; i
++)
332 strvec_pushf(&pack_objects
.args
, "--uri-protocol=%s",
333 uri_protocols
->items
[i
].string
);
336 pack_objects
.in
= -1;
337 pack_objects
.out
= -1;
338 pack_objects
.err
= -1;
339 pack_objects
.clean_on_exit
= 1;
341 if (start_command(&pack_objects
))
342 die("git upload-pack: unable to fork git-pack-objects");
344 pipe_fd
= xfdopen(pack_objects
.in
, "w");
346 if (pack_data
->shallow_nr
)
347 for_each_commit_graft(write_one_shallow
, pipe_fd
);
349 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
350 fprintf(pipe_fd
, "%s\n",
351 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
352 fprintf(pipe_fd
, "--not\n");
353 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
354 fprintf(pipe_fd
, "%s\n",
355 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
356 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
357 fprintf(pipe_fd
, "%s\n",
358 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
359 fprintf(pipe_fd
, "\n");
363 /* We read from pack_objects.err to capture stderr output for
364 * progress bar, and pack_objects.out to capture the pack data.
368 struct pollfd pfd
[2];
369 int pe
, pu
, pollsize
, polltimeout
;
372 reset_timeout(pack_data
->timeout
);
377 if (0 <= pack_objects
.out
) {
378 pfd
[pollsize
].fd
= pack_objects
.out
;
379 pfd
[pollsize
].events
= POLLIN
;
383 if (0 <= pack_objects
.err
) {
384 pfd
[pollsize
].fd
= pack_objects
.err
;
385 pfd
[pollsize
].events
= POLLIN
;
393 polltimeout
= pack_data
->keepalive
< 0
395 : 1000 * pack_data
->keepalive
;
397 ret
= poll(pfd
, pollsize
, polltimeout
);
400 if (errno
!= EINTR
) {
401 error_errno("poll failed, resuming");
406 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
407 /* Status ready; we ship that in the side-band
408 * or dump to the standard error.
410 sz
= xread(pack_objects
.err
, progress
,
413 send_client_data(2, progress
, sz
,
414 pack_data
->use_sideband
);
416 close(pack_objects
.err
);
417 pack_objects
.err
= -1;
421 /* give priority to status messages */
424 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
425 int result
= relay_pack_data(pack_objects
.out
,
427 pack_data
->use_sideband
,
431 close(pack_objects
.out
);
432 pack_objects
.out
= -1;
433 } else if (result
< 0) {
439 * We hit the keepalive timeout without saying anything; send
440 * an empty message on the data sideband just to let the other
441 * side know we're still working on it, but don't have any data
444 * If we don't have a sideband channel, there's no room in the
445 * protocol to say anything, so those clients are just out of
448 if (!ret
&& pack_data
->use_sideband
) {
449 static const char buf
[] = "0005\1";
450 write_or_die(1, buf
, 5);
454 if (finish_command(&pack_objects
)) {
455 error("git upload-pack: git-pack-objects died with error.");
460 if (output_state
->used
> 0) {
461 send_client_data(1, output_state
->buffer
, output_state
->used
,
462 pack_data
->use_sideband
);
463 fprintf(stderr
, "flushed.\n");
466 if (pack_data
->use_sideband
)
472 send_client_data(3, abort_msg
, strlen(abort_msg
),
473 pack_data
->use_sideband
);
474 die("git upload-pack: %s", abort_msg
);
477 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
479 int we_knew_they_have
= 0;
480 struct object
*o
= parse_object_with_flags(the_repository
, oid
,
481 PARSE_OBJECT_SKIP_HASH_CHECK
|
482 PARSE_OBJECT_DISCARD_TREE
);
485 die("oops (%s)", oid_to_hex(oid
));
486 if (o
->type
== OBJ_COMMIT
) {
487 struct commit_list
*parents
;
488 struct commit
*commit
= (struct commit
*)o
;
489 if (o
->flags
& THEY_HAVE
)
490 we_knew_they_have
= 1;
492 o
->flags
|= THEY_HAVE
;
493 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
494 data
->oldest_have
= commit
->date
;
495 for (parents
= commit
->parents
;
497 parents
= parents
->next
)
498 parents
->item
->object
.flags
|= THEY_HAVE
;
500 if (!we_knew_they_have
) {
501 add_object_array(o
, NULL
, &data
->have_obj
);
507 static int got_oid(struct upload_pack_data
*data
,
508 const char *hex
, struct object_id
*oid
)
510 if (get_oid_hex(hex
, oid
))
511 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
512 if (!odb_has_object(the_repository
->objects
, oid
, 0))
514 return do_got_oid(data
, oid
);
517 static int ok_to_give_up(struct upload_pack_data
*data
)
519 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
521 if (!data
->have_obj
.nr
)
524 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
525 COMMON_KNOWN
, data
->oldest_have
,
529 static int get_common_commits(struct upload_pack_data
*data
,
530 struct packet_reader
*reader
)
532 struct object_id oid
;
533 char last_hex
[GIT_MAX_HEXSZ
+ 1];
541 reset_timeout(data
->timeout
);
543 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
544 if (data
->multi_ack
== MULTI_ACK_DETAILED
547 && ok_to_give_up(data
)) {
549 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
551 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
552 packet_write_fmt(1, "NAK\n");
554 if (data
->no_done
&& sent_ready
) {
555 packet_write_fmt(1, "ACK %s\n", last_hex
);
558 if (data
->stateless_rpc
)
564 if (skip_prefix(reader
->line
, "have ", &arg
)) {
565 switch (got_oid(data
, arg
, &oid
)) {
566 case -1: /* they have what we do not */
569 && ok_to_give_up(data
)) {
570 const char *hex
= oid_to_hex(&oid
);
571 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
573 packet_write_fmt(1, "ACK %s ready\n", hex
);
575 packet_write_fmt(1, "ACK %s continue\n", hex
);
580 oid_to_hex_r(last_hex
, &oid
);
581 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
582 packet_write_fmt(1, "ACK %s common\n", last_hex
);
583 else if (data
->multi_ack
)
584 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
585 else if (data
->have_obj
.nr
== 1)
586 packet_write_fmt(1, "ACK %s\n", last_hex
);
591 if (!strcmp(reader
->line
, "done")) {
592 if (data
->have_obj
.nr
> 0) {
594 packet_write_fmt(1, "ACK %s\n", last_hex
);
597 packet_write_fmt(1, "NAK\n");
600 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
604 static int allow_hidden_refs(enum allow_uor allow_uor
)
606 if ((allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
)
608 return !(allow_uor
& (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
611 static void for_each_namespaced_ref_1(each_ref_fn fn
,
612 struct upload_pack_data
*data
)
614 const char **excludes
= NULL
;
616 * If `data->allow_uor` allows fetching hidden refs, we need to
617 * mark all references (including hidden ones), to check in
618 * `is_our_ref()` below.
620 * Otherwise, we only care about whether each reference's object
621 * has the OUR_REF bit set or not, so do not need to visit
624 if (allow_hidden_refs(data
->allow_uor
))
625 excludes
= hidden_refs_to_excludes(&data
->hidden_refs
);
627 refs_for_each_namespaced_ref(get_main_ref_store(the_repository
),
632 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
634 return o
->flags
& ((allow_hidden_refs(allow_uor
) ? 0 : HIDDEN_REF
) | OUR_REF
);
638 * on successful case, it's up to the caller to close cmd->out
640 static int do_reachable_revlist(struct child_process
*cmd
,
641 struct object_array
*src
,
642 struct object_array
*reachable
,
643 enum allow_uor allow_uor
)
649 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
656 * If the next rev-list --stdin encounters an unknown commit,
657 * it terminates, which will cause SIGPIPE in the write loop
660 sigchain_push(SIGPIPE
, SIG_IGN
);
662 if (start_command(cmd
))
665 cmd_in
= xfdopen(cmd
->in
, "w");
667 for (i
= get_max_object_index(the_repository
); 0 < i
; ) {
668 o
= get_indexed_object(the_repository
, --i
);
671 if (reachable
&& o
->type
== OBJ_COMMIT
)
672 o
->flags
&= ~TMP_MARK
;
673 if (!is_our_ref(o
, allow_uor
))
675 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
678 for (i
= 0; i
< src
->nr
; i
++) {
679 o
= src
->objects
[i
].item
;
680 if (is_our_ref(o
, allow_uor
)) {
682 add_object_array(o
, NULL
, reachable
);
685 if (reachable
&& o
->type
== OBJ_COMMIT
)
686 o
->flags
|= TMP_MARK
;
687 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
690 if (ferror(cmd_in
) || fflush(cmd_in
))
694 sigchain_pop(SIGPIPE
);
699 sigchain_pop(SIGPIPE
);
708 static int get_reachable_list(struct upload_pack_data
*data
,
709 struct object_array
*reachable
)
711 struct child_process cmd
= CHILD_PROCESS_INIT
;
714 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
715 const unsigned hexsz
= the_hash_algo
->hexsz
;
718 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
719 data
->allow_uor
) < 0) {
724 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
725 struct object_id oid
;
728 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
731 o
= lookup_object(the_repository
, &oid
);
732 if (o
&& o
->type
== OBJ_COMMIT
) {
733 o
->flags
&= ~TMP_MARK
;
736 for (i
= get_max_object_index(the_repository
); 0 < i
; i
--) {
737 o
= get_indexed_object(the_repository
, i
- 1);
738 if (o
&& o
->type
== OBJ_COMMIT
&&
739 (o
->flags
& TMP_MARK
)) {
740 add_object_array(o
, NULL
, reachable
);
741 o
->flags
&= ~TMP_MARK
;
746 if (finish_command(&cmd
)) {
754 child_process_clear(&cmd
);
758 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
760 struct child_process cmd
= CHILD_PROCESS_INIT
;
764 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
768 * The commits out of the rev-list are not ancestors of
771 i
= read_in_full(cmd
.out
, buf
, 1);
778 * rev-list may have died by encountering a bad commit
779 * in the history, in which case we do want to bail out
780 * even when it showed no commit.
782 if (finish_command(&cmd
))
785 /* All the non-tip ones are ancestors of what we advertised */
791 child_process_clear(&cmd
);
795 static void check_non_tip(struct upload_pack_data
*data
)
800 * In the normal in-process case without
801 * uploadpack.allowReachableSHA1InWant,
802 * non-tip requests can never happen.
804 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
806 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
807 /* All the non-tip ones are ancestors of what we advertised */
811 /* Pick one of them (we know there at least is one) */
812 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
813 struct object
*o
= data
->want_obj
.objects
[i
].item
;
814 if (!is_our_ref(o
, data
->allow_uor
)) {
815 error("git upload-pack: not our ref %s",
816 oid_to_hex(&o
->oid
));
817 packet_writer_error(&data
->writer
,
818 "upload-pack: not our ref %s",
819 oid_to_hex(&o
->oid
));
825 static void send_shallow(struct upload_pack_data
*data
,
826 struct commit_list
*result
)
829 struct object
*object
= &result
->item
->object
;
830 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
831 packet_writer_write(&data
->writer
, "shallow %s",
832 oid_to_hex(&object
->oid
));
833 register_shallow(the_repository
, &object
->oid
);
836 result
= result
->next
;
840 static void send_unshallow(struct upload_pack_data
*data
)
844 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
845 struct object
*object
= data
->shallows
.objects
[i
].item
;
846 if (object
->flags
& NOT_SHALLOW
) {
847 struct commit_list
*parents
;
848 packet_writer_write(&data
->writer
, "unshallow %s",
849 oid_to_hex(&object
->oid
));
850 object
->flags
&= ~CLIENT_SHALLOW
;
852 * We want to _register_ "object" as shallow, but we
853 * also need to traverse object's parents to deepen a
854 * shallow clone. Unregister it for now so we can
855 * parse and add the parents to the want list, then
858 unregister_shallow(&object
->oid
);
860 parse_commit_or_die((struct commit
*)object
);
861 parents
= ((struct commit
*)object
)->parents
;
863 add_object_array(&parents
->item
->object
,
864 NULL
, &data
->want_obj
);
865 parents
= parents
->next
;
867 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
869 /* make sure commit traversal conforms to client */
870 register_shallow(the_repository
, &object
->oid
);
874 static int check_ref(const char *refname_full
, const char *referent UNUSED
, const struct object_id
*oid
,
875 int flag
, void *cb_data
);
876 static void deepen(struct upload_pack_data
*data
, int depth
)
878 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
881 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
882 struct object
*object
= data
->shallows
.objects
[i
].item
;
883 object
->flags
|= NOT_SHALLOW
;
885 } else if (data
->deepen_relative
) {
886 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
887 struct commit_list
*result
;
890 * Checking for reachable shallows requires that our refs be
891 * marked with OUR_REF.
893 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
895 for_each_namespaced_ref_1(check_ref
, data
);
897 get_reachable_list(data
, &reachable_shallows
);
898 result
= get_shallow_commits(&reachable_shallows
,
900 SHALLOW
, NOT_SHALLOW
);
901 send_shallow(data
, result
);
902 free_commit_list(result
);
903 object_array_clear(&reachable_shallows
);
905 struct commit_list
*result
;
907 result
= get_shallow_commits(&data
->want_obj
, depth
,
908 SHALLOW
, NOT_SHALLOW
);
909 send_shallow(data
, result
);
910 free_commit_list(result
);
913 send_unshallow(data
);
916 static void deepen_by_rev_list(struct upload_pack_data
*data
,
920 struct commit_list
*result
;
922 disable_commit_graph(the_repository
);
923 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
924 send_shallow(data
, result
);
925 free_commit_list(result
);
926 send_unshallow(data
);
929 /* Returns 1 if a shallow list is sent or 0 otherwise */
930 static int send_shallow_list(struct upload_pack_data
*data
)
934 if (data
->depth
> 0 && data
->deepen_rev_list
)
935 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
936 if (data
->depth
> 0) {
937 deepen(data
, data
->depth
);
939 } else if (data
->deepen_rev_list
) {
940 struct strvec av
= STRVEC_INIT
;
943 strvec_push(&av
, "rev-list");
944 if (data
->deepen_since
)
945 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
946 if (oidset_size(&data
->deepen_not
)) {
947 const struct object_id
*oid
;
948 struct oidset_iter iter
;
949 strvec_push(&av
, "--not");
950 oidset_iter_init(&data
->deepen_not
, &iter
);
951 while ((oid
= oidset_iter_next(&iter
)))
952 strvec_push(&av
, oid_to_hex(oid
));
953 strvec_push(&av
, "--not");
955 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
956 struct object
*o
= data
->want_obj
.objects
[i
].item
;
957 strvec_push(&av
, oid_to_hex(&o
->oid
));
959 deepen_by_rev_list(data
, av
.nr
, av
.v
);
963 if (data
->shallows
.nr
> 0) {
965 for (i
= 0; i
< data
->shallows
.nr
; i
++)
966 register_shallow(the_repository
,
967 &data
->shallows
.objects
[i
].item
->oid
);
971 data
->shallow_nr
+= data
->shallows
.nr
;
975 static int process_shallow(const char *line
, struct object_array
*shallows
)
978 if (skip_prefix(line
, "shallow ", &arg
)) {
979 struct object_id oid
;
980 struct object
*object
;
981 if (get_oid_hex(arg
, &oid
))
982 die("invalid shallow line: %s", line
);
983 object
= parse_object(the_repository
, &oid
);
986 if (object
->type
!= OBJ_COMMIT
)
987 die("invalid shallow object %s", oid_to_hex(&oid
));
988 if (!(object
->flags
& CLIENT_SHALLOW
)) {
989 object
->flags
|= CLIENT_SHALLOW
;
990 add_object_array(object
, NULL
, shallows
);
998 static int process_deepen(const char *line
, int *depth
)
1001 if (skip_prefix(line
, "deepen ", &arg
)) {
1003 *depth
= (int)strtol(arg
, &end
, 0);
1004 if (!end
|| *end
|| *depth
<= 0)
1005 die("Invalid deepen: %s", line
);
1012 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
1015 if (skip_prefix(line
, "deepen-since ", &arg
)) {
1017 *deepen_since
= parse_timestamp(arg
, &end
, 0);
1018 if (!end
|| *end
|| !deepen_since
||
1019 /* revisions.c's max_age -1 is special */
1020 *deepen_since
== -1)
1021 die("Invalid deepen-since: %s", line
);
1022 *deepen_rev_list
= 1;
1028 static int process_deepen_not(const char *line
, struct oidset
*deepen_not
, int *deepen_rev_list
)
1031 if (skip_prefix(line
, "deepen-not ", &arg
)) {
1034 struct object_id oid
;
1035 cnt
= expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
);
1037 die("git upload-pack: ambiguous deepen-not: %s", line
);
1039 die("git upload-pack: deepen-not is not a ref: %s", line
);
1040 oidset_insert(deepen_not
, &oid
);
1042 *deepen_rev_list
= 1;
1048 NORETURN
__attribute__((format(printf
,2,3)))
1049 static void send_err_and_die(struct upload_pack_data
*data
,
1050 const char *fmt
, ...)
1052 struct strbuf buf
= STRBUF_INIT
;
1056 strbuf_vaddf(&buf
, fmt
, ap
);
1059 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1063 static void check_one_filter(struct upload_pack_data
*data
,
1064 struct list_objects_filter_options
*opts
)
1066 const char *key
= list_object_filter_config_name(opts
->choice
);
1067 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1072 allowed
= (intptr_t)item
->util
;
1074 allowed
= data
->allow_filter_fallback
;
1077 send_err_and_die(data
, "filter '%s' not supported", key
);
1079 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1080 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1081 send_err_and_die(data
,
1082 "tree filter allows max depth %lu, but got %lu",
1083 data
->tree_filter_max_depth
,
1084 opts
->tree_exclude_depth
);
1087 static void check_filter_recurse(struct upload_pack_data
*data
,
1088 struct list_objects_filter_options
*opts
)
1092 check_one_filter(data
, opts
);
1093 if (opts
->choice
!= LOFC_COMBINE
)
1096 for (i
= 0; i
< opts
->sub_nr
; i
++)
1097 check_filter_recurse(data
, &opts
->sub
[i
]);
1100 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1102 check_filter_recurse(data
, &data
->filter_options
);
1105 static void receive_needs(struct upload_pack_data
*data
,
1106 struct packet_reader
*reader
)
1108 int has_non_tip
= 0;
1110 data
->shallow_nr
= 0;
1113 const char *features
;
1114 struct object_id oid_buf
;
1118 reset_timeout(data
->timeout
);
1119 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1122 if (process_shallow(reader
->line
, &data
->shallows
))
1124 if (process_deepen(reader
->line
, &data
->depth
))
1126 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1128 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1131 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1132 if (!data
->filter_capability_requested
)
1133 die("git upload-pack: filtering capability not negotiated");
1134 list_objects_filter_die_if_populated(&data
->filter_options
);
1135 parse_list_objects_filter(&data
->filter_options
, arg
);
1136 die_if_using_banned_filter(data
);
1140 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1141 parse_oid_hex(arg
, &oid_buf
, &features
))
1142 die("git upload-pack: protocol error, "
1143 "expected to get object ID, not '%s'", reader
->line
);
1145 if (parse_feature_request(features
, "deepen-relative"))
1146 data
->deepen_relative
= 1;
1147 if (parse_feature_request(features
, "multi_ack_detailed"))
1148 data
->multi_ack
= MULTI_ACK_DETAILED
;
1149 else if (parse_feature_request(features
, "multi_ack"))
1150 data
->multi_ack
= MULTI_ACK
;
1151 if (parse_feature_request(features
, "no-done"))
1153 if (parse_feature_request(features
, "thin-pack"))
1154 data
->use_thin_pack
= 1;
1155 if (parse_feature_request(features
, "ofs-delta"))
1156 data
->use_ofs_delta
= 1;
1157 if (parse_feature_request(features
, "side-band-64k"))
1158 data
->use_sideband
= LARGE_PACKET_MAX
;
1159 else if (parse_feature_request(features
, "side-band"))
1160 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1161 if (parse_feature_request(features
, "no-progress"))
1162 data
->no_progress
= 1;
1163 if (parse_feature_request(features
, "include-tag"))
1164 data
->use_include_tag
= 1;
1165 if (data
->allow_filter
&&
1166 parse_feature_request(features
, "filter"))
1167 data
->filter_capability_requested
= 1;
1169 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1171 char *client_sid
= xstrndup(arg
, feature_len
);
1172 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1176 o
= parse_object_with_flags(the_repository
, &oid_buf
,
1177 PARSE_OBJECT_SKIP_HASH_CHECK
|
1178 PARSE_OBJECT_DISCARD_TREE
);
1180 packet_writer_error(&data
->writer
,
1181 "upload-pack: not our ref %s",
1182 oid_to_hex(&oid_buf
));
1183 die("git upload-pack: not our ref %s",
1184 oid_to_hex(&oid_buf
));
1186 if (!(o
->flags
& WANTED
)) {
1188 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1189 || is_our_ref(o
, data
->allow_uor
)))
1191 add_object_array(o
, NULL
, &data
->want_obj
);
1196 * We have sent all our refs already, and the other end
1197 * should have chosen out of them. When we are operating
1198 * in the stateless RPC mode, however, their choice may
1199 * have been based on the set of older refs advertised
1200 * by another process that handled the initial request.
1203 check_non_tip(data
);
1205 if (!data
->use_sideband
&& data
->daemon_mode
)
1206 data
->no_progress
= 1;
1208 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1211 if (send_shallow_list(data
))
1215 /* return non-zero if the ref is hidden, otherwise 0 */
1216 static int mark_our_ref(const char *refname
, const char *refname_full
,
1217 const struct object_id
*oid
, const struct strvec
*hidden_refs
)
1219 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1221 if (ref_is_hidden(refname
, refname_full
, hidden_refs
)) {
1222 o
->flags
|= HIDDEN_REF
;
1225 o
->flags
|= OUR_REF
;
1229 static int check_ref(const char *refname_full
, const char *referent UNUSED
,const struct object_id
*oid
,
1230 int flag UNUSED
, void *cb_data
)
1232 const char *refname
= strip_namespace(refname_full
);
1233 struct upload_pack_data
*data
= cb_data
;
1235 mark_our_ref(refname
, refname_full
, oid
, &data
->hidden_refs
);
1239 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1241 struct string_list_item
*item
;
1245 for_each_string_list_item(item
, symref
)
1246 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1249 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1250 if (d
->advertise_sid
)
1251 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1254 static void write_v0_ref(struct upload_pack_data
*data
,
1255 const char *refname
, const char *refname_nons
,
1256 const struct object_id
*oid
)
1258 static const char *capabilities
= "multi_ack thin-pack side-band"
1259 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1260 " deepen-relative no-progress include-tag multi_ack_detailed";
1261 struct object_id peeled
;
1263 if (mark_our_ref(refname_nons
, refname
, oid
, &data
->hidden_refs
))
1267 struct strbuf symref_info
= STRBUF_INIT
;
1268 struct strbuf session_id
= STRBUF_INIT
;
1270 format_symref_info(&symref_info
, &data
->symref
);
1271 format_session_id(&session_id
, data
);
1272 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1273 oid_to_hex(oid
), refname_nons
,
1275 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1276 " allow-tip-sha1-in-want" : "",
1277 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1278 " allow-reachable-sha1-in-want" : "",
1279 data
->no_done
? " no-done" : "",
1281 data
->allow_filter
? " filter" : "",
1283 the_hash_algo
->name
,
1284 git_user_agent_sanitized());
1285 strbuf_release(&symref_info
);
1286 strbuf_release(&session_id
);
1287 data
->sent_capabilities
= 1;
1289 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1291 capabilities
= NULL
;
1292 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
1293 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1297 static int send_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
1298 int flag UNUSED
, void *cb_data
)
1300 write_v0_ref(cb_data
, refname
, strip_namespace(refname
), oid
);
1304 static int find_symref(const char *refname
, const char *referent UNUSED
,
1305 const struct object_id
*oid UNUSED
,
1306 int flag
, void *cb_data
)
1308 const char *symref_target
;
1309 struct string_list_item
*item
;
1311 if ((flag
& REF_ISSYMREF
) == 0)
1313 symref_target
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1314 refname
, 0, NULL
, &flag
);
1315 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1316 die("'%s' is a symref but it is not?", refname
);
1317 item
= string_list_append(cb_data
, strip_namespace(refname
));
1318 item
->util
= xstrdup(strip_namespace(symref_target
));
1322 static int parse_object_filter_config(const char *var
, const char *value
,
1323 const struct key_value_info
*kvi
,
1324 struct upload_pack_data
*data
)
1326 struct strbuf buf
= STRBUF_INIT
;
1327 const char *sub
, *key
;
1330 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1334 if (!strcmp(key
, "allow"))
1335 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1339 strbuf_add(&buf
, sub
, sub_len
);
1341 if (!strcmp(key
, "allow"))
1342 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1343 (void *)(intptr_t)git_config_bool(var
, value
);
1344 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1346 strbuf_release(&buf
);
1347 return config_error_nonbool(var
);
1349 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1350 (void *)(intptr_t)1;
1351 data
->tree_filter_max_depth
= git_config_ulong(var
, value
,
1355 strbuf_release(&buf
);
1359 static int upload_pack_config(const char *var
, const char *value
,
1360 const struct config_context
*ctx
,
1363 struct upload_pack_data
*data
= cb_data
;
1365 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1366 if (git_config_bool(var
, value
))
1367 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1369 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1370 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1371 if (git_config_bool(var
, value
))
1372 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1374 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1375 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1376 if (git_config_bool(var
, value
))
1377 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1379 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1380 } else if (!strcmp("uploadpack.keepalive", var
)) {
1381 data
->keepalive
= git_config_int(var
, value
, ctx
->kvi
);
1382 if (!data
->keepalive
)
1383 data
->keepalive
= -1;
1384 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1385 data
->allow_filter
= git_config_bool(var
, value
);
1386 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1387 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1388 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1389 data
->allow_sideband_all
= git_config_bool(var
, value
);
1390 } else if (!strcmp("uploadpack.blobpackfileuri", var
)) {
1392 data
->allow_packfile_uris
= 1;
1393 } else if (!strcmp("core.precomposeunicode", var
)) {
1394 precomposed_unicode
= git_config_bool(var
, value
);
1395 } else if (!strcmp("transfer.advertisesid", var
)) {
1396 data
->advertise_sid
= git_config_bool(var
, value
);
1399 if (parse_object_filter_config(var
, value
, ctx
->kvi
, data
) < 0)
1402 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
1405 static int upload_pack_protected_config(const char *var
, const char *value
,
1406 const struct config_context
*ctx UNUSED
,
1409 struct upload_pack_data
*data
= cb_data
;
1411 if (!strcmp("uploadpack.packobjectshook", var
))
1412 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1416 static void get_upload_pack_config(struct repository
*r
,
1417 struct upload_pack_data
*data
)
1419 repo_config(r
, upload_pack_config
, data
);
1420 git_protected_config(upload_pack_protected_config
, data
);
1422 data
->allow_sideband_all
|= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1425 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1428 struct packet_reader reader
;
1429 struct upload_pack_data data
;
1431 upload_pack_data_init(&data
);
1432 get_upload_pack_config(the_repository
, &data
);
1434 data
.stateless_rpc
= stateless_rpc
;
1435 data
.timeout
= timeout
;
1437 data
.daemon_mode
= 1;
1439 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1440 find_symref
, &data
.symref
);
1442 if (advertise_refs
|| !data
.stateless_rpc
) {
1443 reset_timeout(data
.timeout
);
1446 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1448 for_each_namespaced_ref_1(send_ref
, &data
);
1449 if (!data
.sent_capabilities
) {
1450 const char *refname
= "capabilities^{}";
1451 write_v0_ref(&data
, refname
, refname
, null_oid(the_hash_algo
));
1454 * fflush stdout before calling advertise_shallow_grafts because send_ref
1457 fflush_or_die(stdout
);
1458 advertise_shallow_grafts(1);
1461 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1463 for_each_namespaced_ref_1(check_ref
, &data
);
1466 if (!advertise_refs
) {
1467 packet_reader_init(&reader
, 0, NULL
, 0,
1468 PACKET_READ_CHOMP_NEWLINE
|
1469 PACKET_READ_DIE_ON_ERR_PACKET
);
1471 receive_needs(&data
, &reader
);
1474 * An EOF at this exact point in negotiation should be
1475 * acceptable from stateless clients as they will consume the
1476 * shallow list before doing subsequent rpc with haves/etc.
1478 if (data
.stateless_rpc
)
1479 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1481 if (data
.want_obj
.nr
&&
1482 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1483 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1484 get_common_commits(&data
, &reader
);
1485 create_pack_file(&data
, NULL
);
1489 upload_pack_data_clear(&data
);
1492 static int parse_want(struct packet_writer
*writer
, const char *line
,
1493 struct object_array
*want_obj
)
1496 if (skip_prefix(line
, "want ", &arg
)) {
1497 struct object_id oid
;
1500 if (get_oid_hex(arg
, &oid
))
1501 die("git upload-pack: protocol error, "
1502 "expected to get oid, not '%s'", line
);
1504 o
= parse_object_with_flags(the_repository
, &oid
,
1505 PARSE_OBJECT_SKIP_HASH_CHECK
|
1506 PARSE_OBJECT_DISCARD_TREE
);
1509 packet_writer_error(writer
,
1510 "upload-pack: not our ref %s",
1512 die("git upload-pack: not our ref %s",
1516 if (!(o
->flags
& WANTED
)) {
1518 add_object_array(o
, NULL
, want_obj
);
1527 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1528 struct strmap
*wanted_refs
,
1529 struct strvec
*hidden_refs
,
1530 struct object_array
*want_obj
)
1532 const char *refname_nons
;
1533 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1534 struct object_id oid
;
1535 struct object
*o
= NULL
;
1536 struct strbuf refname
= STRBUF_INIT
;
1538 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1539 if (ref_is_hidden(refname_nons
, refname
.buf
, hidden_refs
) ||
1540 refs_read_ref(get_main_ref_store(the_repository
), refname
.buf
, &oid
)) {
1541 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1542 die("unknown ref %s", refname_nons
);
1544 strbuf_release(&refname
);
1546 if (strmap_put(wanted_refs
, refname_nons
, oiddup(&oid
))) {
1547 packet_writer_error(writer
, "duplicate want-ref %s",
1549 die("duplicate want-ref %s", refname_nons
);
1552 if (!starts_with(refname_nons
, "refs/tags/")) {
1553 struct commit
*commit
= lookup_commit_in_graph(the_repository
, &oid
);
1555 o
= &commit
->object
;
1559 o
= parse_object_or_die(the_repository
, &oid
, refname_nons
);
1561 if (!(o
->flags
& WANTED
)) {
1563 add_object_array(o
, NULL
, want_obj
);
1572 static int parse_have(const char *line
, struct upload_pack_data
*data
)
1575 if (skip_prefix(line
, "have ", &arg
)) {
1576 struct object_id oid
;
1578 got_oid(data
, arg
, &oid
);
1579 data
->seen_haves
= 1;
1586 static void trace2_fetch_info(struct upload_pack_data
*data
)
1588 struct json_writer jw
= JSON_WRITER_INIT
;
1590 jw_object_begin(&jw
, 0);
1591 jw_object_intmax(&jw
, "haves", data
->have_obj
.nr
);
1592 jw_object_intmax(&jw
, "wants", data
->want_obj
.nr
);
1593 jw_object_intmax(&jw
, "want-refs", strmap_get_size(&data
->wanted_refs
));
1594 jw_object_intmax(&jw
, "depth", data
->depth
);
1595 jw_object_intmax(&jw
, "shallows", data
->shallows
.nr
);
1596 jw_object_bool(&jw
, "deepen-since", data
->deepen_since
);
1597 jw_object_intmax(&jw
, "deepen-not", oidset_size(&data
->deepen_not
));
1598 jw_object_bool(&jw
, "deepen-relative", data
->deepen_relative
);
1599 if (data
->filter_options
.choice
)
1600 jw_object_string(&jw
, "filter", list_object_filter_config_name(data
->filter_options
.choice
));
1602 jw_object_null(&jw
, "filter");
1605 trace2_data_json("upload-pack", the_repository
, "fetch-info", &jw
);
1610 static void process_args(struct packet_reader
*request
,
1611 struct upload_pack_data
*data
)
1613 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1614 const char *arg
= request
->line
;
1618 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1620 if (data
->allow_ref_in_want
&&
1621 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1622 &data
->hidden_refs
, &data
->want_obj
))
1624 /* process have line */
1625 if (parse_have(arg
, data
))
1628 /* process args like thin-pack */
1629 if (!strcmp(arg
, "thin-pack")) {
1630 data
->use_thin_pack
= 1;
1633 if (!strcmp(arg
, "ofs-delta")) {
1634 data
->use_ofs_delta
= 1;
1637 if (!strcmp(arg
, "no-progress")) {
1638 data
->no_progress
= 1;
1641 if (!strcmp(arg
, "include-tag")) {
1642 data
->use_include_tag
= 1;
1645 if (!strcmp(arg
, "done")) {
1649 if (!strcmp(arg
, "wait-for-done")) {
1650 data
->wait_for_done
= 1;
1654 /* Shallow related arguments */
1655 if (process_shallow(arg
, &data
->shallows
))
1657 if (process_deepen(arg
, &data
->depth
))
1659 if (process_deepen_since(arg
, &data
->deepen_since
,
1660 &data
->deepen_rev_list
))
1662 if (process_deepen_not(arg
, &data
->deepen_not
,
1663 &data
->deepen_rev_list
))
1665 if (!strcmp(arg
, "deepen-relative")) {
1666 data
->deepen_relative
= 1;
1670 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1671 list_objects_filter_die_if_populated(&data
->filter_options
);
1672 parse_list_objects_filter(&data
->filter_options
, p
);
1673 die_if_using_banned_filter(data
);
1677 if (data
->allow_sideband_all
&&
1678 !strcmp(arg
, "sideband-all")) {
1679 data
->writer
.use_sideband
= 1;
1683 if (data
->allow_packfile_uris
&&
1684 skip_prefix(arg
, "packfile-uris ", &p
)) {
1685 if (data
->uri_protocols
.nr
)
1686 send_err_and_die(data
,
1687 "multiple packfile-uris lines forbidden");
1688 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1692 /* ignore unknown lines maybe? */
1693 die("unexpected line: '%s'", arg
);
1696 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1697 string_list_clear(&data
->uri_protocols
, 0);
1699 if (request
->status
!= PACKET_READ_FLUSH
)
1700 die(_("expected flush after fetch arguments"));
1702 if (trace2_is_enabled())
1703 trace2_fetch_info(data
);
1706 static int send_acks(struct upload_pack_data
*data
, struct object_array
*acks
)
1710 packet_writer_write(&data
->writer
, "acknowledgments\n");
1714 packet_writer_write(&data
->writer
, "NAK\n");
1716 for (i
= 0; i
< acks
->nr
; i
++) {
1717 packet_writer_write(&data
->writer
, "ACK %s\n",
1718 oid_to_hex(&acks
->objects
[i
].item
->oid
));
1721 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1723 packet_writer_write(&data
->writer
, "ready\n");
1730 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1736 } else if (send_acks(data
, &data
->have_obj
)) {
1737 packet_writer_delim(&data
->writer
);
1741 packet_writer_flush(&data
->writer
);
1748 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1750 struct hashmap_iter iter
;
1751 const struct strmap_entry
*e
;
1753 if (strmap_empty(&data
->wanted_refs
))
1756 packet_writer_write(&data
->writer
, "wanted-refs\n");
1758 strmap_for_each_entry(&data
->wanted_refs
, &iter
, e
) {
1759 packet_writer_write(&data
->writer
, "%s %s\n",
1760 oid_to_hex(e
->value
),
1764 packet_writer_delim(&data
->writer
);
1767 static void send_shallow_info(struct upload_pack_data
*data
)
1769 /* No shallow info needs to be sent */
1770 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1771 !is_repository_shallow(the_repository
))
1774 packet_writer_write(&data
->writer
, "shallow-info\n");
1776 if (!send_shallow_list(data
) &&
1777 is_repository_shallow(the_repository
))
1778 deepen(data
, INFINITE_DEPTH
);
1784 UPLOAD_PROCESS_ARGS
= 0,
1790 int upload_pack_v2(struct repository
*r
, struct packet_reader
*request
)
1792 enum upload_state state
= UPLOAD_PROCESS_ARGS
;
1793 struct upload_pack_data data
;
1795 clear_object_flags(the_repository
, ALL_FLAGS
);
1797 upload_pack_data_init(&data
);
1798 data
.use_sideband
= LARGE_PACKET_MAX
;
1799 get_upload_pack_config(r
, &data
);
1801 while (state
!= UPLOAD_DONE
) {
1803 case UPLOAD_PROCESS_ARGS
:
1804 process_args(request
, &data
);
1806 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1808 * Request didn't contain any 'want' lines (and
1809 * the request does not contain
1810 * "wait-for-done", in which it is reasonable
1811 * to just send 'have's without 'want's); guess
1812 * they didn't want anything.
1814 state
= UPLOAD_DONE
;
1815 } else if (data
.seen_haves
) {
1817 * Request had 'have' lines, so lets ACK them.
1819 state
= UPLOAD_SEND_ACKS
;
1822 * Request had 'want's but no 'have's so we can
1823 * immediately go to construct and send a pack.
1825 state
= UPLOAD_SEND_PACK
;
1828 case UPLOAD_SEND_ACKS
:
1829 if (process_haves_and_send_acks(&data
))
1830 state
= UPLOAD_SEND_PACK
;
1832 state
= UPLOAD_DONE
;
1834 case UPLOAD_SEND_PACK
:
1835 send_wanted_ref_info(&data
);
1836 send_shallow_info(&data
);
1838 if (data
.uri_protocols
.nr
) {
1839 create_pack_file(&data
, &data
.uri_protocols
);
1841 packet_writer_write(&data
.writer
, "packfile\n");
1842 create_pack_file(&data
, NULL
);
1844 state
= UPLOAD_DONE
;
1851 upload_pack_data_clear(&data
);
1855 int upload_pack_advertise(struct repository
*r
,
1856 struct strbuf
*value
)
1858 struct upload_pack_data data
;
1860 upload_pack_data_init(&data
);
1861 get_upload_pack_config(r
, &data
);
1864 strbuf_addstr(value
, "shallow wait-for-done");
1866 if (data
.allow_filter
)
1867 strbuf_addstr(value
, " filter");
1869 if (data
.allow_ref_in_want
)
1870 strbuf_addstr(value
, " ref-in-want");
1872 if (data
.allow_sideband_all
)
1873 strbuf_addstr(value
, " sideband-all");
1875 if (data
.allow_packfile_uris
)
1876 strbuf_addstr(value
, " packfile-uris");
1879 upload_pack_data_clear(&data
);