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