Skip to content

Commit ad33b6a

Browse files
Kevin Willforddscho
Kevin Willford
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 eca6d35 commit ad33b6a

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
@@ -756,6 +756,15 @@ core.gvfs::
756756
Bit value 4
757757
Normally git write-tree ensures that the objects referenced by the
758758
directory exist in the object database. This option disables this check.
759+
GVFS_NO_DELETE_OUTSIDE_SPARSECHECKOUT::
760+
Bit value 8
761+
When marking entries to remove from the index and the working
762+
directory this option will take into account what the
763+
skip-worktree bit was set to so that if the entry has the
764+
skip-worktree bit set it will not be removed from the working
765+
directory. This will allow virtualized working directories to
766+
detect the change to HEAD and use the new commit tree to show
767+
the files that are in the working directory.
759768
--
760769

761770
core.sparseCheckout::

gvfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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
void gvfs_load_config_value(const char *value);
1819
int gvfs_config_is_set(int 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
@@ -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"
@@ -2686,6 +2687,27 @@ static int deleted_entry(const struct cache_entry *ce,
26862687

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

0 commit comments

Comments
 (0)