@matheusbsbs / matheus-bagliano / commits / 5101100dcc
Fork 0

ref-filter: refactor `grab_objectname()`

Prepares `grab_objectname()` for more generic usage. This change will allow us to reuse `grab_objectname()` for the `tree` and `parent` atoms in a following commit. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hariom Verma committed Aug 21, 2020 at 21:41 UTC 5101100dcc741694a1ca355c2e26e20daa348a28
1 file changed +21 -15
ref-filter.c
+21 -15
@@ -918,21 +918,27 @@ int verify_ref_format(struct ref_format *format)
918 return 0;
919 }
920
921 -static int grab_objectname(const char *name, const struct object_id *oid,
921 +static const char *do_grab_objectname(const char *field, const struct object_id *oid,
922 + struct used_atom *atom)
923 +{
924 + switch (atom->u.objectname.option) {
925 + case O_FULL:
926 + return oid_to_hex(oid);
927 + case O_LENGTH:
928 + return find_unique_abbrev(oid, atom->u.objectname.length);
929 + case O_SHORT:
930 + return find_unique_abbrev(oid, DEFAULT_ABBREV);
931 + default:
932 + BUG("unknown %%(%s) option", field);
933 + }
934 +}
935 +
936 +static int grab_objectname(const char *name, const char *field, const struct object_id *oid,
937 struct atom_value *v, struct used_atom *atom)
938 {
924 - if (starts_with(name, "objectname")) {
925 - if (atom->u.objectname.option == O_SHORT) {
926 - v->s = xstrdup(find_unique_abbrev(oid, DEFAULT_ABBREV));
927 - return 1;
928 - } else if (atom->u.objectname.option == O_FULL) {
929 - v->s = xstrdup(oid_to_hex(oid));
930 - return 1;
931 - } else if (atom->u.objectname.option == O_LENGTH) {
932 - v->s = xstrdup(find_unique_abbrev(oid, atom->u.objectname.length));
933 - return 1;
934 - } else
935 - BUG("unknown %%(objectname) option");
939 + if (starts_with(name, field)) {
940 + v->s = xstrdup(do_grab_objectname(field, oid, atom));
941 + return 1;
942 }
943 return 0;
944 }
@@ -960,7 +966,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
966 } else if (!strcmp(name, "deltabase"))
967 v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
968 else if (deref)
963 - grab_objectname(name, &oi->oid, v, &used_atom[i]);
969 + grab_objectname(name, "objectname", &oi->oid, v, &used_atom[i]);
970 }
971 }
972
@@ -1740,7 +1746,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1746 v->s = xstrdup(buf + 1);
1747 }
1748 continue;
1743 - } else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) {
1749 + } else if (!deref && grab_objectname(name, "objectname", &ref->objectname, v, atom)) {
1750 continue;
1751 } else if (!strcmp(name, "HEAD")) {
1752 if (atom->u.head && !strcmp(ref->refname, atom->u.head))