{"thread":{"id":"58500","subject":"[PATCH v6 0/1] grep: integrate with sparse index","startedAt":"2022-09-23T04:16:09Z","lastAt":"2022-09-23T04:21:42Z","messageCount":3,"participants":["Shaoxuan Yuan"],"isPatch":true,"patchVersion":6,"patchTotal":1},"messages":[{"id":"463494","messageId":"20220923041551.27790-1-shaoxuan.yuan02@gmail.com","threadId":"58500","inReplyTo":null,"subject":"[PATCH v6 0/1] grep: integrate with sparse index","fromName":"Shaoxuan Yuan","fromEmail":"shaoxuan.yuan02@gmail.com","sentAt":"2022-09-23T04:15:50Z","receivedAt":"2022-09-23T04:16:09Z","isPatch":true,"sender":{"key":"shaoxuan.yuan02@gmail.com","avatar":"https://avatars.githubusercontent.com/u/46557895?v=4"},"body":"Integrate `git-grep` with sparse-index and test the performance\nimprovement.\n\nChanges since v5\n----------------\n\n* Drop the `--sparse` option patch and edit corresponding tests. \n  We can wait until a better name is decided to replace `--sparse`.\n\n* Modify the commit message, especially get rid of the `--sparse`\n  occurences.\n\nChanges since v4\n----------------\n* Reset the length of `struct strbuf name` back to `name_base_len`,\n  instead of 0, after `grep_tree()` returns.\n\n* Add test cases in t1092 for `grep` recursing into submodules.\n\n* Add a few NEEDSWORK to explain the current problem with submodules.\n\nChanges since v3\n----------------\n* Shorten the perf result tables in commit message.\n\n* Update the commit message to reflect the changes in the commit.\n\n* Update the commit message to indicate the performance improvement\n  is dependent on the pathspec.\n\n* Stop passing `ce_mode` through `check_attr`. Instead, set the\n  `base_len` to 0 to make the code more reasonable and less abuse of\n  `check_attr`.\n\n* Remove another invention of `base`. Use the existing `name` as the\n  argument for `grep_tree()`, and reset it back to `ce->name` after\n  `grep_tree()` returns.\n\n* Update the p2000 test to use a more general pathspec for better\n  compatibility (i.e. do not use git repository specific pathspec).\n\n* Add tests to t1092 'grep is not expanded' to verify the change\n  brought by \"builtin/grep.c: walking tree instead of expanding index\n  with --sparse\": the index *never* expands.\n\nChanges since v2\n----------------\n\n* Modify the commit message for \"builtin/grep.c: integrate with sparse\n  index\" to make it obvious that the perf test results are not from\n  p2000 tests, but from manual perf runs.\n\n* Add tree-walking logic as an extra (the third) patch to improve the\n  performance when --sparse is used. This resolved the left-over-bit\n  in v2 [1].\n\n[1] https://lore.kernel.org/git/20220829232843.183711-1-shaoxuan.yuan02@gmail.com/\n\nChanges since v1\n----------------\n\n* Rewrite the commit message for \"builtin/grep.c: add --sparse option\"\n  to be clearer.\n\n* Update the documentation (both in-code and man page) for --sparse.\n\n* Add a few tests to test the new behavior (when _only_ --cached is\n  supplied).\n\n* Reformat the perf test results to not look like directly from p2000\n  tests.\n\n* Put the \"command_requires_full_index\" lines right after parse_options().\n\n* Add a pathspec test in t1092, and reword a few test documentations.\n\nShaoxuan Yuan (1):\n  builtin/grep.c: integrate with sparse index\n\n builtin/grep.c                           | 48 +++++++++++++++-\n t/perf/p2000-sparse-operations.sh        |  1 +\n t/t1092-sparse-checkout-compatibility.sh | 72 ++++++++++++++++++++++++\n 3 files changed, 118 insertions(+), 3 deletions(-)\n\nRange-diff against v5:\n1:  1d00d23bf9 < -:  ---------- builtin/grep.c: add --sparse option\n2:  926b8d2462 < -:  ---------- builtin/grep.c: integrate with sparse index\n3:  18b65034fe ! 1:  8604111d74 builtin/grep.c: walking tree instead of expanding index with --sparse\n    @@ Metadata\n     Author: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>\n     \n      ## Commit message ##\n    -    builtin/grep.c: walking tree instead of expanding index with --sparse\n    +    builtin/grep.c: integrate with sparse index\n     \n    -    Before this patch, whenever --sparse is used, `git-grep` utilizes the\n    -    ensure_full_index() method to expand the index and search all the\n    -    entries. Because this method requires walking all the trees and\n    -    constructing the index, it is the slow part within the whole command.\n    +    Turn on sparse index and remove ensure_full_index().\n    +\n    +    Before this patch, `git-grep` utilizes the ensure_full_index() method to\n    +    expand the index and search all the entries. Because this method\n    +    requires walking all the trees and constructing the index, it is the\n    +    slow part within the whole command.\n     \n         To achieve better performance, this patch uses grep_tree() to search the\n         sparse directory entries and get rid of the ensure_full_index() method.\n    @@ Commit message\n            result of expanding the index.\n     \n         2) grep_tree() utilizes pathspecs to limit the scope of searching.\n    -       ensure_full_index() always expands the index when --sparse is used,\n    -       that means it will always walk all the trees and blobs in the repo\n    -       without caring if the user only wants a subset of the content, i.e.\n    -       using a pathspec. On the other hand, grep_tree() will only search\n    -       the contents that match the pathspec, and thus possibly walking fewer\n    -       trees.\n    +       ensure_full_index() always expands the index, which means it will\n    +       always walk all the trees and blobs in the repo without caring if\n    +       the user only wants a subset of the content, i.e. using a pathspec.\n    +       On the other hand, grep_tree() will only search the contents that\n    +       match the pathspec, and thus possibly walking fewer trees.\n     \n         3) grep_tree() does not construct and copy back a new index, while\n            ensure_full_index() does. This also saves some time.\n    @@ Commit message\n         - Summary:\n     \n         p2000 tests demonstrate a ~71% execution time reduction for\n    -    `git grep --cached --sparse bogus -- \"f2/f1/f1/*\"` using tree-walking\n    -    logic. However, notice that this result varies depending on the pathspec\n    +    `git grep --cached bogus -- \"f2/f1/f1/*\"` using tree-walking logic.\n    +    However, notice that this result varies depending on the pathspec\n         given. See below \"Command used for testing\" for more details.\n     \n         Test                              HEAD~   HEAD\n    @@ Commit message\n     \n         - Command used for testing:\n     \n    -            git grep --cached --sparse bogus -- \"f2/f1/f1/*\"\n    +            git grep --cached bogus -- \"f2/f1/f1/*\"\n     \n         The reason for specifying a pathspec is that, if we don't specify a\n         pathspec, then grep_tree() will walk all the trees and blobs to find the\n    @@ Commit message\n     \n                 Command used:\n     \n    -                    git grep --cached --sparse bogus\n    +                    git grep --cached bogus\n     \n                 Test                                HEAD~  HEAD\n                 ---------------------------------------------------\n    @@ Commit message\n         Suggested-by: Derrick Stolee <derrickstolee@github.com>\n         Helped-by: Derrick Stolee <derrickstolee@github.com>\n         Helped-by: Victoria Dye <vdye@github.com>\n    +    Helped-by: Elijah Newren <newren@gmail.com>\n         Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>\n     \n      ## builtin/grep.c ##\n    @@ builtin/grep.c: static int grep_cache(struct grep_opt *opt,\n      \tif (repo_read_index(repo) < 0)\n      \t\tdie(_(\"index file corrupt\"));\n      \n    --\tif (grep_sparse)\n    --\t\tensure_full_index(repo->index);\n    --\n    +-\t/* TODO: audit for interaction with sparse-index. */\n    +-\tensure_full_index(repo->index);\n      \tfor (nr = 0; nr < repo->index->cache_nr; nr++) {\n      \t\tconst struct cache_entry *ce = repo->index->cache[nr];\n      \n    @@ builtin/grep.c: static int grep_cache(struct grep_opt *opt,\n     +\t\t\tstruct tree_desc tree;\n     +\t\t\tvoid *data;\n     +\t\t\tunsigned long size;\n    -+\n    -+\t\t\tdata = read_object_file(&ce->oid, &type, &size);\n    -+\t\t\tinit_tree_desc(&tree, data, size);\n      \n     -\t\tif (S_ISREG(ce->ce_mode) &&\n    ++\t\t\tdata = read_object_file(&ce->oid, &type, &size);\n    ++\t\t\tinit_tree_desc(&tree, data, size);\n    ++\n     +\t\t\thit |= grep_tree(opt, pathspec, &tree, &name, 0, 0);\n     +\t\t\tstrbuf_setlen(&name, name_base_len);\n     +\t\t\tstrbuf_addstr(&name, ce->name);\n    @@ builtin/grep.c: static int grep_cache(struct grep_opt *opt,\n      \t\t    match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,\n      \t\t\t\t   S_ISDIR(ce->ce_mode) ||\n      \t\t\t\t   S_ISGITLINK(ce->ce_mode))) {\n    +@@ builtin/grep.c: int cmd_grep(int argc, const char **argv, const char *prefix)\n    + \t\t\t     PARSE_OPT_KEEP_DASHDASH |\n    + \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n    + \n    ++\tif (the_repository->gitdir) {\n    ++\t\tprepare_repo_settings(the_repository);\n    ++\t\tthe_repository->settings.command_requires_full_index = 0;\n    ++\t}\n    ++\n    + \tif (use_index && !startup_info->have_repository) {\n    + \t\tint fallback = 0;\n    + \t\tgit_config_get_bool(\"grep.fallbacktonoindex\", &fallback);\n     \n      ## t/perf/p2000-sparse-operations.sh ##\n     @@ t/perf/p2000-sparse-operations.sh: test_perf_on_all git read-tree -mu HEAD\n    @@ t/t1092-sparse-checkout-compatibility.sh: init_repos () {\n      run_on_sparse () {\n      \t(\n      \t\tcd sparse-checkout &&\n    -@@ t/t1092-sparse-checkout-compatibility.sh: test_expect_success 'grep is not expanded' '\n    +@@ t/t1092-sparse-checkout-compatibility.sh: test_expect_success 'sparse index is not expanded: rm' '\n    + \tensure_not_expanded rm -r deep\n    + '\n      \n    - \t# All files within the folder1/* pathspec are sparse,\n    - \t# so this command does not find any matches\n    --\tensure_not_expanded ! grep a -- folder1/*\n    ++test_expect_success 'grep with and --cached' '\n    ++\tinit_repos &&\n    ++\n    ++\ttest_all_match git grep --cached a &&\n    ++\ttest_all_match git grep --cached a -- \"folder1/*\"\n    ++'\n    ++\n    ++test_expect_success 'grep is not expanded' '\n    ++\tinit_repos &&\n    ++\n    ++\tensure_not_expanded grep a &&\n    ++\tensure_not_expanded grep a -- deep/* &&\n    ++\n    ++\t# All files within the folder1/* pathspec are sparse,\n    ++\t# so this command does not find any matches\n     +\tensure_not_expanded ! grep a -- folder1/* &&\n     +\n     +\t# test out-of-cone pathspec with or without wildcard\n    -+\tensure_not_expanded grep --sparse --cached a -- \"folder1/a\" &&\n    -+\tensure_not_expanded grep --sparse --cached a -- \"folder1/*\" &&\n    ++\tensure_not_expanded grep --cached a -- \"folder1/a\" &&\n    ++\tensure_not_expanded grep --cached a -- \"folder1/*\" &&\n     +\n     +\t# test in-cone pathspec with or without wildcard\n    -+\tensure_not_expanded grep --sparse --cached a -- \"deep/a\" &&\n    -+\tensure_not_expanded grep --sparse --cached a -- \"deep/*\"\n    ++\tensure_not_expanded grep --cached a -- \"deep/a\" &&\n    ++\tensure_not_expanded grep --cached a -- \"deep/*\"\n     +'\n     +\n     +# NEEDSWORK: when running `grep` in the superproject with --recurse-submodules,\n    @@ t/t1092-sparse-checkout-compatibility.sh: test_expect_success 'grep is not expan\n     +\t# do not use ensure_not_expanded() here, becasue `grep` should be\n     +\t# run in the superproject, not in \"./sparse-index\"\n     +\tGIT_TRACE2_EVENT=\"$(pwd)/trace2.txt\" \\\n    -+\tgit grep --sparse --cached --recurse-submodules a -- \"*/folder1/*\" &&\n    ++\tgit grep --cached --recurse-submodules a -- \"*/folder1/*\" &&\n     +\ttest_region ! index ensure_full_index trace2.txt\n     +'\n     +\n    @@ t/t1092-sparse-checkout-compatibility.sh: test_expect_success 'grep is not expan\n     +\tsparse-checkout/folder1/a:a\n     +\tsparse-index/folder1/a:a\n     +\tEOF\n    -+\tgit grep --sparse --cached --recurse-submodules a -- \"*/folder1/*\" >actual &&\n    ++\tgit grep --cached --recurse-submodules a -- \"*/folder1/*\" >actual &&\n     +\ttest_cmp actual expect\n    - '\n    - \n    ++'\n    ++\n      test_done\n\nbase-commit: 1b3d6e17fe83eb6f79ffbac2f2c61bbf1eaef5f8\n-- \n2.37.0\n\n"},{"id":"463495","messageId":"20220923041551.27790-2-shaoxuan.yuan02@gmail.com","threadId":"58500","inReplyTo":"20220923041551.27790-1-shaoxuan.yuan02@gmail.com","subject":"[PATCH v6 1/1] builtin/grep.c: integrate with sparse index","fromName":"Shaoxuan Yuan","fromEmail":"shaoxuan.yuan02@gmail.com","sentAt":"2022-09-23T04:15:51Z","receivedAt":"2022-09-23T04:16:09Z","isPatch":true,"sender":{"key":"shaoxuan.yuan02@gmail.com","avatar":"https://avatars.githubusercontent.com/u/46557895?v=4"},"body":"Turn on sparse index and remove ensure_full_index().\n\nBefore this patch, `git-grep` utilizes the ensure_full_index() method to\nexpand the index and search all the entries. Because this method\nrequires walking all the trees and constructing the index, it is the\nslow part within the whole command.\n\nTo achieve better performance, this patch uses grep_tree() to search the\nsparse directory entries and get rid of the ensure_full_index() method.\n\nWhy grep_tree() is a better choice over ensure_full_index()?\n\n1) grep_tree() is as correct as ensure_full_index(). grep_tree() looks\n   into every sparse-directory entry (represented by a tree) recursively\n   when looping over the index, and the result of doing so matches the\n   result of expanding the index.\n\n2) grep_tree() utilizes pathspecs to limit the scope of searching.\n   ensure_full_index() always expands the index, which means it will\n   always walk all the trees and blobs in the repo without caring if\n   the user only wants a subset of the content, i.e. using a pathspec.\n   On the other hand, grep_tree() will only search the contents that\n   match the pathspec, and thus possibly walking fewer trees.\n\n3) grep_tree() does not construct and copy back a new index, while\n   ensure_full_index() does. This also saves some time.\n\n----------------\nPerformance test\n\n- Summary:\n\np2000 tests demonstrate a ~71% execution time reduction for\n`git grep --cached bogus -- \"f2/f1/f1/*\"` using tree-walking logic.\nHowever, notice that this result varies depending on the pathspec\ngiven. See below \"Command used for testing\" for more details.\n\nTest                              HEAD~   HEAD\n-------------------------------------------------------\n2000.78: git grep ... (full-v3)   0.35    0.39 (≈)\n2000.79: git grep ... (full-v4)   0.36    0.30 (≈)\n2000.80: git grep ... (sparse-v3) 0.88    0.23 (-73.8%)\n2000.81: git grep ... (sparse-v4) 0.83    0.26 (-68.6%)\n\n- Command used for testing:\n\n\tgit grep --cached bogus -- \"f2/f1/f1/*\"\n\nThe reason for specifying a pathspec is that, if we don't specify a\npathspec, then grep_tree() will walk all the trees and blobs to find the\npattern, and the time consumed doing so is not too different from using\nthe original ensure_full_index() method, which also spends most of the\ntime walking trees. However, when a pathspec is specified, this latest\nlogic will only walk the area of trees enclosed by the pathspec, and the\ntime consumed is reasonably a lot less.\n\nGenerally speaking, because the performance gain is acheived by walking\nless trees, which are specified by the pathspec, the HEAD time v.s.\nHEAD~ time in sparse-v[3|4], should be proportional to\n\"pathspec enclosed area\" v.s. \"all area\", respectively. Namely, the\nwider the <pathspec> is encompassing, the less the performance\ndifference between HEAD~ and HEAD, and vice versa.\n\nThat is, if we don't specify a pathspec, the performance difference [1]\nis indistinguishable: both methods walk all the trees and take generally\nsame amount of time (even with the index construction time included for\nensure_full_index()).\n\n[1] Performance test result without pathspec (hence walking all trees):\n\n\tCommand used:\n\n\t\tgit grep --cached bogus\n\n\tTest                                HEAD~  HEAD\n\t---------------------------------------------------\n\t2000.78: git grep ... (full-v3)     6.17   5.19 (≈)\n\t2000.79: git grep ... (full-v4)     6.19   5.46 (≈)\n\t2000.80: git grep ... (sparse-v3)   6.57   6.44 (≈)\n\t2000.81: git grep ... (sparse-v4)   6.65   6.28 (≈)\n\n--------------------------\nNEEDSWORK about submodules\n\nThere are a few NEEDSWORKs that belong to improvements beyond this\ntopic. See the NEEDSWORK in builtin/grep.c::grep_submodule() for\nmore context. The other two NEEDSWORKs in t1092 are also relative.\n\nSuggested-by: Derrick Stolee <derrickstolee@github.com>\nHelped-by: Derrick Stolee <derrickstolee@github.com>\nHelped-by: Victoria Dye <vdye@github.com>\nHelped-by: Elijah Newren <newren@gmail.com>\nSigned-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>\n---\n builtin/grep.c                           | 48 +++++++++++++++-\n t/perf/p2000-sparse-operations.sh        |  1 +\n t/t1092-sparse-checkout-compatibility.sh | 72 ++++++++++++++++++++++++\n 3 files changed, 118 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex e6bcdf860c..5fa927d4e2 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -458,6 +458,33 @@ static int grep_submodule(struct grep_opt *opt,\n \t * subrepo's odbs to the in-memory alternates list.\n \t */\n \tobj_read_lock();\n+\n+\t/*\n+\t * NEEDSWORK: when reading a submodule, the sparsity settings in the\n+\t * superproject are incorrectly forgotten or misused. For example:\n+\t *\n+\t * 1. \"command_requires_full_index\"\n+\t * \tWhen this setting is turned on for `grep`, only the superproject\n+\t *\tknows it. All the submodules are read with their own configs\n+\t *\tand get prepare_repo_settings()'d. Therefore, these submodules\n+\t *\t\"forget\" the sparse-index feature switch. As a result, the index\n+\t *\tof these submodules are expanded unexpectedly.\n+\t *\n+\t * 2. \"core_apply_sparse_checkout\"\n+\t *\tWhen running `grep` in the superproject, this setting is\n+\t *\tpopulated using the superproject's configs. However, once\n+\t *\tinitialized, this config is globally accessible and is read by\n+\t *\tprepare_repo_settings() for the submodules. For instance, if a\n+\t *\tsubmodule is using a sparse-checkout, however, the superproject\n+\t *\tis not, the result is that the config from the superproject will\n+\t *\tdictate the behavior for the submodule, making it \"forget\" its\n+\t *\tsparse-checkout state.\n+\t *\n+\t * 3. \"core_sparse_checkout_cone\"\n+\t *\tditto.\n+\t *\n+\t * Note that this list is not exhaustive.\n+\t */\n \trepo_read_gitmodules(subrepo, 0);\n \n \t/*\n@@ -520,8 +547,6 @@ static int grep_cache(struct grep_opt *opt,\n \tif (repo_read_index(repo) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n-\t/* TODO: audit for interaction with sparse-index. */\n-\tensure_full_index(repo->index);\n \tfor (nr = 0; nr < repo->index->cache_nr; nr++) {\n \t\tconst struct cache_entry *ce = repo->index->cache[nr];\n \n@@ -530,8 +555,20 @@ static int grep_cache(struct grep_opt *opt,\n \n \t\tstrbuf_setlen(&name, name_base_len);\n \t\tstrbuf_addstr(&name, ce->name);\n+\t\tif (S_ISSPARSEDIR(ce->ce_mode)) {\n+\t\t\tenum object_type type;\n+\t\t\tstruct tree_desc tree;\n+\t\t\tvoid *data;\n+\t\t\tunsigned long size;\n \n-\t\tif (S_ISREG(ce->ce_mode) &&\n+\t\t\tdata = read_object_file(&ce->oid, &type, &size);\n+\t\t\tinit_tree_desc(&tree, data, size);\n+\n+\t\t\thit |= grep_tree(opt, pathspec, &tree, &name, 0, 0);\n+\t\t\tstrbuf_setlen(&name, name_base_len);\n+\t\t\tstrbuf_addstr(&name, ce->name);\n+\t\t\tfree(data);\n+\t\t} else if (S_ISREG(ce->ce_mode) &&\n \t\t    match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,\n \t\t\t\t   S_ISDIR(ce->ce_mode) ||\n \t\t\t\t   S_ISGITLINK(ce->ce_mode))) {\n@@ -984,6 +1021,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\t\t     PARSE_OPT_KEEP_DASHDASH |\n \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION);\n \n+\tif (the_repository->gitdir) {\n+\t\tprepare_repo_settings(the_repository);\n+\t\tthe_repository->settings.command_requires_full_index = 0;\n+\t}\n+\n \tif (use_index && !startup_info->have_repository) {\n \t\tint fallback = 0;\n \t\tgit_config_get_bool(\"grep.fallbacktonoindex\", &fallback);\ndiff --git a/t/perf/p2000-sparse-operations.sh b/t/perf/p2000-sparse-operations.sh\nindex fce8151d41..3242cfe91a 100755\n--- a/t/perf/p2000-sparse-operations.sh\n+++ b/t/perf/p2000-sparse-operations.sh\n@@ -124,5 +124,6 @@ test_perf_on_all git read-tree -mu HEAD\n test_perf_on_all git checkout-index -f --all\n test_perf_on_all git update-index --add --remove $SPARSE_CONE/a\n test_perf_on_all \"git rm -f $SPARSE_CONE/a && git checkout HEAD -- $SPARSE_CONE/a\"\n+test_perf_on_all git grep --cached --sparse bogus -- \"f2/f1/f1/*\"\n \n test_done\ndiff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh\nindex b9350c075c..711b52fb46 100755\n--- a/t/t1092-sparse-checkout-compatibility.sh\n+++ b/t/t1092-sparse-checkout-compatibility.sh\n@@ -162,6 +162,19 @@ init_repos () {\n \tgit -C sparse-index sparse-checkout set deep\n }\n \n+init_repos_as_submodules () {\n+\tgit reset --hard &&\n+\tinit_repos &&\n+\tgit submodule add ./full-checkout &&\n+\tgit submodule add ./sparse-checkout &&\n+\tgit submodule add ./sparse-index &&\n+\n+\tgit submodule status >actual &&\n+\tgrep full-checkout actual &&\n+\tgrep sparse-checkout actual &&\n+\tgrep sparse-index actual\n+}\n+\n run_on_sparse () {\n \t(\n \t\tcd sparse-checkout &&\n@@ -1981,4 +1994,63 @@ test_expect_success 'sparse index is not expanded: rm' '\n \tensure_not_expanded rm -r deep\n '\n \n+test_expect_success 'grep with and --cached' '\n+\tinit_repos &&\n+\n+\ttest_all_match git grep --cached a &&\n+\ttest_all_match git grep --cached a -- \"folder1/*\"\n+'\n+\n+test_expect_success 'grep is not expanded' '\n+\tinit_repos &&\n+\n+\tensure_not_expanded grep a &&\n+\tensure_not_expanded grep a -- deep/* &&\n+\n+\t# All files within the folder1/* pathspec are sparse,\n+\t# so this command does not find any matches\n+\tensure_not_expanded ! grep a -- folder1/* &&\n+\n+\t# test out-of-cone pathspec with or without wildcard\n+\tensure_not_expanded grep --cached a -- \"folder1/a\" &&\n+\tensure_not_expanded grep --cached a -- \"folder1/*\" &&\n+\n+\t# test in-cone pathspec with or without wildcard\n+\tensure_not_expanded grep --cached a -- \"deep/a\" &&\n+\tensure_not_expanded grep --cached a -- \"deep/*\"\n+'\n+\n+# NEEDSWORK: when running `grep` in the superproject with --recurse-submodules,\n+# Git expands the index of the submodules unexpectedly. Even though `grep`\n+# builtin is marked as \"command_requires_full_index = 0\", this config is only\n+# useful for the superproject. Namely, the submodules have their own configs,\n+# which are _not_ populated by the one-time sparse-index feature switch.\n+test_expect_failure 'grep within submodules is not expanded' '\n+\tinit_repos_as_submodules &&\n+\n+\t# do not use ensure_not_expanded() here, becasue `grep` should be\n+\t# run in the superproject, not in \"./sparse-index\"\n+\tGIT_TRACE2_EVENT=\"$(pwd)/trace2.txt\" \\\n+\tgit grep --cached --recurse-submodules a -- \"*/folder1/*\" &&\n+\ttest_region ! index ensure_full_index trace2.txt\n+'\n+\n+# NEEDSWORK: this test is not actually testing the code. The design purpose\n+# of this test is to verify the grep result when the submodules are using a\n+# sparse-index. Namely, we want \"folder1/\" as a tree (a sparse directory); but\n+# because of the index expansion, we are now grepping the \"folder1/a\" blob.\n+# Because of the problem stated above 'grep within submodules is not expanded',\n+# we don't have the ideal test environment yet.\n+test_expect_success 'grep sparse directory within submodules' '\n+\tinit_repos_as_submodules &&\n+\n+\tcat >expect <<-\\EOF &&\n+\tfull-checkout/folder1/a:a\n+\tsparse-checkout/folder1/a:a\n+\tsparse-index/folder1/a:a\n+\tEOF\n+\tgit grep --cached --recurse-submodules a -- \"*/folder1/*\" >actual &&\n+\ttest_cmp actual expect\n+'\n+\n test_done\n-- \n2.37.0\n\n"},{"id":"463498","messageId":"ec27fcee-2ad5-ba50-b891-444e7c97f50f@gmail.com","threadId":"58500","inReplyTo":"20220923041551.27790-1-shaoxuan.yuan02@gmail.com","subject":"Re: [PATCH v6 0/1] grep: integrate with sparse index","fromName":"Shaoxuan Yuan","fromEmail":"shaoxuan.yuan02@gmail.com","sentAt":"2022-09-23T04:21:32Z","receivedAt":"2022-09-23T04:21:42Z","isPatch":true,"sender":{"key":"shaoxuan.yuan02@gmail.com","avatar":"https://avatars.githubusercontent.com/u/46557895?v=4"},"body":"Oh, shoot...\n\nSorry that I forgot to \"--in-reply-to\", please ignore this thread :(\n\nThanks,\nShaoxuan\n"}]}