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"
21 #include "argv-array.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 enum allow_uor allow_uor
;
89 struct list_objects_filter_options filter_options
;
91 struct packet_writer writer
;
93 const char *pack_objects_hook
;
95 unsigned stateless_rpc
: 1; /* v0 only */
96 unsigned no_done
: 1; /* v0 only */
97 unsigned daemon_mode
: 1; /* v0 only */
98 unsigned filter_capability_requested
: 1; /* v0 only */
100 unsigned use_thin_pack
: 1;
101 unsigned use_ofs_delta
: 1;
102 unsigned no_progress
: 1;
103 unsigned use_include_tag
: 1;
104 unsigned allow_filter
: 1;
106 unsigned done
: 1; /* v2 only */
107 unsigned allow_ref_in_want
: 1; /* v2 only */
108 unsigned allow_sideband_all
: 1; /* v2 only */
111 static void upload_pack_data_init(struct upload_pack_data
*data
)
113 struct string_list symref
= STRING_LIST_INIT_DUP
;
114 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
115 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
116 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
117 struct oid_array haves
= OID_ARRAY_INIT
;
118 struct object_array shallows
= OBJECT_ARRAY_INIT
;
119 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
120 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
122 memset(data
, 0, sizeof(*data
));
123 data
->symref
= symref
;
124 data
->wanted_refs
= wanted_refs
;
125 data
->want_obj
= want_obj
;
126 data
->have_obj
= have_obj
;
128 data
->shallows
= shallows
;
129 data
->deepen_not
= deepen_not
;
130 data
->extra_edge_obj
= extra_edge_obj
;
131 packet_writer_init(&data
->writer
, 1);
136 static void upload_pack_data_clear(struct upload_pack_data
*data
)
138 string_list_clear(&data
->symref
, 1);
139 string_list_clear(&data
->wanted_refs
, 1);
140 object_array_clear(&data
->want_obj
);
141 object_array_clear(&data
->have_obj
);
142 oid_array_clear(&data
->haves
);
143 object_array_clear(&data
->shallows
);
144 string_list_clear(&data
->deepen_not
, 0);
145 object_array_clear(&data
->extra_edge_obj
);
146 list_objects_filter_release(&data
->filter_options
);
148 free((char *)data
->pack_objects_hook
);
151 static void reset_timeout(unsigned int timeout
)
156 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
160 send_sideband(1, fd
, data
, sz
, use_sideband
);
167 /* XXX: are we happy to lose stuff here? */
168 xwrite(fd
, data
, sz
);
171 write_or_die(fd
, data
, sz
);
174 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
177 if (graft
->nr_parent
== -1)
178 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
182 static void create_pack_file(struct upload_pack_data
*pack_data
)
184 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
185 char data
[8193], progress
[128];
186 char abort_msg
[] = "aborting due to possible repository "
187 "corruption on the remote side.";
193 if (!pack_data
->pack_objects_hook
)
194 pack_objects
.git_cmd
= 1;
196 argv_array_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
197 argv_array_push(&pack_objects
.args
, "git");
198 pack_objects
.use_shell
= 1;
201 if (pack_data
->shallow_nr
) {
202 argv_array_push(&pack_objects
.args
, "--shallow-file");
203 argv_array_push(&pack_objects
.args
, "");
205 argv_array_push(&pack_objects
.args
, "pack-objects");
206 argv_array_push(&pack_objects
.args
, "--revs");
207 if (pack_data
->use_thin_pack
)
208 argv_array_push(&pack_objects
.args
, "--thin");
210 argv_array_push(&pack_objects
.args
, "--stdout");
211 if (pack_data
->shallow_nr
)
212 argv_array_push(&pack_objects
.args
, "--shallow");
213 if (!pack_data
->no_progress
)
214 argv_array_push(&pack_objects
.args
, "--progress");
215 if (pack_data
->use_ofs_delta
)
216 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
217 if (pack_data
->use_include_tag
)
218 argv_array_push(&pack_objects
.args
, "--include-tag");
219 if (pack_data
->filter_options
.choice
) {
221 expand_list_objects_filter_spec(&pack_data
->filter_options
);
222 if (pack_objects
.use_shell
) {
223 struct strbuf buf
= STRBUF_INIT
;
224 sq_quote_buf(&buf
, spec
);
225 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
226 strbuf_release(&buf
);
228 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
233 pack_objects
.in
= -1;
234 pack_objects
.out
= -1;
235 pack_objects
.err
= -1;
237 if (start_command(&pack_objects
))
238 die("git upload-pack: unable to fork git-pack-objects");
240 pipe_fd
= xfdopen(pack_objects
.in
, "w");
242 if (pack_data
->shallow_nr
)
243 for_each_commit_graft(write_one_shallow
, pipe_fd
);
245 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
246 fprintf(pipe_fd
, "%s\n",
247 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
248 fprintf(pipe_fd
, "--not\n");
249 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
250 fprintf(pipe_fd
, "%s\n",
251 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
252 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
253 fprintf(pipe_fd
, "%s\n",
254 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
255 fprintf(pipe_fd
, "\n");
259 /* We read from pack_objects.err to capture stderr output for
260 * progress bar, and pack_objects.out to capture the pack data.
264 struct pollfd pfd
[2];
265 int pe
, pu
, pollsize
, polltimeout
;
268 reset_timeout(pack_data
->timeout
);
273 if (0 <= pack_objects
.out
) {
274 pfd
[pollsize
].fd
= pack_objects
.out
;
275 pfd
[pollsize
].events
= POLLIN
;
279 if (0 <= pack_objects
.err
) {
280 pfd
[pollsize
].fd
= pack_objects
.err
;
281 pfd
[pollsize
].events
= POLLIN
;
289 polltimeout
= pack_data
->keepalive
< 0
291 : 1000 * pack_data
->keepalive
;
293 ret
= poll(pfd
, pollsize
, polltimeout
);
296 if (errno
!= EINTR
) {
297 error_errno("poll failed, resuming");
302 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
303 /* Status ready; we ship that in the side-band
304 * or dump to the standard error.
306 sz
= xread(pack_objects
.err
, progress
,
309 send_client_data(2, progress
, sz
,
310 pack_data
->use_sideband
);
312 close(pack_objects
.err
);
313 pack_objects
.err
= -1;
317 /* give priority to status messages */
320 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
321 /* Data ready; we keep the last byte to ourselves
322 * in case we detect broken rev-list, so that we
323 * can leave the stream corrupted. This is
324 * unfortunate -- unpack-objects would happily
325 * accept a valid packdata with trailing garbage,
326 * so appending garbage after we pass all the
327 * pack data is not good enough to signal
328 * breakage to downstream.
336 sz
= xread(pack_objects
.out
, cp
,
337 sizeof(data
) - outsz
);
341 close(pack_objects
.out
);
342 pack_objects
.out
= -1;
348 buffered
= data
[sz
-1] & 0xFF;
353 send_client_data(1, data
, sz
,
354 pack_data
->use_sideband
);
358 * We hit the keepalive timeout without saying anything; send
359 * an empty message on the data sideband just to let the other
360 * side know we're still working on it, but don't have any data
363 * If we don't have a sideband channel, there's no room in the
364 * protocol to say anything, so those clients are just out of
367 if (!ret
&& pack_data
->use_sideband
) {
368 static const char buf
[] = "0005\1";
369 write_or_die(1, buf
, 5);
373 if (finish_command(&pack_objects
)) {
374 error("git upload-pack: git-pack-objects died with error.");
381 send_client_data(1, data
, 1,
382 pack_data
->use_sideband
);
383 fprintf(stderr
, "flushed.\n");
385 if (pack_data
->use_sideband
)
390 send_client_data(3, abort_msg
, sizeof(abort_msg
),
391 pack_data
->use_sideband
);
392 die("git upload-pack: %s", abort_msg
);
395 static int do_got_oid(struct upload_pack_data
*data
, const struct object_id
*oid
)
397 int we_knew_they_have
= 0;
398 struct object
*o
= parse_object(the_repository
, oid
);
401 die("oops (%s)", oid_to_hex(oid
));
402 if (o
->type
== OBJ_COMMIT
) {
403 struct commit_list
*parents
;
404 struct commit
*commit
= (struct commit
*)o
;
405 if (o
->flags
& THEY_HAVE
)
406 we_knew_they_have
= 1;
408 o
->flags
|= THEY_HAVE
;
409 if (!data
->oldest_have
|| (commit
->date
< data
->oldest_have
))
410 data
->oldest_have
= commit
->date
;
411 for (parents
= commit
->parents
;
413 parents
= parents
->next
)
414 parents
->item
->object
.flags
|= THEY_HAVE
;
416 if (!we_knew_they_have
) {
417 add_object_array(o
, NULL
, &data
->have_obj
);
423 static int got_oid(struct upload_pack_data
*data
,
424 const char *hex
, struct object_id
*oid
)
426 if (get_oid_hex(hex
, oid
))
427 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
428 if (!has_object_file(oid
))
430 return do_got_oid(data
, oid
);
433 static int ok_to_give_up(struct upload_pack_data
*data
)
435 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
437 if (!data
->have_obj
.nr
)
440 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
441 COMMON_KNOWN
, data
->oldest_have
,
445 static int get_common_commits(struct upload_pack_data
*data
,
446 struct packet_reader
*reader
)
448 struct object_id oid
;
449 char last_hex
[GIT_MAX_HEXSZ
+ 1];
454 save_commit_buffer
= 0;
459 reset_timeout(data
->timeout
);
461 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
462 if (data
->multi_ack
== MULTI_ACK_DETAILED
465 && ok_to_give_up(data
)) {
467 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
469 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
470 packet_write_fmt(1, "NAK\n");
472 if (data
->no_done
&& sent_ready
) {
473 packet_write_fmt(1, "ACK %s\n", last_hex
);
476 if (data
->stateless_rpc
)
482 if (skip_prefix(reader
->line
, "have ", &arg
)) {
483 switch (got_oid(data
, arg
, &oid
)) {
484 case -1: /* they have what we do not */
487 && ok_to_give_up(data
)) {
488 const char *hex
= oid_to_hex(&oid
);
489 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
491 packet_write_fmt(1, "ACK %s ready\n", hex
);
493 packet_write_fmt(1, "ACK %s continue\n", hex
);
498 oid_to_hex_r(last_hex
, &oid
);
499 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
500 packet_write_fmt(1, "ACK %s common\n", last_hex
);
501 else if (data
->multi_ack
)
502 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
503 else if (data
->have_obj
.nr
== 1)
504 packet_write_fmt(1, "ACK %s\n", last_hex
);
509 if (!strcmp(reader
->line
, "done")) {
510 if (data
->have_obj
.nr
> 0) {
512 packet_write_fmt(1, "ACK %s\n", last_hex
);
515 packet_write_fmt(1, "NAK\n");
518 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
522 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
524 int allow_hidden_ref
= (allow_uor
&
525 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
526 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
530 * on successful case, it's up to the caller to close cmd->out
532 static int do_reachable_revlist(struct child_process
*cmd
,
533 struct object_array
*src
,
534 struct object_array
*reachable
,
535 enum allow_uor allow_uor
)
537 static const char *argv
[] = {
538 "rev-list", "--stdin", NULL
,
541 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
543 const unsigned hexsz
= the_hash_algo
->hexsz
;
552 * If the next rev-list --stdin encounters an unknown commit,
553 * it terminates, which will cause SIGPIPE in the write loop
556 sigchain_push(SIGPIPE
, SIG_IGN
);
558 if (start_command(cmd
))
562 namebuf
[hexsz
+ 1] = '\n';
563 for (i
= get_max_object_index(); 0 < i
; ) {
564 o
= get_indexed_object(--i
);
567 if (reachable
&& o
->type
== OBJ_COMMIT
)
568 o
->flags
&= ~TMP_MARK
;
569 if (!is_our_ref(o
, allow_uor
))
571 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
572 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
575 namebuf
[hexsz
] = '\n';
576 for (i
= 0; i
< src
->nr
; i
++) {
577 o
= src
->objects
[i
].item
;
578 if (is_our_ref(o
, allow_uor
)) {
580 add_object_array(o
, NULL
, reachable
);
583 if (reachable
&& o
->type
== OBJ_COMMIT
)
584 o
->flags
|= TMP_MARK
;
585 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
586 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
591 sigchain_pop(SIGPIPE
);
596 sigchain_pop(SIGPIPE
);
605 static int get_reachable_list(struct upload_pack_data
*data
,
606 struct object_array
*reachable
)
608 struct child_process cmd
= CHILD_PROCESS_INIT
;
611 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
612 const unsigned hexsz
= the_hash_algo
->hexsz
;
614 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
615 data
->allow_uor
) < 0)
618 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
619 struct object_id oid
;
622 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
625 o
= lookup_object(the_repository
, &oid
);
626 if (o
&& o
->type
== OBJ_COMMIT
) {
627 o
->flags
&= ~TMP_MARK
;
630 for (i
= get_max_object_index(); 0 < i
; i
--) {
631 o
= get_indexed_object(i
- 1);
632 if (o
&& o
->type
== OBJ_COMMIT
&&
633 (o
->flags
& TMP_MARK
)) {
634 add_object_array(o
, NULL
, reachable
);
635 o
->flags
&= ~TMP_MARK
;
640 if (finish_command(&cmd
))
646 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
648 struct child_process cmd
= CHILD_PROCESS_INIT
;
652 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
656 * The commits out of the rev-list are not ancestors of
659 i
= read_in_full(cmd
.out
, buf
, 1);
666 * rev-list may have died by encountering a bad commit
667 * in the history, in which case we do want to bail out
668 * even when it showed no commit.
670 if (finish_command(&cmd
))
673 /* All the non-tip ones are ancestors of what we advertised */
677 sigchain_pop(SIGPIPE
);
683 static void check_non_tip(struct upload_pack_data
*data
)
688 * In the normal in-process case without
689 * uploadpack.allowReachableSHA1InWant,
690 * non-tip requests can never happen.
692 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
694 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
695 /* All the non-tip ones are ancestors of what we advertised */
699 /* Pick one of them (we know there at least is one) */
700 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
701 struct object
*o
= data
->want_obj
.objects
[i
].item
;
702 if (!is_our_ref(o
, data
->allow_uor
)) {
703 packet_writer_error(&data
->writer
,
704 "upload-pack: not our ref %s",
705 oid_to_hex(&o
->oid
));
706 die("git upload-pack: not our ref %s",
707 oid_to_hex(&o
->oid
));
712 static void send_shallow(struct upload_pack_data
*data
,
713 struct commit_list
*result
)
716 struct object
*object
= &result
->item
->object
;
717 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
718 packet_writer_write(&data
->writer
, "shallow %s",
719 oid_to_hex(&object
->oid
));
720 register_shallow(the_repository
, &object
->oid
);
723 result
= result
->next
;
727 static void send_unshallow(struct upload_pack_data
*data
)
731 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
732 struct object
*object
= data
->shallows
.objects
[i
].item
;
733 if (object
->flags
& NOT_SHALLOW
) {
734 struct commit_list
*parents
;
735 packet_writer_write(&data
->writer
, "unshallow %s",
736 oid_to_hex(&object
->oid
));
737 object
->flags
&= ~CLIENT_SHALLOW
;
739 * We want to _register_ "object" as shallow, but we
740 * also need to traverse object's parents to deepen a
741 * shallow clone. Unregister it for now so we can
742 * parse and add the parents to the want list, then
745 unregister_shallow(&object
->oid
);
747 parse_commit_or_die((struct commit
*)object
);
748 parents
= ((struct commit
*)object
)->parents
;
750 add_object_array(&parents
->item
->object
,
751 NULL
, &data
->want_obj
);
752 parents
= parents
->next
;
754 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
756 /* make sure commit traversal conforms to client */
757 register_shallow(the_repository
, &object
->oid
);
761 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
762 int flag
, void *cb_data
);
763 static void deepen(struct upload_pack_data
*data
, int depth
)
765 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
768 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
769 struct object
*object
= data
->shallows
.objects
[i
].item
;
770 object
->flags
|= NOT_SHALLOW
;
772 } else if (data
->deepen_relative
) {
773 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
774 struct commit_list
*result
;
777 * Checking for reachable shallows requires that our refs be
778 * marked with OUR_REF.
780 head_ref_namespaced(check_ref
, NULL
);
781 for_each_namespaced_ref(check_ref
, NULL
);
783 get_reachable_list(data
, &reachable_shallows
);
784 result
= get_shallow_commits(&reachable_shallows
,
786 SHALLOW
, NOT_SHALLOW
);
787 send_shallow(data
, result
);
788 free_commit_list(result
);
789 object_array_clear(&reachable_shallows
);
791 struct commit_list
*result
;
793 result
= get_shallow_commits(&data
->want_obj
, depth
,
794 SHALLOW
, NOT_SHALLOW
);
795 send_shallow(data
, result
);
796 free_commit_list(result
);
799 send_unshallow(data
);
802 static void deepen_by_rev_list(struct upload_pack_data
*data
,
806 struct commit_list
*result
;
808 disable_commit_graph(the_repository
);
809 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
810 send_shallow(data
, result
);
811 free_commit_list(result
);
812 send_unshallow(data
);
815 /* Returns 1 if a shallow list is sent or 0 otherwise */
816 static int send_shallow_list(struct upload_pack_data
*data
)
820 if (data
->depth
> 0 && data
->deepen_rev_list
)
821 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
822 if (data
->depth
> 0) {
823 deepen(data
, data
->depth
);
825 } else if (data
->deepen_rev_list
) {
826 struct argv_array av
= ARGV_ARRAY_INIT
;
829 argv_array_push(&av
, "rev-list");
830 if (data
->deepen_since
)
831 argv_array_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
832 if (data
->deepen_not
.nr
) {
833 argv_array_push(&av
, "--not");
834 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
835 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
836 argv_array_push(&av
, s
->string
);
838 argv_array_push(&av
, "--not");
840 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
841 struct object
*o
= data
->want_obj
.objects
[i
].item
;
842 argv_array_push(&av
, oid_to_hex(&o
->oid
));
844 deepen_by_rev_list(data
, av
.argc
, av
.argv
);
845 argv_array_clear(&av
);
848 if (data
->shallows
.nr
> 0) {
850 for (i
= 0; i
< data
->shallows
.nr
; i
++)
851 register_shallow(the_repository
,
852 &data
->shallows
.objects
[i
].item
->oid
);
856 data
->shallow_nr
+= data
->shallows
.nr
;
860 static int process_shallow(const char *line
, struct object_array
*shallows
)
863 if (skip_prefix(line
, "shallow ", &arg
)) {
864 struct object_id oid
;
865 struct object
*object
;
866 if (get_oid_hex(arg
, &oid
))
867 die("invalid shallow line: %s", line
);
868 object
= parse_object(the_repository
, &oid
);
871 if (object
->type
!= OBJ_COMMIT
)
872 die("invalid shallow object %s", oid_to_hex(&oid
));
873 if (!(object
->flags
& CLIENT_SHALLOW
)) {
874 object
->flags
|= CLIENT_SHALLOW
;
875 add_object_array(object
, NULL
, shallows
);
883 static int process_deepen(const char *line
, int *depth
)
886 if (skip_prefix(line
, "deepen ", &arg
)) {
888 *depth
= (int)strtol(arg
, &end
, 0);
889 if (!end
|| *end
|| *depth
<= 0)
890 die("Invalid deepen: %s", line
);
897 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
900 if (skip_prefix(line
, "deepen-since ", &arg
)) {
902 *deepen_since
= parse_timestamp(arg
, &end
, 0);
903 if (!end
|| *end
|| !deepen_since
||
904 /* revisions.c's max_age -1 is special */
906 die("Invalid deepen-since: %s", line
);
907 *deepen_rev_list
= 1;
913 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
916 if (skip_prefix(line
, "deepen-not ", &arg
)) {
918 struct object_id oid
;
919 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
920 die("git upload-pack: ambiguous deepen-not: %s", line
);
921 string_list_append(deepen_not
, ref
);
923 *deepen_rev_list
= 1;
929 static void receive_needs(struct upload_pack_data
*data
,
930 struct packet_reader
*reader
)
934 data
->shallow_nr
= 0;
937 const char *features
;
938 struct object_id oid_buf
;
941 reset_timeout(data
->timeout
);
942 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
945 if (process_shallow(reader
->line
, &data
->shallows
))
947 if (process_deepen(reader
->line
, &data
->depth
))
949 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
951 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
954 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
955 if (!data
->filter_capability_requested
)
956 die("git upload-pack: filtering capability not negotiated");
957 list_objects_filter_die_if_populated(&data
->filter_options
);
958 parse_list_objects_filter(&data
->filter_options
, arg
);
962 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
963 parse_oid_hex(arg
, &oid_buf
, &features
))
964 die("git upload-pack: protocol error, "
965 "expected to get object ID, not '%s'", reader
->line
);
967 if (parse_feature_request(features
, "deepen-relative"))
968 data
->deepen_relative
= 1;
969 if (parse_feature_request(features
, "multi_ack_detailed"))
970 data
->multi_ack
= MULTI_ACK_DETAILED
;
971 else if (parse_feature_request(features
, "multi_ack"))
972 data
->multi_ack
= MULTI_ACK
;
973 if (parse_feature_request(features
, "no-done"))
975 if (parse_feature_request(features
, "thin-pack"))
976 data
->use_thin_pack
= 1;
977 if (parse_feature_request(features
, "ofs-delta"))
978 data
->use_ofs_delta
= 1;
979 if (parse_feature_request(features
, "side-band-64k"))
980 data
->use_sideband
= LARGE_PACKET_MAX
;
981 else if (parse_feature_request(features
, "side-band"))
982 data
->use_sideband
= DEFAULT_PACKET_MAX
;
983 if (parse_feature_request(features
, "no-progress"))
984 data
->no_progress
= 1;
985 if (parse_feature_request(features
, "include-tag"))
986 data
->use_include_tag
= 1;
987 if (data
->allow_filter
&&
988 parse_feature_request(features
, "filter"))
989 data
->filter_capability_requested
= 1;
991 o
= parse_object(the_repository
, &oid_buf
);
993 packet_writer_error(&data
->writer
,
994 "upload-pack: not our ref %s",
995 oid_to_hex(&oid_buf
));
996 die("git upload-pack: not our ref %s",
997 oid_to_hex(&oid_buf
));
999 if (!(o
->flags
& WANTED
)) {
1001 if (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1002 || is_our_ref(o
, data
->allow_uor
)))
1004 add_object_array(o
, NULL
, &data
->want_obj
);
1009 * We have sent all our refs already, and the other end
1010 * should have chosen out of them. When we are operating
1011 * in the stateless RPC mode, however, their choice may
1012 * have been based on the set of older refs advertised
1013 * by another process that handled the initial request.
1016 check_non_tip(data
);
1018 if (!data
->use_sideband
&& data
->daemon_mode
)
1019 data
->no_progress
= 1;
1021 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1024 if (send_shallow_list(data
))
1028 /* return non-zero if the ref is hidden, otherwise 0 */
1029 static int mark_our_ref(const char *refname
, const char *refname_full
,
1030 const struct object_id
*oid
)
1032 struct object
*o
= lookup_unknown_object(oid
);
1034 if (ref_is_hidden(refname
, refname_full
)) {
1035 o
->flags
|= HIDDEN_REF
;
1038 o
->flags
|= OUR_REF
;
1042 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1043 int flag
, void *cb_data
)
1045 const char *refname
= strip_namespace(refname_full
);
1047 mark_our_ref(refname
, refname_full
, oid
);
1051 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1053 struct string_list_item
*item
;
1057 for_each_string_list_item(item
, symref
)
1058 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1061 static int send_ref(const char *refname
, const struct object_id
*oid
,
1062 int flag
, void *cb_data
)
1064 static const char *capabilities
= "multi_ack thin-pack side-band"
1065 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1066 " deepen-relative no-progress include-tag multi_ack_detailed";
1067 const char *refname_nons
= strip_namespace(refname
);
1068 struct object_id peeled
;
1069 struct upload_pack_data
*data
= cb_data
;
1071 if (mark_our_ref(refname_nons
, refname
, oid
))
1075 struct strbuf symref_info
= STRBUF_INIT
;
1077 format_symref_info(&symref_info
, &data
->symref
);
1078 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1079 oid_to_hex(oid
), refname_nons
,
1081 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1082 " allow-tip-sha1-in-want" : "",
1083 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1084 " allow-reachable-sha1-in-want" : "",
1085 data
->stateless_rpc
? " no-done" : "",
1087 data
->allow_filter
? " filter" : "",
1088 git_user_agent_sanitized());
1089 strbuf_release(&symref_info
);
1091 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1093 capabilities
= NULL
;
1094 if (!peel_ref(refname
, &peeled
))
1095 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1099 static int find_symref(const char *refname
, const struct object_id
*oid
,
1100 int flag
, void *cb_data
)
1102 const char *symref_target
;
1103 struct string_list_item
*item
;
1105 if ((flag
& REF_ISSYMREF
) == 0)
1107 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1108 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1109 die("'%s' is a symref but it is not?", refname
);
1110 item
= string_list_append(cb_data
, strip_namespace(refname
));
1111 item
->util
= xstrdup(strip_namespace(symref_target
));
1115 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1117 struct upload_pack_data
*data
= cb_data
;
1119 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1120 if (git_config_bool(var
, value
))
1121 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1123 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1124 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1125 if (git_config_bool(var
, value
))
1126 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1128 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1129 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1130 if (git_config_bool(var
, value
))
1131 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1133 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1134 } else if (!strcmp("uploadpack.keepalive", var
)) {
1135 data
->keepalive
= git_config_int(var
, value
);
1136 if (!data
->keepalive
)
1137 data
->keepalive
= -1;
1138 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1139 data
->allow_filter
= git_config_bool(var
, value
);
1140 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1141 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1142 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1143 data
->allow_sideband_all
= git_config_bool(var
, value
);
1144 } else if (!strcmp("core.precomposeunicode", var
)) {
1145 precomposed_unicode
= git_config_bool(var
, value
);
1148 if (current_config_scope() != CONFIG_SCOPE_LOCAL
&&
1149 current_config_scope() != CONFIG_SCOPE_WORKTREE
) {
1150 if (!strcmp("uploadpack.packobjectshook", var
))
1151 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1154 return parse_hide_refs_config(var
, value
, "uploadpack");
1157 void upload_pack(struct upload_pack_options
*options
)
1159 struct packet_reader reader
;
1160 struct upload_pack_data data
;
1162 upload_pack_data_init(&data
);
1164 git_config(upload_pack_config
, &data
);
1166 data
.stateless_rpc
= options
->stateless_rpc
;
1167 data
.daemon_mode
= options
->daemon_mode
;
1168 data
.timeout
= options
->timeout
;
1170 head_ref_namespaced(find_symref
, &data
.symref
);
1172 if (options
->advertise_refs
|| !data
.stateless_rpc
) {
1173 reset_timeout(data
.timeout
);
1174 head_ref_namespaced(send_ref
, &data
);
1175 for_each_namespaced_ref(send_ref
, &data
);
1176 advertise_shallow_grafts(1);
1179 head_ref_namespaced(check_ref
, NULL
);
1180 for_each_namespaced_ref(check_ref
, NULL
);
1183 if (!options
->advertise_refs
) {
1184 packet_reader_init(&reader
, 0, NULL
, 0,
1185 PACKET_READ_CHOMP_NEWLINE
|
1186 PACKET_READ_DIE_ON_ERR_PACKET
);
1188 receive_needs(&data
, &reader
);
1189 if (data
.want_obj
.nr
) {
1190 get_common_commits(&data
, &reader
);
1191 create_pack_file(&data
);
1195 upload_pack_data_clear(&data
);
1198 static int parse_want(struct packet_writer
*writer
, const char *line
,
1199 struct object_array
*want_obj
)
1202 if (skip_prefix(line
, "want ", &arg
)) {
1203 struct object_id oid
;
1206 if (get_oid_hex(arg
, &oid
))
1207 die("git upload-pack: protocol error, "
1208 "expected to get oid, not '%s'", line
);
1210 o
= parse_object(the_repository
, &oid
);
1212 packet_writer_error(writer
,
1213 "upload-pack: not our ref %s",
1215 die("git upload-pack: not our ref %s",
1219 if (!(o
->flags
& WANTED
)) {
1221 add_object_array(o
, NULL
, want_obj
);
1230 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1231 struct string_list
*wanted_refs
,
1232 struct object_array
*want_obj
)
1235 if (skip_prefix(line
, "want-ref ", &arg
)) {
1236 struct object_id oid
;
1237 struct string_list_item
*item
;
1240 if (read_ref(arg
, &oid
)) {
1241 packet_writer_error(writer
, "unknown ref %s", arg
);
1242 die("unknown ref %s", arg
);
1245 item
= string_list_append(wanted_refs
, arg
);
1246 item
->util
= oiddup(&oid
);
1248 o
= parse_object_or_die(&oid
, arg
);
1249 if (!(o
->flags
& WANTED
)) {
1251 add_object_array(o
, NULL
, want_obj
);
1260 static int parse_have(const char *line
, struct oid_array
*haves
)
1263 if (skip_prefix(line
, "have ", &arg
)) {
1264 struct object_id oid
;
1266 if (get_oid_hex(arg
, &oid
))
1267 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1268 oid_array_append(haves
, &oid
);
1275 static void process_args(struct packet_reader
*request
,
1276 struct upload_pack_data
*data
)
1278 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1279 const char *arg
= request
->line
;
1283 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1285 if (data
->allow_ref_in_want
&&
1286 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1289 /* process have line */
1290 if (parse_have(arg
, &data
->haves
))
1293 /* process args like thin-pack */
1294 if (!strcmp(arg
, "thin-pack")) {
1295 data
->use_thin_pack
= 1;
1298 if (!strcmp(arg
, "ofs-delta")) {
1299 data
->use_ofs_delta
= 1;
1302 if (!strcmp(arg
, "no-progress")) {
1303 data
->no_progress
= 1;
1306 if (!strcmp(arg
, "include-tag")) {
1307 data
->use_include_tag
= 1;
1310 if (!strcmp(arg
, "done")) {
1315 /* Shallow related arguments */
1316 if (process_shallow(arg
, &data
->shallows
))
1318 if (process_deepen(arg
, &data
->depth
))
1320 if (process_deepen_since(arg
, &data
->deepen_since
,
1321 &data
->deepen_rev_list
))
1323 if (process_deepen_not(arg
, &data
->deepen_not
,
1324 &data
->deepen_rev_list
))
1326 if (!strcmp(arg
, "deepen-relative")) {
1327 data
->deepen_relative
= 1;
1331 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1332 list_objects_filter_die_if_populated(&data
->filter_options
);
1333 parse_list_objects_filter(&data
->filter_options
, p
);
1337 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1338 data
->allow_sideband_all
) &&
1339 !strcmp(arg
, "sideband-all")) {
1340 data
->writer
.use_sideband
= 1;
1344 /* ignore unknown lines maybe? */
1345 die("unexpected line: '%s'", arg
);
1348 if (request
->status
!= PACKET_READ_FLUSH
)
1349 die(_("expected flush after fetch arguments"));
1352 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1357 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1358 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1360 if (!has_object_file(oid
))
1363 oid_array_append(common
, oid
);
1365 do_got_oid(data
, oid
);
1371 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1375 packet_writer_write(&data
->writer
, "acknowledgments\n");
1379 packet_writer_write(&data
->writer
, "NAK\n");
1381 for (i
= 0; i
< acks
->nr
; i
++) {
1382 packet_writer_write(&data
->writer
, "ACK %s\n",
1383 oid_to_hex(&acks
->oid
[i
]));
1386 if (ok_to_give_up(data
)) {
1388 packet_writer_write(&data
->writer
, "ready\n");
1395 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1397 struct oid_array common
= OID_ARRAY_INIT
;
1400 process_haves(data
, &common
);
1403 } else if (send_acks(data
, &common
)) {
1404 packet_writer_delim(&data
->writer
);
1408 packet_writer_flush(&data
->writer
);
1412 oid_array_clear(&data
->haves
);
1413 oid_array_clear(&common
);
1417 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1419 const struct string_list_item
*item
;
1421 if (!data
->wanted_refs
.nr
)
1424 packet_writer_write(&data
->writer
, "wanted-refs\n");
1426 for_each_string_list_item(item
, &data
->wanted_refs
) {
1427 packet_writer_write(&data
->writer
, "%s %s\n",
1428 oid_to_hex(item
->util
),
1432 packet_writer_delim(&data
->writer
);
1435 static void send_shallow_info(struct upload_pack_data
*data
)
1437 /* No shallow info needs to be sent */
1438 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1439 !is_repository_shallow(the_repository
))
1442 packet_writer_write(&data
->writer
, "shallow-info\n");
1444 if (!send_shallow_list(data
) &&
1445 is_repository_shallow(the_repository
))
1446 deepen(data
, INFINITE_DEPTH
);
1452 FETCH_PROCESS_ARGS
= 0,
1458 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1459 struct packet_reader
*request
)
1461 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1462 struct upload_pack_data data
;
1464 clear_object_flags(ALL_FLAGS
);
1466 upload_pack_data_init(&data
);
1467 data
.use_sideband
= LARGE_PACKET_MAX
;
1469 git_config(upload_pack_config
, &data
);
1471 while (state
!= FETCH_DONE
) {
1473 case FETCH_PROCESS_ARGS
:
1474 process_args(request
, &data
);
1476 if (!data
.want_obj
.nr
) {
1478 * Request didn't contain any 'want' lines,
1479 * guess they didn't want anything.
1482 } else if (data
.haves
.nr
) {
1484 * Request had 'have' lines, so lets ACK them.
1486 state
= FETCH_SEND_ACKS
;
1489 * Request had 'want's but no 'have's so we can
1490 * immedietly go to construct and send a pack.
1492 state
= FETCH_SEND_PACK
;
1495 case FETCH_SEND_ACKS
:
1496 if (process_haves_and_send_acks(&data
))
1497 state
= FETCH_SEND_PACK
;
1501 case FETCH_SEND_PACK
:
1502 send_wanted_ref_info(&data
);
1503 send_shallow_info(&data
);
1505 packet_writer_write(&data
.writer
, "packfile\n");
1506 create_pack_file(&data
);
1514 upload_pack_data_clear(&data
);
1518 int upload_pack_advertise(struct repository
*r
,
1519 struct strbuf
*value
)
1522 int allow_filter_value
;
1523 int allow_ref_in_want
;
1524 int allow_sideband_all_value
;
1526 strbuf_addstr(value
, "shallow");
1528 if (!repo_config_get_bool(the_repository
,
1529 "uploadpack.allowfilter",
1530 &allow_filter_value
) &&
1532 strbuf_addstr(value
, " filter");
1534 if (!repo_config_get_bool(the_repository
,
1535 "uploadpack.allowrefinwant",
1536 &allow_ref_in_want
) &&
1538 strbuf_addstr(value
, " ref-in-want");
1540 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1541 (!repo_config_get_bool(the_repository
,
1542 "uploadpack.allowsidebandall",
1543 &allow_sideband_all_value
) &&
1544 allow_sideband_all_value
))
1545 strbuf_addstr(value
, " sideband-all");