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