Skip to content

Commit c8cc895

Browse files
Kevin Willfordderrickstolee
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 <kewillf@microsoft.com>
1 parent 999cb17 commit c8cc895

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Documentation/config/core.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,15 @@ core.gvfs::
726726
Bit value 4
727727
Normally git write-tree ensures that the objects referenced by the
728728
directory exist in the object database. This option disables this check.
729+
GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT::
730+
Bit value 8
731+
When marking entries to remove from the index and the working
732+
directory this option will take into account what the
733+
skip-worktree bit was set to so that if the entry has the
734+
skip-worktree bit set it will not be removed from the working
735+
directory. This will allow virtualized working directories to
736+
detect the change to HEAD and use the new commit tree to show
737+
the files that are in the working directory.
729738
--
730739

731740
core.sparseCheckout::

gvfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#define GVFS_SKIP_SHA_ON_INDEX (1 << 0)
1717
#define GVFS_MISSING_OK (1 << 2)
18+
#define GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT (1 << 3)
1819

1920
static inline int gvfs_config_is_set(int mask) {
2021
return (core_gvfs & mask) == mask;

t/t1090-sparse-checkout-scope.sh

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

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

unpack-trees.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "gvfs.h"
23
#include "strvec.h"
34
#include "repository.h"
45
#include "config.h"
@@ -2644,6 +2645,27 @@ static int deleted_entry(const struct cache_entry *ce,
26442645

26452646
if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
26462647
return -1;
2648+
2649+
/*
2650+
* When marking entries to remove from the index and the working
2651+
* directory this option will take into account what the
2652+
* skip-worktree bit was set to so that if the entry has the
2653+
* skip-worktree bit set it will not be removed from the working
2654+
* directory. This will allow virtualized working directories to
2655+
* detect the change to HEAD and use the new commit tree to show
2656+
* the files that are in the working directory.
2657+
*
2658+
* old is the cache_entry that will have the skip-worktree bit set
2659+
* which will need to be preserved when the CE_REMOVE entry is added
2660+
*/
2661+
if (gvfs_config_is_set(GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT) &&
2662+
old &&
2663+
old->ce_flags & CE_SKIP_WORKTREE) {
2664+
add_entry(o, old, CE_REMOVE, 0);
2665+
invalidate_ce_path(old, o);
2666+
return 1;
2667+
}
2668+
26472669
add_entry(o, ce, CE_REMOVE, 0);
26482670
invalidate_ce_path(ce, o);
26492671
return 1;

0 commit comments

Comments
 (0)