]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: refactor `grab_objectname()`
authorHariom Verma <hariom18599@gmail.com>
Fri, 21 Aug 2020 21:41:44 +0000 (21:41 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Aug 2020 20:52:50 +0000 (13:52 -0700)
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>
ref-filter.c

index e60765f156f5196fe28bfe17913d4b867b464c62..9bf92db6df3d7185bd8bc0096d7be5b8aaa4a6d8 100644 (file)
@@ -918,21 +918,27 @@ int verify_ref_format(struct ref_format *format)
        return 0;
 }
 
-static int grab_objectname(const char *name, const struct object_id *oid,
+static const char *do_grab_objectname(const char *field, const struct object_id *oid,
+                                     struct used_atom *atom)
+{
+       switch (atom->u.objectname.option) {
+       case O_FULL:
+               return oid_to_hex(oid);
+       case O_LENGTH:
+               return find_unique_abbrev(oid, atom->u.objectname.length);
+       case O_SHORT:
+               return find_unique_abbrev(oid, DEFAULT_ABBREV);
+       default:
+               BUG("unknown %%(%s) option", field);
+       }
+}
+
+static int grab_objectname(const char *name, const char *field, const struct object_id *oid,
                           struct atom_value *v, struct used_atom *atom)
 {
-       if (starts_with(name, "objectname")) {
-               if (atom->u.objectname.option == O_SHORT) {
-                       v->s = xstrdup(find_unique_abbrev(oid, DEFAULT_ABBREV));
-                       return 1;
-               } else if (atom->u.objectname.option == O_FULL) {
-                       v->s = xstrdup(oid_to_hex(oid));
-                       return 1;
-               } else if (atom->u.objectname.option == O_LENGTH) {
-                       v->s = xstrdup(find_unique_abbrev(oid, atom->u.objectname.length));
-                       return 1;
-               } else
-                       BUG("unknown %%(objectname) option");
+       if (starts_with(name, field)) {
+               v->s = xstrdup(do_grab_objectname(field, oid, atom));
+               return 1;
        }
        return 0;
 }
@@ -960,7 +966,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
                } else if (!strcmp(name, "deltabase"))
                        v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
                else if (deref)
-                       grab_objectname(name, &oi->oid, v, &used_atom[i]);
+                       grab_objectname(name, "objectname", &oi->oid, v, &used_atom[i]);
        }
 }
 
@@ -1740,7 +1746,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
                                v->s = xstrdup(buf + 1);
                        }
                        continue;
-               } else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) {
+               } else if (!deref && grab_objectname(name, "objectname", &ref->objectname, v, atom)) {
                        continue;
                } else if (!strcmp(name, "HEAD")) {
                        if (atom->u.head && !strcmp(ref->refname, atom->u.head))