git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/3] Remove unused var
@ 2021-07-26 18:33 David Turner
  2021-07-26 18:33 ` [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: David Turner @ 2021-07-26 18:33 UTC (permalink / raw)
  To: git; +Cc: David Turner

Signed-off-by: David Turner <dturner@twosigma.com>
---
 t/t4060-diff-submodule-option-diff-format.sh | 1 -
 1 file changed, 1 deletion(-)

diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
index dc7b242697..69b9946931 100755
--- a/t/t4060-diff-submodule-option-diff-format.sh
+++ b/t/t4060-diff-submodule-option-diff-format.sh
@@ -361,7 +361,6 @@ test_expect_success 'typechanged submodule(submodule->blob)' '
 rm -f sm1 &&
 test_create_repo sm1 &&
 head6=$(add_file sm1 foo6 foo7)
-fullhead6=$(cd sm1; git rev-parse --verify HEAD)
 test_expect_success 'nonexistent commit' '
 	git diff-index -p --submodule=diff HEAD >actual &&
 	cat >expected <<-EOF &&
-- 
2.11.GIT


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

* [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules
  2021-07-26 18:33 [PATCH 1/3] Remove unused var David Turner
@ 2021-07-26 18:33 ` David Turner
  2021-07-26 22:57   ` Junio C Hamano
  2021-07-26 18:33 ` [PATCH 3/3] diff --submodule=diff: Don't print failure message twice David Turner
  2021-07-26 22:47 ` [PATCH 1/3] Remove unused var Junio C Hamano
  2 siblings, 1 reply; 12+ messages in thread
From: David Turner @ 2021-07-26 18:33 UTC (permalink / raw)
  To: git; +Cc: David Turner

If you have ever initialized a submodule, open_submodule will open it.
If you then delete the submodule's worktree directory (but don't
remove it from .gitmodules), git diff --submodule=diff would crash as
it attempted to chdir into the now-deleted working tree directory.

Signed-off-by: David Turner <dturner@twosigma.com>
---
 submodule.c                                  |  3 ++
 t/t4060-diff-submodule-option-diff-format.sh | 45 ++++++++++++++++++++++++----
 2 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/submodule.c b/submodule.c
index 0b1d9c1dde..9031527a16 100644
--- a/submodule.c
+++ b/submodule.c
@@ -673,6 +673,9 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
 	    !(right || is_null_oid(two)))
 		goto done;
 
+	if (!is_directory(path))
+		goto done;
+
 	if (left)
 		old_oid = one;
 	if (right)
diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
index 69b9946931..10e330c08a 100755
--- a/t/t4060-diff-submodule-option-diff-format.sh
+++ b/t/t4060-diff-submodule-option-diff-format.sh
@@ -703,10 +703,26 @@ test_expect_success 'path filter' '
 	diff_cmp expected actual
 '
 
-commit_file sm2
+cat >.gitmodules <<-EOF
+[submodule "sm2"]
+	path = sm2
+	url = bogus_url
+EOF
+git add .gitmodules
+commit_file sm2 .gitmodules
+
 test_expect_success 'given commit' '
 	git diff-index -p --submodule=diff HEAD^ >actual &&
 	cat >expected <<-EOF &&
+	diff --git a/.gitmodules b/.gitmodules
+	new file mode 100644
+	index 1234567..89abcde
+	--- /dev/null
+	+++ b/.gitmodules
+	@@ -0,0 +1,3 @@
+	+[submodule "sm2"]
+	+path = sm2
+	+url = bogus_url
 	Submodule sm1 $head7...0000000 (submodule deleted)
 	Submodule sm2 0000000...$head9 (new submodule)
 	diff --git a/sm2/foo8 b/sm2/foo8
@@ -728,15 +744,21 @@ test_expect_success 'given commit' '
 '
 
 test_expect_success 'setup .git file for sm2' '
-	(cd sm2 &&
-	 REAL="$(pwd)/../.real" &&
-	 mv .git "$REAL" &&
-	 echo "gitdir: $REAL" >.git)
+	git submodule absorbgitdirs sm2
 '
 
 test_expect_success 'diff --submodule=diff with .git file' '
 	git diff --submodule=diff HEAD^ >actual &&
 	cat >expected <<-EOF &&
+	diff --git a/.gitmodules b/.gitmodules
+	new file mode 100644
+	index 1234567..89abcde
+	--- /dev/null
+	+++ b/.gitmodules
+	@@ -0,0 +1,3 @@
+	+[submodule "sm2"]
+	+path = sm2
+	+url = bogus_url
 	Submodule sm1 $head7...0000000 (submodule deleted)
 	Submodule sm2 0000000...$head9 (new submodule)
 	diff --git a/sm2/foo8 b/sm2/foo8
@@ -757,6 +779,19 @@ test_expect_success 'diff --submodule=diff with .git file' '
 	diff_cmp expected actual
 '
 
+mv sm2 sm2-bak
+
+test_expect_success 'deleted submodule with .git file' '
+	git diff-index -p --submodule=diff HEAD >actual &&
+	cat >expected <<-EOF &&
+	Submodule sm1 $head7...0000000 (submodule deleted)
+	Submodule sm2 $head9...0000000 (submodule deleted)
+	EOF
+	diff_cmp expected actual
+'
+
+mv sm2-bak sm2
+
 test_expect_success 'setup nested submodule' '
 	git submodule add -f ./sm2 &&
 	git commit -a -m "add sm2" &&
-- 
2.11.GIT


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

* [PATCH 3/3] diff --submodule=diff: Don't print failure message twice
  2021-07-26 18:33 [PATCH 1/3] Remove unused var David Turner
  2021-07-26 18:33 ` [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
@ 2021-07-26 18:33 ` David Turner
  2021-07-26 22:47   ` Junio C Hamano
  2021-07-26 22:47 ` [PATCH 1/3] Remove unused var Junio C Hamano
  2 siblings, 1 reply; 12+ messages in thread
From: David Turner @ 2021-07-26 18:33 UTC (permalink / raw)
  To: git; +Cc: David Turner

When we fail to start a diff command inside a submodule, immediately
exit the routine rather than trying to finish the command and printing
a second message.

Signed-off-by: David Turner <dturner@twosigma.com>
---
 submodule.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/submodule.c b/submodule.c
index 9031527a16..0acfad3d4c 100644
--- a/submodule.c
+++ b/submodule.c
@@ -713,8 +713,10 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
 		strvec_push(&cp.args, oid_to_hex(new_oid));
 
 	prepare_submodule_repo_env(&cp.env_array);
-	if (start_command(&cp))
+	if (start_command(&cp)) {
 		diff_emit_submodule_error(o, "(diff failed)\n");
+		goto done;
+	}
 
 	while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
 		diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
-- 
2.11.GIT


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

* Re: [PATCH 1/3] Remove unused var
  2021-07-26 18:33 [PATCH 1/3] Remove unused var David Turner
  2021-07-26 18:33 ` [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
  2021-07-26 18:33 ` [PATCH 3/3] diff --submodule=diff: Don't print failure message twice David Turner
@ 2021-07-26 22:47 ` Junio C Hamano
  2 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2021-07-26 22:47 UTC (permalink / raw)
  To: David Turner; +Cc: git

David Turner <dturner@twosigma.com> writes:

> Signed-off-by: David Turner <dturner@twosigma.com>
> ---
>  t/t4060-diff-submodule-option-diff-format.sh | 1 -
>  1 file changed, 1 deletion(-)
>
> diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
> index dc7b242697..69b9946931 100755
> --- a/t/t4060-diff-submodule-option-diff-format.sh
> +++ b/t/t4060-diff-submodule-option-diff-format.sh
> @@ -361,7 +361,6 @@ test_expect_success 'typechanged submodule(submodule->blob)' '
>  rm -f sm1 &&
>  test_create_repo sm1 &&
>  head6=$(add_file sm1 foo6 foo7)
> -fullhead6=$(cd sm1; git rev-parse --verify HEAD)
>  test_expect_success 'nonexistent commit' '
>  	git diff-index -p --submodule=diff HEAD >actual &&
>  	cat >expected <<-EOF &&

Makes sense.  THanks.

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

* Re: [PATCH 3/3] diff --submodule=diff: Don't print failure message twice
  2021-07-26 18:33 ` [PATCH 3/3] diff --submodule=diff: Don't print failure message twice David Turner
@ 2021-07-26 22:47   ` Junio C Hamano
  0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2021-07-26 22:47 UTC (permalink / raw)
  To: David Turner; +Cc: git

David Turner <dturner@twosigma.com> writes:

> When we fail to start a diff command inside a submodule, immediately
> exit the routine rather than trying to finish the command and printing
> a second message.
>
> Signed-off-by: David Turner <dturner@twosigma.com>
> ---
>  submodule.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/submodule.c b/submodule.c
> index 9031527a16..0acfad3d4c 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -713,8 +713,10 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
>  		strvec_push(&cp.args, oid_to_hex(new_oid));
>  
>  	prepare_submodule_repo_env(&cp.env_array);
> -	if (start_command(&cp))
> +	if (start_command(&cp)) {
>  		diff_emit_submodule_error(o, "(diff failed)\n");
> +		goto done;
> +	}
>  
>  	while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
>  		diff_emit_submodule_pipethrough(o, sb.buf, sb.len);

Again, makes sense.  Thanks.

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

* Re: [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules
  2021-07-26 18:33 ` [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
@ 2021-07-26 22:57   ` Junio C Hamano
  2021-07-27 17:35     ` David Turner
  0 siblings, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2021-07-26 22:57 UTC (permalink / raw)
  To: David Turner; +Cc: git

David Turner <dturner@twosigma.com> writes:

> If you have ever initialized a submodule, open_submodule will open it.
> If you then delete the submodule's worktree directory (but don't
> remove it from .gitmodules), git diff --submodule=diff would crash as
> it attempted to chdir into the now-deleted working tree directory.

Hmph.  So what does the failure look like?  The child process inside
start_command() attempts chdir() and reports CHILD_ERR_CHDIR back,
and we catch it as an error by reading from notify_pipe[0] and report
failure from start_command()?  Or are we talking about a more severe
"crash" of an uncontrolled kind?

Bypassing the execution of diff in the submodule like this patch
does may avoid such a failure, but is that all we need to "fix" this
issue?  What does the user expect after removing a submodule that
way and runs "diff" with the "--submodule=diff" option?  Shouldn't
we be giving "all lines from all files have been removed" patch?

Thanks.

> Signed-off-by: David Turner <dturner@twosigma.com>
> ---
>  submodule.c                                  |  3 ++
>  t/t4060-diff-submodule-option-diff-format.sh | 45 ++++++++++++++++++++++++----
>  2 files changed, 43 insertions(+), 5 deletions(-)
>
> diff --git a/submodule.c b/submodule.c
> index 0b1d9c1dde..9031527a16 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -673,6 +673,9 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
>  	    !(right || is_null_oid(two)))
>  		goto done;
>  
> +	if (!is_directory(path))
> +		goto done;
> +
>  	if (left)
>  		old_oid = one;
>  	if (right)
> diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
> index 69b9946931..10e330c08a 100755
> --- a/t/t4060-diff-submodule-option-diff-format.sh
> +++ b/t/t4060-diff-submodule-option-diff-format.sh
> @@ -703,10 +703,26 @@ test_expect_success 'path filter' '
>  	diff_cmp expected actual
>  '
>  
> -commit_file sm2
> +cat >.gitmodules <<-EOF
> +[submodule "sm2"]
> +	path = sm2
> +	url = bogus_url
> +EOF
> +git add .gitmodules
> +commit_file sm2 .gitmodules
> +
>  test_expect_success 'given commit' '
>  	git diff-index -p --submodule=diff HEAD^ >actual &&
>  	cat >expected <<-EOF &&
> +	diff --git a/.gitmodules b/.gitmodules
> +	new file mode 100644
> +	index 1234567..89abcde
> +	--- /dev/null
> +	+++ b/.gitmodules
> +	@@ -0,0 +1,3 @@
> +	+[submodule "sm2"]
> +	+path = sm2
> +	+url = bogus_url
>  	Submodule sm1 $head7...0000000 (submodule deleted)
>  	Submodule sm2 0000000...$head9 (new submodule)
>  	diff --git a/sm2/foo8 b/sm2/foo8
> @@ -728,15 +744,21 @@ test_expect_success 'given commit' '
>  '
>  
>  test_expect_success 'setup .git file for sm2' '
> -	(cd sm2 &&
> -	 REAL="$(pwd)/../.real" &&
> -	 mv .git "$REAL" &&
> -	 echo "gitdir: $REAL" >.git)
> +	git submodule absorbgitdirs sm2
>  '
>  
>  test_expect_success 'diff --submodule=diff with .git file' '
>  	git diff --submodule=diff HEAD^ >actual &&
>  	cat >expected <<-EOF &&
> +	diff --git a/.gitmodules b/.gitmodules
> +	new file mode 100644
> +	index 1234567..89abcde
> +	--- /dev/null
> +	+++ b/.gitmodules
> +	@@ -0,0 +1,3 @@
> +	+[submodule "sm2"]
> +	+path = sm2
> +	+url = bogus_url
>  	Submodule sm1 $head7...0000000 (submodule deleted)
>  	Submodule sm2 0000000...$head9 (new submodule)
>  	diff --git a/sm2/foo8 b/sm2/foo8
> @@ -757,6 +779,19 @@ test_expect_success 'diff --submodule=diff with .git file' '
>  	diff_cmp expected actual
>  '
>  
> +mv sm2 sm2-bak
> +
> +test_expect_success 'deleted submodule with .git file' '
> +	git diff-index -p --submodule=diff HEAD >actual &&
> +	cat >expected <<-EOF &&
> +	Submodule sm1 $head7...0000000 (submodule deleted)
> +	Submodule sm2 $head9...0000000 (submodule deleted)
> +	EOF
> +	diff_cmp expected actual
> +'
> +
> +mv sm2-bak sm2
> +
>  test_expect_success 'setup nested submodule' '
>  	git submodule add -f ./sm2 &&
>  	git commit -a -m "add sm2" &&

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

* RE: [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules
  2021-07-26 22:57   ` Junio C Hamano
@ 2021-07-27 17:35     ` David Turner
  2021-08-31  6:17       ` Junio C Hamano
  0 siblings, 1 reply; 12+ messages in thread
From: David Turner @ 2021-07-27 17:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git@vger.kernel.org, novalis@novalis.org

> From: Junio C Hamano <gitster@pobox.com>
> Sent: Monday, July 26, 2021 6:57 PM
> To: David Turner <David.Turner@twosigma.com>
> Cc: git@vger.kernel.org
> Subject: Re: [PATCH 2/3] diff --submodule=diff: do not fail on 
> ever-initialied deleted submodules
> 
> David Turner <dturner@twosigma.com> writes:
> 
> > If you have ever initialized a submodule, open_submodule will open it.
> > If you then delete the submodule's worktree directory (but don't 
> > remove it from .gitmodules), git diff --submodule=diff would crash 
> > as it attempted to chdir into the now-deleted working tree directory.
> 
> Hmph.  So what does the failure look like?  The child process inside
> start_command() attempts chdir() and reports CHILD_ERR_CHDIR back, and 
> we catch it as an error by reading from notify_pipe[0] and report 
> failure from start_command()?  Or are we talking about a more severe 
> "crash" of an uncontrolled kind?

It's the first kind.

> Bypassing the execution of diff in the submodule like this patch does 
> may avoid such a failure, but is that all we need to "fix" this issue?  
> What does the user expect after removing a submodule that way and runs 
> "diff" with the "-- submodule=diff" option?  Shouldn't we be giving 
> "all lines from all files have been removed" patch?

Just a note: this only matters if the submodules git dir is
absorbed.  If not, then we no longer have anywhere to run the
diff.  But that case does not trigger this error, because in that
case, open_submodule fails, so we don't resolve a left commit, so
we exit early, which is the only thing we could do.

If absorbed, then we could, in theory, go into the submodule's
absorbed git dir (.git/modules/sm2) and run the diff there.  But
in practice, that's a bit more complicated, because `git diff`
expects to be run from inside a working directory, not a git dir.
So it looks in the config for core.worktree, and does
chdir("../../../sm2"), which is the very dir that we're trying to
avoid visiting because it's been deleted.  We could work around
this by setting GIT_WORK_TREE (and GIT_DIR) to ".".  This
actually seems to work, but it's a little weird to set
GIT_WORK_TREE to something that is not a working tree just to
avoid an unnecessary chdir.  To my mild surprise, it also seems
to work correctly in the case of deleted nested (absorbed)
submodules.  What do you think of this idea?

(Side note: The same bit about chdir into the working tree is
true for diff-tree even though it normally does not need anything
from the working tree.  I say "normally", because in the case of
nested submodules, it might need to look inside those submodules,
which might themselves not be absorbed.  Of course, this case
cannot obtain if the submodule in the worktree has been deleted.
We should consider fixing the docs for git diff-tree
--submodule=diff to specify that it only works if -p is passed.)

(Sorry if the formatting on this email ends up bad -- corporate
email is... corporate .  I'm going to CC my personal address so
that I can use a better mailer on future replies). 

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

* Re: [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules
  2021-07-27 17:35     ` David Turner
@ 2021-08-31  6:17       ` Junio C Hamano
  2021-08-31 13:12         ` [PATCH v4 1/3] Remove unused var David Turner
  0 siblings, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2021-08-31  6:17 UTC (permalink / raw)
  To: David Turner; +Cc: git@vger.kernel.org, novalis@novalis.org

David Turner <David.Turner@twosigma.com> writes:

>> From: Junio C Hamano <gitster@pobox.com>
>> Sent: Monday, July 26, 2021 6:57 PM
>> To: David Turner <David.Turner@twosigma.com>
>> Cc: git@vger.kernel.org
>> Subject: Re: [PATCH 2/3] diff --submodule=diff: do not fail on 
>> ever-initialied deleted submodules
>> 
>> David Turner <dturner@twosigma.com> writes:
>> 
>> > If you have ever initialized a submodule, open_submodule will open it.
>> > If you then delete the submodule's worktree directory (but don't 
>> > remove it from .gitmodules), git diff --submodule=diff would crash 
>> > as it attempted to chdir into the now-deleted working tree directory.
>> 
>> Hmph.  So what does the failure look like?  The child process inside
>> start_command() attempts chdir() and reports CHILD_ERR_CHDIR back, and 
>> we catch it as an error by reading from notify_pipe[0] and report 
>> failure from start_command()?  Or are we talking about a more severe 
>> "crash" of an uncontrolled kind?
>
> It's the first kind.
>
>> Bypassing the execution of diff in the submodule like this patch does 
>> may avoid such a failure, but is that all we need to "fix" this issue?  
>> What does the user expect after removing a submodule that way and runs 
>> "diff" with the "-- submodule=diff" option?  Shouldn't we be giving 
>> "all lines from all files have been removed" patch?
>
> Just a note: this only matters if the submodules git dir is
> absorbed.  If not, then we no longer have anywhere to run the
> diff.  But that case does not trigger this error, because in that
> case, open_submodule fails, so we don't resolve a left commit, so
> we exit early, which is the only thing we could do.
>
> If absorbed, then we could, in theory, go into the submodule's
> absorbed git dir (.git/modules/sm2) and run the diff there.  But
> in practice, that's a bit more complicated, because `git diff`
> expects to be run from inside a working directory, not a git dir.
> So it looks in the config for core.worktree, and does
> chdir("../../../sm2"), which is the very dir that we're trying to
> avoid visiting because it's been deleted.  We could work around
> this by setting GIT_WORK_TREE (and GIT_DIR) to ".".  This
> actually seems to work, but it's a little weird to set
> GIT_WORK_TREE to something that is not a working tree just to
> avoid an unnecessary chdir.  To my mild surprise, it also seems
> to work correctly in the case of deleted nested (absorbed)
> submodules.  What do you think of this idea?
>
> (Side note: The same bit about chdir into the working tree is
> true for diff-tree even though it normally does not need anything
> from the working tree.  I say "normally", because in the case of
> nested submodules, it might need to look inside those submodules,
> which might themselves not be absorbed.  Of course, this case
> cannot obtain if the submodule in the worktree has been deleted.
> We should consider fixing the docs for git diff-tree
> --submodule=diff to specify that it only works if -p is passed.)
>
> (Sorry if the formatting on this email ends up bad -- corporate
> email is... corporate .  I'm going to CC my personal address so
> that I can use a better mailer on future replies). 

That I had to ask questions based on the proposed log message and
you needed to answer to clarify means that the next person who
encounters this commit in "git log" would likely have to ask the
same question, and worse, unlike I had you, there is nobody to whom
they ask for help understanding this commit.

Thanks.

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

* [PATCH v4 1/3] Remove unused var
  2021-08-31  6:17       ` Junio C Hamano
@ 2021-08-31 13:12         ` David Turner
  2021-08-31 13:12           ` [PATCH v4 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
                             ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: David Turner @ 2021-08-31 13:12 UTC (permalink / raw)
  To: git; +Cc: novalis, David Turner

Signed-off-by: David Turner <dturner@twosigma.com>
---
 t/t4060-diff-submodule-option-diff-format.sh | 1 -
 1 file changed, 1 deletion(-)

diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
index dc7b242697..69b9946931 100755
--- a/t/t4060-diff-submodule-option-diff-format.sh
+++ b/t/t4060-diff-submodule-option-diff-format.sh
@@ -361,7 +361,6 @@ test_expect_success 'typechanged submodule(submodule->blob)' '
 rm -f sm1 &&
 test_create_repo sm1 &&
 head6=$(add_file sm1 foo6 foo7)
-fullhead6=$(cd sm1; git rev-parse --verify HEAD)
 test_expect_success 'nonexistent commit' '
 	git diff-index -p --submodule=diff HEAD >actual &&
 	cat >expected <<-EOF &&
-- 
2.11.GIT


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

* [PATCH v4 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules
  2021-08-31 13:12         ` [PATCH v4 1/3] Remove unused var David Turner
@ 2021-08-31 13:12           ` David Turner
  2021-08-31 13:12           ` [PATCH v4 3/3] diff --submodule=diff: Don't print failure message twice David Turner
  2021-08-31 17:16           ` [PATCH v4 1/3] Remove unused var Junio C Hamano
  2 siblings, 0 replies; 12+ messages in thread
From: David Turner @ 2021-08-31 13:12 UTC (permalink / raw)
  To: git; +Cc: novalis, David Turner

If you have ever initialized a submodule, open_submodule will open it.
If you then delete the submodule's worktree directory (but don't
remove it from .gitmodules), git diff --submodule=diff would error out
as it attempted to chdir into the now-deleted working tree directory.

This only matters if the submodules git dir is absorbed.  If not, then
we no longer have anywhere to run the diff.  But that case does not
trigger this error, because in that case, open_submodule fails, so we
don't resolve a left commit, so we exit early, which is the only thing
we could do.

If absorbed, then we can run the diff from the submodule's absorbed
git dir (.git/modules/sm2).  In practice, that's a bit more
complicated, because `git diff` expects to be run from inside a
working directory, not a git dir.  So it looks in the config for
core.worktree, and does chdir("../../../sm2"), which is the very dir
that we're trying to avoid visiting because it's been deleted.  We
work around this by setting GIT_WORK_TREE (and GIT_DIR) to ".".  It is
little weird to set GIT_WORK_TREE to something that is not a working
tree just to avoid an unnecessary chdir, but it works.

Signed-off-by: David Turner <dturner@twosigma.com
---
 submodule.c                                  |  10 ++
 t/t4060-diff-submodule-option-diff-format.sh | 158 +++++++++++++++++++++++++--
 2 files changed, 161 insertions(+), 7 deletions(-)

diff --git a/submodule.c b/submodule.c
index 0b1d9c1dde..8aeff95cfd 100644
--- a/submodule.c
+++ b/submodule.c
@@ -710,6 +710,16 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
 		strvec_push(&cp.args, oid_to_hex(new_oid));
 
 	prepare_submodule_repo_env(&cp.env_array);
+
+	if (!is_directory(path)) {
+		/* fall back to absorbed git dir, if any */
+		if (!sub)
+			goto done;
+		cp.dir = sub->gitdir;
+		strvec_push(&cp.env_array, GIT_DIR_ENVIRONMENT "=.");
+		strvec_push(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT "=.");
+	}
+
 	if (start_command(&cp))
 		diff_emit_submodule_error(o, "(diff failed)\n");
 
diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
index 69b9946931..d86e38abd8 100755
--- a/t/t4060-diff-submodule-option-diff-format.sh
+++ b/t/t4060-diff-submodule-option-diff-format.sh
@@ -703,10 +703,26 @@ test_expect_success 'path filter' '
 	diff_cmp expected actual
 '
 
-commit_file sm2
+cat >.gitmodules <<-EOF
+[submodule "sm2"]
+	path = sm2
+	url = bogus_url
+EOF
+git add .gitmodules
+commit_file sm2 .gitmodules
+
 test_expect_success 'given commit' '
 	git diff-index -p --submodule=diff HEAD^ >actual &&
 	cat >expected <<-EOF &&
+	diff --git a/.gitmodules b/.gitmodules
+	new file mode 100644
+	index 1234567..89abcde
+	--- /dev/null
+	+++ b/.gitmodules
+	@@ -0,0 +1,3 @@
+	+[submodule "sm2"]
+	+path = sm2
+	+url = bogus_url
 	Submodule sm1 $head7...0000000 (submodule deleted)
 	Submodule sm2 0000000...$head9 (new submodule)
 	diff --git a/sm2/foo8 b/sm2/foo8
@@ -728,15 +744,21 @@ test_expect_success 'given commit' '
 '
 
 test_expect_success 'setup .git file for sm2' '
-	(cd sm2 &&
-	 REAL="$(pwd)/../.real" &&
-	 mv .git "$REAL" &&
-	 echo "gitdir: $REAL" >.git)
+	git submodule absorbgitdirs sm2
 '
 
 test_expect_success 'diff --submodule=diff with .git file' '
 	git diff --submodule=diff HEAD^ >actual &&
 	cat >expected <<-EOF &&
+	diff --git a/.gitmodules b/.gitmodules
+	new file mode 100644
+	index 1234567..89abcde
+	--- /dev/null
+	+++ b/.gitmodules
+	@@ -0,0 +1,3 @@
+	+[submodule "sm2"]
+	+path = sm2
+	+url = bogus_url
 	Submodule sm1 $head7...0000000 (submodule deleted)
 	Submodule sm2 0000000...$head9 (new submodule)
 	diff --git a/sm2/foo8 b/sm2/foo8
@@ -757,9 +779,67 @@ test_expect_success 'diff --submodule=diff with .git file' '
 	diff_cmp expected actual
 '
 
+mv sm2 sm2-bak
+
+test_expect_success 'deleted submodule with .git file' '
+	git diff-index -p --submodule=diff HEAD >actual &&
+	cat >expected <<-EOF &&
+	Submodule sm1 $head7...0000000 (submodule deleted)
+	Submodule sm2 $head9...0000000 (submodule deleted)
+	diff --git a/sm2/foo8 b/sm2/foo8
+	deleted file mode 100644
+	index 1234567..89abcde
+	--- a/sm2/foo8
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo8
+	diff --git a/sm2/foo9 b/sm2/foo9
+	deleted file mode 100644
+	index 1234567..89abcde
+	--- a/sm2/foo9
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo9
+	EOF
+	diff_cmp expected actual
+'
+
+echo submodule-to-blob>sm2
+
+test_expect_success 'typechanged(submodule->blob) submodule with .git file' '
+	git diff-index -p --submodule=diff HEAD >actual &&
+	cat >expected <<-EOF &&
+	Submodule sm1 $head7...0000000 (submodule deleted)
+	Submodule sm2 $head9...0000000 (submodule deleted)
+	diff --git a/sm2/foo8 b/sm2/foo8
+	deleted file mode 100644
+	index 1234567..89abcde
+	--- a/sm2/foo8
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo8
+	diff --git a/sm2/foo9 b/sm2/foo9
+	deleted file mode 100644
+	index 1234567..89abcde
+	--- a/sm2/foo9
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo9
+	diff --git a/sm2 b/sm2
+	new file mode 100644
+	index 1234567..89abcde
+	--- /dev/null
+	+++ b/sm2
+	@@ -0,0 +1 @@
+	+submodule-to-blob
+	EOF
+	diff_cmp expected actual
+'
+
+rm sm2
+mv sm2-bak sm2
+
 test_expect_success 'setup nested submodule' '
-	git submodule add -f ./sm2 &&
-	git commit -a -m "add sm2" &&
 	git -C sm2 submodule add ../sm2 nested &&
 	git -C sm2 commit -a -m "nested sub" &&
 	head10=$(git -C sm2 rev-parse --short --verify HEAD)
@@ -790,6 +870,7 @@ test_expect_success 'diff --submodule=diff with moved nested submodule HEAD' '
 
 test_expect_success 'diff --submodule=diff recurses into nested submodules' '
 	cat >expected <<-EOF &&
+	Submodule sm1 $head7...0000000 (submodule deleted)
 	Submodule sm2 contains modified content
 	Submodule sm2 $head9..$head10:
 	diff --git a/sm2/.gitmodules b/sm2/.gitmodules
@@ -829,4 +910,67 @@ test_expect_success 'diff --submodule=diff recurses into nested submodules' '
 	diff_cmp expected actual
 '
 
+(cd sm2; commit_file nested)
+commit_file sm2
+head12=$(cd sm2; git rev-parse --short --verify HEAD)
+
+mv sm2 sm2-bak
+
+test_expect_success 'diff --submodule=diff recurses into deleted nested submodules' '
+	cat >expected <<-EOF &&
+	Submodule sm1 $head7...0000000 (submodule deleted)
+	Submodule sm2 $head12...0000000 (submodule deleted)
+	diff --git a/sm2/.gitmodules b/sm2/.gitmodules
+	deleted file mode 100644
+	index 3a816b8..0000000
+	--- a/sm2/.gitmodules
+	+++ /dev/null
+	@@ -1,3 +0,0 @@
+	-[submodule "nested"]
+	-	path = nested
+	-	url = ../sm2
+	diff --git a/sm2/foo8 b/sm2/foo8
+	deleted file mode 100644
+	index db9916b..0000000
+	--- a/sm2/foo8
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo8
+	diff --git a/sm2/foo9 b/sm2/foo9
+	deleted file mode 100644
+	index 9c3b4f6..0000000
+	--- a/sm2/foo9
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo9
+	Submodule nested $head11...0000000 (submodule deleted)
+	diff --git a/sm2/nested/file b/sm2/nested/file
+	deleted file mode 100644
+	index ca281f5..0000000
+	--- a/sm2/nested/file
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-nested content
+	diff --git a/sm2/nested/foo8 b/sm2/nested/foo8
+	deleted file mode 100644
+	index db9916b..0000000
+	--- a/sm2/nested/foo8
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo8
+	diff --git a/sm2/nested/foo9 b/sm2/nested/foo9
+	deleted file mode 100644
+	index 9c3b4f6..0000000
+	--- a/sm2/nested/foo9
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-foo9
+	EOF
+	git diff --submodule=diff >actual 2>err &&
+	test_must_be_empty err &&
+	diff_cmp expected actual
+'
+
+mv sm2-bak sm2
+
 test_done
-- 
2.11.GIT


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

* [PATCH v4 3/3] diff --submodule=diff: Don't print failure message twice
  2021-08-31 13:12         ` [PATCH v4 1/3] Remove unused var David Turner
  2021-08-31 13:12           ` [PATCH v4 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
@ 2021-08-31 13:12           ` David Turner
  2021-08-31 17:16           ` [PATCH v4 1/3] Remove unused var Junio C Hamano
  2 siblings, 0 replies; 12+ messages in thread
From: David Turner @ 2021-08-31 13:12 UTC (permalink / raw)
  To: git; +Cc: novalis, David Turner

When we fail to start a diff command inside a submodule, immediately
exit the routine rather than trying to finish the command and printing
a second message.

Signed-off-by: David Turner <dturner@twosigma.com>
---
 submodule.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/submodule.c b/submodule.c
index 8aeff95cfd..ab5f050f0e 100644
--- a/submodule.c
+++ b/submodule.c
@@ -720,8 +720,10 @@ void show_submodule_inline_diff(struct diff_options *o, const char *path,
 		strvec_push(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT "=.");
 	}
 
-	if (start_command(&cp))
+	if (start_command(&cp)) {
 		diff_emit_submodule_error(o, "(diff failed)\n");
+		goto done;
+	}
 
 	while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
 		diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
-- 
2.11.GIT


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

* Re: [PATCH v4 1/3] Remove unused var
  2021-08-31 13:12         ` [PATCH v4 1/3] Remove unused var David Turner
  2021-08-31 13:12           ` [PATCH v4 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
  2021-08-31 13:12           ` [PATCH v4 3/3] diff --submodule=diff: Don't print failure message twice David Turner
@ 2021-08-31 17:16           ` Junio C Hamano
  2 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2021-08-31 17:16 UTC (permalink / raw)
  To: David Turner; +Cc: git, novalis

David Turner <dturner@twosigma.com> writes:

> Signed-off-by: David Turner <dturner@twosigma.com>
> ---
>  t/t4060-diff-submodule-option-diff-format.sh | 1 -
>  1 file changed, 1 deletion(-)

Of course this still makes sense ;-)

Let me retitle it to "t4060: remove unused variable" to help readers
of "git shortlog --no-merges", though, while queuing.

Other two patches looked sensible to me, but I'd welcome comments by
submodule users, of course ;-)

Thanks.

> diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
> index dc7b242697..69b9946931 100755
> --- a/t/t4060-diff-submodule-option-diff-format.sh
> +++ b/t/t4060-diff-submodule-option-diff-format.sh
> @@ -361,7 +361,6 @@ test_expect_success 'typechanged submodule(submodule->blob)' '
>  rm -f sm1 &&
>  test_create_repo sm1 &&
>  head6=$(add_file sm1 foo6 foo7)
> -fullhead6=$(cd sm1; git rev-parse --verify HEAD)
>  test_expect_success 'nonexistent commit' '
>  	git diff-index -p --submodule=diff HEAD >actual &&
>  	cat >expected <<-EOF &&

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

end of thread, other threads:[~2021-08-31 17:16 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 18:33 [PATCH 1/3] Remove unused var David Turner
2021-07-26 18:33 ` [PATCH 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
2021-07-26 22:57   ` Junio C Hamano
2021-07-27 17:35     ` David Turner
2021-08-31  6:17       ` Junio C Hamano
2021-08-31 13:12         ` [PATCH v4 1/3] Remove unused var David Turner
2021-08-31 13:12           ` [PATCH v4 2/3] diff --submodule=diff: do not fail on ever-initialied deleted submodules David Turner
2021-08-31 13:12           ` [PATCH v4 3/3] diff --submodule=diff: Don't print failure message twice David Turner
2021-08-31 17:16           ` [PATCH v4 1/3] Remove unused var Junio C Hamano
2021-07-26 18:33 ` [PATCH 3/3] diff --submodule=diff: Don't print failure message twice David Turner
2021-07-26 22:47   ` Junio C Hamano
2021-07-26 22:47 ` [PATCH 1/3] Remove unused var Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).