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