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

[PATCH] contrib/subtree: verify HEAD is valid before adding a subtree

From
BZBingwu Zhang <xtex@envs.net>
Date
Feb 10, 2025, 02:11 UTC
Message-ID
<20250210021128.31083-2-xtex@envs.net>
From: Bingwu Zhang <xtex@aosc.io>

After initializing a new repository or switching to a orphan branch, HEAD is a symbolic reference to refs/heads/xxx while the pointed branch head does not exist until a initial commit.

"git subtree add" will try to ensure that working tree and index are
clean, but as HEAD is invalid, diff-index always fails:
  fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
  Use '--' to separate paths from revisions, like this:
  'git <command> [<revision>...] -- [<file>...]'
  fatal: working tree has modifications.  Cannot add.
It says "working tree has modifications" but it is not the case.

Add a check using "git show-ref --verify" to ensure that HEAD is a valid reference and give a clearer error message.

Signed-off-by: Bingwu Zhang <xtex@aosc.io>
---
 contrib/subtree/git-subtree.sh | 5 +++++
 1 file changed, 5 insertions(+)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 15ae86db1b27..41eb816e454a 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -770,6 +770,11 @@ copy_or_skip () {
 # Usage: ensure_clean
 ensure_clean () {
 	assert test $# = 0
+	# verify HEAD, or else "git diff-index HEAD" will fail
+	if ! git show-ref --verify --quiet HEAD 2>&1
+	then
+		die "fatal: HEAD is not a valid reference.   Subtree cannot be committed as the first commit of a branch."
+	fi
 	if ! git diff-index HEAD --exit-code --quiet 2>&1
 	then
 		die "fatal: working tree has modifications.  Cannot add."

base-commit: f93ff170b93a1782659637824b25923245ac9dd1
-- 
2.48.1
Next: Junio C Hamano
Message 1 of 3 in “contrib/subtree: verify HEAD is valid before adding a subtree”
  1. contrib/subtree: verify HEAD is valid before adding a subtreeBingwu Zhang, Feb 10, 2025
  2. Junio C HamanoFeb 10, 2025
  3. Bingwu ZhangFeb 11, 2025

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.