git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree
@ 2025-02-10  2:11 Bingwu Zhang
  2025-02-10 16:28 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Bingwu Zhang @ 2025-02-10  2:11 UTC (permalink / raw)
  To: Junio C Hamano, git; +Cc: Bingwu Zhang, apenwarr

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree
  2025-02-10  2:11 [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree Bingwu Zhang
@ 2025-02-10 16:28 ` Junio C Hamano
  2025-02-11  1:13   ` Bingwu Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2025-02-10 16:28 UTC (permalink / raw)
  To: Bingwu Zhang; +Cc: git, Bingwu Zhang, apenwarr

Bingwu Zhang <xtex@envs.net> writes:

> 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.

[Disclaimer.  I do not use "git subtree" at all myself, and I may be
missing the usual expectation of end-users of that command in the
following description.]

Good finding.  I can see that an unborn HEAD would cause the command
fail.

> It says "working tree has modifications" but it is not the case.

I am not sure if "it is not the case" is true, though.  If the index
is empty (i.e. nothing has been added yet) and the HEAD is unborn,
shouldn't that state be considered that working tree has no
modifications?  IOW, wouldn't this part of the code that uses
"diff-index HEAD" want to consider that an unborn HEAD equivalent to
an empty tree for the purpose of the comparison?

In short, "is not" -> "may not be", perhaps?

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

And if we want to treat an unborn HEAD equivalent to an empty tree,
then dying upon seeing "show-ref" fail would not be a good solution
to the problem, no?  Shouldn't the updated logic to deal with an
unborn HEAD be more like "if we see that the HEAD is unborn, then we
are happy iff the index is empty; if HEAD already points at a
commit, then we are happy iff the working tree has no changes
relative to it"?

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree
  2025-02-10 16:28 ` Junio C Hamano
@ 2025-02-11  1:13   ` Bingwu Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Bingwu Zhang @ 2025-02-11  1:13 UTC (permalink / raw)
  To: Junio C Hamano, apenwarr; +Cc: git

On Tuesday, February 11, 2025 12:28:46 AM GMT+8 Junio C Hamano wrote:
> In short, "is not" -> "may not be", perhaps?

Yes, thank you!

> And if we want to treat an unborn HEAD equivalent to an empty tree,
> then dying upon seeing "show-ref" fail would not be a good solution
> to the problem, no?  Shouldn't the updated logic to deal with an
> unborn HEAD be more like "if we see that the HEAD is unborn, then we
> are happy iff the index is empty; if HEAD already points at a
> commit, then we are happy iff the working tree has no changes
> relative to it"?

I did consider treating an unborn HEAD as a empty tree and test if index is 
empty. However I searched and failed to find out a git subcommand to do that 
test (:
Do you have any suggestions?

-- 
Bingwu Zhang
xtex @ Tue, 11 Feb 2025 01:10:17 +0000




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-02-11  1:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-10  2:11 [PATCH] contrib/subtree: verify HEAD is valid before adding a subtree Bingwu Zhang
2025-02-10 16:28 ` Junio C Hamano
2025-02-11  1:13   ` Bingwu Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).