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