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