t5559: fix test failures with LIB_HTTPD_SSL
[git/debian.git] / ls-refs.c
blob697d4beb8de43a4a7ae48be8284e10c6be2a9bfd
1 #include "cache.h"
2 #include "repository.h"
3 #include "refs.h"
4 #include "remote.h"
5 #include "strvec.h"
6 #include "ls-refs.h"
7 #include "pkt-line.h"
8 #include "config.h"
9 #include "string-list.h"
11 static int config_read;
12 static int advertise_unborn;
13 static int allow_unborn;
15 static void ensure_config_read(void)
17 const char *str = NULL;
19 if (config_read)
20 return;
22 if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) {
24 * If there is no such config, advertise and allow it by
25 * default.
27 advertise_unborn = 1;
28 allow_unborn = 1;
29 } else {
30 if (!strcmp(str, "advertise")) {
31 advertise_unborn = 1;
32 allow_unborn = 1;
33 } else if (!strcmp(str, "allow")) {
34 allow_unborn = 1;
35 } else if (!strcmp(str, "ignore")) {
36 /* do nothing */
37 } else {
38 die(_("invalid value for '%s': '%s'"),
39 "lsrefs.unborn", str);
42 config_read = 1;
46 * If we see this many or more "ref-prefix" lines from the client, we consider
47 * it "too many" and will avoid using the prefix feature entirely.
49 #define TOO_MANY_PREFIXES 65536
52 * Check if one of the prefixes is a prefix of the ref.
53 * If no prefixes were provided, all refs match.
55 static int ref_match(const struct strvec *prefixes, const char *refname)
57 int i;
59 if (!prefixes->nr)
60 return 1; /* no restriction */
62 for (i = 0; i < prefixes->nr; i++) {
63 const char *prefix = prefixes->v[i];
65 if (starts_with(refname, prefix))
66 return 1;
69 return 0;
72 struct ls_refs_data {
73 unsigned peel;
74 unsigned symrefs;
75 struct strvec prefixes;
76 struct strbuf buf;
77 struct string_list hidden_refs;
78 unsigned unborn : 1;
81 static int send_ref(const char *refname, const struct object_id *oid,
82 int flag, void *cb_data)
84 struct ls_refs_data *data = cb_data;
85 const char *refname_nons = strip_namespace(refname);
87 strbuf_reset(&data->buf);
89 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
90 return 0;
92 if (!ref_match(&data->prefixes, refname_nons))
93 return 0;
95 if (oid)
96 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
97 else
98 strbuf_addf(&data->buf, "unborn %s", refname_nons);
99 if (data->symrefs && flag & REF_ISSYMREF) {
100 struct object_id unused;
101 const char *symref_target = resolve_ref_unsafe(refname, 0,
102 &unused,
103 &flag);
105 if (!symref_target)
106 die("'%s' is a symref but it is not?", refname);
108 strbuf_addf(&data->buf, " symref-target:%s",
109 strip_namespace(symref_target));
112 if (data->peel && oid) {
113 struct object_id peeled;
114 if (!peel_iterated_oid(oid, &peeled))
115 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
118 strbuf_addch(&data->buf, '\n');
119 packet_fwrite(stdout, data->buf.buf, data->buf.len);
121 return 0;
124 static void send_possibly_unborn_head(struct ls_refs_data *data)
126 struct strbuf namespaced = STRBUF_INIT;
127 struct object_id oid;
128 int flag;
129 int oid_is_null;
131 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
132 if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
133 return; /* bad ref */
134 oid_is_null = is_null_oid(&oid);
135 if (!oid_is_null ||
136 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
137 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
138 strbuf_release(&namespaced);
141 static int ls_refs_config(const char *var, const char *value,
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 string_list_init_dup(&data.hidden_refs);
162 ensure_config_read();
163 git_config(ls_refs_config, &data);
165 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
166 const char *arg = request->line;
167 const char *out;
169 if (!strcmp("peel", arg))
170 data.peel = 1;
171 else if (!strcmp("symrefs", arg))
172 data.symrefs = 1;
173 else if (skip_prefix(arg, "ref-prefix ", &out)) {
174 if (data.prefixes.nr < TOO_MANY_PREFIXES)
175 strvec_push(&data.prefixes, out);
177 else if (!strcmp("unborn", arg))
178 data.unborn = allow_unborn;
179 else
180 die(_("unexpected line: '%s'"), arg);
183 if (request->status != PACKET_READ_FLUSH)
184 die(_("expected flush after ls-refs arguments"));
187 * If we saw too many prefixes, we must avoid using them at all; as
188 * soon as we have any prefix, they are meant to form a comprehensive
189 * list.
191 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
192 strvec_clear(&data.prefixes);
194 send_possibly_unborn_head(&data);
195 if (!data.prefixes.nr)
196 strvec_push(&data.prefixes, "");
197 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
198 get_git_namespace(), data.prefixes.v,
199 send_ref, &data);
200 packet_fflush(stdout);
201 strvec_clear(&data.prefixes);
202 strbuf_release(&data.buf);
203 string_list_clear(&data.hidden_refs, 0);
204 return 0;
207 int ls_refs_advertise(struct repository *r, struct strbuf *value)
209 if (value) {
210 ensure_config_read();
211 if (advertise_unborn)
212 strbuf_addstr(value, "unborn");
215 return 1;