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-reach.h"
29 /* Remember to update object flag allocation in object.h */
30 #define THEY_HAVE (1u << 11)
31 #define OUR_REF (1u << 12)
32 #define WANTED (1u << 13)
33 #define COMMON_KNOWN (1u << 14)
35 #define SHALLOW (1u << 16)
36 #define NOT_SHALLOW (1u << 17)
37 #define CLIENT_SHALLOW (1u << 18)
38 #define HIDDEN_REF (1u << 19)
40 static timestamp_t oldest_have
;
42 static int deepen_relative
;
45 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
46 static int no_progress
, daemon_mode
;
47 /* Allow specifying sha1 if it is a ref tip. */
48 #define ALLOW_TIP_SHA1 01
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 #define ALLOW_REACHABLE_SHA1 02
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 #define ALLOW_ANY_SHA1 07
53 static unsigned int allow_unadvertised_object_request
;
54 static int shallow_nr
;
55 static struct object_array have_obj
;
56 static struct object_array want_obj
;
57 static struct object_array extra_edge_obj
;
58 static unsigned int timeout
;
59 static int keepalive
= 5;
61 * otherwise maximum packet size (up to 65520 bytes).
63 static int use_sideband
;
64 static int stateless_rpc
;
65 static const char *pack_objects_hook
;
67 static int filter_capability_requested
;
68 static int allow_filter
;
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(the_repository
, 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 ok_to_give_up(void)
344 return can_all_from_reach_with_flag(&want_obj
, THEY_HAVE
,
345 COMMON_KNOWN
, oldest_have
);
348 static int get_common_commits(void)
350 struct object_id oid
;
351 char last_hex
[GIT_MAX_HEXSZ
+ 1];
356 save_commit_buffer
= 0;
359 char *line
= packet_read_line(0, NULL
);
365 if (multi_ack
== 2 && got_common
366 && !got_other
&& ok_to_give_up()) {
368 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
370 if (have_obj
.nr
== 0 || multi_ack
)
371 packet_write_fmt(1, "NAK\n");
373 if (no_done
&& sent_ready
) {
374 packet_write_fmt(1, "ACK %s\n", last_hex
);
383 if (skip_prefix(line
, "have ", &arg
)) {
384 switch (got_oid(arg
, &oid
)) {
385 case -1: /* they have what we do not */
387 if (multi_ack
&& ok_to_give_up()) {
388 const char *hex
= oid_to_hex(&oid
);
389 if (multi_ack
== 2) {
391 packet_write_fmt(1, "ACK %s ready\n", hex
);
393 packet_write_fmt(1, "ACK %s continue\n", hex
);
398 oid_to_hex_r(last_hex
, &oid
);
400 packet_write_fmt(1, "ACK %s common\n", last_hex
);
402 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
403 else if (have_obj
.nr
== 1)
404 packet_write_fmt(1, "ACK %s\n", last_hex
);
409 if (!strcmp(line
, "done")) {
410 if (have_obj
.nr
> 0) {
412 packet_write_fmt(1, "ACK %s\n", last_hex
);
415 packet_write_fmt(1, "NAK\n");
418 die("git upload-pack: expected SHA1 list, got '%s'", line
);
422 static int is_our_ref(struct object
*o
)
424 int allow_hidden_ref
= (allow_unadvertised_object_request
&
425 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
426 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
430 * on successful case, it's up to the caller to close cmd->out
432 static int do_reachable_revlist(struct child_process
*cmd
,
433 struct object_array
*src
,
434 struct object_array
*reachable
)
436 static const char *argv
[] = {
437 "rev-list", "--stdin", NULL
,
440 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
450 * If the next rev-list --stdin encounters an unknown commit,
451 * it terminates, which will cause SIGPIPE in the write loop
454 sigchain_push(SIGPIPE
, SIG_IGN
);
456 if (start_command(cmd
))
460 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
461 for (i
= get_max_object_index(); 0 < i
; ) {
462 o
= get_indexed_object(--i
);
465 if (reachable
&& o
->type
== OBJ_COMMIT
)
466 o
->flags
&= ~TMP_MARK
;
469 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
470 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
473 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
474 for (i
= 0; i
< src
->nr
; i
++) {
475 o
= src
->objects
[i
].item
;
478 add_object_array(o
, NULL
, reachable
);
481 if (reachable
&& o
->type
== OBJ_COMMIT
)
482 o
->flags
|= TMP_MARK
;
483 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
484 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
489 sigchain_pop(SIGPIPE
);
494 sigchain_pop(SIGPIPE
);
503 static int get_reachable_list(struct object_array
*src
,
504 struct object_array
*reachable
)
506 struct child_process cmd
= CHILD_PROCESS_INIT
;
509 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
510 const unsigned hexsz
= the_hash_algo
->hexsz
;
512 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
515 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
516 struct object_id sha1
;
519 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
522 o
= lookup_object(the_repository
, sha1
.hash
);
523 if (o
&& o
->type
== OBJ_COMMIT
) {
524 o
->flags
&= ~TMP_MARK
;
527 for (i
= get_max_object_index(); 0 < i
; i
--) {
528 o
= get_indexed_object(i
- 1);
529 if (o
&& o
->type
== OBJ_COMMIT
&&
530 (o
->flags
& TMP_MARK
)) {
531 add_object_array(o
, NULL
, reachable
);
532 o
->flags
&= ~TMP_MARK
;
537 if (finish_command(&cmd
))
543 static int has_unreachable(struct object_array
*src
)
545 struct child_process cmd
= CHILD_PROCESS_INIT
;
549 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
553 * The commits out of the rev-list are not ancestors of
556 i
= read_in_full(cmd
.out
, buf
, 1);
563 * rev-list may have died by encountering a bad commit
564 * in the history, in which case we do want to bail out
565 * even when it showed no commit.
567 if (finish_command(&cmd
))
570 /* All the non-tip ones are ancestors of what we advertised */
574 sigchain_pop(SIGPIPE
);
580 static void check_non_tip(void)
585 * In the normal in-process case without
586 * uploadpack.allowReachableSHA1InWant,
587 * non-tip requests can never happen.
589 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
591 if (!has_unreachable(&want_obj
))
592 /* All the non-tip ones are ancestors of what we advertised */
596 /* Pick one of them (we know there at least is one) */
597 for (i
= 0; i
< want_obj
.nr
; i
++) {
598 struct object
*o
= want_obj
.objects
[i
].item
;
600 die("git upload-pack: not our ref %s",
601 oid_to_hex(&o
->oid
));
605 static void send_shallow(struct commit_list
*result
)
608 struct object
*object
= &result
->item
->object
;
609 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
610 packet_write_fmt(1, "shallow %s",
611 oid_to_hex(&object
->oid
));
612 register_shallow(the_repository
, &object
->oid
);
615 result
= result
->next
;
619 static void send_unshallow(const struct object_array
*shallows
)
623 for (i
= 0; i
< shallows
->nr
; i
++) {
624 struct object
*object
= shallows
->objects
[i
].item
;
625 if (object
->flags
& NOT_SHALLOW
) {
626 struct commit_list
*parents
;
627 packet_write_fmt(1, "unshallow %s",
628 oid_to_hex(&object
->oid
));
629 object
->flags
&= ~CLIENT_SHALLOW
;
631 * We want to _register_ "object" as shallow, but we
632 * also need to traverse object's parents to deepen a
633 * shallow clone. Unregister it for now so we can
634 * parse and add the parents to the want list, then
637 unregister_shallow(&object
->oid
);
639 parse_commit_or_die((struct commit
*)object
);
640 parents
= ((struct commit
*)object
)->parents
;
642 add_object_array(&parents
->item
->object
,
644 parents
= parents
->next
;
646 add_object_array(object
, NULL
, &extra_edge_obj
);
648 /* make sure commit traversal conforms to client */
649 register_shallow(the_repository
, &object
->oid
);
653 static void deepen(int depth
, int deepen_relative
,
654 struct object_array
*shallows
)
656 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
659 for (i
= 0; i
< shallows
->nr
; i
++) {
660 struct object
*object
= shallows
->objects
[i
].item
;
661 object
->flags
|= NOT_SHALLOW
;
663 } else if (deepen_relative
) {
664 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
665 struct commit_list
*result
;
667 get_reachable_list(shallows
, &reachable_shallows
);
668 result
= get_shallow_commits(&reachable_shallows
,
670 SHALLOW
, NOT_SHALLOW
);
671 send_shallow(result
);
672 free_commit_list(result
);
673 object_array_clear(&reachable_shallows
);
675 struct commit_list
*result
;
677 result
= get_shallow_commits(&want_obj
, depth
,
678 SHALLOW
, NOT_SHALLOW
);
679 send_shallow(result
);
680 free_commit_list(result
);
683 send_unshallow(shallows
);
686 static void deepen_by_rev_list(int ac
, const char **av
,
687 struct object_array
*shallows
)
689 struct commit_list
*result
;
691 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
692 send_shallow(result
);
693 free_commit_list(result
);
694 send_unshallow(shallows
);
697 /* Returns 1 if a shallow list is sent or 0 otherwise */
698 static int send_shallow_list(int depth
, int deepen_rev_list
,
699 timestamp_t deepen_since
,
700 struct string_list
*deepen_not
,
701 struct object_array
*shallows
)
705 if (depth
> 0 && deepen_rev_list
)
706 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
708 deepen(depth
, deepen_relative
, shallows
);
710 } else if (deepen_rev_list
) {
711 struct argv_array av
= ARGV_ARRAY_INIT
;
714 argv_array_push(&av
, "rev-list");
716 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
717 if (deepen_not
->nr
) {
718 argv_array_push(&av
, "--not");
719 for (i
= 0; i
< deepen_not
->nr
; i
++) {
720 struct string_list_item
*s
= deepen_not
->items
+ i
;
721 argv_array_push(&av
, s
->string
);
723 argv_array_push(&av
, "--not");
725 for (i
= 0; i
< want_obj
.nr
; i
++) {
726 struct object
*o
= want_obj
.objects
[i
].item
;
727 argv_array_push(&av
, oid_to_hex(&o
->oid
));
729 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
);
730 argv_array_clear(&av
);
733 if (shallows
->nr
> 0) {
735 for (i
= 0; i
< shallows
->nr
; i
++)
736 register_shallow(the_repository
,
737 &shallows
->objects
[i
].item
->oid
);
741 shallow_nr
+= shallows
->nr
;
745 static int process_shallow(const char *line
, struct object_array
*shallows
)
748 if (skip_prefix(line
, "shallow ", &arg
)) {
749 struct object_id oid
;
750 struct object
*object
;
751 if (get_oid_hex(arg
, &oid
))
752 die("invalid shallow line: %s", line
);
753 object
= parse_object(the_repository
, &oid
);
756 if (object
->type
!= OBJ_COMMIT
)
757 die("invalid shallow object %s", oid_to_hex(&oid
));
758 if (!(object
->flags
& CLIENT_SHALLOW
)) {
759 object
->flags
|= CLIENT_SHALLOW
;
760 add_object_array(object
, NULL
, shallows
);
768 static int process_deepen(const char *line
, int *depth
)
771 if (skip_prefix(line
, "deepen ", &arg
)) {
773 *depth
= (int)strtol(arg
, &end
, 0);
774 if (!end
|| *end
|| *depth
<= 0)
775 die("Invalid deepen: %s", line
);
782 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
785 if (skip_prefix(line
, "deepen-since ", &arg
)) {
787 *deepen_since
= parse_timestamp(arg
, &end
, 0);
788 if (!end
|| *end
|| !deepen_since
||
789 /* revisions.c's max_age -1 is special */
791 die("Invalid deepen-since: %s", line
);
792 *deepen_rev_list
= 1;
798 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
801 if (skip_prefix(line
, "deepen-not ", &arg
)) {
803 struct object_id oid
;
804 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
805 die("git upload-pack: ambiguous deepen-not: %s", line
);
806 string_list_append(deepen_not
, ref
);
808 *deepen_rev_list
= 1;
814 static void receive_needs(void)
816 struct object_array shallows
= OBJECT_ARRAY_INIT
;
817 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
820 timestamp_t deepen_since
= 0;
821 int deepen_rev_list
= 0;
826 const char *features
;
827 struct object_id oid_buf
;
828 char *line
= packet_read_line(0, NULL
);
835 if (process_shallow(line
, &shallows
))
837 if (process_deepen(line
, &depth
))
839 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
841 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
844 if (skip_prefix(line
, "filter ", &arg
)) {
845 if (!filter_capability_requested
)
846 die("git upload-pack: filtering capability not negotiated");
847 parse_list_objects_filter(&filter_options
, arg
);
851 if (!skip_prefix(line
, "want ", &arg
) ||
852 parse_oid_hex(arg
, &oid_buf
, &features
))
853 die("git upload-pack: protocol error, "
854 "expected to get object ID, not '%s'", line
);
856 if (parse_feature_request(features
, "deepen-relative"))
858 if (parse_feature_request(features
, "multi_ack_detailed"))
860 else if (parse_feature_request(features
, "multi_ack"))
862 if (parse_feature_request(features
, "no-done"))
864 if (parse_feature_request(features
, "thin-pack"))
866 if (parse_feature_request(features
, "ofs-delta"))
868 if (parse_feature_request(features
, "side-band-64k"))
869 use_sideband
= LARGE_PACKET_MAX
;
870 else if (parse_feature_request(features
, "side-band"))
871 use_sideband
= DEFAULT_PACKET_MAX
;
872 if (parse_feature_request(features
, "no-progress"))
874 if (parse_feature_request(features
, "include-tag"))
876 if (allow_filter
&& parse_feature_request(features
, "filter"))
877 filter_capability_requested
= 1;
879 o
= parse_object(the_repository
, &oid_buf
);
882 "ERR upload-pack: not our ref %s",
883 oid_to_hex(&oid_buf
));
884 die("git upload-pack: not our ref %s",
885 oid_to_hex(&oid_buf
));
887 if (!(o
->flags
& WANTED
)) {
889 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
892 add_object_array(o
, NULL
, &want_obj
);
897 * We have sent all our refs already, and the other end
898 * should have chosen out of them. When we are operating
899 * in the stateless RPC mode, however, their choice may
900 * have been based on the set of older refs advertised
901 * by another process that handled the initial request.
906 if (!use_sideband
&& daemon_mode
)
909 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
912 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
913 &deepen_not
, &shallows
))
915 object_array_clear(&shallows
);
918 /* return non-zero if the ref is hidden, otherwise 0 */
919 static int mark_our_ref(const char *refname
, const char *refname_full
,
920 const struct object_id
*oid
)
922 struct object
*o
= lookup_unknown_object(oid
->hash
);
924 if (ref_is_hidden(refname
, refname_full
)) {
925 o
->flags
|= HIDDEN_REF
;
932 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
933 int flag
, void *cb_data
)
935 const char *refname
= strip_namespace(refname_full
);
937 mark_our_ref(refname
, refname_full
, oid
);
941 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
943 struct string_list_item
*item
;
947 for_each_string_list_item(item
, symref
)
948 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
951 static int send_ref(const char *refname
, const struct object_id
*oid
,
952 int flag
, void *cb_data
)
954 static const char *capabilities
= "multi_ack thin-pack side-band"
955 " side-band-64k ofs-delta shallow deepen-since deepen-not"
956 " deepen-relative no-progress include-tag multi_ack_detailed";
957 const char *refname_nons
= strip_namespace(refname
);
958 struct object_id peeled
;
960 if (mark_our_ref(refname_nons
, refname
, oid
))
964 struct strbuf symref_info
= STRBUF_INIT
;
966 format_symref_info(&symref_info
, cb_data
);
967 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
968 oid_to_hex(oid
), refname_nons
,
970 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
971 " allow-tip-sha1-in-want" : "",
972 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
973 " allow-reachable-sha1-in-want" : "",
974 stateless_rpc
? " no-done" : "",
976 allow_filter
? " filter" : "",
977 git_user_agent_sanitized());
978 strbuf_release(&symref_info
);
980 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
983 if (!peel_ref(refname
, &peeled
))
984 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
988 static int find_symref(const char *refname
, const struct object_id
*oid
,
989 int flag
, void *cb_data
)
991 const char *symref_target
;
992 struct string_list_item
*item
;
994 if ((flag
& REF_ISSYMREF
) == 0)
996 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
997 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
998 die("'%s' is a symref but it is not?", refname
);
999 item
= string_list_append(cb_data
, refname
);
1000 item
->util
= xstrdup(symref_target
);
1004 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1006 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1007 if (git_config_bool(var
, value
))
1008 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1010 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1011 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1012 if (git_config_bool(var
, value
))
1013 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1015 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1016 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1017 if (git_config_bool(var
, value
))
1018 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1020 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1021 } else if (!strcmp("uploadpack.keepalive", var
)) {
1022 keepalive
= git_config_int(var
, value
);
1025 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1026 if (!strcmp("uploadpack.packobjectshook", var
))
1027 return git_config_string(&pack_objects_hook
, var
, value
);
1028 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1029 allow_filter
= git_config_bool(var
, value
);
1031 return parse_hide_refs_config(var
, value
, "uploadpack");
1034 void upload_pack(struct upload_pack_options
*options
)
1036 struct string_list symref
= STRING_LIST_INIT_DUP
;
1038 stateless_rpc
= options
->stateless_rpc
;
1039 timeout
= options
->timeout
;
1040 daemon_mode
= options
->daemon_mode
;
1042 git_config(upload_pack_config
, NULL
);
1044 head_ref_namespaced(find_symref
, &symref
);
1046 if (options
->advertise_refs
|| !stateless_rpc
) {
1048 head_ref_namespaced(send_ref
, &symref
);
1049 for_each_namespaced_ref(send_ref
, &symref
);
1050 advertise_shallow_grafts(1);
1053 head_ref_namespaced(check_ref
, NULL
);
1054 for_each_namespaced_ref(check_ref
, NULL
);
1056 string_list_clear(&symref
, 1);
1057 if (options
->advertise_refs
)
1062 get_common_commits();
1067 struct upload_pack_data
{
1068 struct object_array wants
;
1069 struct oid_array haves
;
1071 struct object_array shallows
;
1072 struct string_list deepen_not
;
1074 timestamp_t deepen_since
;
1075 int deepen_rev_list
;
1076 int deepen_relative
;
1078 unsigned stateless_rpc
: 1;
1080 unsigned use_thin_pack
: 1;
1081 unsigned use_ofs_delta
: 1;
1082 unsigned no_progress
: 1;
1083 unsigned use_include_tag
: 1;
1087 static void upload_pack_data_init(struct upload_pack_data
*data
)
1089 struct object_array wants
= OBJECT_ARRAY_INIT
;
1090 struct oid_array haves
= OID_ARRAY_INIT
;
1091 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1092 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1094 memset(data
, 0, sizeof(*data
));
1095 data
->wants
= wants
;
1096 data
->haves
= haves
;
1097 data
->shallows
= shallows
;
1098 data
->deepen_not
= deepen_not
;
1101 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1103 object_array_clear(&data
->wants
);
1104 oid_array_clear(&data
->haves
);
1105 object_array_clear(&data
->shallows
);
1106 string_list_clear(&data
->deepen_not
, 0);
1109 static int parse_want(const char *line
)
1112 if (skip_prefix(line
, "want ", &arg
)) {
1113 struct object_id oid
;
1116 if (get_oid_hex(arg
, &oid
))
1117 die("git upload-pack: protocol error, "
1118 "expected to get oid, not '%s'", line
);
1120 o
= parse_object(the_repository
, &oid
);
1123 "ERR upload-pack: not our ref %s",
1125 die("git upload-pack: not our ref %s",
1129 if (!(o
->flags
& WANTED
)) {
1131 add_object_array(o
, NULL
, &want_obj
);
1140 static int parse_have(const char *line
, struct oid_array
*haves
)
1143 if (skip_prefix(line
, "have ", &arg
)) {
1144 struct object_id oid
;
1146 if (get_oid_hex(arg
, &oid
))
1147 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1148 oid_array_append(haves
, &oid
);
1155 static void process_args(struct packet_reader
*request
,
1156 struct upload_pack_data
*data
)
1158 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1159 const char *arg
= request
->line
;
1163 if (parse_want(arg
))
1165 /* process have line */
1166 if (parse_have(arg
, &data
->haves
))
1169 /* process args like thin-pack */
1170 if (!strcmp(arg
, "thin-pack")) {
1174 if (!strcmp(arg
, "ofs-delta")) {
1178 if (!strcmp(arg
, "no-progress")) {
1182 if (!strcmp(arg
, "include-tag")) {
1183 use_include_tag
= 1;
1186 if (!strcmp(arg
, "done")) {
1191 /* Shallow related arguments */
1192 if (process_shallow(arg
, &data
->shallows
))
1194 if (process_deepen(arg
, &data
->depth
))
1196 if (process_deepen_since(arg
, &data
->deepen_since
,
1197 &data
->deepen_rev_list
))
1199 if (process_deepen_not(arg
, &data
->deepen_not
,
1200 &data
->deepen_rev_list
))
1202 if (!strcmp(arg
, "deepen-relative")) {
1203 data
->deepen_relative
= 1;
1207 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1208 parse_list_objects_filter(&filter_options
, p
);
1212 /* ignore unknown lines maybe? */
1213 die("unexpected line: '%s'", arg
);
1217 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
)
1222 for (i
= 0; i
< haves
->nr
; i
++) {
1223 const struct object_id
*oid
= &haves
->oid
[i
];
1225 int we_knew_they_have
= 0;
1227 if (!has_object_file(oid
))
1230 oid_array_append(common
, oid
);
1232 o
= parse_object(the_repository
, oid
);
1234 die("oops (%s)", oid_to_hex(oid
));
1235 if (o
->type
== OBJ_COMMIT
) {
1236 struct commit_list
*parents
;
1237 struct commit
*commit
= (struct commit
*)o
;
1238 if (o
->flags
& THEY_HAVE
)
1239 we_knew_they_have
= 1;
1241 o
->flags
|= THEY_HAVE
;
1242 if (!oldest_have
|| (commit
->date
< oldest_have
))
1243 oldest_have
= commit
->date
;
1244 for (parents
= commit
->parents
;
1246 parents
= parents
->next
)
1247 parents
->item
->object
.flags
|= THEY_HAVE
;
1249 if (!we_knew_they_have
)
1250 add_object_array(o
, NULL
, &have_obj
);
1256 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
)
1260 packet_buf_write(response
, "acknowledgments\n");
1264 packet_buf_write(response
, "NAK\n");
1266 for (i
= 0; i
< acks
->nr
; i
++) {
1267 packet_buf_write(response
, "ACK %s\n",
1268 oid_to_hex(&acks
->oid
[i
]));
1271 if (ok_to_give_up()) {
1273 packet_buf_write(response
, "ready\n");
1280 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1282 struct oid_array common
= OID_ARRAY_INIT
;
1283 struct strbuf response
= STRBUF_INIT
;
1286 process_haves(&data
->haves
, &common
);
1289 } else if (send_acks(&common
, &response
)) {
1290 packet_buf_delim(&response
);
1294 packet_buf_flush(&response
);
1299 write_or_die(1, response
.buf
, response
.len
);
1300 strbuf_release(&response
);
1302 oid_array_clear(&data
->haves
);
1303 oid_array_clear(&common
);
1307 static void send_shallow_info(struct upload_pack_data
*data
)
1309 /* No shallow info needs to be sent */
1310 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1311 !is_repository_shallow(the_repository
))
1314 packet_write_fmt(1, "shallow-info\n");
1316 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1317 data
->deepen_since
, &data
->deepen_not
,
1319 is_repository_shallow(the_repository
))
1320 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
);
1326 FETCH_PROCESS_ARGS
= 0,
1332 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1333 struct packet_reader
*request
)
1335 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1336 struct upload_pack_data data
;
1338 git_config(upload_pack_config
, NULL
);
1340 upload_pack_data_init(&data
);
1341 use_sideband
= LARGE_PACKET_MAX
;
1343 while (state
!= FETCH_DONE
) {
1345 case FETCH_PROCESS_ARGS
:
1346 process_args(request
, &data
);
1350 * Request didn't contain any 'want' lines,
1351 * guess they didn't want anything.
1354 } else if (data
.haves
.nr
) {
1356 * Request had 'have' lines, so lets ACK them.
1358 state
= FETCH_SEND_ACKS
;
1361 * Request had 'want's but no 'have's so we can
1362 * immedietly go to construct and send a pack.
1364 state
= FETCH_SEND_PACK
;
1367 case FETCH_SEND_ACKS
:
1368 if (process_haves_and_send_acks(&data
))
1369 state
= FETCH_SEND_PACK
;
1373 case FETCH_SEND_PACK
:
1374 send_shallow_info(&data
);
1376 packet_write_fmt(1, "packfile\n");
1385 upload_pack_data_clear(&data
);
1389 int upload_pack_advertise(struct repository
*r
,
1390 struct strbuf
*value
)
1393 int allow_filter_value
;
1394 strbuf_addstr(value
, "shallow");
1395 if (!repo_config_get_bool(the_repository
,
1396 "uploadpack.allowfilter",
1397 &allow_filter_value
) &&
1399 strbuf_addstr(value
, " filter");