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