Skip to content

Commit 2e96d80

Browse files
committed
checkout-index: integrate with sparse index
Add repository settings to allow usage of the sparse index. In order to prevent unexpected errors when attempting to check out a sparse directory entry, `checkout_file` directly checks whether a found entry is a sparse directory and, if so, exits with an error. The test corresponding to this case now verifies the error message, intentionally differing from the non-sparse index scenarios. Signed-off-by: Victoria Dye <[email protected]>
1 parent cf3e9f4 commit 2e96d80

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

builtin/checkout-index.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static int checkout_file(const char *name, const char *prefix)
6666
int namelen = strlen(name);
6767
int pos = cache_name_pos(name, namelen);
6868
int has_same_name = 0;
69+
int is_file = 0;
6970
int did_checkout = 0;
7071
int errs = 0;
7172

@@ -79,6 +80,9 @@ static int checkout_file(const char *name, const char *prefix)
7980
break;
8081
has_same_name = 1;
8182
pos++;
83+
if (S_ISSPARSEDIR(ce->ce_mode))
84+
break;
85+
is_file = 1;
8286
if (ce_stage(ce) != checkout_stage
8387
&& (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
8488
continue;
@@ -107,6 +111,8 @@ static int checkout_file(const char *name, const char *prefix)
107111
fprintf(stderr, "git checkout-index: %s ", name);
108112
if (!has_same_name)
109113
fprintf(stderr, "is not in the cache");
114+
else if (!is_file)
115+
fprintf(stderr, "is a sparse directory");
110116
else if (checkout_stage)
111117
fprintf(stderr, "does not exist at stage %d",
112118
checkout_stage);
@@ -218,6 +224,9 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
218224
git_config(git_default_config, NULL);
219225
prefix_length = prefix ? strlen(prefix) : 0;
220226

227+
prepare_repo_settings(the_repository);
228+
the_repository->settings.command_requires_full_index = 0;
229+
221230
if (read_cache() < 0) {
222231
die("invalid cache");
223232
}

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,14 @@ test_expect_success 'checkout-index with folders' '
924924
test_all_match test_must_fail git checkout-index -f -- deep/ &&
925925
926926
# Outside checkout definition
927-
test_all_match test_must_fail git checkout-index -f -- folder1/
927+
# Note: although all tests fail (as expected), the messaging differs. For
928+
# non-sparse index checkouts, the error is that the "file" does not appear
929+
# in the index; for sparse checkouts, the error is explicitly that the
930+
# entry is a sparse directory.
931+
run_on_all test_must_fail git checkout-index -f -- folder1/ &&
932+
test_cmp full-checkout-err sparse-checkout-err &&
933+
! test_cmp full-checkout-err sparse-index-err &&
934+
grep "is a sparse directory" sparse-index-err
928935
'
929936

930937
test_expect_success 'checkout-index --all' '
@@ -1037,6 +1044,7 @@ test_expect_success 'sparse-index is not expanded' '
10371044
echo >>sparse-index/untracked.txt &&
10381045
ensure_not_expanded add . &&
10391046
1047+
ensure_not_expanded checkout-index -f a &&
10401048
for ref in update-deep update-folder1 update-folder2 update-deep
10411049
do
10421050
echo >>sparse-index/README.md &&

0 commit comments

Comments
 (0)