]> git.ipfire.org Git - thirdparty/git.git/blame - ls-refs.c
refs: plumb `exclude_patterns` argument throughout
[thirdparty/git.git] / ls-refs.c
CommitLineData
98750588 1#include "git-compat-util.h"
32a8f510 2#include "environment.h"
f394e093 3#include "gettext.h"
d1cbe1e6 4#include "hash.h"
41771fa4 5#include "hex.h"
72d0ea00
BW
6#include "repository.h"
7#include "refs.h"
8#include "remote.h"
dbbcd44f 9#include "strvec.h"
72d0ea00
BW
10#include "ls-refs.h"
11#include "pkt-line.h"
e20b4192 12#include "config.h"
9b67eb6f 13#include "string-list.h"
72d0ea00 14
c4716086
JK
15static enum {
16 UNBORN_IGNORE = 0,
17 UNBORN_ALLOW,
18 UNBORN_ADVERTISE /* implies ALLOW */
4b4e75dd 19} unborn_config(struct repository *r)
59e1205d
JT
20{
21 const char *str = NULL;
22
4b4e75dd 23 if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
59e1205d
JT
24 /*
25 * If there is no such config, advertise and allow it by
26 * default.
27 */
c4716086 28 return UNBORN_ADVERTISE;
59e1205d
JT
29 } else {
30 if (!strcmp(str, "advertise")) {
c4716086 31 return UNBORN_ADVERTISE;
59e1205d 32 } else if (!strcmp(str, "allow")) {
c4716086 33 return UNBORN_ALLOW;
59e1205d 34 } else if (!strcmp(str, "ignore")) {
c4716086 35 return UNBORN_IGNORE;
59e1205d 36 } else {
1a8aea85
JNA
37 die(_("invalid value for '%s': '%s'"),
38 "lsrefs.unborn", str);
59e1205d
JT
39 }
40 }
59e1205d
JT
41}
42
7f0e4f6a
JK
43/*
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.
46 */
47#define TOO_MANY_PREFIXES 65536
48
72d0ea00
BW
49/*
50 * Check if one of the prefixes is a prefix of the ref.
51 * If no prefixes were provided, all refs match.
52 */
ef8d7ac4 53static int ref_match(const struct strvec *prefixes, const char *refname)
72d0ea00
BW
54{
55 int i;
56
d70a9eb6 57 if (!prefixes->nr)
72d0ea00
BW
58 return 1; /* no restriction */
59
d70a9eb6
JK
60 for (i = 0; i < prefixes->nr; i++) {
61 const char *prefix = prefixes->v[i];
72d0ea00
BW
62
63 if (starts_with(refname, prefix))
64 return 1;
65 }
66
67 return 0;
68}
69
70struct ls_refs_data {
71 unsigned peel;
72 unsigned symrefs;
ef8d7ac4 73 struct strvec prefixes;
f54b9f21 74 struct strbuf buf;
9b67eb6f 75 struct string_list hidden_refs;
59e1205d 76 unsigned unborn : 1;
72d0ea00
BW
77};
78
79static int send_ref(const char *refname, const struct object_id *oid,
80 int flag, void *cb_data)
81{
82 struct ls_refs_data *data = cb_data;
83 const char *refname_nons = strip_namespace(refname);
f54b9f21
PS
84
85 strbuf_reset(&data->buf);
72d0ea00 86
9b67eb6f 87 if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
e20b4192
JK
88 return 0;
89
e2f41a0a 90 if (!ref_match(&data->prefixes, refname_nons))
72d0ea00
BW
91 return 0;
92
59e1205d 93 if (oid)
f54b9f21 94 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
59e1205d 95 else
f54b9f21 96 strbuf_addf(&data->buf, "unborn %s", refname_nons);
72d0ea00
BW
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);
102
103 if (!symref_target)
104 die("'%s' is a symref but it is not?", refname);
105
f54b9f21 106 strbuf_addf(&data->buf, " symref-target:%s",
533e0882 107 strip_namespace(symref_target));
72d0ea00
BW
108 }
109
59e1205d 110 if (data->peel && oid) {
72d0ea00 111 struct object_id peeled;
36a31792 112 if (!peel_iterated_oid(oid, &peeled))
f54b9f21 113 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
72d0ea00
BW
114 }
115
f54b9f21 116 strbuf_addch(&data->buf, '\n');
c2509c54 117 packet_fwrite(stdout, data->buf.buf, data->buf.len);
72d0ea00 118
72d0ea00
BW
119 return 0;
120}
121
59e1205d
JT
122static void send_possibly_unborn_head(struct ls_refs_data *data)
123{
124 struct strbuf namespaced = STRBUF_INIT;
125 struct object_id oid;
126 int flag;
127 int oid_is_null;
128
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);
137}
138
783a86c1 139static int ls_refs_config(const char *var, const char *value,
9b67eb6f 140 void *cb_data)
e20b4192 141{
9b67eb6f 142 struct ls_refs_data *data = cb_data;
e20b4192
JK
143 /*
144 * We only serve fetches over v2 for now, so respect only "uploadpack"
145 * config. This may need to eventually be expanded to "receive", but we
146 * don't yet know how that information will be passed to ls-refs.
147 */
9b67eb6f 148 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
e20b4192
JK
149}
150
28a592e4 151int ls_refs(struct repository *r, struct packet_reader *request)
72d0ea00
BW
152{
153 struct ls_refs_data data;
154
155 memset(&data, 0, sizeof(data));
83befd37 156 strvec_init(&data.prefixes);
f54b9f21 157 strbuf_init(&data.buf, 0);
9b67eb6f 158 string_list_init_dup(&data.hidden_refs);
72d0ea00 159
9b67eb6f 160 git_config(ls_refs_config, &data);
e20b4192 161
4845b772 162 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
72d0ea00
BW
163 const char *arg = request->line;
164 const char *out;
165
166 if (!strcmp("peel", arg))
167 data.peel = 1;
168 else if (!strcmp("symrefs", arg))
169 data.symrefs = 1;
7f0e4f6a
JK
170 else if (skip_prefix(arg, "ref-prefix ", &out)) {
171 if (data.prefixes.nr < TOO_MANY_PREFIXES)
172 strvec_push(&data.prefixes, out);
173 }
59e1205d 174 else if (!strcmp("unborn", arg))
4b4e75dd 175 data.unborn = !!unborn_config(r);
ccf09478
JK
176 else
177 die(_("unexpected line: '%s'"), arg);
72d0ea00
BW
178 }
179
4845b772
JK
180 if (request->status != PACKET_READ_FLUSH)
181 die(_("expected flush after ls-refs arguments"));
182
7f0e4f6a
JK
183 /*
184 * If we saw too many prefixes, we must avoid using them at all; as
185 * soon as we have any prefix, they are meant to form a comprehensive
186 * list.
187 */
188 if (data.prefixes.nr >= TOO_MANY_PREFIXES)
189 strvec_clear(&data.prefixes);
190
59e1205d 191 send_possibly_unborn_head(&data);
b3970c70
TB
192 if (!data.prefixes.nr)
193 strvec_push(&data.prefixes, "");
91e2ab15
JK
194 refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
195 get_git_namespace(), data.prefixes.v,
b269ac53 196 NULL, send_ref, &data);
70afef5c 197 packet_fflush(stdout);
ef8d7ac4 198 strvec_clear(&data.prefixes);
f54b9f21 199 strbuf_release(&data.buf);
9b67eb6f 200 string_list_clear(&data.hidden_refs, 0);
72d0ea00
BW
201 return 0;
202}
59e1205d
JT
203
204int ls_refs_advertise(struct repository *r, struct strbuf *value)
205{
4b4e75dd 206 if (value && unborn_config(r) == UNBORN_ADVERTISE)
c4716086 207 strbuf_addstr(value, "unborn");
59e1205d
JT
208
209 return 1;
210}