Re: [PATCH] remote: prefer exact matches when using refspecs
- From
- Jonathan Tan <jonathantanmy@google.com>
- Date
- Jul 31, 2018, 23:33 UTC
- Message-ID
- <20180731233332.187328-1-jonathantanmy@google.com>
- In-Reply-To
- <xmqq1sbj9h08.fsf@gitster-ct.c.googlers.com>
> 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]
Show 19 quoted lines
> 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.