]> git.ipfire.org Git - thirdparty/git.git/blob - ls-refs.c
cache.h: remove dependence on hex.h; make other files include it explicitly
[thirdparty/git.git] / ls-refs.c
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"
11
12 static int config_read;
13 static int advertise_unborn;
14 static int allow_unborn;
15
16 static void ensure_config_read(void)
17 {
18 const char *str = NULL;
19
20 if (config_read)
21 return;
22
23 if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) {
24 /*
25 * If there is no such config, advertise and allow it by
26 * default.
27 */
28 advertise_unborn = 1;
29 allow_unborn = 1;
30 } else {
31 if (!strcmp(str, "advertise")) {
32 advertise_unborn = 1;
33 allow_unborn = 1;
34 } else if (!strcmp(str, "allow")) {
35 allow_unborn = 1;
36 } else if (!strcmp(str, "ignore")) {
37 /* do nothing */
38 } else {
39 die(_("invalid value for '%s': '%s'"),
40 "lsrefs.unborn", str);
41 }
42 }
43 config_read = 1;
44 }
45
46 /*
47 * If we see this many or more "ref-prefix" lines from the client, we consider
48 * it "too many" and will avoid using the prefix feature entirely.
49 */
50 #define TOO_MANY_PREFIXES 65536
51
52 /*
53 * Check if one of the prefixes is a prefix of the ref.
54 * If no prefixes were provided, all refs match.
55 */
56 static int ref_match(const struct strvec *prefixes, const char *refname)
57 {
58 int i;
59
60 if (!prefixes->nr)
61 return 1; /* no restriction */
62
63 for (i = 0; i < prefixes->nr; i++) {
64 const char *prefix = prefixes->v[i];
65
66 if (starts_with(refname, prefix))
67 return 1;
68 }
69
70 return 0;
71 }
72
73 struct ls_refs_data {
74 unsigned peel;
75 unsigned symrefs;
76 struct strvec prefixes;
77 struct strbuf buf;
78 struct string_list hidden_refs;
79 unsigned unborn : 1;
80 };
81
82 static int send_ref(const char *refname, const struct object_id *oid,
83 int flag, void *cb_data)
84 {
85 struct ls_refs_data *data = cb_data;
86 const char *refname_nons = strip_namespace(refname);
87
88 strbuf_reset(&data->buf);
89
90 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
91 return 0;
92
93 if (!ref_match(&data->prefixes, refname_nons))
94 return 0;
95
96 if (oid)
97 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
98 else
99 strbuf_addf(&data->buf, "unborn %s", refname_nons);
100 if (data->symrefs && flag & REF_ISSYMREF) {
101 struct object_id unused;
102 const char *symref_target = resolve_ref_unsafe(refname, 0,
103 &unused,
104 &flag);
105
106 if (!symref_target)
107 die("'%s' is a symref but it is not?", refname);
108
109 strbuf_addf(&data->buf, " symref-target:%s",
110 strip_namespace(symref_target));
111 }
112
113 if (data->peel && oid) {
114 struct object_id peeled;
115 if (!peel_iterated_oid(oid, &peeled))
116 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
117 }
118
119 strbuf_addch(&data->buf, '\n');
120 packet_fwrite(stdout, data->buf.buf, data->buf.len);
121
122 return 0;
123 }
124
125 static void send_possibly_unborn_head(struct ls_refs_data *data)
126 {
127 struct strbuf namespaced = STRBUF_INIT;
128 struct object_id oid;
129 int flag;
130 int oid_is_null;
131
132 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
133 if (!resolve_ref_unsafe(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, oid_is_null ? NULL : &oid, flag, data);
139 strbuf_release(&namespaced);
140 }
141
142 static int ls_refs_config(const char *var, const char *value,
143 void *cb_data)
144 {
145 struct ls_refs_data *data = cb_data;
146 /*
147 * We only serve fetches over v2 for now, so respect only "uploadpack"
148 * config. This may need to eventually be expanded to "receive", but we
149 * don't yet know how that information will be passed to ls-refs.
150 */
151 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
152 }
153
154 int ls_refs(struct repository *r, struct packet_reader *request)
155 {
156 struct ls_refs_data data;
157
158 memset(&data, 0, sizeof(data));
159 strvec_init(&data.prefixes);
160 strbuf_init(&data.buf, 0);
161 string_list_init_dup(&data.hidden_refs);
162
163 ensure_config_read();
164 git_config(ls_refs_config, &data);
165
166 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
167 const char *arg = request->line;
168 const char *out;
169
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);
177 }
178 else if (!strcmp("unborn", arg))
179 data.unborn = allow_unborn;
180 else
181 die(_("unexpected line: '%s'"), arg);
182 }
183
184 if (request->status != PACKET_READ_FLUSH)
185 die(_("expected flush after ls-refs arguments"));
186
187 /*
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.
191 */
192 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
193 strvec_clear(&data.prefixes);
194
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 send_ref, &data);
201 packet_fflush(stdout);
202 strvec_clear(&data.prefixes);
203 strbuf_release(&data.buf);
204 string_list_clear(&data.hidden_refs, 0);
205 return 0;
206 }
207
208 int ls_refs_advertise(struct repository *r, struct strbuf *value)
209 {
210 if (value) {
211 ensure_config_read();
212 if (advertise_unborn)
213 strbuf_addstr(value, "unborn");
214 }
215
216 return 1;
217 }