Skip to content

Commit 225a9aa

Browse files
benpeartdscho
authored andcommitted
Add virtual file system settings and hook proc
On index load, clear/set the skip worktree bits based on the virtual file system data. Use virtual file system data to update skip-worktree bit in unpack-trees. Use virtual file system data to exclude files and folders not explicitly requested. Update 2022-04-05: disable the "present-despite-SKIP_WORKTREE" file removal behavior when 'core.virtualfilesystem' is enabled. Signed-off-by: Ben Peart <benpeart@microsoft.com>
1 parent 4bde6ad commit 225a9aa

18 files changed

+826
-8
lines changed

Diff for: Documentation/config/core.txt

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ Version 2 uses an opaque string so that the monitor can return
111111
something that can be used to determine what files have changed
112112
without race conditions.
113113

114+
core.virtualFilesystem::
115+
If set, the value of this variable is used as a command which
116+
will identify all files and directories that are present in
117+
the working directory. Git will only track and update files
118+
listed in the virtual file system. Using the virtual file system
119+
will supersede the sparse-checkout settings which will be ignored.
120+
See the "virtual file system" section of linkgit:githooks[5].
121+
114122
core.trustctime::
115123
If false, the ctime differences between the index and the
116124
working tree are ignored; useful when the inode change time

Diff for: Documentation/githooks.txt

+20
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,26 @@ and "0" meaning they were not.
758758
Only one parameter should be set to "1" when the hook runs. The hook
759759
running passing "1", "1" should not be possible.
760760

761+
virtualFilesystem
762+
~~~~~~~~~~~~~~~~~~
763+
764+
"Virtual File System" allows populating the working directory sparsely.
765+
The projection data is typically automatically generated by an external
766+
process. Git will limit what files it checks for changes as well as which
767+
directories are checked for untracked files based on the path names given.
768+
Git will also only update those files listed in the projection.
769+
770+
The hook is invoked when the configuration option core.virtualFilesystem
771+
is set. It takes one argument, a version (currently 1).
772+
773+
The hook should output to stdout the list of all files in the working
774+
directory that git should track. The paths are relative to the root
775+
of the working directory and are separated by a single NUL. Full paths
776+
('dir1/a.txt') as well as directories are supported (ie 'dir1/').
777+
778+
The exit status determines whether git will use the data from the
779+
hook. On error, git will abort the command with an error message.
780+
761781
SEE ALSO
762782
--------
763783
linkgit:git-hook[1]

Diff for: Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ LIB_OBJS += utf8.o
11971197
LIB_OBJS += varint.o
11981198
LIB_OBJS += version.o
11991199
LIB_OBJS += versioncmp.o
1200+
LIB_OBJS += virtualfilesystem.o
12001201
LIB_OBJS += walker.o
12011202
LIB_OBJS += wildmatch.o
12021203
LIB_OBJS += worktree.o

Diff for: config.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,11 @@ int git_default_core_config(const char *var, const char *value,
16281628
}
16291629

16301630
if (!strcmp(var, "core.sparsecheckout")) {
1631-
core_apply_sparse_checkout = git_config_bool(var, value);
1631+
/* virtual file system relies on the sparse checkout logic so force it on */
1632+
if (core_virtualfilesystem)
1633+
core_apply_sparse_checkout = 1;
1634+
else
1635+
core_apply_sparse_checkout = git_config_bool(var, value);
16321636
return 0;
16331637
}
16341638

@@ -2719,6 +2723,30 @@ int repo_config_get_max_percent_split_change(struct repository *r)
27192723
return -1; /* default value */
27202724
}
27212725

