Skip to content

Commit 03648ec

Browse files
vdyederrickstolee
authored andcommitted
Merge pull request #423 from vdye/sparse-index/update-index
Sparse index: integrate with `git update-index`
2 parents a18dbe2 + 704ed45 commit 03648ec

File tree

4 files changed

+195
-6
lines changed

4 files changed

+195
-6
lines changed

builtin/update-index.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
410410
if (!verify_path(path, mode))
411411
return error("Invalid path '%s'", path);
412412

413+
if (S_ISSPARSEDIR(mode))
414+
return error("%s: cannot add directory as cache entry", path);
415+
413416
len = strlen(path);
414417
ce = make_empty_cache_entry(&the_index, len);
415418

@@ -744,17 +747,23 @@ static int do_reupdate(int ac, const char **av,
744747
* commit. Update everything in the index.
745748
*/
746749
has_head = 0;
750+
747751
redo:
748-
/* TODO: audit for interaction with sparse-index. */
749-
ensure_full_index(&the_index);
750752
for (pos = 0; pos < active_nr; pos++) {
751753
const struct cache_entry *ce = active_cache[pos];
752754
struct cache_entry *old = NULL;
753755
int save_nr;
754756
char *path;
755757

756-
if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
758+
/*
759+
* We can safely skip re-updating sparse directories because if there
760+
* were any changes to re-update inside of the sparse directory, it
761+
* would not be sparse.
762+
*/
763+
if (S_ISSPARSEDIR(ce->ce_mode) || ce_stage(ce) ||
764+
!ce_path_match(&the_index, ce, &pathspec, NULL))
757765
continue;
766+
758767
if (has_head)
759768
old = read_one_ent(NULL, &head_oid,
760769
ce->name, ce_namelen(ce), 0);
@@ -1079,6 +1088,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10791088

10801089
git_config(git_default_config, NULL);
10811090

1091+
prepare_repo_settings(r);
1092+
the_repository->settings.command_requires_full_index = 0;
1093+
10821094
/* we will diagnose later if it turns out that we need to update it */
10831095
newfd = hold_locked_index(&lock_file, 0);
10841096
if (newfd < 0)

read-cache.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,6 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
13521352
int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
13531353
int new_only = option & ADD_CACHE_NEW_ONLY;
13541354

1355-
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1356-
cache_tree_invalidate_path(istate, ce->name);
1357-
13581355
/*
13591356
* If this entry's path sorts after the last entry in the index,
13601357
* we can avoid searching for it.
@@ -1365,6 +1362,13 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
13651362
else
13661363
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
13671364

1365+
/*
1366+
* Cache tree path should be invalidated only after index_name_stage_pos,
1367+
* in case it expands a sparse index.
1368+
*/
1369+
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1370+
cache_tree_invalidate_path(istate, ce->name);
1371+
13681372
/* existing match? Just replace it. */
13691373
if (pos >= 0) {
13701374
if (!new_only)

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,156 @@ test_expect_success 'reset with wildcard pathspec' '
655655
test_all_match git status --porcelain=v2
656656
'
657657

658+
# NEEDSWORK: although update-index executes without error on files outside
659+
# the sparse checkout definition, it does not actually add the file to the
660+
# index. This is also true when "--no-ignore-skip-worktree-entries" is
661+
# specified.
662+
test_expect_success 'update-index add outside sparse definition' '
663+
init_repos &&
664+
665+
write_script edit-contents <<-\EOF &&
666+
echo text >>$1
667+
EOF
668+
669+
run_on_sparse mkdir -p folder1 &&
670+
run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
671+
672+
# Edit the file only in sparse checkouts so that, when checking the status
673+
# of the index, the unmodified full-checkout is compared to the "modified"
674+
# sparse checkouts.
675+
run_on_sparse ../edit-contents folder1/a &&
676+
677+
test_sparse_match git update-index --add folder1/a &&
678+
test_all_match git status --porcelain=v2 &&
679+
test_sparse_match git update-index --add --no-ignore-skip-worktree-entries folder1/a &&
680+
test_all_match git status --porcelain=v2
681+
'
682+
683+
test_expect_success 'update-index remove outside sparse definition' '
684+
init_repos &&
685+
686+
# When --remove is specified, files outside the sparse checkout definition
687+
# are considered "removed".
688+
rm -f full-checkout/folder1/a &&
689+
test_all_match git update-index --remove folder1/a &&
690+
test_all_match git status --porcelain=v2 &&
691+
692+
git reset --hard &&
693+
694+
# When --ignore-skip-worktree-entries is explicitly specified, a file
695+
# outside the sparse definition is not added to the index as "removed"
696+
# (thus matching the unmodified full-checkout).
697+
test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
698+
test_all_match git status --porcelain=v2 &&
699+
700+
git reset --hard &&
701+
702+
# --force-remove supercedes --ignore-skip-worktree-entries and always
703+
# removes the file from the index.
704+
test_all_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
705+
test_all_match git status --porcelain=v2
706+
'
707+
708+
test_expect_success 'update-index folder add/remove' '
709+
init_repos &&
710+
711+
test_all_match test_must_fail git update-index --add --remove deep &&
712+
test_all_match test_must_fail git update-index --add --remove deep/ &&
713+
714+
# NEEDSWORK: attempting to update-index on an existing folder outside the
715+
# sparse checkout definition does not throw an error (as it does for folders
716+
# inside the definition, or in the full checkout). However, it is a no-op.
717+
test_sparse_match git update-index --add --remove folder1 &&
718+
test_sparse_match git update-index --add --remove folder1/ &&
719+
test_sparse_match git update-index --force-remove folder1/ &&
720+
test_all_match git status --porcelain=v2 &&
721+
722+
# New folders, even in sparse checkouts, throw an error on update-index
723+
run_on_all mkdir folder3 &&
724+
run_on_all cp a folder3/a &&
725+
run_on_all test_must_fail git update-index --add --remove folder3
726+
'
727+
728+
test_expect_success 'update-index with updated flags' '
729+
init_repos &&
730+
731+
# NEEDSWORK: updating flags runs inconsistently on directories, performing no
732+
# operation with warning text specifying the path being ignored if a trailing
733+
# slash is in the path, but throwing an error if there is no trailing slash.
734+
test_all_match test_must_fail git update-index --no-skip-worktree folder1 &&
735+
test_all_match git update-index --no-skip-worktree folder1/ &&
736+
test_all_match git status --porcelain=v2 &&
737+
738+
# Removing the skip-worktree bit from a file outside the sparse checkout
739+
# will cause the file to appear as unstaged and deleted.
740+
test_sparse_match git update-index --no-skip-worktree folder1/a &&
741+
rm -f full-checkout/folder1/a &&
742+
test_all_match git status --porcelain=v2
743+
'
744+
745+
test_expect_success 'update-index --again file outside sparse definition' '
746+
init_repos &&
747+
748+
write_script edit-contents <<-\EOF &&
749+
echo text >>$1
750+
EOF
751+
752+
# When a file is manually added and modified outside the checkout
753+
# definition, it will not be changed with `--again` because its changes are
754+
# not tracked in the index.
755+
run_on_sparse mkdir -p folder1 &&
756+
run_on_sparse ../edit-contents folder1/a &&
757+
test_sparse_match git update-index --again &&
758+
test_sparse_match git status --porcelain=v2 &&
759+
760+
# Update the sparse checkouts so that folder1/a is no longer skipped and
761+
# exists on-disk
762+
run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
763+
test_sparse_match git update-index --no-skip-worktree folder1/a &&
764+
test_all_match git status --porcelain=v2 &&
765+
766+
# Stage change for commit
767+
run_on_all ../edit-contents folder1/a &&
768+
test_all_match git update-index folder1/a &&
769+
test_all_match git status --porcelain=v2 &&
770+
771+
# Modify the file
772+
run_on_all ../edit-contents folder1/a &&
773+
test_all_match git status --porcelain=v2 &&
774+
775+
# Run update-index --again, which re-stages the local changes
776+
test_all_match git update-index --again &&
777+
test_all_match git ls-files -s folder1/a &&
778+
test_all_match git status --porcelain=v2 &&
779+
780+
# Running update-index --again with staged changes after manually deleting
781+
# the file on disk will cause it to fail if --remove is not also specified
782+
run_on_all rm -f folder1/a &&
783+
test_all_match test_must_fail git update-index --again folder1 &&
784+
test_all_match git update-index --remove --again &&
785+
test_all_match git status --porcelain=v2
786+
'
787+
788+
test_expect_success 'update-index --cacheinfo' '
789+
init_repos &&
790+
791+
deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
792+
folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
793+
folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
794+
795+
test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
796+
test_all_match git status --porcelain=v2 &&
797+
798+
# Cannot add sparse directory, even in sparse index case
799+
test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
800+
801+
# Sparse match only - because folder1/a is outside the sparse checkout
802+
# definition (and thus not on-disk), it will appear as "deleted" in
803+
# unstaged changes.
804+
test_all_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
805+
test_sparse_match git status --porcelain=v2
806+
'
807+
658808
test_expect_success 'merge, cherry-pick, and rebase' '
659809
init_repos &&
660810
@@ -993,6 +1143,21 @@ test_expect_success 'sparse index is not expanded: sparse-checkout' '
9931143
ensure_not_expanded sparse-checkout set
9941144
'
9951145

1146+
test_expect_success 'sparse index is not expanded: update-index' '
1147+
init_repos &&
1148+
1149+
echo "test" >sparse-index/README.md &&
1150+
echo "test2" >sparse-index/a &&
1151+
rm -f sparse-index/deep/a &&
1152+
1153+
ensure_not_expanded update-index --add README.md &&
1154+
ensure_not_expanded update-index a &&
1155+
ensure_not_expanded update-index --remove deep/a &&
1156+
1157+
rm -f sparse-index/README.md sparse-index/a &&
1158+
ensure_not_expanded update-index --add --remove --again
1159+
'
1160+
9961161
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
9971162
# in this scenario, but it shouldn't.
9981163
test_expect_success 'reset mixed and checkout orphan' '

t/t2107-update-index-basic.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ test_expect_success '--cacheinfo mode,sha1,path (new syntax)' '
6464
test_cmp expect actual
6565
'
6666

67+
test_expect_success '--cacheinfo does not accept directory mode' '
68+
mkdir folder1 &&
69+
echo content >folder1/content &&
70+
git add folder1 &&
71+
folder1_oid=$(git ls-files -s folder1 | git hash-object --stdin) &&
72+
test_must_fail git update-index --add --cacheinfo 040000 $folder1_oid folder1/
73+
'
74+
6775
test_expect_success '.lock files cleaned up' '
6876
mkdir cleanup &&
6977
(

0 commit comments

Comments
 (0)