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"
30 /* Remember to update object flag allocation in object.h */
31 #define THEY_HAVE (1u << 11)
32 #define OUR_REF (1u << 12)
33 #define WANTED (1u << 13)
34 #define COMMON_KNOWN (1u << 14)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
42 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
44 static timestamp_t oldest_have
;
48 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
49 static int no_progress
, daemon_mode
;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request
;
57 static int shallow_nr
;
58 static struct object_array extra_edge_obj
;
59 static unsigned int timeout
;
60 static int keepalive
= 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband
;
65 static int stateless_rpc
;
66 static const char *pack_objects_hook
;
68 static int filter_capability_requested
;
69 static int allow_filter
;
70 static int allow_ref_in_want
;
71 static struct list_objects_filter_options filter_options
;
73 static void reset_timeout(void)
78 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
81 send_sideband(1, fd
, data
, sz
, use_sideband
);
88 /* XXX: are we happy to lose stuff here? */
92 write_or_die(fd
, data
, sz
);
95 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
98 if (graft
->nr_parent
== -1)
99 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
103 static void create_pack_file(const struct object_array
*have_obj
,
104 const struct object_array
*want_obj
)
106 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
107 char data
[8193], progress
[128];
108 char abort_msg
[] = "aborting due to possible repository "
109 "corruption on the remote side.";
115 if (!pack_objects_hook
)
116 pack_objects
.git_cmd
= 1;
118 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
119 argv_array_push(&pack_objects
.args
, "git");
120 pack_objects
.use_shell
= 1;
124 argv_array_push(&pack_objects
.args
, "--shallow-file");
125 argv_array_push(&pack_objects
.args
, "");
127 argv_array_push(&pack_objects
.args
, "pack-objects");
128 argv_array_push(&pack_objects
.args
, "--revs");
130 argv_array_push(&pack_objects
.args
, "--thin");
132 argv_array_push(&pack_objects
.args
, "--stdout");
134 argv_array_push(&pack_objects
.args
, "--shallow");
136 argv_array_push(&pack_objects
.args
, "--progress");
138 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
140 argv_array_push(&pack_objects
.args
, "--include-tag");
141 if (filter_options
.filter_spec
) {
142 if (pack_objects
.use_shell
) {
143 struct strbuf buf
= STRBUF_INIT
;
144 sq_quote_buf(&buf
, filter_options
.filter_spec
);
145 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
146 strbuf_release(&buf
);
148 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
149 filter_options
.filter_spec
);
153 pack_objects
.in
= -1;
154 pack_objects
.out
= -1;
155 pack_objects
.err
= -1;
157 if (start_command(&pack_objects
))
158 die("git upload-pack: unable to fork git-pack-objects");
160 pipe_fd
= xfdopen(pack_objects
.in
, "w");
163 for_each_commit_graft(write_one_shallow
, pipe_fd
);
165 for (i
= 0; i
< want_obj
->nr
; i
++)
166 fprintf(pipe_fd
, "%s\n",
167 oid_to_hex(&want_obj
->objects
[i
].item
->oid
));
168 fprintf(pipe_fd
, "--not\n");
169 for (i
= 0; i
< have_obj
->nr
; i
++)
170 fprintf(pipe_fd
, "%s\n",
171 oid_to_hex(&have_obj
->objects
[i
].item
->oid
));
172 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
173 fprintf(pipe_fd
, "%s\n",
174 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
175 fprintf(pipe_fd
, "\n");
179 /* We read from pack_objects.err to capture stderr output for
180 * progress bar, and pack_objects.out to capture the pack data.
184 struct pollfd pfd
[2];
185 int pe
, pu
, pollsize
;
193 if (0 <= pack_objects
.out
) {
194 pfd
[pollsize
].fd
= pack_objects
.out
;
195 pfd
[pollsize
].events
= POLLIN
;
199 if (0 <= pack_objects
.err
) {
200 pfd
[pollsize
].fd
= pack_objects
.err
;
201 pfd
[pollsize
].events
= POLLIN
;
209 ret
= poll(pfd
, pollsize
,
210 keepalive
< 0 ? -1 : 1000 * keepalive
);
213 if (errno
!= EINTR
) {
214 error_errno("poll failed, resuming");
219 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
220 /* Status ready; we ship that in the side-band
221 * or dump to the standard error.
223 sz
= xread(pack_objects
.err
, progress
,
226 send_client_data(2, progress
, sz
);
228 close(pack_objects
.err
);
229 pack_objects
.err
= -1;
233 /* give priority to status messages */
236 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
237 /* Data ready; we keep the last byte to ourselves
238 * in case we detect broken rev-list, so that we
239 * can leave the stream corrupted. This is
240 * unfortunate -- unpack-objects would happily
241 * accept a valid packdata with trailing garbage,
242 * so appending garbage after we pass all the
243 * pack data is not good enough to signal
244 * breakage to downstream.
252 sz
= xread(pack_objects
.out
, cp
,
253 sizeof(data
) - outsz
);
257 close(pack_objects
.out
);
258 pack_objects
.out
= -1;
264 buffered
= data
[sz
-1] & 0xFF;
269 send_client_data(1, data
, sz
);
273 * We hit the keepalive timeout without saying anything; send
274 * an empty message on the data sideband just to let the other
275 * side know we're still working on it, but don't have any data
278 * If we don't have a sideband channel, there's no room in the
279 * protocol to say anything, so those clients are just out of
282 if (!ret
&& use_sideband
) {
283 static const char buf
[] = "0005\1";
284 write_or_die(1, buf
, 5);
288 if (finish_command(&pack_objects
)) {
289 error("git upload-pack: git-pack-objects died with error.");
296 send_client_data(1, data
, 1);
297 fprintf(stderr
, "flushed.\n");
304 send_client_data(3, abort_msg
, sizeof(abort_msg
));
305 die("git upload-pack: %s", abort_msg
);
308 static int got_oid(const char *hex
, struct object_id
*oid
,
309 struct object_array
*have_obj
)
312 int we_knew_they_have
= 0;
314 if (get_oid_hex(hex
, oid
))
315 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
316 if (!has_object_file(oid
))
319 o
= parse_object(the_repository
, oid
);
321 die("oops (%s)", oid_to_hex(oid
));
322 if (o
->type
== OBJ_COMMIT
) {
323 struct commit_list
*parents
;
324 struct commit
*commit
= (struct commit
*)o
;
325 if (o
->flags
& THEY_HAVE
)
326 we_knew_they_have
= 1;
328 o
->flags
|= THEY_HAVE
;
329 if (!oldest_have
|| (commit
->date
< oldest_have
))
330 oldest_have
= commit
->date
;
331 for (parents
= commit
->parents
;
333 parents
= parents
->next
)
334 parents
->item
->object
.flags
|= THEY_HAVE
;
336 if (!we_knew_they_have
) {
337 add_object_array(o
, NULL
, have_obj
);
343 static int ok_to_give_up(const struct object_array
*have_obj
,
344 struct object_array
*want_obj
)
346 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
351 return can_all_from_reach_with_flag(want_obj
, THEY_HAVE
,
352 COMMON_KNOWN
, oldest_have
,
356 static int get_common_commits(struct object_array
*have_obj
,
357 struct object_array
*want_obj
)
359 struct object_id oid
;
360 char last_hex
[GIT_MAX_HEXSZ
+ 1];
365 save_commit_buffer
= 0;
368 char *line
= packet_read_line(0, NULL
);
374 if (multi_ack
== 2 && got_common
375 && !got_other
&& ok_to_give_up(have_obj
, want_obj
)) {
377 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
379 if (have_obj
->nr
== 0 || multi_ack
)
380 packet_write_fmt(1, "NAK\n");
382 if (no_done
&& sent_ready
) {
383 packet_write_fmt(1, "ACK %s\n", last_hex
);
392 if (skip_prefix(line
, "have ", &arg
)) {
393 switch (got_oid(arg
, &oid
, have_obj
)) {
394 case -1: /* they have what we do not */
396 if (multi_ack
&& ok_to_give_up(have_obj
, want_obj
)) {
397 const char *hex
= oid_to_hex(&oid
);
398 if (multi_ack
== 2) {
400 packet_write_fmt(1, "ACK %s ready\n", hex
);
402 packet_write_fmt(1, "ACK %s continue\n", hex
);
407 oid_to_hex_r(last_hex
, &oid
);
409 packet_write_fmt(1, "ACK %s common\n", last_hex
);
411 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
412 else if (have_obj
->nr
== 1)
413 packet_write_fmt(1, "ACK %s\n", last_hex
);
418 if (!strcmp(line
, "done")) {
419 if (have_obj
->nr
> 0) {
421 packet_write_fmt(1, "ACK %s\n", last_hex
);
424 packet_write_fmt(1, "NAK\n");
427 die("git upload-pack: expected SHA1 list, got '%s'", line
);
431 static int is_our_ref(struct object
*o
)
433 int allow_hidden_ref
= (allow_unadvertised_object_request
&
434 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
435 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
439 * on successful case, it's up to the caller to close cmd->out
441 static int do_reachable_revlist(struct child_process
*cmd
,
442 struct object_array
*src
,
443 struct object_array
*reachable
)
445 static const char *argv
[] = {
446 "rev-list", "--stdin", NULL
,
449 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
451 const unsigned hexsz
= the_hash_algo
->hexsz
;
460 * If the next rev-list --stdin encounters an unknown commit,
461 * it terminates, which will cause SIGPIPE in the write loop
464 sigchain_push(SIGPIPE
, SIG_IGN
);
466 if (start_command(cmd
))
470 namebuf
[hexsz
+ 1] = '\n';
471 for (i
= get_max_object_index(); 0 < i
; ) {
472 o
= get_indexed_object(--i
);
475 if (reachable
&& o
->type
== OBJ_COMMIT
)
476 o
->flags
&= ~TMP_MARK
;
479 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
480 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
483 namebuf
[hexsz
] = '\n';
484 for (i
= 0; i
< src
->nr
; i
++) {
485 o
= src
->objects
[i
].item
;
488 add_object_array(o
, NULL
, reachable
);
491 if (reachable
&& o
->type
== OBJ_COMMIT
)
492 o
->flags
|= TMP_MARK
;
493 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
494 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
499 sigchain_pop(SIGPIPE
);
504 sigchain_pop(SIGPIPE
);
513 static int get_reachable_list(struct object_array
*src
,
514 struct object_array
*reachable
)
516 struct child_process cmd
= CHILD_PROCESS_INIT
;
519 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
520 const unsigned hexsz
= the_hash_algo
->hexsz
;
522 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
525 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
526 struct object_id sha1
;
529 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
532 o
= lookup_object(the_repository
, sha1
.hash
);
533 if (o
&& o
->type
== OBJ_COMMIT
) {
534 o
->flags
&= ~TMP_MARK
;
537 for (i
= get_max_object_index(); 0 < i
; i
--) {
538 o
= get_indexed_object(i
- 1);
539 if (o
&& o
->type
== OBJ_COMMIT
&&
540 (o
->flags
& TMP_MARK
)) {
541 add_object_array(o
, NULL
, reachable
);
542 o
->flags
&= ~TMP_MARK
;
547 if (finish_command(&cmd
))
553 static int has_unreachable(struct object_array
*src
)
555 struct child_process cmd
= CHILD_PROCESS_INIT
;
559 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
563 * The commits out of the rev-list are not ancestors of
566 i
= read_in_full(cmd
.out
, buf
, 1);
573 * rev-list may have died by encountering a bad commit
574 * in the history, in which case we do want to bail out
575 * even when it showed no commit.
577 if (finish_command(&cmd
))
580 /* All the non-tip ones are ancestors of what we advertised */
584 sigchain_pop(SIGPIPE
);
590 static void check_non_tip(struct object_array
*want_obj
)
595 * In the normal in-process case without
596 * uploadpack.allowReachableSHA1InWant,
597 * non-tip requests can never happen.
599 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
601 if (!has_unreachable(want_obj
))
602 /* All the non-tip ones are ancestors of what we advertised */
606 /* Pick one of them (we know there at least is one) */
607 for (i
= 0; i
< want_obj
->nr
; i
++) {
608 struct object
*o
= want_obj
->objects
[i
].item
;
610 die("git upload-pack: not our ref %s",
611 oid_to_hex(&o
->oid
));
615 static void send_shallow(struct commit_list
*result
)
618 struct object
*object
= &result
->item
->object
;
619 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
620 packet_write_fmt(1, "shallow %s",
621 oid_to_hex(&object
->oid
));
622 register_shallow(the_repository
, &object
->oid
);
625 result
= result
->next
;
629 static void send_unshallow(const struct object_array
*shallows
,
630 struct object_array
*want_obj
)
634 for (i
= 0; i
< shallows
->nr
; i
++) {
635 struct object
*object
= shallows
->objects
[i
].item
;
636 if (object
->flags
& NOT_SHALLOW
) {
637 struct commit_list
*parents
;
638 packet_write_fmt(1, "unshallow %s",
639 oid_to_hex(&object
->oid
));
640 object
->flags
&= ~CLIENT_SHALLOW
;
642 * We want to _register_ "object" as shallow, but we
643 * also need to traverse object's parents to deepen a
644 * shallow clone. Unregister it for now so we can
645 * parse and add the parents to the want list, then
648 unregister_shallow(&object
->oid
);
650 parse_commit_or_die((struct commit
*)object
);
651 parents
= ((struct commit
*)object
)->parents
;
653 add_object_array(&parents
->item
->object
,
655 parents
= parents
->next
;
657 add_object_array(object
, NULL
, &extra_edge_obj
);
659 /* make sure commit traversal conforms to client */
660 register_shallow(the_repository
, &object
->oid
);
664 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
665 int flag
, void *cb_data
);
667 static void deepen(int depth
, int deepen_relative
,
668 struct object_array
*shallows
, struct object_array
*want_obj
)
670 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
673 for (i
= 0; i
< shallows
->nr
; i
++) {
674 struct object
*object
= shallows
->objects
[i
].item
;
675 object
->flags
|= NOT_SHALLOW
;
677 } else if (deepen_relative
) {
678 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
679 struct commit_list
*result
;
682 * Checking for reachable shallows requires that our refs be
683 * marked with OUR_REF.
685 head_ref_namespaced(check_ref
, NULL
);
686 for_each_namespaced_ref(check_ref
, NULL
);
688 get_reachable_list(shallows
, &reachable_shallows
);
689 result
= get_shallow_commits(&reachable_shallows
,
691 SHALLOW
, NOT_SHALLOW
);
692 send_shallow(result
);
693 free_commit_list(result
);
694 object_array_clear(&reachable_shallows
);
696 struct commit_list
*result
;
698 result
= get_shallow_commits(want_obj
, depth
,
699 SHALLOW
, NOT_SHALLOW
);
700 send_shallow(result
);
701 free_commit_list(result
);
704 send_unshallow(shallows
, want_obj
);
707 static void deepen_by_rev_list(int ac
, const char **av
,
708 struct object_array
*shallows
,
709 struct object_array
*want_obj
)
711 struct commit_list
*result
;
713 close_commit_graph(the_repository
);
714 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
715 send_shallow(result
);
716 free_commit_list(result
);
717 send_unshallow(shallows
, want_obj
);
720 /* Returns 1 if a shallow list is sent or 0 otherwise */
721 static int send_shallow_list(int depth
, int deepen_rev_list
,
722 timestamp_t deepen_since
,
723 struct string_list
*deepen_not
,
725 struct object_array
*shallows
,
726 struct object_array
*want_obj
)
730 if (depth
> 0 && deepen_rev_list
)
731 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
733 deepen(depth
, deepen_relative
, shallows
, want_obj
);
735 } else if (deepen_rev_list
) {
736 struct argv_array av
= ARGV_ARRAY_INIT
;
739 argv_array_push(&av
, "rev-list");
741 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
742 if (deepen_not
->nr
) {
743 argv_array_push(&av
, "--not");
744 for (i
= 0; i
< deepen_not
->nr
; i
++) {
745 struct string_list_item
*s
= deepen_not
->items
+ i
;
746 argv_array_push(&av
, s
->string
);
748 argv_array_push(&av
, "--not");
750 for (i
= 0; i
< want_obj
->nr
; i
++) {
751 struct object
*o
= want_obj
->objects
[i
].item
;
752 argv_array_push(&av
, oid_to_hex(&o
->oid
));
754 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
, want_obj
);
755 argv_array_clear(&av
);
758 if (shallows
->nr
> 0) {
760 for (i
= 0; i
< shallows
->nr
; i
++)
761 register_shallow(the_repository
,
762 &shallows
->objects
[i
].item
->oid
);
766 shallow_nr
+= shallows
->nr
;
770 static int process_shallow(const char *line
, struct object_array
*shallows
)
773 if (skip_prefix(line
, "shallow ", &arg
)) {
774 struct object_id oid
;
775 struct object
*object
;
776 if (get_oid_hex(arg
, &oid
))
777 die("invalid shallow line: %s", line
);
778 object
= parse_object(the_repository
, &oid
);
781 if (object
->type
!= OBJ_COMMIT
)
782 die("invalid shallow object %s", oid_to_hex(&oid
));
783 if (!(object
->flags
& CLIENT_SHALLOW
)) {
784 object
->flags
|= CLIENT_SHALLOW
;
785 add_object_array(object
, NULL
, shallows
);
793 static int process_deepen(const char *line
, int *depth
)
796 if (skip_prefix(line
, "deepen ", &arg
)) {
798 *depth
= (int)strtol(arg
, &end
, 0);
799 if (!end
|| *end
|| *depth
<= 0)
800 die("Invalid deepen: %s", line
);
807 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
810 if (skip_prefix(line
, "deepen-since ", &arg
)) {
812 *deepen_since
= parse_timestamp(arg
, &end
, 0);
813 if (!end
|| *end
|| !deepen_since
||
814 /* revisions.c's max_age -1 is special */
816 die("Invalid deepen-since: %s", line
);
817 *deepen_rev_list
= 1;
823 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
826 if (skip_prefix(line
, "deepen-not ", &arg
)) {
828 struct object_id oid
;
829 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
830 die("git upload-pack: ambiguous deepen-not: %s", line
);
831 string_list_append(deepen_not
, ref
);
833 *deepen_rev_list
= 1;
839 static void receive_needs(struct object_array
*want_obj
)
841 struct object_array shallows
= OBJECT_ARRAY_INIT
;
842 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
845 timestamp_t deepen_since
= 0;
846 int deepen_rev_list
= 0;
847 int deepen_relative
= 0;
852 const char *features
;
853 struct object_id oid_buf
;
854 char *line
= packet_read_line(0, NULL
);
861 if (process_shallow(line
, &shallows
))
863 if (process_deepen(line
, &depth
))
865 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
867 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
870 if (skip_prefix(line
, "filter ", &arg
)) {
871 if (!filter_capability_requested
)
872 die("git upload-pack: filtering capability not negotiated");
873 parse_list_objects_filter(&filter_options
, arg
);
877 if (!skip_prefix(line
, "want ", &arg
) ||
878 parse_oid_hex(arg
, &oid_buf
, &features
))
879 die("git upload-pack: protocol error, "
880 "expected to get object ID, not '%s'", line
);
882 if (parse_feature_request(features
, "deepen-relative"))
884 if (parse_feature_request(features
, "multi_ack_detailed"))
886 else if (parse_feature_request(features
, "multi_ack"))
888 if (parse_feature_request(features
, "no-done"))
890 if (parse_feature_request(features
, "thin-pack"))
892 if (parse_feature_request(features
, "ofs-delta"))
894 if (parse_feature_request(features
, "side-band-64k"))
895 use_sideband
= LARGE_PACKET_MAX
;
896 else if (parse_feature_request(features
, "side-band"))
897 use_sideband
= DEFAULT_PACKET_MAX
;
898 if (parse_feature_request(features
, "no-progress"))
900 if (parse_feature_request(features
, "include-tag"))
902 if (allow_filter
&& parse_feature_request(features
, "filter"))
903 filter_capability_requested
= 1;
905 o
= parse_object(the_repository
, &oid_buf
);
908 "ERR upload-pack: not our ref %s",
909 oid_to_hex(&oid_buf
));
910 die("git upload-pack: not our ref %s",
911 oid_to_hex(&oid_buf
));
913 if (!(o
->flags
& WANTED
)) {
915 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
918 add_object_array(o
, NULL
, want_obj
);
923 * We have sent all our refs already, and the other end
924 * should have chosen out of them. When we are operating
925 * in the stateless RPC mode, however, their choice may
926 * have been based on the set of older refs advertised
927 * by another process that handled the initial request.
930 check_non_tip(want_obj
);
932 if (!use_sideband
&& daemon_mode
)
935 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
938 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
939 &deepen_not
, deepen_relative
, &shallows
,
942 object_array_clear(&shallows
);
945 /* return non-zero if the ref is hidden, otherwise 0 */
946 static int mark_our_ref(const char *refname
, const char *refname_full
,
947 const struct object_id
*oid
)
949 struct object
*o
= lookup_unknown_object(oid
->hash
);
951 if (ref_is_hidden(refname
, refname_full
)) {
952 o
->flags
|= HIDDEN_REF
;
959 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
960 int flag
, void *cb_data
)
962 const char *refname
= strip_namespace(refname_full
);
964 mark_our_ref(refname
, refname_full
, oid
);
968 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
970 struct string_list_item
*item
;
974 for_each_string_list_item(item
, symref
)
975 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
978 static int send_ref(const char *refname
, const struct object_id
*oid
,
979 int flag
, void *cb_data
)
981 static const char *capabilities
= "multi_ack thin-pack side-band"
982 " side-band-64k ofs-delta shallow deepen-since deepen-not"
983 " deepen-relative no-progress include-tag multi_ack_detailed";
984 const char *refname_nons
= strip_namespace(refname
);
985 struct object_id peeled
;
987 if (mark_our_ref(refname_nons
, refname
, oid
))
991 struct strbuf symref_info
= STRBUF_INIT
;
993 format_symref_info(&symref_info
, cb_data
);
994 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
995 oid_to_hex(oid
), refname_nons
,
997 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
998 " allow-tip-sha1-in-want" : "",
999 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1000 " allow-reachable-sha1-in-want" : "",
1001 stateless_rpc
? " no-done" : "",
1003 allow_filter
? " filter" : "",
1004 git_user_agent_sanitized());
1005 strbuf_release(&symref_info
);
1007 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1009 capabilities
= NULL
;
1010 if (!peel_ref(refname
, &peeled
))
1011 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1015 static int find_symref(const char *refname
, const struct object_id
*oid
,
1016 int flag
, void *cb_data
)
1018 const char *symref_target
;
1019 struct string_list_item
*item
;
1021 if ((flag
& REF_ISSYMREF
) == 0)
1023 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1024 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1025 die("'%s' is a symref but it is not?", refname
);
1026 item
= string_list_append(cb_data
, refname
);
1027 item
->util
= xstrdup(symref_target
);
1031 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1033 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1034 if (git_config_bool(var
, value
))
1035 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1037 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1038 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1039 if (git_config_bool(var
, value
))
1040 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1042 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1043 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1044 if (git_config_bool(var
, value
))
1045 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1047 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1048 } else if (!strcmp("uploadpack.keepalive", var
)) {
1049 keepalive
= git_config_int(var
, value
);
1052 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1053 allow_filter
= git_config_bool(var
, value
);
1054 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1055 allow_ref_in_want
= git_config_bool(var
, value
);
1058 if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1059 if (!strcmp("uploadpack.packobjectshook", var
))
1060 return git_config_string(&pack_objects_hook
, var
, value
);
1063 return parse_hide_refs_config(var
, value
, "uploadpack");
1066 void upload_pack(struct upload_pack_options
*options
)
1068 struct string_list symref
= STRING_LIST_INIT_DUP
;
1069 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1071 stateless_rpc
= options
->stateless_rpc
;
1072 timeout
= options
->timeout
;
1073 daemon_mode
= options
->daemon_mode
;
1075 git_config(upload_pack_config
, NULL
);
1077 head_ref_namespaced(find_symref
, &symref
);
1079 if (options
->advertise_refs
|| !stateless_rpc
) {
1081 head_ref_namespaced(send_ref
, &symref
);
1082 for_each_namespaced_ref(send_ref
, &symref
);
1083 advertise_shallow_grafts(1);
1086 head_ref_namespaced(check_ref
, NULL
);
1087 for_each_namespaced_ref(check_ref
, NULL
);
1089 string_list_clear(&symref
, 1);
1090 if (options
->advertise_refs
)
1093 receive_needs(&want_obj
);
1095 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1096 get_common_commits(&have_obj
, &want_obj
);
1097 create_pack_file(&have_obj
, &want_obj
);
1101 struct upload_pack_data
{
1102 struct object_array wants
;
1103 struct string_list wanted_refs
;
1104 struct oid_array haves
;
1106 struct object_array shallows
;
1107 struct string_list deepen_not
;
1109 timestamp_t deepen_since
;
1110 int deepen_rev_list
;
1111 int deepen_relative
;
1113 unsigned stateless_rpc
: 1;
1115 unsigned use_thin_pack
: 1;
1116 unsigned use_ofs_delta
: 1;
1117 unsigned no_progress
: 1;
1118 unsigned use_include_tag
: 1;
1122 static void upload_pack_data_init(struct upload_pack_data
*data
)
1124 struct object_array wants
= OBJECT_ARRAY_INIT
;
1125 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1126 struct oid_array haves
= OID_ARRAY_INIT
;
1127 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1128 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1130 memset(data
, 0, sizeof(*data
));
1131 data
->wants
= wants
;
1132 data
->wanted_refs
= wanted_refs
;
1133 data
->haves
= haves
;
1134 data
->shallows
= shallows
;
1135 data
->deepen_not
= deepen_not
;
1138 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1140 object_array_clear(&data
->wants
);
1141 string_list_clear(&data
->wanted_refs
, 1);
1142 oid_array_clear(&data
->haves
);
1143 object_array_clear(&data
->shallows
);
1144 string_list_clear(&data
->deepen_not
, 0);
1147 static int parse_want(const char *line
, struct object_array
*want_obj
)
1150 if (skip_prefix(line
, "want ", &arg
)) {
1151 struct object_id oid
;
1154 if (get_oid_hex(arg
, &oid
))
1155 die("git upload-pack: protocol error, "
1156 "expected to get oid, not '%s'", line
);
1158 o
= parse_object(the_repository
, &oid
);
1161 "ERR upload-pack: not our ref %s",
1163 die("git upload-pack: not our ref %s",
1167 if (!(o
->flags
& WANTED
)) {
1169 add_object_array(o
, NULL
, want_obj
);
1178 static int parse_want_ref(const char *line
, struct string_list
*wanted_refs
,
1179 struct object_array
*want_obj
)
1182 if (skip_prefix(line
, "want-ref ", &arg
)) {
1183 struct object_id oid
;
1184 struct string_list_item
*item
;
1187 if (read_ref(arg
, &oid
)) {
1188 packet_write_fmt(1, "ERR unknown ref %s", arg
);
1189 die("unknown ref %s", arg
);
1192 item
= string_list_append(wanted_refs
, arg
);
1193 item
->util
= oiddup(&oid
);
1195 o
= parse_object_or_die(&oid
, arg
);
1196 if (!(o
->flags
& WANTED
)) {
1198 add_object_array(o
, NULL
, want_obj
);
1207 static int parse_have(const char *line
, struct oid_array
*haves
)
1210 if (skip_prefix(line
, "have ", &arg
)) {
1211 struct object_id oid
;
1213 if (get_oid_hex(arg
, &oid
))
1214 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1215 oid_array_append(haves
, &oid
);
1222 static void process_args(struct packet_reader
*request
,
1223 struct upload_pack_data
*data
,
1224 struct object_array
*want_obj
)
1226 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1227 const char *arg
= request
->line
;
1231 if (parse_want(arg
, want_obj
))
1233 if (allow_ref_in_want
&&
1234 parse_want_ref(arg
, &data
->wanted_refs
, want_obj
))
1236 /* process have line */
1237 if (parse_have(arg
, &data
->haves
))
1240 /* process args like thin-pack */
1241 if (!strcmp(arg
, "thin-pack")) {
1245 if (!strcmp(arg
, "ofs-delta")) {
1249 if (!strcmp(arg
, "no-progress")) {
1253 if (!strcmp(arg
, "include-tag")) {
1254 use_include_tag
= 1;
1257 if (!strcmp(arg
, "done")) {
1262 /* Shallow related arguments */
1263 if (process_shallow(arg
, &data
->shallows
))
1265 if (process_deepen(arg
, &data
->depth
))
1267 if (process_deepen_since(arg
, &data
->deepen_since
,
1268 &data
->deepen_rev_list
))
1270 if (process_deepen_not(arg
, &data
->deepen_not
,
1271 &data
->deepen_rev_list
))
1273 if (!strcmp(arg
, "deepen-relative")) {
1274 data
->deepen_relative
= 1;
1278 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1279 parse_list_objects_filter(&filter_options
, p
);
1283 /* ignore unknown lines maybe? */
1284 die("unexpected line: '%s'", arg
);
1288 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
,
1289 struct object_array
*have_obj
)
1294 for (i
= 0; i
< haves
->nr
; i
++) {
1295 const struct object_id
*oid
= &haves
->oid
[i
];
1297 int we_knew_they_have
= 0;
1299 if (!has_object_file(oid
))
1302 oid_array_append(common
, oid
);
1304 o
= parse_object(the_repository
, oid
);
1306 die("oops (%s)", oid_to_hex(oid
));
1307 if (o
->type
== OBJ_COMMIT
) {
1308 struct commit_list
*parents
;
1309 struct commit
*commit
= (struct commit
*)o
;
1310 if (o
->flags
& THEY_HAVE
)
1311 we_knew_they_have
= 1;
1313 o
->flags
|= THEY_HAVE
;
1314 if (!oldest_have
|| (commit
->date
< oldest_have
))
1315 oldest_have
= commit
->date
;
1316 for (parents
= commit
->parents
;
1318 parents
= parents
->next
)
1319 parents
->item
->object
.flags
|= THEY_HAVE
;
1321 if (!we_knew_they_have
)
1322 add_object_array(o
, NULL
, have_obj
);
1328 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
,
1329 const struct object_array
*have_obj
,
1330 struct object_array
*want_obj
)
1334 packet_buf_write(response
, "acknowledgments\n");
1338 packet_buf_write(response
, "NAK\n");
1340 for (i
= 0; i
< acks
->nr
; i
++) {
1341 packet_buf_write(response
, "ACK %s\n",
1342 oid_to_hex(&acks
->oid
[i
]));
1345 if (ok_to_give_up(have_obj
, want_obj
)) {
1347 packet_buf_write(response
, "ready\n");
1354 static int process_haves_and_send_acks(struct upload_pack_data
*data
,
1355 struct object_array
*have_obj
,
1356 struct object_array
*want_obj
)
1358 struct oid_array common
= OID_ARRAY_INIT
;
1359 struct strbuf response
= STRBUF_INIT
;
1362 process_haves(&data
->haves
, &common
, have_obj
);
1365 } else if (send_acks(&common
, &response
, have_obj
, want_obj
)) {
1366 packet_buf_delim(&response
);
1370 packet_buf_flush(&response
);
1375 write_or_die(1, response
.buf
, response
.len
);
1376 strbuf_release(&response
);
1378 oid_array_clear(&data
->haves
);
1379 oid_array_clear(&common
);
1383 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1385 const struct string_list_item
*item
;
1387 if (!data
->wanted_refs
.nr
)
1390 packet_write_fmt(1, "wanted-refs\n");
1392 for_each_string_list_item(item
, &data
->wanted_refs
) {
1393 packet_write_fmt(1, "%s %s\n",
1394 oid_to_hex(item
->util
),
1401 static void send_shallow_info(struct upload_pack_data
*data
,
1402 struct object_array
*want_obj
)
1404 /* No shallow info needs to be sent */
1405 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1406 !is_repository_shallow(the_repository
))
1409 packet_write_fmt(1, "shallow-info\n");
1411 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1412 data
->deepen_since
, &data
->deepen_not
,
1413 data
->deepen_relative
,
1414 &data
->shallows
, want_obj
) &&
1415 is_repository_shallow(the_repository
))
1416 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
,
1423 FETCH_PROCESS_ARGS
= 0,
1429 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1430 struct packet_reader
*request
)
1432 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1433 struct upload_pack_data data
;
1434 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1435 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1437 clear_object_flags(ALL_FLAGS
);
1439 git_config(upload_pack_config
, NULL
);
1441 upload_pack_data_init(&data
);
1442 use_sideband
= LARGE_PACKET_MAX
;
1444 while (state
!= FETCH_DONE
) {
1446 case FETCH_PROCESS_ARGS
:
1447 process_args(request
, &data
, &want_obj
);
1451 * Request didn't contain any 'want' lines,
1452 * guess they didn't want anything.
1455 } else if (data
.haves
.nr
) {
1457 * Request had 'have' lines, so lets ACK them.
1459 state
= FETCH_SEND_ACKS
;
1462 * Request had 'want's but no 'have's so we can
1463 * immedietly go to construct and send a pack.
1465 state
= FETCH_SEND_PACK
;
1468 case FETCH_SEND_ACKS
:
1469 if (process_haves_and_send_acks(&data
, &have_obj
,
1471 state
= FETCH_SEND_PACK
;
1475 case FETCH_SEND_PACK
:
1476 send_wanted_ref_info(&data
);
1477 send_shallow_info(&data
, &want_obj
);
1479 packet_write_fmt(1, "packfile\n");
1480 create_pack_file(&have_obj
, &want_obj
);
1488 upload_pack_data_clear(&data
);
1489 object_array_clear(&have_obj
);
1490 object_array_clear(&want_obj
);
1494 int upload_pack_advertise(struct repository
*r
,
1495 struct strbuf
*value
)
1498 int allow_filter_value
;
1499 int allow_ref_in_want
;
1501 strbuf_addstr(value
, "shallow");
1503 if (!repo_config_get_bool(the_repository
,
1504 "uploadpack.allowfilter",
1505 &allow_filter_value
) &&
1507 strbuf_addstr(value
, " filter");
1509 if (!repo_config_get_bool(the_repository
,
1510 "uploadpack.allowrefinwant",
1511 &allow_ref_in_want
) &&
1513 strbuf_addstr(value
, " ref-in-want");