Skip to content

Commit 5101100

Browse files
harry-hovgitster
authored andcommitted
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 <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b82445d commit 5101100

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

ref-filter.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -918,21 +918,27 @@ int verify_ref_format(struct ref_format *format)
918918
return 0;
919919
}
920920

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,
922937
struct atom_value *v, struct used_atom *atom)
923938
{
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;
936942
}
937943
return 0;
938944
}
@@ -960,7 +966,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
960966
} else if (!strcmp(name, "deltabase"))
961967
v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
962968
else if (deref)
963-
grab_objectname(name, &oi->oid, v, &used_atom[i]);
969+
grab_objectname(name, "objectname", &oi->oid, v, &used_atom[i]);
964970
}
965971
}
966972

@@ -1740,7 +1746,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
17401746
v->s = xstrdup(buf + 1);
17411747
}
17421748
continue;
1743-
} else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) {
1749+
} else if (!deref && grab_objectname(name, "objectname", &ref->objectname, v, atom)) {
17441750
continue;
17451751
} else if (!strcmp(name, "HEAD")) {
17461752
if (atom->u.head && !strcmp(ref->refname, atom->u.head))

0 commit comments

Comments
 (0)