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