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

Re: Unexpected behavior with :/<text> references

From
Jeff King <peff@peff.net>
Date
Jul 9, 2018, 18:08 UTC
Message-ID
<20180709180804.GA6134@sigill.intra.peff.net>
In-Reply-To
<CAFW+GMAnkLYReotwRvbVt9iPqmuq_8WMc1sck4Jkj_fyafVm3w@mail.gmail.com>
On Sun, Jul 08, 2018 at 10:13:12PM -0700, William Chargin wrote:
> After further investigation, it appears that ":/foo" indeed resolves to
> the commit with message "foobar" (in the above example) if the commits
> are not all created at the same time: e.g., by adding `sleep 1` between
> the commit commands, or exporting `GIT_AUTHOR_DATE`.

I suspect it's actually GIT_COMMITTER_DATE (but I didn't dig into it, so I could be wrong). "Youngest" in terms of commits generally refers to the committer date.

> This leaves only the question of why :/<text> references don't resolve
> to commits reachable only from HEAD, and whether this is the best
> behavior.

I agree that it would make sense to look at HEAD here. The point of ":/" is to search everything reachable, and HEAD is part of that reachability. _Usually_ it doesn't matter because HEAD is pointing to another ref, but the detached case is an exception.

Doing this would bring us in line with the behavior of other parts of Git. E.g., f0298cf1c6 (revision walker: include a detached HEAD in --all, 2009-01-16).

The patch is probably this:
diff --git a/sha1-name.c b/sha1-name.c
index 60d9ef3c7e..641ca12f91 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1650,6 +1650,7 @@ static int get_oid_with_context_1(const char *name,
 			struct commit_list *list = NULL;
 
 			for_each_ref(handle_one_ref, &list);
+			head_ref(handle_one_ref, &list);
 			commit_list_sort_by_date(&list);
 			return get_oid_oneline(name + 2, oid, list);
 		}

but I didn't test it at all. Do you want to try adding a case for this
to our test suite (probably in t4208) and wrapping it up with a commit
message?

-Peff
Previous: William CharginNext: William Chargin
Message 3 of 4 in “Unexpected behavior with :/<text> references”
  1. William CharginJul 9, 2018
  2. William CharginJul 9, 2018
  3. Jeff KingJul 9, 2018
  4. William CharginJul 9, 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.