Skip to content

Commit 59e1205

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba2aa15 commit 59e1205

File tree

7 files changed

+95
-6
lines changed

7 files changed

+95
-6
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ include::config/interactive.txt[]
398398

399399
include::config/log.txt[]
400400

401+
include::config/lsrefs.txt[]
402+
401403
include::config/mailinfo.txt[]
402404

403405
include::config/mailmap.txt[]

Documentation/config/lsrefs.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,20 @@ ls-refs takes in the following arguments:
192192
When specified, only references having a prefix matching one of
193193
the provided prefixes are displayed.
194194

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+
195203
The output of ls-refs is as follows:
196204

197205
output = *ref
198206
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)
200209
ref-attribute = (symref | peeled)
201210
symref = "symref-target:" symref-target
202211
peeled = "peeled:" obj-id

ls-refs.c

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@
77
#include "pkt-line.h"
88
#include "config.h"
99

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+
1043
/*
1144
* Check if one of the prefixes is a prefix of the ref.
1245
* If no prefixes were provided, all refs match.
@@ -32,6 +65,7 @@ struct ls_refs_data {
3265
unsigned peel;
3366
unsigned symrefs;
3467
struct strvec prefixes;
68+
unsigned unborn : 1;
3569
};
3670

3771
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,
4781
if (!ref_match(&data->prefixes, refname_nons))
4882
return 0;
4983

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);
5188
if (data->symrefs && flag & REF_ISSYMREF) {
5289
struct object_id unused;
5390
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,
6198
strip_namespace(symref_target));
6299
}
63100

64-
if (data->peel) {
101+
if (data->peel && oid) {
65102
struct object_id peeled;
66103
if (!peel_ref(refname, &peeled))
67104
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,
74111
return 0;
75112
}
76113

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+
77131
static int ls_refs_config(const char *var, const char *value, void *data)
78132
{
79133
/*
@@ -91,6 +145,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
91145

92146
memset(&data, 0, sizeof(data));
93147

148+
ensure_config_read();
94149
git_config(ls_refs_config, NULL);
95150

96151
while (packet_reader_read(request) == PACKET_READ_NORMAL) {
@@ -103,14 +158,27 @@ int ls_refs(struct repository *r, struct strvec *keys,
103158
data.symrefs = 1;
104159
else if (skip_prefix(arg, "ref-prefix ", &out))
105160
strvec_push(&data.prefixes, out);
161+
else if (!strcmp("unborn", arg))
162+
data.unborn = allow_unborn;
106163
}
107164

108165
if (request->status != PACKET_READ_FLUSH)
109166
die(_("expected flush after ls-refs arguments"));
110167

111-
head_ref_namespaced(send_ref, &data);
168+
send_possibly_unborn_head(&data);
112169
for_each_namespaced_ref(send_ref, &data);
113170
packet_flush(1);
114171
strvec_clear(&data.prefixes);
115172
return 0;
116173
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ struct strvec;
66
struct packet_reader;
77
int ls_refs(struct repository *r, struct strvec *keys,
88
struct packet_reader *request);
9+
int ls_refs_advertise(struct repository *r, struct strbuf *value);
910

1011
#endif /* LS_REFS_H */

serve.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct protocol_capability {
7373

7474
static struct protocol_capability capabilities[] = {
7575
{ "agent", agent_advertise, NULL },
76-
{ "ls-refs", always_advertise, ls_refs },
76+
{ "ls-refs", ls_refs_advertise, ls_refs },
7777
{ "fetch", upload_pack_advertise, upload_pack_v2 },
7878
{ "server-option", always_advertise, NULL },
7979
{ "object-format", object_format_advertise, NULL },

t/t5701-git-serve.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test_expect_success 'test capability advertisement' '
1212
cat >expect <<-EOF &&
1313
version 2
1414
agent=git/$(git version | cut -d" " -f3)
15-
ls-refs
15+
ls-refs=unborn
1616
fetch=shallow
1717
server-option
1818
object-format=$(test_oid algo)

0 commit comments

Comments
 (0)