2726+
int repo_config_get_virtualfilesystem(struct repository *r)
2727+
{
2728+
/* Run only once. */
2729+
static int virtual_filesystem_result = -1;
2730+
if (virtual_filesystem_result >= 0)
2731+
return virtual_filesystem_result;
2732+
2733+
if (repo_config_get_pathname(r, "core.virtualfilesystem", &core_virtualfilesystem))
2734+
core_virtualfilesystem = xstrdup_or_null(getenv("GIT_VIRTUALFILESYSTEM_TEST"));
2735+
2736+
if (core_virtualfilesystem && !*core_virtualfilesystem)
2737+
FREE_AND_NULL(core_virtualfilesystem);
2738+
2739+
/* virtual file system relies on the sparse checkout logic so force it on */
2740+
if (core_virtualfilesystem) {
2741+
core_apply_sparse_checkout = 1;
2742+
virtual_filesystem_result = 1;
2743+
return 1;
2744+
}
2745+
2746+
virtual_filesystem_result = 0;
2747+
return 0;
2748+
}
2749+
27222750
int repo_config_get_index_threads(struct repository *r, int *dest)
27232751
{
27242752
int is_bool, val;

Diff for: config.h

+2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ int repo_config_get_index_threads(struct repository *r, int *dest);
684684
int repo_config_get_split_index(struct repository *r);
685685
int repo_config_get_max_percent_split_change(struct repository *r);
686686

687+
int repo_config_get_virtualfilesystem(struct repository *r);
688+
687689
/* This dies if the configured or default date is in the future */
688690
int repo_config_get_expiry(struct repository *r, const char *key, char **output);
689691

Diff for: dir.c

+34-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "git-compat-util.h"
1313
#include "abspath.h"
14+
#include "virtualfilesystem.h"
1415
#include "config.h"
1516
#include "convert.h"
1617
#include "dir.h"
@@ -1480,6 +1481,19 @@ enum pattern_match_result path_matches_pattern_list(
14801481
int result = NOT_MATCHED;
14811482
size_t slash_pos;
14821483

1484+
if (core_virtualfilesystem) {
1485+
/*
1486+
* The virtual file system data is used to prevent git from traversing
1487+
* any part of the tree that is not in the virtual file system. Return
1488+
* 1 to exclude the entry if it is not found in the virtual file system,
1489+
* else fall through to the regular excludes logic as it may further exclude.
1490+
*/
1491+
if (*dtype == DT_UNKNOWN)
1492+
*dtype = resolve_dtype(DT_UNKNOWN, istate, pathname, pathlen);
1493+
if (is_excluded_from_virtualfilesystem(pathname, pathlen, *dtype) > 0)
1494+
return 1;
1495+
}
1496+
14831497
if (!pl->use_cone_patterns) {
14841498
pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
14851499
dtype, pl, istate);
@@ -1824,8 +1838,22 @@ struct path_pattern *last_matching_pattern(struct dir_struct *dir,
18241838
int is_excluded(struct dir_struct *dir, struct index_state *istate,
18251839
const char *pathname, int *dtype_p)
18261840
{
1827-
struct path_pattern *pattern =
1828-
last_matching_pattern(dir, istate, pathname, dtype_p);
1841+
struct path_pattern *pattern;
1842+
1843+
if (core_virtualfilesystem) {
1844+
/*
1845+
* The virtual file system data is used to prevent git from traversing
1846+
* any part of the tree that is not in the virtual file system. Return
1847+
* 1 to exclude the entry if it is not found in the virtual file system,
1848+
* else fall through to the regular excludes logic as it may further exclude.
1849+
*/
1850+
if (*dtype_p == DT_UNKNOWN)
1851+
*dtype_p = resolve_dtype(DT_UNKNOWN, istate, pathname, strlen(pathname));
1852+
if (is_excluded_from_virtualfilesystem(pathname, strlen(pathname), *dtype_p) > 0)
1853+
return 1;
1854+
}
1855+
1856+
pattern = last_matching_pattern(dir, istate, pathname, dtype_p);
18291857
if (pattern)
18301858
return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
18311859
return 0;
@@ -2443,6 +2471,8 @@ static enum path_treatment treat_path(struct dir_struct *dir,
24432471
ignore_case);
24442472
if (dtype != DT_DIR && has_path_in_index)
24452473
return path_none;
2474+
if (is_excluded_from_virtualfilesystem(path->buf, path->len, dtype) > 0)
2475+
return path_excluded;
24462476

24472477
/*
24482478
* When we are looking at a directory P in the working tree,
@@ -2647,6 +2677,8 @@ static void add_path_to_appropriate_result_list(struct dir_struct *dir,
26472677
/* add the path to the appropriate result list */
26482678
switch (state) {
26492679
case path_excluded:
2680+
if (is_excluded_from_virtualfilesystem(path->buf, path->len, DT_DIR) > 0)
2681+
break;
26502682
if (dir->flags & DIR_SHOW_IGNORED)
26512683
dir_add_name(dir, istate, path->buf, path->len);
26522684
else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||

Diff for: environment.c

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ int core_apply_sparse_checkout;
6969
int core_sparse_checkout_cone;
7070
int sparse_expect_files_outside_of_patterns;
7171
int core_gvfs;
72+
char *core_virtualfilesystem;
7273
int merge_log_config = -1;
7374
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
7475
unsigned long pack_size_limit_cfg;

Diff for: environment.h

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ extern unsigned long pack_size_limit_cfg;
170170
extern int max_allowed_tree_depth;
171171

172172
extern int core_preload_index;
173+
extern char *core_virtualfilesystem;
173174
extern int core_gvfs;
174175
extern int precomposed_unicode;
175176
extern int protect_hfs;

Diff for: meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ libgit_sources = [
465465
'utf8.c',
466466
'varint.c',
467467
'versioncmp.c',
468+
'virtualfilesystem.c',
468469
'walker.c',
469470
'wildmatch.c',
470471
'worktree.c',

Diff for: read-cache.c

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "git-compat-util.h"
1111
#include "bulk-checkin.h"
12+
#include "virtualfilesystem.h"
1213
#include "config.h"
1314
#include "date.h"
1415
#include "diff.h"
@@ -1980,6 +1981,7 @@ static void post_read_index_from(struct index_state *istate)
19801981
tweak_untracked_cache(istate);
19811982
tweak_split_index(istate);
19821983
tweak_fsmonitor(istate);
1984+
apply_virtualfilesystem(istate);
19831985
}
19841986

19851987
static size_t estimate_cache_size_from_compressed(unsigned int entries)

Diff for: sparse-index.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static int add_path_to_index(const struct object_id *oid,
270270
size_t len = base->len;
271271

272272
if (S_ISDIR(mode)) {
273-
int dtype;
273+
int dtype = DT_DIR;
274274
size_t baselen = base->len;
275275
if (!ctx->pl)
276276
return READ_TREE_RECURSIVE;
@@ -394,7 +394,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
394394
struct cache_entry *ce = istate->cache[i];
395395
struct tree *tree;
396396
struct pathspec ps;
397-
int dtype;
397+
int dtype = DT_UNKNOWN;
398398

399399
if (!S_ISSPARSEDIR(ce->ce_mode)) {
400400
set_index_entry(full, full->cache_nr++, ce);
@@ -670,6 +670,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
670670
void clear_skip_worktree_from_present_files(struct index_state *istate)
671671
{
672672
if (!core_apply_sparse_checkout ||
673+
core_virtualfilesystem ||
673674
sparse_expect_files_outside_of_patterns)
674675
return;
675676

Diff for: t/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ integration_tests = [
192192
't1090-sparse-checkout-scope.sh',
193193
't1091-sparse-checkout-builtin.sh',
194194
't1092-sparse-checkout-compatibility.sh',
195+
't1093-virtualfilesystem.sh',
195196
't1100-commit-tree-options.sh',
196197
't1300-config.sh',
197198
't1301-shared-repo.sh',

Diff for: t/t1090-sparse-checkout-scope.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ test_expect_success 'in partial clone, sparse checkout only fetches needed blobs
107107
'
108108

109109
test_expect_success 'checkout does not delete items outside the sparse checkout file' '
110-
# The "sparse.expectfilesoutsideofpatterns" config will prevent the
110+
# The "core.virtualfilesystem" config will prevent the
111111
# SKIP_WORKTREE flag from being dropped on files present on-disk.
112-
test_config sparse.expectfilesoutsideofpatterns true &&
112+
test_config core.virtualfilesystem true &&
113113
114114
test_config core.gvfs 8 &&
115115
git checkout -b outside &&

0 commit comments

Comments
 (0)