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