From a9e0a49dc427a8c442619e7937abeb1af1f35ff2 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 26 Apr 2022 20:43:16 +0000 Subject: [PATCH 1/5] t1092: add compatibility tests for 'git show' Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- t/t1092-sparse-checkout-compatibility.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index 236ab53028..74792b5ebb 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -1151,6 +1151,22 @@ test_expect_success 'clean' ' test_sparse_match test_path_is_dir folder1 ' +test_expect_success 'show (cached blobs/trees)' ' + init_repos && + + test_all_match git show :a && + test_all_match git show :deep/a && + test_sparse_match git show :folder1/a && + + # Asking "git show" for directories in the index + # does not work as implemented. The error message is + # different for a full checkout and a sparse checkout + # when the directory is outside of the cone. + test_all_match test_must_fail git show :deep/ && + test_must_fail git -C full-checkout show :folder1/ && + test_sparse_match test_must_fail git show :folder1/ +' + test_expect_success 'submodule handling' ' init_repos && From a37d14422a4edd4f95abbe9532f518127cd3ef69 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 26 Apr 2022 20:43:17 +0000 Subject: [PATCH 2/5] show: integrate with the sparse index The 'git show' command can take an input to request the state of an object in the index. This can lead to parsing the index in order to load a specific file entry. Without the change presented here, a sparse index would expand to a full one, taking much longer than usual to access a simple file. There is one behavioral change that happens here, though: we now can find a sparse directory entry within the index! Commands that previously failed because we could not find an entry in the worktree or index now succeed because we _do_ find an entry in the index. There might be more work to do to make other situations succeed when looking for an indexed tree, perhaps by looking at or updating the cache-tree extension as needed. These situations include having a full index or asking for a directory that is within the sparse-checkout cone (and hence is not a sparse directory entry in the index). For now, we demonstrate how the sparse index integration is extremely simple for files outside of the cone as well as directories within the cone. A later change will resolve this behavior around sparse directories. Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- builtin/log.c | 5 +++++ t/t1092-sparse-checkout-compatibility.sh | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/builtin/log.c b/builtin/log.c index c211d66d1d..8e2e9912ab 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -661,6 +661,11 @@ int cmd_show(int argc, const char **argv, const char *prefix) init_log_defaults(); git_config(git_log_config, NULL); + if (the_repository->gitdir) { + prepare_repo_settings(the_repository); + the_repository->settings.command_requires_full_index = 0; + } + memset(&match_all, 0, sizeof(match_all)); repo_init_revisions(the_repository, &rev, prefix); git_config(grep_config, &rev.grep_filter); diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index 74792b5ebb..3506c0216f 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -1159,12 +1159,21 @@ test_expect_success 'show (cached blobs/trees)' ' test_sparse_match git show :folder1/a && # Asking "git show" for directories in the index - # does not work as implemented. The error message is - # different for a full checkout and a sparse checkout - # when the directory is outside of the cone. + # had different behavior depending on the existence + # of a sparse index. test_all_match test_must_fail git show :deep/ && test_must_fail git -C full-checkout show :folder1/ && - test_sparse_match test_must_fail git show :folder1/ + test_must_fail git -C sparse-checkout show :folder1/ && + + git -C sparse-index show :folder1/ >actual && + git -C full-checkout show HEAD:folder1 >expect && + + # The output of "git show" includes the way we referenced the + # objects, so strip that out. + test_line_count = 4 actual && + tail -n 2 actual >actual-trunc && + tail -n 2 expect >expect-trunc && + test_cmp expect-trunc actual-trunc ' test_expect_success 'submodule handling' ' @@ -1388,6 +1397,13 @@ test_expect_success 'sparse index is not expanded: diff' ' ensure_not_expanded diff --cached ' +test_expect_success 'sparse index is not expanded: show' ' + init_repos && + + ensure_not_expanded show :a && + ensure_not_expanded show :deep/a +' + test_expect_success 'sparse index is not expanded: update-index' ' init_repos && From 561287d342cc55205b6cac33415ed96a6f112558 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 26 Apr 2022 20:43:18 +0000 Subject: [PATCH 3/5] object-name: reject trees found in the index The get_oid_with_context_1() method is used when parsing revision arguments. One particular case is to take a ":" string and search the index for the given path. In the case of a sparse index, this might find a sparse directory entry, in which case the contained object is a tree. In the case of a full index, this search within the index would fail. In order to maintain identical return state as in a full index, inspect the discovered cache entry to see if it is a sparse directory and reject it. This requires being careful around the only_to_die option to be sure we die only at the correct time. This changes the behavior of 'git show :', but does not bring it entirely into alignment with a full index case. It specifically hits the wrong error message within diagnose_invalid_index_path(). That error message will be corrected in a future change. Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- object-name.c | 19 ++++++++++++++++++- t/t1092-sparse-checkout-compatibility.sh | 11 ++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/object-name.c b/object-name.c index f0e327f91f..2dc5d2549b 100644 --- a/object-name.c +++ b/object-name.c @@ -1881,6 +1881,20 @@ static char *resolve_relative_path(struct repository *r, const char *rel) rel); } +static int reject_tree_in_index(struct repository *repo, + int only_to_die, + const struct cache_entry *ce, + int stage, + const char *prefix, + const char *cp) +{ + if (!S_ISSPARSEDIR(ce->ce_mode)) + return 0; + if (only_to_die) + diagnose_invalid_index_path(repo, stage, prefix, cp); + return -1; +} + static enum get_oid_result get_oid_with_context_1(struct repository *repo, const char *name, unsigned flags, @@ -1955,9 +1969,12 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo, memcmp(ce->name, cp, namelen)) break; if (ce_stage(ce) == stage) { + free(new_path); + if (reject_tree_in_index(repo, only_to_die, ce, + stage, prefix, cp)) + return -1; oidcpy(oid, &ce->oid); oc->mode = ce->ce_mode; - free(new_path); return 0; } pos++; diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index 3506c0216f..08c9cfd359 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -1165,15 +1165,8 @@ test_expect_success 'show (cached blobs/trees)' ' test_must_fail git -C full-checkout show :folder1/ && test_must_fail git -C sparse-checkout show :folder1/ && - git -C sparse-index show :folder1/ >actual && - git -C full-checkout show HEAD:folder1 >expect && - - # The output of "git show" includes the way we referenced the - # objects, so strip that out. - test_line_count = 4 actual && - tail -n 2 actual >actual-trunc && - tail -n 2 expect >expect-trunc && - test_cmp expect-trunc actual-trunc + test_must_fail git -C sparse-index show :folder1/ 2>err && + grep "is in the index, but not at stage 0" err ' test_expect_success 'submodule handling' ' From 4925adb4dac1f794cc5d5c82dee49e2f5f47560f Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 26 Apr 2022 20:43:19 +0000 Subject: [PATCH 4/5] object-name: diagnose trees in index properly When running 'git show :' where '' is a directory, then there is a subtle difference between a full checkout and a sparse checkout. The error message from diagnose_invalid_index_path() reports whether the path is on disk or not. The full checkout will have the directory on disk, but the path will not be in the index. The sparse checkout could have the directory not exist, specifically when that directory is outside of the sparse-checkout cone. In the case of a sparse index, we have yet another state: the path can be a sparse directory in the index. In this case, the error message from diagnose_invalid_index_path() would erroneously say "path '' is in the index, but not at stage 0", which is false. Add special casing around sparse directory entries so we get to the correct error message. This requires two checks in order to get parity with the normal sparse-checkout case. Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- object-name.c | 6 ++++-- t/t1092-sparse-checkout-compatibility.sh | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/object-name.c b/object-name.c index 2dc5d2549b..4d2746574c 100644 --- a/object-name.c +++ b/object-name.c @@ -1832,7 +1832,8 @@ static void diagnose_invalid_index_path(struct repository *r, pos = -pos - 1; if (pos < istate->cache_nr) { ce = istate->cache[pos]; - if (ce_namelen(ce) == namelen && + if (!S_ISSPARSEDIR(ce->ce_mode) && + ce_namelen(ce) == namelen && !memcmp(ce->name, filename, namelen)) die(_("path '%s' is in the index, but not at stage %d\n" "hint: Did you mean ':%d:%s'?"), @@ -1848,7 +1849,8 @@ static void diagnose_invalid_index_path(struct repository *r, pos = -pos - 1; if (pos < istate->cache_nr) { ce = istate->cache[pos]; - if (ce_namelen(ce) == fullname.len && + if (!S_ISSPARSEDIR(ce->ce_mode) && + ce_namelen(ce) == fullname.len && !memcmp(ce->name, fullname.buf, fullname.len)) die(_("path '%s' is in the index, but not '%s'\n" "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"), diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index 08c9cfd359..fa1d560360 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -1158,15 +1158,21 @@ test_expect_success 'show (cached blobs/trees)' ' test_all_match git show :deep/a && test_sparse_match git show :folder1/a && - # Asking "git show" for directories in the index - # had different behavior depending on the existence - # of a sparse index. + # The error message differs depending on whether + # the directory exists in the worktree. test_all_match test_must_fail git show :deep/ && test_must_fail git -C full-checkout show :folder1/ && - test_must_fail git -C sparse-checkout show :folder1/ && + test_sparse_match test_must_fail git show :folder1/ && - test_must_fail git -C sparse-index show :folder1/ 2>err && - grep "is in the index, but not at stage 0" err + # Change the sparse cone for an extra case: + run_on_sparse git sparse-checkout set deep/deeper1 && + + # deep/deeper2 is a sparse directory in the sparse index. + test_sparse_match test_must_fail git show :deep/deeper2/ && + + # deep/deeper2/deepest is not in the sparse index, but + # will trigger an index expansion. + test_sparse_match test_must_fail git show :deep/deeper2/deepest/ ' test_expect_success 'submodule handling' ' From 124b05b23005437fa5fb91863bde2a8f5840e164 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 26 Apr 2022 20:43:20 +0000 Subject: [PATCH 5/5] rev-parse: integrate with sparse index It is not obvious that the 'git rev-parse' builtin would use the sparse index, but it is possible to parse paths out of the index using the ":" syntax. The 'git rev-parse' output is only the OID of the object found at that location, but otherwise behaves similarly to 'git show :'. This includes the failure conditions on directories and the error messages depending on whether a path is in the worktree or not. The only code change required is to change the command_requires_full_index setting in builtin/rev-parse.c, and we can re-use many existing 'git show' tests for the rev-parse case. Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- builtin/rev-parse.c | 3 ++ t/t1092-sparse-checkout-compatibility.sh | 45 +++++++++++++----------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 8480a59f57..4fc6185b2d 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -723,6 +723,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) prefix = setup_git_directory(); git_config(git_default_config, NULL); did_repo_setup = 1; + + prepare_repo_settings(the_repository); + the_repository->settings.command_requires_full_index = 0; } if (!strcmp(arg, "--")) { diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index fa1d560360..93bcfd20bb 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -1151,29 +1151,32 @@ test_expect_success 'clean' ' test_sparse_match test_path_is_dir folder1 ' -test_expect_success 'show (cached blobs/trees)' ' - init_repos && +for builtin in show rev-parse +do + test_expect_success "$builtin (cached blobs/trees)" " + init_repos && - test_all_match git show :a && - test_all_match git show :deep/a && - test_sparse_match git show :folder1/a && + test_all_match git $builtin :a && + test_all_match git $builtin :deep/a && + test_sparse_match git $builtin :folder1/a && - # The error message differs depending on whether - # the directory exists in the worktree. - test_all_match test_must_fail git show :deep/ && - test_must_fail git -C full-checkout show :folder1/ && - test_sparse_match test_must_fail git show :folder1/ && + # The error message differs depending on whether + # the directory exists in the worktree. + test_all_match test_must_fail git $builtin :deep/ && + test_must_fail git -C full-checkout $builtin :folder1/ && + test_sparse_match test_must_fail git $builtin :folder1/ && - # Change the sparse cone for an extra case: - run_on_sparse git sparse-checkout set deep/deeper1 && + # Change the sparse cone for an extra case: + run_on_sparse git sparse-checkout set deep/deeper1 && - # deep/deeper2 is a sparse directory in the sparse index. - test_sparse_match test_must_fail git show :deep/deeper2/ && + # deep/deeper2 is a sparse directory in the sparse index. + test_sparse_match test_must_fail git $builtin :deep/deeper2/ && - # deep/deeper2/deepest is not in the sparse index, but - # will trigger an index expansion. - test_sparse_match test_must_fail git show :deep/deeper2/deepest/ -' + # deep/deeper2/deepest is not in the sparse index, but + # will trigger an index expansion. + test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/ + " +done test_expect_success 'submodule handling' ' init_repos && @@ -1396,11 +1399,13 @@ test_expect_success 'sparse index is not expanded: diff' ' ensure_not_expanded diff --cached ' -test_expect_success 'sparse index is not expanded: show' ' +test_expect_success 'sparse index is not expanded: show and rev-parse' ' init_repos && ensure_not_expanded show :a && - ensure_not_expanded show :deep/a + ensure_not_expanded show :deep/a && + ensure_not_expanded rev-parse :a && + ensure_not_expanded rev-parse :deep/a ' test_expect_success 'sparse index is not expanded: update-index' '