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
;
46 static int deepen_relative
;
49 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
50 static int no_progress
, daemon_mode
;
51 /* Allow specifying sha1 if it is a ref tip. */
52 #define ALLOW_TIP_SHA1 01
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 #define ALLOW_REACHABLE_SHA1 02
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 #define ALLOW_ANY_SHA1 07
57 static unsigned int allow_unadvertised_object_request
;
58 static int shallow_nr
;
59 static struct object_array extra_edge_obj
;
60 static unsigned int timeout
;
61 static int keepalive
= 5;
63 * otherwise maximum packet size (up to 65520 bytes).
65 static int use_sideband
;
66 static int stateless_rpc
;
67 static const char *pack_objects_hook
;
69 static int filter_capability_requested
;
70 static int allow_filter
;
71 static int allow_ref_in_want
;
72 static struct list_objects_filter_options filter_options
;
74 static void reset_timeout(void)
79 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
82 send_sideband(1, fd
, data
, sz
, use_sideband
);
89 /* XXX: are we happy to lose stuff here? */
93 write_or_die(fd
, data
, sz
);
96 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
99 if (graft
->nr_parent
== -1)
100 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
104 static void create_pack_file(const struct object_array
*have_obj
,
105 const struct object_array
*want_obj
)
107 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
108 char data
[8193], progress
[128];
109 char abort_msg
[] = "aborting due to possible repository "
110 "corruption on the remote side.";
116 if (!pack_objects_hook
)
117 pack_objects
.git_cmd
= 1;
119 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
120 argv_array_push(&pack_objects
.args
, "git");
121 pack_objects
.use_shell
= 1;
125 argv_array_push(&pack_objects
.args
, "--shallow-file");
126 argv_array_push(&pack_objects
.args
, "");
128 argv_array_push(&pack_objects
.args
, "pack-objects");
129 argv_array_push(&pack_objects
.args
, "--revs");
131 argv_array_push(&pack_objects
.args
, "--thin");
133 argv_array_push(&pack_objects
.args
, "--stdout");
135 argv_array_push(&pack_objects
.args
, "--shallow");
137 argv_array_push(&pack_objects
.args
, "--progress");
139 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
141 argv_array_push(&pack_objects
.args
, "--include-tag");
142 if (filter_options
.filter_spec
) {
143 struct strbuf expanded_filter_spec
= STRBUF_INIT
;
144 expand_list_objects_filter_spec(&filter_options
,
145 &expanded_filter_spec
);
146 if (pack_objects
.use_shell
) {
147 struct strbuf buf
= STRBUF_INIT
;
148 sq_quote_buf(&buf
, expanded_filter_spec
.buf
);
149 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
150 strbuf_release(&buf
);
152 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
153 expanded_filter_spec
.buf
);
157 pack_objects
.in
= -1;
158 pack_objects
.out
= -1;
159 pack_objects
.err
= -1;
161 if (start_command(&pack_objects
))
162 die("git upload-pack: unable to fork git-pack-objects");
164 pipe_fd
= xfdopen(pack_objects
.in
, "w");
167 for_each_commit_graft(write_one_shallow
, pipe_fd
);
169 for (i
= 0; i
< want_obj
->nr
; i
++)
170 fprintf(pipe_fd
, "%s\n",
171 oid_to_hex(&want_obj
->objects
[i
].item
->oid
));
172 fprintf(pipe_fd
, "--not\n");
173 for (i
= 0; i
< have_obj
->nr
; i
++)
174 fprintf(pipe_fd
, "%s\n",
175 oid_to_hex(&have_obj
->objects
[i
].item
->oid
));
176 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
177 fprintf(pipe_fd
, "%s\n",
178 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
179 fprintf(pipe_fd
, "\n");
183 /* We read from pack_objects.err to capture stderr output for
184 * progress bar, and pack_objects.out to capture the pack data.
188 struct pollfd pfd
[2];
189 int pe
, pu
, pollsize
;
197 if (0 <= pack_objects
.out
) {
198 pfd
[pollsize
].fd
= pack_objects
.out
;
199 pfd
[pollsize
].events
= POLLIN
;
203 if (0 <= pack_objects
.err
) {
204 pfd
[pollsize
].fd
= pack_objects
.err
;
205 pfd
[pollsize
].events
= POLLIN
;
213 ret
= poll(pfd
, pollsize
,
214 keepalive
< 0 ? -1 : 1000 * keepalive
);
217 if (errno
!= EINTR
) {
218 error_errno("poll failed, resuming");
223 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
224 /* Status ready; we ship that in the side-band
225 * or dump to the standard error.
227 sz
= xread(pack_objects
.err
, progress
,
230 send_client_data(2, progress
, sz
);
232 close(pack_objects
.err
);
233 pack_objects
.err
= -1;
237 /* give priority to status messages */
240 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
241 /* Data ready; we keep the last byte to ourselves
242 * in case we detect broken rev-list, so that we
243 * can leave the stream corrupted. This is
244 * unfortunate -- unpack-objects would happily
245 * accept a valid packdata with trailing garbage,
246 * so appending garbage after we pass all the
247 * pack data is not good enough to signal
248 * breakage to downstream.
256 sz
= xread(pack_objects
.out
, cp
,
257 sizeof(data
) - outsz
);
261 close(pack_objects
.out
);
262 pack_objects
.out
= -1;
268 buffered
= data
[sz
-1] & 0xFF;
273 send_client_data(1, data
, sz
);
277 * We hit the keepalive timeout without saying anything; send
278 * an empty message on the data sideband just to let the other
279 * side know we're still working on it, but don't have any data
282 * If we don't have a sideband channel, there's no room in the
283 * protocol to say anything, so those clients are just out of
286 if (!ret
&& use_sideband
) {
287 static const char buf
[] = "0005\1";
288 write_or_die(1, buf
, 5);
292 if (finish_command(&pack_objects
)) {
293 error("git upload-pack: git-pack-objects died with error.");
300 send_client_data(1, data
, 1);
301 fprintf(stderr
, "flushed.\n");
308 send_client_data(3, abort_msg
, sizeof(abort_msg
));
309 die("git upload-pack: %s", abort_msg
);
312 static int got_oid(const char *hex
, struct object_id
*oid
,
313 struct object_array
*have_obj
)
316 int we_knew_they_have
= 0;
318 if (get_oid_hex(hex
, oid
))
319 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
320 if (!has_object_file(oid
))
323 o
= parse_object(the_repository
, oid
);
325 die("oops (%s)", oid_to_hex(oid
));
326 if (o
->type
== OBJ_COMMIT
) {
327 struct commit_list
*parents
;
328 struct commit
*commit
= (struct commit
*)o
;
329 if (o
->flags
& THEY_HAVE
)
330 we_knew_they_have
= 1;
332 o
->flags
|= THEY_HAVE
;
333 if (!oldest_have
|| (commit
->date
< oldest_have
))
334 oldest_have
= commit
->date
;
335 for (parents
= commit
->parents
;
337 parents
= parents
->next
)
338 parents
->item
->object
.flags
|= THEY_HAVE
;
340 if (!we_knew_they_have
) {
341 add_object_array(o
, NULL
, have_obj
);
347 static int ok_to_give_up(const struct object_array
*have_obj
,
348 struct object_array
*want_obj
)
350 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
355 return can_all_from_reach_with_flag(want_obj
, THEY_HAVE
,
356 COMMON_KNOWN
, oldest_have
,
360 static int get_common_commits(struct object_array
*have_obj
,
361 struct object_array
*want_obj
)
363 struct object_id oid
;
364 char last_hex
[GIT_MAX_HEXSZ
+ 1];
369 save_commit_buffer
= 0;
372 char *line
= packet_read_line(0, NULL
);
378 if (multi_ack
== 2 && got_common
379 && !got_other
&& ok_to_give_up(have_obj
, want_obj
)) {
381 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
383 if (have_obj
->nr
== 0 || multi_ack
)
384 packet_write_fmt(1, "NAK\n");
386 if (no_done
&& sent_ready
) {
387 packet_write_fmt(1, "ACK %s\n", last_hex
);
396 if (skip_prefix(line
, "have ", &arg
)) {
397 switch (got_oid(arg
, &oid
, have_obj
)) {
398 case -1: /* they have what we do not */
400 if (multi_ack
&& ok_to_give_up(have_obj
, want_obj
)) {
401 const char *hex
= oid_to_hex(&oid
);
402 if (multi_ack
== 2) {
404 packet_write_fmt(1, "ACK %s ready\n", hex
);
406 packet_write_fmt(1, "ACK %s continue\n", hex
);
411 oid_to_hex_r(last_hex
, &oid
);
413 packet_write_fmt(1, "ACK %s common\n", last_hex
);
415 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
416 else if (have_obj
->nr
== 1)
417 packet_write_fmt(1, "ACK %s\n", last_hex
);
422 if (!strcmp(line
, "done")) {
423 if (have_obj
->nr
> 0) {
425 packet_write_fmt(1, "ACK %s\n", last_hex
);
428 packet_write_fmt(1, "NAK\n");
431 die("git upload-pack: expected SHA1 list, got '%s'", line
);
435 static int is_our_ref(struct object
*o
)
437 int allow_hidden_ref
= (allow_unadvertised_object_request
&
438 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
439 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
443 * on successful case, it's up to the caller to close cmd->out
445 static int do_reachable_revlist(struct child_process
*cmd
,
446 struct object_array
*src
,
447 struct object_array
*reachable
)
449 static const char *argv
[] = {
450 "rev-list", "--stdin", NULL
,
453 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
455 const unsigned hexsz
= the_hash_algo
->hexsz
;
464 * If the next rev-list --stdin encounters an unknown commit,
465 * it terminates, which will cause SIGPIPE in the write loop
468 sigchain_push(SIGPIPE
, SIG_IGN
);
470 if (start_command(cmd
))
474 namebuf
[hexsz
+ 1] = '\n';
475 for (i
= get_max_object_index(); 0 < i
; ) {
476 o
= get_indexed_object(--i
);
479 if (reachable
&& o
->type
== OBJ_COMMIT
)
480 o
->flags
&= ~TMP_MARK
;
483 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
484 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
487 namebuf
[hexsz
] = '\n';
488 for (i
= 0; i
< src
->nr
; i
++) {
489 o
= src
->objects
[i
].item
;
492 add_object_array(o
, NULL
, reachable
);
495 if (reachable
&& o
->type
== OBJ_COMMIT
)
496 o
->flags
|= TMP_MARK
;
497 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
498 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
503 sigchain_pop(SIGPIPE
);
508 sigchain_pop(SIGPIPE
);
517 static int get_reachable_list(struct object_array
*src
,
518 struct object_array
*reachable
)
520 struct child_process cmd
= CHILD_PROCESS_INIT
;
523 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
524 const unsigned hexsz
= the_hash_algo
->hexsz
;
526 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
529 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
530 struct object_id sha1
;
533 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
536 o
= lookup_object(the_repository
, sha1
.hash
);
537 if (o
&& o
->type
== OBJ_COMMIT
) {
538 o
->flags
&= ~TMP_MARK
;
541 for (i
= get_max_object_index(); 0 < i
; i
--) {
542 o
= get_indexed_object(i
- 1);
543 if (o
&& o
->type
== OBJ_COMMIT
&&
544 (o
->flags
& TMP_MARK
)) {
545 add_object_array(o
, NULL
, reachable
);
546 o
->flags
&= ~TMP_MARK
;
551 if (finish_command(&cmd
))
557 static int has_unreachable(struct object_array
*src
)
559 struct child_process cmd
= CHILD_PROCESS_INIT
;
563 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
567 * The commits out of the rev-list are not ancestors of
570 i
= read_in_full(cmd
.out
, buf
, 1);
577 * rev-list may have died by encountering a bad commit
578 * in the history, in which case we do want to bail out
579 * even when it showed no commit.
581 if (finish_command(&cmd
))
584 /* All the non-tip ones are ancestors of what we advertised */
588 sigchain_pop(SIGPIPE
);
594 static void check_non_tip(struct object_array
*want_obj
)
599 * In the normal in-process case without
600 * uploadpack.allowReachableSHA1InWant,
601 * non-tip requests can never happen.
603 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
605 if (!has_unreachable(want_obj
))
606 /* All the non-tip ones are ancestors of what we advertised */
610 /* Pick one of them (we know there at least is one) */
611 for (i
= 0; i
< want_obj
->nr
; i
++) {
612 struct object
*o
= want_obj
->objects
[i
].item
;
614 die("git upload-pack: not our ref %s",
615 oid_to_hex(&o
->oid
));
619 static void send_shallow(struct commit_list
*result
)
622 struct object
*object
= &result
->item
->object
;
623 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
624 packet_write_fmt(1, "shallow %s",
625 oid_to_hex(&object
->oid
));
626 register_shallow(the_repository
, &object
->oid
);
629 result
= result
->next
;
633 static void send_unshallow(const struct object_array
*shallows
,
634 struct object_array
*want_obj
)
638 for (i
= 0; i
< shallows
->nr
; i
++) {
639 struct object
*object
= shallows
->objects
[i
].item
;
640 if (object
->flags
& NOT_SHALLOW
) {
641 struct commit_list
*parents
;
642 packet_write_fmt(1, "unshallow %s",
643 oid_to_hex(&object
->oid
));
644 object
->flags
&= ~CLIENT_SHALLOW
;
646 * We want to _register_ "object" as shallow, but we
647 * also need to traverse object's parents to deepen a
648 * shallow clone. Unregister it for now so we can
649 * parse and add the parents to the want list, then
652 unregister_shallow(&object
->oid
);
654 parse_commit_or_die((struct commit
*)object
);
655 parents
= ((struct commit
*)object
)->parents
;
657 add_object_array(&parents
->item
->object
,
659 parents
= parents
->next
;
661 add_object_array(object
, NULL
, &extra_edge_obj
);
663 /* make sure commit traversal conforms to client */
664 register_shallow(the_repository
, &object
->oid
);
668 static void deepen(int depth
, int deepen_relative
,
669 struct object_array
*shallows
, struct object_array
*want_obj
)
671 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
674 for (i
= 0; i
< shallows
->nr
; i
++) {
675 struct object
*object
= shallows
->objects
[i
].item
;
676 object
->flags
|= NOT_SHALLOW
;
678 } else if (deepen_relative
) {
679 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
680 struct commit_list
*result
;
682 get_reachable_list(shallows
, &reachable_shallows
);
683 result
= get_shallow_commits(&reachable_shallows
,
685 SHALLOW
, NOT_SHALLOW
);
686 send_shallow(result
);
687 free_commit_list(result
);
688 object_array_clear(&reachable_shallows
);
690 struct commit_list
*result
;
692 result
= get_shallow_commits(want_obj
, depth
,
693 SHALLOW
, NOT_SHALLOW
);
694 send_shallow(result
);
695 free_commit_list(result
);
698 send_unshallow(shallows
, want_obj
);
701 static void deepen_by_rev_list(int ac
, const char **av
,
702 struct object_array
*shallows
,
703 struct object_array
*want_obj
)
705 struct commit_list
*result
;
707 close_commit_graph(the_repository
);
708 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
709 send_shallow(result
);
710 free_commit_list(result
);
711 send_unshallow(shallows
, want_obj
);
714 /* Returns 1 if a shallow list is sent or 0 otherwise */
715 static int send_shallow_list(int depth
, int deepen_rev_list
,
716 timestamp_t deepen_since
,
717 struct string_list
*deepen_not
,
718 struct object_array
*shallows
,
719 struct object_array
*want_obj
)
723 if (depth
> 0 && deepen_rev_list
)
724 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
726 deepen(depth
, deepen_relative
, shallows
, want_obj
);
728 } else if (deepen_rev_list
) {
729 struct argv_array av
= ARGV_ARRAY_INIT
;
732 argv_array_push(&av
, "rev-list");
734 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
735 if (deepen_not
->nr
) {
736 argv_array_push(&av
, "--not");
737 for (i
= 0; i
< deepen_not
->nr
; i
++) {
738 struct string_list_item
*s
= deepen_not
->items
+ i
;
739 argv_array_push(&av
, s
->string
);
741 argv_array_push(&av
, "--not");
743 for (i
= 0; i
< want_obj
->nr
; i
++) {
744 struct object
*o
= want_obj
->objects
[i
].item
;
745 argv_array_push(&av
, oid_to_hex(&o
->oid
));
747 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
, want_obj
);
748 argv_array_clear(&av
);
751 if (shallows
->nr
> 0) {
753 for (i
= 0; i
< shallows
->nr
; i
++)
754 register_shallow(the_repository
,
755 &shallows
->objects
[i
].item
->oid
);
759 shallow_nr
+= shallows
->nr
;
763 static int process_shallow(const char *line
, struct object_array
*shallows
)
766 if (skip_prefix(line
, "shallow ", &arg
)) {
767 struct object_id oid
;
768 struct object
*object
;
769 if (get_oid_hex(arg
, &oid
))
770 die("invalid shallow line: %s", line
);
771 object
= parse_object(the_repository
, &oid
);
774 if (object
->type
!= OBJ_COMMIT
)
775 die("invalid shallow object %s", oid_to_hex(&oid
));
776 if (!(object
->flags
& CLIENT_SHALLOW
)) {
777 object
->flags
|= CLIENT_SHALLOW
;
778 add_object_array(object
, NULL
, shallows
);
786 static int process_deepen(const char *line
, int *depth
)
789 if (skip_prefix(line
, "deepen ", &arg
)) {
791 *depth
= (int)strtol(arg
, &end
, 0);
792 if (!end
|| *end
|| *depth
<= 0)
793 die("Invalid deepen: %s", line
);
800 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
803 if (skip_prefix(line
, "deepen-since ", &arg
)) {
805 *deepen_since
= parse_timestamp(arg
, &end
, 0);
806 if (!end
|| *end
|| !deepen_since
||
807 /* revisions.c's max_age -1 is special */
809 die("Invalid deepen-since: %s", line
);
810 *deepen_rev_list
= 1;
816 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
819 if (skip_prefix(line
, "deepen-not ", &arg
)) {
821 struct object_id oid
;
822 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
823 die("git upload-pack: ambiguous deepen-not: %s", line
);
824 string_list_append(deepen_not
, ref
);
826 *deepen_rev_list
= 1;
832 static void receive_needs(struct object_array
*want_obj
)
834 struct object_array shallows
= OBJECT_ARRAY_INIT
;
835 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
838 timestamp_t deepen_since
= 0;
839 int deepen_rev_list
= 0;
844 const char *features
;
845 struct object_id oid_buf
;
846 char *line
= packet_read_line(0, NULL
);
853 if (process_shallow(line
, &shallows
))
855 if (process_deepen(line
, &depth
))
857 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
859 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
862 if (skip_prefix(line
, "filter ", &arg
)) {
863 if (!filter_capability_requested
)
864 die("git upload-pack: filtering capability not negotiated");
865 parse_list_objects_filter(&filter_options
, arg
);
869 if (!skip_prefix(line
, "want ", &arg
) ||
870 parse_oid_hex(arg
, &oid_buf
, &features
))
871 die("git upload-pack: protocol error, "
872 "expected to get object ID, not '%s'", line
);
874 if (parse_feature_request(features
, "deepen-relative"))
876 if (parse_feature_request(features
, "multi_ack_detailed"))
878 else if (parse_feature_request(features
, "multi_ack"))
880 if (parse_feature_request(features
, "no-done"))
882 if (parse_feature_request(features
, "thin-pack"))
884 if (parse_feature_request(features
, "ofs-delta"))
886 if (parse_feature_request(features
, "side-band-64k"))
887 use_sideband
= LARGE_PACKET_MAX
;
888 else if (parse_feature_request(features
, "side-band"))
889 use_sideband
= DEFAULT_PACKET_MAX
;
890 if (parse_feature_request(features
, "no-progress"))
892 if (parse_feature_request(features
, "include-tag"))
894 if (allow_filter
&& parse_feature_request(features
, "filter"))
895 filter_capability_requested
= 1;
897 o
= parse_object(the_repository
, &oid_buf
);
900 "ERR upload-pack: not our ref %s",
901 oid_to_hex(&oid_buf
));
902 die("git upload-pack: not our ref %s",
903 oid_to_hex(&oid_buf
));
905 if (!(o
->flags
& WANTED
)) {
907 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
910 add_object_array(o
, NULL
, want_obj
);
915 * We have sent all our refs already, and the other end
916 * should have chosen out of them. When we are operating
917 * in the stateless RPC mode, however, their choice may
918 * have been based on the set of older refs advertised
919 * by another process that handled the initial request.
922 check_non_tip(want_obj
);
924 if (!use_sideband
&& daemon_mode
)
927 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
930 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
931 &deepen_not
, &shallows
, want_obj
))
933 object_array_clear(&shallows
);
936 /* return non-zero if the ref is hidden, otherwise 0 */
937 static int mark_our_ref(const char *refname
, const char *refname_full
,
938 const struct object_id
*oid
)
940 struct object
*o
= lookup_unknown_object(oid
->hash
);
942 if (ref_is_hidden(refname
, refname_full
)) {
943 o
->flags
|= HIDDEN_REF
;
950 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
951 int flag
, void *cb_data
)
953 const char *refname
= strip_namespace(refname_full
);
955 mark_our_ref(refname
, refname_full
, oid
);
959 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
961 struct string_list_item
*item
;
965 for_each_string_list_item(item
, symref
)
966 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
969 static int send_ref(const char *refname
, const struct object_id
*oid
,
970 int flag
, void *cb_data
)
972 static const char *capabilities
= "multi_ack thin-pack side-band"
973 " side-band-64k ofs-delta shallow deepen-since deepen-not"
974 " deepen-relative no-progress include-tag multi_ack_detailed";
975 const char *refname_nons
= strip_namespace(refname
);
976 struct object_id peeled
;
978 if (mark_our_ref(refname_nons
, refname
, oid
))
982 struct strbuf symref_info
= STRBUF_INIT
;
984 format_symref_info(&symref_info
, cb_data
);
985 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
986 oid_to_hex(oid
), refname_nons
,
988 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
989 " allow-tip-sha1-in-want" : "",
990 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
991 " allow-reachable-sha1-in-want" : "",
992 stateless_rpc
? " no-done" : "",
994 allow_filter
? " filter" : "",
995 git_user_agent_sanitized());
996 strbuf_release(&symref_info
);
998 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1000 capabilities
= NULL
;
1001 if (!peel_ref(refname
, &peeled
))
1002 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1006 static int find_symref(const char *refname
, const struct object_id
*oid
,
1007 int flag
, void *cb_data
)
1009 const char *symref_target
;
1010 struct string_list_item
*item
;
1012 if ((flag
& REF_ISSYMREF
) == 0)
1014 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1015 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1016 die("'%s' is a symref but it is not?", refname
);
1017 item
= string_list_append(cb_data
, refname
);
1018 item
->util
= xstrdup(symref_target
);
1022 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1024 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1025 if (git_config_bool(var
, value
))
1026 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1028 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1029 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1030 if (git_config_bool(var
, value
))
1031 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1033 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1034 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1035 if (git_config_bool(var
, value
))
1036 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1038 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1039 } else if (!strcmp("uploadpack.keepalive", var
)) {
1040 keepalive
= git_config_int(var
, value
);
1043 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1044 allow_filter
= git_config_bool(var
, value
);
1045 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1046 allow_ref_in_want
= git_config_bool(var
, value
);
1049 if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1050 if (!strcmp("uploadpack.packobjectshook", var
))
1051 return git_config_string(&pack_objects_hook
, var
, value
);
1054 return parse_hide_refs_config(var
, value
, "uploadpack");
1057 void upload_pack(struct upload_pack_options
*options
)
1059 struct string_list symref
= STRING_LIST_INIT_DUP
;
1060 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1062 stateless_rpc
= options
->stateless_rpc
;
1063 timeout
= options
->timeout
;
1064 daemon_mode
= options
->daemon_mode
;
1066 git_config(upload_pack_config
, NULL
);
1068 head_ref_namespaced(find_symref
, &symref
);
1070 if (options
->advertise_refs
|| !stateless_rpc
) {
1072 head_ref_namespaced(send_ref
, &symref
);
1073 for_each_namespaced_ref(send_ref
, &symref
);
1074 advertise_shallow_grafts(1);
1077 head_ref_namespaced(check_ref
, NULL
);
1078 for_each_namespaced_ref(check_ref
, NULL
);
1080 string_list_clear(&symref
, 1);
1081 if (options
->advertise_refs
)
1084 receive_needs(&want_obj
);
1086 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1087 get_common_commits(&have_obj
, &want_obj
);
1088 create_pack_file(&have_obj
, &want_obj
);
1092 struct upload_pack_data
{
1093 struct object_array wants
;
1094 struct string_list wanted_refs
;
1095 struct oid_array haves
;
1097 struct object_array shallows
;
1098 struct string_list deepen_not
;
1100 timestamp_t deepen_since
;
1101 int deepen_rev_list
;
1102 int deepen_relative
;
1104 unsigned stateless_rpc
: 1;
1106 unsigned use_thin_pack
: 1;
1107 unsigned use_ofs_delta
: 1;
1108 unsigned no_progress
: 1;
1109 unsigned use_include_tag
: 1;
1113 static void upload_pack_data_init(struct upload_pack_data
*data
)
1115 struct object_array wants
= OBJECT_ARRAY_INIT
;
1116 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1117 struct oid_array haves
= OID_ARRAY_INIT
;
1118 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1119 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1121 memset(data
, 0, sizeof(*data
));
1122 data
->wants
= wants
;
1123 data
->wanted_refs
= wanted_refs
;
1124 data
->haves
= haves
;
1125 data
->shallows
= shallows
;
1126 data
->deepen_not
= deepen_not
;
1129 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1131 object_array_clear(&data
->wants
);
1132 string_list_clear(&data
->wanted_refs
, 1);
1133 oid_array_clear(&data
->haves
);
1134 object_array_clear(&data
->shallows
);
1135 string_list_clear(&data
->deepen_not
, 0);
1138 static int parse_want(const char *line
, struct object_array
*want_obj
)
1141 if (skip_prefix(line
, "want ", &arg
)) {
1142 struct object_id oid
;
1145 if (get_oid_hex(arg
, &oid
))
1146 die("git upload-pack: protocol error, "
1147 "expected to get oid, not '%s'", line
);
1149 o
= parse_object(the_repository
, &oid
);
1152 "ERR upload-pack: not our ref %s",
1154 die("git upload-pack: not our ref %s",
1158 if (!(o
->flags
& WANTED
)) {
1160 add_object_array(o
, NULL
, want_obj
);
1169 static int parse_want_ref(const char *line
, struct string_list
*wanted_refs
,
1170 struct object_array
*want_obj
)
1173 if (skip_prefix(line
, "want-ref ", &arg
)) {
1174 struct object_id oid
;
1175 struct string_list_item
*item
;
1178 if (read_ref(arg
, &oid
)) {
1179 packet_write_fmt(1, "ERR unknown ref %s", arg
);
1180 die("unknown ref %s", arg
);
1183 item
= string_list_append(wanted_refs
, arg
);
1184 item
->util
= oiddup(&oid
);
1186 o
= parse_object_or_die(&oid
, arg
);
1187 if (!(o
->flags
& WANTED
)) {
1189 add_object_array(o
, NULL
, want_obj
);
1198 static int parse_have(const char *line
, struct oid_array
*haves
)
1201 if (skip_prefix(line
, "have ", &arg
)) {
1202 struct object_id oid
;
1204 if (get_oid_hex(arg
, &oid
))
1205 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1206 oid_array_append(haves
, &oid
);
1213 static void process_args(struct packet_reader
*request
,
1214 struct upload_pack_data
*data
,
1215 struct object_array
*want_obj
)
1217 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1218 const char *arg
= request
->line
;
1222 if (parse_want(arg
, want_obj
))
1224 if (allow_ref_in_want
&&
1225 parse_want_ref(arg
, &data
->wanted_refs
, want_obj
))
1227 /* process have line */
1228 if (parse_have(arg
, &data
->haves
))
1231 /* process args like thin-pack */
1232 if (!strcmp(arg
, "thin-pack")) {
1236 if (!strcmp(arg
, "ofs-delta")) {
1240 if (!strcmp(arg
, "no-progress")) {
1244 if (!strcmp(arg
, "include-tag")) {
1245 use_include_tag
= 1;
1248 if (!strcmp(arg
, "done")) {
1253 /* Shallow related arguments */
1254 if (process_shallow(arg
, &data
->shallows
))
1256 if (process_deepen(arg
, &data
->depth
))
1258 if (process_deepen_since(arg
, &data
->deepen_since
,
1259 &data
->deepen_rev_list
))
1261 if (process_deepen_not(arg
, &data
->deepen_not
,
1262 &data
->deepen_rev_list
))
1264 if (!strcmp(arg
, "deepen-relative")) {
1265 data
->deepen_relative
= 1;
1269 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1270 parse_list_objects_filter(&filter_options
, p
);
1274 /* ignore unknown lines maybe? */
1275 die("unexpected line: '%s'", arg
);
1279 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
,
1280 struct object_array
*have_obj
)
1285 for (i
= 0; i
< haves
->nr
; i
++) {
1286 const struct object_id
*oid
= &haves
->oid
[i
];
1288 int we_knew_they_have
= 0;
1290 if (!has_object_file(oid
))
1293 oid_array_append(common
, oid
);
1295 o
= parse_object(the_repository
, oid
);
1297 die("oops (%s)", oid_to_hex(oid
));
1298 if (o
->type
== OBJ_COMMIT
) {
1299 struct commit_list
*parents
;
1300 struct commit
*commit
= (struct commit
*)o
;
1301 if (o
->flags
& THEY_HAVE
)
1302 we_knew_they_have
= 1;
1304 o
->flags
|= THEY_HAVE
;
1305 if (!oldest_have
|| (commit
->date
< oldest_have
))
1306 oldest_have
= commit
->date
;
1307 for (parents
= commit
->parents
;
1309 parents
= parents
->next
)
1310 parents
->item
->object
.flags
|= THEY_HAVE
;
1312 if (!we_knew_they_have
)
1313 add_object_array(o
, NULL
, have_obj
);
1319 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
,
1320 const struct object_array
*have_obj
,
1321 struct object_array
*want_obj
)
1325 packet_buf_write(response
, "acknowledgments\n");
1329 packet_buf_write(response
, "NAK\n");
1331 for (i
= 0; i
< acks
->nr
; i
++) {
1332 packet_buf_write(response
, "ACK %s\n",
1333 oid_to_hex(&acks
->oid
[i
]));
1336 if (ok_to_give_up(have_obj
, want_obj
)) {
1338 packet_buf_write(response
, "ready\n");
1345 static int process_haves_and_send_acks(struct upload_pack_data
*data
,
1346 struct object_array
*have_obj
,
1347 struct object_array
*want_obj
)
1349 struct oid_array common
= OID_ARRAY_INIT
;
1350 struct strbuf response
= STRBUF_INIT
;
1353 process_haves(&data
->haves
, &common
, have_obj
);
1356 } else if (send_acks(&common
, &response
, have_obj
, want_obj
)) {
1357 packet_buf_delim(&response
);
1361 packet_buf_flush(&response
);
1366 write_or_die(1, response
.buf
, response
.len
);
1367 strbuf_release(&response
);
1369 oid_array_clear(&data
->haves
);
1370 oid_array_clear(&common
);
1374 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1376 const struct string_list_item
*item
;
1378 if (!data
->wanted_refs
.nr
)
1381 packet_write_fmt(1, "wanted-refs\n");
1383 for_each_string_list_item(item
, &data
->wanted_refs
) {
1384 packet_write_fmt(1, "%s %s\n",
1385 oid_to_hex(item
->util
),
1392 static void send_shallow_info(struct upload_pack_data
*data
,
1393 struct object_array
*want_obj
)
1395 /* No shallow info needs to be sent */
1396 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1397 !is_repository_shallow(the_repository
))
1400 packet_write_fmt(1, "shallow-info\n");
1402 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1403 data
->deepen_since
, &data
->deepen_not
,
1404 &data
->shallows
, want_obj
) &&
1405 is_repository_shallow(the_repository
))
1406 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
,
1413 FETCH_PROCESS_ARGS
= 0,
1419 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1420 struct packet_reader
*request
)
1422 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1423 struct upload_pack_data data
;
1424 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1425 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1427 clear_object_flags(ALL_FLAGS
);
1429 git_config(upload_pack_config
, NULL
);
1431 upload_pack_data_init(&data
);
1432 use_sideband
= LARGE_PACKET_MAX
;
1434 while (state
!= FETCH_DONE
) {
1436 case FETCH_PROCESS_ARGS
:
1437 process_args(request
, &data
, &want_obj
);
1441 * Request didn't contain any 'want' lines,
1442 * guess they didn't want anything.
1445 } else if (data
.haves
.nr
) {
1447 * Request had 'have' lines, so lets ACK them.
1449 state
= FETCH_SEND_ACKS
;
1452 * Request had 'want's but no 'have's so we can
1453 * immedietly go to construct and send a pack.
1455 state
= FETCH_SEND_PACK
;
1458 case FETCH_SEND_ACKS
:
1459 if (process_haves_and_send_acks(&data
, &have_obj
,
1461 state
= FETCH_SEND_PACK
;
1465 case FETCH_SEND_PACK
:
1466 send_wanted_ref_info(&data
);
1467 send_shallow_info(&data
, &want_obj
);
1469 packet_write_fmt(1, "packfile\n");
1470 create_pack_file(&have_obj
, &want_obj
);
1478 upload_pack_data_clear(&data
);
1479 object_array_clear(&have_obj
);
1480 object_array_clear(&want_obj
);
1484 int upload_pack_advertise(struct repository
*r
,
1485 struct strbuf
*value
)
1488 int allow_filter_value
;
1489 int allow_ref_in_want
;
1491 strbuf_addstr(value
, "shallow");
1493 if (!repo_config_get_bool(the_repository
,
1494 "uploadpack.allowfilter",
1495 &allow_filter_value
) &&
1497 strbuf_addstr(value
, " filter");
1499 if (!repo_config_get_bool(the_repository
,
1500 "uploadpack.allowrefinwant",
1501 &allow_ref_in_want
) &&
1503 strbuf_addstr(value
, " ref-in-want");