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