Skip to content

Commit

Permalink
ChafaTermDb: Prefer more specific and higher-version TE entries
Browse files Browse the repository at this point in the history
  • Loading branch information
hpjansson committed Nov 12, 2024
1 parent 7598908 commit 1995e06
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion chafa/chafa-term-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ parse_version (const gchar *version_str)
gint64 ver = 0;
gint i;

if (!version_str)
return -1;

for (i = 0; version_str [i]; i++)
{
gint n = version_str [i] - (gint) '0';
Expand Down Expand Up @@ -923,6 +926,19 @@ new_term_info_from_def (const TermDef *def)
return ti;
}

static gint
strcmp_wrap (const gchar *a, const gchar *b)
{
if (a == NULL && b == NULL)
return 0;
if (a == NULL)
return 1;
if (b == NULL)
return -1;

return strcmp (a, b);
}

static ChafaTermInfo *
detect_term_of_type (TermType term_type, gchar **envp)
{
Expand All @@ -938,8 +954,15 @@ detect_term_of_type (TermType term_type, gchar **envp)
if (term_def [i].type != term_type)
continue;

/* Pick higher-priority solutions. If the priority is equal, allow
* more specific and higher-version entries of the same TE to take precedence. */
pri = match_term_def (&term_def [i], envp);
if (pri >= best_pri)
if (pri > best_pri
|| (pri == best_pri
&& !strcmp_wrap (term_def [i].name, term_def [best_def_i].name)
&& ((term_def [i].variant && !term_def [best_def_i].variant)
|| (!strcmp_wrap (term_def [i].variant, term_def [best_def_i].variant)
&& parse_version (term_def [i].version) > parse_version (term_def [best_def_i].version)))))
{
best_pri = pri;
best_def_i = i;
Expand Down

0 comments on commit 1995e06

Please sign in to comment.