ref-filter: introduce objectname_atom_parser()

Introduce objectname_atom_parser() which will parse the '%(objectname)' atom and store information into the 'used_atom' structure based on the modifiers used along with the atom. Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Feb 17, 2016 at 23:36 UTC fe63c4d110acd348244bb0065518a62a1a8dbb00
1 file changed +24 -11
ref-filter.c
+24 -11
@@ -43,6 +43,7 @@ static struct used_atom {
43 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB } option;
44 unsigned int nlines;
45 } contents;
46 + enum { O_FULL, O_SHORT } objectname;
47 } u;
48 } *used_atom;
49 static int used_atom_cnt, need_tagged, need_symref;
@@ -102,6 +103,16 @@ static void contents_atom_parser(struct used_atom *atom, const char *arg)
103 die(_("unrecognized %%(contents) argument: %s"), arg);
104 }
105
106 +static void objectname_atom_parser(struct used_atom *atom, const char *arg)
107 +{
108 + if (!arg)
109 + atom->u.objectname = O_FULL;
110 + else if (!strcmp(arg, "short"))
111 + atom->u.objectname = O_SHORT;
112 + else
113 + die(_("unrecognized %%(objectname) argument: %s"), arg);
114 +}
115 +
116 static align_type parse_align_position(const char *s)
117 {
118 if (!strcmp(s, "right"))
@@ -160,7 +171,7 @@ static struct {
171 { "refname" },
172 { "objecttype" },
173 { "objectsize", FIELD_ULONG },
163 - { "objectname" },
174 + { "objectname", FIELD_STR, objectname_atom_parser },
175 { "tree" },
176 { "parent" },
177 { "numparent", FIELD_ULONG },
@@ -440,15 +451,17 @@ static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned lo
451 }
452
453 static int grab_objectname(const char *name, const unsigned char *sha1,
443 - struct atom_value *v)
454 + struct atom_value *v, struct used_atom *atom)
455 {
445 - if (!strcmp(name, "objectname")) {
446 - v->s = xstrdup(sha1_to_hex(sha1));
447 - return 1;
448 - }
449 - if (!strcmp(name, "objectname:short")) {
450 - v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
451 - return 1;
456 + if (starts_with(name, "objectname")) {
457 + if (atom->u.objectname == O_SHORT) {
458 + v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
459 + return 1;
460 + } else if (atom->u.objectname == O_FULL) {
461 + v->s = xstrdup(sha1_to_hex(sha1));
462 + return 1;
463 + } else
464 + die("BUG: unknown %%(objectname) option");
465 }
466 return 0;
467 }
@@ -472,7 +485,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
485 v->s = xstrfmt("%lu", sz);
486 }
487 else if (deref)
475 - grab_objectname(name, obj->oid.hash, v);
488 + grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
489 }
490 }
491
@@ -996,7 +1009,7 @@ static void populate_value(struct ref_array_item *ref)
1009 v->s = xstrdup(buf + 1);
1010 }
1011 continue;
999 - } else if (!deref && grab_objectname(name, ref->objectname, v)) {
1012 + } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
1013 continue;
1014 } else if (!strcmp(name, "HEAD")) {
1015 const char *head;