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