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