1 #include "git-compat-util.h"
3 #include "environment.h"
9 #include "repository.h"
10 #include "object-store-ll.h"
11 #include "oid-array.h"
16 #include "list-objects-filter-options.h"
17 #include "run-command.h"
21 #include "string-list.h"
25 #include "upload-pack.h"
26 #include "commit-graph.h"
27 #include "commit-reach.h"
29 #include "write-or-die.h"
30 #include "json-writer.h"
32 /* Remember to update object flag allocation in object.h */
33 #define THEY_HAVE (1u << 11)
34 #define OUR_REF (1u << 12)
35 #define WANTED (1u << 13)
36 #define COMMON_KNOWN (1u << 14)
38 #define SHALLOW (1u << 16)
39 #define NOT_SHALLOW (1u << 17)
40 #define CLIENT_SHALLOW (1u << 18)
41 #define HIDDEN_REF (1u << 19)
43 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
44 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
46 /* Enum for allowed unadvertised object request (UOR) */
48 /* Allow specifying sha1 if it is a ref tip. */
49 ALLOW_TIP_SHA1
= 0x01,
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 ALLOW_REACHABLE_SHA1
= 0x02,
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
57 * Please annotate, and if possible group together, fields used only
58 * for protocol v0 or only for protocol v2.
60 struct upload_pack_data
{
61 struct string_list symref
; /* v0 only */
62 struct object_array want_obj
;
63 struct object_array have_obj
;
64 struct oid_array haves
; /* v2 only */
65 struct string_list wanted_refs
; /* v2 only */
66 struct strvec hidden_refs
;
68 struct object_array shallows
;
69 struct string_list deepen_not
;
70 struct object_array extra_edge_obj
;
72 timestamp_t deepen_since
;
77 timestamp_t oldest_have
;
79 unsigned int timeout
; /* v0 only */
83 MULTI_ACK_DETAILED
= 2
84 } multi_ack
; /* v0 only */
86 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
89 struct string_list uri_protocols
;
90 enum allow_uor allow_uor
;
92 struct list_objects_filter_options filter_options
;
93 struct string_list allowed_filters
;
95 struct packet_writer writer
;
97 const char *pack_objects_hook
;
99 unsigned stateless_rpc
: 1; /* v0 only */
100 unsigned no_done
: 1; /* v0 only */
101 unsigned daemon_mode
: 1; /* v0 only */
102 unsigned filter_capability_requested
: 1; /* v0 only */
104 unsigned use_thin_pack
: 1;
105 unsigned use_ofs_delta
: 1;
106 unsigned no_progress
: 1;
107 unsigned use_include_tag
: 1;
108 unsigned wait_for_done
: 1;
109 unsigned allow_filter
: 1;
110 unsigned allow_filter_fallback
: 1;
111 unsigned long tree_filter_max_depth
;
113 unsigned done
: 1; /* v2 only */
114 unsigned allow_ref_in_want
: 1; /* v2 only */
115 unsigned allow_sideband_all
: 1; /* v2 only */
116 unsigned allow_packfile_uris
: 1; /* v2 only */
117 unsigned advertise_sid
: 1;
118 unsigned sent_capabilities
: 1;
121 static void upload_pack_data_init(struct upload_pack_data
*data
)
123 struct string_list symref
= STRING_LIST_INIT_DUP
;
124 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
125 struct strvec hidden_refs
= STRVEC_INIT
;
126 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
127 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
128 struct oid_array haves
= OID_ARRAY_INIT
;
129 struct object_array shallows
= OBJECT_ARRAY_INIT
;
130 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
131 struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
132 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
133 struct string_list allowed_filters
= STRING_LIST_INIT_DUP
;
135 memset(data
, 0, sizeof(*data
));
136 data
->symref
= symref
;
137 data
->wanted_refs
= wanted_refs
;
138 data
->hidden_refs
= hidden_refs
;
139 data
->want_obj
= want_obj
;
140 data
->have_obj
= have_obj
;
142 data
->shallows
= shallows
;
143 data
->deepen_not
= deepen_not
;
144 data
->uri_protocols
= uri_protocols
;
145 data
->extra_edge_obj
= extra_edge_obj
;
146 data
->allowed_filters
= allowed_filters
;
147 data
->allow_filter_fallback
= 1;
148 data
->tree_filter_max_depth
= ULONG_MAX
;
149 packet_writer_init(&data
->writer
, 1);
150 list_objects_filter_init(&data
->filter_options
);
153 data
->advertise_sid
= 0;
156 static void upload_pack_data_clear(struct upload_pack_data
*data
)
158 string_list_clear(&data
->symref
, 1);
159 string_list_clear(&data
->wanted_refs
, 1);
160 strvec_clear(&data
->hidden_refs
);
161 object_array_clear(&data
->want_obj
);
162 object_array_clear(&data
->have_obj
);
163 oid_array_clear(&data
->haves
);
164 object_array_clear(&data
->shallows
);
165 string_list_clear(&data
->deepen_not
, 0);
166 object_array_clear(&data
->extra_edge_obj
);
167 list_objects_filter_release(&data
->filter_options
);
168 string_list_clear(&data
->allowed_filters
, 0);
170 free((char *)data
->pack_objects_hook
);
173 static void reset_timeout(unsigned int timeout
)
178 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
182 send_sideband(1, fd
, data
, sz
, use_sideband
);
189 /* XXX: are we happy to lose stuff here? */
190 xwrite(fd
, data
, sz
);
193 write_or_die(fd
, data
, sz
);
196 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
199 if (graft
->nr_parent
== -1)
200 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
204 struct output_state
{
206 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
207 * sideband-64k the band designator takes up 1 byte of space. Because
208 * relay_pack_data keeps the last byte to itself, we make the buffer 1
209 * byte bigger than the intended maximum write size.
211 char buffer
[(LARGE_PACKET_DATA_MAX
- 1) + 1];
213 unsigned packfile_uris_started
: 1;
214 unsigned packfile_started
: 1;
217 static int relay_pack_data(int pack_objects_out
, struct output_state
*os
,
218 int use_sideband
, int write_packfile_line
)
221 * We keep the last byte to ourselves
222 * in case we detect broken rev-list, so that we
223 * can leave the stream corrupted. This is
224 * unfortunate -- unpack-objects would happily
225 * accept a valid packdata with trailing garbage,
226 * so appending garbage after we pass all the
227 * pack data is not good enough to signal
228 * breakage to downstream.
232 readsz
= xread(pack_objects_out
, os
->buffer
+ os
->used
,
233 sizeof(os
->buffer
) - os
->used
);
239 while (!os
->packfile_started
) {
241 if (os
->used
>= 4 && !memcmp(os
->buffer
, "PACK", 4)) {
242 os
->packfile_started
= 1;
243 if (write_packfile_line
) {
244 if (os
->packfile_uris_started
)
246 packet_write_fmt(1, "\1packfile\n");
250 if ((p
= memchr(os
->buffer
, '\n', os
->used
))) {
251 if (!os
->packfile_uris_started
) {
252 os
->packfile_uris_started
= 1;
253 if (!write_packfile_line
)
254 BUG("packfile_uris requires sideband-all");
255 packet_write_fmt(1, "\1packfile-uris\n");
258 packet_write_fmt(1, "\1%s\n", os
->buffer
);
260 os
->used
-= p
- os
->buffer
+ 1;
261 memmove(os
->buffer
, p
+ 1, os
->used
);
271 send_client_data(1, os
->buffer
, os
->used
- 1, use_sideband
);
272 os
->buffer
[0] = os
->buffer
[os
->used
- 1];
275 send_client_data(1, os
->buffer
, os
->used
, use_sideband
);
282 static void create_pack_file(struct upload_pack_data
*pack_data
,
283 const struct string_list
*uri_protocols
)
285 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
286 struct output_state
*output_state
= xcalloc(1, sizeof(struct output_state
));
288 char abort_msg
[] = "aborting due to possible repository "
289 "corruption on the remote side.";
294 if (!pack_data
->pack_objects_hook
)
295 pack_objects
.git_cmd
= 1;
297 strvec_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
298 strvec_push(&pack_objects
.args
, "git");
299 pack_objects
.use_shell
= 1;
302 if (pack_data
->shallow_nr
) {
303 strvec_push(&pack_objects
.args
, "--shallow-file");
304 strvec_push(&pack_objects
.args
, "");
306 strvec_push(&pack_objects
.args
, "pack-objects");
307 strvec_push(&pack_objects
.args
, "--revs");
308 if (pack_data
->use_thin_pack
)
309 strvec_push(&pack_objects
.args
, "--thin");
311 strvec_push(&pack_objects
.args
, "--stdout");
312 if (pack_data
->shallow_nr
)
313 strvec_push(&pack_objects
.args
, "--shallow");
314 if (!pack_data
->no_progress
)
315 strvec_push(&pack_objects
.args
, "--progress");
316 if (pack_data
->use_ofs_delta
)
317 strvec_push(&pack_objects
.args
, "--delta-base-offset");
318 if (pack_data
->use_include_tag
)
319 strvec_push(&pack_objects
.args
, "--include-tag");
320 if (pack_data
->filter_options
.choice
) {
322 expand_list_objects_filter_spec(&pack_data
->filter_options
);
323 strvec_pushf(&pack_objects
.args
, "--filter=%s", spec
);
326 for (i
= 0; i
< uri_protocols
->nr
; i
++)
327 strvec_pushf(&pack_objects
.args
, "--uri-protocol=%s",
328 uri_protocols
->items
[i
].string
);
331 pack_objects
.in
= -1;
332 pack_objects
.out
= -1;
333 pack_objects
.err
= -1;
334 pack_objects
.clean_on_exit
= 1;
336 if (start_command(&pack_objects
))
337 die("git upload-pack: unable to fork git-pack-objects");
339 pipe_fd
= xfdopen(pack_objects
.in
, "w");
341 if (pack_data
->shallow_nr
)
342 for_each_commit_graft(write_one_shallow
, pipe_fd
);
344 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
345 fprintf(pipe_fd
, "%s\n",
346 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
347 fprintf(pipe_fd
, "--not\n");
348 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
349 fprintf(pipe_fd
, "%s\n",
350 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
351 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
352 fprintf(pipe_fd
, "%s\n",
353 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
354 fprintf(pipe_fd
, "\n");
358 /* We read from pack_objects.err to capture stderr output for
359 * progress bar, and pack_objects.out to capture the pack data.
363 struct pollfd pfd
[2];
364 int pe
, pu
, pollsize
, polltimeout
;
367 reset_timeout(pack_data
->timeout
);
372 if (0 <= pack_objects
.out
) {
373 pfd
[pollsize
].fd
= pack_objects
.out
;
374 pfd
[pollsize
].events
= POLLIN
;
378 if (0 <= pack_objects
.err
) {
379 pfd
[pollsize
].fd
= pack_objects
.err
;
380 pfd
[pollsize
].events
= POLLIN
;
388 polltimeout
= pack_data
->keepalive
< 0
390 : 1000 * pack_data
->keepalive
;
392 ret
= poll(pfd
, pollsize
, polltimeout
);
395 if (errno
!= EINTR
) {
396 error_errno("poll failed, resuming");
401 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
402 /* Status ready; we ship that in the side-band
403 * or dump to the standard error.
405 sz
= xread(pack_objects
.err
, progress
,
408 send_client_data(2, progress
, sz
,
409 pack_data
->use_sideband
);
411 close(pack_objects
.err
);
412 pack_objects
.err
= -1;
416 /* give priority to status messages */
419 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
420 int result
= relay_pack_data(pack_objects
.out
,
422 pack_data
->use_sideband
,
426 close(pack_objects
.out
);
427 pack_objects
.out
= -1;
428 } else if (result
< 0) {
434 * We hit the keepalive timeout without saying anything; send
435 * an empty message on the data sideband just to let the other
436 * side know we're still working on it, but don't have any data
439 * If we don't have a sideband channel, there's no room in the
440 * protocol to say anything, so those clients are just out of
443 if (!ret
&& pack_data
->use_sideband
) {
444 static const char buf
[] = "0005\1";
445 write_or_die(1, buf
, 5);
449 if (finish_command(&pack_objects
)) {
450 error("git upload-pack: git-pack-objects died with error.");
455 if (output_state
->used
> 0) {
456 send_client_data(1, output_state
->buffer
, output_state
->used
,
457 pack_data
->use_sideband
);
458 fprintf(stderr
, "flushed.\n");
461 if (pack_data
->use_sideband
)
467 send_client_data(3, abort_msg
, sizeof(abort_msg
),
468 pack_data
->use_sideband
);
469 die("git upload-pack: %s", abort_msg
);
472 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
474 int we_knew_they_have
= 0;
475 struct object
*o
= parse_object(the_repository
, oid
);
478 die("oops (%s)", oid_to_hex(oid
));
479 if (o
->type
== OBJ_COMMIT
) {
480 struct commit_list
*parents
;
481 struct commit
*commit
= (struct commit
*)o
;
482 if (o
->flags
& THEY_HAVE
)
483 we_knew_they_have
= 1;
485 o
->flags
|= THEY_HAVE
;
486 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
487 data
->oldest_have
= commit
->date
;
488 for (parents
= commit
->parents
;
490 parents
= parents
->next
)
491 parents
->item
->object
.flags
|= THEY_HAVE
;
493 if (!we_knew_they_have
) {
494 add_object_array(o
, NULL
, &data
->have_obj
);
500 static int got_oid(struct upload_pack_data
*data
,
501 const char *hex
, struct object_id
*oid
)
503 if (get_oid_hex(hex
, oid
))
504 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
505 if (!repo_has_object_file_with_flags(the_repository
, oid
,
506 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
508 return do_got_oid(data
, oid
);
511 static int ok_to_give_up(struct upload_pack_data
*data
)
513 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
515 if (!data
->have_obj
.nr
)
518 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
519 COMMON_KNOWN
, data
->oldest_have
,
523 static int get_common_commits(struct upload_pack_data
*data
,
524 struct packet_reader
*reader
)
526 struct object_id oid
;
527 char last_hex
[GIT_MAX_HEXSZ
+ 1];
532 save_commit_buffer
= 0;
537 reset_timeout(data
->timeout
);
539 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
540 if (data
->multi_ack
== MULTI_ACK_DETAILED
543 && ok_to_give_up(data
)) {
545 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
547 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
548 packet_write_fmt(1, "NAK\n");
550 if (data
->no_done
&& sent_ready
) {
551 packet_write_fmt(1, "ACK %s\n", last_hex
);
554 if (data
->stateless_rpc
)
560 if (skip_prefix(reader
->line
, "have ", &arg
)) {
561 switch (got_oid(data
, arg
, &oid
)) {
562 case -1: /* they have what we do not */
565 && ok_to_give_up(data
)) {
566 const char *hex
= oid_to_hex(&oid
);
567 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
569 packet_write_fmt(1, "ACK %s ready\n", hex
);
571 packet_write_fmt(1, "ACK %s continue\n", hex
);
576 oid_to_hex_r(last_hex
, &oid
);
577 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
578 packet_write_fmt(1, "ACK %s common\n", last_hex
);
579 else if (data
->multi_ack
)
580 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
581 else if (data
->have_obj
.nr
== 1)
582 packet_write_fmt(1, "ACK %s\n", last_hex
);
587 if (!strcmp(reader
->line
, "done")) {
588 if (data
->have_obj
.nr
> 0) {
590 packet_write_fmt(1, "ACK %s\n", last_hex
);
593 packet_write_fmt(1, "NAK\n");
596 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
600 static int allow_hidden_refs(enum allow_uor allow_uor
)
602 if ((allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
)
604 return !(allow_uor
& (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
607 static void for_each_namespaced_ref_1(each_ref_fn fn
,
608 struct upload_pack_data
*data
)
610 const char **excludes
= NULL
;
612 * If `data->allow_uor` allows fetching hidden refs, we need to
613 * mark all references (including hidden ones), to check in
614 * `is_our_ref()` below.
616 * Otherwise, we only care about whether each reference's object
617 * has the OUR_REF bit set or not, so do not need to visit
620 if (allow_hidden_refs(data
->allow_uor
))
621 excludes
= hidden_refs_to_excludes(&data
->hidden_refs
);
623 for_each_namespaced_ref(excludes
, fn
, data
);
627 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
629 return o
->flags
& ((allow_hidden_refs(allow_uor
) ? 0 : HIDDEN_REF
) | OUR_REF
);
633 * on successful case, it's up to the caller to close cmd->out
635 static int do_reachable_revlist(struct child_process
*cmd
,
636 struct object_array
*src
,
637 struct object_array
*reachable
,
638 enum allow_uor allow_uor
)
644 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
651 * If the next rev-list --stdin encounters an unknown commit,
652 * it terminates, which will cause SIGPIPE in the write loop
655 sigchain_push(SIGPIPE
, SIG_IGN
);
657 if (start_command(cmd
))
660 cmd_in
= xfdopen(cmd
->in
, "w");
662 for (i
= get_max_object_index(); 0 < i
; ) {
663 o
= get_indexed_object(--i
);
666 if (reachable
&& o
->type
== OBJ_COMMIT
)
667 o
->flags
&= ~TMP_MARK
;
668 if (!is_our_ref(o
, allow_uor
))
670 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
673 for (i
= 0; i
< src
->nr
; i
++) {
674 o
= src
->objects
[i
].item
;
675 if (is_our_ref(o
, allow_uor
)) {
677 add_object_array(o
, NULL
, reachable
);
680 if (reachable
&& o
->type
== OBJ_COMMIT
)
681 o
->flags
|= TMP_MARK
;
682 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
685 if (ferror(cmd_in
) || fflush(cmd_in
))
689 sigchain_pop(SIGPIPE
);
694 sigchain_pop(SIGPIPE
);
703 static int get_reachable_list(struct upload_pack_data
*data
,
704 struct object_array
*reachable
)
706 struct child_process cmd
= CHILD_PROCESS_INIT
;
709 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
710 const unsigned hexsz
= the_hash_algo
->hexsz
;
712 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
713 data
->allow_uor
) < 0)
716 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
717 struct object_id oid
;
720 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
723 o
= lookup_object(the_repository
, &oid
);
724 if (o
&& o
->type
== OBJ_COMMIT
) {
725 o
->flags
&= ~TMP_MARK
;
728 for (i
= get_max_object_index(); 0 < i
; i
--) {
729 o
= get_indexed_object(i
- 1);
730 if (o
&& o
->type
== OBJ_COMMIT
&&
731 (o
->flags
& TMP_MARK
)) {
732 add_object_array(o
, NULL
, reachable
);
733 o
->flags
&= ~TMP_MARK
;
738 if (finish_command(&cmd
))
744 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
746 struct child_process cmd
= CHILD_PROCESS_INIT
;
750 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
754 * The commits out of the rev-list are not ancestors of
757 i
= read_in_full(cmd
.out
, buf
, 1);
764 * rev-list may have died by encountering a bad commit
765 * in the history, in which case we do want to bail out
766 * even when it showed no commit.
768 if (finish_command(&cmd
))
771 /* All the non-tip ones are ancestors of what we advertised */
780 static void check_non_tip(struct upload_pack_data
*data
)
785 * In the normal in-process case without
786 * uploadpack.allowReachableSHA1InWant,
787 * non-tip requests can never happen.
789 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
791 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
792 /* All the non-tip ones are ancestors of what we advertised */
796 /* Pick one of them (we know there at least is one) */
797 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
798 struct object
*o
= data
->want_obj
.objects
[i
].item
;
799 if (!is_our_ref(o
, data
->allow_uor
)) {
800 error("git upload-pack: not our ref %s",
801 oid_to_hex(&o
->oid
));
802 packet_writer_error(&data
->writer
,
803 "upload-pack: not our ref %s",
804 oid_to_hex(&o
->oid
));
810 static void send_shallow(struct upload_pack_data
*data
,
811 struct commit_list
*result
)
814 struct object
*object
= &result
->item
->object
;
815 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
816 packet_writer_write(&data
->writer
, "shallow %s",
817 oid_to_hex(&object
->oid
));
818 register_shallow(the_repository
, &object
->oid
);
821 result
= result
->next
;
825 static void send_unshallow(struct upload_pack_data
*data
)
829 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
830 struct object
*object
= data
->shallows
.objects
[i
].item
;
831 if (object
->flags
& NOT_SHALLOW
) {
832 struct commit_list
*parents
;
833 packet_writer_write(&data
->writer
, "unshallow %s",
834 oid_to_hex(&object
->oid
));
835 object
->flags
&= ~CLIENT_SHALLOW
;
837 * We want to _register_ "object" as shallow, but we
838 * also need to traverse object's parents to deepen a
839 * shallow clone. Unregister it for now so we can
840 * parse and add the parents to the want list, then
843 unregister_shallow(&object
->oid
);
845 parse_commit_or_die((struct commit
*)object
);
846 parents
= ((struct commit
*)object
)->parents
;
848 add_object_array(&parents
->item
->object
,
849 NULL
, &data
->want_obj
);
850 parents
= parents
->next
;
852 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
854 /* make sure commit traversal conforms to client */
855 register_shallow(the_repository
, &object
->oid
);
859 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
860 int flag
, void *cb_data
);
861 static void deepen(struct upload_pack_data
*data
, int depth
)
863 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
866 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
867 struct object
*object
= data
->shallows
.objects
[i
].item
;
868 object
->flags
|= NOT_SHALLOW
;
870 } else if (data
->deepen_relative
) {
871 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
872 struct commit_list
*result
;
875 * Checking for reachable shallows requires that our refs be
876 * marked with OUR_REF.
878 head_ref_namespaced(check_ref
, data
);
879 for_each_namespaced_ref_1(check_ref
, data
);
881 get_reachable_list(data
, &reachable_shallows
);
882 result
= get_shallow_commits(&reachable_shallows
,
884 SHALLOW
, NOT_SHALLOW
);
885 send_shallow(data
, result
);
886 free_commit_list(result
);
887 object_array_clear(&reachable_shallows
);
889 struct commit_list
*result
;
891 result
= get_shallow_commits(&data
->want_obj
, depth
,
892 SHALLOW
, NOT_SHALLOW
);
893 send_shallow(data
, result
);
894 free_commit_list(result
);
897 send_unshallow(data
);
900 static void deepen_by_rev_list(struct upload_pack_data
*data
,
904 struct commit_list
*result
;
906 disable_commit_graph(the_repository
);
907 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
908 send_shallow(data
, result
);
909 free_commit_list(result
);
910 send_unshallow(data
);
913 /* Returns 1 if a shallow list is sent or 0 otherwise */
914 static int send_shallow_list(struct upload_pack_data
*data
)
918 if (data
->depth
> 0 && data
->deepen_rev_list
)
919 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
920 if (data
->depth
> 0) {
921 deepen(data
, data
->depth
);
923 } else if (data
->deepen_rev_list
) {
924 struct strvec av
= STRVEC_INIT
;
927 strvec_push(&av
, "rev-list");
928 if (data
->deepen_since
)
929 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
930 if (data
->deepen_not
.nr
) {
931 strvec_push(&av
, "--not");
932 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
933 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
934 strvec_push(&av
, s
->string
);
936 strvec_push(&av
, "--not");
938 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
939 struct object
*o
= data
->want_obj
.objects
[i
].item
;
940 strvec_push(&av
, oid_to_hex(&o
->oid
));
942 deepen_by_rev_list(data
, av
.nr
, av
.v
);
946 if (data
->shallows
.nr
> 0) {
948 for (i
= 0; i
< data
->shallows
.nr
; i
++)
949 register_shallow(the_repository
,
950 &data
->shallows
.objects
[i
].item
->oid
);
954 data
->shallow_nr
+= data
->shallows
.nr
;
958 static int process_shallow(const char *line
, struct object_array
*shallows
)
961 if (skip_prefix(line
, "shallow ", &arg
)) {
962 struct object_id oid
;
963 struct object
*object
;
964 if (get_oid_hex(arg
, &oid
))
965 die("invalid shallow line: %s", line
);
966 object
= parse_object(the_repository
, &oid
);
969 if (object
->type
!= OBJ_COMMIT
)
970 die("invalid shallow object %s", oid_to_hex(&oid
));
971 if (!(object
->flags
& CLIENT_SHALLOW
)) {
972 object
->flags
|= CLIENT_SHALLOW
;
973 add_object_array(object
, NULL
, shallows
);
981 static int process_deepen(const char *line
, int *depth
)
984 if (skip_prefix(line
, "deepen ", &arg
)) {
986 *depth
= (int)strtol(arg
, &end
, 0);
987 if (!end
|| *end
|| *depth
<= 0)
988 die("Invalid deepen: %s", line
);
995 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
998 if (skip_prefix(line
, "deepen-since ", &arg
)) {
1000 *deepen_since
= parse_timestamp(arg
, &end
, 0);
1001 if (!end
|| *end
|| !deepen_since
||
1002 /* revisions.c's max_age -1 is special */
1003 *deepen_since
== -1)
1004 die("Invalid deepen-since: %s", line
);
1005 *deepen_rev_list
= 1;
1011 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
1014 if (skip_prefix(line
, "deepen-not ", &arg
)) {
1016 struct object_id oid
;
1017 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
1018 die("git upload-pack: ambiguous deepen-not: %s", line
);
1019 string_list_append(deepen_not
, ref
);
1021 *deepen_rev_list
= 1;
1027 NORETURN
__attribute__((format(printf
,2,3)))
1028 static void send_err_and_die(struct upload_pack_data
*data
,
1029 const char *fmt
, ...)
1031 struct strbuf buf
= STRBUF_INIT
;
1035 strbuf_vaddf(&buf
, fmt
, ap
);
1038 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1042 static void check_one_filter(struct upload_pack_data
*data
,
1043 struct list_objects_filter_options
*opts
)
1045 const char *key
= list_object_filter_config_name(opts
->choice
);
1046 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1051 allowed
= (intptr_t)item
->util
;
1053 allowed
= data
->allow_filter_fallback
;
1056 send_err_and_die(data
, "filter '%s' not supported", key
);
1058 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1059 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1060 send_err_and_die(data
,
1061 "tree filter allows max depth %lu, but got %lu",
1062 data
->tree_filter_max_depth
,
1063 opts
->tree_exclude_depth
);
1066 static void check_filter_recurse(struct upload_pack_data
*data
,
1067 struct list_objects_filter_options
*opts
)
1071 check_one_filter(data
, opts
);
1072 if (opts
->choice
!= LOFC_COMBINE
)
1075 for (i
= 0; i
< opts
->sub_nr
; i
++)
1076 check_filter_recurse(data
, &opts
->sub
[i
]);
1079 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1081 check_filter_recurse(data
, &data
->filter_options
);
1084 static void receive_needs(struct upload_pack_data
*data
,
1085 struct packet_reader
*reader
)
1087 int has_non_tip
= 0;
1089 data
->shallow_nr
= 0;
1092 const char *features
;
1093 struct object_id oid_buf
;
1097 reset_timeout(data
->timeout
);
1098 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1101 if (process_shallow(reader
->line
, &data
->shallows
))
1103 if (process_deepen(reader
->line
, &data
->depth
))
1105 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1107 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1110 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1111 if (!data
->filter_capability_requested
)
1112 die("git upload-pack: filtering capability not negotiated");
1113 list_objects_filter_die_if_populated(&data
->filter_options
);
1114 parse_list_objects_filter(&data
->filter_options
, arg
);
1115 die_if_using_banned_filter(data
);
1119 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1120 parse_oid_hex(arg
, &oid_buf
, &features
))
1121 die("git upload-pack: protocol error, "
1122 "expected to get object ID, not '%s'", reader
->line
);
1124 if (parse_feature_request(features
, "deepen-relative"))
1125 data
->deepen_relative
= 1;
1126 if (parse_feature_request(features
, "multi_ack_detailed"))
1127 data
->multi_ack
= MULTI_ACK_DETAILED
;
1128 else if (parse_feature_request(features
, "multi_ack"))
1129 data
->multi_ack
= MULTI_ACK
;
1130 if (parse_feature_request(features
, "no-done"))
1132 if (parse_feature_request(features
, "thin-pack"))
1133 data
->use_thin_pack
= 1;
1134 if (parse_feature_request(features
, "ofs-delta"))
1135 data
->use_ofs_delta
= 1;
1136 if (parse_feature_request(features
, "side-band-64k"))
1137 data
->use_sideband
= LARGE_PACKET_MAX
;
1138 else if (parse_feature_request(features
, "side-band"))
1139 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1140 if (parse_feature_request(features
, "no-progress"))
1141 data
->no_progress
= 1;
1142 if (parse_feature_request(features
, "include-tag"))
1143 data
->use_include_tag
= 1;
1144 if (data
->allow_filter
&&
1145 parse_feature_request(features
, "filter"))
1146 data
->filter_capability_requested
= 1;
1148 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1150 char *client_sid
= xstrndup(arg
, feature_len
);
1151 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1155 o
= parse_object(the_repository
, &oid_buf
);
1157 packet_writer_error(&data
->writer
,
1158 "upload-pack: not our ref %s",
1159 oid_to_hex(&oid_buf
));
1160 die("git upload-pack: not our ref %s",
1161 oid_to_hex(&oid_buf
));
1163 if (!(o
->flags
& WANTED
)) {
1165 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1166 || is_our_ref(o
, data
->allow_uor
)))
1168 add_object_array(o
, NULL
, &data
->want_obj
);
1173 * We have sent all our refs already, and the other end
1174 * should have chosen out of them. When we are operating
1175 * in the stateless RPC mode, however, their choice may
1176 * have been based on the set of older refs advertised
1177 * by another process that handled the initial request.
1180 check_non_tip(data
);
1182 if (!data
->use_sideband
&& data
->daemon_mode
)
1183 data
->no_progress
= 1;
1185 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1188 if (send_shallow_list(data
))
1192 /* return non-zero if the ref is hidden, otherwise 0 */
1193 static int mark_our_ref(const char *refname
, const char *refname_full
,
1194 const struct object_id
*oid
, const struct strvec
*hidden_refs
)
1196 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1198 if (ref_is_hidden(refname
, refname_full
, hidden_refs
)) {
1199 o
->flags
|= HIDDEN_REF
;
1202 o
->flags
|= OUR_REF
;
1206 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1207 int flag UNUSED
, void *cb_data
)
1209 const char *refname
= strip_namespace(refname_full
);
1210 struct upload_pack_data
*data
= cb_data
;
1212 mark_our_ref(refname
, refname_full
, oid
, &data
->hidden_refs
);
1216 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1218 struct string_list_item
*item
;
1222 for_each_string_list_item(item
, symref
)
1223 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1226 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1227 if (d
->advertise_sid
)
1228 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1231 static void write_v0_ref(struct upload_pack_data
*data
,
1232 const char *refname
, const char *refname_nons
,
1233 const struct object_id
*oid
)
1235 static const char *capabilities
= "multi_ack thin-pack side-band"
1236 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1237 " deepen-relative no-progress include-tag multi_ack_detailed";
1238 struct object_id peeled
;
1240 if (mark_our_ref(refname_nons
, refname
, oid
, &data
->hidden_refs
))
1244 struct strbuf symref_info
= STRBUF_INIT
;
1245 struct strbuf session_id
= STRBUF_INIT
;
1247 format_symref_info(&symref_info
, &data
->symref
);
1248 format_session_id(&session_id
, data
);
1249 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1250 oid_to_hex(oid
), refname_nons
,
1252 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1253 " allow-tip-sha1-in-want" : "",
1254 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1255 " allow-reachable-sha1-in-want" : "",
1256 data
->no_done
? " no-done" : "",
1258 data
->allow_filter
? " filter" : "",
1260 the_hash_algo
->name
,
1261 git_user_agent_sanitized());
1262 strbuf_release(&symref_info
);
1263 strbuf_release(&session_id
);
1264 data
->sent_capabilities
= 1;
1266 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1268 capabilities
= NULL
;
1269 if (!peel_iterated_oid(oid
, &peeled
))
1270 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1274 static int send_ref(const char *refname
, const struct object_id
*oid
,
1275 int flag UNUSED
, void *cb_data
)
1277 write_v0_ref(cb_data
, refname
, strip_namespace(refname
), oid
);
1281 static int find_symref(const char *refname
,
1282 const struct object_id
*oid UNUSED
,
1283 int flag
, void *cb_data
)
1285 const char *symref_target
;
1286 struct string_list_item
*item
;
1288 if ((flag
& REF_ISSYMREF
) == 0)
1290 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1291 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1292 die("'%s' is a symref but it is not?", refname
);
1293 item
= string_list_append(cb_data
, strip_namespace(refname
));
1294 item
->util
= xstrdup(strip_namespace(symref_target
));
1298 static int parse_object_filter_config(const char *var
, const char *value
,
1299 const struct key_value_info
*kvi
,
1300 struct upload_pack_data
*data
)
1302 struct strbuf buf
= STRBUF_INIT
;
1303 const char *sub
, *key
;
1306 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1310 if (!strcmp(key
, "allow"))
1311 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1315 strbuf_add(&buf
, sub
, sub_len
);
1317 if (!strcmp(key
, "allow"))
1318 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1319 (void *)(intptr_t)git_config_bool(var
, value
);
1320 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1322 strbuf_release(&buf
);
1323 return config_error_nonbool(var
);
1325 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1326 (void *)(intptr_t)1;
1327 data
->tree_filter_max_depth
= git_config_ulong(var
, value
,
1331 strbuf_release(&buf
);
1335 static int upload_pack_config(const char *var
, const char *value
,
1336 const struct config_context
*ctx
,
1339 struct upload_pack_data
*data
= cb_data
;
1341 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1342 if (git_config_bool(var
, value
))
1343 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1345 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1346 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1347 if (git_config_bool(var
, value
))
1348 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1350 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1351 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1352 if (git_config_bool(var
, value
))
1353 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1355 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1356 } else if (!strcmp("uploadpack.keepalive", var
)) {
1357 data
->keepalive
= git_config_int(var
, value
, ctx
->kvi
);
1358 if (!data
->keepalive
)
1359 data
->keepalive
= -1;
1360 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1361 data
->allow_filter
= git_config_bool(var
, value
);
1362 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1363 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1364 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1365 data
->allow_sideband_all
= git_config_bool(var
, value
);
1366 } else if (!strcmp("uploadpack.blobpackfileuri", var
)) {
1368 data
->allow_packfile_uris
= 1;
1369 } else if (!strcmp("core.precomposeunicode", var
)) {
1370 precomposed_unicode
= git_config_bool(var
, value
);
1371 } else if (!strcmp("transfer.advertisesid", var
)) {
1372 data
->advertise_sid
= git_config_bool(var
, value
);
1375 if (parse_object_filter_config(var
, value
, ctx
->kvi
, data
) < 0)
1378 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
1381 static int upload_pack_protected_config(const char *var
, const char *value
,
1382 const struct config_context
*ctx UNUSED
,
1385 struct upload_pack_data
*data
= cb_data
;
1387 if (!strcmp("uploadpack.packobjectshook", var
))
1388 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1392 static void get_upload_pack_config(struct repository
*r
,
1393 struct upload_pack_data
*data
)
1395 repo_config(r
, upload_pack_config
, data
);
1396 git_protected_config(upload_pack_protected_config
, data
);
1398 data
->allow_sideband_all
|= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1401 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1404 struct packet_reader reader
;
1405 struct upload_pack_data data
;
1407 upload_pack_data_init(&data
);
1408 get_upload_pack_config(the_repository
, &data
);
1410 data
.stateless_rpc
= stateless_rpc
;
1411 data
.timeout
= timeout
;
1413 data
.daemon_mode
= 1;
1415 head_ref_namespaced(find_symref
, &data
.symref
);
1417 if (advertise_refs
|| !data
.stateless_rpc
) {
1418 reset_timeout(data
.timeout
);
1421 head_ref_namespaced(send_ref
, &data
);
1422 for_each_namespaced_ref_1(send_ref
, &data
);
1423 if (!data
.sent_capabilities
) {
1424 const char *refname
= "capabilities^{}";
1425 write_v0_ref(&data
, refname
, refname
, null_oid());
1428 * fflush stdout before calling advertise_shallow_grafts because send_ref
1431 fflush_or_die(stdout
);
1432 advertise_shallow_grafts(1);
1435 head_ref_namespaced(check_ref
, &data
);
1436 for_each_namespaced_ref_1(check_ref
, &data
);
1439 if (!advertise_refs
) {
1440 packet_reader_init(&reader
, 0, NULL
, 0,
1441 PACKET_READ_CHOMP_NEWLINE
|
1442 PACKET_READ_DIE_ON_ERR_PACKET
);
1444 receive_needs(&data
, &reader
);
1447 * An EOF at this exact point in negotiation should be
1448 * acceptable from stateless clients as they will consume the
1449 * shallow list before doing subsequent rpc with haves/etc.
1451 if (data
.stateless_rpc
)
1452 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1454 if (data
.want_obj
.nr
&&
1455 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1456 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1457 get_common_commits(&data
, &reader
);
1458 create_pack_file(&data
, NULL
);
1462 upload_pack_data_clear(&data
);
1465 static int parse_want(struct packet_writer
*writer
, const char *line
,
1466 struct object_array
*want_obj
)
1469 if (skip_prefix(line
, "want ", &arg
)) {
1470 struct object_id oid
;
1473 if (get_oid_hex(arg
, &oid
))
1474 die("git upload-pack: protocol error, "
1475 "expected to get oid, not '%s'", line
);
1477 o
= parse_object_with_flags(the_repository
, &oid
,
1478 PARSE_OBJECT_SKIP_HASH_CHECK
);
1481 packet_writer_error(writer
,
1482 "upload-pack: not our ref %s",
1484 die("git upload-pack: not our ref %s",
1488 if (!(o
->flags
& WANTED
)) {
1490 add_object_array(o
, NULL
, want_obj
);
1499 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1500 struct string_list
*wanted_refs
,
1501 struct strvec
*hidden_refs
,
1502 struct object_array
*want_obj
)
1504 const char *refname_nons
;
1505 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1506 struct object_id oid
;
1507 struct string_list_item
*item
;
1508 struct object
*o
= NULL
;
1509 struct strbuf refname
= STRBUF_INIT
;
1511 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1512 if (ref_is_hidden(refname_nons
, refname
.buf
, hidden_refs
) ||
1513 read_ref(refname
.buf
, &oid
)) {
1514 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1515 die("unknown ref %s", refname_nons
);
1517 strbuf_release(&refname
);
1519 item
= string_list_append(wanted_refs
, refname_nons
);
1520 item
->util
= oiddup(&oid
);
1522 if (!starts_with(refname_nons
, "refs/tags/")) {
1523 struct commit
*commit
= lookup_commit_in_graph(the_repository
, &oid
);
1525 o
= &commit
->object
;
1529 o
= parse_object_or_die(&oid
, refname_nons
);
1531 if (!(o
->flags
& WANTED
)) {
1533 add_object_array(o
, NULL
, want_obj
);
1542 static int parse_have(const char *line
, struct oid_array
*haves
)
1545 if (skip_prefix(line
, "have ", &arg
)) {
1546 struct object_id oid
;
1548 if (get_oid_hex(arg
, &oid
))
1549 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1550 oid_array_append(haves
, &oid
);
1557 static void trace2_fetch_info(struct upload_pack_data
*data
)
1559 struct json_writer jw
= JSON_WRITER_INIT
;
1561 jw_object_begin(&jw
, 0);
1562 jw_object_intmax(&jw
, "haves", data
->haves
.nr
);
1563 jw_object_intmax(&jw
, "wants", data
->want_obj
.nr
);
1564 jw_object_intmax(&jw
, "want-refs", data
->wanted_refs
.nr
);
1565 jw_object_intmax(&jw
, "depth", data
->depth
);
1566 jw_object_intmax(&jw
, "shallows", data
->shallows
.nr
);
1567 jw_object_bool(&jw
, "deepen-since", data
->deepen_since
);
1568 jw_object_intmax(&jw
, "deepen-not", data
->deepen_not
.nr
);
1569 jw_object_bool(&jw
, "deepen-relative", data
->deepen_relative
);
1570 if (data
->filter_options
.choice
)
1571 jw_object_string(&jw
, "filter", list_object_filter_config_name(data
->filter_options
.choice
));
1573 jw_object_null(&jw
, "filter");
1576 trace2_data_json("upload-pack", the_repository
, "fetch-info", &jw
);
1581 static void process_args(struct packet_reader
*request
,
1582 struct upload_pack_data
*data
)
1584 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1585 const char *arg
= request
->line
;
1589 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1591 if (data
->allow_ref_in_want
&&
1592 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1593 &data
->hidden_refs
, &data
->want_obj
))
1595 /* process have line */
1596 if (parse_have(arg
, &data
->haves
))
1599 /* process args like thin-pack */
1600 if (!strcmp(arg
, "thin-pack")) {
1601 data
->use_thin_pack
= 1;
1604 if (!strcmp(arg
, "ofs-delta")) {
1605 data
->use_ofs_delta
= 1;
1608 if (!strcmp(arg
, "no-progress")) {
1609 data
->no_progress
= 1;
1612 if (!strcmp(arg
, "include-tag")) {
1613 data
->use_include_tag
= 1;
1616 if (!strcmp(arg
, "done")) {
1620 if (!strcmp(arg
, "wait-for-done")) {
1621 data
->wait_for_done
= 1;
1625 /* Shallow related arguments */
1626 if (process_shallow(arg
, &data
->shallows
))
1628 if (process_deepen(arg
, &data
->depth
))
1630 if (process_deepen_since(arg
, &data
->deepen_since
,
1631 &data
->deepen_rev_list
))
1633 if (process_deepen_not(arg
, &data
->deepen_not
,
1634 &data
->deepen_rev_list
))
1636 if (!strcmp(arg
, "deepen-relative")) {
1637 data
->deepen_relative
= 1;
1641 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1642 list_objects_filter_die_if_populated(&data
->filter_options
);
1643 parse_list_objects_filter(&data
->filter_options
, p
);
1644 die_if_using_banned_filter(data
);
1648 if (data
->allow_sideband_all
&&
1649 !strcmp(arg
, "sideband-all")) {
1650 data
->writer
.use_sideband
= 1;
1654 if (data
->allow_packfile_uris
&&
1655 skip_prefix(arg
, "packfile-uris ", &p
)) {
1656 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1660 /* ignore unknown lines maybe? */
1661 die("unexpected line: '%s'", arg
);
1664 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1665 string_list_clear(&data
->uri_protocols
, 0);
1667 if (request
->status
!= PACKET_READ_FLUSH
)
1668 die(_("expected flush after fetch arguments"));
1670 if (trace2_is_enabled())
1671 trace2_fetch_info(data
);
1674 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1679 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1680 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1682 if (!repo_has_object_file_with_flags(the_repository
, oid
,
1683 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
1686 oid_array_append(common
, oid
);
1688 do_got_oid(data
, oid
);
1694 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1698 packet_writer_write(&data
->writer
, "acknowledgments\n");
1702 packet_writer_write(&data
->writer
, "NAK\n");
1704 for (i
= 0; i
< acks
->nr
; i
++) {
1705 packet_writer_write(&data
->writer
, "ACK %s\n",
1706 oid_to_hex(&acks
->oid
[i
]));
1709 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1711 packet_writer_write(&data
->writer
, "ready\n");
1718 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1720 struct oid_array common
= OID_ARRAY_INIT
;
1723 process_haves(data
, &common
);
1726 } else if (send_acks(data
, &common
)) {
1727 packet_writer_delim(&data
->writer
);
1731 packet_writer_flush(&data
->writer
);
1735 oid_array_clear(&data
->haves
);
1736 oid_array_clear(&common
);
1740 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1742 const struct string_list_item
*item
;
1744 if (!data
->wanted_refs
.nr
)
1747 packet_writer_write(&data
->writer
, "wanted-refs\n");
1749 for_each_string_list_item(item
, &data
->wanted_refs
) {
1750 packet_writer_write(&data
->writer
, "%s %s\n",
1751 oid_to_hex(item
->util
),
1755 packet_writer_delim(&data
->writer
);
1758 static void send_shallow_info(struct upload_pack_data
*data
)
1760 /* No shallow info needs to be sent */
1761 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1762 !is_repository_shallow(the_repository
))
1765 packet_writer_write(&data
->writer
, "shallow-info\n");
1767 if (!send_shallow_list(data
) &&
1768 is_repository_shallow(the_repository
))
1769 deepen(data
, INFINITE_DEPTH
);
1775 FETCH_PROCESS_ARGS
= 0,
1781 int upload_pack_v2(struct repository
*r
, struct packet_reader
*request
)
1783 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1784 struct upload_pack_data data
;
1786 clear_object_flags(ALL_FLAGS
);
1788 upload_pack_data_init(&data
);
1789 data
.use_sideband
= LARGE_PACKET_MAX
;
1790 get_upload_pack_config(r
, &data
);
1792 while (state
!= FETCH_DONE
) {
1794 case FETCH_PROCESS_ARGS
:
1795 process_args(request
, &data
);
1797 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1799 * Request didn't contain any 'want' lines (and
1800 * the request does not contain
1801 * "wait-for-done", in which it is reasonable
1802 * to just send 'have's without 'want's); guess
1803 * they didn't want anything.
1806 } else if (data
.haves
.nr
) {
1808 * Request had 'have' lines, so lets ACK them.
1810 state
= FETCH_SEND_ACKS
;
1813 * Request had 'want's but no 'have's so we can
1814 * immedietly go to construct and send a pack.
1816 state
= FETCH_SEND_PACK
;
1819 case FETCH_SEND_ACKS
:
1820 if (process_haves_and_send_acks(&data
))
1821 state
= FETCH_SEND_PACK
;
1825 case FETCH_SEND_PACK
:
1826 send_wanted_ref_info(&data
);
1827 send_shallow_info(&data
);
1829 if (data
.uri_protocols
.nr
) {
1830 create_pack_file(&data
, &data
.uri_protocols
);
1832 packet_writer_write(&data
.writer
, "packfile\n");
1833 create_pack_file(&data
, NULL
);
1842 upload_pack_data_clear(&data
);
1846 int upload_pack_advertise(struct repository
*r
,
1847 struct strbuf
*value
)
1849 struct upload_pack_data data
;
1851 upload_pack_data_init(&data
);
1852 get_upload_pack_config(r
, &data
);
1855 strbuf_addstr(value
, "shallow wait-for-done");
1857 if (data
.allow_filter
)
1858 strbuf_addstr(value
, " filter");
1860 if (data
.allow_ref_in_want
)
1861 strbuf_addstr(value
, " ref-in-want");
1863 if (data
.allow_sideband_all
)
1864 strbuf_addstr(value
, " sideband-all");
1866 if (data
.allow_packfile_uris
)
1867 strbuf_addstr(value
, " packfile-uris");
1870 upload_pack_data_clear(&data
);