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 static timestamp_t oldest_have
;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request
;
54 static int shallow_nr
;
55 static struct object_array extra_edge_obj
;
56 static const char *pack_objects_hook
;
59 * Please annotate, and if possible group together, fields used only
60 * for protocol v0 or only for protocol v2.
62 struct upload_pack_data
{
63 struct string_list symref
; /* v0 only */
64 struct object_array want_obj
;
65 struct object_array have_obj
;
66 struct oid_array haves
; /* v2 only */
67 struct string_list wanted_refs
; /* v2 only */
69 struct object_array shallows
;
70 struct string_list deepen_not
;
72 timestamp_t deepen_since
;
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 list_objects_filter_options filter_options
;
89 struct packet_writer writer
;
91 unsigned stateless_rpc
: 1; /* v0 only */
92 unsigned no_done
: 1; /* v0 only */
93 unsigned daemon_mode
: 1; /* v0 only */
94 unsigned filter_capability_requested
: 1; /* v0 only */
96 unsigned use_thin_pack
: 1;
97 unsigned use_ofs_delta
: 1;
98 unsigned no_progress
: 1;
99 unsigned use_include_tag
: 1;
100 unsigned allow_filter
: 1;
102 unsigned done
: 1; /* v2 only */
103 unsigned allow_ref_in_want
: 1; /* v2 only */
104 unsigned allow_sideband_all
: 1; /* v2 only */
107 static void upload_pack_data_init(struct upload_pack_data
*data
)
109 struct string_list symref
= STRING_LIST_INIT_DUP
;
110 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
111 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
112 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
113 struct oid_array haves
= OID_ARRAY_INIT
;
114 struct object_array shallows
= OBJECT_ARRAY_INIT
;
115 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
117 memset(data
, 0, sizeof(*data
));
118 data
->symref
= symref
;
119 data
->wanted_refs
= wanted_refs
;
120 data
->want_obj
= want_obj
;
121 data
->have_obj
= have_obj
;
123 data
->shallows
= shallows
;
124 data
->deepen_not
= deepen_not
;
125 packet_writer_init(&data
->writer
, 1);
130 static void upload_pack_data_clear(struct upload_pack_data
*data
)
132 string_list_clear(&data
->symref
, 1);
133 string_list_clear(&data
->wanted_refs
, 1);
134 object_array_clear(&data
->want_obj
);
135 object_array_clear(&data
->have_obj
);
136 oid_array_clear(&data
->haves
);
137 object_array_clear(&data
->shallows
);
138 string_list_clear(&data
->deepen_not
, 0);
139 list_objects_filter_release(&data
->filter_options
);
142 static void reset_timeout(unsigned int timeout
)
147 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
151 send_sideband(1, fd
, data
, sz
, use_sideband
);
158 /* XXX: are we happy to lose stuff here? */
159 xwrite(fd
, data
, sz
);
162 write_or_die(fd
, data
, sz
);
165 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
168 if (graft
->nr_parent
== -1)
169 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
173 static void create_pack_file(struct upload_pack_data
*pack_data
)
175 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
176 char data
[8193], progress
[128];
177 char abort_msg
[] = "aborting due to possible repository "
178 "corruption on the remote side.";
184 if (!pack_objects_hook
)
185 pack_objects
.git_cmd
= 1;
187 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
188 argv_array_push(&pack_objects
.args
, "git");
189 pack_objects
.use_shell
= 1;
193 argv_array_push(&pack_objects
.args
, "--shallow-file");
194 argv_array_push(&pack_objects
.args
, "");
196 argv_array_push(&pack_objects
.args
, "pack-objects");
197 argv_array_push(&pack_objects
.args
, "--revs");
198 if (pack_data
->use_thin_pack
)
199 argv_array_push(&pack_objects
.args
, "--thin");
201 argv_array_push(&pack_objects
.args
, "--stdout");
203 argv_array_push(&pack_objects
.args
, "--shallow");
204 if (!pack_data
->no_progress
)
205 argv_array_push(&pack_objects
.args
, "--progress");
206 if (pack_data
->use_ofs_delta
)
207 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
208 if (pack_data
->use_include_tag
)
209 argv_array_push(&pack_objects
.args
, "--include-tag");
210 if (pack_data
->filter_options
.choice
) {
212 expand_list_objects_filter_spec(&pack_data
->filter_options
);
213 if (pack_objects
.use_shell
) {
214 struct strbuf buf
= STRBUF_INIT
;
215 sq_quote_buf(&buf
, spec
);
216 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
217 strbuf_release(&buf
);
219 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
224 pack_objects
.in
= -1;
225 pack_objects
.out
= -1;
226 pack_objects
.err
= -1;
228 if (start_command(&pack_objects
))
229 die("git upload-pack: unable to fork git-pack-objects");
231 pipe_fd
= xfdopen(pack_objects
.in
, "w");
234 for_each_commit_graft(write_one_shallow
, pipe_fd
);
236 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
237 fprintf(pipe_fd
, "%s\n",
238 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
239 fprintf(pipe_fd
, "--not\n");
240 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
241 fprintf(pipe_fd
, "%s\n",
242 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
243 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
244 fprintf(pipe_fd
, "%s\n",
245 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
246 fprintf(pipe_fd
, "\n");
250 /* We read from pack_objects.err to capture stderr output for
251 * progress bar, and pack_objects.out to capture the pack data.
255 struct pollfd pfd
[2];
256 int pe
, pu
, pollsize
, polltimeout
;
259 reset_timeout(pack_data
->timeout
);
264 if (0 <= pack_objects
.out
) {
265 pfd
[pollsize
].fd
= pack_objects
.out
;
266 pfd
[pollsize
].events
= POLLIN
;
270 if (0 <= pack_objects
.err
) {
271 pfd
[pollsize
].fd
= pack_objects
.err
;
272 pfd
[pollsize
].events
= POLLIN
;
280 polltimeout
= pack_data
->keepalive
< 0
282 : 1000 * pack_data
->keepalive
;
284 ret
= poll(pfd
, pollsize
, polltimeout
);
287 if (errno
!= EINTR
) {
288 error_errno("poll failed, resuming");
293 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
294 /* Status ready; we ship that in the side-band
295 * or dump to the standard error.
297 sz
= xread(pack_objects
.err
, progress
,
300 send_client_data(2, progress
, sz
,
301 pack_data
->use_sideband
);
303 close(pack_objects
.err
);
304 pack_objects
.err
= -1;
308 /* give priority to status messages */
311 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
312 /* Data ready; we keep the last byte to ourselves
313 * in case we detect broken rev-list, so that we
314 * can leave the stream corrupted. This is
315 * unfortunate -- unpack-objects would happily
316 * accept a valid packdata with trailing garbage,
317 * so appending garbage after we pass all the
318 * pack data is not good enough to signal
319 * breakage to downstream.
327 sz
= xread(pack_objects
.out
, cp
,
328 sizeof(data
) - outsz
);
332 close(pack_objects
.out
);
333 pack_objects
.out
= -1;
339 buffered
= data
[sz
-1] & 0xFF;
344 send_client_data(1, data
, sz
,
345 pack_data
->use_sideband
);
349 * We hit the keepalive timeout without saying anything; send
350 * an empty message on the data sideband just to let the other
351 * side know we're still working on it, but don't have any data
354 * If we don't have a sideband channel, there's no room in the
355 * protocol to say anything, so those clients are just out of
358 if (!ret
&& pack_data
->use_sideband
) {
359 static const char buf
[] = "0005\1";
360 write_or_die(1, buf
, 5);
364 if (finish_command(&pack_objects
)) {
365 error("git upload-pack: git-pack-objects died with error.");
372 send_client_data(1, data
, 1,
373 pack_data
->use_sideband
);
374 fprintf(stderr
, "flushed.\n");
376 if (pack_data
->use_sideband
)
381 send_client_data(3, abort_msg
, sizeof(abort_msg
),
382 pack_data
->use_sideband
);
383 die("git upload-pack: %s", abort_msg
);
386 static int got_oid(const char *hex
, struct object_id
*oid
,
387 struct object_array
*have_obj
)
390 int we_knew_they_have
= 0;
392 if (get_oid_hex(hex
, oid
))
393 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
394 if (!has_object_file(oid
))
397 o
= parse_object(the_repository
, oid
);
399 die("oops (%s)", oid_to_hex(oid
));
400 if (o
->type
== OBJ_COMMIT
) {
401 struct commit_list
*parents
;
402 struct commit
*commit
= (struct commit
*)o
;
403 if (o
->flags
& THEY_HAVE
)
404 we_knew_they_have
= 1;
406 o
->flags
|= THEY_HAVE
;
407 if (!oldest_have
|| (commit
->date
< oldest_have
))
408 oldest_have
= commit
->date
;
409 for (parents
= commit
->parents
;
411 parents
= parents
->next
)
412 parents
->item
->object
.flags
|= THEY_HAVE
;
414 if (!we_knew_they_have
) {
415 add_object_array(o
, NULL
, have_obj
);
421 static int ok_to_give_up(const struct object_array
*have_obj
,
422 struct object_array
*want_obj
)
424 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
429 return can_all_from_reach_with_flag(want_obj
, THEY_HAVE
,
430 COMMON_KNOWN
, oldest_have
,
434 static int get_common_commits(struct upload_pack_data
*data
,
435 struct packet_reader
*reader
)
437 struct object_id oid
;
438 char last_hex
[GIT_MAX_HEXSZ
+ 1];
443 save_commit_buffer
= 0;
448 reset_timeout(data
->timeout
);
450 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
451 if (data
->multi_ack
== MULTI_ACK_DETAILED
454 && ok_to_give_up(&data
->have_obj
, &data
->want_obj
)) {
456 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
458 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
459 packet_write_fmt(1, "NAK\n");
461 if (data
->no_done
&& sent_ready
) {
462 packet_write_fmt(1, "ACK %s\n", last_hex
);
465 if (data
->stateless_rpc
)
471 if (skip_prefix(reader
->line
, "have ", &arg
)) {
472 switch (got_oid(arg
, &oid
, &data
->have_obj
)) {
473 case -1: /* they have what we do not */
476 && ok_to_give_up(&data
->have_obj
, &data
->want_obj
)) {
477 const char *hex
= oid_to_hex(&oid
);
478 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
480 packet_write_fmt(1, "ACK %s ready\n", hex
);
482 packet_write_fmt(1, "ACK %s continue\n", hex
);
487 oid_to_hex_r(last_hex
, &oid
);
488 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
489 packet_write_fmt(1, "ACK %s common\n", last_hex
);
490 else if (data
->multi_ack
)
491 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
492 else if (data
->have_obj
.nr
== 1)
493 packet_write_fmt(1, "ACK %s\n", last_hex
);
498 if (!strcmp(reader
->line
, "done")) {
499 if (data
->have_obj
.nr
> 0) {
501 packet_write_fmt(1, "ACK %s\n", last_hex
);
504 packet_write_fmt(1, "NAK\n");
507 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
511 static int is_our_ref(struct object
*o
)
513 int allow_hidden_ref
= (allow_unadvertised_object_request
&
514 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
515 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
519 * on successful case, it's up to the caller to close cmd->out
521 static int do_reachable_revlist(struct child_process
*cmd
,
522 struct object_array
*src
,
523 struct object_array
*reachable
)
525 static const char *argv
[] = {
526 "rev-list", "--stdin", NULL
,
529 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
531 const unsigned hexsz
= the_hash_algo
->hexsz
;
540 * If the next rev-list --stdin encounters an unknown commit,
541 * it terminates, which will cause SIGPIPE in the write loop
544 sigchain_push(SIGPIPE
, SIG_IGN
);
546 if (start_command(cmd
))
550 namebuf
[hexsz
+ 1] = '\n';
551 for (i
= get_max_object_index(); 0 < i
; ) {
552 o
= get_indexed_object(--i
);
555 if (reachable
&& o
->type
== OBJ_COMMIT
)
556 o
->flags
&= ~TMP_MARK
;
559 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
560 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
563 namebuf
[hexsz
] = '\n';
564 for (i
= 0; i
< src
->nr
; i
++) {
565 o
= src
->objects
[i
].item
;
568 add_object_array(o
, NULL
, reachable
);
571 if (reachable
&& o
->type
== OBJ_COMMIT
)
572 o
->flags
|= TMP_MARK
;
573 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
574 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
579 sigchain_pop(SIGPIPE
);
584 sigchain_pop(SIGPIPE
);
593 static int get_reachable_list(struct object_array
*src
,
594 struct object_array
*reachable
)
596 struct child_process cmd
= CHILD_PROCESS_INIT
;
599 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
600 const unsigned hexsz
= the_hash_algo
->hexsz
;
602 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
605 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
606 struct object_id oid
;
609 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
612 o
= lookup_object(the_repository
, &oid
);
613 if (o
&& o
->type
== OBJ_COMMIT
) {
614 o
->flags
&= ~TMP_MARK
;
617 for (i
= get_max_object_index(); 0 < i
; i
--) {
618 o
= get_indexed_object(i
- 1);
619 if (o
&& o
->type
== OBJ_COMMIT
&&
620 (o
->flags
& TMP_MARK
)) {
621 add_object_array(o
, NULL
, reachable
);
622 o
->flags
&= ~TMP_MARK
;
627 if (finish_command(&cmd
))
633 static int has_unreachable(struct object_array
*src
)
635 struct child_process cmd
= CHILD_PROCESS_INIT
;
639 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
643 * The commits out of the rev-list are not ancestors of
646 i
= read_in_full(cmd
.out
, buf
, 1);
653 * rev-list may have died by encountering a bad commit
654 * in the history, in which case we do want to bail out
655 * even when it showed no commit.
657 if (finish_command(&cmd
))
660 /* All the non-tip ones are ancestors of what we advertised */
664 sigchain_pop(SIGPIPE
);
670 static void check_non_tip(struct upload_pack_data
*data
)
675 * In the normal in-process case without
676 * uploadpack.allowReachableSHA1InWant,
677 * non-tip requests can never happen.
679 if (!data
->stateless_rpc
680 && !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
682 if (!has_unreachable(&data
->want_obj
))
683 /* All the non-tip ones are ancestors of what we advertised */
687 /* Pick one of them (we know there at least is one) */
688 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
689 struct object
*o
= data
->want_obj
.objects
[i
].item
;
690 if (!is_our_ref(o
)) {
691 packet_writer_error(&data
->writer
,
692 "upload-pack: not our ref %s",
693 oid_to_hex(&o
->oid
));
694 die("git upload-pack: not our ref %s",
695 oid_to_hex(&o
->oid
));
700 static void send_shallow(struct packet_writer
*writer
,
701 struct commit_list
*result
)
704 struct object
*object
= &result
->item
->object
;
705 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
706 packet_writer_write(writer
, "shallow %s",
707 oid_to_hex(&object
->oid
));
708 register_shallow(the_repository
, &object
->oid
);
711 result
= result
->next
;
715 static void send_unshallow(struct packet_writer
*writer
,
716 const struct object_array
*shallows
,
717 struct object_array
*want_obj
)
721 for (i
= 0; i
< shallows
->nr
; i
++) {
722 struct object
*object
= shallows
->objects
[i
].item
;
723 if (object
->flags
& NOT_SHALLOW
) {
724 struct commit_list
*parents
;
725 packet_writer_write(writer
, "unshallow %s",
726 oid_to_hex(&object
->oid
));
727 object
->flags
&= ~CLIENT_SHALLOW
;
729 * We want to _register_ "object" as shallow, but we
730 * also need to traverse object's parents to deepen a
731 * shallow clone. Unregister it for now so we can
732 * parse and add the parents to the want list, then
735 unregister_shallow(&object
->oid
);
737 parse_commit_or_die((struct commit
*)object
);
738 parents
= ((struct commit
*)object
)->parents
;
740 add_object_array(&parents
->item
->object
,
742 parents
= parents
->next
;
744 add_object_array(object
, NULL
, &extra_edge_obj
);
746 /* make sure commit traversal conforms to client */
747 register_shallow(the_repository
, &object
->oid
);
751 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
752 int flag
, void *cb_data
);
753 static void deepen(struct packet_writer
*writer
, int depth
, int deepen_relative
,
754 struct object_array
*shallows
, struct object_array
*want_obj
)
756 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
759 for (i
= 0; i
< shallows
->nr
; i
++) {
760 struct object
*object
= shallows
->objects
[i
].item
;
761 object
->flags
|= NOT_SHALLOW
;
763 } else if (deepen_relative
) {
764 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
765 struct commit_list
*result
;
768 * Checking for reachable shallows requires that our refs be
769 * marked with OUR_REF.
771 head_ref_namespaced(check_ref
, NULL
);
772 for_each_namespaced_ref(check_ref
, NULL
);
774 get_reachable_list(shallows
, &reachable_shallows
);
775 result
= get_shallow_commits(&reachable_shallows
,
777 SHALLOW
, NOT_SHALLOW
);
778 send_shallow(writer
, result
);
779 free_commit_list(result
);
780 object_array_clear(&reachable_shallows
);
782 struct commit_list
*result
;
784 result
= get_shallow_commits(want_obj
, depth
,
785 SHALLOW
, NOT_SHALLOW
);
786 send_shallow(writer
, result
);
787 free_commit_list(result
);
790 send_unshallow(writer
, shallows
, want_obj
);
793 static void deepen_by_rev_list(struct packet_writer
*writer
, int ac
,
795 struct object_array
*shallows
,
796 struct object_array
*want_obj
)
798 struct commit_list
*result
;
800 disable_commit_graph(the_repository
);
801 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
802 send_shallow(writer
, result
);
803 free_commit_list(result
);
804 send_unshallow(writer
, shallows
, want_obj
);
807 /* Returns 1 if a shallow list is sent or 0 otherwise */
808 static int send_shallow_list(struct packet_writer
*writer
,
809 int depth
, int deepen_rev_list
,
810 timestamp_t deepen_since
,
811 struct string_list
*deepen_not
,
813 struct object_array
*shallows
,
814 struct object_array
*want_obj
)
818 if (depth
> 0 && deepen_rev_list
)
819 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
821 deepen(writer
, depth
, deepen_relative
, shallows
, want_obj
);
823 } else if (deepen_rev_list
) {
824 struct argv_array av
= ARGV_ARRAY_INIT
;
827 argv_array_push(&av
, "rev-list");
829 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
830 if (deepen_not
->nr
) {
831 argv_array_push(&av
, "--not");
832 for (i
= 0; i
< deepen_not
->nr
; i
++) {
833 struct string_list_item
*s
= deepen_not
->items
+ i
;
834 argv_array_push(&av
, s
->string
);
836 argv_array_push(&av
, "--not");
838 for (i
= 0; i
< want_obj
->nr
; i
++) {
839 struct object
*o
= want_obj
->objects
[i
].item
;
840 argv_array_push(&av
, oid_to_hex(&o
->oid
));
842 deepen_by_rev_list(writer
, av
.argc
, av
.argv
, shallows
, want_obj
);
843 argv_array_clear(&av
);
846 if (shallows
->nr
> 0) {
848 for (i
= 0; i
< shallows
->nr
; i
++)
849 register_shallow(the_repository
,
850 &shallows
->objects
[i
].item
->oid
);
854 shallow_nr
+= shallows
->nr
;
858 static int process_shallow(const char *line
, struct object_array
*shallows
)
861 if (skip_prefix(line
, "shallow ", &arg
)) {
862 struct object_id oid
;
863 struct object
*object
;
864 if (get_oid_hex(arg
, &oid
))
865 die("invalid shallow line: %s", line
);
866 object
= parse_object(the_repository
, &oid
);
869 if (object
->type
!= OBJ_COMMIT
)
870 die("invalid shallow object %s", oid_to_hex(&oid
));
871 if (!(object
->flags
& CLIENT_SHALLOW
)) {
872 object
->flags
|= CLIENT_SHALLOW
;
873 add_object_array(object
, NULL
, shallows
);
881 static int process_deepen(const char *line
, int *depth
)
884 if (skip_prefix(line
, "deepen ", &arg
)) {
886 *depth
= (int)strtol(arg
, &end
, 0);
887 if (!end
|| *end
|| *depth
<= 0)
888 die("Invalid deepen: %s", line
);
895 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
898 if (skip_prefix(line
, "deepen-since ", &arg
)) {
900 *deepen_since
= parse_timestamp(arg
, &end
, 0);
901 if (!end
|| *end
|| !deepen_since
||
902 /* revisions.c's max_age -1 is special */
904 die("Invalid deepen-since: %s", line
);
905 *deepen_rev_list
= 1;
911 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
914 if (skip_prefix(line
, "deepen-not ", &arg
)) {
916 struct object_id oid
;
917 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
918 die("git upload-pack: ambiguous deepen-not: %s", line
);
919 string_list_append(deepen_not
, ref
);
921 *deepen_rev_list
= 1;
927 static void receive_needs(struct upload_pack_data
*data
,
928 struct packet_reader
*reader
)
935 const char *features
;
936 struct object_id oid_buf
;
939 reset_timeout(data
->timeout
);
940 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
943 if (process_shallow(reader
->line
, &data
->shallows
))
945 if (process_deepen(reader
->line
, &data
->depth
))
947 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
949 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
952 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
953 if (!data
->filter_capability_requested
)
954 die("git upload-pack: filtering capability not negotiated");
955 list_objects_filter_die_if_populated(&data
->filter_options
);
956 parse_list_objects_filter(&data
->filter_options
, arg
);
960 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
961 parse_oid_hex(arg
, &oid_buf
, &features
))
962 die("git upload-pack: protocol error, "
963 "expected to get object ID, not '%s'", reader
->line
);
965 if (parse_feature_request(features
, "deepen-relative"))
966 data
->deepen_relative
= 1;
967 if (parse_feature_request(features
, "multi_ack_detailed"))
968 data
->multi_ack
= MULTI_ACK_DETAILED
;
969 else if (parse_feature_request(features
, "multi_ack"))
970 data
->multi_ack
= MULTI_ACK
;
971 if (parse_feature_request(features
, "no-done"))
973 if (parse_feature_request(features
, "thin-pack"))
974 data
->use_thin_pack
= 1;
975 if (parse_feature_request(features
, "ofs-delta"))
976 data
->use_ofs_delta
= 1;
977 if (parse_feature_request(features
, "side-band-64k"))
978 data
->use_sideband
= LARGE_PACKET_MAX
;
979 else if (parse_feature_request(features
, "side-band"))
980 data
->use_sideband
= DEFAULT_PACKET_MAX
;
981 if (parse_feature_request(features
, "no-progress"))
982 data
->no_progress
= 1;
983 if (parse_feature_request(features
, "include-tag"))
984 data
->use_include_tag
= 1;
985 if (data
->allow_filter
&&
986 parse_feature_request(features
, "filter"))
987 data
->filter_capability_requested
= 1;
989 o
= parse_object(the_repository
, &oid_buf
);
991 packet_writer_error(&data
->writer
,
992 "upload-pack: not our ref %s",
993 oid_to_hex(&oid_buf
));
994 die("git upload-pack: not our ref %s",
995 oid_to_hex(&oid_buf
));
997 if (!(o
->flags
& WANTED
)) {
999 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1002 add_object_array(o
, NULL
, &data
->want_obj
);
1007 * We have sent all our refs already, and the other end
1008 * should have chosen out of them. When we are operating
1009 * in the stateless RPC mode, however, their choice may
1010 * have been based on the set of older refs advertised
1011 * by another process that handled the initial request.
1014 check_non_tip(data
);
1016 if (!data
->use_sideband
&& data
->daemon_mode
)
1017 data
->no_progress
= 1;
1019 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1022 if (send_shallow_list(&data
->writer
,
1024 data
->deepen_rev_list
,
1027 data
->deepen_relative
,
1033 /* return non-zero if the ref is hidden, otherwise 0 */
1034 static int mark_our_ref(const char *refname
, const char *refname_full
,
1035 const struct object_id
*oid
)
1037 struct object
*o
= lookup_unknown_object(oid
);
1039 if (ref_is_hidden(refname
, refname_full
)) {
1040 o
->flags
|= HIDDEN_REF
;
1043 o
->flags
|= OUR_REF
;
1047 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1048 int flag
, void *cb_data
)
1050 const char *refname
= strip_namespace(refname_full
);
1052 mark_our_ref(refname
, refname_full
, oid
);
1056 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1058 struct string_list_item
*item
;
1062 for_each_string_list_item(item
, symref
)
1063 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1066 static int send_ref(const char *refname
, const struct object_id
*oid
,
1067 int flag
, void *cb_data
)
1069 static const char *capabilities
= "multi_ack thin-pack side-band"
1070 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1071 " deepen-relative no-progress include-tag multi_ack_detailed";
1072 const char *refname_nons
= strip_namespace(refname
);
1073 struct object_id peeled
;
1074 struct upload_pack_data
*data
= cb_data
;
1076 if (mark_our_ref(refname_nons
, refname
, oid
))
1080 struct strbuf symref_info
= STRBUF_INIT
;
1082 format_symref_info(&symref_info
, &data
->symref
);
1083 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1084 oid_to_hex(oid
), refname_nons
,
1086 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
1087 " allow-tip-sha1-in-want" : "",
1088 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1089 " allow-reachable-sha1-in-want" : "",
1090 data
->stateless_rpc
? " no-done" : "",
1092 data
->allow_filter
? " filter" : "",
1093 git_user_agent_sanitized());
1094 strbuf_release(&symref_info
);
1096 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1098 capabilities
= NULL
;
1099 if (!peel_ref(refname
, &peeled
))
1100 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1104 static int find_symref(const char *refname
, const struct object_id
*oid
,
1105 int flag
, void *cb_data
)
1107 const char *symref_target
;
1108 struct string_list_item
*item
;
1110 if ((flag
& REF_ISSYMREF
) == 0)
1112 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1113 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1114 die("'%s' is a symref but it is not?", refname
);
1115 item
= string_list_append(cb_data
, strip_namespace(refname
));
1116 item
->util
= xstrdup(strip_namespace(symref_target
));
1120 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1122 struct upload_pack_data
*data
= cb_data
;
1124 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1125 if (git_config_bool(var
, value
))
1126 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1128 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1129 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1130 if (git_config_bool(var
, value
))
1131 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1133 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1134 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1135 if (git_config_bool(var
, value
))
1136 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1138 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1139 } else if (!strcmp("uploadpack.keepalive", var
)) {
1140 data
->keepalive
= git_config_int(var
, value
);
1141 if (!data
->keepalive
)
1142 data
->keepalive
= -1;
1143 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1144 data
->allow_filter
= git_config_bool(var
, value
);
1145 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1146 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1147 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1148 data
->allow_sideband_all
= git_config_bool(var
, value
);
1149 } else if (!strcmp("core.precomposeunicode", var
)) {
1150 precomposed_unicode
= git_config_bool(var
, value
);
1153 if (current_config_scope() != CONFIG_SCOPE_LOCAL
&&
1154 current_config_scope() != CONFIG_SCOPE_WORKTREE
) {
1155 if (!strcmp("uploadpack.packobjectshook", var
))
1156 return git_config_string(&pack_objects_hook
, var
, value
);
1159 return parse_hide_refs_config(var
, value
, "uploadpack");
1162 void upload_pack(struct upload_pack_options
*options
)
1164 struct packet_reader reader
;
1165 struct upload_pack_data data
;
1167 upload_pack_data_init(&data
);
1169 git_config(upload_pack_config
, &data
);
1171 data
.stateless_rpc
= options
->stateless_rpc
;
1172 data
.daemon_mode
= options
->daemon_mode
;
1173 data
.timeout
= options
->timeout
;
1175 head_ref_namespaced(find_symref
, &data
.symref
);
1177 if (options
->advertise_refs
|| !data
.stateless_rpc
) {
1178 reset_timeout(data
.timeout
);
1179 head_ref_namespaced(send_ref
, &data
);
1180 for_each_namespaced_ref(send_ref
, &data
);
1181 advertise_shallow_grafts(1);
1184 head_ref_namespaced(check_ref
, NULL
);
1185 for_each_namespaced_ref(check_ref
, NULL
);
1188 if (!options
->advertise_refs
) {
1189 packet_reader_init(&reader
, 0, NULL
, 0,
1190 PACKET_READ_CHOMP_NEWLINE
|
1191 PACKET_READ_DIE_ON_ERR_PACKET
);
1193 receive_needs(&data
, &reader
);
1194 if (data
.want_obj
.nr
) {
1195 get_common_commits(&data
, &reader
);
1196 create_pack_file(&data
);
1200 upload_pack_data_clear(&data
);
1203 static int parse_want(struct packet_writer
*writer
, const char *line
,
1204 struct object_array
*want_obj
)
1207 if (skip_prefix(line
, "want ", &arg
)) {
1208 struct object_id oid
;
1211 if (get_oid_hex(arg
, &oid
))
1212 die("git upload-pack: protocol error, "
1213 "expected to get oid, not '%s'", line
);
1215 o
= parse_object(the_repository
, &oid
);
1217 packet_writer_error(writer
,
1218 "upload-pack: not our ref %s",
1220 die("git upload-pack: not our ref %s",
1224 if (!(o
->flags
& WANTED
)) {
1226 add_object_array(o
, NULL
, want_obj
);
1235 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1236 struct string_list
*wanted_refs
,
1237 struct object_array
*want_obj
)
1240 if (skip_prefix(line
, "want-ref ", &arg
)) {
1241 struct object_id oid
;
1242 struct string_list_item
*item
;
1245 if (read_ref(arg
, &oid
)) {
1246 packet_writer_error(writer
, "unknown ref %s", arg
);
1247 die("unknown ref %s", arg
);
1250 item
= string_list_append(wanted_refs
, arg
);
1251 item
->util
= oiddup(&oid
);
1253 o
= parse_object_or_die(&oid
, arg
);
1254 if (!(o
->flags
& WANTED
)) {
1256 add_object_array(o
, NULL
, want_obj
);
1265 static int parse_have(const char *line
, struct oid_array
*haves
)
1268 if (skip_prefix(line
, "have ", &arg
)) {
1269 struct object_id oid
;
1271 if (get_oid_hex(arg
, &oid
))
1272 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1273 oid_array_append(haves
, &oid
);
1280 static void process_args(struct packet_reader
*request
,
1281 struct upload_pack_data
*data
)
1283 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1284 const char *arg
= request
->line
;
1288 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1290 if (data
->allow_ref_in_want
&&
1291 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1294 /* process have line */
1295 if (parse_have(arg
, &data
->haves
))
1298 /* process args like thin-pack */
1299 if (!strcmp(arg
, "thin-pack")) {
1300 data
->use_thin_pack
= 1;
1303 if (!strcmp(arg
, "ofs-delta")) {
1304 data
->use_ofs_delta
= 1;
1307 if (!strcmp(arg
, "no-progress")) {
1308 data
->no_progress
= 1;
1311 if (!strcmp(arg
, "include-tag")) {
1312 data
->use_include_tag
= 1;
1315 if (!strcmp(arg
, "done")) {
1320 /* Shallow related arguments */
1321 if (process_shallow(arg
, &data
->shallows
))
1323 if (process_deepen(arg
, &data
->depth
))
1325 if (process_deepen_since(arg
, &data
->deepen_since
,
1326 &data
->deepen_rev_list
))
1328 if (process_deepen_not(arg
, &data
->deepen_not
,
1329 &data
->deepen_rev_list
))
1331 if (!strcmp(arg
, "deepen-relative")) {
1332 data
->deepen_relative
= 1;
1336 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1337 list_objects_filter_die_if_populated(&data
->filter_options
);
1338 parse_list_objects_filter(&data
->filter_options
, p
);
1342 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1343 data
->allow_sideband_all
) &&
1344 !strcmp(arg
, "sideband-all")) {
1345 data
->writer
.use_sideband
= 1;
1349 /* ignore unknown lines maybe? */
1350 die("unexpected line: '%s'", arg
);
1353 if (request
->status
!= PACKET_READ_FLUSH
)
1354 die(_("expected flush after fetch arguments"));
1357 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
,
1358 struct object_array
*have_obj
)
1363 for (i
= 0; i
< haves
->nr
; i
++) {
1364 const struct object_id
*oid
= &haves
->oid
[i
];
1366 int we_knew_they_have
= 0;
1368 if (!has_object_file(oid
))
1371 oid_array_append(common
, oid
);
1373 o
= parse_object(the_repository
, oid
);
1375 die("oops (%s)", oid_to_hex(oid
));
1376 if (o
->type
== OBJ_COMMIT
) {
1377 struct commit_list
*parents
;
1378 struct commit
*commit
= (struct commit
*)o
;
1379 if (o
->flags
& THEY_HAVE
)
1380 we_knew_they_have
= 1;
1382 o
->flags
|= THEY_HAVE
;
1383 if (!oldest_have
|| (commit
->date
< oldest_have
))
1384 oldest_have
= commit
->date
;
1385 for (parents
= commit
->parents
;
1387 parents
= parents
->next
)
1388 parents
->item
->object
.flags
|= THEY_HAVE
;
1390 if (!we_knew_they_have
)
1391 add_object_array(o
, NULL
, have_obj
);
1397 static int send_acks(struct packet_writer
*writer
, struct oid_array
*acks
,
1398 const struct object_array
*have_obj
,
1399 struct object_array
*want_obj
)
1403 packet_writer_write(writer
, "acknowledgments\n");
1407 packet_writer_write(writer
, "NAK\n");
1409 for (i
= 0; i
< acks
->nr
; i
++) {
1410 packet_writer_write(writer
, "ACK %s\n",
1411 oid_to_hex(&acks
->oid
[i
]));
1414 if (ok_to_give_up(have_obj
, want_obj
)) {
1416 packet_writer_write(writer
, "ready\n");
1423 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1425 struct oid_array common
= OID_ARRAY_INIT
;
1428 process_haves(&data
->haves
, &common
, &data
->have_obj
);
1431 } else if (send_acks(&data
->writer
, &common
,
1432 &data
->have_obj
, &data
->want_obj
)) {
1433 packet_writer_delim(&data
->writer
);
1437 packet_writer_flush(&data
->writer
);
1441 oid_array_clear(&data
->haves
);
1442 oid_array_clear(&common
);
1446 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1448 const struct string_list_item
*item
;
1450 if (!data
->wanted_refs
.nr
)
1453 packet_writer_write(&data
->writer
, "wanted-refs\n");
1455 for_each_string_list_item(item
, &data
->wanted_refs
) {
1456 packet_writer_write(&data
->writer
, "%s %s\n",
1457 oid_to_hex(item
->util
),
1461 packet_writer_delim(&data
->writer
);
1464 static void send_shallow_info(struct upload_pack_data
*data
)
1466 /* No shallow info needs to be sent */
1467 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1468 !is_repository_shallow(the_repository
))
1471 packet_writer_write(&data
->writer
, "shallow-info\n");
1473 if (!send_shallow_list(&data
->writer
, data
->depth
,
1474 data
->deepen_rev_list
,
1475 data
->deepen_since
, &data
->deepen_not
,
1476 data
->deepen_relative
,
1477 &data
->shallows
, &data
->want_obj
) &&
1478 is_repository_shallow(the_repository
))
1479 deepen(&data
->writer
, INFINITE_DEPTH
, data
->deepen_relative
,
1480 &data
->shallows
, &data
->want_obj
);
1486 FETCH_PROCESS_ARGS
= 0,
1492 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1493 struct packet_reader
*request
)
1495 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1496 struct upload_pack_data data
;
1498 clear_object_flags(ALL_FLAGS
);
1500 upload_pack_data_init(&data
);
1501 data
.use_sideband
= LARGE_PACKET_MAX
;
1503 git_config(upload_pack_config
, &data
);
1505 while (state
!= FETCH_DONE
) {
1507 case FETCH_PROCESS_ARGS
:
1508 process_args(request
, &data
);
1510 if (!data
.want_obj
.nr
) {
1512 * Request didn't contain any 'want' lines,
1513 * guess they didn't want anything.
1516 } else if (data
.haves
.nr
) {
1518 * Request had 'have' lines, so lets ACK them.
1520 state
= FETCH_SEND_ACKS
;
1523 * Request had 'want's but no 'have's so we can
1524 * immedietly go to construct and send a pack.
1526 state
= FETCH_SEND_PACK
;
1529 case FETCH_SEND_ACKS
:
1530 if (process_haves_and_send_acks(&data
))
1531 state
= FETCH_SEND_PACK
;
1535 case FETCH_SEND_PACK
:
1536 send_wanted_ref_info(&data
);
1537 send_shallow_info(&data
);
1539 packet_writer_write(&data
.writer
, "packfile\n");
1540 create_pack_file(&data
);
1548 upload_pack_data_clear(&data
);
1552 int upload_pack_advertise(struct repository
*r
,
1553 struct strbuf
*value
)
1556 int allow_filter_value
;
1557 int allow_ref_in_want
;
1558 int allow_sideband_all_value
;
1560 strbuf_addstr(value
, "shallow");
1562 if (!repo_config_get_bool(the_repository
,
1563 "uploadpack.allowfilter",
1564 &allow_filter_value
) &&
1566 strbuf_addstr(value
, " filter");
1568 if (!repo_config_get_bool(the_repository
,
1569 "uploadpack.allowrefinwant",
1570 &allow_ref_in_want
) &&
1572 strbuf_addstr(value
, " ref-in-want");
1574 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1575 (!repo_config_get_bool(the_repository
,
1576 "uploadpack.allowsidebandall",
1577 &allow_sideband_all_value
) &&
1578 allow_sideband_all_value
))
1579 strbuf_addstr(value
, " sideband-all");