diff: refactor code to clarify memory ownership of prefixes
[git.git] / ls-refs.c
blob8e3ffff8117461a3ed19e8bf9b14882b2331f6f8
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "gettext.h"
4 #include "hash.h"
5 #include "hex.h"
6 #include "repository.h"
7 #include "refs.h"
8 #include "strvec.h"
9 #include "ls-refs.h"
10 #include "pkt-line.h"
11 #include "config.h"
12 #include "string-list.h"
14 static enum {
15 UNBORN_IGNORE = 0,
16 UNBORN_ALLOW,
17 UNBORN_ADVERTISE /* implies ALLOW */
18 } unborn_config(struct repository *r)
20 const char *str = NULL;
22 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
24 * If there is no such config, advertise and allow it by
25 * default.
27 return UNBORN_ADVERTISE;
28 } else {
29 if (!strcmp(str, "advertise")) {
30 return UNBORN_ADVERTISE;
31 } else if (!strcmp(str, "allow")) {
32 return UNBORN_ALLOW;
33 } else if (!strcmp(str, "ignore")) {
34 return UNBORN_IGNORE;
35 } else {
36 die(_("invalid value for '%s': '%s'"),
37 "lsrefs.unborn", str);
43 * If we see this many or more "ref-prefix" lines from the client, we consider
44 * it "too many" and will avoid using the prefix feature entirely.
46 #define TOO_MANY_PREFIXES 65536
49 * Check if one of the prefixes is a prefix of the ref.
50 * If no prefixes were provided, all refs match.
52 static int ref_match(const struct strvec *prefixes, const char *refname)
54 int i;
56 if (!prefixes->nr)
57 return 1; /* no restriction */
59 for (i = 0; i < prefixes->nr; i++) {
60 const char *prefix = prefixes->v[i];
62 if (starts_with(refname, prefix))
63 return 1;
66 return 0;
69 struct ls_refs_data {
70 unsigned peel;
71 unsigned symrefs;
72 struct strvec prefixes;
73 struct strbuf buf;
74 struct strvec hidden_refs;
75 unsigned unborn : 1;
78 static int send_ref(const char *refname, const struct object_id *oid,
79 int flag, void *cb_data)
81 struct ls_refs_data *data = cb_data;
82 const char *refname_nons = strip_namespace(refname);
84 strbuf_reset(&data->buf);
86 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
87 return 0;
89 if (!ref_match(&data->prefixes, refname_nons))
90 return 0;
92 if (oid)
93 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
94 else
95 strbuf_addf(&data->buf, "unborn %s", refname_nons);
96 if (data->symrefs && flag & REF_ISSYMREF) {
97 struct object_id unused;
98 const char *symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
99 refname,
101 &unused,
102 &flag);
104 if (!symref_target)
105 die("'%s' is a symref but it is not?", refname);
107 strbuf_addf(&data->buf, " symref-target:%s",
108 strip_namespace(symref_target));
111 if (data->peel && oid) {
112 struct object_id peeled;
113 if (!peel_iterated_oid(oid, &peeled))
114 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
117 strbuf_addch(&data->buf, '\n');
118 packet_fwrite(stdout, data->buf.buf, data->buf.len);
120 return 0;
123 static void send_possibly_unborn_head(struct ls_refs_data *data)
125 struct strbuf namespaced = STRBUF_INIT;
126 struct object_id oid;
127 int flag;
128 int oid_is_null;
130 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
131 if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), namespaced.buf, 0, &oid, &flag))
132 return; /* bad ref */
133 oid_is_null = is_null_oid(&oid);
134 if (!oid_is_null ||
135 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
136 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
137 strbuf_release(&namespaced);
140 static int ls_refs_config(const char *var, const char *value,
141 const struct config_context *ctx UNUSED,
142 void *cb_data)
144 struct ls_refs_data *data = cb_data;
146 * We only serve fetches over v2 for now, so respect only "uploadpack"
147 * config. This may need to eventually be expanded to "receive", but we
148 * don't yet know how that information will be passed to ls-refs.
150 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
153 int ls_refs(struct repository *r, struct packet_reader *request)
155 struct ls_refs_data data;
157 memset(&data, 0, sizeof(data));
158 strvec_init(&data.prefixes);
159 strbuf_init(&data.buf, 0);
160 strvec_init(&data.hidden_refs);
162 git_config(ls_refs_config, &data);
164 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
165 const char *arg = request->line;
166 const char *out;
168 if (!strcmp("peel", arg))
169 data.peel = 1;
170 else if (!strcmp("symrefs", arg))
171 data.symrefs = 1;
172 else if (skip_prefix(arg, "ref-prefix ", &out)) {
173 if (data.prefixes.nr < TOO_MANY_PREFIXES)
174 strvec_push(&data.prefixes, out);
176 else if (!strcmp("unborn", arg))
177 data.unborn = !!unborn_config(r);
178 else
179 die(_("unexpected line: '%s'"), arg);
182 if (request->status != PACKET_READ_FLUSH)
183 die(_("expected flush after ls-refs arguments"));
186 * If we saw too many prefixes, we must avoid using them at all; as
187 * soon as we have any prefix, they are meant to form a comprehensive
188 * list.
190 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
191 strvec_clear(&data.prefixes);
193 send_possibly_unborn_head(&data);
194 if (!data.prefixes.nr)
195 strvec_push(&data.prefixes, "");
196 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
197 get_git_namespace(), data.prefixes.v,
198 hidden_refs_to_excludes(&data.hidden_refs),
199 send_ref, &data);
200 packet_fflush(stdout);
201 strvec_clear(&data.prefixes);
202 strbuf_release(&data.buf);
203 strvec_clear(&data.hidden_refs);
204 return 0;
207 int ls_refs_advertise(struct repository *r, struct strbuf *value)
209 if (value && unborn_config(r) == UNBORN_ADVERTISE)
210 strbuf_addstr(value, "unborn");
212 return 1;