6 #include "object-store.h"
12 #include "list-objects.h"
13 #include "list-objects-filter.h"
14 #include "list-objects-filter-options.h"
15 #include "run-command.h"
19 #include "string-list.h"
20 #include "argv-array.h"
21 #include "prio-queue.h"
24 #include "upload-pack.h"
27 /* Remember to update object flag allocation in object.h */
28 #define THEY_HAVE (1u << 11)
29 #define OUR_REF (1u << 12)
30 #define WANTED (1u << 13)
31 #define COMMON_KNOWN (1u << 14)
32 #define REACHABLE (1u << 15)
34 #define SHALLOW (1u << 16)
35 #define NOT_SHALLOW (1u << 17)
36 #define CLIENT_SHALLOW (1u << 18)
37 #define HIDDEN_REF (1u << 19)
39 static timestamp_t oldest_have
;
41 static int deepen_relative
;
44 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
45 static int no_progress
, daemon_mode
;
46 /* Allow specifying sha1 if it is a ref tip. */
47 #define ALLOW_TIP_SHA1 01
48 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
49 #define ALLOW_REACHABLE_SHA1 02
50 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
51 #define ALLOW_ANY_SHA1 07
52 static unsigned int allow_unadvertised_object_request
;
53 static int shallow_nr
;
54 static struct object_array have_obj
;
55 static struct object_array want_obj
;
56 static struct object_array extra_edge_obj
;
57 static unsigned int timeout
;
58 static int keepalive
= 5;
60 * otherwise maximum packet size (up to 65520 bytes).
62 static int use_sideband
;
63 static int stateless_rpc
;
64 static const char *pack_objects_hook
;
66 static int filter_capability_requested
;
67 static int allow_filter
;
68 static int allow_ref_in_want
;
69 static struct list_objects_filter_options filter_options
;
71 static void reset_timeout(void)
76 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
79 send_sideband(1, fd
, data
, sz
, use_sideband
);
86 /* XXX: are we happy to lose stuff here? */
90 write_or_die(fd
, data
, sz
);
93 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
96 if (graft
->nr_parent
== -1)
97 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
101 static void create_pack_file(void)
103 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
104 char data
[8193], progress
[128];
105 char abort_msg
[] = "aborting due to possible repository "
106 "corruption on the remote side.";
112 if (!pack_objects_hook
)
113 pack_objects
.git_cmd
= 1;
115 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
116 argv_array_push(&pack_objects
.args
, "git");
117 pack_objects
.use_shell
= 1;
121 argv_array_push(&pack_objects
.args
, "--shallow-file");
122 argv_array_push(&pack_objects
.args
, "");
124 argv_array_push(&pack_objects
.args
, "pack-objects");
125 argv_array_push(&pack_objects
.args
, "--revs");
127 argv_array_push(&pack_objects
.args
, "--thin");
129 argv_array_push(&pack_objects
.args
, "--stdout");
131 argv_array_push(&pack_objects
.args
, "--shallow");
133 argv_array_push(&pack_objects
.args
, "--progress");
135 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
137 argv_array_push(&pack_objects
.args
, "--include-tag");
138 if (filter_options
.filter_spec
) {
139 if (pack_objects
.use_shell
) {
140 struct strbuf buf
= STRBUF_INIT
;
141 sq_quote_buf(&buf
, filter_options
.filter_spec
);
142 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
143 strbuf_release(&buf
);
145 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
146 filter_options
.filter_spec
);
150 pack_objects
.in
= -1;
151 pack_objects
.out
= -1;
152 pack_objects
.err
= -1;
154 if (start_command(&pack_objects
))
155 die("git upload-pack: unable to fork git-pack-objects");
157 pipe_fd
= xfdopen(pack_objects
.in
, "w");
160 for_each_commit_graft(write_one_shallow
, pipe_fd
);
162 for (i
= 0; i
< want_obj
.nr
; i
++)
163 fprintf(pipe_fd
, "%s\n",
164 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
165 fprintf(pipe_fd
, "--not\n");
166 for (i
= 0; i
< have_obj
.nr
; i
++)
167 fprintf(pipe_fd
, "%s\n",
168 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
169 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
170 fprintf(pipe_fd
, "%s\n",
171 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
172 fprintf(pipe_fd
, "\n");
176 /* We read from pack_objects.err to capture stderr output for
177 * progress bar, and pack_objects.out to capture the pack data.
181 struct pollfd pfd
[2];
182 int pe
, pu
, pollsize
;
190 if (0 <= pack_objects
.out
) {
191 pfd
[pollsize
].fd
= pack_objects
.out
;
192 pfd
[pollsize
].events
= POLLIN
;
196 if (0 <= pack_objects
.err
) {
197 pfd
[pollsize
].fd
= pack_objects
.err
;
198 pfd
[pollsize
].events
= POLLIN
;
206 ret
= poll(pfd
, pollsize
,
207 keepalive
< 0 ? -1 : 1000 * keepalive
);
210 if (errno
!= EINTR
) {
211 error_errno("poll failed, resuming");
216 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
217 /* Status ready; we ship that in the side-band
218 * or dump to the standard error.
220 sz
= xread(pack_objects
.err
, progress
,
223 send_client_data(2, progress
, sz
);
225 close(pack_objects
.err
);
226 pack_objects
.err
= -1;
230 /* give priority to status messages */
233 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
234 /* Data ready; we keep the last byte to ourselves
235 * in case we detect broken rev-list, so that we
236 * can leave the stream corrupted. This is
237 * unfortunate -- unpack-objects would happily
238 * accept a valid packdata with trailing garbage,
239 * so appending garbage after we pass all the
240 * pack data is not good enough to signal
241 * breakage to downstream.
249 sz
= xread(pack_objects
.out
, cp
,
250 sizeof(data
) - outsz
);
254 close(pack_objects
.out
);
255 pack_objects
.out
= -1;
261 buffered
= data
[sz
-1] & 0xFF;
266 send_client_data(1, data
, sz
);
270 * We hit the keepalive timeout without saying anything; send
271 * an empty message on the data sideband just to let the other
272 * side know we're still working on it, but don't have any data
275 * If we don't have a sideband channel, there's no room in the
276 * protocol to say anything, so those clients are just out of
279 if (!ret
&& use_sideband
) {
280 static const char buf
[] = "0005\1";
281 write_or_die(1, buf
, 5);
285 if (finish_command(&pack_objects
)) {
286 error("git upload-pack: git-pack-objects died with error.");
293 send_client_data(1, data
, 1);
294 fprintf(stderr
, "flushed.\n");
301 send_client_data(3, abort_msg
, sizeof(abort_msg
));
302 die("git upload-pack: %s", abort_msg
);
305 static int got_oid(const char *hex
, struct object_id
*oid
)
308 int we_knew_they_have
= 0;
310 if (get_oid_hex(hex
, oid
))
311 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
312 if (!has_object_file(oid
))
315 o
= parse_object(oid
);
317 die("oops (%s)", oid_to_hex(oid
));
318 if (o
->type
== OBJ_COMMIT
) {
319 struct commit_list
*parents
;
320 struct commit
*commit
= (struct commit
*)o
;
321 if (o
->flags
& THEY_HAVE
)
322 we_knew_they_have
= 1;
324 o
->flags
|= THEY_HAVE
;
325 if (!oldest_have
|| (commit
->date
< oldest_have
))
326 oldest_have
= commit
->date
;
327 for (parents
= commit
->parents
;
329 parents
= parents
->next
)
330 parents
->item
->object
.flags
|= THEY_HAVE
;
332 if (!we_knew_they_have
) {
333 add_object_array(o
, NULL
, &have_obj
);
339 static int reachable(struct commit
*want
)
341 struct prio_queue work
= { compare_commits_by_commit_date
};
343 prio_queue_put(&work
, want
);
345 struct commit_list
*list
;
346 struct commit
*commit
= prio_queue_get(&work
);
348 if (commit
->object
.flags
& THEY_HAVE
) {
349 want
->object
.flags
|= COMMON_KNOWN
;
352 if (!commit
->object
.parsed
)
353 parse_object(&commit
->object
.oid
);
354 if (commit
->object
.flags
& REACHABLE
)
356 commit
->object
.flags
|= REACHABLE
;
357 if (commit
->date
< oldest_have
)
359 for (list
= commit
->parents
; list
; list
= list
->next
) {
360 struct commit
*parent
= list
->item
;
361 if (!(parent
->object
.flags
& REACHABLE
))
362 prio_queue_put(&work
, parent
);
365 want
->object
.flags
|= REACHABLE
;
366 clear_commit_marks(want
, REACHABLE
);
367 clear_prio_queue(&work
);
368 return (want
->object
.flags
& COMMON_KNOWN
);
371 static int ok_to_give_up(void)
378 for (i
= 0; i
< want_obj
.nr
; i
++) {
379 struct object
*want
= want_obj
.objects
[i
].item
;
381 if (want
->flags
& COMMON_KNOWN
)
383 want
= deref_tag(want
, "a want line", 0);
384 if (!want
|| want
->type
!= OBJ_COMMIT
) {
385 /* no way to tell if this is reachable by
386 * looking at the ancestry chain alone, so
387 * leave a note to ourselves not to worry about
388 * this object anymore.
390 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
393 if (!reachable((struct commit
*)want
))
399 static int get_common_commits(void)
401 struct object_id oid
;
402 char last_hex
[GIT_MAX_HEXSZ
+ 1];
407 save_commit_buffer
= 0;
410 char *line
= packet_read_line(0, NULL
);
416 if (multi_ack
== 2 && got_common
417 && !got_other
&& ok_to_give_up()) {
419 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
421 if (have_obj
.nr
== 0 || multi_ack
)
422 packet_write_fmt(1, "NAK\n");
424 if (no_done
&& sent_ready
) {
425 packet_write_fmt(1, "ACK %s\n", last_hex
);
434 if (skip_prefix(line
, "have ", &arg
)) {
435 switch (got_oid(arg
, &oid
)) {
436 case -1: /* they have what we do not */
438 if (multi_ack
&& ok_to_give_up()) {
439 const char *hex
= oid_to_hex(&oid
);
440 if (multi_ack
== 2) {
442 packet_write_fmt(1, "ACK %s ready\n", hex
);
444 packet_write_fmt(1, "ACK %s continue\n", hex
);
449 oid_to_hex_r(last_hex
, &oid
);
451 packet_write_fmt(1, "ACK %s common\n", last_hex
);
453 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
454 else if (have_obj
.nr
== 1)
455 packet_write_fmt(1, "ACK %s\n", last_hex
);
460 if (!strcmp(line
, "done")) {
461 if (have_obj
.nr
> 0) {
463 packet_write_fmt(1, "ACK %s\n", last_hex
);
466 packet_write_fmt(1, "NAK\n");
469 die("git upload-pack: expected SHA1 list, got '%s'", line
);
473 static int is_our_ref(struct object
*o
)
475 int allow_hidden_ref
= (allow_unadvertised_object_request
&
476 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
477 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
481 * on successful case, it's up to the caller to close cmd->out
483 static int do_reachable_revlist(struct child_process
*cmd
,
484 struct object_array
*src
,
485 struct object_array
*reachable
)
487 static const char *argv
[] = {
488 "rev-list", "--stdin", NULL
,
491 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
501 * If the next rev-list --stdin encounters an unknown commit,
502 * it terminates, which will cause SIGPIPE in the write loop
505 sigchain_push(SIGPIPE
, SIG_IGN
);
507 if (start_command(cmd
))
511 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
512 for (i
= get_max_object_index(); 0 < i
; ) {
513 o
= get_indexed_object(--i
);
516 if (reachable
&& o
->type
== OBJ_COMMIT
)
517 o
->flags
&= ~TMP_MARK
;
520 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
521 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
524 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
525 for (i
= 0; i
< src
->nr
; i
++) {
526 o
= src
->objects
[i
].item
;
529 add_object_array(o
, NULL
, reachable
);
532 if (reachable
&& o
->type
== OBJ_COMMIT
)
533 o
->flags
|= TMP_MARK
;
534 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
535 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
540 sigchain_pop(SIGPIPE
);
545 sigchain_pop(SIGPIPE
);
554 static int get_reachable_list(struct object_array
*src
,
555 struct object_array
*reachable
)
557 struct child_process cmd
= CHILD_PROCESS_INIT
;
560 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
561 const unsigned hexsz
= the_hash_algo
->hexsz
;
563 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
566 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
567 struct object_id sha1
;
570 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
573 o
= lookup_object(sha1
.hash
);
574 if (o
&& o
->type
== OBJ_COMMIT
) {
575 o
->flags
&= ~TMP_MARK
;
578 for (i
= get_max_object_index(); 0 < i
; i
--) {
579 o
= get_indexed_object(i
- 1);
580 if (o
&& o
->type
== OBJ_COMMIT
&&
581 (o
->flags
& TMP_MARK
)) {
582 add_object_array(o
, NULL
, reachable
);
583 o
->flags
&= ~TMP_MARK
;
588 if (finish_command(&cmd
))
594 static int has_unreachable(struct object_array
*src
)
596 struct child_process cmd
= CHILD_PROCESS_INIT
;
600 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
604 * The commits out of the rev-list are not ancestors of
607 i
= read_in_full(cmd
.out
, buf
, 1);
614 * rev-list may have died by encountering a bad commit
615 * in the history, in which case we do want to bail out
616 * even when it showed no commit.
618 if (finish_command(&cmd
))
621 /* All the non-tip ones are ancestors of what we advertised */
625 sigchain_pop(SIGPIPE
);
631 static void check_non_tip(void)
636 * In the normal in-process case without
637 * uploadpack.allowReachableSHA1InWant,
638 * non-tip requests can never happen.
640 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
642 if (!has_unreachable(&want_obj
))
643 /* All the non-tip ones are ancestors of what we advertised */
647 /* Pick one of them (we know there at least is one) */
648 for (i
= 0; i
< want_obj
.nr
; i
++) {
649 struct object
*o
= want_obj
.objects
[i
].item
;
651 die("git upload-pack: not our ref %s",
652 oid_to_hex(&o
->oid
));
656 static void send_shallow(struct commit_list
*result
)
659 struct object
*object
= &result
->item
->object
;
660 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
661 packet_write_fmt(1, "shallow %s",
662 oid_to_hex(&object
->oid
));
663 register_shallow(the_repository
, &object
->oid
);
666 result
= result
->next
;
670 static void send_unshallow(const struct object_array
*shallows
)
674 for (i
= 0; i
< shallows
->nr
; i
++) {
675 struct object
*object
= shallows
->objects
[i
].item
;
676 if (object
->flags
& NOT_SHALLOW
) {
677 struct commit_list
*parents
;
678 packet_write_fmt(1, "unshallow %s",
679 oid_to_hex(&object
->oid
));
680 object
->flags
&= ~CLIENT_SHALLOW
;
682 * We want to _register_ "object" as shallow, but we
683 * also need to traverse object's parents to deepen a
684 * shallow clone. Unregister it for now so we can
685 * parse and add the parents to the want list, then
688 unregister_shallow(&object
->oid
);
690 parse_commit_or_die((struct commit
*)object
);
691 parents
= ((struct commit
*)object
)->parents
;
693 add_object_array(&parents
->item
->object
,
695 parents
= parents
->next
;
697 add_object_array(object
, NULL
, &extra_edge_obj
);
699 /* make sure commit traversal conforms to client */
700 register_shallow(the_repository
, &object
->oid
);
704 static void deepen(int depth
, int deepen_relative
,
705 struct object_array
*shallows
)
707 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
710 for (i
= 0; i
< shallows
->nr
; i
++) {
711 struct object
*object
= shallows
->objects
[i
].item
;
712 object
->flags
|= NOT_SHALLOW
;
714 } else if (deepen_relative
) {
715 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
716 struct commit_list
*result
;
718 get_reachable_list(shallows
, &reachable_shallows
);
719 result
= get_shallow_commits(&reachable_shallows
,
721 SHALLOW
, NOT_SHALLOW
);
722 send_shallow(result
);
723 free_commit_list(result
);
724 object_array_clear(&reachable_shallows
);
726 struct commit_list
*result
;
728 result
= get_shallow_commits(&want_obj
, depth
,
729 SHALLOW
, NOT_SHALLOW
);
730 send_shallow(result
);
731 free_commit_list(result
);
734 send_unshallow(shallows
);
737 static void deepen_by_rev_list(int ac
, const char **av
,
738 struct object_array
*shallows
)
740 struct commit_list
*result
;
742 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
743 send_shallow(result
);
744 free_commit_list(result
);
745 send_unshallow(shallows
);
748 /* Returns 1 if a shallow list is sent or 0 otherwise */
749 static int send_shallow_list(int depth
, int deepen_rev_list
,
750 timestamp_t deepen_since
,
751 struct string_list
*deepen_not
,
752 struct object_array
*shallows
)
756 if (depth
> 0 && deepen_rev_list
)
757 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
759 deepen(depth
, deepen_relative
, shallows
);
761 } else if (deepen_rev_list
) {
762 struct argv_array av
= ARGV_ARRAY_INIT
;
765 argv_array_push(&av
, "rev-list");
767 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
768 if (deepen_not
->nr
) {
769 argv_array_push(&av
, "--not");
770 for (i
= 0; i
< deepen_not
->nr
; i
++) {
771 struct string_list_item
*s
= deepen_not
->items
+ i
;
772 argv_array_push(&av
, s
->string
);
774 argv_array_push(&av
, "--not");
776 for (i
= 0; i
< want_obj
.nr
; i
++) {
777 struct object
*o
= want_obj
.objects
[i
].item
;
778 argv_array_push(&av
, oid_to_hex(&o
->oid
));
780 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
);
781 argv_array_clear(&av
);
784 if (shallows
->nr
> 0) {
786 for (i
= 0; i
< shallows
->nr
; i
++)
787 register_shallow(the_repository
,
788 &shallows
->objects
[i
].item
->oid
);
792 shallow_nr
+= shallows
->nr
;
796 static int process_shallow(const char *line
, struct object_array
*shallows
)
799 if (skip_prefix(line
, "shallow ", &arg
)) {
800 struct object_id oid
;
801 struct object
*object
;
802 if (get_oid_hex(arg
, &oid
))
803 die("invalid shallow line: %s", line
);
804 object
= parse_object(&oid
);
807 if (object
->type
!= OBJ_COMMIT
)
808 die("invalid shallow object %s", oid_to_hex(&oid
));
809 if (!(object
->flags
& CLIENT_SHALLOW
)) {
810 object
->flags
|= CLIENT_SHALLOW
;
811 add_object_array(object
, NULL
, shallows
);
819 static int process_deepen(const char *line
, int *depth
)
822 if (skip_prefix(line
, "deepen ", &arg
)) {
824 *depth
= (int)strtol(arg
, &end
, 0);
825 if (!end
|| *end
|| *depth
<= 0)
826 die("Invalid deepen: %s", line
);
833 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
836 if (skip_prefix(line
, "deepen-since ", &arg
)) {
838 *deepen_since
= parse_timestamp(arg
, &end
, 0);
839 if (!end
|| *end
|| !deepen_since
||
840 /* revisions.c's max_age -1 is special */
842 die("Invalid deepen-since: %s", line
);
843 *deepen_rev_list
= 1;
849 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
852 if (skip_prefix(line
, "deepen-not ", &arg
)) {
854 struct object_id oid
;
855 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
856 die("git upload-pack: ambiguous deepen-not: %s", line
);
857 string_list_append(deepen_not
, ref
);
859 *deepen_rev_list
= 1;
865 static void receive_needs(void)
867 struct object_array shallows
= OBJECT_ARRAY_INIT
;
868 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
871 timestamp_t deepen_since
= 0;
872 int deepen_rev_list
= 0;
877 const char *features
;
878 struct object_id oid_buf
;
879 char *line
= packet_read_line(0, NULL
);
886 if (process_shallow(line
, &shallows
))
888 if (process_deepen(line
, &depth
))
890 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
892 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
895 if (skip_prefix(line
, "filter ", &arg
)) {
896 if (!filter_capability_requested
)
897 die("git upload-pack: filtering capability not negotiated");
898 parse_list_objects_filter(&filter_options
, arg
);
902 if (!skip_prefix(line
, "want ", &arg
) ||
903 parse_oid_hex(arg
, &oid_buf
, &features
))
904 die("git upload-pack: protocol error, "
905 "expected to get object ID, not '%s'", line
);
907 if (parse_feature_request(features
, "deepen-relative"))
909 if (parse_feature_request(features
, "multi_ack_detailed"))
911 else if (parse_feature_request(features
, "multi_ack"))
913 if (parse_feature_request(features
, "no-done"))
915 if (parse_feature_request(features
, "thin-pack"))
917 if (parse_feature_request(features
, "ofs-delta"))
919 if (parse_feature_request(features
, "side-band-64k"))
920 use_sideband
= LARGE_PACKET_MAX
;
921 else if (parse_feature_request(features
, "side-band"))
922 use_sideband
= DEFAULT_PACKET_MAX
;
923 if (parse_feature_request(features
, "no-progress"))
925 if (parse_feature_request(features
, "include-tag"))
927 if (allow_filter
&& parse_feature_request(features
, "filter"))
928 filter_capability_requested
= 1;
930 o
= parse_object(&oid_buf
);
933 "ERR upload-pack: not our ref %s",
934 oid_to_hex(&oid_buf
));
935 die("git upload-pack: not our ref %s",
936 oid_to_hex(&oid_buf
));
938 if (!(o
->flags
& WANTED
)) {
940 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
943 add_object_array(o
, NULL
, &want_obj
);
948 * We have sent all our refs already, and the other end
949 * should have chosen out of them. When we are operating
950 * in the stateless RPC mode, however, their choice may
951 * have been based on the set of older refs advertised
952 * by another process that handled the initial request.
957 if (!use_sideband
&& daemon_mode
)
960 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
963 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
964 &deepen_not
, &shallows
))
966 object_array_clear(&shallows
);
969 /* return non-zero if the ref is hidden, otherwise 0 */
970 static int mark_our_ref(const char *refname
, const char *refname_full
,
971 const struct object_id
*oid
)
973 struct object
*o
= lookup_unknown_object(oid
->hash
);
975 if (ref_is_hidden(refname
, refname_full
)) {
976 o
->flags
|= HIDDEN_REF
;
983 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
984 int flag
, void *cb_data
)
986 const char *refname
= strip_namespace(refname_full
);
988 mark_our_ref(refname
, refname_full
, oid
);
992 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
994 struct string_list_item
*item
;
998 for_each_string_list_item(item
, symref
)
999 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1002 static int send_ref(const char *refname
, const struct object_id
*oid
,
1003 int flag
, void *cb_data
)
1005 static const char *capabilities
= "multi_ack thin-pack side-band"
1006 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1007 " deepen-relative no-progress include-tag multi_ack_detailed";
1008 const char *refname_nons
= strip_namespace(refname
);
1009 struct object_id peeled
;
1011 if (mark_our_ref(refname_nons
, refname
, oid
))
1015 struct strbuf symref_info
= STRBUF_INIT
;
1017 format_symref_info(&symref_info
, cb_data
);
1018 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1019 oid_to_hex(oid
), refname_nons
,
1021 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
1022 " allow-tip-sha1-in-want" : "",
1023 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1024 " allow-reachable-sha1-in-want" : "",
1025 stateless_rpc
? " no-done" : "",
1027 allow_filter
? " filter" : "",
1028 git_user_agent_sanitized());
1029 strbuf_release(&symref_info
);
1031 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1033 capabilities
= NULL
;
1034 if (!peel_ref(refname
, &peeled
))
1035 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1039 static int find_symref(const char *refname
, const struct object_id
*oid
,
1040 int flag
, void *cb_data
)
1042 const char *symref_target
;
1043 struct string_list_item
*item
;
1045 if ((flag
& REF_ISSYMREF
) == 0)
1047 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1048 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1049 die("'%s' is a symref but it is not?", refname
);
1050 item
= string_list_append(cb_data
, refname
);
1051 item
->util
= xstrdup(symref_target
);
1055 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1057 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1058 if (git_config_bool(var
, value
))
1059 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1061 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1062 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1063 if (git_config_bool(var
, value
))
1064 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1066 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1067 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1068 if (git_config_bool(var
, value
))
1069 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1071 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1072 } else if (!strcmp("uploadpack.keepalive", var
)) {
1073 keepalive
= git_config_int(var
, value
);
1076 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1077 if (!strcmp("uploadpack.packobjectshook", var
))
1078 return git_config_string(&pack_objects_hook
, var
, value
);
1079 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1080 allow_filter
= git_config_bool(var
, value
);
1081 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1082 allow_ref_in_want
= git_config_bool(var
, value
);
1084 return parse_hide_refs_config(var
, value
, "uploadpack");
1087 void upload_pack(struct upload_pack_options
*options
)
1089 struct string_list symref
= STRING_LIST_INIT_DUP
;
1091 stateless_rpc
= options
->stateless_rpc
;
1092 timeout
= options
->timeout
;
1093 daemon_mode
= options
->daemon_mode
;
1095 git_config(upload_pack_config
, NULL
);
1097 head_ref_namespaced(find_symref
, &symref
);
1099 if (options
->advertise_refs
|| !stateless_rpc
) {
1101 head_ref_namespaced(send_ref
, &symref
);
1102 for_each_namespaced_ref(send_ref
, &symref
);
1103 advertise_shallow_grafts(1);
1106 head_ref_namespaced(check_ref
, NULL
);
1107 for_each_namespaced_ref(check_ref
, NULL
);
1109 string_list_clear(&symref
, 1);
1110 if (options
->advertise_refs
)
1115 get_common_commits();
1120 struct upload_pack_data
{
1121 struct object_array wants
;
1122 struct string_list wanted_refs
;
1123 struct oid_array haves
;
1125 struct object_array shallows
;
1126 struct string_list deepen_not
;
1128 timestamp_t deepen_since
;
1129 int deepen_rev_list
;
1130 int deepen_relative
;
1132 unsigned stateless_rpc
: 1;
1134 unsigned use_thin_pack
: 1;
1135 unsigned use_ofs_delta
: 1;
1136 unsigned no_progress
: 1;
1137 unsigned use_include_tag
: 1;
1141 static void upload_pack_data_init(struct upload_pack_data
*data
)
1143 struct object_array wants
= OBJECT_ARRAY_INIT
;
1144 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1145 struct oid_array haves
= OID_ARRAY_INIT
;
1146 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1147 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1149 memset(data
, 0, sizeof(*data
));
1150 data
->wants
= wants
;
1151 data
->wanted_refs
= wanted_refs
;
1152 data
->haves
= haves
;
1153 data
->shallows
= shallows
;
1154 data
->deepen_not
= deepen_not
;
1157 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1159 object_array_clear(&data
->wants
);
1160 string_list_clear(&data
->wanted_refs
, 1);
1161 oid_array_clear(&data
->haves
);
1162 object_array_clear(&data
->shallows
);
1163 string_list_clear(&data
->deepen_not
, 0);
1166 static int parse_want(const char *line
)
1169 if (skip_prefix(line
, "want ", &arg
)) {
1170 struct object_id oid
;
1173 if (get_oid_hex(arg
, &oid
))
1174 die("git upload-pack: protocol error, "
1175 "expected to get oid, not '%s'", line
);
1177 o
= parse_object(&oid
);
1180 "ERR upload-pack: not our ref %s",
1182 die("git upload-pack: not our ref %s",
1186 if (!(o
->flags
& WANTED
)) {
1188 add_object_array(o
, NULL
, &want_obj
);
1197 static int parse_want_ref(const char *line
, struct string_list
*wanted_refs
)
1200 if (skip_prefix(line
, "want-ref ", &arg
)) {
1201 struct object_id oid
;
1202 struct string_list_item
*item
;
1205 if (read_ref(arg
, &oid
)) {
1206 packet_write_fmt(1, "ERR unknown ref %s", arg
);
1207 die("unknown ref %s", arg
);
1210 item
= string_list_append(wanted_refs
, arg
);
1211 item
->util
= oiddup(&oid
);
1213 o
= parse_object_or_die(&oid
, arg
);
1214 if (!(o
->flags
& WANTED
)) {
1216 add_object_array(o
, NULL
, &want_obj
);
1225 static int parse_have(const char *line
, struct oid_array
*haves
)
1228 if (skip_prefix(line
, "have ", &arg
)) {
1229 struct object_id oid
;
1231 if (get_oid_hex(arg
, &oid
))
1232 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1233 oid_array_append(haves
, &oid
);
1240 static void process_args(struct packet_reader
*request
,
1241 struct upload_pack_data
*data
)
1243 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1244 const char *arg
= request
->line
;
1248 if (parse_want(arg
))
1250 if (allow_ref_in_want
&& parse_want_ref(arg
, &data
->wanted_refs
))
1252 /* process have line */
1253 if (parse_have(arg
, &data
->haves
))
1256 /* process args like thin-pack */
1257 if (!strcmp(arg
, "thin-pack")) {
1261 if (!strcmp(arg
, "ofs-delta")) {
1265 if (!strcmp(arg
, "no-progress")) {
1269 if (!strcmp(arg
, "include-tag")) {
1270 use_include_tag
= 1;
1273 if (!strcmp(arg
, "done")) {
1278 /* Shallow related arguments */
1279 if (process_shallow(arg
, &data
->shallows
))
1281 if (process_deepen(arg
, &data
->depth
))
1283 if (process_deepen_since(arg
, &data
->deepen_since
,
1284 &data
->deepen_rev_list
))
1286 if (process_deepen_not(arg
, &data
->deepen_not
,
1287 &data
->deepen_rev_list
))
1289 if (!strcmp(arg
, "deepen-relative")) {
1290 data
->deepen_relative
= 1;
1294 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1295 parse_list_objects_filter(&filter_options
, p
);
1299 /* ignore unknown lines maybe? */
1300 die("unexpected line: '%s'", arg
);
1304 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
)
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(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 oid_array
*acks
, struct strbuf
*response
)
1347 packet_buf_write(response
, "acknowledgments\n");
1351 packet_buf_write(response
, "NAK\n");
1353 for (i
= 0; i
< acks
->nr
; i
++) {
1354 packet_buf_write(response
, "ACK %s\n",
1355 oid_to_hex(&acks
->oid
[i
]));
1358 if (ok_to_give_up()) {
1360 packet_buf_write(response
, "ready\n");
1367 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1369 struct oid_array common
= OID_ARRAY_INIT
;
1370 struct strbuf response
= STRBUF_INIT
;
1373 process_haves(&data
->haves
, &common
);
1376 } else if (send_acks(&common
, &response
)) {
1377 packet_buf_delim(&response
);
1381 packet_buf_flush(&response
);
1386 write_or_die(1, response
.buf
, response
.len
);
1387 strbuf_release(&response
);
1389 oid_array_clear(&data
->haves
);
1390 oid_array_clear(&common
);
1394 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1396 const struct string_list_item
*item
;
1398 if (!data
->wanted_refs
.nr
)
1401 packet_write_fmt(1, "wanted-refs\n");
1403 for_each_string_list_item(item
, &data
->wanted_refs
) {
1404 packet_write_fmt(1, "%s %s\n",
1405 oid_to_hex(item
->util
),
1412 static void send_shallow_info(struct upload_pack_data
*data
)
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_write_fmt(1, "shallow-info\n");
1421 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1422 data
->deepen_since
, &data
->deepen_not
,
1424 is_repository_shallow(the_repository
))
1425 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
);
1431 FETCH_PROCESS_ARGS
= 0,
1437 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1438 struct packet_reader
*request
)
1440 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1441 struct upload_pack_data data
;
1443 git_config(upload_pack_config
, NULL
);
1445 upload_pack_data_init(&data
);
1446 use_sideband
= LARGE_PACKET_MAX
;
1448 while (state
!= FETCH_DONE
) {
1450 case FETCH_PROCESS_ARGS
:
1451 process_args(request
, &data
);
1455 * Request didn't contain any 'want' lines,
1456 * guess they didn't want anything.
1459 } else if (data
.haves
.nr
) {
1461 * Request had 'have' lines, so lets ACK them.
1463 state
= FETCH_SEND_ACKS
;
1466 * Request had 'want's but no 'have's so we can
1467 * immedietly go to construct and send a pack.
1469 state
= FETCH_SEND_PACK
;
1472 case FETCH_SEND_ACKS
:
1473 if (process_haves_and_send_acks(&data
))
1474 state
= FETCH_SEND_PACK
;
1478 case FETCH_SEND_PACK
:
1479 send_wanted_ref_info(&data
);
1480 send_shallow_info(&data
);
1482 packet_write_fmt(1, "packfile\n");
1491 upload_pack_data_clear(&data
);
1495 int upload_pack_advertise(struct repository
*r
,
1496 struct strbuf
*value
)
1499 int allow_filter_value
;
1500 int allow_ref_in_want
;
1502 strbuf_addstr(value
, "shallow");
1504 if (!repo_config_get_bool(the_repository
,
1505 "uploadpack.allowfilter",
1506 &allow_filter_value
) &&
1508 strbuf_addstr(value
, " filter");
1510 if (!repo_config_get_bool(the_repository
,
1511 "uploadpack.allowrefinwant",
1512 &allow_ref_in_want
) &&
1514 strbuf_addstr(value
, " ref-in-want");