Skip to content

Commit 746112c

Browse files
vdyedscho
authored andcommitted
Merge pull request #423 from vdye/sparse-index/update-index
Sparse index: integrate with `git update-index`
2 parents 5acd117 + 6c7201c commit 746112c

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
@@ -411,6 +411,9 @@ static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
411411
if (!verify_path(path, mode))
412412
return error("Invalid path '%s'", path);
413413

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

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

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

10811090
git_config(git_default_config, NULL);
10821091

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

read-cache.c

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

1340-
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1341-
cache_tree_invalidate_path(istate, ce->name);
1342-
13431340
/*
13441341
* If this entry's path sorts after the last entry in the index,
13451342
* we can avoid searching for it.
@@ -1350,6 +1347,13 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
13501347
else
13511348
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
13521349

1350+
/*
1351+
* Cache tree path should be invalidated only after index_name_stage_pos,
1352+
* in case it expands a sparse index.
1353+
*/
1354+
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1355+
cache_tree_invalidate_path(istate, ce->name);
1356+
13531357
/* existing match? Just replace it. */
13541358
if (pos >= 0) {
13551359
if (!new_only)

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,156 @@ test_expect_success 'reset with wildcard pathspec' '
625625
test_all_match git status --porcelain=v2
626626
'
627627

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

1084+
test_expect_success 'sparse index is not expanded: update-index' '
1085+
init_repos &&
1086+
1087+
echo "test" >sparse-index/README.md &&
1088+
echo "test2" >sparse-index/a &&
1089+
rm -f sparse-index/deep/a &&
1090+
1091+
ensure_not_expanded update-index --add README.md &&
1092+
ensure_not_expanded update-index a &&
1093+
ensure_not_expanded update-index --remove deep/a &&
1094+
1095+
rm -f sparse-index/README.md sparse-index/a &&
1096+
ensure_not_expanded update-index --add --remove --again
1097+
'
1098+
9341099
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
9351100
# in this scenario, but it shouldn't.
9361101
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)