From: Bingwu Zhang <xtex@envs.net>
To: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Cc: Bingwu Zhang <xtex@aosc.io>, apenwarr@gmail.com
Subject: [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree
Date: Mon, 10 Feb 2025 10:11:26 +0800 [thread overview]
Message-ID: <20250210021128.31083-2-xtex@envs.net> (raw)
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 reply other threads:[~2025-02-10 2:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-10 2:11 Bingwu Zhang [this message]
2025-02-10 16:28 ` [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree Junio C Hamano
2025-02-11 1:13 ` Bingwu Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250210021128.31083-2-xtex@envs.net \
--to=xtex@envs.net \
--cc=apenwarr@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=xtex@aosc.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.