1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
11 #include "repository.h"
12 #include "object-store-ll.h"
13 #include "oid-array.h"
18 #include "list-objects-filter-options.h"
19 #include "run-command.h"
23 #include "string-list.h"
27 #include "upload-pack.h"
28 #include "commit-graph.h"
29 #include "commit-reach.h"
31 #include "write-or-die.h"
32 #include "json-writer.h"
35 /* Remember to update object flag allocation in object.h */
36 #define THEY_HAVE (1u << 11)
37 #define OUR_REF (1u << 12)
38 #define WANTED (1u << 13)
39 #define COMMON_KNOWN (1u << 14)
41 #define SHALLOW (1u << 16)
42 #define NOT_SHALLOW (1u << 17)
43 #define CLIENT_SHALLOW (1u << 18)
44 #define HIDDEN_REF (1u << 19)
46 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
47 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
49 /* Enum for allowed unadvertised object request (UOR) */
51 /* Allow specifying sha1 if it is a ref tip. */
52 ALLOW_TIP_SHA1
= 0x01,
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 ALLOW_REACHABLE_SHA1
= 0x02,
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
60 * Please annotate, and if possible group together, fields used only
61 * for protocol v0 or only for protocol v2.
63 struct upload_pack_data
{
64 struct string_list symref
; /* v0 only */
65 struct object_array want_obj
;
66 struct object_array have_obj
;
67 struct strmap wanted_refs
; /* v2 only */
68 struct strvec hidden_refs
;
70 struct object_array shallows
;
71 struct oidset deepen_not
;
72 struct object_array extra_edge_obj
;
74 timestamp_t deepen_since
;
79 timestamp_t oldest_have
;
81 unsigned int timeout
; /* v0 only */
85 MULTI_ACK_DETAILED
= 2
86 } multi_ack
; /* v0 only */
88 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
91 struct string_list uri_protocols
;
92 enum allow_uor allow_uor
;
94 struct list_objects_filter_options filter_options
;
95 struct string_list allowed_filters
;
97 struct packet_writer writer
;
99 char *pack_objects_hook
;
101 unsigned stateless_rpc
: 1; /* v0 only */
102 unsigned no_done
: 1; /* v0 only */
103 unsigned daemon_mode
: 1; /* v0 only */
104 unsigned filter_capability_requested
: 1; /* v0 only */
106 unsigned use_thin_pack
: 1;
107 unsigned use_ofs_delta
: 1;
108 unsigned no_progress
: 1;
109 unsigned use_include_tag
: 1;
110 unsigned wait_for_done
: 1;
111 unsigned allow_filter
: 1;
112 unsigned allow_filter_fallback
: 1;
113 unsigned long tree_filter_max_depth
;
115 unsigned done
: 1; /* v2 only */
116 unsigned allow_ref_in_want
: 1; /* v2 only */
117 unsigned allow_sideband_all
: 1; /* v2 only */
118 unsigned seen_haves
: 1; /* v2 only */
119 unsigned allow_packfile_uris
: 1; /* v2 only */
120 unsigned advertise_sid
: 1;
121 unsigned sent_capabilities
: 1;
124 static void upload_pack_data_init(struct upload_pack_data
*data
)
126 struct string_list symref
= STRING_LIST_INIT_DUP
;
127 struct strmap wanted_refs
= STRMAP_INIT
;
128 struct strvec hidden_refs
= STRVEC_INIT
;
129 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
130 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
131 struct object_array shallows
= OBJECT_ARRAY_INIT
;
132 struct oidset deepen_not
= OID_ARRAY_INIT
;
133 struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
134 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
135 struct string_list allowed_filters
= STRING_LIST_INIT_DUP
;
137 memset(data
, 0, sizeof(*data
));
138 data
->symref
= symref
;
139 data
->wanted_refs
= wanted_refs
;
140 data
->hidden_refs
= hidden_refs
;
141 data
->want_obj
= want_obj
;
142 data
->have_obj
= have_obj
;
143 data
->shallows
= shallows
;
144 data
->deepen_not
= deepen_not
;
145 data
->uri_protocols
= uri_protocols
;
146 data
->extra_edge_obj
= extra_edge_obj
;
147 data
->allowed_filters
= allowed_filters
;
148 data
->allow_filter_fallback
= 1;
149 data
->tree_filter_max_depth
= ULONG_MAX
;
150 packet_writer_init(&data
->writer
, 1);
151 list_objects_filter_init(&data
->filter_options
);
154 data
->advertise_sid
= 0;
157 static void upload_pack_data_clear(struct upload_pack_data
*data
)
159 string_list_clear(&data
->symref
, 1);
160 strmap_clear(&data
->wanted_refs
, 1);
161 strvec_clear(&data
->hidden_refs
);
162 object_array_clear(&data
->want_obj
);
163 object_array_clear(&data
->have_obj
);
164 object_array_clear(&data
->shallows
);
165 oidset_clear(&data
->deepen_not
);
166 object_array_clear(&data
->extra_edge_obj
);
167 list_objects_filter_release(&data
->filter_options
);
168 string_list_clear(&data
->allowed_filters
, 0);
169 string_list_clear(&data
->uri_protocols
, 0);
171 free((char *)data
->pack_objects_hook
);
174 static void reset_timeout(unsigned int timeout
)
179 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
183 send_sideband(1, fd
, data
, sz
, use_sideband
);
190 /* XXX: are we happy to lose stuff here? */
191 xwrite(fd
, data
, sz
);
194 write_or_die(fd
, data
, sz
);
197 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
200 if (graft
->nr_parent
== -1)
201 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
205 struct output_state
{
207 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
208 * sideband-64k the band designator takes up 1 byte of space. Because
209 * relay_pack_data keeps the last byte to itself, we make the buffer 1
210 * byte bigger than the intended maximum write size.
212 char buffer
[(LARGE_PACKET_DATA_MAX
- 1) + 1];
214 unsigned packfile_uris_started
: 1;
215 unsigned packfile_started
: 1;
218 static int relay_pack_data(int pack_objects_out
, struct output_state
*os
,
219 int use_sideband
, int write_packfile_line
)
222 * We keep the last byte to ourselves
223 * in case we detect broken rev-list, so that we
224 * can leave the stream corrupted. This is
225 * unfortunate -- unpack-objects would happily
226 * accept a valid packdata with trailing garbage,
227 * so appending garbage after we pass all the
228 * pack data is not good enough to signal
229 * breakage to downstream.
233 readsz
= xread(pack_objects_out
, os
->buffer
+ os
->used
,
234 sizeof(os
->buffer
) - os
->used
);
240 while (!os
->packfile_started
) {
242 if (os
->used
>= 4 && !memcmp(os
->buffer
, "PACK", 4)) {
243 os
->packfile_started
= 1;
244 if (write_packfile_line
) {
245 if (os
->packfile_uris_started
)
247 packet_write_fmt(1, "\1packfile\n");
251 if ((p
= memchr(os
->buffer
, '\n', os
->used
))) {
252 if (!os
->packfile_uris_started
) {
253 os
->packfile_uris_started
= 1;
254 if (!write_packfile_line
)
255 BUG("packfile_uris requires sideband-all");
256 packet_write_fmt(1, "\1packfile-uris\n");
259 packet_write_fmt(1, "\1%s\n", os
->buffer
);
261 os
->used
-= p
- os
->buffer
+ 1;
262 memmove(os
->buffer
, p
+ 1, os
->used
);
272 send_client_data(1, os
->buffer
, os
->used
- 1, use_sideband
);
273 os
->buffer
[0] = os
->buffer
[os
->used
- 1];
276 send_client_data(1, os
->buffer
, os
->used
, use_sideband
);
283 static void create_pack_file(struct upload_pack_data
*pack_data
,
284 const struct string_list
*uri_protocols
)
286 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
287 struct output_state
*output_state
= xcalloc(1, sizeof(struct output_state
));
289 char abort_msg
[] = "aborting due to possible repository "
290 "corruption on the remote side.";
295 if (!pack_data
->pack_objects_hook
)
296 pack_objects
.git_cmd
= 1;
298 strvec_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
299 strvec_push(&pack_objects
.args
, "git");
300 pack_objects
.use_shell
= 1;
303 if (pack_data
->shallow_nr
) {
304 strvec_push(&pack_objects
.args
, "--shallow-file");
305 strvec_push(&pack_objects
.args
, "");
307 strvec_push(&pack_objects
.args
, "pack-objects");
308 strvec_push(&pack_objects
.args
, "--revs");
309 if (pack_data
->use_thin_pack
)
310 strvec_push(&pack_objects
.args
, "--thin");
312 strvec_push(&pack_objects
.args
, "--stdout");
313 if (pack_data
->shallow_nr
)
314 strvec_push(&pack_objects
.args
, "--shallow");
315 if (!pack_data
->no_progress
)
316 strvec_push(&pack_objects
.args
, "--progress");
317 if (pack_data
->use_ofs_delta
)
318 strvec_push(&pack_objects
.args
, "--delta-base-offset");
319 if (pack_data
->use_include_tag
)
320 strvec_push(&pack_objects
.args
, "--include-tag");
321 if (pack_data
->filter_options
.choice
) {
323 expand_list_objects_filter_spec(&pack_data
->filter_options
);
324 strvec_pushf(&pack_objects
.args
, "--filter=%s", spec
);
327 for (i
= 0; i
< uri_protocols
->nr
; i
++)
328 strvec_pushf(&pack_objects
.args
, "--uri-protocol=%s",
329 uri_protocols
->items
[i
].string
);
332 pack_objects
.in
= -1;
333 pack_objects
.out
= -1;
334 pack_objects
.err
= -1;
335 pack_objects
.clean_on_exit
= 1;
337 if (start_command(&pack_objects
))
338 die("git upload-pack: unable to fork git-pack-objects");
340 pipe_fd
= xfdopen(pack_objects
.in
, "w");
342 if (pack_data
->shallow_nr
)
343 for_each_commit_graft(write_one_shallow
, pipe_fd
);
345 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
346 fprintf(pipe_fd
, "%s\n",
347 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
348 fprintf(pipe_fd
, "--not\n");
349 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
350 fprintf(pipe_fd
, "%s\n",
351 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
352 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
353 fprintf(pipe_fd
, "%s\n",
354 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
355 fprintf(pipe_fd
, "\n");
359 /* We read from pack_objects.err to capture stderr output for
360 * progress bar, and pack_objects.out to capture the pack data.
364 struct pollfd pfd
[2];
365 int pe
, pu
, pollsize
, polltimeout
;
368 reset_timeout(pack_data
->timeout
);
373 if (0 <= pack_objects
.out
) {
374 pfd
[pollsize
].fd
= pack_objects
.out
;
375 pfd
[pollsize
].events
= POLLIN
;
379 if (0 <= pack_objects
.err
) {
380 pfd
[pollsize
].fd
= pack_objects
.err
;
381 pfd
[pollsize
].events
= POLLIN
;
389 polltimeout
= pack_data
->keepalive
< 0
391 : 1000 * pack_data
->keepalive
;
393 ret
= poll(pfd
, pollsize
, polltimeout
);
396 if (errno
!= EINTR
) {
397 error_errno("poll failed, resuming");
402 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
403 /* Status ready; we ship that in the side-band
404 * or dump to the standard error.
406 sz
= xread(pack_objects
.err
, progress
,
409 send_client_data(2, progress
, sz
,
410 pack_data
->use_sideband
);
412 close(pack_objects
.err
);
413 pack_objects
.err
= -1;
417 /* give priority to status messages */
420 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
421 int result
= relay_pack_data(pack_objects
.out
,
423 pack_data
->use_sideband
,
427 close(pack_objects
.out
);
428 pack_objects
.out
= -1;
429 } else if (result
< 0) {
435 * We hit the keepalive timeout without saying anything; send
436 * an empty message on the data sideband just to let the other
437 * side know we're still working on it, but don't have any data
440 * If we don't have a sideband channel, there's no room in the
441 * protocol to say anything, so those clients are just out of
444 if (!ret
&& pack_data
->use_sideband
) {
445 static const char buf
[] = "0005\1";
446 write_or_die(1, buf
, 5);
450 if (finish_command(&pack_objects
)) {
451 error("git upload-pack: git-pack-objects died with error.");
456 if (output_state
->used
> 0) {
457 send_client_data(1, output_state
->buffer
, output_state
->used
,
458 pack_data
->use_sideband
);
459 fprintf(stderr
, "flushed.\n");
462 if (pack_data
->use_sideband
)
468 send_client_data(3, abort_msg
, strlen(abort_msg
),
469 pack_data
->use_sideband
);
470 die("git upload-pack: %s", abort_msg
);
473 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
475 int we_knew_they_have
= 0;
476 struct object
*o
= parse_object_with_flags(the_repository
, oid
,
477 PARSE_OBJECT_SKIP_HASH_CHECK
|
478 PARSE_OBJECT_DISCARD_TREE
);
481 die("oops (%s)", oid_to_hex(oid
));
482 if (o
->type
== OBJ_COMMIT
) {
483 struct commit_list
*parents
;
484 struct commit
*commit
= (struct commit
*)o
;
485 if (o
->flags
& THEY_HAVE
)
486 we_knew_they_have
= 1;
488 o
->flags
|= THEY_HAVE
;
489 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
490 data
->oldest_have
= commit
->date
;
491 for (parents
= commit
->parents
;
493 parents
= parents
->next
)
494 parents
->item
->object
.flags
|= THEY_HAVE
;
496 if (!we_knew_they_have
) {
497 add_object_array(o
, NULL
, &data
->have_obj
);
503 static int got_oid(struct upload_pack_data
*data
,
504 const char *hex
, struct object_id
*oid
)
506 if (get_oid_hex(hex
, oid
))
507 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
508 if (!repo_has_object_file_with_flags(the_repository
, oid
,
509 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
511 return do_got_oid(data
, oid
);
514 static int ok_to_give_up(struct upload_pack_data
*data
)
516 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
518 if (!data
->have_obj
.nr
)
521 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
522 COMMON_KNOWN
, data
->oldest_have
,
526 static int get_common_commits(struct upload_pack_data
*data
,
527 struct packet_reader
*reader
)
529 struct object_id oid
;
530 char last_hex
[GIT_MAX_HEXSZ
+ 1];
538 reset_timeout(data
->timeout
);
540 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
541 if (data
->multi_ack
== MULTI_ACK_DETAILED
544 && ok_to_give_up(data
)) {
546 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
548 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
549 packet_write_fmt(1, "NAK\n");
551 if (data
->no_done
&& sent_ready
) {
552 packet_write_fmt(1, "ACK %s\n", last_hex
);
555 if (data
->stateless_rpc
)
561 if (skip_prefix(reader
->line
, "have ", &arg
)) {
562 switch (got_oid(data
, arg
, &oid
)) {
563 case -1: /* they have what we do not */
566 && ok_to_give_up(data
)) {
567 const char *hex
= oid_to_hex(&oid
);
568 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
570 packet_write_fmt(1, "ACK %s ready\n", hex
);
572 packet_write_fmt(1, "ACK %s continue\n", hex
);
577 oid_to_hex_r(last_hex
, &oid
);
578 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
579 packet_write_fmt(1, "ACK %s common\n", last_hex
);
580 else if (data
->multi_ack
)
581 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
582 else if (data
->have_obj
.nr
== 1)
583 packet_write_fmt(1, "ACK %s\n", last_hex
);
588 if (!strcmp(reader
->line
, "done")) {
589 if (data
->have_obj
.nr
> 0) {
591 packet_write_fmt(1, "ACK %s\n", last_hex
);
594 packet_write_fmt(1, "NAK\n");
597 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
601 static int allow_hidden_refs(enum allow_uor allow_uor
)
603 if ((allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
)
605 return !(allow_uor
& (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
608 static void for_each_namespaced_ref_1(each_ref_fn fn
,
609 struct upload_pack_data
*data
)
611 const char **excludes
= NULL
;
613 * If `data->allow_uor` allows fetching hidden refs, we need to
614 * mark all references (including hidden ones), to check in
615 * `is_our_ref()` below.
617 * Otherwise, we only care about whether each reference's object
618 * has the OUR_REF bit set or not, so do not need to visit
621 if (allow_hidden_refs(data
->allow_uor
))
622 excludes
= hidden_refs_to_excludes(&data
->hidden_refs
);
624 refs_for_each_namespaced_ref(get_main_ref_store(the_repository
),
629 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
631 return o
->flags
& ((allow_hidden_refs(allow_uor
) ? 0 : HIDDEN_REF
) | OUR_REF
);
635 * on successful case, it's up to the caller to close cmd->out
637 static int do_reachable_revlist(struct child_process
*cmd
,
638 struct object_array
*src
,
639 struct object_array
*reachable
,
640 enum allow_uor allow_uor
)
646 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
653 * If the next rev-list --stdin encounters an unknown commit,
654 * it terminates, which will cause SIGPIPE in the write loop
657 sigchain_push(SIGPIPE
, SIG_IGN
);
659 if (start_command(cmd
))
662 cmd_in
= xfdopen(cmd
->in
, "w");
664 for (i
= get_max_object_index(); 0 < i
; ) {
665 o
= get_indexed_object(--i
);
668 if (reachable
&& o
->type
== OBJ_COMMIT
)
669 o
->flags
&= ~TMP_MARK
;
670 if (!is_our_ref(o
, allow_uor
))
672 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
675 for (i
= 0; i
< src
->nr
; i
++) {
676 o
= src
->objects
[i
].item
;
677 if (is_our_ref(o
, allow_uor
)) {
679 add_object_array(o
, NULL
, reachable
);
682 if (reachable
&& o
->type
== OBJ_COMMIT
)
683 o
->flags
|= TMP_MARK
;
684 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
687 if (ferror(cmd_in
) || fflush(cmd_in
))
691 sigchain_pop(SIGPIPE
);
696 sigchain_pop(SIGPIPE
);
705 static int get_reachable_list(struct upload_pack_data
*data
,
706 struct object_array
*reachable
)
708 struct child_process cmd
= CHILD_PROCESS_INIT
;
711 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
712 const unsigned hexsz
= the_hash_algo
->hexsz
;
715 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
716 data
->allow_uor
) < 0) {
721 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
722 struct object_id oid
;
725 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
728 o
= lookup_object(the_repository
, &oid
);
729 if (o
&& o
->type
== OBJ_COMMIT
) {
730 o
->flags
&= ~TMP_MARK
;
733 for (i
= get_max_object_index(); 0 < i
; i
--) {
734 o
= get_indexed_object(i
- 1);
735 if (o
&& o
->type
== OBJ_COMMIT
&&
736 (o
->flags
& TMP_MARK
)) {
737 add_object_array(o
, NULL
, reachable
);
738 o
->flags
&= ~TMP_MARK
;
743 if (finish_command(&cmd
)) {
751 child_process_clear(&cmd
);
755 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
757 struct child_process cmd
= CHILD_PROCESS_INIT
;
761 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
765 * The commits out of the rev-list are not ancestors of
768 i
= read_in_full(cmd
.out
, buf
, 1);
775 * rev-list may have died by encountering a bad commit
776 * in the history, in which case we do want to bail out
777 * even when it showed no commit.
779 if (finish_command(&cmd
))
782 /* All the non-tip ones are ancestors of what we advertised */
788 child_process_clear(&cmd
);
792 static void check_non_tip(struct upload_pack_data
*data
)
797 * In the normal in-process case without
798 * uploadpack.allowReachableSHA1InWant,
799 * non-tip requests can never happen.
801 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
803 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
804 /* All the non-tip ones are ancestors of what we advertised */
808 /* Pick one of them (we know there at least is one) */
809 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
810 struct object
*o
= data
->want_obj
.objects
[i
].item
;
811 if (!is_our_ref(o
, data
->allow_uor
)) {
812 error("git upload-pack: not our ref %s",
813 oid_to_hex(&o
->oid
));
814 packet_writer_error(&data
->writer
,
815 "upload-pack: not our ref %s",
816 oid_to_hex(&o
->oid
));
822 static void send_shallow(struct upload_pack_data
*data
,
823 struct commit_list
*result
)
826 struct object
*object
= &result
->item
->object
;
827 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
828 packet_writer_write(&data
->writer
, "shallow %s",
829 oid_to_hex(&object
->oid
));
830 register_shallow(the_repository
, &object
->oid
);
833 result
= result
->next
;
837 static void send_unshallow(struct upload_pack_data
*data
)
841 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
842 struct object
*object
= data
->shallows
.objects
[i
].item
;
843 if (object
->flags
& NOT_SHALLOW
) {
844 struct commit_list
*parents
;
845 packet_writer_write(&data
->writer
, "unshallow %s",
846 oid_to_hex(&object
->oid
));
847 object
->flags
&= ~CLIENT_SHALLOW
;
849 * We want to _register_ "object" as shallow, but we
850 * also need to traverse object's parents to deepen a
851 * shallow clone. Unregister it for now so we can
852 * parse and add the parents to the want list, then
855 unregister_shallow(&object
->oid
);
857 parse_commit_or_die((struct commit
*)object
);
858 parents
= ((struct commit
*)object
)->parents
;
860 add_object_array(&parents
->item
->object
,
861 NULL
, &data
->want_obj
);
862 parents
= parents
->next
;
864 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
866 /* make sure commit traversal conforms to client */
867 register_shallow(the_repository
, &object
->oid
);
871 static int check_ref(const char *refname_full
, const char *referent UNUSED
, const struct object_id
*oid
,
872 int flag
, void *cb_data
);
873 static void deepen(struct upload_pack_data
*data
, int depth
)
875 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
878 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
879 struct object
*object
= data
->shallows
.objects
[i
].item
;
880 object
->flags
|= NOT_SHALLOW
;
882 } else if (data
->deepen_relative
) {
883 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
884 struct commit_list
*result
;
887 * Checking for reachable shallows requires that our refs be
888 * marked with OUR_REF.
890 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
892 for_each_namespaced_ref_1(check_ref
, data
);
894 get_reachable_list(data
, &reachable_shallows
);
895 result
= get_shallow_commits(&reachable_shallows
,
897 SHALLOW
, NOT_SHALLOW
);
898 send_shallow(data
, result
);
899 free_commit_list(result
);
900 object_array_clear(&reachable_shallows
);
902 struct commit_list
*result
;
904 result
= get_shallow_commits(&data
->want_obj
, depth
,
905 SHALLOW
, NOT_SHALLOW
);
906 send_shallow(data
, result
);
907 free_commit_list(result
);
910 send_unshallow(data
);
913 static void deepen_by_rev_list(struct upload_pack_data
*data
,
917 struct commit_list
*result
;
919 disable_commit_graph(the_repository
);
920 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
921 send_shallow(data
, result
);
922 free_commit_list(result
);
923 send_unshallow(data
);
926 /* Returns 1 if a shallow list is sent or 0 otherwise */
927 static int send_shallow_list(struct upload_pack_data
*data
)
931 if (data
->depth
> 0 && data
->deepen_rev_list
)
932 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
933 if (data
->depth
> 0) {
934 deepen(data
, data
->depth
);
936 } else if (data
->deepen_rev_list
) {
937 struct strvec av
= STRVEC_INIT
;
940 strvec_push(&av
, "rev-list");
941 if (data
->deepen_since
)
942 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
943 if (oidset_size(&data
->deepen_not
)) {
944 const struct object_id
*oid
;
945 struct oidset_iter iter
;
946 strvec_push(&av
, "--not");
947 oidset_iter_init(&data
->deepen_not
, &iter
);
948 while ((oid
= oidset_iter_next(&iter
)))
949 strvec_push(&av
, oid_to_hex(oid
));
950 strvec_push(&av
, "--not");
952 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
953 struct object
*o
= data
->want_obj
.objects
[i
].item
;
954 strvec_push(&av
, oid_to_hex(&o
->oid
));
956 deepen_by_rev_list(data
, av
.nr
, av
.v
);
960 if (data
->shallows
.nr
> 0) {
962 for (i
= 0; i
< data
->shallows
.nr
; i
++)
963 register_shallow(the_repository
,
964 &data
->shallows
.objects
[i
].item
->oid
);
968 data
->shallow_nr
+= data
->shallows
.nr
;
972 static int process_shallow(const char *line
, struct object_array
*shallows
)
975 if (skip_prefix(line
, "shallow ", &arg
)) {
976 struct object_id oid
;
977 struct object
*object
;
978 if (get_oid_hex(arg
, &oid
))
979 die("invalid shallow line: %s", line
);
980 object
= parse_object(the_repository
, &oid
);
983 if (object
->type
!= OBJ_COMMIT
)
984 die("invalid shallow object %s", oid_to_hex(&oid
));
985 if (!(object
->flags
& CLIENT_SHALLOW
)) {
986 object
->flags
|= CLIENT_SHALLOW
;
987 add_object_array(object
, NULL
, shallows
);
995 static int process_deepen(const char *line
, int *depth
)
998 if (skip_prefix(line
, "deepen ", &arg
)) {
1000 *depth
= (int)strtol(arg
, &end
, 0);
1001 if (!end
|| *end
|| *depth
<= 0)
1002 die("Invalid deepen: %s", line
);
1009 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
1012 if (skip_prefix(line
, "deepen-since ", &arg
)) {
1014 *deepen_since
= parse_timestamp(arg
, &end
, 0);
1015 if (!end
|| *end
|| !deepen_since
||
1016 /* revisions.c's max_age -1 is special */
1017 *deepen_since
== -1)
1018 die("Invalid deepen-since: %s", line
);
1019 *deepen_rev_list
= 1;
1025 static int process_deepen_not(const char *line
, struct oidset
*deepen_not
, int *deepen_rev_list
)
1028 if (skip_prefix(line
, "deepen-not ", &arg
)) {
1031 struct object_id oid
;
1032 cnt
= expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
);
1034 die("git upload-pack: ambiguous deepen-not: %s", line
);
1036 die("git upload-pack: deepen-not is not a ref: %s", line
);
1037 oidset_insert(deepen_not
, &oid
);
1039 *deepen_rev_list
= 1;
1045 NORETURN
__attribute__((format(printf
,2,3)))
1046 static void send_err_and_die(struct upload_pack_data
*data
,
1047 const char *fmt
, ...)
1049 struct strbuf buf
= STRBUF_INIT
;
1053 strbuf_vaddf(&buf
, fmt
, ap
);
1056 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1060 static void check_one_filter(struct upload_pack_data
*data
,
1061 struct list_objects_filter_options
*opts
)
1063 const char *key
= list_object_filter_config_name(opts
->choice
);
1064 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1069 allowed
= (intptr_t)item
->util
;
1071 allowed
= data
->allow_filter_fallback
;
1074 send_err_and_die(data
, "filter '%s' not supported", key
);
1076 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1077 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1078 send_err_and_die(data
,
1079 "tree filter allows max depth %lu, but got %lu",
1080 data
->tree_filter_max_depth
,
1081 opts
->tree_exclude_depth
);
1084 static void check_filter_recurse(struct upload_pack_data
*data
,
1085 struct list_objects_filter_options
*opts
)
1089 check_one_filter(data
, opts
);
1090 if (opts
->choice
!= LOFC_COMBINE
)
1093 for (i
= 0; i
< opts
->sub_nr
; i
++)
1094 check_filter_recurse(data
, &opts
->sub
[i
]);
1097 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1099 check_filter_recurse(data
, &data
->filter_options
);
1102 static void receive_needs(struct upload_pack_data
*data
,
1103 struct packet_reader
*reader
)
1105 int has_non_tip
= 0;
1107 data
->shallow_nr
= 0;
1110 const char *features
;
1111 struct object_id oid_buf
;
1115 reset_timeout(data
->timeout
);
1116 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1119 if (process_shallow(reader
->line
, &data
->shallows
))
1121 if (process_deepen(reader
->line
, &data
->depth
))
1123 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1125 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1128 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1129 if (!data
->filter_capability_requested
)
1130 die("git upload-pack: filtering capability not negotiated");
1131 list_objects_filter_die_if_populated(&data
->filter_options
);
1132 parse_list_objects_filter(&data
->filter_options
, arg
);
1133 die_if_using_banned_filter(data
);
1137 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1138 parse_oid_hex(arg
, &oid_buf
, &features
))
1139 die("git upload-pack: protocol error, "
1140 "expected to get object ID, not '%s'", reader
->line
);
1142 if (parse_feature_request(features
, "deepen-relative"))
1143 data
->deepen_relative
= 1;
1144 if (parse_feature_request(features
, "multi_ack_detailed"))
1145 data
->multi_ack
= MULTI_ACK_DETAILED
;
1146 else if (parse_feature_request(features
, "multi_ack"))
1147 data
->multi_ack
= MULTI_ACK
;
1148 if (parse_feature_request(features
, "no-done"))
1150 if (parse_feature_request(features
, "thin-pack"))
1151 data
->use_thin_pack
= 1;
1152 if (parse_feature_request(features
, "ofs-delta"))
1153 data
->use_ofs_delta
= 1;
1154 if (parse_feature_request(features
, "side-band-64k"))
1155 data
->use_sideband
= LARGE_PACKET_MAX
;
1156 else if (parse_feature_request(features
, "side-band"))
1157 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1158 if (parse_feature_request(features
, "no-progress"))
1159 data
->no_progress
= 1;
1160 if (parse_feature_request(features
, "include-tag"))
1161 data
->use_include_tag
= 1;
1162 if (data
->allow_filter
&&
1163 parse_feature_request(features
, "filter"))
1164 data
->filter_capability_requested
= 1;
1166 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1168 char *client_sid
= xstrndup(arg
, feature_len
);
1169 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1173 o
= parse_object_with_flags(the_repository
, &oid_buf
,
1174 PARSE_OBJECT_SKIP_HASH_CHECK
|
1175 PARSE_OBJECT_DISCARD_TREE
);
1177 packet_writer_error(&data
->writer
,
1178 "upload-pack: not our ref %s",
1179 oid_to_hex(&oid_buf
));
1180 die("git upload-pack: not our ref %s",
1181 oid_to_hex(&oid_buf
));
1183 if (!(o
->flags
& WANTED
)) {
1185 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1186 || is_our_ref(o
, data
->allow_uor
)))
1188 add_object_array(o
, NULL
, &data
->want_obj
);
1193 * We have sent all our refs already, and the other end
1194 * should have chosen out of them. When we are operating
1195 * in the stateless RPC mode, however, their choice may
1196 * have been based on the set of older refs advertised
1197 * by another process that handled the initial request.
1200 check_non_tip(data
);
1202 if (!data
->use_sideband
&& data
->daemon_mode
)
1203 data
->no_progress
= 1;
1205 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1208 if (send_shallow_list(data
))
1212 /* return non-zero if the ref is hidden, otherwise 0 */
1213 static int mark_our_ref(const char *refname
, const char *refname_full
,
1214 const struct object_id
*oid
, const struct strvec
*hidden_refs
)
1216 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1218 if (ref_is_hidden(refname
, refname_full
, hidden_refs
)) {
1219 o
->flags
|= HIDDEN_REF
;
1222 o
->flags
|= OUR_REF
;
1226 static int check_ref(const char *refname_full
, const char *referent UNUSED
,const struct object_id
*oid
,
1227 int flag UNUSED
, void *cb_data
)
1229 const char *refname
= strip_namespace(refname_full
);
1230 struct upload_pack_data
*data
= cb_data
;
1232 mark_our_ref(refname
, refname_full
, oid
, &data
->hidden_refs
);
1236 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1238 struct string_list_item
*item
;
1242 for_each_string_list_item(item
, symref
)
1243 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1246 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1247 if (d
->advertise_sid
)
1248 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1251 static void write_v0_ref(struct upload_pack_data
*data
,
1252 const char *refname
, const char *refname_nons
,
1253 const struct object_id
*oid
)
1255 static const char *capabilities
= "multi_ack thin-pack side-band"
1256 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1257 " deepen-relative no-progress include-tag multi_ack_detailed";
1258 struct object_id peeled
;
1260 if (mark_our_ref(refname_nons
, refname
, oid
, &data
->hidden_refs
))
1264 struct strbuf symref_info
= STRBUF_INIT
;
1265 struct strbuf session_id
= STRBUF_INIT
;
1267 format_symref_info(&symref_info
, &data
->symref
);
1268 format_session_id(&session_id
, data
);
1269 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1270 oid_to_hex(oid
), refname_nons
,
1272 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1273 " allow-tip-sha1-in-want" : "",
1274 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1275 " allow-reachable-sha1-in-want" : "",
1276 data
->no_done
? " no-done" : "",
1278 data
->allow_filter
? " filter" : "",
1280 the_hash_algo
->name
,
1281 git_user_agent_sanitized());
1282 strbuf_release(&symref_info
);
1283 strbuf_release(&session_id
);
1284 data
->sent_capabilities
= 1;
1286 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1288 capabilities
= NULL
;
1289 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
1290 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1294 static int send_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
1295 int flag UNUSED
, void *cb_data
)
1297 write_v0_ref(cb_data
, refname
, strip_namespace(refname
), oid
);
1301 static int find_symref(const char *refname
, const char *referent UNUSED
,
1302 const struct object_id
*oid UNUSED
,
1303 int flag
, void *cb_data
)
1305 const char *symref_target
;
1306 struct string_list_item
*item
;
1308 if ((flag
& REF_ISSYMREF
) == 0)
1310 symref_target
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1311 refname
, 0, NULL
, &flag
);
1312 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1313 die("'%s' is a symref but it is not?", refname
);
1314 item
= string_list_append(cb_data
, strip_namespace(refname
));
1315 item
->util
= xstrdup(strip_namespace(symref_target
));
1319 static int parse_object_filter_config(const char *var
, const char *value
,
1320 const struct key_value_info
*kvi
,
1321 struct upload_pack_data
*data
)
1323 struct strbuf buf
= STRBUF_INIT
;
1324 const char *sub
, *key
;
1327 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1331 if (!strcmp(key
, "allow"))
1332 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1336 strbuf_add(&buf
, sub
, sub_len
);
1338 if (!strcmp(key
, "allow"))
1339 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1340 (void *)(intptr_t)git_config_bool(var
, value
);
1341 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1343 strbuf_release(&buf
);
1344 return config_error_nonbool(var
);
1346 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1347 (void *)(intptr_t)1;
1348 data
->tree_filter_max_depth
= git_config_ulong(var
, value
,
1352 strbuf_release(&buf
);
1356 static int upload_pack_config(const char *var
, const char *value
,
1357 const struct config_context
*ctx
,
1360 struct upload_pack_data
*data
= cb_data
;
1362 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1363 if (git_config_bool(var
, value
))
1364 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1366 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1367 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1368 if (git_config_bool(var
, value
))
1369 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1371 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1372 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1373 if (git_config_bool(var
, value
))
1374 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1376 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1377 } else if (!strcmp("uploadpack.keepalive", var
)) {
1378 data
->keepalive
= git_config_int(var
, value
, ctx
->kvi
);
1379 if (!data
->keepalive
)
1380 data
->keepalive
= -1;
1381 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1382 data
->allow_filter
= git_config_bool(var
, value
);
1383 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1384 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1385 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1386 data
->allow_sideband_all
= git_config_bool(var
, value
);
1387 } else if (!strcmp("uploadpack.blobpackfileuri", var
)) {
1389 data
->allow_packfile_uris
= 1;
1390 } else if (!strcmp("core.precomposeunicode", var
)) {
1391 precomposed_unicode
= git_config_bool(var
, value
);
1392 } else if (!strcmp("transfer.advertisesid", var
)) {
1393 data
->advertise_sid
= git_config_bool(var
, value
);
1396 if (parse_object_filter_config(var
, value
, ctx
->kvi
, data
) < 0)
1399 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
1402 static int upload_pack_protected_config(const char *var
, const char *value
,
1403 const struct config_context
*ctx UNUSED
,
1406 struct upload_pack_data
*data
= cb_data
;
1408 if (!strcmp("uploadpack.packobjectshook", var
))
1409 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1413 static void get_upload_pack_config(struct repository
*r
,
1414 struct upload_pack_data
*data
)
1416 repo_config(r
, upload_pack_config
, data
);
1417 git_protected_config(upload_pack_protected_config
, data
);
1419 data
->allow_sideband_all
|= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1422 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1425 struct packet_reader reader
;
1426 struct upload_pack_data data
;
1428 upload_pack_data_init(&data
);
1429 get_upload_pack_config(the_repository
, &data
);
1431 data
.stateless_rpc
= stateless_rpc
;
1432 data
.timeout
= timeout
;
1434 data
.daemon_mode
= 1;
1436 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1437 find_symref
, &data
.symref
);
1439 if (advertise_refs
|| !data
.stateless_rpc
) {
1440 reset_timeout(data
.timeout
);
1443 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1445 for_each_namespaced_ref_1(send_ref
, &data
);
1446 if (!data
.sent_capabilities
) {
1447 const char *refname
= "capabilities^{}";
1448 write_v0_ref(&data
, refname
, refname
, null_oid());
1451 * fflush stdout before calling advertise_shallow_grafts because send_ref
1454 fflush_or_die(stdout
);
1455 advertise_shallow_grafts(1);
1458 refs_head_ref_namespaced(get_main_ref_store(the_repository
),
1460 for_each_namespaced_ref_1(check_ref
, &data
);
1463 if (!advertise_refs
) {
1464 packet_reader_init(&reader
, 0, NULL
, 0,
1465 PACKET_READ_CHOMP_NEWLINE
|
1466 PACKET_READ_DIE_ON_ERR_PACKET
);
1468 receive_needs(&data
, &reader
);
1471 * An EOF at this exact point in negotiation should be
1472 * acceptable from stateless clients as they will consume the
1473 * shallow list before doing subsequent rpc with haves/etc.
1475 if (data
.stateless_rpc
)
1476 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1478 if (data
.want_obj
.nr
&&
1479 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1480 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1481 get_common_commits(&data
, &reader
);
1482 create_pack_file(&data
, NULL
);
1486 upload_pack_data_clear(&data
);
1489 static int parse_want(struct packet_writer
*writer
, const char *line
,
1490 struct object_array
*want_obj
)
1493 if (skip_prefix(line
, "want ", &arg
)) {
1494 struct object_id oid
;
1497 if (get_oid_hex(arg
, &oid
))
1498 die("git upload-pack: protocol error, "
1499 "expected to get oid, not '%s'", line
);
1501 o
= parse_object_with_flags(the_repository
, &oid
,
1502 PARSE_OBJECT_SKIP_HASH_CHECK
|
1503 PARSE_OBJECT_DISCARD_TREE
);
1506 packet_writer_error(writer
,
1507 "upload-pack: not our ref %s",
1509 die("git upload-pack: not our ref %s",
1513 if (!(o
->flags
& WANTED
)) {
1515 add_object_array(o
, NULL
, want_obj
);
1524 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1525 struct strmap
*wanted_refs
,
1526 struct strvec
*hidden_refs
,
1527 struct object_array
*want_obj
)
1529 const char *refname_nons
;
1530 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1531 struct object_id oid
;
1532 struct object
*o
= NULL
;
1533 struct strbuf refname
= STRBUF_INIT
;
1535 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1536 if (ref_is_hidden(refname_nons
, refname
.buf
, hidden_refs
) ||
1537 refs_read_ref(get_main_ref_store(the_repository
), refname
.buf
, &oid
)) {
1538 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1539 die("unknown ref %s", refname_nons
);
1541 strbuf_release(&refname
);
1543 if (strmap_put(wanted_refs
, refname_nons
, oiddup(&oid
))) {
1544 packet_writer_error(writer
, "duplicate want-ref %s",
1546 die("duplicate want-ref %s", refname_nons
);
1549 if (!starts_with(refname_nons
, "refs/tags/")) {
1550 struct commit
*commit
= lookup_commit_in_graph(the_repository
, &oid
);
1552 o
= &commit
->object
;
1556 o
= parse_object_or_die(&oid
, refname_nons
);
1558 if (!(o
->flags
& WANTED
)) {
1560 add_object_array(o
, NULL
, want_obj
);
1569 static int parse_have(const char *line
, struct upload_pack_data
*data
)
1572 if (skip_prefix(line
, "have ", &arg
)) {
1573 struct object_id oid
;
1575 got_oid(data
, arg
, &oid
);
1576 data
->seen_haves
= 1;
1583 static void trace2_fetch_info(struct upload_pack_data
*data
)
1585 struct json_writer jw
= JSON_WRITER_INIT
;
1587 jw_object_begin(&jw
, 0);
1588 jw_object_intmax(&jw
, "haves", data
->have_obj
.nr
);
1589 jw_object_intmax(&jw
, "wants", data
->want_obj
.nr
);
1590 jw_object_intmax(&jw
, "want-refs", strmap_get_size(&data
->wanted_refs
));
1591 jw_object_intmax(&jw
, "depth", data
->depth
);
1592 jw_object_intmax(&jw
, "shallows", data
->shallows
.nr
);
1593 jw_object_bool(&jw
, "deepen-since", data
->deepen_since
);
1594 jw_object_intmax(&jw
, "deepen-not", oidset_size(&data
->deepen_not
));
1595 jw_object_bool(&jw
, "deepen-relative", data
->deepen_relative
);
1596 if (data
->filter_options
.choice
)
1597 jw_object_string(&jw
, "filter", list_object_filter_config_name(data
->filter_options
.choice
));
1599 jw_object_null(&jw
, "filter");
1602 trace2_data_json("upload-pack", the_repository
, "fetch-info", &jw
);
1607 static void process_args(struct packet_reader
*request
,
1608 struct upload_pack_data
*data
)
1610 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1611 const char *arg
= request
->line
;
1615 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1617 if (data
->allow_ref_in_want
&&
1618 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1619 &data
->hidden_refs
, &data
->want_obj
))
1621 /* process have line */
1622 if (parse_have(arg
, data
))
1625 /* process args like thin-pack */
1626 if (!strcmp(arg
, "thin-pack")) {
1627 data
->use_thin_pack
= 1;
1630 if (!strcmp(arg
, "ofs-delta")) {
1631 data
->use_ofs_delta
= 1;
1634 if (!strcmp(arg
, "no-progress")) {
1635 data
->no_progress
= 1;
1638 if (!strcmp(arg
, "include-tag")) {
1639 data
->use_include_tag
= 1;
1642 if (!strcmp(arg
, "done")) {
1646 if (!strcmp(arg
, "wait-for-done")) {
1647 data
->wait_for_done
= 1;
1651 /* Shallow related arguments */
1652 if (process_shallow(arg
, &data
->shallows
))
1654 if (process_deepen(arg
, &data
->depth
))
1656 if (process_deepen_since(arg
, &data
->deepen_since
,
1657 &data
->deepen_rev_list
))
1659 if (process_deepen_not(arg
, &data
->deepen_not
,
1660 &data
->deepen_rev_list
))
1662 if (!strcmp(arg
, "deepen-relative")) {
1663 data
->deepen_relative
= 1;
1667 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1668 list_objects_filter_die_if_populated(&data
->filter_options
);
1669 parse_list_objects_filter(&data
->filter_options
, p
);
1670 die_if_using_banned_filter(data
);
1674 if (data
->allow_sideband_all
&&
1675 !strcmp(arg
, "sideband-all")) {
1676 data
->writer
.use_sideband
= 1;
1680 if (data
->allow_packfile_uris
&&
1681 skip_prefix(arg
, "packfile-uris ", &p
)) {
1682 if (data
->uri_protocols
.nr
)
1683 send_err_and_die(data
,
1684 "multiple packfile-uris lines forbidden");
1685 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1689 /* ignore unknown lines maybe? */
1690 die("unexpected line: '%s'", arg
);
1693 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1694 string_list_clear(&data
->uri_protocols
, 0);
1696 if (request
->status
!= PACKET_READ_FLUSH
)
1697 die(_("expected flush after fetch arguments"));
1699 if (trace2_is_enabled())
1700 trace2_fetch_info(data
);
1703 static int send_acks(struct upload_pack_data
*data
, struct object_array
*acks
)
1707 packet_writer_write(&data
->writer
, "acknowledgments\n");
1711 packet_writer_write(&data
->writer
, "NAK\n");
1713 for (i
= 0; i
< acks
->nr
; i
++) {
1714 packet_writer_write(&data
->writer
, "ACK %s\n",
1715 oid_to_hex(&acks
->objects
[i
].item
->oid
));
1718 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1720 packet_writer_write(&data
->writer
, "ready\n");
1727 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1733 } else if (send_acks(data
, &data
->have_obj
)) {
1734 packet_writer_delim(&data
->writer
);
1738 packet_writer_flush(&data
->writer
);
1745 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1747 struct hashmap_iter iter
;
1748 const struct strmap_entry
*e
;
1750 if (strmap_empty(&data
->wanted_refs
))
1753 packet_writer_write(&data
->writer
, "wanted-refs\n");
1755 strmap_for_each_entry(&data
->wanted_refs
, &iter
, e
) {
1756 packet_writer_write(&data
->writer
, "%s %s\n",
1757 oid_to_hex(e
->value
),
1761 packet_writer_delim(&data
->writer
);
1764 static void send_shallow_info(struct upload_pack_data
*data
)
1766 /* No shallow info needs to be sent */
1767 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1768 !is_repository_shallow(the_repository
))
1771 packet_writer_write(&data
->writer
, "shallow-info\n");
1773 if (!send_shallow_list(data
) &&
1774 is_repository_shallow(the_repository
))
1775 deepen(data
, INFINITE_DEPTH
);
1781 FETCH_PROCESS_ARGS
= 0,
1787 int upload_pack_v2(struct repository
*r
, struct packet_reader
*request
)
1789 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1790 struct upload_pack_data data
;
1792 clear_object_flags(ALL_FLAGS
);
1794 upload_pack_data_init(&data
);
1795 data
.use_sideband
= LARGE_PACKET_MAX
;
1796 get_upload_pack_config(r
, &data
);
1798 while (state
!= FETCH_DONE
) {
1800 case FETCH_PROCESS_ARGS
:
1801 process_args(request
, &data
);
1803 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1805 * Request didn't contain any 'want' lines (and
1806 * the request does not contain
1807 * "wait-for-done", in which it is reasonable
1808 * to just send 'have's without 'want's); guess
1809 * they didn't want anything.
1812 } else if (data
.seen_haves
) {
1814 * Request had 'have' lines, so lets ACK them.
1816 state
= FETCH_SEND_ACKS
;
1819 * Request had 'want's but no 'have's so we can
1820 * immediately go to construct and send a pack.
1822 state
= FETCH_SEND_PACK
;
1825 case FETCH_SEND_ACKS
:
1826 if (process_haves_and_send_acks(&data
))
1827 state
= FETCH_SEND_PACK
;
1831 case FETCH_SEND_PACK
:
1832 send_wanted_ref_info(&data
);
1833 send_shallow_info(&data
);
1835 if (data
.uri_protocols
.nr
) {
1836 create_pack_file(&data
, &data
.uri_protocols
);
1838 packet_writer_write(&data
.writer
, "packfile\n");
1839 create_pack_file(&data
, NULL
);
1848 upload_pack_data_clear(&data
);
1852 int upload_pack_advertise(struct repository
*r
,
1853 struct strbuf
*value
)
1855 struct upload_pack_data data
;
1857 upload_pack_data_init(&data
);
1858 get_upload_pack_config(r
, &data
);
1861 strbuf_addstr(value
, "shallow wait-for-done");
1863 if (data
.allow_filter
)
1864 strbuf_addstr(value
, " filter");
1866 if (data
.allow_ref_in_want
)
1867 strbuf_addstr(value
, " ref-in-want");
1869 if (data
.allow_sideband_all
)
1870 strbuf_addstr(value
, " sideband-all");
1872 if (data
.allow_packfile_uris
)
1873 strbuf_addstr(value
, " packfile-uris");
1876 upload_pack_data_clear(&data
);