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
{
198 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
199 * sideband-64k the band designator takes up 1 byte of space. Because
200 * relay_pack_data keeps the last byte to itself, we make the buffer 1
201 * byte bigger than the intended maximum write size.
203 char buffer
[(LARGE_PACKET_DATA_MAX
- 1) + 1];
205 unsigned packfile_uris_started
: 1;
206 unsigned packfile_started
: 1;
209 static int relay_pack_data(int pack_objects_out
, struct output_state
*os
,
210 int use_sideband
, int write_packfile_line
)
213 * We keep the last byte to ourselves
214 * in case we detect broken rev-list, so that we
215 * can leave the stream corrupted. This is
216 * unfortunate -- unpack-objects would happily
217 * accept a valid packdata with trailing garbage,
218 * so appending garbage after we pass all the
219 * pack data is not good enough to signal
220 * breakage to downstream.
224 readsz
= xread(pack_objects_out
, os
->buffer
+ os
->used
,
225 sizeof(os
->buffer
) - os
->used
);
231 while (!os
->packfile_started
) {
233 if (os
->used
>= 4 && !memcmp(os
->buffer
, "PACK", 4)) {
234 os
->packfile_started
= 1;
235 if (write_packfile_line
) {
236 if (os
->packfile_uris_started
)
238 packet_write_fmt(1, "\1packfile\n");
242 if ((p
= memchr(os
->buffer
, '\n', os
->used
))) {
243 if (!os
->packfile_uris_started
) {
244 os
->packfile_uris_started
= 1;
245 if (!write_packfile_line
)
246 BUG("packfile_uris requires sideband-all");
247 packet_write_fmt(1, "\1packfile-uris\n");
250 packet_write_fmt(1, "\1%s\n", os
->buffer
);
252 os
->used
-= p
- os
->buffer
+ 1;
253 memmove(os
->buffer
, p
+ 1, os
->used
);
263 send_client_data(1, os
->buffer
, os
->used
- 1, use_sideband
);
264 os
->buffer
[0] = os
->buffer
[os
->used
- 1];
267 send_client_data(1, os
->buffer
, os
->used
, use_sideband
);
274 static void create_pack_file(struct upload_pack_data
*pack_data
,
275 const struct string_list
*uri_protocols
)
277 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
278 struct output_state
*output_state
= xcalloc(1, sizeof(struct output_state
));
280 char abort_msg
[] = "aborting due to possible repository "
281 "corruption on the remote side.";
286 if (!pack_data
->pack_objects_hook
)
287 pack_objects
.git_cmd
= 1;
289 strvec_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
290 strvec_push(&pack_objects
.args
, "git");
291 pack_objects
.use_shell
= 1;
294 if (pack_data
->shallow_nr
) {
295 strvec_push(&pack_objects
.args
, "--shallow-file");
296 strvec_push(&pack_objects
.args
, "");
298 strvec_push(&pack_objects
.args
, "pack-objects");
299 strvec_push(&pack_objects
.args
, "--revs");
300 if (pack_data
->use_thin_pack
)
301 strvec_push(&pack_objects
.args
, "--thin");
303 strvec_push(&pack_objects
.args
, "--stdout");
304 if (pack_data
->shallow_nr
)
305 strvec_push(&pack_objects
.args
, "--shallow");
306 if (!pack_data
->no_progress
)
307 strvec_push(&pack_objects
.args
, "--progress");
308 if (pack_data
->use_ofs_delta
)
309 strvec_push(&pack_objects
.args
, "--delta-base-offset");
310 if (pack_data
->use_include_tag
)
311 strvec_push(&pack_objects
.args
, "--include-tag");
312 if (pack_data
->filter_options
.choice
) {
314 expand_list_objects_filter_spec(&pack_data
->filter_options
);
315 strvec_pushf(&pack_objects
.args
, "--filter=%s", spec
);
318 for (i
= 0; i
< uri_protocols
->nr
; i
++)
319 strvec_pushf(&pack_objects
.args
, "--uri-protocol=%s",
320 uri_protocols
->items
[i
].string
);
323 pack_objects
.in
= -1;
324 pack_objects
.out
= -1;
325 pack_objects
.err
= -1;
326 pack_objects
.clean_on_exit
= 1;
328 if (start_command(&pack_objects
))
329 die("git upload-pack: unable to fork git-pack-objects");
331 pipe_fd
= xfdopen(pack_objects
.in
, "w");
333 if (pack_data
->shallow_nr
)
334 for_each_commit_graft(write_one_shallow
, pipe_fd
);
336 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
337 fprintf(pipe_fd
, "%s\n",
338 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
339 fprintf(pipe_fd
, "--not\n");
340 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
341 fprintf(pipe_fd
, "%s\n",
342 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
343 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
344 fprintf(pipe_fd
, "%s\n",
345 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
346 fprintf(pipe_fd
, "\n");
350 /* We read from pack_objects.err to capture stderr output for
351 * progress bar, and pack_objects.out to capture the pack data.
355 struct pollfd pfd
[2];
356 int pe
, pu
, pollsize
, polltimeout
;
359 reset_timeout(pack_data
->timeout
);
364 if (0 <= pack_objects
.out
) {
365 pfd
[pollsize
].fd
= pack_objects
.out
;
366 pfd
[pollsize
].events
= POLLIN
;
370 if (0 <= pack_objects
.err
) {
371 pfd
[pollsize
].fd
= pack_objects
.err
;
372 pfd
[pollsize
].events
= POLLIN
;
380 polltimeout
= pack_data
->keepalive
< 0
382 : 1000 * pack_data
->keepalive
;
384 ret
= poll(pfd
, pollsize
, polltimeout
);
387 if (errno
!= EINTR
) {
388 error_errno("poll failed, resuming");
393 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
394 /* Status ready; we ship that in the side-band
395 * or dump to the standard error.
397 sz
= xread(pack_objects
.err
, progress
,
400 send_client_data(2, progress
, sz
,
401 pack_data
->use_sideband
);
403 close(pack_objects
.err
);
404 pack_objects
.err
= -1;
408 /* give priority to status messages */
411 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
412 int result
= relay_pack_data(pack_objects
.out
,
414 pack_data
->use_sideband
,
418 close(pack_objects
.out
);
419 pack_objects
.out
= -1;
420 } else if (result
< 0) {
426 * We hit the keepalive timeout without saying anything; send
427 * an empty message on the data sideband just to let the other
428 * side know we're still working on it, but don't have any data
431 * If we don't have a sideband channel, there's no room in the
432 * protocol to say anything, so those clients are just out of
435 if (!ret
&& pack_data
->use_sideband
) {
436 static const char buf
[] = "0005\1";
437 write_or_die(1, buf
, 5);
441 if (finish_command(&pack_objects
)) {
442 error("git upload-pack: git-pack-objects died with error.");
447 if (output_state
->used
> 0) {
448 send_client_data(1, output_state
->buffer
, output_state
->used
,
449 pack_data
->use_sideband
);
450 fprintf(stderr
, "flushed.\n");
453 if (pack_data
->use_sideband
)
459 send_client_data(3, abort_msg
, sizeof(abort_msg
),
460 pack_data
->use_sideband
);
461 die("git upload-pack: %s", abort_msg
);
464 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
466 int we_knew_they_have
= 0;
467 struct object
*o
= parse_object(the_repository
, oid
);
470 die("oops (%s)", oid_to_hex(oid
));
471 if (o
->type
== OBJ_COMMIT
) {
472 struct commit_list
*parents
;
473 struct commit
*commit
= (struct commit
*)o
;
474 if (o
->flags
& THEY_HAVE
)
475 we_knew_they_have
= 1;
477 o
->flags
|= THEY_HAVE
;
478 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
479 data
->oldest_have
= commit
->date
;
480 for (parents
= commit
->parents
;
482 parents
= parents
->next
)
483 parents
->item
->object
.flags
|= THEY_HAVE
;
485 if (!we_knew_they_have
) {
486 add_object_array(o
, NULL
, &data
->have_obj
);
492 static int got_oid(struct upload_pack_data
*data
,
493 const char *hex
, struct object_id
*oid
)
495 if (get_oid_hex(hex
, oid
))
496 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
497 if (!has_object_file_with_flags(oid
,
498 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
500 return do_got_oid(data
, oid
);
503 static int ok_to_give_up(struct upload_pack_data
*data
)
505 timestamp_t min_generation
= GENERATION_NUMBER_ZERO
;
507 if (!data
->have_obj
.nr
)
510 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
511 COMMON_KNOWN
, data
->oldest_have
,
515 static int get_common_commits(struct upload_pack_data
*data
,
516 struct packet_reader
*reader
)
518 struct object_id oid
;
519 char last_hex
[GIT_MAX_HEXSZ
+ 1];
524 save_commit_buffer
= 0;
529 reset_timeout(data
->timeout
);
531 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
532 if (data
->multi_ack
== MULTI_ACK_DETAILED
535 && ok_to_give_up(data
)) {
537 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
539 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
540 packet_write_fmt(1, "NAK\n");
542 if (data
->no_done
&& sent_ready
) {
543 packet_write_fmt(1, "ACK %s\n", last_hex
);
546 if (data
->stateless_rpc
)
552 if (skip_prefix(reader
->line
, "have ", &arg
)) {
553 switch (got_oid(data
, arg
, &oid
)) {
554 case -1: /* they have what we do not */
557 && ok_to_give_up(data
)) {
558 const char *hex
= oid_to_hex(&oid
);
559 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
561 packet_write_fmt(1, "ACK %s ready\n", hex
);
563 packet_write_fmt(1, "ACK %s continue\n", hex
);
568 oid_to_hex_r(last_hex
, &oid
);
569 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
570 packet_write_fmt(1, "ACK %s common\n", last_hex
);
571 else if (data
->multi_ack
)
572 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
573 else if (data
->have_obj
.nr
== 1)
574 packet_write_fmt(1, "ACK %s\n", last_hex
);
579 if (!strcmp(reader
->line
, "done")) {
580 if (data
->have_obj
.nr
> 0) {
582 packet_write_fmt(1, "ACK %s\n", last_hex
);
585 packet_write_fmt(1, "NAK\n");
588 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
592 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
594 int allow_hidden_ref
= (allow_uor
&
595 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
596 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
600 * on successful case, it's up to the caller to close cmd->out
602 static int do_reachable_revlist(struct child_process
*cmd
,
603 struct object_array
*src
,
604 struct object_array
*reachable
,
605 enum allow_uor allow_uor
)
611 strvec_pushl(&cmd
->args
, "rev-list", "--stdin", NULL
);
618 * If the next rev-list --stdin encounters an unknown commit,
619 * it terminates, which will cause SIGPIPE in the write loop
622 sigchain_push(SIGPIPE
, SIG_IGN
);
624 if (start_command(cmd
))
627 cmd_in
= xfdopen(cmd
->in
, "w");
629 for (i
= get_max_object_index(); 0 < i
; ) {
630 o
= get_indexed_object(--i
);
633 if (reachable
&& o
->type
== OBJ_COMMIT
)
634 o
->flags
&= ~TMP_MARK
;
635 if (!is_our_ref(o
, allow_uor
))
637 if (fprintf(cmd_in
, "^%s\n", oid_to_hex(&o
->oid
)) < 0)
640 for (i
= 0; i
< src
->nr
; i
++) {
641 o
= src
->objects
[i
].item
;
642 if (is_our_ref(o
, allow_uor
)) {
644 add_object_array(o
, NULL
, reachable
);
647 if (reachable
&& o
->type
== OBJ_COMMIT
)
648 o
->flags
|= TMP_MARK
;
649 if (fprintf(cmd_in
, "%s\n", oid_to_hex(&o
->oid
)) < 0)
652 if (ferror(cmd_in
) || fflush(cmd_in
))
656 sigchain_pop(SIGPIPE
);
661 sigchain_pop(SIGPIPE
);
670 static int get_reachable_list(struct upload_pack_data
*data
,
671 struct object_array
*reachable
)
673 struct child_process cmd
= CHILD_PROCESS_INIT
;
676 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
677 const unsigned hexsz
= the_hash_algo
->hexsz
;
679 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
680 data
->allow_uor
) < 0)
683 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
684 struct object_id oid
;
687 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
690 o
= lookup_object(the_repository
, &oid
);
691 if (o
&& o
->type
== OBJ_COMMIT
) {
692 o
->flags
&= ~TMP_MARK
;
695 for (i
= get_max_object_index(); 0 < i
; i
--) {
696 o
= get_indexed_object(i
- 1);
697 if (o
&& o
->type
== OBJ_COMMIT
&&
698 (o
->flags
& TMP_MARK
)) {
699 add_object_array(o
, NULL
, reachable
);
700 o
->flags
&= ~TMP_MARK
;
705 if (finish_command(&cmd
))
711 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
713 struct child_process cmd
= CHILD_PROCESS_INIT
;
717 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
721 * The commits out of the rev-list are not ancestors of
724 i
= read_in_full(cmd
.out
, buf
, 1);
731 * rev-list may have died by encountering a bad commit
732 * in the history, in which case we do want to bail out
733 * even when it showed no commit.
735 if (finish_command(&cmd
))
738 /* All the non-tip ones are ancestors of what we advertised */
747 static void check_non_tip(struct upload_pack_data
*data
)
752 * In the normal in-process case without
753 * uploadpack.allowReachableSHA1InWant,
754 * non-tip requests can never happen.
756 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
758 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
759 /* All the non-tip ones are ancestors of what we advertised */
763 /* Pick one of them (we know there at least is one) */
764 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
765 struct object
*o
= data
->want_obj
.objects
[i
].item
;
766 if (!is_our_ref(o
, data
->allow_uor
)) {
767 packet_writer_error(&data
->writer
,
768 "upload-pack: not our ref %s",
769 oid_to_hex(&o
->oid
));
770 die("git upload-pack: not our ref %s",
771 oid_to_hex(&o
->oid
));
776 static void send_shallow(struct upload_pack_data
*data
,
777 struct commit_list
*result
)
780 struct object
*object
= &result
->item
->object
;
781 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
782 packet_writer_write(&data
->writer
, "shallow %s",
783 oid_to_hex(&object
->oid
));
784 register_shallow(the_repository
, &object
->oid
);
787 result
= result
->next
;
791 static void send_unshallow(struct upload_pack_data
*data
)
795 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
796 struct object
*object
= data
->shallows
.objects
[i
].item
;
797 if (object
->flags
& NOT_SHALLOW
) {
798 struct commit_list
*parents
;
799 packet_writer_write(&data
->writer
, "unshallow %s",
800 oid_to_hex(&object
->oid
));
801 object
->flags
&= ~CLIENT_SHALLOW
;
803 * We want to _register_ "object" as shallow, but we
804 * also need to traverse object's parents to deepen a
805 * shallow clone. Unregister it for now so we can
806 * parse and add the parents to the want list, then
809 unregister_shallow(&object
->oid
);
811 parse_commit_or_die((struct commit
*)object
);
812 parents
= ((struct commit
*)object
)->parents
;
814 add_object_array(&parents
->item
->object
,
815 NULL
, &data
->want_obj
);
816 parents
= parents
->next
;
818 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
820 /* make sure commit traversal conforms to client */
821 register_shallow(the_repository
, &object
->oid
);
825 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
826 int flag
, void *cb_data
);
827 static void deepen(struct upload_pack_data
*data
, int depth
)
829 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
832 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
833 struct object
*object
= data
->shallows
.objects
[i
].item
;
834 object
->flags
|= NOT_SHALLOW
;
836 } else if (data
->deepen_relative
) {
837 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
838 struct commit_list
*result
;
841 * Checking for reachable shallows requires that our refs be
842 * marked with OUR_REF.
844 head_ref_namespaced(check_ref
, NULL
);
845 for_each_namespaced_ref(check_ref
, NULL
);
847 get_reachable_list(data
, &reachable_shallows
);
848 result
= get_shallow_commits(&reachable_shallows
,
850 SHALLOW
, NOT_SHALLOW
);
851 send_shallow(data
, result
);
852 free_commit_list(result
);
853 object_array_clear(&reachable_shallows
);
855 struct commit_list
*result
;
857 result
= get_shallow_commits(&data
->want_obj
, depth
,
858 SHALLOW
, NOT_SHALLOW
);
859 send_shallow(data
, result
);
860 free_commit_list(result
);
863 send_unshallow(data
);
866 static void deepen_by_rev_list(struct upload_pack_data
*data
,
870 struct commit_list
*result
;
872 disable_commit_graph(the_repository
);
873 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
874 send_shallow(data
, result
);
875 free_commit_list(result
);
876 send_unshallow(data
);
879 /* Returns 1 if a shallow list is sent or 0 otherwise */
880 static int send_shallow_list(struct upload_pack_data
*data
)
884 if (data
->depth
> 0 && data
->deepen_rev_list
)
885 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
886 if (data
->depth
> 0) {
887 deepen(data
, data
->depth
);
889 } else if (data
->deepen_rev_list
) {
890 struct strvec av
= STRVEC_INIT
;
893 strvec_push(&av
, "rev-list");
894 if (data
->deepen_since
)
895 strvec_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
896 if (data
->deepen_not
.nr
) {
897 strvec_push(&av
, "--not");
898 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
899 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
900 strvec_push(&av
, s
->string
);
902 strvec_push(&av
, "--not");
904 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
905 struct object
*o
= data
->want_obj
.objects
[i
].item
;
906 strvec_push(&av
, oid_to_hex(&o
->oid
));
908 deepen_by_rev_list(data
, av
.nr
, av
.v
);
912 if (data
->shallows
.nr
> 0) {
914 for (i
= 0; i
< data
->shallows
.nr
; i
++)
915 register_shallow(the_repository
,
916 &data
->shallows
.objects
[i
].item
->oid
);
920 data
->shallow_nr
+= data
->shallows
.nr
;
924 static int process_shallow(const char *line
, struct object_array
*shallows
)
927 if (skip_prefix(line
, "shallow ", &arg
)) {
928 struct object_id oid
;
929 struct object
*object
;
930 if (get_oid_hex(arg
, &oid
))
931 die("invalid shallow line: %s", line
);
932 object
= parse_object(the_repository
, &oid
);
935 if (object
->type
!= OBJ_COMMIT
)
936 die("invalid shallow object %s", oid_to_hex(&oid
));
937 if (!(object
->flags
& CLIENT_SHALLOW
)) {
938 object
->flags
|= CLIENT_SHALLOW
;
939 add_object_array(object
, NULL
, shallows
);
947 static int process_deepen(const char *line
, int *depth
)
950 if (skip_prefix(line
, "deepen ", &arg
)) {
952 *depth
= (int)strtol(arg
, &end
, 0);
953 if (!end
|| *end
|| *depth
<= 0)
954 die("Invalid deepen: %s", line
);
961 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
964 if (skip_prefix(line
, "deepen-since ", &arg
)) {
966 *deepen_since
= parse_timestamp(arg
, &end
, 0);
967 if (!end
|| *end
|| !deepen_since
||
968 /* revisions.c's max_age -1 is special */
970 die("Invalid deepen-since: %s", line
);
971 *deepen_rev_list
= 1;
977 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
980 if (skip_prefix(line
, "deepen-not ", &arg
)) {
982 struct object_id oid
;
983 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
984 die("git upload-pack: ambiguous deepen-not: %s", line
);
985 string_list_append(deepen_not
, ref
);
987 *deepen_rev_list
= 1;
993 NORETURN
__attribute__((format(printf
,2,3)))
994 static void send_err_and_die(struct upload_pack_data
*data
,
995 const char *fmt
, ...)
997 struct strbuf buf
= STRBUF_INIT
;
1001 strbuf_vaddf(&buf
, fmt
, ap
);
1004 packet_writer_error(&data
->writer
, "%s", buf
.buf
);
1008 static void check_one_filter(struct upload_pack_data
*data
,
1009 struct list_objects_filter_options
*opts
)
1011 const char *key
= list_object_filter_config_name(opts
->choice
);
1012 struct string_list_item
*item
= string_list_lookup(&data
->allowed_filters
,
1017 allowed
= (intptr_t)item
->util
;
1019 allowed
= data
->allow_filter_fallback
;
1022 send_err_and_die(data
, "filter '%s' not supported", key
);
1024 if (opts
->choice
== LOFC_TREE_DEPTH
&&
1025 opts
->tree_exclude_depth
> data
->tree_filter_max_depth
)
1026 send_err_and_die(data
,
1027 "tree filter allows max depth %lu, but got %lu",
1028 data
->tree_filter_max_depth
,
1029 opts
->tree_exclude_depth
);
1032 static void check_filter_recurse(struct upload_pack_data
*data
,
1033 struct list_objects_filter_options
*opts
)
1037 check_one_filter(data
, opts
);
1038 if (opts
->choice
!= LOFC_COMBINE
)
1041 for (i
= 0; i
< opts
->sub_nr
; i
++)
1042 check_filter_recurse(data
, &opts
->sub
[i
]);
1045 static void die_if_using_banned_filter(struct upload_pack_data
*data
)
1047 check_filter_recurse(data
, &data
->filter_options
);
1050 static void receive_needs(struct upload_pack_data
*data
,
1051 struct packet_reader
*reader
)
1053 int has_non_tip
= 0;
1055 data
->shallow_nr
= 0;
1058 const char *features
;
1059 struct object_id oid_buf
;
1063 reset_timeout(data
->timeout
);
1064 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
1067 if (process_shallow(reader
->line
, &data
->shallows
))
1069 if (process_deepen(reader
->line
, &data
->depth
))
1071 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
1073 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
1076 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
1077 if (!data
->filter_capability_requested
)
1078 die("git upload-pack: filtering capability not negotiated");
1079 list_objects_filter_die_if_populated(&data
->filter_options
);
1080 parse_list_objects_filter(&data
->filter_options
, arg
);
1081 die_if_using_banned_filter(data
);
1085 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
1086 parse_oid_hex(arg
, &oid_buf
, &features
))
1087 die("git upload-pack: protocol error, "
1088 "expected to get object ID, not '%s'", reader
->line
);
1090 if (parse_feature_request(features
, "deepen-relative"))
1091 data
->deepen_relative
= 1;
1092 if (parse_feature_request(features
, "multi_ack_detailed"))
1093 data
->multi_ack
= MULTI_ACK_DETAILED
;
1094 else if (parse_feature_request(features
, "multi_ack"))
1095 data
->multi_ack
= MULTI_ACK
;
1096 if (parse_feature_request(features
, "no-done"))
1098 if (parse_feature_request(features
, "thin-pack"))
1099 data
->use_thin_pack
= 1;
1100 if (parse_feature_request(features
, "ofs-delta"))
1101 data
->use_ofs_delta
= 1;
1102 if (parse_feature_request(features
, "side-band-64k"))
1103 data
->use_sideband
= LARGE_PACKET_MAX
;
1104 else if (parse_feature_request(features
, "side-band"))
1105 data
->use_sideband
= DEFAULT_PACKET_MAX
;
1106 if (parse_feature_request(features
, "no-progress"))
1107 data
->no_progress
= 1;
1108 if (parse_feature_request(features
, "include-tag"))
1109 data
->use_include_tag
= 1;
1110 if (data
->allow_filter
&&
1111 parse_feature_request(features
, "filter"))
1112 data
->filter_capability_requested
= 1;
1114 arg
= parse_feature_value(features
, "session-id", &feature_len
, NULL
);
1116 char *client_sid
= xstrndup(arg
, feature_len
);
1117 trace2_data_string("transfer", NULL
, "client-sid", client_sid
);
1121 o
= parse_object(the_repository
, &oid_buf
);
1123 packet_writer_error(&data
->writer
,
1124 "upload-pack: not our ref %s",
1125 oid_to_hex(&oid_buf
));
1126 die("git upload-pack: not our ref %s",
1127 oid_to_hex(&oid_buf
));
1129 if (!(o
->flags
& WANTED
)) {
1131 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1132 || is_our_ref(o
, data
->allow_uor
)))
1134 add_object_array(o
, NULL
, &data
->want_obj
);
1139 * We have sent all our refs already, and the other end
1140 * should have chosen out of them. When we are operating
1141 * in the stateless RPC mode, however, their choice may
1142 * have been based on the set of older refs advertised
1143 * by another process that handled the initial request.
1146 check_non_tip(data
);
1148 if (!data
->use_sideband
&& data
->daemon_mode
)
1149 data
->no_progress
= 1;
1151 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1154 if (send_shallow_list(data
))
1158 /* return non-zero if the ref is hidden, otherwise 0 */
1159 static int mark_our_ref(const char *refname
, const char *refname_full
,
1160 const struct object_id
*oid
)
1162 struct object
*o
= lookup_unknown_object(the_repository
, oid
);
1164 if (ref_is_hidden(refname
, refname_full
)) {
1165 o
->flags
|= HIDDEN_REF
;
1168 o
->flags
|= OUR_REF
;
1172 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1173 int flag
, void *cb_data
)
1175 const char *refname
= strip_namespace(refname_full
);
1177 mark_our_ref(refname
, refname_full
, oid
);
1181 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1183 struct string_list_item
*item
;
1187 for_each_string_list_item(item
, symref
)
1188 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1191 static void format_session_id(struct strbuf
*buf
, struct upload_pack_data
*d
) {
1192 if (d
->advertise_sid
)
1193 strbuf_addf(buf
, " session-id=%s", trace2_session_id());
1196 static int send_ref(const char *refname
, const struct object_id
*oid
,
1197 int flag
, void *cb_data
)
1199 static const char *capabilities
= "multi_ack thin-pack side-band"
1200 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1201 " deepen-relative no-progress include-tag multi_ack_detailed";
1202 const char *refname_nons
= strip_namespace(refname
);
1203 struct object_id peeled
;
1204 struct upload_pack_data
*data
= cb_data
;
1206 if (mark_our_ref(refname_nons
, refname
, oid
))
1210 struct strbuf symref_info
= STRBUF_INIT
;
1211 struct strbuf session_id
= STRBUF_INIT
;
1213 format_symref_info(&symref_info
, &data
->symref
);
1214 format_session_id(&session_id
, data
);
1215 packet_fwrite_fmt(stdout
, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1216 oid_to_hex(oid
), refname_nons
,
1218 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1219 " allow-tip-sha1-in-want" : "",
1220 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1221 " allow-reachable-sha1-in-want" : "",
1222 data
->no_done
? " no-done" : "",
1224 data
->allow_filter
? " filter" : "",
1226 the_hash_algo
->name
,
1227 git_user_agent_sanitized());
1228 strbuf_release(&symref_info
);
1229 strbuf_release(&session_id
);
1231 packet_fwrite_fmt(stdout
, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1233 capabilities
= NULL
;
1234 if (!peel_iterated_oid(oid
, &peeled
))
1235 packet_fwrite_fmt(stdout
, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1239 static int find_symref(const char *refname
, const struct object_id
*oid
,
1240 int flag
, void *cb_data
)
1242 const char *symref_target
;
1243 struct string_list_item
*item
;
1245 if ((flag
& REF_ISSYMREF
) == 0)
1247 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1248 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1249 die("'%s' is a symref but it is not?", refname
);
1250 item
= string_list_append(cb_data
, strip_namespace(refname
));
1251 item
->util
= xstrdup(strip_namespace(symref_target
));
1255 static int parse_object_filter_config(const char *var
, const char *value
,
1256 struct upload_pack_data
*data
)
1258 struct strbuf buf
= STRBUF_INIT
;
1259 const char *sub
, *key
;
1262 if (parse_config_key(var
, "uploadpackfilter", &sub
, &sub_len
, &key
))
1266 if (!strcmp(key
, "allow"))
1267 data
->allow_filter_fallback
= git_config_bool(var
, value
);
1271 strbuf_add(&buf
, sub
, sub_len
);
1273 if (!strcmp(key
, "allow"))
1274 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1275 (void *)(intptr_t)git_config_bool(var
, value
);
1276 else if (!strcmp(buf
.buf
, "tree") && !strcmp(key
, "maxdepth")) {
1278 strbuf_release(&buf
);
1279 return config_error_nonbool(var
);
1281 string_list_insert(&data
->allowed_filters
, buf
.buf
)->util
=
1282 (void *)(intptr_t)1;
1283 data
->tree_filter_max_depth
= git_config_ulong(var
, value
);
1286 strbuf_release(&buf
);
1290 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1292 struct upload_pack_data
*data
= cb_data
;
1294 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1295 if (git_config_bool(var
, value
))
1296 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1298 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1299 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1300 if (git_config_bool(var
, value
))
1301 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1303 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1304 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1305 if (git_config_bool(var
, value
))
1306 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1308 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1309 } else if (!strcmp("uploadpack.keepalive", var
)) {
1310 data
->keepalive
= git_config_int(var
, value
);
1311 if (!data
->keepalive
)
1312 data
->keepalive
= -1;
1313 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1314 data
->allow_filter
= git_config_bool(var
, value
);
1315 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1316 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1317 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1318 data
->allow_sideband_all
= git_config_bool(var
, value
);
1319 } else if (!strcmp("core.precomposeunicode", var
)) {
1320 precomposed_unicode
= git_config_bool(var
, value
);
1321 } else if (!strcmp("transfer.advertisesid", var
)) {
1322 data
->advertise_sid
= git_config_bool(var
, value
);
1325 if (parse_object_filter_config(var
, value
, data
) < 0)
1328 return parse_hide_refs_config(var
, value
, "uploadpack");
1331 static int upload_pack_protected_config(const char *var
, const char *value
, void *cb_data
)
1333 struct upload_pack_data
*data
= cb_data
;
1335 if (!strcmp("uploadpack.packobjectshook", var
))
1336 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1340 static void get_upload_pack_config(struct upload_pack_data
*data
)
1342 git_config(upload_pack_config
, data
);
1343 git_protected_config(upload_pack_protected_config
, data
);
1346 void upload_pack(const int advertise_refs
, const int stateless_rpc
,
1349 struct packet_reader reader
;
1350 struct upload_pack_data data
;
1352 upload_pack_data_init(&data
);
1353 get_upload_pack_config(&data
);
1355 data
.stateless_rpc
= stateless_rpc
;
1356 data
.timeout
= timeout
;
1358 data
.daemon_mode
= 1;
1360 head_ref_namespaced(find_symref
, &data
.symref
);
1362 if (advertise_refs
|| !data
.stateless_rpc
) {
1363 reset_timeout(data
.timeout
);
1366 head_ref_namespaced(send_ref
, &data
);
1367 for_each_namespaced_ref(send_ref
, &data
);
1369 * fflush stdout before calling advertise_shallow_grafts because send_ref
1372 fflush_or_die(stdout
);
1373 advertise_shallow_grafts(1);
1376 head_ref_namespaced(check_ref
, NULL
);
1377 for_each_namespaced_ref(check_ref
, NULL
);
1380 if (!advertise_refs
) {
1381 packet_reader_init(&reader
, 0, NULL
, 0,
1382 PACKET_READ_CHOMP_NEWLINE
|
1383 PACKET_READ_DIE_ON_ERR_PACKET
);
1385 receive_needs(&data
, &reader
);
1388 * An EOF at this exact point in negotiation should be
1389 * acceptable from stateless clients as they will consume the
1390 * shallow list before doing subsequent rpc with haves/etc.
1392 if (data
.stateless_rpc
)
1393 reader
.options
|= PACKET_READ_GENTLE_ON_EOF
;
1395 if (data
.want_obj
.nr
&&
1396 packet_reader_peek(&reader
) != PACKET_READ_EOF
) {
1397 reader
.options
&= ~PACKET_READ_GENTLE_ON_EOF
;
1398 get_common_commits(&data
, &reader
);
1399 create_pack_file(&data
, NULL
);
1403 upload_pack_data_clear(&data
);
1406 static int parse_want(struct packet_writer
*writer
, const char *line
,
1407 struct object_array
*want_obj
)
1410 if (skip_prefix(line
, "want ", &arg
)) {
1411 struct object_id oid
;
1412 struct commit
*commit
;
1415 if (get_oid_hex(arg
, &oid
))
1416 die("git upload-pack: protocol error, "
1417 "expected to get oid, not '%s'", line
);
1419 commit
= lookup_commit_in_graph(the_repository
, &oid
);
1421 o
= &commit
->object
;
1423 o
= parse_object_with_flags(the_repository
, &oid
,
1424 PARSE_OBJECT_SKIP_HASH_CHECK
);
1427 packet_writer_error(writer
,
1428 "upload-pack: not our ref %s",
1430 die("git upload-pack: not our ref %s",
1434 if (!(o
->flags
& WANTED
)) {
1436 add_object_array(o
, NULL
, want_obj
);
1445 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1446 struct string_list
*wanted_refs
,
1447 struct object_array
*want_obj
)
1449 const char *refname_nons
;
1450 if (skip_prefix(line
, "want-ref ", &refname_nons
)) {
1451 struct object_id oid
;
1452 struct string_list_item
*item
;
1453 struct object
*o
= NULL
;
1454 struct strbuf refname
= STRBUF_INIT
;
1456 strbuf_addf(&refname
, "%s%s", get_git_namespace(), refname_nons
);
1457 if (ref_is_hidden(refname_nons
, refname
.buf
) ||
1458 read_ref(refname
.buf
, &oid
)) {
1459 packet_writer_error(writer
, "unknown ref %s", refname_nons
);
1460 die("unknown ref %s", refname_nons
);
1462 strbuf_release(&refname
);
1464 item
= string_list_append(wanted_refs
, refname_nons
);
1465 item
->util
= oiddup(&oid
);
1467 if (!starts_with(refname_nons
, "refs/tags/")) {
1468 struct commit
*commit
= lookup_commit_in_graph(the_repository
, &oid
);
1470 o
= &commit
->object
;
1474 o
= parse_object_or_die(&oid
, refname_nons
);
1476 if (!(o
->flags
& WANTED
)) {
1478 add_object_array(o
, NULL
, want_obj
);
1487 static int parse_have(const char *line
, struct oid_array
*haves
)
1490 if (skip_prefix(line
, "have ", &arg
)) {
1491 struct object_id oid
;
1493 if (get_oid_hex(arg
, &oid
))
1494 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1495 oid_array_append(haves
, &oid
);
1502 static void process_args(struct packet_reader
*request
,
1503 struct upload_pack_data
*data
)
1505 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1506 const char *arg
= request
->line
;
1510 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1512 if (data
->allow_ref_in_want
&&
1513 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1516 /* process have line */
1517 if (parse_have(arg
, &data
->haves
))
1520 /* process args like thin-pack */
1521 if (!strcmp(arg
, "thin-pack")) {
1522 data
->use_thin_pack
= 1;
1525 if (!strcmp(arg
, "ofs-delta")) {
1526 data
->use_ofs_delta
= 1;
1529 if (!strcmp(arg
, "no-progress")) {
1530 data
->no_progress
= 1;
1533 if (!strcmp(arg
, "include-tag")) {
1534 data
->use_include_tag
= 1;
1537 if (!strcmp(arg
, "done")) {
1541 if (!strcmp(arg
, "wait-for-done")) {
1542 data
->wait_for_done
= 1;
1546 /* Shallow related arguments */
1547 if (process_shallow(arg
, &data
->shallows
))
1549 if (process_deepen(arg
, &data
->depth
))
1551 if (process_deepen_since(arg
, &data
->deepen_since
,
1552 &data
->deepen_rev_list
))
1554 if (process_deepen_not(arg
, &data
->deepen_not
,
1555 &data
->deepen_rev_list
))
1557 if (!strcmp(arg
, "deepen-relative")) {
1558 data
->deepen_relative
= 1;
1562 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1563 list_objects_filter_die_if_populated(&data
->filter_options
);
1564 parse_list_objects_filter(&data
->filter_options
, p
);
1565 die_if_using_banned_filter(data
);
1569 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1570 data
->allow_sideband_all
) &&
1571 !strcmp(arg
, "sideband-all")) {
1572 data
->writer
.use_sideband
= 1;
1576 if (skip_prefix(arg
, "packfile-uris ", &p
)) {
1577 string_list_split(&data
->uri_protocols
, p
, ',', -1);
1581 /* ignore unknown lines maybe? */
1582 die("unexpected line: '%s'", arg
);
1585 if (data
->uri_protocols
.nr
&& !data
->writer
.use_sideband
)
1586 string_list_clear(&data
->uri_protocols
, 0);
1588 if (request
->status
!= PACKET_READ_FLUSH
)
1589 die(_("expected flush after fetch arguments"));
1592 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1597 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1598 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1600 if (!has_object_file_with_flags(oid
,
1601 OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
))
1604 oid_array_append(common
, oid
);
1606 do_got_oid(data
, oid
);
1612 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1616 packet_writer_write(&data
->writer
, "acknowledgments\n");
1620 packet_writer_write(&data
->writer
, "NAK\n");
1622 for (i
= 0; i
< acks
->nr
; i
++) {
1623 packet_writer_write(&data
->writer
, "ACK %s\n",
1624 oid_to_hex(&acks
->oid
[i
]));
1627 if (!data
->wait_for_done
&& ok_to_give_up(data
)) {
1629 packet_writer_write(&data
->writer
, "ready\n");
1636 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1638 struct oid_array common
= OID_ARRAY_INIT
;
1641 process_haves(data
, &common
);
1644 } else if (send_acks(data
, &common
)) {
1645 packet_writer_delim(&data
->writer
);
1649 packet_writer_flush(&data
->writer
);
1653 oid_array_clear(&data
->haves
);
1654 oid_array_clear(&common
);
1658 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1660 const struct string_list_item
*item
;
1662 if (!data
->wanted_refs
.nr
)
1665 packet_writer_write(&data
->writer
, "wanted-refs\n");
1667 for_each_string_list_item(item
, &data
->wanted_refs
) {
1668 packet_writer_write(&data
->writer
, "%s %s\n",
1669 oid_to_hex(item
->util
),
1673 packet_writer_delim(&data
->writer
);
1676 static void send_shallow_info(struct upload_pack_data
*data
)
1678 /* No shallow info needs to be sent */
1679 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1680 !is_repository_shallow(the_repository
))
1683 packet_writer_write(&data
->writer
, "shallow-info\n");
1685 if (!send_shallow_list(data
) &&
1686 is_repository_shallow(the_repository
))
1687 deepen(data
, INFINITE_DEPTH
);
1693 FETCH_PROCESS_ARGS
= 0,
1699 int upload_pack_v2(struct repository
*r
, struct packet_reader
*request
)
1701 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1702 struct upload_pack_data data
;
1704 clear_object_flags(ALL_FLAGS
);
1706 upload_pack_data_init(&data
);
1707 data
.use_sideband
= LARGE_PACKET_MAX
;
1708 get_upload_pack_config(&data
);
1710 while (state
!= FETCH_DONE
) {
1712 case FETCH_PROCESS_ARGS
:
1713 process_args(request
, &data
);
1715 if (!data
.want_obj
.nr
&& !data
.wait_for_done
) {
1717 * Request didn't contain any 'want' lines (and
1718 * the request does not contain
1719 * "wait-for-done", in which it is reasonable
1720 * to just send 'have's without 'want's); guess
1721 * they didn't want anything.
1724 } else if (data
.haves
.nr
) {
1726 * Request had 'have' lines, so lets ACK them.
1728 state
= FETCH_SEND_ACKS
;
1731 * Request had 'want's but no 'have's so we can
1732 * immedietly go to construct and send a pack.
1734 state
= FETCH_SEND_PACK
;
1737 case FETCH_SEND_ACKS
:
1738 if (process_haves_and_send_acks(&data
))
1739 state
= FETCH_SEND_PACK
;
1743 case FETCH_SEND_PACK
:
1744 send_wanted_ref_info(&data
);
1745 send_shallow_info(&data
);
1747 if (data
.uri_protocols
.nr
) {
1748 create_pack_file(&data
, &data
.uri_protocols
);
1750 packet_writer_write(&data
.writer
, "packfile\n");
1751 create_pack_file(&data
, NULL
);
1760 upload_pack_data_clear(&data
);
1764 int upload_pack_advertise(struct repository
*r
,
1765 struct strbuf
*value
)
1768 int allow_filter_value
;
1769 int allow_ref_in_want
;
1770 int allow_sideband_all_value
;
1773 strbuf_addstr(value
, "shallow wait-for-done");
1775 if (!repo_config_get_bool(the_repository
,
1776 "uploadpack.allowfilter",
1777 &allow_filter_value
) &&
1779 strbuf_addstr(value
, " filter");
1781 if (!repo_config_get_bool(the_repository
,
1782 "uploadpack.allowrefinwant",
1783 &allow_ref_in_want
) &&
1785 strbuf_addstr(value
, " ref-in-want");
1787 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1788 (!repo_config_get_bool(the_repository
,
1789 "uploadpack.allowsidebandall",
1790 &allow_sideband_all_value
) &&
1791 allow_sideband_all_value
))
1792 strbuf_addstr(value
, " sideband-all");
1794 if (!repo_config_get_string(the_repository
,
1795 "uploadpack.blobpackfileuri",
1798 strbuf_addstr(value
, " packfile-uris");