12 #include "list-objects.h"
13 #include "run-command.h"
17 #include "string-list.h"
18 #include "parse-options.h"
19 #include "argv-array.h"
20 #include "prio-queue.h"
23 static const char * const upload_pack_usage
[] = {
24 N_("git upload-pack [<options>] <dir>"),
28 /* Remember to update object flag allocation in object.h */
29 #define THEY_HAVE (1u << 11)
30 #define OUR_REF (1u << 12)
31 #define WANTED (1u << 13)
32 #define COMMON_KNOWN (1u << 14)
33 #define REACHABLE (1u << 15)
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 advertise_refs
;
65 static int stateless_rpc
;
66 static const char *pack_objects_hook
;
68 static void reset_timeout(void)
73 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
76 send_sideband(1, fd
, data
, sz
, use_sideband
);
83 /* XXX: are we happy to lose stuff here? */
87 write_or_die(fd
, data
, sz
);
90 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
93 if (graft
->nr_parent
== -1)
94 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
98 static void create_pack_file(void)
100 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
101 char data
[8193], progress
[128];
102 char abort_msg
[] = "aborting due to possible repository "
103 "corruption on the remote side.";
109 if (!pack_objects_hook
)
110 pack_objects
.git_cmd
= 1;
112 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
113 argv_array_push(&pack_objects
.args
, "git");
114 pack_objects
.use_shell
= 1;
118 argv_array_push(&pack_objects
.args
, "--shallow-file");
119 argv_array_push(&pack_objects
.args
, "");
121 argv_array_push(&pack_objects
.args
, "pack-objects");
122 argv_array_push(&pack_objects
.args
, "--revs");
124 argv_array_push(&pack_objects
.args
, "--thin");
126 argv_array_push(&pack_objects
.args
, "--stdout");
128 argv_array_push(&pack_objects
.args
, "--shallow");
130 argv_array_push(&pack_objects
.args
, "--progress");
132 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
134 argv_array_push(&pack_objects
.args
, "--include-tag");
136 pack_objects
.in
= -1;
137 pack_objects
.out
= -1;
138 pack_objects
.err
= -1;
140 if (start_command(&pack_objects
))
141 die("git upload-pack: unable to fork git-pack-objects");
143 pipe_fd
= xfdopen(pack_objects
.in
, "w");
146 for_each_commit_graft(write_one_shallow
, pipe_fd
);
148 for (i
= 0; i
< want_obj
.nr
; i
++)
149 fprintf(pipe_fd
, "%s\n",
150 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
151 fprintf(pipe_fd
, "--not\n");
152 for (i
= 0; i
< have_obj
.nr
; i
++)
153 fprintf(pipe_fd
, "%s\n",
154 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
155 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
156 fprintf(pipe_fd
, "%s\n",
157 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
158 fprintf(pipe_fd
, "\n");
162 /* We read from pack_objects.err to capture stderr output for
163 * progress bar, and pack_objects.out to capture the pack data.
167 struct pollfd pfd
[2];
168 int pe
, pu
, pollsize
;
176 if (0 <= pack_objects
.out
) {
177 pfd
[pollsize
].fd
= pack_objects
.out
;
178 pfd
[pollsize
].events
= POLLIN
;
182 if (0 <= pack_objects
.err
) {
183 pfd
[pollsize
].fd
= pack_objects
.err
;
184 pfd
[pollsize
].events
= POLLIN
;
192 ret
= poll(pfd
, pollsize
,
193 keepalive
< 0 ? -1 : 1000 * keepalive
);
196 if (errno
!= EINTR
) {
197 error_errno("poll failed, resuming");
202 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
203 /* Status ready; we ship that in the side-band
204 * or dump to the standard error.
206 sz
= xread(pack_objects
.err
, progress
,
209 send_client_data(2, progress
, sz
);
211 close(pack_objects
.err
);
212 pack_objects
.err
= -1;
216 /* give priority to status messages */
219 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
220 /* Data ready; we keep the last byte to ourselves
221 * in case we detect broken rev-list, so that we
222 * can leave the stream corrupted. This is
223 * unfortunate -- unpack-objects would happily
224 * accept a valid packdata with trailing garbage,
225 * so appending garbage after we pass all the
226 * pack data is not good enough to signal
227 * breakage to downstream.
235 sz
= xread(pack_objects
.out
, cp
,
236 sizeof(data
) - outsz
);
240 close(pack_objects
.out
);
241 pack_objects
.out
= -1;
247 buffered
= data
[sz
-1] & 0xFF;
252 send_client_data(1, data
, sz
);
256 * We hit the keepalive timeout without saying anything; send
257 * an empty message on the data sideband just to let the other
258 * side know we're still working on it, but don't have any data
261 * If we don't have a sideband channel, there's no room in the
262 * protocol to say anything, so those clients are just out of
265 if (!ret
&& use_sideband
) {
266 static const char buf
[] = "0005\1";
267 write_or_die(1, buf
, 5);
271 if (finish_command(&pack_objects
)) {
272 error("git upload-pack: git-pack-objects died with error.");
279 send_client_data(1, data
, 1);
280 fprintf(stderr
, "flushed.\n");
287 send_client_data(3, abort_msg
, sizeof(abort_msg
));
288 die("git upload-pack: %s", abort_msg
);
291 static int got_oid(const char *hex
, struct object_id
*oid
)
294 int we_knew_they_have
= 0;
296 if (get_oid_hex(hex
, oid
))
297 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
298 if (!has_object_file(oid
))
301 o
= parse_object(oid
);
303 die("oops (%s)", oid_to_hex(oid
));
304 if (o
->type
== OBJ_COMMIT
) {
305 struct commit_list
*parents
;
306 struct commit
*commit
= (struct commit
*)o
;
307 if (o
->flags
& THEY_HAVE
)
308 we_knew_they_have
= 1;
310 o
->flags
|= THEY_HAVE
;
311 if (!oldest_have
|| (commit
->date
< oldest_have
))
312 oldest_have
= commit
->date
;
313 for (parents
= commit
->parents
;
315 parents
= parents
->next
)
316 parents
->item
->object
.flags
|= THEY_HAVE
;
318 if (!we_knew_they_have
) {
319 add_object_array(o
, NULL
, &have_obj
);
325 static int reachable(struct commit
*want
)
327 struct prio_queue work
= { compare_commits_by_commit_date
};
329 prio_queue_put(&work
, want
);
331 struct commit_list
*list
;
332 struct commit
*commit
= prio_queue_get(&work
);
334 if (commit
->object
.flags
& THEY_HAVE
) {
335 want
->object
.flags
|= COMMON_KNOWN
;
338 if (!commit
->object
.parsed
)
339 parse_object(&commit
->object
.oid
);
340 if (commit
->object
.flags
& REACHABLE
)
342 commit
->object
.flags
|= REACHABLE
;
343 if (commit
->date
< oldest_have
)
345 for (list
= commit
->parents
; list
; list
= list
->next
) {
346 struct commit
*parent
= list
->item
;
347 if (!(parent
->object
.flags
& REACHABLE
))
348 prio_queue_put(&work
, parent
);
351 want
->object
.flags
|= REACHABLE
;
352 clear_commit_marks(want
, REACHABLE
);
353 clear_prio_queue(&work
);
354 return (want
->object
.flags
& COMMON_KNOWN
);
357 static int ok_to_give_up(void)
364 for (i
= 0; i
< want_obj
.nr
; i
++) {
365 struct object
*want
= want_obj
.objects
[i
].item
;
367 if (want
->flags
& COMMON_KNOWN
)
369 want
= deref_tag(want
, "a want line", 0);
370 if (!want
|| want
->type
!= OBJ_COMMIT
) {
371 /* no way to tell if this is reachable by
372 * looking at the ancestry chain alone, so
373 * leave a note to ourselves not to worry about
374 * this object anymore.
376 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
379 if (!reachable((struct commit
*)want
))
385 static int get_common_commits(void)
387 struct object_id oid
;
388 char last_hex
[GIT_MAX_HEXSZ
+ 1];
393 save_commit_buffer
= 0;
396 char *line
= packet_read_line(0, NULL
);
402 if (multi_ack
== 2 && got_common
403 && !got_other
&& ok_to_give_up()) {
405 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
407 if (have_obj
.nr
== 0 || multi_ack
)
408 packet_write_fmt(1, "NAK\n");
410 if (no_done
&& sent_ready
) {
411 packet_write_fmt(1, "ACK %s\n", last_hex
);
420 if (skip_prefix(line
, "have ", &arg
)) {
421 switch (got_oid(arg
, &oid
)) {
422 case -1: /* they have what we do not */
424 if (multi_ack
&& ok_to_give_up()) {
425 const char *hex
= oid_to_hex(&oid
);
426 if (multi_ack
== 2) {
428 packet_write_fmt(1, "ACK %s ready\n", hex
);
430 packet_write_fmt(1, "ACK %s continue\n", hex
);
435 memcpy(last_hex
, oid_to_hex(&oid
), 41);
437 packet_write_fmt(1, "ACK %s common\n", last_hex
);
439 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
440 else if (have_obj
.nr
== 1)
441 packet_write_fmt(1, "ACK %s\n", last_hex
);
446 if (!strcmp(line
, "done")) {
447 if (have_obj
.nr
> 0) {
449 packet_write_fmt(1, "ACK %s\n", last_hex
);
452 packet_write_fmt(1, "NAK\n");
455 die("git upload-pack: expected SHA1 list, got '%s'", line
);
459 static int is_our_ref(struct object
*o
)
461 int allow_hidden_ref
= (allow_unadvertised_object_request
&
462 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
463 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
467 * on successful case, it's up to the caller to close cmd->out
469 static int do_reachable_revlist(struct child_process
*cmd
,
470 struct object_array
*src
,
471 struct object_array
*reachable
)
473 static const char *argv
[] = {
474 "rev-list", "--stdin", NULL
,
477 char namebuf
[42]; /* ^ + SHA-1 + LF */
487 * If the next rev-list --stdin encounters an unknown commit,
488 * it terminates, which will cause SIGPIPE in the write loop
491 sigchain_push(SIGPIPE
, SIG_IGN
);
493 if (start_command(cmd
))
497 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
498 for (i
= get_max_object_index(); 0 < i
; ) {
499 o
= get_indexed_object(--i
);
502 if (reachable
&& o
->type
== OBJ_COMMIT
)
503 o
->flags
&= ~TMP_MARK
;
506 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
507 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
510 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
511 for (i
= 0; i
< src
->nr
; i
++) {
512 o
= src
->objects
[i
].item
;
515 add_object_array(o
, NULL
, reachable
);
518 if (reachable
&& o
->type
== OBJ_COMMIT
)
519 o
->flags
|= TMP_MARK
;
520 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
521 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
526 sigchain_pop(SIGPIPE
);
531 sigchain_pop(SIGPIPE
);
540 static int get_reachable_list(struct object_array
*src
,
541 struct object_array
*reachable
)
543 struct child_process cmd
= CHILD_PROCESS_INIT
;
546 char namebuf
[42]; /* ^ + SHA-1 + LF */
548 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
551 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
552 struct object_id sha1
;
554 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
557 o
= lookup_object(sha1
.hash
);
558 if (o
&& o
->type
== OBJ_COMMIT
) {
559 o
->flags
&= ~TMP_MARK
;
562 for (i
= get_max_object_index(); 0 < i
; i
--) {
563 o
= get_indexed_object(i
- 1);
564 if (o
&& o
->type
== OBJ_COMMIT
&&
565 (o
->flags
& TMP_MARK
)) {
566 add_object_array(o
, NULL
, reachable
);
567 o
->flags
&= ~TMP_MARK
;
572 if (finish_command(&cmd
))
578 static int has_unreachable(struct object_array
*src
)
580 struct child_process cmd
= CHILD_PROCESS_INIT
;
584 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
588 * The commits out of the rev-list are not ancestors of
591 i
= read_in_full(cmd
.out
, buf
, 1);
598 * rev-list may have died by encountering a bad commit
599 * in the history, in which case we do want to bail out
600 * even when it showed no commit.
602 if (finish_command(&cmd
))
605 /* All the non-tip ones are ancestors of what we advertised */
609 sigchain_pop(SIGPIPE
);
615 static void check_non_tip(void)
620 * In the normal in-process case without
621 * uploadpack.allowReachableSHA1InWant,
622 * non-tip requests can never happen.
624 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
626 if (!has_unreachable(&want_obj
))
627 /* All the non-tip ones are ancestors of what we advertised */
631 /* Pick one of them (we know there at least is one) */
632 for (i
= 0; i
< want_obj
.nr
; i
++) {
633 struct object
*o
= want_obj
.objects
[i
].item
;
635 die("git upload-pack: not our ref %s",
636 oid_to_hex(&o
->oid
));
640 static void send_shallow(struct commit_list
*result
)
643 struct object
*object
= &result
->item
->object
;
644 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
645 packet_write_fmt(1, "shallow %s",
646 oid_to_hex(&object
->oid
));
647 register_shallow(&object
->oid
);
650 result
= result
->next
;
654 static void send_unshallow(const struct object_array
*shallows
)
658 for (i
= 0; i
< shallows
->nr
; i
++) {
659 struct object
*object
= shallows
->objects
[i
].item
;
660 if (object
->flags
& NOT_SHALLOW
) {
661 struct commit_list
*parents
;
662 packet_write_fmt(1, "unshallow %s",
663 oid_to_hex(&object
->oid
));
664 object
->flags
&= ~CLIENT_SHALLOW
;
666 * We want to _register_ "object" as shallow, but we
667 * also need to traverse object's parents to deepen a
668 * shallow clone. Unregister it for now so we can
669 * parse and add the parents to the want list, then
672 unregister_shallow(&object
->oid
);
674 parse_commit_or_die((struct commit
*)object
);
675 parents
= ((struct commit
*)object
)->parents
;
677 add_object_array(&parents
->item
->object
,
679 parents
= parents
->next
;
681 add_object_array(object
, NULL
, &extra_edge_obj
);
683 /* make sure commit traversal conforms to client */
684 register_shallow(&object
->oid
);
688 static void deepen(int depth
, int deepen_relative
,
689 struct object_array
*shallows
)
691 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow()) {
694 for (i
= 0; i
< shallows
->nr
; i
++) {
695 struct object
*object
= shallows
->objects
[i
].item
;
696 object
->flags
|= NOT_SHALLOW
;
698 } else if (deepen_relative
) {
699 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
700 struct commit_list
*result
;
702 get_reachable_list(shallows
, &reachable_shallows
);
703 result
= get_shallow_commits(&reachable_shallows
,
705 SHALLOW
, NOT_SHALLOW
);
706 send_shallow(result
);
707 free_commit_list(result
);
708 object_array_clear(&reachable_shallows
);
710 struct commit_list
*result
;
712 result
= get_shallow_commits(&want_obj
, depth
,
713 SHALLOW
, NOT_SHALLOW
);
714 send_shallow(result
);
715 free_commit_list(result
);
718 send_unshallow(shallows
);
722 static void deepen_by_rev_list(int ac
, const char **av
,
723 struct object_array
*shallows
)
725 struct commit_list
*result
;
727 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
728 send_shallow(result
);
729 free_commit_list(result
);
730 send_unshallow(shallows
);
734 static void receive_needs(void)
736 struct object_array shallows
= OBJECT_ARRAY_INIT
;
737 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
740 timestamp_t deepen_since
= 0;
741 int deepen_rev_list
= 0;
746 const char *features
;
747 struct object_id oid_buf
;
748 char *line
= packet_read_line(0, NULL
);
755 if (skip_prefix(line
, "shallow ", &arg
)) {
756 struct object_id oid
;
757 struct object
*object
;
758 if (get_oid_hex(arg
, &oid
))
759 die("invalid shallow line: %s", line
);
760 object
= parse_object(&oid
);
763 if (object
->type
!= OBJ_COMMIT
)
764 die("invalid shallow object %s", oid_to_hex(&oid
));
765 if (!(object
->flags
& CLIENT_SHALLOW
)) {
766 object
->flags
|= CLIENT_SHALLOW
;
767 add_object_array(object
, NULL
, &shallows
);
771 if (skip_prefix(line
, "deepen ", &arg
)) {
773 depth
= strtol(arg
, &end
, 0);
774 if (!end
|| *end
|| depth
<= 0)
775 die("Invalid deepen: %s", line
);
778 if (skip_prefix(line
, "deepen-since ", &arg
)) {
780 deepen_since
= parse_timestamp(arg
, &end
, 0);
781 if (!end
|| *end
|| !deepen_since
||
782 /* revisions.c's max_age -1 is special */
784 die("Invalid deepen-since: %s", line
);
788 if (skip_prefix(line
, "deepen-not ", &arg
)) {
790 struct object_id oid
;
791 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
792 die("git upload-pack: ambiguous deepen-not: %s", line
);
793 string_list_append(&deepen_not
, ref
);
798 if (!skip_prefix(line
, "want ", &arg
) ||
799 get_oid_hex(arg
, &oid_buf
))
800 die("git upload-pack: protocol error, "
801 "expected to get sha, not '%s'", line
);
805 if (parse_feature_request(features
, "deepen-relative"))
807 if (parse_feature_request(features
, "multi_ack_detailed"))
809 else if (parse_feature_request(features
, "multi_ack"))
811 if (parse_feature_request(features
, "no-done"))
813 if (parse_feature_request(features
, "thin-pack"))
815 if (parse_feature_request(features
, "ofs-delta"))
817 if (parse_feature_request(features
, "side-band-64k"))
818 use_sideband
= LARGE_PACKET_MAX
;
819 else if (parse_feature_request(features
, "side-band"))
820 use_sideband
= DEFAULT_PACKET_MAX
;
821 if (parse_feature_request(features
, "no-progress"))
823 if (parse_feature_request(features
, "include-tag"))
826 o
= parse_object(&oid_buf
);
829 "ERR upload-pack: not our ref %s",
830 oid_to_hex(&oid_buf
));
831 die("git upload-pack: not our ref %s",
832 oid_to_hex(&oid_buf
));
834 if (!(o
->flags
& WANTED
)) {
836 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
839 add_object_array(o
, NULL
, &want_obj
);
844 * We have sent all our refs already, and the other end
845 * should have chosen out of them. When we are operating
846 * in the stateless RPC mode, however, their choice may
847 * have been based on the set of older refs advertised
848 * by another process that handled the initial request.
853 if (!use_sideband
&& daemon_mode
)
856 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
858 if (depth
> 0 && deepen_rev_list
)
859 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
861 deepen(depth
, deepen_relative
, &shallows
);
862 else if (deepen_rev_list
) {
863 struct argv_array av
= ARGV_ARRAY_INIT
;
866 argv_array_push(&av
, "rev-list");
868 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
870 argv_array_push(&av
, "--not");
871 for (i
= 0; i
< deepen_not
.nr
; i
++) {
872 struct string_list_item
*s
= deepen_not
.items
+ i
;
873 argv_array_push(&av
, s
->string
);
875 argv_array_push(&av
, "--not");
877 for (i
= 0; i
< want_obj
.nr
; i
++) {
878 struct object
*o
= want_obj
.objects
[i
].item
;
879 argv_array_push(&av
, oid_to_hex(&o
->oid
));
881 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
882 argv_array_clear(&av
);
885 if (shallows
.nr
> 0) {
887 for (i
= 0; i
< shallows
.nr
; i
++)
888 register_shallow(&shallows
.objects
[i
].item
->oid
);
891 shallow_nr
+= shallows
.nr
;
892 object_array_clear(&shallows
);
895 /* return non-zero if the ref is hidden, otherwise 0 */
896 static int mark_our_ref(const char *refname
, const char *refname_full
,
897 const struct object_id
*oid
)
899 struct object
*o
= lookup_unknown_object(oid
->hash
);
901 if (ref_is_hidden(refname
, refname_full
)) {
902 o
->flags
|= HIDDEN_REF
;
909 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
910 int flag
, void *cb_data
)
912 const char *refname
= strip_namespace(refname_full
);
914 mark_our_ref(refname
, refname_full
, oid
);
918 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
920 struct string_list_item
*item
;
924 for_each_string_list_item(item
, symref
)
925 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
928 static int send_ref(const char *refname
, const struct object_id
*oid
,
929 int flag
, void *cb_data
)
931 static const char *capabilities
= "multi_ack thin-pack side-band"
932 " side-band-64k ofs-delta shallow deepen-since deepen-not"
933 " deepen-relative no-progress include-tag multi_ack_detailed";
934 const char *refname_nons
= strip_namespace(refname
);
935 struct object_id peeled
;
937 if (mark_our_ref(refname_nons
, refname
, oid
))
941 struct strbuf symref_info
= STRBUF_INIT
;
943 format_symref_info(&symref_info
, cb_data
);
944 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
945 oid_to_hex(oid
), refname_nons
,
947 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
948 " allow-tip-sha1-in-want" : "",
949 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
950 " allow-reachable-sha1-in-want" : "",
951 stateless_rpc
? " no-done" : "",
953 git_user_agent_sanitized());
954 strbuf_release(&symref_info
);
956 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
959 if (!peel_ref(refname
, &peeled
))
960 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
964 static int find_symref(const char *refname
, const struct object_id
*oid
,
965 int flag
, void *cb_data
)
967 const char *symref_target
;
968 struct string_list_item
*item
;
970 if ((flag
& REF_ISSYMREF
) == 0)
972 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
973 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
974 die("'%s' is a symref but it is not?", refname
);
975 item
= string_list_append(cb_data
, refname
);
976 item
->util
= xstrdup(symref_target
);
980 static void upload_pack(void)
982 struct string_list symref
= STRING_LIST_INIT_DUP
;
984 head_ref_namespaced(find_symref
, &symref
);
986 if (advertise_refs
|| !stateless_rpc
) {
988 head_ref_namespaced(send_ref
, &symref
);
989 for_each_namespaced_ref(send_ref
, &symref
);
990 advertise_shallow_grafts(1);
993 head_ref_namespaced(check_ref
, NULL
);
994 for_each_namespaced_ref(check_ref
, NULL
);
996 string_list_clear(&symref
, 1);
1002 get_common_commits();
1007 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1009 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1010 if (git_config_bool(var
, value
))
1011 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1013 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1014 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1015 if (git_config_bool(var
, value
))
1016 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1018 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1019 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1020 if (git_config_bool(var
, value
))
1021 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1023 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1024 } else if (!strcmp("uploadpack.keepalive", var
)) {
1025 keepalive
= git_config_int(var
, value
);
1028 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1029 if (!strcmp("uploadpack.packobjectshook", var
))
1030 return git_config_string(&pack_objects_hook
, var
, value
);
1032 return parse_hide_refs_config(var
, value
, "uploadpack");
1035 int cmd_main(int argc
, const char **argv
)
1039 struct option options
[] = {
1040 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
1041 N_("quit after a single request/response exchange")),
1042 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
1043 N_("exit immediately after initial ref advertisement")),
1044 OPT_BOOL(0, "strict", &strict
,
1045 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1046 OPT_INTEGER(0, "timeout", &timeout
,
1047 N_("interrupt transfer after <n> seconds of inactivity")),
1051 packet_trace_identity("upload-pack");
1052 check_replace_refs
= 0;
1054 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
1057 usage_with_options(upload_pack_usage
, options
);
1066 if (!enter_repo(dir
, strict
))
1067 die("'%s' does not appear to be a git repository", dir
);
1069 git_config(upload_pack_config
, NULL
);
1071 switch (determine_protocol_version_server()) {
1074 * v1 is just the original protocol with a version string,
1075 * so just fall through after writing the version string.
1077 if (advertise_refs
|| !stateless_rpc
)
1078 packet_write_fmt(1, "version 1\n");
1084 case protocol_unknown_version
:
1085 BUG("unknown protocol version");