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

[PATCH 3/6] remote: don't resolve HEAD in non-repository

From
Jeff King <peff@peff.net>
Date
Mar 5, 2016, 22:11 UTC
Message-ID
<20160305221157.GC31508@sigill.intra.peff.net>
In-Reply-To
<20160305220829.GA31316@sigill.intra.peff.net>

The remote-config code wants to look at HEAD to mark the current branch specially. But if we are not in a repository (e.g., running "git archive --remote"), this makes no sense; there is no HEAD to look at, and we have no current branch.

This doesn't really cause any bugs in practice (if you are not in a repo, you probably don't have a .git/HEAD file), but we should be more careful about triggering the refs code at all in a non-repo. As we grow new ref backends, we would not even know which backend to use.

Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/remote.c b/remote.c
index fc02698..28fd676 100644
--- a/remote.c
+++ b/remote.c
@@ -455,7 +455,6 @@ static void read_config(void)
 {
 	static int loaded;
 	struct object_id oid;
-	const char *head_ref;
 	int flag;
 
 	if (loaded)
@@ -463,10 +462,12 @@ static void read_config(void)
 	loaded = 1;
 
 	current_branch = NULL;
-	head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
-	if (head_ref && (flag & REF_ISSYMREF) &&
-	    skip_prefix(head_ref, "refs/heads/", &head_ref)) {
-		current_branch = make_branch(head_ref, 0);
+	if (startup_info->have_repository) {
+		const char *head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
+		if (head_ref && (flag & REF_ISSYMREF) &&
+		    skip_prefix(head_ref, "refs/heads/", &head_ref)) {
+			current_branch = make_branch(head_ref, 0);
+		}
 	}
 	git_config(handle_config, NULL);
 	alias_all_urls();
-- 
2.8.0.rc1.318.g2193183
Previous: Jeff KingNext: Jeff King
Message 4 of 10 in “avoid touching ref code in non-repositories”
  1. 0/6 avoid touching ref code in non-repositoriesJeff King, Mar 5, 2016
  2. 1/6 setup: make startup_info available everywhereJeff King, Mar 5, 2016
  3. 2/6 setup: set startup_info->have_repository more reliablyJeff King, Mar 5, 2016
  4. 3/6 remote: don't resolve HEAD in non-repositoryJeff King, Mar 5, 2016
  5. 4/6 mailmap: do not resolve blobs in a non-repositoryJeff King, Mar 5, 2016
  6. 5/6 grep: turn off gitlink detection for --no-indexJeff King, Mar 5, 2016
  7. Junio C HamanoMar 7, 2016
  8. 5/6 grep: turn off gitlink detection for --no-indexJeff King, Mar 7, 2016
  9. 6/6 use setup_git_directory() in test-* programsJeff King, Mar 5, 2016
  10. Junio C HamanoMar 7, 2016

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.