Skip to content

Commit 5213052

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 msysgit#70 Signed-off-by: Ben Peart <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
2 parents 1adbe3d + 34b89cb commit 5213052

18 files changed

+890
-7
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
@@ -751,6 +751,26 @@ and "0" meaning they were not.
751751
Only one parameter should be set to "1" when the hook runs. The hook
752752
running passing "1", "1" should not be possible.
753753

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

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ LIB_OBJS += utf8.o
11931193
LIB_OBJS += varint.o
11941194
LIB_OBJS += version.o
11951195
LIB_OBJS += versioncmp.o
1196+
LIB_OBJS += virtualfilesystem.o
11961197
LIB_OBJS += walker.o
11971198
LIB_OBJS += wildmatch.o
11981199
LIB_OBJS += worktree.o

config.c

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

17851785
if (!strcmp(var, "core.sparsecheckout")) {
1786-
core_apply_sparse_checkout = git_config_bool(var, value);
1786+
/* virtual file system relies on the sparse checkout logic so force it on */
1787+
if (core_virtualfilesystem)
1788+
core_apply_sparse_checkout = 1;
1789+
else
1790+
core_apply_sparse_checkout = git_config_bool(var, value);
17871791
return 0;
17881792
}
17891793

@@ -2919,6 +2923,44 @@ int git_config_get_max_percent_split_change(void)
29192923
return -1; /* default value */
29202924
}
29212925

