log-tree: use ref_namespaces instead of if/else-if

The add_ref_decoration() method uses an if/else-if chain to determine if a ref matches a known ref namespace that has a special decoration category. That decoration type is later used to assign a color when writing to stdout. The newly-added ref_namespaces array contains all namespaces, along with information about their decoration type. Check this array instead of this if/else-if chain. This reduces our dependency on string literals being embedded in the decoration logic. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 5, 2022 at 17:58 UTC 94d421b8afa0dc46fcd03143f10b4f3828b20104
1 file changed +16 -10
log-tree.c
+16 -10
@@ -137,6 +137,7 @@ static int ref_filter_match(const char *refname,
137 static int add_ref_decoration(const char *refname, const struct object_id *oid,
138 int flags, void *cb_data)
139 {
140 + int i;
141 struct object *obj;
142 enum object_type objtype;
143 enum decoration_type deco_type = DECORATION_NONE;
@@ -166,16 +167,21 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
167 return 0;
168 obj = lookup_object_by_type(the_repository, oid, objtype);
169
169 - if (starts_with(refname, "refs/heads/"))
170 - deco_type = DECORATION_REF_LOCAL;
171 - else if (starts_with(refname, "refs/remotes/"))
172 - deco_type = DECORATION_REF_REMOTE;
173 - else if (starts_with(refname, "refs/tags/"))
174 - deco_type = DECORATION_REF_TAG;
175 - else if (!strcmp(refname, "refs/stash"))
176 - deco_type = DECORATION_REF_STASH;
177 - else if (!strcmp(refname, "HEAD"))
178 - deco_type = DECORATION_REF_HEAD;
170 + for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
171 + struct ref_namespace_info *info = &ref_namespace[i];
172 +
173 + if (!info->decoration)
174 + continue;
175 + if (info->exact) {
176 + if (!strcmp(refname, info->ref)) {
177 + deco_type = info->decoration;
178 + break;
179 + }
180 + } else if (starts_with(refname, info->ref)) {
181 + deco_type = info->decoration;
182 + break;
183 + }
184 + }
185
186 add_name_decoration(deco_type, refname, obj);
187 while (obj->type == OBJ_TAG) {