6 #include "repository.h"
7 #include "object-store.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "run-command.h"
20 #include "string-list.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
25 #include "upload-pack.h"
27 #include "commit-graph.h"
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)
34 #define REACHABLE (1u << 15)
36 #define SHALLOW (1u << 16)
37 #define NOT_SHALLOW (1u << 17)
38 #define CLIENT_SHALLOW (1u << 18)
39 #define HIDDEN_REF (1u << 19)
41 static timestamp_t oldest_have
;
43 static int deepen_relative
;
46 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
47 static int no_progress
, daemon_mode
;
48 /* Allow specifying sha1 if it is a ref tip. */
49 #define ALLOW_TIP_SHA1 01
50 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51 #define ALLOW_REACHABLE_SHA1 02
52 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53 #define ALLOW_ANY_SHA1 07
54 static unsigned int allow_unadvertised_object_request
;
55 static int shallow_nr
;
56 static struct object_array have_obj
;
57 static struct object_array want_obj
;
58 static struct object_array extra_edge_obj
;
59 static unsigned int timeout
;
60 static int keepalive
= 5;
62 * otherwise maximum packet size (up to 65520 bytes).
64 static int use_sideband
;
65 static int stateless_rpc
;
66 static const char *pack_objects_hook
;
68 static int filter_capability_requested
;
69 static int allow_filter
;
70 static int allow_ref_in_want
;
71 static struct list_objects_filter_options filter_options
;
73 static void reset_timeout(void)
78 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
81 send_sideband(1, fd
, data
, sz
, use_sideband
);
88 /* XXX: are we happy to lose stuff here? */
92 write_or_die(fd
, data
, sz
);
95 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
98 if (graft
->nr_parent
== -1)
99 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
103 static void create_pack_file(void)
105 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
106 char data
[8193], progress
[128];
107 char abort_msg
[] = "aborting due to possible repository "
108 "corruption on the remote side.";
114 if (!pack_objects_hook
)
115 pack_objects
.git_cmd
= 1;
117 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
118 argv_array_push(&pack_objects
.args
, "git");
119 pack_objects
.use_shell
= 1;
123 argv_array_push(&pack_objects
.args
, "--shallow-file");
124 argv_array_push(&pack_objects
.args
, "");
126 argv_array_push(&pack_objects
.args
, "pack-objects");
127 argv_array_push(&pack_objects
.args
, "--revs");
129 argv_array_push(&pack_objects
.args
, "--thin");
131 argv_array_push(&pack_objects
.args
, "--stdout");
133 argv_array_push(&pack_objects
.args
, "--shallow");
135 argv_array_push(&pack_objects
.args
, "--progress");
137 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
139 argv_array_push(&pack_objects
.args
, "--include-tag");
140 if (filter_options
.filter_spec
) {
141 if (pack_objects
.use_shell
) {
142 struct strbuf buf
= STRBUF_INIT
;
143 sq_quote_buf(&buf
, filter_options
.filter_spec
);
144 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
145 strbuf_release(&buf
);
147 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
148 filter_options
.filter_spec
);
152 pack_objects
.in
= -1;
153 pack_objects
.out
= -1;
154 pack_objects
.err
= -1;
156 if (start_command(&pack_objects
))
157 die("git upload-pack: unable to fork git-pack-objects");
159 pipe_fd
= xfdopen(pack_objects
.in
, "w");
162 for_each_commit_graft(write_one_shallow
, pipe_fd
);
164 for (i
= 0; i
< want_obj
.nr
; i
++)
165 fprintf(pipe_fd
, "%s\n",
166 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
167 fprintf(pipe_fd
, "--not\n");
168 for (i
= 0; i
< have_obj
.nr
; i
++)
169 fprintf(pipe_fd
, "%s\n",
170 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
171 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
172 fprintf(pipe_fd
, "%s\n",
173 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
174 fprintf(pipe_fd
, "\n");
178 /* We read from pack_objects.err to capture stderr output for
179 * progress bar, and pack_objects.out to capture the pack data.
183 struct pollfd pfd
[2];
184 int pe
, pu
, pollsize
;
192 if (0 <= pack_objects
.out
) {
193 pfd
[pollsize
].fd
= pack_objects
.out
;
194 pfd
[pollsize
].events
= POLLIN
;
198 if (0 <= pack_objects
.err
) {
199 pfd
[pollsize
].fd
= pack_objects
.err
;
200 pfd
[pollsize
].events
= POLLIN
;
208 ret
= poll(pfd
, pollsize
,
209 keepalive
< 0 ? -1 : 1000 * keepalive
);
212 if (errno
!= EINTR
) {
213 error_errno("poll failed, resuming");
218 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
219 /* Status ready; we ship that in the side-band
220 * or dump to the standard error.
222 sz
= xread(pack_objects
.err
, progress
,
225 send_client_data(2, progress
, sz
);
227 close(pack_objects
.err
);
228 pack_objects
.err
= -1;
232 /* give priority to status messages */
235 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
236 /* Data ready; we keep the last byte to ourselves
237 * in case we detect broken rev-list, so that we
238 * can leave the stream corrupted. This is
239 * unfortunate -- unpack-objects would happily
240 * accept a valid packdata with trailing garbage,
241 * so appending garbage after we pass all the
242 * pack data is not good enough to signal
243 * breakage to downstream.
251 sz
= xread(pack_objects
.out
, cp
,
252 sizeof(data
) - outsz
);
256 close(pack_objects
.out
);
257 pack_objects
.out
= -1;
263 buffered
= data
[sz
-1] & 0xFF;
268 send_client_data(1, data
, sz
);
272 * We hit the keepalive timeout without saying anything; send
273 * an empty message on the data sideband just to let the other
274 * side know we're still working on it, but don't have any data
277 * If we don't have a sideband channel, there's no room in the
278 * protocol to say anything, so those clients are just out of
281 if (!ret
&& use_sideband
) {
282 static const char buf
[] = "0005\1";
283 write_or_die(1, buf
, 5);
287 if (finish_command(&pack_objects
)) {
288 error("git upload-pack: git-pack-objects died with error.");
295 send_client_data(1, data
, 1);
296 fprintf(stderr
, "flushed.\n");
303 send_client_data(3, abort_msg
, sizeof(abort_msg
));
304 die("git upload-pack: %s", abort_msg
);
307 static int got_oid(const char *hex
, struct object_id
*oid
)
310 int we_knew_they_have
= 0;
312 if (get_oid_hex(hex
, oid
))
313 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
314 if (!has_object_file(oid
))
317 o
= parse_object(the_repository
, oid
);
319 die("oops (%s)", oid_to_hex(oid
));
320 if (o
->type
== OBJ_COMMIT
) {
321 struct commit_list
*parents
;
322 struct commit
*commit
= (struct commit
*)o
;
323 if (o
->flags
& THEY_HAVE
)
324 we_knew_they_have
= 1;
326 o
->flags
|= THEY_HAVE
;
327 if (!oldest_have
|| (commit
->date
< oldest_have
))
328 oldest_have
= commit
->date
;
329 for (parents
= commit
->parents
;
331 parents
= parents
->next
)
332 parents
->item
->object
.flags
|= THEY_HAVE
;
334 if (!we_knew_they_have
) {
335 add_object_array(o
, NULL
, &have_obj
);
341 static int reachable(struct commit
*want
)
343 struct prio_queue work
= { compare_commits_by_commit_date
};
345 prio_queue_put(&work
, want
);
347 struct commit_list
*list
;
348 struct commit
*commit
= prio_queue_get(&work
);
350 if (commit
->object
.flags
& THEY_HAVE
) {
351 want
->object
.flags
|= COMMON_KNOWN
;
354 if (!commit
->object
.parsed
)
355 parse_object(the_repository
, &commit
->object
.oid
);
356 if (commit
->object
.flags
& REACHABLE
)
358 commit
->object
.flags
|= REACHABLE
;
359 if (commit
->date
< oldest_have
)
361 for (list
= commit
->parents
; list
; list
= list
->next
) {
362 struct commit
*parent
= list
->item
;
363 if (!(parent
->object
.flags
& REACHABLE
))
364 prio_queue_put(&work
, parent
);
367 want
->object
.flags
|= REACHABLE
;
368 clear_commit_marks(want
, REACHABLE
);
369 clear_prio_queue(&work
);
370 return (want
->object
.flags
& COMMON_KNOWN
);
373 static int ok_to_give_up(void)
380 for (i
= 0; i
< want_obj
.nr
; i
++) {
381 struct object
*want
= want_obj
.objects
[i
].item
;
383 if (want
->flags
& COMMON_KNOWN
)
385 want
= deref_tag(the_repository
, want
, "a want line", 0);
386 if (!want
|| want
->type
!= OBJ_COMMIT
) {
387 /* no way to tell if this is reachable by
388 * looking at the ancestry chain alone, so
389 * leave a note to ourselves not to worry about
390 * this object anymore.
392 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
395 if (!reachable((struct commit
*)want
))
401 static int get_common_commits(void)
403 struct object_id oid
;
404 char last_hex
[GIT_MAX_HEXSZ
+ 1];
409 save_commit_buffer
= 0;
412 char *line
= packet_read_line(0, NULL
);
418 if (multi_ack
== 2 && got_common
419 && !got_other
&& ok_to_give_up()) {
421 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
423 if (have_obj
.nr
== 0 || multi_ack
)
424 packet_write_fmt(1, "NAK\n");
426 if (no_done
&& sent_ready
) {
427 packet_write_fmt(1, "ACK %s\n", last_hex
);
436 if (skip_prefix(line
, "have ", &arg
)) {
437 switch (got_oid(arg
, &oid
)) {
438 case -1: /* they have what we do not */
440 if (multi_ack
&& ok_to_give_up()) {
441 const char *hex
= oid_to_hex(&oid
);
442 if (multi_ack
== 2) {
444 packet_write_fmt(1, "ACK %s ready\n", hex
);
446 packet_write_fmt(1, "ACK %s continue\n", hex
);
451 oid_to_hex_r(last_hex
, &oid
);
453 packet_write_fmt(1, "ACK %s common\n", last_hex
);
455 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
456 else if (have_obj
.nr
== 1)
457 packet_write_fmt(1, "ACK %s\n", last_hex
);
462 if (!strcmp(line
, "done")) {
463 if (have_obj
.nr
> 0) {
465 packet_write_fmt(1, "ACK %s\n", last_hex
);
468 packet_write_fmt(1, "NAK\n");
471 die("git upload-pack: expected SHA1 list, got '%s'", line
);
475 static int is_our_ref(struct object
*o
)
477 int allow_hidden_ref
= (allow_unadvertised_object_request
&
478 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
479 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
483 * on successful case, it's up to the caller to close cmd->out
485 static int do_reachable_revlist(struct child_process
*cmd
,
486 struct object_array
*src
,
487 struct object_array
*reachable
)
489 static const char *argv
[] = {
490 "rev-list", "--stdin", NULL
,
493 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
503 * If the next rev-list --stdin encounters an unknown commit,
504 * it terminates, which will cause SIGPIPE in the write loop
507 sigchain_push(SIGPIPE
, SIG_IGN
);
509 if (start_command(cmd
))
513 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
514 for (i
= get_max_object_index(); 0 < i
; ) {
515 o
= get_indexed_object(--i
);
518 if (reachable
&& o
->type
== OBJ_COMMIT
)
519 o
->flags
&= ~TMP_MARK
;
522 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
523 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
526 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
527 for (i
= 0; i
< src
->nr
; i
++) {
528 o
= src
->objects
[i
].item
;
531 add_object_array(o
, NULL
, reachable
);
534 if (reachable
&& o
->type
== OBJ_COMMIT
)
535 o
->flags
|= TMP_MARK
;
536 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
537 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
542 sigchain_pop(SIGPIPE
);
547 sigchain_pop(SIGPIPE
);
556 static int get_reachable_list(struct object_array
*src
,
557 struct object_array
*reachable
)
559 struct child_process cmd
= CHILD_PROCESS_INIT
;
562 char namebuf
[GIT_MAX_HEXSZ
+ 2]; /* ^ + hash + LF */
563 const unsigned hexsz
= the_hash_algo
->hexsz
;
565 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
568 while ((i
= read_in_full(cmd
.out
, namebuf
, hexsz
+ 1)) == hexsz
+ 1) {
569 struct object_id sha1
;
572 if (parse_oid_hex(namebuf
, &sha1
, &p
) || *p
!= '\n')
575 o
= lookup_object(the_repository
, sha1
.hash
);
576 if (o
&& o
->type
== OBJ_COMMIT
) {
577 o
->flags
&= ~TMP_MARK
;
580 for (i
= get_max_object_index(); 0 < i
; i
--) {
581 o
= get_indexed_object(i
- 1);
582 if (o
&& o
->type
== OBJ_COMMIT
&&
583 (o
->flags
& TMP_MARK
)) {
584 add_object_array(o
, NULL
, reachable
);
585 o
->flags
&= ~TMP_MARK
;
590 if (finish_command(&cmd
))
596 static int has_unreachable(struct object_array
*src
)
598 struct child_process cmd
= CHILD_PROCESS_INIT
;
602 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
606 * The commits out of the rev-list are not ancestors of
609 i
= read_in_full(cmd
.out
, buf
, 1);
616 * rev-list may have died by encountering a bad commit
617 * in the history, in which case we do want to bail out
618 * even when it showed no commit.
620 if (finish_command(&cmd
))
623 /* All the non-tip ones are ancestors of what we advertised */
627 sigchain_pop(SIGPIPE
);
633 static void check_non_tip(void)
638 * In the normal in-process case without
639 * uploadpack.allowReachableSHA1InWant,
640 * non-tip requests can never happen.
642 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
644 if (!has_unreachable(&want_obj
))
645 /* All the non-tip ones are ancestors of what we advertised */
649 /* Pick one of them (we know there at least is one) */
650 for (i
= 0; i
< want_obj
.nr
; i
++) {
651 struct object
*o
= want_obj
.objects
[i
].item
;
653 die("git upload-pack: not our ref %s",
654 oid_to_hex(&o
->oid
));
658 static void send_shallow(struct commit_list
*result
)
661 struct object
*object
= &result
->item
->object
;
662 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
663 packet_write_fmt(1, "shallow %s",
664 oid_to_hex(&object
->oid
));
665 register_shallow(the_repository
, &object
->oid
);
668 result
= result
->next
;
672 static void send_unshallow(const struct object_array
*shallows
)
676 for (i
= 0; i
< shallows
->nr
; i
++) {
677 struct object
*object
= shallows
->objects
[i
].item
;
678 if (object
->flags
& NOT_SHALLOW
) {
679 struct commit_list
*parents
;
680 packet_write_fmt(1, "unshallow %s",
681 oid_to_hex(&object
->oid
));
682 object
->flags
&= ~CLIENT_SHALLOW
;
684 * We want to _register_ "object" as shallow, but we
685 * also need to traverse object's parents to deepen a
686 * shallow clone. Unregister it for now so we can
687 * parse and add the parents to the want list, then
690 unregister_shallow(&object
->oid
);
692 parse_commit_or_die((struct commit
*)object
);
693 parents
= ((struct commit
*)object
)->parents
;
695 add_object_array(&parents
->item
->object
,
697 parents
= parents
->next
;
699 add_object_array(object
, NULL
, &extra_edge_obj
);
701 /* make sure commit traversal conforms to client */
702 register_shallow(the_repository
, &object
->oid
);
706 static void deepen(int depth
, int deepen_relative
,
707 struct object_array
*shallows
)
709 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
712 for (i
= 0; i
< shallows
->nr
; i
++) {
713 struct object
*object
= shallows
->objects
[i
].item
;
714 object
->flags
|= NOT_SHALLOW
;
716 } else if (deepen_relative
) {
717 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
718 struct commit_list
*result
;
720 get_reachable_list(shallows
, &reachable_shallows
);
721 result
= get_shallow_commits(&reachable_shallows
,
723 SHALLOW
, NOT_SHALLOW
);
724 send_shallow(result
);
725 free_commit_list(result
);
726 object_array_clear(&reachable_shallows
);
728 struct commit_list
*result
;
730 result
= get_shallow_commits(&want_obj
, depth
,
731 SHALLOW
, NOT_SHALLOW
);
732 send_shallow(result
);
733 free_commit_list(result
);
736 send_unshallow(shallows
);
739 static void deepen_by_rev_list(int ac
, const char **av
,
740 struct object_array
*shallows
)
742 struct commit_list
*result
;
744 close_commit_graph(the_repository
);
745 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
746 send_shallow(result
);
747 free_commit_list(result
);
748 send_unshallow(shallows
);
751 /* Returns 1 if a shallow list is sent or 0 otherwise */
752 static int send_shallow_list(int depth
, int deepen_rev_list
,
753 timestamp_t deepen_since
,
754 struct string_list
*deepen_not
,
755 struct object_array
*shallows
)
759 if (depth
> 0 && deepen_rev_list
)
760 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
762 deepen(depth
, deepen_relative
, shallows
);
764 } else if (deepen_rev_list
) {
765 struct argv_array av
= ARGV_ARRAY_INIT
;
768 argv_array_push(&av
, "rev-list");
770 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
771 if (deepen_not
->nr
) {
772 argv_array_push(&av
, "--not");
773 for (i
= 0; i
< deepen_not
->nr
; i
++) {
774 struct string_list_item
*s
= deepen_not
->items
+ i
;
775 argv_array_push(&av
, s
->string
);
777 argv_array_push(&av
, "--not");
779 for (i
= 0; i
< want_obj
.nr
; i
++) {
780 struct object
*o
= want_obj
.objects
[i
].item
;
781 argv_array_push(&av
, oid_to_hex(&o
->oid
));
783 deepen_by_rev_list(av
.argc
, av
.argv
, shallows
);
784 argv_array_clear(&av
);
787 if (shallows
->nr
> 0) {
789 for (i
= 0; i
< shallows
->nr
; i
++)
790 register_shallow(the_repository
,
791 &shallows
->objects
[i
].item
->oid
);
795 shallow_nr
+= shallows
->nr
;
799 static int process_shallow(const char *line
, struct object_array
*shallows
)
802 if (skip_prefix(line
, "shallow ", &arg
)) {
803 struct object_id oid
;
804 struct object
*object
;
805 if (get_oid_hex(arg
, &oid
))
806 die("invalid shallow line: %s", line
);
807 object
= parse_object(the_repository
, &oid
);
810 if (object
->type
!= OBJ_COMMIT
)
811 die("invalid shallow object %s", oid_to_hex(&oid
));
812 if (!(object
->flags
& CLIENT_SHALLOW
)) {
813 object
->flags
|= CLIENT_SHALLOW
;
814 add_object_array(object
, NULL
, shallows
);
822 static int process_deepen(const char *line
, int *depth
)
825 if (skip_prefix(line
, "deepen ", &arg
)) {
827 *depth
= (int)strtol(arg
, &end
, 0);
828 if (!end
|| *end
|| *depth
<= 0)
829 die("Invalid deepen: %s", line
);
836 static int process_deepen_since(const char *line
, timestamp_t
*deepen_since
, int *deepen_rev_list
)
839 if (skip_prefix(line
, "deepen-since ", &arg
)) {
841 *deepen_since
= parse_timestamp(arg
, &end
, 0);
842 if (!end
|| *end
|| !deepen_since
||
843 /* revisions.c's max_age -1 is special */
845 die("Invalid deepen-since: %s", line
);
846 *deepen_rev_list
= 1;
852 static int process_deepen_not(const char *line
, struct string_list
*deepen_not
, int *deepen_rev_list
)
855 if (skip_prefix(line
, "deepen-not ", &arg
)) {
857 struct object_id oid
;
858 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
859 die("git upload-pack: ambiguous deepen-not: %s", line
);
860 string_list_append(deepen_not
, ref
);
862 *deepen_rev_list
= 1;
868 static void receive_needs(void)
870 struct object_array shallows
= OBJECT_ARRAY_INIT
;
871 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
874 timestamp_t deepen_since
= 0;
875 int deepen_rev_list
= 0;
880 const char *features
;
881 struct object_id oid_buf
;
882 char *line
= packet_read_line(0, NULL
);
889 if (process_shallow(line
, &shallows
))
891 if (process_deepen(line
, &depth
))
893 if (process_deepen_since(line
, &deepen_since
, &deepen_rev_list
))
895 if (process_deepen_not(line
, &deepen_not
, &deepen_rev_list
))
898 if (skip_prefix(line
, "filter ", &arg
)) {
899 if (!filter_capability_requested
)
900 die("git upload-pack: filtering capability not negotiated");
901 parse_list_objects_filter(&filter_options
, arg
);
905 if (!skip_prefix(line
, "want ", &arg
) ||
906 parse_oid_hex(arg
, &oid_buf
, &features
))
907 die("git upload-pack: protocol error, "
908 "expected to get object ID, not '%s'", line
);
910 if (parse_feature_request(features
, "deepen-relative"))
912 if (parse_feature_request(features
, "multi_ack_detailed"))
914 else if (parse_feature_request(features
, "multi_ack"))
916 if (parse_feature_request(features
, "no-done"))
918 if (parse_feature_request(features
, "thin-pack"))
920 if (parse_feature_request(features
, "ofs-delta"))
922 if (parse_feature_request(features
, "side-band-64k"))
923 use_sideband
= LARGE_PACKET_MAX
;
924 else if (parse_feature_request(features
, "side-band"))
925 use_sideband
= DEFAULT_PACKET_MAX
;
926 if (parse_feature_request(features
, "no-progress"))
928 if (parse_feature_request(features
, "include-tag"))
930 if (allow_filter
&& parse_feature_request(features
, "filter"))
931 filter_capability_requested
= 1;
933 o
= parse_object(the_repository
, &oid_buf
);
936 "ERR upload-pack: not our ref %s",
937 oid_to_hex(&oid_buf
));
938 die("git upload-pack: not our ref %s",
939 oid_to_hex(&oid_buf
));
941 if (!(o
->flags
& WANTED
)) {
943 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
946 add_object_array(o
, NULL
, &want_obj
);
951 * We have sent all our refs already, and the other end
952 * should have chosen out of them. When we are operating
953 * in the stateless RPC mode, however, their choice may
954 * have been based on the set of older refs advertised
955 * by another process that handled the initial request.
960 if (!use_sideband
&& daemon_mode
)
963 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
966 if (send_shallow_list(depth
, deepen_rev_list
, deepen_since
,
967 &deepen_not
, &shallows
))
969 object_array_clear(&shallows
);
972 /* return non-zero if the ref is hidden, otherwise 0 */
973 static int mark_our_ref(const char *refname
, const char *refname_full
,
974 const struct object_id
*oid
)
976 struct object
*o
= lookup_unknown_object(oid
->hash
);
978 if (ref_is_hidden(refname
, refname_full
)) {
979 o
->flags
|= HIDDEN_REF
;
986 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
987 int flag
, void *cb_data
)
989 const char *refname
= strip_namespace(refname_full
);
991 mark_our_ref(refname
, refname_full
, oid
);
995 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
997 struct string_list_item
*item
;
1001 for_each_string_list_item(item
, symref
)
1002 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
1005 static int send_ref(const char *refname
, const struct object_id
*oid
,
1006 int flag
, void *cb_data
)
1008 static const char *capabilities
= "multi_ack thin-pack side-band"
1009 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1010 " deepen-relative no-progress include-tag multi_ack_detailed";
1011 const char *refname_nons
= strip_namespace(refname
);
1012 struct object_id peeled
;
1014 if (mark_our_ref(refname_nons
, refname
, oid
))
1018 struct strbuf symref_info
= STRBUF_INIT
;
1020 format_symref_info(&symref_info
, cb_data
);
1021 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1022 oid_to_hex(oid
), refname_nons
,
1024 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
1025 " allow-tip-sha1-in-want" : "",
1026 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
1027 " allow-reachable-sha1-in-want" : "",
1028 stateless_rpc
? " no-done" : "",
1030 allow_filter
? " filter" : "",
1031 git_user_agent_sanitized());
1032 strbuf_release(&symref_info
);
1034 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
1036 capabilities
= NULL
;
1037 if (!peel_ref(refname
, &peeled
))
1038 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
1042 static int find_symref(const char *refname
, const struct object_id
*oid
,
1043 int flag
, void *cb_data
)
1045 const char *symref_target
;
1046 struct string_list_item
*item
;
1048 if ((flag
& REF_ISSYMREF
) == 0)
1050 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1051 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1052 die("'%s' is a symref but it is not?", refname
);
1053 item
= string_list_append(cb_data
, refname
);
1054 item
->util
= xstrdup(symref_target
);
1058 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1060 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1061 if (git_config_bool(var
, value
))
1062 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1064 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1065 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1066 if (git_config_bool(var
, value
))
1067 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1069 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1070 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1071 if (git_config_bool(var
, value
))
1072 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1074 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1075 } else if (!strcmp("uploadpack.keepalive", var
)) {
1076 keepalive
= git_config_int(var
, value
);
1079 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1080 if (!strcmp("uploadpack.packobjectshook", var
))
1081 return git_config_string(&pack_objects_hook
, var
, value
);
1082 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1083 allow_filter
= git_config_bool(var
, value
);
1084 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1085 allow_ref_in_want
= git_config_bool(var
, value
);
1087 return parse_hide_refs_config(var
, value
, "uploadpack");
1090 void upload_pack(struct upload_pack_options
*options
)
1092 struct string_list symref
= STRING_LIST_INIT_DUP
;
1094 stateless_rpc
= options
->stateless_rpc
;
1095 timeout
= options
->timeout
;
1096 daemon_mode
= options
->daemon_mode
;
1098 git_config(upload_pack_config
, NULL
);
1100 head_ref_namespaced(find_symref
, &symref
);
1102 if (options
->advertise_refs
|| !stateless_rpc
) {
1104 head_ref_namespaced(send_ref
, &symref
);
1105 for_each_namespaced_ref(send_ref
, &symref
);
1106 advertise_shallow_grafts(1);
1109 head_ref_namespaced(check_ref
, NULL
);
1110 for_each_namespaced_ref(check_ref
, NULL
);
1112 string_list_clear(&symref
, 1);
1113 if (options
->advertise_refs
)
1118 get_common_commits();
1123 struct upload_pack_data
{
1124 struct object_array wants
;
1125 struct string_list wanted_refs
;
1126 struct oid_array haves
;
1128 struct object_array shallows
;
1129 struct string_list deepen_not
;
1131 timestamp_t deepen_since
;
1132 int deepen_rev_list
;
1133 int deepen_relative
;
1135 unsigned stateless_rpc
: 1;
1137 unsigned use_thin_pack
: 1;
1138 unsigned use_ofs_delta
: 1;
1139 unsigned no_progress
: 1;
1140 unsigned use_include_tag
: 1;
1144 static void upload_pack_data_init(struct upload_pack_data
*data
)
1146 struct object_array wants
= OBJECT_ARRAY_INIT
;
1147 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1148 struct oid_array haves
= OID_ARRAY_INIT
;
1149 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1150 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1152 memset(data
, 0, sizeof(*data
));
1153 data
->wants
= wants
;
1154 data
->wanted_refs
= wanted_refs
;
1155 data
->haves
= haves
;
1156 data
->shallows
= shallows
;
1157 data
->deepen_not
= deepen_not
;
1160 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1162 object_array_clear(&data
->wants
);
1163 string_list_clear(&data
->wanted_refs
, 1);
1164 oid_array_clear(&data
->haves
);
1165 object_array_clear(&data
->shallows
);
1166 string_list_clear(&data
->deepen_not
, 0);
1169 static int parse_want(const char *line
)
1172 if (skip_prefix(line
, "want ", &arg
)) {
1173 struct object_id oid
;
1176 if (get_oid_hex(arg
, &oid
))
1177 die("git upload-pack: protocol error, "
1178 "expected to get oid, not '%s'", line
);
1180 o
= parse_object(the_repository
, &oid
);
1183 "ERR upload-pack: not our ref %s",
1185 die("git upload-pack: not our ref %s",
1189 if (!(o
->flags
& WANTED
)) {
1191 add_object_array(o
, NULL
, &want_obj
);
1200 static int parse_want_ref(const char *line
, struct string_list
*wanted_refs
)
1203 if (skip_prefix(line
, "want-ref ", &arg
)) {
1204 struct object_id oid
;
1205 struct string_list_item
*item
;
1208 if (read_ref(arg
, &oid
)) {
1209 packet_write_fmt(1, "ERR unknown ref %s", arg
);
1210 die("unknown ref %s", arg
);
1213 item
= string_list_append(wanted_refs
, arg
);
1214 item
->util
= oiddup(&oid
);
1216 o
= parse_object_or_die(&oid
, arg
);
1217 if (!(o
->flags
& WANTED
)) {
1219 add_object_array(o
, NULL
, &want_obj
);
1228 static int parse_have(const char *line
, struct oid_array
*haves
)
1231 if (skip_prefix(line
, "have ", &arg
)) {
1232 struct object_id oid
;
1234 if (get_oid_hex(arg
, &oid
))
1235 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1236 oid_array_append(haves
, &oid
);
1243 static void process_args(struct packet_reader
*request
,
1244 struct upload_pack_data
*data
)
1246 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1247 const char *arg
= request
->line
;
1251 if (parse_want(arg
))
1253 if (allow_ref_in_want
&& parse_want_ref(arg
, &data
->wanted_refs
))
1255 /* process have line */
1256 if (parse_have(arg
, &data
->haves
))
1259 /* process args like thin-pack */
1260 if (!strcmp(arg
, "thin-pack")) {
1264 if (!strcmp(arg
, "ofs-delta")) {
1268 if (!strcmp(arg
, "no-progress")) {
1272 if (!strcmp(arg
, "include-tag")) {
1273 use_include_tag
= 1;
1276 if (!strcmp(arg
, "done")) {
1281 /* Shallow related arguments */
1282 if (process_shallow(arg
, &data
->shallows
))
1284 if (process_deepen(arg
, &data
->depth
))
1286 if (process_deepen_since(arg
, &data
->deepen_since
,
1287 &data
->deepen_rev_list
))
1289 if (process_deepen_not(arg
, &data
->deepen_not
,
1290 &data
->deepen_rev_list
))
1292 if (!strcmp(arg
, "deepen-relative")) {
1293 data
->deepen_relative
= 1;
1297 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1298 parse_list_objects_filter(&filter_options
, p
);
1302 /* ignore unknown lines maybe? */
1303 die("unexpected line: '%s'", arg
);
1307 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
)
1312 for (i
= 0; i
< haves
->nr
; i
++) {
1313 const struct object_id
*oid
= &haves
->oid
[i
];
1315 int we_knew_they_have
= 0;
1317 if (!has_object_file(oid
))
1320 oid_array_append(common
, oid
);
1322 o
= parse_object(the_repository
, oid
);
1324 die("oops (%s)", oid_to_hex(oid
));
1325 if (o
->type
== OBJ_COMMIT
) {
1326 struct commit_list
*parents
;
1327 struct commit
*commit
= (struct commit
*)o
;
1328 if (o
->flags
& THEY_HAVE
)
1329 we_knew_they_have
= 1;
1331 o
->flags
|= THEY_HAVE
;
1332 if (!oldest_have
|| (commit
->date
< oldest_have
))
1333 oldest_have
= commit
->date
;
1334 for (parents
= commit
->parents
;
1336 parents
= parents
->next
)
1337 parents
->item
->object
.flags
|= THEY_HAVE
;
1339 if (!we_knew_they_have
)
1340 add_object_array(o
, NULL
, &have_obj
);
1346 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
)
1350 packet_buf_write(response
, "acknowledgments\n");
1354 packet_buf_write(response
, "NAK\n");
1356 for (i
= 0; i
< acks
->nr
; i
++) {
1357 packet_buf_write(response
, "ACK %s\n",
1358 oid_to_hex(&acks
->oid
[i
]));
1361 if (ok_to_give_up()) {
1363 packet_buf_write(response
, "ready\n");
1370 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1372 struct oid_array common
= OID_ARRAY_INIT
;
1373 struct strbuf response
= STRBUF_INIT
;
1376 process_haves(&data
->haves
, &common
);
1379 } else if (send_acks(&common
, &response
)) {
1380 packet_buf_delim(&response
);
1384 packet_buf_flush(&response
);
1389 write_or_die(1, response
.buf
, response
.len
);
1390 strbuf_release(&response
);
1392 oid_array_clear(&data
->haves
);
1393 oid_array_clear(&common
);
1397 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1399 const struct string_list_item
*item
;
1401 if (!data
->wanted_refs
.nr
)
1404 packet_write_fmt(1, "wanted-refs\n");
1406 for_each_string_list_item(item
, &data
->wanted_refs
) {
1407 packet_write_fmt(1, "%s %s\n",
1408 oid_to_hex(item
->util
),
1415 static void send_shallow_info(struct upload_pack_data
*data
)
1417 /* No shallow info needs to be sent */
1418 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1419 !is_repository_shallow(the_repository
))
1422 packet_write_fmt(1, "shallow-info\n");
1424 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1425 data
->deepen_since
, &data
->deepen_not
,
1427 is_repository_shallow(the_repository
))
1428 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
);
1434 FETCH_PROCESS_ARGS
= 0,
1440 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1441 struct packet_reader
*request
)
1443 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1444 struct upload_pack_data data
;
1446 git_config(upload_pack_config
, NULL
);
1448 upload_pack_data_init(&data
);
1449 use_sideband
= LARGE_PACKET_MAX
;
1451 while (state
!= FETCH_DONE
) {
1453 case FETCH_PROCESS_ARGS
:
1454 process_args(request
, &data
);
1458 * Request didn't contain any 'want' lines,
1459 * guess they didn't want anything.
1462 } else if (data
.haves
.nr
) {
1464 * Request had 'have' lines, so lets ACK them.
1466 state
= FETCH_SEND_ACKS
;
1469 * Request had 'want's but no 'have's so we can
1470 * immedietly go to construct and send a pack.
1472 state
= FETCH_SEND_PACK
;
1475 case FETCH_SEND_ACKS
:
1476 if (process_haves_and_send_acks(&data
))
1477 state
= FETCH_SEND_PACK
;
1481 case FETCH_SEND_PACK
:
1482 send_wanted_ref_info(&data
);
1483 send_shallow_info(&data
);
1485 packet_write_fmt(1, "packfile\n");
1494 upload_pack_data_clear(&data
);
1498 int upload_pack_advertise(struct repository
*r
,
1499 struct strbuf
*value
)
1502 int allow_filter_value
;
1503 int allow_ref_in_want
;
1505 strbuf_addstr(value
, "shallow");
1507 if (!repo_config_get_bool(the_repository
,
1508 "uploadpack.allowfilter",
1509 &allow_filter_value
) &&
1511 strbuf_addstr(value
, " filter");
1513 if (!repo_config_get_bool(the_repository
,
1514 "uploadpack.allowrefinwant",
1515 &allow_ref_in_want
) &&
1517 strbuf_addstr(value
, " ref-in-want");