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