Not only can you use git sparse-checkout
as mentioned in Simon East's answer, with rules like
/*
!^node_modules
But, starting with Git 2.41 (Q2 2023), you can validate those rules!
"git sparse-checkout
"(man) command learns a debugging aid for the sparse rule definitions.
See commit 00408ad, commit 24fc2cd (27 Mar 2023) by William Sprent (williams-unity
).
(Merged by Junio C Hamano -- gitster
-- in commit d02343b, 11 Apr 2023)
Signed-off-by: William Sprent
There exists no direct way to interrogate git about which paths are matched by a given set of sparsity rules.
It is possible to get this information from git, but it includes checking out the commit that contains the paths, applying the sparse checkout patterns and then using something like 'git ls-files -t
'(man) to check if the skip worktree bit is set.
This works in some case, but there are cases where it is awkward or infeasible to generate a checkout for this purpose.
Exposing the pattern matching of sparse checkout enables more tooling to be built and avoids a situation where tools that want to reason about sparse checkouts start containing parallel implementation of the rules.
To accommodate this, add a 'check-rules
' subcommand to the 'sparse-checkout
' builtin along the lines of the 'git check-ignore
'(man) and 'git check-attr
'(man) commands.
The new command accepts a list of paths on stdin and outputs just the ones the match the sparse checkout.
To allow for use in a bare repository and to allow for interrogating about other patterns than the current ones, include a '--rules-file
' option which allows the caller to explicitly pass sparse checkout rules in the format accepted by 'sparse-checkout set --stdin
'.
To allow for reuse of the handling of input patterns for the '--rules-file
' flag, modify 'add_patterns_from_input()
' to be able to read from a 'FILE
' instead of just stdin.
To allow for reuse of the logic which decides whether or not rules should be interpreted as cone-mode patterns, split that part out of 'update_modes()
' such that can be called without modifying the config.
An alternative could have been to create a new 'check-sparsity
' command.
However, placing it under 'sparse-checkout
' allows for
- a) more easily re-using the sparse checkout pattern matching and cone/non-code mode handling, and
- b) keeps the documentation for the command next to the experimental warning and the cone-mode discussion.
git sparse-checkout
now includes in its man page:
'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
git sparse-checkout
now includes in its man page:
'check-rules'
Check whether sparsity rules match one or more paths.
By default check-rules
reads a list of paths from stdin and outputs only
the ones that match the current sparsity rules. The input is expected to consist
of one path per line, matching the output of git ls-tree --name-only
including
that pathnames that begin with a double quote (") are interpreted as C-style
quoted strings.
When called with the --rules-file <file>
flag the input files are matched
against the sparse checkout rules found in <file>
instead of the current ones.
The rules in the files are expected to be in the same form as accepted by git sparse-checkout set --stdin
(in particular, they must be newline-delimited).
By default, the rules passed to the --rules-file
option are interpreted as
cone mode directories. To pass non-cone mode patterns with --rules-file
,
combine the option with the --no-cone
option.
When called with the -z
flag, the format of the paths input on stdin as well
as the output paths are \0 terminated and not quoted. Note that this does not
apply to the format of the rules passed with the --rules-file
option.
Examples:
cat >rules <<-\EOF &&
folder1
deep/deeper1/deepest
EOF
git -C bare ls-tree -r --name-only HEAD >all-files &&
git -C bare sparse-checkout check-rules --cone \
--rules-file ../rules >check-rules-file <all-files &&
git -C repo sparse-checkout set --cone --stdin <rules&&
git -C repo ls-files -t >out &&
sed -n "/^S /!s/^. //p" out >ls-files &&
git -C repo sparse-checkout check-rules >check-rules-default <all-files