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 (!strcmp("uploadpack.allowfilter", var
)) {
1080 allow_filter
= git_config_bool(var
, value
);
1081 } else if (!strcmp("uploadpack.allowrefinwant", var
)) {
1082 allow_ref_in_want
= git_config_bool(var
, value
);
1085 if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1086 if (!strcmp("uploadpack.packobjectshook", var
))
1087 return git_config_string(&pack_objects_hook
, var
, value
);
1090 return parse_hide_refs_config(var
, value
, "uploadpack");
1093 void upload_pack(struct upload_pack_options
*options
)
1095 struct string_list symref
= STRING_LIST_INIT_DUP
;
1097 stateless_rpc
= options
->stateless_rpc
;
1098 timeout
= options
->timeout
;
1099 daemon_mode
= options
->daemon_mode
;
1101 git_config(upload_pack_config
, NULL
);
1103 head_ref_namespaced(find_symref
, &symref
);
1105 if (options
->advertise_refs
|| !stateless_rpc
) {
1107 head_ref_namespaced(send_ref
, &symref
);
1108 for_each_namespaced_ref(send_ref
, &symref
);
1109 advertise_shallow_grafts(1);
1112 head_ref_namespaced(check_ref
, NULL
);
1113 for_each_namespaced_ref(check_ref
, NULL
);
1115 string_list_clear(&symref
, 1);
1116 if (options
->advertise_refs
)
1121 get_common_commits();
1126 struct upload_pack_data
{
1127 struct object_array wants
;
1128 struct string_list wanted_refs
;
1129 struct oid_array haves
;
1131 struct object_array shallows
;
1132 struct string_list deepen_not
;
1134 timestamp_t deepen_since
;
1135 int deepen_rev_list
;
1136 int deepen_relative
;
1138 unsigned stateless_rpc
: 1;
1140 unsigned use_thin_pack
: 1;
1141 unsigned use_ofs_delta
: 1;
1142 unsigned no_progress
: 1;
1143 unsigned use_include_tag
: 1;
1147 static void upload_pack_data_init(struct upload_pack_data
*data
)
1149 struct object_array wants
= OBJECT_ARRAY_INIT
;
1150 struct string_list wanted_refs
= STRING_LIST_INIT_DUP
;
1151 struct oid_array haves
= OID_ARRAY_INIT
;
1152 struct object_array shallows
= OBJECT_ARRAY_INIT
;
1153 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
1155 memset(data
, 0, sizeof(*data
));
1156 data
->wants
= wants
;
1157 data
->wanted_refs
= wanted_refs
;
1158 data
->haves
= haves
;
1159 data
->shallows
= shallows
;
1160 data
->deepen_not
= deepen_not
;
1163 static void upload_pack_data_clear(struct upload_pack_data
*data
)
1165 object_array_clear(&data
->wants
);
1166 string_list_clear(&data
->wanted_refs
, 1);
1167 oid_array_clear(&data
->haves
);
1168 object_array_clear(&data
->shallows
);
1169 string_list_clear(&data
->deepen_not
, 0);
1172 static int parse_want(const char *line
)
1175 if (skip_prefix(line
, "want ", &arg
)) {
1176 struct object_id oid
;
1179 if (get_oid_hex(arg
, &oid
))
1180 die("git upload-pack: protocol error, "
1181 "expected to get oid, not '%s'", line
);
1183 o
= parse_object(the_repository
, &oid
);
1186 "ERR upload-pack: not our ref %s",
1188 die("git upload-pack: not our ref %s",
1192 if (!(o
->flags
& WANTED
)) {
1194 add_object_array(o
, NULL
, &want_obj
);
1203 static int parse_want_ref(const char *line
, struct string_list
*wanted_refs
)
1206 if (skip_prefix(line
, "want-ref ", &arg
)) {
1207 struct object_id oid
;
1208 struct string_list_item
*item
;
1211 if (read_ref(arg
, &oid
)) {
1212 packet_write_fmt(1, "ERR unknown ref %s", arg
);
1213 die("unknown ref %s", arg
);
1216 item
= string_list_append(wanted_refs
, arg
);
1217 item
->util
= oiddup(&oid
);
1219 o
= parse_object_or_die(&oid
, arg
);
1220 if (!(o
->flags
& WANTED
)) {
1222 add_object_array(o
, NULL
, &want_obj
);
1231 static int parse_have(const char *line
, struct oid_array
*haves
)
1234 if (skip_prefix(line
, "have ", &arg
)) {
1235 struct object_id oid
;
1237 if (get_oid_hex(arg
, &oid
))
1238 die("git upload-pack: expected SHA1 object, got '%s'", arg
);
1239 oid_array_append(haves
, &oid
);
1246 static void process_args(struct packet_reader
*request
,
1247 struct upload_pack_data
*data
)
1249 while (packet_reader_read(request
) != PACKET_READ_FLUSH
) {
1250 const char *arg
= request
->line
;
1254 if (parse_want(arg
))
1256 if (allow_ref_in_want
&& parse_want_ref(arg
, &data
->wanted_refs
))
1258 /* process have line */
1259 if (parse_have(arg
, &data
->haves
))
1262 /* process args like thin-pack */
1263 if (!strcmp(arg
, "thin-pack")) {
1267 if (!strcmp(arg
, "ofs-delta")) {
1271 if (!strcmp(arg
, "no-progress")) {
1275 if (!strcmp(arg
, "include-tag")) {
1276 use_include_tag
= 1;
1279 if (!strcmp(arg
, "done")) {
1284 /* Shallow related arguments */
1285 if (process_shallow(arg
, &data
->shallows
))
1287 if (process_deepen(arg
, &data
->depth
))
1289 if (process_deepen_since(arg
, &data
->deepen_since
,
1290 &data
->deepen_rev_list
))
1292 if (process_deepen_not(arg
, &data
->deepen_not
,
1293 &data
->deepen_rev_list
))
1295 if (!strcmp(arg
, "deepen-relative")) {
1296 data
->deepen_relative
= 1;
1300 if (allow_filter
&& skip_prefix(arg
, "filter ", &p
)) {
1301 parse_list_objects_filter(&filter_options
, p
);
1305 /* ignore unknown lines maybe? */
1306 die("unexpected line: '%s'", arg
);
1310 static int process_haves(struct oid_array
*haves
, struct oid_array
*common
)
1315 for (i
= 0; i
< haves
->nr
; i
++) {
1316 const struct object_id
*oid
= &haves
->oid
[i
];
1318 int we_knew_they_have
= 0;
1320 if (!has_object_file(oid
))
1323 oid_array_append(common
, oid
);
1325 o
= parse_object(the_repository
, oid
);
1327 die("oops (%s)", oid_to_hex(oid
));
1328 if (o
->type
== OBJ_COMMIT
) {
1329 struct commit_list
*parents
;
1330 struct commit
*commit
= (struct commit
*)o
;
1331 if (o
->flags
& THEY_HAVE
)
1332 we_knew_they_have
= 1;
1334 o
->flags
|= THEY_HAVE
;
1335 if (!oldest_have
|| (commit
->date
< oldest_have
))
1336 oldest_have
= commit
->date
;
1337 for (parents
= commit
->parents
;
1339 parents
= parents
->next
)
1340 parents
->item
->object
.flags
|= THEY_HAVE
;
1342 if (!we_knew_they_have
)
1343 add_object_array(o
, NULL
, &have_obj
);
1349 static int send_acks(struct oid_array
*acks
, struct strbuf
*response
)
1353 packet_buf_write(response
, "acknowledgments\n");
1357 packet_buf_write(response
, "NAK\n");
1359 for (i
= 0; i
< acks
->nr
; i
++) {
1360 packet_buf_write(response
, "ACK %s\n",
1361 oid_to_hex(&acks
->oid
[i
]));
1364 if (ok_to_give_up()) {
1366 packet_buf_write(response
, "ready\n");
1373 static int process_haves_and_send_acks(struct upload_pack_data
*data
)
1375 struct oid_array common
= OID_ARRAY_INIT
;
1376 struct strbuf response
= STRBUF_INIT
;
1379 process_haves(&data
->haves
, &common
);
1382 } else if (send_acks(&common
, &response
)) {
1383 packet_buf_delim(&response
);
1387 packet_buf_flush(&response
);
1392 write_or_die(1, response
.buf
, response
.len
);
1393 strbuf_release(&response
);
1395 oid_array_clear(&data
->haves
);
1396 oid_array_clear(&common
);
1400 static void send_wanted_ref_info(struct upload_pack_data
*data
)
1402 const struct string_list_item
*item
;
1404 if (!data
->wanted_refs
.nr
)
1407 packet_write_fmt(1, "wanted-refs\n");
1409 for_each_string_list_item(item
, &data
->wanted_refs
) {
1410 packet_write_fmt(1, "%s %s\n",
1411 oid_to_hex(item
->util
),
1418 static void send_shallow_info(struct upload_pack_data
*data
)
1420 /* No shallow info needs to be sent */
1421 if (!data
->depth
&& !data
->deepen_rev_list
&& !data
->shallows
.nr
&&
1422 !is_repository_shallow(the_repository
))
1425 packet_write_fmt(1, "shallow-info\n");
1427 if (!send_shallow_list(data
->depth
, data
->deepen_rev_list
,
1428 data
->deepen_since
, &data
->deepen_not
,
1430 is_repository_shallow(the_repository
))
1431 deepen(INFINITE_DEPTH
, data
->deepen_relative
, &data
->shallows
);
1437 FETCH_PROCESS_ARGS
= 0,
1443 int upload_pack_v2(struct repository
*r
, struct argv_array
*keys
,
1444 struct packet_reader
*request
)
1446 enum fetch_state state
= FETCH_PROCESS_ARGS
;
1447 struct upload_pack_data data
;
1449 git_config(upload_pack_config
, NULL
);
1451 upload_pack_data_init(&data
);
1452 use_sideband
= LARGE_PACKET_MAX
;
1454 while (state
!= FETCH_DONE
) {
1456 case FETCH_PROCESS_ARGS
:
1457 process_args(request
, &data
);
1461 * Request didn't contain any 'want' lines,
1462 * guess they didn't want anything.
1465 } else if (data
.haves
.nr
) {
1467 * Request had 'have' lines, so lets ACK them.
1469 state
= FETCH_SEND_ACKS
;
1472 * Request had 'want's but no 'have's so we can
1473 * immedietly go to construct and send a pack.
1475 state
= FETCH_SEND_PACK
;
1478 case FETCH_SEND_ACKS
:
1479 if (process_haves_and_send_acks(&data
))
1480 state
= FETCH_SEND_PACK
;
1484 case FETCH_SEND_PACK
:
1485 send_wanted_ref_info(&data
);
1486 send_shallow_info(&data
);
1488 packet_write_fmt(1, "packfile\n");
1497 upload_pack_data_clear(&data
);
1501 int upload_pack_advertise(struct repository
*r
,
1502 struct strbuf
*value
)
1505 int allow_filter_value
;
1506 int allow_ref_in_want
;
1508 strbuf_addstr(value
, "shallow");
1510 if (!repo_config_get_bool(the_repository
,
1511 "uploadpack.allowfilter",
1512 &allow_filter_value
) &&
1514 strbuf_addstr(value
, " filter");
1516 if (!repo_config_get_bool(the_repository
,
1517 "uploadpack.allowrefinwant",
1518 &allow_ref_in_want
) &&
1520 strbuf_addstr(value
, " ref-in-want");