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

Re: Redefine semantics of find_unique_abbrev()

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 2, 2008, 07:35 UTC
Message-ID
<7vd4qdtw7k.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<20080301050641.GB8969@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 5 quoted lines
> I have been meaning to clean up and submit a similar patch from the
> 1.5.4 freeze period. However, your patch will always print the
> 40-hexdigit version, which looks quite ugly in status output. Instead,
> we can do much better by finding the longest subsequence we _do_ know
> about, and adding one digit to it.

Here is what I ended up with. Instead of saying "missing", I said "exists", which I think makes the logic easier to read, at least to me.

That is, "for objects we have, make sure it uniquely identifies, otherwise, make sure the phoney name is long enough such that it would not name any existing object".

---
 sha1_name.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 9d088cc..8358ba2 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -192,26 +192,25 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
 
 const char *find_unique_abbrev(const unsigned char *sha1, int len)
 {
-	int status, is_null;
+	int status, exists;
 	static char hex[41];
 
-	is_null = is_null_sha1(sha1);
+	exists = has_sha1_file(sha1);
 	memcpy(hex, sha1_to_hex(sha1), 40);
 	if (len == 40 || !len)
 		return hex;
 	while (len < 40) {
 		unsigned char sha1_ret[20];
 		status = get_short_sha1(hex, len, sha1_ret, 1);
-		if (!status ||
-		    (is_null && status != SHORT_NAME_AMBIGUOUS)) {
+		if (exists
+		    ? !status
+		    : status == SHORT_NAME_NOT_FOUND) {
 			hex[len] = 0;
 			return hex;
 		}
-		if (status != SHORT_NAME_AMBIGUOUS)
-			return NULL;
 		len++;
 	}
-	return NULL;
+	return hex;
 }
 
 static int ambiguous_path(const char *path, int len)
Previous: Jeff KingNext: Jeff King
Message 3 of 7 in “Redefine semantics of find_unique_abbrev()”
  1. Junio C HamanoMar 1, 2008
  2. Jeff KingMar 1, 2008
  3. Junio C HamanoMar 2, 2008
  4. Jeff KingMar 2, 2008
  5. Junio C HamanoMar 2, 2008
  6. Clean up find_unique_abbrev() callersJunio C Hamano, Mar 2, 2008
  7. Jeff KingMar 2, 2008

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.