Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hash.h"
7 #include "hex.h"
8 #include "repository.h"
9 #include "refs.h"
10 #include "strvec.h"
11 #include "ls-refs.h"
12 #include "pkt-line.h"
13 #include "config.h"
14 #include "string-list.h"
15
16 static enum {
17 UNBORN_IGNORE = 0,
18 UNBORN_ALLOW,
19 UNBORN_ADVERTISE /* implies ALLOW */
20 } unborn_config(struct repository *r)
21 {
22 const char *str = NULL;
23
24 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
25 /*
26 * If there is no such config, advertise and allow it by
27 * default.
28 */
29 return UNBORN_ADVERTISE;
30 } else {
31 if (!strcmp(str, "advertise")) {
32 return UNBORN_ADVERTISE;
33 } else if (!strcmp(str, "allow")) {
34 return UNBORN_ALLOW;
35 } else if (!strcmp(str, "ignore")) {
36 return UNBORN_IGNORE;
37 } else {
38 die(_("invalid value for '%s': '%s'"),
39 "lsrefs.unborn", str);
40 }
41 }
42 }
43
44 /*
45 * If we see this many or more "ref-prefix" lines from the client, we consider
46 * it "too many" and will avoid using the prefix feature entirely.
47 */
48 #define TOO_MANY_PREFIXES 65536
49
50 /*
51 * Check if one of the prefixes is a prefix of the ref.
52 * If no prefixes were provided, all refs match.
53 */
54 static int ref_match(const struct strvec *prefixes, const char *refname)
55 {
56 if (!prefixes->nr)
57 return 1; /* no restriction */
58
59 for (size_t i = 0; i < prefixes->nr; i++) {
60 const char *prefix = prefixes->v[i];
61
62 if (starts_with(refname, prefix))
63 return 1;
64 }
65
66 return 0;
67 }
68
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;
76 };
77
78 static int send_ref(const struct reference *ref, void *cb_data)
79 {
80 struct ls_refs_data *data = cb_data;
81 const char *refname_nons = strip_namespace(ref->name);
82
83 strbuf_reset(&data->buf);
84
85 if (ref_is_hidden(refname_nons, ref->name, &data->hidden_refs))
86 return 0;
87
88 if (!ref_match(&data->prefixes, refname_nons))
89 return 0;
90
91 if (ref->oid)
92 strbuf_addf(&data->buf, "%s %s", oid_to_hex(ref->oid), refname_nons);
93 else
94 strbuf_addf(&data->buf, "unborn %s", refname_nons);
95 if (data->symrefs && ref->flags & REF_ISSYMREF) {
96 int unused_flag;
97 struct object_id unused;
98 const char *symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
99 ref->name,
100 0,
101 &unused,
102 &unused_flag);
103
104 if (!symref_target)
105 die("'%s' is a symref but it is not?", ref->name);
106
107 strbuf_addf(&data->buf, " symref-target:%s",
108 strip_namespace(symref_target));
109 }
110
111 if (data->peel && ref->oid) {
112 struct object_id peeled;
113 if (!reference_get_peeled_oid(the_repository, ref, &peeled))
114 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
115 }
116
117 strbuf_addch(&data->buf, '\n');
118 packet_fwrite(stdout, data->buf.buf, data->buf.len);
119
120 return 0;
121 }
122
123 static void send_possibly_unborn_head(struct ls_refs_data *data)
124 {
125 struct strbuf namespaced = STRBUF_INIT;
126 struct object_id oid;
127 int flag;
128 int oid_is_null;
129
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
135 if (!oid_is_null ||
136 (data->unborn && data->symrefs && (flag & REF_ISSYMREF))) {
137 struct reference ref = {
138 .name = namespaced.buf,
139 .oid = oid_is_null ? NULL : &oid,
140 .flags = flag,
141 };
142
143 send_ref(&ref, data);
144 }
145 strbuf_release(&namespaced);
146 }
147
148 static int ls_refs_config(const char *var, const char *value,
149 const struct config_context *ctx UNUSED,
150 void *cb_data)
151 {
152 struct ls_refs_data *data = cb_data;
153 /*
154 * We only serve fetches over v2 for now, so respect only "uploadpack"
155 * config. This may need to eventually be expanded to "receive", but we
156 * don't yet know how that information will be passed to ls-refs.
157 */
158 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
159 }
160
161 int ls_refs(struct repository *r, struct packet_reader *request)
162 {
163 struct refs_for_each_ref_options opts = { 0 };
164 struct ls_refs_data data;
165
166 memset(&data, 0, sizeof(data));
167 strvec_init(&data.prefixes);
168 strbuf_init(&data.buf, 0);
169 strvec_init(&data.hidden_refs);
170
171 repo_config(the_repository, ls_refs_config, &data);
172
173 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
174 const char *arg = request->line;
175 const char *out;
176
177 if (!strcmp("peel", arg))
178 data.peel = 1;
179 else if (!strcmp("symrefs", arg))
180 data.symrefs = 1;
181 else if (skip_prefix(arg, "ref-prefix ", &out)) {
182 if (data.prefixes.nr < TOO_MANY_PREFIXES)
183 strvec_push(&data.prefixes, out);
184 }
185 else if (!strcmp("unborn", arg))
186 data.unborn = !!unborn_config(r);
187 else
188 die(_("unexpected line: '%s'"), arg);
189 }
190
191 if (request->status != PACKET_READ_FLUSH)
192 die(_("expected flush after ls-refs arguments"));
193
194 /*
195 * If we saw too many prefixes, we must avoid using them at all; as
196 * soon as we have any prefix, they are meant to form a comprehensive
197 * list.
198 */
199 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
200 strvec_clear(&data.prefixes);
201
202 send_possibly_unborn_head(&data);
203 if (!data.prefixes.nr)
204 strvec_push(&data.prefixes, "");
205
206 opts.exclude_patterns = hidden_refs_to_excludes(&data.hidden_refs);
207 opts.namespace = get_git_namespace();
208
209 refs_for_each_ref_in_prefixes(get_main_ref_store(r), data.prefixes.v,
210 &opts, send_ref, &data);
211 packet_fflush(stdout);
212 strvec_clear(&data.prefixes);
213 strbuf_release(&data.buf);
214 strvec_clear(&data.hidden_refs);
215 return 0;
216 }
217
218 int ls_refs_advertise(struct repository *r, struct strbuf *value)
219 {
220 if (value && unborn_config(r) == UNBORN_ADVERTISE)
221 strbuf_addstr(value, "unborn");
222
223 return 1;
224 }