2926+
int git_config_get_virtualfilesystem(void)
2927+
{
2928+
/* Run only once. */
2929+
static int virtual_filesystem_result = -1;
2930+
if (virtual_filesystem_result >= 0)
2931+
return virtual_filesystem_result;
2932+
2933+
if (git_config_get_pathname("core.virtualfilesystem", &core_virtualfilesystem))
2934+
core_virtualfilesystem = getenv("GIT_VIRTUALFILESYSTEM_TEST");
2935+
2936+
if (core_virtualfilesystem && !*core_virtualfilesystem)
2937+
core_virtualfilesystem = NULL;
2938+
2939+
if (core_virtualfilesystem) {
2940+
/*
2941+
* Some git commands spawn helpers and redirect the index to a different
2942+
* location. These include "difftool -d" and the sequencer
2943+
* (i.e. `git rebase -i`, `git cherry-pick` and `git revert`) and others.
2944+
* In those instances we don't want to update their temporary index with
2945+
* our virtualization data.
2946+
*/
2947+
char *default_index_file = xstrfmt("%s/%s", the_repository->gitdir, "index");
2948+
int should_run_hook = !strcmp(default_index_file, the_repository->index_file);
2949+
2950+
free(default_index_file);
2951+
if (should_run_hook) {
2952+
/* virtual file system relies on the sparse checkout logic so force it on */
2953+
core_apply_sparse_checkout = 1;
2954+
virtual_filesystem_result = 1;
2955+
return 1;
2956+
}
2957+
core_virtualfilesystem = NULL;
2958+
}
2959+
2960+
virtual_filesystem_result = 0;
2961+
return 0;
2962+
}
2963+
29222964
int git_config_get_index_threads(int *dest)
29232965
{
29242966
int is_bool, val;

config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ int git_config_get_pathname(const char *key, const char **dest);
703703
int git_config_get_index_threads(int *dest);
704704
int git_config_get_split_index(void);
705705
int git_config_get_max_percent_split_change(void);
706+
int git_config_get_virtualfilesystem(void);
706707

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

dir.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include "git-compat-util.h"
99
#include "abspath.h"
10+
#include "virtualfilesystem.h"
1011
#include "config.h"
1112
#include "convert.h"
1213
#include "dir.h"
@@ -1432,6 +1433,17 @@ enum pattern_match_result path_matches_pattern_list(
14321433
int result = NOT_MATCHED;
14331434
size_t slash_pos;
14341435

1436+
/*
1437+
* The virtual file system data is used to prevent git from traversing
1438+
* any part of the tree that is not in the virtual file system. Return
1439+
* 1 to exclude the entry if it is not found in the virtual file system,
1440+
* else fall through to the regular excludes logic as it may further exclude.
1441+
*/
1442+
if (*dtype == DT_UNKNOWN)
1443+
*dtype = resolve_dtype(DT_UNKNOWN, istate, pathname, pathlen);
1444+
if (is_excluded_from_virtualfilesystem(pathname, pathlen, *dtype) > 0)
1445+
return 1;
1446+
14351447
if (!pl->use_cone_patterns) {
14361448
pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
14371449
dtype, pl, istate);
@@ -1776,8 +1788,20 @@ struct path_pattern *last_matching_pattern(struct dir_struct *dir,
17761788
int is_excluded(struct dir_struct *dir, struct index_state *istate,
17771789
const char *pathname, int *dtype_p)
17781790
{
1779-
struct path_pattern *pattern =
1780-
last_matching_pattern(dir, istate, pathname, dtype_p);
1791+
struct path_pattern *pattern;
1792+
1793+
/*
1794+
* The virtual file system data is used to prevent git from traversing
1795+
* any part of the tree that is not in the virtual file system. Return
1796+
* 1 to exclude the entry if it is not found in the virtual file system,
1797+
* else fall through to the regular excludes logic as it may further exclude.
1798+
*/
1799+
if (*dtype_p == DT_UNKNOWN)
1800+
*dtype_p = resolve_dtype(DT_UNKNOWN, istate, pathname, strlen(pathname));
1801+
if (is_excluded_from_virtualfilesystem(pathname, strlen(pathname), *dtype_p) > 0)
1802+
return 1;
1803+
1804+
pattern = last_matching_pattern(dir, istate, pathname, dtype_p);
17811805
if (pattern)
17821806
return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
17831807
return 0;
@@ -2363,6 +2387,8 @@ static enum path_treatment treat_path(struct dir_struct *dir,
23632387
ignore_case);
23642388
if (dtype != DT_DIR && has_path_in_index)
23652389
return path_none;
2390+
if (is_excluded_from_virtualfilesystem(path->buf, path->len, dtype) > 0)
2391+
return path_excluded;
23662392

23672393
/*
23682394
* When we are looking at a directory P in the working tree,
@@ -2567,6 +2593,8 @@ static void add_path_to_appropriate_result_list(struct dir_struct *dir,
25672593
/* add the path to the appropriate result list */
25682594
switch (state) {
25692595
case path_excluded:
2596+
if (is_excluded_from_virtualfilesystem(path->buf, path->len, DT_DIR) > 0)
2597+
break;
25702598
if (dir->flags & DIR_SHOW_IGNORED)
25712599
dir_add_name(dir, istate, path->buf, path->len);
25722600
else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int core_apply_sparse_checkout;
7878
int core_sparse_checkout_cone;
7979
int sparse_expect_files_outside_of_patterns;
8080
int core_gvfs;
81+
const char *core_virtualfilesystem;
8182
int merge_log_config = -1;
8283
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
8384
unsigned long pack_size_limit_cfg;

environment.h

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

148148
extern int core_preload_index;
149+
extern const char *core_virtualfilesystem;
149150
extern int core_gvfs;
150151
extern int precomposed_unicode;
151152
extern int protect_hfs;

hook.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
185185
.hook_name = hook_name,
186186
.options = options,
187187
};
188-
const char *const hook_path = find_hook(hook_name);
188+
const char *hook_path = find_hook(hook_name);
189189
int ret = 0;
190190
const struct run_process_parallel_opts opts = {
191191
.tr2_category = "hook",
@@ -201,6 +201,18 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
201201
.data = &cb_data,
202202
};
203203

204+
/*
205+
* Backwards compatibility hack in VFS for Git: when originally
206+
* introduced (and used!), it was called `post-indexchanged`, but this
207+
* name was changed during the review on the Git mailing list.
208+
*
209+
* Therefore, when the `post-index-change` hook is not found, let's
210+
* look for a hook with the old name (which would be found in case of
211+
* already-existing checkouts).
212+
*/
213+
if (!hook_path && !strcmp(hook_name, "post-index-change"))
214+
hook_path = find_hook("post-indexchanged");
215+
204216
if (!options)
205217
BUG("a struct run_hooks_opt must be provided to run_hooks");
206218

read-cache.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#include "git-compat-util.h"
77
#include "bulk-checkin.h"
8+
#include "virtualfilesystem.h"
89
#include "config.h"
910
#include "date.h"
1011
#include "diff.h"
@@ -1982,6 +1983,7 @@ static void post_read_index_from(struct index_state *istate)
19821983
tweak_untracked_cache(istate);
19831984
tweak_split_index(istate);
19841985
tweak_fsmonitor(istate);
1986+
apply_virtualfilesystem(istate);
19851987
}
19861988

19871989
static size_t estimate_cache_size_from_compressed(unsigned int entries)

0 commit comments

Comments
 (0)