doc: clarify <src> in refspec syntax
[git/gitster.git] / ls-refs.c
blobc824aea7146a3e825c54b62bc7a77da82365940f
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hash.h"
7 #include "hex.h"
8 #include "repository.h"
9 #include "refs.h"
10 #include "strvec.h"
11 #include "ls-refs.h"
12 #include "pkt-line.h"
13 #include "config.h"
14 #include "string-list.h"
16 static enum {
17 UNBORN_IGNORE = 0,
18 UNBORN_ALLOW,
19 UNBORN_ADVERTISE /* implies ALLOW */
20 } unborn_config(struct repository *r)
22 const char *str = NULL;
24 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
26 * If there is no such config, advertise and allow it by
27 * default.
29 return UNBORN_ADVERTISE;
30 } else {
31 if (!strcmp(str, "advertise")) {
32 return UNBORN_ADVERTISE;
33 } else if (!strcmp(str, "allow")) {
34 return UNBORN_ALLOW;
35 } else if (!strcmp(str, "ignore")) {
36 return UNBORN_IGNORE;
37 } else {
38 die(_("invalid value for '%s': '%s'"),
39 "lsrefs.unborn", str);
45 * If we see this many or more "ref-prefix" lines from the client, we consider
46 * it "too many" and will avoid using the prefix feature entirely.
48 #define TOO_MANY_PREFIXES 65536
51 * Check if one of the prefixes is a prefix of the ref.
52 * If no prefixes were provided, all refs match.
54 static int ref_match(const struct strvec *prefixes, const char *refname)
56 int i;
58 if (!prefixes->nr)
59 return 1; /* no restriction */
61 for (i = 0; i < prefixes->nr; i++) {
62 const char *prefix = prefixes->v[i];
64 if (starts_with(refname, prefix))
65 return 1;
68 return 0;
71 struct ls_refs_data {
72 unsigned peel;
73 unsigned symrefs;
74 struct strvec prefixes;
75 struct strbuf buf;
76 struct strvec hidden_refs;
77 unsigned unborn : 1;
80 static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
81 int flag, void *cb_data)
83 struct ls_refs_data *data = cb_data;
84 const char *refname_nons = strip_namespace(refname);
86 strbuf_reset(&data->buf);
88 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
89 return 0;
91 if (!ref_match(&data->prefixes, refname_nons))
92 return 0;
94 if (oid)
95 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
96 else
97 strbuf_addf(&data->buf, "unborn %s", refname_nons);
98 if (data->symrefs && flag & REF_ISSYMREF) {
99 struct object_id unused;
100 const char *symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
101 refname,
103 &unused,
104 &flag);
106 if (!symref_target)
107 die("'%s' is a symref but it is not?", refname);
109 strbuf_addf(&data->buf, " symref-target:%s",
110 strip_namespace(symref_target));
113 if (data->peel && oid) {
114 struct object_id peeled;
115 if (!peel_iterated_oid(the_repository, oid, &peeled))
116 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
119 strbuf_addch(&data->buf, '\n');
120 packet_fwrite(stdout, data->buf.buf, data->buf.len);
122 return 0;
125 static void send_possibly_unborn_head(struct ls_refs_data *data)
127 struct strbuf namespaced = STRBUF_INIT;
128 struct object_id oid;
129 int flag;
130 int oid_is_null;
132 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
133 if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), namespaced.buf, 0, &oid, &flag))
134 return; /* bad ref */
135 oid_is_null = is_null_oid(&oid);
136 if (!oid_is_null ||
137 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
138 send_ref(namespaced.buf, NULL, oid_is_null ? NULL : &oid, flag, data);
139 strbuf_release(&namespaced);
142 static int ls_refs_config(const char *var, const char *value,
143 const struct config_context *ctx UNUSED,
144 void *cb_data)
146 struct ls_refs_data *data = cb_data;
148 * We only serve fetches over v2 for now, so respect only "uploadpack"
149 * config. This may need to eventually be expanded to "receive", but we
150 * don't yet know how that information will be passed to ls-refs.
152 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
155 int ls_refs(struct repository *r, struct packet_reader *request)
157 struct ls_refs_data data;
159 memset(&data, 0, sizeof(data));
160 strvec_init(&data.prefixes);
161 strbuf_init(&data.buf, 0);
162 strvec_init(&data.hidden_refs);
164 git_config(ls_refs_config, &data);
166 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
167 const char *arg = request->line;
168 const char *out;
170 if (!strcmp("peel", arg))
171 data.peel = 1;
172 else if (!strcmp("symrefs", arg))
173 data.symrefs = 1;
174 else if (skip_prefix(arg, "ref-prefix ", &out)) {
175 if (data.prefixes.nr < TOO_MANY_PREFIXES)
176 strvec_push(&data.prefixes, out);
178 else if (!strcmp("unborn", arg))
179 data.unborn = !!unborn_config(r);
180 else
181 die(_("unexpected line: '%s'"), arg);
184 if (request->status != PACKET_READ_FLUSH)
185 die(_("expected flush after ls-refs arguments"));
188 * If we saw too many prefixes, we must avoid using them at all; as
189 * soon as we have any prefix, they are meant to form a comprehensive
190 * list.
192 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
193 strvec_clear(&data.prefixes);
195 send_possibly_unborn_head(&data);
196 if (!data.prefixes.nr)
197 strvec_push(&data.prefixes, "");
198 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
199 get_git_namespace(), data.prefixes.v,
200 hidden_refs_to_excludes(&data.hidden_refs),
201 send_ref, &data);
202 packet_fflush(stdout);
203 strvec_clear(&data.prefixes);
204 strbuf_release(&data.buf);
205 strvec_clear(&data.hidden_refs);
206 return 0;
209 int ls_refs_advertise(struct repository *r, struct strbuf *value)
211 if (value && unborn_config(r) == UNBORN_ADVERTISE)
212 strbuf_addstr(value, "unborn");
214 return 1;