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