Skip to content

Commit 94d421b

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 97e61e0 commit 94d421b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

log-tree.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static int ref_filter_match(const char *refname,
137137
static int add_ref_decoration(const char *refname, const struct object_id *oid,
138138
int flags, void *cb_data)
139139
{
140+
int i;
140141
struct object *obj;
141142
enum object_type objtype;
142143
enum decoration_type deco_type = DECORATION_NONE;
@@ -166,16 +167,21 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
166167
return 0;
167168
obj = lookup_object_by_type(the_repository, oid, objtype);
168169

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+
}
179185

180186
add_name_decoration(deco_type, refname, obj);
181187
while (obj->type == OBJ_TAG) {

0 commit comments

Comments
 (0)