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