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