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