> That is, something like this, perhaps.  The resulting behaviour
> should match how "git rev-parse X" would give precedence to tag X
> over branch X by going this route.  What do you think?

[snip]

>  static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, 
> const char *name)
>  {
>       const struct ref *ref;
> +     const struct ref *best_match = NULL;
> +     int best_score = -1;
> +
>       for (ref = refs; ref; ref = ref->next) {
> -             if (refname_match(name, ref->name))
> -                     return ref;
> +             int score = refname_match(name, ref->name);
> +
> +             if ((score && (best_score < 0 || score < best_score))) {
> +                     best_match = ref;
> +                     best_score = score;
> +             }
>       }
> -     return NULL;
> +     return best_match;
>  }

This looks good to me. I've checked that refname_match (and
branch_merge_matches(), which returns the result of refname_match()
directly) is only used in "if" contexts, so making it return a value
other than 1 is fine.

I would initialize best_score to INT_MAX to avoid needing the
"best_score < 0" comparison, but don't feel strongly about it.

Reply via email to