Skip to content

Commit 1981410

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 <kewillf@microsoft.com>
1 parent bf82f65 commit 1981410

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
@@ -741,6 +741,15 @@ core.gvfs::
741741
Bit value 4
742742
Normally git write-tree ensures that the objects referenced by the
743743
directory exist in the object database. This option disables this check.
744+
GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT::
745+
Bit value 8
746+
When marking entries to remove from the index and the working
747+
directory this option will take into account what the
748+
skip-worktree bit was set to so that if the entry has the
749+
skip-worktree bit set it will not be removed from the working
750+
directory. This will allow virtualized working directories to
751+
detect the change to HEAD and use the new commit tree to show
752+
the files that are in the working directory.
744753
--
745754

746755
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
@@ -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
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "advice.h"
3+
#include "gvfs.h"
34
#include "strvec.h"
45
#include "repository.h"
56
#include "config.h"
@@ -2669,6 +2670,27 @@ static int deleted_entry(const struct cache_entry *ce,
26692670

26702671
if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
26712672
return -1;
2673+
2674+
/*
2675+
* When marking entries to remove from the index and the working
2676+
* directory this option will take into account what the
2677+
* skip-worktree bit was set to so that if the entry has the
2678+
* skip-worktree bit set it will not be removed from the working
2679+
* directory. This will allow virtualized working directories to
2680+
* detect the change to HEAD and use the new commit tree to show
2681+
* the files that are in the working directory.
2682+
*
2683+
* old is the cache_entry that will have the skip-worktree bit set
2684+
* which will need to be preserved when the CE_REMOVE entry is added
2685+
*/
2686+
if (gvfs_config_is_set(GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT) &&
2687+
old &&
2688+
old->ce_flags & CE_SKIP_WORKTREE) {
2689+
add_entry(o, old, CE_REMOVE, 0);
2690+
invalidate_ce_path(old, o);
2691+
return 1;
2692+
}
2693+
26722694
add_entry(o, ce, CE_REMOVE, 0);
26732695
invalidate_ce_path(ce, o);
26742696
return 1;

0 commit comments

Comments
 (0)