object-name: diagnose trees in index properly

When running 'git show :<path>' where '<path>' 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 '<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 <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 26, 2022 at 20:43 UTC 4925adb4dac1f794cc5d5c82dee49e2f5f47560f
2 files changed +16 -8
object-name.c
+4 -2
@@ -1832,7 +1832,8 @@ static void diagnose_invalid_index_path(struct repository *r,
1832 pos = -pos - 1;
1833 if (pos < istate->cache_nr) {
1834 ce = istate->cache[pos];
1835 - if (ce_namelen(ce) == namelen &&
1835 + if (!S_ISSPARSEDIR(ce->ce_mode) &&
1836 + ce_namelen(ce) == namelen &&
1837 !memcmp(ce->name, filename, namelen))
1838 die(_("path '%s' is in the index, but not at stage %d\n"
1839 "hint: Did you mean ':%d:%s'?"),
@@ -1848,7 +1849,8 @@ static void diagnose_invalid_index_path(struct repository *r,
1849 pos = -pos - 1;
1850 if (pos < istate->cache_nr) {
1851 ce = istate->cache[pos];
1851 - if (ce_namelen(ce) == fullname.len &&
1852 + if (!S_ISSPARSEDIR(ce->ce_mode) &&
1853 + ce_namelen(ce) == fullname.len &&
1854 !memcmp(ce->name, fullname.buf, fullname.len))
1855 die(_("path '%s' is in the index, but not '%s'\n"
1856 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
t/t1092-sparse-checkout-compatibility.sh
+12 -6
@@ -1158,15 +1158,21 @@ test_expect_success 'show (cached blobs/trees)' '
1158 test_all_match git show :deep/a &&
1159 test_sparse_match git show :folder1/a &&
1160
1161 - # Asking "git show" for directories in the index
1162 - # had different behavior depending on the existence
1163 - # of a sparse index.
1161 + # The error message differs depending on whether
1162 + # the directory exists in the worktree.
1163 test_all_match test_must_fail git show :deep/ &&
1164 test_must_fail git -C full-checkout show :folder1/ &&
1166 - test_must_fail git -C sparse-checkout show :folder1/ &&
1165 + test_sparse_match test_must_fail git show :folder1/ &&
1166
1168 - test_must_fail git -C sparse-index show :folder1/ 2>err &&
1169 - grep "is in the index, but not at stage 0" err
1167 + # Change the sparse cone for an extra case:
1168 + run_on_sparse git sparse-checkout set deep/deeper1 &&
1169 +
1170 + # deep/deeper2 is a sparse directory in the sparse index.
1171 + test_sparse_match test_must_fail git show :deep/deeper2/ &&
1172 +
1173 + # deep/deeper2/deepest is not in the sparse index, but
1174 + # will trigger an index expansion.
1175 + test_sparse_match test_must_fail git show :deep/deeper2/deepest/
1176 '
1177
1178 test_expect_success 'submodule handling' '