Merge branch 'ss/hashmap-typofix'
[alt-git.git] / ls-refs.c
blob8091b0cca8cae05112230e7644674e37138dfa93
1 #include "cache.h"
2 #include "hex.h"
3 #include "repository.h"
4 #include "refs.h"
5 #include "remote.h"
6 #include "strvec.h"
7 #include "ls-refs.h"
8 #include "pkt-line.h"
9 #include "config.h"
10 #include "string-list.h"
12 static enum {
13 UNBORN_IGNORE = 0,
14 UNBORN_ALLOW,
15 UNBORN_ADVERTISE /* implies ALLOW */
16 } unborn_config(struct repository *r)
18 const char *str = NULL;
20 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
22 * If there is no such config, advertise and allow it by
23 * default.
25 return UNBORN_ADVERTISE;
26 } else {
27 if (!strcmp(str, "advertise")) {
28 return UNBORN_ADVERTISE;
29 } else if (!strcmp(str, "allow")) {
30 return UNBORN_ALLOW;
31 } else if (!strcmp(str, "ignore")) {
32 return UNBORN_IGNORE;
33 } else {
34 die(_("invalid value for '%s': '%s'"),
35 "lsrefs.unborn", str);
41 * If we see this many or more "ref-prefix" lines from the client, we consider
42 * it "too many" and will avoid using the prefix feature entirely.
44 #define TOO_MANY_PREFIXES 65536
47 * Check if one of the prefixes is a prefix of the ref.
48 * If no prefixes were provided, all refs match.
50 static int ref_match(const struct strvec *prefixes, const char *refname)
52 int i;
54 if (!prefixes->nr)
55 return 1; /* no restriction */
57 for (i = 0; i < prefixes->nr; i++) {
58 const char *prefix = prefixes->v[i];
60 if (starts_with(refname, prefix))
61 return 1;
64 return 0;
67 struct ls_refs_data {
68 unsigned peel;
69 unsigned symrefs;
70 struct strvec prefixes;
71 struct strbuf buf;
72 struct string_list hidden_refs;
73 unsigned unborn : 1;
76 static int send_ref(const char *refname, const struct object_id *oid,
77 int flag, void *cb_data)
79 struct ls_refs_data *data = cb_data;
80 const char *refname_nons = strip_namespace(refname);
82 strbuf_reset(&data->buf);
84 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
85 return 0;
87 if (!ref_match(&data->prefixes, refname_nons))
88 return 0;
90 if (oid)
91 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
92 else
93 strbuf_addf(&data->buf, "unborn %s", refname_nons);
94 if (data->symrefs && flag & REF_ISSYMREF) {
95 struct object_id unused;
96 const char *symref_target = resolve_ref_unsafe(refname, 0,
97 &unused,
98 &flag);
100 if (!symref_target)
101 die("'%s' is a symref but it is not?", refname);
103 strbuf_addf(&data->buf, " symref-target:%s",
104 strip_namespace(symref_target));
107 if (data->peel && oid) {
108 struct object_id peeled;
109 if (!peel_iterated_oid(oid, &peeled))
110 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
113 strbuf_addch(&data->buf, '\n');
114 packet_fwrite(stdout, data->buf.buf, data->buf.len);
116 return 0;
119 static void send_possibly_unborn_head(struct ls_refs_data *data)
121 struct strbuf namespaced = STRBUF_INIT;
122 struct object_id oid;
123 int flag;
124 int oid_is_null;
126 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
127 if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
128 return; /* bad ref */
129 oid_is_null = is_null_oid(&oid);
130 if (!oid_is_null ||
131 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
132 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
133 strbuf_release(&namespaced);
136 static int ls_refs_config(const char *var, const char *value,
137 void *cb_data)
139 struct ls_refs_data *data = cb_data;
141 * We only serve fetches over v2 for now, so respect only "uploadpack"
142 * config. This may need to eventually be expanded to "receive", but we
143 * don't yet know how that information will be passed to ls-refs.
145 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
148 int ls_refs(struct repository *r, struct packet_reader *request)
150 struct ls_refs_data data;
152 memset(&data, 0, sizeof(data));
153 strvec_init(&data.prefixes);
154 strbuf_init(&data.buf, 0);
155 string_list_init_dup(&data.hidden_refs);
157 git_config(ls_refs_config, &data);
159 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
160 const char *arg = request->line;
161 const char *out;
163 if (!strcmp("peel", arg))
164 data.peel = 1;
165 else if (!strcmp("symrefs", arg))
166 data.symrefs = 1;
167 else if (skip_prefix(arg, "ref-prefix ", &out)) {
168 if (data.prefixes.nr < TOO_MANY_PREFIXES)
169 strvec_push(&data.prefixes, out);
171 else if (!strcmp("unborn", arg))
172 data.unborn = !!unborn_config(r);
173 else
174 die(_("unexpected line: '%s'"), arg);
177 if (request->status != PACKET_READ_FLUSH)
178 die(_("expected flush after ls-refs arguments"));
181 * If we saw too many prefixes, we must avoid using them at all; as
182 * soon as we have any prefix, they are meant to form a comprehensive
183 * list.
185 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
186 strvec_clear(&data.prefixes);
188 send_possibly_unborn_head(&data);
189 if (!data.prefixes.nr)
190 strvec_push(&data.prefixes, "");
191 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
192 get_git_namespace(), data.prefixes.v,
193 send_ref, &data);
194 packet_fflush(stdout);
195 strvec_clear(&data.prefixes);
196 strbuf_release(&data.buf);
197 string_list_clear(&data.hidden_refs, 0);
198 return 0;
201 int ls_refs_advertise(struct repository *r, struct strbuf *value)
203 if (value && unborn_config(r) == UNBORN_ADVERTISE)
204 strbuf_addstr(value, "unborn");
206 return 1;