Skip to content

Commit b83f0d2

Browse files
committed
Merge virtualfilesystem hook
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. The hook was first contributed in private, but was extended via the following pull requests: #15 #27 #33 #70 Signed-off-by: Ben Peart <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
2 parents 958cceb + 9995935 commit b83f0d2

18 files changed

+936
-11
lines changed

Documentation/config/core.txt

Lines changed: 8 additions & 0 deletions
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

Documentation/githooks.txt

Lines changed: 20 additions & 0 deletions
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]

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ LIB_OBJS += utf8.o
11911191
LIB_OBJS += varint.o
11921192
LIB_OBJS += version.o
11931193
LIB_OBJS += versioncmp.o
1194+
LIB_OBJS += virtualfilesystem.o
11941195
LIB_OBJS += walker.o
11951196
LIB_OBJS += wildmatch.o
11961197
LIB_OBJS += worktree.o

config.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,11 @@ int git_default_core_config(const char *var, const char *value,
16721672
}
16731673

16741674
if (!strcmp(var, "core.sparsecheckout")) {
1675-
core_apply_sparse_checkout = git_config_bool(var, value);
1675+
/* virtual file system relies on the sparse checkout logic so force it on */
1676+
if (core_virtualfilesystem)
1677+
core_apply_sparse_checkout = 1;
1678+
else
1679+
core_apply_sparse_checkout = git_config_bool(var, value);
16761680
return 0;
16771681
}
16781682

@@ -2837,6 +2841,44 @@ int git_config_get_max_percent_split_change(void)
28372841
return -1; /* default value */
28382842
}
28392843

