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