Thread (2 messages) flat view 2 messages, 2 authors, 2018-07-31

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

From: Jonathan Tan <hidden>
Date: 2018-07-31 23:33:37

Possibly related (same subject, not in this thread)

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.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help