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 ":<path>" syntax. The 'git rev-parse' output is only the OID of the object found at that location, but otherwise behaves similarly to 'git show :<path>'. 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 <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 26, 2022 at 20:43 UTC 124b05b23005437fa5fb91863bde2a8f5840e164
2 files changed +28 -20
builtin/rev-parse.c
+3
@@ -723,6 +723,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
723 prefix = setup_git_directory();
724 git_config(git_default_config, NULL);
725 did_repo_setup = 1;
726 +
727 + prepare_repo_settings(the_repository);
728 + the_repository->settings.command_requires_full_index = 0;
729 }
730
731 if (!strcmp(arg, "--")) {
t/t1092-sparse-checkout-compatibility.sh
+25 -20
@@ -1151,29 +1151,32 @@ test_expect_success 'clean' '
1151 test_sparse_match test_path_is_dir folder1
1152 '
1153
1154 -test_expect_success 'show (cached blobs/trees)' '
1155 - init_repos &&
1154 +for builtin in show rev-parse
1155 +do
1156 + test_expect_success "$builtin (cached blobs/trees)" "
1157 + init_repos &&
1158
1157 - test_all_match git show :a &&
1158 - test_all_match git show :deep/a &&
1159 - test_sparse_match git show :folder1/a &&
1159 + test_all_match git $builtin :a &&
1160 + test_all_match git $builtin :deep/a &&
1161 + test_sparse_match git $builtin :folder1/a &&
1162
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/ &&
1165 - test_sparse_match test_must_fail git show :folder1/ &&
1163 + # The error message differs depending on whether
1164 + # the directory exists in the worktree.
1165 + test_all_match test_must_fail git $builtin :deep/ &&
1166 + test_must_fail git -C full-checkout $builtin :folder1/ &&
1167 + test_sparse_match test_must_fail git $builtin :folder1/ &&
1168
1167 - # Change the sparse cone for an extra case:
1168 - run_on_sparse git sparse-checkout set deep/deeper1 &&
1169 + # Change the sparse cone for an extra case:
1170 + run_on_sparse git sparse-checkout set deep/deeper1 &&
1171
1170 - # deep/deeper2 is a sparse directory in the sparse index.
1171 - test_sparse_match test_must_fail git show :deep/deeper2/ &&
1172 + # deep/deeper2 is a sparse directory in the sparse index.
1173 + test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
1174
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 -'
1175 + # deep/deeper2/deepest is not in the sparse index, but
1176 + # will trigger an index expansion.
1177 + test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
1178 + "
1179 +done
1180
1181 test_expect_success 'submodule handling' '
1182 init_repos &&
@@ -1396,11 +1399,13 @@ test_expect_success 'sparse index is not expanded: diff' '
1399 ensure_not_expanded diff --cached
1400 '
1401
1399 -test_expect_success 'sparse index is not expanded: show' '
1402 +test_expect_success 'sparse index is not expanded: show and rev-parse' '
1403 init_repos &&
1404
1405 ensure_not_expanded show :a &&
1403 - ensure_not_expanded show :deep/a
1406 + ensure_not_expanded show :deep/a &&
1407 + ensure_not_expanded rev-parse :a &&
1408 + ensure_not_expanded rev-parse :deep/a
1409 '
1410
1411 test_expect_success 'sparse index is not expanded: update-index' '