config.txt: describe handling of whitespace further
[alt-git.git] / ls-refs.c
blob0e49b932c308b24a5654910d5e0197cf1388162e
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 "remote.h"
9 #include "strvec.h"
10 #include "ls-refs.h"
11 #include "pkt-line.h"
12 #include "config.h"
13 #include "string-list.h"
15 static enum {
16 UNBORN_IGNORE = 0,
17 UNBORN_ALLOW,
18 UNBORN_ADVERTISE /* implies ALLOW */
19 } unborn_config(struct repository *r)
21 const char *str = NULL;
23 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
25 * If there is no such config, advertise and allow it by
26 * default.
28 return UNBORN_ADVERTISE;
29 } else {
30 if (!strcmp(str, "advertise")) {
31 return UNBORN_ADVERTISE;
32 } else if (!strcmp(str, "allow")) {
33 return UNBORN_ALLOW;
34 } else if (!strcmp(str, "ignore")) {
35 return UNBORN_IGNORE;
36 } else {
37 die(_("invalid value for '%s': '%s'"),
38 "lsrefs.unborn", str);
44 * If we see this many or more "ref-prefix" lines from the client, we consider
45 * it "too many" and will avoid using the prefix feature entirely.
47 #define TOO_MANY_PREFIXES 65536
50 * Check if one of the prefixes is a prefix of the ref.
51 * If no prefixes were provided, all refs match.
53 static int ref_match(const struct strvec *prefixes, const char *refname)
55 int i;
57 if (!prefixes->nr)
58 return 1; /* no restriction */
60 for (i = 0; i < prefixes->nr; i++) {
61 const char *prefix = prefixes->v[i];
63 if (starts_with(refname, prefix))
64 return 1;
67 return 0;
70 struct ls_refs_data {
71 unsigned peel;
72 unsigned symrefs;
73 struct strvec prefixes;
74 struct strbuf buf;
75 struct strvec hidden_refs;
76 unsigned unborn : 1;
79 static int send_ref(const char *refname, const struct object_id *oid,
80 int flag, void *cb_data)
82 struct ls_refs_data *data = cb_data;
83 const char *refname_nons = strip_namespace(refname);
85 strbuf_reset(&data->buf);
87 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
88 return 0;
90 if (!ref_match(&data->prefixes, refname_nons))
91 return 0;
93 if (oid)
94 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
95 else
96 strbuf_addf(&data->buf, "unborn %s", refname_nons);
97 if (data->symrefs && flag & REF_ISSYMREF) {
98 struct object_id unused;
99 const char *symref_target = resolve_ref_unsafe(refname, 0,
100 &unused,
101 &flag);
103 if (!symref_target)
104 die("'%s' is a symref but it is not?", refname);
106 strbuf_addf(&data->buf, " symref-target:%s",
107 strip_namespace(symref_target));
110 if (data->peel && oid) {
111 struct object_id peeled;
112 if (!peel_iterated_oid(oid, &peeled))
113 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
116 strbuf_addch(&data->buf, '\n');
117 packet_fwrite(stdout, data->buf.buf, data->buf.len);
119 return 0;
122 static void send_possibly_unborn_head(struct ls_refs_data *data)
124 struct strbuf namespaced = STRBUF_INIT;
125 struct object_id oid;
126 int flag;
127 int oid_is_null;
129 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
130 if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
131 return; /* bad ref */
132 oid_is_null = is_null_oid(&oid);
133 if (!oid_is_null ||
134 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
135 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
136 strbuf_release(&namespaced);
139 static int ls_refs_config(const char *var, const char *value,
140 const struct config_context *ctx UNUSED,
141 void *cb_data)
143 struct ls_refs_data *data = cb_data;
145 * We only serve fetches over v2 for now, so respect only "uploadpack"
146 * config. This may need to eventually be expanded to "receive", but we
147 * don't yet know how that information will be passed to ls-refs.
149 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
152 int ls_refs(struct repository *r, struct packet_reader *request)
154 struct ls_refs_data data;
156 memset(&data, 0, sizeof(data));
157 strvec_init(&data.prefixes);
158 strbuf_init(&data.buf, 0);
159 strvec_init(&data.hidden_refs);
161 git_config(ls_refs_config, &data);
163 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
164 const char *arg = request->line;
165 const char *out;
167 if (!strcmp("peel", arg))
168 data.peel = 1;
169 else if (!strcmp("symrefs", arg))
170 data.symrefs = 1;
171 else if (skip_prefix(arg, "ref-prefix ", &out)) {
172 if (data.prefixes.nr < TOO_MANY_PREFIXES)
173 strvec_push(&data.prefixes, out);
175 else if (!strcmp("unborn", arg))
176 data.unborn = !!unborn_config(r);
177 else
178 die(_("unexpected line: '%s'"), arg);
181 if (request->status != PACKET_READ_FLUSH)
182 die(_("expected flush after ls-refs arguments"));
185 * If we saw too many prefixes, we must avoid using them at all; as
186 * soon as we have any prefix, they are meant to form a comprehensive
187 * list.
189 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
190 strvec_clear(&data.prefixes);
192 send_possibly_unborn_head(&data);
193 if (!data.prefixes.nr)
194 strvec_push(&data.prefixes, "");
195 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
196 get_git_namespace(), data.prefixes.v,
197 hidden_refs_to_excludes(&data.hidden_refs),
198 send_ref, &data);
199 packet_fflush(stdout);
200 strvec_clear(&data.prefixes);
201 strbuf_release(&data.buf);
202 strvec_clear(&data.hidden_refs);
203 return 0;
206 int ls_refs_advertise(struct repository *r, struct strbuf *value)
208 if (value && unborn_config(r) == UNBORN_ADVERTISE)
209 strbuf_addstr(value, "unborn");
211 return 1;