]>
Commit | Line | Data |
---|---|---|
72d0ea00 BW |
1 | #include "cache.h" |
2 | #include "repository.h" | |
3 | #include "refs.h" | |
4 | #include "remote.h" | |
dbbcd44f | 5 | #include "strvec.h" |
72d0ea00 BW |
6 | #include "ls-refs.h" |
7 | #include "pkt-line.h" | |
e20b4192 | 8 | #include "config.h" |
72d0ea00 | 9 | |
59e1205d JT |
10 | static int config_read; |
11 | static int advertise_unborn; | |
12 | static int allow_unborn; | |
13 | ||
14 | static void ensure_config_read(void) | |
15 | { | |
16 | const char *str = NULL; | |
17 | ||
18 | if (config_read) | |
19 | return; | |
20 | ||
21 | if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) { | |
22 | /* | |
23 | * If there is no such config, advertise and allow it by | |
24 | * default. | |
25 | */ | |
26 | advertise_unborn = 1; | |
27 | allow_unborn = 1; | |
28 | } else { | |
29 | if (!strcmp(str, "advertise")) { | |
30 | advertise_unborn = 1; | |
31 | allow_unborn = 1; | |
32 | } else if (!strcmp(str, "allow")) { | |
33 | allow_unborn = 1; | |
34 | } else if (!strcmp(str, "ignore")) { | |
35 | /* do nothing */ | |
36 | } else { | |
1a8aea85 JNA |
37 | die(_("invalid value for '%s': '%s'"), |
38 | "lsrefs.unborn", str); | |
59e1205d JT |
39 | } |
40 | } | |
41 | config_read = 1; | |
42 | } | |
43 | ||
7f0e4f6a JK |
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 | ||
72d0ea00 BW |
50 | /* |
51 | * Check if one of the prefixes is a prefix of the ref. | |
52 | * If no prefixes were provided, all refs match. | |
53 | */ | |
ef8d7ac4 | 54 | static int ref_match(const struct strvec *prefixes, const char *refname) |
72d0ea00 BW |
55 | { |
56 | int i; | |
57 | ||
d70a9eb6 | 58 | if (!prefixes->nr) |
72d0ea00 BW |
59 | return 1; /* no restriction */ |
60 | ||
d70a9eb6 JK |
61 | for (i = 0; i < prefixes->nr; i++) { |
62 | const char *prefix = prefixes->v[i]; | |
72d0ea00 BW |
63 | |
64 | if (starts_with(refname, prefix)) | |
65 | return 1; | |
66 | } | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
71 | struct ls_refs_data { | |
72 | unsigned peel; | |
73 | unsigned symrefs; | |
ef8d7ac4 | 74 | struct strvec prefixes; |
f54b9f21 | 75 | struct strbuf buf; |
59e1205d | 76 | unsigned unborn : 1; |
72d0ea00 BW |
77 | }; |
78 | ||
79 | static 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 | |
e20b4192 JK |
87 | if (ref_is_hidden(refname_nons, refname)) |
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 |
122 | static 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 | ||
e20b4192 JK |
139 | static int ls_refs_config(const char *var, const char *value, void *data) |
140 | { | |
141 | /* | |
142 | * We only serve fetches over v2 for now, so respect only "uploadpack" | |
143 | * config. This may need to eventually be expanded to "receive", but we | |
144 | * don't yet know how that information will be passed to ls-refs. | |
145 | */ | |
146 | return parse_hide_refs_config(var, value, "uploadpack"); | |
147 | } | |
148 | ||
28a592e4 | 149 | int ls_refs(struct repository *r, struct packet_reader *request) |
72d0ea00 BW |
150 | { |
151 | struct ls_refs_data data; | |
152 | ||
153 | memset(&data, 0, sizeof(data)); | |
83befd37 | 154 | strvec_init(&data.prefixes); |
f54b9f21 | 155 | strbuf_init(&data.buf, 0); |
72d0ea00 | 156 | |
59e1205d | 157 | ensure_config_read(); |
e20b4192 JK |
158 | git_config(ls_refs_config, NULL); |
159 | ||
4845b772 | 160 | while (packet_reader_read(request) == PACKET_READ_NORMAL) { |
72d0ea00 BW |
161 | const char *arg = request->line; |
162 | const char *out; | |
163 | ||
164 | if (!strcmp("peel", arg)) | |
165 | data.peel = 1; | |
166 | else if (!strcmp("symrefs", arg)) | |
167 | data.symrefs = 1; | |
7f0e4f6a JK |
168 | else if (skip_prefix(arg, "ref-prefix ", &out)) { |
169 | if (data.prefixes.nr < TOO_MANY_PREFIXES) | |
170 | strvec_push(&data.prefixes, out); | |
171 | } | |
59e1205d JT |
172 | else if (!strcmp("unborn", arg)) |
173 | data.unborn = allow_unborn; | |
ccf09478 JK |
174 | else |
175 | die(_("unexpected line: '%s'"), arg); | |
72d0ea00 BW |
176 | } |
177 | ||
4845b772 JK |
178 | if (request->status != PACKET_READ_FLUSH) |
179 | die(_("expected flush after ls-refs arguments")); | |
180 | ||
7f0e4f6a JK |
181 | /* |
182 | * If we saw too many prefixes, we must avoid using them at all; as | |
183 | * soon as we have any prefix, they are meant to form a comprehensive | |
184 | * list. | |
185 | */ | |
186 | if (data.prefixes.nr >= TOO_MANY_PREFIXES) | |
187 | strvec_clear(&data.prefixes); | |
188 | ||
59e1205d | 189 | send_possibly_unborn_head(&data); |
b3970c70 TB |
190 | if (!data.prefixes.nr) |
191 | strvec_push(&data.prefixes, ""); | |
192 | for_each_fullref_in_prefixes(get_git_namespace(), data.prefixes.v, | |
67985e4e | 193 | send_ref, &data); |
70afef5c | 194 | packet_fflush(stdout); |
ef8d7ac4 | 195 | strvec_clear(&data.prefixes); |
f54b9f21 | 196 | strbuf_release(&data.buf); |
72d0ea00 BW |
197 | return 0; |
198 | } | |
59e1205d JT |
199 | |
200 | int ls_refs_advertise(struct repository *r, struct strbuf *value) | |
201 | { | |
202 | if (value) { | |
203 | ensure_config_read(); | |
204 | if (advertise_unborn) | |
205 | strbuf_addstr(value, "unborn"); | |
206 | } | |
207 | ||
208 | return 1; | |
209 | } |