12 #include "list-objects.h"
13 #include "list-objects-filter.h"
14 #include "list-objects-filter-options.h"
15 #include "run-command.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "argv-array.h"
22 #include "prio-queue.h"
26 static const char * const upload_pack_usage
[] = {
27 N_("git upload-pack [<options>] <dir>"),
31 /* Remember to update object flag allocation in object.h */
32 #define THEY_HAVE (1u << 11)
33 #define OUR_REF (1u << 12)
34 #define WANTED (1u << 13)
35 #define COMMON_KNOWN (1u << 14)
36 #define REACHABLE (1u << 15)
38 #define SHALLOW (1u << 16)
39 #define NOT_SHALLOW (1u << 17)
40 #define CLIENT_SHALLOW (1u << 18)
41 #define HIDDEN_REF (1u << 19)
43 static timestamp_t oldest_have
;
45 static int deepen_relative
;
48 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
49 static int no_progress
, daemon_mode
;
50 /* Allow specifying sha1 if it is a ref tip. */
51 #define ALLOW_TIP_SHA1 01
52 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
53 #define ALLOW_REACHABLE_SHA1 02
54 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
55 #define ALLOW_ANY_SHA1 07
56 static unsigned int allow_unadvertised_object_request
;
57 static int shallow_nr
;
58 static struct object_array have_obj
;
59 static struct object_array want_obj
;
60 static struct object_array extra_edge_obj
;
61 static unsigned int timeout
;
62 static int keepalive
= 5;
64 * otherwise maximum packet size (up to 65520 bytes).
66 static int use_sideband
;
67 static int advertise_refs
;
68 static int stateless_rpc
;
69 static const char *pack_objects_hook
;
71 static int filter_capability_requested
;
72 static int allow_filter
;
73 static struct list_objects_filter_options filter_options
;
75 static void reset_timeout(void)
80 static void send_client_data(int fd
, const char *data
, ssize_t sz
)
83 send_sideband(1, fd
, data
, sz
, use_sideband
);
90 /* XXX: are we happy to lose stuff here? */
94 write_or_die(fd
, data
, sz
);
97 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
100 if (graft
->nr_parent
== -1)
101 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
105 static void create_pack_file(void)
107 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
108 char data
[8193], progress
[128];
109 char abort_msg
[] = "aborting due to possible repository "
110 "corruption on the remote side.";
116 if (!pack_objects_hook
)
117 pack_objects
.git_cmd
= 1;
119 argv_array_push(&pack_objects
.args
, pack_objects_hook
);
120 argv_array_push(&pack_objects
.args
, "git");
121 pack_objects
.use_shell
= 1;
125 argv_array_push(&pack_objects
.args
, "--shallow-file");
126 argv_array_push(&pack_objects
.args
, "");
128 argv_array_push(&pack_objects
.args
, "pack-objects");
129 argv_array_push(&pack_objects
.args
, "--revs");
131 argv_array_push(&pack_objects
.args
, "--thin");
133 argv_array_push(&pack_objects
.args
, "--stdout");
135 argv_array_push(&pack_objects
.args
, "--shallow");
137 argv_array_push(&pack_objects
.args
, "--progress");
139 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
141 argv_array_push(&pack_objects
.args
, "--include-tag");
142 if (filter_options
.filter_spec
) {
143 if (pack_objects
.use_shell
) {
144 struct strbuf buf
= STRBUF_INIT
;
145 sq_quote_buf(&buf
, filter_options
.filter_spec
);
146 argv_array_pushf(&pack_objects
.args
, "--filter=%s", buf
.buf
);
147 strbuf_release(&buf
);
149 argv_array_pushf(&pack_objects
.args
, "--filter=%s",
150 filter_options
.filter_spec
);
154 pack_objects
.in
= -1;
155 pack_objects
.out
= -1;
156 pack_objects
.err
= -1;
158 if (start_command(&pack_objects
))
159 die("git upload-pack: unable to fork git-pack-objects");
161 pipe_fd
= xfdopen(pack_objects
.in
, "w");
164 for_each_commit_graft(write_one_shallow
, pipe_fd
);
166 for (i
= 0; i
< want_obj
.nr
; i
++)
167 fprintf(pipe_fd
, "%s\n",
168 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
169 fprintf(pipe_fd
, "--not\n");
170 for (i
= 0; i
< have_obj
.nr
; i
++)
171 fprintf(pipe_fd
, "%s\n",
172 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
173 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
174 fprintf(pipe_fd
, "%s\n",
175 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
176 fprintf(pipe_fd
, "\n");
180 /* We read from pack_objects.err to capture stderr output for
181 * progress bar, and pack_objects.out to capture the pack data.
185 struct pollfd pfd
[2];
186 int pe
, pu
, pollsize
;
194 if (0 <= pack_objects
.out
) {
195 pfd
[pollsize
].fd
= pack_objects
.out
;
196 pfd
[pollsize
].events
= POLLIN
;
200 if (0 <= pack_objects
.err
) {
201 pfd
[pollsize
].fd
= pack_objects
.err
;
202 pfd
[pollsize
].events
= POLLIN
;
210 ret
= poll(pfd
, pollsize
,
211 keepalive
< 0 ? -1 : 1000 * keepalive
);
214 if (errno
!= EINTR
) {
215 error_errno("poll failed, resuming");
220 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
221 /* Status ready; we ship that in the side-band
222 * or dump to the standard error.
224 sz
= xread(pack_objects
.err
, progress
,
227 send_client_data(2, progress
, sz
);
229 close(pack_objects
.err
);
230 pack_objects
.err
= -1;
234 /* give priority to status messages */
237 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
238 /* Data ready; we keep the last byte to ourselves
239 * in case we detect broken rev-list, so that we
240 * can leave the stream corrupted. This is
241 * unfortunate -- unpack-objects would happily
242 * accept a valid packdata with trailing garbage,
243 * so appending garbage after we pass all the
244 * pack data is not good enough to signal
245 * breakage to downstream.
253 sz
= xread(pack_objects
.out
, cp
,
254 sizeof(data
) - outsz
);
258 close(pack_objects
.out
);
259 pack_objects
.out
= -1;
265 buffered
= data
[sz
-1] & 0xFF;
270 send_client_data(1, data
, sz
);
274 * We hit the keepalive timeout without saying anything; send
275 * an empty message on the data sideband just to let the other
276 * side know we're still working on it, but don't have any data
279 * If we don't have a sideband channel, there's no room in the
280 * protocol to say anything, so those clients are just out of
283 if (!ret
&& use_sideband
) {
284 static const char buf
[] = "0005\1";
285 write_or_die(1, buf
, 5);
289 if (finish_command(&pack_objects
)) {
290 error("git upload-pack: git-pack-objects died with error.");
297 send_client_data(1, data
, 1);
298 fprintf(stderr
, "flushed.\n");
305 send_client_data(3, abort_msg
, sizeof(abort_msg
));
306 die("git upload-pack: %s", abort_msg
);
309 static int got_oid(const char *hex
, struct object_id
*oid
)
312 int we_knew_they_have
= 0;
314 if (get_oid_hex(hex
, oid
))
315 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
316 if (!has_object_file(oid
))
319 o
= parse_object(oid
);
321 die("oops (%s)", oid_to_hex(oid
));
322 if (o
->type
== OBJ_COMMIT
) {
323 struct commit_list
*parents
;
324 struct commit
*commit
= (struct commit
*)o
;
325 if (o
->flags
& THEY_HAVE
)
326 we_knew_they_have
= 1;
328 o
->flags
|= THEY_HAVE
;
329 if (!oldest_have
|| (commit
->date
< oldest_have
))
330 oldest_have
= commit
->date
;
331 for (parents
= commit
->parents
;
333 parents
= parents
->next
)
334 parents
->item
->object
.flags
|= THEY_HAVE
;
336 if (!we_knew_they_have
) {
337 add_object_array(o
, NULL
, &have_obj
);
343 static int reachable(struct commit
*want
)
345 struct prio_queue work
= { compare_commits_by_commit_date
};
347 prio_queue_put(&work
, want
);
349 struct commit_list
*list
;
350 struct commit
*commit
= prio_queue_get(&work
);
352 if (commit
->object
.flags
& THEY_HAVE
) {
353 want
->object
.flags
|= COMMON_KNOWN
;
356 if (!commit
->object
.parsed
)
357 parse_object(&commit
->object
.oid
);
358 if (commit
->object
.flags
& REACHABLE
)
360 commit
->object
.flags
|= REACHABLE
;
361 if (commit
->date
< oldest_have
)
363 for (list
= commit
->parents
; list
; list
= list
->next
) {
364 struct commit
*parent
= list
->item
;
365 if (!(parent
->object
.flags
& REACHABLE
))
366 prio_queue_put(&work
, parent
);
369 want
->object
.flags
|= REACHABLE
;
370 clear_commit_marks(want
, REACHABLE
);
371 clear_prio_queue(&work
);
372 return (want
->object
.flags
& COMMON_KNOWN
);
375 static int ok_to_give_up(void)
382 for (i
= 0; i
< want_obj
.nr
; i
++) {
383 struct object
*want
= want_obj
.objects
[i
].item
;
385 if (want
->flags
& COMMON_KNOWN
)
387 want
= deref_tag(want
, "a want line", 0);
388 if (!want
|| want
->type
!= OBJ_COMMIT
) {
389 /* no way to tell if this is reachable by
390 * looking at the ancestry chain alone, so
391 * leave a note to ourselves not to worry about
392 * this object anymore.
394 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
397 if (!reachable((struct commit
*)want
))
403 static int get_common_commits(void)
405 struct object_id oid
;
406 char last_hex
[GIT_MAX_HEXSZ
+ 1];
411 save_commit_buffer
= 0;
414 char *line
= packet_read_line(0, NULL
);
420 if (multi_ack
== 2 && got_common
421 && !got_other
&& ok_to_give_up()) {
423 packet_write_fmt(1, "ACK %s ready\n", last_hex
);
425 if (have_obj
.nr
== 0 || multi_ack
)
426 packet_write_fmt(1, "NAK\n");
428 if (no_done
&& sent_ready
) {
429 packet_write_fmt(1, "ACK %s\n", last_hex
);
438 if (skip_prefix(line
, "have ", &arg
)) {
439 switch (got_oid(arg
, &oid
)) {
440 case -1: /* they have what we do not */
442 if (multi_ack
&& ok_to_give_up()) {
443 const char *hex
= oid_to_hex(&oid
);
444 if (multi_ack
== 2) {
446 packet_write_fmt(1, "ACK %s ready\n", hex
);
448 packet_write_fmt(1, "ACK %s continue\n", hex
);
453 memcpy(last_hex
, oid_to_hex(&oid
), 41);
455 packet_write_fmt(1, "ACK %s common\n", last_hex
);
457 packet_write_fmt(1, "ACK %s continue\n", last_hex
);
458 else if (have_obj
.nr
== 1)
459 packet_write_fmt(1, "ACK %s\n", last_hex
);
464 if (!strcmp(line
, "done")) {
465 if (have_obj
.nr
> 0) {
467 packet_write_fmt(1, "ACK %s\n", last_hex
);
470 packet_write_fmt(1, "NAK\n");
473 die("git upload-pack: expected SHA1 list, got '%s'", line
);
477 static int is_our_ref(struct object
*o
)
479 int allow_hidden_ref
= (allow_unadvertised_object_request
&
480 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
481 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
485 * on successful case, it's up to the caller to close cmd->out
487 static int do_reachable_revlist(struct child_process
*cmd
,
488 struct object_array
*src
,
489 struct object_array
*reachable
)
491 static const char *argv
[] = {
492 "rev-list", "--stdin", NULL
,
495 char namebuf
[42]; /* ^ + SHA-1 + LF */
505 * If the next rev-list --stdin encounters an unknown commit,
506 * it terminates, which will cause SIGPIPE in the write loop
509 sigchain_push(SIGPIPE
, SIG_IGN
);
511 if (start_command(cmd
))
515 namebuf
[GIT_SHA1_HEXSZ
+ 1] = '\n';
516 for (i
= get_max_object_index(); 0 < i
; ) {
517 o
= get_indexed_object(--i
);
520 if (reachable
&& o
->type
== OBJ_COMMIT
)
521 o
->flags
&= ~TMP_MARK
;
524 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
525 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 2) < 0)
528 namebuf
[GIT_SHA1_HEXSZ
] = '\n';
529 for (i
= 0; i
< src
->nr
; i
++) {
530 o
= src
->objects
[i
].item
;
533 add_object_array(o
, NULL
, reachable
);
536 if (reachable
&& o
->type
== OBJ_COMMIT
)
537 o
->flags
|= TMP_MARK
;
538 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
539 if (write_in_full(cmd
->in
, namebuf
, GIT_SHA1_HEXSZ
+ 1) < 0)
544 sigchain_pop(SIGPIPE
);
549 sigchain_pop(SIGPIPE
);
558 static int get_reachable_list(struct object_array
*src
,
559 struct object_array
*reachable
)
561 struct child_process cmd
= CHILD_PROCESS_INIT
;
564 char namebuf
[42]; /* ^ + SHA-1 + LF */
566 if (do_reachable_revlist(&cmd
, src
, reachable
) < 0)
569 while ((i
= read_in_full(cmd
.out
, namebuf
, 41)) == 41) {
570 struct object_id sha1
;
572 if (namebuf
[40] != '\n' || get_oid_hex(namebuf
, &sha1
))
575 o
= lookup_object(sha1
.hash
);
576 if (o
&& o
->type
== OBJ_COMMIT
) {
577 o
->flags
&= ~TMP_MARK
;
580 for (i
= get_max_object_index(); 0 < i
; i
--) {
581 o
= get_indexed_object(i
- 1);
582 if (o
&& o
->type
== OBJ_COMMIT
&&
583 (o
->flags
& TMP_MARK
)) {
584 add_object_array(o
, NULL
, reachable
);
585 o
->flags
&= ~TMP_MARK
;
590 if (finish_command(&cmd
))
596 static int has_unreachable(struct object_array
*src
)
598 struct child_process cmd
= CHILD_PROCESS_INIT
;
602 if (do_reachable_revlist(&cmd
, src
, NULL
) < 0)
606 * The commits out of the rev-list are not ancestors of
609 i
= read_in_full(cmd
.out
, buf
, 1);
616 * rev-list may have died by encountering a bad commit
617 * in the history, in which case we do want to bail out
618 * even when it showed no commit.
620 if (finish_command(&cmd
))
623 /* All the non-tip ones are ancestors of what we advertised */
627 sigchain_pop(SIGPIPE
);
633 static void check_non_tip(void)
638 * In the normal in-process case without
639 * uploadpack.allowReachableSHA1InWant,
640 * non-tip requests can never happen.
642 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
644 if (!has_unreachable(&want_obj
))
645 /* All the non-tip ones are ancestors of what we advertised */
649 /* Pick one of them (we know there at least is one) */
650 for (i
= 0; i
< want_obj
.nr
; i
++) {
651 struct object
*o
= want_obj
.objects
[i
].item
;
653 die("git upload-pack: not our ref %s",
654 oid_to_hex(&o
->oid
));
658 static void send_shallow(struct commit_list
*result
)
661 struct object
*object
= &result
->item
->object
;
662 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
663 packet_write_fmt(1, "shallow %s",
664 oid_to_hex(&object
->oid
));
665 register_shallow(&object
->oid
);
668 result
= result
->next
;
672 static void send_unshallow(const struct object_array
*shallows
)
676 for (i
= 0; i
< shallows
->nr
; i
++) {
677 struct object
*object
= shallows
->objects
[i
].item
;
678 if (object
->flags
& NOT_SHALLOW
) {
679 struct commit_list
*parents
;
680 packet_write_fmt(1, "unshallow %s",
681 oid_to_hex(&object
->oid
));
682 object
->flags
&= ~CLIENT_SHALLOW
;
684 * We want to _register_ "object" as shallow, but we
685 * also need to traverse object's parents to deepen a
686 * shallow clone. Unregister it for now so we can
687 * parse and add the parents to the want list, then
690 unregister_shallow(&object
->oid
);
692 parse_commit_or_die((struct commit
*)object
);
693 parents
= ((struct commit
*)object
)->parents
;
695 add_object_array(&parents
->item
->object
,
697 parents
= parents
->next
;
699 add_object_array(object
, NULL
, &extra_edge_obj
);
701 /* make sure commit traversal conforms to client */
702 register_shallow(&object
->oid
);
706 static void deepen(int depth
, int deepen_relative
,
707 struct object_array
*shallows
)
709 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow()) {
712 for (i
= 0; i
< shallows
->nr
; i
++) {
713 struct object
*object
= shallows
->objects
[i
].item
;
714 object
->flags
|= NOT_SHALLOW
;
716 } else if (deepen_relative
) {
717 struct object_array reachable_shallows
= OBJECT_ARRAY_INIT
;
718 struct commit_list
*result
;
720 get_reachable_list(shallows
, &reachable_shallows
);
721 result
= get_shallow_commits(&reachable_shallows
,
723 SHALLOW
, NOT_SHALLOW
);
724 send_shallow(result
);
725 free_commit_list(result
);
726 object_array_clear(&reachable_shallows
);
728 struct commit_list
*result
;
730 result
= get_shallow_commits(&want_obj
, depth
,
731 SHALLOW
, NOT_SHALLOW
);
732 send_shallow(result
);
733 free_commit_list(result
);
736 send_unshallow(shallows
);
740 static void deepen_by_rev_list(int ac
, const char **av
,
741 struct object_array
*shallows
)
743 struct commit_list
*result
;
745 result
= get_shallow_commits_by_rev_list(ac
, av
, SHALLOW
, NOT_SHALLOW
);
746 send_shallow(result
);
747 free_commit_list(result
);
748 send_unshallow(shallows
);
752 static void receive_needs(void)
754 struct object_array shallows
= OBJECT_ARRAY_INIT
;
755 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
758 timestamp_t deepen_since
= 0;
759 int deepen_rev_list
= 0;
764 const char *features
;
765 struct object_id oid_buf
;
766 char *line
= packet_read_line(0, NULL
);
773 if (skip_prefix(line
, "shallow ", &arg
)) {
774 struct object_id oid
;
775 struct object
*object
;
776 if (get_oid_hex(arg
, &oid
))
777 die("invalid shallow line: %s", line
);
778 object
= parse_object(&oid
);
781 if (object
->type
!= OBJ_COMMIT
)
782 die("invalid shallow object %s", oid_to_hex(&oid
));
783 if (!(object
->flags
& CLIENT_SHALLOW
)) {
784 object
->flags
|= CLIENT_SHALLOW
;
785 add_object_array(object
, NULL
, &shallows
);
789 if (skip_prefix(line
, "deepen ", &arg
)) {
791 depth
= strtol(arg
, &end
, 0);
792 if (!end
|| *end
|| depth
<= 0)
793 die("Invalid deepen: %s", line
);
796 if (skip_prefix(line
, "deepen-since ", &arg
)) {
798 deepen_since
= parse_timestamp(arg
, &end
, 0);
799 if (!end
|| *end
|| !deepen_since
||
800 /* revisions.c's max_age -1 is special */
802 die("Invalid deepen-since: %s", line
);
806 if (skip_prefix(line
, "deepen-not ", &arg
)) {
808 struct object_id oid
;
809 if (expand_ref(arg
, strlen(arg
), &oid
, &ref
) != 1)
810 die("git upload-pack: ambiguous deepen-not: %s", line
);
811 string_list_append(&deepen_not
, ref
);
816 if (skip_prefix(line
, "filter ", &arg
)) {
817 if (!filter_capability_requested
)
818 die("git upload-pack: filtering capability not negotiated");
819 parse_list_objects_filter(&filter_options
, arg
);
822 if (!skip_prefix(line
, "want ", &arg
) ||
823 get_oid_hex(arg
, &oid_buf
))
824 die("git upload-pack: protocol error, "
825 "expected to get sha, not '%s'", line
);
829 if (parse_feature_request(features
, "deepen-relative"))
831 if (parse_feature_request(features
, "multi_ack_detailed"))
833 else if (parse_feature_request(features
, "multi_ack"))
835 if (parse_feature_request(features
, "no-done"))
837 if (parse_feature_request(features
, "thin-pack"))
839 if (parse_feature_request(features
, "ofs-delta"))
841 if (parse_feature_request(features
, "side-band-64k"))
842 use_sideband
= LARGE_PACKET_MAX
;
843 else if (parse_feature_request(features
, "side-band"))
844 use_sideband
= DEFAULT_PACKET_MAX
;
845 if (parse_feature_request(features
, "no-progress"))
847 if (parse_feature_request(features
, "include-tag"))
849 if (allow_filter
&& parse_feature_request(features
, "filter"))
850 filter_capability_requested
= 1;
852 o
= parse_object(&oid_buf
);
855 "ERR upload-pack: not our ref %s",
856 oid_to_hex(&oid_buf
));
857 die("git upload-pack: not our ref %s",
858 oid_to_hex(&oid_buf
));
860 if (!(o
->flags
& WANTED
)) {
862 if (!((allow_unadvertised_object_request
& ALLOW_ANY_SHA1
) == ALLOW_ANY_SHA1
865 add_object_array(o
, NULL
, &want_obj
);
870 * We have sent all our refs already, and the other end
871 * should have chosen out of them. When we are operating
872 * in the stateless RPC mode, however, their choice may
873 * have been based on the set of older refs advertised
874 * by another process that handled the initial request.
879 if (!use_sideband
&& daemon_mode
)
882 if (depth
== 0 && !deepen_rev_list
&& shallows
.nr
== 0)
884 if (depth
> 0 && deepen_rev_list
)
885 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
887 deepen(depth
, deepen_relative
, &shallows
);
888 else if (deepen_rev_list
) {
889 struct argv_array av
= ARGV_ARRAY_INIT
;
892 argv_array_push(&av
, "rev-list");
894 argv_array_pushf(&av
, "--max-age=%"PRItime
, deepen_since
);
896 argv_array_push(&av
, "--not");
897 for (i
= 0; i
< deepen_not
.nr
; i
++) {
898 struct string_list_item
*s
= deepen_not
.items
+ i
;
899 argv_array_push(&av
, s
->string
);
901 argv_array_push(&av
, "--not");
903 for (i
= 0; i
< want_obj
.nr
; i
++) {
904 struct object
*o
= want_obj
.objects
[i
].item
;
905 argv_array_push(&av
, oid_to_hex(&o
->oid
));
907 deepen_by_rev_list(av
.argc
, av
.argv
, &shallows
);
908 argv_array_clear(&av
);
911 if (shallows
.nr
> 0) {
913 for (i
= 0; i
< shallows
.nr
; i
++)
914 register_shallow(&shallows
.objects
[i
].item
->oid
);
917 shallow_nr
+= shallows
.nr
;
918 object_array_clear(&shallows
);
921 /* return non-zero if the ref is hidden, otherwise 0 */
922 static int mark_our_ref(const char *refname
, const char *refname_full
,
923 const struct object_id
*oid
)
925 struct object
*o
= lookup_unknown_object(oid
->hash
);
927 if (ref_is_hidden(refname
, refname_full
)) {
928 o
->flags
|= HIDDEN_REF
;
935 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
936 int flag
, void *cb_data
)
938 const char *refname
= strip_namespace(refname_full
);
940 mark_our_ref(refname
, refname_full
, oid
);
944 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
946 struct string_list_item
*item
;
950 for_each_string_list_item(item
, symref
)
951 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
954 static int send_ref(const char *refname
, const struct object_id
*oid
,
955 int flag
, void *cb_data
)
957 static const char *capabilities
= "multi_ack thin-pack side-band"
958 " side-band-64k ofs-delta shallow deepen-since deepen-not"
959 " deepen-relative no-progress include-tag multi_ack_detailed";
960 const char *refname_nons
= strip_namespace(refname
);
961 struct object_id peeled
;
963 if (mark_our_ref(refname_nons
, refname
, oid
))
967 struct strbuf symref_info
= STRBUF_INIT
;
969 format_symref_info(&symref_info
, cb_data
);
970 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
971 oid_to_hex(oid
), refname_nons
,
973 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
974 " allow-tip-sha1-in-want" : "",
975 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
976 " allow-reachable-sha1-in-want" : "",
977 stateless_rpc
? " no-done" : "",
979 allow_filter
? " filter" : "",
980 git_user_agent_sanitized());
981 strbuf_release(&symref_info
);
983 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
986 if (!peel_ref(refname
, &peeled
))
987 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
991 static int find_symref(const char *refname
, const struct object_id
*oid
,
992 int flag
, void *cb_data
)
994 const char *symref_target
;
995 struct string_list_item
*item
;
997 if ((flag
& REF_ISSYMREF
) == 0)
999 symref_target
= resolve_ref_unsafe(refname
, 0, NULL
, &flag
);
1000 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
1001 die("'%s' is a symref but it is not?", refname
);
1002 item
= string_list_append(cb_data
, refname
);
1003 item
->util
= xstrdup(symref_target
);
1007 static void upload_pack(void)
1009 struct string_list symref
= STRING_LIST_INIT_DUP
;
1011 head_ref_namespaced(find_symref
, &symref
);
1013 if (advertise_refs
|| !stateless_rpc
) {
1015 head_ref_namespaced(send_ref
, &symref
);
1016 for_each_namespaced_ref(send_ref
, &symref
);
1017 advertise_shallow_grafts(1);
1020 head_ref_namespaced(check_ref
, NULL
);
1021 for_each_namespaced_ref(check_ref
, NULL
);
1023 string_list_clear(&symref
, 1);
1029 get_common_commits();
1034 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
1036 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
1037 if (git_config_bool(var
, value
))
1038 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
1040 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
1041 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
1042 if (git_config_bool(var
, value
))
1043 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
1045 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
1046 } else if (!strcmp("uploadpack.allowanysha1inwant", var
)) {
1047 if (git_config_bool(var
, value
))
1048 allow_unadvertised_object_request
|= ALLOW_ANY_SHA1
;
1050 allow_unadvertised_object_request
&= ~ALLOW_ANY_SHA1
;
1051 } else if (!strcmp("uploadpack.keepalive", var
)) {
1052 keepalive
= git_config_int(var
, value
);
1055 } else if (current_config_scope() != CONFIG_SCOPE_REPO
) {
1056 if (!strcmp("uploadpack.packobjectshook", var
))
1057 return git_config_string(&pack_objects_hook
, var
, value
);
1058 } else if (!strcmp("uploadpack.allowfilter", var
)) {
1059 allow_filter
= git_config_bool(var
, value
);
1061 return parse_hide_refs_config(var
, value
, "uploadpack");
1064 int cmd_main(int argc
, const char **argv
)
1068 struct option options
[] = {
1069 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
1070 N_("quit after a single request/response exchange")),
1071 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
1072 N_("exit immediately after initial ref advertisement")),
1073 OPT_BOOL(0, "strict", &strict
,
1074 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1075 OPT_INTEGER(0, "timeout", &timeout
,
1076 N_("interrupt transfer after <n> seconds of inactivity")),
1080 packet_trace_identity("upload-pack");
1081 check_replace_refs
= 0;
1083 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
1086 usage_with_options(upload_pack_usage
, options
);
1095 if (!enter_repo(dir
, strict
))
1096 die("'%s' does not appear to be a git repository", dir
);
1098 git_config(upload_pack_config
, NULL
);
1100 switch (determine_protocol_version_server()) {
1103 * v1 is just the original protocol with a version string,
1104 * so just fall through after writing the version string.
1106 if (advertise_refs
|| !stateless_rpc
)
1107 packet_write_fmt(1, "version 1\n");
1113 case protocol_unknown_version
:
1114 BUG("unknown protocol version");