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 struct object_array extra_edge_obj
;
57 * Please annotate, and if possible group together, fields used only
58 * for protocol v0 or only for protocol v2.
60 struct upload_pack_data
{
61 struct string_list symref
; /* v0 only */
62 struct object_array want_obj
;
63 struct object_array have_obj
;
64 struct oid_array haves
; /* v2 only */
65 struct string_list wanted_refs
; /* v2 only */
67 struct object_array shallows
;
68 struct string_list deepen_not
;
70 timestamp_t deepen_since
;
76 unsigned int timeout
; /* v0 only */
80 MULTI_ACK_DETAILED
= 2
81 } multi_ack
; /* v0 only */
83 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
86 struct list_objects_filter_options filter_options
;
88 struct packet_writer writer
;
90 const char *pack_objects_hook
;
92 unsigned stateless_rpc
: 1; /* v0 only */
93 unsigned no_done
: 1; /* v0 only */
94 unsigned daemon_mode
: 1; /* v0 only */
95 unsigned filter_capability_requested
: 1; /* v0 only */
97 unsigned use_thin_pack
: 1;
98 unsigned use_ofs_delta
: 1;
99 unsigned no_progress
: 1;
100 unsigned use_include_tag
: 1;
101 unsigned allow_filter
: 1;
103 unsigned done
: 1; /* v2 only */
104 unsigned allow_ref_in_want
: 1; /* v2 only */
105 unsigned allow_sideband_all
: 1; /* v2 only */
108 static void upload_pack_data_init(struct upload_pack_data
*data
)
110 struct string_list symref
= STRING_LIST_INIT_DUP
;
111 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
112 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
113 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
114 struct oid_array haves
= OID_ARRAY_INIT
;
115 struct object_array shallows
= OBJECT_ARRAY_INIT
;
116 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
118 memset(data
, 0, sizeof(*data
));
119 data
->symref
= symref
;
120 data
->wanted_refs
= wanted_refs
;
121 data
->want_obj
= want_obj
;
122 data
->have_obj
= have_obj
;
124 data
->shallows
= shallows
;
125 data
->deepen_not
= deepen_not
;
126 packet_writer_init(&data
->writer
, 1);
131 static void upload_pack_data_clear(struct upload_pack_data
*data
)
133 string_list_clear(&data
->symref
, 1);
134 string_list_clear(&data
->wanted_refs
, 1);
135 object_array_clear(&data
->want_obj
);
136 object_array_clear(&data
->have_obj
);
137 oid_array_clear(&data
->haves
);
138 object_array_clear(&data
->shallows
);
139 string_list_clear(&data
->deepen_not
, 0);
140 list_objects_filter_release(&data
->filter_options
);
142 free((char *)data
->pack_objects_hook
);
145 static void reset_timeout(unsigned int timeout
)
150 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
154 send_sideband(1, fd
, data
, sz
, use_sideband
);
161 /* XXX: are we happy to lose stuff here? */
162 xwrite(fd
, data
, sz
);
165 write_or_die(fd
, data
, sz
);
168 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
171 if (graft
->nr_parent
== -1)
172 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
176 static void create_pack_file(struct upload_pack_data
*pack_data
)
178 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
179 char data
[8193], progress
[128];
180 char abort_msg
[] = "aborting due to possible repository "
181 "corruption on the remote side.";
187 if (!pack_data
->pack_objects_hook
)
188 pack_objects
.git_cmd
= 1;
190 argv_array_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
191 argv_array_push(&pack_objects
.args
, "git");
192 pack_objects
.use_shell
= 1;
195 if (pack_data
->shallow_nr
) {
196 argv_array_push(&pack_objects
.args
, "--shallow-file");
197 argv_array_push(&pack_objects
.args
, "");
199 argv_array_push(&pack_objects
.args
, "pack-objects");
200 argv_array_push(&pack_objects
.args
, "--revs");
201 if (pack_data
->use_thin_pack
)
202 argv_array_push(&pack_objects
.args
, "--thin");
204 argv_array_push(&pack_objects
.args
, "--stdout");
205 if (pack_data
->shallow_nr
)
206 argv_array_push(&pack_objects
.args
, "--shallow");
207 if (!pack_data
->no_progress
)
208 argv_array_push(&pack_objects
.args
, "--progress");
209 if (pack_data
->use_ofs_delta
)
210 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
211 if (pack_data
->use_include_tag
)
212 argv_array_push(&pack_objects
.args
, "--include-tag");
213 if (pack_data
->filter_options
.choice
) {
215 expand_list_objects_filter_spec(&pack_data
->filter_options
);
216 if (pack_objects
.use_shell
) {
217 struct strbuf buf
= STRBUF_INIT
;
218 sq_quote_buf(&buf
, spec
);
219 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
220 strbuf_release(&buf
);
222 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
227 pack_objects
.in
= -1;
228 pack_objects
.out
= -1;
229 pack_objects
.err
= -1;
231 if (start_command(&pack_objects
))
232 die("git upload-pack: unable to fork git-pack-objects");
234 pipe_fd
= xfdopen(pack_objects
.in
, "w");
236 if (pack_data
->shallow_nr
)
237 for_each_commit_graft(write_one_shallow
, pipe_fd
);
239 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
240 fprintf(pipe_fd
, "%s\n",
241 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
242 fprintf(pipe_fd
, "--not\n");
243 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
244 fprintf(pipe_fd
, "%s\n",
245 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
246 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
247 fprintf(pipe_fd
, "%s\n",
248 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
249 fprintf(pipe_fd
, "\n");
253 /* We read from pack_objects.err to capture stderr output for
254 * progress bar, and pack_objects.out to capture the pack data.
258 struct pollfd pfd
[2];
259 int pe
, pu
, pollsize
, polltimeout
;
262 reset_timeout(pack_data
->timeout
);
267 if (0 <= pack_objects
.out
) {
268 pfd
[pollsize
].fd
= pack_objects
.out
;
269 pfd
[pollsize
].events
= POLLIN
;
273 if (0 <= pack_objects
.err
) {
274 pfd
[pollsize
].fd
= pack_objects
.err
;
275 pfd
[pollsize
].events
= POLLIN
;
283 polltimeout
= pack_data
->keepalive
< 0
285 : 1000 * pack_data
->keepalive
;
287 ret
= poll(pfd
, pollsize
, polltimeout
);
290 if (errno
!= EINTR
) {
291 error_errno("poll failed, resuming");
296 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
297 /* Status ready; we ship that in the side-band
298 * or dump to the standard error.
300 sz
= xread(pack_objects
.err
, progress
,
303 send_client_data(2, progress
, sz
,
304 pack_data
->use_sideband
);
306 close(pack_objects
.err
);
307 pack_objects
.err
= -1;
311 /* give priority to status messages */
314 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
315 /* Data ready; we keep the last byte to ourselves
316 * in case we detect broken rev-list, so that we
317 * can leave the stream corrupted. This is
318 * unfortunate -- unpack-objects would happily
319 * accept a valid packdata with trailing garbage,
320 * so appending garbage after we pass all the
321 * pack data is not good enough to signal
322 * breakage to downstream.
330 sz
= xread(pack_objects
.out
, cp
,
331 sizeof(data
) - outsz
);
335 close(pack_objects
.out
);
336 pack_objects
.out
= -1;
342 buffered
= data
[sz
-1] & 0xFF;
347 send_client_data(1, data
, sz
,
348 pack_data
->use_sideband
);
352 * We hit the keepalive timeout without saying anything; send
353 * an empty message on the data sideband just to let the other
354 * side know we're still working on it, but don't have any data
357 * If we don't have a sideband channel, there's no room in the
358 * protocol to say anything, so those clients are just out of
361 if (!ret
&& pack_data
->use_sideband
) {
362 static const char buf
[] = "0005\1";
363 write_or_die(1, buf
, 5);
367 if (finish_command(&pack_objects
)) {
368 error("git upload-pack: git-pack-objects died with error.");
375 send_client_data(1, data
, 1,
376 pack_data
->use_sideband
);
377 fprintf(stderr
, "flushed.\n");
379 if (pack_data
->use_sideband
)
384 send_client_data(3, abort_msg
, sizeof(abort_msg
),
385 pack_data
->use_sideband
);
386 die("git upload-pack: %s", abort_msg
);
389 static int got_oid(const char *hex
, struct object_id
*oid
,
390 struct object_array
*have_obj
)
393 int we_knew_they_have
= 0;
395 if (get_oid_hex(hex
, oid
))
396 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
397 if (!has_object_file(oid
))
400 o
= parse_object(the_repository
, oid
);
402 die("oops (%s)", oid_to_hex(oid
));
403 if (o
->type
== OBJ_COMMIT
) {
404 struct commit_list
*parents
;
405 struct commit
*commit
= (struct commit
*)o
;
406 if (o
->flags
& THEY_HAVE
)
407 we_knew_they_have
= 1;
409 o
->flags
|= THEY_HAVE
;
410 if (!oldest_have
|| (commit
->date
< oldest_have
))
411 oldest_have
= commit
->date
;
412 for (parents
= commit
->parents
;
414 parents
= parents
->next
)
415 parents
->item
->object
.flags
|= THEY_HAVE
;
417 if (!we_knew_they_have
) {
418 add_object_array(o
, NULL
, have_obj
);
424 static int ok_to_give_up(const struct object_array
*have_obj
,
425 struct object_array
*want_obj
)
427 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
432 return can_all_from_reach_with_flag(want_obj
, THEY_HAVE
,
433 COMMON_KNOWN
, oldest_have
,
437 static int get_common_commits(struct upload_pack_data
*data
,
438 struct packet_reader
*reader
)
440 struct object_id oid
;
441 char last_hex
[GIT_MAX_HEXSZ
+ 1];
446 save_commit_buffer
= 0;
451 reset_timeout(data
->timeout
);
453 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
454 if (data
->multi_ack
== MULTI_ACK_DETAILED
457 && ok_to_give_up(&data
->have_obj
, &data
->want_obj
)) {
459 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
461 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
462 packet_write_fmt(1, "NAK\n");
464 if (data
->no_done
&& sent_ready
) {
465 packet_write_fmt(1, "ACK %s\n", last_hex
);
468 if (data
->stateless_rpc
)
474 if (skip_prefix(reader
->line
, "have ", &arg
)) {
475 switch (got_oid(arg
, &oid
, &data
->have_obj
)) {
476 case -1: /* they have what we do not */
479 && ok_to_give_up(&data
->have_obj
, &data
->want_obj
)) {
480 const char *hex
= oid_to_hex(&oid
);
481 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
483 packet_write_fmt(1, "ACK %s ready\n", hex
);
485 packet_write_fmt(1, "ACK %s continue\n", hex
);
490 oid_to_hex_r(last_hex
, &oid
);
491 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
492 packet_write_fmt(1, "ACK %s common\n", last_hex
);
493 else if (data
->multi_ack
)
494 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
495 else if (data
->have_obj
.nr
== 1)
496 packet_write_fmt(1, "ACK %s\n", last_hex
);
501 if (!strcmp(reader
->line
, "done")) {
502 if (data
->have_obj
.nr
> 0) {
504 packet_write_fmt(1, "ACK %s\n", last_hex
);
507 packet_write_fmt(1, "NAK\n");
510 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
514 static int is_our_ref(struct object
*o
)
516 int allow_hidden_ref
= (allow_unadvertised_object_request
&
517 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
518 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
522 * on successful case, it's up to the caller to close cmd->out
524 static int do_reachable_revlist(struct child_process
*cmd
,
525 struct object_array
*src
,
526 struct object_array
*reachable
)
528 static const char *argv
[] = {
529 "rev-list", "--stdin", NULL
,
532 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
534 const unsigned hexsz
= the_hash_algo
->hexsz
;
543 * If the next rev-list --stdin encounters an unknown commit,
544 * it terminates, which will cause SIGPIPE in the write loop
547 sigchain_push(SIGPIPE
, SIG_IGN
);
549 if (start_command(cmd
))
553 namebuf
[hexsz
+ 1] = '\n';
554 for (i
= get_max_object_index(); 0 < i
; ) {
555 o
= get_indexed_object(--i
);
558 if (reachable
&& o
->type
== OBJ_COMMIT
)
559 o
->flags
&= ~TMP_MARK
;
562 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
563 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
566 namebuf
[hexsz
] = '\n';
567 for (i
= 0; i
< src
->nr
; i
++) {
568 o
= src
->objects
[i
].item
;
571 add_object_array(o
, NULL
, reachable
);
574 if (reachable
&& o
->type
== OBJ_COMMIT
)
575 o
->flags
|= TMP_MARK
;
576 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
577 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
582 sigchain_pop(SIGPIPE
);
587 sigchain_pop(SIGPIPE
);
596 static int get_reachable_list(struct object_array
*src
,
597 struct object_array
*reachable
)
599 struct child_process cmd
= CHILD_PROCESS_INIT
;
602 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
603 const unsigned hexsz
= the_hash_algo
->hexsz
;
605 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
608 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
609 struct object_id oid
;
612 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
615 o
= lookup_object(the_repository
, &oid
);
616 if (o
&& o
->type
== OBJ_COMMIT
) {
617 o
->flags
&= ~TMP_MARK
;
620 for (i
= get_max_object_index(); 0 < i
; i
--) {
621 o
= get_indexed_object(i
- 1);
622 if (o
&& o
->type
== OBJ_COMMIT
&&
623 (o
->flags
& TMP_MARK
)) {
624 add_object_array(o
, NULL
, reachable
);
625 o
->flags
&= ~TMP_MARK
;
630 if (finish_command(&cmd
))
636 static int has_unreachable(struct object_array
*src
)
638 struct child_process cmd
= CHILD_PROCESS_INIT
;
642 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
646 * The commits out of the rev-list are not ancestors of
649 i
= read_in_full(cmd
.out
, buf
, 1);
656 * rev-list may have died by encountering a bad commit
657 * in the history, in which case we do want to bail out
658 * even when it showed no commit.
660 if (finish_command(&cmd
))
663 /* All the non-tip ones are ancestors of what we advertised */
667 sigchain_pop(SIGPIPE
);
673 static void check_non_tip(struct upload_pack_data
*data
)
678 * In the normal in-process case without
679 * uploadpack.allowReachableSHA1InWant,
680 * non-tip requests can never happen.
682 if (!data
->stateless_rpc
683 && !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
685 if (!has_unreachable(&data
->want_obj
))
686 /* All the non-tip ones are ancestors of what we advertised */
690 /* Pick one of them (we know there at least is one) */
691 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
692 struct object
*o
= data
->want_obj
.objects
[i
].item
;
693 if (!is_our_ref(o
)) {
694 packet_writer_error(&data
->writer
,
695 "upload-pack: not our ref %s",
696 oid_to_hex(&o
->oid
));
697 die("git upload-pack: not our ref %s",
698 oid_to_hex(&o
->oid
));
703 static void send_shallow(struct upload_pack_data
*data
,
704 struct commit_list
*result
)
707 struct object
*object
= &result
->item
->object
;
708 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
709 packet_writer_write(&data
->writer
, "shallow %s",
710 oid_to_hex(&object
->oid
));
711 register_shallow(the_repository
, &object
->oid
);
714 result
= result
->next
;
718 static void send_unshallow(struct upload_pack_data
*data
)
722 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
723 struct object
*object
= data
->shallows
.objects
[i
].item
;
724 if (object
->flags
& NOT_SHALLOW
) {
725 struct commit_list
*parents
;
726 packet_writer_write(&data
->writer
, "unshallow %s",
727 oid_to_hex(&object
->oid
));
728 object
->flags
&= ~CLIENT_SHALLOW
;
730 * We want to _register_ "object" as shallow, but we
731 * also need to traverse object's parents to deepen a
732 * shallow clone. Unregister it for now so we can
733 * parse and add the parents to the want list, then
736 unregister_shallow(&object
->oid
);
738 parse_commit_or_die((struct commit
*)object
);
739 parents
= ((struct commit
*)object
)->parents
;
741 add_object_array(&parents
->item
->object
,
742 NULL
, &data
->want_obj
);
743 parents
= parents
->next
;
745 add_object_array(object
, NULL
, &extra_edge_obj
);
747 /* make sure commit traversal conforms to client */
748 register_shallow(the_repository
, &object
->oid
);
752 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
753 int flag
, void *cb_data
);
754 static void deepen(struct upload_pack_data
*data
, int depth
)
756 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
759 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
760 struct object
*object
= data
->shallows
.objects
[i
].item
;
761 object
->flags
|= NOT_SHALLOW
;
763 } else if (data
->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(&data
->shallows
, &reachable_shallows
);
775 result
= get_shallow_commits(&reachable_shallows
,
777 SHALLOW
, NOT_SHALLOW
);
778 send_shallow(data
, result
);
779 free_commit_list(result
);
780 object_array_clear(&reachable_shallows
);
782 struct commit_list
*result
;
784 result
= get_shallow_commits(&data
->want_obj
, depth
,
785 SHALLOW
, NOT_SHALLOW
);
786 send_shallow(data
, result
);
787 free_commit_list(result
);
790 send_unshallow(data
);
793 static void deepen_by_rev_list(struct upload_pack_data
*data
,
797 struct commit_list
*result
;
799 disable_commit_graph(the_repository
);
800 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
801 send_shallow(data
, result
);
802 free_commit_list(result
);
803 send_unshallow(data
);
806 /* Returns 1 if a shallow list is sent or 0 otherwise */
807 static int send_shallow_list(struct upload_pack_data
*data
)
811 if (data
->depth
> 0 && data
->deepen_rev_list
)
812 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
813 if (data
->depth
> 0) {
814 deepen(data
, data
->depth
);
816 } else if (data
->deepen_rev_list
) {
817 struct argv_array av
= ARGV_ARRAY_INIT
;
820 argv_array_push(&av
, "rev-list");
821 if (data
->deepen_since
)
822 argv_array_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
823 if (data
->deepen_not
.nr
) {
824 argv_array_push(&av
, "--not");
825 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
826 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
827 argv_array_push(&av
, s
->string
);
829 argv_array_push(&av
, "--not");
831 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
832 struct object
*o
= data
->want_obj
.objects
[i
].item
;
833 argv_array_push(&av
, oid_to_hex(&o
->oid
));
835 deepen_by_rev_list(data
, av
.argc
, av
.argv
);
836 argv_array_clear(&av
);
839 if (data
->shallows
.nr
> 0) {
841 for (i
= 0; i
< data
->shallows
.nr
; i
++)
842 register_shallow(the_repository
,
843 &data
->shallows
.objects
[i
].item
->oid
);
847 data
->shallow_nr
+= data
->shallows
.nr
;
851 static int process_shallow(const char *line
, struct object_array
*shallows
)
854 if (skip_prefix(line
, "shallow ", &arg
)) {
855 struct object_id oid
;
856 struct object
*object
;
857 if (get_oid_hex(arg
, &oid
))
858 die("invalid shallow line: %s", line
);
859 object
= parse_object(the_repository
, &oid
);
862 if (object
->type
!= OBJ_COMMIT
)
863 die("invalid shallow object %s", oid_to_hex(&oid
));
864 if (!(object
->flags
& CLIENT_SHALLOW
)) {
865 object
->flags
|= CLIENT_SHALLOW
;
866 add_object_array(object
, NULL
, shallows
);
874 static int process_deepen(const char *line
, int *depth
)
877 if (skip_prefix(line
, "deepen ", &arg
)) {
879 *depth
= (int)strtol(arg
, &end
, 0);
880 if (!end
|| *end
|| *depth
<= 0)
881 die("Invalid deepen: %s", line
);
888 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
891 if (skip_prefix(line
, "deepen-since ", &arg
)) {
893 *deepen_since
= parse_timestamp(arg
, &end
, 0);
894 if (!end
|| *end
|| !deepen_since
||
895 /* revisions.c's max_age -1 is special */
897 die("Invalid deepen-since: %s", line
);
898 *deepen_rev_list
= 1;
904 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
907 if (skip_prefix(line
, "deepen-not ", &arg
)) {
909 struct object_id oid
;
910 if (expand_ref(the_repository
, arg
, strlen(arg
), &oid
, &ref
) != 1)
911 die("git upload-pack: ambiguous deepen-not: %s", line
);
912 string_list_append(deepen_not
, ref
);
914 *deepen_rev_list
= 1;
920 static void receive_needs(struct upload_pack_data
*data
,
921 struct packet_reader
*reader
)
925 data
->shallow_nr
= 0;
928 const char *features
;
929 struct object_id oid_buf
;
932 reset_timeout(data
->timeout
);
933 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
936 if (process_shallow(reader
->line
, &data
->shallows
))
938 if (process_deepen(reader
->line
, &data
->depth
))
940 if (process_deepen_since(reader
->line
, &data
->deepen_since
, &data
->deepen_rev_list
))
942 if (process_deepen_not(reader
->line
, &data
->deepen_not
, &data
->deepen_rev_list
))
945 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
946 if (!data
->filter_capability_requested
)
947 die("git upload-pack: filtering capability not negotiated");
948 list_objects_filter_die_if_populated(&data
->filter_options
);
949 parse_list_objects_filter(&data
->filter_options
, arg
);
953 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
954 parse_oid_hex(arg
, &oid_buf
, &features
))
955 die("git upload-pack: protocol error, "
956 "expected to get object ID, not '%s'", reader
->line
);
958 if (parse_feature_request(features
, "deepen-relative"))
959 data
->deepen_relative
= 1;
960 if (parse_feature_request(features
, "multi_ack_detailed"))
961 data
->multi_ack
= MULTI_ACK_DETAILED
;
962 else if (parse_feature_request(features
, "multi_ack"))
963 data
->multi_ack
= MULTI_ACK
;
964 if (parse_feature_request(features
, "no-done"))
966 if (parse_feature_request(features
, "thin-pack"))
967 data
->use_thin_pack
= 1;
968 if (parse_feature_request(features
, "ofs-delta"))
969 data
->use_ofs_delta
= 1;
970 if (parse_feature_request(features
, "side-band-64k"))
971 data
->use_sideband
= LARGE_PACKET_MAX
;
972 else if (parse_feature_request(features
, "side-band"))
973 data
->use_sideband
= DEFAULT_PACKET_MAX
;
974 if (parse_feature_request(features
, "no-progress"))
975 data
->no_progress
= 1;
976 if (parse_feature_request(features
, "include-tag"))
977 data
->use_include_tag
= 1;
978 if (data
->allow_filter
&&
979 parse_feature_request(features
, "filter"))
980 data
->filter_capability_requested
= 1;
982 o
= parse_object(the_repository
, &oid_buf
);
984 packet_writer_error(&data
->writer
,
985 "upload-pack: not our ref %s",
986 oid_to_hex(&oid_buf
));
987 die("git upload-pack: not our ref %s",
988 oid_to_hex(&oid_buf
));
990 if (!(o
->flags
& WANTED
)) {
992 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
995 add_object_array(o
, NULL
, &data
->want_obj
);
1000 * We have sent all our refs already, and the other end
1001 * should have chosen out of them. When we are operating
1002 * in the stateless RPC mode, however, their choice may
1003 * have been based on the set of older refs advertised
1004 * by another process that handled the initial request.
1007 check_non_tip(data
);
1009 if (!data
->use_sideband
&& data
->daemon_mode
)
1010 data
->no_progress
= 1;
1012 if (data
->depth
== 0 && !data
->deepen_rev_list
&& data
->shallows
.nr
== 0)
1015 if (send_shallow_list(data
))
1019 /* return non-zero if the ref is hidden, otherwise 0 */
1020 static int mark_our_ref(const char *refname
, const char *refname_full
,
1021 const struct object_id
*oid
)
1023 struct object
*o
= lookup_unknown_object(oid
);
1025 if (ref_is_hidden(refname
, refname_full
)) {
1026 o
->flags
|= HIDDEN_REF
;
1029 o
->flags
|= OUR_REF
;
1033 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1034 int flag
, void *cb_data
)
1036 const char *refname
= strip_namespace(refname_full
);
1038 mark_our_ref(refname
, refname_full
, oid
);
1042 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1044 struct string_list_item
*item
;
1048 for_each_string_list_item(item
, symref
)
1049 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1052 static int send_ref(const char *refname
, const struct object_id
*oid
,
1053 int flag
, void *cb_data
)
1055 static const char *capabilities
= "multi_ack thin-pack side-band"
1056 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1057 " deepen-relative no-progress include-tag multi_ack_detailed";
1058 const char *refname_nons
= strip_namespace(refname
);
1059 struct object_id peeled
;
1060 struct upload_pack_data
*data
= cb_data
;
1062 if (mark_our_ref(refname_nons
, refname
, oid
))
1066 struct strbuf symref_info
= STRBUF_INIT
;
1068 format_symref_info(&symref_info
, &data
->symref
);
1069 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1070 oid_to_hex(oid
), refname_nons
,
1072 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
1073 " allow-tip-sha1-in-want" : "",
1074 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1075 " allow-reachable-sha1-in-want" : "",
1076 data
->stateless_rpc
? " no-done" : "",
1078 data
->allow_filter
? " filter" : "",
1079 git_user_agent_sanitized());
1080 strbuf_release(&symref_info
);
1082 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1084 capabilities
= NULL
;
1085 if (!peel_ref(refname
, &peeled
))
1086 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1090 static int find_symref(const char *refname
, const struct object_id
*oid
,
1091 int flag
, void *cb_data
)
1093 const char *symref_target
;
1094 struct string_list_item
*item
;
1096 if ((flag
& REF_ISSYMREF
) == 0)
1098 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1099 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1100 die("'%s' is a symref but it is not?", refname
);
1101 item
= string_list_append(cb_data
, strip_namespace(refname
));
1102 item
->util
= xstrdup(strip_namespace(symref_target
));
1106 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1108 struct upload_pack_data
*data
= cb_data
;
1110 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1111 if (git_config_bool(var
, value
))
1112 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1114 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1115 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1116 if (git_config_bool(var
, value
))
1117 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1119 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1120 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1121 if (git_config_bool(var
, value
))
1122 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1124 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1125 } else if (!strcmp("uploadpack.keepalive", var
)) {
1126 data
->keepalive
= git_config_int(var
, value
);
1127 if (!data
->keepalive
)
1128 data
->keepalive
= -1;
1129 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1130 data
->allow_filter
= git_config_bool(var
, value
);
1131 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1132 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1133 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1134 data
->allow_sideband_all
= git_config_bool(var
, value
);
1135 } else if (!strcmp("core.precomposeunicode", var
)) {
1136 precomposed_unicode
= git_config_bool(var
, value
);
1139 if (current_config_scope() != CONFIG_SCOPE_LOCAL
&&
1140 current_config_scope() != CONFIG_SCOPE_WORKTREE
) {
1141 if (!strcmp("uploadpack.packobjectshook", var
))
1142 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1145 return parse_hide_refs_config(var
, value
, "uploadpack");
1148 void upload_pack(struct upload_pack_options
*options
)
1150 struct packet_reader reader
;
1151 struct upload_pack_data data
;
1153 upload_pack_data_init(&data
);
1155 git_config(upload_pack_config
, &data
);
1157 data
.stateless_rpc
= options
->stateless_rpc
;
1158 data
.daemon_mode
= options
->daemon_mode
;
1159 data
.timeout
= options
->timeout
;
1161 head_ref_namespaced(find_symref
, &data
.symref
);
1163 if (options
->advertise_refs
|| !data
.stateless_rpc
) {
1164 reset_timeout(data
.timeout
);
1165 head_ref_namespaced(send_ref
, &data
);
1166 for_each_namespaced_ref(send_ref
, &data
);
1167 advertise_shallow_grafts(1);
1170 head_ref_namespaced(check_ref
, NULL
);
1171 for_each_namespaced_ref(check_ref
, NULL
);
1174 if (!options
->advertise_refs
) {
1175 packet_reader_init(&reader
, 0, NULL
, 0,
1176 PACKET_READ_CHOMP_NEWLINE
|
1177 PACKET_READ_DIE_ON_ERR_PACKET
);
1179 receive_needs(&data
, &reader
);
1180 if (data
.want_obj
.nr
) {
1181 get_common_commits(&data
, &reader
);
1182 create_pack_file(&data
);
1186 upload_pack_data_clear(&data
);
1189 static int parse_want(struct packet_writer
*writer
, const char *line
,
1190 struct object_array
*want_obj
)
1193 if (skip_prefix(line
, "want ", &arg
)) {
1194 struct object_id oid
;
1197 if (get_oid_hex(arg
, &oid
))
1198 die("git upload-pack: protocol error, "
1199 "expected to get oid, not '%s'", line
);
1201 o
= parse_object(the_repository
, &oid
);
1203 packet_writer_error(writer
,
1204 "upload-pack: not our ref %s",
1206 die("git upload-pack: not our ref %s",
1210 if (!(o
->flags
& WANTED
)) {
1212 add_object_array(o
, NULL
, want_obj
);
1221 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1222 struct string_list
*wanted_refs
,
1223 struct object_array
*want_obj
)
1226 if (skip_prefix(line
, "want-ref ", &arg
)) {
1227 struct object_id oid
;
1228 struct string_list_item
*item
;
1231 if (read_ref(arg
, &oid
)) {
1232 packet_writer_error(writer
, "unknown ref %s", arg
);
1233 die("unknown ref %s", arg
);
1236 item
= string_list_append(wanted_refs
, arg
);
1237 item
->util
= oiddup(&oid
);
1239 o
= parse_object_or_die(&oid
, arg
);
1240 if (!(o
->flags
& WANTED
)) {
1242 add_object_array(o
, NULL
, want_obj
);
1251 static int parse_have(const char *line
, struct oid_array
*haves
)
1254 if (skip_prefix(line
, "have ", &arg
)) {
1255 struct object_id oid
;
1257 if (get_oid_hex(arg
, &oid
))
1258 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1259 oid_array_append(haves
, &oid
);
1266 static void process_args(struct packet_reader
*request
,
1267 struct upload_pack_data
*data
)
1269 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1270 const char *arg
= request
->line
;
1274 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1276 if (data
->allow_ref_in_want
&&
1277 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1280 /* process have line */
1281 if (parse_have(arg
, &data
->haves
))
1284 /* process args like thin-pack */
1285 if (!strcmp(arg
, "thin-pack")) {
1286 data
->use_thin_pack
= 1;
1289 if (!strcmp(arg
, "ofs-delta")) {
1290 data
->use_ofs_delta
= 1;
1293 if (!strcmp(arg
, "no-progress")) {
1294 data
->no_progress
= 1;
1297 if (!strcmp(arg
, "include-tag")) {
1298 data
->use_include_tag
= 1;
1301 if (!strcmp(arg
, "done")) {
1306 /* Shallow related arguments */
1307 if (process_shallow(arg
, &data
->shallows
))
1309 if (process_deepen(arg
, &data
->depth
))
1311 if (process_deepen_since(arg
, &data
->deepen_since
,
1312 &data
->deepen_rev_list
))
1314 if (process_deepen_not(arg
, &data
->deepen_not
,
1315 &data
->deepen_rev_list
))
1317 if (!strcmp(arg
, "deepen-relative")) {
1318 data
->deepen_relative
= 1;
1322 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1323 list_objects_filter_die_if_populated(&data
->filter_options
);
1324 parse_list_objects_filter(&data
->filter_options
, p
);
1328 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1329 data
->allow_sideband_all
) &&
1330 !strcmp(arg
, "sideband-all")) {
1331 data
->writer
.use_sideband
= 1;
1335 /* ignore unknown lines maybe? */
1336 die("unexpected line: '%s'", arg
);
1339 if (request
->status
!= PACKET_READ_FLUSH
)
1340 die(_("expected flush after fetch arguments"));
1343 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
,
1344 struct object_array
*have_obj
)
1349 for (i
= 0; i
< haves
->nr
; i
++) {
1350 const struct object_id
*oid
= &haves
->oid
[i
];
1352 int we_knew_they_have
= 0;
1354 if (!has_object_file(oid
))
1357 oid_array_append(common
, oid
);
1359 o
= parse_object(the_repository
, oid
);
1361 die("oops (%s)", oid_to_hex(oid
));
1362 if (o
->type
== OBJ_COMMIT
) {
1363 struct commit_list
*parents
;
1364 struct commit
*commit
= (struct commit
*)o
;
1365 if (o
->flags
& THEY_HAVE
)
1366 we_knew_they_have
= 1;
1368 o
->flags
|= THEY_HAVE
;
1369 if (!oldest_have
|| (commit
->date
< oldest_have
))
1370 oldest_have
= commit
->date
;
1371 for (parents
= commit
->parents
;
1373 parents
= parents
->next
)
1374 parents
->item
->object
.flags
|= THEY_HAVE
;
1376 if (!we_knew_they_have
)
1377 add_object_array(o
, NULL
, have_obj
);
1383 static int send_acks(struct packet_writer
*writer
, struct oid_array
*acks
,
1384 const struct object_array
*have_obj
,
1385 struct object_array
*want_obj
)
1389 packet_writer_write(writer
, "acknowledgments\n");
1393 packet_writer_write(writer
, "NAK\n");
1395 for (i
= 0; i
< acks
->nr
; i
++) {
1396 packet_writer_write(writer
, "ACK %s\n",
1397 oid_to_hex(&acks
->oid
[i
]));
1400 if (ok_to_give_up(have_obj
, want_obj
)) {
1402 packet_writer_write(writer
, "ready\n");
1409 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1411 struct oid_array common
= OID_ARRAY_INIT
;
1414 process_haves(&data
->haves
, &common
, &data
->have_obj
);
1417 } else if (send_acks(&data
->writer
, &common
,
1418 &data
->have_obj
, &data
->want_obj
)) {
1419 packet_writer_delim(&data
->writer
);
1423 packet_writer_flush(&data
->writer
);
1427 oid_array_clear(&data
->haves
);
1428 oid_array_clear(&common
);
1432 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1434 const struct string_list_item
*item
;
1436 if (!data
->wanted_refs
.nr
)
1439 packet_writer_write(&data
->writer
, "wanted-refs\n");
1441 for_each_string_list_item(item
, &data
->wanted_refs
) {
1442 packet_writer_write(&data
->writer
, "%s %s\n",
1443 oid_to_hex(item
->util
),
1447 packet_writer_delim(&data
->writer
);
1450 static void send_shallow_info(struct upload_pack_data
*data
)
1452 /* No shallow info needs to be sent */
1453 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1454 !is_repository_shallow(the_repository
))
1457 packet_writer_write(&data
->writer
, "shallow-info\n");
1459 if (!send_shallow_list(data
) &&
1460 is_repository_shallow(the_repository
))
1461 deepen(data
, INFINITE_DEPTH
);
1467 FETCH_PROCESS_ARGS
= 0,
1473 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1474 struct packet_reader
*request
)
1476 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1477 struct upload_pack_data data
;
1479 clear_object_flags(ALL_FLAGS
);
1481 upload_pack_data_init(&data
);
1482 data
.use_sideband
= LARGE_PACKET_MAX
;
1484 git_config(upload_pack_config
, &data
);
1486 while (state
!= FETCH_DONE
) {
1488 case FETCH_PROCESS_ARGS
:
1489 process_args(request
, &data
);
1491 if (!data
.want_obj
.nr
) {
1493 * Request didn't contain any 'want' lines,
1494 * guess they didn't want anything.
1497 } else if (data
.haves
.nr
) {
1499 * Request had 'have' lines, so lets ACK them.
1501 state
= FETCH_SEND_ACKS
;
1504 * Request had 'want's but no 'have's so we can
1505 * immedietly go to construct and send a pack.
1507 state
= FETCH_SEND_PACK
;
1510 case FETCH_SEND_ACKS
:
1511 if (process_haves_and_send_acks(&data
))
1512 state
= FETCH_SEND_PACK
;
1516 case FETCH_SEND_PACK
:
1517 send_wanted_ref_info(&data
);
1518 send_shallow_info(&data
);
1520 packet_writer_write(&data
.writer
, "packfile\n");
1521 create_pack_file(&data
);
1529 upload_pack_data_clear(&data
);
1533 int upload_pack_advertise(struct repository
*r
,
1534 struct strbuf
*value
)
1537 int allow_filter_value
;
1538 int allow_ref_in_want
;
1539 int allow_sideband_all_value
;
1541 strbuf_addstr(value
, "shallow");
1543 if (!repo_config_get_bool(the_repository
,
1544 "uploadpack.allowfilter",
1545 &allow_filter_value
) &&
1547 strbuf_addstr(value
, " filter");
1549 if (!repo_config_get_bool(the_repository
,
1550 "uploadpack.allowrefinwant",
1551 &allow_ref_in_want
) &&
1553 strbuf_addstr(value
, " ref-in-want");
1555 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1556 (!repo_config_get_bool(the_repository
,
1557 "uploadpack.allowsidebandall",
1558 &allow_sideband_all_value
) &&
1559 allow_sideband_all_value
))
1560 strbuf_addstr(value
, " sideband-all");