]>
Commit | Line | Data |
---|---|---|
c95b7585 KN |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
3 | #include "parse-options.h" | |
4 | #include "refs.h" | |
5 | #include "wildmatch.h" | |
cbd53a21 | 6 | #include "object-store.h" |
109cd76d | 7 | #include "repository.h" |
c95b7585 KN |
8 | #include "commit.h" |
9 | #include "remote.h" | |
10 | #include "color.h" | |
11 | #include "tag.h" | |
12 | #include "quote.h" | |
13 | #include "ref-filter.h" | |
35257aa0 | 14 | #include "revision.h" |
ce592082 | 15 | #include "utf8.h" |
90c00408 KN |
16 | #include "git-compat-util.h" |
17 | #include "version.h" | |
b1d31c89 | 18 | #include "trailer.h" |
d4919bb2 | 19 | #include "wt-status.h" |
a91aca44 | 20 | #include "commit-slab.h" |
819807b3 | 21 | #include "commit-graph.h" |
c95b7585 | 22 | |
6eac70fa KN |
23 | static struct ref_msg { |
24 | const char *gone; | |
25 | const char *ahead; | |
26 | const char *behind; | |
27 | const char *ahead_behind; | |
28 | } msgs = { | |
29 | /* Untranslated plumbing messages: */ | |
30 | "gone", | |
31 | "ahead %d", | |
32 | "behind %d", | |
33 | "ahead %d, behind %d" | |
34 | }; | |
35 | ||
36 | void setup_ref_filter_porcelain_msg(void) | |
37 | { | |
38 | msgs.gone = _("gone"); | |
39 | msgs.ahead = _("ahead %d"); | |
40 | msgs.behind = _("behind %d"); | |
41 | msgs.ahead_behind = _("ahead %d, behind %d"); | |
42 | } | |
c95b7585 KN |
43 | |
44 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; | |
4f3e3b37 | 45 | typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status; |
a8e7e385 | 46 | typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source; |
c95b7585 | 47 | |
5bd881d9 KN |
48 | struct align { |
49 | align_type position; | |
50 | unsigned int width; | |
51 | }; | |
52 | ||
c58492d4 | 53 | struct if_then_else { |
4f3e3b37 KN |
54 | cmp_status cmp_status; |
55 | const char *str; | |
c58492d4 KN |
56 | unsigned int then_atom_seen : 1, |
57 | else_atom_seen : 1, | |
58 | condition_satisfied : 1; | |
59 | }; | |
60 | ||
b180e6fe | 61 | struct refname_atom { |
1a34728e KN |
62 | enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option; |
63 | int lstrip, rstrip; | |
b180e6fe KN |
64 | }; |
65 | ||
aa46a0da OT |
66 | static struct expand_data { |
67 | struct object_id oid; | |
68 | enum object_type type; | |
69 | unsigned long size; | |
70 | off_t disk_size; | |
71 | struct object_id delta_base_oid; | |
72 | void *content; | |
73 | ||
74 | struct object_info info; | |
75 | } oi, oi_deref; | |
76 | ||
50cd83dc KN |
77 | /* |
78 | * An atom is a valid field atom listed below, possibly prefixed with | |
79 | * a "*" to denote deref_tag(). | |
80 | * | |
81 | * We parse given format string and sort specifiers, and make a list | |
82 | * of properties that we need to extract out of objects. ref_array_item | |
83 | * structure will hold an array of values extracted that can be | |
84 | * indexed with the "atom number", which is an index into this | |
85 | * array. | |
86 | */ | |
b072add7 KN |
87 | static struct used_atom { |
88 | const char *name; | |
89 | cmp_type type; | |
a8e7e385 | 90 | info_source source; |
fd935cc7 KN |
91 | union { |
92 | char color[COLOR_MAXLEN]; | |
5bd881d9 | 93 | struct align align; |
7743fcca | 94 | struct { |
cc72385f | 95 | enum { |
9700fae5 | 96 | RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF |
cc72385f | 97 | } option; |
3ba308cb | 98 | struct refname_atom refname; |
cc72385f | 99 | unsigned int nobracket : 1, push : 1, push_remote : 1; |
7743fcca | 100 | } remote_ref; |
452db397 | 101 | struct { |
b1d31c89 | 102 | enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option; |
67a20a00 | 103 | struct process_trailer_options trailer_opts; |
452db397 KN |
104 | unsigned int nlines; |
105 | } contents; | |
4f3e3b37 KN |
106 | struct { |
107 | cmp_status cmp_status; | |
108 | const char *str; | |
109 | } if_then_else; | |
42d0eb05 KN |
110 | struct { |
111 | enum { O_FULL, O_LENGTH, O_SHORT } option; | |
112 | unsigned int length; | |
113 | } objectname; | |
b180e6fe | 114 | struct refname_atom refname; |
613a0e52 | 115 | char *head; |
fd935cc7 | 116 | } u; |
b072add7 | 117 | } *used_atom; |
50cd83dc | 118 | static int used_atom_cnt, need_tagged, need_symref; |
50cd83dc | 119 | |
e2e7a245 OT |
120 | /* |
121 | * Expand string, append it to strbuf *sb, then return error code ret. | |
122 | * Allow to save few lines of code. | |
123 | */ | |
124 | static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...) | |
125 | { | |
126 | va_list ap; | |
127 | va_start(ap, fmt); | |
128 | strbuf_vaddf(sb, fmt, ap); | |
129 | va_end(ap); | |
130 | return ret; | |
131 | } | |
132 | ||
74efea94 OT |
133 | static int color_atom_parser(const struct ref_format *format, struct used_atom *atom, |
134 | const char *color_value, struct strbuf *err) | |
fd935cc7 KN |
135 | { |
136 | if (!color_value) | |
74efea94 | 137 | return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)")); |
fd935cc7 | 138 | if (color_parse(color_value, atom->u.color) < 0) |
74efea94 OT |
139 | return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"), |
140 | color_value); | |
11b087ad JK |
141 | /* |
142 | * We check this after we've parsed the color, which lets us complain | |
143 | * about syntactically bogus color names even if they won't be used. | |
144 | */ | |
145 | if (!want_color(format->use_color)) | |
146 | color_parse("", atom->u.color); | |
74efea94 | 147 | return 0; |
fd935cc7 KN |
148 | } |
149 | ||
74efea94 OT |
150 | static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg, |
151 | const char *name, struct strbuf *err) | |
5339bdad KN |
152 | { |
153 | if (!arg) | |
b180e6fe | 154 | atom->option = R_NORMAL; |
5339bdad | 155 | else if (!strcmp(arg, "short")) |
b180e6fe | 156 | atom->option = R_SHORT; |
44a6b6ce JH |
157 | else if (skip_prefix(arg, "lstrip=", &arg) || |
158 | skip_prefix(arg, "strip=", &arg)) { | |
17938f17 | 159 | atom->option = R_LSTRIP; |
1a0ca5e3 | 160 | if (strtol_i(arg, 10, &atom->lstrip)) |
74efea94 | 161 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg); |
1a34728e KN |
162 | } else if (skip_prefix(arg, "rstrip=", &arg)) { |
163 | atom->option = R_RSTRIP; | |
164 | if (strtol_i(arg, 10, &atom->rstrip)) | |
74efea94 | 165 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg); |
b180e6fe | 166 | } else |
74efea94 OT |
167 | return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg); |
168 | return 0; | |
b180e6fe KN |
169 | } |
170 | ||
74efea94 OT |
171 | static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom, |
172 | const char *arg, struct strbuf *err) | |
5339bdad | 173 | { |
7743fcca KN |
174 | struct string_list params = STRING_LIST_INIT_DUP; |
175 | int i; | |
176 | ||
cc72385f JS |
177 | if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) |
178 | atom->u.remote_ref.push = 1; | |
179 | ||
7743fcca | 180 | if (!arg) { |
3ba308cb | 181 | atom->u.remote_ref.option = RR_REF; |
74efea94 OT |
182 | return refname_atom_parser_internal(&atom->u.remote_ref.refname, |
183 | arg, atom->name, err); | |
7743fcca KN |
184 | } |
185 | ||
186 | atom->u.remote_ref.nobracket = 0; | |
187 | string_list_split(¶ms, arg, ',', -1); | |
188 | ||
189 | for (i = 0; i < params.nr; i++) { | |
190 | const char *s = params.items[i].string; | |
191 | ||
3ba308cb | 192 | if (!strcmp(s, "track")) |
7743fcca KN |
193 | atom->u.remote_ref.option = RR_TRACK; |
194 | else if (!strcmp(s, "trackshort")) | |
195 | atom->u.remote_ref.option = RR_TRACKSHORT; | |
196 | else if (!strcmp(s, "nobracket")) | |
197 | atom->u.remote_ref.nobracket = 1; | |
cc72385f JS |
198 | else if (!strcmp(s, "remotename")) { |
199 | atom->u.remote_ref.option = RR_REMOTE_NAME; | |
200 | atom->u.remote_ref.push_remote = 1; | |
9700fae5 W |
201 | } else if (!strcmp(s, "remoteref")) { |
202 | atom->u.remote_ref.option = RR_REMOTE_REF; | |
203 | atom->u.remote_ref.push_remote = 1; | |
cc72385f | 204 | } else { |
3ba308cb | 205 | atom->u.remote_ref.option = RR_REF; |
74efea94 OT |
206 | if (refname_atom_parser_internal(&atom->u.remote_ref.refname, |
207 | arg, atom->name, err)) { | |
208 | string_list_clear(¶ms, 0); | |
209 | return -1; | |
210 | } | |
3ba308cb | 211 | } |
7743fcca KN |
212 | } |
213 | ||
214 | string_list_clear(¶ms, 0); | |
74efea94 | 215 | return 0; |
5339bdad KN |
216 | } |
217 | ||
aa46a0da OT |
218 | static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom, |
219 | const char *arg, struct strbuf *err) | |
220 | { | |
221 | if (arg) | |
222 | return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments")); | |
223 | if (*atom->name == '*') | |
224 | oi_deref.info.typep = &oi_deref.type; | |
225 | else | |
226 | oi.info.typep = &oi.type; | |
227 | return 0; | |
228 | } | |
229 | ||
230 | static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom, | |
231 | const char *arg, struct strbuf *err) | |
232 | { | |
233 | if (arg) | |
234 | return strbuf_addf_ret(err, -1, _("%%(objectsize) does not take arguments")); | |
235 | if (*atom->name == '*') | |
236 | oi_deref.info.sizep = &oi_deref.size; | |
237 | else | |
238 | oi.info.sizep = &oi.size; | |
239 | return 0; | |
240 | } | |
241 | ||
74efea94 OT |
242 | static int body_atom_parser(const struct ref_format *format, struct used_atom *atom, |
243 | const char *arg, struct strbuf *err) | |
452db397 KN |
244 | { |
245 | if (arg) | |
74efea94 | 246 | return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments")); |
452db397 | 247 | atom->u.contents.option = C_BODY_DEP; |
74efea94 | 248 | return 0; |
452db397 KN |
249 | } |
250 | ||
74efea94 OT |
251 | static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom, |
252 | const char *arg, struct strbuf *err) | |
452db397 KN |
253 | { |
254 | if (arg) | |
74efea94 | 255 | return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments")); |
452db397 | 256 | atom->u.contents.option = C_SUB; |
74efea94 | 257 | return 0; |
452db397 KN |
258 | } |
259 | ||
74efea94 OT |
260 | static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, |
261 | const char *arg, struct strbuf *err) | |
b1d31c89 | 262 | { |
67a20a00 TB |
263 | struct string_list params = STRING_LIST_INIT_DUP; |
264 | int i; | |
265 | ||
266 | if (arg) { | |
267 | string_list_split(¶ms, arg, ',', -1); | |
268 | for (i = 0; i < params.nr; i++) { | |
269 | const char *s = params.items[i].string; | |
270 | if (!strcmp(s, "unfold")) | |
271 | atom->u.contents.trailer_opts.unfold = 1; | |
272 | else if (!strcmp(s, "only")) | |
273 | atom->u.contents.trailer_opts.only_trailers = 1; | |
74efea94 OT |
274 | else { |
275 | strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s); | |
276 | string_list_clear(¶ms, 0); | |
277 | return -1; | |
278 | } | |
67a20a00 TB |
279 | } |
280 | } | |
b1d31c89 | 281 | atom->u.contents.option = C_TRAILERS; |
67a20a00 | 282 | string_list_clear(¶ms, 0); |
74efea94 | 283 | return 0; |
b1d31c89 JK |
284 | } |
285 | ||
74efea94 OT |
286 | static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom, |
287 | const char *arg, struct strbuf *err) | |
452db397 KN |
288 | { |
289 | if (!arg) | |
290 | atom->u.contents.option = C_BARE; | |
291 | else if (!strcmp(arg, "body")) | |
292 | atom->u.contents.option = C_BODY; | |
293 | else if (!strcmp(arg, "signature")) | |
294 | atom->u.contents.option = C_SIG; | |
295 | else if (!strcmp(arg, "subject")) | |
296 | atom->u.contents.option = C_SUB; | |
7a5edbdb TB |
297 | else if (skip_prefix(arg, "trailers", &arg)) { |
298 | skip_prefix(arg, ":", &arg); | |
74efea94 OT |
299 | if (trailers_atom_parser(format, atom, *arg ? arg : NULL, err)) |
300 | return -1; | |
7a5edbdb | 301 | } else if (skip_prefix(arg, "lines=", &arg)) { |
452db397 KN |
302 | atom->u.contents.option = C_LINES; |
303 | if (strtoul_ui(arg, 10, &atom->u.contents.nlines)) | |
74efea94 | 304 | return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg); |
452db397 | 305 | } else |
74efea94 OT |
306 | return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg); |
307 | return 0; | |
452db397 KN |
308 | } |
309 | ||
74efea94 OT |
310 | static int objectname_atom_parser(const struct ref_format *format, struct used_atom *atom, |
311 | const char *arg, struct strbuf *err) | |
fe63c4d1 KN |
312 | { |
313 | if (!arg) | |
42d0eb05 | 314 | atom->u.objectname.option = O_FULL; |
fe63c4d1 | 315 | else if (!strcmp(arg, "short")) |
42d0eb05 KN |
316 | atom->u.objectname.option = O_SHORT; |
317 | else if (skip_prefix(arg, "short=", &arg)) { | |
318 | atom->u.objectname.option = O_LENGTH; | |
319 | if (strtoul_ui(arg, 10, &atom->u.objectname.length) || | |
320 | atom->u.objectname.length == 0) | |
74efea94 | 321 | return strbuf_addf_ret(err, -1, _("positive value expected objectname:short=%s"), arg); |
42d0eb05 KN |
322 | if (atom->u.objectname.length < MINIMUM_ABBREV) |
323 | atom->u.objectname.length = MINIMUM_ABBREV; | |
324 | } else | |
74efea94 OT |
325 | return strbuf_addf_ret(err, -1, _("unrecognized %%(objectname) argument: %s"), arg); |
326 | return 0; | |
fe63c4d1 KN |
327 | } |
328 | ||
74efea94 OT |
329 | static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom, |
330 | const char *arg, struct strbuf *err) | |
a7984101 | 331 | { |
74efea94 | 332 | return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); |
a7984101 KN |
333 | } |
334 | ||
25a8d79e KN |
335 | static align_type parse_align_position(const char *s) |
336 | { | |
337 | if (!strcmp(s, "right")) | |
338 | return ALIGN_RIGHT; | |
339 | else if (!strcmp(s, "middle")) | |
340 | return ALIGN_MIDDLE; | |
341 | else if (!strcmp(s, "left")) | |
342 | return ALIGN_LEFT; | |
343 | return -1; | |
344 | } | |
345 | ||
74efea94 OT |
346 | static int align_atom_parser(const struct ref_format *format, struct used_atom *atom, |
347 | const char *arg, struct strbuf *err) | |
5bd881d9 KN |
348 | { |
349 | struct align *align = &atom->u.align; | |
350 | struct string_list params = STRING_LIST_INIT_DUP; | |
351 | int i; | |
352 | unsigned int width = ~0U; | |
353 | ||
354 | if (!arg) | |
74efea94 | 355 | return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)")); |
5bd881d9 KN |
356 | |
357 | align->position = ALIGN_LEFT; | |
358 | ||
359 | string_list_split(¶ms, arg, ',', -1); | |
360 | for (i = 0; i < params.nr; i++) { | |
361 | const char *s = params.items[i].string; | |
362 | int position; | |
363 | ||
395fb8f9 KN |
364 | if (skip_prefix(s, "position=", &s)) { |
365 | position = parse_align_position(s); | |
74efea94 OT |
366 | if (position < 0) { |
367 | strbuf_addf(err, _("unrecognized position:%s"), s); | |
368 | string_list_clear(¶ms, 0); | |
369 | return -1; | |
370 | } | |
395fb8f9 KN |
371 | align->position = position; |
372 | } else if (skip_prefix(s, "width=", &s)) { | |
74efea94 OT |
373 | if (strtoul_ui(s, 10, &width)) { |
374 | strbuf_addf(err, _("unrecognized width:%s"), s); | |
375 | string_list_clear(¶ms, 0); | |
376 | return -1; | |
377 | } | |
395fb8f9 | 378 | } else if (!strtoul_ui(s, 10, &width)) |
5bd881d9 KN |
379 | ; |
380 | else if ((position = parse_align_position(s)) >= 0) | |
381 | align->position = position; | |
74efea94 OT |
382 | else { |
383 | strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s); | |
384 | string_list_clear(¶ms, 0); | |
385 | return -1; | |
386 | } | |
5bd881d9 KN |
387 | } |
388 | ||
74efea94 OT |
389 | if (width == ~0U) { |
390 | string_list_clear(¶ms, 0); | |
391 | return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom")); | |
392 | } | |
5bd881d9 KN |
393 | align->width = width; |
394 | string_list_clear(¶ms, 0); | |
74efea94 | 395 | return 0; |
5bd881d9 KN |
396 | } |
397 | ||
74efea94 OT |
398 | static int if_atom_parser(const struct ref_format *format, struct used_atom *atom, |
399 | const char *arg, struct strbuf *err) | |
4f3e3b37 KN |
400 | { |
401 | if (!arg) { | |
402 | atom->u.if_then_else.cmp_status = COMPARE_NONE; | |
74efea94 | 403 | return 0; |
4f3e3b37 KN |
404 | } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) { |
405 | atom->u.if_then_else.cmp_status = COMPARE_EQUAL; | |
406 | } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) { | |
407 | atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL; | |
74efea94 OT |
408 | } else |
409 | return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg); | |
410 | return 0; | |
4f3e3b37 KN |
411 | } |
412 | ||
74efea94 OT |
413 | static int head_atom_parser(const struct ref_format *format, struct used_atom *atom, |
414 | const char *arg, struct strbuf *unused_err) | |
613a0e52 | 415 | { |
efbd4fdf | 416 | atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); |
74efea94 | 417 | return 0; |
613a0e52 | 418 | } |
4f3e3b37 | 419 | |
c95b7585 KN |
420 | static struct { |
421 | const char *name; | |
a8e7e385 | 422 | info_source source; |
c95b7585 | 423 | cmp_type cmp_type; |
74efea94 OT |
424 | int (*parser)(const struct ref_format *format, struct used_atom *atom, |
425 | const char *arg, struct strbuf *err); | |
c95b7585 | 426 | } valid_atom[] = { |
a8e7e385 | 427 | { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
aa46a0da OT |
428 | { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser }, |
429 | { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser }, | |
a8e7e385 OT |
430 | { "objectname", SOURCE_OTHER, FIELD_STR, objectname_atom_parser }, |
431 | { "tree", SOURCE_OBJ }, | |
432 | { "parent", SOURCE_OBJ }, | |
433 | { "numparent", SOURCE_OBJ, FIELD_ULONG }, | |
434 | { "object", SOURCE_OBJ }, | |
435 | { "type", SOURCE_OBJ }, | |
436 | { "tag", SOURCE_OBJ }, | |
437 | { "author", SOURCE_OBJ }, | |
438 | { "authorname", SOURCE_OBJ }, | |
439 | { "authoremail", SOURCE_OBJ }, | |
440 | { "authordate", SOURCE_OBJ, FIELD_TIME }, | |
441 | { "committer", SOURCE_OBJ }, | |
442 | { "committername", SOURCE_OBJ }, | |
443 | { "committeremail", SOURCE_OBJ }, | |
444 | { "committerdate", SOURCE_OBJ, FIELD_TIME }, | |
445 | { "tagger", SOURCE_OBJ }, | |
446 | { "taggername", SOURCE_OBJ }, | |
447 | { "taggeremail", SOURCE_OBJ }, | |
448 | { "taggerdate", SOURCE_OBJ, FIELD_TIME }, | |
449 | { "creator", SOURCE_OBJ }, | |
450 | { "creatordate", SOURCE_OBJ, FIELD_TIME }, | |
451 | { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser }, | |
452 | { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser }, | |
453 | { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser }, | |
454 | { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser }, | |
455 | { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, | |
456 | { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, | |
457 | { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, | |
458 | { "flag", SOURCE_NONE }, | |
459 | { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, | |
460 | { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, | |
461 | { "align", SOURCE_NONE, FIELD_STR, align_atom_parser }, | |
462 | { "end", SOURCE_NONE }, | |
463 | { "if", SOURCE_NONE, FIELD_STR, if_atom_parser }, | |
464 | { "then", SOURCE_NONE }, | |
465 | { "else", SOURCE_NONE }, | |
c95b7585 KN |
466 | }; |
467 | ||
574e96a2 KN |
468 | #define REF_FORMATTING_STATE_INIT { 0, NULL } |
469 | ||
470 | struct ref_formatting_stack { | |
471 | struct ref_formatting_stack *prev; | |
472 | struct strbuf output; | |
c58492d4 | 473 | void (*at_end)(struct ref_formatting_stack **stack); |
ce592082 | 474 | void *at_end_data; |
574e96a2 KN |
475 | }; |
476 | ||
477 | struct ref_formatting_state { | |
478 | int quote_style; | |
479 | struct ref_formatting_stack *stack; | |
480 | }; | |
481 | ||
3a25761a KN |
482 | struct atom_value { |
483 | const char *s; | |
3fc8439c OT |
484 | int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state, |
485 | struct strbuf *err); | |
e467dc14 | 486 | uintmax_t value; /* used for sorting when not FIELD_STR */ |
c58fc856 | 487 | struct used_atom *atom; |
3a25761a KN |
488 | }; |
489 | ||
c95b7585 KN |
490 | /* |
491 | * Used to parse format string and sort specifiers | |
492 | */ | |
ab7ded34 | 493 | static int parse_ref_filter_atom(const struct ref_format *format, |
e6ff7b3b OT |
494 | const char *atom, const char *ep, |
495 | struct strbuf *err) | |
c95b7585 KN |
496 | { |
497 | const char *sp; | |
4de707ea | 498 | const char *arg; |
e94ce139 | 499 | int i, at, atom_len; |
c95b7585 KN |
500 | |
501 | sp = atom; | |
502 | if (*sp == '*' && sp < ep) | |
503 | sp++; /* deref */ | |
504 | if (ep <= sp) | |
e6ff7b3b OT |
505 | return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), |
506 | (int)(ep-atom), atom); | |
c95b7585 KN |
507 | |
508 | /* Do we have the atom already used elsewhere? */ | |
509 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 KN |
510 | int len = strlen(used_atom[i].name); |
511 | if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) | |
c95b7585 KN |
512 | return i; |
513 | } | |
514 | ||
e94ce139 SG |
515 | /* |
516 | * If the atom name has a colon, strip it and everything after | |
517 | * it off - it specifies the format for this entry, and | |
518 | * shouldn't be used for checking against the valid_atom | |
519 | * table. | |
520 | */ | |
521 | arg = memchr(sp, ':', ep - sp); | |
522 | atom_len = (arg ? arg : ep) - sp; | |
523 | ||
c95b7585 KN |
524 | /* Is the atom a valid one? */ |
525 | for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { | |
526 | int len = strlen(valid_atom[i].name); | |
e94ce139 | 527 | if (len == atom_len && !memcmp(valid_atom[i].name, sp, len)) |
c95b7585 KN |
528 | break; |
529 | } | |
530 | ||
531 | if (ARRAY_SIZE(valid_atom) <= i) | |
e6ff7b3b OT |
532 | return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"), |
533 | (int)(ep-atom), atom); | |
c95b7585 KN |
534 | |
535 | /* Add it in, including the deref prefix */ | |
536 | at = used_atom_cnt; | |
537 | used_atom_cnt++; | |
538 | REALLOC_ARRAY(used_atom, used_atom_cnt); | |
b072add7 KN |
539 | used_atom[at].name = xmemdupz(atom, ep - atom); |
540 | used_atom[at].type = valid_atom[i].cmp_type; | |
a8e7e385 | 541 | used_atom[at].source = valid_atom[i].source; |
aa46a0da OT |
542 | if (used_atom[at].source == SOURCE_OBJ) { |
543 | if (*atom == '*') | |
544 | oi_deref.info.contentp = &oi_deref.content; | |
545 | else | |
546 | oi.info.contentp = &oi.content; | |
547 | } | |
bea4dbea | 548 | if (arg) { |
4de707ea | 549 | arg = used_atom[at].name + (arg - atom) + 1; |
bea4dbea TB |
550 | if (!*arg) { |
551 | /* | |
552 | * Treat empty sub-arguments list as NULL (i.e., | |
553 | * "%(atom:)" is equivalent to "%(atom)"). | |
554 | */ | |
555 | arg = NULL; | |
556 | } | |
557 | } | |
fd935cc7 | 558 | memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); |
74efea94 OT |
559 | if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) |
560 | return -1; | |
c95b7585 KN |
561 | if (*atom == '*') |
562 | need_tagged = 1; | |
01f95825 | 563 | if (!strcmp(valid_atom[i].name, "symref")) |
c95b7585 KN |
564 | need_symref = 1; |
565 | return at; | |
566 | } | |
567 | ||
63d89fbc KN |
568 | static void quote_formatting(struct strbuf *s, const char *str, int quote_style) |
569 | { | |
570 | switch (quote_style) { | |
571 | case QUOTE_NONE: | |
572 | strbuf_addstr(s, str); | |
573 | break; | |
574 | case QUOTE_SHELL: | |
575 | sq_quote_buf(s, str); | |
576 | break; | |
577 | case QUOTE_PERL: | |
578 | perl_quote_buf(s, str); | |
579 | break; | |
580 | case QUOTE_PYTHON: | |
581 | python_quote_buf(s, str); | |
582 | break; | |
583 | case QUOTE_TCL: | |
584 | tcl_quote_buf(s, str); | |
585 | break; | |
586 | } | |
587 | } | |
588 | ||
3fc8439c OT |
589 | static int append_atom(struct atom_value *v, struct ref_formatting_state *state, |
590 | struct strbuf *unused_err) | |
63d89fbc | 591 | { |
ce592082 KN |
592 | /* |
593 | * Quote formatting is only done when the stack has a single | |
594 | * element. Otherwise quote formatting is done on the | |
595 | * element's entire output strbuf when the %(end) atom is | |
596 | * encountered. | |
597 | */ | |
598 | if (!state->stack->prev) | |
599 | quote_formatting(&state->stack->output, v->s, state->quote_style); | |
600 | else | |
601 | strbuf_addstr(&state->stack->output, v->s); | |
3fc8439c | 602 | return 0; |
63d89fbc KN |
603 | } |
604 | ||
574e96a2 KN |
605 | static void push_stack_element(struct ref_formatting_stack **stack) |
606 | { | |
607 | struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack)); | |
608 | ||
609 | strbuf_init(&s->output, 0); | |
610 | s->prev = *stack; | |
611 | *stack = s; | |
612 | } | |
613 | ||
614 | static void pop_stack_element(struct ref_formatting_stack **stack) | |
615 | { | |
616 | struct ref_formatting_stack *current = *stack; | |
617 | struct ref_formatting_stack *prev = current->prev; | |
618 | ||
619 | if (prev) | |
620 | strbuf_addbuf(&prev->output, ¤t->output); | |
621 | strbuf_release(¤t->output); | |
622 | free(current); | |
623 | *stack = prev; | |
624 | } | |
625 | ||
c58492d4 | 626 | static void end_align_handler(struct ref_formatting_stack **stack) |
ce592082 | 627 | { |
c58492d4 KN |
628 | struct ref_formatting_stack *cur = *stack; |
629 | struct align *align = (struct align *)cur->at_end_data; | |
ce592082 KN |
630 | struct strbuf s = STRBUF_INIT; |
631 | ||
c58492d4 KN |
632 | strbuf_utf8_align(&s, align->position, align->width, cur->output.buf); |
633 | strbuf_swap(&cur->output, &s); | |
ce592082 KN |
634 | strbuf_release(&s); |
635 | } | |
636 | ||
3fc8439c OT |
637 | static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
638 | struct strbuf *unused_err) | |
ce592082 | 639 | { |
1472b5bf | 640 | struct ref_formatting_stack *new_stack; |
ce592082 KN |
641 | |
642 | push_stack_element(&state->stack); | |
1472b5bf BW |
643 | new_stack = state->stack; |
644 | new_stack->at_end = end_align_handler; | |
645 | new_stack->at_end_data = &atomv->atom->u.align; | |
3fc8439c | 646 | return 0; |
ce592082 KN |
647 | } |
648 | ||
c58492d4 KN |
649 | static void if_then_else_handler(struct ref_formatting_stack **stack) |
650 | { | |
651 | struct ref_formatting_stack *cur = *stack; | |
652 | struct ref_formatting_stack *prev = cur->prev; | |
653 | struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data; | |
654 | ||
655 | if (!if_then_else->then_atom_seen) | |
656 | die(_("format: %%(if) atom used without a %%(then) atom")); | |
657 | ||
658 | if (if_then_else->else_atom_seen) { | |
659 | /* | |
660 | * There is an %(else) atom: we need to drop one state from the | |
661 | * stack, either the %(else) branch if the condition is satisfied, or | |
662 | * the %(then) branch if it isn't. | |
663 | */ | |
664 | if (if_then_else->condition_satisfied) { | |
665 | strbuf_reset(&cur->output); | |
666 | pop_stack_element(&cur); | |
667 | } else { | |
668 | strbuf_swap(&cur->output, &prev->output); | |
669 | strbuf_reset(&cur->output); | |
670 | pop_stack_element(&cur); | |
671 | } | |
672 | } else if (!if_then_else->condition_satisfied) { | |
673 | /* | |
674 | * No %(else) atom: just drop the %(then) branch if the | |
675 | * condition is not satisfied. | |
676 | */ | |
677 | strbuf_reset(&cur->output); | |
678 | } | |
679 | ||
680 | *stack = cur; | |
681 | free(if_then_else); | |
682 | } | |
683 | ||
3fc8439c OT |
684 | static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
685 | struct strbuf *unused_err) | |
c58492d4 | 686 | { |
1472b5bf | 687 | struct ref_formatting_stack *new_stack; |
c58492d4 KN |
688 | struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1); |
689 | ||
4f3e3b37 KN |
690 | if_then_else->str = atomv->atom->u.if_then_else.str; |
691 | if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status; | |
692 | ||
c58492d4 | 693 | push_stack_element(&state->stack); |
1472b5bf BW |
694 | new_stack = state->stack; |
695 | new_stack->at_end = if_then_else_handler; | |
696 | new_stack->at_end_data = if_then_else; | |
3fc8439c | 697 | return 0; |
c58492d4 KN |
698 | } |
699 | ||
700 | static int is_empty(const char *s) | |
701 | { | |
702 | while (*s != '\0') { | |
703 | if (!isspace(*s)) | |
704 | return 0; | |
705 | s++; | |
706 | } | |
707 | return 1; | |
708 | } | |
709 | ||
3fc8439c OT |
710 | static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
711 | struct strbuf *err) | |
c58492d4 KN |
712 | { |
713 | struct ref_formatting_stack *cur = state->stack; | |
714 | struct if_then_else *if_then_else = NULL; | |
715 | ||
716 | if (cur->at_end == if_then_else_handler) | |
717 | if_then_else = (struct if_then_else *)cur->at_end_data; | |
718 | if (!if_then_else) | |
3fc8439c | 719 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom")); |
c58492d4 | 720 | if (if_then_else->then_atom_seen) |
3fc8439c | 721 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once")); |
c58492d4 | 722 | if (if_then_else->else_atom_seen) |
3fc8439c | 723 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)")); |
c58492d4 KN |
724 | if_then_else->then_atom_seen = 1; |
725 | /* | |
4f3e3b37 KN |
726 | * If the 'equals' or 'notequals' attribute is used then |
727 | * perform the required comparison. If not, only non-empty | |
728 | * strings satisfy the 'if' condition. | |
c58492d4 | 729 | */ |
4f3e3b37 KN |
730 | if (if_then_else->cmp_status == COMPARE_EQUAL) { |
731 | if (!strcmp(if_then_else->str, cur->output.buf)) | |
732 | if_then_else->condition_satisfied = 1; | |
733 | } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) { | |
734 | if (strcmp(if_then_else->str, cur->output.buf)) | |
735 | if_then_else->condition_satisfied = 1; | |
736 | } else if (cur->output.len && !is_empty(cur->output.buf)) | |
c58492d4 KN |
737 | if_then_else->condition_satisfied = 1; |
738 | strbuf_reset(&cur->output); | |
3fc8439c | 739 | return 0; |
c58492d4 KN |
740 | } |
741 | ||
3fc8439c OT |
742 | static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
743 | struct strbuf *err) | |
c58492d4 KN |
744 | { |
745 | struct ref_formatting_stack *prev = state->stack; | |
746 | struct if_then_else *if_then_else = NULL; | |
747 | ||
748 | if (prev->at_end == if_then_else_handler) | |
749 | if_then_else = (struct if_then_else *)prev->at_end_data; | |
750 | if (!if_then_else) | |
3fc8439c | 751 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom")); |
c58492d4 | 752 | if (!if_then_else->then_atom_seen) |
3fc8439c | 753 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom")); |
c58492d4 | 754 | if (if_then_else->else_atom_seen) |
3fc8439c | 755 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once")); |
c58492d4 KN |
756 | if_then_else->else_atom_seen = 1; |
757 | push_stack_element(&state->stack); | |
758 | state->stack->at_end_data = prev->at_end_data; | |
759 | state->stack->at_end = prev->at_end; | |
3fc8439c | 760 | return 0; |
ce592082 KN |
761 | } |
762 | ||
3fc8439c OT |
763 | static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
764 | struct strbuf *err) | |
ce592082 KN |
765 | { |
766 | struct ref_formatting_stack *current = state->stack; | |
767 | struct strbuf s = STRBUF_INIT; | |
768 | ||
769 | if (!current->at_end) | |
3fc8439c | 770 | return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom")); |
c58492d4 KN |
771 | current->at_end(&state->stack); |
772 | ||
773 | /* Stack may have been popped within at_end(), hence reset the current pointer */ | |
774 | current = state->stack; | |
ce592082 KN |
775 | |
776 | /* | |
777 | * Perform quote formatting when the stack element is that of | |
778 | * a supporting atom. If nested then perform quote formatting | |
779 | * only on the topmost supporting atom. | |
780 | */ | |
c58492d4 | 781 | if (!current->prev->prev) { |
ce592082 KN |
782 | quote_formatting(&s, current->output.buf, state->quote_style); |
783 | strbuf_swap(¤t->output, &s); | |
784 | } | |
785 | strbuf_release(&s); | |
786 | pop_stack_element(&state->stack); | |
3fc8439c | 787 | return 0; |
ce592082 KN |
788 | } |
789 | ||
c95b7585 KN |
790 | /* |
791 | * In a format string, find the next occurrence of %(atom). | |
792 | */ | |
793 | static const char *find_next(const char *cp) | |
794 | { | |
795 | while (*cp) { | |
796 | if (*cp == '%') { | |
797 | /* | |
798 | * %( is the start of an atom; | |
799 | * %% is a quoted per-cent. | |
800 | */ | |
801 | if (cp[1] == '(') | |
802 | return cp; | |
803 | else if (cp[1] == '%') | |
804 | cp++; /* skip over two % */ | |
805 | /* otherwise this is a singleton, literal % */ | |
806 | } | |
807 | cp++; | |
808 | } | |
809 | return NULL; | |
810 | } | |
811 | ||
812 | /* | |
813 | * Make sure the format string is well formed, and parse out | |
814 | * the used atoms. | |
815 | */ | |
4a68e36d | 816 | int verify_ref_format(struct ref_format *format) |
c95b7585 KN |
817 | { |
818 | const char *cp, *sp; | |
819 | ||
bf285ae6 | 820 | format->need_color_reset_at_eol = 0; |
4a68e36d | 821 | for (cp = format->format; *cp && (sp = find_next(cp)); ) { |
e6ff7b3b | 822 | struct strbuf err = STRBUF_INIT; |
c95b7585 KN |
823 | const char *color, *ep = strchr(sp, ')'); |
824 | int at; | |
825 | ||
826 | if (!ep) | |
1823c619 | 827 | return error(_("malformed format string %s"), sp); |
c95b7585 | 828 | /* sp points at "%(" and ep points at the closing ")" */ |
e6ff7b3b OT |
829 | at = parse_ref_filter_atom(format, sp + 2, ep, &err); |
830 | if (at < 0) | |
831 | die("%s", err.buf); | |
c95b7585 KN |
832 | cp = ep + 1; |
833 | ||
b072add7 | 834 | if (skip_prefix(used_atom[at].name, "color:", &color)) |
bf285ae6 | 835 | format->need_color_reset_at_eol = !!strcmp(color, "reset"); |
e6ff7b3b | 836 | strbuf_release(&err); |
c95b7585 | 837 | } |
11b087ad JK |
838 | if (format->need_color_reset_at_eol && !want_color(format->use_color)) |
839 | format->need_color_reset_at_eol = 0; | |
c95b7585 KN |
840 | return 0; |
841 | } | |
842 | ||
17769795 | 843 | static int grab_objectname(const char *name, const struct object_id *oid, |
fe63c4d1 | 844 | struct atom_value *v, struct used_atom *atom) |
c95b7585 | 845 | { |
fe63c4d1 | 846 | if (starts_with(name, "objectname")) { |
42d0eb05 | 847 | if (atom->u.objectname.option == O_SHORT) { |
aab9583f | 848 | v->s = xstrdup(find_unique_abbrev(oid, DEFAULT_ABBREV)); |
fe63c4d1 | 849 | return 1; |
42d0eb05 | 850 | } else if (atom->u.objectname.option == O_FULL) { |
17769795 | 851 | v->s = xstrdup(oid_to_hex(oid)); |
fe63c4d1 | 852 | return 1; |
42d0eb05 | 853 | } else if (atom->u.objectname.option == O_LENGTH) { |
aab9583f | 854 | v->s = xstrdup(find_unique_abbrev(oid, atom->u.objectname.length)); |
42d0eb05 | 855 | return 1; |
fe63c4d1 | 856 | } else |
033abf97 | 857 | BUG("unknown %%(objectname) option"); |
c95b7585 KN |
858 | } |
859 | return 0; | |
860 | } | |
861 | ||
862 | /* See grab_values */ | |
aa46a0da | 863 | static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) |
c95b7585 KN |
864 | { |
865 | int i; | |
866 | ||
867 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 868 | const char *name = used_atom[i].name; |
c95b7585 KN |
869 | struct atom_value *v = &val[i]; |
870 | if (!!deref != (*name == '*')) | |
871 | continue; | |
872 | if (deref) | |
873 | name++; | |
874 | if (!strcmp(name, "objecttype")) | |
aa46a0da | 875 | v->s = type_name(oi->type); |
c95b7585 | 876 | else if (!strcmp(name, "objectsize")) { |
aa46a0da OT |
877 | v->value = oi->size; |
878 | v->s = xstrfmt("%lu", oi->size); | |
c95b7585 KN |
879 | } |
880 | else if (deref) | |
aa46a0da | 881 | grab_objectname(name, &oi->oid, v, &used_atom[i]); |
c95b7585 KN |
882 | } |
883 | } | |
884 | ||
885 | /* See grab_values */ | |
886 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
887 | { | |
888 | int i; | |
889 | struct tag *tag = (struct tag *) obj; | |
890 | ||
891 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 892 | const char *name = used_atom[i].name; |
c95b7585 KN |
893 | struct atom_value *v = &val[i]; |
894 | if (!!deref != (*name == '*')) | |
895 | continue; | |
896 | if (deref) | |
897 | name++; | |
898 | if (!strcmp(name, "tag")) | |
899 | v->s = tag->tag; | |
900 | else if (!strcmp(name, "type") && tag->tagged) | |
debca9d2 | 901 | v->s = type_name(tag->tagged->type); |
a5e03bf5 | 902 | else if (!strcmp(name, "object") && tag->tagged) |
f2fd0760 | 903 | v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); |
c95b7585 KN |
904 | } |
905 | } | |
906 | ||
907 | /* See grab_values */ | |
908 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
909 | { | |
910 | int i; | |
911 | struct commit *commit = (struct commit *) obj; | |
912 | ||
913 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 914 | const char *name = used_atom[i].name; |
c95b7585 KN |
915 | struct atom_value *v = &val[i]; |
916 | if (!!deref != (*name == '*')) | |
917 | continue; | |
918 | if (deref) | |
919 | name++; | |
920 | if (!strcmp(name, "tree")) { | |
2e27bd77 | 921 | v->s = xstrdup(oid_to_hex(get_commit_tree_oid(commit))); |
c95b7585 | 922 | } |
a5e03bf5 | 923 | else if (!strcmp(name, "numparent")) { |
e467dc14 JS |
924 | v->value = commit_list_count(commit->parents); |
925 | v->s = xstrfmt("%lu", (unsigned long)v->value); | |
c95b7585 KN |
926 | } |
927 | else if (!strcmp(name, "parent")) { | |
c95b7585 | 928 | struct commit_list *parents; |
a5e03bf5 JK |
929 | struct strbuf s = STRBUF_INIT; |
930 | for (parents = commit->parents; parents; parents = parents->next) { | |
c95b7585 | 931 | struct commit *parent = parents->item; |
a5e03bf5 JK |
932 | if (parents != commit->parents) |
933 | strbuf_addch(&s, ' '); | |
f2fd0760 | 934 | strbuf_addstr(&s, oid_to_hex(&parent->object.oid)); |
c95b7585 | 935 | } |
a5e03bf5 | 936 | v->s = strbuf_detach(&s, NULL); |
c95b7585 KN |
937 | } |
938 | } | |
939 | } | |
940 | ||
941 | static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) | |
942 | { | |
943 | const char *eol; | |
944 | while (*buf) { | |
945 | if (!strncmp(buf, who, wholen) && | |
946 | buf[wholen] == ' ') | |
947 | return buf + wholen + 1; | |
948 | eol = strchr(buf, '\n'); | |
949 | if (!eol) | |
950 | return ""; | |
951 | eol++; | |
952 | if (*eol == '\n') | |
953 | return ""; /* end of header */ | |
954 | buf = eol; | |
955 | } | |
956 | return ""; | |
957 | } | |
958 | ||
959 | static const char *copy_line(const char *buf) | |
960 | { | |
961 | const char *eol = strchrnul(buf, '\n'); | |
962 | return xmemdupz(buf, eol - buf); | |
963 | } | |
964 | ||
965 | static const char *copy_name(const char *buf) | |
966 | { | |
967 | const char *cp; | |
968 | for (cp = buf; *cp && *cp != '\n'; cp++) { | |
969 | if (!strncmp(cp, " <", 2)) | |
970 | return xmemdupz(buf, cp - buf); | |
971 | } | |
972 | return ""; | |
973 | } | |
974 | ||
975 | static const char *copy_email(const char *buf) | |
976 | { | |
977 | const char *email = strchr(buf, '<'); | |
978 | const char *eoemail; | |
979 | if (!email) | |
980 | return ""; | |
981 | eoemail = strchr(email, '>'); | |
982 | if (!eoemail) | |
983 | return ""; | |
984 | return xmemdupz(email, eoemail + 1 - email); | |
985 | } | |
986 | ||
987 | static char *copy_subject(const char *buf, unsigned long len) | |
988 | { | |
989 | char *r = xmemdupz(buf, len); | |
990 | int i; | |
991 | ||
992 | for (i = 0; i < len; i++) | |
993 | if (r[i] == '\n') | |
994 | r[i] = ' '; | |
995 | ||
996 | return r; | |
997 | } | |
998 | ||
999 | static void grab_date(const char *buf, struct atom_value *v, const char *atomname) | |
1000 | { | |
1001 | const char *eoemail = strstr(buf, "> "); | |
1002 | char *zone; | |
dddbad72 | 1003 | timestamp_t timestamp; |
c95b7585 | 1004 | long tz; |
d939af12 | 1005 | struct date_mode date_mode = { DATE_NORMAL }; |
c95b7585 KN |
1006 | const char *formatp; |
1007 | ||
1008 | /* | |
1009 | * We got here because atomname ends in "date" or "date<something>"; | |
1010 | * it's not possible that <something> is not ":<format>" because | |
1011 | * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no | |
1012 | * ":" means no format is specified, and use the default. | |
1013 | */ | |
1014 | formatp = strchr(atomname, ':'); | |
1015 | if (formatp != NULL) { | |
1016 | formatp++; | |
d939af12 | 1017 | parse_date_format(formatp, &date_mode); |
c95b7585 KN |
1018 | } |
1019 | ||
1020 | if (!eoemail) | |
1021 | goto bad; | |
1aeb7e75 | 1022 | timestamp = parse_timestamp(eoemail + 2, &zone, 10); |
dddbad72 | 1023 | if (timestamp == TIME_MAX) |
c95b7585 KN |
1024 | goto bad; |
1025 | tz = strtol(zone, NULL, 10); | |
1026 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) | |
1027 | goto bad; | |
d939af12 | 1028 | v->s = xstrdup(show_date(timestamp, tz, &date_mode)); |
e467dc14 | 1029 | v->value = timestamp; |
c95b7585 KN |
1030 | return; |
1031 | bad: | |
1032 | v->s = ""; | |
e467dc14 | 1033 | v->value = 0; |
c95b7585 KN |
1034 | } |
1035 | ||
1036 | /* See grab_values */ | |
1037 | static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
1038 | { | |
1039 | int i; | |
1040 | int wholen = strlen(who); | |
1041 | const char *wholine = NULL; | |
1042 | ||
1043 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1044 | const char *name = used_atom[i].name; |
c95b7585 KN |
1045 | struct atom_value *v = &val[i]; |
1046 | if (!!deref != (*name == '*')) | |
1047 | continue; | |
1048 | if (deref) | |
1049 | name++; | |
1050 | if (strncmp(who, name, wholen)) | |
1051 | continue; | |
1052 | if (name[wholen] != 0 && | |
1053 | strcmp(name + wholen, "name") && | |
1054 | strcmp(name + wholen, "email") && | |
1055 | !starts_with(name + wholen, "date")) | |
1056 | continue; | |
1057 | if (!wholine) | |
1058 | wholine = find_wholine(who, wholen, buf, sz); | |
1059 | if (!wholine) | |
1060 | return; /* no point looking for it */ | |
1061 | if (name[wholen] == 0) | |
1062 | v->s = copy_line(wholine); | |
1063 | else if (!strcmp(name + wholen, "name")) | |
1064 | v->s = copy_name(wholine); | |
1065 | else if (!strcmp(name + wholen, "email")) | |
1066 | v->s = copy_email(wholine); | |
1067 | else if (starts_with(name + wholen, "date")) | |
1068 | grab_date(wholine, v, name); | |
1069 | } | |
1070 | ||
1071 | /* | |
1072 | * For a tag or a commit object, if "creator" or "creatordate" is | |
1073 | * requested, do something special. | |
1074 | */ | |
1075 | if (strcmp(who, "tagger") && strcmp(who, "committer")) | |
1076 | return; /* "author" for commit object is not wanted */ | |
1077 | if (!wholine) | |
1078 | wholine = find_wholine(who, wholen, buf, sz); | |
1079 | if (!wholine) | |
1080 | return; | |
1081 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1082 | const char *name = used_atom[i].name; |
c95b7585 KN |
1083 | struct atom_value *v = &val[i]; |
1084 | if (!!deref != (*name == '*')) | |
1085 | continue; | |
1086 | if (deref) | |
1087 | name++; | |
1088 | ||
1089 | if (starts_with(name, "creatordate")) | |
1090 | grab_date(wholine, v, name); | |
1091 | else if (!strcmp(name, "creator")) | |
1092 | v->s = copy_line(wholine); | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | static void find_subpos(const char *buf, unsigned long sz, | |
1097 | const char **sub, unsigned long *sublen, | |
1098 | const char **body, unsigned long *bodylen, | |
1099 | unsigned long *nonsiglen, | |
1100 | const char **sig, unsigned long *siglen) | |
1101 | { | |
1102 | const char *eol; | |
1103 | /* skip past header until we hit empty line */ | |
1104 | while (*buf && *buf != '\n') { | |
1105 | eol = strchrnul(buf, '\n'); | |
1106 | if (*eol) | |
1107 | eol++; | |
1108 | buf = eol; | |
1109 | } | |
1110 | /* skip any empty lines */ | |
1111 | while (*buf == '\n') | |
1112 | buf++; | |
1113 | ||
1114 | /* parse signature first; we might not even have a subject line */ | |
1115 | *sig = buf + parse_signature(buf, strlen(buf)); | |
1116 | *siglen = strlen(*sig); | |
1117 | ||
1118 | /* subject is first non-empty line */ | |
1119 | *sub = buf; | |
1120 | /* subject goes to first empty line */ | |
1121 | while (buf < *sig && *buf && *buf != '\n') { | |
1122 | eol = strchrnul(buf, '\n'); | |
1123 | if (*eol) | |
1124 | eol++; | |
1125 | buf = eol; | |
1126 | } | |
1127 | *sublen = buf - *sub; | |
1128 | /* drop trailing newline, if present */ | |
1129 | if (*sublen && (*sub)[*sublen - 1] == '\n') | |
1130 | *sublen -= 1; | |
1131 | ||
1132 | /* skip any empty lines */ | |
1133 | while (*buf == '\n') | |
1134 | buf++; | |
1135 | *body = buf; | |
1136 | *bodylen = strlen(buf); | |
1137 | *nonsiglen = *sig - buf; | |
1138 | } | |
1139 | ||
1bb38e5a KN |
1140 | /* |
1141 | * If 'lines' is greater than 0, append that many lines from the given | |
1142 | * 'buf' of length 'size' to the given strbuf. | |
1143 | */ | |
1144 | static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines) | |
1145 | { | |
1146 | int i; | |
1147 | const char *sp, *eol; | |
1148 | size_t len; | |
1149 | ||
1150 | sp = buf; | |
1151 | ||
1152 | for (i = 0; i < lines && sp < buf + size; i++) { | |
1153 | if (i) | |
1154 | strbuf_addstr(out, "\n "); | |
1155 | eol = memchr(sp, '\n', size - (sp - buf)); | |
1156 | len = eol ? eol - sp : size - (sp - buf); | |
1157 | strbuf_add(out, sp, len); | |
1158 | if (!eol) | |
1159 | break; | |
1160 | sp = eol + 1; | |
1161 | } | |
1162 | } | |
1163 | ||
c95b7585 KN |
1164 | /* See grab_values */ |
1165 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
1166 | { | |
1167 | int i; | |
1168 | const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; | |
1169 | unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; | |
1170 | ||
1171 | for (i = 0; i < used_atom_cnt; i++) { | |
452db397 KN |
1172 | struct used_atom *atom = &used_atom[i]; |
1173 | const char *name = atom->name; | |
c95b7585 KN |
1174 | struct atom_value *v = &val[i]; |
1175 | if (!!deref != (*name == '*')) | |
1176 | continue; | |
1177 | if (deref) | |
1178 | name++; | |
1179 | if (strcmp(name, "subject") && | |
1180 | strcmp(name, "body") && | |
67a20a00 | 1181 | !starts_with(name, "trailers") && |
452db397 | 1182 | !starts_with(name, "contents")) |
c95b7585 KN |
1183 | continue; |
1184 | if (!subpos) | |
1185 | find_subpos(buf, sz, | |
1186 | &subpos, &sublen, | |
1187 | &bodypos, &bodylen, &nonsiglen, | |
1188 | &sigpos, &siglen); | |
1189 | ||
452db397 | 1190 | if (atom->u.contents.option == C_SUB) |
c95b7585 | 1191 | v->s = copy_subject(subpos, sublen); |
452db397 | 1192 | else if (atom->u.contents.option == C_BODY_DEP) |
c95b7585 | 1193 | v->s = xmemdupz(bodypos, bodylen); |
452db397 | 1194 | else if (atom->u.contents.option == C_BODY) |
c95b7585 | 1195 | v->s = xmemdupz(bodypos, nonsiglen); |
452db397 | 1196 | else if (atom->u.contents.option == C_SIG) |
c95b7585 | 1197 | v->s = xmemdupz(sigpos, siglen); |
452db397 | 1198 | else if (atom->u.contents.option == C_LINES) { |
1bb38e5a KN |
1199 | struct strbuf s = STRBUF_INIT; |
1200 | const char *contents_end = bodylen + bodypos - siglen; | |
1201 | ||
1bb38e5a | 1202 | /* Size is the length of the message after removing the signature */ |
452db397 | 1203 | append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines); |
1bb38e5a | 1204 | v->s = strbuf_detach(&s, NULL); |
b1d31c89 | 1205 | } else if (atom->u.contents.option == C_TRAILERS) { |
67a20a00 | 1206 | struct strbuf s = STRBUF_INIT; |
b1d31c89 | 1207 | |
67a20a00 TB |
1208 | /* Format the trailer info according to the trailer_opts given */ |
1209 | format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts); | |
1210 | ||
1211 | v->s = strbuf_detach(&s, NULL); | |
452db397 KN |
1212 | } else if (atom->u.contents.option == C_BARE) |
1213 | v->s = xstrdup(subpos); | |
c95b7585 KN |
1214 | } |
1215 | } | |
1216 | ||
1217 | /* | |
1218 | * We want to have empty print-string for field requests | |
1219 | * that do not apply (e.g. "authordate" for a tag object) | |
1220 | */ | |
1221 | static void fill_missing_values(struct atom_value *val) | |
1222 | { | |
1223 | int i; | |
1224 | for (i = 0; i < used_atom_cnt; i++) { | |
1225 | struct atom_value *v = &val[i]; | |
1226 | if (v->s == NULL) | |
1227 | v->s = ""; | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | /* | |
1232 | * val is a list of atom_value to hold returned values. Extract | |
1233 | * the values for atoms in used_atom array out of (obj, buf, sz). | |
1234 | * when deref is false, (obj, buf, sz) is the object that is | |
1235 | * pointed at by the ref itself; otherwise it is the object the | |
1236 | * ref (which is a tag) refers to. | |
1237 | */ | |
1238 | static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) | |
1239 | { | |
c95b7585 KN |
1240 | switch (obj->type) { |
1241 | case OBJ_TAG: | |
1242 | grab_tag_values(val, deref, obj, buf, sz); | |
1243 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
1244 | grab_person("tagger", val, deref, obj, buf, sz); | |
1245 | break; | |
1246 | case OBJ_COMMIT: | |
1247 | grab_commit_values(val, deref, obj, buf, sz); | |
1248 | grab_sub_body_contents(val, deref, obj, buf, sz); | |
1249 | grab_person("author", val, deref, obj, buf, sz); | |
1250 | grab_person("committer", val, deref, obj, buf, sz); | |
1251 | break; | |
1252 | case OBJ_TREE: | |
1253 | /* grab_tree_values(val, deref, obj, buf, sz); */ | |
1254 | break; | |
1255 | case OBJ_BLOB: | |
1256 | /* grab_blob_values(val, deref, obj, buf, sz); */ | |
1257 | break; | |
1258 | default: | |
1259 | die("Eh? Object of type %d?", obj->type); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | static inline char *copy_advance(char *dst, const char *src) | |
1264 | { | |
1265 | while (*src) | |
1266 | *dst++ = *src++; | |
1267 | return dst; | |
1268 | } | |
1269 | ||
1a0ca5e3 | 1270 | static const char *lstrip_ref_components(const char *refname, int len) |
0571979b | 1271 | { |
a7984101 | 1272 | long remaining = len; |
0571979b JK |
1273 | const char *start = refname; |
1274 | ||
1a0ca5e3 KN |
1275 | if (len < 0) { |
1276 | int i; | |
1277 | const char *p = refname; | |
1278 | ||
1279 | /* Find total no of '/' separated path-components */ | |
1280 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) | |
1281 | ; | |
1282 | /* | |
1283 | * The number of components we need to strip is now | |
1284 | * the total minus the components to be left (Plus one | |
1285 | * because we count the number of '/', but the number | |
1286 | * of components is one more than the no of '/'). | |
1287 | */ | |
1288 | remaining = i + len + 1; | |
1289 | } | |
0571979b | 1290 | |
1a0ca5e3 | 1291 | while (remaining > 0) { |
0571979b JK |
1292 | switch (*start++) { |
1293 | case '\0': | |
3a42980f | 1294 | return ""; |
0571979b JK |
1295 | case '/': |
1296 | remaining--; | |
1297 | break; | |
1298 | } | |
1299 | } | |
1a0ca5e3 | 1300 | |
0571979b JK |
1301 | return start; |
1302 | } | |
1303 | ||
1a34728e KN |
1304 | static const char *rstrip_ref_components(const char *refname, int len) |
1305 | { | |
1306 | long remaining = len; | |
1307 | char *start = xstrdup(refname); | |
1308 | ||
1309 | if (len < 0) { | |
1310 | int i; | |
1311 | const char *p = refname; | |
1312 | ||
1313 | /* Find total no of '/' separated path-components */ | |
1314 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) | |
1315 | ; | |
1316 | /* | |
1317 | * The number of components we need to strip is now | |
1318 | * the total minus the components to be left (Plus one | |
1319 | * because we count the number of '/', but the number | |
1320 | * of components is one more than the no of '/'). | |
1321 | */ | |
1322 | remaining = i + len + 1; | |
1323 | } | |
1324 | ||
1325 | while (remaining-- > 0) { | |
1326 | char *p = strrchr(start, '/'); | |
1327 | if (p == NULL) | |
1328 | return ""; | |
1329 | else | |
1330 | p[0] = '\0'; | |
1331 | } | |
1332 | return start; | |
1333 | } | |
1334 | ||
a7984101 KN |
1335 | static const char *show_ref(struct refname_atom *atom, const char *refname) |
1336 | { | |
1337 | if (atom->option == R_SHORT) | |
1338 | return shorten_unambiguous_ref(refname, warn_ambiguous_refs); | |
17938f17 KN |
1339 | else if (atom->option == R_LSTRIP) |
1340 | return lstrip_ref_components(refname, atom->lstrip); | |
1a34728e KN |
1341 | else if (atom->option == R_RSTRIP) |
1342 | return rstrip_ref_components(refname, atom->rstrip); | |
a7984101 KN |
1343 | else |
1344 | return refname; | |
1345 | } | |
1346 | ||
5339bdad KN |
1347 | static void fill_remote_ref_details(struct used_atom *atom, const char *refname, |
1348 | struct branch *branch, const char **s) | |
1349 | { | |
1350 | int num_ours, num_theirs; | |
3ba308cb KN |
1351 | if (atom->u.remote_ref.option == RR_REF) |
1352 | *s = show_ref(&atom->u.remote_ref.refname, refname); | |
7743fcca | 1353 | else if (atom->u.remote_ref.option == RR_TRACK) { |
d7d1b496 JH |
1354 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
1355 | NULL, AHEAD_BEHIND_FULL) < 0) { | |
6eac70fa | 1356 | *s = xstrdup(msgs.gone); |
7743fcca | 1357 | } else if (!num_ours && !num_theirs) |
5339bdad KN |
1358 | *s = ""; |
1359 | else if (!num_ours) | |
6eac70fa | 1360 | *s = xstrfmt(msgs.behind, num_theirs); |
5339bdad | 1361 | else if (!num_theirs) |
6eac70fa | 1362 | *s = xstrfmt(msgs.ahead, num_ours); |
5339bdad | 1363 | else |
6eac70fa | 1364 | *s = xstrfmt(msgs.ahead_behind, |
5339bdad | 1365 | num_ours, num_theirs); |
7743fcca KN |
1366 | if (!atom->u.remote_ref.nobracket && *s[0]) { |
1367 | const char *to_free = *s; | |
1368 | *s = xstrfmt("[%s]", *s); | |
1369 | free((void *)to_free); | |
1370 | } | |
1371 | } else if (atom->u.remote_ref.option == RR_TRACKSHORT) { | |
d7d1b496 JH |
1372 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
1373 | NULL, AHEAD_BEHIND_FULL) < 0) | |
5339bdad KN |
1374 | return; |
1375 | ||
1376 | if (!num_ours && !num_theirs) | |
1377 | *s = "="; | |
1378 | else if (!num_ours) | |
1379 | *s = "<"; | |
1380 | else if (!num_theirs) | |
1381 | *s = ">"; | |
1382 | else | |
1383 | *s = "<>"; | |
cc72385f JS |
1384 | } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { |
1385 | int explicit; | |
1386 | const char *remote = atom->u.remote_ref.push ? | |
1387 | pushremote_for_branch(branch, &explicit) : | |
1388 | remote_for_branch(branch, &explicit); | |
1389 | if (explicit) | |
1390 | *s = xstrdup(remote); | |
1391 | else | |
1392 | *s = ""; | |
9700fae5 W |
1393 | } else if (atom->u.remote_ref.option == RR_REMOTE_REF) { |
1394 | int explicit; | |
1395 | const char *merge; | |
1396 | ||
1397 | merge = remote_ref_for_branch(branch, atom->u.remote_ref.push, | |
1398 | &explicit); | |
1399 | if (explicit) | |
1400 | *s = xstrdup(merge); | |
1401 | else | |
1402 | *s = ""; | |
3ba308cb | 1403 | } else |
033abf97 | 1404 | BUG("unhandled RR_* enum"); |
5339bdad KN |
1405 | } |
1406 | ||
d4919bb2 KN |
1407 | char *get_head_description(void) |
1408 | { | |
1409 | struct strbuf desc = STRBUF_INIT; | |
1410 | struct wt_status_state state; | |
1411 | memset(&state, 0, sizeof(state)); | |
1412 | wt_status_get_state(&state, 1); | |
1413 | if (state.rebase_in_progress || | |
a236f900 KS |
1414 | state.rebase_interactive_in_progress) { |
1415 | if (state.branch) | |
1416 | strbuf_addf(&desc, _("(no branch, rebasing %s)"), | |
1417 | state.branch); | |
1418 | else | |
1419 | strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"), | |
1420 | state.detached_from); | |
1421 | } else if (state.bisect_in_progress) | |
d4919bb2 KN |
1422 | strbuf_addf(&desc, _("(no branch, bisect started on %s)"), |
1423 | state.branch); | |
1424 | else if (state.detached_from) { | |
d4919bb2 | 1425 | if (state.detached_at) |
66f5f6dc ÆAB |
1426 | /* |
1427 | * TRANSLATORS: make sure this matches "HEAD | |
1428 | * detached at " in wt-status.c | |
1429 | */ | |
d4919bb2 KN |
1430 | strbuf_addf(&desc, _("(HEAD detached at %s)"), |
1431 | state.detached_from); | |
1432 | else | |
66f5f6dc ÆAB |
1433 | /* |
1434 | * TRANSLATORS: make sure this matches "HEAD | |
1435 | * detached from " in wt-status.c | |
1436 | */ | |
d4919bb2 KN |
1437 | strbuf_addf(&desc, _("(HEAD detached from %s)"), |
1438 | state.detached_from); | |
1439 | } | |
1440 | else | |
1441 | strbuf_addstr(&desc, _("(no branch)")); | |
1442 | free(state.branch); | |
1443 | free(state.onto); | |
1444 | free(state.detached_from); | |
1445 | return strbuf_detach(&desc, NULL); | |
1446 | } | |
1447 | ||
a7984101 KN |
1448 | static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) |
1449 | { | |
1450 | if (!ref->symref) | |
1451 | return ""; | |
1452 | else | |
1453 | return show_ref(&atom->u.refname, ref->symref); | |
1454 | } | |
1455 | ||
1456 | static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref) | |
1457 | { | |
1458 | if (ref->kind & FILTER_REFS_DETACHED_HEAD) | |
1459 | return get_head_description(); | |
1460 | return show_ref(&atom->u.refname, ref->refname); | |
5339bdad KN |
1461 | } |
1462 | ||
aa46a0da OT |
1463 | static int get_object(struct ref_array_item *ref, int deref, struct object **obj, |
1464 | struct expand_data *oi, struct strbuf *err) | |
2bbc6e8a | 1465 | { |
04f6ee1a OT |
1466 | /* parse_object_buffer() will set eaten to 0 if free() will be needed */ |
1467 | int eaten = 1; | |
aa46a0da OT |
1468 | if (oi->info.contentp) { |
1469 | /* We need to know that to use parse_object_buffer properly */ | |
1470 | oi->info.sizep = &oi->size; | |
1471 | oi->info.typep = &oi->type; | |
1472 | } | |
1473 | if (oid_object_info_extended(the_repository, &oi->oid, &oi->info, | |
1474 | OBJECT_INFO_LOOKUP_REPLACE)) | |
1475 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), | |
1476 | oid_to_hex(&oi->oid), ref->refname); | |
1477 | ||
1478 | if (oi->info.contentp) { | |
c83149ac | 1479 | *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten); |
aa46a0da OT |
1480 | if (!obj) { |
1481 | if (!eaten) | |
1482 | free(oi->content); | |
1483 | return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"), | |
1484 | oid_to_hex(&oi->oid), ref->refname); | |
1485 | } | |
1486 | grab_values(ref->value, deref, *obj, oi->content, oi->size); | |
e2255179 | 1487 | } |
aa46a0da OT |
1488 | |
1489 | grab_common_values(ref->value, deref, oi); | |
2bbc6e8a | 1490 | if (!eaten) |
aa46a0da OT |
1491 | free(oi->content); |
1492 | return 0; | |
2bbc6e8a OT |
1493 | } |
1494 | ||
c95b7585 KN |
1495 | /* |
1496 | * Parse the object referred by ref, and grab needed value. | |
1497 | */ | |
e339611b | 1498 | static int populate_value(struct ref_array_item *ref, struct strbuf *err) |
c95b7585 | 1499 | { |
c95b7585 | 1500 | struct object *obj; |
2bbc6e8a | 1501 | int i; |
aa46a0da | 1502 | struct object_info empty = OBJECT_INFO_INIT; |
c95b7585 KN |
1503 | |
1504 | ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value)); | |
1505 | ||
1506 | if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { | |
c95b7585 | 1507 | ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, |
efbd4fdf | 1508 | NULL, NULL); |
c95b7585 KN |
1509 | if (!ref->symref) |
1510 | ref->symref = ""; | |
1511 | } | |
1512 | ||
1513 | /* Fill in specials first */ | |
1514 | for (i = 0; i < used_atom_cnt; i++) { | |
fd935cc7 | 1515 | struct used_atom *atom = &used_atom[i]; |
b072add7 | 1516 | const char *name = used_atom[i].name; |
c95b7585 KN |
1517 | struct atom_value *v = &ref->value[i]; |
1518 | int deref = 0; | |
1519 | const char *refname; | |
c95b7585 KN |
1520 | struct branch *branch = NULL; |
1521 | ||
63d89fbc | 1522 | v->handler = append_atom; |
c58fc856 | 1523 | v->atom = atom; |
63d89fbc | 1524 | |
c95b7585 KN |
1525 | if (*name == '*') { |
1526 | deref = 1; | |
1527 | name++; | |
1528 | } | |
1529 | ||
1530 | if (starts_with(name, "refname")) | |
a7984101 | 1531 | refname = get_refname(atom, ref); |
c95b7585 | 1532 | else if (starts_with(name, "symref")) |
a7984101 | 1533 | refname = get_symref(atom, ref); |
c95b7585 KN |
1534 | else if (starts_with(name, "upstream")) { |
1535 | const char *branch_name; | |
20a9c156 | 1536 | v->s = ""; |
c95b7585 KN |
1537 | /* only local branches may have an upstream */ |
1538 | if (!skip_prefix(ref->refname, "refs/heads/", | |
1539 | &branch_name)) | |
1540 | continue; | |
1541 | branch = branch_get(branch_name); | |
1542 | ||
1543 | refname = branch_get_upstream(branch, NULL); | |
5339bdad KN |
1544 | if (refname) |
1545 | fill_remote_ref_details(atom, refname, branch, &v->s); | |
1546 | continue; | |
cc72385f | 1547 | } else if (atom->u.remote_ref.push) { |
c95b7585 | 1548 | const char *branch_name; |
20a9c156 | 1549 | v->s = ""; |
c95b7585 KN |
1550 | if (!skip_prefix(ref->refname, "refs/heads/", |
1551 | &branch_name)) | |
1552 | continue; | |
1553 | branch = branch_get(branch_name); | |
1554 | ||
cc72385f JS |
1555 | if (atom->u.remote_ref.push_remote) |
1556 | refname = NULL; | |
1557 | else { | |
1558 | refname = branch_get_push(branch, NULL); | |
1559 | if (!refname) | |
1560 | continue; | |
1561 | } | |
5339bdad KN |
1562 | fill_remote_ref_details(atom, refname, branch, &v->s); |
1563 | continue; | |
fd935cc7 KN |
1564 | } else if (starts_with(name, "color:")) { |
1565 | v->s = atom->u.color; | |
c95b7585 KN |
1566 | continue; |
1567 | } else if (!strcmp(name, "flag")) { | |
1568 | char buf[256], *cp = buf; | |
1569 | if (ref->flag & REF_ISSYMREF) | |
1570 | cp = copy_advance(cp, ",symref"); | |
1571 | if (ref->flag & REF_ISPACKED) | |
1572 | cp = copy_advance(cp, ",packed"); | |
1573 | if (cp == buf) | |
1574 | v->s = ""; | |
1575 | else { | |
1576 | *cp = '\0'; | |
1577 | v->s = xstrdup(buf + 1); | |
1578 | } | |
1579 | continue; | |
17769795 | 1580 | } else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) { |
c95b7585 KN |
1581 | continue; |
1582 | } else if (!strcmp(name, "HEAD")) { | |
613a0e52 | 1583 | if (atom->u.head && !strcmp(ref->refname, atom->u.head)) |
c95b7585 KN |
1584 | v->s = "*"; |
1585 | else | |
1586 | v->s = " "; | |
1587 | continue; | |
5bd881d9 | 1588 | } else if (starts_with(name, "align")) { |
ce592082 | 1589 | v->handler = align_atom_handler; |
20a9c156 | 1590 | v->s = ""; |
ce592082 KN |
1591 | continue; |
1592 | } else if (!strcmp(name, "end")) { | |
1593 | v->handler = end_atom_handler; | |
20a9c156 | 1594 | v->s = ""; |
ce592082 | 1595 | continue; |
4f3e3b37 KN |
1596 | } else if (starts_with(name, "if")) { |
1597 | const char *s; | |
20a9c156 | 1598 | v->s = ""; |
4f3e3b37 KN |
1599 | if (skip_prefix(name, "if:", &s)) |
1600 | v->s = xstrdup(s); | |
c58492d4 KN |
1601 | v->handler = if_atom_handler; |
1602 | continue; | |
1603 | } else if (!strcmp(name, "then")) { | |
1604 | v->handler = then_atom_handler; | |
20a9c156 | 1605 | v->s = ""; |
c58492d4 KN |
1606 | continue; |
1607 | } else if (!strcmp(name, "else")) { | |
1608 | v->handler = else_atom_handler; | |
20a9c156 | 1609 | v->s = ""; |
c58492d4 | 1610 | continue; |
c95b7585 KN |
1611 | } else |
1612 | continue; | |
1613 | ||
c95b7585 KN |
1614 | if (!deref) |
1615 | v->s = refname; | |
a5e03bf5 JK |
1616 | else |
1617 | v->s = xstrfmt("%s^{}", refname); | |
c95b7585 KN |
1618 | } |
1619 | ||
1620 | for (i = 0; i < used_atom_cnt; i++) { | |
1621 | struct atom_value *v = &ref->value[i]; | |
aa46a0da OT |
1622 | if (v->s == NULL && used_atom[i].source == SOURCE_NONE) |
1623 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), | |
1624 | oid_to_hex(&ref->objectname), ref->refname); | |
c95b7585 | 1625 | } |
aa46a0da OT |
1626 | |
1627 | if (need_tagged) | |
1628 | oi.info.contentp = &oi.content; | |
1629 | if (!memcmp(&oi.info, &empty, sizeof(empty)) && | |
1630 | !memcmp(&oi_deref.info, &empty, sizeof(empty))) | |
e339611b | 1631 | return 0; |
c95b7585 | 1632 | |
aa46a0da OT |
1633 | |
1634 | oi.oid = ref->objectname; | |
1635 | if (get_object(ref, 0, &obj, &oi, err)) | |
e339611b | 1636 | return -1; |
c95b7585 KN |
1637 | |
1638 | /* | |
1639 | * If there is no atom that wants to know about tagged | |
1640 | * object, we are done. | |
1641 | */ | |
1642 | if (!need_tagged || (obj->type != OBJ_TAG)) | |
e339611b | 1643 | return 0; |
c95b7585 KN |
1644 | |
1645 | /* | |
1646 | * If it is a tag object, see if we use a value that derefs | |
1647 | * the object, and if we do grab the object it refers to. | |
1648 | */ | |
aa46a0da | 1649 | oi_deref.oid = ((struct tag *)obj)->tagged->oid; |
c95b7585 KN |
1650 | |
1651 | /* | |
1652 | * NEEDSWORK: This derefs tag only once, which | |
1653 | * is good to deal with chains of trust, but | |
1654 | * is not consistent with what deref_tag() does | |
1655 | * which peels the onion to the core. | |
1656 | */ | |
aa46a0da | 1657 | return get_object(ref, 1, &obj, &oi_deref, err); |
c95b7585 KN |
1658 | } |
1659 | ||
1660 | /* | |
1661 | * Given a ref, return the value for the atom. This lazily gets value | |
1662 | * out of the object by calling populate value. | |
1663 | */ | |
e339611b OT |
1664 | static int get_ref_atom_value(struct ref_array_item *ref, int atom, |
1665 | struct atom_value **v, struct strbuf *err) | |
c95b7585 KN |
1666 | { |
1667 | if (!ref->value) { | |
e339611b OT |
1668 | if (populate_value(ref, err)) |
1669 | return -1; | |
c95b7585 KN |
1670 | fill_missing_values(ref->value); |
1671 | } | |
1672 | *v = &ref->value[atom]; | |
e339611b | 1673 | return 0; |
c95b7585 KN |
1674 | } |
1675 | ||
a91aca44 JK |
1676 | /* |
1677 | * Unknown has to be "0" here, because that's the default value for | |
1678 | * contains_cache slab entries that have not yet been assigned. | |
1679 | */ | |
ee2bd06b | 1680 | enum contains_result { |
a91aca44 JK |
1681 | CONTAINS_UNKNOWN = 0, |
1682 | CONTAINS_NO, | |
1683 | CONTAINS_YES | |
ee2bd06b KN |
1684 | }; |
1685 | ||
a91aca44 JK |
1686 | define_commit_slab(contains_cache, enum contains_result); |
1687 | ||
4d4bc414 JK |
1688 | struct ref_filter_cbdata { |
1689 | struct ref_array *array; | |
1690 | struct ref_filter *filter; | |
a91aca44 | 1691 | struct contains_cache contains_cache; |
ac3f5a34 | 1692 | struct contains_cache no_contains_cache; |
ee2bd06b KN |
1693 | }; |
1694 | ||
1695 | /* | |
1696 | * Mimicking the real stack, this stack lives on the heap, avoiding stack | |
1697 | * overflows. | |
1698 | * | |
1699 | * At each recursion step, the stack items points to the commits whose | |
1700 | * ancestors are to be inspected. | |
1701 | */ | |
1702 | struct contains_stack { | |
1703 | int nr, alloc; | |
1704 | struct contains_stack_entry { | |
1705 | struct commit *commit; | |
1706 | struct commit_list *parents; | |
1707 | } *contains_stack; | |
1708 | }; | |
1709 | ||
1710 | static int in_commit_list(const struct commit_list *want, struct commit *c) | |
1711 | { | |
1712 | for (; want; want = want->next) | |
f2fd0760 | 1713 | if (!oidcmp(&want->item->object.oid, &c->object.oid)) |
ee2bd06b KN |
1714 | return 1; |
1715 | return 0; | |
1716 | } | |
1717 | ||
1718 | /* | |
8fb572af | 1719 | * Test whether the candidate is contained in the list. |
ee2bd06b KN |
1720 | * Do not recurse to find out, though, but return -1 if inconclusive. |
1721 | */ | |
1722 | static enum contains_result contains_test(struct commit *candidate, | |
a91aca44 | 1723 | const struct commit_list *want, |
819807b3 DS |
1724 | struct contains_cache *cache, |
1725 | uint32_t cutoff) | |
ee2bd06b | 1726 | { |
a91aca44 JK |
1727 | enum contains_result *cached = contains_cache_at(cache, candidate); |
1728 | ||
1729 | /* If we already have the answer cached, return that. */ | |
1730 | if (*cached) | |
1731 | return *cached; | |
1732 | ||
ee2bd06b KN |
1733 | /* or are we it? */ |
1734 | if (in_commit_list(want, candidate)) { | |
a91aca44 | 1735 | *cached = CONTAINS_YES; |
a0262c51 | 1736 | return CONTAINS_YES; |
ee2bd06b KN |
1737 | } |
1738 | ||
a91aca44 | 1739 | /* Otherwise, we don't know; prepare to recurse */ |
d344d1cb | 1740 | parse_commit_or_die(candidate); |
819807b3 DS |
1741 | |
1742 | if (candidate->generation < cutoff) | |
1743 | return CONTAINS_NO; | |
1744 | ||
a0262c51 | 1745 | return CONTAINS_UNKNOWN; |
ee2bd06b KN |
1746 | } |
1747 | ||
1748 | static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack) | |
1749 | { | |
1750 | ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc); | |
1751 | contains_stack->contains_stack[contains_stack->nr].commit = candidate; | |
1752 | contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents; | |
1753 | } | |
1754 | ||
1755 | static enum contains_result contains_tag_algo(struct commit *candidate, | |
a91aca44 JK |
1756 | const struct commit_list *want, |
1757 | struct contains_cache *cache) | |
ee2bd06b KN |
1758 | { |
1759 | struct contains_stack contains_stack = { 0, 0, NULL }; | |
819807b3 DS |
1760 | enum contains_result result; |
1761 | uint32_t cutoff = GENERATION_NUMBER_INFINITY; | |
1762 | const struct commit_list *p; | |
1763 | ||
1764 | for (p = want; p; p = p->next) { | |
1765 | struct commit *c = p->item; | |
dade47c0 | 1766 | load_commit_graph_info(the_repository, c); |
819807b3 DS |
1767 | if (c->generation < cutoff) |
1768 | cutoff = c->generation; | |
1769 | } | |
ee2bd06b | 1770 | |
819807b3 | 1771 | result = contains_test(candidate, want, cache, cutoff); |
ee2bd06b KN |
1772 | if (result != CONTAINS_UNKNOWN) |
1773 | return result; | |
1774 | ||
1775 | push_to_contains_stack(candidate, &contains_stack); | |
1776 | while (contains_stack.nr) { | |
1777 | struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1]; | |
1778 | struct commit *commit = entry->commit; | |
1779 | struct commit_list *parents = entry->parents; | |
1780 | ||
1781 | if (!parents) { | |
a91aca44 | 1782 | *contains_cache_at(cache, commit) = CONTAINS_NO; |
ee2bd06b KN |
1783 | contains_stack.nr--; |
1784 | } | |
1785 | /* | |
1786 | * If we just popped the stack, parents->item has been marked, | |
a0262c51 | 1787 | * therefore contains_test will return a meaningful yes/no. |
ee2bd06b | 1788 | */ |
819807b3 | 1789 | else switch (contains_test(parents->item, want, cache, cutoff)) { |
ee2bd06b | 1790 | case CONTAINS_YES: |
a91aca44 | 1791 | *contains_cache_at(cache, commit) = CONTAINS_YES; |
ee2bd06b KN |
1792 | contains_stack.nr--; |
1793 | break; | |
1794 | case CONTAINS_NO: | |
1795 | entry->parents = parents->next; | |
1796 | break; | |
1797 | case CONTAINS_UNKNOWN: | |
1798 | push_to_contains_stack(parents->item, &contains_stack); | |
1799 | break; | |
1800 | } | |
1801 | } | |
1802 | free(contains_stack.contains_stack); | |
819807b3 | 1803 | return contains_test(candidate, want, cache, cutoff); |
ee2bd06b KN |
1804 | } |
1805 | ||
a91aca44 | 1806 | static int commit_contains(struct ref_filter *filter, struct commit *commit, |
ac3f5a34 | 1807 | struct commit_list *list, struct contains_cache *cache) |
ee2bd06b KN |
1808 | { |
1809 | if (filter->with_commit_tag_algo) | |
ac3f5a34 ÆAB |
1810 | return contains_tag_algo(commit, list, cache) == CONTAINS_YES; |
1811 | return is_descendant_of(commit, list); | |
ee2bd06b KN |
1812 | } |
1813 | ||
bef0e12b KN |
1814 | /* |
1815 | * Return 1 if the refname matches one of the patterns, otherwise 0. | |
1816 | * A pattern can be a literal prefix (e.g. a refname "refs/heads/master" | |
1817 | * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref | |
1818 | * matches "refs/heads/mas*", too). | |
1819 | */ | |
3bb16a8b | 1820 | static int match_pattern(const struct ref_filter *filter, const char *refname) |
bef0e12b | 1821 | { |
3bb16a8b NTND |
1822 | const char **patterns = filter->name_patterns; |
1823 | unsigned flags = 0; | |
1824 | ||
1825 | if (filter->ignore_case) | |
1826 | flags |= WM_CASEFOLD; | |
1827 | ||
bef0e12b KN |
1828 | /* |
1829 | * When no '--format' option is given we need to skip the prefix | |
1830 | * for matching refs of tags and branches. | |
1831 | */ | |
1832 | (void)(skip_prefix(refname, "refs/tags/", &refname) || | |
1833 | skip_prefix(refname, "refs/heads/", &refname) || | |
1834 | skip_prefix(refname, "refs/remotes/", &refname) || | |
1835 | skip_prefix(refname, "refs/", &refname)); | |
1836 | ||
1837 | for (; *patterns; patterns++) { | |
55d34269 | 1838 | if (!wildmatch(*patterns, refname, flags)) |
bef0e12b KN |
1839 | return 1; |
1840 | } | |
1841 | return 0; | |
1842 | } | |
1843 | ||
c95b7585 KN |
1844 | /* |
1845 | * Return 1 if the refname matches one of the patterns, otherwise 0. | |
1846 | * A pattern can be path prefix (e.g. a refname "refs/heads/master" | |
bef0e12b KN |
1847 | * matches a pattern "refs/heads/" but not "refs/heads/m") or a |
1848 | * wildcard (e.g. the same ref matches "refs/heads/m*", too). | |
c95b7585 | 1849 | */ |
3bb16a8b | 1850 | static int match_name_as_path(const struct ref_filter *filter, const char *refname) |
c95b7585 | 1851 | { |
3bb16a8b | 1852 | const char **pattern = filter->name_patterns; |
c95b7585 | 1853 | int namelen = strlen(refname); |
3bb16a8b NTND |
1854 | unsigned flags = WM_PATHNAME; |
1855 | ||
1856 | if (filter->ignore_case) | |
1857 | flags |= WM_CASEFOLD; | |
1858 | ||
c95b7585 KN |
1859 | for (; *pattern; pattern++) { |
1860 | const char *p = *pattern; | |
1861 | int plen = strlen(p); | |
1862 | ||
1863 | if ((plen <= namelen) && | |
1864 | !strncmp(refname, p, plen) && | |
1865 | (refname[plen] == '\0' || | |
1866 | refname[plen] == '/' || | |
1867 | p[plen-1] == '/')) | |
1868 | return 1; | |
639ab5ef | 1869 | if (!wildmatch(p, refname, flags)) |
c95b7585 KN |
1870 | return 1; |
1871 | } | |
1872 | return 0; | |
1873 | } | |
1874 | ||
bef0e12b KN |
1875 | /* Return 1 if the refname matches one of the patterns, otherwise 0. */ |
1876 | static int filter_pattern_match(struct ref_filter *filter, const char *refname) | |
1877 | { | |
1878 | if (!*filter->name_patterns) | |
1879 | return 1; /* No pattern always matches */ | |
1880 | if (filter->match_as_path) | |
3bb16a8b NTND |
1881 | return match_name_as_path(filter, refname); |
1882 | return match_pattern(filter, refname); | |
bef0e12b KN |
1883 | } |
1884 | ||
cfe004a5 JK |
1885 | /* |
1886 | * Find the longest prefix of pattern we can pass to | |
1887 | * `for_each_fullref_in()`, namely the part of pattern preceding the | |
1888 | * first glob character. (Note that `for_each_fullref_in()` is | |
1889 | * perfectly happy working with a prefix that doesn't end at a | |
1890 | * pathname component boundary.) | |
1891 | */ | |
1892 | static void find_longest_prefix(struct strbuf *out, const char *pattern) | |
1893 | { | |
1894 | const char *p; | |
1895 | ||
1896 | for (p = pattern; *p && !is_glob_special(*p); p++) | |
1897 | ; | |
1898 | ||
1899 | strbuf_add(out, pattern, p - pattern); | |
1900 | } | |
1901 | ||
1902 | /* | |
1903 | * This is the same as for_each_fullref_in(), but it tries to iterate | |
1904 | * only over the patterns we'll care about. Note that it _doesn't_ do a full | |
1905 | * pattern match, so the callback still has to match each ref individually. | |
1906 | */ | |
1907 | static int for_each_fullref_in_pattern(struct ref_filter *filter, | |
1908 | each_ref_fn cb, | |
1909 | void *cb_data, | |
1910 | int broken) | |
1911 | { | |
1912 | struct strbuf prefix = STRBUF_INIT; | |
1913 | int ret; | |
1914 | ||
1915 | if (!filter->match_as_path) { | |
1916 | /* | |
1917 | * in this case, the patterns are applied after | |
1918 | * prefixes like "refs/heads/" etc. are stripped off, | |
1919 | * so we have to look at everything: | |
1920 | */ | |
1921 | return for_each_fullref_in("", cb, cb_data, broken); | |
1922 | } | |
1923 | ||
e674eb25 JK |
1924 | if (filter->ignore_case) { |
1925 | /* | |
1926 | * we can't handle case-insensitive comparisons, | |
1927 | * so just return everything and let the caller | |
1928 | * sort it out. | |
1929 | */ | |
1930 | return for_each_fullref_in("", cb, cb_data, broken); | |
1931 | } | |
1932 | ||
cfe004a5 JK |
1933 | if (!filter->name_patterns[0]) { |
1934 | /* no patterns; we have to look at everything */ | |
1935 | return for_each_fullref_in("", cb, cb_data, broken); | |
1936 | } | |
1937 | ||
1938 | if (filter->name_patterns[1]) { | |
1939 | /* | |
1940 | * multiple patterns; in theory this could still work as long | |
1941 | * as the patterns are disjoint. We'd just make multiple calls | |
1942 | * to for_each_ref(). But if they're not disjoint, we'd end up | |
1943 | * reporting the same ref multiple times. So let's punt on that | |
1944 | * for now. | |
1945 | */ | |
1946 | return for_each_fullref_in("", cb, cb_data, broken); | |
1947 | } | |
1948 | ||
1949 | find_longest_prefix(&prefix, filter->name_patterns[0]); | |
1950 | ||
1951 | ret = for_each_fullref_in(prefix.buf, cb, cb_data, broken); | |
1952 | strbuf_release(&prefix); | |
1953 | return ret; | |
1954 | } | |
1955 | ||
68411046 KN |
1956 | /* |
1957 | * Given a ref (sha1, refname), check if the ref belongs to the array | |
1958 | * of sha1s. If the given ref is a tag, check if the given tag points | |
1959 | * at one of the sha1s in the given sha1 array. | |
1960 | * the given sha1_array. | |
1961 | * NEEDSWORK: | |
1962 | * 1. Only a single level of inderection is obtained, we might want to | |
1963 | * change this to account for multiple levels (e.g. annotated tags | |
1964 | * pointing to annotated tags pointing to a commit.) | |
1965 | * 2. As the refs are cached we might know what refname peels to without | |
1966 | * the need to parse the object via parse_object(). peel_ref() might be a | |
1967 | * more efficient alternative to obtain the pointee. | |
1968 | */ | |
910650d2 | 1969 | static const struct object_id *match_points_at(struct oid_array *points_at, |
4ce3621a | 1970 | const struct object_id *oid, |
1971 | const char *refname) | |
68411046 | 1972 | { |
4ce3621a | 1973 | const struct object_id *tagged_oid = NULL; |
68411046 KN |
1974 | struct object *obj; |
1975 | ||
910650d2 | 1976 | if (oid_array_lookup(points_at, oid) >= 0) |
4ce3621a | 1977 | return oid; |
109cd76d | 1978 | obj = parse_object(the_repository, oid); |
68411046 KN |
1979 | if (!obj) |
1980 | die(_("malformed object at '%s'"), refname); | |
1981 | if (obj->type == OBJ_TAG) | |
4ce3621a | 1982 | tagged_oid = &((struct tag *)obj)->tagged->oid; |
910650d2 | 1983 | if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0) |
4ce3621a | 1984 | return tagged_oid; |
68411046 KN |
1985 | return NULL; |
1986 | } | |
1987 | ||
0ffaa00f JK |
1988 | /* |
1989 | * Allocate space for a new ref_array_item and copy the name and oid to it. | |
1990 | * | |
1991 | * Callers can then fill in other struct members at their leisure. | |
1992 | */ | |
c95b7585 | 1993 | static struct ref_array_item *new_ref_array_item(const char *refname, |
0ffaa00f | 1994 | const struct object_id *oid) |
c95b7585 | 1995 | { |
96ffc06f | 1996 | struct ref_array_item *ref; |
0ffaa00f | 1997 | |
96ffc06f | 1998 | FLEX_ALLOC_STR(ref, refname, refname); |
53df97a2 | 1999 | oidcpy(&ref->objectname, oid); |
c95b7585 KN |
2000 | |
2001 | return ref; | |
2002 | } | |
2003 | ||
427cbc9d JK |
2004 | struct ref_array_item *ref_array_push(struct ref_array *array, |
2005 | const char *refname, | |
2006 | const struct object_id *oid) | |
2007 | { | |
2008 | struct ref_array_item *ref = new_ref_array_item(refname, oid); | |
2009 | ||
2010 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); | |
2011 | array->items[array->nr++] = ref; | |
c95b7585 KN |
2012 | |
2013 | return ref; | |
2014 | } | |
2015 | ||
2111aa79 | 2016 | static int ref_kind_from_refname(const char *refname) |
5b4f2851 KN |
2017 | { |
2018 | unsigned int i; | |
2019 | ||
2020 | static struct { | |
2021 | const char *prefix; | |
2022 | unsigned int kind; | |
2023 | } ref_kind[] = { | |
2024 | { "refs/heads/" , FILTER_REFS_BRANCHES }, | |
2025 | { "refs/remotes/" , FILTER_REFS_REMOTES }, | |
2026 | { "refs/tags/", FILTER_REFS_TAGS} | |
2027 | }; | |
2028 | ||
2111aa79 | 2029 | if (!strcmp(refname, "HEAD")) |
5b4f2851 KN |
2030 | return FILTER_REFS_DETACHED_HEAD; |
2031 | ||
2032 | for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { | |
2033 | if (starts_with(refname, ref_kind[i].prefix)) | |
2034 | return ref_kind[i].kind; | |
2035 | } | |
2036 | ||
2037 | return FILTER_REFS_OTHERS; | |
2038 | } | |
2039 | ||
2111aa79 LP |
2040 | static int filter_ref_kind(struct ref_filter *filter, const char *refname) |
2041 | { | |
2042 | if (filter->kind == FILTER_REFS_BRANCHES || | |
2043 | filter->kind == FILTER_REFS_REMOTES || | |
2044 | filter->kind == FILTER_REFS_TAGS) | |
2045 | return filter->kind; | |
2046 | return ref_kind_from_refname(refname); | |
2047 | } | |
2048 | ||
c95b7585 KN |
2049 | /* |
2050 | * A call-back given to for_each_ref(). Filter refs and keep them for | |
2051 | * later object processing. | |
2052 | */ | |
14de7fba | 2053 | static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
c95b7585 KN |
2054 | { |
2055 | struct ref_filter_cbdata *ref_cbdata = cb_data; | |
14de7fba | 2056 | struct ref_filter *filter = ref_cbdata->filter; |
c95b7585 | 2057 | struct ref_array_item *ref; |
35257aa0 | 2058 | struct commit *commit = NULL; |
5b4f2851 | 2059 | unsigned int kind; |
c95b7585 KN |
2060 | |
2061 | if (flag & REF_BAD_NAME) { | |
1823c619 | 2062 | warning(_("ignoring ref with broken name %s"), refname); |
c95b7585 KN |
2063 | return 0; |
2064 | } | |
2065 | ||
7ebc8cbe | 2066 | if (flag & REF_ISBROKEN) { |
1823c619 | 2067 | warning(_("ignoring broken ref %s"), refname); |
7ebc8cbe JH |
2068 | return 0; |
2069 | } | |
2070 | ||
5b4f2851 KN |
2071 | /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ |
2072 | kind = filter_ref_kind(filter, refname); | |
2073 | if (!(kind & filter->kind)) | |
2074 | return 0; | |
2075 | ||
bef0e12b | 2076 | if (!filter_pattern_match(filter, refname)) |
c95b7585 KN |
2077 | return 0; |
2078 | ||
4ce3621a | 2079 | if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname)) |
68411046 KN |
2080 | return 0; |
2081 | ||
35257aa0 KN |
2082 | /* |
2083 | * A merge filter is applied on refs pointing to commits. Hence | |
2084 | * obtain the commit using the 'oid' available and discard all | |
2085 | * non-commits early. The actual filtering is done later. | |
2086 | */ | |
ac3f5a34 | 2087 | if (filter->merge_commit || filter->with_commit || filter->no_commit || filter->verbose) { |
21e1ee8f SB |
2088 | commit = lookup_commit_reference_gently(the_repository, oid, |
2089 | 1); | |
35257aa0 KN |
2090 | if (!commit) |
2091 | return 0; | |
ac3f5a34 | 2092 | /* We perform the filtering for the '--contains' option... */ |
ee2bd06b | 2093 | if (filter->with_commit && |
ac3f5a34 ÆAB |
2094 | !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache)) |
2095 | return 0; | |
2096 | /* ...or for the `--no-contains' option */ | |
2097 | if (filter->no_commit && | |
2098 | commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache)) | |
ee2bd06b | 2099 | return 0; |
35257aa0 KN |
2100 | } |
2101 | ||
c95b7585 KN |
2102 | /* |
2103 | * We do not open the object yet; sort may only need refname | |
2104 | * to do its job and the resulting list may yet to be pruned | |
2105 | * by maxcount logic. | |
2106 | */ | |
427cbc9d | 2107 | ref = ref_array_push(ref_cbdata->array, refname, oid); |
35257aa0 | 2108 | ref->commit = commit; |
0ffaa00f | 2109 | ref->flag = flag; |
5b4f2851 | 2110 | ref->kind = kind; |
c95b7585 | 2111 | |
c95b7585 KN |
2112 | return 0; |
2113 | } | |
2114 | ||
2115 | /* Free memory allocated for a ref_array_item */ | |
2116 | static void free_array_item(struct ref_array_item *item) | |
2117 | { | |
2118 | free((char *)item->symref); | |
c95b7585 KN |
2119 | free(item); |
2120 | } | |
2121 | ||
2122 | /* Free all memory allocated for ref_array */ | |
2123 | void ref_array_clear(struct ref_array *array) | |
2124 | { | |
2125 | int i; | |
2126 | ||
2127 | for (i = 0; i < array->nr; i++) | |
2128 | free_array_item(array->items[i]); | |
6a83d902 | 2129 | FREE_AND_NULL(array->items); |
c95b7585 KN |
2130 | array->nr = array->alloc = 0; |
2131 | } | |
2132 | ||
35257aa0 KN |
2133 | static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata) |
2134 | { | |
2135 | struct rev_info revs; | |
2136 | int i, old_nr; | |
2137 | struct ref_filter *filter = ref_cbdata->filter; | |
2138 | struct ref_array *array = ref_cbdata->array; | |
2139 | struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr); | |
2140 | ||
2141 | init_revisions(&revs, NULL); | |
2142 | ||
2143 | for (i = 0; i < array->nr; i++) { | |
2144 | struct ref_array_item *item = array->items[i]; | |
2145 | add_pending_object(&revs, &item->commit->object, item->refname); | |
2146 | to_clear[i] = item->commit; | |
2147 | } | |
2148 | ||
2149 | filter->merge_commit->object.flags |= UNINTERESTING; | |
2150 | add_pending_object(&revs, &filter->merge_commit->object, ""); | |
2151 | ||
2152 | revs.limited = 1; | |
2153 | if (prepare_revision_walk(&revs)) | |
2154 | die(_("revision walk setup failed")); | |
2155 | ||
2156 | old_nr = array->nr; | |
2157 | array->nr = 0; | |
2158 | ||
2159 | for (i = 0; i < old_nr; i++) { | |
2160 | struct ref_array_item *item = array->items[i]; | |
2161 | struct commit *commit = item->commit; | |
2162 | ||
2163 | int is_merged = !!(commit->object.flags & UNINTERESTING); | |
2164 | ||
2165 | if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE)) | |
2166 | array->items[array->nr++] = array->items[i]; | |
2167 | else | |
2168 | free_array_item(item); | |
2169 | } | |
2170 | ||
5dee6d6f | 2171 | clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS); |
35257aa0 KN |
2172 | clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS); |
2173 | free(to_clear); | |
2174 | } | |
2175 | ||
14de7fba KN |
2176 | /* |
2177 | * API for filtering a set of refs. Based on the type of refs the user | |
2178 | * has requested, we iterate through those refs and apply filters | |
2179 | * as per the given ref_filter structure and finally store the | |
2180 | * filtered refs in the ref_array structure. | |
2181 | */ | |
2182 | int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type) | |
2183 | { | |
2184 | struct ref_filter_cbdata ref_cbdata; | |
35257aa0 | 2185 | int ret = 0; |
5b4f2851 | 2186 | unsigned int broken = 0; |
14de7fba KN |
2187 | |
2188 | ref_cbdata.array = array; | |
2189 | ref_cbdata.filter = filter; | |
2190 | ||
5b4f2851 KN |
2191 | if (type & FILTER_REFS_INCLUDE_BROKEN) |
2192 | broken = 1; | |
2193 | filter->kind = type & FILTER_REFS_KIND_MASK; | |
2194 | ||
a91aca44 | 2195 | init_contains_cache(&ref_cbdata.contains_cache); |
ac3f5a34 | 2196 | init_contains_cache(&ref_cbdata.no_contains_cache); |
a91aca44 | 2197 | |
35257aa0 | 2198 | /* Simple per-ref filtering */ |
5b4f2851 | 2199 | if (!filter->kind) |
14de7fba | 2200 | die("filter_refs: invalid type"); |
5b4f2851 KN |
2201 | else { |
2202 | /* | |
2203 | * For common cases where we need only branches or remotes or tags, | |
2204 | * we only iterate through those refs. If a mix of refs is needed, | |
2205 | * we iterate over all refs and filter out required refs with the help | |
2206 | * of filter_ref_kind(). | |
2207 | */ | |
2208 | if (filter->kind == FILTER_REFS_BRANCHES) | |
2209 | ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken); | |
2210 | else if (filter->kind == FILTER_REFS_REMOTES) | |
2211 | ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken); | |
2212 | else if (filter->kind == FILTER_REFS_TAGS) | |
2213 | ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken); | |
2214 | else if (filter->kind & FILTER_REFS_ALL) | |
cfe004a5 | 2215 | ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken); |
5b4f2851 KN |
2216 | if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) |
2217 | head_ref(ref_filter_handler, &ref_cbdata); | |
2218 | } | |
2219 | ||
a91aca44 | 2220 | clear_contains_cache(&ref_cbdata.contains_cache); |
ac3f5a34 | 2221 | clear_contains_cache(&ref_cbdata.no_contains_cache); |
35257aa0 KN |
2222 | |
2223 | /* Filters that need revision walking */ | |
2224 | if (filter->merge_commit) | |
2225 | do_merge_filter(&ref_cbdata); | |
2226 | ||
2227 | return ret; | |
14de7fba KN |
2228 | } |
2229 | ||
c95b7585 KN |
2230 | static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b) |
2231 | { | |
2232 | struct atom_value *va, *vb; | |
2233 | int cmp; | |
b072add7 | 2234 | cmp_type cmp_type = used_atom[s->atom].type; |
3bb16a8b | 2235 | int (*cmp_fn)(const char *, const char *); |
e339611b | 2236 | struct strbuf err = STRBUF_INIT; |
c95b7585 | 2237 | |
e339611b OT |
2238 | if (get_ref_atom_value(a, s->atom, &va, &err)) |
2239 | die("%s", err.buf); | |
2240 | if (get_ref_atom_value(b, s->atom, &vb, &err)) | |
2241 | die("%s", err.buf); | |
2242 | strbuf_release(&err); | |
3bb16a8b | 2243 | cmp_fn = s->ignore_case ? strcasecmp : strcmp; |
90c00408 KN |
2244 | if (s->version) |
2245 | cmp = versioncmp(va->s, vb->s); | |
2246 | else if (cmp_type == FIELD_STR) | |
3bb16a8b | 2247 | cmp = cmp_fn(va->s, vb->s); |
90c00408 | 2248 | else { |
e467dc14 | 2249 | if (va->value < vb->value) |
c95b7585 | 2250 | cmp = -1; |
e467dc14 | 2251 | else if (va->value == vb->value) |
3bb16a8b | 2252 | cmp = cmp_fn(a->refname, b->refname); |
c95b7585 KN |
2253 | else |
2254 | cmp = 1; | |
c95b7585 | 2255 | } |
90c00408 | 2256 | |
c95b7585 KN |
2257 | return (s->reverse) ? -cmp : cmp; |
2258 | } | |
2259 | ||
83fc4d64 | 2260 | static int compare_refs(const void *a_, const void *b_, void *ref_sorting) |
c95b7585 KN |
2261 | { |
2262 | struct ref_array_item *a = *((struct ref_array_item **)a_); | |
2263 | struct ref_array_item *b = *((struct ref_array_item **)b_); | |
2264 | struct ref_sorting *s; | |
2265 | ||
2266 | for (s = ref_sorting; s; s = s->next) { | |
2267 | int cmp = cmp_ref_sorting(s, a, b); | |
2268 | if (cmp) | |
2269 | return cmp; | |
2270 | } | |
2271 | return 0; | |
2272 | } | |
2273 | ||
2274 | void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) | |
2275 | { | |
83fc4d64 | 2276 | QSORT_S(array->items, array->nr, compare_refs, sorting); |
c95b7585 KN |
2277 | } |
2278 | ||
574e96a2 | 2279 | static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) |
c95b7585 | 2280 | { |
574e96a2 KN |
2281 | struct strbuf *s = &state->stack->output; |
2282 | ||
c95b7585 KN |
2283 | while (*cp && (!ep || cp < ep)) { |
2284 | if (*cp == '%') { | |
2285 | if (cp[1] == '%') | |
2286 | cp++; | |
2287 | else { | |
d2330973 | 2288 | int ch = hex2chr(cp + 1); |
c95b7585 | 2289 | if (0 <= ch) { |
574e96a2 | 2290 | strbuf_addch(s, ch); |
c95b7585 KN |
2291 | cp += 3; |
2292 | continue; | |
2293 | } | |
2294 | } | |
2295 | } | |
574e96a2 | 2296 | strbuf_addch(s, *cp); |
c95b7585 KN |
2297 | cp++; |
2298 | } | |
2299 | } | |
2300 | ||
3019eca9 | 2301 | int format_ref_array_item(struct ref_array_item *info, |
4a68e36d | 2302 | const struct ref_format *format, |
3019eca9 OT |
2303 | struct strbuf *final_buf, |
2304 | struct strbuf *error_buf) | |
c95b7585 KN |
2305 | { |
2306 | const char *cp, *sp, *ep; | |
574e96a2 KN |
2307 | struct ref_formatting_state state = REF_FORMATTING_STATE_INIT; |
2308 | ||
4a68e36d | 2309 | state.quote_style = format->quote_style; |
574e96a2 | 2310 | push_stack_element(&state.stack); |
c95b7585 | 2311 | |
4a68e36d | 2312 | for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) { |
c95b7585 | 2313 | struct atom_value *atomv; |
e6ff7b3b | 2314 | int pos; |
c95b7585 KN |
2315 | |
2316 | ep = strchr(sp, ')'); | |
2317 | if (cp < sp) | |
574e96a2 | 2318 | append_literal(cp, sp, &state); |
e6ff7b3b | 2319 | pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf); |
e339611b OT |
2320 | if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) || |
2321 | atomv->handler(atomv, &state, error_buf)) { | |
3fc8439c OT |
2322 | pop_stack_element(&state.stack); |
2323 | return -1; | |
2324 | } | |
c95b7585 KN |
2325 | } |
2326 | if (*cp) { | |
2327 | sp = cp + strlen(cp); | |
574e96a2 | 2328 | append_literal(cp, sp, &state); |
c95b7585 | 2329 | } |
bf285ae6 | 2330 | if (format->need_color_reset_at_eol) { |
c95b7585 | 2331 | struct atom_value resetv; |
51331aad | 2332 | resetv.s = GIT_COLOR_RESET; |
3fc8439c OT |
2333 | if (append_atom(&resetv, &state, error_buf)) { |
2334 | pop_stack_element(&state.stack); | |
2335 | return -1; | |
2336 | } | |
c95b7585 | 2337 | } |
3019eca9 OT |
2338 | if (state.stack->prev) { |
2339 | pop_stack_element(&state.stack); | |
2340 | return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing")); | |
c95b7585 | 2341 | } |
99c6a71d | 2342 | strbuf_addbuf(final_buf, &state.stack->output); |
574e96a2 | 2343 | pop_stack_element(&state.stack); |
3019eca9 | 2344 | return 0; |
99c6a71d KN |
2345 | } |
2346 | ||
4a68e36d JK |
2347 | void show_ref_array_item(struct ref_array_item *info, |
2348 | const struct ref_format *format) | |
99c6a71d KN |
2349 | { |
2350 | struct strbuf final_buf = STRBUF_INIT; | |
3019eca9 | 2351 | struct strbuf error_buf = STRBUF_INIT; |
99c6a71d | 2352 | |
3019eca9 OT |
2353 | if (format_ref_array_item(info, format, &final_buf, &error_buf)) |
2354 | die("%s", error_buf.buf); | |
99c6a71d | 2355 | fwrite(final_buf.buf, 1, final_buf.len, stdout); |
3019eca9 | 2356 | strbuf_release(&error_buf); |
99c6a71d | 2357 | strbuf_release(&final_buf); |
c95b7585 KN |
2358 | putchar('\n'); |
2359 | } | |
2360 | ||
53df97a2 | 2361 | void pretty_print_ref(const char *name, const struct object_id *oid, |
4a68e36d | 2362 | const struct ref_format *format) |
2111aa79 LP |
2363 | { |
2364 | struct ref_array_item *ref_item; | |
0ffaa00f | 2365 | ref_item = new_ref_array_item(name, oid); |
2111aa79 | 2366 | ref_item->kind = ref_kind_from_refname(name); |
4a68e36d | 2367 | show_ref_array_item(ref_item, format); |
2111aa79 LP |
2368 | free_array_item(ref_item); |
2369 | } | |
2370 | ||
29ef53cd JK |
2371 | static int parse_sorting_atom(const char *atom) |
2372 | { | |
ab7ded34 JK |
2373 | /* |
2374 | * This parses an atom using a dummy ref_format, since we don't | |
2375 | * actually care about the formatting details. | |
2376 | */ | |
2377 | struct ref_format dummy = REF_FORMAT_INIT; | |
29ef53cd | 2378 | const char *end = atom + strlen(atom); |
e6ff7b3b OT |
2379 | struct strbuf err = STRBUF_INIT; |
2380 | int res = parse_ref_filter_atom(&dummy, atom, end, &err); | |
2381 | if (res < 0) | |
2382 | die("%s", err.buf); | |
2383 | strbuf_release(&err); | |
2384 | return res; | |
29ef53cd JK |
2385 | } |
2386 | ||
c95b7585 KN |
2387 | /* If no sorting option is given, use refname to sort as default */ |
2388 | struct ref_sorting *ref_default_sorting(void) | |
2389 | { | |
2390 | static const char cstr_name[] = "refname"; | |
2391 | ||
2392 | struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting)); | |
2393 | ||
2394 | sorting->next = NULL; | |
29ef53cd | 2395 | sorting->atom = parse_sorting_atom(cstr_name); |
c95b7585 KN |
2396 | return sorting; |
2397 | } | |
2398 | ||
18a25650 | 2399 | void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg) |
c95b7585 | 2400 | { |
c95b7585 | 2401 | struct ref_sorting *s; |
c95b7585 | 2402 | |
c95b7585 KN |
2403 | s = xcalloc(1, sizeof(*s)); |
2404 | s->next = *sorting_tail; | |
2405 | *sorting_tail = s; | |
2406 | ||
2407 | if (*arg == '-') { | |
2408 | s->reverse = 1; | |
2409 | arg++; | |
2410 | } | |
90c00408 KN |
2411 | if (skip_prefix(arg, "version:", &arg) || |
2412 | skip_prefix(arg, "v:", &arg)) | |
2413 | s->version = 1; | |
29ef53cd | 2414 | s->atom = parse_sorting_atom(arg); |
18a25650 JK |
2415 | } |
2416 | ||
2417 | int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset) | |
2418 | { | |
2419 | if (!arg) /* should --no-sort void the list ? */ | |
2420 | return -1; | |
2421 | parse_ref_sorting(opt->value, arg); | |
c95b7585 KN |
2422 | return 0; |
2423 | } | |
5afcb905 KN |
2424 | |
2425 | int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset) | |
2426 | { | |
2427 | struct ref_filter *rf = opt->value; | |
1e43ed98 | 2428 | struct object_id oid; |
17d6c744 | 2429 | int no_merged = starts_with(opt->long_name, "no"); |
5afcb905 | 2430 | |
17d6c744 ÆAB |
2431 | if (rf->merge) { |
2432 | if (no_merged) { | |
2433 | return opterror(opt, "is incompatible with --merged", 0); | |
2434 | } else { | |
2435 | return opterror(opt, "is incompatible with --no-merged", 0); | |
2436 | } | |
2437 | } | |
5afcb905 | 2438 | |
17d6c744 | 2439 | rf->merge = no_merged |
5afcb905 KN |
2440 | ? REF_FILTER_MERGED_OMIT |
2441 | : REF_FILTER_MERGED_INCLUDE; | |
2442 | ||
1e43ed98 | 2443 | if (get_oid(arg, &oid)) |
5afcb905 KN |
2444 | die(_("malformed object name %s"), arg); |
2445 | ||
21e1ee8f SB |
2446 | rf->merge_commit = lookup_commit_reference_gently(the_repository, |
2447 | &oid, 0); | |
5afcb905 KN |
2448 | if (!rf->merge_commit) |
2449 | return opterror(opt, "must point to a commit", 0); | |
2450 | ||
2451 | return 0; | |
2452 | } |