upload-pack: support hidden refs with protocol v2

In the v2 protocol, upload-pack's advertisement has been moved to the "ls-refs" command. That command does not respect hidden-ref config (like transfer.hiderefs) at all, and advertises everything. While there are some features that are not supported in v2 (e.g., v2 always allows fetching any sha1 without respect to advertisements), the lack of this feature is not documented and is likely just a bug. Let's make it work, as otherwise upgrading a server to a v2-capable git will start exposing these refs that the repository admin has asked to remain hidden. Note that we assume we're operating on behalf of a fetch here, since that's the only thing implemented in v2 at this point. See the in-code comment. We'll have to figure out how this works when the v2 push protocol is designed (both here in ls-refs, but also rejecting updates to hidden refs). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 18, 2018 at 07:47 UTC e20b4192a3783f5e2241cce5a439933941836a99
2 files changed +22
ls-refs.c
+16
@@ -5,6 +5,7 @@
5 #include "argv-array.h"
6 #include "ls-refs.h"
7 #include "pkt-line.h"
8 +#include "config.h"
9
10 /*
11 * Check if one of the prefixes is a prefix of the ref.
@@ -40,6 +41,9 @@ static int send_ref(const char *refname, const struct object_id *oid,
41 const char *refname_nons = strip_namespace(refname);
42 struct strbuf refline = STRBUF_INIT;
43
44 + if (ref_is_hidden(refname_nons, refname))
45 + return 0;
46 +
47 if (!ref_match(&data->prefixes, refname))
48 return 0;
49
@@ -69,6 +73,16 @@ static int send_ref(const char *refname, const struct object_id *oid,
73 return 0;
74 }
75
76 +static int ls_refs_config(const char *var, const char *value, void *data)
77 +{
78 + /*
79 + * We only serve fetches over v2 for now, so respect only "uploadpack"
80 + * config. This may need to eventually be expanded to "receive", but we
81 + * don't yet know how that information will be passed to ls-refs.
82 + */
83 + return parse_hide_refs_config(var, value, "uploadpack");
84 +}
85 +
86 int ls_refs(struct repository *r, struct argv_array *keys,
87 struct packet_reader *request)
88 {
@@ -76,6 +90,8 @@ int ls_refs(struct repository *r, struct argv_array *keys,
90
91 memset(&data, 0, sizeof(data));
92
93 + git_config(ls_refs_config, NULL);
94 +
95 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
96 const char *arg = request->line;
97 const char *out;
t/t5512-ls-remote.sh
+6
@@ -204,6 +204,12 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
204 grep refs/tags/magic actual
205 '
206
207 +test_expect_success 'protocol v2 supports hiderefs' '
208 + test_config uploadpack.hiderefs refs/tags &&
209 + git -c protocol.version=2 ls-remote . >actual &&
210 + ! grep refs/tags actual
211 +'
212 +
213 test_expect_success 'ls-remote --symref' '
214 git fetch origin &&
215 cat >expect <<-EOF &&