git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] remote: prefer exact matches when using refspecs

From
JTJonathan 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.

Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 6 in “remote: prefer exact matches when using refspecs”
  1. remote: prefer exact matches when using refspecsJonathan Tan, Jul 31, 2018
  2. Jonathan NiederJul 31, 2018
  3. Junio C HamanoJul 31, 2018
  4. Junio C HamanoJul 31, 2018
  5. Jonathan TanJul 31, 2018
  6. Junio C HamanoAug 1, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.