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 static unsigned int allow_unadvertised_object_request
;
50 static int shallow_nr
;
51 static struct object_array have_obj
;
52 static struct object_array want_obj
;
53 static struct object_array extra_edge_obj
;
54 static unsigned int timeout
;
55 static int keepalive
= 5;
57 * otherwise maximum packet size (up to 65520 bytes).
59 static int use_sideband
;
60 static int advertise_refs
;
61 static int stateless_rpc
;
62 static const char *pack_objects_hook
;
64 static void reset_timeout(void)
69 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
72 send_sideband(1, fd
, data
, sz
, use_sideband
);
79 /* XXX: are we happy to lose stuff here? */
83 write_or_die(fd
, data
, sz
);
86 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
89 if (graft
->nr_parent
== -1)
90 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
94 static void create_pack_file(void)
96 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
97 char data
[8193], progress
[128];
98 char abort_msg
[] = "aborting due to possible repository "
99 "corruption on the remote side.";
105 if (!pack_objects_hook
)
106 pack_objects
.git_cmd
= 1;
108 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
109 argv_array_push(&pack_objects
.args
, "git");
110 pack_objects
.use_shell
= 1;
114 argv_array_push(&pack_objects
.args
, "--shallow-file");
115 argv_array_push(&pack_objects
.args
, "");
117 argv_array_push(&pack_objects
.args
, "pack-objects");
118 argv_array_push(&pack_objects
.args
, "--revs");
120 argv_array_push(&pack_objects
.args
, "--thin");
122 argv_array_push(&pack_objects
.args
, "--stdout");
124 argv_array_push(&pack_objects
.args
, "--shallow");
126 argv_array_push(&pack_objects
.args
, "--progress");
128 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
130 argv_array_push(&pack_objects
.args
, "--include-tag");
132 pack_objects
.in
= -1;
133 pack_objects
.out
= -1;
134 pack_objects
.err
= -1;
136 if (start_command(&pack_objects
))
137 die("git upload-pack: unable to fork git-pack-objects");
139 pipe_fd
= xfdopen(pack_objects
.in
, "w");
142 for_each_commit_graft(write_one_shallow
, pipe_fd
);
144 for (i
= 0; i
< want_obj
.nr
; i
++)
145 fprintf(pipe_fd
, "%s\n",
146 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
147 fprintf(pipe_fd
, "--not\n");
148 for (i
= 0; i
< have_obj
.nr
; i
++)
149 fprintf(pipe_fd
, "%s\n",
150 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
151 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
152 fprintf(pipe_fd
, "%s\n",
153 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
154 fprintf(pipe_fd
, "\n");
158 /* We read from pack_objects.err to capture stderr output for
159 * progress bar, and pack_objects.out to capture the pack data.
163 struct pollfd pfd
[2];
164 int pe
, pu
, pollsize
;
172 if (0 <= pack_objects
.out
) {
173 pfd
[pollsize
].fd
= pack_objects
.out
;
174 pfd
[pollsize
].events
= POLLIN
;
178 if (0 <= pack_objects
.err
) {
179 pfd
[pollsize
].fd
= pack_objects
.err
;
180 pfd
[pollsize
].events
= POLLIN
;
188 ret
= poll(pfd
, pollsize
,
189 keepalive
< 0 ? -1 : 1000 * keepalive
);
192 if (errno
!= EINTR
) {
193 error_errno("poll failed, resuming");
198 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
199 /* Status ready; we ship that in the side-band
200 * or dump to the standard error.
202 sz
= xread(pack_objects
.err
, progress
,
205 send_client_data(2, progress
, sz
);
207 close(pack_objects
.err
);
208 pack_objects
.err
= -1;
212 /* give priority to status messages */
215 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
216 /* Data ready; we keep the last byte to ourselves
217 * in case we detect broken rev-list, so that we
218 * can leave the stream corrupted. This is
219 * unfortunate -- unpack-objects would happily
220 * accept a valid packdata with trailing garbage,
221 * so appending garbage after we pass all the
222 * pack data is not good enough to signal
223 * breakage to downstream.
231 sz
= xread(pack_objects
.out
, cp
,
232 sizeof(data
) - outsz
);
236 close(pack_objects
.out
);
237 pack_objects
.out
= -1;
243 buffered
= data
[sz
-1] & 0xFF;
248 send_client_data(1, data
, sz
);
252 * We hit the keepalive timeout without saying anything; send
253 * an empty message on the data sideband just to let the other
254 * side know we're still working on it, but don't have any data
257 * If we don't have a sideband channel, there's no room in the
258 * protocol to say anything, so those clients are just out of
261 if (!ret
&& use_sideband
) {
262 static const char buf
[] = "0005\1";
263 write_or_die(1, buf
, 5);
267 if (finish_command(&pack_objects
)) {
268 error("git upload-pack: git-pack-objects died with error.");
275 send_client_data(1, data
, 1);
276 fprintf(stderr
, "flushed.\n");
283 send_client_data(3, abort_msg
, sizeof(abort_msg
));
284 die("git upload-pack: %s", abort_msg
);
287 static int got_sha1(const char *hex
, unsigned char *sha1
)
290 int we_knew_they_have
= 0;
292 if (get_sha1_hex(hex
, sha1
))
293 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
294 if (!has_sha1_file(sha1
))
297 o
= parse_object(sha1
);
299 die("oops (%s)", sha1_to_hex(sha1
));
300 if (o
->type
== OBJ_COMMIT
) {
301 struct commit_list
*parents
;
302 struct commit
*commit
= (struct commit
*)o
;
303 if (o
->flags
& THEY_HAVE
)
304 we_knew_they_have
= 1;
306 o
->flags
|= THEY_HAVE
;
307 if (!oldest_have
|| (commit
->date
< oldest_have
))
308 oldest_have
= commit
->date
;
309 for (parents
= commit
->parents
;
311 parents
= parents
->next
)
312 parents
->item
->object
.flags
|= THEY_HAVE
;
314 if (!we_knew_they_have
) {
315 add_object_array(o
, NULL
, &have_obj
);
321 static int reachable(struct commit
*want
)
323 struct prio_queue work
= { compare_commits_by_commit_date
};
325 prio_queue_put(&work
, want
);
327 struct commit_list
*list
;
328 struct commit
*commit
= prio_queue_get(&work
);
330 if (commit
->object
.flags
& THEY_HAVE
) {
331 want
->object
.flags
|= COMMON_KNOWN
;
334 if (!commit
->object
.parsed
)
335 parse_object(commit
->object
.oid
.hash
);
336 if (commit
->object
.flags
& REACHABLE
)
338 commit
->object
.flags
|= REACHABLE
;
339 if (commit
->date
< oldest_have
)
341 for (list
= commit
->parents
; list
; list
= list
->next
) {
342 struct commit
*parent
= list
->item
;
343 if (!(parent
->object
.flags
& REACHABLE
))
344 prio_queue_put(&work
, parent
);
347 want
->object
.flags
|= REACHABLE
;
348 clear_commit_marks(want
, REACHABLE
);
349 clear_prio_queue(&work
);
350 return (want
->object
.flags
& COMMON_KNOWN
);
353 static int ok_to_give_up(void)
360 for (i
= 0; i
< want_obj
.nr
; i
++) {
361 struct object
*want
= want_obj
.objects
[i
].item
;
363 if (want
->flags
& COMMON_KNOWN
)
365 want
= deref_tag(want
, "a want line", 0);
366 if (!want
|| want
->type
!= OBJ_COMMIT
) {
367 /* no way to tell if this is reachable by
368 * looking at the ancestry chain alone, so
369 * leave a note to ourselves not to worry about
370 * this object anymore.
372 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
375 if (!reachable((struct commit
*)want
))
381 static int get_common_commits(void)
383 unsigned char sha1
[20];
389 save_commit_buffer
= 0;
392 char *line
= packet_read_line(0, NULL
);
398 if (multi_ack
== 2 && got_common
399 && !got_other
&& ok_to_give_up()) {
401 packet_write(1, "ACK %s ready\n", last_hex
);
403 if (have_obj
.nr
== 0 || multi_ack
)
404 packet_write(1, "NAK\n");
406 if (no_done
&& sent_ready
) {
407 packet_write(1, "ACK %s\n", last_hex
);
416 if (skip_prefix(line
, "have ", &arg
)) {
417 switch (got_sha1(arg
, sha1
)) {
418 case -1: /* they have what we do not */
420 if (multi_ack
&& ok_to_give_up()) {
421 const char *hex
= sha1_to_hex(sha1
);
422 if (multi_ack
== 2) {
424 packet_write(1, "ACK %s ready\n", hex
);
426 packet_write(1, "ACK %s continue\n", hex
);
431 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
433 packet_write(1, "ACK %s common\n", last_hex
);
435 packet_write(1, "ACK %s continue\n", last_hex
);
436 else if (have_obj
.nr
== 1)
437 packet_write(1, "ACK %s\n", last_hex
);
442 if (!strcmp(line
, "done")) {
443 if (have_obj
.nr
> 0) {
445 packet_write(1, "ACK %s\n", last_hex
);
448 packet_write(1, "NAK\n");
451 die("git upload-pack: expected SHA1 list, got '%s'", line
);
455 static int is_our_ref(struct object
*o
)
457 int allow_hidden_ref
= (allow_unadvertised_object_request
&
458 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
459 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
463 * on successful case, it's up to the caller to close cmd->out
465 static int do_reachable_revlist(struct child_process
*cmd
,
466 struct object_array
*src
,
467 struct object_array
*reachable
)
469 static const char *argv
[] = {
470 "rev-list", "--stdin", NULL
,
473 char namebuf
[42]; /* ^ + SHA-1 + LF */
483 * If the next rev-list --stdin encounters an unknown commit,
484 * it terminates, which will cause SIGPIPE in the write loop
487 sigchain_push(SIGPIPE
, SIG_IGN
);
489 if (start_command(cmd
))
494 for (i
= get_max_object_index(); 0 < i
; ) {
495 o
= get_indexed_object(--i
);
498 if (reachable
&& o
->type
== OBJ_COMMIT
)
499 o
->flags
&= ~TMP_MARK
;
502 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
503 if (write_in_full(cmd
->in
, namebuf
, 42) < 0)
507 for (i
= 0; i
< src
->nr
; i
++) {
508 o
= src
->objects
[i
].item
;
511 add_object_array(o
, NULL
, reachable
);
514 if (reachable
&& o
->type
== OBJ_COMMIT
)
515 o
->flags
|= TMP_MARK
;
516 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
517 if (write_in_full(cmd
->in
, namebuf
, 41) < 0)
522 sigchain_pop(SIGPIPE
);
527 sigchain_pop(SIGPIPE
);
536 static int get_reachable_list(struct object_array
*src
,
537 struct object_array
*reachable
)
539 struct child_process cmd
= CHILD_PROCESS_INIT
;
542 char namebuf
[42]; /* ^ + SHA-1 + LF */
544 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
547 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
548 struct object_id sha1
;
550 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
553 o
= lookup_object(sha1
.hash
);
554 if (o
&& o
->type
== OBJ_COMMIT
) {
555 o
->flags
&= ~TMP_MARK
;
558 for (i
= get_max_object_index(); 0 < i
; i
--) {
559 o
= get_indexed_object(i
- 1);
560 if (o
&& o
->type
== OBJ_COMMIT
&&
561 (o
->flags
& TMP_MARK
)) {
562 add_object_array(o
, NULL
, reachable
);
563 o
->flags
&= ~TMP_MARK
;
568 if (finish_command(&cmd
))
574 static int has_unreachable(struct object_array
*src
)
576 struct child_process cmd
= CHILD_PROCESS_INIT
;
580 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
584 * The commits out of the rev-list are not ancestors of
587 i
= read_in_full(cmd
.out
, buf
, 1);
594 * rev-list may have died by encountering a bad commit
595 * in the history, in which case we do want to bail out
596 * even when it showed no commit.
598 if (finish_command(&cmd
))
601 /* All the non-tip ones are ancestors of what we advertised */
605 sigchain_pop(SIGPIPE
);
611 static void check_non_tip(void)
616 * In the normal in-process case without
617 * uploadpack.allowReachableSHA1InWant,
618 * non-tip requests can never happen.
620 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
622 if (!has_unreachable(&want_obj
))
623 /* All the non-tip ones are ancestors of what we advertised */
627 /* Pick one of them (we know there at least is one) */
628 for (i
= 0; i
< want_obj
.nr
; i
++) {
629 struct object
*o
= want_obj
.objects
[i
].item
;
631 die("git upload-pack: not our ref %s",
632 oid_to_hex(&o
->oid
));
636 static void send_shallow(struct commit_list
*result
)
639 struct object
*object
= &result
->item
->object
;
640 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
641 packet_write(1, "shallow %s",
642 oid_to_hex(&object
->oid
));
643 register_shallow(object
->oid
.hash
);
646 result
= result
->next
;
650 static void send_unshallow(const struct object_array
*shallows
)
654 for (i
= 0; i
< shallows
->nr
; i
++) {
655 struct object
*object
= shallows
->objects
[i
].item
;
656 if (object
->flags
& NOT_SHALLOW
) {
657 struct commit_list
*parents
;
658 packet_write(1, "unshallow %s",
659 oid_to_hex(&object
->oid
));
660 object
->flags
&= ~CLIENT_SHALLOW
;
662 * We want to _register_ "object" as shallow, but we
663 * also need to traverse object's parents to deepen a
664 * shallow clone. Unregister it for now so we can
665 * parse and add the parents to the want list, then
668 unregister_shallow(object
->oid
.hash
);
670 parse_commit_or_die((struct commit
*)object
);
671 parents
= ((struct commit
*)object
)->parents
;
673 add_object_array(&parents
->item
->object
,
675 parents
= parents
->next
;
677 add_object_array(object
, NULL
, &extra_edge_obj
);
679 /* make sure commit traversal conforms to client */
680 register_shallow(object
->oid
.hash
);
684 static void deepen(int depth
, int deepen_relative
,
685 struct object_array
*shallows
)
687 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow()) {
690 for (i
= 0; i
< shallows
->nr
; i
++) {
691 struct object
*object
= shallows
->objects
[i
].item
;
692 object
->flags
|= NOT_SHALLOW
;
694 } else if (deepen_relative
) {
695 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
696 struct commit_list
*result
;
698 get_reachable_list(shallows
, &reachable_shallows
);
699 result
= get_shallow_commits(&reachable_shallows
,
701 SHALLOW
, NOT_SHALLOW
);
702 send_shallow(result
);
703 free_commit_list(result
);
704 object_array_clear(&reachable_shallows
);
706 struct commit_list
*result
;
708 result
= get_shallow_commits(&want_obj
, depth
,
709 SHALLOW
, NOT_SHALLOW
);
710 send_shallow(result
);
711 free_commit_list(result
);
714 send_unshallow(shallows
);
718 static void deepen_by_rev_list(int ac
, const char **av
,
719 struct object_array
*shallows
)
721 struct commit_list
*result
;
723 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
724 send_shallow(result
);
725 free_commit_list(result
);
726 send_unshallow(shallows
);
730 static void receive_needs(void)
732 struct object_array shallows
= OBJECT_ARRAY_INIT
;
733 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
736 unsigned long deepen_since
= 0;
737 int deepen_rev_list
= 0;
742 const char *features
;
743 unsigned char sha1_buf
[20];
744 char *line
= packet_read_line(0, NULL
);
751 if (skip_prefix(line
, "shallow ", &arg
)) {
752 unsigned char sha1
[20];
753 struct object
*object
;
754 if (get_sha1_hex(arg
, sha1
))
755 die("invalid shallow line: %s", line
);
756 object
= parse_object(sha1
);
759 if (object
->type
!= OBJ_COMMIT
)
760 die("invalid shallow object %s", sha1_to_hex(sha1
));
761 if (!(object
->flags
& CLIENT_SHALLOW
)) {
762 object
->flags
|= CLIENT_SHALLOW
;
763 add_object_array(object
, NULL
, &shallows
);
767 if (skip_prefix(line
, "deepen ", &arg
)) {
769 depth
= strtol(arg
, &end
, 0);
770 if (!end
|| *end
|| depth
<= 0)
771 die("Invalid deepen: %s", line
);
774 if (skip_prefix(line
, "deepen-since ", &arg
)) {
776 deepen_since
= strtoul(arg
, &end
, 0);
777 if (!end
|| *end
|| !deepen_since
||
778 /* revisions.c's max_age -1 is special */
780 die("Invalid deepen-since: %s", line
);
784 if (skip_prefix(line
, "deepen-not ", &arg
)) {
786 unsigned char sha1
[20];
787 if (expand_ref(arg
, strlen(arg
), sha1
, &ref
) != 1)
788 die("git upload-pack: ambiguous deepen-not: %s", line
);
789 string_list_append(&deepen_not
, ref
);
794 if (!skip_prefix(line
, "want ", &arg
) ||
795 get_sha1_hex(arg
, sha1_buf
))
796 die("git upload-pack: protocol error, "
797 "expected to get sha, not '%s'", line
);
801 if (parse_feature_request(features
, "deepen-relative"))
803 if (parse_feature_request(features
, "multi_ack_detailed"))
805 else if (parse_feature_request(features
, "multi_ack"))
807 if (parse_feature_request(features
, "no-done"))
809 if (parse_feature_request(features
, "thin-pack"))
811 if (parse_feature_request(features
, "ofs-delta"))
813 if (parse_feature_request(features
, "side-band-64k"))
814 use_sideband
= LARGE_PACKET_MAX
;
815 else if (parse_feature_request(features
, "side-band"))
816 use_sideband
= DEFAULT_PACKET_MAX
;
817 if (parse_feature_request(features
, "no-progress"))
819 if (parse_feature_request(features
, "include-tag"))
822 o
= parse_object(sha1_buf
);
824 die("git upload-pack: not our ref %s",
825 sha1_to_hex(sha1_buf
));
826 if (!(o
->flags
& WANTED
)) {
830 add_object_array(o
, NULL
, &want_obj
);
835 * We have sent all our refs already, and the other end
836 * should have chosen out of them. When we are operating
837 * in the stateless RPC mode, however, their choice may
838 * have been based on the set of older refs advertised
839 * by another process that handled the initial request.
844 if (!use_sideband
&& daemon_mode
)
847 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
849 if (depth
> 0 && deepen_rev_list
)
850 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
852 deepen(depth
, deepen_relative
, &shallows
);
853 else if (deepen_rev_list
) {
854 struct argv_array av
= ARGV_ARRAY_INIT
;
857 argv_array_push(&av
, "rev-list");
859 argv_array_pushf(&av
, "--max-age=%lu", deepen_since
);
861 argv_array_push(&av
, "--not");
862 for (i
= 0; i
< deepen_not
.nr
; i
++) {
863 struct string_list_item
*s
= deepen_not
.items
+ i
;
864 argv_array_push(&av
, s
->string
);
866 argv_array_push(&av
, "--not");
868 for (i
= 0; i
< want_obj
.nr
; i
++) {
869 struct object
*o
= want_obj
.objects
[i
].item
;
870 argv_array_push(&av
, oid_to_hex(&o
->oid
));
872 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
873 argv_array_clear(&av
);
876 if (shallows
.nr
> 0) {
878 for (i
= 0; i
< shallows
.nr
; i
++)
879 register_shallow(shallows
.objects
[i
].item
->oid
.hash
);
882 shallow_nr
+= shallows
.nr
;
883 free(shallows
.objects
);
886 /* return non-zero if the ref is hidden, otherwise 0 */
887 static int mark_our_ref(const char *refname
, const char *refname_full
,
888 const struct object_id
*oid
)
890 struct object
*o
= lookup_unknown_object(oid
->hash
);
892 if (ref_is_hidden(refname
, refname_full
)) {
893 o
->flags
|= HIDDEN_REF
;
900 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
901 int flag
, void *cb_data
)
903 const char *refname
= strip_namespace(refname_full
);
905 mark_our_ref(refname
, refname_full
, oid
);
909 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
911 struct string_list_item
*item
;
915 for_each_string_list_item(item
, symref
)
916 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
919 static int send_ref(const char *refname
, const struct object_id
*oid
,
920 int flag
, void *cb_data
)
922 static const char *capabilities
= "multi_ack thin-pack side-band"
923 " side-band-64k ofs-delta shallow deepen-since deepen-not"
924 " deepen-relative no-progress include-tag multi_ack_detailed";
925 const char *refname_nons
= strip_namespace(refname
);
926 struct object_id peeled
;
928 if (mark_our_ref(refname_nons
, refname
, oid
))
932 struct strbuf symref_info
= STRBUF_INIT
;
934 format_symref_info(&symref_info
, cb_data
);
935 packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
936 oid_to_hex(oid
), refname_nons
,
938 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
939 " allow-tip-sha1-in-want" : "",
940 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
941 " allow-reachable-sha1-in-want" : "",
942 stateless_rpc
? " no-done" : "",
944 git_user_agent_sanitized());
945 strbuf_release(&symref_info
);
947 packet_write(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
950 if (!peel_ref(refname
, peeled
.hash
))
951 packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
955 static int find_symref(const char *refname
, const struct object_id
*oid
,
956 int flag
, void *cb_data
)
958 const char *symref_target
;
959 struct string_list_item
*item
;
960 struct object_id unused
;
962 if ((flag
& REF_ISSYMREF
) == 0)
964 symref_target
= resolve_ref_unsafe(refname
, 0, unused
.hash
, &flag
);
965 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
966 die("'%s' is a symref but it is not?", refname
);
967 item
= string_list_append(cb_data
, refname
);
968 item
->util
= xstrdup(symref_target
);
972 static void upload_pack(void)
974 struct string_list symref
= STRING_LIST_INIT_DUP
;
976 head_ref_namespaced(find_symref
, &symref
);
978 if (advertise_refs
|| !stateless_rpc
) {
980 head_ref_namespaced(send_ref
, &symref
);
981 for_each_namespaced_ref(send_ref
, &symref
);
982 advertise_shallow_grafts(1);
985 head_ref_namespaced(check_ref
, NULL
);
986 for_each_namespaced_ref(check_ref
, NULL
);
988 string_list_clear(&symref
, 1);
994 get_common_commits();
999 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1001 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1002 if (git_config_bool(var
, value
))
1003 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1005 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1006 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1007 if (git_config_bool(var
, value
))
1008 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1010 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1011 } else if (!strcmp("uploadpack.keepalive", var
)) {
1012 keepalive
= git_config_int(var
, value
);
1015 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1016 if (!strcmp("uploadpack.packobjectshook", var
))
1017 return git_config_string(&pack_objects_hook
, var
, value
);
1019 return parse_hide_refs_config(var
, value
, "uploadpack");
1022 int cmd_main(int argc
, const char **argv
)
1026 struct option options
[] = {
1027 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
1028 N_("quit after a single request/response exchange")),
1029 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
1030 N_("exit immediately after initial ref advertisement")),
1031 OPT_BOOL(0, "strict", &strict
,
1032 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1033 OPT_INTEGER(0, "timeout", &timeout
,
1034 N_("interrupt transfer after <n> seconds of inactivity")),
1038 packet_trace_identity("upload-pack");
1039 check_replace_refs
= 0;
1041 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
1044 usage_with_options(upload_pack_usage
, options
);
1053 if (!enter_repo(dir
, strict
))
1054 die("'%s' does not appear to be a git repository", dir
);
1056 git_config(upload_pack_config
, NULL
);