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 struct list_objects_filter_options filter_options
;
70 static void reset_timeout(void)
75 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
78 send_sideband(1, fd
, data
, sz
, use_sideband
);
85 /* XXX: are we happy to lose stuff here? */
89 write_or_die(fd
, data
, sz
);
92 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
95 if (graft
->nr_parent
== -1)
96 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
100 static void create_pack_file(void)
102 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
103 char data
[8193], progress
[128];
104 char abort_msg
[] = "aborting due to possible repository "
105 "corruption on the remote side.";
111 if (!pack_objects_hook
)
112 pack_objects
.git_cmd
= 1;
114 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
115 argv_array_push(&pack_objects
.args
, "git");
116 pack_objects
.use_shell
= 1;
120 argv_array_push(&pack_objects
.args
, "--shallow-file");
121 argv_array_push(&pack_objects
.args
, "");
123 argv_array_push(&pack_objects
.args
, "pack-objects");
124 argv_array_push(&pack_objects
.args
, "--revs");
126 argv_array_push(&pack_objects
.args
, "--thin");
128 argv_array_push(&pack_objects
.args
, "--stdout");
130 argv_array_push(&pack_objects
.args
, "--shallow");
132 argv_array_push(&pack_objects
.args
, "--progress");
134 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
136 argv_array_push(&pack_objects
.args
, "--include-tag");
137 if (filter_options
.filter_spec
) {
138 if (pack_objects
.use_shell
) {
139 struct strbuf buf
= STRBUF_INIT
;
140 sq_quote_buf(&buf
, filter_options
.filter_spec
);
141 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
142 strbuf_release(&buf
);
144 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
145 filter_options
.filter_spec
);
149 pack_objects
.in
= -1;
150 pack_objects
.out
= -1;
151 pack_objects
.err
= -1;
153 if (start_command(&pack_objects
))
154 die("git upload-pack: unable to fork git-pack-objects");
156 pipe_fd
= xfdopen(pack_objects
.in
, "w");
159 for_each_commit_graft(write_one_shallow
, pipe_fd
);
161 for (i
= 0; i
< want_obj
.nr
; i
++)
162 fprintf(pipe_fd
, "%s\n",
163 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
164 fprintf(pipe_fd
, "--not\n");
165 for (i
= 0; i
< have_obj
.nr
; i
++)
166 fprintf(pipe_fd
, "%s\n",
167 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
168 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
169 fprintf(pipe_fd
, "%s\n",
170 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
171 fprintf(pipe_fd
, "\n");
175 /* We read from pack_objects.err to capture stderr output for
176 * progress bar, and pack_objects.out to capture the pack data.
180 struct pollfd pfd
[2];
181 int pe
, pu
, pollsize
;
189 if (0 <= pack_objects
.out
) {
190 pfd
[pollsize
].fd
= pack_objects
.out
;
191 pfd
[pollsize
].events
= POLLIN
;
195 if (0 <= pack_objects
.err
) {
196 pfd
[pollsize
].fd
= pack_objects
.err
;
197 pfd
[pollsize
].events
= POLLIN
;
205 ret
= poll(pfd
, pollsize
,
206 keepalive
< 0 ? -1 : 1000 * keepalive
);
209 if (errno
!= EINTR
) {
210 error_errno("poll failed, resuming");
215 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
216 /* Status ready; we ship that in the side-band
217 * or dump to the standard error.
219 sz
= xread(pack_objects
.err
, progress
,
222 send_client_data(2, progress
, sz
);
224 close(pack_objects
.err
);
225 pack_objects
.err
= -1;
229 /* give priority to status messages */
232 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
233 /* Data ready; we keep the last byte to ourselves
234 * in case we detect broken rev-list, so that we
235 * can leave the stream corrupted. This is
236 * unfortunate -- unpack-objects would happily
237 * accept a valid packdata with trailing garbage,
238 * so appending garbage after we pass all the
239 * pack data is not good enough to signal
240 * breakage to downstream.
248 sz
= xread(pack_objects
.out
, cp
,
249 sizeof(data
) - outsz
);
253 close(pack_objects
.out
);
254 pack_objects
.out
= -1;
260 buffered
= data
[sz
-1] & 0xFF;
265 send_client_data(1, data
, sz
);
269 * We hit the keepalive timeout without saying anything; send
270 * an empty message on the data sideband just to let the other
271 * side know we're still working on it, but don't have any data
274 * If we don't have a sideband channel, there's no room in the
275 * protocol to say anything, so those clients are just out of
278 if (!ret
&& use_sideband
) {
279 static const char buf
[] = "0005\1";
280 write_or_die(1, buf
, 5);
284 if (finish_command(&pack_objects
)) {
285 error("git upload-pack: git-pack-objects died with error.");
292 send_client_data(1, data
, 1);
293 fprintf(stderr
, "flushed.\n");
300 send_client_data(3, abort_msg
, sizeof(abort_msg
));
301 die("git upload-pack: %s", abort_msg
);
304 static int got_oid(const char *hex
, struct object_id
*oid
)
307 int we_knew_they_have
= 0;
309 if (get_oid_hex(hex
, oid
))
310 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
311 if (!has_object_file(oid
))
314 o
= parse_object(oid
);
316 die("oops (%s)", oid_to_hex(oid
));
317 if (o
->type
== OBJ_COMMIT
) {
318 struct commit_list
*parents
;
319 struct commit
*commit
= (struct commit
*)o
;
320 if (o
->flags
& THEY_HAVE
)
321 we_knew_they_have
= 1;
323 o
->flags
|= THEY_HAVE
;
324 if (!oldest_have
|| (commit
->date
< oldest_have
))
325 oldest_have
= commit
->date
;
326 for (parents
= commit
->parents
;
328 parents
= parents
->next
)
329 parents
->item
->object
.flags
|= THEY_HAVE
;
331 if (!we_knew_they_have
) {
332 add_object_array(o
, NULL
, &have_obj
);
338 static int reachable(struct commit
*want
)
340 struct prio_queue work
= { compare_commits_by_commit_date
};
342 prio_queue_put(&work
, want
);
344 struct commit_list
*list
;
345 struct commit
*commit
= prio_queue_get(&work
);
347 if (commit
->object
.flags
& THEY_HAVE
) {
348 want
->object
.flags
|= COMMON_KNOWN
;
351 if (!commit
->object
.parsed
)
352 parse_object(&commit
->object
.oid
);
353 if (commit
->object
.flags
& REACHABLE
)
355 commit
->object
.flags
|= REACHABLE
;
356 if (commit
->date
< oldest_have
)
358 for (list
= commit
->parents
; list
; list
= list
->next
) {
359 struct commit
*parent
= list
->item
;
360 if (!(parent
->object
.flags
& REACHABLE
))
361 prio_queue_put(&work
, parent
);
364 want
->object
.flags
|= REACHABLE
;
365 clear_commit_marks(want
, REACHABLE
);
366 clear_prio_queue(&work
);
367 return (want
->object
.flags
& COMMON_KNOWN
);
370 static int ok_to_give_up(void)
377 for (i
= 0; i
< want_obj
.nr
; i
++) {
378 struct object
*want
= want_obj
.objects
[i
].item
;
380 if (want
->flags
& COMMON_KNOWN
)
382 want
= deref_tag(want
, "a want line", 0);
383 if (!want
|| want
->type
!= OBJ_COMMIT
) {
384 /* no way to tell if this is reachable by
385 * looking at the ancestry chain alone, so
386 * leave a note to ourselves not to worry about
387 * this object anymore.
389 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
392 if (!reachable((struct commit
*)want
))
398 static int get_common_commits(void)
400 struct object_id oid
;
401 char last_hex
[GIT_MAX_HEXSZ
+ 1];
406 save_commit_buffer
= 0;
409 char *line
= packet_read_line(0, NULL
);
415 if (multi_ack
== 2 && got_common
416 && !got_other
&& ok_to_give_up()) {
418 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
420 if (have_obj
.nr
== 0 || multi_ack
)
421 packet_write_fmt(1, "NAK\n");
423 if (no_done
&& sent_ready
) {
424 packet_write_fmt(1, "ACK %s\n", last_hex
);
433 if (skip_prefix(line
, "have ", &arg
)) {
434 switch (got_oid(arg
, &oid
)) {
435 case -1: /* they have what we do not */
437 if (multi_ack
&& ok_to_give_up()) {
438 const char *hex
= oid_to_hex(&oid
);
439 if (multi_ack
== 2) {
441 packet_write_fmt(1, "ACK %s ready\n", hex
);
443 packet_write_fmt(1, "ACK %s continue\n", hex
);
448 oid_to_hex_r(last_hex
, &oid
);
450 packet_write_fmt(1, "ACK %s common\n", last_hex
);
452 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
453 else if (have_obj
.nr
== 1)
454 packet_write_fmt(1, "ACK %s\n", last_hex
);
459 if (!strcmp(line
, "done")) {
460 if (have_obj
.nr
> 0) {
462 packet_write_fmt(1, "ACK %s\n", last_hex
);
465 packet_write_fmt(1, "NAK\n");
468 die("git upload-pack: expected SHA1 list, got '%s'", line
);
472 static int is_our_ref(struct object
*o
)
474 int allow_hidden_ref
= (allow_unadvertised_object_request
&
475 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
476 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
480 * on successful case, it's up to the caller to close cmd->out
482 static int do_reachable_revlist(struct child_process
*cmd
,
483 struct object_array
*src
,
484 struct object_array
*reachable
)
486 static const char *argv
[] = {
487 "rev-list", "--stdin", NULL
,
490 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
500 * If the next rev-list --stdin encounters an unknown commit,
501 * it terminates, which will cause SIGPIPE in the write loop
504 sigchain_push(SIGPIPE
, SIG_IGN
);
506 if (start_command(cmd
))
510 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
511 for (i
= get_max_object_index(); 0 < i
; ) {
512 o
= get_indexed_object(--i
);
515 if (reachable
&& o
->type
== OBJ_COMMIT
)
516 o
->flags
&= ~TMP_MARK
;
519 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
520 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
523 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
524 for (i
= 0; i
< src
->nr
; i
++) {
525 o
= src
->objects
[i
].item
;
528 add_object_array(o
, NULL
, reachable
);
531 if (reachable
&& o
->type
== OBJ_COMMIT
)
532 o
->flags
|= TMP_MARK
;
533 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
534 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
539 sigchain_pop(SIGPIPE
);
544 sigchain_pop(SIGPIPE
);
553 static int get_reachable_list(struct object_array
*src
,
554 struct object_array
*reachable
)
556 struct child_process cmd
= CHILD_PROCESS_INIT
;
559 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
560 const unsigned hexsz
= the_hash_algo
->hexsz
;
562 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
565 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
566 struct object_id sha1
;
569 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
572 o
= lookup_object(sha1
.hash
);
573 if (o
&& o
->type
== OBJ_COMMIT
) {
574 o
->flags
&= ~TMP_MARK
;
577 for (i
= get_max_object_index(); 0 < i
; i
--) {
578 o
= get_indexed_object(i
- 1);
579 if (o
&& o
->type
== OBJ_COMMIT
&&
580 (o
->flags
& TMP_MARK
)) {
581 add_object_array(o
, NULL
, reachable
);
582 o
->flags
&= ~TMP_MARK
;
587 if (finish_command(&cmd
))
593 static int has_unreachable(struct object_array
*src
)
595 struct child_process cmd
= CHILD_PROCESS_INIT
;
599 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
603 * The commits out of the rev-list are not ancestors of
606 i
= read_in_full(cmd
.out
, buf
, 1);
613 * rev-list may have died by encountering a bad commit
614 * in the history, in which case we do want to bail out
615 * even when it showed no commit.
617 if (finish_command(&cmd
))
620 /* All the non-tip ones are ancestors of what we advertised */
624 sigchain_pop(SIGPIPE
);
630 static void check_non_tip(void)
635 * In the normal in-process case without
636 * uploadpack.allowReachableSHA1InWant,
637 * non-tip requests can never happen.
639 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
641 if (!has_unreachable(&want_obj
))
642 /* All the non-tip ones are ancestors of what we advertised */
646 /* Pick one of them (we know there at least is one) */
647 for (i
= 0; i
< want_obj
.nr
; i
++) {
648 struct object
*o
= want_obj
.objects
[i
].item
;
650 die("git upload-pack: not our ref %s",
651 oid_to_hex(&o
->oid
));
655 static void send_shallow(struct commit_list
*result
)
658 struct object
*object
= &result
->item
->object
;
659 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
660 packet_write_fmt(1, "shallow %s",
661 oid_to_hex(&object
->oid
));
662 register_shallow(the_repository
, &object
->oid
);
665 result
= result
->next
;
669 static void send_unshallow(const struct object_array
*shallows
)
673 for (i
= 0; i
< shallows
->nr
; i
++) {
674 struct object
*object
= shallows
->objects
[i
].item
;
675 if (object
->flags
& NOT_SHALLOW
) {
676 struct commit_list
*parents
;
677 packet_write_fmt(1, "unshallow %s",
678 oid_to_hex(&object
->oid
));
679 object
->flags
&= ~CLIENT_SHALLOW
;
681 * We want to _register_ "object" as shallow, but we
682 * also need to traverse object's parents to deepen a
683 * shallow clone. Unregister it for now so we can
684 * parse and add the parents to the want list, then
687 unregister_shallow(&object
->oid
);
689 parse_commit_or_die((struct commit
*)object
);
690 parents
= ((struct commit
*)object
)->parents
;
692 add_object_array(&parents
->item
->object
,
694 parents
= parents
->next
;
696 add_object_array(object
, NULL
, &extra_edge_obj
);
698 /* make sure commit traversal conforms to client */
699 register_shallow(the_repository
, &object
->oid
);
703 static void deepen(int depth
, int deepen_relative
,
704 struct object_array
*shallows
)
706 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
709 for (i
= 0; i
< shallows
->nr
; i
++) {
710 struct object
*object
= shallows
->objects
[i
].item
;
711 object
->flags
|= NOT_SHALLOW
;
713 } else if (deepen_relative
) {
714 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
715 struct commit_list
*result
;
717 get_reachable_list(shallows
, &reachable_shallows
);
718 result
= get_shallow_commits(&reachable_shallows
,
720 SHALLOW
, NOT_SHALLOW
);
721 send_shallow(result
);
722 free_commit_list(result
);
723 object_array_clear(&reachable_shallows
);
725 struct commit_list
*result
;
727 result
= get_shallow_commits(&want_obj
, depth
,
728 SHALLOW
, NOT_SHALLOW
);
729 send_shallow(result
);
730 free_commit_list(result
);
733 send_unshallow(shallows
);
736 static void deepen_by_rev_list(int ac
, const char **av
,
737 struct object_array
*shallows
)
739 struct commit_list
*result
;
741 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
742 send_shallow(result
);
743 free_commit_list(result
);
744 send_unshallow(shallows
);
747 /* Returns 1 if a shallow list is sent or 0 otherwise */
748 static int send_shallow_list(int depth
, int deepen_rev_list
,
749 timestamp_t deepen_since
,
750 struct string_list
*deepen_not
,
751 struct object_array
*shallows
)
755 if (depth
> 0 && deepen_rev_list
)
756 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
758 deepen(depth
, deepen_relative
, shallows
);
760 } else if (deepen_rev_list
) {
761 struct argv_array av
= ARGV_ARRAY_INIT
;
764 argv_array_push(&av
, "rev-list");
766 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
767 if (deepen_not
->nr
) {
768 argv_array_push(&av
, "--not");
769 for (i
= 0; i
< deepen_not
->nr
; i
++) {
770 struct string_list_item
*s
= deepen_not
->items
+ i
;
771 argv_array_push(&av
, s
->string
);
773 argv_array_push(&av
, "--not");
775 for (i
= 0; i
< want_obj
.nr
; i
++) {
776 struct object
*o
= want_obj
.objects
[i
].item
;
777 argv_array_push(&av
, oid_to_hex(&o
->oid
));
779 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
);
780 argv_array_clear(&av
);
783 if (shallows
->nr
> 0) {
785 for (i
= 0; i
< shallows
->nr
; i
++)
786 register_shallow(the_repository
,
787 &shallows
->objects
[i
].item
->oid
);
791 shallow_nr
+= shallows
->nr
;
795 static int process_shallow(const char *line
, struct object_array
*shallows
)
798 if (skip_prefix(line
, "shallow ", &arg
)) {
799 struct object_id oid
;
800 struct object
*object
;
801 if (get_oid_hex(arg
, &oid
))
802 die("invalid shallow line: %s", line
);
803 object
= parse_object(&oid
);
806 if (object
->type
!= OBJ_COMMIT
)
807 die("invalid shallow object %s", oid_to_hex(&oid
));
808 if (!(object
->flags
& CLIENT_SHALLOW
)) {
809 object
->flags
|= CLIENT_SHALLOW
;
810 add_object_array(object
, NULL
, shallows
);
818 static int process_deepen(const char *line
, int *depth
)
821 if (skip_prefix(line
, "deepen ", &arg
)) {
823 *depth
= (int)strtol(arg
, &end
, 0);
824 if (!end
|| *end
|| *depth
<= 0)
825 die("Invalid deepen: %s", line
);
832 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
835 if (skip_prefix(line
, "deepen-since ", &arg
)) {
837 *deepen_since
= parse_timestamp(arg
, &end
, 0);
838 if (!end
|| *end
|| !deepen_since
||
839 /* revisions.c's max_age -1 is special */
841 die("Invalid deepen-since: %s", line
);
842 *deepen_rev_list
= 1;
848 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
851 if (skip_prefix(line
, "deepen-not ", &arg
)) {
853 struct object_id oid
;
854 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
855 die("git upload-pack: ambiguous deepen-not: %s", line
);
856 string_list_append(deepen_not
, ref
);
858 *deepen_rev_list
= 1;
864 static void receive_needs(void)
866 struct object_array shallows
= OBJECT_ARRAY_INIT
;
867 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
870 timestamp_t deepen_since
= 0;
871 int deepen_rev_list
= 0;
876 const char *features
;
877 struct object_id oid_buf
;
878 char *line
= packet_read_line(0, NULL
);
885 if (process_shallow(line
, &shallows
))
887 if (process_deepen(line
, &depth
))
889 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
891 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
894 if (skip_prefix(line
, "filter ", &arg
)) {
895 if (!filter_capability_requested
)
896 die("git upload-pack: filtering capability not negotiated");
897 parse_list_objects_filter(&filter_options
, arg
);
901 if (!skip_prefix(line
, "want ", &arg
) ||
902 parse_oid_hex(arg
, &oid_buf
, &features
))
903 die("git upload-pack: protocol error, "
904 "expected to get object ID, not '%s'", line
);
906 if (parse_feature_request(features
, "deepen-relative"))
908 if (parse_feature_request(features
, "multi_ack_detailed"))
910 else if (parse_feature_request(features
, "multi_ack"))
912 if (parse_feature_request(features
, "no-done"))
914 if (parse_feature_request(features
, "thin-pack"))
916 if (parse_feature_request(features
, "ofs-delta"))
918 if (parse_feature_request(features
, "side-band-64k"))
919 use_sideband
= LARGE_PACKET_MAX
;
920 else if (parse_feature_request(features
, "side-band"))
921 use_sideband
= DEFAULT_PACKET_MAX
;
922 if (parse_feature_request(features
, "no-progress"))
924 if (parse_feature_request(features
, "include-tag"))
926 if (allow_filter
&& parse_feature_request(features
, "filter"))
927 filter_capability_requested
= 1;
929 o
= parse_object(&oid_buf
);
932 "ERR upload-pack: not our ref %s",
933 oid_to_hex(&oid_buf
));
934 die("git upload-pack: not our ref %s",
935 oid_to_hex(&oid_buf
));
937 if (!(o
->flags
& WANTED
)) {
939 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
942 add_object_array(o
, NULL
, &want_obj
);
947 * We have sent all our refs already, and the other end
948 * should have chosen out of them. When we are operating
949 * in the stateless RPC mode, however, their choice may
950 * have been based on the set of older refs advertised
951 * by another process that handled the initial request.
956 if (!use_sideband
&& daemon_mode
)
959 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
962 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
963 &deepen_not
, &shallows
))
965 object_array_clear(&shallows
);
968 /* return non-zero if the ref is hidden, otherwise 0 */
969 static int mark_our_ref(const char *refname
, const char *refname_full
,
970 const struct object_id
*oid
)
972 struct object
*o
= lookup_unknown_object(oid
->hash
);
974 if (ref_is_hidden(refname
, refname_full
)) {
975 o
->flags
|= HIDDEN_REF
;
982 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
983 int flag
, void *cb_data
)
985 const char *refname
= strip_namespace(refname_full
);
987 mark_our_ref(refname
, refname_full
, oid
);
991 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
993 struct string_list_item
*item
;
997 for_each_string_list_item(item
, symref
)
998 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1001 static int send_ref(const char *refname
, const struct object_id
*oid
,
1002 int flag
, void *cb_data
)
1004 static const char *capabilities
= "multi_ack thin-pack side-band"
1005 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1006 " deepen-relative no-progress include-tag multi_ack_detailed";
1007 const char *refname_nons
= strip_namespace(refname
);
1008 struct object_id peeled
;
1010 if (mark_our_ref(refname_nons
, refname
, oid
))
1014 struct strbuf symref_info
= STRBUF_INIT
;
1016 format_symref_info(&symref_info
, cb_data
);
1017 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1018 oid_to_hex(oid
), refname_nons
,
1020 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
1021 " allow-tip-sha1-in-want" : "",
1022 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1023 " allow-reachable-sha1-in-want" : "",
1024 stateless_rpc
? " no-done" : "",
1026 allow_filter
? " filter" : "",
1027 git_user_agent_sanitized());
1028 strbuf_release(&symref_info
);
1030 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1032 capabilities
= NULL
;
1033 if (!peel_ref(refname
, &peeled
))
1034 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1038 static int find_symref(const char *refname
, const struct object_id
*oid
,
1039 int flag
, void *cb_data
)
1041 const char *symref_target
;
1042 struct string_list_item
*item
;
1044 if ((flag
& REF_ISSYMREF
) == 0)
1046 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1047 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1048 die("'%s' is a symref but it is not?", refname
);
1049 item
= string_list_append(cb_data
, refname
);
1050 item
->util
= xstrdup(symref_target
);
1054 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1056 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1057 if (git_config_bool(var
, value
))
1058 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1060 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1061 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1062 if (git_config_bool(var
, value
))
1063 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1065 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1066 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1067 if (git_config_bool(var
, value
))
1068 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1070 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1071 } else if (!strcmp("uploadpack.keepalive", var
)) {
1072 keepalive
= git_config_int(var
, value
);
1075 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1076 if (!strcmp("uploadpack.packobjectshook", var
))
1077 return git_config_string(&pack_objects_hook
, var
, value
);
1078 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1079 allow_filter
= git_config_bool(var
, value
);
1081 return parse_hide_refs_config(var
, value
, "uploadpack");
1084 void upload_pack(struct upload_pack_options
*options
)
1086 struct string_list symref
= STRING_LIST_INIT_DUP
;
1088 stateless_rpc
= options
->stateless_rpc
;
1089 timeout
= options
->timeout
;
1090 daemon_mode
= options
->daemon_mode
;
1092 git_config(upload_pack_config
, NULL
);
1094 head_ref_namespaced(find_symref
, &symref
);
1096 if (options
->advertise_refs
|| !stateless_rpc
) {
1098 head_ref_namespaced(send_ref
, &symref
);
1099 for_each_namespaced_ref(send_ref
, &symref
);
1100 advertise_shallow_grafts(1);
1103 head_ref_namespaced(check_ref
, NULL
);
1104 for_each_namespaced_ref(check_ref
, NULL
);
1106 string_list_clear(&symref
, 1);
1107 if (options
->advertise_refs
)
1112 get_common_commits();
1117 struct upload_pack_data
{
1118 struct object_array wants
;
1119 struct oid_array haves
;
1121 struct object_array shallows
;
1122 struct string_list deepen_not
;
1124 timestamp_t deepen_since
;
1125 int deepen_rev_list
;
1126 int deepen_relative
;
1128 unsigned stateless_rpc
: 1;
1130 unsigned use_thin_pack
: 1;
1131 unsigned use_ofs_delta
: 1;
1132 unsigned no_progress
: 1;
1133 unsigned use_include_tag
: 1;
1137 static void upload_pack_data_init(struct upload_pack_data
*data
)
1139 struct object_array wants
= OBJECT_ARRAY_INIT
;
1140 struct oid_array haves
= OID_ARRAY_INIT
;
1141 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1142 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1144 memset(data
, 0, sizeof(*data
));
1145 data
->wants
= wants
;
1146 data
->haves
= haves
;
1147 data
->shallows
= shallows
;
1148 data
->deepen_not
= deepen_not
;
1151 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1153 object_array_clear(&data
->wants
);
1154 oid_array_clear(&data
->haves
);
1155 object_array_clear(&data
->shallows
);
1156 string_list_clear(&data
->deepen_not
, 0);
1159 static int parse_want(const char *line
)
1162 if (skip_prefix(line
, "want ", &arg
)) {
1163 struct object_id oid
;
1166 if (get_oid_hex(arg
, &oid
))
1167 die("git upload-pack: protocol error, "
1168 "expected to get oid, not '%s'", line
);
1170 o
= parse_object(&oid
);
1173 "ERR upload-pack: not our ref %s",
1175 die("git upload-pack: not our ref %s",
1179 if (!(o
->flags
& WANTED
)) {
1181 add_object_array(o
, NULL
, &want_obj
);
1190 static int parse_have(const char *line
, struct oid_array
*haves
)
1193 if (skip_prefix(line
, "have ", &arg
)) {
1194 struct object_id oid
;
1196 if (get_oid_hex(arg
, &oid
))
1197 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1198 oid_array_append(haves
, &oid
);
1205 static void process_args(struct packet_reader
*request
,
1206 struct upload_pack_data
*data
)
1208 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1209 const char *arg
= request
->line
;
1213 if (parse_want(arg
))
1215 /* process have line */
1216 if (parse_have(arg
, &data
->haves
))
1219 /* process args like thin-pack */
1220 if (!strcmp(arg
, "thin-pack")) {
1224 if (!strcmp(arg
, "ofs-delta")) {
1228 if (!strcmp(arg
, "no-progress")) {
1232 if (!strcmp(arg
, "include-tag")) {
1233 use_include_tag
= 1;
1236 if (!strcmp(arg
, "done")) {
1241 /* Shallow related arguments */
1242 if (process_shallow(arg
, &data
->shallows
))
1244 if (process_deepen(arg
, &data
->depth
))
1246 if (process_deepen_since(arg
, &data
->deepen_since
,
1247 &data
->deepen_rev_list
))
1249 if (process_deepen_not(arg
, &data
->deepen_not
,
1250 &data
->deepen_rev_list
))
1252 if (!strcmp(arg
, "deepen-relative")) {
1253 data
->deepen_relative
= 1;
1257 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1258 parse_list_objects_filter(&filter_options
, p
);
1262 /* ignore unknown lines maybe? */
1263 die("unexpected line: '%s'", arg
);
1267 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
)
1272 for (i
= 0; i
< haves
->nr
; i
++) {
1273 const struct object_id
*oid
= &haves
->oid
[i
];
1275 int we_knew_they_have
= 0;
1277 if (!has_object_file(oid
))
1280 oid_array_append(common
, oid
);
1282 o
= parse_object(oid
);
1284 die("oops (%s)", oid_to_hex(oid
));
1285 if (o
->type
== OBJ_COMMIT
) {
1286 struct commit_list
*parents
;
1287 struct commit
*commit
= (struct commit
*)o
;
1288 if (o
->flags
& THEY_HAVE
)
1289 we_knew_they_have
= 1;
1291 o
->flags
|= THEY_HAVE
;
1292 if (!oldest_have
|| (commit
->date
< oldest_have
))
1293 oldest_have
= commit
->date
;
1294 for (parents
= commit
->parents
;
1296 parents
= parents
->next
)
1297 parents
->item
->object
.flags
|= THEY_HAVE
;
1299 if (!we_knew_they_have
)
1300 add_object_array(o
, NULL
, &have_obj
);
1306 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
)
1310 packet_buf_write(response
, "acknowledgments\n");
1314 packet_buf_write(response
, "NAK\n");
1316 for (i
= 0; i
< acks
->nr
; i
++) {
1317 packet_buf_write(response
, "ACK %s\n",
1318 oid_to_hex(&acks
->oid
[i
]));
1321 if (ok_to_give_up()) {
1323 packet_buf_write(response
, "ready\n");
1330 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1332 struct oid_array common
= OID_ARRAY_INIT
;
1333 struct strbuf response
= STRBUF_INIT
;
1336 process_haves(&data
->haves
, &common
);
1339 } else if (send_acks(&common
, &response
)) {
1340 packet_buf_delim(&response
);
1344 packet_buf_flush(&response
);
1349 write_or_die(1, response
.buf
, response
.len
);
1350 strbuf_release(&response
);
1352 oid_array_clear(&data
->haves
);
1353 oid_array_clear(&common
);
1357 static void send_shallow_info(struct upload_pack_data
*data
)
1359 /* No shallow info needs to be sent */
1360 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1361 !is_repository_shallow(the_repository
))
1364 packet_write_fmt(1, "shallow-info\n");
1366 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1367 data
->deepen_since
, &data
->deepen_not
,
1369 is_repository_shallow(the_repository
))
1370 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
);
1376 FETCH_PROCESS_ARGS
= 0,
1382 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1383 struct packet_reader
*request
)
1385 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1386 struct upload_pack_data data
;
1388 git_config(upload_pack_config
, NULL
);
1390 upload_pack_data_init(&data
);
1391 use_sideband
= LARGE_PACKET_MAX
;
1393 while (state
!= FETCH_DONE
) {
1395 case FETCH_PROCESS_ARGS
:
1396 process_args(request
, &data
);
1400 * Request didn't contain any 'want' lines,
1401 * guess they didn't want anything.
1404 } else if (data
.haves
.nr
) {
1406 * Request had 'have' lines, so lets ACK them.
1408 state
= FETCH_SEND_ACKS
;
1411 * Request had 'want's but no 'have's so we can
1412 * immedietly go to construct and send a pack.
1414 state
= FETCH_SEND_PACK
;
1417 case FETCH_SEND_ACKS
:
1418 if (process_haves_and_send_acks(&data
))
1419 state
= FETCH_SEND_PACK
;
1423 case FETCH_SEND_PACK
:
1424 send_shallow_info(&data
);
1426 packet_write_fmt(1, "packfile\n");
1435 upload_pack_data_clear(&data
);
1439 int upload_pack_advertise(struct repository
*r
,
1440 struct strbuf
*value
)
1443 int allow_filter_value
;
1444 strbuf_addstr(value
, "shallow");
1445 if (!repo_config_get_bool(the_repository
,
1446 "uploadpack.allowfilter",
1447 &allow_filter_value
) &&
1449 strbuf_addstr(value
, " filter");