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 /* Enum for allowed unadvertised object request (UOR) */
49 /* Allow specifying sha1 if it is a ref tip. */
50 ALLOW_TIP_SHA1
= 0x01,
51 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52 ALLOW_REACHABLE_SHA1
= 0x02,
53 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
58 * Please annotate, and if possible group together, fields used only
59 * for protocol v0 or only for protocol v2.
61 struct upload_pack_data
{
62 struct string_list symref
; /* v0 only */
63 struct object_array want_obj
;
64 struct object_array have_obj
;
65 struct oid_array haves
; /* v2 only */
66 struct string_list wanted_refs
; /* v2 only */
68 struct object_array shallows
;
69 struct string_list deepen_not
;
70 struct object_array extra_edge_obj
;
72 timestamp_t deepen_since
;
78 unsigned int timeout
; /* v0 only */
82 MULTI_ACK_DETAILED
= 2
83 } multi_ack
; /* v0 only */
85 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
88 enum allow_uor allow_uor
;
90 struct list_objects_filter_options filter_options
;
92 struct packet_writer writer
;
94 const char *pack_objects_hook
;
96 unsigned stateless_rpc
: 1; /* v0 only */
97 unsigned no_done
: 1; /* v0 only */
98 unsigned daemon_mode
: 1; /* v0 only */
99 unsigned filter_capability_requested
: 1; /* v0 only */
101 unsigned use_thin_pack
: 1;
102 unsigned use_ofs_delta
: 1;
103 unsigned no_progress
: 1;
104 unsigned use_include_tag
: 1;
105 unsigned allow_filter
: 1;
107 unsigned done
: 1; /* v2 only */
108 unsigned allow_ref_in_want
: 1; /* v2 only */
109 unsigned allow_sideband_all
: 1; /* v2 only */
112 static void upload_pack_data_init(struct upload_pack_data
*data
)
114 struct string_list symref
= STRING_LIST_INIT_DUP
;
115 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
116 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
117 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
118 struct oid_array haves
= OID_ARRAY_INIT
;
119 struct object_array shallows
= OBJECT_ARRAY_INIT
;
120 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
121 struct object_array extra_edge_obj
= OBJECT_ARRAY_INIT
;
123 memset(data
, 0, sizeof(*data
));
124 data
->symref
= symref
;
125 data
->wanted_refs
= wanted_refs
;
126 data
->want_obj
= want_obj
;
127 data
->have_obj
= have_obj
;
129 data
->shallows
= shallows
;
130 data
->deepen_not
= deepen_not
;
131 data
->extra_edge_obj
= extra_edge_obj
;
132 packet_writer_init(&data
->writer
, 1);
137 static void upload_pack_data_clear(struct upload_pack_data
*data
)
139 string_list_clear(&data
->symref
, 1);
140 string_list_clear(&data
->wanted_refs
, 1);
141 object_array_clear(&data
->want_obj
);
142 object_array_clear(&data
->have_obj
);
143 oid_array_clear(&data
->haves
);
144 object_array_clear(&data
->shallows
);
145 string_list_clear(&data
->deepen_not
, 0);
146 object_array_clear(&data
->extra_edge_obj
);
147 list_objects_filter_release(&data
->filter_options
);
149 free((char *)data
->pack_objects_hook
);
152 static void reset_timeout(unsigned int timeout
)
157 static void send_client_data(int fd
, const char *data
, ssize_t sz
,
161 send_sideband(1, fd
, data
, sz
, use_sideband
);
168 /* XXX: are we happy to lose stuff here? */
169 xwrite(fd
, data
, sz
);
172 write_or_die(fd
, data
, sz
);
175 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
178 if (graft
->nr_parent
== -1)
179 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
183 static void create_pack_file(struct upload_pack_data
*pack_data
)
185 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
186 char data
[8193], progress
[128];
187 char abort_msg
[] = "aborting due to possible repository "
188 "corruption on the remote side.";
194 if (!pack_data
->pack_objects_hook
)
195 pack_objects
.git_cmd
= 1;
197 argv_array_push(&pack_objects
.args
, pack_data
->pack_objects_hook
);
198 argv_array_push(&pack_objects
.args
, "git");
199 pack_objects
.use_shell
= 1;
202 if (pack_data
->shallow_nr
) {
203 argv_array_push(&pack_objects
.args
, "--shallow-file");
204 argv_array_push(&pack_objects
.args
, "");
206 argv_array_push(&pack_objects
.args
, "pack-objects");
207 argv_array_push(&pack_objects
.args
, "--revs");
208 if (pack_data
->use_thin_pack
)
209 argv_array_push(&pack_objects
.args
, "--thin");
211 argv_array_push(&pack_objects
.args
, "--stdout");
212 if (pack_data
->shallow_nr
)
213 argv_array_push(&pack_objects
.args
, "--shallow");
214 if (!pack_data
->no_progress
)
215 argv_array_push(&pack_objects
.args
, "--progress");
216 if (pack_data
->use_ofs_delta
)
217 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
218 if (pack_data
->use_include_tag
)
219 argv_array_push(&pack_objects
.args
, "--include-tag");
220 if (pack_data
->filter_options
.choice
) {
222 expand_list_objects_filter_spec(&pack_data
->filter_options
);
223 if (pack_objects
.use_shell
) {
224 struct strbuf buf
= STRBUF_INIT
;
225 sq_quote_buf(&buf
, spec
);
226 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
227 strbuf_release(&buf
);
229 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
234 pack_objects
.in
= -1;
235 pack_objects
.out
= -1;
236 pack_objects
.err
= -1;
238 if (start_command(&pack_objects
))
239 die("git upload-pack: unable to fork git-pack-objects");
241 pipe_fd
= xfdopen(pack_objects
.in
, "w");
243 if (pack_data
->shallow_nr
)
244 for_each_commit_graft(write_one_shallow
, pipe_fd
);
246 for (i
= 0; i
< pack_data
->want_obj
.nr
; i
++)
247 fprintf(pipe_fd
, "%s\n",
248 oid_to_hex(&pack_data
->want_obj
.objects
[i
].item
->oid
));
249 fprintf(pipe_fd
, "--not\n");
250 for (i
= 0; i
< pack_data
->have_obj
.nr
; i
++)
251 fprintf(pipe_fd
, "%s\n",
252 oid_to_hex(&pack_data
->have_obj
.objects
[i
].item
->oid
));
253 for (i
= 0; i
< pack_data
->extra_edge_obj
.nr
; i
++)
254 fprintf(pipe_fd
, "%s\n",
255 oid_to_hex(&pack_data
->extra_edge_obj
.objects
[i
].item
->oid
));
256 fprintf(pipe_fd
, "\n");
260 /* We read from pack_objects.err to capture stderr output for
261 * progress bar, and pack_objects.out to capture the pack data.
265 struct pollfd pfd
[2];
266 int pe
, pu
, pollsize
, polltimeout
;
269 reset_timeout(pack_data
->timeout
);
274 if (0 <= pack_objects
.out
) {
275 pfd
[pollsize
].fd
= pack_objects
.out
;
276 pfd
[pollsize
].events
= POLLIN
;
280 if (0 <= pack_objects
.err
) {
281 pfd
[pollsize
].fd
= pack_objects
.err
;
282 pfd
[pollsize
].events
= POLLIN
;
290 polltimeout
= pack_data
->keepalive
< 0
292 : 1000 * pack_data
->keepalive
;
294 ret
= poll(pfd
, pollsize
, polltimeout
);
297 if (errno
!= EINTR
) {
298 error_errno("poll failed, resuming");
303 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
304 /* Status ready; we ship that in the side-band
305 * or dump to the standard error.
307 sz
= xread(pack_objects
.err
, progress
,
310 send_client_data(2, progress
, sz
,
311 pack_data
->use_sideband
);
313 close(pack_objects
.err
);
314 pack_objects
.err
= -1;
318 /* give priority to status messages */
321 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
322 /* Data ready; we keep the last byte to ourselves
323 * in case we detect broken rev-list, so that we
324 * can leave the stream corrupted. This is
325 * unfortunate -- unpack-objects would happily
326 * accept a valid packdata with trailing garbage,
327 * so appending garbage after we pass all the
328 * pack data is not good enough to signal
329 * breakage to downstream.
337 sz
= xread(pack_objects
.out
, cp
,
338 sizeof(data
) - outsz
);
342 close(pack_objects
.out
);
343 pack_objects
.out
= -1;
349 buffered
= data
[sz
-1] & 0xFF;
354 send_client_data(1, data
, sz
,
355 pack_data
->use_sideband
);
359 * We hit the keepalive timeout without saying anything; send
360 * an empty message on the data sideband just to let the other
361 * side know we're still working on it, but don't have any data
364 * If we don't have a sideband channel, there's no room in the
365 * protocol to say anything, so those clients are just out of
368 if (!ret
&& pack_data
->use_sideband
) {
369 static const char buf
[] = "0005\1";
370 write_or_die(1, buf
, 5);
374 if (finish_command(&pack_objects
)) {
375 error("git upload-pack: git-pack-objects died with error.");
382 send_client_data(1, data
, 1,
383 pack_data
->use_sideband
);
384 fprintf(stderr
, "flushed.\n");
386 if (pack_data
->use_sideband
)
391 send_client_data(3, abort_msg
, sizeof(abort_msg
),
392 pack_data
->use_sideband
);
393 die("git upload-pack: %s", abort_msg
);
396 static int got_oid(const char *hex
, struct object_id
*oid
,
397 struct object_array
*have_obj
)
400 int we_knew_they_have
= 0;
402 if (get_oid_hex(hex
, oid
))
403 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
404 if (!has_object_file(oid
))
407 o
= parse_object(the_repository
, oid
);
409 die("oops (%s)", oid_to_hex(oid
));
410 if (o
->type
== OBJ_COMMIT
) {
411 struct commit_list
*parents
;
412 struct commit
*commit
= (struct commit
*)o
;
413 if (o
->flags
& THEY_HAVE
)
414 we_knew_they_have
= 1;
416 o
->flags
|= THEY_HAVE
;
417 if (!oldest_have
|| (commit
->date
< oldest_have
))
418 oldest_have
= commit
->date
;
419 for (parents
= commit
->parents
;
421 parents
= parents
->next
)
422 parents
->item
->object
.flags
|= THEY_HAVE
;
424 if (!we_knew_they_have
) {
425 add_object_array(o
, NULL
, have_obj
);
431 static int ok_to_give_up(struct upload_pack_data
*data
)
433 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
435 if (!data
->have_obj
.nr
)
438 return can_all_from_reach_with_flag(&data
->want_obj
, THEY_HAVE
,
439 COMMON_KNOWN
, oldest_have
,
443 static int get_common_commits(struct upload_pack_data
*data
,
444 struct packet_reader
*reader
)
446 struct object_id oid
;
447 char last_hex
[GIT_MAX_HEXSZ
+ 1];
452 save_commit_buffer
= 0;
457 reset_timeout(data
->timeout
);
459 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
460 if (data
->multi_ack
== MULTI_ACK_DETAILED
463 && ok_to_give_up(data
)) {
465 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
467 if (data
->have_obj
.nr
== 0 || data
->multi_ack
)
468 packet_write_fmt(1, "NAK\n");
470 if (data
->no_done
&& sent_ready
) {
471 packet_write_fmt(1, "ACK %s\n", last_hex
);
474 if (data
->stateless_rpc
)
480 if (skip_prefix(reader
->line
, "have ", &arg
)) {
481 switch (got_oid(arg
, &oid
, &data
->have_obj
)) {
482 case -1: /* they have what we do not */
485 && ok_to_give_up(data
)) {
486 const char *hex
= oid_to_hex(&oid
);
487 if (data
->multi_ack
== MULTI_ACK_DETAILED
) {
489 packet_write_fmt(1, "ACK %s ready\n", hex
);
491 packet_write_fmt(1, "ACK %s continue\n", hex
);
496 oid_to_hex_r(last_hex
, &oid
);
497 if (data
->multi_ack
== MULTI_ACK_DETAILED
)
498 packet_write_fmt(1, "ACK %s common\n", last_hex
);
499 else if (data
->multi_ack
)
500 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
501 else if (data
->have_obj
.nr
== 1)
502 packet_write_fmt(1, "ACK %s\n", last_hex
);
507 if (!strcmp(reader
->line
, "done")) {
508 if (data
->have_obj
.nr
> 0) {
510 packet_write_fmt(1, "ACK %s\n", last_hex
);
513 packet_write_fmt(1, "NAK\n");
516 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
520 static int is_our_ref(struct object
*o
, enum allow_uor allow_uor
)
522 int allow_hidden_ref
= (allow_uor
&
523 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
524 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
528 * on successful case, it's up to the caller to close cmd->out
530 static int do_reachable_revlist(struct child_process
*cmd
,
531 struct object_array
*src
,
532 struct object_array
*reachable
,
533 enum allow_uor allow_uor
)
535 static const char *argv
[] = {
536 "rev-list", "--stdin", NULL
,
539 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
541 const unsigned hexsz
= the_hash_algo
->hexsz
;
550 * If the next rev-list --stdin encounters an unknown commit,
551 * it terminates, which will cause SIGPIPE in the write loop
554 sigchain_push(SIGPIPE
, SIG_IGN
);
556 if (start_command(cmd
))
560 namebuf
[hexsz
+ 1] = '\n';
561 for (i
= get_max_object_index(); 0 < i
; ) {
562 o
= get_indexed_object(--i
);
565 if (reachable
&& o
->type
== OBJ_COMMIT
)
566 o
->flags
&= ~TMP_MARK
;
567 if (!is_our_ref(o
, allow_uor
))
569 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
570 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
573 namebuf
[hexsz
] = '\n';
574 for (i
= 0; i
< src
->nr
; i
++) {
575 o
= src
->objects
[i
].item
;
576 if (is_our_ref(o
, allow_uor
)) {
578 add_object_array(o
, NULL
, reachable
);
581 if (reachable
&& o
->type
== OBJ_COMMIT
)
582 o
->flags
|= TMP_MARK
;
583 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
584 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
589 sigchain_pop(SIGPIPE
);
594 sigchain_pop(SIGPIPE
);
603 static int get_reachable_list(struct upload_pack_data
*data
,
604 struct object_array
*reachable
)
606 struct child_process cmd
= CHILD_PROCESS_INIT
;
609 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
610 const unsigned hexsz
= the_hash_algo
->hexsz
;
612 if (do_reachable_revlist(&cmd
, &data
->shallows
, reachable
,
613 data
->allow_uor
) < 0)
616 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
617 struct object_id oid
;
620 if (parse_oid_hex(namebuf
, &oid
, &p
) || *p
!= '\n')
623 o
= lookup_object(the_repository
, &oid
);
624 if (o
&& o
->type
== OBJ_COMMIT
) {
625 o
->flags
&= ~TMP_MARK
;
628 for (i
= get_max_object_index(); 0 < i
; i
--) {
629 o
= get_indexed_object(i
- 1);
630 if (o
&& o
->type
== OBJ_COMMIT
&&
631 (o
->flags
& TMP_MARK
)) {
632 add_object_array(o
, NULL
, reachable
);
633 o
->flags
&= ~TMP_MARK
;
638 if (finish_command(&cmd
))
644 static int has_unreachable(struct object_array
*src
, enum allow_uor allow_uor
)
646 struct child_process cmd
= CHILD_PROCESS_INIT
;
650 if (do_reachable_revlist(&cmd
, src
, NULL
, allow_uor
) < 0)
654 * The commits out of the rev-list are not ancestors of
657 i
= read_in_full(cmd
.out
, buf
, 1);
664 * rev-list may have died by encountering a bad commit
665 * in the history, in which case we do want to bail out
666 * even when it showed no commit.
668 if (finish_command(&cmd
))
671 /* All the non-tip ones are ancestors of what we advertised */
675 sigchain_pop(SIGPIPE
);
681 static void check_non_tip(struct upload_pack_data
*data
)
686 * In the normal in-process case without
687 * uploadpack.allowReachableSHA1InWant,
688 * non-tip requests can never happen.
690 if (!data
->stateless_rpc
&& !(data
->allow_uor
& ALLOW_REACHABLE_SHA1
))
692 if (!has_unreachable(&data
->want_obj
, data
->allow_uor
))
693 /* All the non-tip ones are ancestors of what we advertised */
697 /* Pick one of them (we know there at least is one) */
698 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
699 struct object
*o
= data
->want_obj
.objects
[i
].item
;
700 if (!is_our_ref(o
, data
->allow_uor
)) {
701 packet_writer_error(&data
->writer
,
702 "upload-pack: not our ref %s",
703 oid_to_hex(&o
->oid
));
704 die("git upload-pack: not our ref %s",
705 oid_to_hex(&o
->oid
));
710 static void send_shallow(struct upload_pack_data
*data
,
711 struct commit_list
*result
)
714 struct object
*object
= &result
->item
->object
;
715 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
716 packet_writer_write(&data
->writer
, "shallow %s",
717 oid_to_hex(&object
->oid
));
718 register_shallow(the_repository
, &object
->oid
);
721 result
= result
->next
;
725 static void send_unshallow(struct upload_pack_data
*data
)
729 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
730 struct object
*object
= data
->shallows
.objects
[i
].item
;
731 if (object
->flags
& NOT_SHALLOW
) {
732 struct commit_list
*parents
;
733 packet_writer_write(&data
->writer
, "unshallow %s",
734 oid_to_hex(&object
->oid
));
735 object
->flags
&= ~CLIENT_SHALLOW
;
737 * We want to _register_ "object" as shallow, but we
738 * also need to traverse object's parents to deepen a
739 * shallow clone. Unregister it for now so we can
740 * parse and add the parents to the want list, then
743 unregister_shallow(&object
->oid
);
745 parse_commit_or_die((struct commit
*)object
);
746 parents
= ((struct commit
*)object
)->parents
;
748 add_object_array(&parents
->item
->object
,
749 NULL
, &data
->want_obj
);
750 parents
= parents
->next
;
752 add_object_array(object
, NULL
, &data
->extra_edge_obj
);
754 /* make sure commit traversal conforms to client */
755 register_shallow(the_repository
, &object
->oid
);
759 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
760 int flag
, void *cb_data
);
761 static void deepen(struct upload_pack_data
*data
, int depth
)
763 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
766 for (i
= 0; i
< data
->shallows
.nr
; i
++) {
767 struct object
*object
= data
->shallows
.objects
[i
].item
;
768 object
->flags
|= NOT_SHALLOW
;
770 } else if (data
->deepen_relative
) {
771 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
772 struct commit_list
*result
;
775 * Checking for reachable shallows requires that our refs be
776 * marked with OUR_REF.
778 head_ref_namespaced(check_ref
, NULL
);
779 for_each_namespaced_ref(check_ref
, NULL
);
781 get_reachable_list(data
, &reachable_shallows
);
782 result
= get_shallow_commits(&reachable_shallows
,
784 SHALLOW
, NOT_SHALLOW
);
785 send_shallow(data
, result
);
786 free_commit_list(result
);
787 object_array_clear(&reachable_shallows
);
789 struct commit_list
*result
;
791 result
= get_shallow_commits(&data
->want_obj
, depth
,
792 SHALLOW
, NOT_SHALLOW
);
793 send_shallow(data
, result
);
794 free_commit_list(result
);
797 send_unshallow(data
);
800 static void deepen_by_rev_list(struct upload_pack_data
*data
,
804 struct commit_list
*result
;
806 disable_commit_graph(the_repository
);
807 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
808 send_shallow(data
, result
);
809 free_commit_list(result
);
810 send_unshallow(data
);
813 /* Returns 1 if a shallow list is sent or 0 otherwise */
814 static int send_shallow_list(struct upload_pack_data
*data
)
818 if (data
->depth
> 0 && data
->deepen_rev_list
)
819 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
820 if (data
->depth
> 0) {
821 deepen(data
, data
->depth
);
823 } else if (data
->deepen_rev_list
) {
824 struct argv_array av
= ARGV_ARRAY_INIT
;
827 argv_array_push(&av
, "rev-list");
828 if (data
->deepen_since
)
829 argv_array_pushf(&av
, "--max-age=%"PRItime
, data
->deepen_since
);
830 if (data
->deepen_not
.nr
) {
831 argv_array_push(&av
, "--not");
832 for (i
= 0; i
< data
->deepen_not
.nr
; i
++) {
833 struct string_list_item
*s
= data
->deepen_not
.items
+ i
;
834 argv_array_push(&av
, s
->string
);
836 argv_array_push(&av
, "--not");
838 for (i
= 0; i
< data
->want_obj
.nr
; i
++) {
839 struct object
*o
= data
->want_obj
.objects
[i
].item
;
840 argv_array_push(&av
, oid_to_hex(&o
->oid
));
842 deepen_by_rev_list(data
, av
.argc
, av
.argv
);
843 argv_array_clear(&av
);
846 if (data
->shallows
.nr
> 0) {
848 for (i
= 0; i
< data
->shallows
.nr
; i
++)
849 register_shallow(the_repository
,
850 &data
->shallows
.objects
[i
].item
->oid
);
854 data
->shallow_nr
+= data
->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
)
932 data
->shallow_nr
= 0;
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 (!((data
->allow_uor
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
1000 || is_our_ref(o
, data
->allow_uor
)))
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
))
1026 /* return non-zero if the ref is hidden, otherwise 0 */
1027 static int mark_our_ref(const char *refname
, const char *refname_full
,
1028 const struct object_id
*oid
)
1030 struct object
*o
= lookup_unknown_object(oid
);
1032 if (ref_is_hidden(refname
, refname_full
)) {
1033 o
->flags
|= HIDDEN_REF
;
1036 o
->flags
|= OUR_REF
;
1040 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
1041 int flag
, void *cb_data
)
1043 const char *refname
= strip_namespace(refname_full
);
1045 mark_our_ref(refname
, refname_full
, oid
);
1049 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
1051 struct string_list_item
*item
;
1055 for_each_string_list_item(item
, symref
)
1056 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1059 static int send_ref(const char *refname
, const struct object_id
*oid
,
1060 int flag
, void *cb_data
)
1062 static const char *capabilities
= "multi_ack thin-pack side-band"
1063 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1064 " deepen-relative no-progress include-tag multi_ack_detailed";
1065 const char *refname_nons
= strip_namespace(refname
);
1066 struct object_id peeled
;
1067 struct upload_pack_data
*data
= cb_data
;
1069 if (mark_our_ref(refname_nons
, refname
, oid
))
1073 struct strbuf symref_info
= STRBUF_INIT
;
1075 format_symref_info(&symref_info
, &data
->symref
);
1076 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1077 oid_to_hex(oid
), refname_nons
,
1079 (data
->allow_uor
& ALLOW_TIP_SHA1
) ?
1080 " allow-tip-sha1-in-want" : "",
1081 (data
->allow_uor
& ALLOW_REACHABLE_SHA1
) ?
1082 " allow-reachable-sha1-in-want" : "",
1083 data
->stateless_rpc
? " no-done" : "",
1085 data
->allow_filter
? " filter" : "",
1086 git_user_agent_sanitized());
1087 strbuf_release(&symref_info
);
1089 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1091 capabilities
= NULL
;
1092 if (!peel_ref(refname
, &peeled
))
1093 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1097 static int find_symref(const char *refname
, const struct object_id
*oid
,
1098 int flag
, void *cb_data
)
1100 const char *symref_target
;
1101 struct string_list_item
*item
;
1103 if ((flag
& REF_ISSYMREF
) == 0)
1105 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1106 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1107 die("'%s' is a symref but it is not?", refname
);
1108 item
= string_list_append(cb_data
, strip_namespace(refname
));
1109 item
->util
= xstrdup(strip_namespace(symref_target
));
1113 static int upload_pack_config(const char *var
, const char *value
, void *cb_data
)
1115 struct upload_pack_data
*data
= cb_data
;
1117 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1118 if (git_config_bool(var
, value
))
1119 data
->allow_uor
|= ALLOW_TIP_SHA1
;
1121 data
->allow_uor
&= ~ALLOW_TIP_SHA1
;
1122 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1123 if (git_config_bool(var
, value
))
1124 data
->allow_uor
|= ALLOW_REACHABLE_SHA1
;
1126 data
->allow_uor
&= ~ALLOW_REACHABLE_SHA1
;
1127 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1128 if (git_config_bool(var
, value
))
1129 data
->allow_uor
|= ALLOW_ANY_SHA1
;
1131 data
->allow_uor
&= ~ALLOW_ANY_SHA1
;
1132 } else if (!strcmp("uploadpack.keepalive", var
)) {
1133 data
->keepalive
= git_config_int(var
, value
);
1134 if (!data
->keepalive
)
1135 data
->keepalive
= -1;
1136 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1137 data
->allow_filter
= git_config_bool(var
, value
);
1138 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1139 data
->allow_ref_in_want
= git_config_bool(var
, value
);
1140 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1141 data
->allow_sideband_all
= git_config_bool(var
, value
);
1142 } else if (!strcmp("core.precomposeunicode", var
)) {
1143 precomposed_unicode
= git_config_bool(var
, value
);
1146 if (current_config_scope() != CONFIG_SCOPE_LOCAL
&&
1147 current_config_scope() != CONFIG_SCOPE_WORKTREE
) {
1148 if (!strcmp("uploadpack.packobjectshook", var
))
1149 return git_config_string(&data
->pack_objects_hook
, var
, value
);
1152 return parse_hide_refs_config(var
, value
, "uploadpack");
1155 void upload_pack(struct upload_pack_options
*options
)
1157 struct packet_reader reader
;
1158 struct upload_pack_data data
;
1160 upload_pack_data_init(&data
);
1162 git_config(upload_pack_config
, &data
);
1164 data
.stateless_rpc
= options
->stateless_rpc
;
1165 data
.daemon_mode
= options
->daemon_mode
;
1166 data
.timeout
= options
->timeout
;
1168 head_ref_namespaced(find_symref
, &data
.symref
);
1170 if (options
->advertise_refs
|| !data
.stateless_rpc
) {
1171 reset_timeout(data
.timeout
);
1172 head_ref_namespaced(send_ref
, &data
);
1173 for_each_namespaced_ref(send_ref
, &data
);
1174 advertise_shallow_grafts(1);
1177 head_ref_namespaced(check_ref
, NULL
);
1178 for_each_namespaced_ref(check_ref
, NULL
);
1181 if (!options
->advertise_refs
) {
1182 packet_reader_init(&reader
, 0, NULL
, 0,
1183 PACKET_READ_CHOMP_NEWLINE
|
1184 PACKET_READ_DIE_ON_ERR_PACKET
);
1186 receive_needs(&data
, &reader
);
1187 if (data
.want_obj
.nr
) {
1188 get_common_commits(&data
, &reader
);
1189 create_pack_file(&data
);
1193 upload_pack_data_clear(&data
);
1196 static int parse_want(struct packet_writer
*writer
, const char *line
,
1197 struct object_array
*want_obj
)
1200 if (skip_prefix(line
, "want ", &arg
)) {
1201 struct object_id oid
;
1204 if (get_oid_hex(arg
, &oid
))
1205 die("git upload-pack: protocol error, "
1206 "expected to get oid, not '%s'", line
);
1208 o
= parse_object(the_repository
, &oid
);
1210 packet_writer_error(writer
,
1211 "upload-pack: not our ref %s",
1213 die("git upload-pack: not our ref %s",
1217 if (!(o
->flags
& WANTED
)) {
1219 add_object_array(o
, NULL
, want_obj
);
1228 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1229 struct string_list
*wanted_refs
,
1230 struct object_array
*want_obj
)
1233 if (skip_prefix(line
, "want-ref ", &arg
)) {
1234 struct object_id oid
;
1235 struct string_list_item
*item
;
1238 if (read_ref(arg
, &oid
)) {
1239 packet_writer_error(writer
, "unknown ref %s", arg
);
1240 die("unknown ref %s", arg
);
1243 item
= string_list_append(wanted_refs
, arg
);
1244 item
->util
= oiddup(&oid
);
1246 o
= parse_object_or_die(&oid
, arg
);
1247 if (!(o
->flags
& WANTED
)) {
1249 add_object_array(o
, NULL
, want_obj
);
1258 static int parse_have(const char *line
, struct oid_array
*haves
)
1261 if (skip_prefix(line
, "have ", &arg
)) {
1262 struct object_id oid
;
1264 if (get_oid_hex(arg
, &oid
))
1265 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1266 oid_array_append(haves
, &oid
);
1273 static void process_args(struct packet_reader
*request
,
1274 struct upload_pack_data
*data
)
1276 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
1277 const char *arg
= request
->line
;
1281 if (parse_want(&data
->writer
, arg
, &data
->want_obj
))
1283 if (data
->allow_ref_in_want
&&
1284 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1287 /* process have line */
1288 if (parse_have(arg
, &data
->haves
))
1291 /* process args like thin-pack */
1292 if (!strcmp(arg
, "thin-pack")) {
1293 data
->use_thin_pack
= 1;
1296 if (!strcmp(arg
, "ofs-delta")) {
1297 data
->use_ofs_delta
= 1;
1300 if (!strcmp(arg
, "no-progress")) {
1301 data
->no_progress
= 1;
1304 if (!strcmp(arg
, "include-tag")) {
1305 data
->use_include_tag
= 1;
1308 if (!strcmp(arg
, "done")) {
1313 /* Shallow related arguments */
1314 if (process_shallow(arg
, &data
->shallows
))
1316 if (process_deepen(arg
, &data
->depth
))
1318 if (process_deepen_since(arg
, &data
->deepen_since
,
1319 &data
->deepen_rev_list
))
1321 if (process_deepen_not(arg
, &data
->deepen_not
,
1322 &data
->deepen_rev_list
))
1324 if (!strcmp(arg
, "deepen-relative")) {
1325 data
->deepen_relative
= 1;
1329 if (data
->allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1330 list_objects_filter_die_if_populated(&data
->filter_options
);
1331 parse_list_objects_filter(&data
->filter_options
, p
);
1335 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1336 data
->allow_sideband_all
) &&
1337 !strcmp(arg
, "sideband-all")) {
1338 data
->writer
.use_sideband
= 1;
1342 /* ignore unknown lines maybe? */
1343 die("unexpected line: '%s'", arg
);
1346 if (request
->status
!= PACKET_READ_FLUSH
)
1347 die(_("expected flush after fetch arguments"));
1350 static int process_haves(struct upload_pack_data
*data
, struct oid_array
*common
)
1355 for (i
= 0; i
< data
->haves
.nr
; i
++) {
1356 const struct object_id
*oid
= &data
->haves
.oid
[i
];
1358 int we_knew_they_have
= 0;
1360 if (!has_object_file(oid
))
1363 oid_array_append(common
, oid
);
1365 o
= parse_object(the_repository
, oid
);
1367 die("oops (%s)", oid_to_hex(oid
));
1368 if (o
->type
== OBJ_COMMIT
) {
1369 struct commit_list
*parents
;
1370 struct commit
*commit
= (struct commit
*)o
;
1371 if (o
->flags
& THEY_HAVE
)
1372 we_knew_they_have
= 1;
1374 o
->flags
|= THEY_HAVE
;
1375 if (!oldest_have
|| (commit
->date
< oldest_have
))
1376 oldest_have
= commit
->date
;
1377 for (parents
= commit
->parents
;
1379 parents
= parents
->next
)
1380 parents
->item
->object
.flags
|= THEY_HAVE
;
1382 if (!we_knew_they_have
)
1383 add_object_array(o
, NULL
, &data
->have_obj
);
1389 static int send_acks(struct upload_pack_data
*data
, struct oid_array
*acks
)
1393 packet_writer_write(&data
->writer
, "acknowledgments\n");
1397 packet_writer_write(&data
->writer
, "NAK\n");
1399 for (i
= 0; i
< acks
->nr
; i
++) {
1400 packet_writer_write(&data
->writer
, "ACK %s\n",
1401 oid_to_hex(&acks
->oid
[i
]));
1404 if (ok_to_give_up(data
)) {
1406 packet_writer_write(&data
->writer
, "ready\n");
1413 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1415 struct oid_array common
= OID_ARRAY_INIT
;
1418 process_haves(data
, &common
);
1421 } else if (send_acks(data
, &common
)) {
1422 packet_writer_delim(&data
->writer
);
1426 packet_writer_flush(&data
->writer
);
1430 oid_array_clear(&data
->haves
);
1431 oid_array_clear(&common
);
1435 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1437 const struct string_list_item
*item
;
1439 if (!data
->wanted_refs
.nr
)
1442 packet_writer_write(&data
->writer
, "wanted-refs\n");
1444 for_each_string_list_item(item
, &data
->wanted_refs
) {
1445 packet_writer_write(&data
->writer
, "%s %s\n",
1446 oid_to_hex(item
->util
),
1450 packet_writer_delim(&data
->writer
);
1453 static void send_shallow_info(struct upload_pack_data
*data
)
1455 /* No shallow info needs to be sent */
1456 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1457 !is_repository_shallow(the_repository
))
1460 packet_writer_write(&data
->writer
, "shallow-info\n");
1462 if (!send_shallow_list(data
) &&
1463 is_repository_shallow(the_repository
))
1464 deepen(data
, INFINITE_DEPTH
);
1470 FETCH_PROCESS_ARGS
= 0,
1476 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1477 struct packet_reader
*request
)
1479 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1480 struct upload_pack_data data
;
1482 clear_object_flags(ALL_FLAGS
);
1484 upload_pack_data_init(&data
);
1485 data
.use_sideband
= LARGE_PACKET_MAX
;
1487 git_config(upload_pack_config
, &data
);
1489 while (state
!= FETCH_DONE
) {
1491 case FETCH_PROCESS_ARGS
:
1492 process_args(request
, &data
);
1494 if (!data
.want_obj
.nr
) {
1496 * Request didn't contain any 'want' lines,
1497 * guess they didn't want anything.
1500 } else if (data
.haves
.nr
) {
1502 * Request had 'have' lines, so lets ACK them.
1504 state
= FETCH_SEND_ACKS
;
1507 * Request had 'want's but no 'have's so we can
1508 * immedietly go to construct and send a pack.
1510 state
= FETCH_SEND_PACK
;
1513 case FETCH_SEND_ACKS
:
1514 if (process_haves_and_send_acks(&data
))
1515 state
= FETCH_SEND_PACK
;
1519 case FETCH_SEND_PACK
:
1520 send_wanted_ref_info(&data
);
1521 send_shallow_info(&data
);
1523 packet_writer_write(&data
.writer
, "packfile\n");
1524 create_pack_file(&data
);
1532 upload_pack_data_clear(&data
);
1536 int upload_pack_advertise(struct repository
*r
,
1537 struct strbuf
*value
)
1540 int allow_filter_value
;
1541 int allow_ref_in_want
;
1542 int allow_sideband_all_value
;
1544 strbuf_addstr(value
, "shallow");
1546 if (!repo_config_get_bool(the_repository
,
1547 "uploadpack.allowfilter",
1548 &allow_filter_value
) &&
1550 strbuf_addstr(value
, " filter");
1552 if (!repo_config_get_bool(the_repository
,
1553 "uploadpack.allowrefinwant",
1554 &allow_ref_in_want
) &&
1556 strbuf_addstr(value
, " ref-in-want");
1558 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1559 (!repo_config_get_bool(the_repository
,
1560 "uploadpack.allowsidebandall",
1561 &allow_sideband_all_value
) &&
1562 allow_sideband_all_value
))
1563 strbuf_addstr(value
, " sideband-all");