6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
28 #include "commit-reach.h"
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
37 #define SHALLOW (1u << 16)
38 #define NOT_SHALLOW (1u << 17)
39 #define CLIENT_SHALLOW (1u << 18)
40 #define HIDDEN_REF (1u << 19)
42 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
43 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
45 /* Enum for allowed unadvertised object request (UOR) */
47 /* Allow specifying sha1 if it is a ref tip. */
48 ALLOW_TIP_SHA1
= 0x01,
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 ALLOW_REACHABLE_SHA1
= 0x02,
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 * Please annotate, and if possible group together, fields used only
57 * for protocol v0 or only for protocol v2.
59 struct upload_pack_data
{
60 struct string_list symref
; /* v0 only */
61 struct object_array want_obj
;
62 struct object_array have_obj
;
63 struct oid_array haves
; /* v2 only */
64 struct string_list wanted_refs
; /* v2 only */
66 struct object_array shallows
;
67 struct string_list deepen_not
;
68 struct object_array extra_edge_obj
;
70 timestamp_t deepen_since
;
75 timestamp_t oldest_have
;
77 unsigned int timeout
; /* v0 only */
81 MULTI_ACK_DETAILED
= 2
82 } multi_ack
; /* v0 only */
84 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
87 struct string_list uri_protocols
;
88 enum allow_uor allow_uor
;
90 struct list_objects_filter_options filter_options
;
91 struct string_list allowed_filters
;
93 struct packet_writer writer
;
95 const char *pack_objects_hook
;
97 unsigned stateless_rpc
: 1; /* v0 only */
98 unsigned no_done
: 1; /* v0 only */
99 unsigned daemon_mode
: 1; /* v0 only */
100 unsigned filter_capability_requested
: 1; /* v0 only */
102 unsigned use_thin_pack
: 1;
103 unsigned use_ofs_delta
: 1;
104 unsigned no_progress
: 1;
105 unsigned use_include_tag
: 1;
106 unsigned wait_for_done
: 1;
107 unsigned allow_filter
: 1;
108 unsigned allow_filter_fallback
: 1;
109 unsigned long tree_filter_max_depth
;
111 unsigned done
: 1; /* v2 only */
112 unsigned allow_ref_in_want
: 1; /* v2 only */
113 unsigned allow_sideband_all
: 1; /* v2 only */
114 unsigned advertise_sid
: 1;
117 static void upload_pack_data_init(struct upload_pack_data
*data
)
119 struct string_list symref
= STRING_LIST_INIT_DUP
;
120 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
121 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
122 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
123 struct oid_array haves
= OID_ARRAY_INIT
;
124 struct object_array shallows
= OBJECT_ARRAY_INIT
;
125 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
126 struct string_list uri_protocols
= STRING_LIST_INIT_DUP
;
127 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
128 struct string_list allowed_filters
= STRING_LIST_INIT_DUP
;
130 memset(data
, 0, sizeof(*data
));
131 data
->symref
= symref
;
132 data
->wanted_refs
= wanted_refs
;
133 data
->want_obj
= want_obj
;
134 data
->have_obj
= have_obj
;
136 data
->shallows
= shallows
;
137 data
->deepen_not
= deepen_not
;
138 data
->uri_protocols
= uri_protocols
;
139 data
->extra_edge_obj
= extra_edge_obj
;
140 data
->allowed_filters
= allowed_filters
;
141 data
->allow_filter_fallback
= 1;
142 data
->tree_filter_max_depth
= ULONG_MAX
;
143 packet_writer_init(&data
->writer
, 1);
146 data
->advertise_sid
= 0;
149 static void upload_pack_data_clear(struct upload_pack_data
*data
)
151 string_list_clear(&data
->symref
, 1);
152 string_list_clear(&data
->wanted_refs
, 1);
153 object_array_clear(&data
->want_obj
);
154 object_array_clear(&data
->have_obj
);
155 oid_array_clear(&data
->haves
);
156 object_array_clear(&data
->shallows
);
157 string_list_clear(&data
->deepen_not
, 0);
158 object_array_clear(&data
->extra_edge_obj
);
159 list_objects_filter_release(&data
->filter_options
);
160 string_list_clear(&data
->allowed_filters
, 0);
162 free((char *)data
->pack_objects_hook
);
165 static void reset_timeout(unsigned int timeout
)
170 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
174 send_sideband(1, fd
, data
, sz
, use_sideband
);
181 /* XXX: are we happy to lose stuff here? */
182 xwrite(fd
, data
, sz
);
185 write_or_die(fd
, data
, sz
);
188 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
191 if (graft
->nr_parent
== -1)
192 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
196 struct output_state
{
199 unsigned packfile_uris_started
: 1;
200 unsigned packfile_started
: 1;
203 static int relay_pack_data(int pack_objects_out
, struct output_state
*os
,
204 int use_sideband
, int write_packfile_line
)
207 * We keep the last byte to ourselves
208 * in case we detect broken rev-list, so that we
209 * can leave the stream corrupted. This is
210 * unfortunate -- unpack-objects would happily
211 * accept a valid packdata with trailing garbage,
212 * so appending garbage after we pass all the
213 * pack data is not good enough to signal
214 * breakage to downstream.
218 readsz
= xread(pack_objects_out
, os
->buffer
+ os
->used
,
219 sizeof(os
->buffer
) - os
->used
);
225 while (!os
->packfile_started
) {
227 if (os
->used
>= 4 && !memcmp(os
->buffer
, "PACK", 4)) {
228 os
->packfile_started
= 1;
229 if (write_packfile_line
) {
230 if (os
->packfile_uris_started
)
232 packet_write_fmt(1, "\1packfile\n");
236 if ((p
= memchr(os
->buffer
, '\n', os
->used
))) {
237 if (!os
->packfile_uris_started
) {
238 os
->packfile_uris_started
= 1;
239 if (!write_packfile_line
)
240 BUG("packfile_uris requires sideband-all");
241 packet_write_fmt(1, "\1packfile-uris\n");
244 packet_write_fmt(1, "\1%s\n", os
->buffer
);
246 os
->used
-= p
- os
->buffer
+ 1;
247 memmove(os
->buffer
, p
+ 1, os
->used
);
257 send_client_data(1, os
->buffer
, os
->used
- 1, use_sideband
);
258 os
->buffer
[0] = os
->buffer
[os
->used
- 1];
261 send_client_data(1, os
->buffer
, os
->used
, use_sideband
);
268 static void create_pack_file(struct upload_pack_data
*pack_data
,
269 const struct string_list
*uri_protocols
)
271 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
272 struct output_state output_state
= { { 0 } };
274 char abort_msg
[] = "aborting due to possible repository "
275 "corruption on the remote side.";
280 if (!pack_data
->pack_objects_hook
)
281 pack_objects
.git_cmd
= 1;
283 strvec_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
284 strvec_push(&pack_objects
.args
, "git");
285 pack_objects
.use_shell
= 1;
288 if (pack_data
->shallow_nr
) {
289 strvec_push(&pack_objects
.args
, "--shallow-file");
290 strvec_push(&pack_objects
.args
, "");
292 strvec_push(&pack_objects
.args
, "pack-objects");
293 strvec_push(&pack_objects
.args
, "--revs");
294 if (pack_data
->use_thin_pack
)
295 strvec_push(&pack_objects
.args
, "--thin");
297 strvec_push(&pack_objects
.args
, "--stdout");
298 if (pack_data
->shallow_nr
)
299 strvec_push(&pack_objects
.args
, "--shallow");
300 if (!pack_data
->no_progress
)
301 strvec_push(&pack_objects
.args
, "--progress");
302 if (pack_data
->use_ofs_delta
)
303 strvec_push(&pack_objects
.args
, "--delta-base-offset");
304 if (pack_data
->use_include_tag
)
305 strvec_push(&pack_objects
.args
, "--include-tag");
306 if (pack_data
->filter_options
.choice
) {
308 expand_list_objects_filter_spec(&pack_data
->filter_options
);
309 strvec_pushf(&pack_objects
.args
, "--filter=%s", spec
);
312 for (i
= 0; i
< uri_protocols
->nr
; i
++)
313 strvec_pushf(&pack_objects
.args
, "--uri-protocol=%s",
314 uri_protocols
->items
[i
].string
);
317 pack_objects
.in
= -1;
318 pack_objects
.out
= -1;
319 pack_objects
.err
= -1;
320 pack_objects
.clean_on_exit
= 1;
322 if (start_command(&pack_objects
))
323 die("git upload-pack: unable to fork git-pack-objects");
325 pipe_fd
= xfdopen(pack_objects
.in
, "w");
327 if (pack_data
->shallow_nr
)
328 for_each_commit_graft(write_one_shallow
, pipe_fd
);
330 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
331 fprintf(pipe_fd
, "%s\n",
332 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
333 fprintf(pipe_fd
, "--not\n");
334 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
335 fprintf(pipe_fd
, "%s\n",
336 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
337 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
338 fprintf(pipe_fd
, "%s\n",
339 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
340 fprintf(pipe_fd
, "\n");
344 /* We read from pack_objects.err to capture stderr output for
345 * progress bar, and pack_objects.out to capture the pack data.
349 struct pollfd pfd
[2];
350 int pe
, pu
, pollsize
, polltimeout
;
353 reset_timeout(pack_data
->timeout
);
358 if (0 <= pack_objects
.out
) {
359 pfd
[pollsize
].fd
= pack_objects
.out
;
360 pfd
[pollsize
].events
= POLLIN
;
364 if (0 <= pack_objects
.err
) {
365 pfd
[pollsize
].fd
= pack_objects
.err
;
366 pfd
[pollsize
].events
= POLLIN
;
374 polltimeout
= pack_data
->keepalive
< 0
376 : 1000 * pack_data
->keepalive
;
378 ret
= poll(pfd
, pollsize
, polltimeout
);
381 if (errno
!= EINTR
) {
382 error_errno("poll failed, resuming");
387 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
388 /* Status ready; we ship that in the side-band
389 * or dump to the standard error.
391 sz
= xread(pack_objects
.err
, progress
,
394 send_client_data(2, progress
, sz
,
395 pack_data
->use_sideband
);
397 close(pack_objects
.err
);
398 pack_objects
.err
= -1;
402 /* give priority to status messages */
405 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
406 int result
= relay_pack_data(pack_objects
.out
,
408 pack_data
->use_sideband
,
412 close(pack_objects
.out
);
413 pack_objects
.out
= -1;
414 } else if (result
< 0) {
420 * We hit the keepalive timeout without saying anything; send
421 * an empty message on the data sideband just to let the other
422 * side know we're still working on it, but don't have any data
425 * If we don't have a sideband channel, there's no room in the
426 * protocol to say anything, so those clients are just out of
429 if (!ret
&& pack_data
->use_sideband
) {
430 static const char buf
[] = "0005\1";
431 write_or_die(1, buf
, 5);
435 if (finish_command(&pack_objects
)) {
436 error("git upload-pack: git-pack-objects died with error.");
441 if (output_state
.used
> 0) {
442 send_client_data(1, output_state
.buffer
, output_state
.used
,
443 pack_data
->use_sideband
);
444 fprintf(stderr
, "flushed.\n");
446 if (pack_data
->use_sideband
)
451 send_client_data(3, abort_msg
, sizeof(abort_msg
),
452 pack_data
->use_sideband
);
453 die("git upload-pack: %s", abort_msg
);
456 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
458 int we_knew_they_have
= 0;
459 struct object
*o
= parse_object(the_repository
, oid
);
462 die("oops (%s)", oid_to_hex(oid
));
463 if (o
->type
== OBJ_COMMIT
) {
464 struct commit_list
*parents
;
465 struct commit
*commit
= (struct commit
*)o
;
466 if (o
->flags
& THEY_HAVE
)
467 we_knew_they_have
= 1;
469 o
->flags
|= THEY_HAVE
;
470 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
471 data
->oldest_have
= commit
->date
;
472 for (parents
= commit
->parents
;
474 parents
= parents
->next
)
475 parents
->item
->object
.flags
|= THEY_HAVE
;
477 if (!we_knew_they_have
) {
478 add_object_array(o
, NULL
, &data
->have_obj
);
484 static int got_oid(struct upload_pack_data
*data
,
485 const char *hex
, struct object_id
*oid
)
487 if (get_oid_hex(hex
, oid
))
488 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
489 if (!has_object_file_with_flags(oid
,
490 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
492 return do_got_oid(data
, oid
);
495 static int ok_to_give_up(struct upload_pack_data
*data
)
497 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
499 if (!data
->have_obj
.nr
)
502 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
503 COMMON_KNOWN
, data
->oldest_have
,
507 static int get_common_commits(struct upload_pack_data
*data
,
508 struct packet_reader
*reader
)
510 struct object_id oid
;
511 char last_hex
[GIT_MAX_HEXSZ
+ 1];
516 save_commit_buffer
= 0;
521 reset_timeout(data
->timeout
);
523 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
524 if (data
->multi_ack
== MULTI_ACK_DETAILED
527 && ok_to_give_up(data
)) {
529 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
531 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
532 packet_write_fmt(1, "NAK\n");
534 if (data
->no_done
&& sent_ready
) {
535 packet_write_fmt(1, "ACK %s\n", last_hex
);
538 if (data
->stateless_rpc
)
544 if (skip_prefix(reader
->line
, "have ", &arg
)) {
545 switch (got_oid(data
, arg
, &oid
)) {
546 case -1: /* they have what we do not */
549 && ok_to_give_up(data
)) {
550 const char *hex
= oid_to_hex(&oid
);
551 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
553 packet_write_fmt(1, "ACK %s ready\n", hex
);
555 packet_write_fmt(1, "ACK %s continue\n", hex
);
560 oid_to_hex_r(last_hex
, &oid
);
561 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
562 packet_write_fmt(1, "ACK %s common\n", last_hex
);
563 else if (data
->multi_ack
)
564 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
565 else if (data
->have_obj
.nr
== 1)
566 packet_write_fmt(1, "ACK %s\n", last_hex
);
571 if (!strcmp(reader
->line
, "done")) {
572 if (data
->have_obj
.nr
> 0) {
574 packet_write_fmt(1, "ACK %s\n", last_hex
);
577 packet_write_fmt(1, "NAK\n");
580 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
584 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
586 int allow_hidden_ref
= (allow_uor
&
587 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
588 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
592 * on successful case, it's up to the caller to close cmd->out
594 static int do_reachable_revlist(struct child_process
*cmd
,
595 struct object_array
*src
,
596 struct object_array
*reachable
,
597 enum allow_uor allow_uor
)
603 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
610 * If the next rev-list --stdin encounters an unknown commit,
611 * it terminates, which will cause SIGPIPE in the write loop
614 sigchain_push(SIGPIPE
, SIG_IGN
);
616 if (start_command(cmd
))
619 cmd_in
= xfdopen(cmd
->in
, "w");
621 for (i
= get_max_object_index(); 0 < i
; ) {
622 o
= get_indexed_object(--i
);
625 if (reachable
&& o
->type
== OBJ_COMMIT
)
626 o
->flags
&= ~TMP_MARK
;
627 if (!is_our_ref(o
, allow_uor
))
629 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
632 for (i
= 0; i
< src
->nr
; i
++) {
633 o
= src
->objects
[i
].item
;
634 if (is_our_ref(o
, allow_uor
)) {
636 add_object_array(o
, NULL
, reachable
);
639 if (reachable
&& o
->type
== OBJ_COMMIT
)
640 o
->flags
|= TMP_MARK
;
641 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
644 if (ferror(cmd_in
) || fflush(cmd_in
))
648 sigchain_pop(SIGPIPE
);
653 sigchain_pop(SIGPIPE
);
662 static int get_reachable_list(struct upload_pack_data
*data
,
663 struct object_array
*reachable
)
665 struct child_process cmd
= CHILD_PROCESS_INIT
;
668 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
669 const unsigned hexsz
= the_hash_algo
->hexsz
;
671 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
672 data
->allow_uor
) < 0)
675 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
676 struct object_id oid
;
679 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
682 o
= lookup_object(the_repository
, &oid
);
683 if (o
&& o
->type
== OBJ_COMMIT
) {
684 o
->flags
&= ~TMP_MARK
;
687 for (i
= get_max_object_index(); 0 < i
; i
--) {
688 o
= get_indexed_object(i
- 1);
689 if (o
&& o
->type
== OBJ_COMMIT
&&
690 (o
->flags
& TMP_MARK
)) {
691 add_object_array(o
, NULL
, reachable
);
692 o
->flags
&= ~TMP_MARK
;
697 if (finish_command(&cmd
))
703 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
705 struct child_process cmd
= CHILD_PROCESS_INIT
;
709 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
713 * The commits out of the rev-list are not ancestors of
716 i
= read_in_full(cmd
.out
, buf
, 1);
723 * rev-list may have died by encountering a bad commit
724 * in the history, in which case we do want to bail out
725 * even when it showed no commit.
727 if (finish_command(&cmd
))
730 /* All the non-tip ones are ancestors of what we advertised */
739 static void check_non_tip(struct upload_pack_data
*data
)
744 * In the normal in-process case without
745 * uploadpack.allowReachableSHA1InWant,
746 * non-tip requests can never happen.
748 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
750 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
751 /* All the non-tip ones are ancestors of what we advertised */
755 /* Pick one of them (we know there at least is one) */
756 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
757 struct object
*o
= data
->want_obj
.objects
[i
].item
;
758 if (!is_our_ref(o
, data
->allow_uor
)) {
759 packet_writer_error(&data
->writer
,
760 "upload-pack: not our ref %s",
761 oid_to_hex(&o
->oid
));
762 die("git upload-pack: not our ref %s",
763 oid_to_hex(&o
->oid
));
768 static void send_shallow(struct upload_pack_data
*data
,
769 struct commit_list
*result
)
772 struct object
*object
= &result
->item
->object
;
773 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
774 packet_writer_write(&data
->writer
, "shallow %s",
775 oid_to_hex(&object
->oid
));
776 register_shallow(the_repository
, &object
->oid
);
779 result
= result
->next
;
783 static void send_unshallow(struct upload_pack_data
*data
)
787 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
788 struct object
*object
= data
->shallows
.objects
[i
].item
;
789 if (object
->flags
& NOT_SHALLOW
) {
790 struct commit_list
*parents
;
791 packet_writer_write(&data
->writer
, "unshallow %s",
792 oid_to_hex(&object
->oid
));
793 object
->flags
&= ~CLIENT_SHALLOW
;
795 * We want to _register_ "object" as shallow, but we
796 * also need to traverse object's parents to deepen a
797 * shallow clone. Unregister it for now so we can
798 * parse and add the parents to the want list, then
801 unregister_shallow(&object
->oid
);
803 parse_commit_or_die((struct commit
*)object
);
804 parents
= ((struct commit
*)object
)->parents
;
806 add_object_array(&parents
->item
->object
,
807 NULL
, &data
->want_obj
);
808 parents
= parents
->next
;
810 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
812 /* make sure commit traversal conforms to client */
813 register_shallow(the_repository
, &object
->oid
);
817 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
818 int flag
, void *cb_data
);
819 static void deepen(struct upload_pack_data
*data
, int depth
)
821 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
824 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
825 struct object
*object
= data
->shallows
.objects
[i
].item
;
826 object
->flags
|= NOT_SHALLOW
;
828 } else if (data
->deepen_relative
) {
829 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
830 struct commit_list
*result
;
833 * Checking for reachable shallows requires that our refs be
834 * marked with OUR_REF.
836 head_ref_namespaced(check_ref
, NULL
);
837 for_each_namespaced_ref(check_ref
, NULL
);
839 get_reachable_list(data
, &reachable_shallows
);
840 result
= get_shallow_commits(&reachable_shallows
,
842 SHALLOW
, NOT_SHALLOW
);
843 send_shallow(data
, result
);
844 free_commit_list(result
);
845 object_array_clear(&reachable_shallows
);
847 struct commit_list
*result
;
849 result
= get_shallow_commits(&data
->want_obj
, depth
,
850 SHALLOW
, NOT_SHALLOW
);
851 send_shallow(data
, result
);
852 free_commit_list(result
);
855 send_unshallow(data
);
858 static void deepen_by_rev_list(struct upload_pack_data
*data
,
862 struct commit_list
*result
;
864 disable_commit_graph(the_repository
);
865 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
866 send_shallow(data
, result
);
867 free_commit_list(result
);
868 send_unshallow(data
);
871 /* Returns 1 if a shallow list is sent or 0 otherwise */
872 static int send_shallow_list(struct upload_pack_data
*data
)
876 if (data
->depth
> 0 && data
->deepen_rev_list
)
877 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
878 if (data
->depth
> 0) {
879 deepen(data
, data
->depth
);
881 } else if (data
->deepen_rev_list
) {
882 struct strvec av
= STRVEC_INIT
;
885 strvec_push(&av
, "rev-list");
886 if (data
->deepen_since
)
887 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
888 if (data
->deepen_not
.nr
) {
889 strvec_push(&av
, "--not");
890 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
891 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
892 strvec_push(&av
, s
->string
);
894 strvec_push(&av
, "--not");
896 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
897 struct object
*o
= data
->want_obj
.objects
[i
].item
;
898 strvec_push(&av
, oid_to_hex(&o
->oid
));
900 deepen_by_rev_list(data
, av
.nr
, av
.v
);
904 if (data
->shallows
.nr
> 0) {
906 for (i
= 0; i
< data
->shallows
.nr
; i
++)
907 register_shallow(the_repository
,
908 &data
->shallows
.objects
[i
].item
->oid
);
912 data
->shallow_nr
+= data
->shallows
.nr
;
916 static int process_shallow(const char *line
, struct object_array
*shallows
)
919 if (skip_prefix(line
, "shallow ", &arg
)) {
920 struct object_id oid
;
921 struct object
*object
;
922 if (get_oid_hex(arg
, &oid
))
923 die("invalid shallow line: %s", line
);
924 object
= parse_object(the_repository
, &oid
);
927 if (object
->type
!= OBJ_COMMIT
)
928 die("invalid shallow object %s", oid_to_hex(&oid
));
929 if (!(object
->flags
& CLIENT_SHALLOW
)) {
930 object
->flags
|= CLIENT_SHALLOW
;
931 add_object_array(object
, NULL
, shallows
);
939 static int process_deepen(const char *line
, int *depth
)
942 if (skip_prefix(line
, "deepen ", &arg
)) {
944 *depth
= (int)strtol(arg
, &end
, 0);
945 if (!end
|| *end
|| *depth
<= 0)
946 die("Invalid deepen: %s", line
);
953 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
956 if (skip_prefix(line
, "deepen-since ", &arg
)) {
958 *deepen_since
= parse_timestamp(arg
, &end
, 0);
959 if (!end
|| *end
|| !deepen_since
||
960 /* revisions.c's max_age -1 is special */
962 die("Invalid deepen-since: %s", line
);
963 *deepen_rev_list
= 1;
969 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
972 if (skip_prefix(line
, "deepen-not ", &arg
)) {
974 struct object_id oid
;
975 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
976 die("git upload-pack: ambiguous deepen-not: %s", line
);
977 string_list_append(deepen_not
, ref
);
979 *deepen_rev_list
= 1;
985 NORETURN
__attribute__((format(printf
,2,3)))
986 static void send_err_and_die(struct upload_pack_data
*data
,
987 const char *fmt
, ...)
989 struct strbuf buf
= STRBUF_INIT
;
993 strbuf_vaddf(&buf
, fmt
, ap
);
996 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1000 static void check_one_filter(struct upload_pack_data
*data
,
1001 struct list_objects_filter_options
*opts
)
1003 const char *key
= list_object_filter_config_name(opts
->choice
);
1004 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1009 allowed
= (intptr_t)item
->util
;
1011 allowed
= data
->allow_filter_fallback
;
1014 send_err_and_die(data
, "filter '%s' not supported", key
);
1016 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1017 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1018 send_err_and_die(data
,
1019 "tree filter allows max depth %lu, but got %lu",
1020 data
->tree_filter_max_depth
,
1021 opts
->tree_exclude_depth
);
1024 static void check_filter_recurse(struct upload_pack_data
*data
,
1025 struct list_objects_filter_options
*opts
)
1029 check_one_filter(data
, opts
);
1030 if (opts
->choice
!= LOFC_COMBINE
)
1033 for (i
= 0; i
< opts
->sub_nr
; i
++)
1034 check_filter_recurse(data
, &opts
->sub
[i
]);
1037 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1039 check_filter_recurse(data
, &data
->filter_options
);
1042 static void receive_needs(struct upload_pack_data
*data
,
1043 struct packet_reader
*reader
)
1045 int has_non_tip
= 0;
1047 data
->shallow_nr
= 0;
1050 const char *features
;
1051 struct object_id oid_buf
;
1055 reset_timeout(data
->timeout
);
1056 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1059 if (process_shallow(reader
->line
, &data
->shallows
))
1061 if (process_deepen(reader
->line
, &data
->depth
))
1063 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1065 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1068 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1069 if (!data
->filter_capability_requested
)
1070 die("git upload-pack: filtering capability not negotiated");
1071 list_objects_filter_die_if_populated(&data
->filter_options
);
1072 parse_list_objects_filter(&data
->filter_options
, arg
);
1073 die_if_using_banned_filter(data
);
1077 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1078 parse_oid_hex(arg
, &oid_buf
, &features
))
1079 die("git upload-pack: protocol error, "
1080 "expected to get object ID, not '%s'", reader
->line
);
1082 if (parse_feature_request(features
, "deepen-relative"))
1083 data
->deepen_relative
= 1;
1084 if (parse_feature_request(features
, "multi_ack_detailed"))
1085 data
->multi_ack
= MULTI_ACK_DETAILED
;
1086 else if (parse_feature_request(features
, "multi_ack"))
1087 data
->multi_ack
= MULTI_ACK
;
1088 if (parse_feature_request(features
, "no-done"))
1090 if (parse_feature_request(features
, "thin-pack"))
1091 data
->use_thin_pack
= 1;
1092 if (parse_feature_request(features
, "ofs-delta"))
1093 data
->use_ofs_delta
= 1;
1094 if (parse_feature_request(features
, "side-band-64k"))
1095 data
->use_sideband
= LARGE_PACKET_MAX
;
1096 else if (parse_feature_request(features
, "side-band"))
1097 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1098 if (parse_feature_request(features
, "no-progress"))
1099 data
->no_progress
= 1;
1100 if (parse_feature_request(features
, "include-tag"))
1101 data
->use_include_tag
= 1;
1102 if (data
->allow_filter
&&
1103 parse_feature_request(features
, "filter"))
1104 data
->filter_capability_requested
= 1;
1106 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1108 char *client_sid
= xstrndup(arg
, feature_len
);
1109 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1113 o
= parse_object(the_repository
, &oid_buf
);
1115 packet_writer_error(&data
->writer
,
1116 "upload-pack: not our ref %s",
1117 oid_to_hex(&oid_buf
));
1118 die("git upload-pack: not our ref %s",
1119 oid_to_hex(&oid_buf
));
1121 if (!(o
->flags
& WANTED
)) {
1123 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1124 || is_our_ref(o
, data
->allow_uor
)))
1126 add_object_array(o
, NULL
, &data
->want_obj
);
1131 * We have sent all our refs already, and the other end
1132 * should have chosen out of them. When we are operating
1133 * in the stateless RPC mode, however, their choice may
1134 * have been based on the set of older refs advertised
1135 * by another process that handled the initial request.
1138 check_non_tip(data
);
1140 if (!data
->use_sideband
&& data
->daemon_mode
)
1141 data
->no_progress
= 1;
1143 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1146 if (send_shallow_list(data
))
1150 /* return non-zero if the ref is hidden, otherwise 0 */
1151 static int mark_our_ref(const char *refname
, const char *refname_full
,
1152 const struct object_id
*oid
)
1154 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1156 if (ref_is_hidden(refname
, refname_full
)) {
1157 o
->flags
|= HIDDEN_REF
;
1160 o
->flags
|= OUR_REF
;
1164 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1165 int flag
, void *cb_data
)
1167 const char *refname
= strip_namespace(refname_full
);
1169 mark_our_ref(refname
, refname_full
, oid
);
1173 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1175 struct string_list_item
*item
;
1179 for_each_string_list_item(item
, symref
)
1180 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1183 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1184 if (d
->advertise_sid
)
1185 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1188 static int send_ref(const char *refname
, const struct object_id
*oid
,
1189 int flag
, void *cb_data
)
1191 static const char *capabilities
= "multi_ack thin-pack side-band"
1192 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1193 " deepen-relative no-progress include-tag multi_ack_detailed";
1194 const char *refname_nons
= strip_namespace(refname
);
1195 struct object_id peeled
;
1196 struct upload_pack_data
*data
= cb_data
;
1198 if (mark_our_ref(refname_nons
, refname
, oid
))
1202 struct strbuf symref_info
= STRBUF_INIT
;
1203 struct strbuf session_id
= STRBUF_INIT
;
1205 format_symref_info(&symref_info
, &data
->symref
);
1206 format_session_id(&session_id
, data
);
1207 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1208 oid_to_hex(oid
), refname_nons
,
1210 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1211 " allow-tip-sha1-in-want" : "",
1212 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1213 " allow-reachable-sha1-in-want" : "",
1214 data
->no_done
? " no-done" : "",
1216 data
->allow_filter
? " filter" : "",
1218 the_hash_algo
->name
,
1219 git_user_agent_sanitized());
1220 strbuf_release(&symref_info
);
1221 strbuf_release(&session_id
);
1223 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1225 capabilities
= NULL
;
1226 if (!peel_iterated_oid(oid
, &peeled
))
1227 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1231 static int find_symref(const char *refname
, const struct object_id
*oid
,
1232 int flag
, void *cb_data
)
1234 const char *symref_target
;
1235 struct string_list_item
*item
;
1237 if ((flag
& REF_ISSYMREF
) == 0)
1239 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1240 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1241 die("'%s' is a symref but it is not?", refname
);
1242 item
= string_list_append(cb_data
, strip_namespace(refname
));
1243 item
->util
= xstrdup(strip_namespace(symref_target
));
1247 static int parse_object_filter_config(const char *var
, const char *value
,
1248 struct upload_pack_data
*data
)
1250 struct strbuf buf
= STRBUF_INIT
;
1251 const char *sub
, *key
;
1254 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1258 if (!strcmp(key
, "allow"))
1259 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1263 strbuf_add(&buf
, sub
, sub_len
);
1265 if (!strcmp(key
, "allow"))
1266 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1267 (void *)(intptr_t)git_config_bool(var
, value
);
1268 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1270 strbuf_release(&buf
);
1271 return config_error_nonbool(var
);
1273 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1274 (void *)(intptr_t)1;
1275 data
->tree_filter_max_depth
= git_config_ulong(var
, value
);
1278 strbuf_release(&buf
);
1282 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1284 struct upload_pack_data
*data
= cb_data
;
1286 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1287 if (git_config_bool(var
, value
))
1288 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1290 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1291 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1292 if (git_config_bool(var
, value
))
1293 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1295 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1296 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1297 if (git_config_bool(var
, value
))
1298 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1300 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1301 } else if (!strcmp("uploadpack.keepalive", var
)) {
1302 data
->keepalive
= git_config_int(var
, value
);
1303 if (!data
->keepalive
)
1304 data
->keepalive
= -1;
1305 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1306 data
->allow_filter
= git_config_bool(var
, value
);
1307 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1308 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1309 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1310 data
->allow_sideband_all
= git_config_bool(var
, value
);
1311 } else if (!strcmp("core.precomposeunicode", var
)) {
1312 precomposed_unicode
= git_config_bool(var
, value
);
1313 } else if (!strcmp("transfer.advertisesid", var
)) {
1314 data
->advertise_sid
= git_config_bool(var
, value
);
1317 if (current_config_scope() != CONFIG_SCOPE_LOCAL
&&
1318 current_config_scope() != CONFIG_SCOPE_WORKTREE
) {
1319 if (!strcmp("uploadpack.packobjectshook", var
))
1320 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1323 if (parse_object_filter_config(var
, value
, data
) < 0)
1326 return parse_hide_refs_config(var
, value
, "uploadpack");
1329 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1332 struct packet_reader reader
;
1333 struct upload_pack_data data
;
1335 upload_pack_data_init(&data
);
1337 git_config(upload_pack_config
, &data
);
1339 data
.stateless_rpc
= stateless_rpc
;
1340 data
.timeout
= timeout
;
1342 data
.daemon_mode
= 1;
1344 head_ref_namespaced(find_symref
, &data
.symref
);
1346 if (advertise_refs
|| !data
.stateless_rpc
) {
1347 reset_timeout(data
.timeout
);
1350 head_ref_namespaced(send_ref
, &data
);
1351 for_each_namespaced_ref(send_ref
, &data
);
1353 * fflush stdout before calling advertise_shallow_grafts because send_ref
1356 fflush_or_die(stdout
);
1357 advertise_shallow_grafts(1);
1360 head_ref_namespaced(check_ref
, NULL
);
1361 for_each_namespaced_ref(check_ref
, NULL
);
1364 if (!advertise_refs
) {
1365 packet_reader_init(&reader
, 0, NULL
, 0,
1366 PACKET_READ_CHOMP_NEWLINE
|
1367 PACKET_READ_DIE_ON_ERR_PACKET
);
1369 receive_needs(&data
, &reader
);
1372 * An EOF at this exact point in negotiation should be
1373 * acceptable from stateless clients as they will consume the
1374 * shallow list before doing subsequent rpc with haves/etc.
1376 if (data
.stateless_rpc
)
1377 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1379 if (data
.want_obj
.nr
&&
1380 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1381 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1382 get_common_commits(&data
, &reader
);
1383 create_pack_file(&data
, NULL
);
1387 upload_pack_data_clear(&data
);
1390 static int parse_want(struct packet_writer
*writer
, const char *line
,
1391 struct object_array
*want_obj
)
1394 if (skip_prefix(line
, "want ", &arg
)) {
1395 struct object_id oid
;
1398 if (get_oid_hex(arg
, &oid
))
1399 die("git upload-pack: protocol error, "
1400 "expected to get oid, not '%s'", line
);
1402 o
= parse_object(the_repository
, &oid
);
1404 packet_writer_error(writer
,
1405 "upload-pack: not our ref %s",
1407 die("git upload-pack: not our ref %s",
1411 if (!(o
->flags
& WANTED
)) {
1413 add_object_array(o
, NULL
, want_obj
);
1422 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1423 struct string_list
*wanted_refs
,
1424 struct object_array
*want_obj
)
1426 const char *refname_nons
;
1427 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1428 struct object_id oid
;
1429 struct string_list_item
*item
;
1431 struct strbuf refname
= STRBUF_INIT
;
1433 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1434 if (ref_is_hidden(refname_nons
, refname
.buf
) ||
1435 read_ref(refname
.buf
, &oid
)) {
1436 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1437 die("unknown ref %s", refname_nons
);
1439 strbuf_release(&refname
);
1441 item
= string_list_append(wanted_refs
, refname_nons
);
1442 item
->util
= oiddup(&oid
);
1444 o
= parse_object_or_die(&oid
, refname_nons
);
1445 if (!(o
->flags
& WANTED
)) {
1447 add_object_array(o
, NULL
, want_obj
);
1456 static int parse_have(const char *line
, struct oid_array
*haves
)
1459 if (skip_prefix(line
, "have ", &arg
)) {
1460 struct object_id oid
;
1462 if (get_oid_hex(arg
, &oid
))
1463 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1464 oid_array_append(haves
, &oid
);
1471 static void process_args(struct packet_reader
*request
,
1472 struct upload_pack_data
*data
)
1474 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1475 const char *arg
= request
->line
;
1479 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1481 if (data
->allow_ref_in_want
&&
1482 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1485 /* process have line */
1486 if (parse_have(arg
, &data
->haves
))
1489 /* process args like thin-pack */
1490 if (!strcmp(arg
, "thin-pack")) {
1491 data
->use_thin_pack
= 1;
1494 if (!strcmp(arg
, "ofs-delta")) {
1495 data
->use_ofs_delta
= 1;
1498 if (!strcmp(arg
, "no-progress")) {
1499 data
->no_progress
= 1;
1502 if (!strcmp(arg
, "include-tag")) {
1503 data
->use_include_tag
= 1;
1506 if (!strcmp(arg
, "done")) {
1510 if (!strcmp(arg
, "wait-for-done")) {
1511 data
->wait_for_done
= 1;
1515 /* Shallow related arguments */
1516 if (process_shallow(arg
, &data
->shallows
))
1518 if (process_deepen(arg
, &data
->depth
))
1520 if (process_deepen_since(arg
, &data
->deepen_since
,
1521 &data
->deepen_rev_list
))
1523 if (process_deepen_not(arg
, &data
->deepen_not
,
1524 &data
->deepen_rev_list
))
1526 if (!strcmp(arg
, "deepen-relative")) {
1527 data
->deepen_relative
= 1;
1531 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1532 list_objects_filter_die_if_populated(&data
->filter_options
);
1533 parse_list_objects_filter(&data
->filter_options
, p
);
1534 die_if_using_banned_filter(data
);
1538 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1539 data
->allow_sideband_all
) &&
1540 !strcmp(arg
, "sideband-all")) {
1541 data
->writer
.use_sideband
= 1;
1545 if (skip_prefix(arg
, "packfile-uris ", &p
)) {
1546 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1550 /* ignore unknown lines maybe? */
1551 die("unexpected line: '%s'", arg
);
1554 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1555 string_list_clear(&data
->uri_protocols
, 0);
1557 if (request
->status
!= PACKET_READ_FLUSH
)
1558 die(_("expected flush after fetch arguments"));
1561 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1566 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1567 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1569 if (!has_object_file_with_flags(oid
,
1570 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
1573 oid_array_append(common
, oid
);
1575 do_got_oid(data
, oid
);
1581 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1585 packet_writer_write(&data
->writer
, "acknowledgments\n");
1589 packet_writer_write(&data
->writer
, "NAK\n");
1591 for (i
= 0; i
< acks
->nr
; i
++) {
1592 packet_writer_write(&data
->writer
, "ACK %s\n",
1593 oid_to_hex(&acks
->oid
[i
]));
1596 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1598 packet_writer_write(&data
->writer
, "ready\n");
1605 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1607 struct oid_array common
= OID_ARRAY_INIT
;
1610 process_haves(data
, &common
);
1613 } else if (send_acks(data
, &common
)) {
1614 packet_writer_delim(&data
->writer
);
1618 packet_writer_flush(&data
->writer
);
1622 oid_array_clear(&data
->haves
);
1623 oid_array_clear(&common
);
1627 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1629 const struct string_list_item
*item
;
1631 if (!data
->wanted_refs
.nr
)
1634 packet_writer_write(&data
->writer
, "wanted-refs\n");
1636 for_each_string_list_item(item
, &data
->wanted_refs
) {
1637 packet_writer_write(&data
->writer
, "%s %s\n",
1638 oid_to_hex(item
->util
),
1642 packet_writer_delim(&data
->writer
);
1645 static void send_shallow_info(struct upload_pack_data
*data
)
1647 /* No shallow info needs to be sent */
1648 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1649 !is_repository_shallow(the_repository
))
1652 packet_writer_write(&data
->writer
, "shallow-info\n");
1654 if (!send_shallow_list(data
) &&
1655 is_repository_shallow(the_repository
))
1656 deepen(data
, INFINITE_DEPTH
);
1662 FETCH_PROCESS_ARGS
= 0,
1668 int upload_pack_v2(struct repository
*r
, struct packet_reader
*request
)
1670 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1671 struct upload_pack_data data
;
1673 clear_object_flags(ALL_FLAGS
);
1675 upload_pack_data_init(&data
);
1676 data
.use_sideband
= LARGE_PACKET_MAX
;
1678 git_config(upload_pack_config
, &data
);
1680 while (state
!= FETCH_DONE
) {
1682 case FETCH_PROCESS_ARGS
:
1683 process_args(request
, &data
);
1685 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1687 * Request didn't contain any 'want' lines (and
1688 * the request does not contain
1689 * "wait-for-done", in which it is reasonable
1690 * to just send 'have's without 'want's); guess
1691 * they didn't want anything.
1694 } else if (data
.haves
.nr
) {
1696 * Request had 'have' lines, so lets ACK them.
1698 state
= FETCH_SEND_ACKS
;
1701 * Request had 'want's but no 'have's so we can
1702 * immedietly go to construct and send a pack.
1704 state
= FETCH_SEND_PACK
;
1707 case FETCH_SEND_ACKS
:
1708 if (process_haves_and_send_acks(&data
))
1709 state
= FETCH_SEND_PACK
;
1713 case FETCH_SEND_PACK
:
1714 send_wanted_ref_info(&data
);
1715 send_shallow_info(&data
);
1717 if (data
.uri_protocols
.nr
) {
1718 create_pack_file(&data
, &data
.uri_protocols
);
1720 packet_writer_write(&data
.writer
, "packfile\n");
1721 create_pack_file(&data
, NULL
);
1730 upload_pack_data_clear(&data
);
1734 int upload_pack_advertise(struct repository
*r
,
1735 struct strbuf
*value
)
1738 int allow_filter_value
;
1739 int allow_ref_in_want
;
1740 int allow_sideband_all_value
;
1743 strbuf_addstr(value
, "shallow wait-for-done");
1745 if (!repo_config_get_bool(the_repository
,
1746 "uploadpack.allowfilter",
1747 &allow_filter_value
) &&
1749 strbuf_addstr(value
, " filter");
1751 if (!repo_config_get_bool(the_repository
,
1752 "uploadpack.allowrefinwant",
1753 &allow_ref_in_want
) &&
1755 strbuf_addstr(value
, " ref-in-want");
1757 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1758 (!repo_config_get_bool(the_repository
,
1759 "uploadpack.allowsidebandall",
1760 &allow_sideband_all_value
) &&
1761 allow_sideband_all_value
))
1762 strbuf_addstr(value
, " sideband-all");
1764 if (!repo_config_get_string(the_repository
,
1765 "uploadpack.blobpackfileuri",
1768 strbuf_addstr(value
, " packfile-uris");