6 #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 "parse-options.h"
22 #include "argv-array.h"
23 #include "prio-queue.h"
27 static const char * const upload_pack_usage
[] = {
28 N_("git upload-pack [<options>] <dir>"),
32 /* Remember to update object flag allocation in object.h */
33 #define THEY_HAVE (1u << 11)
34 #define OUR_REF (1u << 12)
35 #define WANTED (1u << 13)
36 #define COMMON_KNOWN (1u << 14)
37 #define REACHABLE (1u << 15)
39 #define SHALLOW (1u << 16)
40 #define NOT_SHALLOW (1u << 17)
41 #define CLIENT_SHALLOW (1u << 18)
42 #define HIDDEN_REF (1u << 19)
44 static timestamp_t oldest_have
;
46 static int deepen_relative
;
49 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
50 static int no_progress
, daemon_mode
;
51 /* Allow specifying sha1 if it is a ref tip. */
52 #define ALLOW_TIP_SHA1 01
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 #define ALLOW_REACHABLE_SHA1 02
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 #define ALLOW_ANY_SHA1 07
57 static unsigned int allow_unadvertised_object_request
;
58 static int shallow_nr
;
59 static struct object_array have_obj
;
60 static struct object_array want_obj
;
61 static struct object_array extra_edge_obj
;
62 static unsigned int timeout
;
63 static int keepalive
= 5;
65 * otherwise maximum packet size (up to 65520 bytes).
67 static int use_sideband
;
68 static int advertise_refs
;
69 static int stateless_rpc
;
70 static const char *pack_objects_hook
;
72 static int filter_capability_requested
;
73 static int allow_filter
;
74 static struct list_objects_filter_options filter_options
;
76 static void reset_timeout(void)
81 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
84 send_sideband(1, fd
, data
, sz
, use_sideband
);
91 /* XXX: are we happy to lose stuff here? */
95 write_or_die(fd
, data
, sz
);
98 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
101 if (graft
->nr_parent
== -1)
102 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
106 static void create_pack_file(void)
108 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
109 char data
[8193], progress
[128];
110 char abort_msg
[] = "aborting due to possible repository "
111 "corruption on the remote side.";
117 if (!pack_objects_hook
)
118 pack_objects
.git_cmd
= 1;
120 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
121 argv_array_push(&pack_objects
.args
, "git");
122 pack_objects
.use_shell
= 1;
126 argv_array_push(&pack_objects
.args
, "--shallow-file");
127 argv_array_push(&pack_objects
.args
, "");
129 argv_array_push(&pack_objects
.args
, "pack-objects");
130 argv_array_push(&pack_objects
.args
, "--revs");
132 argv_array_push(&pack_objects
.args
, "--thin");
134 argv_array_push(&pack_objects
.args
, "--stdout");
136 argv_array_push(&pack_objects
.args
, "--shallow");
138 argv_array_push(&pack_objects
.args
, "--progress");
140 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
142 argv_array_push(&pack_objects
.args
, "--include-tag");
143 if (filter_options
.filter_spec
) {
144 if (pack_objects
.use_shell
) {
145 struct strbuf buf
= STRBUF_INIT
;
146 sq_quote_buf(&buf
, filter_options
.filter_spec
);
147 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
148 strbuf_release(&buf
);
150 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
151 filter_options
.filter_spec
);
155 pack_objects
.in
= -1;
156 pack_objects
.out
= -1;
157 pack_objects
.err
= -1;
159 if (start_command(&pack_objects
))
160 die("git upload-pack: unable to fork git-pack-objects");
162 pipe_fd
= xfdopen(pack_objects
.in
, "w");
165 for_each_commit_graft(write_one_shallow
, pipe_fd
);
167 for (i
= 0; i
< want_obj
.nr
; i
++)
168 fprintf(pipe_fd
, "%s\n",
169 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
170 fprintf(pipe_fd
, "--not\n");
171 for (i
= 0; i
< have_obj
.nr
; i
++)
172 fprintf(pipe_fd
, "%s\n",
173 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
174 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
175 fprintf(pipe_fd
, "%s\n",
176 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
177 fprintf(pipe_fd
, "\n");
181 /* We read from pack_objects.err to capture stderr output for
182 * progress bar, and pack_objects.out to capture the pack data.
186 struct pollfd pfd
[2];
187 int pe
, pu
, pollsize
;
195 if (0 <= pack_objects
.out
) {
196 pfd
[pollsize
].fd
= pack_objects
.out
;
197 pfd
[pollsize
].events
= POLLIN
;
201 if (0 <= pack_objects
.err
) {
202 pfd
[pollsize
].fd
= pack_objects
.err
;
203 pfd
[pollsize
].events
= POLLIN
;
211 ret
= poll(pfd
, pollsize
,
212 keepalive
< 0 ? -1 : 1000 * keepalive
);
215 if (errno
!= EINTR
) {
216 error_errno("poll failed, resuming");
221 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
222 /* Status ready; we ship that in the side-band
223 * or dump to the standard error.
225 sz
= xread(pack_objects
.err
, progress
,
228 send_client_data(2, progress
, sz
);
230 close(pack_objects
.err
);
231 pack_objects
.err
= -1;
235 /* give priority to status messages */
238 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
239 /* Data ready; we keep the last byte to ourselves
240 * in case we detect broken rev-list, so that we
241 * can leave the stream corrupted. This is
242 * unfortunate -- unpack-objects would happily
243 * accept a valid packdata with trailing garbage,
244 * so appending garbage after we pass all the
245 * pack data is not good enough to signal
246 * breakage to downstream.
254 sz
= xread(pack_objects
.out
, cp
,
255 sizeof(data
) - outsz
);
259 close(pack_objects
.out
);
260 pack_objects
.out
= -1;
266 buffered
= data
[sz
-1] & 0xFF;
271 send_client_data(1, data
, sz
);
275 * We hit the keepalive timeout without saying anything; send
276 * an empty message on the data sideband just to let the other
277 * side know we're still working on it, but don't have any data
280 * If we don't have a sideband channel, there's no room in the
281 * protocol to say anything, so those clients are just out of
284 if (!ret
&& use_sideband
) {
285 static const char buf
[] = "0005\1";
286 write_or_die(1, buf
, 5);
290 if (finish_command(&pack_objects
)) {
291 error("git upload-pack: git-pack-objects died with error.");
298 send_client_data(1, data
, 1);
299 fprintf(stderr
, "flushed.\n");
306 send_client_data(3, abort_msg
, sizeof(abort_msg
));
307 die("git upload-pack: %s", abort_msg
);
310 static int got_oid(const char *hex
, struct object_id
*oid
)
313 int we_knew_they_have
= 0;
315 if (get_oid_hex(hex
, oid
))
316 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
317 if (!has_object_file(oid
))
320 o
= parse_object(oid
);
322 die("oops (%s)", oid_to_hex(oid
));
323 if (o
->type
== OBJ_COMMIT
) {
324 struct commit_list
*parents
;
325 struct commit
*commit
= (struct commit
*)o
;
326 if (o
->flags
& THEY_HAVE
)
327 we_knew_they_have
= 1;
329 o
->flags
|= THEY_HAVE
;
330 if (!oldest_have
|| (commit
->date
< oldest_have
))
331 oldest_have
= commit
->date
;
332 for (parents
= commit
->parents
;
334 parents
= parents
->next
)
335 parents
->item
->object
.flags
|= THEY_HAVE
;
337 if (!we_knew_they_have
) {
338 add_object_array(o
, NULL
, &have_obj
);
344 static int reachable(struct commit
*want
)
346 struct prio_queue work
= { compare_commits_by_commit_date
};
348 prio_queue_put(&work
, want
);
350 struct commit_list
*list
;
351 struct commit
*commit
= prio_queue_get(&work
);
353 if (commit
->object
.flags
& THEY_HAVE
) {
354 want
->object
.flags
|= COMMON_KNOWN
;
357 if (!commit
->object
.parsed
)
358 parse_object(&commit
->object
.oid
);
359 if (commit
->object
.flags
& REACHABLE
)
361 commit
->object
.flags
|= REACHABLE
;
362 if (commit
->date
< oldest_have
)
364 for (list
= commit
->parents
; list
; list
= list
->next
) {
365 struct commit
*parent
= list
->item
;
366 if (!(parent
->object
.flags
& REACHABLE
))
367 prio_queue_put(&work
, parent
);
370 want
->object
.flags
|= REACHABLE
;
371 clear_commit_marks(want
, REACHABLE
);
372 clear_prio_queue(&work
);
373 return (want
->object
.flags
& COMMON_KNOWN
);
376 static int ok_to_give_up(void)
383 for (i
= 0; i
< want_obj
.nr
; i
++) {
384 struct object
*want
= want_obj
.objects
[i
].item
;
386 if (want
->flags
& COMMON_KNOWN
)
388 want
= deref_tag(want
, "a want line", 0);
389 if (!want
|| want
->type
!= OBJ_COMMIT
) {
390 /* no way to tell if this is reachable by
391 * looking at the ancestry chain alone, so
392 * leave a note to ourselves not to worry about
393 * this object anymore.
395 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
398 if (!reachable((struct commit
*)want
))
404 static int get_common_commits(void)
406 struct object_id oid
;
407 char last_hex
[GIT_MAX_HEXSZ
+ 1];
412 save_commit_buffer
= 0;
415 char *line
= packet_read_line(0, NULL
);
421 if (multi_ack
== 2 && got_common
422 && !got_other
&& ok_to_give_up()) {
424 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
426 if (have_obj
.nr
== 0 || multi_ack
)
427 packet_write_fmt(1, "NAK\n");
429 if (no_done
&& sent_ready
) {
430 packet_write_fmt(1, "ACK %s\n", last_hex
);
439 if (skip_prefix(line
, "have ", &arg
)) {
440 switch (got_oid(arg
, &oid
)) {
441 case -1: /* they have what we do not */
443 if (multi_ack
&& ok_to_give_up()) {
444 const char *hex
= oid_to_hex(&oid
);
445 if (multi_ack
== 2) {
447 packet_write_fmt(1, "ACK %s ready\n", hex
);
449 packet_write_fmt(1, "ACK %s continue\n", hex
);
454 memcpy(last_hex
, oid_to_hex(&oid
), 41);
456 packet_write_fmt(1, "ACK %s common\n", last_hex
);
458 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
459 else if (have_obj
.nr
== 1)
460 packet_write_fmt(1, "ACK %s\n", last_hex
);
465 if (!strcmp(line
, "done")) {
466 if (have_obj
.nr
> 0) {
468 packet_write_fmt(1, "ACK %s\n", last_hex
);
471 packet_write_fmt(1, "NAK\n");
474 die("git upload-pack: expected SHA1 list, got '%s'", line
);
478 static int is_our_ref(struct object
*o
)
480 int allow_hidden_ref
= (allow_unadvertised_object_request
&
481 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
482 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
486 * on successful case, it's up to the caller to close cmd->out
488 static int do_reachable_revlist(struct child_process
*cmd
,
489 struct object_array
*src
,
490 struct object_array
*reachable
)
492 static const char *argv
[] = {
493 "rev-list", "--stdin", NULL
,
496 char namebuf
[42]; /* ^ + SHA-1 + LF */
506 * If the next rev-list --stdin encounters an unknown commit,
507 * it terminates, which will cause SIGPIPE in the write loop
510 sigchain_push(SIGPIPE
, SIG_IGN
);
512 if (start_command(cmd
))
516 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
517 for (i
= get_max_object_index(); 0 < i
; ) {
518 o
= get_indexed_object(--i
);
521 if (reachable
&& o
->type
== OBJ_COMMIT
)
522 o
->flags
&= ~TMP_MARK
;
525 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
526 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
529 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
530 for (i
= 0; i
< src
->nr
; i
++) {
531 o
= src
->objects
[i
].item
;
534 add_object_array(o
, NULL
, reachable
);
537 if (reachable
&& o
->type
== OBJ_COMMIT
)
538 o
->flags
|= TMP_MARK
;
539 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
540 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
545 sigchain_pop(SIGPIPE
);
550 sigchain_pop(SIGPIPE
);
559 static int get_reachable_list(struct object_array
*src
,
560 struct object_array
*reachable
)
562 struct child_process cmd
= CHILD_PROCESS_INIT
;
565 char namebuf
[42]; /* ^ + SHA-1 + LF */
567 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
570 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
571 struct object_id sha1
;
573 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
576 o
= lookup_object(sha1
.hash
);
577 if (o
&& o
->type
== OBJ_COMMIT
) {
578 o
->flags
&= ~TMP_MARK
;
581 for (i
= get_max_object_index(); 0 < i
; i
--) {
582 o
= get_indexed_object(i
- 1);
583 if (o
&& o
->type
== OBJ_COMMIT
&&
584 (o
->flags
& TMP_MARK
)) {
585 add_object_array(o
, NULL
, reachable
);
586 o
->flags
&= ~TMP_MARK
;
591 if (finish_command(&cmd
))
597 static int has_unreachable(struct object_array
*src
)
599 struct child_process cmd
= CHILD_PROCESS_INIT
;
603 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
607 * The commits out of the rev-list are not ancestors of
610 i
= read_in_full(cmd
.out
, buf
, 1);
617 * rev-list may have died by encountering a bad commit
618 * in the history, in which case we do want to bail out
619 * even when it showed no commit.
621 if (finish_command(&cmd
))
624 /* All the non-tip ones are ancestors of what we advertised */
628 sigchain_pop(SIGPIPE
);
634 static void check_non_tip(void)
639 * In the normal in-process case without
640 * uploadpack.allowReachableSHA1InWant,
641 * non-tip requests can never happen.
643 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
645 if (!has_unreachable(&want_obj
))
646 /* All the non-tip ones are ancestors of what we advertised */
650 /* Pick one of them (we know there at least is one) */
651 for (i
= 0; i
< want_obj
.nr
; i
++) {
652 struct object
*o
= want_obj
.objects
[i
].item
;
654 die("git upload-pack: not our ref %s",
655 oid_to_hex(&o
->oid
));
659 static void send_shallow(struct commit_list
*result
)
662 struct object
*object
= &result
->item
->object
;
663 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
664 packet_write_fmt(1, "shallow %s",
665 oid_to_hex(&object
->oid
));
666 register_shallow(the_repository
, &object
->oid
);
669 result
= result
->next
;
673 static void send_unshallow(const struct object_array
*shallows
)
677 for (i
= 0; i
< shallows
->nr
; i
++) {
678 struct object
*object
= shallows
->objects
[i
].item
;
679 if (object
->flags
& NOT_SHALLOW
) {
680 struct commit_list
*parents
;
681 packet_write_fmt(1, "unshallow %s",
682 oid_to_hex(&object
->oid
));
683 object
->flags
&= ~CLIENT_SHALLOW
;
685 * We want to _register_ "object" as shallow, but we
686 * also need to traverse object's parents to deepen a
687 * shallow clone. Unregister it for now so we can
688 * parse and add the parents to the want list, then
691 unregister_shallow(&object
->oid
);
693 parse_commit_or_die((struct commit
*)object
);
694 parents
= ((struct commit
*)object
)->parents
;
696 add_object_array(&parents
->item
->object
,
698 parents
= parents
->next
;
700 add_object_array(object
, NULL
, &extra_edge_obj
);
702 /* make sure commit traversal conforms to client */
703 register_shallow(the_repository
, &object
->oid
);
707 static void deepen(int depth
, int deepen_relative
,
708 struct object_array
*shallows
)
710 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow(the_repository
)) {
713 for (i
= 0; i
< shallows
->nr
; i
++) {
714 struct object
*object
= shallows
->objects
[i
].item
;
715 object
->flags
|= NOT_SHALLOW
;
717 } else if (deepen_relative
) {
718 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
719 struct commit_list
*result
;
721 get_reachable_list(shallows
, &reachable_shallows
);
722 result
= get_shallow_commits(&reachable_shallows
,
724 SHALLOW
, NOT_SHALLOW
);
725 send_shallow(result
);
726 free_commit_list(result
);
727 object_array_clear(&reachable_shallows
);
729 struct commit_list
*result
;
731 result
= get_shallow_commits(&want_obj
, depth
,
732 SHALLOW
, NOT_SHALLOW
);
733 send_shallow(result
);
734 free_commit_list(result
);
737 send_unshallow(shallows
);
741 static void deepen_by_rev_list(int ac
, const char **av
,
742 struct object_array
*shallows
)
744 struct commit_list
*result
;
746 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
747 send_shallow(result
);
748 free_commit_list(result
);
749 send_unshallow(shallows
);
753 static void receive_needs(void)
755 struct object_array shallows
= OBJECT_ARRAY_INIT
;
756 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
759 timestamp_t deepen_since
= 0;
760 int deepen_rev_list
= 0;
765 const char *features
;
766 struct object_id oid_buf
;
767 char *line
= packet_read_line(0, NULL
);
774 if (skip_prefix(line
, "shallow ", &arg
)) {
775 struct object_id oid
;
776 struct object
*object
;
777 if (get_oid_hex(arg
, &oid
))
778 die("invalid shallow line: %s", line
);
779 object
= parse_object(&oid
);
782 if (object
->type
!= OBJ_COMMIT
)
783 die("invalid shallow object %s", oid_to_hex(&oid
));
784 if (!(object
->flags
& CLIENT_SHALLOW
)) {
785 object
->flags
|= CLIENT_SHALLOW
;
786 add_object_array(object
, NULL
, &shallows
);
790 if (skip_prefix(line
, "deepen ", &arg
)) {
792 depth
= strtol(arg
, &end
, 0);
793 if (!end
|| *end
|| depth
<= 0)
794 die("Invalid deepen: %s", line
);
797 if (skip_prefix(line
, "deepen-since ", &arg
)) {
799 deepen_since
= parse_timestamp(arg
, &end
, 0);
800 if (!end
|| *end
|| !deepen_since
||
801 /* revisions.c's max_age -1 is special */
803 die("Invalid deepen-since: %s", line
);
807 if (skip_prefix(line
, "deepen-not ", &arg
)) {
809 struct object_id oid
;
810 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
811 die("git upload-pack: ambiguous deepen-not: %s", line
);
812 string_list_append(&deepen_not
, ref
);
817 if (skip_prefix(line
, "filter ", &arg
)) {
818 if (!filter_capability_requested
)
819 die("git upload-pack: filtering capability not negotiated");
820 parse_list_objects_filter(&filter_options
, arg
);
823 if (!skip_prefix(line
, "want ", &arg
) ||
824 get_oid_hex(arg
, &oid_buf
))
825 die("git upload-pack: protocol error, "
826 "expected to get sha, not '%s'", line
);
830 if (parse_feature_request(features
, "deepen-relative"))
832 if (parse_feature_request(features
, "multi_ack_detailed"))
834 else if (parse_feature_request(features
, "multi_ack"))
836 if (parse_feature_request(features
, "no-done"))
838 if (parse_feature_request(features
, "thin-pack"))
840 if (parse_feature_request(features
, "ofs-delta"))
842 if (parse_feature_request(features
, "side-band-64k"))
843 use_sideband
= LARGE_PACKET_MAX
;
844 else if (parse_feature_request(features
, "side-band"))
845 use_sideband
= DEFAULT_PACKET_MAX
;
846 if (parse_feature_request(features
, "no-progress"))
848 if (parse_feature_request(features
, "include-tag"))
850 if (allow_filter
&& parse_feature_request(features
, "filter"))
851 filter_capability_requested
= 1;
853 o
= parse_object(&oid_buf
);
856 "ERR upload-pack: not our ref %s",
857 oid_to_hex(&oid_buf
));
858 die("git upload-pack: not our ref %s",
859 oid_to_hex(&oid_buf
));
861 if (!(o
->flags
& WANTED
)) {
863 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
866 add_object_array(o
, NULL
, &want_obj
);
871 * We have sent all our refs already, and the other end
872 * should have chosen out of them. When we are operating
873 * in the stateless RPC mode, however, their choice may
874 * have been based on the set of older refs advertised
875 * by another process that handled the initial request.
880 if (!use_sideband
&& daemon_mode
)
883 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
885 if (depth
> 0 && deepen_rev_list
)
886 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
888 deepen(depth
, deepen_relative
, &shallows
);
889 else if (deepen_rev_list
) {
890 struct argv_array av
= ARGV_ARRAY_INIT
;
893 argv_array_push(&av
, "rev-list");
895 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
897 argv_array_push(&av
, "--not");
898 for (i
= 0; i
< deepen_not
.nr
; i
++) {
899 struct string_list_item
*s
= deepen_not
.items
+ i
;
900 argv_array_push(&av
, s
->string
);
902 argv_array_push(&av
, "--not");
904 for (i
= 0; i
< want_obj
.nr
; i
++) {
905 struct object
*o
= want_obj
.objects
[i
].item
;
906 argv_array_push(&av
, oid_to_hex(&o
->oid
));
908 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
909 argv_array_clear(&av
);
912 if (shallows
.nr
> 0) {
914 for (i
= 0; i
< shallows
.nr
; i
++)
915 register_shallow(the_repository
,
916 &shallows
.objects
[i
].item
->oid
);
919 shallow_nr
+= shallows
.nr
;
920 object_array_clear(&shallows
);
923 /* return non-zero if the ref is hidden, otherwise 0 */
924 static int mark_our_ref(const char *refname
, const char *refname_full
,
925 const struct object_id
*oid
)
927 struct object
*o
= lookup_unknown_object(oid
->hash
);
929 if (ref_is_hidden(refname
, refname_full
)) {
930 o
->flags
|= HIDDEN_REF
;
937 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
938 int flag
, void *cb_data
)
940 const char *refname
= strip_namespace(refname_full
);
942 mark_our_ref(refname
, refname_full
, oid
);
946 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
948 struct string_list_item
*item
;
952 for_each_string_list_item(item
, symref
)
953 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
956 static int send_ref(const char *refname
, const struct object_id
*oid
,
957 int flag
, void *cb_data
)
959 static const char *capabilities
= "multi_ack thin-pack side-band"
960 " side-band-64k ofs-delta shallow deepen-since deepen-not"
961 " deepen-relative no-progress include-tag multi_ack_detailed";
962 const char *refname_nons
= strip_namespace(refname
);
963 struct object_id peeled
;
965 if (mark_our_ref(refname_nons
, refname
, oid
))
969 struct strbuf symref_info
= STRBUF_INIT
;
971 format_symref_info(&symref_info
, cb_data
);
972 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
973 oid_to_hex(oid
), refname_nons
,
975 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
976 " allow-tip-sha1-in-want" : "",
977 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
978 " allow-reachable-sha1-in-want" : "",
979 stateless_rpc
? " no-done" : "",
981 allow_filter
? " filter" : "",
982 git_user_agent_sanitized());
983 strbuf_release(&symref_info
);
985 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
988 if (!peel_ref(refname
, &peeled
))
989 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
993 static int find_symref(const char *refname
, const struct object_id
*oid
,
994 int flag
, void *cb_data
)
996 const char *symref_target
;
997 struct string_list_item
*item
;
999 if ((flag
& REF_ISSYMREF
) == 0)
1001 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1002 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1003 die("'%s' is a symref but it is not?", refname
);
1004 item
= string_list_append(cb_data
, refname
);
1005 item
->util
= xstrdup(symref_target
);
1009 static void upload_pack(void)
1011 struct string_list symref
= STRING_LIST_INIT_DUP
;
1013 head_ref_namespaced(find_symref
, &symref
);
1015 if (advertise_refs
|| !stateless_rpc
) {
1017 head_ref_namespaced(send_ref
, &symref
);
1018 for_each_namespaced_ref(send_ref
, &symref
);
1019 advertise_shallow_grafts(1);
1022 head_ref_namespaced(check_ref
, NULL
);
1023 for_each_namespaced_ref(check_ref
, NULL
);
1025 string_list_clear(&symref
, 1);
1031 get_common_commits();
1036 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1038 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1039 if (git_config_bool(var
, value
))
1040 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1042 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1043 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1044 if (git_config_bool(var
, value
))
1045 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1047 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1048 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1049 if (git_config_bool(var
, value
))
1050 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1052 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1053 } else if (!strcmp("uploadpack.keepalive", var
)) {
1054 keepalive
= git_config_int(var
, value
);
1057 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1058 if (!strcmp("uploadpack.packobjectshook", var
))
1059 return git_config_string(&pack_objects_hook
, var
, value
);
1060 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1061 allow_filter
= git_config_bool(var
, value
);
1063 return parse_hide_refs_config(var
, value
, "uploadpack");
1066 int cmd_main(int argc
, const char **argv
)
1070 struct option options
[] = {
1071 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
1072 N_("quit after a single request/response exchange")),
1073 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
1074 N_("exit immediately after initial ref advertisement")),
1075 OPT_BOOL(0, "strict", &strict
,
1076 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1077 OPT_INTEGER(0, "timeout", &timeout
,
1078 N_("interrupt transfer after <n> seconds of inactivity")),
1082 packet_trace_identity("upload-pack");
1083 check_replace_refs
= 0;
1085 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
1088 usage_with_options(upload_pack_usage
, options
);
1097 if (!enter_repo(dir
, strict
))
1098 die("'%s' does not appear to be a git repository", dir
);
1100 git_config(upload_pack_config
, NULL
);
1102 switch (determine_protocol_version_server()) {
1105 * v1 is just the original protocol with a version string,
1106 * so just fall through after writing the version string.
1108 if (advertise_refs
|| !stateless_rpc
)
1109 packet_write_fmt(1, "version 1\n");
1115 case protocol_unknown_version
:
1116 BUG("unknown protocol version");