11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
17 #include "parse-options.h"
18 #include "argv-array.h"
19 #include "prio-queue.h"
21 static const char * const upload_pack_usage
[] = {
22 N_("git upload-pack [<options>] <dir>"),
26 /* Remember to update object flag allocation in object.h */
27 #define THEY_HAVE (1u << 11)
28 #define OUR_REF (1u << 12)
29 #define WANTED (1u << 13)
30 #define COMMON_KNOWN (1u << 14)
31 #define REACHABLE (1u << 15)
33 #define SHALLOW (1u << 16)
34 #define NOT_SHALLOW (1u << 17)
35 #define CLIENT_SHALLOW (1u << 18)
36 #define HIDDEN_REF (1u << 19)
38 static unsigned long oldest_have
;
40 static int deepen_relative
;
43 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
44 static int no_progress
, daemon_mode
;
45 /* Allow specifying sha1 if it is a ref tip. */
46 #define ALLOW_TIP_SHA1 01
47 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
48 #define ALLOW_REACHABLE_SHA1 02
49 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
50 #define ALLOW_ANY_SHA1 07
51 static unsigned int allow_unadvertised_object_request
;
52 static int shallow_nr
;
53 static struct object_array have_obj
;
54 static struct object_array want_obj
;
55 static struct object_array extra_edge_obj
;
56 static unsigned int timeout
;
57 static int keepalive
= 5;
59 * otherwise maximum packet size (up to 65520 bytes).
61 static int use_sideband
;
62 static int advertise_refs
;
63 static int stateless_rpc
;
64 static const char *pack_objects_hook
;
66 static void reset_timeout(void)
71 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
74 send_sideband(1, fd
, data
, sz
, use_sideband
);
81 /* XXX: are we happy to lose stuff here? */
85 write_or_die(fd
, data
, sz
);
88 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
91 if (graft
->nr_parent
== -1)
92 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
96 static void create_pack_file(void)
98 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
99 char data
[8193], progress
[128];
100 char abort_msg
[] = "aborting due to possible repository "
101 "corruption on the remote side.";
107 if (!pack_objects_hook
)
108 pack_objects
.git_cmd
= 1;
110 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
111 argv_array_push(&pack_objects
.args
, "git");
112 pack_objects
.use_shell
= 1;
116 argv_array_push(&pack_objects
.args
, "--shallow-file");
117 argv_array_push(&pack_objects
.args
, "");
119 argv_array_push(&pack_objects
.args
, "pack-objects");
120 argv_array_push(&pack_objects
.args
, "--revs");
122 argv_array_push(&pack_objects
.args
, "--thin");
124 argv_array_push(&pack_objects
.args
, "--stdout");
126 argv_array_push(&pack_objects
.args
, "--shallow");
128 argv_array_push(&pack_objects
.args
, "--progress");
130 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
132 argv_array_push(&pack_objects
.args
, "--include-tag");
134 pack_objects
.in
= -1;
135 pack_objects
.out
= -1;
136 pack_objects
.err
= -1;
138 if (start_command(&pack_objects
))
139 die("git upload-pack: unable to fork git-pack-objects");
141 pipe_fd
= xfdopen(pack_objects
.in
, "w");
144 for_each_commit_graft(write_one_shallow
, pipe_fd
);
146 for (i
= 0; i
< want_obj
.nr
; i
++)
147 fprintf(pipe_fd
, "%s\n",
148 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
149 fprintf(pipe_fd
, "--not\n");
150 for (i
= 0; i
< have_obj
.nr
; i
++)
151 fprintf(pipe_fd
, "%s\n",
152 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
153 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
154 fprintf(pipe_fd
, "%s\n",
155 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
156 fprintf(pipe_fd
, "\n");
160 /* We read from pack_objects.err to capture stderr output for
161 * progress bar, and pack_objects.out to capture the pack data.
165 struct pollfd pfd
[2];
166 int pe
, pu
, pollsize
;
174 if (0 <= pack_objects
.out
) {
175 pfd
[pollsize
].fd
= pack_objects
.out
;
176 pfd
[pollsize
].events
= POLLIN
;
180 if (0 <= pack_objects
.err
) {
181 pfd
[pollsize
].fd
= pack_objects
.err
;
182 pfd
[pollsize
].events
= POLLIN
;
190 ret
= poll(pfd
, pollsize
,
191 keepalive
< 0 ? -1 : 1000 * keepalive
);
194 if (errno
!= EINTR
) {
195 error_errno("poll failed, resuming");
200 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
201 /* Status ready; we ship that in the side-band
202 * or dump to the standard error.
204 sz
= xread(pack_objects
.err
, progress
,
207 send_client_data(2, progress
, sz
);
209 close(pack_objects
.err
);
210 pack_objects
.err
= -1;
214 /* give priority to status messages */
217 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
218 /* Data ready; we keep the last byte to ourselves
219 * in case we detect broken rev-list, so that we
220 * can leave the stream corrupted. This is
221 * unfortunate -- unpack-objects would happily
222 * accept a valid packdata with trailing garbage,
223 * so appending garbage after we pass all the
224 * pack data is not good enough to signal
225 * breakage to downstream.
233 sz
= xread(pack_objects
.out
, cp
,
234 sizeof(data
) - outsz
);
238 close(pack_objects
.out
);
239 pack_objects
.out
= -1;
245 buffered
= data
[sz
-1] & 0xFF;
250 send_client_data(1, data
, sz
);
254 * We hit the keepalive timeout without saying anything; send
255 * an empty message on the data sideband just to let the other
256 * side know we're still working on it, but don't have any data
259 * If we don't have a sideband channel, there's no room in the
260 * protocol to say anything, so those clients are just out of
263 if (!ret
&& use_sideband
) {
264 static const char buf
[] = "0005\1";
265 write_or_die(1, buf
, 5);
269 if (finish_command(&pack_objects
)) {
270 error("git upload-pack: git-pack-objects died with error.");
277 send_client_data(1, data
, 1);
278 fprintf(stderr
, "flushed.\n");
285 send_client_data(3, abort_msg
, sizeof(abort_msg
));
286 die("git upload-pack: %s", abort_msg
);
289 static int got_sha1(const char *hex
, unsigned char *sha1
)
292 int we_knew_they_have
= 0;
294 if (get_sha1_hex(hex
, sha1
))
295 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
296 if (!has_sha1_file(sha1
))
299 o
= parse_object(sha1
);
301 die("oops (%s)", sha1_to_hex(sha1
));
302 if (o
->type
== OBJ_COMMIT
) {
303 struct commit_list
*parents
;
304 struct commit
*commit
= (struct commit
*)o
;
305 if (o
->flags
& THEY_HAVE
)
306 we_knew_they_have
= 1;
308 o
->flags
|= THEY_HAVE
;
309 if (!oldest_have
|| (commit
->date
< oldest_have
))
310 oldest_have
= commit
->date
;
311 for (parents
= commit
->parents
;
313 parents
= parents
->next
)
314 parents
->item
->object
.flags
|= THEY_HAVE
;
316 if (!we_knew_they_have
) {
317 add_object_array(o
, NULL
, &have_obj
);
323 static int reachable(struct commit
*want
)
325 struct prio_queue work
= { compare_commits_by_commit_date
};
327 prio_queue_put(&work
, want
);
329 struct commit_list
*list
;
330 struct commit
*commit
= prio_queue_get(&work
);
332 if (commit
->object
.flags
& THEY_HAVE
) {
333 want
->object
.flags
|= COMMON_KNOWN
;
336 if (!commit
->object
.parsed
)
337 parse_object(commit
->object
.oid
.hash
);
338 if (commit
->object
.flags
& REACHABLE
)
340 commit
->object
.flags
|= REACHABLE
;
341 if (commit
->date
< oldest_have
)
343 for (list
= commit
->parents
; list
; list
= list
->next
) {
344 struct commit
*parent
= list
->item
;
345 if (!(parent
->object
.flags
& REACHABLE
))
346 prio_queue_put(&work
, parent
);
349 want
->object
.flags
|= REACHABLE
;
350 clear_commit_marks(want
, REACHABLE
);
351 clear_prio_queue(&work
);
352 return (want
->object
.flags
& COMMON_KNOWN
);
355 static int ok_to_give_up(void)
362 for (i
= 0; i
< want_obj
.nr
; i
++) {
363 struct object
*want
= want_obj
.objects
[i
].item
;
365 if (want
->flags
& COMMON_KNOWN
)
367 want
= deref_tag(want
, "a want line", 0);
368 if (!want
|| want
->type
!= OBJ_COMMIT
) {
369 /* no way to tell if this is reachable by
370 * looking at the ancestry chain alone, so
371 * leave a note to ourselves not to worry about
372 * this object anymore.
374 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
377 if (!reachable((struct commit
*)want
))
383 static int get_common_commits(void)
385 unsigned char sha1
[20];
391 save_commit_buffer
= 0;
394 char *line
= packet_read_line(0, NULL
);
400 if (multi_ack
== 2 && got_common
401 && !got_other
&& ok_to_give_up()) {
403 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
405 if (have_obj
.nr
== 0 || multi_ack
)
406 packet_write_fmt(1, "NAK\n");
408 if (no_done
&& sent_ready
) {
409 packet_write_fmt(1, "ACK %s\n", last_hex
);
418 if (skip_prefix(line
, "have ", &arg
)) {
419 switch (got_sha1(arg
, sha1
)) {
420 case -1: /* they have what we do not */
422 if (multi_ack
&& ok_to_give_up()) {
423 const char *hex
= sha1_to_hex(sha1
);
424 if (multi_ack
== 2) {
426 packet_write_fmt(1, "ACK %s ready\n", hex
);
428 packet_write_fmt(1, "ACK %s continue\n", hex
);
433 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
435 packet_write_fmt(1, "ACK %s common\n", last_hex
);
437 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
438 else if (have_obj
.nr
== 1)
439 packet_write_fmt(1, "ACK %s\n", last_hex
);
444 if (!strcmp(line
, "done")) {
445 if (have_obj
.nr
> 0) {
447 packet_write_fmt(1, "ACK %s\n", last_hex
);
450 packet_write_fmt(1, "NAK\n");
453 die("git upload-pack: expected SHA1 list, got '%s'", line
);
457 static int is_our_ref(struct object
*o
)
459 int allow_hidden_ref
= (allow_unadvertised_object_request
&
460 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
461 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
465 * on successful case, it's up to the caller to close cmd->out
467 static int do_reachable_revlist(struct child_process
*cmd
,
468 struct object_array
*src
,
469 struct object_array
*reachable
)
471 static const char *argv
[] = {
472 "rev-list", "--stdin", NULL
,
475 char namebuf
[42]; /* ^ + SHA-1 + LF */
485 * If the next rev-list --stdin encounters an unknown commit,
486 * it terminates, which will cause SIGPIPE in the write loop
489 sigchain_push(SIGPIPE
, SIG_IGN
);
491 if (start_command(cmd
))
496 for (i
= get_max_object_index(); 0 < i
; ) {
497 o
= get_indexed_object(--i
);
500 if (reachable
&& o
->type
== OBJ_COMMIT
)
501 o
->flags
&= ~TMP_MARK
;
504 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
505 if (write_in_full(cmd
->in
, namebuf
, 42) < 0)
509 for (i
= 0; i
< src
->nr
; i
++) {
510 o
= src
->objects
[i
].item
;
513 add_object_array(o
, NULL
, reachable
);
516 if (reachable
&& o
->type
== OBJ_COMMIT
)
517 o
->flags
|= TMP_MARK
;
518 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
519 if (write_in_full(cmd
->in
, namebuf
, 41) < 0)
524 sigchain_pop(SIGPIPE
);
529 sigchain_pop(SIGPIPE
);
538 static int get_reachable_list(struct object_array
*src
,
539 struct object_array
*reachable
)
541 struct child_process cmd
= CHILD_PROCESS_INIT
;
544 char namebuf
[42]; /* ^ + SHA-1 + LF */
546 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
549 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
550 struct object_id sha1
;
552 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
555 o
= lookup_object(sha1
.hash
);
556 if (o
&& o
->type
== OBJ_COMMIT
) {
557 o
->flags
&= ~TMP_MARK
;
560 for (i
= get_max_object_index(); 0 < i
; i
--) {
561 o
= get_indexed_object(i
- 1);
562 if (o
&& o
->type
== OBJ_COMMIT
&&
563 (o
->flags
& TMP_MARK
)) {
564 add_object_array(o
, NULL
, reachable
);
565 o
->flags
&= ~TMP_MARK
;
570 if (finish_command(&cmd
))
576 static int has_unreachable(struct object_array
*src
)
578 struct child_process cmd
= CHILD_PROCESS_INIT
;
582 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
586 * The commits out of the rev-list are not ancestors of
589 i
= read_in_full(cmd
.out
, buf
, 1);
596 * rev-list may have died by encountering a bad commit
597 * in the history, in which case we do want to bail out
598 * even when it showed no commit.
600 if (finish_command(&cmd
))
603 /* All the non-tip ones are ancestors of what we advertised */
607 sigchain_pop(SIGPIPE
);
613 static void check_non_tip(void)
618 * In the normal in-process case without
619 * uploadpack.allowReachableSHA1InWant,
620 * non-tip requests can never happen.
622 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
624 if (!has_unreachable(&want_obj
))
625 /* All the non-tip ones are ancestors of what we advertised */
629 /* Pick one of them (we know there at least is one) */
630 for (i
= 0; i
< want_obj
.nr
; i
++) {
631 struct object
*o
= want_obj
.objects
[i
].item
;
633 die("git upload-pack: not our ref %s",
634 oid_to_hex(&o
->oid
));
638 static void send_shallow(struct commit_list
*result
)
641 struct object
*object
= &result
->item
->object
;
642 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
643 packet_write_fmt(1, "shallow %s",
644 oid_to_hex(&object
->oid
));
645 register_shallow(&object
->oid
);
648 result
= result
->next
;
652 static void send_unshallow(const struct object_array
*shallows
)
656 for (i
= 0; i
< shallows
->nr
; i
++) {
657 struct object
*object
= shallows
->objects
[i
].item
;
658 if (object
->flags
& NOT_SHALLOW
) {
659 struct commit_list
*parents
;
660 packet_write_fmt(1, "unshallow %s",
661 oid_to_hex(&object
->oid
));
662 object
->flags
&= ~CLIENT_SHALLOW
;
664 * We want to _register_ "object" as shallow, but we
665 * also need to traverse object's parents to deepen a
666 * shallow clone. Unregister it for now so we can
667 * parse and add the parents to the want list, then
670 unregister_shallow(&object
->oid
);
672 parse_commit_or_die((struct commit
*)object
);
673 parents
= ((struct commit
*)object
)->parents
;
675 add_object_array(&parents
->item
->object
,
677 parents
= parents
->next
;
679 add_object_array(object
, NULL
, &extra_edge_obj
);
681 /* make sure commit traversal conforms to client */
682 register_shallow(&object
->oid
);
686 static void deepen(int depth
, int deepen_relative
,
687 struct object_array
*shallows
)
689 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow()) {
692 for (i
= 0; i
< shallows
->nr
; i
++) {
693 struct object
*object
= shallows
->objects
[i
].item
;
694 object
->flags
|= NOT_SHALLOW
;
696 } else if (deepen_relative
) {
697 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
698 struct commit_list
*result
;
700 get_reachable_list(shallows
, &reachable_shallows
);
701 result
= get_shallow_commits(&reachable_shallows
,
703 SHALLOW
, NOT_SHALLOW
);
704 send_shallow(result
);
705 free_commit_list(result
);
706 object_array_clear(&reachable_shallows
);
708 struct commit_list
*result
;
710 result
= get_shallow_commits(&want_obj
, depth
,
711 SHALLOW
, NOT_SHALLOW
);
712 send_shallow(result
);
713 free_commit_list(result
);
716 send_unshallow(shallows
);
720 static void deepen_by_rev_list(int ac
, const char **av
,
721 struct object_array
*shallows
)
723 struct commit_list
*result
;
725 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
726 send_shallow(result
);
727 free_commit_list(result
);
728 send_unshallow(shallows
);
732 static void receive_needs(void)
734 struct object_array shallows
= OBJECT_ARRAY_INIT
;
735 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
738 unsigned long deepen_since
= 0;
739 int deepen_rev_list
= 0;
744 const char *features
;
745 unsigned char sha1_buf
[20];
746 char *line
= packet_read_line(0, NULL
);
753 if (skip_prefix(line
, "shallow ", &arg
)) {
754 unsigned char sha1
[20];
755 struct object
*object
;
756 if (get_sha1_hex(arg
, sha1
))
757 die("invalid shallow line: %s", line
);
758 object
= parse_object(sha1
);
761 if (object
->type
!= OBJ_COMMIT
)
762 die("invalid shallow object %s", sha1_to_hex(sha1
));
763 if (!(object
->flags
& CLIENT_SHALLOW
)) {
764 object
->flags
|= CLIENT_SHALLOW
;
765 add_object_array(object
, NULL
, &shallows
);
769 if (skip_prefix(line
, "deepen ", &arg
)) {
771 depth
= strtol(arg
, &end
, 0);
772 if (!end
|| *end
|| depth
<= 0)
773 die("Invalid deepen: %s", line
);
776 if (skip_prefix(line
, "deepen-since ", &arg
)) {
778 deepen_since
= strtoul(arg
, &end
, 0);
779 if (!end
|| *end
|| !deepen_since
||
780 /* revisions.c's max_age -1 is special */
782 die("Invalid deepen-since: %s", line
);
786 if (skip_prefix(line
, "deepen-not ", &arg
)) {
788 unsigned char sha1
[20];
789 if (expand_ref(arg
, strlen(arg
), sha1
, &ref
) != 1)
790 die("git upload-pack: ambiguous deepen-not: %s", line
);
791 string_list_append(&deepen_not
, ref
);
796 if (!skip_prefix(line
, "want ", &arg
) ||
797 get_sha1_hex(arg
, sha1_buf
))
798 die("git upload-pack: protocol error, "
799 "expected to get sha, not '%s'", line
);
803 if (parse_feature_request(features
, "deepen-relative"))
805 if (parse_feature_request(features
, "multi_ack_detailed"))
807 else if (parse_feature_request(features
, "multi_ack"))
809 if (parse_feature_request(features
, "no-done"))
811 if (parse_feature_request(features
, "thin-pack"))
813 if (parse_feature_request(features
, "ofs-delta"))
815 if (parse_feature_request(features
, "side-band-64k"))
816 use_sideband
= LARGE_PACKET_MAX
;
817 else if (parse_feature_request(features
, "side-band"))
818 use_sideband
= DEFAULT_PACKET_MAX
;
819 if (parse_feature_request(features
, "no-progress"))
821 if (parse_feature_request(features
, "include-tag"))
824 o
= parse_object(sha1_buf
);
827 "ERR upload-pack: not our ref %s",
828 sha1_to_hex(sha1_buf
));
829 die("git upload-pack: not our ref %s",
830 sha1_to_hex(sha1_buf
));
832 if (!(o
->flags
& WANTED
)) {
834 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
837 add_object_array(o
, NULL
, &want_obj
);
842 * We have sent all our refs already, and the other end
843 * should have chosen out of them. When we are operating
844 * in the stateless RPC mode, however, their choice may
845 * have been based on the set of older refs advertised
846 * by another process that handled the initial request.
851 if (!use_sideband
&& daemon_mode
)
854 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
856 if (depth
> 0 && deepen_rev_list
)
857 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
859 deepen(depth
, deepen_relative
, &shallows
);
860 else if (deepen_rev_list
) {
861 struct argv_array av
= ARGV_ARRAY_INIT
;
864 argv_array_push(&av
, "rev-list");
866 argv_array_pushf(&av
, "--max-age=%lu", deepen_since
);
868 argv_array_push(&av
, "--not");
869 for (i
= 0; i
< deepen_not
.nr
; i
++) {
870 struct string_list_item
*s
= deepen_not
.items
+ i
;
871 argv_array_push(&av
, s
->string
);
873 argv_array_push(&av
, "--not");
875 for (i
= 0; i
< want_obj
.nr
; i
++) {
876 struct object
*o
= want_obj
.objects
[i
].item
;
877 argv_array_push(&av
, oid_to_hex(&o
->oid
));
879 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
880 argv_array_clear(&av
);
883 if (shallows
.nr
> 0) {
885 for (i
= 0; i
< shallows
.nr
; i
++)
886 register_shallow(&shallows
.objects
[i
].item
->oid
);
889 shallow_nr
+= shallows
.nr
;
890 free(shallows
.objects
);
893 /* return non-zero if the ref is hidden, otherwise 0 */
894 static int mark_our_ref(const char *refname
, const char *refname_full
,
895 const struct object_id
*oid
)
897 struct object
*o
= lookup_unknown_object(oid
->hash
);
899 if (ref_is_hidden(refname
, refname_full
)) {
900 o
->flags
|= HIDDEN_REF
;
907 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
908 int flag
, void *cb_data
)
910 const char *refname
= strip_namespace(refname_full
);
912 mark_our_ref(refname
, refname_full
, oid
);
916 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
918 struct string_list_item
*item
;
922 for_each_string_list_item(item
, symref
)
923 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
926 static int send_ref(const char *refname
, const struct object_id
*oid
,
927 int flag
, void *cb_data
)
929 static const char *capabilities
= "multi_ack thin-pack side-band"
930 " side-band-64k ofs-delta shallow deepen-since deepen-not"
931 " deepen-relative no-progress include-tag multi_ack_detailed";
932 const char *refname_nons
= strip_namespace(refname
);
933 struct object_id peeled
;
935 if (mark_our_ref(refname_nons
, refname
, oid
))
939 struct strbuf symref_info
= STRBUF_INIT
;
941 format_symref_info(&symref_info
, cb_data
);
942 packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n",
943 oid_to_hex(oid
), refname_nons
,
945 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
946 " allow-tip-sha1-in-want" : "",
947 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
948 " allow-reachable-sha1-in-want" : "",
949 stateless_rpc
? " no-done" : "",
951 git_user_agent_sanitized());
952 strbuf_release(&symref_info
);
954 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
957 if (!peel_ref(refname
, peeled
.hash
))
958 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
962 static int find_symref(const char *refname
, const struct object_id
*oid
,
963 int flag
, void *cb_data
)
965 const char *symref_target
;
966 struct string_list_item
*item
;
967 struct object_id unused
;
969 if ((flag
& REF_ISSYMREF
) == 0)
971 symref_target
= resolve_ref_unsafe(refname
, 0, unused
.hash
, &flag
);
972 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
973 die("'%s' is a symref but it is not?", refname
);
974 item
= string_list_append(cb_data
, refname
);
975 item
->util
= xstrdup(symref_target
);
979 static void upload_pack(void)
981 struct string_list symref
= STRING_LIST_INIT_DUP
;
983 head_ref_namespaced(find_symref
, &symref
);
985 if (advertise_refs
|| !stateless_rpc
) {
987 head_ref_namespaced(send_ref
, &symref
);
988 for_each_namespaced_ref(send_ref
, &symref
);
989 advertise_shallow_grafts(1);
992 head_ref_namespaced(check_ref
, NULL
);
993 for_each_namespaced_ref(check_ref
, NULL
);
995 string_list_clear(&symref
, 1);
1001 get_common_commits();
1006 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1008 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1009 if (git_config_bool(var
, value
))
1010 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1012 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1013 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1014 if (git_config_bool(var
, value
))
1015 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1017 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1018 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1019 if (git_config_bool(var
, value
))
1020 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1022 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1023 } else if (!strcmp("uploadpack.keepalive", var
)) {
1024 keepalive
= git_config_int(var
, value
);
1027 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1028 if (!strcmp("uploadpack.packobjectshook", var
))
1029 return git_config_string(&pack_objects_hook
, var
, value
);
1031 return parse_hide_refs_config(var
, value
, "uploadpack");
1034 int cmd_main(int argc
, const char **argv
)
1038 struct option options
[] = {
1039 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
1040 N_("quit after a single request/response exchange")),
1041 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
1042 N_("exit immediately after initial ref advertisement")),
1043 OPT_BOOL(0, "strict", &strict
,
1044 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1045 OPT_INTEGER(0, "timeout", &timeout
,
1046 N_("interrupt transfer after <n> seconds of inactivity")),
1050 packet_trace_identity("upload-pack");
1051 check_replace_refs
= 0;
1053 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
1056 usage_with_options(upload_pack_usage
, options
);
1065 if (!enter_repo(dir
, strict
))
1066 die("'%s' does not appear to be a git repository", dir
);
1068 git_config(upload_pack_config
, NULL
);