]>
Commit | Line | Data |
---|---|---|
5e3f94df | 1 | #include "git-compat-util.h" |
32a8f510 | 2 | #include "environment.h" |
f394e093 | 3 | #include "gettext.h" |
f46094a5 | 4 | #include "config.h" |
d4a4f929 | 5 | #include "gpg-interface.h" |
41771fa4 | 6 | #include "hex.h" |
c95b7585 | 7 | #include "parse-options.h" |
f5d18f8c | 8 | #include "run-command.h" |
c95b7585 KN |
9 | #include "refs.h" |
10 | #include "wildmatch.h" | |
dabab1d6 | 11 | #include "object-name.h" |
a034e910 | 12 | #include "object-store-ll.h" |
6f2d7430 | 13 | #include "oid-array.h" |
109cd76d | 14 | #include "repository.h" |
c95b7585 | 15 | #include "commit.h" |
a3d2e83a KS |
16 | #include "mailmap.h" |
17 | #include "ident.h" | |
c95b7585 KN |
18 | #include "remote.h" |
19 | #include "color.h" | |
20 | #include "tag.h" | |
21 | #include "quote.h" | |
22 | #include "ref-filter.h" | |
35257aa0 | 23 | #include "revision.h" |
ce592082 | 24 | #include "utf8.h" |
3467663d | 25 | #include "versioncmp.h" |
b1d31c89 | 26 | #include "trailer.h" |
d4919bb2 | 27 | #include "wt-status.h" |
a91aca44 | 28 | #include "commit-slab.h" |
64043556 | 29 | #include "commit-reach.h" |
2582083f NB |
30 | #include "worktree.h" |
31 | #include "hashmap.h" | |
c95b7585 | 32 | |
6eac70fa KN |
33 | static struct ref_msg { |
34 | const char *gone; | |
35 | const char *ahead; | |
36 | const char *behind; | |
37 | const char *ahead_behind; | |
38 | } msgs = { | |
39 | /* Untranslated plumbing messages: */ | |
40 | "gone", | |
41 | "ahead %d", | |
42 | "behind %d", | |
43 | "ahead %d, behind %d" | |
44 | }; | |
45 | ||
46 | void setup_ref_filter_porcelain_msg(void) | |
47 | { | |
48 | msgs.gone = _("gone"); | |
49 | msgs.ahead = _("ahead %d"); | |
50 | msgs.behind = _("behind %d"); | |
51 | msgs.ahead_behind = _("ahead %d, behind %d"); | |
52 | } | |
c95b7585 KN |
53 | |
54 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; | |
4f3e3b37 | 55 | typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status; |
a8e7e385 | 56 | typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source; |
c95b7585 | 57 | |
5bd881d9 KN |
58 | struct align { |
59 | align_type position; | |
60 | unsigned int width; | |
61 | }; | |
62 | ||
c58492d4 | 63 | struct if_then_else { |
4f3e3b37 KN |
64 | cmp_status cmp_status; |
65 | const char *str; | |
c58492d4 KN |
66 | unsigned int then_atom_seen : 1, |
67 | else_atom_seen : 1, | |
68 | condition_satisfied : 1; | |
69 | }; | |
70 | ||
b180e6fe | 71 | struct refname_atom { |
1a34728e KN |
72 | enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option; |
73 | int lstrip, rstrip; | |
b180e6fe KN |
74 | }; |
75 | ||
ee82a487 HV |
76 | static struct ref_trailer_buf { |
77 | struct string_list filter_list; | |
78 | struct strbuf sepbuf; | |
79 | struct strbuf kvsepbuf; | |
80 | } ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT}; | |
81 | ||
aa46a0da OT |
82 | static struct expand_data { |
83 | struct object_id oid; | |
84 | enum object_type type; | |
85 | unsigned long size; | |
86 | off_t disk_size; | |
87 | struct object_id delta_base_oid; | |
88 | void *content; | |
89 | ||
90 | struct object_info info; | |
91 | } oi, oi_deref; | |
92 | ||
2582083f | 93 | struct ref_to_worktree_entry { |
e2b5038d | 94 | struct hashmap_entry ent; |
2582083f NB |
95 | struct worktree *wt; /* key is wt->head_ref */ |
96 | }; | |
97 | ||
5cf88fd8 | 98 | static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED, |
939af16e EW |
99 | const struct hashmap_entry *eptr, |
100 | const struct hashmap_entry *kptr, | |
2582083f NB |
101 | const void *keydata_aka_refname) |
102 | { | |
939af16e EW |
103 | const struct ref_to_worktree_entry *e, *k; |
104 | ||
105 | e = container_of(eptr, const struct ref_to_worktree_entry, ent); | |
106 | k = container_of(kptr, const struct ref_to_worktree_entry, ent); | |
107 | ||
2582083f NB |
108 | return strcmp(e->wt->head_ref, |
109 | keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref); | |
110 | } | |
111 | ||
112 | static struct ref_to_worktree_map { | |
113 | struct hashmap map; | |
114 | struct worktree **worktrees; | |
115 | } ref_to_worktree_map; | |
116 | ||
1197f1a4 ZH |
117 | /* |
118 | * The enum atom_type is used as the index of valid_atom array. | |
119 | * In the atom parsing stage, it will be passed to used_atom.atom_type | |
120 | * as the identifier of the atom type. We can check the type of used_atom | |
121 | * entry by `if (used_atom[i].atom_type == ATOM_*)`. | |
122 | */ | |
123 | enum atom_type { | |
124 | ATOM_REFNAME, | |
125 | ATOM_OBJECTTYPE, | |
126 | ATOM_OBJECTSIZE, | |
127 | ATOM_OBJECTNAME, | |
128 | ATOM_DELTABASE, | |
129 | ATOM_TREE, | |
130 | ATOM_PARENT, | |
131 | ATOM_NUMPARENT, | |
132 | ATOM_OBJECT, | |
133 | ATOM_TYPE, | |
134 | ATOM_TAG, | |
135 | ATOM_AUTHOR, | |
136 | ATOM_AUTHORNAME, | |
137 | ATOM_AUTHOREMAIL, | |
138 | ATOM_AUTHORDATE, | |
139 | ATOM_COMMITTER, | |
140 | ATOM_COMMITTERNAME, | |
141 | ATOM_COMMITTEREMAIL, | |
142 | ATOM_COMMITTERDATE, | |
143 | ATOM_TAGGER, | |
144 | ATOM_TAGGERNAME, | |
145 | ATOM_TAGGEREMAIL, | |
146 | ATOM_TAGGERDATE, | |
147 | ATOM_CREATOR, | |
148 | ATOM_CREATORDATE, | |
f5d18f8c | 149 | ATOM_DESCRIBE, |
1197f1a4 ZH |
150 | ATOM_SUBJECT, |
151 | ATOM_BODY, | |
152 | ATOM_TRAILERS, | |
153 | ATOM_CONTENTS, | |
26c9c03f | 154 | ATOM_SIGNATURE, |
bd0708c7 | 155 | ATOM_RAW, |
1197f1a4 ZH |
156 | ATOM_UPSTREAM, |
157 | ATOM_PUSH, | |
158 | ATOM_SYMREF, | |
159 | ATOM_FLAG, | |
160 | ATOM_HEAD, | |
161 | ATOM_COLOR, | |
162 | ATOM_WORKTREEPATH, | |
163 | ATOM_ALIGN, | |
164 | ATOM_END, | |
165 | ATOM_IF, | |
166 | ATOM_THEN, | |
167 | ATOM_ELSE, | |
b9dee075 | 168 | ATOM_REST, |
49abcd21 | 169 | ATOM_AHEADBEHIND, |
1197f1a4 ZH |
170 | }; |
171 | ||
50cd83dc KN |
172 | /* |
173 | * An atom is a valid field atom listed below, possibly prefixed with | |
174 | * a "*" to denote deref_tag(). | |
175 | * | |
176 | * We parse given format string and sort specifiers, and make a list | |
177 | * of properties that we need to extract out of objects. ref_array_item | |
178 | * structure will hold an array of values extracted that can be | |
179 | * indexed with the "atom number", which is an index into this | |
180 | * array. | |
181 | */ | |
b072add7 | 182 | static struct used_atom { |
1197f1a4 | 183 | enum atom_type atom_type; |
b072add7 KN |
184 | const char *name; |
185 | cmp_type type; | |
a8e7e385 | 186 | info_source source; |
fd935cc7 KN |
187 | union { |
188 | char color[COLOR_MAXLEN]; | |
5bd881d9 | 189 | struct align align; |
7743fcca | 190 | struct { |
cc72385f | 191 | enum { |
9700fae5 | 192 | RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF |
cc72385f | 193 | } option; |
3ba308cb | 194 | struct refname_atom refname; |
cc72385f | 195 | unsigned int nobracket : 1, push : 1, push_remote : 1; |
7743fcca | 196 | } remote_ref; |
452db397 | 197 | struct { |
905f0a4e HV |
198 | enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES, |
199 | C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option; | |
67a20a00 | 200 | struct process_trailer_options trailer_opts; |
452db397 KN |
201 | unsigned int nlines; |
202 | } contents; | |
bd0708c7 ZH |
203 | struct { |
204 | enum { RAW_BARE, RAW_LENGTH } option; | |
205 | } raw_data; | |
4f3e3b37 KN |
206 | struct { |
207 | cmp_status cmp_status; | |
208 | const char *str; | |
209 | } if_then_else; | |
42d0eb05 KN |
210 | struct { |
211 | enum { O_FULL, O_LENGTH, O_SHORT } option; | |
212 | unsigned int length; | |
87d3beb6 | 213 | } oid; |
0caf20f2 ZH |
214 | struct { |
215 | enum { O_SIZE, O_SIZE_DISK } option; | |
216 | } objectsize; | |
a3d2e83a KS |
217 | struct { |
218 | enum { N_RAW, N_MAILMAP } option; | |
219 | } name_option; | |
220 | struct { | |
221 | enum { | |
222 | EO_RAW = 0, | |
223 | EO_TRIM = 1<<0, | |
224 | EO_LOCALPART = 1<<1, | |
225 | EO_MAILMAP = 1<<2, | |
226 | } option; | |
b82445dc | 227 | } email_option; |
26c9c03f KS |
228 | struct { |
229 | enum { S_BARE, S_GRADE, S_SIGNER, S_KEY, | |
230 | S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option; | |
231 | } signature; | |
f5d18f8c | 232 | const char **describe_args; |
b180e6fe | 233 | struct refname_atom refname; |
613a0e52 | 234 | char *head; |
fd935cc7 | 235 | } u; |
b072add7 | 236 | } *used_atom; |
50cd83dc | 237 | static int used_atom_cnt, need_tagged, need_symref; |
50cd83dc | 238 | |
e2e7a245 OT |
239 | /* |
240 | * Expand string, append it to strbuf *sb, then return error code ret. | |
241 | * Allow to save few lines of code. | |
242 | */ | |
48ca53ca | 243 | __attribute__((format (printf, 3, 4))) |
e2e7a245 OT |
244 | static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...) |
245 | { | |
246 | va_list ap; | |
247 | va_start(ap, fmt); | |
248 | strbuf_vaddf(sb, fmt, ap); | |
249 | va_end(ap); | |
250 | return ret; | |
251 | } | |
252 | ||
a33d0fae JK |
253 | static int err_no_arg(struct strbuf *sb, const char *name) |
254 | { | |
1955ef10 JK |
255 | size_t namelen = strchrnul(name, ':') - name; |
256 | strbuf_addf(sb, _("%%(%.*s) does not take arguments"), | |
257 | (int)namelen, name); | |
a33d0fae JK |
258 | return -1; |
259 | } | |
260 | ||
dda4fc1a JK |
261 | static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg) |
262 | { | |
1955ef10 JK |
263 | size_t namelen = strchrnul(name, ':') - name; |
264 | strbuf_addf(sb, _("unrecognized %%(%.*s) argument: %s"), | |
265 | (int)namelen, name, arg); | |
dda4fc1a JK |
266 | return -1; |
267 | } | |
268 | ||
f46094a5 KS |
269 | /* |
270 | * Parse option of name "candidate" in the option string "to_parse" of | |
271 | * the form | |
272 | * | |
273 | * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..." | |
274 | * | |
275 | * The remaining part of "to_parse" is stored in "end" (if we are | |
276 | * parsing the last candidate, then this is NULL) and the value of | |
277 | * the candidate is stored in "valuestart" and its length in "valuelen", | |
278 | * that is the portion after "=". Since it is possible for a "candidate" | |
279 | * to not have a value, in such cases, "valuestart" is set to point to | |
280 | * NULL and "valuelen" to 0. | |
281 | * | |
282 | * The function returns 1 on success. It returns 0 if we don't find | |
283 | * "candidate" in "to_parse" or we find "candidate" but it is followed | |
284 | * by more chars (for example, "candidatefoo"), that is, we don't find | |
285 | * an exact match. | |
286 | * | |
287 | * This function only does the above for one "candidate" at a time. So | |
288 | * it has to be called each time trying to parse a "candidate" in the | |
289 | * option string "to_parse". | |
290 | */ | |
291 | static int match_atom_arg_value(const char *to_parse, const char *candidate, | |
292 | const char **end, const char **valuestart, | |
293 | size_t *valuelen) | |
294 | { | |
295 | const char *atom; | |
296 | ||
297 | if (!skip_prefix(to_parse, candidate, &atom)) | |
298 | return 0; /* definitely not "candidate" */ | |
299 | ||
300 | if (*atom == '=') { | |
301 | /* we just saw "candidate=" */ | |
302 | *valuestart = atom + 1; | |
303 | atom = strchrnul(*valuestart, ','); | |
304 | *valuelen = atom - *valuestart; | |
305 | } else if (*atom != ',' && *atom != '\0') { | |
306 | /* key begins with "candidate" but has more chars */ | |
307 | return 0; | |
308 | } else { | |
309 | /* just "candidate" without "=val" */ | |
310 | *valuestart = NULL; | |
311 | *valuelen = 0; | |
312 | } | |
313 | ||
314 | /* atom points at either the ',' or NUL after this key[=val] */ | |
315 | if (*atom == ',') | |
316 | atom++; | |
317 | else if (*atom) | |
318 | BUG("Why is *atom not NULL yet?"); | |
319 | ||
320 | *end = atom; | |
321 | return 1; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Parse boolean option of name "candidate" in the option list "to_parse" | |
326 | * of the form | |
327 | * | |
328 | * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..." | |
329 | * | |
330 | * The remaining part of "to_parse" is stored in "end" (if we are parsing | |
331 | * the last candidate, then this is NULL) and the value (if given) is | |
332 | * parsed and stored in "val", so "val" always points to either 0 or 1. | |
333 | * If the value is not given, then "val" is set to point to 1. | |
334 | * | |
335 | * The boolean value is parsed using "git_parse_maybe_bool()", so the | |
336 | * accepted values are | |
337 | * | |
338 | * to set true - "1", "yes", "true" | |
339 | * to set false - "0", "no", "false" | |
340 | * | |
341 | * This function returns 1 on success. It returns 0 when we don't find | |
342 | * an exact match for "candidate" or when the boolean value given is | |
343 | * not valid. | |
344 | */ | |
345 | static int match_atom_bool_arg(const char *to_parse, const char *candidate, | |
346 | const char **end, int *val) | |
347 | { | |
348 | const char *argval; | |
349 | char *strval; | |
350 | size_t arglen; | |
351 | int v; | |
352 | ||
353 | if (!match_atom_arg_value(to_parse, candidate, end, &argval, &arglen)) | |
354 | return 0; | |
355 | ||
356 | if (!argval) { | |
357 | *val = 1; | |
358 | return 1; | |
359 | } | |
360 | ||
361 | strval = xstrndup(argval, arglen); | |
362 | v = git_parse_maybe_bool(strval); | |
363 | free(strval); | |
364 | ||
365 | if (v == -1) | |
366 | return 0; | |
367 | ||
368 | *val = v; | |
369 | ||
370 | return 1; | |
371 | } | |
372 | ||
e85fcb35 | 373 | static int color_atom_parser(struct ref_format *format, struct used_atom *atom, |
74efea94 | 374 | const char *color_value, struct strbuf *err) |
fd935cc7 KN |
375 | { |
376 | if (!color_value) | |
74efea94 | 377 | return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)")); |
fd935cc7 | 378 | if (color_parse(color_value, atom->u.color) < 0) |
74efea94 OT |
379 | return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"), |
380 | color_value); | |
11b087ad JK |
381 | /* |
382 | * We check this after we've parsed the color, which lets us complain | |
383 | * about syntactically bogus color names even if they won't be used. | |
384 | */ | |
385 | if (!want_color(format->use_color)) | |
386 | color_parse("", atom->u.color); | |
74efea94 | 387 | return 0; |
fd935cc7 KN |
388 | } |
389 | ||
74efea94 OT |
390 | static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg, |
391 | const char *name, struct strbuf *err) | |
5339bdad KN |
392 | { |
393 | if (!arg) | |
b180e6fe | 394 | atom->option = R_NORMAL; |
5339bdad | 395 | else if (!strcmp(arg, "short")) |
b180e6fe | 396 | atom->option = R_SHORT; |
44a6b6ce JH |
397 | else if (skip_prefix(arg, "lstrip=", &arg) || |
398 | skip_prefix(arg, "strip=", &arg)) { | |
17938f17 | 399 | atom->option = R_LSTRIP; |
1a0ca5e3 | 400 | if (strtol_i(arg, 10, &atom->lstrip)) |
74efea94 | 401 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg); |
1a34728e KN |
402 | } else if (skip_prefix(arg, "rstrip=", &arg)) { |
403 | atom->option = R_RSTRIP; | |
404 | if (strtol_i(arg, 10, &atom->rstrip)) | |
74efea94 | 405 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg); |
b180e6fe | 406 | } else |
1955ef10 | 407 | return err_bad_arg(err, name, arg); |
74efea94 | 408 | return 0; |
b180e6fe KN |
409 | } |
410 | ||
5fe9e1ce JK |
411 | static int remote_ref_atom_parser(struct ref_format *format UNUSED, |
412 | struct used_atom *atom, | |
74efea94 | 413 | const char *arg, struct strbuf *err) |
5339bdad | 414 | { |
7743fcca KN |
415 | struct string_list params = STRING_LIST_INIT_DUP; |
416 | int i; | |
417 | ||
cc72385f JS |
418 | if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) |
419 | atom->u.remote_ref.push = 1; | |
420 | ||
7743fcca | 421 | if (!arg) { |
3ba308cb | 422 | atom->u.remote_ref.option = RR_REF; |
74efea94 OT |
423 | return refname_atom_parser_internal(&atom->u.remote_ref.refname, |
424 | arg, atom->name, err); | |
7743fcca KN |
425 | } |
426 | ||
427 | atom->u.remote_ref.nobracket = 0; | |
428 | string_list_split(¶ms, arg, ',', -1); | |
429 | ||
430 | for (i = 0; i < params.nr; i++) { | |
431 | const char *s = params.items[i].string; | |
432 | ||
3ba308cb | 433 | if (!strcmp(s, "track")) |
7743fcca KN |
434 | atom->u.remote_ref.option = RR_TRACK; |
435 | else if (!strcmp(s, "trackshort")) | |
436 | atom->u.remote_ref.option = RR_TRACKSHORT; | |
437 | else if (!strcmp(s, "nobracket")) | |
438 | atom->u.remote_ref.nobracket = 1; | |
cc72385f JS |
439 | else if (!strcmp(s, "remotename")) { |
440 | atom->u.remote_ref.option = RR_REMOTE_NAME; | |
441 | atom->u.remote_ref.push_remote = 1; | |
9700fae5 W |
442 | } else if (!strcmp(s, "remoteref")) { |
443 | atom->u.remote_ref.option = RR_REMOTE_REF; | |
444 | atom->u.remote_ref.push_remote = 1; | |
cc72385f | 445 | } else { |
3ba308cb | 446 | atom->u.remote_ref.option = RR_REF; |
74efea94 OT |
447 | if (refname_atom_parser_internal(&atom->u.remote_ref.refname, |
448 | arg, atom->name, err)) { | |
449 | string_list_clear(¶ms, 0); | |
450 | return -1; | |
451 | } | |
3ba308cb | 452 | } |
7743fcca KN |
453 | } |
454 | ||
455 | string_list_clear(¶ms, 0); | |
74efea94 | 456 | return 0; |
5339bdad KN |
457 | } |
458 | ||
5fe9e1ce JK |
459 | static int objecttype_atom_parser(struct ref_format *format UNUSED, |
460 | struct used_atom *atom, | |
aa46a0da OT |
461 | const char *arg, struct strbuf *err) |
462 | { | |
463 | if (arg) | |
a33d0fae | 464 | return err_no_arg(err, "objecttype"); |
aa46a0da OT |
465 | if (*atom->name == '*') |
466 | oi_deref.info.typep = &oi_deref.type; | |
467 | else | |
468 | oi.info.typep = &oi.type; | |
469 | return 0; | |
470 | } | |
471 | ||
5fe9e1ce JK |
472 | static int objectsize_atom_parser(struct ref_format *format UNUSED, |
473 | struct used_atom *atom, | |
aa46a0da OT |
474 | const char *arg, struct strbuf *err) |
475 | { | |
1867ce6c | 476 | if (!arg) { |
0caf20f2 | 477 | atom->u.objectsize.option = O_SIZE; |
1867ce6c OT |
478 | if (*atom->name == '*') |
479 | oi_deref.info.sizep = &oi_deref.size; | |
480 | else | |
481 | oi.info.sizep = &oi.size; | |
482 | } else if (!strcmp(arg, "disk")) { | |
0caf20f2 | 483 | atom->u.objectsize.option = O_SIZE_DISK; |
1867ce6c OT |
484 | if (*atom->name == '*') |
485 | oi_deref.info.disk_sizep = &oi_deref.disk_size; | |
486 | else | |
487 | oi.info.disk_sizep = &oi.disk_size; | |
488 | } else | |
dda4fc1a | 489 | return err_bad_arg(err, "objectsize", arg); |
aa46a0da OT |
490 | return 0; |
491 | } | |
492 | ||
5fe9e1ce JK |
493 | static int deltabase_atom_parser(struct ref_format *format UNUSED, |
494 | struct used_atom *atom, | |
33311fa1 | 495 | const char *arg, struct strbuf *err) |
aa46a0da OT |
496 | { |
497 | if (arg) | |
a33d0fae | 498 | return err_no_arg(err, "deltabase"); |
aa46a0da | 499 | if (*atom->name == '*') |
b99b6bcc | 500 | oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid; |
aa46a0da | 501 | else |
b99b6bcc | 502 | oi.info.delta_base_oid = &oi.delta_base_oid; |
aa46a0da OT |
503 | return 0; |
504 | } | |
505 | ||
5fe9e1ce JK |
506 | static int body_atom_parser(struct ref_format *format UNUSED, |
507 | struct used_atom *atom, | |
74efea94 | 508 | const char *arg, struct strbuf *err) |
452db397 KN |
509 | { |
510 | if (arg) | |
a33d0fae | 511 | return err_no_arg(err, "body"); |
452db397 | 512 | atom->u.contents.option = C_BODY_DEP; |
74efea94 | 513 | return 0; |
452db397 KN |
514 | } |
515 | ||
5fe9e1ce JK |
516 | static int subject_atom_parser(struct ref_format *format UNUSED, |
517 | struct used_atom *atom, | |
74efea94 | 518 | const char *arg, struct strbuf *err) |
452db397 | 519 | { |
905f0a4e HV |
520 | if (!arg) |
521 | atom->u.contents.option = C_SUB; | |
522 | else if (!strcmp(arg, "sanitize")) | |
523 | atom->u.contents.option = C_SUB_SANITIZE; | |
524 | else | |
dda4fc1a | 525 | return err_bad_arg(err, "subject", arg); |
74efea94 | 526 | return 0; |
452db397 KN |
527 | } |
528 | ||
26c9c03f KS |
529 | static int parse_signature_option(const char *arg) |
530 | { | |
531 | if (!arg) | |
532 | return S_BARE; | |
533 | else if (!strcmp(arg, "signer")) | |
534 | return S_SIGNER; | |
535 | else if (!strcmp(arg, "grade")) | |
536 | return S_GRADE; | |
537 | else if (!strcmp(arg, "key")) | |
538 | return S_KEY; | |
539 | else if (!strcmp(arg, "fingerprint")) | |
540 | return S_FINGERPRINT; | |
541 | else if (!strcmp(arg, "primarykeyfingerprint")) | |
542 | return S_PRI_KEY_FP; | |
543 | else if (!strcmp(arg, "trustlevel")) | |
544 | return S_TRUST_LEVEL; | |
545 | return -1; | |
546 | } | |
547 | ||
548 | static int signature_atom_parser(struct ref_format *format UNUSED, | |
549 | struct used_atom *atom, | |
550 | const char *arg, struct strbuf *err) | |
551 | { | |
552 | int opt = parse_signature_option(arg); | |
553 | if (opt < 0) | |
554 | return err_bad_arg(err, "signature", arg); | |
555 | atom->u.signature.option = opt; | |
556 | return 0; | |
557 | } | |
558 | ||
29c9f2c3 JK |
559 | static int trailers_atom_parser(struct ref_format *format UNUSED, |
560 | struct used_atom *atom, | |
74efea94 | 561 | const char *arg, struct strbuf *err) |
b1d31c89 | 562 | { |
e5fba5d5 JK |
563 | atom->u.contents.trailer_opts.no_divider = 1; |
564 | ||
67a20a00 | 565 | if (arg) { |
ee82a487 HV |
566 | const char *argbuf = xstrfmt("%s)", arg); |
567 | char *invalid_arg = NULL; | |
568 | ||
569 | if (format_set_trailers_options(&atom->u.contents.trailer_opts, | |
570 | &ref_trailer_buf.filter_list, | |
571 | &ref_trailer_buf.sepbuf, | |
572 | &ref_trailer_buf.kvsepbuf, | |
573 | &argbuf, &invalid_arg)) { | |
574 | if (!invalid_arg) | |
575 | strbuf_addf(err, _("expected %%(trailers:key=<value>)")); | |
576 | else | |
577 | strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg); | |
578 | free((char *)invalid_arg); | |
579 | return -1; | |
67a20a00 TB |
580 | } |
581 | } | |
b1d31c89 | 582 | atom->u.contents.option = C_TRAILERS; |
74efea94 | 583 | return 0; |
b1d31c89 JK |
584 | } |
585 | ||
e85fcb35 | 586 | static int contents_atom_parser(struct ref_format *format, struct used_atom *atom, |
74efea94 | 587 | const char *arg, struct strbuf *err) |
452db397 KN |
588 | { |
589 | if (!arg) | |
590 | atom->u.contents.option = C_BARE; | |
591 | else if (!strcmp(arg, "body")) | |
592 | atom->u.contents.option = C_BODY; | |
6d79cd84 KS |
593 | else if (!strcmp(arg, "size")) { |
594 | atom->type = FIELD_ULONG; | |
b6839fda | 595 | atom->u.contents.option = C_LENGTH; |
6d79cd84 | 596 | } else if (!strcmp(arg, "signature")) |
452db397 KN |
597 | atom->u.contents.option = C_SIG; |
598 | else if (!strcmp(arg, "subject")) | |
599 | atom->u.contents.option = C_SUB; | |
2c22e102 HV |
600 | else if (!strcmp(arg, "trailers")) { |
601 | if (trailers_atom_parser(format, atom, NULL, err)) | |
602 | return -1; | |
603 | } else if (skip_prefix(arg, "trailers:", &arg)) { | |
604 | if (trailers_atom_parser(format, atom, arg, err)) | |
74efea94 | 605 | return -1; |
7a5edbdb | 606 | } else if (skip_prefix(arg, "lines=", &arg)) { |
452db397 KN |
607 | atom->u.contents.option = C_LINES; |
608 | if (strtoul_ui(arg, 10, &atom->u.contents.nlines)) | |
74efea94 | 609 | return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg); |
452db397 | 610 | } else |
dda4fc1a | 611 | return err_bad_arg(err, "contents", arg); |
74efea94 | 612 | return 0; |
452db397 KN |
613 | } |
614 | ||
f5d18f8c KS |
615 | static int describe_atom_option_parser(struct strvec *args, const char **arg, |
616 | struct strbuf *err) | |
617 | { | |
618 | const char *argval; | |
619 | size_t arglen = 0; | |
620 | int optval = 0; | |
621 | ||
622 | if (match_atom_bool_arg(*arg, "tags", arg, &optval)) { | |
623 | if (!optval) | |
624 | strvec_push(args, "--no-tags"); | |
625 | else | |
626 | strvec_push(args, "--tags"); | |
627 | return 1; | |
628 | } | |
629 | ||
630 | if (match_atom_arg_value(*arg, "abbrev", arg, &argval, &arglen)) { | |
631 | char *endptr; | |
632 | ||
633 | if (!arglen) | |
634 | return strbuf_addf_ret(err, -1, | |
635 | _("argument expected for %s"), | |
636 | "describe:abbrev"); | |
637 | if (strtol(argval, &endptr, 10) < 0) | |
638 | return strbuf_addf_ret(err, -1, | |
639 | _("positive value expected %s=%s"), | |
640 | "describe:abbrev", argval); | |
641 | if (endptr - argval != arglen) | |
642 | return strbuf_addf_ret(err, -1, | |
643 | _("cannot fully parse %s=%s"), | |
644 | "describe:abbrev", argval); | |
645 | ||
646 | strvec_pushf(args, "--abbrev=%.*s", (int)arglen, argval); | |
647 | return 1; | |
648 | } | |
649 | ||
650 | if (match_atom_arg_value(*arg, "match", arg, &argval, &arglen)) { | |
651 | if (!arglen) | |
652 | return strbuf_addf_ret(err, -1, | |
653 | _("value expected %s="), | |
654 | "describe:match"); | |
655 | ||
656 | strvec_pushf(args, "--match=%.*s", (int)arglen, argval); | |
657 | return 1; | |
658 | } | |
659 | ||
660 | if (match_atom_arg_value(*arg, "exclude", arg, &argval, &arglen)) { | |
661 | if (!arglen) | |
662 | return strbuf_addf_ret(err, -1, | |
663 | _("value expected %s="), | |
664 | "describe:exclude"); | |
665 | ||
666 | strvec_pushf(args, "--exclude=%.*s", (int)arglen, argval); | |
667 | return 1; | |
668 | } | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | static int describe_atom_parser(struct ref_format *format UNUSED, | |
674 | struct used_atom *atom, | |
675 | const char *arg, struct strbuf *err) | |
676 | { | |
677 | struct strvec args = STRVEC_INIT; | |
678 | ||
679 | for (;;) { | |
680 | int found = 0; | |
681 | const char *bad_arg = arg; | |
682 | ||
683 | if (!arg || !*arg) | |
684 | break; | |
685 | ||
686 | found = describe_atom_option_parser(&args, &arg, err); | |
687 | if (found < 0) | |
688 | return found; | |
689 | if (!found) | |
690 | return err_bad_arg(err, "describe", bad_arg); | |
691 | } | |
692 | atom->u.describe_args = strvec_detach(&args); | |
693 | return 0; | |
694 | } | |
695 | ||
5fe9e1ce JK |
696 | static int raw_atom_parser(struct ref_format *format UNUSED, |
697 | struct used_atom *atom, | |
698 | const char *arg, struct strbuf *err) | |
bd0708c7 ZH |
699 | { |
700 | if (!arg) | |
701 | atom->u.raw_data.option = RAW_BARE; | |
6d79cd84 KS |
702 | else if (!strcmp(arg, "size")) { |
703 | atom->type = FIELD_ULONG; | |
bd0708c7 | 704 | atom->u.raw_data.option = RAW_LENGTH; |
6d79cd84 | 705 | } else |
dda4fc1a | 706 | return err_bad_arg(err, "raw", arg); |
bd0708c7 ZH |
707 | return 0; |
708 | } | |
709 | ||
5fe9e1ce JK |
710 | static int oid_atom_parser(struct ref_format *format UNUSED, |
711 | struct used_atom *atom, | |
87d3beb6 | 712 | const char *arg, struct strbuf *err) |
fe63c4d1 KN |
713 | { |
714 | if (!arg) | |
87d3beb6 | 715 | atom->u.oid.option = O_FULL; |
fe63c4d1 | 716 | else if (!strcmp(arg, "short")) |
87d3beb6 | 717 | atom->u.oid.option = O_SHORT; |
42d0eb05 | 718 | else if (skip_prefix(arg, "short=", &arg)) { |
87d3beb6 HV |
719 | atom->u.oid.option = O_LENGTH; |
720 | if (strtoul_ui(arg, 10, &atom->u.oid.length) || | |
721 | atom->u.oid.length == 0) | |
e7601eb5 | 722 | return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name); |
87d3beb6 HV |
723 | if (atom->u.oid.length < MINIMUM_ABBREV) |
724 | atom->u.oid.length = MINIMUM_ABBREV; | |
42d0eb05 | 725 | } else |
1955ef10 | 726 | return err_bad_arg(err, atom->name, arg); |
74efea94 | 727 | return 0; |
fe63c4d1 KN |
728 | } |
729 | ||
a3d2e83a KS |
730 | static int person_name_atom_parser(struct ref_format *format UNUSED, |
731 | struct used_atom *atom, | |
732 | const char *arg, struct strbuf *err) | |
b82445dc HV |
733 | { |
734 | if (!arg) | |
a3d2e83a KS |
735 | atom->u.name_option.option = N_RAW; |
736 | else if (!strcmp(arg, "mailmap")) | |
737 | atom->u.name_option.option = N_MAILMAP; | |
b82445dc | 738 | else |
285da432 | 739 | return err_bad_arg(err, atom->name, arg); |
74efea94 | 740 | return 0; |
fe63c4d1 KN |
741 | } |
742 | ||
a3d2e83a KS |
743 | static int email_atom_option_parser(struct used_atom *atom, |
744 | const char **arg, struct strbuf *err) | |
745 | { | |
746 | if (!*arg) | |
747 | return EO_RAW; | |
748 | if (skip_prefix(*arg, "trim", arg)) | |
749 | return EO_TRIM; | |
750 | if (skip_prefix(*arg, "localpart", arg)) | |
751 | return EO_LOCALPART; | |
752 | if (skip_prefix(*arg, "mailmap", arg)) | |
753 | return EO_MAILMAP; | |
754 | return -1; | |
755 | } | |
756 | ||
757 | static int person_email_atom_parser(struct ref_format *format UNUSED, | |
758 | struct used_atom *atom, | |
759 | const char *arg, struct strbuf *err) | |
760 | { | |
761 | for (;;) { | |
762 | int opt = email_atom_option_parser(atom, &arg, err); | |
763 | const char *bad_arg = arg; | |
764 | ||
765 | if (opt < 0) | |
766 | return err_bad_arg(err, atom->name, bad_arg); | |
767 | atom->u.email_option.option |= opt; | |
768 | ||
769 | if (!arg || !*arg) | |
770 | break; | |
771 | if (*arg == ',') | |
772 | arg++; | |
773 | else | |
774 | return err_bad_arg(err, atom->name, bad_arg); | |
775 | } | |
776 | return 0; | |
777 | } | |
778 | ||
5fe9e1ce JK |
779 | static int refname_atom_parser(struct ref_format *format UNUSED, |
780 | struct used_atom *atom, | |
74efea94 | 781 | const char *arg, struct strbuf *err) |
a7984101 | 782 | { |
74efea94 | 783 | return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); |
a7984101 KN |
784 | } |
785 | ||
25a8d79e KN |
786 | static align_type parse_align_position(const char *s) |
787 | { | |
788 | if (!strcmp(s, "right")) | |
789 | return ALIGN_RIGHT; | |
790 | else if (!strcmp(s, "middle")) | |
791 | return ALIGN_MIDDLE; | |
792 | else if (!strcmp(s, "left")) | |
793 | return ALIGN_LEFT; | |
794 | return -1; | |
795 | } | |
796 | ||
5fe9e1ce JK |
797 | static int align_atom_parser(struct ref_format *format UNUSED, |
798 | struct used_atom *atom, | |
74efea94 | 799 | const char *arg, struct strbuf *err) |
5bd881d9 KN |
800 | { |
801 | struct align *align = &atom->u.align; | |
802 | struct string_list params = STRING_LIST_INIT_DUP; | |
803 | int i; | |
804 | unsigned int width = ~0U; | |
805 | ||
806 | if (!arg) | |
74efea94 | 807 | return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)")); |
5bd881d9 KN |
808 | |
809 | align->position = ALIGN_LEFT; | |
810 | ||
811 | string_list_split(¶ms, arg, ',', -1); | |
812 | for (i = 0; i < params.nr; i++) { | |
813 | const char *s = params.items[i].string; | |
814 | int position; | |
815 | ||
395fb8f9 KN |
816 | if (skip_prefix(s, "position=", &s)) { |
817 | position = parse_align_position(s); | |
74efea94 OT |
818 | if (position < 0) { |
819 | strbuf_addf(err, _("unrecognized position:%s"), s); | |
820 | string_list_clear(¶ms, 0); | |
821 | return -1; | |
822 | } | |
395fb8f9 KN |
823 | align->position = position; |
824 | } else if (skip_prefix(s, "width=", &s)) { | |
74efea94 OT |
825 | if (strtoul_ui(s, 10, &width)) { |
826 | strbuf_addf(err, _("unrecognized width:%s"), s); | |
827 | string_list_clear(¶ms, 0); | |
828 | return -1; | |
829 | } | |
395fb8f9 | 830 | } else if (!strtoul_ui(s, 10, &width)) |
5bd881d9 KN |
831 | ; |
832 | else if ((position = parse_align_position(s)) >= 0) | |
833 | align->position = position; | |
74efea94 | 834 | else { |
68e2ea0b | 835 | strbuf_addf(err, _("unrecognized %%(%s) argument: %s"), "align", s); |
74efea94 OT |
836 | string_list_clear(¶ms, 0); |
837 | return -1; | |
838 | } | |
5bd881d9 KN |
839 | } |
840 | ||
74efea94 OT |
841 | if (width == ~0U) { |
842 | string_list_clear(¶ms, 0); | |
843 | return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom")); | |
844 | } | |
5bd881d9 KN |
845 | align->width = width; |
846 | string_list_clear(¶ms, 0); | |
74efea94 | 847 | return 0; |
5bd881d9 KN |
848 | } |
849 | ||
5fe9e1ce JK |
850 | static int if_atom_parser(struct ref_format *format UNUSED, |
851 | struct used_atom *atom, | |
74efea94 | 852 | const char *arg, struct strbuf *err) |
4f3e3b37 KN |
853 | { |
854 | if (!arg) { | |
855 | atom->u.if_then_else.cmp_status = COMPARE_NONE; | |
74efea94 | 856 | return 0; |
4f3e3b37 KN |
857 | } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) { |
858 | atom->u.if_then_else.cmp_status = COMPARE_EQUAL; | |
859 | } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) { | |
860 | atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL; | |
74efea94 | 861 | } else |
dda4fc1a | 862 | return err_bad_arg(err, "if", arg); |
74efea94 | 863 | return 0; |
4f3e3b37 KN |
864 | } |
865 | ||
29c9f2c3 | 866 | static int rest_atom_parser(struct ref_format *format UNUSED, |
5fe9e1ce | 867 | struct used_atom *atom UNUSED, |
b9dee075 ZH |
868 | const char *arg, struct strbuf *err) |
869 | { | |
870 | if (arg) | |
a33d0fae | 871 | return err_no_arg(err, "rest"); |
b9dee075 ZH |
872 | return 0; |
873 | } | |
874 | ||
29c9f2c3 JK |
875 | static int ahead_behind_atom_parser(struct ref_format *format, |
876 | struct used_atom *atom UNUSED, | |
49abcd21 DS |
877 | const char *arg, struct strbuf *err) |
878 | { | |
879 | struct string_list_item *item; | |
880 | ||
881 | if (!arg) | |
882 | return strbuf_addf_ret(err, -1, _("expected format: %%(ahead-behind:<committish>)")); | |
883 | ||
884 | item = string_list_append(&format->bases, arg); | |
885 | item->util = lookup_commit_reference_by_name(arg); | |
886 | if (!item->util) | |
887 | die("failed to find '%s'", arg); | |
888 | ||
889 | return 0; | |
890 | } | |
891 | ||
5fe9e1ce JK |
892 | static int head_atom_parser(struct ref_format *format UNUSED, |
893 | struct used_atom *atom, | |
afc1a946 | 894 | const char *arg, struct strbuf *err) |
613a0e52 | 895 | { |
afc1a946 | 896 | if (arg) |
a33d0fae | 897 | return err_no_arg(err, "HEAD"); |
efbd4fdf | 898 | atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); |
74efea94 | 899 | return 0; |
613a0e52 | 900 | } |
4f3e3b37 | 901 | |
c95b7585 KN |
902 | static struct { |
903 | const char *name; | |
a8e7e385 | 904 | info_source source; |
c95b7585 | 905 | cmp_type cmp_type; |
e85fcb35 | 906 | int (*parser)(struct ref_format *format, struct used_atom *atom, |
74efea94 | 907 | const char *arg, struct strbuf *err); |
c95b7585 | 908 | } valid_atom[] = { |
1197f1a4 ZH |
909 | [ATOM_REFNAME] = { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
910 | [ATOM_OBJECTTYPE] = { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser }, | |
911 | [ATOM_OBJECTSIZE] = { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser }, | |
912 | [ATOM_OBJECTNAME] = { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser }, | |
913 | [ATOM_DELTABASE] = { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser }, | |
914 | [ATOM_TREE] = { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser }, | |
915 | [ATOM_PARENT] = { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser }, | |
916 | [ATOM_NUMPARENT] = { "numparent", SOURCE_OBJ, FIELD_ULONG }, | |
917 | [ATOM_OBJECT] = { "object", SOURCE_OBJ }, | |
918 | [ATOM_TYPE] = { "type", SOURCE_OBJ }, | |
919 | [ATOM_TAG] = { "tag", SOURCE_OBJ }, | |
920 | [ATOM_AUTHOR] = { "author", SOURCE_OBJ }, | |
a3d2e83a | 921 | [ATOM_AUTHORNAME] = { "authorname", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
1197f1a4 ZH |
922 | [ATOM_AUTHOREMAIL] = { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
923 | [ATOM_AUTHORDATE] = { "authordate", SOURCE_OBJ, FIELD_TIME }, | |
924 | [ATOM_COMMITTER] = { "committer", SOURCE_OBJ }, | |
a3d2e83a | 925 | [ATOM_COMMITTERNAME] = { "committername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
1197f1a4 ZH |
926 | [ATOM_COMMITTEREMAIL] = { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
927 | [ATOM_COMMITTERDATE] = { "committerdate", SOURCE_OBJ, FIELD_TIME }, | |
928 | [ATOM_TAGGER] = { "tagger", SOURCE_OBJ }, | |
a3d2e83a | 929 | [ATOM_TAGGERNAME] = { "taggername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser }, |
1197f1a4 ZH |
930 | [ATOM_TAGGEREMAIL] = { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser }, |
931 | [ATOM_TAGGERDATE] = { "taggerdate", SOURCE_OBJ, FIELD_TIME }, | |
932 | [ATOM_CREATOR] = { "creator", SOURCE_OBJ }, | |
933 | [ATOM_CREATORDATE] = { "creatordate", SOURCE_OBJ, FIELD_TIME }, | |
f5d18f8c | 934 | [ATOM_DESCRIBE] = { "describe", SOURCE_OBJ, FIELD_STR, describe_atom_parser }, |
1197f1a4 ZH |
935 | [ATOM_SUBJECT] = { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser }, |
936 | [ATOM_BODY] = { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser }, | |
937 | [ATOM_TRAILERS] = { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser }, | |
938 | [ATOM_CONTENTS] = { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser }, | |
26c9c03f | 939 | [ATOM_SIGNATURE] = { "signature", SOURCE_OBJ, FIELD_STR, signature_atom_parser }, |
bd0708c7 | 940 | [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser }, |
1197f1a4 ZH |
941 | [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, |
942 | [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, | |
943 | [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, | |
944 | [ATOM_FLAG] = { "flag", SOURCE_NONE }, | |
945 | [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, | |
946 | [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, | |
947 | [ATOM_WORKTREEPATH] = { "worktreepath", SOURCE_NONE }, | |
948 | [ATOM_ALIGN] = { "align", SOURCE_NONE, FIELD_STR, align_atom_parser }, | |
949 | [ATOM_END] = { "end", SOURCE_NONE }, | |
950 | [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser }, | |
951 | [ATOM_THEN] = { "then", SOURCE_NONE }, | |
952 | [ATOM_ELSE] = { "else", SOURCE_NONE }, | |
b9dee075 | 953 | [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser }, |
49abcd21 | 954 | [ATOM_AHEADBEHIND] = { "ahead-behind", SOURCE_OTHER, FIELD_STR, ahead_behind_atom_parser }, |
5a59a230 NTND |
955 | /* |
956 | * Please update $__git_ref_fieldlist in git-completion.bash | |
957 | * when you add new atoms | |
958 | */ | |
c95b7585 KN |
959 | }; |
960 | ||
9865b6e6 | 961 | #define REF_FORMATTING_STATE_INIT { 0 } |
574e96a2 KN |
962 | |
963 | struct ref_formatting_stack { | |
964 | struct ref_formatting_stack *prev; | |
965 | struct strbuf output; | |
c58492d4 | 966 | void (*at_end)(struct ref_formatting_stack **stack); |
ce592082 | 967 | void *at_end_data; |
574e96a2 KN |
968 | }; |
969 | ||
970 | struct ref_formatting_state { | |
971 | int quote_style; | |
972 | struct ref_formatting_stack *stack; | |
973 | }; | |
974 | ||
3a25761a KN |
975 | struct atom_value { |
976 | const char *s; | |
bd0708c7 | 977 | ssize_t s_size; |
3fc8439c OT |
978 | int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state, |
979 | struct strbuf *err); | |
e467dc14 | 980 | uintmax_t value; /* used for sorting when not FIELD_STR */ |
c58fc856 | 981 | struct used_atom *atom; |
3a25761a KN |
982 | }; |
983 | ||
bd0708c7 ZH |
984 | #define ATOM_SIZE_UNSPECIFIED (-1) |
985 | ||
986 | #define ATOM_VALUE_INIT { \ | |
987 | .s_size = ATOM_SIZE_UNSPECIFIED \ | |
988 | } | |
989 | ||
c95b7585 KN |
990 | /* |
991 | * Used to parse format string and sort specifiers | |
992 | */ | |
e85fcb35 | 993 | static int parse_ref_filter_atom(struct ref_format *format, |
e6ff7b3b OT |
994 | const char *atom, const char *ep, |
995 | struct strbuf *err) | |
c95b7585 KN |
996 | { |
997 | const char *sp; | |
4de707ea | 998 | const char *arg; |
e94ce139 | 999 | int i, at, atom_len; |
c95b7585 KN |
1000 | |
1001 | sp = atom; | |
1002 | if (*sp == '*' && sp < ep) | |
1003 | sp++; /* deref */ | |
1004 | if (ep <= sp) | |
e6ff7b3b OT |
1005 | return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), |
1006 | (int)(ep-atom), atom); | |
c95b7585 | 1007 | |
e94ce139 SG |
1008 | /* |
1009 | * If the atom name has a colon, strip it and everything after | |
1010 | * it off - it specifies the format for this entry, and | |
1011 | * shouldn't be used for checking against the valid_atom | |
1012 | * table. | |
1013 | */ | |
1014 | arg = memchr(sp, ':', ep - sp); | |
1015 | atom_len = (arg ? arg : ep) - sp; | |
1016 | ||
bd0708c7 ZH |
1017 | /* Do we have the atom already used elsewhere? */ |
1018 | for (i = 0; i < used_atom_cnt; i++) { | |
1019 | int len = strlen(used_atom[i].name); | |
1020 | if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) | |
1021 | return i; | |
1022 | } | |
1023 | ||
c95b7585 KN |
1024 | /* Is the atom a valid one? */ |
1025 | for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { | |
1026 | int len = strlen(valid_atom[i].name); | |
e94ce139 | 1027 | if (len == atom_len && !memcmp(valid_atom[i].name, sp, len)) |
c95b7585 KN |
1028 | break; |
1029 | } | |
1030 | ||
1031 | if (ARRAY_SIZE(valid_atom) <= i) | |
e6ff7b3b OT |
1032 | return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"), |
1033 | (int)(ep-atom), atom); | |
47bd3d0c SG |
1034 | if (valid_atom[i].source != SOURCE_NONE && !have_git_dir()) |
1035 | return strbuf_addf_ret(err, -1, | |
1036 | _("not a git repository, but the field '%.*s' requires access to object data"), | |
1037 | (int)(ep-atom), atom); | |
c95b7585 KN |
1038 | |
1039 | /* Add it in, including the deref prefix */ | |
1040 | at = used_atom_cnt; | |
1041 | used_atom_cnt++; | |
1042 | REALLOC_ARRAY(used_atom, used_atom_cnt); | |
1197f1a4 | 1043 | used_atom[at].atom_type = i; |
b072add7 KN |
1044 | used_atom[at].name = xmemdupz(atom, ep - atom); |
1045 | used_atom[at].type = valid_atom[i].cmp_type; | |
a8e7e385 | 1046 | used_atom[at].source = valid_atom[i].source; |
aa46a0da OT |
1047 | if (used_atom[at].source == SOURCE_OBJ) { |
1048 | if (*atom == '*') | |
1049 | oi_deref.info.contentp = &oi_deref.content; | |
1050 | else | |
1051 | oi.info.contentp = &oi.content; | |
1052 | } | |
bea4dbea | 1053 | if (arg) { |
4de707ea | 1054 | arg = used_atom[at].name + (arg - atom) + 1; |
bea4dbea TB |
1055 | if (!*arg) { |
1056 | /* | |
1057 | * Treat empty sub-arguments list as NULL (i.e., | |
1058 | * "%(atom:)" is equivalent to "%(atom)"). | |
1059 | */ | |
1060 | arg = NULL; | |
1061 | } | |
1062 | } | |
fd935cc7 | 1063 | memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); |
74efea94 OT |
1064 | if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) |
1065 | return -1; | |
c95b7585 KN |
1066 | if (*atom == '*') |
1067 | need_tagged = 1; | |
1197f1a4 | 1068 | if (i == ATOM_SYMREF) |
c95b7585 KN |
1069 | need_symref = 1; |
1070 | return at; | |
1071 | } | |
1072 | ||
bd0708c7 | 1073 | static void quote_formatting(struct strbuf *s, const char *str, ssize_t len, int quote_style) |
63d89fbc KN |
1074 | { |
1075 | switch (quote_style) { | |
1076 | case QUOTE_NONE: | |
bd0708c7 ZH |
1077 | if (len < 0) |
1078 | strbuf_addstr(s, str); | |
1079 | else | |
1080 | strbuf_add(s, str, len); | |
63d89fbc KN |
1081 | break; |
1082 | case QUOTE_SHELL: | |
1083 | sq_quote_buf(s, str); | |
1084 | break; | |
1085 | case QUOTE_PERL: | |
7121c4d4 ZH |
1086 | if (len < 0) |
1087 | perl_quote_buf(s, str); | |
1088 | else | |
1089 | perl_quote_buf_with_len(s, str, len); | |
63d89fbc KN |
1090 | break; |
1091 | case QUOTE_PYTHON: | |
1092 | python_quote_buf(s, str); | |
1093 | break; | |
1094 | case QUOTE_TCL: | |
1095 | tcl_quote_buf(s, str); | |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | ||
3fc8439c | 1100 | static int append_atom(struct atom_value *v, struct ref_formatting_state *state, |
5fe9e1ce | 1101 | struct strbuf *err UNUSED) |
63d89fbc | 1102 | { |
ce592082 KN |
1103 | /* |
1104 | * Quote formatting is only done when the stack has a single | |
1105 | * element. Otherwise quote formatting is done on the | |
1106 | * element's entire output strbuf when the %(end) atom is | |
1107 | * encountered. | |
1108 | */ | |
1109 | if (!state->stack->prev) | |
bd0708c7 ZH |
1110 | quote_formatting(&state->stack->output, v->s, v->s_size, state->quote_style); |
1111 | else if (v->s_size < 0) | |
ce592082 | 1112 | strbuf_addstr(&state->stack->output, v->s); |
bd0708c7 ZH |
1113 | else |
1114 | strbuf_add(&state->stack->output, v->s, v->s_size); | |
3fc8439c | 1115 | return 0; |
63d89fbc KN |
1116 | } |
1117 | ||
574e96a2 KN |
1118 | static void push_stack_element(struct ref_formatting_stack **stack) |
1119 | { | |
1120 | struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack)); | |
1121 | ||
1122 | strbuf_init(&s->output, 0); | |
1123 | s->prev = *stack; | |
1124 | *stack = s; | |
1125 | } | |
1126 | ||
1127 | static void pop_stack_element(struct ref_formatting_stack **stack) | |
1128 | { | |
1129 | struct ref_formatting_stack *current = *stack; | |
1130 | struct ref_formatting_stack *prev = current->prev; | |
1131 | ||
1132 | if (prev) | |
1133 | strbuf_addbuf(&prev->output, ¤t->output); | |
1134 | strbuf_release(¤t->output); | |
1135 | free(current); | |
1136 | *stack = prev; | |
1137 | } | |
1138 | ||
c58492d4 | 1139 | static void end_align_handler(struct ref_formatting_stack **stack) |
ce592082 | 1140 | { |
c58492d4 KN |
1141 | struct ref_formatting_stack *cur = *stack; |
1142 | struct align *align = (struct align *)cur->at_end_data; | |
ce592082 KN |
1143 | struct strbuf s = STRBUF_INIT; |
1144 | ||
c58492d4 KN |
1145 | strbuf_utf8_align(&s, align->position, align->width, cur->output.buf); |
1146 | strbuf_swap(&cur->output, &s); | |
ce592082 KN |
1147 | strbuf_release(&s); |
1148 | } | |
1149 | ||
3fc8439c | 1150 | static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
5fe9e1ce | 1151 | struct strbuf *err UNUSED) |
ce592082 | 1152 | { |
1472b5bf | 1153 | struct ref_formatting_stack *new_stack; |
ce592082 KN |
1154 | |
1155 | push_stack_element(&state->stack); | |
1472b5bf BW |
1156 | new_stack = state->stack; |
1157 | new_stack->at_end = end_align_handler; | |
1158 | new_stack->at_end_data = &atomv->atom->u.align; | |
3fc8439c | 1159 | return 0; |
ce592082 KN |
1160 | } |
1161 | ||
c58492d4 KN |
1162 | static void if_then_else_handler(struct ref_formatting_stack **stack) |
1163 | { | |
1164 | struct ref_formatting_stack *cur = *stack; | |
1165 | struct ref_formatting_stack *prev = cur->prev; | |
1166 | struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data; | |
1167 | ||
1168 | if (!if_then_else->then_atom_seen) | |
d7d30bad | 1169 | die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then"); |
c58492d4 KN |
1170 | |
1171 | if (if_then_else->else_atom_seen) { | |
1172 | /* | |
1173 | * There is an %(else) atom: we need to drop one state from the | |
1174 | * stack, either the %(else) branch if the condition is satisfied, or | |
1175 | * the %(then) branch if it isn't. | |
1176 | */ | |
1177 | if (if_then_else->condition_satisfied) { | |
1178 | strbuf_reset(&cur->output); | |
1179 | pop_stack_element(&cur); | |
1180 | } else { | |
1181 | strbuf_swap(&cur->output, &prev->output); | |
1182 | strbuf_reset(&cur->output); | |
1183 | pop_stack_element(&cur); | |
1184 | } | |
1185 | } else if (!if_then_else->condition_satisfied) { | |
1186 | /* | |
1187 | * No %(else) atom: just drop the %(then) branch if the | |
1188 | * condition is not satisfied. | |
1189 | */ | |
1190 | strbuf_reset(&cur->output); | |
1191 | } | |
1192 | ||
1193 | *stack = cur; | |
1194 | free(if_then_else); | |
1195 | } | |
1196 | ||
3fc8439c | 1197 | static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
5fe9e1ce | 1198 | struct strbuf *err UNUSED) |
c58492d4 | 1199 | { |
1472b5bf | 1200 | struct ref_formatting_stack *new_stack; |
241b5d3e RS |
1201 | struct if_then_else *if_then_else = xcalloc(1, |
1202 | sizeof(struct if_then_else)); | |
c58492d4 | 1203 | |
4f3e3b37 KN |
1204 | if_then_else->str = atomv->atom->u.if_then_else.str; |
1205 | if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status; | |
1206 | ||
c58492d4 | 1207 | push_stack_element(&state->stack); |
1472b5bf BW |
1208 | new_stack = state->stack; |
1209 | new_stack->at_end = if_then_else_handler; | |
1210 | new_stack->at_end_data = if_then_else; | |
3fc8439c | 1211 | return 0; |
c58492d4 KN |
1212 | } |
1213 | ||
bd0708c7 | 1214 | static int is_empty(struct strbuf *buf) |
c58492d4 | 1215 | { |
bd0708c7 ZH |
1216 | const char *cur = buf->buf; |
1217 | const char *end = buf->buf + buf->len; | |
1218 | ||
1219 | while (cur != end && (isspace(*cur))) | |
1220 | cur++; | |
1221 | ||
1222 | return cur == end; | |
1223 | } | |
c58492d4 | 1224 | |
5fe9e1ce JK |
1225 | static int then_atom_handler(struct atom_value *atomv UNUSED, |
1226 | struct ref_formatting_state *state, | |
3fc8439c | 1227 | struct strbuf *err) |
c58492d4 KN |
1228 | { |
1229 | struct ref_formatting_stack *cur = state->stack; | |
1230 | struct if_then_else *if_then_else = NULL; | |
bd0708c7 | 1231 | size_t str_len = 0; |
c58492d4 KN |
1232 | |
1233 | if (cur->at_end == if_then_else_handler) | |
1234 | if_then_else = (struct if_then_else *)cur->at_end_data; | |
1235 | if (!if_then_else) | |
d7d30bad | 1236 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if"); |
c58492d4 | 1237 | if (if_then_else->then_atom_seen) |
3fc8439c | 1238 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once")); |
c58492d4 | 1239 | if (if_then_else->else_atom_seen) |
3fc8439c | 1240 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)")); |
c58492d4 | 1241 | if_then_else->then_atom_seen = 1; |
bd0708c7 ZH |
1242 | if (if_then_else->str) |
1243 | str_len = strlen(if_then_else->str); | |
c58492d4 | 1244 | /* |
4f3e3b37 KN |
1245 | * If the 'equals' or 'notequals' attribute is used then |
1246 | * perform the required comparison. If not, only non-empty | |
1247 | * strings satisfy the 'if' condition. | |
c58492d4 | 1248 | */ |
4f3e3b37 | 1249 | if (if_then_else->cmp_status == COMPARE_EQUAL) { |
bd0708c7 ZH |
1250 | if (str_len == cur->output.len && |
1251 | !memcmp(if_then_else->str, cur->output.buf, cur->output.len)) | |
4f3e3b37 KN |
1252 | if_then_else->condition_satisfied = 1; |
1253 | } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) { | |
bd0708c7 ZH |
1254 | if (str_len != cur->output.len || |
1255 | memcmp(if_then_else->str, cur->output.buf, cur->output.len)) | |
4f3e3b37 | 1256 | if_then_else->condition_satisfied = 1; |
bd0708c7 | 1257 | } else if (cur->output.len && !is_empty(&cur->output)) |
c58492d4 KN |
1258 | if_then_else->condition_satisfied = 1; |
1259 | strbuf_reset(&cur->output); | |
3fc8439c | 1260 | return 0; |
c58492d4 KN |
1261 | } |
1262 | ||
5fe9e1ce JK |
1263 | static int else_atom_handler(struct atom_value *atomv UNUSED, |
1264 | struct ref_formatting_state *state, | |
3fc8439c | 1265 | struct strbuf *err) |
c58492d4 KN |
1266 | { |
1267 | struct ref_formatting_stack *prev = state->stack; | |
1268 | struct if_then_else *if_then_else = NULL; | |
1269 | ||
1270 | if (prev->at_end == if_then_else_handler) | |
1271 | if_then_else = (struct if_then_else *)prev->at_end_data; | |
1272 | if (!if_then_else) | |
d7d30bad | 1273 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if"); |
c58492d4 | 1274 | if (!if_then_else->then_atom_seen) |
d7d30bad | 1275 | return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then"); |
c58492d4 | 1276 | if (if_then_else->else_atom_seen) |
3fc8439c | 1277 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once")); |
c58492d4 KN |
1278 | if_then_else->else_atom_seen = 1; |
1279 | push_stack_element(&state->stack); | |
1280 | state->stack->at_end_data = prev->at_end_data; | |
1281 | state->stack->at_end = prev->at_end; | |
3fc8439c | 1282 | return 0; |
ce592082 KN |
1283 | } |
1284 | ||
5fe9e1ce JK |
1285 | static int end_atom_handler(struct atom_value *atomv UNUSED, |
1286 | struct ref_formatting_state *state, | |
3fc8439c | 1287 | struct strbuf *err) |
ce592082 KN |
1288 | { |
1289 | struct ref_formatting_stack *current = state->stack; | |
1290 | struct strbuf s = STRBUF_INIT; | |
1291 | ||
1292 | if (!current->at_end) | |
3fc8439c | 1293 | return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom")); |
c58492d4 KN |
1294 | current->at_end(&state->stack); |
1295 | ||
1296 | /* Stack may have been popped within at_end(), hence reset the current pointer */ | |
1297 | current = state->stack; | |
ce592082 KN |
1298 | |
1299 | /* | |
1300 | * Perform quote formatting when the stack element is that of | |
1301 | * a supporting atom. If nested then perform quote formatting | |
1302 | * only on the topmost supporting atom. | |
1303 | */ | |
c58492d4 | 1304 | if (!current->prev->prev) { |
bd0708c7 | 1305 | quote_formatting(&s, current->output.buf, current->output.len, state->quote_style); |
ce592082 KN |
1306 | strbuf_swap(¤t->output, &s); |
1307 | } | |
1308 | strbuf_release(&s); | |
1309 | pop_stack_element(&state->stack); | |
3fc8439c | 1310 | return 0; |
ce592082 KN |
1311 | } |
1312 | ||
c95b7585 KN |
1313 | /* |
1314 | * In a format string, find the next occurrence of %(atom). | |
1315 | */ | |
1316 | static const char *find_next(const char *cp) | |
1317 | { | |
1318 | while (*cp) { | |
1319 | if (*cp == '%') { | |
1320 | /* | |
1321 | * %( is the start of an atom; | |
1322 | * %% is a quoted per-cent. | |
1323 | */ | |
1324 | if (cp[1] == '(') | |
1325 | return cp; | |
1326 | else if (cp[1] == '%') | |
1327 | cp++; /* skip over two % */ | |
1328 | /* otherwise this is a singleton, literal % */ | |
1329 | } | |
1330 | cp++; | |
1331 | } | |
1332 | return NULL; | |
1333 | } | |
1334 | ||
b9dee075 ZH |
1335 | static int reject_atom(enum atom_type atom_type) |
1336 | { | |
1337 | return atom_type == ATOM_REST; | |
1338 | } | |
1339 | ||
c95b7585 KN |
1340 | /* |
1341 | * Make sure the format string is well formed, and parse out | |
1342 | * the used atoms. | |
1343 | */ | |
4a68e36d | 1344 | int verify_ref_format(struct ref_format *format) |
c95b7585 KN |
1345 | { |
1346 | const char *cp, *sp; | |
1347 | ||
bf285ae6 | 1348 | format->need_color_reset_at_eol = 0; |
4a68e36d | 1349 | for (cp = format->format; *cp && (sp = find_next(cp)); ) { |
e6ff7b3b | 1350 | struct strbuf err = STRBUF_INIT; |
c95b7585 KN |
1351 | const char *color, *ep = strchr(sp, ')'); |
1352 | int at; | |
1353 | ||
1354 | if (!ep) | |
1823c619 | 1355 | return error(_("malformed format string %s"), sp); |
c95b7585 | 1356 | /* sp points at "%(" and ep points at the closing ")" */ |
e6ff7b3b OT |
1357 | at = parse_ref_filter_atom(format, sp + 2, ep, &err); |
1358 | if (at < 0) | |
1359 | die("%s", err.buf); | |
b9dee075 ZH |
1360 | if (reject_atom(used_atom[at].atom_type)) |
1361 | die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2); | |
7121c4d4 ZH |
1362 | |
1363 | if ((format->quote_style == QUOTE_PYTHON || | |
1364 | format->quote_style == QUOTE_SHELL || | |
1365 | format->quote_style == QUOTE_TCL) && | |
1366 | used_atom[at].atom_type == ATOM_RAW && | |
1367 | used_atom[at].u.raw_data.option == RAW_BARE) | |
f7337193 | 1368 | die(_("--format=%.*s cannot be used with " |
7121c4d4 | 1369 | "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2); |
c95b7585 KN |
1370 | cp = ep + 1; |
1371 | ||
b072add7 | 1372 | if (skip_prefix(used_atom[at].name, "color:", &color)) |
bf285ae6 | 1373 | format->need_color_reset_at_eol = !!strcmp(color, "reset"); |
e6ff7b3b | 1374 | strbuf_release(&err); |
c95b7585 | 1375 | } |
11b087ad JK |
1376 | if (format->need_color_reset_at_eol && !want_color(format->use_color)) |
1377 | format->need_color_reset_at_eol = 0; | |
c95b7585 KN |
1378 | return 0; |
1379 | } | |
1380 | ||
87d3beb6 HV |
1381 | static const char *do_grab_oid(const char *field, const struct object_id *oid, |
1382 | struct used_atom *atom) | |
c95b7585 | 1383 | { |
87d3beb6 | 1384 | switch (atom->u.oid.option) { |
5101100d HV |
1385 | case O_FULL: |
1386 | return oid_to_hex(oid); | |
1387 | case O_LENGTH: | |
d850b7a5 ÆAB |
1388 | return repo_find_unique_abbrev(the_repository, oid, |
1389 | atom->u.oid.length); | |
5101100d | 1390 | case O_SHORT: |
d850b7a5 ÆAB |
1391 | return repo_find_unique_abbrev(the_repository, oid, |
1392 | DEFAULT_ABBREV); | |
5101100d HV |
1393 | default: |
1394 | BUG("unknown %%(%s) option", field); | |
1395 | } | |
1396 | } | |
1397 | ||
87d3beb6 HV |
1398 | static int grab_oid(const char *name, const char *field, const struct object_id *oid, |
1399 | struct atom_value *v, struct used_atom *atom) | |
c95b7585 | 1400 | { |
5101100d | 1401 | if (starts_with(name, field)) { |
87d3beb6 | 1402 | v->s = xstrdup(do_grab_oid(field, oid, atom)); |
5101100d | 1403 | return 1; |
c95b7585 KN |
1404 | } |
1405 | return 0; | |
1406 | } | |
1407 | ||
1408 | /* See grab_values */ | |
aa46a0da | 1409 | static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) |
c95b7585 KN |
1410 | { |
1411 | int i; | |
1412 | ||
1413 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1414 | const char *name = used_atom[i].name; |
1197f1a4 | 1415 | enum atom_type atom_type = used_atom[i].atom_type; |
c95b7585 KN |
1416 | struct atom_value *v = &val[i]; |
1417 | if (!!deref != (*name == '*')) | |
1418 | continue; | |
1419 | if (deref) | |
1420 | name++; | |
1197f1a4 | 1421 | if (atom_type == ATOM_OBJECTTYPE) |
f0062d3b | 1422 | v->s = xstrdup(type_name(oi->type)); |
1197f1a4 | 1423 | else if (atom_type == ATOM_OBJECTSIZE) { |
0caf20f2 ZH |
1424 | if (used_atom[i].u.objectsize.option == O_SIZE_DISK) { |
1425 | v->value = oi->disk_size; | |
1426 | v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size); | |
1427 | } else if (used_atom[i].u.objectsize.option == O_SIZE) { | |
1428 | v->value = oi->size; | |
1429 | v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size); | |
1430 | } | |
1197f1a4 | 1431 | } else if (atom_type == ATOM_DELTABASE) |
33311fa1 | 1432 | v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); |
1197f1a4 | 1433 | else if (atom_type == ATOM_OBJECTNAME && deref) |
87d3beb6 | 1434 | grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]); |
c95b7585 KN |
1435 | } |
1436 | } | |
1437 | ||
1438 | /* See grab_values */ | |
e0329199 | 1439 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj) |
c95b7585 KN |
1440 | { |
1441 | int i; | |
1442 | struct tag *tag = (struct tag *) obj; | |
1443 | ||
1444 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1445 | const char *name = used_atom[i].name; |
1197f1a4 | 1446 | enum atom_type atom_type = used_atom[i].atom_type; |
c95b7585 KN |
1447 | struct atom_value *v = &val[i]; |
1448 | if (!!deref != (*name == '*')) | |
1449 | continue; | |
1450 | if (deref) | |
1451 | name++; | |
1197f1a4 | 1452 | if (atom_type == ATOM_TAG) |
f0062d3b | 1453 | v->s = xstrdup(tag->tag); |
1197f1a4 | 1454 | else if (atom_type == ATOM_TYPE && tag->tagged) |
f0062d3b | 1455 | v->s = xstrdup(type_name(tag->tagged->type)); |
1197f1a4 | 1456 | else if (atom_type == ATOM_OBJECT && tag->tagged) |
f2fd0760 | 1457 | v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); |
c95b7585 KN |
1458 | } |
1459 | } | |
1460 | ||
1461 | /* See grab_values */ | |
e0329199 | 1462 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj) |
c95b7585 KN |
1463 | { |
1464 | int i; | |
1465 | struct commit *commit = (struct commit *) obj; | |
1466 | ||
1467 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1468 | const char *name = used_atom[i].name; |
1197f1a4 | 1469 | enum atom_type atom_type = used_atom[i].atom_type; |
c95b7585 KN |
1470 | struct atom_value *v = &val[i]; |
1471 | if (!!deref != (*name == '*')) | |
1472 | continue; | |
1473 | if (deref) | |
1474 | name++; | |
1197f1a4 ZH |
1475 | if (atom_type == ATOM_TREE && |
1476 | grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i])) | |
837adb10 | 1477 | continue; |
1197f1a4 | 1478 | if (atom_type == ATOM_NUMPARENT) { |
e467dc14 JS |
1479 | v->value = commit_list_count(commit->parents); |
1480 | v->s = xstrfmt("%lu", (unsigned long)v->value); | |
c95b7585 | 1481 | } |
1197f1a4 | 1482 | else if (atom_type == ATOM_PARENT) { |
c95b7585 | 1483 | struct commit_list *parents; |
a5e03bf5 JK |
1484 | struct strbuf s = STRBUF_INIT; |
1485 | for (parents = commit->parents; parents; parents = parents->next) { | |
26bc0aaf | 1486 | struct object_id *oid = &parents->item->object.oid; |
a5e03bf5 JK |
1487 | if (parents != commit->parents) |
1488 | strbuf_addch(&s, ' '); | |
26bc0aaf | 1489 | strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i])); |
c95b7585 | 1490 | } |
a5e03bf5 | 1491 | v->s = strbuf_detach(&s, NULL); |
c95b7585 KN |
1492 | } |
1493 | } | |
1494 | } | |
1495 | ||
5c326d12 | 1496 | static const char *find_wholine(const char *who, int wholen, const char *buf) |
c95b7585 KN |
1497 | { |
1498 | const char *eol; | |
1499 | while (*buf) { | |
1500 | if (!strncmp(buf, who, wholen) && | |
1501 | buf[wholen] == ' ') | |
1502 | return buf + wholen + 1; | |
1503 | eol = strchr(buf, '\n'); | |
1504 | if (!eol) | |
1505 | return ""; | |
1506 | eol++; | |
1507 | if (*eol == '\n') | |
1508 | return ""; /* end of header */ | |
1509 | buf = eol; | |
1510 | } | |
1511 | return ""; | |
1512 | } | |
1513 | ||
1514 | static const char *copy_line(const char *buf) | |
1515 | { | |
1516 | const char *eol = strchrnul(buf, '\n'); | |
1517 | return xmemdupz(buf, eol - buf); | |
1518 | } | |
1519 | ||
1520 | static const char *copy_name(const char *buf) | |
1521 | { | |
1522 | const char *cp; | |
1523 | for (cp = buf; *cp && *cp != '\n'; cp++) { | |
20869d1a | 1524 | if (starts_with(cp, " <")) |
c95b7585 KN |
1525 | return xmemdupz(buf, cp - buf); |
1526 | } | |
8b3f33ef | 1527 | return xstrdup(""); |
c95b7585 KN |
1528 | } |
1529 | ||
a3d2e83a KS |
1530 | static const char *find_end_of_email(const char *email, int opt) |
1531 | { | |
1532 | const char *eoemail; | |
1533 | ||
1534 | if (opt & EO_LOCALPART) { | |
1535 | eoemail = strchr(email, '@'); | |
1536 | if (eoemail) | |
1537 | return eoemail; | |
1538 | return strchr(email, '>'); | |
1539 | } | |
1540 | ||
1541 | if (opt & EO_TRIM) | |
1542 | return strchr(email, '>'); | |
1543 | ||
1544 | /* | |
1545 | * The option here is either the raw email option or the raw | |
1546 | * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases, | |
1547 | * we directly grab the whole email including the closing | |
1548 | * angle brackets. | |
1549 | * | |
1550 | * If EO_MAILMAP was set with any other option (that is either | |
1551 | * EO_TRIM or EO_LOCALPART), we already grab the end of email | |
1552 | * above. | |
1553 | */ | |
1554 | eoemail = strchr(email, '>'); | |
1555 | if (eoemail) | |
1556 | eoemail++; | |
1557 | return eoemail; | |
1558 | } | |
1559 | ||
b82445dc | 1560 | static const char *copy_email(const char *buf, struct used_atom *atom) |
c95b7585 KN |
1561 | { |
1562 | const char *email = strchr(buf, '<'); | |
1563 | const char *eoemail; | |
a3d2e83a KS |
1564 | int opt = atom->u.email_option.option; |
1565 | ||
c95b7585 | 1566 | if (!email) |
8b3f33ef | 1567 | return xstrdup(""); |
a3d2e83a KS |
1568 | |
1569 | if (opt & (EO_LOCALPART | EO_TRIM)) | |
b82445dc | 1570 | email++; |
b82445dc | 1571 | |
a3d2e83a | 1572 | eoemail = find_end_of_email(email, opt); |
c95b7585 | 1573 | if (!eoemail) |
8b3f33ef | 1574 | return xstrdup(""); |
b82445dc | 1575 | return xmemdupz(email, eoemail - email); |
c95b7585 KN |
1576 | } |
1577 | ||
1578 | static char *copy_subject(const char *buf, unsigned long len) | |
1579 | { | |
9f75ce3d | 1580 | struct strbuf sb = STRBUF_INIT; |
c95b7585 KN |
1581 | int i; |
1582 | ||
9f75ce3d PB |
1583 | for (i = 0; i < len; i++) { |
1584 | if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n') | |
1585 | continue; /* ignore CR in CRLF */ | |
c95b7585 | 1586 | |
9f75ce3d PB |
1587 | if (buf[i] == '\n') |
1588 | strbuf_addch(&sb, ' '); | |
1589 | else | |
1590 | strbuf_addch(&sb, buf[i]); | |
1591 | } | |
1592 | return strbuf_detach(&sb, NULL); | |
c95b7585 KN |
1593 | } |
1594 | ||
1595 | static void grab_date(const char *buf, struct atom_value *v, const char *atomname) | |
1596 | { | |
1597 | const char *eoemail = strstr(buf, "> "); | |
1598 | char *zone; | |
dddbad72 | 1599 | timestamp_t timestamp; |
c95b7585 | 1600 | long tz; |
f1842898 | 1601 | struct date_mode date_mode = DATE_MODE_INIT; |
c95b7585 KN |
1602 | const char *formatp; |
1603 | ||
1604 | /* | |
1605 | * We got here because atomname ends in "date" or "date<something>"; | |
1606 | * it's not possible that <something> is not ":<format>" because | |
1607 | * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no | |
1608 | * ":" means no format is specified, and use the default. | |
1609 | */ | |
1610 | formatp = strchr(atomname, ':'); | |
afe8a907 | 1611 | if (formatp) { |
c95b7585 | 1612 | formatp++; |
d939af12 | 1613 | parse_date_format(formatp, &date_mode); |
46176d77 VD |
1614 | |
1615 | /* | |
1616 | * If this is a sort field and a format was specified, we'll | |
1617 | * want to compare formatted date by string value. | |
1618 | */ | |
1619 | v->atom->type = FIELD_STR; | |
c95b7585 KN |
1620 | } |
1621 | ||
1622 | if (!eoemail) | |
1623 | goto bad; | |
1aeb7e75 | 1624 | timestamp = parse_timestamp(eoemail + 2, &zone, 10); |
dddbad72 | 1625 | if (timestamp == TIME_MAX) |
c95b7585 KN |
1626 | goto bad; |
1627 | tz = strtol(zone, NULL, 10); | |
1628 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) | |
1629 | goto bad; | |
d939af12 | 1630 | v->s = xstrdup(show_date(timestamp, tz, &date_mode)); |
e467dc14 | 1631 | v->value = timestamp; |
974c919d | 1632 | date_mode_release(&date_mode); |
c95b7585 KN |
1633 | return; |
1634 | bad: | |
f0062d3b | 1635 | v->s = xstrdup(""); |
e467dc14 | 1636 | v->value = 0; |
c95b7585 KN |
1637 | } |
1638 | ||
a3d2e83a KS |
1639 | static struct string_list mailmap = STRING_LIST_INIT_NODUP; |
1640 | ||
c95b7585 | 1641 | /* See grab_values */ |
5c326d12 | 1642 | static void grab_person(const char *who, struct atom_value *val, int deref, void *buf) |
c95b7585 KN |
1643 | { |
1644 | int i; | |
1645 | int wholen = strlen(who); | |
1646 | const char *wholine = NULL; | |
a3d2e83a KS |
1647 | const char *headers[] = { "author ", "committer ", |
1648 | "tagger ", NULL }; | |
c95b7585 KN |
1649 | |
1650 | for (i = 0; i < used_atom_cnt; i++) { | |
a3d2e83a KS |
1651 | struct used_atom *atom = &used_atom[i]; |
1652 | const char *name = atom->name; | |
c95b7585 | 1653 | struct atom_value *v = &val[i]; |
a3d2e83a KS |
1654 | struct strbuf mailmap_buf = STRBUF_INIT; |
1655 | ||
c95b7585 KN |
1656 | if (!!deref != (*name == '*')) |
1657 | continue; | |
1658 | if (deref) | |
1659 | name++; | |
1660 | if (strncmp(who, name, wholen)) | |
1661 | continue; | |
1662 | if (name[wholen] != 0 && | |
a3d2e83a | 1663 | !starts_with(name + wholen, "name") && |
b82445dc | 1664 | !starts_with(name + wholen, "email") && |
c95b7585 KN |
1665 | !starts_with(name + wholen, "date")) |
1666 | continue; | |
a3d2e83a KS |
1667 | |
1668 | if ((starts_with(name + wholen, "name") && | |
1669 | (atom->u.name_option.option == N_MAILMAP)) || | |
1670 | (starts_with(name + wholen, "email") && | |
1671 | (atom->u.email_option.option & EO_MAILMAP))) { | |
1672 | if (!mailmap.items) | |
1673 | read_mailmap(&mailmap); | |
1674 | strbuf_addstr(&mailmap_buf, buf); | |
1675 | apply_mailmap_to_header(&mailmap_buf, headers, &mailmap); | |
1676 | wholine = find_wholine(who, wholen, mailmap_buf.buf); | |
1677 | } else { | |
5c326d12 | 1678 | wholine = find_wholine(who, wholen, buf); |
a3d2e83a KS |
1679 | } |
1680 | ||
c95b7585 KN |
1681 | if (!wholine) |
1682 | return; /* no point looking for it */ | |
1683 | if (name[wholen] == 0) | |
1684 | v->s = copy_line(wholine); | |
a3d2e83a | 1685 | else if (starts_with(name + wholen, "name")) |
c95b7585 | 1686 | v->s = copy_name(wholine); |
b82445dc HV |
1687 | else if (starts_with(name + wholen, "email")) |
1688 | v->s = copy_email(wholine, &used_atom[i]); | |
c95b7585 KN |
1689 | else if (starts_with(name + wholen, "date")) |
1690 | grab_date(wholine, v, name); | |
a3d2e83a KS |
1691 | |
1692 | strbuf_release(&mailmap_buf); | |
c95b7585 KN |
1693 | } |
1694 | ||
1695 | /* | |
1696 | * For a tag or a commit object, if "creator" or "creatordate" is | |
1697 | * requested, do something special. | |
1698 | */ | |
1699 | if (strcmp(who, "tagger") && strcmp(who, "committer")) | |
1700 | return; /* "author" for commit object is not wanted */ | |
1701 | if (!wholine) | |
5c326d12 | 1702 | wholine = find_wholine(who, wholen, buf); |
c95b7585 KN |
1703 | if (!wholine) |
1704 | return; | |
1705 | for (i = 0; i < used_atom_cnt; i++) { | |
b072add7 | 1706 | const char *name = used_atom[i].name; |
1197f1a4 | 1707 | enum atom_type atom_type = used_atom[i].atom_type; |
c95b7585 KN |
1708 | struct atom_value *v = &val[i]; |
1709 | if (!!deref != (*name == '*')) | |
1710 | continue; | |
1711 | if (deref) | |
1712 | name++; | |
1713 | ||
1197f1a4 | 1714 | if (atom_type == ATOM_CREATORDATE) |
c95b7585 | 1715 | grab_date(wholine, v, name); |
1197f1a4 | 1716 | else if (atom_type == ATOM_CREATOR) |
c95b7585 KN |
1717 | v->s = copy_line(wholine); |
1718 | } | |
1719 | } | |
1720 | ||
26c9c03f KS |
1721 | static void grab_signature(struct atom_value *val, int deref, struct object *obj) |
1722 | { | |
1723 | int i; | |
1724 | struct commit *commit = (struct commit *) obj; | |
1725 | struct signature_check sigc = { 0 }; | |
1726 | int signature_checked = 0; | |
1727 | ||
1728 | for (i = 0; i < used_atom_cnt; i++) { | |
1729 | struct used_atom *atom = &used_atom[i]; | |
1730 | const char *name = atom->name; | |
1731 | struct atom_value *v = &val[i]; | |
1732 | int opt; | |
1733 | ||
1734 | if (!!deref != (*name == '*')) | |
1735 | continue; | |
1736 | if (deref) | |
1737 | name++; | |
1738 | ||
1739 | if (!skip_prefix(name, "signature", &name) || | |
1740 | (*name && *name != ':')) | |
1741 | continue; | |
1742 | if (!*name) | |
1743 | name = NULL; | |
1744 | else | |
1745 | name++; | |
1746 | ||
1747 | opt = parse_signature_option(name); | |
1748 | if (opt < 0) | |
1749 | continue; | |
1750 | ||
1751 | if (!signature_checked) { | |
1752 | check_commit_signature(commit, &sigc); | |
1753 | signature_checked = 1; | |
1754 | } | |
1755 | ||
1756 | switch (opt) { | |
1757 | case S_BARE: | |
1758 | v->s = xstrdup(sigc.output ? sigc.output: ""); | |
1759 | break; | |
1760 | case S_SIGNER: | |
1761 | v->s = xstrdup(sigc.signer ? sigc.signer : ""); | |
1762 | break; | |
1763 | case S_GRADE: | |
1764 | switch (sigc.result) { | |
1765 | case 'G': | |
1766 | switch (sigc.trust_level) { | |
1767 | case TRUST_UNDEFINED: | |
1768 | case TRUST_NEVER: | |
1769 | v->s = xstrfmt("%c", (char)'U'); | |
1770 | break; | |
1771 | default: | |
1772 | v->s = xstrfmt("%c", (char)'G'); | |
1773 | break; | |
1774 | } | |
1775 | break; | |
1776 | case 'B': | |
1777 | case 'E': | |
1778 | case 'N': | |
1779 | case 'X': | |
1780 | case 'Y': | |
1781 | case 'R': | |
1782 | v->s = xstrfmt("%c", (char)sigc.result); | |
1783 | break; | |
1784 | } | |
1785 | break; | |
1786 | case S_KEY: | |
1787 | v->s = xstrdup(sigc.key ? sigc.key : ""); | |
1788 | break; | |
1789 | case S_FINGERPRINT: | |
1790 | v->s = xstrdup(sigc.fingerprint ? | |
1791 | sigc.fingerprint : ""); | |
1792 | break; | |
1793 | case S_PRI_KEY_FP: | |
1794 | v->s = xstrdup(sigc.primary_key_fingerprint ? | |
1795 | sigc.primary_key_fingerprint : ""); | |
1796 | break; | |
1797 | case S_TRUST_LEVEL: | |
1798 | v->s = xstrdup(gpg_trust_level_to_str(sigc.trust_level)); | |
1799 | break; | |
1800 | } | |
1801 | } | |
1802 | ||
1803 | if (signature_checked) | |
1804 | signature_check_clear(&sigc); | |
1805 | } | |
1806 | ||
5c326d12 | 1807 | static void find_subpos(const char *buf, |
83dff3eb | 1808 | const char **sub, size_t *sublen, |
1809 | const char **body, size_t *bodylen, | |
1810 | size_t *nonsiglen, | |
1811 | const char **sig, size_t *siglen) | |
c95b7585 | 1812 | { |
482c1191 | 1813 | struct strbuf payload = STRBUF_INIT; |
1814 | struct strbuf signature = STRBUF_INIT; | |
c95b7585 | 1815 | const char *eol; |
482c1191 | 1816 | const char *end = buf + strlen(buf); |
1817 | const char *sigstart; | |
1818 | ||
88bce0e2 | 1819 | /* parse signature first; we might not even have a subject line */ |
1820 | parse_signature(buf, end - buf, &payload, &signature); | |
b6046abc | 1821 | strbuf_release(&payload); |
482c1191 | 1822 | |
c95b7585 KN |
1823 | /* skip past header until we hit empty line */ |
1824 | while (*buf && *buf != '\n') { | |
1825 | eol = strchrnul(buf, '\n'); | |
1826 | if (*eol) | |
1827 | eol++; | |
1828 | buf = eol; | |
1829 | } | |
1830 | /* skip any empty lines */ | |
1831 | while (*buf == '\n') | |
1832 | buf++; | |
482c1191 | 1833 | *sig = strbuf_detach(&signature, siglen); |
1834 | sigstart = buf + parse_signed_buffer(buf, strlen(buf)); | |
c95b7585 KN |
1835 | |
1836 | /* subject is first non-empty line */ | |
1837 | *sub = buf; | |
9f75ce3d | 1838 | /* subject goes to first empty line before signature begins */ |
8e1c5fcf JK |
1839 | if ((eol = strstr(*sub, "\n\n")) || |
1840 | (eol = strstr(*sub, "\r\n\r\n"))) { | |
482c1191 | 1841 | eol = eol < sigstart ? eol : sigstart; |
8e1c5fcf | 1842 | } else { |
9f75ce3d | 1843 | /* treat whole message as subject */ |
b01e1c7e | 1844 | eol = sigstart; |
c95b7585 | 1845 | } |
9f75ce3d | 1846 | buf = eol; |
c95b7585 KN |
1847 | *sublen = buf - *sub; |
1848 | /* drop trailing newline, if present */ | |
9f75ce3d PB |
1849 | while (*sublen && ((*sub)[*sublen - 1] == '\n' || |
1850 | (*sub)[*sublen - 1] == '\r')) | |
c95b7585 KN |
1851 | *sublen -= 1; |
1852 | ||
1853 | /* skip any empty lines */ | |
9f75ce3d | 1854 | while (*buf == '\n' || *buf == '\r') |
c95b7585 KN |
1855 | buf++; |
1856 | *body = buf; | |
1857 | *bodylen = strlen(buf); | |
482c1191 | 1858 | *nonsiglen = sigstart - buf; |
c95b7585 KN |
1859 | } |
1860 | ||
1bb38e5a KN |
1861 | /* |
1862 | * If 'lines' is greater than 0, append that many lines from the given | |
1863 | * 'buf' of length 'size' to the given strbuf. | |
1864 | */ | |
1865 | static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines) | |
1866 | { | |
1867 | int i; | |
1868 | const char *sp, *eol; | |
1869 | size_t len; | |
1870 | ||
1871 | sp = buf; | |
1872 | ||
1873 | for (i = 0; i < lines && sp < buf + size; i++) { | |
1874 | if (i) | |
1875 | strbuf_addstr(out, "\n "); | |
1876 | eol = memchr(sp, '\n', size - (sp - buf)); | |
1877 | len = eol ? eol - sp : size - (sp - buf); | |
1878 | strbuf_add(out, sp, len); | |
1879 | if (!eol) | |
1880 | break; | |
1881 | sp = eol + 1; | |
1882 | } | |
1883 | } | |
1884 | ||
f5d18f8c KS |
1885 | static void grab_describe_values(struct atom_value *val, int deref, |
1886 | struct object *obj) | |
1887 | { | |
1888 | struct commit *commit = (struct commit *)obj; | |
1889 | int i; | |
1890 | ||
1891 | for (i = 0; i < used_atom_cnt; i++) { | |
1892 | struct used_atom *atom = &used_atom[i]; | |
1893 | enum atom_type type = atom->atom_type; | |
1894 | const char *name = atom->name; | |
1895 | struct atom_value *v = &val[i]; | |
1896 | ||
1897 | struct child_process cmd = CHILD_PROCESS_INIT; | |
1898 | struct strbuf out = STRBUF_INIT; | |
1899 | struct strbuf err = STRBUF_INIT; | |
1900 | ||
1901 | if (type != ATOM_DESCRIBE) | |
1902 | continue; | |
1903 | ||
1904 | if (!!deref != (*name == '*')) | |
1905 | continue; | |
1906 | ||
1907 | cmd.git_cmd = 1; | |
1908 | strvec_push(&cmd.args, "describe"); | |
1909 | strvec_pushv(&cmd.args, atom->u.describe_args); | |
1910 | strvec_push(&cmd.args, oid_to_hex(&commit->object.oid)); | |
1911 | if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) { | |
1912 | error(_("failed to run 'describe'")); | |
1913 | v->s = xstrdup(""); | |
1914 | continue; | |
1915 | } | |
1916 | strbuf_rtrim(&out); | |
1917 | v->s = strbuf_detach(&out, NULL); | |
1918 | ||
1919 | strbuf_release(&err); | |
1920 | } | |
1921 | } | |
1922 | ||
c95b7585 | 1923 | /* See grab_values */ |
311d0b8e | 1924 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data) |
c95b7585 KN |
1925 | { |
1926 | int i; | |
1927 | const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; | |
83dff3eb | 1928 | size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; |
311d0b8e | 1929 | void *buf = data->content; |
c95b7585 KN |
1930 | |
1931 | for (i = 0; i < used_atom_cnt; i++) { | |
452db397 KN |
1932 | struct used_atom *atom = &used_atom[i]; |
1933 | const char *name = atom->name; | |
c95b7585 | 1934 | struct atom_value *v = &val[i]; |
bd0708c7 | 1935 | enum atom_type atom_type = atom->atom_type; |
482c1191 | 1936 | |
c95b7585 KN |
1937 | if (!!deref != (*name == '*')) |
1938 | continue; | |
1939 | if (deref) | |
1940 | name++; | |
311d0b8e | 1941 | |
bd0708c7 ZH |
1942 | if (atom_type == ATOM_RAW) { |
1943 | unsigned long buf_size = data->size; | |
1944 | ||
1945 | if (atom->u.raw_data.option == RAW_BARE) { | |
1946 | v->s = xmemdupz(buf, buf_size); | |
1947 | v->s_size = buf_size; | |
1948 | } else if (atom->u.raw_data.option == RAW_LENGTH) { | |
6d79cd84 KS |
1949 | v->value = buf_size; |
1950 | v->s = xstrfmt("%"PRIuMAX, v->value); | |
bd0708c7 ZH |
1951 | } |
1952 | continue; | |
1953 | } | |
1954 | ||
311d0b8e ZH |
1955 | if ((data->type != OBJ_TAG && |
1956 | data->type != OBJ_COMMIT) || | |
1957 | (strcmp(name, "body") && | |
1958 | !starts_with(name, "subject") && | |
1959 | !starts_with(name, "trailers") && | |
1960 | !starts_with(name, "contents"))) | |
c95b7585 KN |
1961 | continue; |
1962 | if (!subpos) | |
5c326d12 | 1963 | find_subpos(buf, |
c95b7585 KN |
1964 | &subpos, &sublen, |
1965 | &bodypos, &bodylen, &nonsiglen, | |
1966 | &sigpos, &siglen); | |
1967 | ||
452db397 | 1968 | if (atom->u.contents.option == C_SUB) |
c95b7585 | 1969 | v->s = copy_subject(subpos, sublen); |
905f0a4e HV |
1970 | else if (atom->u.contents.option == C_SUB_SANITIZE) { |
1971 | struct strbuf sb = STRBUF_INIT; | |
1972 | format_sanitized_subject(&sb, subpos, sublen); | |
1973 | v->s = strbuf_detach(&sb, NULL); | |
1974 | } else if (atom->u.contents.option == C_BODY_DEP) | |
c95b7585 | 1975 | v->s = xmemdupz(bodypos, bodylen); |
6d79cd84 KS |
1976 | else if (atom->u.contents.option == C_LENGTH) { |
1977 | v->value = strlen(subpos); | |
1978 | v->s = xstrfmt("%"PRIuMAX, v->value); | |
1979 | } else if (atom->u.contents.option == C_BODY) | |
c95b7585 | 1980 | v->s = xmemdupz(bodypos, nonsiglen); |
452db397 | 1981 | else if (atom->u.contents.option == C_SIG) |
c95b7585 | 1982 | v->s = xmemdupz(sigpos, siglen); |
452db397 | 1983 | else if (atom->u.contents.option == C_LINES) { |
1bb38e5a | 1984 | struct strbuf s = STRBUF_INIT; |
88bce0e2 | 1985 | const char *contents_end = bodypos + nonsiglen; |
1bb38e5a | 1986 | |
1bb38e5a | 1987 | /* Size is the length of the message after removing the signature */ |
452db397 | 1988 | append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines); |
1bb38e5a | 1989 | v->s = strbuf_detach(&s, NULL); |
b1d31c89 | 1990 | } else if (atom->u.contents.option == C_TRAILERS) { |
67a20a00 | 1991 | struct strbuf s = STRBUF_INIT; |
b1d31c89 | 1992 | |
67a20a00 TB |
1993 | /* Format the trailer info according to the trailer_opts given */ |
1994 | format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts); | |
1995 | ||
1996 | v->s = strbuf_detach(&s, NULL); | |
452db397 KN |
1997 | } else if (atom->u.contents.option == C_BARE) |
1998 | v->s = xstrdup(subpos); | |
482c1191 | 1999 | |
c95b7585 | 2000 | } |
482c1191 | 2001 | free((void *)sigpos); |
c95b7585 KN |
2002 | } |
2003 | ||
2004 | /* | |
2005 | * We want to have empty print-string for field requests | |
2006 | * that do not apply (e.g. "authordate" for a tag object) | |
2007 | */ | |
2008 | static void fill_missing_values(struct atom_value *val) | |
2009 | { | |
2010 | int i; | |
2011 | for (i = 0; i < used_atom_cnt; i++) { | |
2012 | struct atom_value *v = &val[i]; | |
afe8a907 | 2013 | if (!v->s) |
f0062d3b | 2014 | v->s = xstrdup(""); |
c95b7585 KN |
2015 | } |
2016 | } | |
2017 | ||
2018 | /* | |
2019 | * val is a list of atom_value to hold returned values. Extract | |
2020 | * the values for atoms in used_atom array out of (obj, buf, sz). | |
2021 | * when deref is false, (obj, buf, sz) is the object that is | |
2022 | * pointed at by the ref itself; otherwise it is the object the | |
2023 | * ref (which is a tag) refers to. | |
2024 | */ | |
311d0b8e | 2025 | static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data) |
c95b7585 | 2026 | { |
311d0b8e ZH |
2027 | void *buf = data->content; |
2028 | ||
c95b7585 KN |
2029 | switch (obj->type) { |
2030 | case OBJ_TAG: | |
e0329199 | 2031 | grab_tag_values(val, deref, obj); |
311d0b8e | 2032 | grab_sub_body_contents(val, deref, data); |
5c326d12 | 2033 | grab_person("tagger", val, deref, buf); |
f5d18f8c | 2034 | grab_describe_values(val, deref, obj); |
c95b7585 KN |
2035 | break; |
2036 | case OBJ_COMMIT: | |
e0329199 | 2037 | grab_commit_values(val, deref, obj); |
311d0b8e | 2038 | grab_sub_body_contents(val, deref, data); |
5c326d12 JK |
2039 | grab_person("author", val, deref, buf); |
2040 | grab_person("committer", val, deref, buf); | |
26c9c03f | 2041 | grab_signature(val, deref, obj); |
f5d18f8c | 2042 | grab_describe_values(val, deref, obj); |
c95b7585 KN |
2043 | break; |
2044 | case OBJ_TREE: | |
2045 | /* grab_tree_values(val, deref, obj, buf, sz); */ | |
bd0708c7 | 2046 | grab_sub_body_contents(val, deref, data); |
c95b7585 KN |
2047 | break; |
2048 | case OBJ_BLOB: | |
2049 | /* grab_blob_values(val, deref, obj, buf, sz); */ | |
bd0708c7 | 2050 | grab_sub_body_contents(val, deref, data); |
c95b7585 KN |
2051 | break; |
2052 | default: | |
2053 | die("Eh? Object of type %d?", obj->type); | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | static inline char *copy_advance(char *dst, const char *src) | |
2058 | { | |
2059 | while (*src) | |
2060 | *dst++ = *src++; | |
2061 | return dst; | |
2062 | } | |
2063 | ||
1a0ca5e3 | 2064 | static const char *lstrip_ref_components(const char *refname, int len) |
0571979b | 2065 | { |
a7984101 | 2066 | long remaining = len; |
f0062d3b OT |
2067 | const char *start = xstrdup(refname); |
2068 | const char *to_free = start; | |
0571979b | 2069 | |
1a0ca5e3 KN |
2070 | if (len < 0) { |
2071 | int i; | |
2072 | const char *p = refname; | |
2073 | ||
2074 | /* Find total no of '/' separated path-components */ | |
2075 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) | |
2076 | ; | |
2077 | /* | |
2078 | * The number of components we need to strip is now | |
2079 | * the total minus the components to be left (Plus one | |
2080 | * because we count the number of '/', but the number | |
2081 | * of components is one more than the no of '/'). | |
2082 | */ | |
2083 | remaining = i + len + 1; | |
2084 | } | |
0571979b | 2085 | |
1a0ca5e3 | 2086 | while (remaining > 0) { |
0571979b JK |
2087 | switch (*start++) { |
2088 | case '\0': | |
f0062d3b OT |
2089 | free((char *)to_free); |
2090 | return xstrdup(""); | |
0571979b JK |
2091 | case '/': |
2092 | remaining--; | |
2093 | break; | |
2094 | } | |
2095 | } | |
1a0ca5e3 | 2096 | |
f0062d3b OT |
2097 | start = xstrdup(start); |
2098 | free((char *)to_free); | |
0571979b JK |
2099 | return start; |
2100 | } | |
2101 | ||
1a34728e KN |
2102 | static const char *rstrip_ref_components(const char *refname, int len) |
2103 | { | |
2104 | long remaining = len; | |
f0062d3b OT |
2105 | const char *start = xstrdup(refname); |
2106 | const char *to_free = start; | |
1a34728e KN |
2107 | |
2108 | if (len < 0) { | |
2109 | int i; | |
2110 | const char *p = refname; | |
2111 | ||
2112 | /* Find total no of '/' separated path-components */ | |
2113 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) | |
2114 | ; | |
2115 | /* | |
2116 | * The number of components we need to strip is now | |
2117 | * the total minus the components to be left (Plus one | |
2118 | * because we count the number of '/', but the number | |
2119 | * of components is one more than the no of '/'). | |
2120 | */ | |
2121 | remaining = i + len + 1; | |
2122 | } | |
2123 | ||
2124 | while (remaining-- > 0) { | |
2125 | char *p = strrchr(start, '/'); | |
afe8a907 | 2126 | if (!p) { |
f0062d3b OT |
2127 | free((char *)to_free); |
2128 | return xstrdup(""); | |
2129 | } else | |
1a34728e KN |
2130 | p[0] = '\0'; |
2131 | } | |
2132 | return start; | |
2133 | } | |
2134 | ||
a7984101 KN |
2135 | static const char *show_ref(struct refname_atom *atom, const char *refname) |
2136 | { | |
2137 | if (atom->option == R_SHORT) | |
2138 | return shorten_unambiguous_ref(refname, warn_ambiguous_refs); | |
17938f17 KN |
2139 | else if (atom->option == R_LSTRIP) |
2140 | return lstrip_ref_components(refname, atom->lstrip); | |
1a34728e KN |
2141 | else if (atom->option == R_RSTRIP) |
2142 | return rstrip_ref_components(refname, atom->rstrip); | |
a7984101 | 2143 | else |
f0062d3b | 2144 | return xstrdup(refname); |
a7984101 KN |
2145 | } |
2146 | ||
5339bdad KN |
2147 | static void fill_remote_ref_details(struct used_atom *atom, const char *refname, |
2148 | struct branch *branch, const char **s) | |
2149 | { | |
2150 | int num_ours, num_theirs; | |
3ba308cb KN |
2151 | if (atom->u.remote_ref.option == RR_REF) |
2152 | *s = show_ref(&atom->u.remote_ref.refname, refname); | |
7743fcca | 2153 | else if (atom->u.remote_ref.option == RR_TRACK) { |
d7d1b496 | 2154 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
c646d093 DR |
2155 | NULL, atom->u.remote_ref.push, |
2156 | AHEAD_BEHIND_FULL) < 0) { | |
6eac70fa | 2157 | *s = xstrdup(msgs.gone); |
7743fcca | 2158 | } else if (!num_ours && !num_theirs) |
f0062d3b | 2159 | *s = xstrdup(""); |
5339bdad | 2160 | else if (!num_ours) |
6eac70fa | 2161 | *s = xstrfmt(msgs.behind, num_theirs); |
5339bdad | 2162 | else if (!num_theirs) |
6eac70fa | 2163 | *s = xstrfmt(msgs.ahead, num_ours); |
5339bdad | 2164 | else |
6eac70fa | 2165 | *s = xstrfmt(msgs.ahead_behind, |
5339bdad | 2166 | num_ours, num_theirs); |
7743fcca KN |
2167 | if (!atom->u.remote_ref.nobracket && *s[0]) { |
2168 | const char *to_free = *s; | |
2169 | *s = xstrfmt("[%s]", *s); | |
2170 | free((void *)to_free); | |
2171 | } | |
2172 | } else if (atom->u.remote_ref.option == RR_TRACKSHORT) { | |
d7d1b496 | 2173 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
c646d093 DR |
2174 | NULL, atom->u.remote_ref.push, |
2175 | AHEAD_BEHIND_FULL) < 0) { | |
f0062d3b | 2176 | *s = xstrdup(""); |
5339bdad | 2177 | return; |
f0062d3b | 2178 | } |
5339bdad | 2179 | if (!num_ours && !num_theirs) |
f0062d3b | 2180 | *s = xstrdup("="); |
5339bdad | 2181 | else if (!num_ours) |
f0062d3b | 2182 | *s = xstrdup("<"); |
5339bdad | 2183 | else if (!num_theirs) |
f0062d3b | 2184 | *s = xstrdup(">"); |
5339bdad | 2185 | else |
f0062d3b | 2186 | *s = xstrdup("<>"); |
cc72385f JS |
2187 | } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { |
2188 | int explicit; | |
2189 | const char *remote = atom->u.remote_ref.push ? | |
2190 | pushremote_for_branch(branch, &explicit) : | |
2191 | remote_for_branch(branch, &explicit); | |
f0062d3b | 2192 | *s = xstrdup(explicit ? remote : ""); |
9700fae5 | 2193 | } else if (atom->u.remote_ref.option == RR_REMOTE_REF) { |
9700fae5 W |
2194 | const char *merge; |
2195 | ||
af8ccd8a JK |
2196 | merge = remote_ref_for_branch(branch, atom->u.remote_ref.push); |
2197 | *s = xstrdup(merge ? merge : ""); | |
3ba308cb | 2198 | } else |
033abf97 | 2199 | BUG("unhandled RR_* enum"); |
5339bdad KN |
2200 | } |
2201 | ||
d4919bb2 KN |
2202 | char *get_head_description(void) |
2203 | { | |
2204 | struct strbuf desc = STRBUF_INIT; | |
2205 | struct wt_status_state state; | |
2206 | memset(&state, 0, sizeof(state)); | |
78845457 | 2207 | wt_status_get_state(the_repository, &state, 1); |
d4919bb2 | 2208 | if (state.rebase_in_progress || |
a236f900 KS |
2209 | state.rebase_interactive_in_progress) { |
2210 | if (state.branch) | |
2708ce62 | 2211 | strbuf_addf(&desc, _("(no branch, rebasing %s)"), |
a236f900 KS |
2212 | state.branch); |
2213 | else | |
2708ce62 | 2214 | strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"), |
a236f900 KS |
2215 | state.detached_from); |
2216 | } else if (state.bisect_in_progress) | |
2708ce62 | 2217 | strbuf_addf(&desc, _("(no branch, bisect started on %s)"), |
990adccb | 2218 | state.bisecting_from); |
d4919bb2 | 2219 | else if (state.detached_from) { |
d4919bb2 | 2220 | if (state.detached_at) |
2708ce62 ÆAB |
2221 | strbuf_addf(&desc, _("(HEAD detached at %s)"), |
2222 | state.detached_from); | |
d4919bb2 | 2223 | else |
2708ce62 ÆAB |
2224 | strbuf_addf(&desc, _("(HEAD detached from %s)"), |
2225 | state.detached_from); | |
2226 | } else | |
2227 | strbuf_addstr(&desc, _("(no branch)")); | |
28438e84 | 2228 | |
abcac2e1 RJ |
2229 | wt_status_state_free_buffers(&state); |
2230 | ||
d4919bb2 KN |
2231 | return strbuf_detach(&desc, NULL); |
2232 | } | |
2233 | ||
a7984101 KN |
2234 | static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) |
2235 | { | |
2236 | if (!ref->symref) | |
f0062d3b | 2237 | return xstrdup(""); |
a7984101 KN |
2238 | else |
2239 | return show_ref(&atom->u.refname, ref->symref); | |
2240 | } | |
2241 | ||
2242 | static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref) | |
2243 | { | |
2244 | if (ref->kind & FILTER_REFS_DETACHED_HEAD) | |
2245 | return get_head_description(); | |
2246 | return show_ref(&atom->u.refname, ref->refname); | |
5339bdad KN |
2247 | } |
2248 | ||
aa46a0da OT |
2249 | static int get_object(struct ref_array_item *ref, int deref, struct object **obj, |
2250 | struct expand_data *oi, struct strbuf *err) | |
2bbc6e8a | 2251 | { |
04f6ee1a OT |
2252 | /* parse_object_buffer() will set eaten to 0 if free() will be needed */ |
2253 | int eaten = 1; | |
aa46a0da OT |
2254 | if (oi->info.contentp) { |
2255 | /* We need to know that to use parse_object_buffer properly */ | |
2256 | oi->info.sizep = &oi->size; | |
2257 | oi->info.typep = &oi->type; | |
2258 | } | |
2259 | if (oid_object_info_extended(the_repository, &oi->oid, &oi->info, | |
2260 | OBJECT_INFO_LOOKUP_REPLACE)) | |
2261 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), | |
2262 | oid_to_hex(&oi->oid), ref->refname); | |
5305a553 OT |
2263 | if (oi->info.disk_sizep && oi->disk_size < 0) |
2264 | BUG("Object size is less than zero."); | |
aa46a0da OT |
2265 | |
2266 | if (oi->info.contentp) { | |
c83149ac | 2267 | *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten); |
c6854508 | 2268 | if (!*obj) { |
aa46a0da OT |
2269 | if (!eaten) |
2270 | free(oi->content); | |
2271 | return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"), | |
2272 | oid_to_hex(&oi->oid), ref->refname); | |
2273 | } | |
311d0b8e | 2274 | grab_values(ref->value, deref, *obj, oi); |
e2255179 | 2275 | } |
aa46a0da OT |
2276 | |
2277 | grab_common_values(ref->value, deref, oi); | |
2bbc6e8a | 2278 | if (!eaten) |
aa46a0da OT |
2279 | free(oi->content); |
2280 | return 0; | |
2bbc6e8a OT |
2281 | } |
2282 | ||
2582083f NB |
2283 | static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees) |
2284 | { | |
2285 | int i; | |
2286 | ||
2287 | for (i = 0; worktrees[i]; i++) { | |
2288 | if (worktrees[i]->head_ref) { | |
2289 | struct ref_to_worktree_entry *entry; | |
2290 | entry = xmalloc(sizeof(*entry)); | |
2291 | entry->wt = worktrees[i]; | |
d22245a2 EW |
2292 | hashmap_entry_init(&entry->ent, |
2293 | strhash(worktrees[i]->head_ref)); | |
2582083f | 2294 | |
b94e5c1d | 2295 | hashmap_add(map, &entry->ent); |
2582083f NB |
2296 | } |
2297 | } | |
2298 | } | |
2299 | ||
2300 | static void lazy_init_worktree_map(void) | |
2301 | { | |
2302 | if (ref_to_worktree_map.worktrees) | |
2303 | return; | |
2304 | ||
03f2465b | 2305 | ref_to_worktree_map.worktrees = get_worktrees(); |
2582083f NB |
2306 | hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0); |
2307 | populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees); | |
2308 | } | |
2309 | ||
fe6258c3 | 2310 | static char *get_worktree_path(const struct ref_array_item *ref) |
2582083f | 2311 | { |
f23a4651 | 2312 | struct hashmap_entry entry, *e; |
2582083f NB |
2313 | struct ref_to_worktree_entry *lookup_result; |
2314 | ||
2315 | lazy_init_worktree_map(); | |
2316 | ||
2317 | hashmap_entry_init(&entry, strhash(ref->refname)); | |
f23a4651 | 2318 | e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname); |
2582083f | 2319 | |
f23a4651 | 2320 | if (!e) |
2582083f | 2321 | return xstrdup(""); |
f23a4651 EW |
2322 | |
2323 | lookup_result = container_of(e, struct ref_to_worktree_entry, ent); | |
2324 | ||
2325 | return xstrdup(lookup_result->wt->path); | |
2582083f NB |
2326 | } |
2327 | ||
c95b7585 KN |
2328 | /* |
2329 | * Parse the object referred by ref, and grab needed value. | |
2330 | */ | |
e339611b | 2331 | static int populate_value(struct ref_array_item *ref, struct strbuf *err) |
c95b7585 | 2332 | { |
c95b7585 | 2333 | struct object *obj; |
2bbc6e8a | 2334 | int i; |
aa46a0da | 2335 | struct object_info empty = OBJECT_INFO_INIT; |
49abcd21 | 2336 | int ahead_behind_atoms = 0; |
c95b7585 | 2337 | |
ca56dadb | 2338 | CALLOC_ARRAY(ref->value, used_atom_cnt); |
c95b7585 KN |
2339 | |
2340 | if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { | |
c95b7585 | 2341 | ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, |
efbd4fdf | 2342 | NULL, NULL); |
c95b7585 | 2343 | if (!ref->symref) |
f0062d3b | 2344 | ref->symref = xstrdup(""); |
c95b7585 KN |
2345 | } |
2346 | ||
2347 | /* Fill in specials first */ | |
2348 | for (i = 0; i < used_atom_cnt; i++) { | |
fd935cc7 | 2349 | struct used_atom *atom = &used_atom[i]; |
1197f1a4 | 2350 | enum atom_type atom_type = atom->atom_type; |
b072add7 | 2351 | const char *name = used_atom[i].name; |
c95b7585 KN |
2352 | struct atom_value *v = &ref->value[i]; |
2353 | int deref = 0; | |
2354 | const char *refname; | |
c95b7585 KN |
2355 | struct branch *branch = NULL; |
2356 | ||
bd0708c7 | 2357 | v->s_size = ATOM_SIZE_UNSPECIFIED; |
63d89fbc | 2358 | v->handler = append_atom; |
6d79cd84 | 2359 | v->value = 0; |
c58fc856 | 2360 | v->atom = atom; |
63d89fbc | 2361 | |
c95b7585 KN |
2362 | if (*name == '*') { |
2363 | deref = 1; | |
2364 | name++; | |
2365 | } | |
2366 | ||
1197f1a4 | 2367 | if (atom_type == ATOM_REFNAME) |
a7984101 | 2368 | refname = get_refname(atom, ref); |
1197f1a4 | 2369 | else if (atom_type == ATOM_WORKTREEPATH) { |
2582083f | 2370 | if (ref->kind == FILTER_REFS_BRANCHES) |
fe6258c3 | 2371 | v->s = get_worktree_path(ref); |
2582083f NB |
2372 | else |
2373 | v->s = xstrdup(""); | |
2374 | continue; | |
2375 | } | |
1197f1a4 | 2376 | else if (atom_type == ATOM_SYMREF) |
a7984101 | 2377 | refname = get_symref(atom, ref); |
1197f1a4 | 2378 | else if (atom_type == ATOM_UPSTREAM) { |
c95b7585 KN |
2379 | const char *branch_name; |
2380 | /* only local branches may have an upstream */ | |
2381 | if (!skip_prefix(ref->refname, "refs/heads/", | |
f0062d3b OT |
2382 | &branch_name)) { |
2383 | v->s = xstrdup(""); | |
c95b7585 | 2384 | continue; |
f0062d3b | 2385 | } |
c95b7585 KN |
2386 | branch = branch_get(branch_name); |
2387 | ||
2388 | refname = branch_get_upstream(branch, NULL); | |
5339bdad KN |
2389 | if (refname) |
2390 | fill_remote_ref_details(atom, refname, branch, &v->s); | |
f0062d3b OT |
2391 | else |
2392 | v->s = xstrdup(""); | |
5339bdad | 2393 | continue; |
1197f1a4 | 2394 | } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) { |
c95b7585 | 2395 | const char *branch_name; |
f0062d3b | 2396 | v->s = xstrdup(""); |
c95b7585 KN |
2397 | if (!skip_prefix(ref->refname, "refs/heads/", |
2398 | &branch_name)) | |
2399 | continue; | |
2400 | branch = branch_get(branch_name); | |
2401 | ||
cc72385f JS |
2402 | if (atom->u.remote_ref.push_remote) |
2403 | refname = NULL; | |
2404 | else { | |
2405 | refname = branch_get_push(branch, NULL); | |
2406 | if (!refname) | |
2407 | continue; | |
2408 | } | |
f0062d3b OT |
2409 | /* We will definitely re-init v->s on the next line. */ |
2410 | free((char *)v->s); | |
5339bdad KN |
2411 | fill_remote_ref_details(atom, refname, branch, &v->s); |
2412 | continue; | |
1197f1a4 | 2413 | } else if (atom_type == ATOM_COLOR) { |
f0062d3b | 2414 | v->s = xstrdup(atom->u.color); |
c95b7585 | 2415 | continue; |
1197f1a4 | 2416 | } else if (atom_type == ATOM_FLAG) { |
c95b7585 KN |
2417 | char buf[256], *cp = buf; |
2418 | if (ref->flag & REF_ISSYMREF) | |
2419 | cp = copy_advance(cp, ",symref"); | |
2420 | if (ref->flag & REF_ISPACKED) | |
2421 | cp = copy_advance(cp, ",packed"); | |
2422 | if (cp == buf) | |
f0062d3b | 2423 | v->s = xstrdup(""); |
c95b7585 KN |
2424 | else { |
2425 | *cp = '\0'; | |
2426 | v->s = xstrdup(buf + 1); | |
2427 | } | |
2428 | continue; | |
1197f1a4 ZH |
2429 | } else if (!deref && atom_type == ATOM_OBJECTNAME && |
2430 | grab_oid(name, "objectname", &ref->objectname, v, atom)) { | |
2431 | continue; | |
2432 | } else if (atom_type == ATOM_HEAD) { | |
613a0e52 | 2433 | if (atom->u.head && !strcmp(ref->refname, atom->u.head)) |
f0062d3b | 2434 | v->s = xstrdup("*"); |
c95b7585 | 2435 | else |
f0062d3b | 2436 | v->s = xstrdup(" "); |
c95b7585 | 2437 | continue; |
1197f1a4 | 2438 | } else if (atom_type == ATOM_ALIGN) { |
ce592082 | 2439 | v->handler = align_atom_handler; |
f0062d3b | 2440 | v->s = xstrdup(""); |
ce592082 | 2441 | continue; |
1197f1a4 | 2442 | } else if (atom_type == ATOM_END) { |
ce592082 | 2443 | v->handler = end_atom_handler; |
f0062d3b | 2444 | v->s = xstrdup(""); |
ce592082 | 2445 | continue; |
1197f1a4 | 2446 | } else if (atom_type == ATOM_IF) { |
4f3e3b37 | 2447 | const char *s; |
4f3e3b37 KN |
2448 | if (skip_prefix(name, "if:", &s)) |
2449 | v->s = xstrdup(s); | |
f0062d3b OT |
2450 | else |
2451 | v->s = xstrdup(""); | |
c58492d4 KN |
2452 | v->handler = if_atom_handler; |
2453 | continue; | |
1197f1a4 | 2454 | } else if (atom_type == ATOM_THEN) { |
c58492d4 | 2455 | v->handler = then_atom_handler; |
f0062d3b | 2456 | v->s = xstrdup(""); |
c58492d4 | 2457 | continue; |
1197f1a4 | 2458 | } else if (atom_type == ATOM_ELSE) { |
c58492d4 | 2459 | v->handler = else_atom_handler; |
f0062d3b | 2460 | v->s = xstrdup(""); |
c58492d4 | 2461 | continue; |
b9dee075 ZH |
2462 | } else if (atom_type == ATOM_REST) { |
2463 | if (ref->rest) | |
2464 | v->s = xstrdup(ref->rest); | |
2465 | else | |
2466 | v->s = xstrdup(""); | |
2467 | continue; | |
49abcd21 DS |
2468 | } else if (atom_type == ATOM_AHEADBEHIND) { |
2469 | if (ref->counts) { | |
2470 | const struct ahead_behind_count *count; | |
2471 | count = ref->counts[ahead_behind_atoms++]; | |
2472 | v->s = xstrfmt("%d %d", count->ahead, count->behind); | |
2473 | } else { | |
2474 | /* Not a commit. */ | |
2475 | v->s = xstrdup(""); | |
2476 | } | |
2477 | continue; | |
c95b7585 KN |
2478 | } else |
2479 | continue; | |
2480 | ||
c95b7585 | 2481 | if (!deref) |
f0062d3b | 2482 | v->s = xstrdup(refname); |
a5e03bf5 JK |
2483 | else |
2484 | v->s = xstrfmt("%s^{}", refname); | |
f0062d3b | 2485 | free((char *)refname); |
c95b7585 KN |
2486 | } |
2487 | ||
2488 | for (i = 0; i < used_atom_cnt; i++) { | |
2489 | struct atom_value *v = &ref->value[i]; | |
aa46a0da OT |
2490 | if (v->s == NULL && used_atom[i].source == SOURCE_NONE) |
2491 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), | |
2492 | oid_to_hex(&ref->objectname), ref->refname); | |
c95b7585 | 2493 | } |
aa46a0da OT |
2494 | |
2495 | if (need_tagged) | |
2496 | oi.info.contentp = &oi.content; | |
2497 | if (!memcmp(&oi.info, &empty, sizeof(empty)) && | |
2498 | !memcmp(&oi_deref.info, &empty, sizeof(empty))) | |
e339611b | 2499 | return 0; |
c95b7585 | 2500 | |
aa46a0da OT |
2501 | |
2502 | oi.oid = ref->objectname; | |
2503 | if (get_object(ref, 0, &obj, &oi, err)) | |
e339611b | 2504 | return -1; |
c95b7585 KN |
2505 | |
2506 | /* | |
2507 | * If there is no atom that wants to know about tagged | |
2508 | * object, we are done. | |
2509 | */ | |
2510 | if (!need_tagged || (obj->type != OBJ_TAG)) | |
e339611b | 2511 | return 0; |
c95b7585 KN |
2512 | |
2513 | /* | |
188782ec VD |
2514 | * If it is a tag object, see if we use the peeled value. If we do, |
2515 | * grab the peeled OID. | |
c95b7585 | 2516 | */ |
188782ec VD |
2517 | if (need_tagged && peel_iterated_oid(&obj->oid, &oi_deref.oid)) |
2518 | die("bad tag"); | |
c95b7585 | 2519 | |
aa46a0da | 2520 | return get_object(ref, 1, &obj, &oi_deref, err); |
c95b7585 KN |
2521 | } |
2522 | ||
2523 | /* | |
2524 | * Given a ref, return the value for the atom. This lazily gets value | |
2525 | * out of the object by calling populate value. | |
2526 | */ | |
e339611b OT |
2527 | static int get_ref_atom_value(struct ref_array_item *ref, int atom, |
2528 | struct atom_value **v, struct strbuf *err) | |
c95b7585 KN |
2529 | { |
2530 | if (!ref->value) { | |
e339611b OT |
2531 | if (populate_value(ref, err)) |
2532 | return -1; | |
c95b7585 KN |
2533 | fill_missing_values(ref->value); |
2534 | } | |
2535 | *v = &ref->value[atom]; | |
e339611b | 2536 | return 0; |
c95b7585 KN |
2537 | } |
2538 | ||
bef0e12b KN |
2539 | /* |
2540 | * Return 1 if the refname matches one of the patterns, otherwise 0. | |
2541 | * A pattern can be a literal prefix (e.g. a refname "refs/heads/master" | |
2542 | * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref | |
2543 | * matches "refs/heads/mas*", too). | |
2544 | */ | |
284c55de JK |
2545 | static int match_pattern(const char **patterns, const char *refname, |
2546 | int ignore_case) | |
bef0e12b | 2547 | { |
3bb16a8b NTND |
2548 | unsigned flags = 0; |
2549 | ||
284c55de | 2550 | if (ignore_case) |
3bb16a8b NTND |
2551 | flags |= WM_CASEFOLD; |
2552 | ||
bef0e12b KN |
2553 | /* |
2554 | * When no '--format' option is given we need to skip the prefix | |
2555 | * for matching refs of tags and branches. | |
2556 | */ | |
2557 | (void)(skip_prefix(refname, "refs/tags/", &refname) || | |
2558 | skip_prefix(refname, "refs/heads/", &refname) || | |
2559 | skip_prefix(refname, "refs/remotes/", &refname) || | |
2560 | skip_prefix(refname, "refs/", &refname)); | |
2561 | ||
2562 | for (; *patterns; patterns++) { | |
55d34269 | 2563 | if (!wildmatch(*patterns, refname, flags)) |
bef0e12b KN |
2564 | return 1; |
2565 | } | |
2566 | return 0; | |
2567 | } | |
2568 | ||
c95b7585 KN |
2569 | /* |
2570 | * Return 1 if the refname matches one of the patterns, otherwise 0. | |
2571 | * A pattern can be path prefix (e.g. a refname "refs/heads/master" | |
bef0e12b KN |
2572 | * matches a pattern "refs/heads/" but not "refs/heads/m") or a |
2573 | * wildcard (e.g. the same ref matches "refs/heads/m*", too). | |
c95b7585 | 2574 | */ |
284c55de JK |
2575 | static int match_name_as_path(const char **pattern, const char *refname, |
2576 | int ignore_case) | |
c95b7585 KN |
2577 | { |
2578 | int namelen = strlen(refname); | |
3bb16a8b NTND |
2579 | unsigned flags = WM_PATHNAME; |
2580 | ||
284c55de | 2581 | if (ignore_case) |
3bb16a8b NTND |
2582 | flags |= WM_CASEFOLD; |
2583 | ||
c95b7585 KN |
2584 | for (; *pattern; pattern++) { |
2585 | const char *p = *pattern; | |
2586 | int plen = strlen(p); | |
2587 | ||
2588 | if ((plen <= namelen) && | |
2589 | !strncmp(refname, p, plen) && | |
2590 | (refname[plen] == '\0' || | |
2591 | refname[plen] == '/' || | |
2592 | p[plen-1] == '/')) | |
2593 | return 1; | |
639ab5ef | 2594 | if (!wildmatch(p, refname, flags)) |
c95b7585 KN |
2595 | return 1; |
2596 | } | |
2597 | return 0; | |
2598 | } | |
2599 | ||
bef0e12b KN |
2600 | /* Return 1 if the refname matches one of the patterns, otherwise 0. */ |
2601 | static int filter_pattern_match(struct ref_filter *filter, const char *refname) | |
2602 | { | |
2603 | if (!*filter->name_patterns) | |
2604 | return 1; /* No pattern always matches */ | |
2605 | if (filter->match_as_path) | |
284c55de JK |
2606 | return match_name_as_path(filter->name_patterns, refname, |
2607 | filter->ignore_case); | |
2608 | return match_pattern(filter->name_patterns, refname, | |
2609 | filter->ignore_case); | |
bef0e12b KN |
2610 | } |
2611 | ||
8255dd8a TB |
2612 | static int filter_exclude_match(struct ref_filter *filter, const char *refname) |
2613 | { | |
2614 | if (!filter->exclude.nr) | |
2615 | return 0; | |
2616 | if (filter->match_as_path) | |
2617 | return match_name_as_path(filter->exclude.v, refname, | |
2618 | filter->ignore_case); | |
2619 | return match_pattern(filter->exclude.v, refname, filter->ignore_case); | |
bef0e12b KN |
2620 | } |
2621 | ||
cfe004a5 JK |
2622 | /* |
2623 | * This is the same as for_each_fullref_in(), but it tries to iterate | |
2624 | * only over the patterns we'll care about. Note that it _doesn't_ do a full | |
2625 | * pattern match, so the callback still has to match each ref individually. | |
2626 | */ | |
2627 | static int for_each_fullref_in_pattern(struct ref_filter *filter, | |
2628 | each_ref_fn cb, | |
67985e4e | 2629 | void *cb_data) |
cfe004a5 | 2630 | { |
cfe004a5 JK |
2631 | if (!filter->match_as_path) { |
2632 | /* | |
2633 | * in this case, the patterns are applied after | |
2634 | * prefixes like "refs/heads/" etc. are stripped off, | |
2635 | * so we have to look at everything: | |
2636 | */ | |
67985e4e | 2637 | return for_each_fullref_in("", cb, cb_data); |
cfe004a5 JK |
2638 | } |
2639 | ||
e674eb25 JK |
2640 | if (filter->ignore_case) { |
2641 | /* | |
2642 | * we can't handle case-insensitive comparisons, | |
2643 | * so just return everything and let the caller | |
2644 | * sort it out. | |
2645 | */ | |
67985e4e | 2646 | return for_each_fullref_in("", cb, cb_data); |
e674eb25 JK |
2647 | } |
2648 | ||
cfe004a5 JK |
2649 | if (!filter->name_patterns[0]) { |
2650 | /* no patterns; we have to look at everything */ | |
59c35fac TB |
2651 | return refs_for_each_fullref_in(get_main_ref_store(the_repository), |
2652 | "", filter->exclude.v, cb, cb_data); | |
cfe004a5 JK |
2653 | } |
2654 | ||
91e2ab15 JK |
2655 | return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository), |
2656 | NULL, filter->name_patterns, | |
59c35fac | 2657 | filter->exclude.v, |
91e2ab15 | 2658 | cb, cb_data); |
cfe004a5 JK |
2659 | } |
2660 | ||
68411046 | 2661 | /* |
c79eddf5 JK |
2662 | * Given a ref (oid, refname), check if the ref belongs to the array |
2663 | * of oids. If the given ref is a tag, check if the given tag points | |
d9e00621 JK |
2664 | * at one of the oids in the given oid array. Returns non-zero if a |
2665 | * match is found. | |
2666 | * | |
68411046 | 2667 | * NEEDSWORK: |
468887f0 | 2668 | * As the refs are cached we might know what refname peels to without |
68411046 KN |
2669 | * the need to parse the object via parse_object(). peel_ref() might be a |
2670 | * more efficient alternative to obtain the pointee. | |
2671 | */ | |
d9e00621 JK |
2672 | static int match_points_at(struct oid_array *points_at, |
2673 | const struct object_id *oid, | |
2674 | const char *refname) | |
68411046 | 2675 | { |
68411046 KN |
2676 | struct object *obj; |
2677 | ||
910650d2 | 2678 | if (oid_array_lookup(points_at, oid) >= 0) |
d9e00621 | 2679 | return 1; |
870eb53a JK |
2680 | obj = parse_object_with_flags(the_repository, oid, |
2681 | PARSE_OBJECT_SKIP_HASH_CHECK); | |
468887f0 | 2682 | while (obj && obj->type == OBJ_TAG) { |
b9584c58 JK |
2683 | struct tag *tag = (struct tag *)obj; |
2684 | ||
2685 | if (parse_tag(tag) < 0) { | |
2686 | obj = NULL; | |
2687 | break; | |
2688 | } | |
2689 | ||
d9e00621 JK |
2690 | if (oid_array_lookup(points_at, get_tagged_oid(tag)) >= 0) |
2691 | return 1; | |
b9584c58 JK |
2692 | |
2693 | obj = tag->tagged; | |
468887f0 | 2694 | } |
68411046 KN |
2695 | if (!obj) |
2696 | die(_("malformed object at '%s'"), refname); | |
d9e00621 | 2697 | return 0; |
68411046 KN |
2698 | } |
2699 | ||
0ffaa00f JK |
2700 | /* |
2701 | * Allocate space for a new ref_array_item and copy the name and oid to it. | |
2702 | * | |
2703 | * Callers can then fill in other struct members at their leisure. | |
2704 | */ | |
c95b7585 | 2705 | static struct ref_array_item *new_ref_array_item(const char *refname, |
0ffaa00f | 2706 | const struct object_id *oid) |
c95b7585 | 2707 | { |
96ffc06f | 2708 | struct ref_array_item *ref; |
0ffaa00f | 2709 | |
96ffc06f | 2710 | FLEX_ALLOC_STR(ref, refname, refname); |
53df97a2 | 2711 | oidcpy(&ref->objectname, oid); |
b9dee075 | 2712 | ref->rest = NULL; |
c95b7585 KN |
2713 | |
2714 | return ref; | |
2715 | } | |
2716 | ||
613d9915 VD |
2717 | static void ref_array_append(struct ref_array *array, struct ref_array_item *ref) |
2718 | { | |
2719 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); | |
2720 | array->items[array->nr++] = ref; | |
2721 | } | |
2722 | ||
427cbc9d JK |
2723 | struct ref_array_item *ref_array_push(struct ref_array *array, |
2724 | const char *refname, | |
2725 | const struct object_id *oid) | |
2726 | { | |
2727 | struct ref_array_item *ref = new_ref_array_item(refname, oid); | |
613d9915 | 2728 | ref_array_append(array, ref); |
c95b7585 KN |
2729 | return ref; |
2730 | } | |
2731 | ||
2111aa79 | 2732 | static int ref_kind_from_refname(const char *refname) |
5b4f2851 KN |
2733 | { |
2734 | unsigned int i; | |
2735 | ||
2736 | static struct { | |
2737 | const char *prefix; | |
2738 | unsigned int kind; | |
2739 | } ref_kind[] = { | |
2740 | { "refs/heads/" , FILTER_REFS_BRANCHES }, | |
2741 | { "refs/remotes/" , FILTER_REFS_REMOTES }, | |
2742 | { "refs/tags/", FILTER_REFS_TAGS} | |
2743 | }; | |
2744 | ||
2111aa79 | 2745 | if (!strcmp(refname, "HEAD")) |
5b4f2851 KN |
2746 | return FILTER_REFS_DETACHED_HEAD; |
2747 | ||
2748 | for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { | |
2749 | if (starts_with(refname, ref_kind[i].prefix)) | |
2750 | return ref_kind[i].kind; | |
2751 | } | |
2752 | ||
2753 | return FILTER_REFS_OTHERS; | |
2754 | } | |
2755 | ||
2111aa79 LP |
2756 | static int filter_ref_kind(struct ref_filter *filter, const char *refname) |
2757 | { | |
2758 | if (filter->kind == FILTER_REFS_BRANCHES || | |
2759 | filter->kind == FILTER_REFS_REMOTES || | |
2760 | filter->kind == FILTER_REFS_TAGS) | |
2761 | return filter->kind; | |
2762 | return ref_kind_from_refname(refname); | |
2763 | } | |
2764 | ||
613d9915 VD |
2765 | static struct ref_array_item *apply_ref_filter(const char *refname, const struct object_id *oid, |
2766 | int flag, struct ref_filter *filter) | |
c95b7585 | 2767 | { |
c95b7585 | 2768 | struct ref_array_item *ref; |
35257aa0 | 2769 | struct commit *commit = NULL; |
5b4f2851 | 2770 | unsigned int kind; |
c95b7585 KN |
2771 | |
2772 | if (flag & REF_BAD_NAME) { | |
1823c619 | 2773 | warning(_("ignoring ref with broken name %s"), refname); |
613d9915 | 2774 | return NULL; |
c95b7585 KN |
2775 | } |
2776 | ||
7ebc8cbe | 2777 | if (flag & REF_ISBROKEN) { |
1823c619 | 2778 | warning(_("ignoring broken ref %s"), refname); |
613d9915 | 2779 | return NULL; |
7ebc8cbe JH |
2780 | } |
2781 | ||
5b4f2851 KN |
2782 | /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ |
2783 | kind = filter_ref_kind(filter, refname); | |
2784 | if (!(kind & filter->kind)) | |
613d9915 | 2785 | return NULL; |
5b4f2851 | 2786 | |
bef0e12b | 2787 | if (!filter_pattern_match(filter, refname)) |
613d9915 | 2788 | return NULL; |
c95b7585 | 2789 | |
8255dd8a | 2790 | if (filter_exclude_match(filter, refname)) |
613d9915 | 2791 | return NULL; |
8255dd8a | 2792 | |
4ce3621a | 2793 | if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname)) |
613d9915 | 2794 | return NULL; |
68411046 | 2795 | |
35257aa0 KN |
2796 | /* |
2797 | * A merge filter is applied on refs pointing to commits. Hence | |
2798 | * obtain the commit using the 'oid' available and discard all | |
2799 | * non-commits early. The actual filtering is done later. | |
2800 | */ | |
21bf9339 AL |
2801 | if (filter->reachable_from || filter->unreachable_from || |
2802 | filter->with_commit || filter->no_commit || filter->verbose) { | |
2803 | commit = lookup_commit_reference_gently(the_repository, oid, 1); | |
35257aa0 | 2804 | if (!commit) |
613d9915 | 2805 | return NULL; |
ac3f5a34 | 2806 | /* We perform the filtering for the '--contains' option... */ |
ee2bd06b | 2807 | if (filter->with_commit && |
6d6e5c53 | 2808 | !commit_contains(filter, commit, filter->with_commit, &filter->internal.contains_cache)) |
613d9915 | 2809 | return NULL; |
ac3f5a34 ÆAB |
2810 | /* ...or for the `--no-contains' option */ |
2811 | if (filter->no_commit && | |
6d6e5c53 | 2812 | commit_contains(filter, commit, filter->no_commit, &filter->internal.no_contains_cache)) |
613d9915 | 2813 | return NULL; |
35257aa0 KN |
2814 | } |
2815 | ||
c95b7585 KN |
2816 | /* |
2817 | * We do not open the object yet; sort may only need refname | |
2818 | * to do its job and the resulting list may yet to be pruned | |
2819 | * by maxcount logic. | |
2820 | */ | |
613d9915 | 2821 | ref = new_ref_array_item(refname, oid); |
35257aa0 | 2822 | ref->commit = commit; |
0ffaa00f | 2823 | ref->flag = flag; |
5b4f2851 | 2824 | ref->kind = kind; |
c95b7585 | 2825 | |
613d9915 VD |
2826 | return ref; |
2827 | } | |
2828 | ||
2829 | struct ref_filter_cbdata { | |
2830 | struct ref_array *array; | |
2831 | struct ref_filter *filter; | |
2832 | }; | |
2833 | ||
2834 | /* | |
2835 | * A call-back given to for_each_ref(). Filter refs and keep them for | |
2836 | * later object processing. | |
2837 | */ | |
2838 | static int filter_one(const char *refname, const struct object_id *oid, int flag, void *cb_data) | |
2839 | { | |
2840 | struct ref_filter_cbdata *ref_cbdata = cb_data; | |
2841 | struct ref_array_item *ref; | |
2842 | ||
2843 | ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter); | |
2844 | if (ref) | |
2845 | ref_array_append(ref_cbdata->array, ref); | |
2846 | ||
c95b7585 KN |
2847 | return 0; |
2848 | } | |
2849 | ||
2850 | /* Free memory allocated for a ref_array_item */ | |
2851 | static void free_array_item(struct ref_array_item *item) | |
2852 | { | |
2853 | free((char *)item->symref); | |
f0062d3b | 2854 | if (item->value) { |
14d30cdf MÅ |
2855 | int i; |
2856 | for (i = 0; i < used_atom_cnt; i++) | |
2857 | free((char *)item->value[i].s); | |
f0062d3b OT |
2858 | free(item->value); |
2859 | } | |
49abcd21 | 2860 | free(item->counts); |
c95b7585 KN |
2861 | free(item); |
2862 | } | |
2863 | ||
bd98f977 VD |
2864 | struct ref_filter_and_format_cbdata { |
2865 | struct ref_filter *filter; | |
2866 | struct ref_format *format; | |
2867 | ||
2868 | struct ref_filter_and_format_internal { | |
2869 | int count; | |
2870 | } internal; | |
2871 | }; | |
2872 | ||
2873 | static int filter_and_format_one(const char *refname, const struct object_id *oid, int flag, void *cb_data) | |
2874 | { | |
2875 | struct ref_filter_and_format_cbdata *ref_cbdata = cb_data; | |
2876 | struct ref_array_item *ref; | |
2877 | struct strbuf output = STRBUF_INIT, err = STRBUF_INIT; | |
2878 | ||
2879 | ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter); | |
2880 | if (!ref) | |
2881 | return 0; | |
2882 | ||
2883 | if (format_ref_array_item(ref, ref_cbdata->format, &output, &err)) | |
2884 | die("%s", err.buf); | |
2885 | ||
2886 | if (output.len || !ref_cbdata->format->array_opts.omit_empty) { | |
2887 | fwrite(output.buf, 1, output.len, stdout); | |
2888 | putchar('\n'); | |
2889 | } | |
2890 | ||
2891 | strbuf_release(&output); | |
2892 | strbuf_release(&err); | |
2893 | free_array_item(ref); | |
2894 | ||
2895 | /* | |
2896 | * Increment the running count of refs that match the filter. If | |
2897 | * max_count is set and we've reached the max, stop the ref | |
2898 | * iteration by returning a nonzero value. | |
2899 | */ | |
2900 | if (ref_cbdata->format->array_opts.max_count && | |
2901 | ++ref_cbdata->internal.count >= ref_cbdata->format->array_opts.max_count) | |
2902 | return 1; | |
2903 | ||
2904 | return 0; | |
2905 | } | |
2906 | ||
c95b7585 KN |
2907 | /* Free all memory allocated for ref_array */ |
2908 | void ref_array_clear(struct ref_array *array) | |
2909 | { | |
2910 | int i; | |
2911 | ||
2912 | for (i = 0; i < array->nr; i++) | |
2913 | free_array_item(array->items[i]); | |
6a83d902 | 2914 | FREE_AND_NULL(array->items); |
c95b7585 | 2915 | array->nr = array->alloc = 0; |
14d30cdf | 2916 | |
d7cf4188 AH |
2917 | for (i = 0; i < used_atom_cnt; i++) { |
2918 | struct used_atom *atom = &used_atom[i]; | |
2919 | if (atom->atom_type == ATOM_HEAD) | |
2920 | free(atom->u.head); | |
2921 | free((char *)atom->name); | |
2922 | } | |
14d30cdf MÅ |
2923 | FREE_AND_NULL(used_atom); |
2924 | used_atom_cnt = 0; | |
2925 | ||
2582083f | 2926 | if (ref_to_worktree_map.worktrees) { |
6da1a258 | 2927 | hashmap_clear_and_free(&(ref_to_worktree_map.map), |
c8e424c9 | 2928 | struct ref_to_worktree_entry, ent); |
2582083f NB |
2929 | free_worktrees(ref_to_worktree_map.worktrees); |
2930 | ref_to_worktree_map.worktrees = NULL; | |
2931 | } | |
49abcd21 DS |
2932 | |
2933 | FREE_AND_NULL(array->counts); | |
c95b7585 KN |
2934 | } |
2935 | ||
a1b19aa5 AL |
2936 | #define EXCLUDE_REACHED 0 |
2937 | #define INCLUDE_REACHED 1 | |
2938 | static void reach_filter(struct ref_array *array, | |
311bfe18 | 2939 | struct commit_list **check_reachable, |
a1b19aa5 | 2940 | int include_reached) |
35257aa0 | 2941 | { |
35257aa0 | 2942 | int i, old_nr; |
5336d506 | 2943 | struct commit **to_clear; |
21bf9339 | 2944 | |
311bfe18 | 2945 | if (!*check_reachable) |
21bf9339 | 2946 | return; |
35257aa0 | 2947 | |
ca56dadb | 2948 | CALLOC_ARRAY(to_clear, array->nr); |
35257aa0 KN |
2949 | for (i = 0; i < array->nr; i++) { |
2950 | struct ref_array_item *item = array->items[i]; | |
35257aa0 KN |
2951 | to_clear[i] = item->commit; |
2952 | } | |
2953 | ||
cbfe360b | 2954 | tips_reachable_from_bases(the_repository, |
311bfe18 | 2955 | *check_reachable, |
cbfe360b DS |
2956 | to_clear, array->nr, |
2957 | UNINTERESTING); | |
35257aa0 KN |
2958 | |
2959 | old_nr = array->nr; | |
2960 | array->nr = 0; | |
2961 | ||
2962 | for (i = 0; i < old_nr; i++) { | |
2963 | struct ref_array_item *item = array->items[i]; | |
2964 | struct commit *commit = item->commit; | |
2965 | ||
2966 | int is_merged = !!(commit->object.flags & UNINTERESTING); | |
2967 | ||
a1b19aa5 | 2968 | if (is_merged == include_reached) |
35257aa0 KN |
2969 | array->items[array->nr++] = array->items[i]; |
2970 | else | |
2971 | free_array_item(item); | |
2972 | } | |
2973 | ||
5dee6d6f | 2974 | clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS); |
21bf9339 | 2975 | |
311bfe18 JK |
2976 | while (*check_reachable) { |
2977 | struct commit *merge_commit = pop_commit(check_reachable); | |
21bf9339 AL |
2978 | clear_commit_marks(merge_commit, ALL_REV_FLAGS); |
2979 | } | |
2980 | ||
35257aa0 KN |
2981 | free(to_clear); |
2982 | } | |
2983 | ||
49abcd21 DS |
2984 | void filter_ahead_behind(struct repository *r, |
2985 | struct ref_format *format, | |
2986 | struct ref_array *array) | |
2987 | { | |
2988 | struct commit **commits; | |
2989 | size_t commits_nr = format->bases.nr + array->nr; | |
2990 | ||
2991 | if (!format->bases.nr || !array->nr) | |
2992 | return; | |
2993 | ||
2994 | ALLOC_ARRAY(commits, commits_nr); | |
2995 | for (size_t i = 0; i < format->bases.nr; i++) | |
2996 | commits[i] = format->bases.items[i].util; | |
2997 | ||
2998 | ALLOC_ARRAY(array->counts, st_mult(format->bases.nr, array->nr)); | |
2999 | ||
3000 | commits_nr = format->bases.nr; | |
3001 | array->counts_nr = 0; | |
3002 | for (size_t i = 0; i < array->nr; i++) { | |
3003 | const char *name = array->items[i]->refname; | |
3004 | commits[commits_nr] = lookup_commit_reference_by_name(name); | |
3005 | ||
3006 | if (!commits[commits_nr]) | |
3007 | continue; | |
3008 | ||
3009 | CALLOC_ARRAY(array->items[i]->counts, format->bases.nr); | |
3010 | for (size_t j = 0; j < format->bases.nr; j++) { | |
3011 | struct ahead_behind_count *count; | |
3012 | count = &array->counts[array->counts_nr++]; | |
3013 | count->tip_index = commits_nr; | |
3014 | count->base_index = j; | |
3015 | ||
3016 | array->items[i]->counts[j] = count; | |
3017 | } | |
3018 | commits_nr++; | |
3019 | } | |
3020 | ||
3021 | ahead_behind(r, commits, commits_nr, array->counts, array->counts_nr); | |
3022 | free(commits); | |
3023 | } | |
3024 | ||
613d9915 | 3025 | static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref_fn fn, void *cb_data) |
14de7fba | 3026 | { |
35257aa0 | 3027 | int ret = 0; |
14de7fba | 3028 | |
5b4f2851 KN |
3029 | filter->kind = type & FILTER_REFS_KIND_MASK; |
3030 | ||
6d6e5c53 VD |
3031 | init_contains_cache(&filter->internal.contains_cache); |
3032 | init_contains_cache(&filter->internal.no_contains_cache); | |
a91aca44 | 3033 | |
35257aa0 | 3034 | /* Simple per-ref filtering */ |
5b4f2851 | 3035 | if (!filter->kind) |
14de7fba | 3036 | die("filter_refs: invalid type"); |
5b4f2851 KN |
3037 | else { |
3038 | /* | |
3039 | * For common cases where we need only branches or remotes or tags, | |
3040 | * we only iterate through those refs. If a mix of refs is needed, | |
3041 | * we iterate over all refs and filter out required refs with the help | |
3042 | * of filter_ref_kind(). | |
3043 | */ | |
3044 | if (filter->kind == FILTER_REFS_BRANCHES) | |
613d9915 | 3045 | ret = for_each_fullref_in("refs/heads/", fn, cb_data); |
5b4f2851 | 3046 | else if (filter->kind == FILTER_REFS_REMOTES) |
613d9915 | 3047 | ret = for_each_fullref_in("refs/remotes/", fn, cb_data); |
5b4f2851 | 3048 | else if (filter->kind == FILTER_REFS_TAGS) |
613d9915 | 3049 | ret = for_each_fullref_in("refs/tags/", fn, cb_data); |
5b4f2851 | 3050 | else if (filter->kind & FILTER_REFS_ALL) |
613d9915 | 3051 | ret = for_each_fullref_in_pattern(filter, fn, cb_data); |
5b4f2851 | 3052 | if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) |
613d9915 | 3053 | head_ref(fn, cb_data); |
5b4f2851 KN |
3054 | } |
3055 | ||
6d6e5c53 VD |
3056 | clear_contains_cache(&filter->internal.contains_cache); |
3057 | clear_contains_cache(&filter->internal.no_contains_cache); | |
35257aa0 | 3058 | |
613d9915 VD |
3059 | return ret; |
3060 | } | |
3061 | ||
3062 | /* | |
3063 | * API for filtering a set of refs. Based on the type of refs the user | |
3064 | * has requested, we iterate through those refs and apply filters | |
3065 | * as per the given ref_filter structure and finally store the | |
3066 | * filtered refs in the ref_array structure. | |
3067 | */ | |
3068 | int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type) | |
3069 | { | |
3070 | struct ref_filter_cbdata ref_cbdata; | |
3071 | int save_commit_buffer_orig; | |
3072 | int ret = 0; | |
3073 | ||
3074 | ref_cbdata.array = array; | |
3075 | ref_cbdata.filter = filter; | |
3076 | ||
3077 | save_commit_buffer_orig = save_commit_buffer; | |
3078 | save_commit_buffer = 0; | |
3079 | ||
3080 | ret = do_filter_refs(filter, type, filter_one, &ref_cbdata); | |
35257aa0 KN |
3081 | |
3082 | /* Filters that need revision walking */ | |
311bfe18 JK |
3083 | reach_filter(array, &filter->reachable_from, INCLUDE_REACHED); |
3084 | reach_filter(array, &filter->unreachable_from, EXCLUDE_REACHED); | |
35257aa0 | 3085 | |
359b01ca | 3086 | save_commit_buffer = save_commit_buffer_orig; |
35257aa0 | 3087 | return ret; |
14de7fba KN |
3088 | } |
3089 | ||
bd98f977 VD |
3090 | static inline int can_do_iterative_format(struct ref_filter *filter, |
3091 | struct ref_sorting *sorting, | |
3092 | struct ref_format *format) | |
3093 | { | |
3094 | /* | |
3095 | * Filtering & formatting results within a single ref iteration | |
3096 | * callback is not compatible with options that require | |
3097 | * post-processing a filtered ref_array. These include: | |
3098 | * - filtering on reachability | |
3099 | * - sorting the filtered results | |
3100 | * - including ahead-behind information in the formatted output | |
3101 | */ | |
3102 | return !(filter->reachable_from || | |
3103 | filter->unreachable_from || | |
3104 | sorting || | |
3105 | format->bases.nr); | |
3106 | } | |
3107 | ||
e7574b0c VD |
3108 | void filter_and_format_refs(struct ref_filter *filter, unsigned int type, |
3109 | struct ref_sorting *sorting, | |
3110 | struct ref_format *format) | |
3111 | { | |
bd98f977 VD |
3112 | if (can_do_iterative_format(filter, sorting, format)) { |
3113 | int save_commit_buffer_orig; | |
3114 | struct ref_filter_and_format_cbdata ref_cbdata = { | |
3115 | .filter = filter, | |
3116 | .format = format, | |
3117 | }; | |
3118 | ||
3119 | save_commit_buffer_orig = save_commit_buffer; | |
3120 | save_commit_buffer = 0; | |
3121 | ||
3122 | do_filter_refs(filter, type, filter_and_format_one, &ref_cbdata); | |
3123 | ||
3124 | save_commit_buffer = save_commit_buffer_orig; | |
3125 | } else { | |
3126 | struct ref_array array = { 0 }; | |
3127 | filter_refs(&array, filter, type); | |
3128 | filter_ahead_behind(the_repository, format, &array); | |
3129 | ref_array_sort(sorting, &array); | |
3130 | print_formatted_ref_array(&array, format); | |
3131 | ref_array_clear(&array); | |
3132 | } | |
e7574b0c VD |
3133 | } |
3134 | ||
2708ce62 ÆAB |
3135 | static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b) |
3136 | { | |
3137 | if (!(a->kind ^ b->kind)) | |
3138 | BUG("ref_kind_from_refname() should only mark one ref as HEAD"); | |
3139 | if (a->kind & FILTER_REFS_DETACHED_HEAD) | |
3140 | return -1; | |
3141 | else if (b->kind & FILTER_REFS_DETACHED_HEAD) | |
3142 | return 1; | |
3143 | BUG("should have died in the xor check above"); | |
3144 | return 0; | |
3145 | } | |
3146 | ||
bd0708c7 ZH |
3147 | static int memcasecmp(const void *vs1, const void *vs2, size_t n) |
3148 | { | |
3149 | const char *s1 = vs1, *s2 = vs2; | |
3150 | const char *end = s1 + n; | |
3151 | ||
3152 | for (; s1 < end; s1++, s2++) { | |
3153 | int diff = tolower(*s1) - tolower(*s2); | |
3154 | if (diff) | |
3155 | return diff; | |
3156 | } | |
3157 | return 0; | |
3158 | } | |
3159 | ||
98e7ab6d JH |
3160 | struct ref_sorting { |
3161 | struct ref_sorting *next; | |
3162 | int atom; /* index into used_atom array (internal) */ | |
3163 | enum ref_sorting_order sort_flags; | |
3164 | }; | |
3165 | ||
c95b7585 KN |
3166 | static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b) |
3167 | { | |
3168 | struct atom_value *va, *vb; | |
3169 | int cmp; | |
4045f659 | 3170 | int cmp_detached_head = 0; |
b072add7 | 3171 | cmp_type cmp_type = used_atom[s->atom].type; |
e339611b | 3172 | struct strbuf err = STRBUF_INIT; |
c95b7585 | 3173 | |
e339611b OT |
3174 | if (get_ref_atom_value(a, s->atom, &va, &err)) |
3175 | die("%s", err.buf); | |
3176 | if (get_ref_atom_value(b, s->atom, &vb, &err)) | |
3177 | die("%s", err.buf); | |
3178 | strbuf_release(&err); | |
2708ce62 ÆAB |
3179 | if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST && |
3180 | ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) { | |
3181 | cmp = compare_detached_head(a, b); | |
4045f659 | 3182 | cmp_detached_head = 1; |
2708ce62 | 3183 | } else if (s->sort_flags & REF_SORTING_VERSION) { |
90c00408 | 3184 | cmp = versioncmp(va->s, vb->s); |
75c50e59 | 3185 | } else if (cmp_type == FIELD_STR) { |
bd0708c7 ZH |
3186 | if (va->s_size < 0 && vb->s_size < 0) { |
3187 | int (*cmp_fn)(const char *, const char *); | |
3188 | cmp_fn = s->sort_flags & REF_SORTING_ICASE | |
3189 | ? strcasecmp : strcmp; | |
3190 | cmp = cmp_fn(va->s, vb->s); | |
3191 | } else { | |
3192 | size_t a_size = va->s_size < 0 ? | |
3193 | strlen(va->s) : va->s_size; | |
3194 | size_t b_size = vb->s_size < 0 ? | |
3195 | strlen(vb->s) : vb->s_size; | |
3196 | int (*cmp_fn)(const void *, const void *, size_t); | |
3197 | cmp_fn = s->sort_flags & REF_SORTING_ICASE | |
3198 | ? memcasecmp : memcmp; | |
3199 | ||
3200 | cmp = cmp_fn(va->s, vb->s, b_size > a_size ? | |
3201 | a_size : b_size); | |
3202 | if (!cmp) { | |
3203 | if (a_size > b_size) | |
3204 | cmp = 1; | |
3205 | else if (a_size < b_size) | |
3206 | cmp = -1; | |
3207 | } | |
3208 | } | |
75c50e59 | 3209 | } else { |
e467dc14 | 3210 | if (va->value < vb->value) |
c95b7585 | 3211 | cmp = -1; |
e467dc14 | 3212 | else if (va->value == vb->value) |
7c5045fc | 3213 | cmp = 0; |
c95b7585 KN |
3214 | else |
3215 | cmp = 1; | |
c95b7585 | 3216 | } |
90c00408 | 3217 | |
4045f659 ÆAB |
3218 | return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head) |
3219 | ? -cmp : cmp; | |
c95b7585 KN |
3220 | } |
3221 | ||
83fc4d64 | 3222 | static int compare_refs(const void *a_, const void *b_, void *ref_sorting) |
c95b7585 KN |
3223 | { |
3224 | struct ref_array_item *a = *((struct ref_array_item **)a_); | |
3225 | struct ref_array_item *b = *((struct ref_array_item **)b_); | |
3226 | struct ref_sorting *s; | |
3227 | ||
3228 | for (s = ref_sorting; s; s = s->next) { | |
3229 | int cmp = cmp_ref_sorting(s, a, b); | |
3230 | if (cmp) | |
3231 | return cmp; | |
3232 | } | |
7c5045fc | 3233 | s = ref_sorting; |
7c269a7b | 3234 | return s && s->sort_flags & REF_SORTING_ICASE ? |
7c5045fc JK |
3235 | strcasecmp(a->refname, b->refname) : |
3236 | strcmp(a->refname, b->refname); | |
c95b7585 KN |
3237 | } |
3238 | ||
7c269a7b ÆAB |
3239 | void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, |
3240 | unsigned int mask, int on) | |
76f9e569 | 3241 | { |
7c269a7b ÆAB |
3242 | for (; sorting; sorting = sorting->next) { |
3243 | if (on) | |
3244 | sorting->sort_flags |= mask; | |
3245 | else | |
3246 | sorting->sort_flags &= ~mask; | |
3247 | } | |
c95b7585 KN |
3248 | } |
3249 | ||
3250 | void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) | |
3251 | { | |
56d26ade VD |
3252 | if (sorting) |
3253 | QSORT_S(array->items, array->nr, compare_refs, sorting); | |
c95b7585 KN |
3254 | } |
3255 | ||
574e96a2 | 3256 | static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) |
c95b7585 | 3257 | { |
574e96a2 KN |
3258 | struct strbuf *s = &state->stack->output; |
3259 | ||
c95b7585 KN |
3260 | while (*cp && (!ep || cp < ep)) { |
3261 | if (*cp == '%') { | |
3262 | if (cp[1] == '%') | |
3263 | cp++; | |
3264 | else { | |
d2330973 | 3265 | int ch = hex2chr(cp + 1); |
c95b7585 | 3266 | if (0 <= ch) { |
574e96a2 | 3267 | strbuf_addch(s, ch); |
c95b7585 KN |
3268 | cp += 3; |
3269 | continue; | |
3270 | } | |
3271 | } | |
3272 | } | |
574e96a2 | 3273 | strbuf_addch(s, *cp); |
c95b7585 KN |
3274 | cp++; |
3275 | } | |
3276 | } | |
3277 | ||
3019eca9 | 3278 | int format_ref_array_item(struct ref_array_item *info, |
e85fcb35 ZH |
3279 | struct ref_format *format, |
3280 | struct strbuf *final_buf, | |
3281 | struct strbuf *error_buf) | |
c95b7585 KN |
3282 | { |
3283 | const char *cp, *sp, *ep; | |
574e96a2 KN |
3284 | struct ref_formatting_state state = REF_FORMATTING_STATE_INIT; |
3285 | ||
4a68e36d | 3286 | state.quote_style = format->quote_style; |
574e96a2 | 3287 | push_stack_element(&state.stack); |
c95b7585 | 3288 | |
4a68e36d | 3289 | for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) { |
c95b7585 | 3290 | struct atom_value *atomv; |
e6ff7b3b | 3291 | int pos; |
c95b7585 KN |
3292 | |
3293 | ep = strchr(sp, ')'); | |
3294 | if (cp < sp) | |
574e96a2 | 3295 | append_literal(cp, sp, &state); |
e6ff7b3b | 3296 | pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf); |
e339611b OT |
3297 | if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) || |
3298 | atomv->handler(atomv, &state, error_buf)) { | |
3fc8439c OT |
3299 | pop_stack_element(&state.stack); |
3300 | return -1; | |
3301 | } | |
c95b7585 KN |
3302 | } |
3303 | if (*cp) { | |
3304 | sp = cp + strlen(cp); | |
574e96a2 | 3305 | append_literal(cp, sp, &state); |
c95b7585 | 3306 | } |
bf285ae6 | 3307 | if (format->need_color_reset_at_eol) { |
bd0708c7 | 3308 | struct atom_value resetv = ATOM_VALUE_INIT; |
51331aad | 3309 | resetv.s = GIT_COLOR_RESET; |
3fc8439c OT |
3310 | if (append_atom(&resetv, &state, error_buf)) { |
3311 | pop_stack_element(&state.stack); | |
3312 | return -1; | |
3313 | } | |
c95b7585 | 3314 | } |
3019eca9 OT |
3315 | if (state.stack->prev) { |
3316 | pop_stack_element(&state.stack); | |
3317 | return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing")); | |
c95b7585 | 3318 | } |
99c6a71d | 3319 | strbuf_addbuf(final_buf, &state.stack->output); |
574e96a2 | 3320 | pop_stack_element(&state.stack); |
3019eca9 | 3321 | return 0; |
99c6a71d KN |
3322 | } |
3323 | ||
e7574b0c VD |
3324 | void print_formatted_ref_array(struct ref_array *array, struct ref_format *format) |
3325 | { | |
3326 | int total; | |
3327 | struct strbuf output = STRBUF_INIT, err = STRBUF_INIT; | |
3328 | ||
3329 | total = format->array_opts.max_count; | |
3330 | if (!total || array->nr < total) | |
3331 | total = array->nr; | |
3332 | for (int i = 0; i < total; i++) { | |
3333 | strbuf_reset(&err); | |
3334 | strbuf_reset(&output); | |
3335 | if (format_ref_array_item(array->items[i], format, &output, &err)) | |
3336 | die("%s", err.buf); | |
3337 | if (output.len || !format->array_opts.omit_empty) { | |
3338 | fwrite(output.buf, 1, output.len, stdout); | |
3339 | putchar('\n'); | |
3340 | } | |
3341 | } | |
3342 | ||
3343 | strbuf_release(&err); | |
3344 | strbuf_release(&output); | |
3345 | } | |
3346 | ||
53df97a2 | 3347 | void pretty_print_ref(const char *name, const struct object_id *oid, |
e85fcb35 | 3348 | struct ref_format *format) |
2111aa79 LP |
3349 | { |
3350 | struct ref_array_item *ref_item; | |
22f69a85 ZH |
3351 | struct strbuf output = STRBUF_INIT; |
3352 | struct strbuf err = STRBUF_INIT; | |
3353 | ||
0ffaa00f | 3354 | ref_item = new_ref_array_item(name, oid); |
2111aa79 | 3355 | ref_item->kind = ref_kind_from_refname(name); |
22f69a85 ZH |
3356 | if (format_ref_array_item(ref_item, format, &output, &err)) |
3357 | die("%s", err.buf); | |
3358 | fwrite(output.buf, 1, output.len, stdout); | |
3359 | putchar('\n'); | |
3360 | ||
3361 | strbuf_release(&err); | |
3362 | strbuf_release(&output); | |
2111aa79 LP |
3363 | free_array_item(ref_item); |
3364 | } | |
3365 | ||
29ef53cd JK |
3366 | static int parse_sorting_atom(const char *atom) |
3367 | { | |
ab7ded34 JK |
3368 | /* |
3369 | * This parses an atom using a dummy ref_format, since we don't | |
3370 | * actually care about the formatting details. | |
3371 | */ | |
3372 | struct ref_format dummy = REF_FORMAT_INIT; | |
29ef53cd | 3373 | const char *end = atom + strlen(atom); |
e6ff7b3b OT |
3374 | struct strbuf err = STRBUF_INIT; |
3375 | int res = parse_ref_filter_atom(&dummy, atom, end, &err); | |
3376 | if (res < 0) | |
3377 | die("%s", err.buf); | |
3378 | strbuf_release(&err); | |
3379 | return res; | |
29ef53cd JK |
3380 | } |
3381 | ||
98e7ab6d | 3382 | static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg) |
c95b7585 | 3383 | { |
c95b7585 | 3384 | struct ref_sorting *s; |
c95b7585 | 3385 | |
ca56dadb | 3386 | CALLOC_ARRAY(s, 1); |
c95b7585 KN |
3387 | s->next = *sorting_tail; |
3388 | *sorting_tail = s; | |
3389 | ||
3390 | if (*arg == '-') { | |
7c269a7b | 3391 | s->sort_flags |= REF_SORTING_REVERSE; |
c95b7585 KN |
3392 | arg++; |
3393 | } | |
90c00408 KN |
3394 | if (skip_prefix(arg, "version:", &arg) || |
3395 | skip_prefix(arg, "v:", &arg)) | |
7c269a7b | 3396 | s->sort_flags |= REF_SORTING_VERSION; |
29ef53cd | 3397 | s->atom = parse_sorting_atom(arg); |
18a25650 JK |
3398 | } |
3399 | ||
98e7ab6d | 3400 | struct ref_sorting *ref_sorting_options(struct string_list *options) |
18a25650 | 3401 | { |
98e7ab6d JH |
3402 | struct string_list_item *item; |
3403 | struct ref_sorting *sorting = NULL, **tail = &sorting; | |
3404 | ||
56d26ade | 3405 | if (options->nr) { |
98e7ab6d JH |
3406 | for_each_string_list_item(item, options) |
3407 | parse_ref_sorting(tail, item->string); | |
3408 | } | |
3409 | ||
95be717c | 3410 | /* |
98e7ab6d JH |
3411 | * From here on, the ref_sorting list should be used to talk |
3412 | * about the sort order used for the output. The caller | |
3413 | * should not touch the string form anymore. | |
95be717c | 3414 | */ |
98e7ab6d JH |
3415 | string_list_clear(options, 0); |
3416 | return sorting; | |
c95b7585 | 3417 | } |
5afcb905 | 3418 | |
e5fb0286 ÆAB |
3419 | void ref_sorting_release(struct ref_sorting *sorting) |
3420 | { | |
3421 | while (sorting) { | |
3422 | struct ref_sorting *next = sorting->next; | |
3423 | free(sorting); | |
3424 | sorting = next; | |
3425 | } | |
3426 | } | |
3427 | ||
5afcb905 KN |
3428 | int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset) |
3429 | { | |
3430 | struct ref_filter *rf = opt->value; | |
1e43ed98 | 3431 | struct object_id oid; |
21bf9339 | 3432 | struct commit *merge_commit; |
5afcb905 | 3433 | |
517fe807 JK |
3434 | BUG_ON_OPT_NEG(unset); |
3435 | ||
d850b7a5 | 3436 | if (repo_get_oid(the_repository, arg, &oid)) |
5afcb905 KN |
3437 | die(_("malformed object name %s"), arg); |
3438 | ||
21bf9339 AL |
3439 | merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0); |
3440 | ||
3441 | if (!merge_commit) | |
9440b831 | 3442 | return error(_("option `%s' must point to a commit"), opt->long_name); |
5afcb905 | 3443 | |
21bf9339 AL |
3444 | if (starts_with(opt->long_name, "no")) |
3445 | commit_list_insert(merge_commit, &rf->unreachable_from); | |
3446 | else | |
3447 | commit_list_insert(merge_commit, &rf->reachable_from); | |
3448 | ||
5afcb905 KN |
3449 | return 0; |
3450 | } | |
b571fb98 JK |
3451 | |
3452 | void ref_filter_init(struct ref_filter *filter) | |
3453 | { | |
3454 | struct ref_filter blank = REF_FILTER_INIT; | |
3455 | memcpy(filter, &blank, sizeof(blank)); | |
3456 | } | |
3457 | ||
3458 | void ref_filter_clear(struct ref_filter *filter) | |
3459 | { | |
8255dd8a | 3460 | strvec_clear(&filter->exclude); |
b571fb98 JK |
3461 | oid_array_clear(&filter->points_at); |
3462 | free_commit_list(filter->with_commit); | |
3463 | free_commit_list(filter->no_commit); | |
3464 | free_commit_list(filter->reachable_from); | |
3465 | free_commit_list(filter->unreachable_from); | |
3466 | ref_filter_init(filter); | |
3467 | } |