In some circumstances, "git grep --textconv --recurse-submodules"
ignores the textconv attributes from the submodules and erroneuosly
apply the attributes defined in the superproject on the submodules'
files. The textconv cache is also saved on the superproject, even for
submodule objects.
A fix for these problems will probably require at least three changes:
- Some textconv and attributes functions (as well as their callees) will
have to be adjusted to work with arbitrary repositories. Note that
"fill_textconv()", for example, already receives a "struct repository"
but it writes the textconv cache using "write_loose_object()", which
implicitly works on "the_repository".
- grep.c functions will have to call textconv/userdiff routines passing
the "repo" field from "struct grep_source" instead of the one from
"struct grep_opt". The latter always points to "the_repository" on
"git grep" executions (see its initialization in builtin/grep.c), but
the former points to the correct repository that each source (an
object, file, or buffer) comes from.
- "userdiff_find_by_path()" might need to use a different attributes
stack for each repository it works on or reset its internal static
stack when the repository is changed throughout the calls.
For now, let's add some tests to demonstrate these problems, and also
update a NEEDSWORK comment in grep.h that mentions this bug to reference
the added tests.
Signed-off-by: Matheus Tavares <redacted>
---
grep.h | 6 +-
t/t7814-grep-recurse-submodules.sh | 103 +++++++++++++++++++++++++++++
2 files changed, 106 insertions(+), 3 deletions(-)
@@ -441,4 +441,107 @@ test_expect_success 'grep --recurse-submodules with --cached ignores worktree motest_must_failgitgrep--recurse-submodules--cached"A modified line in submodule">actual2>&1&&test_must_be_emptyactual'++test_expect_failure'grep --textconv: superproject .gitattributes does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">.gitattributes&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv: superproject .gitattributes (from index) does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">.gitattributes&&+gitadd.gitattributes&&+rm.gitattributes&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv: superproject .git/info/attributes does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+super_attr="$(gitrev-parse--git-pathinfo/attributes)"&&+test_when_finished"rm -f \"$super_attr\""&&+echo"a diff=d2x">"$super_attr"&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++# Note: what currently prevents this test from passing is not that the+# .gitattributes file from "./submodule" is being ignored, but that it is being+# propagated to the nested "./submodule/sub" files.+#+test_expect_failure'grep --textconv corectly reads submodule .gitattributes''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">submodule/.gitattributes&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv corectly reads submodule .gitattributes (from index)''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">submodule/.gitattributes&&+git-Csubmoduleadd.gitattributes&&+rmsubmodule/.gitattributes&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv corectly reads submodule .git/info/attributes''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&++submodule_attr="$(git-Csubmodulerev-parse--path-format=absolute--git-pathinfo/attributes)"&&+test_when_finished"rm -f \"$submodule_attr\""&&+echo"a diff=d2x">"$submodule_attr"&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep saves textconv cache in the appropriated repository''+reset_and_clean&&+test_config_globaldiff.d2x_cached.textconv"sed -e \"s/d/x/\""&&+test_config_globaldiff.d2x_cached.cachetextconvtrue&&+echo"a diff=d2x_cached">submodule/.gitattributes&&++# We only read/write to the textconv cache when grepping from an OID,+# as the working tree file might have modifications.+gitgrep--textconv--cached--recurse-submodulesx&&++super_textconv_cache="$(gitrev-parse--git-pathrefs/notes/textconv/d2x_cached)"&&+sub_textconv_cache="$(git-Csubmodulerev-parse\+--path-format=absolute--git-pathrefs/notes/textconv/d2x_cached)" &&+test_path_is_missing"$super_textconv_cache"&&+test_path_is_file"$sub_textconv_cache"+'+ test_done
From: Eric Sunshine <hidden> Date: 2021-09-28 17:16:09
On Tue, Sep 28, 2021 at 1:08 PM Matheus Tavares
[off-list ref] wrote:
In some circumstances, "git grep --textconv --recurse-submodules"
ignores the textconv attributes from the submodules and erroneuosly
apply the attributes defined in the superproject on the submodules'
files. The textconv cache is also saved on the superproject, even for
submodule objects.
On Tue, Sep 28, 2021 at 2:16 PM Eric Sunshine [off-list ref] wrote:
On Tue, Sep 28, 2021 at 1:08 PM Matheus Tavares
[off-list ref] wrote:
quoted
In some circumstances, "git grep --textconv --recurse-submodules"
ignores the textconv attributes from the submodules and erroneuosly
apply the attributes defined in the superproject on the submodules'
files. The textconv cache is also saved on the superproject, even for
submodule objects.
In some circumstances, "git grep --textconv --recurse-submodules"
ignores the textconv attributes from the submodules and erroneously
applies the attributes defined in the superproject on the submodules'
files. The textconv cache is also saved on the superproject, even for
submodule objects.
A fix for these problems will probably require at least three changes:
- Some textconv and attributes functions (as well as their callees) will
have to be adjusted to work with arbitrary repositories. Note that
"fill_textconv()", for example, already receives a "struct repository"
but it writes the textconv cache using "write_loose_object()", which
implicitly works on "the_repository".
- grep.c functions will have to call textconv/userdiff routines passing
the "repo" field from "struct grep_source" instead of the one from
"struct grep_opt". The latter always points to "the_repository" on
"git grep" executions (see its initialization in builtin/grep.c), but
the former points to the correct repository that each source (an
object, file, or buffer) comes from.
- "userdiff_find_by_path()" might need to use a different attributes
stack for each repository it works on or reset its internal static
stack when the repository is changed throughout the calls.
For now, let's add some tests to demonstrate these problems, and also
update a NEEDSWORK comment in grep.h that mentions this bug to reference
the added tests.
Signed-off-by: Matheus Tavares <redacted>
---
Changed in v2: fixed typos in commit message and test names
grep.h | 6 +-
t/t7814-grep-recurse-submodules.sh | 103 +++++++++++++++++++++++++++++
2 files changed, 106 insertions(+), 3 deletions(-)
@@ -441,4 +441,107 @@ test_expect_success 'grep --recurse-submodules with --cached ignores worktree motest_must_failgitgrep--recurse-submodules--cached"A modified line in submodule">actual2>&1&&test_must_be_emptyactual'++test_expect_failure'grep --textconv: superproject .gitattributes does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">.gitattributes&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv: superproject .gitattributes (from index) does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">.gitattributes&&+gitadd.gitattributes&&+rm.gitattributes&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv: superproject .git/info/attributes does not affect submodules''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+super_attr="$(gitrev-parse--git-pathinfo/attributes)"&&+test_when_finished"rm -f \"$super_attr\""&&+echo"a diff=d2x">"$super_attr"&&++cat>expect<<-\EOF&&+a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++# Note: what currently prevents this test from passing is not that the+# .gitattributes file from "./submodule" is being ignored, but that it is being+# propagated to the nested "./submodule/sub" files.+#+test_expect_failure'grep --textconv correctly reads submodule .gitattributes''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">submodule/.gitattributes&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv correctly reads submodule .gitattributes (from index)''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&+echo"a diff=d2x">submodule/.gitattributes&&+git-Csubmoduleadd.gitattributes&&+rmsubmodule/.gitattributes&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep --textconv correctly reads submodule .git/info/attributes''+reset_and_clean&&+test_config_globaldiff.d2x.textconv"sed -e \"s/d/x/\""&&++submodule_attr="$(git-Csubmodulerev-parse--path-format=absolute--git-pathinfo/attributes)"&&+test_when_finished"rm -f \"$submodule_attr\""&&+echo"a diff=d2x">"$submodule_attr"&&++cat>expect<<-\EOF&&+submodule/a:(1|2)x(3|4)+EOF+gitgrep--textconv--recurse-submodulesx>actual&&+test_cmpexpectactual+'++test_expect_failure'grep saves textconv cache in the appropriate repository''+reset_and_clean&&+test_config_globaldiff.d2x_cached.textconv"sed -e \"s/d/x/\""&&+test_config_globaldiff.d2x_cached.cachetextconvtrue&&+echo"a diff=d2x_cached">submodule/.gitattributes&&++# We only read/write to the textconv cache when grepping from an OID,+# as the working tree file might have modifications.+gitgrep--textconv--cached--recurse-submodulesx&&++super_textconv_cache="$(gitrev-parse--git-pathrefs/notes/textconv/d2x_cached)"&&+sub_textconv_cache="$(git-Csubmodulerev-parse\+--path-format=absolute--git-pathrefs/notes/textconv/d2x_cached)" &&+test_path_is_missing"$super_textconv_cache"&&+test_path_is_file"$sub_textconv_cache"+'+ test_done