ls-refs: report unborn targets of symrefs

When cloning, we choose the default branch based on the remote HEAD. But if there is no remote HEAD reported (which could happen if the target of the remote HEAD is unborn), we'll fall back to using our local init.defaultBranch. Traditionally this hasn't been a big deal, because most repos used "master" as the default. But these days it is likely to cause confusion if the server and client implementations choose different values (e.g., if the remote started with "main", we may choose "master" locally, create commits there, and then the user is surprised when they push to "master" and not "main"). To solve this, the remote needs to communicate the target of the HEAD symref, even if it is unborn, and "git clone" needs to use this information. Currently, symrefs that have unborn targets (such as in this case) are not communicated by the protocol. Teach Git to advertise and support the "unborn" feature in "ls-refs" (by default, this is advertised, but server administrators may turn this off through the lsrefs.unborn config). This feature indicates that "ls-refs" supports the "unborn" argument; when it is specified, "ls-refs" will send the HEAD symref with the name of its unborn target. This change is only for protocol v2. A similar change for protocol v0 would require independent protocol design (there being no analogous position to signal support for "unborn") and client-side plumbing of the data required, so the scope of this patch set is limited to protocol v2. The client side will be updated to use this in a subsequent commit. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Feb 5, 2021 at 12:48 UTC 59e1205d167c9acc17114a2f96425325470b1db8
7 files changed +95 -6
Documentation/config.txt
+2
@@ -398,6 +398,8 @@ include::config/interactive.txt[]
398
399 include::config/log.txt[]
400
401 +include::config/lsrefs.txt[]
402 +
403 include::config/mailinfo.txt[]
404
405 include::config/mailmap.txt[]
Documentation/config/lsrefs.txt new
+9
@@ -0,0 +1,9 @@
1 +lsrefs.unborn::
2 + May be "advertise" (the default), "allow", or "ignore". If "advertise",
3 + the server will respond to the client sending "unborn" (as described in
4 + protocol-v2.txt) and will advertise support for this feature during the
5 + protocol v2 capability advertisement. "allow" is the same as
6 + "advertise" except that the server will not advertise support for this
7 + feature; this is useful for load-balanced servers that cannot be
8 + updated atomically (for example), since the administrator could
9 + configure "allow", then after a delay, configure "advertise".
Documentation/technical/protocol-v2.txt
+10 -1
@@ -192,11 +192,20 @@ ls-refs takes in the following arguments:
192 When specified, only references having a prefix matching one of
193 the provided prefixes are displayed.
194
195 +If the 'unborn' feature is advertised the following argument can be
196 +included in the client's request.
197 +
198 + unborn
199 + The server will send information about HEAD even if it is a symref
200 + pointing to an unborn branch in the form "unborn HEAD
201 + symref-target:<target>".
202 +
203 The output of ls-refs is as follows:
204
205 output = *ref
206 flush-pkt
199 - ref = PKT-LINE(obj-id SP refname *(SP ref-attribute) LF)
207 + obj-id-or-unborn = (obj-id | "unborn")
208 + ref = PKT-LINE(obj-id-or-unborn SP refname *(SP ref-attribute) LF)
209 ref-attribute = (symref | peeled)
210 symref = "symref-target:" symref-target
211 peeled = "peeled:" obj-id
ls-refs.c
+71 -3
@@ -7,6 +7,39 @@
7 #include "pkt-line.h"
8 #include "config.h"
9
10 +static int config_read;
11 +static int advertise_unborn;
12 +static int allow_unborn;
13 +
14 +static void ensure_config_read(void)
15 +{
16 + const char *str = NULL;
17 +
18 + if (config_read)
19 + return;
20 +
21 + if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) {
22 + /*
23 + * If there is no such config, advertise and allow it by
24 + * default.
25 + */
26 + advertise_unborn = 1;
27 + allow_unborn = 1;
28 + } else {
29 + if (!strcmp(str, "advertise")) {
30 + advertise_unborn = 1;
31 + allow_unborn = 1;
32 + } else if (!strcmp(str, "allow")) {
33 + allow_unborn = 1;
34 + } else if (!strcmp(str, "ignore")) {
35 + /* do nothing */
36 + } else {
37 + die(_("invalid value '%s' for lsrefs.unborn"), str);
38 + }
39 + }
40 + config_read = 1;
41 +}
42 +
43 /*
44 * Check if one of the prefixes is a prefix of the ref.
45 * If no prefixes were provided, all refs match.
@@ -32,6 +65,7 @@ struct ls_refs_data {
65 unsigned peel;
66 unsigned symrefs;
67 struct strvec prefixes;
68 + unsigned unborn : 1;
69 };
70
71 static int send_ref(const char *refname, const struct object_id *oid,
@@ -47,7 +81,10 @@ static int send_ref(const char *refname, const struct object_id *oid,
81 if (!ref_match(&data->prefixes, refname_nons))
82 return 0;
83
50 - strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
84 + if (oid)
85 + strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
86 + else
87 + strbuf_addf(&refline, "unborn %s", refname_nons);
88 if (data->symrefs && flag & REF_ISSYMREF) {
89 struct object_id unused;
90 const char *symref_target = resolve_ref_unsafe(refname, 0,
@@ -61,7 +98,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
98 strip_namespace(symref_target));
99 }
100
64 - if (data->peel) {
101 + if (data->peel && oid) {
102 struct object_id peeled;
103 if (!peel_ref(refname, &peeled))
104 strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
@@ -74,6 +111,23 @@ static int send_ref(const char *refname, const struct object_id *oid,
111 return 0;
112 }
113
114 +static void send_possibly_unborn_head(struct ls_refs_data *data)
115 +{
116 + struct strbuf namespaced = STRBUF_INIT;
117 + struct object_id oid;
118 + int flag;
119 + int oid_is_null;
120 +
121 + strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
122 + if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
123 + return; /* bad ref */
124 + oid_is_null = is_null_oid(&oid);
125 + if (!oid_is_null ||
126 + (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
127 + send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
128 + strbuf_release(&namespaced);
129 +}
130 +
131 static int ls_refs_config(const char *var, const char *value, void *data)
132 {
133 /*
@@ -91,6 +145,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
145
146 memset(&data, 0, sizeof(data));
147
148 + ensure_config_read();
149 git_config(ls_refs_config, NULL);
150
151 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
@@ -103,14 +158,27 @@ int ls_refs(struct repository *r, struct strvec *keys,
158 data.symrefs = 1;
159 else if (skip_prefix(arg, "ref-prefix ", &out))
160 strvec_push(&data.prefixes, out);
161 + else if (!strcmp("unborn", arg))
162 + data.unborn = allow_unborn;
163 }
164
165 if (request->status != PACKET_READ_FLUSH)
166 die(_("expected flush after ls-refs arguments"));
167
111 - head_ref_namespaced(send_ref, &data);
168 + send_possibly_unborn_head(&data);
169 for_each_namespaced_ref(send_ref, &data);
170 packet_flush(1);
171 strvec_clear(&data.prefixes);
172 return 0;
173 }
174 +
175 +int ls_refs_advertise(struct repository *r, struct strbuf *value)
176 +{
177 + if (value) {
178 + ensure_config_read();
179 + if (advertise_unborn)
180 + strbuf_addstr(value, "unborn");
181 + }
182 +
183 + return 1;
184 +}
ls-refs.h
+1
@@ -6,5 +6,6 @@ struct strvec;
6 struct packet_reader;
7 int ls_refs(struct repository *r, struct strvec *keys,
8 struct packet_reader *request);
9 +int ls_refs_advertise(struct repository *r, struct strbuf *value);
10
11 #endif /* LS_REFS_H */
serve.c
+1 -1
@@ -73,7 +73,7 @@ struct protocol_capability {
73
74 static struct protocol_capability capabilities[] = {
75 { "agent", agent_advertise, NULL },
76 - { "ls-refs", always_advertise, ls_refs },
76 + { "ls-refs", ls_refs_advertise, ls_refs },
77 { "fetch", upload_pack_advertise, upload_pack_v2 },
78 { "server-option", always_advertise, NULL },
79 { "object-format", object_format_advertise, NULL },
t/t5701-git-serve.sh
+1 -1
@@ -12,7 +12,7 @@ test_expect_success 'test capability advertisement' '
12 cat >expect <<-EOF &&
13 version 2
14 agent=git/$(git version | cut -d" " -f3)
15 - ls-refs
15 + ls-refs=unborn
16 fetch=shallow
17 server-option
18 object-format=$(test_oid algo)