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 int allow_sideband_all
;
76 static void reset_timeout(void)
81 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
84 send_sideband(1, fd
, data
, sz
, use_sideband
);
91 /* XXX: are we happy to lose stuff here? */
95 write_or_die(fd
, data
, sz
);
98 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
101 if (graft
->nr_parent
== -1)
102 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
106 static void create_pack_file(const struct object_array
*have_obj
,
107 const struct object_array
*want_obj
)
109 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
110 char data
[8193], progress
[128];
111 char abort_msg
[] = "aborting due to possible repository "
112 "corruption on the remote side.";
118 if (!pack_objects_hook
)
119 pack_objects
.git_cmd
= 1;
121 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
122 argv_array_push(&pack_objects
.args
, "git");
123 pack_objects
.use_shell
= 1;
127 argv_array_push(&pack_objects
.args
, "--shallow-file");
128 argv_array_push(&pack_objects
.args
, "");
130 argv_array_push(&pack_objects
.args
, "pack-objects");
131 argv_array_push(&pack_objects
.args
, "--revs");
133 argv_array_push(&pack_objects
.args
, "--thin");
135 argv_array_push(&pack_objects
.args
, "--stdout");
137 argv_array_push(&pack_objects
.args
, "--shallow");
139 argv_array_push(&pack_objects
.args
, "--progress");
141 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
143 argv_array_push(&pack_objects
.args
, "--include-tag");
144 if (filter_options
.filter_spec
) {
145 if (pack_objects
.use_shell
) {
146 struct strbuf buf
= STRBUF_INIT
;
147 sq_quote_buf(&buf
, filter_options
.filter_spec
);
148 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
149 strbuf_release(&buf
);
151 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
152 filter_options
.filter_spec
);
156 pack_objects
.in
= -1;
157 pack_objects
.out
= -1;
158 pack_objects
.err
= -1;
160 if (start_command(&pack_objects
))
161 die("git upload-pack: unable to fork git-pack-objects");
163 pipe_fd
= xfdopen(pack_objects
.in
, "w");
166 for_each_commit_graft(write_one_shallow
, pipe_fd
);
168 for (i
= 0; i
< want_obj
->nr
; i
++)
169 fprintf(pipe_fd
, "%s\n",
170 oid_to_hex(&want_obj
->objects
[i
].item
->oid
));
171 fprintf(pipe_fd
, "--not\n");
172 for (i
= 0; i
< have_obj
->nr
; i
++)
173 fprintf(pipe_fd
, "%s\n",
174 oid_to_hex(&have_obj
->objects
[i
].item
->oid
));
175 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
176 fprintf(pipe_fd
, "%s\n",
177 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
178 fprintf(pipe_fd
, "\n");
182 /* We read from pack_objects.err to capture stderr output for
183 * progress bar, and pack_objects.out to capture the pack data.
187 struct pollfd pfd
[2];
188 int pe
, pu
, pollsize
;
196 if (0 <= pack_objects
.out
) {
197 pfd
[pollsize
].fd
= pack_objects
.out
;
198 pfd
[pollsize
].events
= POLLIN
;
202 if (0 <= pack_objects
.err
) {
203 pfd
[pollsize
].fd
= pack_objects
.err
;
204 pfd
[pollsize
].events
= POLLIN
;
212 ret
= poll(pfd
, pollsize
,
213 keepalive
< 0 ? -1 : 1000 * keepalive
);
216 if (errno
!= EINTR
) {
217 error_errno("poll failed, resuming");
222 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
223 /* Status ready; we ship that in the side-band
224 * or dump to the standard error.
226 sz
= xread(pack_objects
.err
, progress
,
229 send_client_data(2, progress
, sz
);
231 close(pack_objects
.err
);
232 pack_objects
.err
= -1;
236 /* give priority to status messages */
239 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
240 /* Data ready; we keep the last byte to ourselves
241 * in case we detect broken rev-list, so that we
242 * can leave the stream corrupted. This is
243 * unfortunate -- unpack-objects would happily
244 * accept a valid packdata with trailing garbage,
245 * so appending garbage after we pass all the
246 * pack data is not good enough to signal
247 * breakage to downstream.
255 sz
= xread(pack_objects
.out
, cp
,
256 sizeof(data
) - outsz
);
260 close(pack_objects
.out
);
261 pack_objects
.out
= -1;
267 buffered
= data
[sz
-1] & 0xFF;
272 send_client_data(1, data
, sz
);
276 * We hit the keepalive timeout without saying anything; send
277 * an empty message on the data sideband just to let the other
278 * side know we're still working on it, but don't have any data
281 * If we don't have a sideband channel, there's no room in the
282 * protocol to say anything, so those clients are just out of
285 if (!ret
&& use_sideband
) {
286 static const char buf
[] = "0005\1";
287 write_or_die(1, buf
, 5);
291 if (finish_command(&pack_objects
)) {
292 error("git upload-pack: git-pack-objects died with error.");
299 send_client_data(1, data
, 1);
300 fprintf(stderr
, "flushed.\n");
307 send_client_data(3, abort_msg
, sizeof(abort_msg
));
308 die("git upload-pack: %s", abort_msg
);
311 static int got_oid(const char *hex
, struct object_id
*oid
,
312 struct object_array
*have_obj
)
315 int we_knew_they_have
= 0;
317 if (get_oid_hex(hex
, oid
))
318 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
319 if (!has_object_file(oid
))
322 o
= parse_object(the_repository
, oid
);
324 die("oops (%s)", oid_to_hex(oid
));
325 if (o
->type
== OBJ_COMMIT
) {
326 struct commit_list
*parents
;
327 struct commit
*commit
= (struct commit
*)o
;
328 if (o
->flags
& THEY_HAVE
)
329 we_knew_they_have
= 1;
331 o
->flags
|= THEY_HAVE
;
332 if (!oldest_have
|| (commit
->date
< oldest_have
))
333 oldest_have
= commit
->date
;
334 for (parents
= commit
->parents
;
336 parents
= parents
->next
)
337 parents
->item
->object
.flags
|= THEY_HAVE
;
339 if (!we_knew_they_have
) {
340 add_object_array(o
, NULL
, have_obj
);
346 static int ok_to_give_up(const struct object_array
*have_obj
,
347 struct object_array
*want_obj
)
349 uint32_t min_generation
= GENERATION_NUMBER_ZERO
;
354 return can_all_from_reach_with_flag(want_obj
, THEY_HAVE
,
355 COMMON_KNOWN
, oldest_have
,
359 static int get_common_commits(struct packet_reader
*reader
,
360 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;
376 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
) {
377 if (multi_ack
== 2 && got_common
378 && !got_other
&& ok_to_give_up(have_obj
, want_obj
)) {
380 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
382 if (have_obj
->nr
== 0 || multi_ack
)
383 packet_write_fmt(1, "NAK\n");
385 if (no_done
&& sent_ready
) {
386 packet_write_fmt(1, "ACK %s\n", last_hex
);
395 if (skip_prefix(reader
->line
, "have ", &arg
)) {
396 switch (got_oid(arg
, &oid
, have_obj
)) {
397 case -1: /* they have what we do not */
399 if (multi_ack
&& ok_to_give_up(have_obj
, want_obj
)) {
400 const char *hex
= oid_to_hex(&oid
);
401 if (multi_ack
== 2) {
403 packet_write_fmt(1, "ACK %s ready\n", hex
);
405 packet_write_fmt(1, "ACK %s continue\n", hex
);
410 oid_to_hex_r(last_hex
, &oid
);
412 packet_write_fmt(1, "ACK %s common\n", last_hex
);
414 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
415 else if (have_obj
->nr
== 1)
416 packet_write_fmt(1, "ACK %s\n", last_hex
);
421 if (!strcmp(reader
->line
, "done")) {
422 if (have_obj
->nr
> 0) {
424 packet_write_fmt(1, "ACK %s\n", last_hex
);
427 packet_write_fmt(1, "NAK\n");
430 die("git upload-pack: expected SHA1 list, got '%s'", reader
->line
);
434 static int is_our_ref(struct object
*o
)
436 int allow_hidden_ref
= (allow_unadvertised_object_request
&
437 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
438 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
442 * on successful case, it's up to the caller to close cmd->out
444 static int do_reachable_revlist(struct child_process
*cmd
,
445 struct object_array
*src
,
446 struct object_array
*reachable
)
448 static const char *argv
[] = {
449 "rev-list", "--stdin", NULL
,
452 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
454 const unsigned hexsz
= the_hash_algo
->hexsz
;
463 * If the next rev-list --stdin encounters an unknown commit,
464 * it terminates, which will cause SIGPIPE in the write loop
467 sigchain_push(SIGPIPE
, SIG_IGN
);
469 if (start_command(cmd
))
473 namebuf
[hexsz
+ 1] = '\n';
474 for (i
= get_max_object_index(); 0 < i
; ) {
475 o
= get_indexed_object(--i
);
478 if (reachable
&& o
->type
== OBJ_COMMIT
)
479 o
->flags
&= ~TMP_MARK
;
482 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), hexsz
);
483 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 2) < 0)
486 namebuf
[hexsz
] = '\n';
487 for (i
= 0; i
< src
->nr
; i
++) {
488 o
= src
->objects
[i
].item
;
491 add_object_array(o
, NULL
, reachable
);
494 if (reachable
&& o
->type
== OBJ_COMMIT
)
495 o
->flags
|= TMP_MARK
;
496 memcpy(namebuf
, oid_to_hex(&o
->oid
), hexsz
);
497 if (write_in_full(cmd
->in
, namebuf
, hexsz
+ 1) < 0)
502 sigchain_pop(SIGPIPE
);
507 sigchain_pop(SIGPIPE
);
516 static int get_reachable_list(struct object_array
*src
,
517 struct object_array
*reachable
)
519 struct child_process cmd
= CHILD_PROCESS_INIT
;
522 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
523 const unsigned hexsz
= the_hash_algo
->hexsz
;
525 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
528 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
529 struct object_id sha1
;
532 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
535 o
= lookup_object(the_repository
, sha1
.hash
);
536 if (o
&& o
->type
== OBJ_COMMIT
) {
537 o
->flags
&= ~TMP_MARK
;
540 for (i
= get_max_object_index(); 0 < i
; i
--) {
541 o
= get_indexed_object(i
- 1);
542 if (o
&& o
->type
== OBJ_COMMIT
&&
543 (o
->flags
& TMP_MARK
)) {
544 add_object_array(o
, NULL
, reachable
);
545 o
->flags
&= ~TMP_MARK
;
550 if (finish_command(&cmd
))
556 static int has_unreachable(struct object_array
*src
)
558 struct child_process cmd
= CHILD_PROCESS_INIT
;
562 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
566 * The commits out of the rev-list are not ancestors of
569 i
= read_in_full(cmd
.out
, buf
, 1);
576 * rev-list may have died by encountering a bad commit
577 * in the history, in which case we do want to bail out
578 * even when it showed no commit.
580 if (finish_command(&cmd
))
583 /* All the non-tip ones are ancestors of what we advertised */
587 sigchain_pop(SIGPIPE
);
593 static void check_non_tip(struct object_array
*want_obj
)
598 * In the normal in-process case without
599 * uploadpack.allowReachableSHA1InWant,
600 * non-tip requests can never happen.
602 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
604 if (!has_unreachable(want_obj
))
605 /* All the non-tip ones are ancestors of what we advertised */
609 /* Pick one of them (we know there at least is one) */
610 for (i
= 0; i
< want_obj
->nr
; i
++) {
611 struct object
*o
= want_obj
->objects
[i
].item
;
613 die("git upload-pack: not our ref %s",
614 oid_to_hex(&o
->oid
));
618 static void send_shallow(struct packet_writer
*writer
,
619 struct commit_list
*result
)
622 struct object
*object
= &result
->item
->object
;
623 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
624 packet_writer_write(writer
, "shallow %s",
625 oid_to_hex(&object
->oid
));
626 register_shallow(the_repository
, &object
->oid
);
629 result
= result
->next
;
633 static void send_unshallow(struct packet_writer
*writer
,
634 const struct object_array
*shallows
,
635 struct object_array
*want_obj
)
639 for (i
= 0; i
< shallows
->nr
; i
++) {
640 struct object
*object
= shallows
->objects
[i
].item
;
641 if (object
->flags
& NOT_SHALLOW
) {
642 struct commit_list
*parents
;
643 packet_writer_write(writer
, "unshallow %s",
644 oid_to_hex(&object
->oid
));
645 object
->flags
&= ~CLIENT_SHALLOW
;
647 * We want to _register_ "object" as shallow, but we
648 * also need to traverse object's parents to deepen a
649 * shallow clone. Unregister it for now so we can
650 * parse and add the parents to the want list, then
653 unregister_shallow(&object
->oid
);
655 parse_commit_or_die((struct commit
*)object
);
656 parents
= ((struct commit
*)object
)->parents
;
658 add_object_array(&parents
->item
->object
,
660 parents
= parents
->next
;
662 add_object_array(object
, NULL
, &extra_edge_obj
);
664 /* make sure commit traversal conforms to client */
665 register_shallow(the_repository
, &object
->oid
);
669 static void deepen(struct packet_writer
*writer
, int depth
, int deepen_relative
,
670 struct object_array
*shallows
, struct object_array
*want_obj
)
672 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
675 for (i
= 0; i
< shallows
->nr
; i
++) {
676 struct object
*object
= shallows
->objects
[i
].item
;
677 object
->flags
|= NOT_SHALLOW
;
679 } else if (deepen_relative
) {
680 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
681 struct commit_list
*result
;
683 get_reachable_list(shallows
, &reachable_shallows
);
684 result
= get_shallow_commits(&reachable_shallows
,
686 SHALLOW
, NOT_SHALLOW
);
687 send_shallow(writer
, result
);
688 free_commit_list(result
);
689 object_array_clear(&reachable_shallows
);
691 struct commit_list
*result
;
693 result
= get_shallow_commits(want_obj
, depth
,
694 SHALLOW
, NOT_SHALLOW
);
695 send_shallow(writer
, result
);
696 free_commit_list(result
);
699 send_unshallow(writer
, shallows
, want_obj
);
702 static void deepen_by_rev_list(struct packet_writer
*writer
, int ac
,
704 struct object_array
*shallows
,
705 struct object_array
*want_obj
)
707 struct commit_list
*result
;
709 close_commit_graph(the_repository
);
710 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
711 send_shallow(writer
, result
);
712 free_commit_list(result
);
713 send_unshallow(writer
, shallows
, want_obj
);
716 /* Returns 1 if a shallow list is sent or 0 otherwise */
717 static int send_shallow_list(struct packet_writer
*writer
,
718 int depth
, int deepen_rev_list
,
719 timestamp_t deepen_since
,
720 struct string_list
*deepen_not
,
721 struct object_array
*shallows
,
722 struct object_array
*want_obj
)
726 if (depth
> 0 && deepen_rev_list
)
727 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
729 deepen(writer
, depth
, deepen_relative
, shallows
, want_obj
);
731 } else if (deepen_rev_list
) {
732 struct argv_array av
= ARGV_ARRAY_INIT
;
735 argv_array_push(&av
, "rev-list");
737 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
738 if (deepen_not
->nr
) {
739 argv_array_push(&av
, "--not");
740 for (i
= 0; i
< deepen_not
->nr
; i
++) {
741 struct string_list_item
*s
= deepen_not
->items
+ i
;
742 argv_array_push(&av
, s
->string
);
744 argv_array_push(&av
, "--not");
746 for (i
= 0; i
< want_obj
->nr
; i
++) {
747 struct object
*o
= want_obj
->objects
[i
].item
;
748 argv_array_push(&av
, oid_to_hex(&o
->oid
));
750 deepen_by_rev_list(writer
, av
.argc
, av
.argv
, shallows
, want_obj
);
751 argv_array_clear(&av
);
754 if (shallows
->nr
> 0) {
756 for (i
= 0; i
< shallows
->nr
; i
++)
757 register_shallow(the_repository
,
758 &shallows
->objects
[i
].item
->oid
);
762 shallow_nr
+= shallows
->nr
;
766 static int process_shallow(const char *line
, struct object_array
*shallows
)
769 if (skip_prefix(line
, "shallow ", &arg
)) {
770 struct object_id oid
;
771 struct object
*object
;
772 if (get_oid_hex(arg
, &oid
))
773 die("invalid shallow line: %s", line
);
774 object
= parse_object(the_repository
, &oid
);
777 if (object
->type
!= OBJ_COMMIT
)
778 die("invalid shallow object %s", oid_to_hex(&oid
));
779 if (!(object
->flags
& CLIENT_SHALLOW
)) {
780 object
->flags
|= CLIENT_SHALLOW
;
781 add_object_array(object
, NULL
, shallows
);
789 static int process_deepen(const char *line
, int *depth
)
792 if (skip_prefix(line
, "deepen ", &arg
)) {
794 *depth
= (int)strtol(arg
, &end
, 0);
795 if (!end
|| *end
|| *depth
<= 0)
796 die("Invalid deepen: %s", line
);
803 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
806 if (skip_prefix(line
, "deepen-since ", &arg
)) {
808 *deepen_since
= parse_timestamp(arg
, &end
, 0);
809 if (!end
|| *end
|| !deepen_since
||
810 /* revisions.c's max_age -1 is special */
812 die("Invalid deepen-since: %s", line
);
813 *deepen_rev_list
= 1;
819 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
822 if (skip_prefix(line
, "deepen-not ", &arg
)) {
824 struct object_id oid
;
825 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
826 die("git upload-pack: ambiguous deepen-not: %s", line
);
827 string_list_append(deepen_not
, ref
);
829 *deepen_rev_list
= 1;
835 static void receive_needs(struct packet_reader
*reader
, struct object_array
*want_obj
)
837 struct object_array shallows
= OBJECT_ARRAY_INIT
;
838 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
841 timestamp_t deepen_since
= 0;
842 int deepen_rev_list
= 0;
843 struct packet_writer writer
;
846 packet_writer_init(&writer
, 1);
849 const char *features
;
850 struct object_id oid_buf
;
854 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
857 if (process_shallow(reader
->line
, &shallows
))
859 if (process_deepen(reader
->line
, &depth
))
861 if (process_deepen_since(reader
->line
, &deepen_since
, &deepen_rev_list
))
863 if (process_deepen_not(reader
->line
, &deepen_not
, &deepen_rev_list
))
866 if (skip_prefix(reader
->line
, "filter ", &arg
)) {
867 if (!filter_capability_requested
)
868 die("git upload-pack: filtering capability not negotiated");
869 parse_list_objects_filter(&filter_options
, arg
);
873 if (!skip_prefix(reader
->line
, "want ", &arg
) ||
874 parse_oid_hex(arg
, &oid_buf
, &features
))
875 die("git upload-pack: protocol error, "
876 "expected to get object ID, not '%s'", reader
->line
);
878 if (parse_feature_request(features
, "deepen-relative"))
880 if (parse_feature_request(features
, "multi_ack_detailed"))
882 else if (parse_feature_request(features
, "multi_ack"))
884 if (parse_feature_request(features
, "no-done"))
886 if (parse_feature_request(features
, "thin-pack"))
888 if (parse_feature_request(features
, "ofs-delta"))
890 if (parse_feature_request(features
, "side-band-64k"))
891 use_sideband
= LARGE_PACKET_MAX
;
892 else if (parse_feature_request(features
, "side-band"))
893 use_sideband
= DEFAULT_PACKET_MAX
;
894 if (parse_feature_request(features
, "no-progress"))
896 if (parse_feature_request(features
, "include-tag"))
898 if (allow_filter
&& parse_feature_request(features
, "filter"))
899 filter_capability_requested
= 1;
901 o
= parse_object(the_repository
, &oid_buf
);
903 packet_writer_error(&writer
,
904 "upload-pack: not our ref %s",
905 oid_to_hex(&oid_buf
));
906 die("git upload-pack: not our ref %s",
907 oid_to_hex(&oid_buf
));
909 if (!(o
->flags
& WANTED
)) {
911 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
914 add_object_array(o
, NULL
, want_obj
);
919 * We have sent all our refs already, and the other end
920 * should have chosen out of them. When we are operating
921 * in the stateless RPC mode, however, their choice may
922 * have been based on the set of older refs advertised
923 * by another process that handled the initial request.
926 check_non_tip(want_obj
);
928 if (!use_sideband
&& daemon_mode
)
931 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
934 if (send_shallow_list(&writer
, depth
, deepen_rev_list
, deepen_since
,
935 &deepen_not
, &shallows
, want_obj
))
937 object_array_clear(&shallows
);
940 /* return non-zero if the ref is hidden, otherwise 0 */
941 static int mark_our_ref(const char *refname
, const char *refname_full
,
942 const struct object_id
*oid
)
944 struct object
*o
= lookup_unknown_object(oid
->hash
);
946 if (ref_is_hidden(refname
, refname_full
)) {
947 o
->flags
|= HIDDEN_REF
;
954 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
955 int flag
, void *cb_data
)
957 const char *refname
= strip_namespace(refname_full
);
959 mark_our_ref(refname
, refname_full
, oid
);
963 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
965 struct string_list_item
*item
;
969 for_each_string_list_item(item
, symref
)
970 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
973 static int send_ref(const char *refname
, const struct object_id
*oid
,
974 int flag
, void *cb_data
)
976 static const char *capabilities
= "multi_ack thin-pack side-band"
977 " side-band-64k ofs-delta shallow deepen-since deepen-not"
978 " deepen-relative no-progress include-tag multi_ack_detailed";
979 const char *refname_nons
= strip_namespace(refname
);
980 struct object_id peeled
;
982 if (mark_our_ref(refname_nons
, refname
, oid
))
986 struct strbuf symref_info
= STRBUF_INIT
;
988 format_symref_info(&symref_info
, cb_data
);
989 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
990 oid_to_hex(oid
), refname_nons
,
992 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
993 " allow-tip-sha1-in-want" : "",
994 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
995 " allow-reachable-sha1-in-want" : "",
996 stateless_rpc
? " no-done" : "",
998 allow_filter
? " filter" : "",
999 git_user_agent_sanitized());
1000 strbuf_release(&symref_info
);
1002 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1004 capabilities
= NULL
;
1005 if (!peel_ref(refname
, &peeled
))
1006 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1010 static int find_symref(const char *refname
, const struct object_id
*oid
,
1011 int flag
, void *cb_data
)
1013 const char *symref_target
;
1014 struct string_list_item
*item
;
1016 if ((flag
& REF_ISSYMREF
) == 0)
1018 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1019 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1020 die("'%s' is a symref but it is not?", refname
);
1021 item
= string_list_append(cb_data
, refname
);
1022 item
->util
= xstrdup(symref_target
);
1026 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1028 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1029 if (git_config_bool(var
, value
))
1030 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1032 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1033 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1034 if (git_config_bool(var
, value
))
1035 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1037 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1038 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1039 if (git_config_bool(var
, value
))
1040 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1042 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1043 } else if (!strcmp("uploadpack.keepalive", var
)) {
1044 keepalive
= git_config_int(var
, value
);
1047 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1048 allow_filter
= git_config_bool(var
, value
);
1049 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1050 allow_ref_in_want
= git_config_bool(var
, value
);
1051 } else if (!strcmp("uploadpack.allowsidebandall", var
)) {
1052 allow_sideband_all
= git_config_bool(var
, value
);
1055 if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1056 if (!strcmp("uploadpack.packobjectshook", var
))
1057 return git_config_string(&pack_objects_hook
, var
, value
);
1060 return parse_hide_refs_config(var
, value
, "uploadpack");
1063 void upload_pack(struct upload_pack_options
*options
)
1065 struct string_list symref
= STRING_LIST_INIT_DUP
;
1066 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1067 struct packet_reader reader
;
1069 stateless_rpc
= options
->stateless_rpc
;
1070 timeout
= options
->timeout
;
1071 daemon_mode
= options
->daemon_mode
;
1073 git_config(upload_pack_config
, NULL
);
1075 head_ref_namespaced(find_symref
, &symref
);
1077 if (options
->advertise_refs
|| !stateless_rpc
) {
1079 head_ref_namespaced(send_ref
, &symref
);
1080 for_each_namespaced_ref(send_ref
, &symref
);
1081 advertise_shallow_grafts(1);
1084 head_ref_namespaced(check_ref
, NULL
);
1085 for_each_namespaced_ref(check_ref
, NULL
);
1087 string_list_clear(&symref
, 1);
1088 if (options
->advertise_refs
)
1091 packet_reader_init(&reader
, 0, NULL
, 0,
1092 PACKET_READ_CHOMP_NEWLINE
|
1093 PACKET_READ_DIE_ON_ERR_PACKET
);
1095 receive_needs(&reader
, &want_obj
);
1097 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1098 get_common_commits(&reader
, &have_obj
, &want_obj
);
1099 create_pack_file(&have_obj
, &want_obj
);
1103 struct upload_pack_data
{
1104 struct object_array wants
;
1105 struct string_list wanted_refs
;
1106 struct oid_array haves
;
1108 struct object_array shallows
;
1109 struct string_list deepen_not
;
1111 timestamp_t deepen_since
;
1112 int deepen_rev_list
;
1113 int deepen_relative
;
1115 struct packet_writer writer
;
1117 unsigned stateless_rpc
: 1;
1119 unsigned use_thin_pack
: 1;
1120 unsigned use_ofs_delta
: 1;
1121 unsigned no_progress
: 1;
1122 unsigned use_include_tag
: 1;
1126 static void upload_pack_data_init(struct upload_pack_data
*data
)
1128 struct object_array wants
= OBJECT_ARRAY_INIT
;
1129 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1130 struct oid_array haves
= OID_ARRAY_INIT
;
1131 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1132 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1134 memset(data
, 0, sizeof(*data
));
1135 data
->wants
= wants
;
1136 data
->wanted_refs
= wanted_refs
;
1137 data
->haves
= haves
;
1138 data
->shallows
= shallows
;
1139 data
->deepen_not
= deepen_not
;
1140 packet_writer_init(&data
->writer
, 1);
1143 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1145 object_array_clear(&data
->wants
);
1146 string_list_clear(&data
->wanted_refs
, 1);
1147 oid_array_clear(&data
->haves
);
1148 object_array_clear(&data
->shallows
);
1149 string_list_clear(&data
->deepen_not
, 0);
1152 static int parse_want(struct packet_writer
*writer
, const char *line
,
1153 struct object_array
*want_obj
)
1156 if (skip_prefix(line
, "want ", &arg
)) {
1157 struct object_id oid
;
1160 if (get_oid_hex(arg
, &oid
))
1161 die("git upload-pack: protocol error, "
1162 "expected to get oid, not '%s'", line
);
1164 o
= parse_object(the_repository
, &oid
);
1166 packet_writer_error(writer
,
1167 "upload-pack: not our ref %s",
1169 die("git upload-pack: not our ref %s",
1173 if (!(o
->flags
& WANTED
)) {
1175 add_object_array(o
, NULL
, want_obj
);
1184 static int parse_want_ref(struct packet_writer
*writer
, const char *line
,
1185 struct string_list
*wanted_refs
,
1186 struct object_array
*want_obj
)
1189 if (skip_prefix(line
, "want-ref ", &arg
)) {
1190 struct object_id oid
;
1191 struct string_list_item
*item
;
1194 if (read_ref(arg
, &oid
)) {
1195 packet_writer_error(writer
, "unknown ref %s", arg
);
1196 die("unknown ref %s", arg
);
1199 item
= string_list_append(wanted_refs
, arg
);
1200 item
->util
= oiddup(&oid
);
1202 o
= parse_object_or_die(&oid
, arg
);
1203 if (!(o
->flags
& WANTED
)) {
1205 add_object_array(o
, NULL
, want_obj
);
1214 static int parse_have(const char *line
, struct oid_array
*haves
)
1217 if (skip_prefix(line
, "have ", &arg
)) {
1218 struct object_id oid
;
1220 if (get_oid_hex(arg
, &oid
))
1221 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1222 oid_array_append(haves
, &oid
);
1229 static void process_args(struct packet_reader
*request
,
1230 struct upload_pack_data
*data
,
1231 struct object_array
*want_obj
)
1233 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1234 const char *arg
= request
->line
;
1238 if (parse_want(&data
->writer
, arg
, want_obj
))
1240 if (allow_ref_in_want
&&
1241 parse_want_ref(&data
->writer
, arg
, &data
->wanted_refs
,
1244 /* process have line */
1245 if (parse_have(arg
, &data
->haves
))
1248 /* process args like thin-pack */
1249 if (!strcmp(arg
, "thin-pack")) {
1253 if (!strcmp(arg
, "ofs-delta")) {
1257 if (!strcmp(arg
, "no-progress")) {
1261 if (!strcmp(arg
, "include-tag")) {
1262 use_include_tag
= 1;
1265 if (!strcmp(arg
, "done")) {
1270 /* Shallow related arguments */
1271 if (process_shallow(arg
, &data
->shallows
))
1273 if (process_deepen(arg
, &data
->depth
))
1275 if (process_deepen_since(arg
, &data
->deepen_since
,
1276 &data
->deepen_rev_list
))
1278 if (process_deepen_not(arg
, &data
->deepen_not
,
1279 &data
->deepen_rev_list
))
1281 if (!strcmp(arg
, "deepen-relative")) {
1282 data
->deepen_relative
= 1;
1286 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1287 parse_list_objects_filter(&filter_options
, p
);
1291 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1292 allow_sideband_all
) &&
1293 !strcmp(arg
, "sideband-all")) {
1294 data
->writer
.use_sideband
= 1;
1298 /* ignore unknown lines maybe? */
1299 die("unexpected line: '%s'", arg
);
1303 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
,
1304 struct object_array
*have_obj
)
1309 for (i
= 0; i
< haves
->nr
; i
++) {
1310 const struct object_id
*oid
= &haves
->oid
[i
];
1312 int we_knew_they_have
= 0;
1314 if (!has_object_file(oid
))
1317 oid_array_append(common
, oid
);
1319 o
= parse_object(the_repository
, oid
);
1321 die("oops (%s)", oid_to_hex(oid
));
1322 if (o
->type
== OBJ_COMMIT
) {
1323 struct commit_list
*parents
;
1324 struct commit
*commit
= (struct commit
*)o
;
1325 if (o
->flags
& THEY_HAVE
)
1326 we_knew_they_have
= 1;
1328 o
->flags
|= THEY_HAVE
;
1329 if (!oldest_have
|| (commit
->date
< oldest_have
))
1330 oldest_have
= commit
->date
;
1331 for (parents
= commit
->parents
;
1333 parents
= parents
->next
)
1334 parents
->item
->object
.flags
|= THEY_HAVE
;
1336 if (!we_knew_they_have
)
1337 add_object_array(o
, NULL
, have_obj
);
1343 static int send_acks(struct packet_writer
*writer
, struct oid_array
*acks
,
1344 const struct object_array
*have_obj
,
1345 struct object_array
*want_obj
)
1349 packet_writer_write(writer
, "acknowledgments\n");
1353 packet_writer_write(writer
, "NAK\n");
1355 for (i
= 0; i
< acks
->nr
; i
++) {
1356 packet_writer_write(writer
, "ACK %s\n",
1357 oid_to_hex(&acks
->oid
[i
]));
1360 if (ok_to_give_up(have_obj
, want_obj
)) {
1362 packet_writer_write(writer
, "ready\n");
1369 static int process_haves_and_send_acks(struct upload_pack_data
*data
,
1370 struct object_array
*have_obj
,
1371 struct object_array
*want_obj
)
1373 struct oid_array common
= OID_ARRAY_INIT
;
1376 process_haves(&data
->haves
, &common
, have_obj
);
1379 } else if (send_acks(&data
->writer
, &common
, have_obj
, want_obj
)) {
1380 packet_writer_delim(&data
->writer
);
1384 packet_writer_flush(&data
->writer
);
1388 oid_array_clear(&data
->haves
);
1389 oid_array_clear(&common
);
1393 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1395 const struct string_list_item
*item
;
1397 if (!data
->wanted_refs
.nr
)
1400 packet_writer_write(&data
->writer
, "wanted-refs\n");
1402 for_each_string_list_item(item
, &data
->wanted_refs
) {
1403 packet_writer_write(&data
->writer
, "%s %s\n",
1404 oid_to_hex(item
->util
),
1408 packet_writer_delim(&data
->writer
);
1411 static void send_shallow_info(struct upload_pack_data
*data
,
1412 struct object_array
*want_obj
)
1414 /* No shallow info needs to be sent */
1415 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1416 !is_repository_shallow(the_repository
))
1419 packet_writer_write(&data
->writer
, "shallow-info\n");
1421 if (!send_shallow_list(&data
->writer
, data
->depth
,
1422 data
->deepen_rev_list
,
1423 data
->deepen_since
, &data
->deepen_not
,
1424 &data
->shallows
, want_obj
) &&
1425 is_repository_shallow(the_repository
))
1426 deepen(&data
->writer
, INFINITE_DEPTH
, data
->deepen_relative
,
1427 &data
->shallows
, want_obj
);
1433 FETCH_PROCESS_ARGS
= 0,
1439 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1440 struct packet_reader
*request
)
1442 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1443 struct upload_pack_data data
;
1444 struct object_array have_obj
= OBJECT_ARRAY_INIT
;
1445 struct object_array want_obj
= OBJECT_ARRAY_INIT
;
1447 clear_object_flags(ALL_FLAGS
);
1449 git_config(upload_pack_config
, NULL
);
1451 upload_pack_data_init(&data
);
1452 use_sideband
= LARGE_PACKET_MAX
;
1454 while (state
!= FETCH_DONE
) {
1456 case FETCH_PROCESS_ARGS
:
1457 process_args(request
, &data
, &want_obj
);
1461 * Request didn't contain any 'want' lines,
1462 * guess they didn't want anything.
1465 } else if (data
.haves
.nr
) {
1467 * Request had 'have' lines, so lets ACK them.
1469 state
= FETCH_SEND_ACKS
;
1472 * Request had 'want's but no 'have's so we can
1473 * immedietly go to construct and send a pack.
1475 state
= FETCH_SEND_PACK
;
1478 case FETCH_SEND_ACKS
:
1479 if (process_haves_and_send_acks(&data
, &have_obj
,
1481 state
= FETCH_SEND_PACK
;
1485 case FETCH_SEND_PACK
:
1486 send_wanted_ref_info(&data
);
1487 send_shallow_info(&data
, &want_obj
);
1489 packet_writer_write(&data
.writer
, "packfile\n");
1490 create_pack_file(&have_obj
, &want_obj
);
1498 upload_pack_data_clear(&data
);
1499 object_array_clear(&have_obj
);
1500 object_array_clear(&want_obj
);
1504 int upload_pack_advertise(struct repository
*r
,
1505 struct strbuf
*value
)
1508 int allow_filter_value
;
1509 int allow_ref_in_want
;
1510 int allow_sideband_all_value
;
1512 strbuf_addstr(value
, "shallow");
1514 if (!repo_config_get_bool(the_repository
,
1515 "uploadpack.allowfilter",
1516 &allow_filter_value
) &&
1518 strbuf_addstr(value
, " filter");
1520 if (!repo_config_get_bool(the_repository
,
1521 "uploadpack.allowrefinwant",
1522 &allow_ref_in_want
) &&
1524 strbuf_addstr(value
, " ref-in-want");
1526 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1527 (!repo_config_get_bool(the_repository
,
1528 "uploadpack.allowsidebandall",
1529 &allow_sideband_all_value
) &&
1530 allow_sideband_all_value
))
1531 strbuf_addstr(value
, " sideband-all");