2844+
int git_config_get_virtualfilesystem(void)
2845+
{
2846+
/* Run only once. */
2847+
static int virtual_filesystem_result = -1;
2848+
if (virtual_filesystem_result >= 0)
2849+
return virtual_filesystem_result;
2850+
2851+
if (git_config_get_pathname("core.virtualfilesystem", &core_virtualfilesystem))
2852+
core_virtualfilesystem = getenv("GIT_VIRTUALFILESYSTEM_TEST");
2853+
2854+
if (core_virtualfilesystem && !*core_virtualfilesystem)
2855+
core_virtualfilesystem = NULL;
2856+
2857+
if (core_virtualfilesystem) {
2858+
/*
2859+
* Some git commands spawn helpers and redirect the index to a different
2860+
* location. These include "difftool -d" and the sequencer
2861+
* (i.e. `git rebase -i`, `git cherry-pick` and `git revert`) and others.
2862+
* In those instances we don't want to update their temporary index with
2863+
* our virtualization data.
2864+
*/
2865+
char *default_index_file = xstrfmt("%s/%s", the_repository->gitdir, "index");
2866+
int should_run_hook = !strcmp(default_index_file, the_repository->index_file);
2867+
2868+
free(default_index_file);
2869+
if (should_run_hook) {
2870+
/* virtual file system relies on the sparse checkout logic so force it on */
2871+
core_apply_sparse_checkout = 1;
2872+
virtual_filesystem_result = 1;
2873+
return 1;
2874+
}
2875+
core_virtualfilesystem = NULL;
2876+
}
2877+
2878+
virtual_filesystem_result = 0;
2879+
return 0;
2880+
}
2881+
28402882
int git_config_get_index_threads(int *dest)
28412883
{
28422884
int is_bool, val;

config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ int git_config_get_pathname(const char *key, char **dest);
701701
int git_config_get_index_threads(int *dest);
702702
int git_config_get_split_index(void);
703703
int git_config_get_max_percent_split_change(void);
704+
int git_config_get_virtualfilesystem(void);
704705

705706
/* This dies if the configured or default date is in the future */
706707
int git_config_get_expiry(const char *key, const char **output);

dir.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "git-compat-util.h"
1212
#include "abspath.h"
13+
#include "virtualfilesystem.h"
1314
#include "config.h"
1415
#include "convert.h"
1516
#include "dir.h"
@@ -1478,6 +1479,19 @@ enum pattern_match_result path_matches_pattern_list(
14781479
int result = NOT_MATCHED;
14791480
size_t slash_pos;
14801481

1482+
if (core_virtualfilesystem) {
1483+
/*
1484+
* The virtual file system data is used to prevent git from traversing
1485+
* any part of the tree that is not in the virtual file system. Return
1486+
* 1 to exclude the entry if it is not found in the virtual file system,
1487+
* else fall through to the regular excludes logic as it may further exclude.
1488+
*/
1489+
if (*dtype == DT_UNKNOWN)
1490+
*dtype = resolve_dtype(DT_UNKNOWN, istate, pathname, pathlen);
1491+
if (is_excluded_from_virtualfilesystem(pathname, pathlen, *dtype) > 0)
1492+
return 1;
1493+
}
1494+
14811495
if (!pl->use_cone_patterns) {
14821496
pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
14831497
dtype, pl, istate);
@@ -1822,8 +1836,22 @@ struct path_pattern *last_matching_pattern(struct dir_struct *dir,
18221836
int is_excluded(struct dir_struct *dir, struct index_state *istate,
18231837
const char *pathname, int *dtype_p)
18241838
{
1825-
struct path_pattern *pattern =
1826-
last_matching_pattern(dir, istate, pathname, dtype_p);
1839+
struct path_pattern *pattern;
1840+
1841+
if (core_virtualfilesystem) {
1842+
/*
1843+
* The virtual file system data is used to prevent git from traversing
1844+
* any part of the tree that is not in the virtual file system. Return
1845+
* 1 to exclude the entry if it is not found in the virtual file system,
1846+
* else fall through to the regular excludes logic as it may further exclude.
1847+
*/
1848+
if (*dtype_p == DT_UNKNOWN)
1849+
*dtype_p = resolve_dtype(DT_UNKNOWN, istate, pathname, strlen(pathname));
1850+
if (is_excluded_from_virtualfilesystem(pathname, strlen(pathname), *dtype_p) > 0)
1851+
return 1;
1852+
}
1853+
1854+
pattern = last_matching_pattern(dir, istate, pathname, dtype_p);
18271855
if (pattern)
18281856
return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
18291857
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) ||

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int core_apply_sparse_checkout;
8181
int core_sparse_checkout_cone;
8282
int sparse_expect_files_outside_of_patterns;
8383
int core_gvfs;
84+
char *core_virtualfilesystem;
8485
int merge_log_config = -1;
8586
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
8687
unsigned long pack_size_limit_cfg;

environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ int get_shared_repository(void);
155155
void reset_shared_repository(void);
156156

157157
extern int core_preload_index;
158+
extern char *core_virtualfilesystem;
158159
extern int core_gvfs;
159160
extern int precomposed_unicode;
160161
extern int protect_hfs;

hook.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
193193
.hook_name = hook_name,
194194
.options = options,
195195
};
196-
const char *const hook_path = find_hook(hook_name);
196+
const char *hook_path = find_hook(hook_name);
197197
int ret = 0;
198198
const struct run_process_parallel_opts opts = {
199199
.tr2_category = "hook",
@@ -209,6 +209,18 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
209209
.data = &cb_data,
210210
};
211211

212+
/*
213+
* Backwards compatibility hack in VFS for Git: when originally
214+
* introduced (and used!), it was called `post-indexchanged`, but this
215+
* name was changed during the review on the Git mailing list.
216+
*
217+
* Therefore, when the `post-index-change` hook is not found, let's
218+
* look for a hook with the old name (which would be found in case of
219+
* already-existing checkouts).
220+
*/
221+
if (!hook_path && !strcmp(hook_name, "post-index-change"))
222+
hook_path = find_hook("post-indexchanged");
223+
212224
if (!options)
213225
BUG("a struct run_hooks_opt must be provided to run_hooks");
214226

read-cache.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "git-compat-util.h"
1010
#include "bulk-checkin.h"
11+
#include "virtualfilesystem.h"
1112
#include "config.h"
1213
#include "date.h"
1314
#include "diff.h"
@@ -1978,6 +1979,7 @@ static void post_read_index_from(struct index_state *istate)
19781979
tweak_untracked_cache(istate);
19791980
tweak_split_index(istate);
19801981
tweak_fsmonitor(istate);
1982+
apply_virtualfilesystem(istate);
19811983
}
19821984

19831985
static size_t estimate_cache_size_from_compressed(unsigned int entries)

sparse-index.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static int add_path_to_index(const struct object_id *oid,
264264
size_t len = base->len;
265265

266266
if (S_ISDIR(mode)) {
267-
int dtype;
267+
int dtype = DT_DIR;
268268
size_t baselen = base->len;
269269
if (!ctx->pl)
270270
return READ_TREE_RECURSIVE;
@@ -388,7 +388,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
388388
struct cache_entry *ce = istate->cache[i];
389389
struct tree *tree;
390390
struct pathspec ps;
391-
int dtype;
391+
int dtype = DT_UNKNOWN;
392392

393393
if (!S_ISSPARSEDIR(ce->ce_mode)) {
394394
set_index_entry(full, full->cache_nr++, ce);
@@ -663,6 +663,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
663663
void clear_skip_worktree_from_present_files(struct index_state *istate)
664664
{
665665
if (!core_apply_sparse_checkout ||
666+
core_virtualfilesystem ||
666667
sparse_expect_files_outside_of_patterns)
667668
return;
668669

t/t1090-sparse-checkout-scope.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ test_expect_success 'in partial clone, sparse checkout only fetches needed blobs
108108
'
109109

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

0 commit comments

Comments
 (0)