3 #include "environment.h"
9 #include "repository.h"
10 #include "object-store.h"
16 #include "list-objects.h"
17 #include "list-objects-filter.h"
18 #include "list-objects-filter-options.h"
19 #include "run-command.h"
23 #include "string-list.h"
25 #include "prio-queue.h"
28 #include "upload-pack.h"
30 #include "commit-graph.h"
31 #include "commit-reach.h"
33 #include "write-or-die.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 oid_array haves
; /* v2 only */
68 struct string_list wanted_refs
; /* v2 only */
69 struct string_list hidden_refs
;
71 struct object_array shallows
;
72 struct string_list deepen_not
;
73 struct object_array extra_edge_obj
;
75 timestamp_t deepen_since
;
80 timestamp_t oldest_have
;
82 unsigned int timeout
; /* v0 only */
86 MULTI_ACK_DETAILED
= 2
87 } multi_ack
; /* v0 only */
89 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
92 struct string_list uri_protocols
;
93 enum allow_uor allow_uor
;
95 struct list_objects_filter_options filter_options
;
96 struct string_list allowed_filters
;
98 struct packet_writer writer
;
100 const char *pack_objects_hook
;
102 unsigned stateless_rpc
: 1; /* v0 only */
103 unsigned no_done
: 1; /* v0 only */
104 unsigned daemon_mode
: 1; /* v0 only */
105 unsigned filter_capability_requested
: 1; /* v0 only */
107 unsigned use_thin_pack
: 1;
108 unsigned use_ofs_delta
: 1;
109 unsigned no_progress
: 1;
110 unsigned use_include_tag
: 1;
111 unsigned wait_for_done
: 1;
112 unsigned allow_filter
: 1;
113 unsigned allow_filter_fallback
: 1;
114 unsigned long tree_filter_max_depth
;
116 unsigned done
: 1; /* v2 only */
117 unsigned allow_ref_in_want
: 1; /* v2 only */
118 unsigned allow_sideband_all
: 1; /* v2 only */
119 unsigned advertise_sid
: 1;
122 static void upload_pack_data_init(struct upload_pack_data
*data
)
124 struct string_list symref
= STRING_LIST_INIT_DUP
;
125 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
126 struct string_list hidden_refs
= STRING_LIST_INIT_DUP
;
127 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
128 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
129 struct oid_array haves
= OID_ARRAY_INIT
;
130 struct object_array shallows
= OBJECT_ARRAY_INIT
;
131 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
132 struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
133 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
134 struct string_list allowed_filters
= STRING_LIST_INIT_DUP
;
136 memset(data
, 0, sizeof(*data
));
137 data
->symref
= symref
;
138 data
->wanted_refs
= wanted_refs
;
139 data
->hidden_refs
= hidden_refs
;
140 data
->want_obj
= want_obj
;
141 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 string_list_clear(&data
->wanted_refs
, 1);
161 string_list_clear(&data
->hidden_refs
, 0);
162 object_array_clear(&data
->want_obj
);
163 object_array_clear(&data
->have_obj
);
164 oid_array_clear(&data
->haves
);
165 object_array_clear(&data
->shallows
);
166 string_list_clear(&data
->deepen_not
, 0);
167 object_array_clear(&data
->extra_edge_obj
);
168 list_objects_filter_release(&data
->filter_options
);
169 string_list_clear(&data
->allowed_filters
, 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
, sizeof(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(the_repository
, oid
);
479 die("oops (%s)", oid_to_hex(oid
));
480 if (o
->type
== OBJ_COMMIT
) {
481 struct commit_list
*parents
;
482 struct commit
*commit
= (struct commit
*)o
;
483 if (o
->flags
& THEY_HAVE
)
484 we_knew_they_have
= 1;
486 o
->flags
|= THEY_HAVE
;
487 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
488 data
->oldest_have
= commit
->date
;
489 for (parents
= commit
->parents
;
491 parents
= parents
->next
)
492 parents
->item
->object
.flags
|= THEY_HAVE
;
494 if (!we_knew_they_have
) {
495 add_object_array(o
, NULL
, &data
->have_obj
);
501 static int got_oid(struct upload_pack_data
*data
,
502 const char *hex
, struct object_id
*oid
)
504 if (get_oid_hex(hex
, oid
))
505 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
506 if (!repo_has_object_file_with_flags(the_repository
, oid
,
507 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
509 return do_got_oid(data
, oid
);
512 static int ok_to_give_up(struct upload_pack_data
*data
)
514 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
516 if (!data
->have_obj
.nr
)
519 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
520 COMMON_KNOWN
, data
->oldest_have
,
524 static int get_common_commits(struct upload_pack_data
*data
,
525 struct packet_reader
*reader
)
527 struct object_id oid
;
528 char last_hex
[GIT_MAX_HEXSZ
+ 1];
533 save_commit_buffer
= 0;
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 is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
603 int allow_hidden_ref
= (allow_uor
&
604 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
605 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
609 * on successful case, it's up to the caller to close cmd->out
611 static int do_reachable_revlist(struct child_process
*cmd
,
612 struct object_array
*src
,
613 struct object_array
*reachable
,
614 enum allow_uor allow_uor
)
620 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
627 * If the next rev-list --stdin encounters an unknown commit,
628 * it terminates, which will cause SIGPIPE in the write loop
631 sigchain_push(SIGPIPE
, SIG_IGN
);
633 if (start_command(cmd
))
636 cmd_in
= xfdopen(cmd
->in
, "w");
638 for (i
= get_max_object_index(); 0 < i
; ) {
639 o
= get_indexed_object(--i
);
642 if (reachable
&& o
->type
== OBJ_COMMIT
)
643 o
->flags
&= ~TMP_MARK
;
644 if (!is_our_ref(o
, allow_uor
))
646 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
649 for (i
= 0; i
< src
->nr
; i
++) {
650 o
= src
->objects
[i
].item
;
651 if (is_our_ref(o
, allow_uor
)) {
653 add_object_array(o
, NULL
, reachable
);
656 if (reachable
&& o
->type
== OBJ_COMMIT
)
657 o
->flags
|= TMP_MARK
;
658 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
661 if (ferror(cmd_in
) || fflush(cmd_in
))
665 sigchain_pop(SIGPIPE
);
670 sigchain_pop(SIGPIPE
);
679 static int get_reachable_list(struct upload_pack_data
*data
,
680 struct object_array
*reachable
)
682 struct child_process cmd
= CHILD_PROCESS_INIT
;
685 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
686 const unsigned hexsz
= the_hash_algo
->hexsz
;
688 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
689 data
->allow_uor
) < 0)
692 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
693 struct object_id oid
;
696 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
699 o
= lookup_object(the_repository
, &oid
);
700 if (o
&& o
->type
== OBJ_COMMIT
) {
701 o
->flags
&= ~TMP_MARK
;
704 for (i
= get_max_object_index(); 0 < i
; i
--) {
705 o
= get_indexed_object(i
- 1);
706 if (o
&& o
->type
== OBJ_COMMIT
&&
707 (o
->flags
& TMP_MARK
)) {
708 add_object_array(o
, NULL
, reachable
);
709 o
->flags
&= ~TMP_MARK
;
714 if (finish_command(&cmd
))
720 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
722 struct child_process cmd
= CHILD_PROCESS_INIT
;
726 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
730 * The commits out of the rev-list are not ancestors of
733 i
= read_in_full(cmd
.out
, buf
, 1);
740 * rev-list may have died by encountering a bad commit
741 * in the history, in which case we do want to bail out
742 * even when it showed no commit.
744 if (finish_command(&cmd
))
747 /* All the non-tip ones are ancestors of what we advertised */
756 static void check_non_tip(struct upload_pack_data
*data
)
761 * In the normal in-process case without
762 * uploadpack.allowReachableSHA1InWant,
763 * non-tip requests can never happen.
765 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
767 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
768 /* All the non-tip ones are ancestors of what we advertised */
772 /* Pick one of them (we know there at least is one) */
773 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
774 struct object
*o
= data
->want_obj
.objects
[i
].item
;
775 if (!is_our_ref(o
, data
->allow_uor
)) {
776 packet_writer_error(&data
->writer
,
777 "upload-pack: not our ref %s",
778 oid_to_hex(&o
->oid
));
779 die("git upload-pack: not our ref %s",
780 oid_to_hex(&o
->oid
));
785 static void send_shallow(struct upload_pack_data
*data
,
786 struct commit_list
*result
)
789 struct object
*object
= &result
->item
->object
;
790 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
791 packet_writer_write(&data
->writer
, "shallow %s",
792 oid_to_hex(&object
->oid
));
793 register_shallow(the_repository
, &object
->oid
);
796 result
= result
->next
;
800 static void send_unshallow(struct upload_pack_data
*data
)
804 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
805 struct object
*object
= data
->shallows
.objects
[i
].item
;
806 if (object
->flags
& NOT_SHALLOW
) {
807 struct commit_list
*parents
;
808 packet_writer_write(&data
->writer
, "unshallow %s",
809 oid_to_hex(&object
->oid
));
810 object
->flags
&= ~CLIENT_SHALLOW
;
812 * We want to _register_ "object" as shallow, but we
813 * also need to traverse object's parents to deepen a
814 * shallow clone. Unregister it for now so we can
815 * parse and add the parents to the want list, then
818 unregister_shallow(&object
->oid
);
820 parse_commit_or_die((struct commit
*)object
);
821 parents
= ((struct commit
*)object
)->parents
;
823 add_object_array(&parents
->item
->object
,
824 NULL
, &data
->want_obj
);
825 parents
= parents
->next
;
827 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
829 /* make sure commit traversal conforms to client */
830 register_shallow(the_repository
, &object
->oid
);
834 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
835 int flag
, void *cb_data
);
836 static void deepen(struct upload_pack_data
*data
, int depth
)
838 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
841 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
842 struct object
*object
= data
->shallows
.objects
[i
].item
;
843 object
->flags
|= NOT_SHALLOW
;
845 } else if (data
->deepen_relative
) {
846 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
847 struct commit_list
*result
;
850 * Checking for reachable shallows requires that our refs be
851 * marked with OUR_REF.
853 head_ref_namespaced(check_ref
, data
);
854 for_each_namespaced_ref(check_ref
, data
);
856 get_reachable_list(data
, &reachable_shallows
);
857 result
= get_shallow_commits(&reachable_shallows
,
859 SHALLOW
, NOT_SHALLOW
);
860 send_shallow(data
, result
);
861 free_commit_list(result
);
862 object_array_clear(&reachable_shallows
);
864 struct commit_list
*result
;
866 result
= get_shallow_commits(&data
->want_obj
, depth
,
867 SHALLOW
, NOT_SHALLOW
);
868 send_shallow(data
, result
);
869 free_commit_list(result
);
872 send_unshallow(data
);
875 static void deepen_by_rev_list(struct upload_pack_data
*data
,
879 struct commit_list
*result
;
881 disable_commit_graph(the_repository
);
882 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
883 send_shallow(data
, result
);
884 free_commit_list(result
);
885 send_unshallow(data
);
888 /* Returns 1 if a shallow list is sent or 0 otherwise */
889 static int send_shallow_list(struct upload_pack_data
*data
)
893 if (data
->depth
> 0 && data
->deepen_rev_list
)
894 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
895 if (data
->depth
> 0) {
896 deepen(data
, data
->depth
);
898 } else if (data
->deepen_rev_list
) {
899 struct strvec av
= STRVEC_INIT
;
902 strvec_push(&av
, "rev-list");
903 if (data
->deepen_since
)
904 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
905 if (data
->deepen_not
.nr
) {
906 strvec_push(&av
, "--not");
907 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
908 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
909 strvec_push(&av
, s
->string
);
911 strvec_push(&av
, "--not");
913 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
914 struct object
*o
= data
->want_obj
.objects
[i
].item
;
915 strvec_push(&av
, oid_to_hex(&o
->oid
));
917 deepen_by_rev_list(data
, av
.nr
, av
.v
);
921 if (data
->shallows
.nr
> 0) {
923 for (i
= 0; i
< data
->shallows
.nr
; i
++)
924 register_shallow(the_repository
,
925 &data
->shallows
.objects
[i
].item
->oid
);
929 data
->shallow_nr
+= data
->shallows
.nr
;
933 static int process_shallow(const char *line
, struct object_array
*shallows
)
936 if (skip_prefix(line
, "shallow ", &arg
)) {
937 struct object_id oid
;
938 struct object
*object
;
939 if (get_oid_hex(arg
, &oid
))
940 die("invalid shallow line: %s", line
);
941 object
= parse_object(the_repository
, &oid
);
944 if (object
->type
!= OBJ_COMMIT
)
945 die("invalid shallow object %s", oid_to_hex(&oid
));
946 if (!(object
->flags
& CLIENT_SHALLOW
)) {
947 object
->flags
|= CLIENT_SHALLOW
;
948 add_object_array(object
, NULL
, shallows
);
956 static int process_deepen(const char *line
, int *depth
)
959 if (skip_prefix(line
, "deepen ", &arg
)) {
961 *depth
= (int)strtol(arg
, &end
, 0);
962 if (!end
|| *end
|| *depth
<= 0)
963 die("Invalid deepen: %s", line
);
970 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
973 if (skip_prefix(line
, "deepen-since ", &arg
)) {
975 *deepen_since
= parse_timestamp(arg
, &end
, 0);
976 if (!end
|| *end
|| !deepen_since
||
977 /* revisions.c's max_age -1 is special */
979 die("Invalid deepen-since: %s", line
);
980 *deepen_rev_list
= 1;
986 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
989 if (skip_prefix(line
, "deepen-not ", &arg
)) {
991 struct object_id oid
;
992 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
993 die("git upload-pack: ambiguous deepen-not: %s", line
);
994 string_list_append(deepen_not
, ref
);
996 *deepen_rev_list
= 1;
1002 NORETURN
__attribute__((format(printf
,2,3)))
1003 static void send_err_and_die(struct upload_pack_data
*data
,
1004 const char *fmt
, ...)
1006 struct strbuf buf
= STRBUF_INIT
;
1010 strbuf_vaddf(&buf
, fmt
, ap
);
1013 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1017 static void check_one_filter(struct upload_pack_data
*data
,
1018 struct list_objects_filter_options
*opts
)
1020 const char *key
= list_object_filter_config_name(opts
->choice
);
1021 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1026 allowed
= (intptr_t)item
->util
;
1028 allowed
= data
->allow_filter_fallback
;
1031 send_err_and_die(data
, "filter '%s' not supported", key
);
1033 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1034 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1035 send_err_and_die(data
,
1036 "tree filter allows max depth %lu, but got %lu",
1037 data
->tree_filter_max_depth
,
1038 opts
->tree_exclude_depth
);
1041 static void check_filter_recurse(struct upload_pack_data
*data
,
1042 struct list_objects_filter_options
*opts
)
1046 check_one_filter(data
, opts
);
1047 if (opts
->choice
!= LOFC_COMBINE
)
1050 for (i
= 0; i
< opts
->sub_nr
; i
++)
1051 check_filter_recurse(data
, &opts
->sub
[i
]);
1054 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1056 check_filter_recurse(data
, &data
->filter_options
);
1059 static void receive_needs(struct upload_pack_data
*data
,
1060 struct packet_reader
*reader
)
1062 int has_non_tip
= 0;
1064 data
->shallow_nr
= 0;
1067 const char *features
;
1068 struct object_id oid_buf
;
1072 reset_timeout(data
->timeout
);
1073 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1076 if (process_shallow(reader
->line
, &data
->shallows
))
1078 if (process_deepen(reader
->line
, &data
->depth
))
1080 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1082 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1085 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1086 if (!data
->filter_capability_requested
)
1087 die("git upload-pack: filtering capability not negotiated");
1088 list_objects_filter_die_if_populated(&data
->filter_options
);
1089 parse_list_objects_filter(&data
->filter_options
, arg
);
1090 die_if_using_banned_filter(data
);
1094 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1095 parse_oid_hex(arg
, &oid_buf
, &features
))
1096 die("git upload-pack: protocol error, "
1097 "expected to get object ID, not '%s'", reader
->line
);
1099 if (parse_feature_request(features
, "deepen-relative"))
1100 data
->deepen_relative
= 1;
1101 if (parse_feature_request(features
, "multi_ack_detailed"))
1102 data
->multi_ack
= MULTI_ACK_DETAILED
;
1103 else if (parse_feature_request(features
, "multi_ack"))
1104 data
->multi_ack
= MULTI_ACK
;
1105 if (parse_feature_request(features
, "no-done"))
1107 if (parse_feature_request(features
, "thin-pack"))
1108 data
->use_thin_pack
= 1;
1109 if (parse_feature_request(features
, "ofs-delta"))
1110 data
->use_ofs_delta
= 1;
1111 if (parse_feature_request(features
, "side-band-64k"))
1112 data
->use_sideband
= LARGE_PACKET_MAX
;
1113 else if (parse_feature_request(features
, "side-band"))
1114 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1115 if (parse_feature_request(features
, "no-progress"))
1116 data
->no_progress
= 1;
1117 if (parse_feature_request(features
, "include-tag"))
1118 data
->use_include_tag
= 1;
1119 if (data
->allow_filter
&&
1120 parse_feature_request(features
, "filter"))
1121 data
->filter_capability_requested
= 1;
1123 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1125 char *client_sid
= xstrndup(arg
, feature_len
);
1126 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1130 o
= parse_object(the_repository
, &oid_buf
);
1132 packet_writer_error(&data
->writer
,
1133 "upload-pack: not our ref %s",
1134 oid_to_hex(&oid_buf
));
1135 die("git upload-pack: not our ref %s",
1136 oid_to_hex(&oid_buf
));
1138 if (!(o
->flags
& WANTED
)) {
1140 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1141 || is_our_ref(o
, data
->allow_uor
)))
1143 add_object_array(o
, NULL
, &data
->want_obj
);
1148 * We have sent all our refs already, and the other end
1149 * should have chosen out of them. When we are operating
1150 * in the stateless RPC mode, however, their choice may
1151 * have been based on the set of older refs advertised
1152 * by another process that handled the initial request.
1155 check_non_tip(data
);
1157 if (!data
->use_sideband
&& data
->daemon_mode
)
1158 data
->no_progress
= 1;
1160 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1163 if (send_shallow_list(data
))
1167 /* return non-zero if the ref is hidden, otherwise 0 */
1168 static int mark_our_ref(const char *refname
, const char *refname_full
,
1169 const struct object_id
*oid
, const struct string_list
*hidden_refs
)
1171 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1173 if (ref_is_hidden(refname
, refname_full
, hidden_refs
)) {
1174 o
->flags
|= HIDDEN_REF
;
1177 o
->flags
|= OUR_REF
;
1181 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1182 int flag UNUSED
, void *cb_data
)
1184 const char *refname
= strip_namespace(refname_full
);
1185 struct upload_pack_data
*data
= cb_data
;
1187 mark_our_ref(refname
, refname_full
, oid
, &data
->hidden_refs
);
1191 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1193 struct string_list_item
*item
;
1197 for_each_string_list_item(item
, symref
)
1198 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1201 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1202 if (d
->advertise_sid
)
1203 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1206 static int send_ref(const char *refname
, const struct object_id
*oid
,
1207 int flag UNUSED
, void *cb_data
)
1209 static const char *capabilities
= "multi_ack thin-pack side-band"
1210 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1211 " deepen-relative no-progress include-tag multi_ack_detailed";
1212 const char *refname_nons
= strip_namespace(refname
);
1213 struct object_id peeled
;
1214 struct upload_pack_data
*data
= cb_data
;
1216 if (mark_our_ref(refname_nons
, refname
, oid
, &data
->hidden_refs
))
1220 struct strbuf symref_info
= STRBUF_INIT
;
1221 struct strbuf session_id
= STRBUF_INIT
;
1223 format_symref_info(&symref_info
, &data
->symref
);
1224 format_session_id(&session_id
, data
);
1225 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1226 oid_to_hex(oid
), refname_nons
,
1228 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1229 " allow-tip-sha1-in-want" : "",
1230 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1231 " allow-reachable-sha1-in-want" : "",
1232 data
->no_done
? " no-done" : "",
1234 data
->allow_filter
? " filter" : "",
1236 the_hash_algo
->name
,
1237 git_user_agent_sanitized());
1238 strbuf_release(&symref_info
);
1239 strbuf_release(&session_id
);
1241 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1243 capabilities
= NULL
;
1244 if (!peel_iterated_oid(oid
, &peeled
))
1245 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1249 static int find_symref(const char *refname
,
1250 const struct object_id
*oid UNUSED
,
1251 int flag
, void *cb_data
)
1253 const char *symref_target
;
1254 struct string_list_item
*item
;
1256 if ((flag
& REF_ISSYMREF
) == 0)
1258 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1259 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1260 die("'%s' is a symref but it is not?", refname
);
1261 item
= string_list_append(cb_data
, strip_namespace(refname
));
1262 item
->util
= xstrdup(strip_namespace(symref_target
));
1266 static int parse_object_filter_config(const char *var
, const char *value
,
1267 struct upload_pack_data
*data
)
1269 struct strbuf buf
= STRBUF_INIT
;
1270 const char *sub
, *key
;
1273 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1277 if (!strcmp(key
, "allow"))
1278 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1282 strbuf_add(&buf
, sub
, sub_len
);
1284 if (!strcmp(key
, "allow"))
1285 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1286 (void *)(intptr_t)git_config_bool(var
, value
);
1287 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1289 strbuf_release(&buf
);
1290 return config_error_nonbool(var
);
1292 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1293 (void *)(intptr_t)1;
1294 data
->tree_filter_max_depth
= git_config_ulong(var
, value
);
1297 strbuf_release(&buf
);
1301 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1303 struct upload_pack_data
*data
= cb_data
;
1305 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1306 if (git_config_bool(var
, value
))
1307 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1309 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1310 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1311 if (git_config_bool(var
, value
))
1312 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1314 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1315 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1316 if (git_config_bool(var
, value
))
1317 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1319 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1320 } else if (!strcmp("uploadpack.keepalive", var
)) {
1321 data
->keepalive
= git_config_int(var
, value
);
1322 if (!data
->keepalive
)
1323 data
->keepalive
= -1;
1324 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1325 data
->allow_filter
= git_config_bool(var
, value
);
1326 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1327 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1328 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1329 data
->allow_sideband_all
= git_config_bool(var
, value
);
1330 } else if (!strcmp("core.precomposeunicode", var
)) {
1331 precomposed_unicode
= git_config_bool(var
, value
);
1332 } else if (!strcmp("transfer.advertisesid", var
)) {
1333 data
->advertise_sid
= git_config_bool(var
, value
);
1336 if (parse_object_filter_config(var
, value
, data
) < 0)
1339 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
1342 static int upload_pack_protected_config(const char *var
, const char *value
, void *cb_data
)
1344 struct upload_pack_data
*data
= cb_data
;
1346 if (!strcmp("uploadpack.packobjectshook", var
))
1347 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1351 static void get_upload_pack_config(struct upload_pack_data
*data
)
1353 git_config(upload_pack_config
, data
);
1354 git_protected_config(upload_pack_protected_config
, data
);
1357 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1360 struct packet_reader reader
;
1361 struct upload_pack_data data
;
1363 upload_pack_data_init(&data
);
1364 get_upload_pack_config(&data
);
1366 data
.stateless_rpc
= stateless_rpc
;
1367 data
.timeout
= timeout
;
1369 data
.daemon_mode
= 1;
1371 head_ref_namespaced(find_symref
, &data
.symref
);
1373 if (advertise_refs
|| !data
.stateless_rpc
) {
1374 reset_timeout(data
.timeout
);
1377 head_ref_namespaced(send_ref
, &data
);
1378 for_each_namespaced_ref(send_ref
, &data
);
1380 * fflush stdout before calling advertise_shallow_grafts because send_ref
1383 fflush_or_die(stdout
);
1384 advertise_shallow_grafts(1);
1387 head_ref_namespaced(check_ref
, &data
);
1388 for_each_namespaced_ref(check_ref
, &data
);
1391 if (!advertise_refs
) {
1392 packet_reader_init(&reader
, 0, NULL
, 0,
1393 PACKET_READ_CHOMP_NEWLINE
|
1394 PACKET_READ_DIE_ON_ERR_PACKET
);
1396 receive_needs(&data
, &reader
);
1399 * An EOF at this exact point in negotiation should be
1400 * acceptable from stateless clients as they will consume the
1401 * shallow list before doing subsequent rpc with haves/etc.
1403 if (data
.stateless_rpc
)
1404 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1406 if (data
.want_obj
.nr
&&
1407 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1408 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1409 get_common_commits(&data
, &reader
);
1410 create_pack_file(&data
, NULL
);
1414 upload_pack_data_clear(&data
);
1417 static int parse_want(struct packet_writer
*writer
, const char *line
,
1418 struct object_array
*want_obj
)
1421 if (skip_prefix(line
, "want ", &arg
)) {
1422 struct object_id oid
;
1425 if (get_oid_hex(arg
, &oid
))
1426 die("git upload-pack: protocol error, "
1427 "expected to get oid, not '%s'", line
);
1429 o
= parse_object_with_flags(the_repository
, &oid
,
1430 PARSE_OBJECT_SKIP_HASH_CHECK
);
1433 packet_writer_error(writer
,
1434 "upload-pack: not our ref %s",
1436 die("git upload-pack: not our ref %s",
1440 if (!(o
->flags
& WANTED
)) {
1442 add_object_array(o
, NULL
, want_obj
);
1451 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1452 struct string_list
*wanted_refs
,
1453 struct string_list
*hidden_refs
,
1454 struct object_array
*want_obj
)
1456 const char *refname_nons
;
1457 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1458 struct object_id oid
;
1459 struct string_list_item
*item
;
1460 struct object
*o
= NULL
;
1461 struct strbuf refname
= STRBUF_INIT
;
1463 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1464 if (ref_is_hidden(refname_nons
, refname
.buf
, hidden_refs
) ||
1465 read_ref(refname
.buf
, &oid
)) {
1466 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1467 die("unknown ref %s", refname_nons
);
1469 strbuf_release(&refname
);
1471 item
= string_list_append(wanted_refs
, refname_nons
);
1472 item
->util
= oiddup(&oid
);
1474 if (!starts_with(refname_nons
, "refs/tags/")) {
1475 struct commit
*commit
= lookup_commit_in_graph(the_repository
, &oid
);
1477 o
= &commit
->object
;
1481 o
= parse_object_or_die(&oid
, refname_nons
);
1483 if (!(o
->flags
& WANTED
)) {
1485 add_object_array(o
, NULL
, want_obj
);
1494 static int parse_have(const char *line
, struct oid_array
*haves
)
1497 if (skip_prefix(line
, "have ", &arg
)) {
1498 struct object_id oid
;
1500 if (get_oid_hex(arg
, &oid
))
1501 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1502 oid_array_append(haves
, &oid
);
1509 static void process_args(struct packet_reader
*request
,
1510 struct upload_pack_data
*data
)
1512 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1513 const char *arg
= request
->line
;
1517 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1519 if (data
->allow_ref_in_want
&&
1520 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1521 &data
->hidden_refs
, &data
->want_obj
))
1523 /* process have line */
1524 if (parse_have(arg
, &data
->haves
))
1527 /* process args like thin-pack */
1528 if (!strcmp(arg
, "thin-pack")) {
1529 data
->use_thin_pack
= 1;
1532 if (!strcmp(arg
, "ofs-delta")) {
1533 data
->use_ofs_delta
= 1;
1536 if (!strcmp(arg
, "no-progress")) {
1537 data
->no_progress
= 1;
1540 if (!strcmp(arg
, "include-tag")) {
1541 data
->use_include_tag
= 1;
1544 if (!strcmp(arg
, "done")) {
1548 if (!strcmp(arg
, "wait-for-done")) {
1549 data
->wait_for_done
= 1;
1553 /* Shallow related arguments */
1554 if (process_shallow(arg
, &data
->shallows
))
1556 if (process_deepen(arg
, &data
->depth
))
1558 if (process_deepen_since(arg
, &data
->deepen_since
,
1559 &data
->deepen_rev_list
))
1561 if (process_deepen_not(arg
, &data
->deepen_not
,
1562 &data
->deepen_rev_list
))
1564 if (!strcmp(arg
, "deepen-relative")) {
1565 data
->deepen_relative
= 1;
1569 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1570 list_objects_filter_die_if_populated(&data
->filter_options
);
1571 parse_list_objects_filter(&data
->filter_options
, p
);
1572 die_if_using_banned_filter(data
);
1576 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1577 data
->allow_sideband_all
) &&
1578 !strcmp(arg
, "sideband-all")) {
1579 data
->writer
.use_sideband
= 1;
1583 if (skip_prefix(arg
, "packfile-uris ", &p
)) {
1584 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1588 /* ignore unknown lines maybe? */
1589 die("unexpected line: '%s'", arg
);
1592 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1593 string_list_clear(&data
->uri_protocols
, 0);
1595 if (request
->status
!= PACKET_READ_FLUSH
)
1596 die(_("expected flush after fetch arguments"));
1599 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1604 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1605 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1607 if (!repo_has_object_file_with_flags(the_repository
, oid
,
1608 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
1611 oid_array_append(common
, oid
);
1613 do_got_oid(data
, oid
);
1619 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1623 packet_writer_write(&data
->writer
, "acknowledgments\n");
1627 packet_writer_write(&data
->writer
, "NAK\n");
1629 for (i
= 0; i
< acks
->nr
; i
++) {
1630 packet_writer_write(&data
->writer
, "ACK %s\n",
1631 oid_to_hex(&acks
->oid
[i
]));
1634 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1636 packet_writer_write(&data
->writer
, "ready\n");
1643 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1645 struct oid_array common
= OID_ARRAY_INIT
;
1648 process_haves(data
, &common
);
1651 } else if (send_acks(data
, &common
)) {
1652 packet_writer_delim(&data
->writer
);
1656 packet_writer_flush(&data
->writer
);
1660 oid_array_clear(&data
->haves
);
1661 oid_array_clear(&common
);
1665 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1667 const struct string_list_item
*item
;
1669 if (!data
->wanted_refs
.nr
)
1672 packet_writer_write(&data
->writer
, "wanted-refs\n");
1674 for_each_string_list_item(item
, &data
->wanted_refs
) {
1675 packet_writer_write(&data
->writer
, "%s %s\n",
1676 oid_to_hex(item
->util
),
1680 packet_writer_delim(&data
->writer
);
1683 static void send_shallow_info(struct upload_pack_data
*data
)
1685 /* No shallow info needs to be sent */
1686 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1687 !is_repository_shallow(the_repository
))
1690 packet_writer_write(&data
->writer
, "shallow-info\n");
1692 if (!send_shallow_list(data
) &&
1693 is_repository_shallow(the_repository
))
1694 deepen(data
, INFINITE_DEPTH
);
1700 FETCH_PROCESS_ARGS
= 0,
1706 int upload_pack_v2(struct repository
*r UNUSED
, struct packet_reader
*request
)
1708 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1709 struct upload_pack_data data
;
1711 clear_object_flags(ALL_FLAGS
);
1713 upload_pack_data_init(&data
);
1714 data
.use_sideband
= LARGE_PACKET_MAX
;
1715 get_upload_pack_config(&data
);
1717 while (state
!= FETCH_DONE
) {
1719 case FETCH_PROCESS_ARGS
:
1720 process_args(request
, &data
);
1722 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1724 * Request didn't contain any 'want' lines (and
1725 * the request does not contain
1726 * "wait-for-done", in which it is reasonable
1727 * to just send 'have's without 'want's); guess
1728 * they didn't want anything.
1731 } else if (data
.haves
.nr
) {
1733 * Request had 'have' lines, so lets ACK them.
1735 state
= FETCH_SEND_ACKS
;
1738 * Request had 'want's but no 'have's so we can
1739 * immedietly go to construct and send a pack.
1741 state
= FETCH_SEND_PACK
;
1744 case FETCH_SEND_ACKS
:
1745 if (process_haves_and_send_acks(&data
))
1746 state
= FETCH_SEND_PACK
;
1750 case FETCH_SEND_PACK
:
1751 send_wanted_ref_info(&data
);
1752 send_shallow_info(&data
);
1754 if (data
.uri_protocols
.nr
) {
1755 create_pack_file(&data
, &data
.uri_protocols
);
1757 packet_writer_write(&data
.writer
, "packfile\n");
1758 create_pack_file(&data
, NULL
);
1767 upload_pack_data_clear(&data
);
1771 int upload_pack_advertise(struct repository
*r
,
1772 struct strbuf
*value
)
1775 int allow_filter_value
;
1776 int allow_ref_in_want
;
1777 int allow_sideband_all_value
;
1780 strbuf_addstr(value
, "shallow wait-for-done");
1782 if (!repo_config_get_bool(r
,
1783 "uploadpack.allowfilter",
1784 &allow_filter_value
) &&
1786 strbuf_addstr(value
, " filter");
1788 if (!repo_config_get_bool(r
,
1789 "uploadpack.allowrefinwant",
1790 &allow_ref_in_want
) &&
1792 strbuf_addstr(value
, " ref-in-want");
1794 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1795 (!repo_config_get_bool(r
,
1796 "uploadpack.allowsidebandall",
1797 &allow_sideband_all_value
) &&
1798 allow_sideband_all_value
))
1799 strbuf_addstr(value
, " sideband-all");
1801 if (!repo_config_get_string(r
,
1802 "uploadpack.blobpackfileuri",
1805 strbuf_addstr(value
, " packfile-uris");