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

[PATCH 1/2] validate_headref: tighten ref-matching to just branches

From
Jeff King <peff@peff.net>
Date
Jan 29, 2009, 08:30 UTC
Message-ID
<20090129083015.GA6273@coredump.intra.peff.net>
In-Reply-To
<20090129080145.GA777@coredump.intra.peff.net>

When we are trying to determine whether a directory contains a git repository, one of the tests we do is to check whether HEAD is either a symlink or a symref into the "refs/" hierarchy, or a detached HEAD.

We can tighten this a little more, though: a non-detached HEAD should always point to a branch (since checking out anything else should result in detachment), so it is safe to check for "refs/heads/".

Signed-off-by: Jeff King <peff@peff.net>
---
When I was writing the commit message, my spider sense tingled a little
bit. I don't think this is an unreasonable thing to do, but I also don't
know that it helps in any meaningful way. There is no evidence of people
having other stuff in their HEADs anyway.
 path.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/path.c b/path.c
index a074aea..108d9e9 100644
--- a/path.c
+++ b/path.c
@@ -154,7 +154,7 @@ int validate_headref(const char *path)
 	/* Make sure it is a "refs/.." symlink */
 	if (S_ISLNK(st.st_mode)) {
 		len = readlink(path, buffer, sizeof(buffer)-1);
-		if (len >= 5 && !memcmp("refs/", buffer, 5))
+		if (len >= 11 && !memcmp("refs/heads/", buffer, 11))
 			return 0;
 		return -1;
 	}
@@ -178,7 +178,7 @@ int validate_headref(const char *path)
 		len -= 4;
 		while (len && isspace(*buf))
 			buf++, len--;
-		if (len >= 5 && !memcmp("refs/", buf, 5))
+		if (len >= 11 && !memcmp("refs/heads/", buf, 11))
 			return 0;
 	}
 
-- 
1.6.1.1.425.gdbb13
Previous: Jeff KingNext: Jeff King
Message 4 of 6 in “symbolic ref: refuse non-ref targets in HEAD”
  1. symbolic ref: refuse non-ref targets in HEADJeff King, Jan 29, 2009
  2. Junio C HamanoJan 29, 2009
  3. Jeff KingJan 29, 2009
  4. 1/2 validate_headref: tighten ref-matching to just branchesJeff King, Jan 29, 2009
  5. 2/2 symbolic ref: refuse non-ref targets in HEADJeff King, Jan 29, 2009
  6. Jeff KingJan 29, 2009

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.