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