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