Skip to content

Commit bbd201c

Browse files
Kevin Willforddscho
authored andcommitted
gvfs: prevent files to be deleted outside the sparse checkout
Prevent the sparse checkout to delete files that were marked with skip-worktree bit and are not in the sparse-checkout file. This is because everything with the skip-worktree bit turned on is being virtualized and will be removed with the change of HEAD. There was only one failing test when running with these changes that was checking to make sure the worktree narrows on checkout which was expected since we would no longer be narrowing the worktree. Update 2022-04-05: temporarily set 'sparse.expectfilesoutsideofpatterns' in test (until we start disabling the "remove present-despite-SKIP_WORKTREE" behavior with 'core.virtualfilesystem' in a later commit). Signed-off-by: Kevin Willford <[email protected]>
1 parent 1d54ae4 commit bbd201c

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Documentation/config/core.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,15 @@ core.gvfs::
755755
Bit value 4
756756
Normally git write-tree ensures that the objects referenced by the
757757
directory exist in the object database. This option disables this check.
758+
GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT::
759+
Bit value 8
760+
When marking entries to remove from the index and the working
761+
directory this option will take into account what the
762+
skip-worktree bit was set to so that if the entry has the
763+
skip-worktree bit set it will not be removed from the working
764+
directory. This will allow virtualized working directories to
765+
detect the change to HEAD and use the new commit tree to show
766+
the files that are in the working directory.
758767
--
759768

760769
core.sparseCheckout::

gvfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct repository;
1313
*/
1414
#define GVFS_SKIP_SHA_ON_INDEX (1 << 0)
1515
#define GVFS_MISSING_OK (1 << 2)
16+
#define GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT (1 << 3)
1617

1718
int gvfs_config_is_set(struct repository *r, int mask);
1819

t/t1090-sparse-checkout-scope.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ test_expect_success 'in partial clone, sparse checkout only fetches needed blobs
106106
test_cmp expect actual
107107
'
108108

109+
test_expect_success 'checkout does not delete items outside the sparse checkout file' '
110+
# The "sparse.expectfilesoutsideofpatterns" config will prevent the
111+
# SKIP_WORKTREE flag from being dropped on files present on-disk.
112+
test_config sparse.expectfilesoutsideofpatterns true &&
113+
114+
test_config core.gvfs 8 &&
115+
git checkout -b outside &&
116+
echo "new file1" >d &&
117+
git add --sparse d &&
118+
git commit -m "branch initial" &&
119+
echo "new file1" >e &&
120+
git add --sparse e &&
121+
git commit -m "skipped worktree" &&
122+
git update-index --skip-worktree e &&
123+
echo "/d" >.git/info/sparse-checkout &&
124+
git checkout HEAD^ &&
125+
test_path_is_file d &&
126+
test_path_is_file e
127+
'
128+
109129
test_expect_success MINGW 'no unnecessary opendir() with fscache' '
110130
git clone . fscache-test &&
111131
(

unpack-trees.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "git-compat-util.h"
55
#include "advice.h"
6+
#include "gvfs.h"
67
#include "strvec.h"
78
#include "repository.h"
89
#include "parse.h"
@@ -2688,6 +2689,27 @@ static int deleted_entry(const struct cache_entry *ce,
26882689

26892690
if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
26902691
return -1;
2692+
2693+
/*
2694+
* When marking entries to remove from the index and the working
2695+
* directory this option will take into account what the
2696+
* skip-worktree bit was set to so that if the entry has the
2697+
* skip-worktree bit set it will not be removed from the working
2698+
* directory. This will allow virtualized working directories to
2699+
* detect the change to HEAD and use the new commit tree to show
2700+
* the files that are in the working directory.
2701+
*
2702+
* old is the cache_entry that will have the skip-worktree bit set
2703+
* which will need to be preserved when the CE_REMOVE entry is added
2704+
*/
2705+
if (gvfs_config_is_set(the_repository, GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT) &&
2706+
old &&
2707+
old->ce_flags & CE_SKIP_WORKTREE) {
2708+
add_entry(o, old, CE_REMOVE, 0);
2709+
invalidate_ce_path(old, o);
2710+
return 1;
2711+
}
2712+
26912713
add_entry(o, ce, CE_REMOVE, 0);
26922714
invalidate_ce_path(ce, o);
26932715
return 1;

0 commit comments

Comments
 (0)